From 7507b82a189c28f4a4a1a6a1abd80252cd48eb8b Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 11 Dec 2015 13:16:39 +0800 Subject: [PATCH] MDL-49463 tool_lp: New form base class for persistent models --- admin/tool/lp/classes/form/competency.php | 95 ++----- .../lp/classes/form/competency_framework.php | 84 +++--- admin/tool/lp/classes/form/persistent.php | 263 ++++++++++++++++++ admin/tool/lp/classes/form/plan.php | 55 +--- admin/tool/lp/classes/form/template.php | 70 +---- admin/tool/lp/classes/form/user_evidence.php | 54 +--- admin/tool/lp/classes/output/plans_page.php | 2 +- admin/tool/lp/classes/persistent.php | 26 +- admin/tool/lp/editcompetency.php | 15 +- admin/tool/lp/editcompetencyframework.php | 11 +- admin/tool/lp/editplan.php | 8 +- admin/tool/lp/edittemplate.php | 12 +- admin/tool/lp/user_evidence_edit.php | 14 +- 13 files changed, 384 insertions(+), 325 deletions(-) create mode 100644 admin/tool/lp/classes/form/persistent.php diff --git a/admin/tool/lp/classes/form/competency.php b/admin/tool/lp/classes/form/competency.php index b096f220557..7ac307154fb 100644 --- a/admin/tool/lp/classes/form/competency.php +++ b/admin/tool/lp/classes/form/competency.php @@ -23,13 +23,9 @@ */ namespace tool_lp\form; - defined('MOODLE_INTERNAL') || die(); -use moodleform; -use tool_lp\api; - -require_once($CFG->libdir.'/formslib.php'); +use stdClass; /** * Competency framework form. @@ -38,7 +34,9 @@ require_once($CFG->libdir.'/formslib.php'); * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class competency extends moodleform { +class competency extends persistent { + + protected static $persistentclass = 'tool_lp\\competency'; /** * Define the form - called by parent constructor @@ -47,26 +45,13 @@ class competency extends moodleform { global $PAGE; $mform = $this->_form; - $id = $this->_customdata['id']; $framework = $this->_customdata['competencyframework']; $parent = $this->_customdata['parent']; - $competency = $this->_customdata['competency']; - - $mform->addElement('hidden', 'id'); - $mform->setType('id', PARAM_INT); - $mform->setDefault('id', 0); - - $mform->addElement('hidden', 'parentid'); - $mform->setType('parentid', PARAM_INT); - if ($parent) { - $mform->setDefault('parentid', $parent->get_id()); - } else { - $mform->setDefault('parentid', 0); - } + $competency = $this->get_persistent(); $mform->addElement('hidden', 'competencyframeworkid'); $mform->setType('competencyframeworkid', PARAM_INT); - $mform->setDefault('competencyframeworkid', $framework->get_id()); + $mform->setConstant('competencyframeworkid', $framework->get_id()); $mform->addElement('header', 'generalhdr', get_string('general')); @@ -120,70 +105,38 @@ class competency extends moodleform { $mform->addHelpButton('visible', 'visible', 'tool_lp'); $this->add_action_buttons(true, get_string('savechanges', 'tool_lp')); - - if (!empty($competency) && !$this->is_submitted()) { - $record = $competency->to_record(); - // Massage for editor API. - $record->description = array('text' => $record->description, 'format' => $record->descriptionformat); - $this->set_data($record); - } - } /** - * Get form data. - * Conveniently removes non-desired properties. + * Convert some fields. + * * @return object */ - public function get_data() { - $data = parent::get_data(); - if (is_object($data)) { - unset($data->submitbutton); - - // Ensure that we have the format expected by the persistent. - if (empty($data->scaleid)) { - $data->scaleid = null; - $data->scaleconfiguration = null; - } + protected static function convert_fields(stdClass $data) { + $data = parent::convert_fields($data); + if (empty($data->scaleid)) { + $data->scaleid = null; + $data->scaleconfiguration = null; } return $data; } /** - * Form validation. - * @param array $data - * @param array $files - * @return array + * Extra validation. + * + * @param stdClass $data Data to validate. + * @param array $files Array of files. + * @param array $errors Currently reported errors. + * @return array of additional errors, or overridden errors. */ - public function validation($data, $files) { - $errors = parent::validation($data, $files); - $competency = $this->_customdata['competency']; - if (!$competency) { - $competency = new \tool_lp\competency(); - } - - // Fetch data like this to remove CSRF tokens, etc... - $data = $this->get_submitted_data(); - unset($data->submitbutton); - - // Ensure that we send the expected format to the persistent. - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; - if (empty($data->scaleid)) { - $data->scaleid = null; - $data->scaleconfiguration = null; - } - - // Validate from the model. - $competency->from_record($data); - $errors = $competency->get_errors(); - + protected function extra_validation($data, $files, array &$errors) { + $newerrors = array(); // Move the error from scaleconfiguration to the form element scale ID. if (isset($errors['scaleconfiguration']) && !isset($errors['scaleid'])) { - $errors['scaleid'] = $errors['scaleconfiguration']; + $newerrors['scaleid'] = $errors['scaleconfiguration']; unset($errors['scaleconfiguration']); } - - return $errors; + return $newerrors; } + } diff --git a/admin/tool/lp/classes/form/competency_framework.php b/admin/tool/lp/classes/form/competency_framework.php index e94456fff56..fd9ad4daf8a 100644 --- a/admin/tool/lp/classes/form/competency_framework.php +++ b/admin/tool/lp/classes/form/competency_framework.php @@ -23,13 +23,9 @@ */ namespace tool_lp\form; - defined('MOODLE_INTERNAL') || die(); -use moodleform; -use tool_lp\api; - -require_once($CFG->libdir.'/formslib.php'); +use stdClass; /** * Competency framework form. @@ -38,7 +34,9 @@ require_once($CFG->libdir.'/formslib.php'); * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class competency_framework extends moodleform { +class competency_framework extends persistent { + + protected static $persistentclass = 'tool_lp\\competency_framework'; /** * Define the form - called by parent constructor @@ -47,17 +45,12 @@ class competency_framework extends moodleform { global $PAGE; $mform = $this->_form; - $id = $this->_customdata['id']; $context = $this->_customdata['context']; - $framework = null; + $framework = $this->get_persistent(); - if ($id) { - $framework = api::read_framework($id);; - } - - $mform->addElement('hidden', 'id'); - $mform->setType('id', PARAM_INT); - $mform->setDefault('id', 0); + $mform->addElement('hidden', 'contextid'); + $mform->setType('contextid', PARAM_INT); + $mform->setConstant('contextid', $context->id); $mform->addElement('header', 'generalhdr', get_string('general')); @@ -110,55 +103,46 @@ class competency_framework extends moodleform { $mform->setDefault('taxonomies', $taxdefaults); $this->add_action_buttons(true, get_string('savechanges', 'tool_lp')); - - if ($framework && !$this->is_submitted()) { - $record = $framework->to_record(); - // Massage for editor API. - $record->description = array('text' => $record->description, 'format' => $record->descriptionformat); - // New hair cut for taxonomies. - $record->taxonomies = $framework->get_taxonomies(); - $this->set_data($record); - } - } /** - * Get form data. - * Conveniently removes non-desired properties. + * Convert some fields. + * * @return object */ - public function get_data() { - $data = parent::get_data(); - if (is_object($data)) { - unset($data->submitbutton); - } + protected static function convert_fields(stdClass $data) { + $data = parent::convert_fields($data); + $data->taxonomies = implode(',', $data->taxonomies); return $data; } /** - * Form validation. - * @param array $data - * @param array $files - * @return array + * Extra validation. + * + * @param stdClass $data Data to validate. + * @param array $files Array of files. + * @param array $errors Currently reported errors. + * @return array of additional errors, or overridden errors. */ - public function validation($data, $files) { - $context = $this->_customdata['context']; - - $data = $this->get_submitted_data(); - unset($data->submitbutton); - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; - $data->contextid = $context->id; - $data->taxonomies = implode(',', $data->taxonomies); - - $framework = new \tool_lp\competency_framework(0, $data); - $errors = $framework->get_errors(); + protected function extra_validation($data, $files, array &$errors) { + $newerrors = array(); + // Move the error from scaleconfiguration to the form element scale ID. if (isset($errors['scaleconfiguration']) && !isset($errors['scaleid'])) { - $errors['scaleid'] = $errors['scaleconfiguration']; + $newerrors['scaleid'] = $errors['scaleconfiguration']; unset($errors['scaleconfiguration']); } + return $newerrors; + } - return $errors; + /** + * Get the default data. + * + * @return stdClass + */ + protected function get_default_data() { + $data = parent::get_default_data(); + $data->taxonomies = $this->get_persistent()->get_taxonomies(); + return $data; } } diff --git a/admin/tool/lp/classes/form/persistent.php b/admin/tool/lp/classes/form/persistent.php new file mode 100644 index 00000000000..aad5b6d206e --- /dev/null +++ b/admin/tool/lp/classes/form/persistent.php @@ -0,0 +1,263 @@ +. + +/** + * Persistent form abstract. + * + * @package tool_lp + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_lp\form; +defined('MOODLE_INTERNAL') || die(); + +use coding_exception; +use moodleform; +use stdClass; + +require_once($CFG->libdir.'/formslib.php'); + +/** + * Persistent form abstract class. + * + * This provides some shortcuts to validate objects based on the persistent model. + * + * Note that all mandatory fields (non-optional) of your model should be included in the + * form definition. Mandatory fields which are not editable by the user should be + * as hidden and constant. + * + * $mform->addElement('hidden', 'userid'); + * $mform->setType('userid', PARAM_INT); + * $mform->setConstant('userid', $this->_customdata['userid']); + * + * You may exclude some fields from the validation should your form include other + * properties such as files. To do so use the $foreignfields property. + * + * @package tool_lp + * @copyright 2015 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class persistent extends moodleform { + + /** @var string The fully qualified classname. */ + protected static $persistentclass = null; + + /** @var array Fields to remove when getting the final data. */ + protected static $fieldstoremove = array('submitbutton'); + + /** @var array Fields to remove from the persistent validation. */ + protected static $foreignfields = array(); + + /** @var \tool_lp\peristent Reference to the persistent. */ + private $persistent = null; + + /** + * Constructor. + * + * The 'persistent' has to be passed as custom data when 'editing'. + * + * Note that in order for your persistent to be reloaded after form submission you should + * either override the URL to include the ID to your resource, or add the ID to the form + * fields. + * + * @param mixed $action + * @param mixed $customdata + * @param string $method + * @param string $target + * @param mixed $attributes + * @param bool $editable + */ + public function __construct($action = null, $customdata = null, $method = 'post', $target = '', $attributes = null, $editable = true) { + if (empty(static::$persistentclass)) { + throw new coding_exception('Static property $persistentclass must be set.'); + } else if (!is_subclass_of(static::$persistentclass, 'tool_lp\\persistent')) { + throw new coding_exception('Static property $persistentclass is not valid.'); + } else if (!array_key_exists('persistent', $customdata)) { + throw new coding_exception('The custom data \'persistent\' key must be set, even if it is null.'); + } + + // Make a copy of the persistent passed, this ensures validation and object reference issues. + $persistendata = new stdClass(); + $persistent = isset($customdata['persistent']) ? $customdata['persistent'] : null; + if ($persistent) { + if (!($persistent instanceof static::$persistentclass)) { + throw new coding_exception('Invalid persistent'); + } + $persistendata = $persistent->to_record(); + unset($persistent); + } + + $this->persistent = new static::$persistentclass(); + $this->persistent->from_record($persistendata); + + // TODO Make PHP7 compatible. + unset($customdata['persistent']); + parent::moodleform($action, $customdata, $method, $target, $attributes, $editable); + + // Load the defaults. + $this->set_data($this->get_default_data()); + } + + /** + * Convert some fields. + * + * @param stdClass $data The whole data set. + * @return stdClass The amended data set. + */ + protected static function convert_fields(stdClass $data) { + $class = static::$persistentclass; + $properties = $class::get_formatted_properties(); + + foreach ($data as $field => $value) { + // Replace formatted properties. + if (isset($properties[$field])) { + $formatfield = $properties[$field]; + $data->$formatfield = $data->{$field}['format']; + $data->$field = $data->{$field}['text']; + } + } + + return $data; + } + + /** + * Define extra validation mechanims. + * + * The data here: + * - does not include {@link self::$fieldstoremove}. + * - does include {@link self::$foreignfields}. + * - was converted to map persistent-like data, e.g. array $description to string $description + int $descriptionformat. + * + * You can modify the $errors parameter in order to remove some validation errors should you + * need to. However, the best practice is to return new or overriden errors. Only modify the + * errors passed by reference when you have no other option. + * + * Do not add any logic here, it is only intended to be used by child classes. + * + * @param stdClass $data Data to validate. + * @param array $files Array of files. + * @param array $errors Currently reported errors. + * @return array of additional errors, or overridden errors. + */ + protected function extra_validation($data, $files, array &$errors) { + return array(); + } + + /** + * Get the default data. + * + * This is the data that is prepopulated in the form at it loads, we automatically + * fetch all the properties of the persistent however some needs to be converted + * to map the form structure. + * + * Extend this class if you need to add more conversion. + * + * @return stdClass + */ + protected function get_default_data() { + $data = $this->get_persistent()->to_record(); + $class = static::$persistentclass; + $properties = $class::get_formatted_properties(); + + foreach ($data as $field => $value) { + // Convert formatted properties. + if (isset($properties[$field])) { + $data->$field = array( + 'text' => $data->$field, + 'format' => $data->{$properties[$field]} + ); + unset($data->{$properties[$field]}); + } + } + + return $data; + } + + /** + * Get form data. + * + * Conveniently removes non-desired properties and add the ID property. + * + * @return object|null + */ + public function get_data() { + $data = parent::get_data(); + if (is_object($data)) { + foreach (static::$fieldstoremove as $field) { + unset($data->{$field}); + } + $data = static::convert_fields($data); + + // Ensure that the ID is set. + $data->id = $this->persistent->get_id(); + } + return $data; + } + + /** + * Return the persistent object associated with this form instance. + * + * @return tool_lp\persistent + */ + final protected function get_persistent() { + return $this->persistent; + } + + /** + * Get the submitted form data. + * + * Conveniently removes non-desired properties. + * + * @return object|null + */ + public function get_submitted_data() { + $data = parent::get_submitted_data(); + if (is_object($data)) { + foreach (static::$fieldstoremove as $field) { + unset($data->{$field}); + } + $data = static::convert_fields($data); + } + return $data; + } + + /** + * Form validation. + * + * If you need extra validation, use {@link self::extra_validation()}. + * + * @param array $data + * @param array $files + * @return array + */ + public final function validation($data, $files) { + $errors = parent::validation($data, $files); + $data = $this->get_submitted_data(); + + // Only validate compatible fields. + $persistentdata = array_diff_key((array) $data, array_flip((array) static::$foreignfields)); + $persistent = $this->get_persistent(); + $persistent->from_record((object) $persistentdata); + $errors = array_merge($errors, $persistent->get_errors()); + + // Apply extra validation. + $extraerrors = $this->extra_validation($data, $files, $errors); + $errors = array_merge($errors, (array) $extraerrors); + + return $errors; + } +} diff --git a/admin/tool/lp/classes/form/plan.php b/admin/tool/lp/classes/form/plan.php index 2dd1d3763c9..fda7d5062a9 100644 --- a/admin/tool/lp/classes/form/plan.php +++ b/admin/tool/lp/classes/form/plan.php @@ -23,13 +23,10 @@ */ namespace tool_lp\form; - defined('MOODLE_INTERNAL') || die(); -use moodleform; -use tool_lp\api; use tool_lp\plan as planpersistent; -require_once($CFG->libdir.'/formslib.php'); +use required_capability_exception; /** * Learning plan form. @@ -38,7 +35,9 @@ require_once($CFG->libdir.'/formslib.php'); * @copyright 2015 David Monllao * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class plan extends moodleform { +class plan extends persistent { + + protected static $persistentclass = 'tool_lp\\plan'; /** * Define the form - called by parent constructor @@ -47,12 +46,9 @@ class plan extends moodleform { $mform = $this->_form; $context = $this->_customdata['context']; - $mform->addElement('hidden', 'id'); - $mform->setType('id', PARAM_INT); - $mform->setDefault('id', 0); - - $mform->addElement('hidden', 'userid', $this->_customdata['userid']); + $mform->addElement('hidden', 'userid'); $mform->setType('userid', PARAM_INT); + $mform->setConstant('userid', $this->_customdata['userid']); $mform->addElement('text', 'name', get_string('planname', 'tool_lp')); $mform->setType('name', PARAM_TEXT); @@ -74,45 +70,6 @@ class plan extends moodleform { } $this->add_action_buttons(true, get_string('savechanges', 'tool_lp')); - - if (isset($this->_customdata['plan'])) { - if (!$this->is_submitted()) { - $plan = $this->_customdata['plan']; - $record = $plan->to_record(); - $record->description = array('text' => $record->description, 'format' => $record->descriptionformat); - $this->set_data($record); - } - } - } - /** - * Get form data. - * Conveniently removes non-desired properties. - * @return object - */ - public function get_data() { - $data = parent::get_data(); - if (is_object($data)) { - unset($data->submitbutton); - } - return $data; - } - - /** - * Get the template select options from the templates list. - * - * @return array|false - */ - protected function get_template_options() { - if (empty($this->_customdata['templates'])) { - return false; - } - - $options = array('' => get_string('choosedots')); - foreach ($this->_customdata['templates'] as $template) { - $options[$template->get_id()] = $template->get_shortname(); - } - return $options; - } } diff --git a/admin/tool/lp/classes/form/template.php b/admin/tool/lp/classes/form/template.php index ab3b4d947a4..1ca86ef9cba 100644 --- a/admin/tool/lp/classes/form/template.php +++ b/admin/tool/lp/classes/form/template.php @@ -23,14 +23,8 @@ */ namespace tool_lp\form; - defined('MOODLE_INTERNAL') || die(); -use moodleform; -use tool_lp\api; - -require_once($CFG->libdir.'/formslib.php'); - /** * Learning plan template form. * @@ -38,24 +32,23 @@ require_once($CFG->libdir.'/formslib.php'); * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class template extends moodleform { +class template extends persistent { + + protected static $persistentclass = 'tool_lp\\template'; /** * Define the form - called by parent constructor */ public function definition() { $mform = $this->_form; - $template = $this->_customdata['template']; - if (empty($template)) { - $id = 0; - } else { - $id = $template->get_id(); - } + $context = $this->_customdata['context']; - $mform->addElement('hidden', 'id'); - $mform->setType('id', PARAM_INT); - $mform->setDefault('id', 0); + $mform->addElement('hidden', 'contextid'); + $mform->setType('contextid', PARAM_INT); + $mform->setConstant('contextid', $context->id); + + $mform->addElement('header', 'generalhdr', get_string('general')); $mform->addElement('text', 'shortname', get_string('shortname', 'tool_lp')); @@ -80,51 +73,6 @@ class template extends moodleform { $this->add_action_buttons(true, get_string('savechanges', 'tool_lp')); - if (!$this->is_submitted() && !empty($template)) { - $record = $template->to_record(); - // Massage for editor API. - $record->description = array('text' => $record->description, 'format' => $record->descriptionformat); - $this->set_data($record); - } - - } - - /** - * Get form data. - * Conveniently removes non-desired properties. - * @return object - */ - public function get_data() { - $data = parent::get_data(); - if (is_object($data)) { - unset($data->submitbutton); - } - return $data; - } - - /** - * Extra the form. - * - * @param array $data - * @param array $files - * @return array - */ - public function validation($data, $files) { - $data = $this->get_submitted_data(); // To remove extra fields (sesskey, __qf_, ...). - unset($data->submitbutton); - - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; - - $template = new \tool_lp\template(0, $data); - $errors = $template->get_errors(); - - // The context ID is not submitted via this form. - if (isset($errors['contextid'])) { - unset($errors['contextid']); - } - - return $errors; } } diff --git a/admin/tool/lp/classes/form/user_evidence.php b/admin/tool/lp/classes/form/user_evidence.php index 028c0645a00..932eb92bba9 100644 --- a/admin/tool/lp/classes/form/user_evidence.php +++ b/admin/tool/lp/classes/form/user_evidence.php @@ -25,9 +25,6 @@ namespace tool_lp\form; defined('MOODLE_INTERNAL') || die(); -use moodleform; -require_once($CFG->libdir.'/formslib.php'); - /** * User evidence form class. * @@ -35,14 +32,19 @@ require_once($CFG->libdir.'/formslib.php'); * @copyright 2015 Frédéric Massart - FMCorz.net * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class user_evidence extends moodleform { +class user_evidence extends persistent { + + protected static $persistentclass = 'tool_lp\\user_evidence'; + + protected static $foreignfields = array('files'); - /** - * Define the form - called by parent constructor - */ public function definition() { $mform = $this->_form; + $mform->addElement('hidden', 'userid'); + $mform->setType('userid', PARAM_INT); + $mform->setConstant('userid', $this->_customdata['userid']); + $mform->addElement('header', 'generalhdr', get_string('general')); $mform->addElement('text', 'name', get_string('userevidencename', 'tool_lp'), 'maxlength="100"'); @@ -54,7 +56,7 @@ class user_evidence extends moodleform { $mform->setType('description', PARAM_TEXT); $mform->addElement('url', 'url', get_string('userevidenceurl', 'tool_lp'), array(), array('usefilepicker' => false)); - $mform->setType('url', PARAM_URL); + $mform->setType('url', PARAM_RAW_TRIMMED); // Can not use PARAM_URL, it silently converts bad URLs to ''. $mform->addElement('filemanager', 'files', get_string('userevidencefiles', 'tool_lp'), array(), $this->_customdata['fileareaoptions']); @@ -62,40 +64,4 @@ class user_evidence extends moodleform { $this->add_action_buttons(); } - /** - * Get form data. - * Conveniently removes non-desired properties. - * @return object - */ - public function get_data() { - $data = parent::get_data(); - if (is_object($data)) { - unset($data->submitbutton); - } - return $data; - } - - /** - * Extra validation the form. - * - * @param array $data - * @param array $files - * @return array - */ - public function validation($data, $files) { - $data = $this->get_submitted_data(); // To remove extra fields (sesskey, __qf_, ...). - unset($data->submitbutton); - unset($data->files); - - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; - $data->userid = $this->_customdata['userid']; - $data->id = $this->_customdata['id']; - - $template = new \tool_lp\user_evidence(0, $data); - $errors = $template->get_errors(); - - return $errors; - } - } diff --git a/admin/tool/lp/classes/output/plans_page.php b/admin/tool/lp/classes/output/plans_page.php index cba82cb12be..f269ecd0f66 100644 --- a/admin/tool/lp/classes/output/plans_page.php +++ b/admin/tool/lp/classes/output/plans_page.php @@ -70,7 +70,7 @@ class plans_page implements renderable, templatable { if (plan::can_manage_user($userid) || plan::can_manage_user_draft($userid)) { $addplan = new single_button( new moodle_url('/admin/tool/lp/editplan.php', array('userid' => $userid)), - get_string('addnewplan', 'tool_lp') + get_string('addnewplan', 'tool_lp'), 'get' ); $this->navigation[] = $addplan; } diff --git a/admin/tool/lp/classes/persistent.php b/admin/tool/lp/classes/persistent.php index 1ec18906af8..eb244334339 100644 --- a/admin/tool/lp/classes/persistent.php +++ b/admin/tool/lp/classes/persistent.php @@ -191,7 +191,8 @@ abstract class persistent { // List of reserved property names. Mostly because we have methods (getters/setters) which would confict with them. // Think about backwards compability before adding new ones here! - $reserved = array('errors', 'records', 'records_select', 'property_default_value', 'property_error_message'); + $reserved = array('errors', 'formatted_properties', 'records', 'records_select', 'property_default_value', + 'property_error_message'); foreach ($def as $property => $definition) { @@ -218,6 +219,29 @@ abstract class persistent { return $def; } + /** + * Gets all the formatted properties. + * + * Formatted properties are properties which have a format associated with them. + * + * @return array Keys are property names, values are property format names. + */ + final public static function get_formatted_properties() { + $properties = static::properties_definition(); + + $formatted = array(); + foreach ($properties as $property => $definition) { + $propertyformat = $property . 'format'; + // TODO MDL-52454 Check PARAM_RAW. + if ($definition['type'] == PARAM_TEXT && array_key_exists($propertyformat, $properties) + && $properties[$propertyformat]['type'] == PARAM_INT) { + $formatted[$property] = $propertyformat; + } + } + + return $formatted; + } + /** * Gets the default value for a property. * diff --git a/admin/tool/lp/editcompetency.php b/admin/tool/lp/editcompetency.php index 21b3ed5f101..07398ae1de6 100644 --- a/admin/tool/lp/editcompetency.php +++ b/admin/tool/lp/editcompetency.php @@ -34,7 +34,6 @@ $parentid = optional_param('parentid', 0, PARAM_INT); require_login(); $pagecontext = context::instance_by_id($pagecontextid); - // Set up the page. $url = new moodle_url("/admin/tool/lp/editcompetency.php", array('id' => $id, 'competencyframeworkid' => $competencyframeworkid, 'parentid' => $parentid, 'pagecontextid' => $pagecontextid)); @@ -71,8 +70,8 @@ $PAGE->set_heading($title); $PAGE->navbar->add($competencyframework->get_shortname(), $frameworkurl); $output = $PAGE->get_renderer('tool_lp'); -$form = new \tool_lp\form\competency($url->out(false), array('id' => $id, 'competencyframework' => $competencyframework, - 'parent' => $parent, 'competency' => $competency)); +$form = new \tool_lp\form\competency($url->out(false), array('competencyframework' => $competencyframework, + 'parent' => $parent, 'persistent' => $competency)); if ($form->is_cancelled()) { redirect($frameworkurl); @@ -83,18 +82,12 @@ echo $output->heading($pagetitle); $data = $form->get_data(); if ($data) { - // Save the changes and continue back to the manage page. - // Massage the editor data. - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; - if (empty($data->id)) { - // Create new framework. - require_sesskey(); + require_sesskey(); + if (empty($competency)) { \tool_lp\api::create_competency($data); echo $output->notification(get_string('competencycreated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($frameworkurl); } else { - require_sesskey(); \tool_lp\api::update_competency($data); echo $output->notification(get_string('competencyupdated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($frameworkurl); diff --git a/admin/tool/lp/editcompetencyframework.php b/admin/tool/lp/editcompetencyframework.php index 0299acc9125..df8a72e45e0 100644 --- a/admin/tool/lp/editcompetencyframework.php +++ b/admin/tool/lp/editcompetencyframework.php @@ -28,6 +28,7 @@ require_once($CFG->libdir.'/adminlib.php'); $id = optional_param('id', 0, PARAM_INT); $pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to where we can from. +$framework = null; if (!empty($id)) { // Always use the context from the framework when it exists. $framework = new \tool_lp\competency_framework($id); @@ -43,7 +44,6 @@ require_capability('tool/lp:competencymanage', $context); // We keep the original context in the URLs, so that we remain in the same context. $url = new moodle_url("/admin/tool/lp/editcompetencyframework.php", array('id' => $id, 'pagecontextid' => $pagecontextid)); $frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid)); -$formurl = new moodle_url("/admin/tool/lp/editcompetencyframework.php", array('pagecontextid' => $pagecontextid)); $title = get_string('competencies', 'tool_lp'); if (empty($id)) { @@ -60,7 +60,7 @@ $PAGE->set_url($url); $PAGE->set_title($title); $PAGE->set_heading($title); $output = $PAGE->get_renderer('tool_lp'); -$form = new \tool_lp\form\competency_framework($formurl->out(false), array('id' => $id, 'context' => $context)); +$form = new \tool_lp\form\competency_framework($url->out(false), array('context' => $context, 'persistent' => $framework)); if ($form->is_cancelled()) { redirect($frameworksurl); @@ -71,19 +71,14 @@ echo $output->heading($pagetitle); $data = $form->get_data(); if ($data) { - // Save the changes and continue back to the manage page. - // Massage the editor data. - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; + require_sesskey(); if (empty($data->id)) { // Create new framework. - require_sesskey(); $data->contextid = $context->id; \tool_lp\api::create_framework($data); echo $output->notification(get_string('competencyframeworkcreated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($frameworksurl); } else { - require_sesskey(); \tool_lp\api::update_framework($data); echo $output->notification(get_string('competencyframeworkupdated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($frameworksurl); diff --git a/admin/tool/lp/editplan.php b/admin/tool/lp/editplan.php index d95b17cfdb2..6b9e625fb43 100644 --- a/admin/tool/lp/editplan.php +++ b/admin/tool/lp/editplan.php @@ -50,7 +50,7 @@ if (empty($id)) { $output = $PAGE->get_renderer('tool_lp'); // Custom data to pass to the form. -$customdata = array('userid' => $userid, 'context' => $PAGE->context); +$customdata = array('userid' => $userid, 'context' => $PAGE->context, 'persistent' => $plan); // User can create plan if he can_manage_user with active/complete status // or if he can_manage_user_draft with draft status. @@ -64,7 +64,6 @@ if ($plan != null) { if (!$plan->can_be_edited()) { throw new coding_exception('Completed plan can not be edited'); } - $customdata['plan'] = $plan; } else if (!$cancreate) { throw new required_capability_exception($PAGE->context, 'tool/lp:planmanage', 'nopermissions', ''); } @@ -83,15 +82,12 @@ if (!empty($subtitle)) { $data = $form->get_data(); if ($data) { - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; + require_sesskey(); if (empty($data->id)) { - require_sesskey(); \tool_lp\api::create_plan($data); echo $output->notification(get_string('plancreated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($returnurl); } else { - require_sesskey(); \tool_lp\api::update_plan($data); echo $output->notification(get_string('planupdated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($returnurl); diff --git a/admin/tool/lp/edittemplate.php b/admin/tool/lp/edittemplate.php index 1df20cc3edf..b26fe4df934 100644 --- a/admin/tool/lp/edittemplate.php +++ b/admin/tool/lp/edittemplate.php @@ -43,7 +43,6 @@ require_capability('tool/lp:templatemanage', $context); // We keep the original context in the URLs, so that we remain in the same context. $url = new moodle_url("/admin/tool/lp/edittemplate.php", array('id' => $id, 'pagecontextid' => $pagecontextid)); -$formurl = new moodle_url("/admin/tool/lp/edittemplate.php", array('pagecontextid' => $pagecontextid)); if (empty($id)) { $pagetitle = get_string('addnewtemplate', 'tool_lp'); @@ -54,7 +53,7 @@ if (empty($id)) { list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle); } -$form = new \tool_lp\form\template($formurl->out(false), array('template' => $template, 'context' => $context)); +$form = new \tool_lp\form\template($url->out(false), array('persistent' => $template, 'context' => $context)); if ($form->is_cancelled()) { redirect($returnurl); } @@ -68,19 +67,12 @@ if (!empty($subtitle)) { $data = $form->get_data(); if ($data) { - // Save the changes and continue back to the manage page. - // Massage the editor data. - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; + require_sesskey(); if (empty($data->id)) { - // Create new template. - require_sesskey(); - $data->contextid = $context->id; \tool_lp\api::create_template($data); echo $output->notification(get_string('templatecreated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($returnurl); } else { - require_sesskey(); \tool_lp\api::update_template($data); echo $output->notification(get_string('templateupdated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($returnurl); diff --git a/admin/tool/lp/user_evidence_edit.php b/admin/tool/lp/user_evidence_edit.php index ec68c7978b0..7890444bb60 100644 --- a/admin/tool/lp/user_evidence_edit.php +++ b/admin/tool/lp/user_evidence_edit.php @@ -59,9 +59,8 @@ $context = $PAGE->context; $fileareaoptions = array('subdirs' => false); $customdata = array( - 'id' => $id, 'fileareaoptions' => $fileareaoptions, - 'userevidence' => $userevidence, + 'persistent' => $userevidence, 'userid' => $userid, ); @@ -85,12 +84,6 @@ if ($form->is_cancelled()) { $itemid = null; if ($userevidence) { $itemid = $userevidence->get_id(); - $data = $userevidence->to_record(); - $data->description = (object) array( - 'format' => $data->descriptionformat, - 'text' => $data->description - ); - $form->set_data($data); } // Massaging the file API. @@ -109,10 +102,6 @@ if (!empty($subtitle)) { // Hurray, the user has submitted the form! Everyone loves forms :)! if ($data = $form->get_data()) { require_sesskey(); - $data->descriptionformat = $data->description['format']; - $data->description = $data->description['text']; - $data->userid = $userid; - $draftitemid = $data->files; unset($data->files); @@ -121,7 +110,6 @@ if ($data = $form->get_data()) { echo $output->notification(get_string('userevidencecreated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($returnurl); } else { - $data->id = $userevidence->get_id(); \tool_lp\api::update_user_evidence($data, $draftitemid); echo $output->notification(get_string('userevidenceupdated', 'tool_lp'), 'notifysuccess'); echo $output->continue_button($returnurl);