Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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      throw new \moodle_exception('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->get_formatted_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]->get_formatted_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->activityheader->disable();
 111  $PAGE->navbar->add(get_string('nodetitle', 'forumreport_summary'));
 112  
 113  // Activate the secondary nav tab.
 114  navigation_node::override_active_url(new moodle_url('/mod/forum/report/summary/index.php',
 115      ['courseid' => $courseid, 'forumid' => $forumid]));
 116  
 117  $allowbulkoperations = !$download && !empty($CFG->messaging) && has_capability('moodle/course:bulkmessaging', $context);
 118  $canseeprivatereplies = false;
 119  $hasviewall = false;
 120  $privatereplycapcount = 0;
 121  $viewallcount = 0;
 122  $canview = false;
 123  
 124  foreach ($cms as $cm) {
 125      $forumcontext = $cm->context;
 126  
 127      // This capability is required in at least one of the given contexts to view any version of the report.
 128      if (has_capability('forumreport/summary:view', $forumcontext)) {
 129          $canview = true;
 130      }
 131  
 132      if (has_capability('mod/forum:readprivatereplies', $forumcontext)) {
 133          $privatereplycapcount++;
 134      }
 135  
 136      if (has_capability('forumreport/summary:viewall', $forumcontext)) {
 137          $viewallcount++;
 138      }
 139  }
 140  
 141  if (!$canview) {
 142      redirect($redirecturl);
 143  }
 144  
 145  // Only use private replies if user has that cap in all forums in the report.
 146  if ($numforums === $privatereplycapcount) {
 147      $canseeprivatereplies = true;
 148  }
 149  
 150  // Will only show all users if user has the cap for all forums in the report.
 151  if ($numforums === $viewallcount) {
 152      $hasviewall = true;
 153  }
 154  
 155  // Prepare and display the report.
 156  $table = new \forumreport_summary\summary_table($courseid, $filters, $allowbulkoperations,
 157          $canseeprivatereplies, $perpage, $canexport, $iscoursereport, $accessallforums);
 158  $table->baseurl = $pageurl;
 159  
 160  $eventparams = [
 161      'context' => $context,
 162      'other' => [
 163          'forumid' => $forumid,
 164          'hasviewall' => $hasviewall,
 165      ],
 166  ];
 167  
 168  if ($download) {
 169      \forumreport_summary\event\report_downloaded::create($eventparams)->trigger();
 170      $table->download($download);
 171  } else {
 172      \forumreport_summary\event\report_viewed::create($eventparams)->trigger();
 173  
 174      echo $OUTPUT->header();
 175  
 176      if (!empty($filters['groups'])) {
 177          \core\notification::info(get_string('viewsdisclaimer', 'forumreport_summary'));
 178      }
 179  
 180      // Allow switching to course report (or other forum user has access to).
 181      $reporturl = new moodle_url('/mod/forum/report/summary/index.php', ['courseid' => $courseid]);
 182      $forumselect = new single_select($reporturl, 'forumid', $forumselectoptions, $forumid, '');
 183      $forumselect->set_label(get_string('forumselectlabel', 'forumreport_summary'));
 184      echo $OUTPUT->render($forumselect);
 185      echo $OUTPUT->heading(get_string('nodetitle', 'forumreport_summary'), 2, 'pb-5 mt-3');
 186  
 187      // Render the report filters form.
 188      $renderer = $PAGE->get_renderer('forumreport_summary');
 189  
 190      unset($filters['forums']);
 191      echo $renderer->render_filters_form($course, $cms, $pageurl, $filters);
 192      $table->show_download_buttons_at(array(TABLE_P_BOTTOM));
 193      echo $renderer->render_summary_table($table);
 194      echo $OUTPUT->footer();
 195  }