]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14113 when adding/editing posts the target group is now visible + fixed permissio...
authorskodak <skodak>
Mon, 14 Apr 2008 18:35:18 +0000 (18:35 +0000)
committerskodak <skodak>
Mon, 14 Apr 2008 18:35:18 +0000 (18:35 +0000)
mod/forum/post.php
mod/forum/post_form.php

index a9ba5d090e7be7f1100b22022a7e8c45642ba352..fa27d01da51e49d87020a47af1ca60065e994057 100644 (file)
 
         if (!empty($forum)) {      // User is starting a new discussion in a forum
             if (! $forum = get_record('forum', 'id', $forum)) {
-                print_error('The forum number was incorrect');
+                error('The forum number was incorrect');
             }
         } else if (!empty($reply)) {      // User is writing a new reply
             if (! $parent = forum_get_post_full($reply)) {
-                print_error('Parent post ID was incorrect');
+                error('Parent post ID was incorrect');
             }
             if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) {
-                print_error('This post is not part of a discussion!');
+                error('This post is not part of a discussion!');
             }
             if (! $forum = get_record('forum', 'id', $discussion->forum)) {
-                print_error('The forum number was incorrect');
+                error('The forum number was incorrect');
             }
         }
         if (! $course = get_record('course', 'id', $forum->course)) {
-            print_error('The course number was incorrect');
+            error('The course number was incorrect');
         }
 
         if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
-            print_error('Could not get the course module for the forum instance.');
+            error('Could not get the course module for the forum instance.');
         } else {
             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
         }
 
     if (!empty($forum)) {      // User is starting a new discussion in a forum
         if (! $forum = get_record("forum", "id", $forum)) {
-            print_error("The forum number was incorrect ($forum)");
+            error("The forum number was incorrect ($forum)");
         }
         if (! $course = get_record("course", "id", $forum->course)) {
-            print_error("The course number was incorrect ($forum->course)");
+            error("The course number was incorrect ($forum->course)");
         }
         if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
-            print_error("Incorrect course module");
+            error("Incorrect course module");
         }
 
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
         if ($groupmode = groups_get_activity_groupmode($cm)) {
             $post->groupid = groups_get_activity_group($cm);
             if (empty($post->groupid)) {
-                $post->groupid = -1; //TODO: why -1??
+                $post->groupid = -1;
             }
         } else {
-            $post->groupid = -1; //TODO: why -1??
+            $post->groupid = null;
         }
         forum_set_return();
 
     } else if (!empty($reply)) {      // User is writing a new reply
 
         if (! $parent = forum_get_post_full($reply)) {
-            print_error("Parent post ID was incorrect");
+            error("Parent post ID was incorrect");
         }
         if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
-            print_error("This post is not part of a discussion!");
+            error("This post is not part of a discussion!");
         }
         if (! $forum = get_record("forum", "id", $discussion->forum)) {
-            print_error("The forum number was incorrect ($discussion->forum)");
+            error("The forum number was incorrect ($discussion->forum)");
         }
         if (! $course = get_record("course", "id", $discussion->course)) {
-            print_error("The course number was incorrect ($discussion->course)");
+            error("The course number was incorrect ($discussion->course)");
         }
         if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
-            print_error("Incorrect cm");
+            error("Incorrect cm");
         }
 
         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
             }
         }
 
