diff --git a/admin/tool/lp/classes/api.php b/admin/tool/lp/classes/api.php index fd3e4b128c2..cb74ce17601 100644 --- a/admin/tool/lp/classes/api.php +++ b/admin/tool/lp/classes/api.php @@ -902,8 +902,7 @@ class api { } // OK - all set. - $templatecompetency = new template_competency(); - return $templatecompetency->count_templates($competencyid, $onlyvisible); + return template_competency::count_templates($competencyid, $onlyvisible); } /** @@ -927,8 +926,7 @@ class api { } // OK - all set. - $templatecompetency = new template_competency(); - return $templatecompetency->list_templates($competencyid, $onlyvisible); + return template_competency::list_templates($competencyid, $onlyvisible); } @@ -954,8 +952,7 @@ class api { } // OK - all set. - $templatecompetency = new template_competency(); - return $templatecompetency->count_competencies($templateid, $onlyvisible); + return template_competency::count_competencies($templateid, $onlyvisible); } /** @@ -980,8 +977,7 @@ class api { } // OK - all set. - $templatecompetency = new template_competency(); - return $templatecompetency->list_competencies($templateid, $onlyvisible); + return template_competency::list_competencies($templateid, $onlyvisible); } /** @@ -1006,13 +1002,11 @@ class api { throw new coding_exception('The competency does not exist'); } - $templatecompetency = new template_competency(); - $exists = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyid)); + $exists = template_competency::get_records(array('templateid' => $templateid, 'competencyid' => $competencyid)); if (!$exists) { - $templatecompetency->from_record($record); - if ($templatecompetency->create()) { - return true; - } + $templatecompetency = new template_competency(0, $record); + $templatecompetency->create(); + return true; } return false; } @@ -1039,8 +1033,7 @@ class api { throw new coding_exception('The competency does not exist'); } - $templatecompetency = new template_competency(); - $exists = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyid)); + $exists = template_competency::get_records(array('templateid' => $templateid, 'competencyid' => $competencyid)); if ($exists) { $link = array_pop($exists); return $link->delete(); @@ -1065,21 +1058,20 @@ class api { require_capability('tool/lp:templatemanage', $context); $down = true; - $templatecompetency = new template_competency(); - $matches = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyidfrom)); + $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 = $templatecompetency->get_records(array('templateid' => $templateid, 'competencyid' => $competencyidto)); + $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 = $templatecompetency->get_records(array('templateid' => $templateid), 'sortorder', 'ASC', 0, 0); + $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. diff --git a/admin/tool/lp/classes/output/template_competencies_page.php b/admin/tool/lp/classes/output/template_competencies_page.php index 0066961c401..44e93f10dc2 100644 --- a/admin/tool/lp/classes/output/template_competencies_page.php +++ b/admin/tool/lp/classes/output/template_competencies_page.php @@ -29,6 +29,7 @@ use renderer_base; use stdClass; use moodle_url; use context; +use context_system; use tool_lp\api; /** diff --git a/admin/tool/lp/classes/template_competency.php b/admin/tool/lp/classes/template_competency.php index 7fe07d458cc..2b9a53a7726 100644 --- a/admin/tool/lp/classes/template_competency.php +++ b/admin/tool/lp/classes/template_competency.php @@ -33,141 +33,43 @@ use stdClass; */ class template_competency extends persistent { - /** @var int $templateid The template id */ - private $templateid = 0; - - /** @var int $competencyid The competency id */ - private $competencyid = 0; - - /** @var int $sortorder A number used to influence sorting */ - private $sortorder = 0; + const TABLE = 'tool_lp_template_competency'; /** - * Method that provides the table name matching this class. + * Return the definition of the properties of this model. * - * @return string + * @return array */ - public function get_table_name() { - return 'tool_lp_template_competency'; + protected static function define_properties() { + return array( + 'templateid' => array( + 'type' => PARAM_INT, + 'default' => 0, + ), + 'competencyid' => array( + 'type' => PARAM_INT, + 'default' => 0, + ), + 'sortorder' => array( + 'type' => PARAM_INT, + 'default' => null, + ), + ); } /** - * Get the competency id - * - * @return int The competency id - */ - public function get_competencyid() { - return $this->competencyid; - } - - /** - * Set the competency id - * - * @param int $competencyid The competency id - */ - public function set_competencyid($competencyid) { - $this->competencyid = $competencyid; - } - - /** - * Get the sort order index. - * - * @return string The sort order index - */ - public function get_sortorder() { - return $this->sortorder; - } - - /** - * Set the sort order index. - * - * @param string $sortorder The sort order index - */ - public function set_sortorder($sortorder) { - $this->sortorder = $sortorder; - } - - /** - * Get the template id - * - * @return int The template id - */ - public function get_templateid() { - return $this->templateid; - } - - /** - * Set the template id - * - * @param int $templateid The template id - */ - public function set_templateid($templateid) { - $this->templateid = $templateid; - } - - /** - * Populate this class with data from a DB record. - * - * @param stdClass $record A DB record. - * @return template_competency - */ - public function from_record($record) { - if (isset($record->id)) { - $this->set_id($record->id); - } - if (isset($record->templateid)) { - $this->set_templateid($record->templateid); - } - if (isset($record->competencyid)) { - $this->set_competencyid($record->competencyid); - } - if (isset($record->sortorder)) { - $this->set_sortorder($record->sortorder); - } - if (isset($record->timecreated)) { - $this->set_timecreated($record->timecreated); - } - if (isset($record->timemodified)) { - $this->set_timemodified($record->timemodified); - } - if (isset($record->usermodified)) { - $this->set_usermodified($record->usermodified); - } - return $this; - } - - /** - * Create a DB record from this class. - * - * @return stdClass - */ - public function to_record() { - $record = new stdClass(); - $record->id = $this->get_id(); - $record->templateid = $this->get_templateid(); - $record->competencyid = $this->get_competencyid(); - $record->sortorder = $this->get_sortorder(); - $record->timecreated = $this->get_timecreated(); - $record->timemodified = $this->get_timemodified(); - $record->usermodified = $this->get_usermodified(); - - return $record; - } - - /** - * Count the templates using this competency + * Count the templates using a competency. * * @param int $competencyid The competency id * @param bool $onlyvisible If true, only count visible templates using this competency. * @return int */ - public function count_templates($competencyid, $onlyvisible) { + public static function count_templates($competencyid, $onlyvisible) { global $DB; - $template = new template(); $sql = 'SELECT COUNT(template.id) - FROM {' . $this->get_table_name() . '} tplcomp - JOIN {' . $template->get_table_name() . '} tpl + FROM {' . self::TABLE . '} tplcomp + JOIN {' . template::TABLE . '} tpl ON tplcomp.templateid = tpl.id WHERE tplcomp.competencyid = ? '; $params = array($competencyid); @@ -183,19 +85,18 @@ class template_competency extends persistent { } /** - * List the templates using this competency. + * List the templates using a competency. * * @param int $competencyid The competency id * @param bool $onlyvisible If true, only count visible templates using this competency. * @return array[competency] */ - public function list_templates($competencyid, $onlyvisible) { + public static function list_templates($competencyid, $onlyvisible) { global $DB; - $template = new template(); $sql = 'SELECT tpl.* - FROM {' . $template->get_table_name() . '} tpl - JOIN {' . $this->get_table_name() . '} tplcomp + FROM {' . template::TABLE . '} tpl + JOIN {' . self::TABLE . '} tplcomp ON tplcomp.templateid = tpl.id WHERE tplcomp.competencyid = ? '; $params = array($competencyid); @@ -216,19 +117,18 @@ class template_competency extends persistent { } /** - * Count the competencies in this template. + * Count the competencies in a template. * * @param int $templateid The template id * @param bool $onlyvisible If true, only count visible competencies in this template. * @return int */ - public function count_competencies($templateid, $onlyvisible) { + public static function count_competencies($templateid, $onlyvisible) { global $DB; - $competency = new competency(); $sql = 'SELECT COUNT(comp.id) - FROM {' . $this->get_table_name() . '} tplcomp - JOIN {' . $competency->get_table_name() . '} comp + FROM {' . self::TABLE . '} tplcomp + JOIN {' . competency::TABLE . '} comp ON tplcomp.competencyid = comp.id WHERE tplcomp.templateid = ? '; $params = array($templateid); @@ -250,13 +150,12 @@ class template_competency extends persistent { * @param bool $onlyvisible If true, only count visible competencies in this template. * @return array[competency] */ - public function list_competencies($templateid, $onlyvisible) { + public static function list_competencies($templateid, $onlyvisible) { global $DB; - $competency = new competency(); $sql = 'SELECT comp.* - FROM {' . $competency->get_table_name() . '} comp - JOIN {' . $this->get_table_name() . '} tplcomp + FROM {' . competency::TABLE . '} comp + JOIN {' . self::TABLE . '} tplcomp ON tplcomp.competencyid = comp.id WHERE tplcomp.templateid = ? ORDER BY tplcomp.sortorder ASC'; @@ -278,12 +177,40 @@ class template_competency extends persistent { } /** - * Add a default for the sortorder field to the default create logic. + * Hook to execute before validate. * - * @return persistent + * @return void */ - public function create() { - $this->sortorder = $this->count_records(array('templateid' => $this->get_templateid())); - return parent::create(); + protected function before_validate() { + if ($this->get_sortorder() === null) { + $this->set_sortorder($this->count_records(array('templateid' => $this->get_templateid()))); + } } + + /** + * Validate competencyid. + * + * @param int $value ID. + * @return true|lang_string + */ + protected function validate_competencyid($value) { + if (!competency::record_exists($value)) { + return new lang_string('invaliddata', 'error'); + } + return true; + } + + /** + * Validate templateid. + * + * @param int $value ID. + * @return true|lang_string + */ + protected function validate_templateid($value) { + if (!template::record_exists($value)) { + return new lang_string('invaliddata', 'error'); + } + return true; + } + }