Skip to content

Commit

Permalink
MDL-68645 output: Do not apply filters when creating calendar events
Browse files Browse the repository at this point in the history
Applying filters on an activity module description when using it as a
new calendar event's description is bad m'kay? We need to store the raw
text and apply the filters only when we actually display the text. That
way, filters (such as multi-language content) may actually fully work
and we do not initialise the theme and output machinery.

Additionally, we need to explicitly set the format of the description
text to HTML (because we have converted it to it already). Otherwise it
defaults to the current user's preferred editor format.

This is still a pragmatic hot-fix solution. The proper solution would be
to pass the raw text, format and embedded files.
  • Loading branch information
mudrd8mz committed May 13, 2020
1 parent f883c68 commit 005b00d
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 24 deletions.
6 changes: 4 additions & 2 deletions completion/classes/api.php
Expand Up @@ -87,7 +87,8 @@ public static function update_completion_date_event($cmid, $modulename, $instanc
if ($completionexpectedtime !== null) {
// Calendar event exists so update it.
$event->name = get_string('completionexpectedfor', 'completion', $lang);
$event->description = format_module_intro($modulename, $instance, $cmid);
$event->description = format_module_intro($modulename, $instance, $cmid, false);
$event->format = FORMAT_HTML;
$event->timestart = $completionexpectedtime;
$event->timesort = $completionexpectedtime;
$event->visible = instance_is_visible($modulename, $instance);
Expand All @@ -104,7 +105,8 @@ public static function update_completion_date_event($cmid, $modulename, $instanc
// Event doesn't exist so create one.
if ($completionexpectedtime !== null) {
$event->name = get_string('completionexpectedfor', 'completion', $lang);
$event->description = format_module_intro($modulename, $instance, $cmid);
$event->description = format_module_intro($modulename, $instance, $cmid, false);
$event->format = FORMAT_HTML;
$event->courseid = $instance->course;
$event->groupid = 0;
$event->userid = 0;
Expand Down
3 changes: 2 additions & 1 deletion mod/assign/lib.php
Expand Up @@ -297,7 +297,8 @@ function assign_update_events($assign, $override = null) {

$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->description = format_module_intro('assign', $assigninstance, $cmid);
$event->description = format_module_intro('assign', $assigninstance, $cmid, false);
$event->format = FORMAT_HTML;
// Events module won't show user events when the courseid is nonzero.
$event->courseid = ($userid) ? 0 : $assigninstance->course;
$event->groupid = $groupid;
Expand Down
12 changes: 8 additions & 4 deletions mod/chat/lib.php
Expand Up @@ -123,7 +123,8 @@ function chat_add_instance($chat) {
$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->name = $chat->name;
$event->description = format_module_intro('chat', $chat, $chat->coursemodule);
$event->description = format_module_intro('chat', $chat, $chat->coursemodule, false);
$event->format = FORMAT_HTML;
$event->courseid = $chat->course;
$event->groupid = 0;
$event->userid = 0;
Expand Down Expand Up @@ -169,7 +170,8 @@ function chat_update_instance($chat) {
if ($chat->schedule > 0) {
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->name = $chat->name;
$event->description = format_module_intro('chat', $chat, $chat->coursemodule);
$event->description = format_module_intro('chat', $chat, $chat->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $chat->chattime;
$event->timesort = $chat->chattime;

Expand All @@ -186,7 +188,8 @@ function chat_update_instance($chat) {
$event = new stdClass();
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->name = $chat->name;
$event->description = format_module_intro('chat', $chat, $chat->coursemodule);
$event->description = format_module_intro('chat', $chat, $chat->coursemodule, false);
$event->format = FORMAT_HTML;
$event->courseid = $chat->course;
$event->groupid = 0;
$event->userid = 0;
Expand Down Expand Up @@ -460,7 +463,8 @@ function chat_prepare_update_events($chat, $cm = null) {
$event = new stdClass();
$event->name = $chat->name;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->description = format_module_intro('chat', $chat, $cm->id);
$event->description = format_module_intro('chat', $chat, $cm->id, false);
$event->format = FORMAT_HTML;
$event->timestart = $chat->chattime;
$event->timesort = $chat->chattime;
if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id,
Expand Down
12 changes: 8 additions & 4 deletions mod/choice/locallib.php
Expand Up @@ -52,7 +52,8 @@ function choice_set_events($choice) {
if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) {
// Calendar event exists so update it.
$event->name = get_string('calendarstart', 'choice', $choice->name);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $choice->timeopen;
$event->timesort = $choice->timeopen;
$event->visible = instance_is_visible('choice', $choice);
Expand All @@ -68,7 +69,8 @@ function choice_set_events($choice) {
// Event doesn't exist so create one.
if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) {
$event->name = get_string('calendarstart', 'choice', $choice->name);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
$event->format = FORMAT_HTML;
$event->courseid = $choice->course;
$event->groupid = 0;
$event->userid = 0;
Expand All @@ -91,7 +93,8 @@ function choice_set_events($choice) {
if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
// Calendar event exists so update it.
$event->name = get_string('calendarend', 'choice', $choice->name);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $choice->timeclose;
$event->timesort = $choice->timeclose;
$event->visible = instance_is_visible('choice', $choice);
Expand All @@ -107,7 +110,8 @@ function choice_set_events($choice) {
// Event doesn't exist so create one.
if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
$event->name = get_string('calendarend', 'choice', $choice->name);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule);
$event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
$event->format = FORMAT_HTML;
$event->courseid = $choice->course;
$event->groupid = 0;
$event->userid = 0;
Expand Down
12 changes: 8 additions & 4 deletions mod/data/locallib.php
Expand Up @@ -607,7 +607,8 @@ function data_set_events($data) {
if ($data->timeavailablefrom > 0) {
// Calendar event exists so update it.
$event->name = get_string('calendarstart', 'data', $data->name);
$event->description = format_module_intro('data', $data, $data->coursemodule);
$event->description = format_module_intro('data', $data, $data->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $data->timeavailablefrom;
$event->timesort = $data->timeavailablefrom;
$event->visible = instance_is_visible('data', $data);
Expand All @@ -623,7 +624,8 @@ function data_set_events($data) {
// Event doesn't exist so create one.
if (isset($data->timeavailablefrom) && $data->timeavailablefrom > 0) {
$event->name = get_string('calendarstart', 'data', $data->name);
$event->description = format_module_intro('data', $data, $data->coursemodule);
$event->description = format_module_intro('data', $data, $data->coursemodule, false);
$event->format = FORMAT_HTML;
$event->courseid = $data->course;
$event->groupid = 0;
$event->userid = 0;
Expand All @@ -646,7 +648,8 @@ function data_set_events($data) {
if ($data->timeavailableto > 0) {
// Calendar event exists so update it.
$event->name = get_string('calendarend', 'data', $data->name);
$event->description = format_module_intro('data', $data, $data->coursemodule);
$event->description = format_module_intro('data', $data, $data->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $data->timeavailableto;
$event->timesort = $data->timeavailableto;
$event->visible = instance_is_visible('data', $data);
Expand All @@ -662,7 +665,8 @@ function data_set_events($data) {
// Event doesn't exist so create one.
if (isset($data->timeavailableto) && $data->timeavailableto > 0) {
$event->name = get_string('calendarend', 'data', $data->name);
$event->description = format_module_intro('data', $data, $data->coursemodule);
$event->description = format_module_intro('data', $data, $data->coursemodule, false);
$event->format = FORMAT_HTML;
$event->courseid = $data->course;
$event->groupid = 0;
$event->userid = 0;
Expand Down
6 changes: 4 additions & 2 deletions mod/feedback/lib.php
Expand Up @@ -809,7 +809,8 @@ function feedback_set_events($feedback) {
$event->eventtype = FEEDBACK_EVENT_TYPE_OPEN;
$event->type = empty($feedback->timeclose) ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
$event->name = get_string('calendarstart', 'feedback', $feedback->name);
$event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
$event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $feedback->timeopen;
$event->timesort = $feedback->timeopen;
$event->visible = instance_is_visible('feedback', $feedback);
Expand Down Expand Up @@ -844,7 +845,8 @@ function feedback_set_events($feedback) {
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = FEEDBACK_EVENT_TYPE_CLOSE;
$event->name = get_string('calendarend', 'feedback', $feedback->name);
$event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule);
$event->description = format_module_intro('feedback', $feedback, $feedback->coursemodule, false);
$event->format = FORMAT_HTML;
$event->timestart = $feedback->timeclose;
$event->timesort = $feedback->timeclose;
$event->visible = instance_is_visible('feedback', $feedback);
Expand Down
3 changes: 2 additions & 1 deletion mod/forum/locallib.php
Expand Up @@ -723,7 +723,8 @@ function forum_update_calendar($forum, $cmid) {

if (!empty($forum->duedate)) {
$event->name = get_string('calendardue', 'forum', $forum->name);
$event->description = format_module_intro('forum', $forum, $cmid);
$event->description = format_module_intro('forum', $forum, $cmid, false);
$event->format = FORMAT_HTML;
$event->courseid = $forum->course;
$event->modulename = 'forum';
$event->instance = $forum->id;
Expand Down
3 changes: 2 additions & 1 deletion mod/lesson/lib.php
Expand Up @@ -162,7 +162,8 @@ function lesson_update_events($lesson, $override = null) {

$event = new stdClass();
$event->type = !$deadline ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
$event->description = format_module_intro('lesson', $lesson, $cmid);
$event->description = format_module_intro('lesson', $lesson, $cmid, false);
$event->format = FORMAT_HTML;
// Events module won't show user events when the courseid is nonzero.
$event->courseid = ($userid) ? 0 : $lesson->course;
$event->groupid = $groupid;
Expand Down
3 changes: 2 additions & 1 deletion mod/quiz/lib.php
Expand Up @@ -1260,7 +1260,8 @@ function quiz_update_events($quiz, $override = null) {

$event = new stdClass();
$event->type = !$timeclose ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
$event->description = format_module_intro('quiz', $quiz, $cmid);
$event->description = format_module_intro('quiz', $quiz, $cmid, false);
$event->format = FORMAT_HTML;
// Events module won't show user events when the courseid is nonzero.
$event->courseid = ($userid) ? 0 : $quiz->course;
$event->groupid = $groupid;
Expand Down
12 changes: 8 additions & 4 deletions mod/scorm/locallib.php
Expand Up @@ -2424,7 +2424,8 @@ function scorm_update_calendar(stdClass $scorm, $cmid) {
if ((!empty($scorm->timeopen)) && ($scorm->timeopen > 0)) {
// Calendar event exists so update it.
$event->name = get_string('calendarstart', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->description = format_module_intro('scorm', $scorm, $cmid, false);
$event->format = FORMAT_HTML;
$event->timestart = $scorm->timeopen;
$event->timesort = $scorm->timeopen;
$event->visible = instance_is_visible('scorm', $scorm);
Expand All @@ -2441,7 +2442,8 @@ function scorm_update_calendar(stdClass $scorm, $cmid) {
// Event doesn't exist so create one.
if ((!empty($scorm->timeopen)) && ($scorm->timeopen > 0)) {
$event->name = get_string('calendarstart', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->description = format_module_intro('scorm', $scorm, $cmid, false);
$event->format = FORMAT_HTML;
$event->courseid = $scorm->course;
$event->groupid = 0;
$event->userid = 0;
Expand All @@ -2465,7 +2467,8 @@ function scorm_update_calendar(stdClass $scorm, $cmid) {
if ((!empty($scorm->timeclose)) && ($scorm->timeclose > 0)) {
// Calendar event exists so update it.
$event->name = get_string('calendarend', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->description = format_module_intro('scorm', $scorm, $cmid, false);
$event->format = FORMAT_HTML;
$event->timestart = $scorm->timeclose;
$event->timesort = $scorm->timeclose;
$event->visible = instance_is_visible('scorm', $scorm);
Expand All @@ -2482,7 +2485,8 @@ function scorm_update_calendar(stdClass $scorm, $cmid) {
// Event doesn't exist so create one.
if ((!empty($scorm->timeclose)) && ($scorm->timeclose > 0)) {
$event->name = get_string('calendarend', 'scorm', $scorm->name);
$event->description = format_module_intro('scorm', $scorm, $cmid);
$event->description = format_module_intro('scorm', $scorm, $cmid, false);
$event->format = FORMAT_HTML;
$event->courseid = $scorm->course;
$event->groupid = 0;
$event->userid = 0;
Expand Down
21 changes: 21 additions & 0 deletions mod/upgrade.txt
Expand Up @@ -5,6 +5,27 @@ information provided here is intended especially for developers.

* The callback get_shortcuts() is now deprecated. Please use get_course_content_items and get_all_content_items instead.
See source code examples in get_course_content_items() and get_all_content_items() in mod/lti/lib.php for details.
* When creating the calendar events and setting the event description to match the module intro description, the filters
must not be applied on the passed description text. Doing so leads to loosing some expected text filters features and
causes unnecessarily early theme and output initialisation in unit tests. If your activity creates calendar events,
you probably have code like:
```
$event->description = format_module_intro('quiz', $quiz, $cmid);
```
You need to change it to:
```
$event->description = format_module_intro('quiz', $quiz, $cmid, false);
$event->format = FORMAT_HTML;
```
Even this is still technically wrong. Content should normally only be formatted just before it is output. Ideally, we
should pass the raw description text, format and have a way to copy the embedded files; or provide another way for the
calendar to call the right format_text() later. The calendar API does not allow us to do these things easily at the
moment. Therefore, this compromise approach is used. The false parameter added ensures that text filters are not run
at this time which is important. And the format must be set to HTML, because otherwise it would use the current user's
preferred editor default format.
* Related to the above and to help with detecting the problematic places in contributed 3rd party modules, the
testing_module_generator::create_instance() now throws coding_exception if creating a module instance initialised the
theme and output as a side effect.

=== 3.8 ===

Expand Down
1 change: 1 addition & 0 deletions mod/workshop/lib.php
Expand Up @@ -1668,6 +1668,7 @@ function workshop_calendar_update(stdClass $workshop, $cmid) {
// the common properties for all events
$base = new stdClass();
$base->description = format_module_intro('workshop', $workshop, $cmid, false);
$base->format = FORMAT_HTML;
$base->courseid = $workshop->course;
$base->groupid = 0;
$base->userid = 0;
Expand Down

0 comments on commit 005b00d

Please sign in to comment.