]> git.mjollnir.org Git - moodle.git/commitdiff
replace MySQL-only indexes on text fields (hotpot_question.name + hotpot_strings...
authorgbateson <gbateson>
Fri, 1 Sep 2006 02:08:39 +0000 (02:08 +0000)
committergbateson <gbateson>
Fri, 1 Sep 2006 02:08:39 +0000 (02:08 +0000)
mod/hotpot/db/mysql.php
mod/hotpot/db/postgres7.php
mod/hotpot/db/update_to_v2.php
mod/hotpot/lib.php
mod/hotpot/restorelib.php
mod/hotpot/version.php

index ab75b1e84f6eea1f7b5ae64b4355a5488bd42f87..551d0a799fbef0ab35946ce58ca5790e41086da3 100644 (file)
@@ -37,6 +37,10 @@ function hotpot_upgrade($oldversion) {
         require_once $update_to_v2;
         $ok = $ok && hotpot_update_to_v2_1_18();
     }
+    if ($oldversion < 2006083101) {
+        require_once $update_to_v2;
+        $ok = $ok && hotpot_update_to_v2_2();
+    }
 
     return $ok;
 }
index e5f80e4f94d0b62c2b6ead3a07eb2b25fa53e7c1..59ee0216101afc3887a6e47523a5e97529c0785c 100644 (file)
@@ -45,6 +45,10 @@ function hotpot_upgrade($oldversion) {
         require_once $update_to_v2;
         $ok = $ok && hotpot_update_to_v2_1_21();
     }
+    if ($oldversion < 2006083101) {
+        require_once $update_to_v2;
+        $ok = $ok && hotpot_update_to_v2_2();
+    }
 
     return $ok;
 }
index b8eb14a854de95f3f4a2b1481dca2996e5ad24dc..0406adf54d1f93defe11271dd5986b2818bfa33e 100644 (file)
@@ -1,4 +1,56 @@
 <?PHP
+function hotpot=update_to_v2_2() {
+    global $CFG;
+    $ok = true;
+
+    // remove the index on hotpot_questions.name
+    $table = 'hotpot_questions';
+    $field = 'name';
+    $index = "{$table}_{$field}_idx";
+    if (strtolower($CFG->dbtype)=='postgres7') {
+        $index = "{$CFG->prefix}$index";
+    }
+    hotpot_db_delete_index("{$CFG->prefix}$table", $index);
+    
+    // add new hotpot_questions.md5key field (and index)
+    $table = 'hotpot_questions';
+    $field = 'md5key';
+    $ok = $ok && hotpot_db_update_field_type($table, '', $field, 'VARCHAR', 32, '', 'NOT NULL', '', 'name');
+    $ok = $ok && hotpot_db_add_index($table, $field);
+
+    // add new values hotpot_questions.md5key
+    $table = 'hotpot_questions';
+    if ($records = get_records($table)) {
+        foreach ($records as $record) {
+            $ok = $ok && set_field($table, 'md5key', md5($record->name), 'id', $record->id);
+        }
+    }
+    
+    // remove the index on hotpot_strings.string
+    $table = 'hotpot_strings';
+    $field = 'string';
+    $index = "{$table}_{$field}_idx";
+    if (strtolower($CFG->dbtype)=='postgres7') {
+        $index = "{$CFG->prefix}$index";
+    }
+    hotpot_db_delete_index("{$CFG->prefix}$table", $index);
+
+    // add new hotpot_strings.md5key field (and index)
+    $table = 'hotpot_strings';
+    $field = 'md5key';
+    $ok = $ok && hotpot_db_update_field_type($table, '', $field, 'VARCHAR', 32, '', 'NOT NULL', '', 'string');
+    $ok = $ok && hotpot_db_add_index($table, $field);
+
+    // add new values hotpot_strings.md5key
+    $table = 'hotpot_strings';
+    if ($records = get_records($table)) {
+        foreach ($records as $record) {
+            $ok = $ok && set_field($table, 'md5key', md5($record->string), 'id', $record->id);
+        }
+    }
+    
+    return $ok;
+}
 function hotpot_update_to_v2_1_21() {
     global $CFG;
     $ok = true;
index 133bd88c363052f52685727235152b973858b2d3..e7c0eb838ecb6effa922f72551eae62f3ecf25d2 100644 (file)
@@ -1962,7 +1962,8 @@ function hotpot_add_response(&$attempt, &$question, &$response) {
             $questionname = $question->name;
         }
 
-        if (!$question->id = get_field('hotpot_questions', 'id', 'name', $question->name, 'hotpot', $attempt->hotpot)) {
+        $question->md5key = md5($question->name);
+        if (!$question->id = get_field('hotpot_questions', 'id', 'hotpot', $attempt->hotpot, 'md5key', $question->md5key, 'name', $question->name)) {
             // add question record
             if (!$question->id = insert_record('hotpot_questions', $question)) {
                 error("Could not add question record (attempt_id=$attempt->id): ".$db->ErrorMsg(), $next_url);
@@ -2133,11 +2134,13 @@ function hotpot_string_id($str) {
     if (isset($str) && $str<>'') {
 
         // get the id from the table if it is already there
-        if (!$id = get_field('hotpot_strings', 'id', 'string', $str)) {
+               $md5key = md5($str);
+        if (!$id = get_field('hotpot_strings', 'id', 'md5key', $md5key, 'string', $str)) {
 
             // create a string record
-            $record = NULL;
+            $record = new stdClass();
             $record->string = $str;
+            $record->md5key = $md5key;
 
             // try and add the new string record
             if (!$id = insert_record('hotpot_strings', $record)) {
index c68b2f0f6f1b05409162903226635a19722a6b83..8dc340b160dcc207df8b81d23311a4486718eb0e 100644 (file)
@@ -299,7 +299,7 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys,
         }
     }
 
-    // update secondary keys, if any
+    // update foreign keys, if any
     $ok = true;
     foreach ($foreign_keys as $key=>$value) {
         if (is_numeric($value)) {
@@ -336,6 +336,14 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys,
         }
     }
 
+    // set md5 keys if necessary (restoring from Moodle<1.6)
+       if ($table=='hotpot_questions' && empty($record->md5key)) {
+               $record->md5key = md5($record->name);
+       }
+       if ($table=='hotpot_strings' && empty($record->md5key)) {
+               $record->md5key = md5($record->string);
+       }
+
     // check all "not null" fields have been set
     foreach ($table_columns[$table] as $column) {
         if ($column->not_null) {
index abb56c62712324ebe2b1e849520ea0483e759d5d..b6dbdef88b26011fb1b16ad9ce489dcdf81ff373 100644 (file)
@@ -3,7 +3,7 @@
 ///  Code fragment to define the version of hotpot
 ///  This fragment is called by moodle_needs_upgrading() and /admin/index.php
 /////////////////////////////////////////////////////////////////////////////////
-$module->version  = 2006083100;   // release date of this version (see note below)
+$module->version  = 2006083101;   // release date of this version (see note below)
 $module->release  = 'v2.2.0';    // human-friendly version name (used in mod/hotpot/lib.php)
 $module->requires = 2006080900;  // Requires this Moodle version
 $module->cron     = 0;            // period for cron to check this module (secs)