From c1b7a5e537c3adf72d1f502e4f1f43a969b6c5ec Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:08:12 +0000 Subject: [PATCH] accesslib: introducing make_context_subobj() - and refactor callers We had many copies of the subcontext building bit. Time to abstract it, and fix callers... --- lib/accesslib.php | 45 ++++++++++++++++++++++++--------------------- lib/datalib.php | 9 ++------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 39990bea37..d58953540d 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -833,13 +833,8 @@ function get_user_courses_bycap($userid, $cap, $sess, $doanything, $sort='c.sort if ($rs->RecordCount()) { while ($c = rs_fetch_next_record($rs)) { // build the context obj - $ctx = new StdClass; - $ctx->id = $c->ctxid; unset($c->ctxid); - $ctx->path = $c->ctxpath; unset($c->ctxpath); - $ctx->depth = $c->ctxdepth; unset($c->ctxdepth); - $ctx->instanceid = $c->id; - $ctx->contextlevel = CONTEXT_COURSE; - $c->context = $ctx; + $c = make_context_subobj($c); + if (has_cap_fromsess($cap, $ctx, $sess, $doanything)) { $courses[] = $c; if ($limit > 0 && $cc++ > $limit) { @@ -904,13 +899,8 @@ function get_context_users_byrole ($context, $roleid, $fields=NULL, $where=NULL, if ($rs->RecordCount()) { while ($u = rs_fetch_next_record($rs)) { // build the context obj - $ctx = new StdClass; - $ctx->id = $u->ctxid; unset($u->ctxid); - $ctx->path = $u->ctxpath; unset($u->ctxpath); - $ctx->depth = $u->ctxdepth; unset($u->ctxdepth); - $ctx->instanceid = $u->id; - $ctx->contextlevel = CONTEXT_USER; - $u->context = $ctx; + $u = make_context_subobj($u); + $users[] = $u; if ($limit > 0 && $cc++ > $limit) { break; @@ -1022,13 +1012,8 @@ function get_context_users_bycap ($context, $capability='moodle/course:view', $f if ($rs->RecordCount()) { while ($u = rs_fetch_next_record($rs)) { // build the context obj - $ctx = new StdClass; - $ctx->id = $u->ctxid; unset($u->ctxid); - $ctx->path = $u->ctxpath; unset($u->ctxpath); - $ctx->depth = $u->ctxdepth; unset($u->ctxdepth); - $ctx->instanceid = $u->id; - $ctx->contextlevel = CONTEXT_USER; - $u->context = $ctx; + $u = make_context_subobj($u); + $users[] = $u; if ($limit > 0 && $cc++ > $limit) { break; @@ -4617,4 +4602,22 @@ function build_context_path() { } +/** + * Turn the ctx* fields in an objectlike record + * into a context subobject. This allows + * us to SELECT from major tables JOINing with + * context at no cost, saving a ton of context + * lookups... + */ +function make_context_subobj($rec) { + $ctx = new StdClass; + $ctx->id = $rec->ctxid; unset($rec->ctxid); + $ctx->path = $rec->ctxpath; unset($rec->ctxpath); + $ctx->depth = $rec->ctxdepth; unset($rec->ctxdepth); + $ctx->instanceid = $rec->id; + $ctx->contextlevel = CONTEXT_COURSE; + $rec->context = $ctx; + return $rec; +} + ?> diff --git a/lib/datalib.php b/lib/datalib.php index 232377c424..f0e24fbb04 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -683,13 +683,8 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NUL if ($rs->RecordCount()) { while ($c = rs_fetch_next_record($rs)) { // build the context obj - $ctx = new StdClass; - $ctx->id = $c->ctxid; unset($c->ctxid); - $ctx->path = $c->ctxpath; unset($c->ctxpath); - $ctx->depth = $c->ctxdepth; unset($c->ctxdepth); - $ctx->instanceid = $c->id; - $ctx->contextlevel = CONTEXT_COURSE; - $c->context = $ctx; + $c = make_context_subobj($c); + $courses[$c->id] = $c; if ($limit > 0 && $cc++ > $limit) { break; -- 2.39.5