From 755d2c74f630b3f1a6ee349b7db63fbb26e4d011 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 30 Dec 2010 00:41:47 +0100 Subject: [PATCH 01/22] MDL-25708 recordsets - fix admin dir uses --- admin/multilangupgrade.php | 45 ++++++++++---------- admin/user/lib.php | 11 +++-- admin/user/user_bulk_confirm.php | 21 +++++---- admin/user/user_bulk_delete.php | 15 +++---- admin/user/user_bulk_forcepasswordchange.php | 17 ++++---- admin/user/user_bulk_message.php | 15 +++---- 6 files changed, 59 insertions(+), 65 deletions(-) diff --git a/admin/multilangupgrade.php b/admin/multilangupgrade.php index d25c70953a5..77c221905db 100644 --- a/admin/multilangupgrade.php +++ b/admin/multilangupgrade.php @@ -56,35 +56,34 @@ foreach ($tables as $table) { if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only // first find candidate records $sql = "SELECT id, $column FROM $fulltable WHERE $column LIKE '%%' OR $column LIKE '%get_recordset_sql($sql)) { - foreach ($rs as $data) { - $text = $data->$column; - $id = $data->id; - if ($i % 600 == 0) { - echo '
'; - } - if ($i % 10 == 0) { - echo '.'; - } - $i++; + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $data) { + $text = $data->$column; + $id = $data->id; + if ($i % 600 == 0) { + echo '
'; + } + if ($i % 10 == 0) { + echo '.'; + } + $i++; - if (empty($text) or is_numeric($text)) { - continue; // nothing to do - } + if (empty($text) or is_numeric($text)) { + continue; // nothing to do + } - $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is'; - $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text); + $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is'; + $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text); - if (is_null($newtext)) { - continue; // regex error - } + if (is_null($newtext)) { + continue; // regex error + } - if ($newtext != $text) { - $DB->execute("UPDATE $fulltable SET $column=? WHERE id=?", array($newtext, $id)); - } + if ($newtext != $text) { + $DB->execute("UPDATE $fulltable SET $column=? WHERE id=?", array($newtext, $id)); } - $rs->close(); } + $rs->close(); } } } diff --git a/admin/user/lib.php b/admin/user/lib.php index 3f21653850e..25775cba590 100644 --- a/admin/user/lib.php +++ b/admin/user/lib.php @@ -11,14 +11,13 @@ function add_selection_all($ufiltering) { list($sqlwhere, $params) = $ufiltering->get_sql_filter("id<>:exguest AND deleted <> 1", array('exguest'=>$CFG->siteguest)); - if ($rs = $DB->get_recordset_select('user', $sqlwhere, $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname')) { - foreach ($rs as $user) { - if (!isset($SESSION->bulk_users[$user->id])) { - $SESSION->bulk_users[$user->id] = $user->id; - } + $rs = $DB->get_recordset_select('user', $sqlwhere, $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname'); + foreach ($rs as $user) { + if (!isset($SESSION->bulk_users[$user->id])) { + $SESSION->bulk_users[$user->id] = $user->id; } - $rs->close(); } + $rs->close(); } function get_selection_data($ufiltering) { diff --git a/admin/user/user_bulk_confirm.php b/admin/user/user_bulk_confirm.php index 7f103419101..c7ba604ea5e 100755 --- a/admin/user/user_bulk_confirm.php +++ b/admin/user/user_bulk_confirm.php @@ -24,19 +24,18 @@ echo $OUTPUT->header(); if ($confirm and confirm_sesskey()) { list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users); - if ($rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, username, secret, confirmed, auth, firstname, lastname')) { - foreach ($rs as $user) { - if ($user->confirmed) { - continue; - } - $auth = get_auth_plugin($user->auth); - $result = $auth->user_confirm($user->username, $user->secret); - if ($result != AUTH_CONFIRM_OK && $result != AUTH_CONFIRM_ALREADY) { - echo $OUTPUT->notification(get_string('usernotconfirmed', '', fullname($user, true))); - } + $rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, username, secret, confirmed, auth, firstname, lastname'); + foreach ($rs as $user) { + if ($user->confirmed) { + continue; + } + $auth = get_auth_plugin($user->auth); + $result = $auth->user_confirm($user->username, $user->secret); + if ($result != AUTH_CONFIRM_OK && $result != AUTH_CONFIRM_ALREADY) { + echo $OUTPUT->notification(get_string('usernotconfirmed', '', fullname($user, true))); } - $rs->close(); } + $rs->close(); redirect($return, get_string('changessaved')); } else { diff --git a/admin/user/user_bulk_delete.php b/admin/user/user_bulk_delete.php index 26e11f0f738..a8b6705ce8f 100755 --- a/admin/user/user_bulk_delete.php +++ b/admin/user/user_bulk_delete.php @@ -25,16 +25,15 @@ echo $OUTPUT->header(); if ($confirm and confirm_sesskey()) { list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users); - if ($rs = $DB->get_recordset_select('user', "id $in", $params)) { - foreach ($rs as $user) { - if (!is_siteadmin($user) and $USER->id != $user->id and delete_user($user)) { - unset($SESSION->bulk_users[$user->id]); - } else { - echo $OUTPUT->notification(get_string('deletednot', '', fullname($user, true))); - } + $rs = $DB->get_recordset_select('user', "id $in", $params); + foreach ($rs as $user) { + if (!is_siteadmin($user) and $USER->id != $user->id and delete_user($user)) { + unset($SESSION->bulk_users[$user->id]); + } else { + echo $OUTPUT->notification(get_string('deletednot', '', fullname($user, true))); } - $rs->close(); } + $rs->close(); session_gc(); // remove stale sessions redirect($return, get_string('changessaved')); diff --git a/admin/user/user_bulk_forcepasswordchange.php b/admin/user/user_bulk_forcepasswordchange.php index 6988c741c0c..d67c9427aec 100644 --- a/admin/user/user_bulk_forcepasswordchange.php +++ b/admin/user/user_bulk_forcepasswordchange.php @@ -38,17 +38,16 @@ if ($confirm and confirm_sesskey()) { $parts = array_chunk($SESSION->bulk_users, 300); foreach ($parts as $users) { list($in, $params) = $DB->get_in_or_equal($users); - if ($rs = $DB->get_recordset_select('user', "id $in", $params)) { - foreach ($rs as $user) { - if (!empty($changeable[$user->auth])) { - set_user_preference('auth_forcepasswordchange', 1, $user->id); - unset($SESSION->bulk_users[$user->id]); - } else { - echo $OUTPUT->notification(get_string('forcepasswordchangenot', '', fullname($user, true))); - } + $rs = $DB->get_recordset_select('user', "id $in", $params); + foreach ($rs as $user) { + if (!empty($changeable[$user->auth])) { + set_user_preference('auth_forcepasswordchange', 1, $user->id); + unset($SESSION->bulk_users[$user->id]); + } else { + echo $OUTPUT->notification(get_string('forcepasswordchangenot', '', fullname($user, true))); } - $rs->close(); } + $rs->close(); } echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess'); echo $OUTPUT->continue_button($return); diff --git a/admin/user/user_bulk_message.php b/admin/user/user_bulk_message.php index 52127fd7dc1..ceb5bcd95fc 100755 --- a/admin/user/user_bulk_message.php +++ b/admin/user/user_bulk_message.php @@ -25,15 +25,14 @@ if (empty($CFG->messaging)) { if ($confirm and !empty($msg) and confirm_sesskey()) { list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users); - if ($rs = $DB->get_recordset_select('user', "id $in", $params)) { - foreach ($rs as $user) { - //TODO we should probably support all text formats here or only FORMAT_MOODLE - //For now bulk messaging is still using the html editor and its supplying html - //so we have to use html format for it to be displayed correctly - message_post_message($USER, $user, $msg, FORMAT_HTML, 'direct'); - } - $rs->close(); + $rs = $DB->get_recordset_select('user', "id $in", $params); + foreach ($rs as $user) { + //TODO we should probably support all text formats here or only FORMAT_MOODLE + //For now bulk messaging is still using the html editor and its supplying html + //so we have to use html format for it to be displayed correctly + message_post_message($USER, $user, $msg, FORMAT_HTML, 'direct'); } + $rs->close(); redirect($return); } From a0c3fc6304e3b1f57b637347744f98c140ddd3a0 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 30 Dec 2010 01:18:25 +0100 Subject: [PATCH 02/22] MDL-25708 recordsets - fix course & enrol dir uses --- course/lib.php | 17 ++++++++--------- course/user.php | 7 ++++--- enrol/authorize/localfuncs.php | 6 ++++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/course/lib.php b/course/lib.php index 28983a9cec7..d5a5ba5c7f1 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1757,17 +1757,16 @@ function rebuild_course_cache($courseid=0, $clearonly=false) { @set_time_limit(0); // this could take a while! MDL-10954 } - if ($rs = $DB->get_recordset("course", $select,'','id,fullname')) { - foreach ($rs as $course) { - $modinfo = serialize(get_array_of_activities($course->id)); - $DB->set_field("course", "modinfo", $modinfo, array("id"=>$course->id)); - // update cached global COURSE too ;-) - if ($course->id == $COURSE->id) { - $COURSE->modinfo = $modinfo; - } + $rs = $DB->get_recordset("course", $select,'','id,fullname'); + foreach ($rs as $course) { + $modinfo = serialize(get_array_of_activities($course->id)); + $DB->set_field("course", "modinfo", $modinfo, array("id"=>$course->id)); + // update cached global COURSE too ;-) + if ($course->id == $COURSE->id) { + $COURSE->modinfo = $modinfo; } - $rs->close(); } + $rs->close(); // reset the fast modinfo cache $reset = 'reset'; get_fast_modinfo($reset); diff --git a/course/user.php b/course/user.php index c50dc40031b..b5ab6cd9916 100644 --- a/course/user.php +++ b/course/user.php @@ -400,7 +400,8 @@ switch ($mode) { } // Check if result is empty - if (!$rs = $DB->get_recordset_sql($sql)) { + $rs = $DB->get_recordset_sql($sql); + if (!$rs->valid()) { if ($course->id != 1) { $error = get_string('nocompletions', 'coursereport_completion'); @@ -409,6 +410,7 @@ switch ($mode) { } echo $OUTPUT->notification($error); + $rs->close(); // not going to loop (but break), close rs break; } @@ -437,8 +439,7 @@ switch ($mode) { $courses['unstarted'][] = $c_info; } } - - $rs->close(); + $rs->close(); // after loop, close rs // Loop through course status groups foreach ($courses as $type => $infos) { diff --git a/enrol/authorize/localfuncs.php b/enrol/authorize/localfuncs.php index a1f946fb5e1..a05c20e63dd 100644 --- a/enrol/authorize/localfuncs.php +++ b/enrol/authorize/localfuncs.php @@ -194,7 +194,9 @@ function send_welcome_messages($orderdata) { WHERE e.id IN(" . implode(',', $orderdata) . ") ORDER BY e.userid"; - if (!$rs = $DB->get_recordset_sql($sql)) { + $rs = $DB->get_recordset_sql($sql); + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return; } @@ -247,7 +249,7 @@ function send_welcome_messages($orderdata) { } while ($ei); - $rs->close(); + $rs->close(); // end of iteration, close rs } } From fc5016fcc83892696546a9a6132c632bea364617 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 30 Dec 2010 01:21:15 +0100 Subject: [PATCH 03/22] MDL-25708 recordsets - fix group dir uses --- group/overview.php | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/group/overview.php b/group/overview.php index 53a5a8be029..88d348cfe1a 100644 --- a/group/overview.php +++ b/group/overview.php @@ -80,26 +80,25 @@ $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, u.firstname, u.la WHERE g.courseid = :courseid $groupwhere $groupingwhere ORDER BY g.name, u.lastname, u.firstname"; -if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $row) { - $user = new stdClass(); - $user->id = $row->userid; - $user->firstname = $row->firstname; - $user->lastname = $row->lastname; - $user->username = $row->username; - $user->idnumber = $row->idnumber; - if (!$row->groupingid) { - $row->groupingid = -1; - } - if (!array_key_exists($row->groupid, $members[$row->groupingid])) { - $members[$row->groupingid][$row->groupid] = array(); - } - if(isset($user->id)){ - $members[$row->groupingid][$row->groupid][] = $user; - } +$rs = $DB->get_recordset_sql($sql, $params); +foreach ($rs as $row) { + $user = new stdClass(); + $user->id = $row->userid; + $user->firstname = $row->firstname; + $user->lastname = $row->lastname; + $user->username = $row->username; + $user->idnumber = $row->idnumber; + if (!$row->groupingid) { + $row->groupingid = -1; + } + if (!array_key_exists($row->groupid, $members[$row->groupingid])) { + $members[$row->groupingid][$row->groupid] = array(); + } + if(isset($user->id)){ + $members[$row->groupingid][$row->groupid][] = $user; } - $rs->close(); } +$rs->close(); navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid))); $PAGE->navbar->add(get_string('overview', 'group')); From afa559e9bc469a7fe4d90bb2b89e7fd22cf7fd18 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 30 Dec 2010 01:48:43 +0100 Subject: [PATCH 04/22] MDL-25708 recordsets - fix accesslib and datalib uses --- lib/accesslib.php | 84 +++++++++++++++++++++-------------------------- lib/datalib.php | 70 +++++++++++++++++++-------------------- 2 files changed, 71 insertions(+), 83 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 87a82873b88..3322f4ef8e6 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -425,12 +425,11 @@ function get_role_access($roleid, $accessdata = null) { if (!isset($ACCESSLIB_PRIVATE->croncache[$roleid])) { $ACCESSLIB_PRIVATE->croncache[$roleid] = array(); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $rd) { - $ACCESSLIB_PRIVATE->croncache[$roleid][] = $rd; - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $rd) { + $ACCESSLIB_PRIVATE->croncache[$roleid][] = $rd; } + $rs->close(); } foreach ($ACCESSLIB_PRIVATE->croncache[$roleid] as $rd) { @@ -439,14 +438,15 @@ function get_role_access($roleid, $accessdata = null) { } } else { - if ($rs = $DB->get_recordset_sql($sql, $params)) { + $rs = $DB->get_recordset_sql($sql, $params); + if ($rs->valid()) { foreach ($rs as $rd) { $k = "{$rd->path}:{$roleid}"; $accessdata['rdef'][$k][$rd->capability] = $rd->permission; } unset($rd); - $rs->close(); } + $rs->close(); } return $accessdata; @@ -480,14 +480,15 @@ function get_default_frontpage_role_access($roleid, $accessdata = null) { ORDER BY ctx.depth, ctx.path"; $params = array($roleid, "$base/%"); - if ($rs = $DB->get_recordset_sql($sql, $params)) { + $rs = $DB->get_recordset_sql($sql, $params); + if ($rs->valid()) { foreach ($rs as $rd) { $k = "{$rd->path}:{$roleid}"; $accessdata['rdef'][$k][$rd->capability] = $rd->permission; } unset($rd); - $rs->close(); } + $rs->close(); return $accessdata; } @@ -1315,18 +1316,15 @@ function load_subcontext($userid, $context, &$accessdata) { $params = array($context->id, $context->path."/%"); $newrdefs = array(); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $rd) { - $k = "{$rd->path}:{$rd->roleid}"; - if (!array_key_exists($k, $newrdefs)) { - $newrdefs[$k] = array(); - } - $newrdefs[$k][$rd->capability] = $rd->permission; + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $rd) { + $k = "{$rd->path}:{$rd->roleid}"; + if (!array_key_exists($k, $newrdefs)) { + $newrdefs[$k] = array(); } - $rs->close(); - } else { - debugging('Bad SQL encountered!'); + $newrdefs[$k][$rd->capability] = $rd->permission; } + $rs->close(); compact_rdefs($newrdefs); foreach ($newrdefs as $key=>$value) { @@ -1389,13 +1387,12 @@ function get_role_access_bycontext($roleid, $context, $accessdata = null) { ORDER BY ctx.depth ASC, ctx.path DESC, rc.roleid ASC "; $params = array($roleid, $context->path."/%"); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $rd) { - $k = "{$rd->path}:{$roleid}"; - $accessdata['rdef'][$k][$rd->capability] = $rd->permission; - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $rd) { + $k = "{$rd->path}:{$roleid}"; + $accessdata['rdef'][$k][$rd->capability] = $rd->permission; } + $rs->close(); return $accessdata; } @@ -1615,13 +1612,12 @@ function load_temp_role($context, $roleid, array $accessdata) { AND rc.roleid = ? ORDER BY ctx.depth, ctx.path"; $params = array($context->path."/%", $roleid); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $rd) { - $k = "{$rd->path}:{$roleid}"; - $accessdata['rdef'][$k][$rd->capability] = $rd->permission; - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $rd) { + $k = "{$rd->path}:{$roleid}"; + $accessdata['rdef'][$k][$rd->capability] = $rd->permission; } + $rs->close(); // // Say we loaded everything for the course context @@ -2132,12 +2128,11 @@ function cleanup_contexts() { // transactions used only for performance reasons here $transaction = $DB->start_delegated_transaction(); - if ($rs = $DB->get_recordset_sql($sql)) { - foreach ($rs as $ctx) { - delete_context($ctx->contextlevel, $ctx->instanceid); - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $ctx) { + delete_context($ctx->contextlevel, $ctx->instanceid); } + $rs->close(); $transaction->allow_commit(); return true; @@ -5335,16 +5330,13 @@ function get_user_capability_course($capability, $userid = null, $doanything = t // Note the result can be used directly as a context (we are going to), the course // fields are just appended. - if (!$rs = $DB->get_recordset_sql("SELECT x.*, c.id AS courseid $fieldlist - FROM {course} c - INNER JOIN {context} x - ON (c.id=x.instanceid AND x.contextlevel=".CONTEXT_COURSE.") - $orderby")) { - return false; - } - - // Check capability for each course in turn $courses = array(); + $rs = $DB->get_recordset_sql("SELECT x.*, c.id AS courseid $fieldlist + FROM {course} c + INNER JOIN {context} x + ON (c.id=x.instanceid AND x.contextlevel=".CONTEXT_COURSE.") + $orderby"); + // Check capability for each course in turn foreach ($rs as $coursecontext) { if (has_capability($capability, $coursecontext, $userid, $doanything)) { // We've got the capability. Make the record look like a course record @@ -5357,7 +5349,7 @@ function get_user_capability_course($capability, $userid = null, $doanything = t } } $rs->close(); - return $courses; + return empty($courses) ? false : $courses; } /** diff --git a/lib/datalib.php b/lib/datalib.php index cb3d92660df..79a69e4fd94 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -438,6 +438,12 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); + $totalcount = 0; + if (!$limitfrom) { + $limitfrom = 0; + } + $visiblecourses = array(); + $sql = "SELECT $fields $ccselect FROM {course} c $ccjoin @@ -445,17 +451,8 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c ORDER BY $sort"; // pull out all course matching the cat - if (!$rs = $DB->get_recordset_sql($sql, $params)) { - return array(); - } - $totalcount = 0; - - if (!$limitfrom) { - $limitfrom = 0; - } - + $rs = $DB->get_recordset_sql($sql, $params); // iteration will have to be done inside loop to keep track of the limitfrom and limitnum - $visiblecourses = array(); foreach($rs as $course) { context_instance_preload($course); if ($course->visible <= 0) { @@ -764,35 +761,35 @@ function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $record $searchcond = implode(" AND ", $searchcond); + $courses = array(); + $c = 0; // counts how many visible courses we've seen + + // Tiki pagination + $limitfrom = $page * $recordsperpage; + $limitto = $limitfrom + $recordsperpage; + list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); $sql = "SELECT c.* $ccselect FROM {course} c $ccjoin WHERE $searchcond AND c.id <> ".SITEID." ORDER BY $sort"; - $courses = array(); - $c = 0; // counts how many visible courses we've seen - if ($rs = $DB->get_recordset_sql($sql, $params)) { - // Tiki pagination - $limitfrom = $page * $recordsperpage; - $limitto = $limitfrom + $recordsperpage; - - foreach($rs as $course) { - context_instance_preload($course); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - if ($course->visible || has_capability('moodle/course:viewhiddencourses', $coursecontext)) { - // Don't exit this loop till the end - // we need to count all the visible courses - // to update $totalcount - if ($c >= $limitfrom && $c < $limitto) { - $courses[$course->id] = $course; - } - $c++; + $rs = $DB->get_recordset_sql($sql, $params); + foreach($rs as $course) { + context_instance_preload($course); + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + if ($course->visible || has_capability('moodle/course:viewhiddencourses', $coursecontext)) { + // Don't exit this loop till the end + // we need to count all the visible courses + // to update $totalcount + if ($c >= $limitfrom && $c < $limitto) { + $courses[$course->id] = $course; } + $c++; } - $rs->close(); } + $rs->close(); // our caller expects 2 bits of data - our return // array, and an updated $totalcount @@ -857,16 +854,15 @@ function get_categories($parent='none', $sort=NULL, $shallow=true) { } $categories = array(); - if( $rs = $DB->get_recordset_sql($sql, $params) ){ - foreach($rs as $cat) { - context_instance_preload($cat); - $catcontext = get_context_instance(CONTEXT_COURSECAT, $cat->id); - if ($cat->visible || has_capability('moodle/category:viewhiddencategories', $catcontext)) { - $categories[$cat->id] = $cat; - } + $rs = $DB->get_recordset_sql($sql, $params); + foreach($rs as $cat) { + context_instance_preload($cat); + $catcontext = get_context_instance(CONTEXT_COURSECAT, $cat->id); + if ($cat->visible || has_capability('moodle/category:viewhiddencategories', $catcontext)) { + $categories[$cat->id] = $cat; } - $rs->close(); } + $rs->close(); return $categories; } From b967c54185a1c432ed4d5094554e1eb02c872a46 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sat, 1 Jan 2011 16:28:11 +0100 Subject: [PATCH 05/22] MDL-25708 recordsets - fix events/grade/group/moodle libs --- lib/eventslib.php | 33 ++++++++++++++++----------------- lib/gradelib.php | 37 +++++++++++++++++-------------------- lib/grouplib.php | 5 ++++- lib/moodlelib.php | 35 ++++++++++++++++------------------- 4 files changed, 53 insertions(+), 57 deletions(-) diff --git a/lib/eventslib.php b/lib/eventslib.php index d5f30450426..9cc29382abb 100755 --- a/lib/eventslib.php +++ b/lib/eventslib.php @@ -435,25 +435,24 @@ function events_cron($eventname='') { $params = array(); } - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $qhandler) { - if (isset($failed[$qhandler->handlerid])) { - // do not try to dispatch any later events when one already asked for retry or ended with exception - continue; - } - $status = events_process_queued_handler($qhandler); - if ($status === false) { - // handler is asking for retry, do not send other events to this handler now - $failed[$qhandler->handlerid] = $qhandler->handlerid; - } else if ($status === NULL) { - // means completely broken handler, event data was purged - $failed[$qhandler->handlerid] = $qhandler->handlerid; - } else { - $processed++; - } + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $qhandler) { + if (isset($failed[$qhandler->handlerid])) { + // do not try to dispatch any later events when one already asked for retry or ended with exception + continue; + } + $status = events_process_queued_handler($qhandler); + if ($status === false) { + // handler is asking for retry, do not send other events to this handler now + $failed[$qhandler->handlerid] = $qhandler->handlerid; + } else if ($status === NULL) { + // means completely broken handler, event data was purged + $failed[$qhandler->handlerid] = $qhandler->handlerid; + } else { + $processed++; } - $rs->close(); } + $rs->close(); // remove events that do not have any handlers waiting $sql = "SELECT eq.id diff --git a/lib/gradelib.php b/lib/gradelib.php index c0dad2075d8..1fd916fa591 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -1206,13 +1206,12 @@ function grade_uninstalled_module($modname) { WHERE itemtype='mod' AND itemmodule=?"; // go all items for this module and delete them including the grades - if ($rs = $DB->get_recordset_sql($sql, array($modname))) { - foreach ($rs as $item) { - $grade_item = new grade_item($item, false); - $grade_item->delete('moduninstall'); - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, array($modname)); + foreach ($rs as $item) { + $grade_item = new grade_item($item, false); + $grade_item->delete('moduninstall'); } + $rs->close(); } /** @@ -1260,14 +1259,13 @@ function grade_cron() { SELECT 'x' FROM {grade_items} c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)"; // go through all courses that have proper final grades and lock them if needed - if ($rs = $DB->get_recordset_sql($sql, array($now))) { - foreach ($rs as $item) { - $grade_item = new grade_item($item, false); - $grade_item->locked = $now; - $grade_item->update('locktime'); - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, array($now)); + foreach ($rs as $item) { + $grade_item = new grade_item($item, false); + $grade_item->locked = $now; + $grade_item->update('locktime'); } + $rs->close(); $grade_inst = new grade_grade(); $fields = 'g.'.implode(',g.', $grade_inst->required_fields); @@ -1278,14 +1276,13 @@ function grade_cron() { SELECT 'x' FROM {grade_items} c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)"; // go through all courses that have proper final grades and lock them if needed - if ($rs = $DB->get_recordset_sql($sql, array($now))) { - foreach ($rs as $grade) { - $grade_grade = new grade_grade($grade, false); - $grade_grade->locked = $now; - $grade_grade->update('locktime'); - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, array($now)); + foreach ($rs as $grade) { + $grade_grade = new grade_grade($grade, false); + $grade_grade->locked = $now; + $grade_grade->update('locktime'); } + $rs->close(); //TODO: do not run this cleanup every cron invocation // cleanup history tables diff --git a/lib/grouplib.php b/lib/grouplib.php index 5eaf10831d3..17cba0375d3 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -199,7 +199,10 @@ function groups_get_user_groups($courseid, $userid=0) { WHERE gm.userid = ? AND g.courseid = ?"; $params = array($userid, $courseid); - if (!$rs = $DB->get_recordset_sql($sql, $params)) { + $rs = $DB->get_recordset_sql($sql, $params); + + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return array('0' => array()); } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index dd9bbef8619..e7fe450c36c 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8012,14 +8012,13 @@ function notify_login_failures() { GROUP BY ip HAVING COUNT(*) >= ?"; $params = array($CFG->lastnotifyfailure, $CFG->notifyloginthreshold); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $iprec) { - if (!empty($iprec->ip)) { - set_cache_flag('login_failure_by_ip', $iprec->ip, '1', 0); - } + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $iprec) { + if (!empty($iprec->ip)) { + set_cache_flag('login_failure_by_ip', $iprec->ip, '1', 0); } - $rs->close(); } + $rs->close(); /// Get all the INFOs with more than notifyloginthreshold failures since lastnotifyfailure /// and insert them into the cache_flags temp table @@ -8030,14 +8029,13 @@ function notify_login_failures() { GROUP BY info HAVING count(*) >= ?"; $params = array($CFG->lastnotifyfailure, $CFG->notifyloginthreshold); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $inforec) { - if (!empty($inforec->info)) { - set_cache_flag('login_failure_by_info', $inforec->info, '1', 0); - } + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $inforec) { + if (!empty($inforec->info)) { + set_cache_flag('login_failure_by_info', $inforec->info, '1', 0); } - $rs->close(); } + $rs->close(); /// Now, select all the login error logged records belonging to the ips and infos /// since lastnotifyfailure, that we have stored in the cache_flags table @@ -8063,14 +8061,13 @@ function notify_login_failures() { $count = 0; $messages = ''; /// Iterate over the logs recordset - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $log) { - $log->time = userdate($log->time); - $messages .= get_string('notifyloginfailuresmessage','',$log)."\n"; - $count++; - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $log) { + $log->time = userdate($log->time); + $messages .= get_string('notifyloginfailuresmessage','',$log)."\n"; + $count++; } + $rs->close(); /// If we haven't run in the last hour and /// we have something useful to report and we From 419178d71357fcafd27f2ba4ad3cdca6ace627d9 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 2 Jan 2011 16:39:29 +0100 Subject: [PATCH 06/22] MDL-25708 recordsets - fix completion --- .../completion_criteria_activity.php | 12 +++++------ lib/completion/completion_criteria_course.php | 12 +++++------ lib/completion/completion_criteria_date.php | 13 +++++------- .../completion_criteria_duration.php | 21 ++++++++----------- lib/completion/completion_criteria_grade.php | 13 +++++------- lib/completion/cron.php | 12 ++++++++--- 6 files changed, 38 insertions(+), 45 deletions(-) diff --git a/lib/completion/completion_criteria_activity.php b/lib/completion/completion_criteria_activity.php index feb5aa67f9b..803dd94750c 100644 --- a/lib/completion/completion_criteria_activity.php +++ b/lib/completion/completion_criteria_activity.php @@ -228,15 +228,13 @@ class completion_criteria_activity extends completion_criteria { '; // Loop through completions, and mark as complete - if ($rs = $DB->get_recordset_sql($sql)) { - foreach ($rs as $record) { + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $record) { - $completion = new completion_criteria_completion((array)$record); - $completion->mark_complete($record->timecompleted); - } - - $rs->close(); + $completion = new completion_criteria_completion((array)$record); + $completion->mark_complete($record->timecompleted); } + $rs->close(); } /** diff --git a/lib/completion/completion_criteria_course.php b/lib/completion/completion_criteria_course.php index afafeb52cbf..2107c213fa6 100644 --- a/lib/completion/completion_criteria_course.php +++ b/lib/completion/completion_criteria_course.php @@ -185,14 +185,12 @@ class completion_criteria_course extends completion_criteria { "; // Loop through completions, and mark as complete - if ($rs = $DB->get_recordset_sql($sql)) { - foreach ($rs as $record) { - $completion = new completion_criteria_completion((array)$record); - $completion->mark_complete($record->timecompleted); - } - - $rs->close(); + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $record) { + $completion = new completion_criteria_completion((array)$record); + $completion->mark_complete($record->timecompleted); } + $rs->close(); } /** diff --git a/lib/completion/completion_criteria_date.php b/lib/completion/completion_criteria_date.php index 9551a8826c0..9bd66a2a6fb 100644 --- a/lib/completion/completion_criteria_date.php +++ b/lib/completion/completion_criteria_date.php @@ -178,15 +178,12 @@ class completion_criteria_date extends completion_criteria { '; // Loop through completions, and mark as complete - if ($rs = $DB->get_recordset_sql($sql, array(time()))) { - foreach ($rs as $record) { - - $completion = new completion_criteria_completion((array)$record); - $completion->mark_complete($record->timeend); - } - - $rs->close(); + $rs = $DB->get_recordset_sql($sql, array(time())); + foreach ($rs as $record) { + $completion = new completion_criteria_completion((array)$record); + $completion->mark_complete($record->timeend); } + $rs->close(); } /** diff --git a/lib/completion/completion_criteria_duration.php b/lib/completion/completion_criteria_duration.php index 96847ecf362..bb4ab66abb0 100644 --- a/lib/completion/completion_criteria_duration.php +++ b/lib/completion/completion_criteria_duration.php @@ -219,22 +219,19 @@ class completion_criteria_duration extends completion_criteria { // Loop through completions, and mark as complete $now = time(); - if ($rs = $DB->get_recordset_sql($sql, array($now, $now))) { - foreach ($rs as $record) { + $rs = $DB->get_recordset_sql($sql, array($now, $now)); + foreach ($rs as $record) { - $completion = new completion_criteria_completion((array)$record); + $completion = new completion_criteria_completion((array)$record); - // Use time start if not 0, otherwise use timeenrolled - if ($record->otimestart) { - $completion->mark_complete($record->ctimestart); - } - else { - $completion->mark_complete($record->ctimeenrolled); - } + // Use time start if not 0, otherwise use timeenrolled + if ($record->otimestart) { + $completion->mark_complete($record->ctimestart); + } else { + $completion->mark_complete($record->ctimeenrolled); } - - $rs->close(); } + $rs->close(); } /** diff --git a/lib/completion/completion_criteria_grade.php b/lib/completion/completion_criteria_grade.php index 985942a2c38..00e75aba568 100644 --- a/lib/completion/completion_criteria_grade.php +++ b/lib/completion/completion_criteria_grade.php @@ -214,15 +214,12 @@ class completion_criteria_grade extends completion_criteria { '; // Loop through completions, and mark as complete - if ($rs = $DB->get_recordset_sql($sql)) { - foreach ($rs as $record) { - - $completion = new completion_criteria_completion((array)$record); - $completion->mark_complete($record->timecompleted); - } - - $rs->close(); + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $record) { + $completion = new completion_criteria_completion((array)$record); + $completion->mark_complete($record->timecompleted); } + $rs->close(); } /** diff --git a/lib/completion/cron.php b/lib/completion/cron.php index 1c30809f220..281a9d76d25 100644 --- a/lib/completion/cron.php +++ b/lib/completion/cron.php @@ -118,9 +118,12 @@ function completion_cron_mark_started() { userid "; - // Check if result is empty $now = time(); - if (!$rs = $DB->get_recordset_sql($sql, array($now, $now, $now, $now))) { + $rs = $DB->get_recordset_sql($sql, array($now, $now, $now, $now)); + + // Check if result is empty + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return; } @@ -267,8 +270,11 @@ function completion_cron_completions() { userid '; + $rs = $DB->get_recordset_sql($sql, array('timestarted' => $timestarted)); + // Check if result is empty - if (!$rs = $DB->get_recordset_sql($sql, array('timestarted' => $timestarted))) { + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return; } From 5a4a7b425b6e7a7c590743df69791b322c151f39 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 2 Jan 2011 17:12:17 +0100 Subject: [PATCH 07/22] MDL-25708 recordsets - fix upgradelib and dml stuff --- lib/db/upgradelib.php | 47 +++++++++++++------------- lib/dml/moodle_database.php | 10 +++--- lib/dml/oci_native_moodle_database.php | 13 ++++--- lib/dml/pdo_moodle_database.php | 8 +++-- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index 0653ef766ff..acfd2d90873 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -38,15 +38,14 @@ function upgrade_fix_category_depths() { $sql = "SELECT c.id FROM {course_categories} c WHERE c.parent > 0 AND c.parent NOT IN (SELECT pc.id FROM {course_categories} pc)"; - if ($rs = $DB->get_recordset_sql($sql)) { - foreach ($rs as $cat) { - $cat->depth = 1; - $cat->path = '/'.$cat->id; - $cat->parent = 0; - $DB->update_record('course_categories', $cat); - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $cat) { + $cat->depth = 1; + $cat->path = '/'.$cat->id; + $cat->parent = 0; + $DB->update_record('course_categories', $cat); } + $rs->close(); // now add path and depth to top level categories $sql = "UPDATE {course_categories} @@ -60,14 +59,13 @@ function upgrade_fix_category_depths() { $sql = "SELECT c.id, pc.path FROM {course_categories} c, {course_categories} pc WHERE c.parent=pc.id AND c.depth=0 AND pc.depth=?"; - if ($rs = $DB->get_recordset_sql($sql, array($parentdepth))) { - foreach ($rs as $cat) { - $cat->depth = $parentdepth+1; - $cat->path = $cat->path.'/'.$cat->id; - $DB->update_record('course_categories', $cat); - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, array($parentdepth)); + foreach ($rs as $cat) { + $cat->depth = $parentdepth+1; + $cat->path = $cat->path.'/'.$cat->id; + $DB->update_record('course_categories', $cat); } + $rs->close(); $parentdepth++; if ($parentdepth > 100) { //something must have gone wrong - nobody can have more than 100 levels of categories, right? @@ -378,7 +376,9 @@ function upgrade_migrate_files_blog() { $count = $DB->count_records_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'"); - if ($rs = $DB->get_recordset_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'")) { + $rs = $DB->get_recordset_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'"); + + if ($rs->valid()) { upgrade_set_timeout(60*20); // set up timeout, may also abort execution @@ -419,8 +419,8 @@ function upgrade_migrate_files_blog() { $DB->update_record('post', $entry); $pbar->update($i, $count, "Migrated blog attachments - $i/$count."); } - $rs->close(); } + $rs->close(); @rmdir("$CFG->dataroot/blog/attachments/"); @rmdir("$CFG->dataroot/blog/"); @@ -539,13 +539,12 @@ function upgrade_fix_incorrect_mnethostids() { $params = array_merge($in_params, array($current_mnet_localhost_host->id)); - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $rec) { - $DB->set_field('user', 'mnethostid', $current_mnet_localhost_host->id, array('id' => $rec->id)); - upgrade_set_timeout(60); /// Give upgrade at least 60 more seconds - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $rec) { + $DB->set_field('user', 'mnethostid', $current_mnet_localhost_host->id, array('id' => $rec->id)); + upgrade_set_timeout(60); /// Give upgrade at least 60 more seconds } + $rs->close(); // fix up any host records that have incorrect ids $DB->set_field_select('mnet_host', 'applicationid', $moodleapplicationid, 'id = ? or id = ?', array($current_mnet_localhost_host->id, $current_mnet_all_hosts_host->id)); @@ -642,4 +641,4 @@ function update_fix_automated_backup_config() { unset_config('backup_sche_gradebook_history'); unset_config('disablescheduleddbackups'); -} \ No newline at end of file +} diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 3fa9b29fc0c..b21edf11abd 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -1550,12 +1550,10 @@ abstract class moodle_database { * @throws dml_exception if error */ public function record_exists_sql($sql, array $params=null) { - if ($mrs = $this->get_recordset_sql($sql, $params, 0, 1)) { - $return = $mrs->valid(); - $mrs->close(); - return $return; - } - return false; + $mrs = $this->get_recordset_sql($sql, $params, 0, 1); + $return = $mrs->valid(); + $mrs->close(); + return $return; } /** diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index 7da58101235..1ab255f9781 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -968,15 +968,14 @@ class oci_native_moodle_database extends moodle_database { $strictness = (int)$strictness; if ($strictness == IGNORE_MULTIPLE) { // do not limit here - ORA does not like that - if (!$rs = $this->get_recordset_sql($sql, $params)) { - return false; - } - foreach ($rs as $result) { - $rs->close(); - return $result; + $rs = $this->get_recordset_sql($sql, $params); + $result = false; + foreach ($rs as $rec) { + $result = $rec; + break; } $rs->close(); - return false; + return $result; } return parent::get_record_sql($sql, $params, $strictness); } diff --git a/lib/dml/pdo_moodle_database.php b/lib/dml/pdo_moodle_database.php index 9af61935092..2cc877083f2 100644 --- a/lib/dml/pdo_moodle_database.php +++ b/lib/dml/pdo_moodle_database.php @@ -287,7 +287,9 @@ abstract class pdo_moodle_database extends moodle_database { * @return array of values */ public function get_fieldset_sql($sql, array $params=null) { - if(!$rs = $this->get_recordset_sql($sql, $params)) { + $rs = $this->get_recordset_sql($sql, $params); + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return false; } $result = array(); @@ -312,7 +314,9 @@ abstract class pdo_moodle_database extends moodle_database { * @return array of objects, or empty array if no records were found, or false if an error occurred. */ public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) { - if(!$rs = $this->get_recordset_sql($sql, $params, $limitfrom, $limitnum)) { + $rs = $this->get_recordset_sql($sql, $params, $limitfrom, $limitnum); + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return false; } $objects = array(); From be981316f40282683509d15ecbadf92455e0cc90 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 3 Jan 2011 17:23:47 +0100 Subject: [PATCH 08/22] MDL-25708 recordsets - fix file_storage --- lib/filestorage/file_storage.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index 679cac1e18c..383da522e14 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -1214,6 +1214,7 @@ class file_storage { foreach ($rs as $dir) { $this->delete_area_files($dir->contextid, $dir->component, $dir->filearea, $dir->itemid); } + $rs->close(); mtrace('done.'); // remove trash pool files once a day @@ -1226,12 +1227,14 @@ class file_storage { FROM {files} f LEFT OUTER JOIN {context} c ON f.contextid = c.id WHERE c.id IS NULL"; - if ($rs = $DB->get_recordset_sql($sql)) { + $rs = $DB->get_recordset_sql($sql); + if ($rs->valid()) { $fs = get_file_storage(); foreach ($rs as $ctx) { $fs->delete_area_files($ctx->contextid); } } + $rs->close(); mtrace('done.'); mtrace('Deleting trash files... ', ''); From 1b42e67747c93ab3b2c0b453e6812d3b372f9556 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 3 Jan 2011 17:31:02 +0100 Subject: [PATCH 09/22] MDL-25708 recordsets - fix lib/grade/xxx stuff --- lib/grade/grade_category.php | 5 +++-- lib/grade/grade_grade.php | 13 ++++++------- lib/grade/grade_item.php | 5 +++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 98cbb6caa63..abe61205271 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -481,7 +481,8 @@ class grade_category extends grade_object { ORDER BY g.userid"; // group the results by userid and aggregate the grades for this user - if ($rs = $DB->get_recordset_sql($sql, $params)) { + $rs = $DB->get_recordset_sql($sql, $params); + if ($rs->valid()) { $prevuser = 0; $grade_values = array(); $excluded = array(); @@ -506,9 +507,9 @@ class grade_category extends grade_object { $oldgrade = $used; } } - $rs->close(); $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one } + $rs->close(); return true; } diff --git a/lib/grade/grade_grade.php b/lib/grade/grade_grade.php index 1b710aefc02..97c4233711f 100644 --- a/lib/grade/grade_grade.php +++ b/lib/grade/grade_grade.php @@ -390,14 +390,13 @@ class grade_grade extends grade_object { $now = time(); // no rounding needed, this is not supposed to be called every 10 seconds list($usql, $params) = $DB->get_in_or_equal($items); $params[] = $now; - if ($rs = $DB->get_recordset_select('grade_grades', "itemid $usql AND locked = 0 AND locktime > 0 AND locktime < ?", $params)) { - foreach ($rs as $grade) { - $grade_grade = new grade_grade($grade, false); - $grade_grade->locked = time(); - $grade_grade->update('locktime'); - } - $rs->close(); + $rs = $DB->get_recordset_select('grade_grades', "itemid $usql AND locked = 0 AND locktime > 0 AND locktime < ?", $params); + foreach ($rs as $grade) { + $grade_grade = new grade_grade($grade, false); + $grade_grade->locked = time(); + $grade_grade->update('locktime'); } + $rs->close(); } /** diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index f4301ec5988..630e915694c 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -1722,7 +1722,8 @@ class grade_item extends grade_object { $return = true; // group the grades by userid and use formula on the group - if ($rs = $DB->get_recordset_sql($sql, $params)) { + $rs = $DB->get_recordset_sql($sql, $params); + if ($rs->valid()) { $prevuser = 0; $grade_records = array(); $oldgrade = null; @@ -1740,11 +1741,11 @@ class grade_item extends grade_object { } $grade_records['gi'.$used->itemid] = $used->finalgrade; } - $rs->close(); if (!$this->use_formula($prevuser, $grade_records, $useditems, $oldgrade)) { $return = false; } } + $rs->close(); return $return; } From 20222a3d7b6cc632ede580fd6c17cf2846b89a08 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 4 Jan 2011 17:36:47 +0100 Subject: [PATCH 10/22] MDL-25708 recordsets - fix message/lib.php --- message/lib.php | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/message/lib.php b/message/lib.php index 4409bf943b8..4bf9a7f8bbb 100644 --- a/message/lib.php +++ b/message/lib.php @@ -269,24 +269,21 @@ function message_get_contacts($user1=null, &$user2=null) { GROUP BY $userfields ORDER BY u.firstname ASC"; - if ($rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id))){ - foreach($rs as $rd){ + $rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id)); + foreach ($rs as $rd) { + if ($rd->lastaccess >= $timefrom) { + // they have been active recently, so are counted online + $onlinecontacts[] = $rd; - if($rd->lastaccess >= $timefrom){ - // they have been active recently, so are counted online - $onlinecontacts[] = $rd; + } else { + $offlinecontacts[] = $rd; + } - }else{ - $offlinecontacts[] = $rd; - } - - if (!empty($user2) && $user2->id==$rd->id) { - $user2->iscontact = true; - } + if (!empty($user2) && $user2->id==$rd->id) { + $user2->iscontact = true; } - unset($rd); - $rs->close(); } + $rs->close(); // get messages from anyone who isn't in our contact list and count the number // of messages we have from each of them @@ -298,13 +295,11 @@ function message_get_contacts($user1=null, &$user2=null) { GROUP BY $userfields ORDER BY u.firstname ASC"; - if($rs = $DB->get_recordset_sql($strangersql, array($USER->id))){ - foreach($rs as $rd){ - $strangers[] = $rd; - } - unset($rd); - $rs->close(); + $rs = $DB->get_recordset_sql($strangersql, array($USER->id)); + foreach ($rs as $rd) { + $strangers[] = $rd; } + $rs->close(); return array($onlinecontacts, $offlinecontacts, $strangers); } From 6b1b1d0339fa1c1bd081109a3d853f173112a063 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 7 Jan 2011 11:12:50 +0100 Subject: [PATCH 11/22] MDL-25708 recordsets - assignment and data modules --- mod/assignment/db/upgrade.php | 7 ++-- mod/assignment/lib.php | 5 ++- mod/data/db/upgrade.php | 68 +++++++++++++++++----------------- mod/data/lib.php | 69 ++++++++++++++++++----------------- 4 files changed, 76 insertions(+), 73 deletions(-) diff --git a/mod/assignment/db/upgrade.php b/mod/assignment/db/upgrade.php index d5e891cb306..2841111f860 100644 --- a/mod/assignment/db/upgrade.php +++ b/mod/assignment/db/upgrade.php @@ -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'); } diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 634f663134e..9c51c0942db 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -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(); } /** diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index 1d85846155e..371da31f4ee 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -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); } diff --git a/mod/data/lib.php b/mod/data/lib.php index a22aa6b1ff2..534e424437d 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -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"); - } - $notenrolled[$record->userid] = true; + $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"); + } + $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 From 05c707dacc8b495e63fd7b2e254362ebdc2e5806 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 10 Jan 2011 15:09:37 +0100 Subject: [PATCH 12/22] MDL-25708 recordsets - folder module --- mod/folder/db/upgradelib.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mod/folder/db/upgradelib.php b/mod/folder/db/upgradelib.php index 016f079ad78..0be56e1c70e 100644 --- a/mod/folder/db/upgradelib.php +++ b/mod/folder/db/upgradelib.php @@ -48,7 +48,10 @@ function folder_20_migrate() { return; } - if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'directory', 'migrated'=>0))) { + $candidates = $DB->get_recordset('resource_old', array('type'=>'directory', 'migrated'=>0)); + + if (!$candidates->valid()) { + $candidates->close(); // Not going to iterate (but exit), close rs return; } @@ -101,4 +104,4 @@ function folder_20_migrate() { // clear all course modinfo caches rebuild_course_cache(0, true); -} \ No newline at end of file +} From 56d1c48f24899503e7681cf5991ea7cfa27d7576 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 11 Jan 2011 15:17:55 +0100 Subject: [PATCH 13/22] MDL-25708 recordsets - forum module --- mod/forum/db/upgrade.php | 5 +++-- mod/forum/lib.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index a4f310615d0..3a567553f46 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -104,7 +104,8 @@ function xmldb_forum_upgrade($oldversion) { $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom"); - if ($rs = $DB->get_recordset_sql("SELECT p.id, p.attachment, p.userid, d.forum, f.course, cm.id AS cmid $sqlfrom ORDER BY f.course, f.id, d.id")) { + $rs = $DB->get_recordset_sql("SELECT p.id, p.attachment, p.userid, d.forum, f.course, cm.id AS cmid $sqlfrom ORDER BY f.course, f.id, d.id"); + if ($rs->valid()) { $pbar = new progress_bar('migrateforumfiles', 500, true); @@ -146,8 +147,8 @@ function xmldb_forum_upgrade($oldversion) { @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum/$post->forum"); @rmdir("$CFG->dataroot/$post->course/$CFG->moddata/forum"); } - $rs->close(); } + $rs->close(); upgrade_mod_savepoint(true, 2008081900, 'forum'); } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 8ffaaa9961b..8d74f63479a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1504,7 +1504,8 @@ function forum_upgrade_grades() { $sql = "SELECT f.*, cm.idnumber AS cmidnumber, f.course AS courseid FROM {forum} f, {course_modules} cm, {modules} m WHERE m.name='forum' AND m.id=cm.module AND cm.instance=f.id"; - if ($rs = $DB->get_recordset_sql($sql)) { + $rs = $DB->get_recordset_sql($sql); + if ($rs->valid()) { $pbar = new progress_bar('forumupgradegrades', 500, true); $i=0; foreach ($rs as $forum) { @@ -1513,8 +1514,8 @@ function forum_upgrade_grades() { forum_update_grades($forum, 0, false); $pbar->update($i, $count, "Updating Forum grades ($i/$count)."); } - $rs->close(); } + $rs->close(); } /** From ec577b05385bc5992cb5ec8e260d1c7ec8394d40 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 11 Jan 2011 15:27:42 +0100 Subject: [PATCH 14/22] MDL-25708 recordsets - glossary module --- mod/glossary/db/upgrade.php | 47 +++++++++++++++++++------------------ mod/glossary/lib.php | 10 ++++---- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index f6507cfe0d1..722bdf1413f 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -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); } diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 858093a0b70..498036b9c29 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -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 From a8307c22c22ef171555b3b04b959344e167007cb Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Wed, 12 Jan 2011 15:31:04 +0100 Subject: [PATCH 15/22] MDL-25708 recordsets - imscp and lesson modules --- mod/imscp/db/upgradelib.php | 5 +++-- mod/lesson/lib.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mod/imscp/db/upgradelib.php b/mod/imscp/db/upgradelib.php index e905f397c51..5138318e6e7 100644 --- a/mod/imscp/db/upgradelib.php +++ b/mod/imscp/db/upgradelib.php @@ -49,7 +49,9 @@ function imscp_20_migrate() { return; } - if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'ims', 'migrated'=>0))) { + $candidates = $DB->get_recordset('resource_old', array('type'=>'ims', 'migrated'=>0)); + if (!$candidates->valid()) { + $candidates->close(); // Not going to iterate (but exit), close rs return; } @@ -133,7 +135,6 @@ function imscp_20_migrate() { fulldelete($root); } } - $candidates->close(); // clear all course modinfo caches diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 3bfaa4178be..ed5a0a0c79f 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -429,7 +429,8 @@ function lesson_upgrade_grades() { $sql = "SELECT l.*, cm.idnumber AS cmidnumber, l.course AS courseid FROM {lesson} l, {course_modules} cm, {modules} m WHERE m.name='lesson' AND m.id=cm.module AND cm.instance=l.id"; - if ($rs = $DB->get_recordset_sql($sql)) { + $rs = $DB->get_recordset_sql($sql); + if ($rs->valid()) { $pbar = new progress_bar('lessonupgradegrades', 500, true); $i=0; foreach ($rs as $lesson) { @@ -438,8 +439,8 @@ function lesson_upgrade_grades() { lesson_update_grades($lesson, 0, false); $pbar->update($i, $count, "Updating Lesson grades ($i/$count)."); } - $rs->close(); } + $rs->close(); } /** From 3f2efc20addb0cc8ee5374a8e4011faaf7aa4ccf Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Wed, 12 Jan 2011 15:35:33 +0100 Subject: [PATCH 16/22] MDL-25708 recordsets - page and quiz modules --- mod/page/db/upgradelib.php | 26 ++++++++++++-------------- mod/quiz/lib.php | 5 +++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/mod/page/db/upgradelib.php b/mod/page/db/upgradelib.php index 3a159b61d16..3ffeb7aa366 100644 --- a/mod/page/db/upgradelib.php +++ b/mod/page/db/upgradelib.php @@ -51,24 +51,22 @@ function page_20_migrate() { $fs = get_file_storage(); - if ($candidates = $DB->get_recordset('resource_old', array('type'=>'html', 'migrated'=>0))) { - foreach ($candidates as $candidate) { - page_20_migrate_candidate($candidate, $fs, FORMAT_HTML); - } - $candidates->close(); + $candidates = $DB->get_recordset('resource_old', array('type'=>'html', 'migrated'=>0)); + foreach ($candidates as $candidate) { + page_20_migrate_candidate($candidate, $fs, FORMAT_HTML); } + $candidates->close(); - if ($candidates = $DB->get_recordset('resource_old', array('type'=>'text', 'migrated'=>0))) { - foreach ($candidates as $candidate) { - //there might be some rubbish instead of format int value - $format = (int)$candidate->reference; - if ($format < 0 or $format > 4) { - $format = FORMAT_MOODLE; - } - page_20_migrate_candidate($candidate, $fs, $format); + $candidates = $DB->get_recordset('resource_old', array('type'=>'text', 'migrated'=>0)); + foreach ($candidates as $candidate) { + //there might be some rubbish instead of format int value + $format = (int)$candidate->reference; + if ($format < 0 or $format > 4) { + $format = FORMAT_MOODLE; } - $candidates->close(); + page_20_migrate_candidate($candidate, $fs, $format); } + $candidates->close(); // clear all course modinfo caches rebuild_course_cache(0, true); diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index a3078e5c6ec..0030c2a0bd6 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -604,7 +604,8 @@ function quiz_upgrade_grades() { $sql = "SELECT a.*, cm.idnumber AS cmidnumber, a.course AS courseid FROM {quiz} a, {course_modules} cm, {modules} m WHERE m.name='quiz' 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()) { $pbar = new progress_bar('quizupgradegrades', 500, true); $i=0; foreach ($rs as $quiz) { @@ -613,8 +614,8 @@ function quiz_upgrade_grades() { quiz_update_grades($quiz, 0, false); $pbar->update($i, $count, "Updating Quiz grades ($i/$count)."); } - $rs->close(); } + $rs->close(); } /** From 8ce9a2b26d2cbfc6b1bf60cb699fae1fc644176e Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 13 Jan 2011 15:39:27 +0100 Subject: [PATCH 17/22] MDL-25708 recordsets - resource module --- mod/resource/db/upgrade.php | 20 ++++++++++---------- mod/resource/db/upgradelib.php | 4 +++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/mod/resource/db/upgrade.php b/mod/resource/db/upgrade.php index dc2982554b9..f5478ba2376 100644 --- a/mod/resource/db/upgrade.php +++ b/mod/resource/db/upgrade.php @@ -226,17 +226,17 @@ function xmldb_resource_upgrade($oldversion) { JOIN {modules} m ON m.name='resource' JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = r.id)"; - if ($instances = $DB->get_recordset_sql($sql)) { - foreach ($instances as $instance) { - $context = get_context_instance(CONTEXT_MODULE, $instance->cmid); - $component = 'mod_resource'; - $filearea = 'content'; - $itemid = 0; - $filepath = file_correct_filepath(dirname($instance->mainfile)); - $filename = basename($instance->mainfile); - file_set_sortorder($context->id, $component, $filearea, $itemid, $filepath, $filename, 1); - } + $instances = $DB->get_recordset_sql($sql); + foreach ($instances as $instance) { + $context = get_context_instance(CONTEXT_MODULE, $instance->cmid); + $component = 'mod_resource'; + $filearea = 'content'; + $itemid = 0; + $filepath = file_correct_filepath(dirname($instance->mainfile)); + $filename = basename($instance->mainfile); + file_set_sortorder($context->id, $component, $filearea, $itemid, $filepath, $filename, 1); } + $instances->close(); /// Define field mainfile to be dropped from resource $table = new xmldb_table('resource'); diff --git a/mod/resource/db/upgradelib.php b/mod/resource/db/upgradelib.php index df04b0c4701..41626142ed6 100644 --- a/mod/resource/db/upgradelib.php +++ b/mod/resource/db/upgradelib.php @@ -42,7 +42,9 @@ function resource_20_migrate() { $withrelativelinks = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/x-shockwave-flash'); // note: pdf doc and other types may contain links too, but we do not support relative links there - if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0))) { + $candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0)); + if (!$candidates->valid()) { + $candidates->close(); // Not going to iterate (but exit), close rs return; } From 676874df1d24d1e5e62c4b87aa71ccaa9eb3f9c4 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 13 Jan 2011 15:43:48 +0100 Subject: [PATCH 18/22] MDL-25708 recordsets - scorm module --- mod/scorm/db/upgrade.php | 5 +++-- mod/scorm/lib.php | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mod/scorm/db/upgrade.php b/mod/scorm/db/upgrade.php index ba04865f9c3..445799f14ed 100644 --- a/mod/scorm/db/upgrade.php +++ b/mod/scorm/db/upgrade.php @@ -140,7 +140,8 @@ function xmldb_scorm_upgrade($oldversion) { $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom"); - if ($rs = $DB->get_recordset_sql("SELECT s.id, s.scormtype, s.reference, s.course, cm.id AS cmid $sqlfrom ORDER BY s.course, s.id")) { + $rs = $DB->get_recordset_sql("SELECT s.id, s.scormtype, s.reference, s.course, cm.id AS cmid $sqlfrom ORDER BY s.course, s.id"); + if ($rs->valid()) { $pbar = new progress_bar('migratescormfiles', 500, true); @@ -213,8 +214,8 @@ function xmldb_scorm_upgrade($oldversion) { @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/"); @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/"); } - $rs->close(); } + $rs->close(); /// scorm savepoint reached upgrade_mod_savepoint(true, 2008090304, 'scorm'); diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index 6c985f8b220..88ecd280917 100755 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -575,7 +575,8 @@ function scorm_upgrade_grades() { $sql = "SELECT s.*, cm.idnumber AS cmidnumber, s.course AS courseid FROM {scorm} s, {course_modules} cm, {modules} m WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id"; - if ($rs = $DB->get_recordset_sql($sql)) { + $rs = $DB->get_recordset_sql($sql); + if ($rs->valid()) { $pbar = new progress_bar('scormupgradegrades', 500, true); $i=0; foreach ($rs as $scorm) { @@ -584,8 +585,8 @@ function scorm_upgrade_grades() { scorm_update_grades($scorm, 0, false); $pbar->update($i, $count, "Updating Scorm grades ($i/$count)."); } - $rs->close(); } + $rs->close(); } /** From a1d870aa95dc5f49d7d6a967f9682b564efa20aa Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 14 Jan 2011 15:51:53 +0100 Subject: [PATCH 19/22] MDL-25708 recordsets - survey module --- mod/survey/lib.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mod/survey/lib.php b/mod/survey/lib.php index fce3534e8ad..fa51e1692f1 100644 --- a/mod/survey/lib.php +++ b/mod/survey/lib.php @@ -232,13 +232,15 @@ function survey_print_recent_activity($course, $viewfullnames, $timestart) { $slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right? - if (!$rs = $DB->get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time, - u.firstname, u.lastname, u.email, u.picture - FROM {survey_answers} sa - JOIN {user} u ON u.id = sa.userid - WHERE sa.survey IN ($slist) AND sa.time > ? - GROUP BY sa.userid, sa.survey, u.firstname, u.lastname, u.email, u.picture - ORDER BY time ASC", array($timestart))) { + $rs = $DB->get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time, + u.firstname, u.lastname, u.email, u.picture + FROM {survey_answers} sa + JOIN {user} u ON u.id = sa.userid + WHERE sa.survey IN ($slist) AND sa.time > ? + GROUP BY sa.userid, sa.survey, u.firstname, u.lastname, u.email, u.picture + ORDER BY time ASC", array($timestart)); + if (!$rs->valid()) { + $rs->close(); // Not going to iterate (but exit), close rs return false; } From cfd62f13a1f85b53d73004c5982a3f8e1910ec27 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sat, 22 Jan 2011 12:54:40 +0100 Subject: [PATCH 20/22] MDL-25708 recordsets - url module --- mod/url/db/upgradelib.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mod/url/db/upgradelib.php b/mod/url/db/upgradelib.php index 8ff7adcf64a..8478fafd118 100644 --- a/mod/url/db/upgradelib.php +++ b/mod/url/db/upgradelib.php @@ -50,7 +50,9 @@ function url_20_migrate() { return; } - if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0))) { + $candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0)); + if (!$candidates->valid()) { + $candidates->close(); // Not going to iterate (but exit), close rs return; } @@ -135,4 +137,4 @@ function url_20_migrate() { // clear all course modinfo caches rebuild_course_cache(0, true); -} \ No newline at end of file +} From cd5fdfd87a910bda6a1caad185d25103b54ccca4 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sat, 22 Jan 2011 13:34:28 +0100 Subject: [PATCH 21/22] MDL-25708 recordsets - tag and user directory --- tag/lib.php | 15 +++++++-------- user/selector/lib.php | 9 ++++----- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tag/lib.php b/tag/lib.php index ae9e4c0c646..c1d96c30696 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -409,16 +409,15 @@ function tag_get_id($tags, $return_value=null) { list($tag_string, $params) = $DB->get_in_or_equal($tags); - if ($rs = $DB->get_recordset_sql("SELECT * FROM {tag} WHERE name $tag_string ORDER BY name", $params)) { - foreach ($rs as $record) { - if ($return_value == TAG_RETURN_OBJECT) { - $result[$record->name] = $record; - } else { // TAG_RETURN_ARRAY - $result[$record->name] = $record->id; - } + $rs = $DB->get_recordset_sql("SELECT * FROM {tag} WHERE name $tag_string ORDER BY name", $params); + foreach ($rs as $record) { + if ($return_value == TAG_RETURN_OBJECT) { + $result[$record->name] = $record; + } else { // TAG_RETURN_ARRAY + $result[$record->name] = $record->id; } - $rs->close(); } + $rs->close(); if ($return_an_int) { return array_pop($result); diff --git a/user/selector/lib.php b/user/selector/lib.php index c6215dbc798..0aba4e37483 100644 --- a/user/selector/lib.php +++ b/user/selector/lib.php @@ -763,12 +763,11 @@ class group_non_members_selector extends groups_user_selector_base { JOIN {groups} g ON gm.groupid = g.id WHERE u.id $membersidsclause AND g.courseid = :courseid "; $params['courseid'] = $courseid; - if ($rs = $DB->get_recordset_sql($sql, $params)) { - foreach ($rs as $usergroup) { - $usergroups[$usergroup->userid][$usergroup->id] = $usergroup; - } - $rs->close(); + $rs = $DB->get_recordset_sql($sql, $params); + foreach ($rs as $usergroup) { + $usergroups[$usergroup->userid][$usergroup->id] = $usergroup; } + $rs->close(); foreach ($potentialmembersids as $userid) { if (isset($usergroups[$userid])) { From cc65dc366d5e6620e8253792fa917801e8cbe927 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 30 Jan 2011 13:43:06 +0100 Subject: [PATCH 22/22] MDL-25708 recordsets - found one more recent use. fixed. --- grade/report/user/lib.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/grade/report/user/lib.php b/grade/report/user/lib.php index 6d66a6e31e3..7a84094a356 100644 --- a/grade/report/user/lib.php +++ b/grade/report/user/lib.php @@ -634,12 +634,11 @@ class grade_report_user extends grade_report { GROUP BY gg.itemid"; $sum_array = array(); - if ($sums = $DB->get_recordset_sql($sql, $params)) { - foreach ($sums as $itemid => $csum) { - $sum_array[$itemid] = $csum->sum; - } - $sums->close(); + $sums = $DB->get_recordset_sql($sql, $params); + foreach ($sums as $itemid => $csum) { + $sum_array[$itemid] = $csum->sum; } + $sums->close(); $columncount=0;