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

Course Modules with name of '0' break legacy cron task runner



    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 2.8.6
    • Fix Version/s: None
    • Component/s: Administration
    • Labels:
    • Affected Branches:


      We noticed that some aspects of our cron tasks were not running. Looking through the cron log we saw that when the lagacy cron runner ran it was failing with an "Invalid course module ID" error. After doing some tracing I narrowed the error down to happening at the update_calendar line in the block below:

      Line 1863 mod/assign/locallib.php

      foreach ($newlyavailable as $record) {
          $cm = get_coursemodule_from_instance('assign', $record->id, 0, false, MUST_EXIST);
          $context = context_module::instance($cm->id);
          $assignment = new assign($context, null, null);
          $assignment->update_calendar($cm->id); // <-- beginning of error train

      Inside that update_calendar method Moodle is trying to fetch a course_module record for the passed in course module ID. Despite having a record in the DB for some reason the code was claiming that the course module was invalid. Looking at the record I noticed that the associated name for the course module was "0"(zero).

      It looks like the course_modinfo constructor ignores modules if their name fails the PHP empty test:

      course_modinfo constructor around line 492

      // Loop through each piece of module data, constructing it
      static $modexists = array();
      foreach ($coursemodinfo->modinfo as $mod) {
          if (empty($mod->name)) {
              // something is wrong here

      When we changed the name of the module to something else the problem went away. Unfortunately this more or less kills some aspects of our site since once that cron task fails none of the rest of that cron task runs. We can try to advise our users to not name stuff "0" but that is not a silver bullet.

      I think Moodle should really be a tad more robust in this specific instance so that cron tasks for the whole site are not brought to a halt due to an unfortunate naming scheme.

      There are probably a wide array of approaches for addressing this, but I think that maybe the course_modinfo constructor should maybe be checking for empty strings (or even false) instead of using the PHP empty check.


          Issue Links



              Unassigned Unassigned
              hernan43 Ray Hernandez
              Component watchers:
              Andrew Lyons, Dongsheng Cai, Huong Nguyen, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze
              2 Vote for this issue
              3 Start watching this issue