moodle_local_treestudyplan/classes/local/forms/studyplan_editform.php

113 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace local_treestudyplan\local\forms;
2023-10-19 17:48:43 +02:00
use local_treestudyplan\studyplan;
use local_treestudyplan\local\helpers\webservicehelper;
2023-10-20 14:03:13 +02:00
use stdClass;
/**
* Moodleform class for the studyplan editor. A Moodleform is used here to facilitate a rich editor
* in the studyplan description
*/
class studyplan_editform extends formbase {
2023-10-19 17:48:43 +02:00
/**
* Capability required to edit study plans
* @var string
*/
const CAP_EDIT = "local/treestudyplan:editstudyplan";
2023-10-20 14:03:13 +02:00
/**
2023-10-20 14:03:13 +02:00
* Translate parameters into customdata.
*
2023-10-19 17:48:43 +02:00
* @param object $params The parameters for form initialization
2023-10-20 14:03:13 +02:00
* @return array Form data based on parameters
*/
public static function init_customdata(object $params) {
$customdata = new stdClass;
$customdata->plan = studyplan::find_by_id($params->studyplanid);
$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.
*/
2023-10-20 14:03:13 +02:00
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
*
2023-10-20 14:03:13 +02:00
* @param object $customdata The parameters for form initialization
* @return array Form data based on parameters
*/
2023-10-20 14:03:13 +02:00
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;
2023-10-20 14:03:13 +02:00
$customdata = (object)$this->_customdata;
// Define the form
2023-10-20 14:03:13 +02:00
$mform->addElement('editor', 'description_editor',
get_string('studyplan_description', 'local_treestudyplan'),
null,
$customdata->editoroptions);
$mform->setType('description_editor', PARAM_RAW);
}
/**
2023-10-20 14:03:13 +02:00
* 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.
*/
2023-10-20 14:03:13 +02:00
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);
2023-10-20 14:03:13 +02:00
// Use our own abstraction to update the record, so caches are maintained
$customdata->plan->edit(['description' => $entry->description,
'descriptionformat' => $entry->descriptionformat]);
}
}