113 lines
No EOL
4.4 KiB
PHP
113 lines
No EOL
4.4 KiB
PHP
<?php
|
|
|
|
namespace local_treestudyplan\local\forms;
|
|
use local_treestudyplan\studyplan;
|
|
use local_treestudyplan\local\helpers\webservicehelper;
|
|
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 {
|
|
/**
|
|
* Capability required to edit study plans
|
|
* @var string
|
|
*/
|
|
const CAP_EDIT = "local/treestudyplan:editstudyplan";
|
|
|
|
/**
|
|
* Translate parameters into customdata.
|
|
*
|
|
* @param object $params The parameters for form initialization
|
|
* @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.
|
|
*/
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
} |