Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-69461

Trigger course viewed event before any output in course/view.php

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Icon: Minor Minor
    • None
    • 3.8.4
    • Course, Events API
    • None
    • MOODLE_38_STABLE

      I think it would be nice to trigger the course_viewed event before any output on the course/view.php page. This would make it possible to write an observer to catch that event and redirect the user to an intermediate page before being sent to course/view.php.

      Currently, there is a warning "You should really redirect before you start page output" if you try to do a page redirect from an observer that catches the course_viewed event.

      I propose moving the call to the course_view function before the call to "echo $OUTPUT->header();". Here is a patch as an example (Moodle 3.8):

      diff --git a/course/view.php b/course/view.php
      index 6d65bc9c59..2cec4a9e5c 100644
      --- a/course/view.php
      +++ b/course/view.php
      @@ -241,6 +241,12 @@
           }
       
           $PAGE->set_heading($course->fullname);
      +
      +    // Trigger course viewed event.
      +    // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
      +    // anything after that point.
      +    course_view(context_course::instance($course->id), $section);
      +
           echo $OUTPUT->header();
       
           if ($USER->editing == 1 && !empty($CFG->enableasyncbackup)) {
      @@ -292,10 +298,6 @@
       
           echo html_writer::end_tag('div');
       
      -    // Trigger course viewed event.
      -    // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
      -    // anything after that point.
      -    course_view(context_course::instance($course->id), $section);
       
           // Include course AJAX
           include_course_ajax($course, $modnamesused);
      

            Unassigned Unassigned
            jamaur Jason Maur
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.