📄 update_to_v2.php
字号:
// start progress report $i = 0; $count = count($records); hotpot_update_print("Updating $count log records ... "); // update foreign keys in all $records foreach ($records as $record) { // update course module name $record->module = $modulename; // check if module id was given (usually it is) if ($record->cmid) { // update course module id, if necessary if (isset($new[$modulename][$record->cmid])) { $record->cmid = $new[$modulename][$record->cmid]; } else { // could not update course module id $ok = hotpot_update_print_warning('cmid', $record->cmid, 'log', $record->id) && $ok; unset($record->id); } // update url and info switch ($record->action) { case "add": case "update": case "view": $record->url = "view.php?id=".$record->cmid; $record->info = $moduleid; break; case "view all": // do nothing break; case "report": $record->url = "report.php?id=".$record->cmid; $record->info = $moduleid; break; case "attempt": case "submit": case "review": $id = substr(strrchr($record->url,"="),1); if (isset($new->attempt[$id])) { $id = $new->attempt[$id]; } $record->url = "review.php?id=".$record->cmid."&attempt=$id"; $record->info = $moduleid; break; default: // unknown log action $ok = hotpot_update_print_warning('action', $record->action, 'log', $record->id) && $ok; unset($record->id); } // end switch } if (isset($record->id)) { $ok = $ok && update_record($table, $record); } $i++; hotpot_update_print_progress($i); } // end foreach // finish progress report hotpot_update_print_ok($ok); } return $ok;}function hotpot_update_fields($table, $feedback=false) { global $CFG, $db; $ok = true; // check the table exists if (hotpot_db_table_exists($table)) { switch ($table) { case 'hotpot' : // == ADD == hotpot_db_update_field_type($table, '', 'location', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0); hotpot_db_update_field_type($table, '', 'navigation', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 1); hotpot_db_update_field_type($table, '', 'outputformat', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 1); hotpot_db_update_field_type($table, '', 'shownextquiz', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0); hotpot_db_update_field_type($table, '', 'forceplugins', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0); hotpot_db_update_field_type($table, '', 'password', 'VARCHAR', 255, '', 'NOT NULL', ''); hotpot_db_update_field_type($table, '', 'subnet', 'VARCHAR', 255, '', 'NOT NULL', ''); // == ALTER == hotpot_db_update_field_type($table, 'summary', 'summary', 'TEXT', '', '', 'NOT NULL', ''); hotpot_db_update_field_type($table, 'reference', 'reference', 'VARCHAR', 255, '', 'NOT NULL', ''); // == REMOVE == hotpot_db_remove_field($table, 'intro'); hotpot_db_remove_field($table, 'attemptonlast'); hotpot_db_remove_field($table, 'sumgrades'); hotpot_db_set_table_comment($table, 'details about Hot Potatoes quizzes'); break; case 'hotpot_events' : // == ADD == hotpot_db_update_field_type($table, '', 'hotpot', 'INTEGER', 10, 'UNSIGNED', 'NOT NULL'); hotpot_db_update_field_type($table, '', 'attempt', 'INTEGER', 6, 'UNSIGNED', 'NOT NULL'); hotpot_db_update_field_type($table, '', 'details', 'TEXT', '', '', '', ''); hotpot_db_update_field_type($table, '', 'timestart', 'INTEGER', 10, 'UNSIGNED', 'NOT NULL', 0); hotpot_db_update_field_type($table, '', 'timefinish', 'INTEGER', 10, 'UNSIGNED', 'NOT NULL', 0); // == ALTER == hotpot_db_update_field_type($table, 'score', 'score', 'INTEGER', 6, 'UNSIGNED', 'NULL'); hotpot_db_update_field_type($table, 'wrong', 'penalties', 'INTEGER', 6, 'UNSIGNED', 'NULL'); hotpot_db_update_field_type($table, 'starttime', 'starttime', 'INTEGER', 10, 'UNSIGNED', 'NULL'); hotpot_db_update_field_type($table, 'endtime', 'endtime', 'INTEGER', 10, 'UNSIGNED', 'NULL'); // save and switch off SQL message echo $debug = $db->debug; $db->debug = $feedback; // get array mapping course module ids to hotpot ids $hotpotmoduleid = get_field('modules', 'id', 'name', 'hotpot'); $coursemodules = get_records('course_modules', 'module', $hotpotmoduleid, 'id', 'id, instance'); // get all event records if (hotpot_db_field_exists($table, 'hotpotid')) { $records = get_records($table, '', '', 'userid,hotpotid,time'); } else { $records = false; // table has already been updated } if ($records) { $count = count($records); hotpot_update_print("Updating $count records in $table ... "); $ids = array_keys($records); foreach ($ids as $i=>$id) { // reference to current record $record = &$records[$id]; // set timestart and timefinish (the times recorded by Moodle) if (empty($record->timestart) && $record->time) { $record->timestart = $record->time; } if (empty($record->timefinish) && $record->timestart) { if ($record->starttime && $record->endtime) { $duration = ($record->endtime - $record->starttime); } else { if (($i+1)>=$count) { $nextrecord = NULL; } else { $nextrecord = &$records[$ids[$i+1]]; } if (isset($nextrecord) && $nextrecord->userid==$record->userid && $nextrecord->hotpotid==$record->hotpotid) { $duration = $nextrecord->time - $record->time; } else { $duration = NULL; } } if (isset($duration)) { $record->timefinish = $record->timestart + $duration; } } // unset score and penalties, if quiz was abandoned if (empty($record->endtime) || (empty($record->penalties) && empty($record->score))) { unset($record->score); unset($record->penalties); } // get last (=previous) record if ($i==0) { $lastrecord = NULL; } else { $lastrecord = &$records[$ids[$i-1]]; } // increment or reset $attempt number if (isset($lastrecord) && $lastrecord->userid==$record->userid && $lastrecord->hotpotid==$record->hotpotid) { $attempt++; } else { $attempt = 1; } // set $record->$attempt, if necessary if (empty($record->attempt) || $record->attempt<$attempt) { $record->attempt = $attempt; } else { $attempt = $record->attempt; } // set hotpot id and update record if (isset($record->hotpotid) && isset($record->id)) { if (isset($coursemodules[$record->hotpotid])) { $record->hotpot = $coursemodules[$record->hotpotid]->instance; hotpot_db_update_record($table, $record, true); } else { // hotpotid is invalid (shouldn't happen) $ok = hotpot_update_print_warning('hotpotid', $record->hotpotid, $table, $record->id) && $ok; delete_records($table, 'id', $record->id); } } else { // empty record (shouldn't happen) } hotpot_update_print_progress($i); } // finish progress report hotpot_update_print_ok($ok); } // restore SQL message echo setting $db->debug = $debug; // == REMOVE == hotpot_db_remove_field($table, 'hotpotid'); hotpot_db_remove_field($table, 'course'); hotpot_db_remove_field($table, 'time'); hotpot_db_remove_field($table, 'event'); hotpot_db_set_table_comment($table, 'details about Hot Potatoes quiz attempts'); break; case 'hotpotatoes' : // == ALTER == hotpot_db_update_field_type($table, 'intro', 'summary', 'TEXT', '', '', '', 'NULL'); break; } } return $ok;}function hotpot_update_string_id_list($table, &$record, $field, &$new) { $ok = true; if (isset($record->$field)) { $oldids = explode(',', $record->$field); $newids = array(); foreach ($oldids as $id) { if (isset($new['string'][$id])) { $newids[] = $new['string'][$id]; } else if (is_numeric($id)) { // string id could not be updated $ok = hotpot_update_print_warning("string id in $field", $id, $table, $record->id) && $ok; } else { // ignore non-numeric ids (e.g. blanks) } } if ($ok) { $record->$field = implode(',', $newids); } } return $ok;}///////////////////////////// print functions///////////////////////////function hotpot_update_print($msg=false, $n=300) { // this function prints $msg and flush output buffer if ($msg) { if (is_string($msg)) { print $msg; } else { print strftime("%X", time()); } } // fill output buffer if ($n) { print str_repeat(" ", $n); } // some browser's require newline to flush print "\n"; // flush PHP's output buffer flush();}function hotpot_update_print_progress($i) { if ($i%10==0) { $msg = '.'; hotpot_update_print($msg); }}function hotpot_update_print_ok($ok) { if ($ok) { hotpot_update_print('<font color="green">'.get_string('success')."</font><br />\n"); } else { hotpot_update_print('<font color="red">'.get_string('error')."</font><br />\n"); }}function hotpot_update_print_warning($field, $value, $table, $id) { hotpot_update_print("<li><b>Warning:</b> invalid $field field (value=$value) in $table (id=$id)</li>\n"); return true;}///////////////////////////// database functions///////////////////////////function hotpot_db_index_exists($table, $index, $feedback=false) { global $CFG, $db; $exists = false; // save and switch off SQL message echo $debug = $db->debug; $db->debug = $feedback; switch (strtolower($CFG->dbfamily)) { case 'mysql' : $rs = $db->Execute("SHOW INDEX FROM `$table`"); if ($rs && $rs->RecordCount()>0) { $records = $rs->GetArray(); foreach ($records as $record) { if (isset($record['Key_name']) && $record['Key_name']==$index) { $exists = true; break; } } } break; case 'postgres' : $rs = $db->Execute("SELECT relname FROM pg_class WHERE relname = '$index' AND relkind='i'"); if ($rs && $rs->RecordCount()>0) { $exists = true; } break; } // restore SQL message echo $db->debug = $debug; return $exists;}function hotpot_db_delete_index($table, $index, $feedback=false) { global $CFG, $db; $ok = true; // check index exists if (hotpot_db_index_exists($table, $index)) { switch (strtolower($CFG->dbfamily)) { case 'mysql' : $sql = "ALTER TABLE `$table` DROP INDEX `$index`"; break; case 'postgres' : $sql = "DROP INDEX $index"; break; default: // unknown database type $sql = ''; break; } if ($sql) { // save and switch off SQL message echo $debug = $db->debug; $db->debug = $feedback; $ok = $db->Execute($sql) ? true : false; // restore SQL message echo $db->debug = $debug; } else { // unknown database type $ok = false; } } return $ok;}function hotpot_db_add_index($table, $field, $length='') { global $CFG, $db; if (strtolower($CFG->dbfamily)=='postgres') { $index = "{$CFG->prefix}{$table}_{$field}_idx"; } else { // mysql (and others) $index = "{$table}_{$field}_idx"; } $table = "{$CFG->prefix}$table"; // delete $index if it already exists $ok = hotpot_db_delete_index($table, $index); switch (strtolower($CFG->dbfamily)) { case 'mysql' : $ok = $ok && $db->Execute("ALTER TABLE `$table` ADD INDEX `$index` (`$field`)"); break; case 'postgres' : $ok = $ok && $db->Execute("CREATE INDEX $index ON $table (\"$field\")"); break; default: // unknown database type $ok = false; break; } return $ok;}function hotpot_db_table_exists($table, $feedback=false) { return hotpot_db_object_exists($table, '', $feedback);}function hotpot_db_field_exists($table, $field, $feedback=false) { return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -