Skip to content

Commit

Permalink
MDL-49462 tool_lp: added template competencies page
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson authored and Frederic Massart committed Apr 18, 2016
1 parent 24546ef commit 54c5a73
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 61 deletions.
Expand Up @@ -14,9 +14,9 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Handle add/remove course competency links.
* Handle add/remove competency links.
*
* @module tool_lp/coursecompetencies
* @module tool_lp/competencies
* @package tool_lp
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand All @@ -27,10 +27,12 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
/**
* Constructor
*
* @param {int} courseid
* @param {int} itemid
* @param {string} itemtype
*/
var coursecompetencies = function(courseid) {
this.courseid = courseid;
var competencies = function(itemid, itemtype) {
this.itemid = itemid;
this.itemtype = itemtype;
this.selectedCompetency = 0;
var localthis = this;
var loadframeworks = ajax.call([
Expand All @@ -53,15 +55,15 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
}).fail(notification.exception);
};

coursecompetencies.prototype.registerDragDrop = function() {
competencies.prototype.registerDragDrop = function() {
var localthis = this;
// Init this module.
str.get_string('movecoursecompetency', 'tool_lp').done(
str.get_string('movecompetency', 'tool_lp').done(
function(movestring) {
dragdrop.dragdrop('movecoursecompetency',
dragdrop.dragdrop('movecompetency',
movestring,
{ identifier: 'movecoursecompetency', component: 'tool_lp'},
{ identifier: 'movecoursecompetencyafter', component: 'tool_lp'},
{ identifier: 'movecompetency', component: 'tool_lp'},
{ identifier: 'movecompetencyafter', component: 'tool_lp'},
'drag-samenode',
'drag-parentnode',
'drag-handlecontainer',
Expand All @@ -73,20 +75,29 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial

};

coursecompetencies.prototype.handleDrop = function(drag, drop) {
competencies.prototype.handleDrop = function(drag, drop) {
var fromid = $(drag).data('id');
var toid = $(drop).data('id');
var localthis = this;

var requests = ajax.call([
{ methodname: 'tool_lp_reorder_course_competency',
args: { courseid: localthis.courseid, competencyidfrom: fromid, competencyidto: toid } }
if (localthis.itemtype == 'course') {
var requests = ajax.call([
{ methodname: 'tool_lp_reorder_course_competency',
args: { courseid: localthis.itemid, competencyidfrom: fromid, competencyidto: toid } }
]);
} else if (localthis.itemtype == 'template') {
var requests = ajax.call([
{ methodname: 'tool_lp_reorder_template_competency',
args: { templateid: localthis.itemid, competencyidfrom: fromid, competencyidto: toid } }
]);
requests[0].fail(notification.exception);
} else {
return null;
}

requests[0].fail(notification.exception);
};

coursecompetencies.prototype.applyFilter = function(e) {
competencies.prototype.applyFilter = function(e) {
e.preventDefault();
var localthis = this;
var searchInput = $('[data-region="filtercompetencies"] input');
Expand Down Expand Up @@ -115,7 +126,7 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
}).fail(notification.exception);
};

coursecompetencies.prototype.initLinkCourseCompetencies = function() {
competencies.prototype.initLinkCourseCompetencies = function() {
var localthis = this;

var competencytree = new ariatree('[data-enhance=linktree]', function(target) {
Expand Down Expand Up @@ -143,25 +154,41 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
}

$(e.target).attr('disabled', 'disabled');

// Add the link and reload the page template.
var requests = ajax.call([
{ methodname: 'tool_lp_add_competency_to_course',
args: { courseid: localthis.courseid, competencyid: localthis.selectedCompetency } },
{ methodname: 'tool_lp_data_for_course_competencies_page',
args: { courseid: localthis.courseid } }
]);
if (localthis.itemtype == 'course') {
var requests = ajax.call([
{ methodname: 'tool_lp_add_competency_to_course',
args: { courseid: localthis.itemid, competencyid: localthis.selectedCompetency } },
{ methodname: 'tool_lp_data_for_course_competencies_page',
args: { courseid: localthis.itemid } }
]);
var pagerender = 'tool_lp/course_competencies_page';
var pageregion = 'coursecompetenciespage';
} else if (localthis.itemtype == 'template') {
var requests = ajax.call([
{ methodname: 'tool_lp_add_competency_to_template',
args: { templateid: localthis.itemid, competencyid: localthis.selectedCompetency } },
{ methodname: 'tool_lp_data_for_template_competencies_page',
args: { templateid: localthis.itemid } }
]);
var pagerender = 'tool_lp/template_competencies_page';
var pageregion = 'templatecompetenciespage';
} else {
return null;
}

requests[1].done(function(context) {
templates.render('tool_lp/course_competencies_page', context).done(function(html, js) {
templates.render(pagerender, context).done(function(html, js) {
localthis.popup.close();
$('[data-region="coursecompetenciespage"]').replaceWith(html);
$('[data-region="' + pageregion + '"]').replaceWith(html);
templates.runTemplateJS(js);
}).fail(notification.exception);
}).fail(notification.exception);
});
};

coursecompetencies.prototype.registerEvents = function() {
competencies.prototype.registerEvents = function() {
var localthis = this;
$('[data-region="actions"] button').click(function(e) {
return localthis.openCompetencySelector.call(localthis, e);
Expand All @@ -172,23 +199,36 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
var deleteid = $(e.target).closest('[data-id]').data('id');

// Delete the link and reload the page template.
var requests = ajax.call([
{ methodname: 'tool_lp_remove_competency_from_course',
args: { courseid: localthis.courseid, competencyid: deleteid } },
{ methodname: 'tool_lp_data_for_course_competencies_page',
args: { courseid: localthis.courseid } }
]);
if (localthis.itemtype == 'course') {
var requests = ajax.call([
{ methodname: 'tool_lp_remove_competency_from_course',
args: { courseid: localthis.itemid, competencyid: deleteid } },
{ methodname: 'tool_lp_data_for_course_competencies_page',
args: { courseid: localthis.itemid } }
]);
var pagerender = 'tool_lp/course_competencies_page';
var pageregion = 'coursecompetenciespage';
} else if (localthis.itemtype == 'template') {
var requests = ajax.call([
{ methodname: 'tool_lp_remove_competency_from_template',
args: { templateid: localthis.itemid, competencyid: deleteid } },
{ methodname: 'tool_lp_data_for_template_competencies_page',
args: { templateid: localthis.itemid } }
]);
var pagerender = 'tool_lp/template_competencies_page';
var pageregion = 'templatecompetenciespage';
}

requests[1].done(function(context) {
templates.render('tool_lp/course_competencies_page', context).done(function(html, js) {
$('[data-region="coursecompetenciespage"]').replaceWith(html);
templates.render(pagerender, context).done(function(html, js) {
$('[data-region="' + pageregion + '"]').replaceWith(html);
templates.runTemplateJS(js);
}).fail(notification.exception);
}).fail(notification.exception);
});
};

coursecompetencies.prototype.addCompetencyChildren = function(parent, competencies) {
competencies.prototype.addCompetencyChildren = function(parent, competencies) {
var i;

for (i = 0; i < competencies.length; i++) {
Expand All @@ -202,7 +242,7 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
}
};

coursecompetencies.prototype.searchCompetencies = function() {
competencies.prototype.searchCompetencies = function() {
var localthis = this;
var deferred = $.Deferred();
var searchInput = $('[data-region="filtercompetencies"] input');
Expand Down Expand Up @@ -238,7 +278,7 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
return deferred.promise();
};

coursecompetencies.prototype.openCompetencySelector = function(e) {
competencies.prototype.openCompetencySelector = function(e) {
e.preventDefault();
var localthis = this;

Expand All @@ -247,7 +287,7 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
framework.selected = true;
var context = { framework: framework, frameworks: localthis.frameworks, competencies: competencies, search: '' };
templates.render('tool_lp/link_course_competencies', context).done(function(html) {
str.get_string('linkcoursecompetencies', 'tool_lp').done(function(title) {;
str.get_string('linkcompetencies', 'tool_lp').done(function(title) {;
localthis.popup = new dialogue(
title,
html, // The link UI.
Expand All @@ -258,5 +298,5 @@ define(['jquery', 'core/notification', 'core/ajax', 'core/templates', 'core/dial
}).fail(notification.exception);
};

return /** @alias module:core/tree */ coursecompetencies;
return /** @alias module:core/tree */ competencies;
});
66 changes: 56 additions & 10 deletions admin/tool/lp/classes/api.php
Expand Up @@ -962,9 +962,9 @@ public static function list_competencies_in_template($templateid) {
*/
public static function add_competency_to_template($templateid, $competencyid) {
// First we do a permissions check.
$context = context_course::instance($courseid);
$context = context_system::instance();

require_capability('tool/lp:templatemanage', $context);
require_capability('tool/lp:templatecompetencymanage', $context);

$record = new stdClass();
$record->templateid = $templateid;
Expand All @@ -973,7 +973,7 @@ public static function add_competency_to_template($templateid, $competencyid) {
$competency = new competency();
$competency->set_id($competencyid);
if (!$competency->read()) {
throw new coding_exception('The competency does not exist');
throw new coding_exception('The competency does not exist');
}

$templatecompetency = new template_competency();
Expand All @@ -998,7 +998,7 @@ public static function remove_competency_from_template($templateid, $competencyi
// First we do a permissions check.
$context = context_system::instance();

require_capability('tool/lp:templatemanage', $context);
require_capability('tool/lp:templatecompetencymanage', $context);

$record = new stdClass();
$record->templateid = $templateid;
Expand All @@ -1010,12 +1010,6 @@ public static function remove_competency_from_template($templateid, $competencyi
throw new coding_exception('The competency does not exist');
}

$template = new template();
$template->set_id($template);
if (!$template->read()) {
throw new coding_exception('The learning plan template does not exist');
}

$templatecompetency = new template_competency();
$exists = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyid));
if ($exists) {
Expand Down Expand Up @@ -1188,4 +1182,56 @@ public static function delete_plan($id) {

return $plan->delete();
}

/**
* Move the template competency up or down in the display list.
*
* Requires tool/lp:templatecompetencymanage capability at the system context.
*
* @param int $templateid The template id
* @param int $competencyidfrom The id of the competency we are moving.
* @param int $competencyidto The id of the competency we are moving to.
* @return boolean
*/
public static function reorder_template_competency($templateid, $competencyidfrom, $competencyidto) {
// First we do a permissions check.
$context = context_system::instance();

require_capability('tool/lp:templatecompetencymanage', $context);

$down = true;
$templatecompetency = new template_competency();
$matches = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyidfrom));
if (count($matches) == 0) {
throw new coding_exception('The link does not exist');
}

$competencyfrom = array_pop($matches);
$matches = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyidto));
if (count($matches) == 0) {
throw new coding_exception('The link does not exist');
}

$competencyto = array_pop($matches);

$all = $templatecompetency->get_records(array('templateid' => $templateid), 'sortorder', 'ASC', 0, 0);

if ($competencyfrom->get_sortorder() > $competencyto->get_sortorder()) {
// We are moving up, so put it before the "to" item.
$down = false;
}

foreach ($all as $id => $templatecompetency) {
$sort = $templatecompetency->get_sortorder();
if ($down && $sort > $competencyfrom->get_sortorder() && $sort <= $competencyto->get_sortorder()) {
$templatecompetency->set_sortorder($templatecompetency->get_sortorder() - 1);
$templatecompetency->update();
} else if (!$down && $sort >= $competencyto->get_sortorder() && $sort < $competencyfrom->get_sortorder()) {
$templatecompetency->set_sortorder($templatecompetency->get_sortorder() + 1);
$templatecompetency->update();
}
}
$competencyfrom->set_sortorder($competencyto->get_sortorder());
return $competencyfrom->update();
}
}
65 changes: 65 additions & 0 deletions admin/tool/lp/classes/external.php
Expand Up @@ -1966,6 +1966,71 @@ public static function reorder_course_competency_returns() {
return new external_value(PARAM_BOOL, 'True if successful.');
}

/**
* Returns description of reorder_template_competency() parameters.
*
* @return external_function_parameters
*/
public static function reorder_template_competency_parameters() {
$templateid = new external_value(
PARAM_INT,
'The template id',
VALUE_REQUIRED
);
$competencyidfrom = new external_value(
PARAM_INT,
'The competency id we are moving',
VALUE_REQUIRED
);
$competencyidto = new external_value(
PARAM_INT,
'The competency id we are moving to',
VALUE_REQUIRED
);
$params = array(
'templateid' => $templateid,
'competencyidfrom' => $competencyidfrom,
'competencyidto' => $competencyidto,
);
return new external_function_parameters($params);
}

/**
* Expose to AJAX
* @return boolean
*/
public static function reorder_template_competency_is_allowed_from_ajax() {
return true;
}

/**
* Change the order of template competencies.
*
* @param int $templateid The template id
* @param int $competencyidfrom The competency to move.
* @param int $competencyidto The competency to move to.
* @return bool
*/
public static function reorder_template_competency($templateid, $competencyidfrom, $competencyidto) {
$params = self::validate_parameters(self::reorder_template_competency_parameters(),
array(
'templateid' => $templateid,
'competencyidfrom' => $competencyidfrom,
'competencyidto' => $competencyidto,
));

return api::reorder_template_competency($params['templateid'], $params['competencyidfrom'], $params['competencyidto']);
}

/**
* Returns description of reorder_template_competency() result value.
*
* @return external_description
*/
public static function reorder_template_competency_returns() {
return new external_value(PARAM_BOOL, 'True if successful.');
}

/**
* Returns the external structure of a full template record.
*
Expand Down

0 comments on commit 54c5a73

Please sign in to comment.