]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13936 fixed group handling when adding new discussions; merged from MOODLE_19_STABLE
authorskodak <skodak>
Mon, 14 Apr 2008 19:05:36 +0000 (19:05 +0000)
committerskodak <skodak>
Mon, 14 Apr 2008 19:05:36 +0000 (19:05 +0000)
mod/forum/lib.php
mod/forum/post.php
mod/forum/post_form.php
mod/forum/view.php

index 32ce89e5aff0fe637482861fcb0f1a99f247a1f1..7c856280d6d7f5e258be6e94432c58d316068398 100644 (file)
@@ -63,6 +63,7 @@ function forum_add_instance($forum) {
         $discussion->assessed = $forum->assessed;
         $discussion->format   = $forum->type;
         $discussion->mailnow  = false;
+        $discussion->groupid  = -1;
 
         if (! forum_add_discussion($discussion, $discussion->intro)) {
             error('Could not add the discussion for this forum');
@@ -4194,7 +4195,7 @@ function forum_user_has_posted($forumid, $did, $userid) {
 /**
  *
  */
-function forum_user_can_post_discussion($forum, $currentgroup=-1, $unused=-1, $cm=NULL, $context=NULL) {
+function forum_user_can_post_discussion($forum, $currentgroup=null, $unused=-1, $cm=NULL, $context=NULL) {
 // $forum is an object
     global $USER;
 
@@ -4219,7 +4220,7 @@ function forum_user_can_post_discussion($forum, $currentgroup=-1, $unused=-1, $c
         return false;
     }
 
-    if ($currentgroup == -1) {
+    if ($currentgroup === null) {
         $currentgroup = groups_get_activity_group($cm);
     }
 
index fa27d01da51e49d87020a47af1ca60065e994057..c08ccc609bbb1769660bc06cea079e252287356b 100644 (file)
@@ -13,6 +13,7 @@
     $prune   = optional_param('prune', 0, PARAM_INT);
     $name    = optional_param('name', '', PARAM_CLEAN);
     $confirm = optional_param('confirm', 0, PARAM_INT);
+    $groupid = optional_param('groupid', null, PARAM_INT);
 
 
     //these page_params will be passed as hidden variables later in the form.
@@ -80,7 +81,7 @@
 
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
 
-        if (! forum_user_can_post_discussion($forum, -1, -1, $cm)) {
+        if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) {
             if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {  // User is a guest here!
                 $SESSION->wantsurl = $FULLME;
                 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
         $post->userid     = $USER->id;
         $post->message    = '';
 
-        if ($groupmode = groups_get_activity_groupmode($cm)) {
-            $post->groupid = groups_get_activity_group($cm);
-            if (empty($post->groupid)) {
-                $post->groupid = -1;
-            }
+        if (isset($groupid)) {
+            $post->groupid = $groupid;
         } else {
-            $post->groupid = null;
+            $post->groupid = groups_get_activity_group($cm);
         }
+
         forum_set_return();
 
     } else if (!empty($reply)) {      // User is writing a new reply
             }
         }
 
-        if (groupmode($course, $cm) == SEPARATEGROUPS) {   // Make sure user can post here
+        // Make sure user can post here
+        if (groupmode($course, $cm) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
             if ($discussion->groupid == -1) {
-                if (!has_capability('moodle/site:accessallgroups', $modcontext)) {
-                    print_error('nopostforum', 'forum');
-                }
+                print_error('nopostforum', 'forum');
             } else {
                 if (!groups_is_member($discussion->groupid)) {
                     print_error('nopostforum', 'forum');
         $post->userid      = $USER->id;
         $post->message     = '';
 
-        if ($groupmode = groups_get_activity_groupmode($cm)) {
-            $post->groupid = $discussion->groupid;
-        } else {
-            $post->groupid = null;
-        }
+        $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
 
         $strre = get_string('re', 'forum');
         if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
         $post->edit   = $edit;
         $post->course = $course->id;
         $post->forum  = $forum->id;
-        if ($groupmode = groups_get_activity_groupmode($cm)) {
-            $post->groupid = $discussion->groupid;
-        } else {
-            $post->groupid = null;
-        }
+        $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
 
         trusttext_prepare_edit($post->message, $post->format, can_use_html_editor(), $modcontext);
 
         trusttext_after_edit($fromform->message, $modcontext);
 
         if ($fromform->edit) {           // Updating a post
+            unset($fromform->groupid);
             $fromform->id = $fromform->edit;
             $message = '';
 
 
 
         } else if ($fromform->discussion) { // Adding a new post to an existing discussion
+            unset($fromform->groupid);
             $message = '';
             $addpost=$fromform;
             $addpost->forum=$forum->id;
             if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
                 error('Can not add discussion, sorry.');
             }
+            if (empty($fromform->groupid)) {
+                $fromform->groupid = -1;
+            }
+
             $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
             $discussion = $fromform;
             $discussion->name  = $fromform->subject;
     if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
         error("You cannot reply to this post");
     }
-    if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, -1, -1, $cm, $modcontext)) {
+    if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
         error("You cannot start a new discussion in this forum");
     }
 
index 77cf05df0d892248ab226bf2da4a3248b90b9292..ee5a23fbcf40722fcff012daaa6dc13bba55b4d7 100644 (file)
@@ -82,8 +82,8 @@ class mod_forum_post_form extends moodleform {
             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
         }
 
-        if (groups_get_activity_groupmode($cm, $course) and !empty($post->groupid)) { // hack alert
-            if ($post->groupid == -1) {
+        if (groups_get_activity_groupmode($cm, $course)) { // hack alert
+            if (empty($post->groupid)) {
                 $groupname = get_string('allparticipants');
             } else {
                 $group = groups_get_group($post->groupid);
index 4887d1db9dd90fd73f874d132652981269ebc416..517b5d78b65b45a29529519e96a930857e9656ff 100644 (file)
                 print_box(format_text($forum->intro), 'generalbox', 'intro');
             }
             echo '<p align="center">';
-            if (forum_user_can_post_discussion($forum, -1, -1, $cm)) {
+            if (forum_user_can_post_discussion($forum, null, -1, $cm)) {
                 print_string("allowsdiscussions", "forum");
             } else {
                 echo '&nbsp;';