MDL-53700 competency: Implementing a URL resolver

This commit is contained in:
Frederic Massart 2016-04-07 18:56:09 +08:00
parent d8370908b5
commit 07fc0ec380
20 changed files with 287 additions and 59 deletions

View File

@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* URL resolver.
*
* @package tool_lp
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp;
defined('MOODLE_INTERNAL') || die();
use moodle_url;
/**
* URL resolver class.
*
* @package tool_lp
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class url_resolver {
/**
* The URL where the frameworks can be found.
*
* @param int $pagecontextid The ID of the context that we are browsing.
* @return moodle_url
*/
public function frameworks($pagecontextid) {
return new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid));
}
/**
* The URL where the plan can be found.
*
* @param int $planid The plan ID.
* @return moodle_url
*/
public function plan($planid) {
return new moodle_url('/admin/tool/lp/plan.php', array('id' => $planid));
}
/**
* The URL where the plans of a user can be found.
*
* @param int $userid The user ID.
* @return moodle_url
*/
public function plans($userid) {
return new moodle_url('/admin/tool/lp/plans.php', array('userid' => $userid));
}
/**
* The URL where the user competency can be found.
*
* @param int $usercompetency The user competency ID
* @return moodle_url
*/
public function user_competency($usercompetencyid) {
return new moodle_url('/admin/tool/lp/user_competency.php', array('id' => $usercompetencyid));
}
/**
* The URL where the user competency can be found in the context of a plan.
*
* @param int $userid The user ID
* @param int $competencyid The competency ID.
* @param int $planid The plan ID.
* @return moodle_url
*/
public function user_competency_in_plan($userid, $competencyid, $planid) {
return new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $userid,
'competencyid' => $competencyid,
'planid' => $planid
));
}
/**
* The URL where the user evidence (of prior learning) can be found.
*
* @param int $usercompetency The user evidence ID
* @return moodle_url
*/
public function user_evidence($userevidenceid) {
return new moodle_url('/admin/tool/lp/user_evidence.php', array('id' => $userevidenceid));
}
}

View File

@ -30,10 +30,10 @@ if (isguestuser()) {
}
\core_competency\api::require_enabled();
$id = optional_param('id', null, PARAM_INT);
$id = required_param('id', PARAM_INT);
$userevidence = \core_competency\api::read_user_evidence($id);
$url = new moodle_url('/admin/tool/lp/user_evidence_list.php', array('id' => $id));
$url = new moodle_url('/admin/tool/lp/user_evidence.php', array('id' => $id));
list($title, $subtitle) = \tool_lp\page_helper::setup_for_user_evidence($userevidence->get_userid(), $url, $userevidence);
$output = $PAGE->get_renderer('tool_lp');

View File

