PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/report/summary/index.php

https://github.com/mudrd8mz/moodle
PHP | 190 lines | 124 code | 33 blank | 33 comment | 17 complexity | ffbefc3ce2008e1a0dbf5fee470054ee MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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/>.
  16. /**
  17. * This script displays the forum summary report for the given parameters, within a user's capabilities.
  18. *
  19. * @package forumreport_summary
  20. * @copyright 2019 Michael Hawkins <michaelh@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once("../../../../config.php");
  24. if (isguestuser()) {
  25. print_error('noguest');
  26. }
  27. $courseid = required_param('courseid', PARAM_INT);
  28. $forumid = optional_param('forumid', 0, PARAM_INT);
  29. $perpage = optional_param('perpage', \forumreport_summary\summary_table::DEFAULT_PER_PAGE, PARAM_INT);
  30. $download = optional_param('download', '', PARAM_ALPHA);
  31. $filters = [];
  32. $pageurlparams = [
  33. 'courseid' => $courseid,
  34. 'perpage' => $perpage,
  35. ];
  36. // Establish filter values.
  37. $filters['groups'] = optional_param_array('filtergroups', [], PARAM_INT);
  38. $filters['datefrom'] = optional_param_array('datefrom', ['enabled' => 0], PARAM_INT);
  39. $filters['dateto'] = optional_param_array('dateto', ['enabled' => 0], PARAM_INT);
  40. $modinfo = get_fast_modinfo($courseid);
  41. $course = $modinfo->get_course();
  42. $courseforums = $modinfo->instances['forum'];
  43. $cms = [];
  44. // Determine which forums the user has access to in the course.
  45. $accessallforums = false;
  46. $allforumidsincourse = array_keys($courseforums);
  47. $forumsvisibletouser = [];
  48. $forumselectoptions = [0 => get_string('forumselectcourseoption', 'forumreport_summary')];
  49. foreach ($courseforums as $courseforumid => $courseforum) {
  50. if ($courseforum->uservisible) {
  51. $forumsvisibletouser[$courseforumid] = $courseforum;
  52. $forumselectoptions[$courseforumid] = $courseforum->name;
  53. }
  54. }
  55. if ($forumid) {
  56. if (!isset($forumsvisibletouser[$forumid])) {
  57. throw new \moodle_exception('A valid forum ID is required to generate a summary report.');
  58. }
  59. $filters['forums'] = [$forumid];
  60. $title = $forumsvisibletouser[$forumid]->name;
  61. $forumcm = $forumsvisibletouser[$forumid];
  62. $cms[] = $forumcm;
  63. require_login($courseid, false, $forumcm);
  64. $context = $forumcm->context;
  65. $canexport = !$download && has_capability('mod/forum:exportforum', $context);
  66. $redirecturl = new moodle_url('/mod/forum/view.php', ['id' => $forumid]);
  67. $numforums = 1;
  68. $pageurlparams['forumid'] = $forumid;
  69. $iscoursereport = false;
  70. } else {
  71. // Course level report.
  72. require_login($courseid, false);
  73. $filters['forums'] = array_keys($forumsvisibletouser);
  74. // Fetch the forum CMs for the course.
  75. foreach ($forumsvisibletouser as $visibleforum) {
  76. $cms[] = $visibleforum;
  77. }
  78. $context = \context_course::instance($courseid);
  79. $title = $course->fullname;
  80. // Export currently only supports single forum exports.
  81. $canexport = false;
  82. $redirecturl = new moodle_url('/course/view.php', ['id' => $courseid]);
  83. $numforums = count($forumsvisibletouser);
  84. $iscoursereport = true;
  85. // Specify whether user has access to all forums in the course.
  86. $accessallforums = empty(array_diff($allforumidsincourse, $filters['forums']));
  87. }
  88. $pageurl = new moodle_url('/mod/forum/report/summary/index.php', $pageurlparams);
  89. $PAGE->set_url($pageurl);
  90. $PAGE->set_pagelayout('report');
  91. $PAGE->set_title($title);
  92. $PAGE->set_heading($course->fullname);
  93. $PAGE->navbar->add(get_string('nodetitle', 'forumreport_summary'));
  94. $allowbulkoperations = !$download && !empty($CFG->messaging) && has_capability('moodle/course:bulkmessaging', $context);
  95. $canseeprivatereplies = false;
  96. $hasviewall = false;
  97. $privatereplycapcount = 0;
  98. $viewallcount = 0;
  99. $canview = false;
  100. foreach ($cms as $cm) {
  101. $forumcontext = $cm->context;
  102. // This capability is required in at least one of the given contexts to view any version of the report.
  103. if (has_capability('forumreport/summary:view', $forumcontext)) {
  104. $canview = true;
  105. }
  106. if (has_capability('mod/forum:readprivatereplies', $forumcontext)) {
  107. $privatereplycapcount++;
  108. }
  109. if (has_capability('forumreport/summary:viewall', $forumcontext)) {
  110. $viewallcount++;
  111. }
  112. }
  113. if (!$canview) {
  114. redirect($redirecturl);
  115. }
  116. // Only use private replies if user has that cap in all forums in the report.
  117. if ($numforums === $privatereplycapcount) {
  118. $canseeprivatereplies = true;
  119. }
  120. // Will only show all users if user has the cap for all forums in the report.
  121. if ($numforums === $viewallcount) {
  122. $hasviewall = true;
  123. }
  124. // Prepare and display the report.
  125. $table = new \forumreport_summary\summary_table($courseid, $filters, $allowbulkoperations,
  126. $canseeprivatereplies, $perpage, $canexport, $iscoursereport, $accessallforums);
  127. $table->baseurl = $pageurl;
  128. $eventparams = [
  129. 'context' => $context,
  130. 'other' => [
  131. 'forumid' => $forumid,
  132. 'hasviewall' => $hasviewall,
  133. ],
  134. ];
  135. if ($download) {
  136. \forumreport_summary\event\report_downloaded::create($eventparams)->trigger();
  137. $table->download($download);
  138. } else {
  139. \forumreport_summary\event\report_viewed::create($eventparams)->trigger();
  140. echo $OUTPUT->header();
  141. echo $OUTPUT->heading(get_string('summarytitle', 'forumreport_summary', $title), 2, 'pb-5');
  142. if (!empty($filters['groups'])) {
  143. \core\notification::info(get_string('viewsdisclaimer', 'forumreport_summary'));
  144. }
  145. // Allow switching to course report (or other forum user has access to).
  146. $reporturl = new moodle_url('/mod/forum/report/summary/index.php', ['courseid' => $courseid]);
  147. $forumselect = new single_select($reporturl, 'forumid', $forumselectoptions, $forumid, '');
  148. $forumselect->set_label(get_string('forumselectlabel', 'forumreport_summary'));
  149. echo $OUTPUT->render($forumselect);
  150. // Render the report filters form.
  151. $renderer = $PAGE->get_renderer('forumreport_summary');
  152. unset($filters['forums']);
  153. echo $renderer->render_filters_form($course, $cms, $pageurl, $filters);
  154. $table->show_download_buttons_at(array(TABLE_P_BOTTOM));
  155. echo $renderer->render_summary_table($table);
  156. echo $OUTPUT->footer();
  157. }