MDL-25708 recordsets - fix accesslib and datalib uses

This commit is contained in:
Eloy Lafuente (stronk7)
2011-01-25 19:58:59 +01:00
parent fc5016fcc8
commit afa559e9bc
2 changed files with 71 additions and 83 deletions
+38 -46
View File
@@ -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;
}
/**
+33 -37
View File
@@ -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;
}