Skip to content

Commit

Permalink
MDL-25708 recordsets - fix events/grade/group/moodle libs
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jan 25, 2011
1 parent afa559e commit b967c54
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
33 changes: 16 additions & 17 deletions lib/eventslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 17 additions & 20 deletions lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/grouplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
35 changes: 16 additions & 19 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b967c54

Please sign in to comment.