-        if (groupmode($course, $cm)) {   // Make sure user can post here
-            $mygroupid = mygroupid($course->id);
-            if (!((empty($mygroupid) and $discussion->groupid == -1)
-                    || (groups_is_member($discussion->groupid)/*$mygroupid == $discussion->groupid*/)
-                    || has_capability('moodle/site:accessallgroups', $modcontext, NULL, false) )) {
-                print_error('nopostdiscussion', 'forum');
+        if (groupmode($course, $cm) == SEPARATEGROUPS) {   // Make sure user can post here
+            if ($discussion->groupid == -1) {
+                if (!has_capability('moodle/site:accessallgroups', $modcontext)) {
+                    print_error('nopostforum', 'forum');
+                }
+            } else {
+                if (!groups_is_member($discussion->groupid)) {
+                    print_error('nopostforum', 'forum');
+                }
             }
         }
+
         if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
             print_error("activityiscurrentlyhidden");
         }
         $post->userid      = $USER->id;
         $post->message     = '';
 
+        if ($groupmode = groups_get_activity_groupmode($cm)) {
+            $post->groupid = $discussion->groupid;
+        } else {
+            $post->groupid = null;
+        }
+
         $strre = get_string('re', 'forum');
         if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
             $post->subject = $strre.' '.$post->subject;
     } else if (!empty($edit)) {  // User is editing their own post
 
         if (! $post = forum_get_post_full($edit)) {
-            print_error("Post ID was incorrect");
+            error("Post ID was incorrect");
         }
         if ($post->parent) {
             if (! $parent = forum_get_post_full($post->parent)) {
-                print_error("Parent post ID was incorrect ($post->parent)");
+                error("Parent post ID was incorrect ($post->parent)");
             }
         }
 
         if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
-            print_error("This post is not part of a discussion! ($edit)");
+            error("This post is not part of a discussion! ($edit)");
         }
         if (! $forum = get_record("forum", "id", $discussion->forum)) {
-            print_error("The forum number was incorrect ($discussion->forum)");
+            error("The forum number was incorrect ($discussion->forum)");
         }
         if (! $course = get_record("course", "id", $discussion->course)) {
-            print_error("The course number was incorrect ($discussion->course)");
+            error("The course number was incorrect ($discussion->course)");
         }
         if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
-            print_error('Could not get the course module for the forum instance.');
+            error('Could not get the course module for the forum instance.');
         } else {
             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
         }
         if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
             if (((time() - $post->created) > $CFG->maxeditingtime) and
                         !has_capability('mod/forum:editanypost', $modcontext)) {
-                print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
+                error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
             }
         }
         if (($post->userid <> $USER->id) and
                     !has_capability('mod/forum:editanypost', $modcontext)) {
-            print_error("You can't edit other people's posts!");
+            error("You can't edit other people's posts!");
         }
 
 
         $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;
+        }
 
         trusttext_prepare_edit($post->message, $post->format, can_use_html_editor(), $modcontext);
 
     }else if (!empty($delete)) {  // User is deleting a post
 
         if (! $post = forum_get_post_full($delete)) {
-            print_error("Post ID was incorrect");
+            error("Post ID was incorrect");
         }
         if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
-            print_error("This post is not part of a discussion!");
+            error("This post is not part of a discussion!");
         }
         if (! $forum = get_record("forum", "id", $discussion->forum)) {
-            print_error("The forum number was incorrect ($discussion->forum)");
+            error("The forum number was incorrect ($discussion->forum)");
         }
         if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
-            print_error('Could not get the course module for the forum instance.');
+            error('Could not get the course module for the forum instance.');
         }
         if (!$course = get_record('course', 'id', $forum->course)) {
-            print_error('Incorrect course');
+            error('Incorrect course');
         }
 
         require_login($course, false, $cm);
 
         if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
                     || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
-            print_error("You can't delete this post!");
+            error("You can't delete this post!");
         }
 
 
 
                     redirect(forum_go_back_to($discussionurl));
                 } else {
-                    print_error("An error occurred while deleting record $post->id");
+                    error("An error occurred while deleting record $post->id");
                 }
             }
 
     } else if (!empty($prune)) {  // Pruning
 
         if (!$post = forum_get_post_full($prune)) {
-            print_error("Post ID was incorrect");
+            error("Post ID was incorrect");
         }
         if (!$discussion = get_record("forum_discussions", "id", $post->discussion)) {
-            print_error("This post is not part of a discussion!");
+            error("This post is not part of a discussion!");
         }
         if (!$forum = get_record("forum", "id", $discussion->forum)) {
-            print_error("The forum number was incorrect ($discussion->forum)");
+            error("The forum number was incorrect ($discussion->forum)");
         }
         if ($forum->type == 'single') {
-            print_error('Discussions from this forum cannot be split');
+            error('Discussions from this forum cannot be split');
         }
         if (!$post->parent) {
-            print_error('This is already the first post in the discussion');
+            error('This is already the first post in the discussion');
         }
         if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
-            print_error('Could not get the course module for the forum instance.');
+            error('Could not get the course module for the forum instance.');
         } else {
             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
         }
         if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
