]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15110 towards hotpot dml conversion
authorskodak <skodak>
Sun, 8 Jun 2008 16:07:11 +0000 (16:07 +0000)
committerskodak <skodak>
Sun, 8 Jun 2008 16:07:11 +0000 (16:07 +0000)
mod/hotpot/attempt.php
mod/hotpot/backuplib.php
mod/hotpot/defaults.php
mod/hotpot/grade.php
mod/hotpot/index.php
mod/hotpot/report/click/report.php

index 48c6102d351202f6ba13ade9b7eb8f4d999ea064..14a0ff4e8342a77955c66bdaf65ff656007b2ff4 100644 (file)
@@ -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)) {
     }
 
     // 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());
 
             // 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));
         }
     }
 
     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());
     }
 
     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());
             }
         }
 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) {
index 8dd2cd54a28a6fef722d06f275f21d3101a09c35..5bd499706ae620e218a1d58d3112ce9d491a7b21 100644 (file)
     //
     //-----------------------------------------------------------
     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';
         $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
     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 = '';
         $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
     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';
         $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
     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';
             $ids = implode(',', $ids);
             $table = 'hotpot_strings';
             $select = "id IN ($ids)";
+            $params = array();
             $records_tag = 'STRING_DATA';
             $records_tags = array();
             $record_tag = 'STRING';
             $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
         }
         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)
         // 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));
     }
     ////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) {
         } 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;
 
     ////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
 
         // 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;
     }
index 8e678f78c60f2a70475975b7c5a48f0869456aa8..5d2820df5d01183ffa81a719c0a688b3a9824ee0 100644 (file)
@@ -1,7 +1,7 @@
 <?php // $Id$
     if (empty($CFG->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);
         }
     }
index 843414c9160136e807731563696faf6a263179a9..08ffc8a38e476714a9899fd6b1887f462c186925 100644 (file)
@@ -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");
     }
 
index 930c5657b1dab944ed62c8877be1fe20bb329be1..33bb76a31b0bcb0d0a64de42266e73d7c2166cbe 100644 (file)
                     notify("<b>$hotpot->name</b>");
 
                     // 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'));
                     $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());
                                 }
                             }
         $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
     $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,
         // 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)");
             }
         }
     }
index 32ff520d50a17fa73b720f563f2ad12834883a88..f5fa08f3dd44ae9cb7f98808255c28a205d6b6b9 100644 (file)
@@ -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,