MDL-50482 tool_lp: Add ability to duplicate a learning plan template

This commit is contained in:
Issam Taboubi
2016-04-18 10:58:36 +08:00
committed by Frederic Massart
parent 46fdd1d8ef
commit 76c107eac1
8 changed files with 180 additions and 9 deletions
@@ -1 +1 @@
define(["jquery","core/templates","core/ajax","core/notification","core/str"],function(a,b,c,d,e){var f=0,g=0,h=function(c,d){a('[data-region="managetemplates"]').replaceWith(c),b.runTemplateJS(d)},i=function(a){b.render("tool_lp/manage_templates_page",a).done(h).fail(d.exception)},j=function(){var a=c.call([{methodname:"tool_lp_delete_template",args:{id:g}},{methodname:"tool_lp_data_for_templates_manage_page",args:{pagecontext:{contextid:f}}}]);a[1].done(i).fail(d.exception)},k=function(b){b.preventDefault();var f=a(this).attr("data-templateid");g=f;var h=c.call([{methodname:"tool_lp_read_template",args:{id:g}}]);h[0].done(function(a){e.get_strings([{key:"confirm",component:"moodle"},{key:"deletetemplate",component:"tool_lp",param:a.shortname},{key:"delete",component:"moodle"},{key:"cancel",component:"moodle"}]).done(function(a){d.confirm(a[0],a[1],a[2],a[3],j)}).fail(d.exception)}).fail(d.exception)};return{deleteHandler:k,init:function(a){f=a}}});
define(["jquery","core/templates","core/ajax","core/notification","core/str"],function(a,b,c,d,e){var f=0,g=0,h=function(c,d){a('[data-region="managetemplates"]').replaceWith(c),b.runTemplateJS(d)},i=function(a){b.render("tool_lp/manage_templates_page",a).done(h).fail(d.exception)},j=function(){var a=c.call([{methodname:"tool_lp_delete_template",args:{id:g}},{methodname:"tool_lp_data_for_templates_manage_page",args:{pagecontext:{contextid:f}}}]);a[1].done(i).fail(d.exception)},k=function(b){b.preventDefault(),g=a(this).attr("data-templateid");var e=c.call([{methodname:"tool_lp_duplicate_template",args:{id:g}},{methodname:"tool_lp_data_for_templates_manage_page",args:{pagecontext:{contextid:f}}}]);e[1].done(i).fail(d.exception)},l=function(b){b.preventDefault();var f=a(this).attr("data-templateid");g=f;var h=c.call([{methodname:"tool_lp_read_template",args:{id:g}}]);h[0].done(function(a){e.get_strings([{key:"confirm",component:"moodle"},{key:"deletetemplate",component:"tool_lp",param:a.shortname},{key:"delete",component:"moodle"},{key:"cancel",component:"moodle"}]).done(function(a){d.confirm(a[0],a[1],a[2],a[3],j)}).fail(d.exception)}).fail(d.exception)};return{deleteHandler:l,duplicateHandler:k,init:function(a){f=a}}});
@@ -14,9 +14,9 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Delete learning plan templates via ajax.
* Handle actions on learning plan templates via ajax.
*
* @module tool_lp/templatedelete
* @module tool_lp/templateactions
* @package tool_lp
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -75,6 +75,31 @@ define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str'
requests[1].done(reloadList).fail(notification.exception);
};
/**
* Duplicate a template and reload the page.
* @method doDuplicate
* @param {Event} e
*/
var doDuplicate = function(e) {
e.preventDefault();
templateid = $(this).attr('data-templateid');
// We are chaining ajax requests here.
var requests = ajax.call([{
methodname: 'tool_lp_duplicate_template',
args: { id: templateid }
}, {
methodname: 'tool_lp_data_for_templates_manage_page',
args: {
pagecontext: {
contextid: pagecontextid
}
}
}]);
requests[1].done(reloadList).fail(notification.exception);
};
/**
* Handler for "Delete learning plan template" actions.
* @method confirmDelete
@@ -110,8 +135,7 @@ define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str'
};
return /** @alias module:tool_lp/templatedelete */ {
return /** @alias module:tool_lp/templateactions */ {
// Public variables and functions.
/**
* Expose the event handler for the delete.
@@ -120,6 +144,13 @@ define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str'
*/
deleteHandler: confirmDelete,
/**
* Expose the event handler for the duplicate.
* @method duplicateHandler
* @param {Event} e
*/
duplicateHandler: doDuplicate,
/**
* Initialise the module.
* @method init
+30
View File
@@ -756,6 +756,36 @@ class api {
return $template;
}
/**
* Duplicate a learning plan template.
*
* Requires tool/lp:templatemanage capability at the template context.
*
* @param int $id the template id.
* @return template
*/
public static function duplicate_template($id) {
$template = new template($id);
// First we do a permissions check.
require_capability('tool/lp:templatemanage', $template->get_context());
// OK - all set.
$templatecompetency = new template_competency();
$competencies = $templatecompetency->list_competencies($id, false);
// Adding the suffix copy.
$template->set_shortname(get_string('duplicateditemname', 'tool_lp', $template->get_shortname()));
$duplicatedtemplate = $template->create();
// Associate each competency for the duplicated template.
foreach ($competencies as $competency) {
self::add_competency_to_template($duplicatedtemplate->get_id(), $competency->get_id());
}
return $duplicatedtemplate;
}
/**
* Delete a learning plan template by id.
*
+51
View File
@@ -2578,6 +2578,57 @@ class external extends external_api {
return new external_value(PARAM_BOOL, 'True if the update was successful');
}
/**
* Returns description of duplicate_template() parameters.
*
* @return \external_function_parameters
*/
public static function duplicate_template_parameters() {
$templateid = new external_value(
PARAM_INT,
'The template id',
VALUE_REQUIRED
);
$params = array(
'id' => $templateid
);
return new external_function_parameters($params);
}
/**
* Expose to AJAX.
* @return boolean
*/
public static function duplicate_template_is_allowed_from_ajax() {
return true;
}
/**
* Duplicate a learning plan template.
*
* @param int $id the id of the learning plan template to duplicate
* @return boolean Record of new template.
*/
public static function duplicate_template($id) {
$params = self::validate_parameters(self::duplicate_template_parameters(),
array(
'id' => $id,
));
$result = api::duplicate_template($params['id']);
return $result->to_record();
}
/**
* Returns description of duplicate_template() result value.
*
* @return \external_description
*/
public static function duplicate_template_returns() {
return self::get_template_external_structure();
}
/**
* Returns description of list_templates() parameters.
*
+8
View File
@@ -276,6 +276,14 @@ $functions = array(
'type' => 'write',
'capabilities' => 'tool/lp:templatemanage',
),
'tool_lp_duplicate_template' => array(
'classname' => 'tool_lp\external',
'methodname' => 'duplicate_template',
'classpath' => '',
'description' => 'Duplicate learning plan template.',
'type' => 'write',
'capabilities' => 'tool/lp:templatemanage',
),
'tool_lp_read_template' => array(
'classname' => 'tool_lp\external',
'methodname' => 'read_template',
+1
View File
@@ -52,6 +52,7 @@ $string['deletethisplan'] = 'Delete this learning plan';
$string['description'] = 'Description';
$string['duedate'] = 'Due date';
$string['duedate_help'] = 'The date that a learning plan should be completed by.';
$string['duplicateditemname'] = '{$a} (copy)';
$string['editcompetency'] = 'Edit competency';
$string['editcompetencyframework'] = 'Edit competency framework';
$string['editplan'] = 'Edit learning plan';
@@ -57,6 +57,11 @@
{{#pix}}t/edit{{/pix}} {{#str}}edit{{/str}}
</a>
</li>
<li>
<a data-action="duplicatetemplate" data-templateid="{{id}}" href="#">
{{#pix}}t/copy{{/pix}} {{#str}}duplicate{{/str}}
</a>
</li>
<li>
<a data-action="deletetemplate" data-templateid="{{id}}" href="#">
{{#pix}}t/delete{{/pix}} {{#str}}delete{{/str}}
@@ -85,14 +90,15 @@
{{#js}}
// Initialise the JS.
require(['tool_lp/templatedelete',
require(['tool_lp/templateactions',
'tool_lp/menubar'],
function(deleteMod, menubar) {
function(actionsMod, menubar) {
deleteMod.init({{pagecontextid}});
actionsMod.init({{pagecontextid}});
menubar.enhance('.templateactions', {
'[data-action="deletetemplate"]': deleteMod.deleteHandler
'[data-action="deletetemplate"]': actionsMod.deleteHandler,
'[data-action="duplicatetemplate"]': actionsMod.duplicateHandler
});
});
+44
View File
@@ -1314,6 +1314,50 @@ class tool_lp_external_testcase extends externallib_advanced_testcase {
external::reorder_template_competency($template->id, $competency1->id, $competency2->id);
}
/**
* Test we can duplicate learning plan template.
*/
public function test_duplicate_learning_plan_template() {
$this->setUser($this->creator);
$syscontext = context_system::instance();
// Create a template.
$template = external::create_template('shortname', 'idnumber', time(), 'description', FORMAT_HTML, true,
array('contextid' => $syscontext->id));
$template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template);
// Create a competency framework.
$framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, 1,
$this->scaleconfiguration1, true, array('contextid' => context_system::instance()->id));
$framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
// Create multiple competencies.
$competency1 = external::create_competency('shortname1', 'idnumber1', 'description', FORMAT_HTML, true, $framework->id, 0);
$competency1 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency1);
$competency2 = external::create_competency('shortname2', 'idnumber2', 'description', FORMAT_HTML, true, $framework->id, 0);
$competency2 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency2);
$competency3 = external::create_competency('shortname3', 'idnumber3', 'description', FORMAT_HTML, true, $framework->id, 0);
$competency3 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency3);
// Add the competencies.
external::add_competency_to_template($template->id, $competency1->id);
external::add_competency_to_template($template->id, $competency2->id);
external::add_competency_to_template($template->id, $competency3->id);
// Duplicate the learning plan template.
$duplicatedtemplate = external::duplicate_template($template->id);
$result = external::list_competencies_in_template($template->id);
$resultduplicated = external::list_competencies_in_template($duplicatedtemplate->id);
$this->assertEquals(count($result), count($resultduplicated));
$this->assertContains($template->shortname, $duplicatedtemplate->shortname);
$this->assertEquals($duplicatedtemplate->description, $template->description);
$this->assertEquals($duplicatedtemplate->descriptionformat, $template->descriptionformat);
$this->assertEquals($duplicatedtemplate->visible, $template->visible);
}
/**
* Test that we can return scale values for a scale with the scale ID.
*/