Merge branch 'MDL-59919-master' of git://github.com/ryanwyllie/moodle

This commit is contained in:
Jun Pataleta 2017-09-12 14:58:06 +08:00
commit 63b384d02a
6 changed files with 100 additions and 14 deletions

View file

@ -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;

View file

@ -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
*/
@ -42,6 +60,7 @@ class create extends \moodleform {
$mform = $this->_form;
$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)) {
@ -70,7 +89,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');

View file

@ -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;
}

View file

@ -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);

View file

@ -3452,20 +3452,25 @@ 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;
$courseid = isset($args['courseid']) ? clean_param($args['courseid'], 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 ($starttime) {
@ -3490,8 +3495,22 @@ function calendar_output_fragment_event_form($args) {
$mform->set_data($data);
} 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,
@ -3501,13 +3520,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.
@ -3522,6 +3534,10 @@ function calendar_output_fragment_event_form($args) {
}
}
if ($hasformdata) {
$mform->is_validated();
}
$html .= $mform->render();
return $html;
}

View file

@ -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' => [