Merge branch 'MDL-28949d' of git://github.com/srynot4sale/moodle
authorSam Hemelryk <sam@moodle.com>
Mon, 28 May 2012 20:49:29 +0000 (08:49 +1200)
committerSam Hemelryk <sam@moodle.com>
Mon, 28 May 2012 20:49:29 +0000 (08:49 +1200)
Conflicts:
lib/db/upgrade.php
version.php

1  2 
lib/db/upgrade.php
version.php

@@@ -648,42 -648,75 +648,112 @@@ function xmldb_main_upgrade($oldversion
          upgrade_main_savepoint(true, 2012052100.00);
      }
  
 -    if ($oldversion < 2012052500.02) {
 +    if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
 +        //first get all instances of duplicate records
 +        $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
 +        $duplicates = $DB->get_recordset_sql($sql, array());
 +
 +        foreach ($duplicates as $duplicate) {
 +            $pointer = 0;
 +            //now get all the records for this user/course
 +            $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
 +            $completions = $DB->get_records_select('course_completions', $sql,
 +                array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
 +            $needsupdate = false;
 +            $origcompletion = null;
 +            foreach ($completions as $completion) {
 +                $pointer++;
 +                if ($pointer === 1) { //keep 1st record but delete all others.
 +                    $origcompletion = $completion;
 +                } else {
 +                    //we need to keep the "oldest" of all these fields as the valid completion record.
 +                    $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
 +                    foreach ($fieldstocheck as $f) {
 +                        if ($origcompletion->$f > $completion->$f) {
 +                            $origcompletion->$f = $completion->$f;
 +                            $needsupdate = true;
 +                        }
 +                    }
 +                    $DB->delete_records('course_completions', array('id'=>$completion->id));
 +                }
 +            }
 +            if ($needsupdate) {
 +                $DB->update_record('course_completions', $origcompletion);
 +            }
 +        }
 +
 +        // Main savepoint reached
 +        upgrade_main_savepoint(true, 2012052500.03);
 +    }
+     /**
+      * Major clean up of course completion tables
+      */
 -        upgrade_main_savepoint(true, 2012052500.02);
++    if ($oldversion < 2012052900.00) {
+         // Clean up all instances of duplicate records
+         // Add indexes to prevent new duplicates
+         upgrade_course_completion_remove_duplicates(
+             'course_completions',
+             array('userid', 'course'),
+             array('timecompleted', 'timestarted', 'timeenrolled')
+         );
+         // Define index useridcourse (unique) to be added to course_completions
+         $table = new xmldb_table('course_completions');
+         $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
+         // Conditionally launch add index useridcourse
+         if (!$dbman->index_exists($table, $index)) {
+             $dbman->add_index($table, $index);
+         }
+         // Main savepoint reached
 -    if ($oldversion < 2012052500.03) {
++        upgrade_main_savepoint(true, 2012052900.00);
+     }
 -        upgrade_main_savepoint(true, 2012052500.03);
++    if ($oldversion < 2012052900.01) {
+         upgrade_course_completion_remove_duplicates(
+             'course_completion_crit_compl',
+             array('userid', 'course', 'criteriaid'),
+             array('timecompleted')
+         );
+         // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
+         $table = new xmldb_table('course_completion_crit_compl');
+         $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
+         // Conditionally launch add index useridcoursecriteraid
+         if (!$dbman->index_exists($table, $index)) {
+             $dbman->add_index($table, $index);
+         }
+         // Main savepoint reached
 -    if ($oldversion < 2012052500.04) {
++        upgrade_main_savepoint(true, 2012052900.01);
+     }
 -        upgrade_main_savepoint(true, 2012052500.04);
++    if ($oldversion < 2012052900.02) {
+         upgrade_course_completion_remove_duplicates(
+             'course_completion_aggr_methd',
+             array('course', 'criteriatype')
+         );
+         // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
+         $table = new xmldb_table('course_completion_aggr_methd');
+         $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
+         // Conditionally launch add index coursecriteratype
+         if (!$dbman->index_exists($table, $index)) {
+             $dbman->add_index($table, $index);
+         }
+         // Main savepoint reached
++        upgrade_main_savepoint(true, 2012052900.02);
+     }
      return true;
--}
++}
diff --cc version.php
@@@ -30,7 -30,7 +30,7 @@@
  defined('MOODLE_INTERNAL') || die();
  
  
- $version  = 2012052500.03;              // YYYYMMDD      = weekly release date of this DEV branch
 -$version  = 2012052500.04;              // YYYYMMDD      = weekly release date of this DEV branch
++$version  = 2012052900.02;              // YYYYMMDD      = weekly release date of this DEV branch
                                          //         RR    = release increments - 00 in DEV branches
                                          //           .XX = incremental changes