MDL-25708 recordsets - glossary module
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 11 Jan 2011 14:27:42 +0000 (15:27 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 25 Jan 2011 18:59:01 +0000 (19:59 +0100)
mod/glossary/db/upgrade.php
mod/glossary/lib.php

index f6507cf..722bdf1 100644 (file)
@@ -45,7 +45,8 @@ function xmldb_glossary_upgrade($oldversion) {
 
         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
 
-        if ($rs = $DB->get_recordset_sql("SELECT ge.id, ge.userid, ge.attachment, ge.glossaryid, ge.sourceglossaryid, g.course, cm.id AS cmid $sqlfrom ORDER BY g.course, g.id")) {
+        $rs = $DB->get_recordset_sql("SELECT ge.id, ge.userid, ge.attachment, ge.glossaryid, ge.sourceglossaryid, g.course, cm.id AS cmid $sqlfrom ORDER BY g.course, g.id");
+        if ($rs->valid()) {
 
             $pbar = new progress_bar('migrateglossaryfiles', 500, true);
 
@@ -92,8 +93,8 @@ function xmldb_glossary_upgrade($oldversion) {
                 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid");
                 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary");
             }
-            $rs->close();
         }
+        $rs->close();
 
         upgrade_mod_savepoint(true, 2008081900, 'glossary');
     }
@@ -240,30 +241,30 @@ function xmldb_glossary_upgrade($oldversion) {
             $modcontext     = null;
 
         /// move glossary comments to comments table
-            if ($rs = $DB->get_recordset_sql($sql)) {
-                foreach($rs as $res) {
-                    if ($res->glossaryid != $lastglossaryid || $res->courseid != $lastcourseid) {
-                        $cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid);
-                        if ($cm) {
-                            $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
-                        }
-                        $lastglossaryid = $res->glossaryid;
-                        $lastcourseid   = $res->courseid;
-                    }
-                    $cmt = new stdClass();
-                    $cmt->contextid     = $modcontext->id;
-                    $cmt->commentarea   = 'glossary_entry';
-                    $cmt->itemid        = $res->itemid;
-                    $cmt->content       = $res->commentcontent;
-                    $cmt->format        = $res->format;
-                    $cmt->userid        = $res->userid;
-                    $cmt->timecreated    = $res->timemodified;
-                    $cmt_id = $DB->insert_record('comments', $cmt);
-                    if (!empty($cmt_id)) {
-                        $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
+            $rs = $DB->get_recordset_sql($sql);
+            foreach($rs as $res) {
+                if ($res->glossaryid != $lastglossaryid || $res->courseid != $lastcourseid) {
+                    $cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid);
+                    if ($cm) {
+                        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
                     }
+                    $lastglossaryid = $res->glossaryid;
+                    $lastcourseid   = $res->courseid;
+                }
+                $cmt = new stdClass();
+                $cmt->contextid     = $modcontext->id;
+                $cmt->commentarea   = 'glossary_entry';
+                $cmt->itemid        = $res->itemid;
+                $cmt->content       = $res->commentcontent;
+                $cmt->format        = $res->format;
+                $cmt->userid        = $res->userid;
+                $cmt->timecreated    = $res->timemodified;
+                $cmt_id = $DB->insert_record('comments', $cmt);
+                if (!empty($cmt_id)) {
+                    $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
                 }
             }
+            $rs->close();
             $dbman->drop_table($table);
         }
 
index 858093a..498036b 100644 (file)
@@ -506,7 +506,8 @@ function glossary_upgrade_grades() {
     $sql = "SELECT g.*, cm.idnumber AS cmidnumber, g.course AS courseid
               FROM {glossary} g, {course_modules} cm, {modules} m
              WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id";
-    if ($rs = $DB->get_recordset_sql($sql)) {
+    $rs = $DB->get_recordset_sql($sql);
+    if ($rs->valid()) {
         $pbar = new progress_bar('glossaryupgradegrades', 500, true);
         $i=0;
         foreach ($rs as $glossary) {
@@ -515,8 +516,8 @@ function glossary_upgrade_grades() {
             glossary_update_grades($glossary, 0, false);
             $pbar->update($i, $count, "Updating Glossary grades ($i/$count).");
         }
-        $rs->close();
     }
+    $rs->close();
 }
 
 /**
@@ -2462,7 +2463,8 @@ function glossary_reset_userdata($data) {
 
         $course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
         $notenrolled = array();
-        if ($rs = $DB->get_recordset_sql($entriessql, $params)) {
+        $rs = $DB->get_recordset_sql($entriessql, $params);
+        if ($rs->valid()) {
             foreach ($rs as $entry) {
                 if (array_key_exists($entry->userid, $notenrolled) or !$entry->userexists or $entry->userdeleted
                   or !is_enrolled($course_context , $entry->userid)) {
@@ -2479,9 +2481,9 @@ function glossary_reset_userdata($data) {
                     }
                 }
             }
-            $rs->close();
             $status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'glossary'), 'error'=>false);
         }
+        $rs->close();
     }
 
     // remove all ratings