plan = studyplan::find_by_id($params->studyplan_id); $customdata->context = $customdata->plan->context(); $customdata->editoroptions = [ 'trusttext' => true, 'subdirs' => true, 'maxfiles' => 20, 'maxbytes' => 20*1024*1024, 'context' => \context_system::instance(), ]; return $customdata; } /** * Validate security access for this form based on the customdata generated by init_customdata * Return true if validation passes, false or throw an exception if it does not. * * @param object $customdata The customdata for this form * @return bool True if security validation passes. * @throws \moodle_exception if access denied for a specific reason. */ public static function check_security(object $customdata) { webservicehelper::require_capabilities(self::CAP_EDIT,$customdata->context); } /** * Generate form data from parameters * Also validate parameters and access permissions here * * @param object $customdata The parameters for form initialization * @return array Form data based on parameters */ public function init_formdata(object $customdata) { global $DB; /* Use direct database retrieval to avoid our abstractions causing trouble with existing moodle code assumptions. The form API does seem needlessly convoluted in it's use, but we need the editor... */ $entry = $DB->get_record(studyplan::TABLE, ['id' => $customdata->plan->id()]); $entry = file_prepare_standard_editor( $entry, 'description', $customdata->editoroptions, \context_system::instance(), 'local_treestudyplan', 'studyplan', $entry->id); return $entry; } /** * Set up the form definition */ public function definition() { $mform = $this->_form; $customdata = (object)$this->_customdata; // Define the form $mform->addElement('editor', 'description_editor', get_string('studyplan_description', 'local_treestudyplan'), null, $customdata->editoroptions); $mform->setType('description_editor', PARAM_RAW); } /** * Process the submitted data and perform necessary actions * @param object $entry The processed form data; * @return bool True if submission successful * @throws \moodle_exception if an error must be given for a specific reason. */ protected function process_submitted_data($entry) { $customdata = (object)$this->_customdata; // Process the provided files in the description editor $entry = file_postupdate_standard_editor($entry, 'description', $customdata->editoroptions, \context_system::instance(), 'local_treestudyplan', 'studyplan', $entry->id); // Use our own abstraction to update the record, so caches are maintained $customdata->plan->edit(['description' => $entry->description, 'descriptionformat' => $entry->descriptionformat]); } }