]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: introducing make_context_subobj() - and refactor callers
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:08:12 +0000 (07:08 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:08:12 +0000 (07:08 +0000)
We had many copies of the subcontext building bit. Time to
abstract it, and fix callers...

lib/accesslib.php
lib/datalib.php

index 39990bea3711c588c1911aec31a5e7fc94770638..d58953540dc772c3dc5d9c91df9519cd5193dfac 100755 (executable)
@@ -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;
+}
+
 ?>
index 232377c42416a18841cd183a9c10976b6eeaade0..f0e24fbb04fc5ebd6ce1f3e765e9aa894af4ee8a 100644 (file)
@@ -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;