diff --git a/mod/forum/classes/local/renderers/discussion_list.php b/mod/forum/classes/local/renderers/discussion_list.php index bde944f0054..307f40201cf 100644 --- a/mod/forum/classes/local/renderers/discussion_list.php +++ b/mod/forum/classes/local/renderers/discussion_list.php @@ -163,6 +163,7 @@ class discussion_list { $forumview = [ 'forum' => (array) $forumexporter->export($this->renderer), + 'newdiscussionhtml' => $this->get_discussion_form($user, $cm, $groupid), 'groupchangemenu' => groups_print_activity_menu( $cm, $this->urlfactory->get_forum_view_url_from_forum($forum), @@ -185,7 +186,6 @@ class discussion_list { $forumview, [ 'pagination' => $this->renderer->render(new \paging_bar($alldiscussionscount, $pageno, $pagesize, $PAGE->url, 'p')), - 'newdiscussionhtml' => $this->get_discussion_form($user, $cm, $groupid) ], $exportedposts ); @@ -193,43 +193,56 @@ class discussion_list { return $this->renderer->render_from_template($this->template, $forumview); } + /** + * Get the mod_forum_post_form. This is the default boiler plate from mod_forum/post_form.php with the inpage flag caveat + * + * @param stdClass $user The user the form is being generated for + * @param \cm_info $cm + * @param int $groupid The groupid if any + * + * @return string The rendered html + */ private function get_discussion_form(stdClass $user, \cm_info $cm, ?int $groupid) { global $PAGE; $forum = $this->forum; $forumrecord = $this->legacydatamapperfactory->get_forum_data_mapper()->to_legacy_object($forum); $modcontext = \context_module::instance($cm->id); - $post = new stdClass(); - $post->course = $forum->get_course_id(); - $post->forum = $forum->get_id(); - $post->discussion = 0; // Ie discussion # not defined yet. - $post->parent = 0; - $post->subject = ''; - $post->userid = $user->id; - $post->message = ''; - $post->messageformat = editors_get_preferred_format(); - $post->messagetrust = 0; - $post->groupid = $groupid; - + $coursecontext = \context_course::instance($forum->get_course_id()); + $post = (object) [ + 'course' => $forum->get_course_id(), + 'forum' => $forum->get_id(), + 'discussion' => 0, // Ie discussion # not defined yet. + 'parent' => 0, + 'subject' => '', + 'userid' => $user->id, + 'message' => '', + 'messageformat' => editors_get_preferred_format(), + 'messagetrust' => 0, + 'groupid' => $groupid, + ]; $thresholdwarning = forum_check_throttling($forum, $cm); - //$forumrecord = $DB->get_record('forum', array('id' => $forum->get_id())); + $mformpost = new \mod_forum_post_form('post.php', array('course' => $forum->get_course_record(), 'cm' => $cm, - 'coursecontext' => $forum->get_context(), + 'coursecontext' => $coursecontext, 'modcontext' => $modcontext, 'forum' => $forumrecord, 'post' => $post, 'subscribe' => \mod_forum\subscriptions::is_subscribed($user->id, $forumrecord, null, $cm), 'thresholdwarning' => $thresholdwarning, + 'inpagereply' => true, 'edit' => 0), 'post', '', array('id' => 'mformforum')); - // Setup the file related components. - $draftitemid = file_get_submitted_draft_itemid('attachments'); - $attachoptions = \mod_forum_post_form::attachment_options($forumrecord); - file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', null, $attachoptions); $params = array('reply' => 0, 'forum' => $forumrecord->id, 'edit' => 0) + - (isset($post->groupid) ? array('groupid' => $post->groupid) : array()); + (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) + + array( + 'userid' => $post->userid, + 'parent' => $post->parent, + 'discussion' => $post->discussion, + 'course' => $forum->get_course_id() + ); $mformpost->set_data($params); return $mformpost->render(); diff --git a/mod/forum/classes/post_form.php b/mod/forum/classes/post_form.php index 1d9d303932e..5afcea7cf2b 100644 --- a/mod/forum/classes/post_form.php +++ b/mod/forum/classes/post_form.php @@ -94,8 +94,11 @@ class mod_forum_post_form extends moodleform { $edit = $this->_customdata['edit']; $thresholdwarning = $this->_customdata['thresholdwarning']; $canreplyprivately = $this->_customdata['canreplyprivately']; + $stripped = isset($this->_customdata['inpagereply']) ? $this->_customdata['inpagereply'] : false; - $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data + if (!$stripped) { + $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data + } // If there is a warning message and we are not editing a post we need to handle the warning. if (!empty($thresholdwarning) && !$edit) { @@ -115,146 +118,156 @@ class mod_forum_post_form extends moodleform { $mform->setType('message', PARAM_RAW); $mform->addRule('message', get_string('required'), 'required', null, 'client'); - $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext); + if (!$stripped) { + $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext); - if (\mod_forum\subscriptions::is_forcesubscribed($forum)) { - $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum')); - $mform->freeze('discussionsubscribe'); - $mform->setDefaults('discussionsubscribe', 0); - $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum'); + if (\mod_forum\subscriptions::is_forcesubscribed($forum)) { + $mform->addElement('advcheckbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum')); + $mform->freeze('discussionsubscribe'); + $mform->setDefaults('discussionsubscribe', 0); + $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum'); - } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) { - $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum')); - $mform->freeze('discussionsubscribe'); - $mform->setDefaults('discussionsubscribe', 0); - $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum'); + } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) { + $mform->addElement('advcheckbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum')); + $mform->freeze('discussionsubscribe'); + $mform->setDefaults('discussionsubscribe', 0); + $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum'); - } else { - $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum')); - $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum'); - } - - if (forum_can_create_attachment($forum, $modcontext)) { - $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum)); - $mform->addHelpButton('attachments', 'attachment', 'forum'); - } - - if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) { - $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum')); - $mform->addHelpButton('pinned', 'discussionpinned', 'forum'); - } - - if (empty($post->id) && $manageactivities) { - $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum')); - } - - if ((empty($post->id) && $canreplyprivately) || (!empty($post) && !empty($post->privatereplyto))) { - // Only show the option to change private reply settings if this is a new post and the user can reply - // privately, or if this is already private reply, in which case the state is shown but is not editable. - $mform->addElement('checkbox', 'isprivatereply', get_string('privatereply', 'forum')); - $mform->addHelpButton('isprivatereply', 'privatereply', 'forum'); - if (!empty($post->privatereplyto)) { - $mform->setDefault('isprivatereply', 1); - $mform->freeze('isprivatereply'); - } - } - - if ($groupmode = groups_get_activity_groupmode($cm, $course)) { - $groupdata = groups_get_activity_allowed_groups($cm); - - $groupinfo = array(); - foreach ($groupdata as $groupid => $group) { - // Check whether this user can post in this group. - // We must make this check because all groups are returned for a visible grouped activity. - if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) { - // Build the data for the groupinfo select. - $groupinfo[$groupid] = $group->name; - } else { - unset($groupdata[$groupid]); - } - } - $groupcount = count($groupinfo); - - // Check whether a user can post to all of their own groups. - - // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met. - // 1) It only makes sense to allow this when a user is in more than one group. - // Note: This check must come before we consider adding accessallgroups, because that is not a real group. - $canposttoowngroups = empty($post->edit) && $groupcount > 1; - - // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply. - $canposttoowngroups = $canposttoowngroups && empty($post->parent); - - // 3) You also need the canposttoowngroups capability. - $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext); - if ($canposttoowngroups) { - // This user is in multiple groups, and can post to all of their own groups. - // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a - // user is a member of. - $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum')); - $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum'); - $mform->disabledIf('groupinfo', 'posttomygroups', 'checked'); - } - - // Check whether this user can post to all groups. - // Posts to the 'All participants' group go to all groups, not to each group in a list. - // It makes sense to allow this, even if there currently aren't any groups because there may be in the future. - if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) { - // Note: We must reverse in this manner because array_unshift renumbers the array. - $groupinfo = array_reverse($groupinfo, true ); - $groupinfo[-1] = get_string('allparticipants'); - $groupinfo = array_reverse($groupinfo, true ); - $groupcount++; - } - - // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons. - // 1) This is a new post (not an edit), and there are at least two groups to choose from. - $canselectgroupfornew = empty($post->edit) && $groupcount > 1; - - // 2) This is editing of an existing post and the user is allowed to movediscussions. - // We allow this because the post may have been moved from another forum where groups are not available. - // We show this even if no groups are available as groups *may* have been available but now are not. - $canselectgroupformove = $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext); - - // Important: You can *only* change the group for a top level post. Never any reply. - $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove); - - if ($canselectgroup) { - $mform->addElement('select','groupinfo', get_string('group'), $groupinfo); - $mform->setDefault('groupinfo', $post->groupid); - $mform->setType('groupinfo', PARAM_INT); } else { - if (empty($post->groupid)) { - $groupname = get_string('allparticipants'); - } else { - $groupname = format_string($groupdata[$post->groupid]->name); - } - $mform->addElement('static', 'groupinfo', get_string('group'), $groupname); + $mform->addElement('advcheckbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum')); + $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum'); } - } - if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { - $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum')); + if (forum_can_create_attachment($forum, $modcontext)) { + $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, + self::attachment_options($forum)); + $mform->addHelpButton('attachments', 'attachment', 'forum'); + } - $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'), array('optional' => true)); - $mform->addHelpButton('timestart', 'displaystart', 'forum'); + if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) { + $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum')); + $mform->addHelpButton('pinned', 'discussionpinned', 'forum'); + } - $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'), array('optional' => true)); - $mform->addHelpButton('timeend', 'displayend', 'forum'); + if ((empty($post->id) && $canreplyprivately) || (!empty($post) && !empty($post->privatereplyto))) { + // Only show the option to change private reply settings if this is a new post and the user can reply + // privately, or if this is already private reply, in which case the state is shown but is not editable. + $mform->addElement('checkbox', 'isprivatereply', get_string('privatereply', 'forum')); + $mform->addHelpButton('isprivatereply', 'privatereply', 'forum'); + if (!empty($post->privatereplyto)) { + $mform->setDefault('isprivatereply', 1); + $mform->freeze('isprivatereply'); + } + } - } else { - $mform->addElement('hidden', 'timestart'); - $mform->setType('timestart', PARAM_INT); - $mform->addElement('hidden', 'timeend'); - $mform->setType('timeend', PARAM_INT); - $mform->setConstants(array('timestart' => 0, 'timeend' => 0)); - } + if ($groupmode = groups_get_activity_groupmode($cm, $course)) { + $groupdata = groups_get_activity_allowed_groups($cm); + if (empty($post->id) && $manageactivities) { + $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum')); + } - if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) { - $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); + if ($groupmode = groups_get_activity_groupmode($cm, $course)) { + $groupdata = groups_get_activity_allowed_groups($cm); - $mform->addElement('tags', 'tags', get_string('tags'), - array('itemtype' => 'forum_posts', 'component' => 'mod_forum')); + $groupinfo = array(); + foreach ($groupdata as $groupid => $group) { + // Check whether this user can post in this group. + // We must make this check because all groups are returned for a visible grouped activity. + if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) { + // Build the data for the groupinfo select. + $groupinfo[$groupid] = $group->name; + } else { + unset($groupdata[$groupid]); + } + } + $groupcount = count($groupinfo); + + // Check whether a user can post to all of their own groups. + + // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met. + // 1) It only makes sense to allow this when a user is in more than one group. + // Note: This check must come before we consider adding accessallgroups, because that is not a real group. + $canposttoowngroups = empty($post->edit) && $groupcount > 1; + + // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply. + $canposttoowngroups = $canposttoowngroups && empty($post->parent); + + // 3) You also need the canposttoowngroups capability. + $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext); + if ($canposttoowngroups) { + // This user is in multiple groups, and can post to all of their own groups. + // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a + // user is a member of. + $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum')); + $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum'); + $mform->disabledIf('groupinfo', 'posttomygroups', 'checked'); + } + + // Check whether this user can post to all groups. + // Posts to the 'All participants' group go to all groups, not to each group in a list. + // It makes sense to allow this, even if there currently aren't any groups because there may be in the future. + if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) { + // Note: We must reverse in this manner because array_unshift renumbers the array. + $groupinfo = array_reverse($groupinfo, true); + $groupinfo[-1] = get_string('allparticipants'); + $groupinfo = array_reverse($groupinfo, true); + $groupcount++; + } + + // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons. + // 1) This is a new post (not an edit), and there are at least two groups to choose from. + $canselectgroupfornew = empty($post->edit) && $groupcount > 1; + + // 2) This is editing of an existing post and the user is allowed to movediscussions. + // We allow this because the post may have been moved from another forum where groups are not available. + // We show this even if no groups are available as groups *may* have been available but now are not. + $canselectgroupformove = + $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext); + + // Important: You can *only* change the group for a top level post. Never any reply. + $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove); + + if ($canselectgroup) { + $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo); + $mform->setDefault('groupinfo', $post->groupid); + $mform->setType('groupinfo', PARAM_INT); + } else { + if (empty($post->groupid)) { + $groupname = get_string('allparticipants'); + } else { + $groupname = format_string($groupdata[$post->groupid]->name); + } + $mform->addElement('static', 'groupinfo', get_string('group'), $groupname); + } + } + } + + if (!empty($CFG->forum_enabletimedposts) && !$post->parent && + has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { + $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum')); + + $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'), + array('optional' => true)); + $mform->addHelpButton('timestart', 'displaystart', 'forum'); + + $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'), + array('optional' => true)); + $mform->addHelpButton('timeend', 'displayend', 'forum'); + + } else { + $mform->addElement('hidden', 'timestart'); + $mform->setType('timestart', PARAM_INT); + $mform->addElement('hidden', 'timeend'); + $mform->setType('timeend', PARAM_INT); + $mform->setConstants(array('timestart' => 0, 'timeend' => 0)); + } + + if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) { + $mform->addElement('header', 'tagshdr', get_string('tags', 'tag')); + + $mform->addElement('tags', 'tags', get_string('tags'), + array('itemtype' => 'forum_posts', 'component' => 'mod_forum')); + } } //------------------------------------------------------------------------------- @@ -265,7 +278,23 @@ class mod_forum_post_form extends moodleform { $submit_string = get_string('posttoforum', 'forum'); } - $this->add_action_buttons(true, $submit_string); + // Always register a no submit button so it can be picked up if redirecting to the original post form. + $mform->registerNoSubmitButton('advancedadddiscussion'); + + // This is an inpage add discussion which requires custom buttons. + if ($stripped) { + $buttonarray = array(); + $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submit_string); + $buttonarray[] = &$mform->createElement('submit', 'advancedadddiscussion', + get_string('advanced')); + $buttonarray[] = &$mform->createElement('button', 'cancelbtn', + get_string('cancel', 'core'), + ['class' => 'btn-secondary', 'data-toggle' => 'collapse', 'data-target' => "#collapseAddForm"]); + $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); + $mform->closeHeaderBefore('buttonar'); + } else { + $this->add_action_buttons(true, $submit_string); + } $mform->addElement('hidden', 'course'); $mform->setType('course', PARAM_INT); diff --git a/mod/forum/post.php b/mod/forum/post.php index 4aa76e77057..fdea9c05ef0 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -781,14 +781,14 @@ if ($mformpost->is_cancelled()) { } else { redirect($urlfactory->get_discussion_view_url_from_discussion($discussionentity)); } -} else if ($fromform = $mformpost->get_data()) { +} else if ($mformpost->is_submitted() && !$mformpost->no_submit_button_pressed()) { if (empty($SESSION->fromurl)) { $errordestination = $urlfactory->get_forum_view_url_from_forum($forumentity); } else { $errordestination = $SESSION->fromurl; } - + $fromform = $mformpost->get_data(); $fromform->itemid = $fromform->message['itemid']; $fromform->messageformat = $fromform->message['format']; $fromform->message = $fromform->message['text']; diff --git a/mod/forum/templates/discussion_list.mustache b/mod/forum/templates/discussion_list.mustache index 6abcf924ff2..1f5303bcdbb 100644 --- a/mod/forum/templates/discussion_list.mustache +++ b/mod/forum/templates/discussion_list.mustache @@ -46,7 +46,7 @@ {{#str}}addanewdiscussion, forum{{/str}} {{/discussion_create_text}} -