📄 update_to_v2.php
字号:
} // hotpot_questions: change type of "text" field to "INT(10)" $ok = $ok && hotpot_db_update_field_type('hotpot_questions', 'text', 'text', 'INTEGER', 10, 'UNSIGNED', 'NULL'); // hotpot_attempts // hotpot_attempts: move "details" to separate table $table = 'hotpot_details'; if (hotpot_db_table_exists($table)) { // do nothing } else { $ok = $ok && hotpot_create_table($table); switch (strtolower($CFG->dbfamily)) { case 'mysql' : case 'postgres' : $sql = " INSERT INTO {$CFG->prefix}$table (attempt, details) SELECT a.id AS attempt, a.details AS details FROM {$CFG->prefix}hotpot_attempts a WHERE a.details IS NOT NULL AND a.details <> '' AND a.details LIKE '<?xml%' AND a.details LIKE '%</hpjsresult>' "; break; default: $sql = ''; break; } if ($sql) { $ok = $ok && execute_sql($sql); } } // hotpot_attempts: remove the "details" field $ok = $ok && hotpot_db_remove_field('hotpot_attempts', 'details'); // hotpot_attempts: create and set status field (1=in-progress, 2=timed-out, 3=abandoned, 4=completed) $ok = $ok && hotpot_db_update_field_type('hotpot_attempts', '', 'status', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 1); $ok = $ok && execute_sql("UPDATE {$CFG->prefix}hotpot_attempts SET status=1 WHERE timefinish=0 AND SCORE IS NULL"); $ok = $ok && execute_sql("UPDATE {$CFG->prefix}hotpot_attempts SET status=3 WHERE timefinish>0 AND SCORE IS NULL"); $ok = $ok && execute_sql("UPDATE {$CFG->prefix}hotpot_attempts SET status=4 WHERE timefinish>0 AND SCORE IS NOT NULL"); // hotpot_attempts: create and set clickreport fields $ok = $ok && hotpot_db_update_field_type('hotpot', '', 'clickreporting', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0); $ok = $ok && hotpot_db_update_field_type('hotpot_attempts', '', 'clickreportid', 'INTEGER', 10, 'UNSIGNED', 'NULL'); $ok = $ok && execute_sql("UPDATE {$CFG->prefix}hotpot_attempts SET clickreportid=id WHERE clickreportid IS NULL"); // hotpot_attempts: create and set studentfeedback field (0=none, 1=formmail, 2=moodleforum, 3=moodlemessaging) $ok = $ok && hotpot_db_update_field_type('hotpot', '', 'studentfeedback', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', '0'); $ok = $ok && hotpot_db_update_field_type('hotpot', '', 'studentfeedbackurl', 'VARCHAR', 255, '', 'NULL'); // add indexes $ok = $ok && hotpot_db_add_index('hotpot_attempts', 'hotpot'); $ok = $ok && hotpot_db_add_index('hotpot_attempts', 'userid'); $ok = $ok && hotpot_db_add_index('hotpot_details', 'attempt'); $ok = $ok && hotpot_db_add_index('hotpot_questions', 'hotpot'); $ok = $ok && hotpot_db_add_index('hotpot_responses', 'attempt'); $ok = $ok && hotpot_db_add_index('hotpot_responses', 'question'); // hotpot_string: correct double-encoded HTML entities $ok = $ok && execute_sql(" UPDATE {$CFG->prefix}hotpot_strings SET string = REPLACE(string, '&','&') WHERE string LIKE '%&#%' AND (string LIKE '<' OR string LIKE '>') "); // hotpot_question: remove questions which refer to deleted hotpots if ($ok) { // try and get all hotpot records if ($records = get_records('hotpot')) { $ids = implode(',', array_keys($records)); $sql = "DELETE FROM {$CFG->prefix}hotpot_questions WHERE hotpot NOT IN ($ids)"; } else { // remove all question records (because there are no valid hotpot ids) $sql = "TRUNCATE {$CFG->prefix}hotpot_questions"; } print "Removing unused question records ..."; execute_sql($sql); } if ($ok) { // remove old 'v6' templates folder (replaced by 'template' folder) $ds = DIRECTORY_SEPARATOR; $dir = "mod{$ds}hotpot{$ds}v6"; print "removing old templates ($dir) ... "; if (hotpot_rm("$CFG->dirroot{$ds}$dir", false)) { print get_string('success'); } else { print "failed<br/>Please remove '$CFG->dirroot{$ds}$dir' manually"; } print "<br />\n"; } return $ok;}function hotpot_update_to_v2_from_v1() { global $CFG; $ok = true; // remove, alter and add fields in database $table = 'hotpot'; if (hotpot_db_table_exists($table)) { $ok = $ok && hotpot_update_fields($table); } else { $ok = $ok && hotpot_create_table($table); } $table = 'hotpot_attempts'; $oldtable = 'hotpot_events'; if (hotpot_db_table_exists($oldtable)) { $ok = $ok && hotpot_update_fields($oldtable); $ok = $ok && hotpot_db_append_table($oldtable, $table); } else { $ok = $ok && hotpot_create_table($table); } // create new tables (from mysql.sql) $ok = $ok && hotpot_create_table('hotpot_questions'); $ok = $ok && hotpot_create_table('hotpot_responses'); $ok = $ok && hotpot_create_table('hotpot_strings'); // remove redundant scripts $files = array('coursefiles.php', 'details.php', 'dummy.html', 'hotpot.php', 'hotpot2db.php'); foreach ($files as $file) { $filepath = "$CFG->dirroot/mod/hotpot/$file"; if (file_exists($filepath)) { @unlink($filepath); // don't worry about errors } } return $ok;}function hotpot_update_to_v2_from_hotpotatoes() { global $CFG; $ok = true; // hope for the best! // check we have the minimum required hotpot module $minimum = 2005031400; $module = get_record("modules", "name", "hotpot"); if (empty($module) || $module->version<$minimum) { if ($module) { print ("<p>The update to the HotPotatoes module requires at least version $minimum of the HotPot module.</p>"); print ("<p>The current version of the HotPot module on this site is $module->version.</p>"); } print ("<p>Please install the latest version of the HotPot module and then try the update again.</p>"); $ok = false; } else { // arrays to map foreign keys $new = array(); $new['hotpot'] = array(); $new['attempt'] = array(); $new['question'] = array(); $new['string'] = array(); // save and switch off SQL message echo global $db; $debug = $db->debug; $db->debug = false; // import hotpotatoes (and save old ids) $ok = $ok && hotpot_update_fields('hotpotatoes'); $ok = $ok && hotpot_transfer_records('hotpotatoes', 'hotpot', array(), 'hotpot', $new); // update course modules and logs $ok = $ok && hotpot_update_course_modules('hotpotatoes', 'hotpot', $new); // import hotpotatoes_strings (and save old ids) $ok = $ok && hotpot_transfer_records('hotpotatoes_strings', 'hotpot_strings', array(), 'string', $new); // import hotpotatoes_attempts (and save old ids) $ok = $ok && hotpot_transfer_records('hotpotatoes_attempts', 'hotpot_attempts', array('hotpotatoes'=>'hotpot'), 'attempt', $new); // import hotpotatoes_questions (and save old ids) $ok = $ok && hotpot_transfer_records('hotpotatoes_questions', 'hotpot_questions', array('hotpotatoes'=>'hotpot'), 'question', $new); // import hotpotatoes_responses $ok = $ok && hotpot_transfer_records('hotpotatoes_responses', 'hotpot_responses', array('attempt'=>'attempt', 'question'=>'question'), 'response', $new); // restore SQL message echo setting $db->debug = $debug; // remove the hotpotatoes tables, if the update went ok if ($ok) { // hotpot_db_remove_table('hotpotatoes'); // hotpot_db_remove_table('hotpotatoes_attempts'); // hotpot_db_remove_table('hotpotatoes_questions'); // hotpot_db_remove_table('hotpotatoes_responses'); // hotpot_db_remove_table('hotpotatoes_strings'); } // hide the hotpotatoes module (see admin/modules.php)) if ($ok && ($module = get_record("modules", "name", "hotpotatoes"))) { set_field("modules", "visible", "0", "id", $module->id); print '<p>All HotPotatoes activities have been imported to the HotPot module.<br />'."\n"; print 'The HotPotatoes module has been hidden and can safely be deleted from this Moodle site.<br />'."\n"; print ' <a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/modules.php">Configuration -> Modules</A>, then click "Delete" for "Hot Potatoes XML Quiz"</p>'."\n"; } } if ($ok) { print '<p align="center">Thank you for using the HotPotatoes module.<br />'; print 'The HotPotatoes module has been replaced by<br />version 2 of the HotPot module. Enjoy!</p>'; } return $ok;}function hotpot_create_table($table) { global $CFG; static $sql; static $xmldb_file; // check table does not already exist if (hotpot_db_table_exists($table)) { return true; } if (! isset($xmldb_file)) { // first time only if (class_exists('XMLDBFile')) { $xmldb_file = new XMLDBFile("$CFG->dirroot/mod/hotpot/db/install.xml"); if (! $xmldb_file->fileExists() || !$xmldb_file->loadXMLStructure() || !$xmldb_file->isLoaded()) { unset($xmldb_file); } } if (empty($xmldb_file)) { $xmldb_file = false; } } if ($xmldb_file) { // Moodle 1.8 (and later) $ok = false; foreach ($xmldb_file->xmldb_structure->tables as $xmldb_table) { if ($xmldb_table->name==$table) { $ok = create_table($xmldb_table); break; } } return $ok; } // Moodle 1.7 (and earlier) if (! isset($sql)) { // first time only $sqlfilepath = "$CFG->dirroot/mod/hotpot/db/$CFG->dbtype.sql"; if (file_exists($sqlfilepath)) { if (function_exists('file_get_contents')) { $sql = file_get_contents($sqlfilepath); } else { // PHP < 4.3 $sql = file($sqlfilepath); if (is_array($sql)) { $sql = implode('', $sql); } } } if (empty($sql)) { $sql = ''; } } // extract and execute all CREATE statements relating to this table if (preg_match_all("/CREATE (TABLE|INDEX)(\s[^;]*)? prefix_{$table}(\s[^;]*)?;/s", $sql, $strings)) { $ok = true; foreach ($strings[0] as $string) { $ok = $ok && modify_database('', $string); } return $ok; } // table could not be created return false;}function hotpot_transfer_records($oldtable, $table, $foreignkeys, $primarykey, &$new) { global $db; $ok = true; // get the records, if any if (hotpot_db_table_exists($oldtable) && ($records = get_records($oldtable))) { // start progress report $i = 0; $count = count($records); hotpot_update_print("Transferring $count records from "$oldtable" to "$table" ... "); // transfer all $records foreach ($records as $record) { switch ($table) { case 'hotpot' : $record->summary = addslashes($record->summary); break; case 'hotpot_attempts' : $record->details = addslashes($record->details); break; case 'hotpot_questions' : $record->name = addslashes($record->name); hotpot_update_string_id_list($table, $record, 'TEXT', $new); break; case 'hotpot_responses' : hotpot_update_string_id_list($table, $record, 'correct', $new); hotpot_update_string_id_list($table, $record, 'ignored', $new); hotpot_update_string_id_list($table, $record, 'wrong', $new); break; case 'hotpot_strings' : $record->string = addslashes($record->string); break; } // update foreign keys, if any foreach ($foreignkeys as $oldkey=>$key) { // transfer (and update) key $value = $record->$oldkey; if (isset($new[$key][$value])) { $record->$key = $new[$key][$value]; } else { // foreign key could not be updated $ok = hotpot_update_print_warning($key, $value, $oldtable, $record->id) && $ok; unset($record->id); } } if ($ok && isset($record->id)) { // store and remove old primary key $id = $record->id; unset($record->id); // add the updated record and store the new id $new[$primarykey][$id] = insert_record($table, $record, true); // check id is numeric if (!is_numeric($new[$primarykey][$id])) { hotpot_update_print("<li>Record could not added to $table table ($oldtable id=$id)</li>\n"); //$ok = false; } } $i++; hotpot_update_print_progress($i); } // finish progress report hotpot_update_print_ok($ok); } return $ok;}function hotpot_update_course_modules($oldmodulename, $modulename, &$new) { $ok = true; $oldmoduleid = get_field('modules', 'id', 'name', $oldmodulename); $moduleid = get_field('modules', 'id', 'name', $modulename); if (is_numeric($oldmoduleid) && is_numeric($moduleid)) { // get module records if ($records = get_records('course_modules', 'module', $oldmoduleid)) { // start progress report $count = count($records); hotpot_update_print("Updating $count course modules from "$oldmodulename" to "$modulename" ... "); // update foreign keys in all $records foreach ($records as $record) { // update instance $instance = $record->instance; if (isset($new[$modulename][$instance])) { $record->instance = $new[$modulename][$instance]; } else if ($record->deleted) { unset($record->id); } else { // could not find new id of course module $ok = hotpot_update_print_warning("$modulename instance", $instance, 'course_modules', $record->id) && $ok; unset($record->id); } // update module id if ($ok && isset($record->id)) { $record->module = $moduleid; $ok = update_record('course_modules', $record); } } // finish progress report hotpot_update_print_ok($ok); } // update logs $ok = $ok && hotpot_update_logs($oldmodulename, $modulename, $moduleid, $new); } return $ok;}function hotpot_update_logs($oldmodulename, $modulename, $moduleid, &$new) { $table = 'log'; $ok = true; // get log records for the oldmodule if ($records = get_records($table, 'module', $oldmodulename)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -