<?PHP
+if (file_exists("$CFG->dirroot/lib/ddllib.php")) {
+ // Moodle 1.8+
+ include_once "$CFG->dirroot/lib/ddllib.php";
+}
+
function hotpot_update_to_v2_2() {
global $CFG;
$ok = true;
$index = "{$CFG->prefix}{$table}_{$field}_idx";
} else {
$index = "{$table}_{$field}_idx";
- }
+ }
hotpot_db_delete_index("{$CFG->prefix}$table", $index);
// add new hotpot_questions.md5key field (and index)
$index = "{$CFG->prefix}{$table}_{$field}_idx";
} else {
$index = "{$table}_{$field}_idx";
- }
+ }
hotpot_db_delete_index("{$CFG->prefix}$table", $index);
// add new hotpot_strings.md5key field (and index)
// ensure setting of default values on certain fields
// this was originally done in postgres7.php, but was found to be incompatible with PG7 :-(
$table="hotpot";
+ execute_sql("UPDATE {$CFG->prefix}$table SET studentfeedbackurl = '' WHERE studentfeedbackurl IS NULL");
$ok = $ok && hotpot_db_update_field_type($table, '', 'studentfeedbackurl', 'VARCHAR', 255, '', 'NOT NULL', '');
$ok = $ok && hotpot_db_update_field_type($table, '', 'studentfeedback', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0);
$ok = $ok && hotpot_db_update_field_type($table, '', 'clickreporting', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0);
$ok = $ok && hotpot_db_update_field_type($table, '', 'score', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0);
$ok = $ok && hotpot_db_update_field_type($table, '', 'weighting', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0);
$ok = $ok && hotpot_db_update_field_type($table, '', 'correct', 'VARCHAR', 255, '', 'NOT NULL', '');
+ execute_sql("UPDATE {$CFG->prefix}$table SET wrong = '' WHERE wrong IS NULL");
$ok = $ok && hotpot_db_update_field_type($table, '', 'wrong', 'VARCHAR', 255, '', 'NOT NULL', '');
+ execute_sql("UPDATE {$CFG->prefix}$table SET ignored = '' WHERE ignored IS NULL");
$ok = $ok && hotpot_db_update_field_type($table, '', 'ignored', 'VARCHAR', 255, '', 'NOT NULL', '');
$ok = $ok && hotpot_db_update_field_type($table, '', 'hints', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0);
$ok = $ok && hotpot_db_update_field_type($table, '', 'clues', 'INTEGER', 4, 'UNSIGNED', 'NOT NULL', 0);
GROUP BY userid, hotpot
HAVING COUNT(*)>1 AND MIN(status)=1
");
- if ($rs && !$rs->EOF) {
+ if ($rs && $rs->RecordCount()) {
$records = $rs->GetArray();
// start message to browser
}
function hotpot_create_table($table) {
global $CFG;
- $ok = true;
+
static $sql;
- if (empty($sql)) { // first time only
- $filepath = "$CFG->dirroot/mod/hotpot/db/$CFG->dbtype.sql";
- if (function_exists('file_get_contents')) {
- $sql = file_get_contents($filepath);
- } else { // PHP < 4.3
- $sql = file($filepath);
- if (is_array($sql)) {
- $sql = implode('', $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($sql)) { // $sql==false
- $sql = '';
+ if (empty($xmldb_file)) {
+ $xmldb_file = false;
}
}
- // check table does not already exist
- if (!hotpot_db_table_exists($table)) {
- // extract and execute all CREATE statements relating to this table
- if (preg_match_all("/CREATE (TABLE|INDEX)(\s[^;]*)? prefix_{$table}(\s[^;]*)?;/s", $sql, $strings)) {
- foreach ($strings[0] as $string) {
- $ok = $ok && modify_database('', $string);
+
+ 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);
+ }
}
- } else {
- // no CREATE statements found for this $table
- $ok = false;
+ }
+ if (empty($sql)) {
+ $sql = '';
}
}
- return $ok;
+
+ // 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;
switch (strtolower($CFG->dbfamily)) {
case 'mysql' :
$rs = $db->Execute("SHOW INDEX FROM `$table`");
- if ($rs && !$rs->EOF) {
+ if ($rs && $rs->RecordCount()>0) {
$records = $rs->GetArray();
foreach ($records as $record) {
if (isset($record['Key_name']) && $record['Key_name']==$index) {
break;
case 'postgres' :
$rs = $db->Execute("SELECT relname FROM pg_class WHERE relname = '$index' AND relkind='i'");
- if ($rs && !$rs->EOF) {
+ if ($rs && $rs->RecordCount()>0) {
$exists = true;
}
break;
if (empty($rs) && debugging()) {
notify($db->ErrorMsg()."<br /><br />$sql");
}
- return ($rs && !$rs->EOF);
+ return ($rs && $rs->RecordCount()>0);
}
function hotpot_db_remove_table($table, $feedback=true) {
global $CFG;
// (except lib/adodb/drivers/adodb-postgre64-inc.php)
$dbversion = '';
$rs = $db->Execute("SELECT version()");
- if ($rs && !$rs->EOF) {
+ if ($rs && $rs->RecordCount()>0) {
$records = $rs->GetArray();
if (preg_match('/\d+\.\d+/', $records[0][0], $matches)) {
$dbversion = $matches[0];