MDL-52064: Improve persistent exporter

- Use to_record instead of custom getters
- Remove custom formatting for taxonomies
- Fix bugs in exporter (PARAM_FORMAT was wrong)
- use $comp->get_context() instead of $comp->get_framework()->get_context()
This commit is contained in:
Damyon Wiese
2016-04-18 10:58:41 +08:00
committed by Frederic Massart
parent 86938d0613
commit a214d35ec2
11 changed files with 74 additions and 52 deletions
File diff suppressed because one or more lines are too long
+17 -1
View File
@@ -683,6 +683,22 @@ define(['jquery',
return false;
};
/**
* Return the string "Selected <taxonomy>".
*
* @function parseTaxonomies
* @param {String} Comma separated list of taxonomies.
* @return {Array} of level => taxonomystr
*/
var parseTaxonomies = function(taxonomiesstr) {
var all = taxonomiesstr.split(',');
all.unshift("");
delete all[0];
// Note we don't need to fill holes, because other functions check for empty anyway.
return all;
};
return {
/**
* Initialise this page (attach event handlers etc).
@@ -696,7 +712,7 @@ define(['jquery',
init: function(model, pagectxid, taxonomies, rulesMods) {
treeModel = model;
pageContextId = pagectxid;
taxonomiesConstants = taxonomies;
taxonomiesConstants = parseTaxonomies(taxonomies);
rulesModules = rulesMods;
$('[data-region="competencyactions"] [data-action="add"]').on('click', addHandler);
+11 -11
View File
@@ -52,7 +52,7 @@ class api {
$competency = new competency(0, $record);
// First we do a permissions check.
require_capability('tool/lp:competencymanage', $competency->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency->get_context());
// OK - all set.
$id = $competency->create();
@@ -71,7 +71,7 @@ class api {
$competency = new competency($id);
// First we do a permissions check.
require_capability('tool/lp:competencymanage', $competency->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency->get_context());
// OK - all set.
return $competency->delete();
@@ -89,7 +89,7 @@ class api {
$current = new competency($id);
// First we do a permissions check.
require_capability('tool/lp:competencymanage', $current->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $current->get_context());
$max = self::count_competencies(array('parentid' => $current->get_parentid(),
'competencyframeworkid' => $current->get_competencyframeworkid()));
@@ -130,7 +130,7 @@ class api {
$current = new competency($id);
// First we do a permissions check.
require_capability('tool/lp:competencymanage', $current->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $current->get_context());
$sortorder = $current->get_sortorder();
if ($sortorder == 0) {
@@ -166,7 +166,7 @@ class api {
$current = new competency($id);
// First we do a permissions check.
require_capability('tool/lp:competencymanage', $current->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $current->get_context());
// This will throw an exception if the parent does not exist.
@@ -212,7 +212,7 @@ class api {
$competency = new competency($record->id);
// First we do a permissions check.
require_capability('tool/lp:competencymanage', $competency->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency->get_context());
// Some things should not be changed in an update - they should use a more specific method.
$record->sortorder = $competency->get_sortorder();
@@ -220,7 +220,7 @@ class api {
$record->competencyframeworkid = $competency->get_competencyframeworkid();
$competency->from_record($record);
require_capability('tool/lp:competencymanage', $competency->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency->get_context());
// OK - all set.
return $competency->update();
@@ -239,7 +239,7 @@ class api {
$competency = new competency($id);
// First we do a permissions check.
$context = $competency->get_framework()->get_context();
$context = $competency->get_context();
if (!has_any_capability(array('tool/lp:competencyread', 'tool/lp:competencymanage'), $context)) {
throw new required_capability_exception($context, 'tool/lp:competencyread', 'nopermissions', '');
}
@@ -1538,7 +1538,7 @@ class api {
public static function list_related_competencies($competencyid) {
$competency = new competency($competencyid);
require_capability('tool/lp:competencymanage', $competency->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency->get_context());
return $competency->get_related_competencies();
}
@@ -1554,7 +1554,7 @@ class api {
$competency1 = new competency($competencyid);
$competency2 = new competency($relatedcompetencyid);
require_capability('tool/lp:competencymanage', $competency1->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency1->get_context());
$relatedcompetency = related_competency::get_relation($competency1->get_id(), $competency2->get_id());
if (!$relatedcompetency->get_id()) {
@@ -1577,7 +1577,7 @@ class api {
// This only check if we have the permission in either competency because both competencies
// should belong to the same framework.
require_capability('tool/lp:competencymanage', $competency->get_framework()->get_context());
require_capability('tool/lp:competencymanage', $competency->get_context());
$relatedcompetency = related_competency::get_relation($competencyid, $relatedcompetencyid);
if ($relatedcompetency->get_id()) {
+16 -16
View File
@@ -1177,7 +1177,7 @@ class external extends external_api {
$competency = api::read_competency($params['id']);
$context = $competency->get_framework()->get_context();
$context = $competency->get_context();
self::validate_context($context);
$output = $PAGE->get_renderer('tool_lp');
$exporter = new competency_exporter($competency, array('context' => $context));
@@ -1225,7 +1225,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
$context = $competency->get_framework()->get_context();
$context = $competency->get_context();
self::validate_context($context);
return api::delete_competency($params['id']);
@@ -1343,7 +1343,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
$params = (object) $params;
@@ -1690,7 +1690,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['competencyid']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::set_parent_competency($params['competencyid'], $params['parentid']);
}
@@ -1734,7 +1734,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::move_up_competency($params['id']);
}
@@ -1778,7 +1778,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::move_down_competency($params['id']);
}
@@ -1822,7 +1822,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::count_courses_using_competency($params['id']);
}
@@ -1866,7 +1866,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::list_courses_using_competency($params['id']);
}
@@ -1991,7 +1991,7 @@ class external extends external_api {
$contextcache = array();
foreach ($competencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_framework()->get_context();
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$exporter = new competency_exporter($competency, array('context' => $context));
@@ -2915,7 +2915,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::count_templates_using_competency($params['id']);
}
@@ -2961,7 +2961,7 @@ class external extends external_api {
));
$competency = api::read_competency($params['id']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
$output = $PAGE->get_renderer('tool_lp');
$templates = api::list_templates_using_competency($params['id']);
@@ -3068,7 +3068,7 @@ class external extends external_api {
foreach ($competencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_framework()->get_context();
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$exporter = new competency_exporter($competency, array('context' => $context));
@@ -3961,7 +3961,7 @@ class external extends external_api {
if ($plan->get_status() == plan::STATUS_COMPLETE) {
if (!isset($contextcache[$r->competency->get_competencyframeworkid()])) {
$contextcache[$r->competency->get_competencyframeworkid()] = $r->competency->get_framework()->get_context();
$contextcache[$r->competency->get_competencyframeworkid()] = $r->competency->get_context();
}
$context = $contextcache[$r->competency->get_competencyframeworkid()];
@@ -4092,7 +4092,7 @@ class external extends external_api {
'relatedcompetencyid' => $relatedcompetencyid
));
$competency = api::read_competency($params['competencyid']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::add_related_competency($params['competencyid'], $params['relatedcompetencyid']);
}
@@ -4143,7 +4143,7 @@ class external extends external_api {
'relatedcompetencyid' => $relatedcompetencyid
));
$competency = api::read_competency($params['competencyid']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
return api::remove_related_competency($params['competencyid'], $params['relatedcompetencyid']);
}
@@ -4185,7 +4185,7 @@ class external extends external_api {
'competencyid' => $competencyid,
));
$competency = api::read_competency($params['competencyid']);
self::validate_context($competency->get_framework()->get_context());
self::validate_context($competency->get_context());
$renderable = new \tool_lp\output\related_competencies($params['competencyid']);
$renderer = $PAGE->get_renderer('tool_lp');
@@ -36,10 +36,4 @@ class competency_framework_exporter extends persistent_exporter {
protected function get_persistent_class() {
return 'tool_lp\\competency_framework';
}
public function export_for_template(renderer_base $output) {
$result = parent::export_for_template($output);
$result->taxonomies = json_encode($this->persistent->get_taxonomies());
return $result;
}
}
+24 -12
View File
@@ -87,6 +87,23 @@ abstract class persistent_exporter implements templatable {
}
}
/**
* Function to guess the correct context, falling back to system context.
*
* @return context
*/
protected function get_context() {
$context = null;
if (isset($this->related['context']) && $this->related['context'] instanceof context) {
$context = $this->related['context'];
} else if (method_exists($this->persistent, 'get_context')) {
$context = $this->persistent->get_context();
} else {
$context = context_system::instance();
}
return $context;
}
/**
* Function to export the renderer data in a format that is suitable for a
* mustache template. This means raw records are generated as in to_record,
@@ -98,23 +115,18 @@ abstract class persistent_exporter implements templatable {
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$properties = $this->persistent->properties_definition();
if (isset($this->related['context']) && $this->related['context'] instanceof context) {
$context = $this->related['context'];
} else if (method_exists($this->persistent, 'get_context')) {
$context = $this->persistent->get_context();
} else {
$context = context_system::instance();
}
$context = $this->get_context();
$record = $this->persistent->to_record();
foreach ($properties as $property => $definition) {
if (!isset($data->$property)) {
$getmethod = 'get_' . $property;
$data->$property = $this->persistent->$getmethod();
$data->$property = $record->$property;
if ($definition['type'] === PARAM_TEXT) {
$propertyformat = $property . 'format';
if (isset($properties[$propertyformat]) && $properties[$propertyformat]['type'] == PARAM_FORMAT) {
$format = $this->persistent->get($propertyformat);
list($text, $format) = external_format_text($data->$property, $format, $context->id);
if (isset($properties[$propertyformat]) && $properties[$propertyformat]['type'] == PARAM_INT) {
$format = $record->$propertyformat;
list($text, $format) = external_format_text($data->$property, $format, $context->id, 'tool_lp', '', 0);
$data->$property = $text;
$data->$propertyformat = $format;
} else {
@@ -99,7 +99,7 @@ class course_competencies_page implements renderable, templatable {
$contextcache = array();
foreach ($this->competencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_framework()->get_context();
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
@@ -49,7 +49,7 @@ class related_competencies implements renderable, templatable {
*/
public function __construct($competencyid) {
$this->competency = api::read_competency($competencyid);
$this->context = $this->competency->get_framework()->get_context();
$this->context = $this->competency->get_context();
$this->relatedcompetencies = api::list_related_competencies($competencyid);
}
@@ -88,7 +88,7 @@ class template_competencies_page implements renderable, templatable {
$contextcache = array();
foreach ($this->competencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_framework()->get_context();
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
+1 -1
View File
@@ -33,7 +33,7 @@ use lang_string;
* @copyright 2015 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plan extends persistent {
class plan extends persistent {
const TABLE = 'tool_lp_plan';
@@ -118,7 +118,7 @@ require(['tool_lp/tree', 'tool_lp/competencytree', 'tool_lp/competencyactions',
'{{search}}',
'[data-enhance=tree]');
actions.init(treeModel, {{pagecontextid}}, {{{framework.taxonomies}}}, {{{rulesmodules}}});
actions.init(treeModel, {{pagecontextid}}, '{{{framework.taxonomies}}}', {{{rulesmodules}}});
});
{{/js}}