]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10990 5) eliminated reply related cap queries for guests and not-logged-in users...
authorskodak <skodak>
Tue, 26 Feb 2008 21:36:48 +0000 (21:36 +0000)
committerskodak <skodak>
Tue, 26 Feb 2008 21:36:48 +0000 (21:36 +0000)
mod/forum/discuss.php
mod/forum/lib.php
mod/forum/view.php

index 576324f2a5efa86082d562e37e17bd622d16540d..558dfa6459b7ab422deaa9340df6a70bb154f96b 100644 (file)
     }
 
     $canreply = false;
-    if (has_capability($capname, $modcontext)) {
+    if (isguestuser() or !isloggedin()) {
+        // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
+        $canreply = ($forum->type != 'news'); // no reply in news forums
+
+    } else if (has_capability($capname, $modcontext)) {
         $groupmode = groups_get_activity_groupmode($cm);
         if ($groupmode) {
             if (has_capability('moodle/site:accessallgroups', $modcontext)) {
         } else {
             $canreply = true;
         }
-    } else { // allow guests to see the link
-        $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-        if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {
-            // User is a guest here ! guests are prompted to login later if try to reply
-            $canreply = true;
-        }
     }
 
 /// Print the controls across the top
index b05ed786509681ff7caa9c767d8fb95dbcfb4812..534edfd9b20a18767e9c8df68fba2302d7a02fc3 100644 (file)
@@ -3823,16 +3823,26 @@ function forum_user_can_post_discussion($forum, $currentgroup=-1, $groupmode=-1,
 // $forum is an object
     global $USER, $SESSION, $COURSE;
 
-    if (!$cm) {
-        debugging('missing cm', DEBUG_DEVELOPER);
-        if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
-            error('Course Module ID was incorrect');
-        }
+    // shortcut - guest and not-logged-in users can not post  
+    if (isguestuser() or !isloggedin()) {
+        return false;
     }
+
     if (!$context) {
+        if (!$cm) {
+            debugging('missing cm', DEBUG_DEVELOPER);
+            if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
+                error('Course Module ID was incorrect');
+            }
+        }
         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     }
 
+    // normal users with temporary guest access can not add discussions
+    if (has_capability('moodle/legacy:guest', $context, $USER->id, false)) {
+        return false;
+    }
+
     if ($currentgroup == -1) {
         $currentgroup = get_current_group($cm->course);
     }
@@ -3881,6 +3891,15 @@ function forum_user_can_post_discussion($forum, $currentgroup=-1, $groupmode=-1,
  * @param $user - user object
  */
 function forum_user_can_post($forum, $user=NULL, $cm=NULL, $context=NULL) {
+    global $USER;
+    if (empty($user)) {
+        $user = $USER;
+    }
+
+    // shortcut - guest and not-logged-in users can not post  
+    if (isguestuser($user) or empty($user->id)) {
+        return false;
+    }
 
     if (!$context) {
         if (!$cm) {
@@ -3892,21 +3911,18 @@ function forum_user_can_post($forum, $user=NULL, $cm=NULL, $context=NULL) {
         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     }
 
+    // normal users with temporary guest access can not post
+    if (has_capability('moodle/legacy:guest', $context, $user->id, false)) {
+        return false;
+    }
+
     if ($forum->type == 'news') {
         $capname = 'mod/forum:replynews';
     } else {
         $capname = 'mod/forum:replypost';
     }
 
-    if (!empty($user)) {
-        $canreply = has_capability($capname, $context, $user->id, false)
-                && !has_capability('moodle/legacy:guest', $context, $user->id, false);
-    } else {
-        $canreply = has_capability($capname, $context, NULL, false)
-                && !has_capability('moodle/legacy:guest', $context, NULL, false);
-    }
-
-    return $canreply;
+    return has_capability($capname, $context, $user->id, false);
 }
 
 
@@ -4093,7 +4109,7 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions=5, $dis
 // and the current user is a guest.
 
     if (forum_user_can_post_discussion($forum, $currentgroup, $groupmode, $cm, $context) ||
-        ($forum->type != 'news' && has_capability('moodle/legacy:guest', $context, NULL, false)) ) {
+        ($forum->type != 'news' and (isguestuser() or !isloggedin())) ) {
 
         echo '<div class="singlebutton forumaddnew">';
         echo "<form id=\"newdiscussionform\" method=\"get\" action=\"$CFG->wwwroot/mod/forum/post.php\">";
index af53e40745ce5d1525dff5631935b35db5565e93..e79ac11a744e6ca747267323670bcddcca2ae6de 100644 (file)
                 print_box(format_text($forum->intro), 'generalbox', 'intro');
             }
             echo '<p align="center">';
-            if (forum_user_can_post_discussion($forum)) {
+            if (forum_user_can_post_discussion($forum, -1, -1, $cm)) {
                 print_string("allowsdiscussions", "forum");
             } else {
                 echo '&nbsp;';