MDL-69097 mod_forum: Apply filters to whole forum grading
[moodle.git] / mod / forum / view.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Displays the list of discussions in a forum.
19  *
20  * @package   mod_forum
21  * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 use mod_forum\grades\forum_gradeitem;
27 require_once('../../config.php');
29 $managerfactory = mod_forum\local\container::get_manager_factory();
30 $legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
31 $vaultfactory = mod_forum\local\container::get_vault_factory();
32 $forumvault = $vaultfactory->get_forum_vault();
33 $discussionvault = $vaultfactory->get_discussion_vault();
34 $postvault = $vaultfactory->get_post_vault();
35 $discussionlistvault = $vaultfactory->get_discussions_in_forum_vault();
37 $cmid = optional_param('id', 0, PARAM_INT);
38 $forumid = optional_param('f', 0, PARAM_INT);
39 $mode = optional_param('mode', 0, PARAM_INT);
40 $showall = optional_param('showall', '', PARAM_INT);
41 $pageno = optional_param('page', 0, PARAM_INT);
42 $search = optional_param('search', '', PARAM_CLEAN);
43 $pageno = optional_param('p', $pageno, PARAM_INT);
44 $pagesize = optional_param('s', 0, PARAM_INT);
45 $sortorder = optional_param('o', null, PARAM_INT);
47 if (!$cmid && !$forumid) {
48     print_error('missingparameter');
49 }
51 if ($cmid) {
52     $forum = $forumvault->get_from_course_module_id($cmid);
53     if (empty($forum)) {
54         throw new \moodle_exception('Unable to find forum with cmid ' . $cmid);
55     }
56 } else {
57     $forum = $forumvault->get_from_id($forumid);
58     if (empty($forum)) {
59         throw new \moodle_exception('Unable to find forum with id ' . $forumid);
60     }
61 }
63 if (!empty($showall)) {
64     // The user wants to see all discussions.
65     $pageno = 0;
66     $pagesize = 0;
67 }
69 $urlfactory = mod_forum\local\container::get_url_factory();
70 $capabilitymanager = $managerfactory->get_capability_manager($forum);
72 $url = $urlfactory->get_forum_view_url_from_forum($forum);
73 $PAGE->set_url($url);
75 $course = $forum->get_course_record();
76 $coursemodule = $forum->get_course_module_record();
77 $cm = \cm_info::create($coursemodule);
79 require_course_login($course, true, $cm);
81 $istypesingle = $forum->get_type() === 'single';
82 $saveddisplaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
84 if ($mode) {
85     $displaymode = $mode;
86 } else {
87     $displaymode = $saveddisplaymode;
88 }
90 if (get_user_preferences('forum_useexperimentalui', false)) {
91     if ($displaymode == FORUM_MODE_NESTED) {
92         $displaymode = FORUM_MODE_NESTED_V2;
93     }
94 } else {
95     if ($displaymode == FORUM_MODE_NESTED_V2) {
96         $displaymode = FORUM_MODE_NESTED;
97     }
98 }
100 if ($displaymode != $saveddisplaymode) {
101     set_user_preference('forum_displaymode', $displaymode);
104 $PAGE->set_context($forum->get_context());
105 $PAGE->set_title($forum->get_name());
106 $PAGE->add_body_class('forumtype-' . $forum->get_type());
107 $PAGE->set_heading($course->fullname);
108 $PAGE->set_button(forum_search_form($course, $search));
110 if ($istypesingle && $displaymode == FORUM_MODE_NESTED_V2) {
111     $PAGE->add_body_class('nested-v2-display-mode reset-style');
112     $settingstrigger = $OUTPUT->render_from_template('mod_forum/settings_drawer_trigger', null);
113     $PAGE->add_header_action($settingstrigger);
116 if (empty($cm->visible) && !has_capability('moodle/course:viewhiddenactivities', $forum->get_context())) {
117     redirect(
118         $urlfactory->get_course_url_from_forum($forum),
119         get_string('activityiscurrentlyhidden'),
120         null,
121         \core\output\notification::NOTIFY_WARNING
122     );
125 if (!$capabilitymanager->can_view_discussions($USER)) {
126     redirect(
127         $urlfactory->get_course_url_from_forum($forum),
128         get_string('noviewdiscussionspermission', 'forum'),
129         null,
130         \core\output\notification::NOTIFY_WARNING
131     );
134 // Mark viewed and trigger the course_module_viewed event.
135 $forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
136 $forumrecord = $forumdatamapper->to_legacy_object($forum);
137 forum_view(
138     $forumrecord,
139     $forum->get_course_record(),
140     $forum->get_course_module_record(),
141     $forum->get_context()
142 );
144 // Return here if we post or set subscription etc.
145 $SESSION->fromdiscussion = qualified_me();
147 if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->get_rss_type() && $forum->get_rss_articles()) {
148     require_once("{$CFG->libdir}/rsslib.php");
150     $rsstitle = format_string($course->shortname, true, [
151             'context' => context_course::instance($course->id),
152         ]) . ': ' . format_string($forum->get_name());
153     rss_add_http_header($forum->get_context(), 'mod_forum', $forumrecord, $rsstitle);
156 echo $OUTPUT->header();
157 echo $OUTPUT->heading(format_string($forum->get_name()), 2);
159 if (!$istypesingle && !empty($forum->get_intro())) {
160     echo $OUTPUT->box(format_module_intro('forum', $forumrecord, $cm->id), 'generalbox', 'intro');
163 if ($sortorder) {
164     set_user_preference('forum_discussionlistsortorder', $sortorder);
167 $sortorder = get_user_preferences('forum_discussionlistsortorder', $discussionlistvault::SORTORDER_LASTPOST_DESC);
169 // Fetch the current groupid.
170 $groupid = groups_get_activity_group($cm, true) ?: null;
171 $rendererfactory = mod_forum\local\container::get_renderer_factory();
172 switch ($forum->get_type()) {
173     case 'single':
174         $forumgradeitem = forum_gradeitem::load_from_forum_entity($forum);
175         if ($capabilitymanager->can_grade($USER)) {
177             if ($forumgradeitem->is_grading_enabled()) {
178                 $groupid = groups_get_activity_group($cm, true) ?: null;
179                 $gradeobj = (object) [
180                     'contextid' => $forum->get_context()->id,
181                     'cmid' => $cmid,
182                     'name' => format_string($forum->get_name()),
183                     'courseid' => $course->id,
184                     'coursename' => format_string($course->shortname),
185                     'experimentaldisplaymode' => $displaymode == FORUM_MODE_NESTED_V2,
186                     'groupid' => $groupid,
187                     'gradingcomponent' => $forumgradeitem->get_grading_component_name(),
188                     'gradingcomponentsubtype' => $forumgradeitem->get_grading_component_subtype(),
189                     'sendstudentnotifications' => $forum->should_notify_students_default_when_grade_for_forum(),
190                 ];
191                 echo $OUTPUT->render_from_template('mod_forum/grades/grade_button', $gradeobj);
192             }
193         } else {
194             if ($forumgradeitem->is_grading_enabled()) {
195                 $groupid = groups_get_activity_group($cm, true) ?: null;
196                 $gradeobj = (object) [
197                     'contextid' => $forum->get_context()->id,
198                     'cmid' => $cmid,
199                     'name' => format_string($forum->get_name()),
200                     'courseid' => $course->id,
201                     'coursename' => format_string($course->shortname),
202                     'groupid' => $groupid,
203                     'userid' => $USER->id,
204                     'gradingcomponent' => $forumgradeitem->get_grading_component_name(),
205                     'gradingcomponentsubtype' => $forumgradeitem->get_grading_component_subtype(),
206                 ];
207                 echo $OUTPUT->render_from_template('mod_forum/grades/view_grade_button', $gradeobj);
208             }
209         }
210         $discussion = $discussionvault->get_last_discussion_in_forum($forum);
211         $discussioncount = $discussionvault->get_count_discussions_in_forum($forum);
212         $hasmultiplediscussions = $discussioncount > 1;
213         $discussionsrenderer = $rendererfactory->get_single_discussion_list_renderer($forum, $discussion,
214             $hasmultiplediscussions, $displaymode);
215         $post = $postvault->get_from_id($discussion->get_first_post_id());
216         $orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
217         $replies = $postvault->get_replies_to_post(
218                 $USER,
219                 $post,
220                 $capabilitymanager->can_view_any_private_reply($USER),
221                 $orderpostsby
222             );
223         echo $discussionsrenderer->render($USER, $post, $replies);
225         if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) {
226             $postids = array_map(function($post) {
227                 return $post->get_id();
228             }, array_merge([$post], array_values($replies)));
229             forum_tp_mark_posts_read($USER, $postids);
230         }
231         break;
232     case 'blog':
233         $discussionsrenderer = $rendererfactory->get_blog_discussion_list_renderer($forum);
234         // Blog forums always show discussions newest first.
235         echo $discussionsrenderer->render($USER, $cm, $groupid, $discussionlistvault::SORTORDER_CREATED_DESC,
236             $pageno, $pagesize);
238         if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forumrecord, $USER)) {
239             $discussions = mod_forum_get_discussion_summaries($forum, $USER, $groupid, null, $pageno, $pagesize);
240             $firstpostids = array_map(function($discussion) {
241                 return $discussion->get_first_post()->get_id();
242             }, array_values($discussions));
243             forum_tp_mark_posts_read($USER, $firstpostids);
244         }
245         break;
246     default:
247         $discussionsrenderer = $rendererfactory->get_discussion_list_renderer($forum);
248         echo $discussionsrenderer->render($USER, $cm, $groupid, $sortorder, $pageno, $pagesize, $displaymode);
251 echo $OUTPUT->footer();