From b7cf551827739105ba57d219dc276f102aec543f Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 8 Jun 2008 16:07:11 +0000 Subject: [PATCH] MDL-15110 towards hotpot dml conversion --- mod/hotpot/attempt.php | 55 +++++++++++++++-------------- mod/hotpot/backuplib.php | 56 ++++++++++++++++++------------ mod/hotpot/defaults.php | 4 +-- mod/hotpot/grade.php | 4 +-- mod/hotpot/index.php | 26 +++++++------- mod/hotpot/report/click/report.php | 4 ++- 6 files changed, 82 insertions(+), 67 deletions(-) diff --git a/mod/hotpot/attempt.php b/mod/hotpot/attempt.php index 48c6102d35..14a0ff4e83 100644 --- a/mod/hotpot/attempt.php +++ b/mod/hotpot/attempt.php @@ -5,16 +5,16 @@ $attemptid = required_param('attemptid', PARAM_INT); // get attempt, hotpot, course and course_module records - if (! $attempt = get_record("hotpot_attempts", "id", $attemptid)) { + if (! $attempt = $DB->get_record("hotpot_attempts", array("id"=>$attemptid))) { print_error('invalidattemptid', 'hotpot'); } if ($attempt->userid != $USER->id) { print_error("invaliduserid"); } - if (! $hotpot = get_record("hotpot", "id", $attempt->hotpot)) { + if (! $hotpot = $DB->get_record("hotpot", array("id"=>$attempt->hotpot))) { print_error('invalidhotpotid', 'hotpot'); } - if (! $course = get_record("course", "id", $hotpot->course)) { + if (! $course = $DB->get_record("course", array("id"=>$hotpot->course))) { print_error('invalidcourseid'); } if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) { @@ -68,18 +68,18 @@ } // check if this is the second (or subsequent) click - if (get_field("hotpot_attempts", "timefinish", "id", $attempt->id)) { + if ($DB->get_field("hotpot_attempts", "timefinish", array("id"=>$attempt->id))) { if ($hotpot->clickreporting==HOTPOT_YES) { // add attempt record for each form submission // records are linked via the "clickreportid" field // update status in previous records in this group - set_field("hotpot_attempts", "status", $attempt->status, "clickreportid", $attempt->clickreportid); + $DB->set_field("hotpot_attempts", "status", $attempt->status, array("clickreportid"=>$attempt->clickreportid)); // add new attempt record unset ($attempt->id); - $attempt->id = insert_record("hotpot_attempts", $attempt); + $attempt->id = $DB->insert_record("hotpot_attempts", $attempt); if (empty($attempt->id)) { print_error('cannotinsertattempt', 'hotpot', $next_url, $DB->get_last_error()); @@ -87,17 +87,17 @@ // add attempt details record, if necessary if (!empty($attempt->details)) { - unset($details); + $details = new object(); $details->attempt = $attempt->id; $details->details = $attempt->details; - if (! insert_record("hotpot_details", $details, false)) { + if (! $DB->insert_record("hotpot_details", $details, false)) { print_error('cannotinsertattempt', 'hotpot', $next_url, $DB->get_last_error()); } } } else { // remove previous responses for this attempt, if required // (N.B. this does NOT remove the attempt record, just the responses) - delete_records("hotpot_responses", "attempt", $attempt->id); + $DB->delete_records("hotpot_responses", array("attempt"=>$attempt->id)); } } @@ -108,10 +108,10 @@ hotpot_add_attempt_details($attempt); // add slashes again, so the details can be added to the database - $attempt->details = addslashes($attempt->details); + $attempt->details = $attempt->details; // update the attempt record - if (! update_record("hotpot_attempts", $attempt)) { + if (! $DB->update_record("hotpot_attempts", $attempt)) { print_error('cannotupdateattempt', 'hotpot', $next_url, $DB->get_last_error()); } @@ -119,21 +119,21 @@ hotpot_update_grades($hotpot, $attempt->userid); // get previous attempt details record, if any - $details_exist = record_exists("hotpot_details", "attempt", $attempt->id); + $details_exist = $DB->record_exists("hotpot_details", array("attempt"=>$attempt->id)); // delete/update/add the attempt details record if (empty($attempt->details)) { if ($details_exist) { - delete_records("hotpot_details", "attempt", $attempt->id); + $DB->delete_records("hotpot_details", array("attempt"=>$attempt->id)); } } else { if ($details_exist) { - set_field("hotpot_details", "details", $attempt->details, "attempt", $attempt->id); + $DB->set_field("hotpot_details", "details", $attempt->details, array("attempt"=>$attempt->id)); } else { - unset($details); + $details = new object(); $details->attempt = $attempt->id; $details->details = $attempt->details; - if (! insert_record("hotpot_details", $details)) { + if (! $DB->insert_record("hotpot_details", $details)) { print_error('cannotinsertattempt', 'hotpot', $next_url, $DB->get_last_error()); } } @@ -171,16 +171,17 @@ function hotpot_get_next_cm(&$cm) { // gets the next module in this section of the course // that is the same type of module as the current module + global $DB; $next_mod = false; // get a list of $ids of modules in this section - if ($ids = get_field('course_sections', 'sequence', 'id', $cm->section)) { + if ($ids = $DB->get_field('course_sections', 'sequence', array('id'=>$cm->section))) { $found = false; $ids = explode(',', $ids); foreach ($ids as $id) { - if ($found && ($cm->module==get_field('course_modules', 'module', 'id', $id))) { + if ($found && ($cm->module==$DB->get_field('course_modules', 'module', array('id'=>$id)))) { $next_mod = $id; break; } else if ($cm->id==$id) { @@ -191,7 +192,7 @@ function hotpot_get_next_cm(&$cm) { return $next_mod; } function hotpot_set_attempt_details(&$attempt) { - global $CFG, $HOTPOT_QUIZTYPE; + global $CFG, $HOTPOT_QUIZTYPE, $DB; // optional_param('showallquestions', 0, PARAM_INT); @@ -433,22 +434,22 @@ function hotpot_set_attempt_details(&$attempt) { } // get previous responses to this question (if any) - $records = get_records_sql(" + $records = $DB->get_records_sql(" SELECT r.* FROM - {$CFG->prefix}hotpot_attempts a, - {$CFG->prefix}hotpot_questions q, - {$CFG->prefix}hotpot_responses r + {hotpot_attempts} a, + {hotpot_questions} q, + {hotpot_responses} r WHERE - a.clickreportid = $attempt->clickreportid AND + a.clickreportid = ? AND a.id = r.attempt AND r.question = q.id AND - q.name = '$questionname' AND - q.hotpot = $attempt->hotpot + q.name = ? AND + q.hotpot = ? ORDER BY a.timefinish - "); + ", array($attempt->clickreportid, $questionname, $attempt->hotpot)); if ($records) { foreach ($records as $record) { diff --git a/mod/hotpot/backuplib.php b/mod/hotpot/backuplib.php index 8dd2cd54a2..5bd499706a 100644 --- a/mod/hotpot/backuplib.php +++ b/mod/hotpot/backuplib.php @@ -36,12 +36,12 @@ // //----------------------------------------------------------- function hotpot_backup_mods($bf, $preferences) { - global $CFG; + global $DB; $status = true; //Iterate over hotpot table - $hotpots = get_records ("hotpot","course",$preferences->backup_course,"id"); + $hotpots = $DB->get_records ("hotpot", array("course"=>$preferences->backup_course), "id"); if ($hotpots) { foreach ($hotpots as $hotpot) { if (function_exists('backup_mod_selected')) { @@ -65,7 +65,8 @@ $level = 3; $status = true; $table = 'hotpot'; - $select = "course=$preferences->backup_course AND id='$instance'"; + $select = "course=? AND id=?"; + $params = array($preferences->backup_course, $instance); $records_tag = ''; $records_tags = array(); $record_tag = 'MOD'; @@ -88,7 +89,7 @@ } return hotpot_backup_records( $bf, $status, $level, - $table, $select, + $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup @@ -97,7 +98,8 @@ function hotpot_backup_attempts($bf, &$parent, $level, $status) { // $parent is a reference to a hotpot record $table = 'hotpot_attempts'; - $select = "hotpot=$parent->id"; + $select = "hotpot=?"; + $params = array($parent->id); $records_tag = 'ATTEMPT_DATA'; $records_tags = array(); $record_tag = 'ATTEMPT'; @@ -108,7 +110,7 @@ $excluded_tags = array('hotpot'); return hotpot_backup_records( $bf, $status, $level, - $table, $select, + $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup @@ -117,7 +119,8 @@ function hotpot_backup_details($bf, &$parent, $level, $status) { // $parent is a reference to an attempt record $table = 'hotpot_details'; - $select = "attempt=$parent->id"; + $select = "attempt=?"; + $params = array($parent->id); $records_tag = ''; $records_tags = array(); $record_tag = ''; @@ -126,7 +129,7 @@ $excluded_tags = array('id','attempt'); return hotpot_backup_records( $bf, $status, $level, - $table, $select, + $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup @@ -135,7 +138,8 @@ function hotpot_backup_responses($bf, &$parent, $level, $status) { // $parent is a reference to an attempt record $table = 'hotpot_responses'; - $select = "attempt=$parent->id"; + $select = "attempt=?"; + $params = array($parent->id); $records_tag = 'RESPONSE_DATA'; $records_tags = array(); $record_tag = 'RESPONSE'; @@ -144,7 +148,7 @@ $excluded_tags = array('id','attempt'); return hotpot_backup_records( $bf, $status, $level, - $table, $select, + $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup @@ -153,7 +157,8 @@ function hotpot_backup_questions($bf, &$parent, $level, $status) { // $parent is a reference to an hotpot record $table = 'hotpot_questions'; - $select = "hotpot=$parent->id"; + $select = "hotpot=?"; + $params = array($parent->id); $records_tag = 'QUESTION_DATA'; $records_tags = array(); $record_tag = 'QUESTION'; @@ -204,6 +209,7 @@ $ids = implode(',', $ids); $table = 'hotpot_strings'; $select = "id IN ($ids)"; + $params = array(); $records_tag = 'STRING_DATA'; $records_tags = array(); $record_tag = 'STRING'; @@ -212,7 +218,7 @@ $excluded_tags = array(''); $status = hotpot_backup_records( $bf, $status, $level, - $table, $select, + $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup @@ -220,7 +226,7 @@ } return $status; } - function hotpot_backup_records(&$bf, $status, $level, $table, $select, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup) { + function hotpot_backup_records(&$bf, $status, $level, $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup) { // general purpose backup function // $bf : resource id of backup file // $status : current status of backup (true or false) @@ -237,7 +243,9 @@ // no further "fwrite"s will be attempted // and the function returns "false". // Otherwise, the function returns "true". - if ($status && ($records = get_records_select($table, $select, 'id'))) { + global $DB; + + if ($status && ($records = $DB->get_records_select($table, $select, $params, 'id'))) { // start a group of records if ($records_tag) { $status = $status && fwrite($bf, start_tag($records_tag, $level, true)); @@ -282,7 +290,7 @@ } ////Return an array of info (name, value) function hotpot_check_backup_mods($course, $user_data=false, $backup_unique_code, $instances=null) { - global $CFG; + global $CFG, $DB; $info = array(); if (isset($instances) && is_array($instances) && count($instances)) { foreach ($instances as $id => $instance) { @@ -291,15 +299,16 @@ } else { // the course data $info[0][0] = get_string('modulenameplural','hotpot'); - $info[0][1] = count_records('hotpot', 'course', $course); + $info[0][1] = $DB->count_records('hotpot', array('course'=>$course)); // the user_data, if requested if ($user_data) { - $table = "{$CFG->prefix}hotpot h, {$CFG->prefix}hotpot_attempts a"; - $select = "h.course = $course AND h.id = a.hotpot"; + $table = "{hotpot} h, {hotpot_attempts} a"; + $select = "h.course = ? AND h.id = a.hotpot"; + $params = array($course); $info[1][0] = get_string('attempts', 'quiz'); - $info[1][1] = count_records_sql("SELECT COUNT(*) FROM $table WHERE $select"); + $info[1][1] = $DB->count_records_sql("SELECT COUNT(*) FROM $table WHERE $select", $params); } } return $info; @@ -307,7 +316,7 @@ ////Return an array of info (name, value) function hotpot_check_backup_mods_instances($instance,$backup_unique_code) { - global $CFG; + global $CFG, $DB; $info = array(); // the course data @@ -316,11 +325,12 @@ // the user_data, if requested if (!empty($instance->userdata)) { - $table = "{$CFG->prefix}hotpot_attempts a"; - $select = "a.hotpot = $instance->id"; + $table = "{hotpot_attempts} a"; + $select = "a.hotpot = ?"; + $params = array($instance->id); $info[$instance->id.'1'][0] = get_string('attempts', 'quiz'); - $info[$instance->id.'1'][1] = count_records_sql("SELECT COUNT(*) FROM $table WHERE $select"); + $info[$instance->id.'1'][1] = $DB->count_records_sql("SELECT COUNT(*) FROM $table WHERE $select", $params); } return $info; } diff --git a/mod/hotpot/defaults.php b/mod/hotpot/defaults.php index 8e678f78c6..5d2820df5d 100644 --- a/mod/hotpot/defaults.php +++ b/mod/hotpot/defaults.php @@ -1,7 +1,7 @@ hotpot_initialdisable)) { - if (!count_records('hotpot')) { - set_field('modules', 'visible', 0, 'name', 'hotpot'); // Disable it by default + if (!$DB->count_records('hotpot')) { + $DB->set_field('modules', 'visible', 0, array('name'=>'hotpot')); // Disable it by default set_config('hotpot_initialdisable', 1); } } diff --git a/mod/hotpot/grade.php b/mod/hotpot/grade.php index 843414c916..08ffc8a38e 100644 --- a/mod/hotpot/grade.php +++ b/mod/hotpot/grade.php @@ -8,11 +8,11 @@ print_error('invalidcoursemodule'); } - if (! $hotpot = get_record("hotpot", "id", $cm->instance)) { + if (! $hotpot = $DB->get_record("hotpot", array("id"=>$cm->instance))) { print_error('invalidhotpotid', 'hotpot'); } - if (! $course = get_record("course", "id", $hotpot->course)) { + if (! $course = $DB->get_record("course", array("id"=>$hotpot->course))) { print_error("invalidcourse"); } diff --git a/mod/hotpot/index.php b/mod/hotpot/index.php index 930c5657b1..33bb76a31b 100644 --- a/mod/hotpot/index.php +++ b/mod/hotpot/index.php @@ -146,7 +146,7 @@ notify("$hotpot->name"); // delete questions and responses for this hotpot - if ($records = get_records_select('hotpot_questions', "hotpot=$hotpot->id", '', 'id,hotpot')) { + if ($records = $DB->get_records('hotpot_questions', array('hotpot'=>$hotpot->id), '', 'id,hotpot')) { $questionids = implode(',', array_keys($records)); hotpot_delete_and_notify('hotpot_questions', "id IN ($questionids)", get_string('question', 'quiz')); hotpot_delete_and_notify('hotpot_responses', "question IN ($questionids)", get_string('answer', 'quiz')); @@ -157,12 +157,12 @@ $attemptcount = 0; // regrade attempts, if any, for this hotpot - if ($attempts = get_records_select('hotpot_attempts', "hotpot=$hotpot->id")) { + if ($attempts = $DB->get_records('hotpot_attempts', array('hotpot'=>$hotpot->id))) { foreach ($attempts as $attempt) { - $attempt->details = get_field('hotpot_details', 'details', 'attempt', $attempt->id); + $attempt->details = $DB->get_field('hotpot_details', 'details', array('attempt'=>$attempt->id)); if ($attempt->details) { hotpot_add_attempt_details($attempt); - if (! update_record('hotpot_attempts', $attempt)) { + if (! $DB->update_record('hotpot_attempts', $attempt)) { print_error('cannotupdateattempt', 'hotpot', $next_url, $DB->get_last_error()); } } @@ -197,9 +197,9 @@ $regrade_hotpots = array(); $concat_field = sql_concat('hotpot', "'_'", 'name'); if ($concat_field) { - $records = get_records_sql(" + $records = $DB->get_records_sql(" SELECT $concat_field, COUNT(*), hotpot, name - FROM {$CFG->prefix}hotpot_questions + FROM {hotpot_questions} WHERE hotpot IN ($hotpotids) GROUP BY hotpot, name HAVING COUNT(*) >1 @@ -218,7 +218,8 @@ $start = microtime(); // get total number of attempts, users and details for these hotpots - $tables = "{$CFG->prefix}hotpot_attempts a"; + $params = array(); + $tables = "{hotpot_attempts} a"; $fields = " a.hotpot AS hotpot, COUNT(DISTINCT a.clickreportid) AS attemptcount, @@ -230,26 +231,27 @@ // do nothing (=get all users) } else { // restrict results to this user only - $select .= " AND a.userid='$USER->id'"; + $select .= " AND a.userid=:userid"; + $params['userid'] = $USER->id; } $usejoin = 0; if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM)) && $usejoin) { // join attempts table and details table - $tables .= ",{$CFG->prefix}hotpot_details d"; + $tables .= ",{hotpot_details} d"; $fields .= ',COUNT(DISTINCT d.id) AS detailcount'; $select .= " AND a.id=d.attempt"; // this may take about twice as long as getting the gradecounts separately :-( // so this operation could be done after getting the $totals from the attempts table } - $totals = get_records_sql("SELECT $fields FROM $tables WHERE $select GROUP BY a.hotpot"); + $totals = $DB->get_records_sql("SELECT $fields FROM $tables WHERE $select GROUP BY a.hotpot", $params); if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM)) && empty($usejoin)) { foreach ($hotpots as $hotpot) { $totals[$hotpot->id]->detailcount = 0; - if ($ids = get_records('hotpot_attempts', 'hotpot', $hotpot->id)) { + if ($ids = $DB->get_records('hotpot_attempts', array('hotpot'=>$hotpot->id))) { $ids = join(',', array_keys($ids)); - $totals[$hotpot->id]->detailcount = count_records_select('hotpot_details', "attempt IN ($ids)"); + $totals[$hotpot->id]->detailcount = $DB->count_records_select('hotpot_details', "attempt IN ($ids)"); } } } diff --git a/mod/hotpot/report/click/report.php b/mod/hotpot/report/click/report.php index 32ff520d50..f5fa08f3dd 100644 --- a/mod/hotpot/report/click/report.php +++ b/mod/hotpot/report/click/report.php @@ -379,8 +379,10 @@ class hotpot_report extends hotpot_default_report { } } function set_data_exercise(&$cm, &$course, &$hotpot, &$questions, &$questionids, &$questioncount, &$blank) { + global $DB; + // get exercise details (course name, section number, activity number, quiztype and question count) - $record = get_record("course_sections", "id", $cm->section); + $record = $DB->get_record("course_sections", array("id"=>$cm->section)); $this->data['exercise'] = array( 'course' => $course->shortname, 'section' => empty($record) ? $blank : $record->section+1, -- 2.39.5