MDL-52119 cbe: Use generated structures for persistent classes

This commit is contained in:
Damyon Wiese
2016-04-18 10:58:42 +08:00
committed by Frederic Massart
parent a6ec05b675
commit 8ec5f810f4
19 changed files with 666 additions and 1587 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1 +1 @@
define(["core/ajax","core/notification","core/templates","tool_lp/tree","jquery"],function(a,b,c,d,e){var f={},g=0,h="",i="",j="",k=function(a,b){var c=0,d=!1;for(a.haschildren=!1,a.children=[],c=0;c<b.length;c++)d=b[c],d.parentid==a.id&&(a.haschildren=!0,a.children.push(d),k(d,b))},l=function(b){var l=e.Deferred();return c.render("tool_lp/loading",{}).done(function(m,n){c.replaceNodeContents(e(i),m,n);var o=a.call([{methodname:"tool_lp_search_competencies",args:{searchtext:b,competencyframeworkid:g,includerelated:!0}}]);o[0].done(function(a){f={};var b=0;for(b=0;b<a.length;b++)f[a[b].id]=a[b];var g=[],m=!1;for(b=0;b<a.length;b++)m=a[b],0===parseInt(m.parentid,10)&&(g.push(m),k(m,a));var n={shortname:h,competencies:g};c.render("tool_lp/competencies_tree_root",n).done(function(a,b){c.replaceNodeContents(e(i),e(a).html(),b);var g=new d(i,!1);if(j){var h=e(i).find("[data-id="+j+"]");h.length&&(g.selectItem(h),g.updateFocus(h))}l.resolve(f)}).fail(l.reject)}).fail(l.reject)}),l.promise()},m=function(a,b){var c=b.selected;j=c.attr("data-id")};return{init:function(a,c,d,e){g=a,h=c,i=e,l(d).fail(b.exception),this.on("selectionchanged",m)},on:function(a,b){e(i).on(a,b)},getChildren:function(a){var b=[];return e.each(f,function(c,d){d.parentid==a&&b.push(d)}),b},getCompetencyFrameworkId:function(){return g},getCompetency:function(a){return f[a]},getCompetencyLevel:function(a){var b=this.getCompetency(a),c=b.path.replace(/^\/|\/$/g,"").split("/").length;return c},hasChildren:function(a){return this.getChildren(a).length>0},reloadCompetencies:function(){return l("").fail(b.exception)},listCompetencies:function(){return f}}});
define(["core/ajax","core/notification","core/templates","tool_lp/tree","jquery"],function(a,b,c,d,e){var f={},g=0,h="",i="",j="",k=function(a,b){var c=0,d=!1;for(a.haschildren=!1,a.children=[],c=0;c<b.length;c++)d=b[c],d.parentid==a.id&&(a.haschildren=!0,a.children.push(d),k(d,b))},l=function(b){var l=e.Deferred();return c.render("tool_lp/loading",{}).done(function(m,n){c.replaceNodeContents(e(i),m,n);var o=a.call([{methodname:"tool_lp_search_competencies",args:{searchtext:b,competencyframeworkid:g}}]);o[0].done(function(a){f={};var b=0;for(b=0;b<a.length;b++)f[a[b].id]=a[b];var g=[],m=!1;for(b=0;b<a.length;b++)m=a[b],0===parseInt(m.parentid,10)&&(g.push(m),k(m,a));var n={shortname:h,competencies:g};c.render("tool_lp/competencies_tree_root",n).done(function(a,b){c.replaceNodeContents(e(i),e(a).html(),b);var g=new d(i,!1);if(j){var h=e(i).find("[data-id="+j+"]");h.length&&(g.selectItem(h),g.updateFocus(h))}l.resolve(f)}).fail(l.reject)}).fail(l.reject)}),l.promise()},m=function(a,b){var c=b.selected;j=c.attr("data-id")};return{init:function(a,c,d,e){g=a,h=c,i=e,l(d).fail(b.exception),this.on("selectionchanged",m)},on:function(a,b){e(i).on(a,b)},getChildren:function(a){var b=[];return e.each(f,function(c,d){d.parentid==a&&b.push(d)}),b},getCompetencyFrameworkId:function(){return g},getCompetency:function(a){return f[a]},getCompetencyLevel:function(a){var b=this.getCompetency(a),c=b.path.replace(/^\/|\/$/g,"").split("/").length;return c},hasChildren:function(a){return this.getChildren(a).length>0},reloadCompetencies:function(){return l("").fail(b.exception)},listCompetencies:function(){return f}}});
+29 -15
View File
@@ -156,8 +156,7 @@ define(['jquery',
methodname: 'tool_lp_search_competencies',
args: {
competencyframeworkid: competency.competencyframeworkid,
searchtext: '',
includerelated: false
searchtext: ''
}
},{
methodname: 'tool_lp_read_competency_framework',
@@ -372,19 +371,20 @@ define(['jquery',
};
var ruleConfigSaveHandler = function(e, config) {
var update = {
id: relatedTarget.id,
shortname: relatedTarget.shortname,
idnumber: relatedTarget.idnumber,
description: relatedTarget.description,
descriptionformat: relatedTarget.descriptionformat,
visible: relatedTarget.visible,
ruletype: config.ruletype,
ruleoutcome: config.ruleoutcome,
ruleconfig: config.ruleconfig
};
var promise = ajax.call([{
methodname: 'tool_lp_update_competency',
args: {
id: relatedTarget.id,
shortname: relatedTarget.shortname,
idnumber: relatedTarget.idnumber,
description: relatedTarget.description,
descriptionformat: relatedTarget.descriptionformat,
visible: relatedTarget.visible,
ruletype: config.ruletype,
ruleoutcome: config.ruleoutcome,
ruleconfig: config.ruleconfig,
}
args: { competency: update }
}]);
promise[0].then(function(result) {
if (result) {
@@ -531,8 +531,6 @@ define(['jquery',
// Listeners to newly loaded related competencies.
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
// We update the full list of competencies.
treeModel.reloadCompetencies();
};
/**
@@ -600,6 +598,22 @@ define(['jquery',
$('[data-region="competencyinfo"]').html(html);
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
});
}).then(function() {
return templates.render('tool_lp/loading', {});
}).then(function(html, js) {
templates.replaceNodeContents('[data-region="relatedcompetencies"]', html, js);
}).done(function() {
ajax.call([{
methodname: 'tool_lp_data_for_related_competencies_section',
args: { competencyid: competency.id },
done: function(context) {
return templates.render('tool_lp/related_competencies', context).done(function(html, js) {
$('[data-region="relatedcompetencies"]').replaceWith(html);
templates.runTemplateJS(js);
updatedRelatedCompetencies();
});
}
}]);
}).fail(notification.exception);
};
+1 -2
View File
@@ -75,8 +75,7 @@ define(['core/ajax', 'core/notification', 'core/templates', 'tool_lp/tree', 'jqu
methodname: 'tool_lp_search_competencies',
args: {
searchtext: searchtext,
competencyframeworkid: competencyFrameworkId,
includerelated: true
competencyframeworkid: competencyFrameworkId
}
}]);
promises[0].done(function(result) {
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -31,12 +31,12 @@ namespace tool_lp\external;
*/
class competency_exporter extends persistent_exporter {
protected function get_related() {
protected static function get_related() {
// We cache the context so it does not need to be retrieved from the framework every time.
return array('context' => '\context');
}
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\competency';
}
}
@@ -33,7 +33,8 @@ use renderer_base;
*/
class competency_framework_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\competency_framework';
}
}
@@ -31,7 +31,7 @@ namespace tool_lp\external;
*/
class course_competency_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\course_competency';
}
}
+82 -5
View File
@@ -30,6 +30,9 @@ use renderer_base;
use context;
use context_system;
use coding_exception;
use external_single_structure;
use external_value;
use external_format_value;
/**
* An extended version of the persistent class with a default implementation of export
@@ -51,7 +54,7 @@ abstract class persistent_exporter {
*
* @return array of 'propertyname' => classname
*/
protected function get_related() {
protected static function get_related() {
return array();
}
@@ -60,7 +63,9 @@ abstract class persistent_exporter {
*
* @return string
*/
abstract protected function get_persistent_class();
protected static function get_persistent_class() {
throw new coding_exception('get_persistent_class() must be overidden.');
}
/**
* Constructor - saves the persistent object, and the related objects.
@@ -69,15 +74,15 @@ abstract class persistent_exporter {
* @param array $related - An optional list of pre-loaded objects related to this persistent.
*/
function __construct(\tool_lp\persistent $persistent, $related = array()) {
$classname = $this->get_persistent_class();
$classname = static::get_persistent_class();
if (!$persistent instanceof $classname) {
throw new coding_exception('Invalid type for persistent. ' .
'Expected: ' . $this->get_persistent_class() . ' got: ' . get_class($persistent));
'Expected: ' . static::get_persistent_class() . ' got: ' . get_class($persistent));
}
$this->persistent = $persistent;
// Cache the valid related objects.
foreach ($this->get_related() as $key => $classname) {
foreach (static::get_related() as $key => $classname) {
if (isset($related[$key]) && ($related[$key] instanceof $classname)) {
$this->related[$key] = $related[$key];
} else {
@@ -136,4 +141,76 @@ abstract class persistent_exporter {
}
return $data;
}
/**
* Modify the list of fields exported for 'read'. This is used when we return additional 'virtual' fields
* that are useful for display, but are not part of the persistent definition.
*
* @param array $fields - The standard list of fields for this persistent.
* @return array The modified list of fields, passed to new external_single_structure.
*/
public static function export_read_properties_structure($fields) {
return $fields;
}
/**
* Function to export the structure of the persistent, so it can be re-used in all
* external function params/returns definitions.
*
* @param string $for - One of 'create', 'update' or 'read' - There are different structures for each.
* @return external_single_structure
*/
public static function export_structure($for) {
$classname = static::get_persistent_class();
$properties = $classname::properties_definition();
$returns = array();
if (!in_array($for, array('create', 'update', 'read'))) {
throw new coding_exception('First parameter for persistent_exporter::export_structure must ' .
'be one of "create", "update" or "read".');
}
foreach ($properties as $property => $definition) {
$required = VALUE_REQUIRED;
if ($for == 'update') {
$required = VALUE_OPTIONAL;
}
$default = null;
$nullallowed = NULL_NOT_ALLOWED;
// We cannot use isset here because we want to detect nulls.
if ($for == 'create' && array_key_exists('default', $definition)) {
$required = VALUE_DEFAULT;
$default = $definition['default'];
}
if (!empty($definition['null'])) {
$nullallowed = NULL_ALLOWED;
}
if ($property == 'id') {
if ($for == 'create') {
continue;
} else {
$required = VALUE_REQUIRED;
}
}
if ($property == 'contextid') {
if ($for == 'create') {
$returns['contextid'] = new external_value(PARAM_INT, 'The context id', VALUE_OPTIONAL);
$returns['contextlevel'] = new external_value(PARAM_ALPHA, 'The context level', VALUE_OPTIONAL);
$returns['instanceid'] = new external_value(PARAM_INT, 'The Instance id', VALUE_OPTIONAL);
} else {
$returns['contextid'] = new external_value(PARAM_INT, 'The context id', VALUE_OPTIONAL);
}
} else {
if ($definition['type'] == PARAM_FORMAT) {
$returns[$property] = new external_format_value($property, $required, $default, $nullallowed);
} else {
$returns[$property] = new external_value($definition['type'], $property, $required, $default, $nullallowed);
}
}
}
if ($for == 'read') {
$returns = static::export_read_properties_structure($returns);
}
return new external_single_structure($returns);
}
}
@@ -31,7 +31,7 @@ namespace tool_lp\external;
*/
class plan_competency_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\plan_competency';
}
}
+9 -1
View File
@@ -24,6 +24,7 @@
namespace tool_lp\external;
use renderer_base;
use external_value;
/**
* Class for exporting plan data.
@@ -33,13 +34,20 @@ use renderer_base;
*/
class plan_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\plan';
}
public function export(renderer_base $output) {
$result = parent::export($output);
$result->statusname = $this->persistent->get_statusname();
$result->usercanupdate = $this->persistent->can_manage();
return $result;
}
public static function export_read_properties_structure($fields) {
$fields['statusname'] = new external_value(PARAM_TEXT, 'statusname, readonly');
$fields['usercanupdate'] = new external_value(PARAM_BOOL, 'usercanupdate, readonly');
return $fields;
}
}
@@ -31,7 +31,7 @@ namespace tool_lp\external;
*/
class related_competency_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\related_competency';
}
}
@@ -31,7 +31,7 @@ namespace tool_lp\external;
*/
class template_competency_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\template_competency';
}
}
+7 -1
View File
@@ -24,6 +24,7 @@
namespace tool_lp\external;
use renderer_base;
use external_value;
/**
* Class for exporting template data.
@@ -33,7 +34,7 @@ use renderer_base;
*/
class template_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\template';
}
@@ -42,4 +43,9 @@ class template_exporter extends persistent_exporter {
$result->duedateformatted = userdate($result->duedate);
return $result;
}
public static function export_read_properties_structure($fields) {
$fields['duedateformatted'] = new external_value(PARAM_TEXT, 'duedateformatted, readonly');
return $fields;
}
}
+10 -2
View File
@@ -25,6 +25,7 @@ namespace tool_lp\external;
use renderer_base;
use context_user;
use external_value;
use tool_lp\user_competency;
/**
@@ -35,11 +36,11 @@ use tool_lp\user_competency;
*/
class user_competency_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\user_competency';
}
protected function get_related() {
protected static function get_related() {
// We cache the scale so it does not need to be retrieved from the framework every time.
return array('scale' => 'grade_scale');
}
@@ -68,4 +69,11 @@ class user_competency_exporter extends persistent_exporter {
$result->statusname = $statusname;
return $result;
}
public static function export_read_properties_structure($fields) {
$fields['gradename'] = new external_value(PARAM_TEXT, 'gradename, readonly');
$fields['proficiencyname'] = new external_value(PARAM_TEXT, 'proficiencyname, readonly');
$fields['statusname'] = new external_value(PARAM_TEXT, 'statusname, readonly');
return $fields;
}
}
@@ -33,7 +33,7 @@ use renderer_base;
*/
class user_competency_plan_exporter extends persistent_exporter {
protected function get_persistent_class() {
protected static function get_persistent_class() {
return 'tool_lp\\user_competency_plan';
}
@@ -89,7 +89,6 @@ class plans_page implements renderable, templatable {
foreach ($this->plans as $plan) {
$exporter = new plan_exporter($plan);
$record = $exporter->export($output);
$record->usercanupdate = $plan->can_manage();
$data->plans[] = $record;
}
}
@@ -16,4 +16,7 @@
{{/visible}}
</ul>
{{/relatedcompetencies}}
{{^relatedcompetencies}}
{{#str}}norelatedcompetencies, tool_lp{{/str}}
{{/relatedcompetencies}}
</div>
File diff suppressed because it is too large Load Diff