@ -35,6 +35,7 @@ use core_competency\competency;
use core_competency\competency_framework;
use core_competency\external\competency_exporter;
use core_competency\external\competency_framework_exporter;
use core_competency\url;
use tool_lpmigrate\framework_processor;
/**
@ -147,7 +148,7 @@ class migrate_framework_results implements renderable, templatable {
}
$data->pluginbaseurl = (new moodle_url('/admin/tool/lpmigrate'))->out(false);
$data->cbebaseurl = (new moodle_url('/admin/tool/lp'))->out(false);
$data->frameworksurl = url::frameworks($this->pagecontext->id)->out(false);
return $data;
}

View File

@ -155,7 +155,7 @@
<div>
<p>
<a href="{{pluginbaseurl}}/frameworks.php" class="btn btn-primary">{{#str}}migratemore, tool_lpmigrate{{/str}}</a>
<a href="{{cbebaseurl}}/competencyframeworks.php?pagecontextid={{frameworkto.contextid}}" class="btn">{{#str}}continuetoframeworks, tool_lpmigrate{{/str}}</a>
<a href="{{frameworksurl}}" class="btn">{{#str}}continuetoframeworks, tool_lpmigrate{{/str}}</a>
</p>
</div>
{{#js}}

View File

@ -78,7 +78,6 @@ class competencies_to_review_page implements renderable, templatable {
$data = array(
'competencies' => $compstoreview,
'cbebaseurl' => (new moodle_url('/admin/tool/lp'))->out(false),
'pluginbaseurl' => (new moodle_url('/blocks/lp'))->out(false),
);

View File

@ -73,7 +73,6 @@ class plans_to_review_page implements renderable, templatable {
$data = array(
'plans' => $planstoreview,
'cbebaseurl' => (new moodle_url('/admin/tool/lp'))->out(false),
'pluginbaseurl' => (new moodle_url('/blocks/lp'))->out(false),
);

View File

@ -31,6 +31,7 @@ use core_competency\external\plan_exporter;
use core_competency\external\user_competency_exporter;
use core_competency\external\user_summary_exporter;
use core_competency\plan;
use core_competency\url;
use renderable;
use renderer_base;
use templatable;
@ -130,7 +131,7 @@ class summary implements renderable, templatable {
'hasplanstoreview' => $this->planstoreview['count'] > 0,
'hasmoreplanstoreview' => $this->planstoreview['count'] > 3,
'cbebaseurl' => (new \moodle_url('/admin/tool/lp'))->out(false),
'plansurl' => url::plans($this->user->id)->out(false),
'pluginbaseurl' => (new \moodle_url('/blocks/lp'))->out(false),
'userid' => $this->user->id,
);

View File

@ -39,7 +39,7 @@
<tbody>
{{#competencies}}
<tr>
<td><a href="{{cbebaseurl}}/user_competency.php?id={{usercompetency.id}}">{{competency.shortname}}</a></td>
<td><a href="{{usercompetency.url}}">{{competency.shortname}}</a></td>
<td>{{user.fullname}}</td>
<td>{{usercompetency.statusname}}</td>
</tr>

View File

@ -39,7 +39,7 @@
<tbody>
{{#plans}}
<tr>
<td><a href="{{cbebaseurl}}/plan.php?id={{plan.id}}">{{plan.name}}</a></td>
<td><a href="{{plan.url}}">{{plan.name}}</a></td>
<td>{{user.fullname}}</td>
<td>{{plan.statusname}}</td>
</tr>

View File

@ -42,15 +42,15 @@
{{#hasactiveplans}}
<ul>
{{#activeplans}}
<li><a href="{{cbebaseurl}}/plan.php?id={{id}}">{{name}}</a></li>
<li><a href="{{url}}">{{name}}</a></li>
{{/activeplans}}
{{#hasmoreplans}}
<li class="more"><a href="{{cbebaseurl}}/plans.php?userid={{userid}}">{{#str}}viewmore, block_lp{{/str}}</a></li>
<li class="more"><a href="{{plansurl}}">{{#str}}viewmore, block_lp{{/str}}</a></li>
{{/hasmoreplans}}
</ul>
{{/hasactiveplans}}
{{^hasactiveplans}}
<p>No active plans at the moment. <a href="{{cbebaseurl}}/plans.php?userid={{userid}}">{{#str}}viewotherplans, block_lp{{/str}}</a></p>
<p>No active plans at the moment. <a href="{{plansurl}}">{{#str}}viewotherplans, block_lp{{/str}}</a></p>
{{/hasactiveplans}}
</div>
{{/hasplans}}
@ -60,7 +60,7 @@
<ul>
{{#compstoreview}}
<li>
<a href="{{cbebaseurl}}/user_competency.php?id={{usercompetency.id}}">{{competency.shortname}}</a> ({{user.fullname}}) - {{usercompetency.statusname}}
<a href="{{usercompetency.url}}">{{competency.shortname}}</a> ({{user.fullname}}) - {{usercompetency.statusname}}
</li>
{{/compstoreview}}
{{#hasmorecompstoreview}}
@ -75,7 +75,7 @@
<ul>
{{#planstoreview}}
<li>
<a href="{{cbebaseurl}}/plan.php?id={{plan.id}}">{{plan.name}}</a> ({{user.fullname}}) - {{plan.statusname}}
<a href="{{plan.url}}">{{plan.name}}</a> ({{user.fullname}}) - {{plan.statusname}}
</li>
{{/planstoreview}}
{{#hasmoreplanstoreview}}

View File

@ -3822,7 +3822,7 @@ class api {
if (!$relation->get_id()) {
$relation->create();
$link = new moodle_url('/admin/tool/lp/user_evidence.php', array('id' => $userevidence->get_id()));
$link = url::user_evidence($userevidence->get_id());
self::add_evidence(
$userevidence->get_userid(),
$competency,

View File

@ -28,6 +28,7 @@ use core_user;
use renderer_base;
use stdClass;
use moodle_url;
use core_competency\url;
/**
* Class for exporting plan data.
@ -99,7 +100,7 @@ class plan_exporter extends persistent_exporter {
$commentareaexporter = new comment_area_exporter($this->persistent->get_comment_object());
$values->commentarea = $commentareaexporter->export($output);
$values->url = new moodle_url('/admin/tool/lp/plan.php', array('id' => $this->persistent->get_id()));
$values->url = url::plan($this->persistent->get_id())->out(false);
return (array) $values;
}
@ -181,7 +182,7 @@ class plan_exporter extends persistent_exporter {
'optional' => true,
),
'url' => array(
'type' => PARAM_TEXT
'type' => PARAM_URL
)
);
}

View File

@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die();
use core_user;
use renderer_base;
use stdClass;
use core_competency\url;
use core_competency\user_competency;
/**
@ -87,6 +88,8 @@ class user_competency_exporter extends persistent_exporter {
$result->reviewer = $userexporter->export($output);
}
$result->url = url::user_competency($this->persistent->get_id())->out(false);
return (array) $result;
}
@ -132,6 +135,9 @@ class user_competency_exporter extends persistent_exporter {
'statusname' => array(
'type' => PARAM_RAW
),
'url' => array(
'type' => PARAM_URL
),
);
}
}

136
competency/classes/url.php Normal file
View File

@ -0,0 +1,136 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* URL manager.
*
* @package core_competency
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_competency;
defined('MOODLE_INTERNAL') || die();
use moodle_url;
/**
* URL manager class.
*
* This class has to be used to get the URL to a resource, this allows for different
* alternate frontends to be used without resorting to core hacks. Note that you
* do not have to use this when you are navigating between pages of your own plugin.
*
* To set another resolver, set the following config value in config.php:
* $CFG->core_competency_url_resolver = 'your_plugin\\your_url_resolver_class';
*
* Your URL resolver should implement the same methods as the ones listed in
* this class (except for {{@link self::get()}}) but not statically.
*
* /!\ Note, resolvers MUST NEVER assume that the resource, or the resources
* represented by the arguments, still exist.
*
* @package core_competency
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class url {
/** @var url_resolver The URL resolver instance.*/
protected static $resolver;
/**
* Defer to the resolver.
*
* @param string $resource The resource type.
* @param array $args The arguments.
* @return mixed
*/
protected static function get($resource, $args) {
global $CFG;
if (!isset(static::$resolver)) {
$klass = !empty($CFG->core_competency_url_resolver) ? $CFG->core_competency_url_resolver : 'tool_lp\\url_resolver';
static::$resolver = new $klass();
}
if (!method_exists(static::$resolver, $resource)) {
debugging("URL for '$resource' not implemented.", DEBUG_DEVELOPER);
return new moodle_url('/');
}
return call_user_func_array([static::$resolver, $resource], $args);
}
/**
* The URL where the frameworks can be found.
*
* @param int $pagecontextid The ID of the context that we are browsing.
* @return moodle_url
*/
public static function frameworks($pagecontextid) {
return static::get(__FUNCTION__, func_get_args());
}
/**
* The URL where the plan can be found.
*
* @param int $planid The plan ID.
* @return moodle_url
*/
public static function plan($planid) {
return static::get(__FUNCTION__, func_get_args());
}
/**
* The URL where the plans of a user can be found.
*
* @param int $userid The user ID.
* @return moodle_url
*/
public static function plans($userid) {
return static::get(__FUNCTION__, func_get_args());
}
/**
* The URL where the user competency can be found.
*
* @param int $usercompetency The user competency ID
* @return moodle_url
*/
public static function user_competency($usercompetencyid) {
return static::get(__FUNCTION__, func_get_args());
}
/**
* The URL where the user competency can be found in the context of a plan.
*
* @param int $userid The user ID
* @param int $competencyid The competency ID.
* @param int $planid The plan ID.
* @return moodle_url
*/
public static function user_competency_in_plan($userid, $competencyid, $planid) {
return static::get(__FUNCTION__, func_get_args());
}
/**
* The URL where the user evidence (of prior learning) can be found.
*
* @param int $usercompetency The user evidence ID
* @return moodle_url
*/
public static function user_evidence($userevidenceid) {
return static::get(__FUNCTION__, func_get_args());
}
}

