2023-10-18 08:46:44 +02:00
|
|
|
<?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-21 23:24:25 +02:00
|
|
|
use moodle_exception;
|
2023-10-20 14:03:13 +02:00
|
|
|
use stdClass;
|
|
|
|
|
2023-10-19 17:35:51 +02:00
|
|
|
/**
|
|
|
|
* 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-19 17:35:51 +02:00
|
|
|
/**
|
2023-10-20 14:03:13 +02:00
|
|
|
* Translate parameters into customdata.
|
2023-10-19 17:35:51 +02:00
|
|
|
*
|
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;
|
2023-10-20 15:08:54 +02:00
|
|
|
$customdata->plan = studyplan::find_by_id($params->studyplan_id);
|
2023-10-20 14:03:13 +02:00
|
|
|
$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
|
2023-10-19 17:35:51 +02:00
|
|
|
* @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) {
|
2023-10-21 23:24:25 +02:00
|
|
|
/*webservicehelper::require_capabilities(self::CAP_EDIT,$customdata->context); */
|
2023-10-19 17:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2023-10-19 17:35:51 +02:00
|
|
|
* @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',
|
2023-10-21 23:24:25 +02:00
|
|
|
$customdata->plan->id());
|
|
|
|
|
2023-10-20 14:03:13 +02:00
|
|
|
return $entry;
|
2023-10-19 17:35:51 +02:00
|
|
|
}
|
2023-10-18 08:46:44 +02:00
|
|
|
|
2023-10-19 17:35:51 +02:00
|
|
|
/**
|
|
|
|
* Set up the form definition
|
|
|
|
*/
|
2023-10-18 08:46:44 +02:00
|
|
|
public function definition() {
|
|
|
|
$mform = $this->_form;
|
2023-10-20 14:03:13 +02:00
|
|
|
$customdata = (object)$this->_customdata;
|
2023-10-19 17:35:51 +02:00
|
|
|
// 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-18 08:46:44 +02:00
|
|
|
}
|
2023-10-19 17:35:51 +02:00
|
|
|
|
|
|
|
/**
|
2023-10-20 14:03:13 +02:00
|
|
|
* Process the submitted data and perform necessary actions
|
|
|
|
* @param object $entry The processed form data;
|
2023-10-21 23:24:25 +02:00
|
|
|
* @return bool false if submission not successful
|
2023-10-19 17:35:51 +02:00
|
|
|
* @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',
|
2023-10-21 23:24:25 +02:00
|
|
|
$customdata->plan->id());
|
|
|
|
|
|
|
|
$f = fopen("/tmp/debug","a+");
|
|
|
|
fputs($f,print_r($entry,true));
|
|
|
|
fclose($f);
|
2023-10-19 17:35:51 +02:00
|
|
|
|
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]);
|
|
|
|
|
2023-10-21 23:24:25 +02:00
|
|
|
|
|
|
|
return true;
|
2023-10-19 17:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-18 08:46:44 +02:00
|
|
|
}
|