Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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