예제 #1
0
 public function after_restore()
 {
     // Fix grade item's sortorder after restore, as it might have duplicates.
     $courseid = $this->get_task()->get_courseid();
     grade_item::fix_duplicate_sortorder($courseid);
 }
예제 #2
0
 /**
  * Test the {@link grade_item::fix_duplicate_sortorder() function with
  * faked duplicate sortorder data.
  */
 public function sub_test_grade_item_fix_sortorder()
 {
     global $DB;
     $this->resetAfterTest(true);
     // Each set is used for filling the db with fake data and will be representing the result of query:
     // "SELECT sortorder from {grade_items} WHERE courseid=? ORDER BY id".
     $testsets = array(array(1, 2, 3), array(5, 6, 7), array(7, 6, 1, 3, 2, 5), array(1, 2, 2, 3, 3, 4, 5), array(1, 1), array(3, 3), array(3, 3, 7, 5, 6, 6, 9, 10, 8, 3), array(7, 7, 3), array(3, 4, 5, 3, 5, 4, 7, 1));
     $origsequence = array();
     // Generate the data and remember the initial sequence or items.
     foreach ($testsets as $testset) {
         $course = $this->getDataGenerator()->create_course();
         foreach ($testset as $sortorder) {
             $this->insert_fake_grade_item_sortorder($course->id, $sortorder);
         }
         $DB->get_records('grade_items');
         $origsequence[$course->id] = $DB->get_fieldset_sql("SELECT id FROM {grade_items} " . "WHERE courseid = ? ORDER BY sortorder, id", array($course->id));
     }
     $duplicatedetectionsql = "SELECT courseid, sortorder\n                                    FROM {grade_items}\n                                WHERE courseid = :courseid\n                                GROUP BY courseid, sortorder\n                                  HAVING COUNT(id) > 1";
     // Do the work.
     foreach ($origsequence as $courseid => $ignore) {
         grade_item::fix_duplicate_sortorder($courseid);
         // Verify that no duplicates are left in the database.
         $dupes = $DB->record_exists_sql($duplicatedetectionsql, array('courseid' => $courseid));
         $this->assertFalse($dupes);
     }
     // Verify that sequences are exactly the same as they were before upgrade script.
     $idx = 0;
     foreach ($origsequence as $courseid => $sequence) {
         if (count($testsets[$idx]) == count(array_unique($testsets[$idx]))) {
             // If there were no duplicates for this course verify that sortorders are not modified.
             $newsortorders = $DB->get_fieldset_sql("SELECT sortorder from {grade_items} WHERE courseid=? ORDER BY id", array($courseid));
             $this->assertEquals($testsets[$idx], $newsortorders);
         }
         $newsequence = $DB->get_fieldset_sql("SELECT id FROM {grade_items} " . "WHERE courseid = ? ORDER BY sortorder, id", array($courseid));
         $this->assertEquals($sequence, $newsequence, "Sequences do not match for test set {$idx} : " . join(',', $testsets[$idx]));
         $idx++;
     }
 }