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

Course_delete_module function deletes module instance which still need to exist for event removal.

    XMLWordPrintable

    Details

    • Testing Instructions:
      Hide
      1. Disable recycle bin
      2. Create lesson, assignment and quiz with individual student overrides of due dates
      3. Delete these modules, there should be no errors

      (this is also covered by a unittest)

      Show
      Disable recycle bin Create lesson, assignment and quiz with individual student overrides of due dates Delete these modules, there should be no errors (this is also covered by a unittest)
    • Affected Branches:
      MOODLE_33_STABLE
    • Fixed Branches:
      MOODLE_32_STABLE, MOODLE_33_STABLE
    • Pull Master Branch:
      wip-MDL-59308-master

      Description

      In the course_delete_module function the module instance gets deleted before all events get removed.

      All events get constructed during the removal process.

       

      // Delete events from calendar.
      if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
          foreach($events as $event) {
              $calendarevent = calendar_event::load($event);
              $calendarevent->delete();
          }
      }
      

      In the calendar_event constructor the context in calculated. In this calculate_context function it may hit the get_course_module_from_instance which require the module instance in the db we deleted before.

      protected function calculate_context() {
          global $USER, $DB;
       
          $context = null;
          if (isset($this->properties->courseid) && $this->properties->courseid > 0) {
              $context = \context_course::instance($this->properties->courseid);
          } else if (isset($this->properties->course) && $this->properties->course > 0) {
              $context = \context_course::instance($this->properties->course);
          } else if (isset($this->properties->groupid) && $this->properties->groupid > 0) {
              $group = $DB->get_record('groups', array('id' => $this->properties->groupid));
              $context = \context_course::instance($group->courseid);
          } else if (isset($this->properties->userid) && $this->properties->userid > 0
              && $this->properties->userid == $USER->id) {
              $context = \context_user::instance($this->properties->userid);
          } else if (isset($this->properties->userid) && $this->properties->userid > 0
              && $this->properties->userid != $USER->id &&
              isset($this->properties->instance) && $this->properties->instance > 0) {
              $cm = get_coursemodule_from_instance($this->properties->modulename, $this->properties->instance, 0,
                  false, MUST_EXIST);
              $context = \context_course::instance($cm->course);
          } else {
              $context = \context_user::instance($this->properties->userid);
          }
       
          return $context;
      }

      My solution to this would be to move the event removal before the module instance  deletion.

       

       

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Fix Release Date:
                  10/Jul/17