View File

@ -26,6 +26,7 @@ defined('MOODLE_INTERNAL') || die();
use core_competency\api;
use core_competency\plan;
use core_competency\url;
use core_competency\user_competency;
use core_competency\user_evidence;
@ -85,17 +86,12 @@ function core_competency_comment_add($comment, $params) {
// Urls.
// TODO MDL-52749 Replace the link to the plan with the user competency page.
// FIXME Use a URL resolver here.
if (empty($plan)) {
$urlname = get_string('userplans', 'core_competency');
$url = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $uc->get_userid()));
$url = url::plans($uc->get_userid());
} else {
$urlname = $competencyname;
$url = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $uc->get_userid(),
'competencyid' => $uc->get_competencyid(),
'planid' => $plan->get_id()
));
$url = url::user_competency_in_plan($uc->get_userid(), $uc->get_competencyid(), $plan->get_id());
}
// Construct the message content.
@ -164,10 +160,7 @@ function core_competency_comment_add($comment, $params) {
$fullname = fullname($user);
$planname = format_string($plan->get_name(), true, array('context' => $plan->get_context()));
$urlname = $planname;
// FIXME Use a URL resolver here.
$url = new moodle_url('/admin/tool/lp/plan.php', array(
'id' => $plan->get_id()
));
$url = url::plan($plan->get_id());
// Construct the message content.
$fullmessagehtml = get_string('usercommentedonaplanhtml', 'core_competency', array(

View File

@ -27,6 +27,7 @@ global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
use core_competency\api;
use core_competency\url;
/**
* Event tests.
@ -1347,7 +1348,7 @@ class core_competency_event_testcase extends advanced_testcase {
$this->assertEquals($description, $event->get_description());
// Test get_url().
$url = new moodle_url('/admin/tool/lp/user_competency.php', ['id' => $evidence->get_usercompetencyid()]);
$url = url::user_competency($evidence->get_usercompetencyid());
$this->assertEquals($url, $event->get_url());
// Test get_objectid_mapping().

View File

@ -25,6 +25,7 @@
defined('MOODLE_INTERNAL') || die();
use core_competency\plan;
use core_competency\url;
use core_competency\user_competency;
global $CFG;
@ -86,11 +87,7 @@ class core_competency_lib_testcase extends advanced_testcase {
$message = array_pop($messages);
$expectedurlname = $c1->get_shortname();
$expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $u1->id,
'competencyid' => $c1->get_id(),
'planid' => $p1->get_id()
));
$expectedurl = url::user_competency_in_plan($u1->id, $c1->get_id(), $p1->get_id());
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u2->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
@ -110,11 +107,7 @@ class core_competency_lib_testcase extends advanced_testcase {
$message = array_pop($messages);
$expectedurlname = $c2->get_shortname();
$expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
'userid' => $u1->id,
'competencyid' => $c2->get_id(),
'planid' => $p2->get_id()
));
$expectedurl = url::user_competency_in_plan($u1->id, $c2->get_id(), $p2->get_id());
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
@ -134,9 +127,7 @@ class core_competency_lib_testcase extends advanced_testcase {
$message = array_pop($messages);
$expectedurlname = get_string('userplans', 'core_competency');
$expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
'userid' => $u1->id,
));
$expectedurl = url::plans($u1->id);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
@ -171,9 +162,7 @@ class core_competency_lib_testcase extends advanced_testcase {
$message = array_pop($messages);
$expectedurlname = get_string('userplans', 'core_competency');
$expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
'userid' => $u1->id,
));
$expectedurl = url::plans($u1->id);
$this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
$this->assertEquals($u1->id, $message->useridto);
$this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
@ -288,9 +277,7 @@ class core_competency_lib_testcase extends advanced_testcase {
$message = array_pop($messages);
$expectedurlname = $p1->get_name();
$expectedurl = new moodle_url('/admin/tool/lp/plan.php', array(
'id' => $p1->get_id()
));
$expectedurl = url::plan($p1->get_id());
$this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
$this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
@ -308,9 +295,7 @@ class core_competency_lib_testcase extends advanced_testcase {
$message = array_pop($messages);
$expectedurlname = $p1->get_name();
$expectedurl = new moodle_url('/admin/tool/lp/plan.php', array(
'id' => $p1->get_id()
));
$expectedurl = url::plan($p1->get_id());
$this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
$this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
$this->assertEquals(FORMAT_HTML, $message->fullmessageformat);

View File

@ -34,7 +34,7 @@ abstract class moodleform_mod extends moodleform {
/**
* Current course.
*
*
* @var mixed
*/
protected $_course;

View File

@ -110,7 +110,6 @@ class external extends external_api {
'courseid' => new external_value(PARAM_INT, 'Course id'),
'user' => user_summary_exporter::get_read_structure(),
'course' => course_summary_exporter::get_read_structure(),
'pluginbaseurl' => new external_value(PARAM_LOCALURL, 'Url to the tool_lp plugin folder on this Moodle site'),
'usercompetencies' => new external_multiple_structure(
new external_single_structure(array(
'usercompetencycourse' => user_competency_course_exporter::get_read_structure(),

View File

@ -33,6 +33,7 @@ use stdClass;
use core_competency\api;
use core_competency\external\user_competency_course_exporter;
use core_competency\external\user_summary_exporter;
use core_competency\url;
use core_competency\user_competency;
use tool_lp\external\competency_summary_exporter;
use tool_lp\external\course_summary_exporter;
@ -83,7 +84,6 @@ class report implements renderable, templatable {
$data->pushratingstouserplans = $coursecompetencysettings->get_pushratingstouserplans();
$data->course = $exporter->export($output);
$data->pluginbaseurl = (new moodle_url('/admin/tool/lp/'))->out(false);
$data->usercompetencies = array();
$scalecache = array();
$frameworkcache = array();
@ -130,11 +130,13 @@ class report implements renderable, templatable {
$exporter = new user_competency_course_exporter($usercompetencycourse, array('scale' => $scale));
$record = $exporter->export($output);
$onerow->usercompetencycourse = $record;
$exporter = new competency_summary_exporter(null, array('competency' => $competency,
'framework' => $framework,
'context' => $framework->get_context(),
'relatedcompetencies' => array(),
'linkedcourses' => array()));
$exporter = new competency_summary_exporter(null, array(
'competency' => $competency,
'framework' => $framework,
'context' => $framework->get_context(),
'relatedcompetencies' => array(),
'linkedcourses' => array()
));
$onerow->competency = $exporter->export($output);
array_push($data->usercompetencies, $onerow);
}