MDL-25708 recordsets - assignment and data modules
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Fri, 7 Jan 2011 10:12:50 +0000 (11:12 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 25 Jan 2011 18:59:00 +0000 (19:59 +0100)
mod/assignment/db/upgrade.php
mod/assignment/lib.php
mod/data/db/upgrade.php
mod/data/lib.php

index d5e891c..2841111 100644 (file)
@@ -49,10 +49,10 @@ function xmldb_assignment_upgrade($oldversion) {
 
         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
 
-        if ($rs = $DB->get_recordset_sql("SELECT s.id, s.userid, s.teacher, s.assignment, a.course, cm.id AS cmid $sqlfrom ORDER BY a.course, s.assignment")) {
+        $rs = $DB->get_recordset_sql("SELECT s.id, s.userid, s.teacher, s.assignment, a.course, cm.id AS cmid $sqlfrom ORDER BY a.course, s.assignment");
 
+        if ($rs->valid()) {
             $pbar = new progress_bar('migrateassignmentfiles', 500, true);
-
             $i = 0;
             foreach ($rs as $submission) {
                 $i++;
@@ -122,9 +122,8 @@ function xmldb_assignment_upgrade($oldversion) {
                 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment");
                 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment");
             }
-            $rs->close();
-
         }
+        $rs->close();
 
         upgrade_mod_savepoint(true, 2008081900, 'assignment');
     }
index 634f663..9c51c09 100644 (file)
@@ -2688,7 +2688,8 @@ function assignment_upgrade_grades() {
     $sql = "SELECT a.*, cm.idnumber AS cmidnumber, a.course AS courseid
               FROM {assignment} a, {course_modules} cm, {modules} m
              WHERE m.name='assignment' AND m.id=cm.module AND cm.instance=a.id";
-    if ($rs = $DB->get_recordset_sql($sql)) {
+    $rs = $DB->get_recordset_sql($sql);
+    if ($rs->valid()) {
         // too much debug output
         $pbar = new progress_bar('assignmentupgradegrades', 500, true);
         $i=0;
@@ -2698,9 +2699,9 @@ function assignment_upgrade_grades() {
             assignment_update_grades($assignment);
             $pbar->update($i, $count, "Updating Assignment grades ($i/$count).");
         }
-        $rs->close();
         upgrade_set_timeout(); // reset to default timeout
     }
+    $rs->close();
 }
 
 /**
index 1d85846..371da31 100644 (file)
@@ -55,16 +55,16 @@ function xmldb_data_upgrade($oldversion) {
     }
 
     if ($oldversion < 2008081400) {
-        if ($rs = $DB->get_recordset('data')) {
-            $pattern = '/\#\#delete\#\#(\s+)\#\#approve\#\#/';
-            $replacement = '##delete##$1##approve##$1##export##';
-            foreach ($rs as $data) {
-                $data->listtemplate = preg_replace($pattern, $replacement, $data->listtemplate);
-                $data->singletemplate = preg_replace($pattern, $replacement, $data->singletemplate);
-                $DB->update_record('data', $data);
-            }
-            $rs->close();
+        $pattern = '/\#\#delete\#\#(\s+)\#\#approve\#\#/';
+        $replacement = '##delete##$1##approve##$1##export##';
+        $rs = $DB->get_recordset('data');
+        foreach ($rs as $data) {
+            $data->listtemplate = preg_replace($pattern, $replacement, $data->listtemplate);
+            $data->singletemplate = preg_replace($pattern, $replacement, $data->singletemplate);
+            $DB->update_record('data', $data);
         }
+        $rs->close();
+
         upgrade_mod_savepoint(true, 2008081400, 'data');
     }
 
@@ -89,8 +89,9 @@ function xmldb_data_upgrade($oldversion) {
 
         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
 
-        if ($rs = $DB->get_recordset_sql("SELECT c.id, f.type, r.dataid, c.recordid, f.id AS fieldid, r.userid, c.content, c.content1, d.course, r.userid, cm.id AS cmid $sqlfrom ORDER BY d.course, d.id")) {
+        $rs = $DB->get_recordset_sql("SELECT c.id, f.type, r.dataid, c.recordid, f.id AS fieldid, r.userid, c.content, c.content1, d.course, r.userid, cm.id AS cmid $sqlfrom ORDER BY d.course, d.id");
 
+        if ($rs->valid()) {
             $pbar = new progress_bar('migratedatafiles', 500, true);
 
             $i = 0;
@@ -142,8 +143,9 @@ function xmldb_data_upgrade($oldversion) {
                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data");
                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata");
             }
-            $rs->close();
         }
+        $rs->close();
+
         upgrade_mod_savepoint(true, 2008091400, 'data');
     }
 
@@ -218,31 +220,31 @@ function xmldb_data_upgrade($oldversion) {
             $lastdataid = null;
             $lastcourseid = null;
             $modcontext = null;
-            if ($rs = $DB->get_recordset_sql($sql)) {
-                foreach($rs as $res) {
-                    if ($res->dataid != $lastdataid || $res->courseid != $lastcourseid) {
-                        $cm = get_coursemodule_from_instance('data', $res->dataid, $res->courseid);
-                        if ($cm) {
-                            $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
-                        }
-                        $lastdataid = $res->dataid;
-                        $lastcourseid = $res->courseid;
-                    }
-                    $cmt = new stdClass();
-                    $cmt->contextid   = $modcontext->id;
-                    $cmt->commentarea = 'database_entry';
-                    $cmt->itemid      = $res->itemid;
-                    $cmt->content     = $res->commentcontent;
-                    $cmt->format      = $res->format;
-                    $cmt->userid      = $res->userid;
-                    $cmt->timecreated = $res->timecreated;
-                    // comments class will throw an exception if error occurs
-                    $cmt_id = $DB->insert_record('comments', $cmt);
-                    if (!empty($cmt_id)) {
-                        $DB->delete_records('data_comments', array('id'=>$res->commentid));
+            $rs = $DB->get_recordset_sql($sql);
+            foreach($rs as $res) {
+                if ($res->dataid != $lastdataid || $res->courseid != $lastcourseid) {
+                    $cm = get_coursemodule_from_instance('data', $res->dataid, $res->courseid);
+                    if ($cm) {
+                        $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
                     }
+                    $lastdataid = $res->dataid;
+                    $lastcourseid = $res->courseid;
+                }
+                $cmt = new stdClass();
+                $cmt->contextid   = $modcontext->id;
+                $cmt->commentarea = 'database_entry';
+                $cmt->itemid      = $res->itemid;
+                $cmt->content     = $res->commentcontent;
+                $cmt->format      = $res->format;
+                $cmt->userid      = $res->userid;
+                $cmt->timecreated = $res->timecreated;
+                // comments class will throw an exception if error occurs
+                $cmt_id = $DB->insert_record('comments', $cmt);
+                if (!empty($cmt_id)) {
+                    $DB->delete_records('data_comments', array('id'=>$res->commentid));
                 }
             }
+            $rs->close();
             // the default exception handler will stop the script if error occurs before
             $dbman->drop_table($table);
         }
index a22aa6b..534e424 100755 (executable)
@@ -361,13 +361,14 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
             $conditions = array('fieldid'=>$this->field->id);
         }
 
-        if ($rs = $DB->get_recordset('data_content', $conditions)) {
+        $rs = $DB->get_recordset('data_content', $conditions);
+        if ($rs->valid()) {
             $fs = get_file_storage();
             foreach ($rs as $content) {
                 $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
             }
-            $rs->close();
         }
+        $rs->close();
 
         return $DB->delete_records('data_content', $conditions);
     }
@@ -1071,7 +1072,8 @@ function data_upgrade_grades() {
     $sql = "SELECT d.*, cm.idnumber AS cmidnumber, d.course AS courseid
               FROM {data} d, {course_modules} cm, {modules} m
              WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
-    if ($rs = $DB->get_recordset_sql($sql)) {
+    $rs = $DB->get_recordset_sql($sql);
+    if ($rs->valid()) {
         // too much debug output
         $pbar = new progress_bar('dataupgradegrades', 500, true);
         $i=0;
@@ -1081,8 +1083,8 @@ function data_upgrade_grades() {
             data_update_grades($data, 0, false);
             $pbar->update($i, $count, "Updating Database grades ($i/$count).");
         }
-        $rs->close();
     }
+    $rs->close();
 }
 
 /**
@@ -2448,39 +2450,38 @@ function data_reset_userdata($data) {
         $course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
         $notenrolled = array();
         $fields = array();
-        if ($rs = $DB->get_recordset_sql($recordssql, array($data->courseid))) {
-            foreach ($rs as $record) {
-                if (array_key_exists($record->userid, $notenrolled) or !$record->userexists or $record->userdeleted
-                  or !is_enrolled($course_context, $record->userid)) {
-                    //delete ratings
-                    if (!$cm = get_coursemodule_from_instance('data', $record->dataid)) {
-                        continue;
-                    }
-                    $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
-                    $ratingdeloptions->contextid = $datacontext->id;
-                    $ratingdeloptions->itemid = $record->id;
-                    $rm->delete_ratings($ratingdeloptions);
-
-                    $DB->delete_records('comments', array('itemid'=>$record->id, 'commentarea'=>'database_entry'));
-                    $DB->delete_records('data_content', array('recordid'=>$record->id));
-                    $DB->delete_records('data_records', array('id'=>$record->id));
-                    // HACK: this is ugly - the recordid should be before the fieldid!
-                    if (!array_key_exists($record->dataid, $fields)) {
-                        if ($fs = $DB->get_records('data_fields', array('dataid'=>$record->dataid))) {
-                            $fields[$record->dataid] = array_keys($fs);
-                        } else {
-                            $fields[$record->dataid] = array();
-                        }
-                    }
-                    foreach($fields[$record->dataid] as $fieldid) {
-                        fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$record->dataid/$fieldid/$record->id");
+        $rs = $DB->get_recordset_sql($recordssql, array($data->courseid));
+        foreach ($rs as $record) {
+            if (array_key_exists($record->userid, $notenrolled) or !$record->userexists or $record->userdeleted
+              or !is_enrolled($course_context, $record->userid)) {
+                //delete ratings
+                if (!$cm = get_coursemodule_from_instance('data', $record->dataid)) {
+                    continue;
+                }
+                $datacontext = get_context_instance(CONTEXT_MODULE, $cm->id);
+                $ratingdeloptions->contextid = $datacontext->id;
+                $ratingdeloptions->itemid = $record->id;
+                $rm->delete_ratings($ratingdeloptions);
+
+                $DB->delete_records('comments', array('itemid'=>$record->id, 'commentarea'=>'database_entry'));
+                $DB->delete_records('data_content', array('recordid'=>$record->id));
+                $DB->delete_records('data_records', array('id'=>$record->id));
+                // HACK: this is ugly - the recordid should be before the fieldid!
+                if (!array_key_exists($record->dataid, $fields)) {
+                    if ($fs = $DB->get_records('data_fields', array('dataid'=>$record->dataid))) {
+                        $fields[$record->dataid] = array_keys($fs);
+                    } else {
+                        $fields[$record->dataid] = array();
                     }
-                    $notenrolled[$record->userid] = true;
                 }
+                foreach($fields[$record->dataid] as $fieldid) {
+                    fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$record->dataid/$fieldid/$record->id");
+                }
+                $notenrolled[$record->userid] = true;
             }
-            $rs->close();
-            $status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'data'), 'error'=>false);
         }
+        $rs->close();
+        $status[] = array('component'=>$componentstr, 'item'=>get_string('deletenotenrolled', 'data'), 'error'=>false);
     }
 
     // remove all ratings