From 5af3778e1328065dfd27ecdb25a4fef17380334a Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 25 Sep 2015 12:55:18 +0800 Subject: [PATCH] MDL-51506 tool_lp: Migrating plan to new model API --- admin/tool/lp/classes/api.php | 15 +- admin/tool/lp/classes/external.php | 15 +- admin/tool/lp/classes/output/plans_page.php | 5 +- admin/tool/lp/classes/plan.php | 316 +++++--------------- 4 files changed, 90 insertions(+), 261 deletions(-) diff --git a/admin/tool/lp/classes/api.php b/admin/tool/lp/classes/api.php index 800594b04e0..fd3e4b128c2 100644 --- a/admin/tool/lp/classes/api.php +++ b/admin/tool/lp/classes/api.php @@ -1127,8 +1127,7 @@ class api { $params['statusdraft'] = plan::STATUS_DRAFT; } - $plans = new plan(); - return $plans->get_records_select($select, $params, 'timemodified DESC'); + return plan::get_records_select($select, $params, 'timemodified DESC'); } /** @@ -1163,7 +1162,7 @@ class api { } $plan = new plan(0, $record); - $id = $plan->create(); + $plan->create(); return $plan; } @@ -1189,13 +1188,13 @@ class api { throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', ''); } - $current = new plan($record->id); + $plan = new plan($record->id); // We don't allow users without planmanage and without // planmanageown to edit plans that other users modified. - if (!$manageplans && !$manageownplan && $USER->id != $current->get_usermodified()) { + if (!$manageplans && !$manageownplan && $USER->id != $plan->get_usermodified()) { throw new \moodle_exception('erroreditingmodifiedplan', 'tool_lp'); - } else if (!$manageplans && $USER->id != $current->get_userid()) { + } else if (!$manageplans && $USER->id != $plan->get_userid()) { throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', ''); } @@ -1204,7 +1203,7 @@ class api { throw new required_capability_exception($context, 'tool/lp:planmanageown', 'nopermissions', ''); } - $plan = new plan($record->id, $record); + $plan->from_record($record); return $plan->update(); } @@ -1227,7 +1226,7 @@ class api { } // We require any of these capabilities to retrieve draft plans. - if ($plan->get_status() === plan::STATUS_DRAFT && + if ($plan->get_status() == plan::STATUS_DRAFT && !has_any_capability(array('tool/lp:planmanageown', 'tool/lp:planmanageall', 'tool/lp:plancreatedraft'), $context)) { // Exception about plancreatedraft as it is the one that is closer to basic users. throw new required_capability_exception($context, 'tool/lp:plancreatedraft', 'nopermissions', ''); diff --git a/admin/tool/lp/classes/external.php b/admin/tool/lp/classes/external.php index ee4f2cb50f9..b031b4ef503 100644 --- a/admin/tool/lp/classes/external.php +++ b/admin/tool/lp/classes/external.php @@ -3291,7 +3291,10 @@ class external extends external_api { $params = (object) $params; $result = api::create_plan($params); - return external_api::clean_returnvalue(self::create_plan_returns(), $result->to_record()); + $record = $result->to_record(); + $record->statusname = $result->get_statusname(); + $record->usercanupdate = $result->can_update(); + return external_api::clean_returnvalue(self::create_plan_returns(), $record); } /** @@ -3404,7 +3407,10 @@ class external extends external_api { $params = (object) $params; $result = api::update_plan($params); - return external_api::clean_returnvalue(self::update_plan_returns(), $result->to_record()); + $record = $result->to_record(); + $record->statusname = $result->get_statusname(); + $record->usercanupdate = $result->can_update(); + return external_api::clean_returnvalue(self::update_plan_returns(), $record); } /** @@ -3451,7 +3457,10 @@ class external extends external_api { )); $result = api::read_plan($params['id']); - return external_api::clean_returnvalue(self::read_plan_returns(), $result->to_record()); + $record = $result->to_record(); + $record->statusname = $result->get_statusname(); + $record->usercanupdate = $result->can_update(); + return external_api::clean_returnvalue(self::read_plan_returns(), $record); } /** diff --git a/admin/tool/lp/classes/output/plans_page.php b/admin/tool/lp/classes/output/plans_page.php index a2d7587696d..54b027a9ab1 100644 --- a/admin/tool/lp/classes/output/plans_page.php +++ b/admin/tool/lp/classes/output/plans_page.php @@ -86,7 +86,10 @@ class plans_page implements renderable, templatable { $data->plans = array(); if ($this->plans) { foreach ($this->plans as $plan) { - $data->plans[] = $plan->to_record(); + $record = $plan->to_record(); + $record['statusname'] = $plan->get_statusname(); + $record['usercanupdate'] = $plan->can_update(); + $data->plans[] = $record; } } diff --git a/admin/tool/lp/classes/plan.php b/admin/tool/lp/classes/plan.php index 2c5fb476bb1..bf4048a4fdb 100644 --- a/admin/tool/lp/classes/plan.php +++ b/admin/tool/lp/classes/plan.php @@ -23,8 +23,9 @@ */ namespace tool_lp; -use stdClass; use context_user; +use dml_missing_record_exception; +use lang_string; /** * Class for loading/storing plans from the DB. @@ -34,6 +35,8 @@ use context_user; */ class plan extends persistent { + const TABLE = 'tool_lp_plan'; + /** Draft status */ const STATUS_DRAFT = 0; @@ -43,174 +46,75 @@ class plan extends persistent { /** Complete status */ const STATUS_COMPLETE = 2; - /** @var string $name Name */ - private $name = ''; - - /** @var string $description Description for this learning plan */ - private $description = ''; - - /** @var int $descriptionformat Format for the description */ - private $descriptionformat = FORMAT_MOODLE; - - /** @var int $userid */ - private $userid = null; - - /** @var bool $templateid */ - private $templateid = null; - - /** @var bool $status The plan status, one of the 3 \tool_lp\plan:STATUS_* constants */ - private $status = null; - - /** @var bool $duedate */ - private $duedate = null; - /** - * 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_plan'; + protected static function define_properties() { + return array( + 'name' => array( + 'type' => PARAM_TEXT, + ), + 'description' => array( + 'type' => PARAM_TEXT, + 'default' => '' + ), + 'descriptionformat' => array( + 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN), + 'type' => PARAM_INT, + 'default' => FORMAT_HTML, + ), + 'userid' => array( + 'type' => PARAM_INT, + ), + 'templateid' => array( + 'type' => PARAM_INT, + 'default' => null, + 'null' => NULL_ALLOWED, + ), + 'status' => array( + 'choices' => array(self::STATUS_DRAFT, self::STATUS_COMPLETE, self::STATUS_ACTIVE), + 'type' => PARAM_INT, + 'default' => self::STATUS_DRAFT, + ), + 'duedate' => array( + 'type' => PARAM_INT, + 'default' => 0, + ), + ); } /** - * Getter method. + * Whether the current user can update the learning plan. * - * @return string + * @return bool|null */ - public function get_name() { - return $this->name; - } + public function can_update() { + global $USER; - /** - * Setter method. - * - * @param string $value value of the field. - * @return string - */ - public function set_name($value) { - $this->name = $value; - } - - /** - * Getter method. - * - * @return string - */ - public function get_description() { - return $this->description; - } - - /** - * Setter method. - * - * @param string $value value of the field. - * @return string - */ - public function set_description($value) { - $this->description = $value; - } - - /** - * Getter method. - * - * @return string - */ - public function get_descriptionformat() { - return $this->descriptionformat; - } - - /** - * Setter method. - * - * @param int $value value of the field. - * @return string - */ - public function set_descriptionformat($value) { - $this->descriptionformat = $value; - } - - /** - * Getter method. - * - * @return string - */ - public function get_userid() { - return $this->userid; - } - - /** - * Setter method. - * - * @param int $value value of the field. - * @return string - */ - public function set_userid($value) { - $this->userid = $value; - } - - /** - * Getter method. - * - * @return string - */ - public function get_templateid() { - return $this->templateid; - } - - /** - * Setter method. - * - * @param int $value value of the field. - * @return string - */ - public function set_templateid($value) { - $this->templateid = $value; - } - - /** - * Getter method. - * - * @return string - */ - public function get_status() { - if ($this->status === null) { + // Null if the record has not been filled. + if (!$userid = $this->get_userid()) { return null; } - return (int)$this->status; + $context = context_user::instance($userid); + + // Not all users can edit all plans, the template should know about it. + if (has_capability('tool/lp:planmanageall', $context) || + has_capability('tool/lp:planmanageown', $context)) { + return true; + } + + // The user that created the template can also edit it if he was the last one that modified it. But + // can't do it if it is already completed. + if ($USER->id == $userid && $this->get_usermodified() == $USER->id && $this->get_status() != self::STATUS_COMPLETE) { + return true; + } + + return false; } - /** - * Setter method. - * - * @param string $value value of the field. - * @return string - */ - public function set_status($value) { - $this->status = $value; - } - - /** - * Getter method. - * - * @return string - */ - public function get_duedate() { - return $this->duedate; - } - - /** - * Setter method. - * - * @param string $value value of the field. - * @return string - */ - public function set_duedate($value) { - $this->duedate = $value; - } - - // Extra methods. /** * Human readable status name. * @@ -239,105 +143,19 @@ class plan extends persistent { } /** - * Whether the current user can update the learning plan. + * Validate the template ID. * - * @return bool + * @param mixed $value The value. + * @return true|lang_string */ - public function get_usercanupdate() { - global $USER; + protected function validate_templateid($value) { - // Null if the record has not been filled. - if (!$userid = $this->get_userid()) { - return null; + // Checks that the template exists. + if (!empty($value) && !template::record_exists($value)) { + return new lang_string('invaliddata', 'error'); } - $context = context_user::instance($userid); - - // Not all users can edit all plans, the template should know about it. - if (has_capability('tool/lp:planmanageall', $context) || - has_capability('tool/lp:planmanageown', $context)) { - return true; - - } - - // The user that created the template can also edit it if he was the last one that modified it. But - // can't do it if it is already completed. - if ($USER->id == $userid && $this->get_usermodified() == $USER->id && $this->get_status() != self::STATUS_COMPLETE) { - return true; - } - - return false; + return true; } - /** - * Converts the object to a standard PHP object. - * - * If it is used to insert/update into DB the extra fields like statusname will be - * ignored, they are useful though when passing the object to templates. - * - * @return \stdClass - */ - public function to_record() { - - $record = new stdClass(); - $record->id = $this->get_id(); - $record->name = $this->get_name(); - $record->description = $this->get_description(); - $record->descriptionformat = $this->get_descriptionformat(); - $record->userid = $this->get_userid(); - $record->templateid = $this->get_templateid(); - $record->status = $this->get_status(); - $record->duedate = $this->get_duedate(); - $record->timecreated = $this->get_timecreated(); - $record->timemodified = $this->get_timemodified(); - $record->usermodified = $this->get_usermodified(); - - // Extra data. - $record->statusname = $this->get_statusname(); - $record->usercanupdate = $this->get_usercanupdate(); - - return $record; - } - - /** - * Get plan object from record. - * - * @param stdClass $record - * @return plan - */ - public function from_record($record) { - if (isset($record->id)) { - $this->set_id($record->id); - } - if (isset($record->name)) { - $this->set_name($record->name); - } - if (isset($record->description)) { - $this->set_description($record->description); - } - if (isset($record->descriptionformat)) { - $this->set_descriptionformat($record->descriptionformat); - } - if (isset($record->userid)) { - $this->set_userid($record->userid); - } - if (isset($record->templateid)) { - $this->set_templateid($record->templateid); - } - if (isset($record->status)) { - $this->set_status($record->status); - } - if (isset($record->duedate)) { - $this->set_duedate($record->duedate); - } - 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); - } - } }