diff --git a/calendar/classes/external/event_exporter_base.php b/calendar/classes/external/event_exporter_base.php index cc7ef38312c..813cb588cc3 100644 --- a/calendar/classes/external/event_exporter_base.php +++ b/calendar/classes/external/event_exporter_base.php @@ -27,6 +27,7 @@ namespace core_calendar\external; defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . "/calendar/lib.php"); +require_once($CFG->libdir . "/filelib.php"); use \core\external\exporter; use \core_calendar\local\event\container; @@ -66,7 +67,14 @@ class event_exporter_base extends exporter { $data = new \stdClass(); $data->id = $event->get_id(); $data->name = $event->get_name(); - $data->description = $event->get_description()->get_value(); + $data->description = file_rewrite_pluginfile_urls( + $event->get_description()->get_value(), + 'pluginfile.php', + $related['context']->id, + 'calendar', + 'event_description', + $event->get_id() + ); $data->descriptionformat = $event->get_description()->get_format(); $data->groupid = $groupid; $data->userid = $userid; diff --git a/calendar/classes/local/event/forms/create.php b/calendar/classes/local/event/forms/create.php index e6c025f433d..998034df946 100644 --- a/calendar/classes/local/event/forms/create.php +++ b/calendar/classes/local/event/forms/create.php @@ -34,6 +34,24 @@ require_once($CFG->dirroot.'/lib/formslib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class create extends \moodleform { + + /** + * Build the editor options using the given context. + * + * @param \context $context A Moodle context + * @return array + */ + public static function build_editor_options(\context $context) { + global $CFG; + + return [ + 'context' => $context, + 'maxfiles' => EDITOR_UNLIMITED_FILES, + 'maxbytes' => $CFG->maxbytes, + 'noclean' => true + ]; + } + /** * The form definition */ @@ -43,6 +61,7 @@ class create extends \moodleform { $mform = $this->_form; $haserror = !empty($this->_customdata['haserror']); $starttime = isset($this->_customdata['starttime']) ? $this->_customdata['starttime'] : 0; + $editoroptions = !(empty($this->_customdata['editoroptions'])) ? $this->_customdata['editoroptions'] : null; $eventtypes = calendar_get_all_allowed_types(); if (empty($eventtypes)) { @@ -71,7 +90,7 @@ class create extends \moodleform { // Start of advanced elements. // Advanced elements are not visible to the user by default. // They are displayed through the user of a show more / less button. - $mform->addElement('editor', 'description', get_string('eventdescription', 'calendar'), ['rows' => 3]); + $mform->addElement('editor', 'description', get_string('eventdescription', 'calendar'), ['rows' => 3], $editoroptions); $mform->setType('description', PARAM_RAW); $mform->setAdvanced('description'); diff --git a/calendar/classes/local/event/mappers/create_update_form_mapper.php b/calendar/classes/local/event/mappers/create_update_form_mapper.php index b0bb691988a..75a688f3141 100644 --- a/calendar/classes/local/event/mappers/create_update_form_mapper.php +++ b/calendar/classes/local/event/mappers/create_update_form_mapper.php @@ -52,7 +52,7 @@ class create_update_form_mapper implements create_update_form_mapper_interface { */ public function from_legacy_event_to_data(\calendar_event $legacyevent) { $legacyevent->count_repeats(); - $data = $legacyevent->properties(true); + $data = $legacyevent->properties(); $data->timedurationuntil = $legacyevent->timestart + $legacyevent->timeduration; $data->duration = (empty($legacyevent->timeduration)) ? 0 : 1; @@ -62,6 +62,17 @@ class create_update_form_mapper implements create_update_form_mapper_interface { $data->groupcourseid = $legacyevent->courseid; } + $data->description = [ + 'text' => $data->description, + 'format' => $data->format + ]; + + // We don't want to return the context because it's not a + // form value and breaks the validation. + if (isset($data->context)) { + unset($data->context); + } + return $data; } diff --git a/calendar/externallib.php b/calendar/externallib.php index 6000a253a1f..afad7dd85bc 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -779,6 +779,7 @@ class core_calendar_external extends external_api { public static function submit_create_update_form($formdata) { global $CFG, $USER, $PAGE; require_once($CFG->dirroot."/calendar/lib.php"); + require_once($CFG->libdir."/filelib.php"); // Parameter validation. $params = self::validate_parameters(self::submit_create_update_form_parameters(), ['formdata' => $formdata]); @@ -816,6 +817,31 @@ class core_calendar_external extends external_api { } $legacyevent->update($properties); + $eventcontext = $legacyevent->context; + + file_remove_editor_orphaned_files($validateddata->description); + + // Take any files added to the description draft file area and + // convert them into the proper event description file area. Also + // parse the description text and replace the URLs to the draft files + // with the @@PLUGIN_FILE@@ placeholder to be persisted in the DB. + $description = file_save_draft_area_files( + $validateddata->description['itemid'], + $eventcontext->id, + 'calendar', + 'event_description', + $legacyevent->id, + create_event_form::build_editor_options($eventcontext), + $validateddata->description['text'] + ); + + // If draft files were found then we need to save the new + // description value. + if ($description != $validateddata->description['text']) { + $properties->id = $legacyevent->id; + $properties->description = $description; + $legacyevent->update($properties); + } $eventmapper = event_container::get_event_mapper(); $event = $eventmapper->from_legacy_event_to_event($legacyevent); diff --git a/calendar/lib.php b/calendar/lib.php index 3ad809b6b59..a2cdb8f30b4 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -3530,19 +3530,24 @@ function calendar_get_view(\calendar_information $calendar, $view) { * @return string The rendered mform fragment. */ function calendar_output_fragment_event_form($args) { - global $CFG, $OUTPUT; - require_once($CFG->dirroot.'/calendar/event_form.php'); + global $CFG, $OUTPUT, $USER; $html = ''; - $data = null; + $data = []; $eventid = isset($args['eventid']) ? clean_param($args['eventid'], PARAM_INT) : null; $starttime = isset($args['starttime']) ? clean_param($args['starttime'], PARAM_INT) : null; $event = null; $hasformdata = isset($args['formdata']) && !empty($args['formdata']); - $formoptions = []; + $context = \context_user::instance($USER->id); + $editoroptions = \core_calendar\local\event\forms\create::build_editor_options($context); + $formoptions = ['editoroptions' => $editoroptions]; + $draftitemid = 0; if ($hasformdata) { parse_str(clean_param($args['formdata'], PARAM_TEXT), $data); + if (isset($data['description']['itemid'])) { + $draftitemid = $data['description']['itemid']; + } } if (isset($args['haserror'])) { @@ -3565,8 +3570,22 @@ function calendar_output_fragment_event_form($args) { ); } else { $event = calendar_event::load($eventid); + $mapper = new \core_calendar\local\event\mappers\create_update_form_mapper(); + $eventdata = $mapper->from_legacy_event_to_data($event); + $data = array_merge((array) $eventdata, $data); $event->count_repeats(); $formoptions['event'] = $event; + $data['description']['text'] = file_prepare_draft_area( + $draftitemid, + $event->context->id, + 'calendar', + 'event_description', + $event->id, + null, + $data['description']['text'] + ); + $data['description']['itemid'] = $draftitemid; + $mform = new \core_calendar\local\event\forms\update( null, $formoptions, @@ -3576,13 +3595,6 @@ function calendar_output_fragment_event_form($args) { true, $data ); - } - - if ($hasformdata) { - $mform->is_validated(); - } else if (!is_null($event)) { - $mapper = new \core_calendar\local\event\mappers\create_update_form_mapper(); - $data = $mapper->from_legacy_event_to_data($event); $mform->set_data($data); // Check to see if this event is part of a subscription or import. @@ -3597,6 +3609,10 @@ function calendar_output_fragment_event_form($args) { } } + if ($hasformdata) { + $mform->is_validated(); + } + $html .= $mform->render(); return $html; } diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index 4537acfddbd..ea3edfd8ef8 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -1500,6 +1500,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { 'description' => [ 'text' => '', 'format' => 1, + 'itemid' => 0 ], 'duration' => 1, 'timedurationuntil' => [ @@ -1623,6 +1624,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { 'description' => [ 'text' => '', 'format' => 1, + 'itemid' => 0 ], 'duration' => 1, 'timedurationuntil' => [ @@ -1750,6 +1752,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { 'description' => [ 'text' => '', 'format' => 1, + 'itemid' => 0, ], 'duration' => 1, 'timedurationuntil' => [ @@ -1949,6 +1952,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { 'description' => [ 'text' => '', 'format' => 1, + 'itemid' => 0 ], 'duration' => 1, 'timedurationuntil' => [ @@ -2020,6 +2024,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { 'description' => [ 'text' => '', 'format' => 1, + 'itemid' => 0 ], 'duration' => 1, 'timedurationuntil' => [ @@ -2092,6 +2097,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { 'description' => [ 'text' => '', 'format' => 1, + 'itemid' => 0 ], 'duration' => 1, 'timedurationuntil' => [