-            print_error("You can't split discussions!");
+            error("You can't split discussions!");
         }
 
         if (!empty($name)) {    // User has confirmed the prune
             $newdiscussion->timeend      = $discussion->timeend;
 
             if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
-                print_error('Could not create new discussion');
+                error('Could not create new discussion');
             }
 
             $newpost = new object();
             $newpost->subject = $name;
 
             if (!update_record("forum_posts", $newpost)) {
-                print_error('Could not update the original post');
+                error('Could not update the original post');
             }
 
             forum_change_discussionid($post->id, $newid);
         print_footer($course);
         die;
     } else {
-        print_error("No operation specified");
+        error("No operation specified");
 
     }
 
     }
 
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
-        print_error('Could not get the course module for the forum instance.');
+        error('Could not get the course module for the forum instance.');
     }
     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
 
-    $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
+    $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
 
     if ($fromform = $mform_post->get_data()) {
 
             if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
                                 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
                                 has_capability('mod/forum:editanypost', $modcontext)) ) {
-                print_error("You can not update this post");
+                error("You can not update this post");
             }
 
             $updatepost = $fromform; //realpost
             exit;
 
         } else {                     // Adding a new discussion
+            if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
+                error('Can not add discussion, sorry.');
+            }
             $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
             $discussion = $fromform;
             $discussion->name  = $fromform->subject;
 
     if ($post->discussion) {
         if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
-            print_error("Could not find top parent of post $post->id");
+            error("Could not find top parent of post $post->id");
         }
     } else {
         $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
 
 // checkup
     if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
-        print_error("You cannot reply to this post");
+        error("You cannot reply to this post");
     }
     if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, -1, -1, $cm, $modcontext)) {
-        print_error("You cannot start a new discussion in this forum");
+        error("You cannot start a new discussion in this forum");
     }
 
     if ($forum->type == 'qanda'
 
     if (!empty($parent)) {
         if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) {
-            print_error('This post is not part of a discussion!');
+            error('This post is not part of a discussion!');
         }
 
         forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
                     (!empty($USER->autosubscribe));
 
 
+    // HACK ALERT: this is very wrong, the defaults should be always initialized before calling $mform->get_data() !!!
     $mform_post->set_data(array(    'general'=>$heading,
                                         'subject'=>$post->subject,
                                         'message'=>$post->message,
index a74fc7c57bc1c2c9f264ef871f61e5bed7a52dc7..77cf05df0d892248ab226bf2da4a3248b90b9292 100644 (file)
@@ -10,10 +10,11 @@ class mod_forum_post_form extends moodleform {
         $mform    =& $this->_form;
 
         $course        = $this->_customdata['course'];
+        $cm            = $this->_customdata['cm'];
         $coursecontext = $this->_customdata['coursecontext'];
         $modcontext    = $this->_customdata['modcontext'];
         $forum         = $this->_customdata['forum'];
-        $post          = $this->_customdata['post'];
+        $post          = $this->_customdata['post']; // hack alert
 
 
         // the upload manager is used directly in post precessing, moodleform::save_files() is not used yet
@@ -60,11 +61,11 @@ class mod_forum_post_form extends moodleform {
 
         }
 
-        if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) {
+        if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
             $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
         }
 
-        if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
+        if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
             $mform->addElement('header', '', get_string('displayperiod', 'forum'));
 
             $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
@@ -80,9 +81,20 @@ class mod_forum_post_form extends moodleform {
             $mform->setType('timeend', PARAM_INT);
             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
         }
+
+        if (groups_get_activity_groupmode($cm, $course) and !empty($post->groupid)) { // hack alert
+            if ($post->groupid == -1) {
+                $groupname = get_string('allparticipants');
+            } else {
+                $group = groups_get_group($post->groupid);
+                $groupname = format_string($group->name);
+            }
+            $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
+        }
+
 //-------------------------------------------------------------------------------
         // buttons
-        if (isset($post->edit)) {
+        if (isset($post->edit)) { // hack alert
             $submit_string = get_string('savechanges');
         } else {
             $submit_string = get_string('posttoforum', 'forum');