/** * Remove a competency from this template. * * @param int $templateid The id of the template * @param int $competencyid The id of the competency * @return bool */ public static function remove_competency_from_template($templateid, $competencyid) { static::require_enabled(); // First we do a permissions check. $template = new template($templateid); if (!$template->can_manage()) { throw new required_capability_exception($template->get_context(), 'moodle/competency:templatemanage', 'nopermissions', ''); } $record = new stdClass(); $record->templateid = $templateid; $record->competencyid = $competencyid; $competency = new competency($competencyid); $exists = template_competency::get_records(array('templateid' => $templateid, 'competencyid' => $competencyid)); if ($exists) { $link = array_pop($exists); return $link->delete(); } return false; }
/** * Move the template competency up or down in the display list. * * Requires moodle/competency:templatemanage 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) { static::require_enabled(); $template = new template($templateid); // First we do a permissions check. if (!$template->can_manage()) { throw new required_capability_exception($template->get_context(), 'moodle/competency:templatemanage', 'nopermissions', ''); } $down = true; $matches = template_competency::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 = template_competency::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 = template_competency::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(); }