public function test_course_integrity_check() { global $DB; $this->resetAfterTest(true); $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true)); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), array('section' => 0)); $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id), array('section' => 0)); $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id), array('section' => 0)); $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid)); $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($correctseq, $section0->sequence); $this->assertEmpty($section1->sequence); $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section0->id, $cms[$page->cmid]->section); $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); $this->assertEmpty(course_integrity_check($course->id)); // Now let's make manual change in DB and let course_integrity_check() fix it: // 1. Module appears twice in one section. $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence . ',' . $page->cmid)); $this->assertEquals(array('Failed integrity check for course [' . $course->id . ']. Sequence for course section [' . $section0->id . '] is "' . $section0->sequence . ',' . $page->cmid . '", must be "' . $section0->sequence . '"'), course_integrity_check($course->id)); $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($correctseq, $section0->sequence); $this->assertEmpty($section1->sequence); $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section0->id, $cms[$page->cmid]->section); $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); // 2. Module appears in two sections (last section wins). $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => '' . $page->cmid)); // First message about double mentioning in sequence, second message about wrong section field for $page. $this->assertEquals(array('Failed integrity check for course [' . $course->id . ']. Course module [' . $page->cmid . '] must be removed from sequence of section [' . $section0->id . '] because it is also present in sequence of section [' . $section1->id . ']', 'Failed integrity check for course [' . $course->id . ']. Course module [' . $page->cmid . '] points to section [' . $section0->id . '] instead of [' . $section1->id . ']'), course_integrity_check($course->id)); $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($forum->cmid . ',' . $quiz->cmid, $section0->sequence); $this->assertEquals('' . $page->cmid, $section1->sequence); $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section1->id, $cms[$page->cmid]->section); $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false). $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => '')); $this->assertEmpty(course_integrity_check($course->id)); // Not an error! $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($forum->cmid . ',' . $quiz->cmid, $section0->sequence); $this->assertEmpty($section1->sequence); $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed. $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true). $this->assertEquals(array('Failed integrity check for course [' . $course->id . ']. Course module [' . $page->cmid . '] is missing from sequence of section [' . $section1->id . ']'), course_integrity_check($course->id, null, null, true)); // Error! $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($forum->cmid . ',' . $quiz->cmid, $section0->sequence); $this->assertEquals('' . $page->cmid, $section1->sequence); // Yay, module added to section. $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed. $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true). $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765)); $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => '')); $this->assertEquals(array('Failed integrity check for course [' . $course->id . ']. Course module [' . $page->cmid . '] is missing from sequence of section [' . $section0->id . ']', 'Failed integrity check for course [' . $course->id . ']. Course module [' . $page->cmid . '] points to section [8765] instead of [' . $section0->id . ']'), course_integrity_check($course->id, null, null, true)); $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($forum->cmid . ',' . $quiz->cmid . ',' . $page->cmid, $section0->sequence); // Module added to section. $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0. $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true). $DB->delete_records('course_modules', array('id' => $page->cmid)); $this->assertEquals(array('Failed integrity check for course [' . $course->id . ']. Course module [' . $page->cmid . '] does not exist but is present in the sequence of section [' . $section0->id . ']'), course_integrity_check($course->id, null, null, true)); $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0)); $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1)); $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section'); $this->assertEquals($forum->cmid . ',' . $quiz->cmid, $section0->sequence); $this->assertEmpty($section1->sequence); $this->assertEquals($section0->id, $cms[$forum->cmid]->section); $this->assertEquals($section0->id, $cms[$quiz->cmid]->section); $this->assertEquals(2, count($cms)); }
/** * For a given course, returns an array of course activity objects * Each item in the array contains he following properties: */ function get_array_of_activities($courseid) { // cm - course module id // mod - name of the module (eg forum) // section - the number of the section (eg week or topic) // name - the name of the instance // visible - is the instance visible or not // groupingid - grouping id // groupmembersonly - is this instance visible to group members only // extra - contains extra string to include in any link global $CFG, $DB; if (!empty($CFG->enableavailability)) { require_once $CFG->libdir . '/conditionlib.php'; } $course = $DB->get_record('course', array('id' => $courseid)); if (empty($course)) { throw new moodle_exception('courseidnotfound'); } $mod = array(); $rawmods = get_course_mods($courseid); if (empty($rawmods)) { return $mod; // always return array } if ($sections = $DB->get_records('course_sections', array('course' => $courseid), 'section ASC', 'id,section,sequence')) { // First check and correct obvious mismatches between course_sections.sequence and course_modules.section. if ($errormessages = course_integrity_check($courseid, $rawmods, $sections)) { debugging(join('<br>', $errormessages)); $rawmods = get_course_mods($courseid); $sections = $DB->get_records('course_sections', array('course' => $courseid), 'section ASC', 'id,section,sequence'); } // Build array of activities. foreach ($sections as $section) { if (!empty($section->sequence)) { $sequence = explode(",", $section->sequence); foreach ($sequence as $seq) { if (empty($rawmods[$seq])) { continue; } $mod[$seq] = new stdClass(); $mod[$seq]->id = $rawmods[$seq]->instance; $mod[$seq]->cm = $rawmods[$seq]->id; $mod[$seq]->mod = $rawmods[$seq]->modname; // Oh dear. Inconsistent names left here for backward compatibility. $mod[$seq]->section = $section->section; $mod[$seq]->sectionid = $rawmods[$seq]->section; $mod[$seq]->module = $rawmods[$seq]->module; $mod[$seq]->added = $rawmods[$seq]->added; $mod[$seq]->score = $rawmods[$seq]->score; $mod[$seq]->idnumber = $rawmods[$seq]->idnumber; $mod[$seq]->visible = $rawmods[$seq]->visible; $mod[$seq]->visibleold = $rawmods[$seq]->visibleold; $mod[$seq]->groupmode = $rawmods[$seq]->groupmode; $mod[$seq]->groupingid = $rawmods[$seq]->groupingid; $mod[$seq]->groupmembersonly = $rawmods[$seq]->groupmembersonly; $mod[$seq]->indent = $rawmods[$seq]->indent; $mod[$seq]->completion = $rawmods[$seq]->completion; $mod[$seq]->extra = ""; $mod[$seq]->completiongradeitemnumber = $rawmods[$seq]->completiongradeitemnumber; $mod[$seq]->completionview = $rawmods[$seq]->completionview; $mod[$seq]->completionexpected = $rawmods[$seq]->completionexpected; $mod[$seq]->availablefrom = $rawmods[$seq]->availablefrom; $mod[$seq]->availableuntil = $rawmods[$seq]->availableuntil; $mod[$seq]->showavailability = $rawmods[$seq]->showavailability; $mod[$seq]->showdescription = $rawmods[$seq]->showdescription; if (!empty($CFG->enableavailability)) { condition_info::fill_availability_conditions($rawmods[$seq]); $mod[$seq]->conditionscompletion = $rawmods[$seq]->conditionscompletion; $mod[$seq]->conditionsgrade = $rawmods[$seq]->conditionsgrade; $mod[$seq]->conditionsfield = $rawmods[$seq]->conditionsfield; } $modname = $mod[$seq]->mod; $functionname = $modname . "_get_coursemodule_info"; if (!file_exists("{$CFG->dirroot}/mod/{$modname}/lib.php")) { continue; } include_once "{$CFG->dirroot}/mod/{$modname}/lib.php"; if ($hasfunction = function_exists($functionname)) { if ($info = $functionname($rawmods[$seq])) { if (!empty($info->icon)) { $mod[$seq]->icon = $info->icon; } if (!empty($info->iconcomponent)) { $mod[$seq]->iconcomponent = $info->iconcomponent; } if (!empty($info->name)) { $mod[$seq]->name = $info->name; } if ($info instanceof cached_cm_info) { // When using cached_cm_info you can include three new fields // that aren't available for legacy code if (!empty($info->content)) { $mod[$seq]->content = $info->content; } if (!empty($info->extraclasses)) { $mod[$seq]->extraclasses = $info->extraclasses; } if (!empty($info->iconurl)) { // Convert URL to string as it's easier to store. Also serialized object contains \0 byte and can not be written to Postgres DB. $url = new moodle_url($info->iconurl); $mod[$seq]->iconurl = $url->out(false); } if (!empty($info->onclick)) { $mod[$seq]->onclick = $info->onclick; } if (!empty($info->customdata)) { $mod[$seq]->customdata = $info->customdata; } } else { // When using a stdclass, the (horrible) deprecated ->extra field // is available for BC if (!empty($info->extra)) { $mod[$seq]->extra = $info->extra; } } } } // When there is no modname_get_coursemodule_info function, // but showdescriptions is enabled, then we use the 'intro' // and 'introformat' fields in the module table if (!$hasfunction && $rawmods[$seq]->showdescription) { if ($modvalues = $DB->get_record($rawmods[$seq]->modname, array('id' => $rawmods[$seq]->instance), 'name, intro, introformat')) { // Set content from intro and introformat. Filters are disabled // because we filter it with format_text at display time $mod[$seq]->content = format_module_intro($rawmods[$seq]->modname, $modvalues, $rawmods[$seq]->id, false); // To save making another query just below, put name in here $mod[$seq]->name = $modvalues->name; } } if (!isset($mod[$seq]->name)) { $mod[$seq]->name = $DB->get_field($rawmods[$seq]->modname, "name", array("id" => $rawmods[$seq]->instance)); } // Minimise the database size by unsetting default options when they are // 'empty'. This list corresponds to code in the cm_info constructor. foreach (array('idnumber', 'groupmode', 'groupingid', 'groupmembersonly', 'indent', 'completion', 'extra', 'extraclasses', 'iconurl', 'onclick', 'content', 'icon', 'iconcomponent', 'customdata', 'showavailability', 'availablefrom', 'availableuntil', 'conditionscompletion', 'conditionsgrade', 'completionview', 'completionexpected', 'score', 'showdescription') as $property) { if (property_exists($mod[$seq], $property) && empty($mod[$seq]->{$property})) { unset($mod[$seq]->{$property}); } } // Special case: this value is usually set to null, but may be 0 if (property_exists($mod[$seq], 'completiongradeitemnumber') && is_null($mod[$seq]->completiongradeitemnumber)) { unset($mod[$seq]->completiongradeitemnumber); } } } } } return $mod; }
$where = ''; $params = array(); } else { list($sql, $params) = $DB->get_in_or_equal($courseslist, SQL_PARAMS_NAMED, 'id'); $where = 'WHERE id ' . $sql; } $coursescount = $DB->get_field_sql('SELECT count(id) FROM {course} ' . $where, $params); if (!$coursescount) { cli_error('No courses found'); } echo "Checking {$coursescount} courses...\n\n"; require_once $CFG->dirroot . '/course/lib.php'; $problems = array(); $courses = $DB->get_fieldset_sql('SELECT id FROM {course} ' . $where, $params); foreach ($courses as $courseid) { $errors = course_integrity_check($courseid, null, null, true, empty($options['fix'])); if ($errors) { if (!empty($options['fix'])) { // Reset the course cache to make sure cache is recalculated next time the course is viewed. rebuild_course_cache($courseid, true); } foreach ($errors as $error) { cli_problem($error); } $problems[] = $courseid; } else { echo "Course [{$courseid}] is OK\n"; } } if (!count($problems)) { echo "\n...All courses are OK\n";