Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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   * Participation report
  19   *
  20   * @package    report
  21   * @subpackage participation
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use core\report_helper;
  27  
  28  require('../../config.php');
  29  require_once($CFG->dirroot.'/lib/tablelib.php');
  30  require_once($CFG->dirroot.'/notes/lib.php');
  31  require_once($CFG->dirroot.'/report/participation/locallib.php');
  32  
  33  define('DEFAULT_PAGE_SIZE', 20);
  34  define('SHOW_ALL_PAGE_SIZE', 5000);
  35  
  36  $id         = required_param('id', PARAM_INT); // course id.
  37  $roleid     = optional_param('roleid', 0, PARAM_INT); // which role to show
  38  $instanceid = optional_param('instanceid', 0, PARAM_INT); // instance we're looking at.
  39  $timefrom   = optional_param('timefrom', 0, PARAM_INT); // how far back to look...
  40  $action     = optional_param('action', '', PARAM_ALPHA);
  41  $page       = optional_param('page', 0, PARAM_INT);                     // which page to show
  42  $perpage    = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);  // how many per page
  43  $currentgroup = optional_param('group', null, PARAM_INT); // Get the active group.
  44  
  45  $url = new moodle_url('/report/participation/index.php', array('id'=>$id));
  46  if ($roleid !== 0) $url->param('roleid');
  47  if ($instanceid !== 0) $url->param('instanceid');
  48  if ($timefrom !== 0) $url->param('timefrom');
  49  if ($action !== '') $url->param('action');
  50  if ($page !== 0) $url->param('page');
  51  if ($perpage !== DEFAULT_PAGE_SIZE) $url->param('perpage');
  52  $PAGE->set_url($url);
  53  $PAGE->set_pagelayout('admin');
  54  
  55  if ($action != 'view' and $action != 'post') {
  56      $action = ''; // default to all (don't restrict)
  57  }
  58  
  59  if (!$course = $DB->get_record('course', array('id'=>$id))) {
  60      print_error('invalidcourse');
  61  }
  62  
  63  if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
  64      print_error('invalidrole');
  65  }
  66  
  67  require_login($course);
  68  $context = context_course::instance($course->id);
  69  require_capability('report/participation:view', $context);
  70  
  71  report_helper::save_selected_report($id, $url);
  72  
  73  $strparticipation = get_string('participationreport');
  74  $strviews         = get_string('views');
  75  $strposts         = get_string('posts');
  76  $strreports       = get_string('reports');
  77  
  78  $actionoptions = report_participation_get_action_options();
  79  if (!array_key_exists($action, $actionoptions)) {
  80      $action = '';
  81  }
  82  
  83  $PAGE->set_title(format_string($course->shortname, true, array('context' => $context)) .': '. $strparticipation);
  84  $PAGE->set_heading(format_string($course->fullname, true, array('context' => $context)));
  85  echo $OUTPUT->header();
  86  
  87  // Print the selector dropdown.
  88  $pluginname = get_string('pluginname', 'report_participation');
  89  report_helper::print_report_selector($pluginname);
  90  // Release session lock.
  91  \core\session\manager::write_close();
  92  
  93  // Logs will not have been recorded before the course timecreated time.
  94  $minlog = $course->timecreated;
  95  $onlyuselegacyreader = false; // Use only legacy log table to aggregate records.
  96  
  97  $logtable = report_participation_get_log_table_name(); // Log table to use for fetaching records.
  98  
  99  // If no log table, then use legacy records.
 100  if (empty($logtable)) {
 101      $onlyuselegacyreader = true;
 102  }
 103  
 104  $modinfo = get_fast_modinfo($course);
 105  
 106  // Print first controls.
 107  report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid);
 108  
 109  $baseurl = new moodle_url('/report/participation/index.php', array(
 110      'id' => $course->id,
 111      'roleid' => $roleid,
 112      'instanceid' => $instanceid,
 113      'timefrom' => $timefrom,
 114      'action' => $action,
 115      'perpage' => $perpage,
 116      'group' => $currentgroup
 117  ));
 118  $select = groups_allgroups_course_menu($course, $baseurl, true, $currentgroup);
 119  
 120  // User cannot see any group.
 121  if (empty($select)) {
 122      echo $OUTPUT->heading(get_string("notingroup"));
 123      echo $OUTPUT->footer();
 124      exit;
 125  } else {
 126      echo $select;
 127  }
 128  
 129  // Fetch current active group.
 130  $groupmode = groups_get_course_groupmode($course);
 131  $currentgroup = $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
 132  
 133  if (!empty($instanceid) && !empty($roleid)) {
 134      $uselegacyreader = $DB->record_exists('log', ['course' => $course->id]);
 135  
 136      // Trigger a report viewed event.
 137      $event = \report_participation\event\report_viewed::create(array('context' => $context,
 138              'other' => array('instanceid' => $instanceid, 'groupid' => $currentgroup, 'roleid' => $roleid,
 139              'timefrom' => $timefrom, 'action' => $action)));
 140      $event->trigger();
 141  
 142      // from here assume we have at least the module we're using.
 143      $cm = $modinfo->cms[$instanceid];
 144  
 145      // Group security checks.
 146      if (!groups_group_visible($currentgroup, $course, $cm)) {
 147          echo $OUTPUT->heading(get_string("notingroup"));
 148          echo $OUTPUT->footer();
 149          exit;
 150      }
 151  
 152      $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
 153      $table->course = $course;
 154  
 155      $actionheader = !empty($action) ? get_string($action) : get_string('allactions');
 156  
 157      if (empty($CFG->messaging)) {
 158          $table->define_columns(array('fullname', 'count'));
 159          $table->define_headers(array(get_string('user'), $actionheader));
 160      } else {
 161          $table->define_columns(array('fullname', 'count', 'select'));
 162          $mastercheckbox = new \core\output\checkbox_toggleall('participants-table', true, [
 163              'id' => 'select-all-participants',
 164              'name' => 'select-all-participants',
 165              'label' => get_string('select'),
 166              // Consistent labels to prevent select column from resizing.
 167              'selectall' => get_string('select'),
 168              'deselectall' => get_string('select'),
 169          ]);
 170          $table->define_headers(array(get_string('user'), $actionheader, $OUTPUT->render($mastercheckbox)));
 171      }
 172      $table->define_baseurl($baseurl);
 173  
 174      $table->set_attribute('class', 'generaltable generalbox reporttable');
 175  
 176      $table->sortable(true,'lastname','ASC');
 177      $table->no_sorting('select');
 178  
 179      $table->set_control_variables(array(
 180                                          TABLE_VAR_SORT    => 'ssort',
 181                                          TABLE_VAR_HIDE    => 'shide',
 182                                          TABLE_VAR_SHOW    => 'sshow',
 183                                          TABLE_VAR_IFIRST  => 'sifirst',
 184                                          TABLE_VAR_ILAST   => 'silast',
 185                                          TABLE_VAR_PAGE    => 'spage'
 186                                          ));
 187      $table->setup();
 188  
 189      // We want to query both the current context and parent contexts.
 190      list($relatedctxsql, $params) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
 191      $params['roleid'] = $roleid;
 192      $params['instanceid'] = $instanceid;
 193      $params['timefrom'] = $timefrom;
 194  
 195      $groupsql = "";
 196      if (!empty($currentgroup)) {
 197          $groupsql = "JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid)";
 198          $params['groupid'] = $currentgroup;
 199      }
 200  
 201      $countsql = "SELECT COUNT(DISTINCT(ra.userid))
 202                     FROM {role_assignments} ra
 203                     JOIN {user} u ON u.id = ra.userid
 204                     $groupsql
 205                    WHERE ra.contextid $relatedctxsql AND ra.roleid = :roleid";
 206  
 207      $totalcount = $DB->count_records_sql($countsql, $params);
 208  
 209      list($twhere, $tparams) = $table->get_sql_where();
 210      if ($twhere) {
 211          $params = array_merge($params, $tparams);
 212          $matchcount = $DB->count_records_sql($countsql.' AND '.$twhere, $params);
 213      } else {
 214          $matchcount = $totalcount;
 215      }
 216  
 217      $modulename = get_string('modulename', $cm->modname);
 218      echo '<div id="participationreport">' . "\n";
 219      echo '<p class="modulename">' . $modulename . ' ' . $strviews . '<br />'."\n"
 220          . $modulename . ' ' . $strposts . '</p>'."\n";
 221  
 222      $table->initialbars($totalcount > $perpage);
 223      $table->pagesize($perpage, $matchcount);
 224  
 225      if ($uselegacyreader || $onlyuselegacyreader) {
 226          list($actionsql, $actionparams) = report_participation_get_action_sql($action, $cm->modname);
 227          $params = array_merge($params, $actionparams);
 228      }
 229  
 230      if (!$onlyuselegacyreader) {
 231          list($crudsql, $crudparams) = report_participation_get_crud_sql($action);
 232          $params = array_merge($params, $crudparams);
 233      }
 234  
 235      $userfieldsapi = \core_user\fields::for_name();
 236      $usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
 237      $users = array();
 238      // If using legacy log then get users from old table.
 239      if ($uselegacyreader || $onlyuselegacyreader) {
 240          $sql = "SELECT ra.userid, $usernamefields, u.idnumber, l.actioncount AS count
 241                    FROM (SELECT DISTINCT userid FROM {role_assignments} WHERE contextid $relatedctxsql AND roleid = :roleid ) ra
 242                    JOIN {user} u ON u.id = ra.userid
 243               $groupsql
 244               LEFT JOIN (
 245                      SELECT userid, COUNT(action) AS actioncount
 246                        FROM {log}
 247                       WHERE cmid = :instanceid
 248                             AND time > :timefrom " . $actionsql .
 249                  " GROUP BY userid) l ON (l.userid = ra.userid)";
 250          if ($twhere) {
 251              $sql .= ' WHERE '.$twhere; // Initial bar.
 252          }
 253  
 254          if ($table->get_sql_sort()) {
 255              $sql .= ' ORDER BY '.$table->get_sql_sort();
 256          }
 257          if (!$users = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
 258              $users = array(); // Tablelib will handle saying 'Nothing to display' for us.
 259          }
 260      }
 261  
 262      // Get record from sql_internal_table_reader and merge with records got from legacy log (if needed).
 263      if (!$onlyuselegacyreader) {
 264          $sql = "SELECT ra.userid, $usernamefields, u.idnumber, COUNT(DISTINCT l.timecreated) AS count
 265                    FROM {user} u
 266                    JOIN {role_assignments} ra ON u.id = ra.userid AND ra.contextid $relatedctxsql AND ra.roleid = :roleid
 267               $groupsql
 268                    LEFT JOIN {" . $logtable . "} l
 269                       ON l.contextinstanceid = :instanceid
 270                         AND l.timecreated > :timefrom" . $crudsql ."
 271                         AND l.edulevel = :edulevel
 272                         AND l.anonymous = 0
 273                         AND l.contextlevel = :contextlevel
 274                         AND (l.origin = 'web' OR l.origin = 'ws')
 275                         AND l.userid = ra.userid";
 276          // We add this after the WHERE statement that may come below.
 277          $groupbysql = " GROUP BY ra.userid, $usernamefields, u.idnumber";
 278  
 279          $params['edulevel'] = core\event\base::LEVEL_PARTICIPATING;
 280          $params['contextlevel'] = CONTEXT_MODULE;
 281  
 282          if ($twhere) {
 283              $sql .= ' WHERE '.$twhere; // Initial bar.
 284          }
 285          $sql .= $groupbysql;
 286          if ($table->get_sql_sort()) {
 287              $sql .= ' ORDER BY '.$table->get_sql_sort();
 288          }
 289          if ($u = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
 290              if (empty($users)) {
 291                  $users = $u;
 292              } else {
 293                  // Merge two users array.
 294                  foreach ($u as $key => $value) {
 295                      if (isset($users[$key]) && !empty($users[$key]->count)) {
 296                          if ($value->count) {
 297                              $users[$key]->count += $value->count;
 298                          }
 299                      } else {
 300                          $users[$key] = $value;
 301                      }
 302                  }
 303              }
 304              unset($u);
 305              $u = null;
 306          }
 307      }
 308  
 309      $data = array();
 310  
 311      $a = new stdClass();
 312      $a->count = $totalcount;
 313      $a->items = format_string($role->name, true, array('context' => $context));
 314  
 315      if ($matchcount != $totalcount) {
 316          $a->count = $matchcount.'/'.$a->count;
 317      }
 318  
 319      echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
 320  
 321      if (!empty($CFG->messaging)) {
 322          echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="participantsform">'."\n";
 323          echo '<div>'."\n";
 324          echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
 325          echo '<input type="hidden" name="returnto" value="'. s($PAGE->url) .'" />'."\n";
 326          echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'."\n";
 327      }
 328  
 329      foreach ($users as $u) {
 330          $data = array();
 331          $data[] = html_writer::link(new moodle_url('/user/view.php', array('id' => $u->userid, 'course' => $course->id)),
 332              fullname($u, true));
 333          $data[] = !empty($u->count) ? get_string('yes').' ('.$u->count.') ' : get_string('no');
 334  
 335          if (!empty($CFG->messaging)) {
 336              $togglegroup = 'participants-table';
 337              if (empty($u->count)) {
 338                  $togglegroup .= ' no';
 339              }
 340              $checkbox = new \core\output\checkbox_toggleall($togglegroup, false, [
 341                  'classes' => 'usercheckbox',
 342                  'name' => 'user' . $u->userid,
 343                  'value' => $u->count,
 344              ]);
 345              $data[] = $OUTPUT->render($checkbox);
 346          }
 347          $table->add_data($data);
 348      }
 349  
 350      $table->print_html();
 351  
 352      if ($perpage == SHOW_ALL_PAGE_SIZE) {
 353          $perpageurl = new moodle_url($baseurl, array('perpage' => DEFAULT_PAGE_SIZE));
 354          echo html_writer::start_div('', array('id' => 'showall'));
 355          echo html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
 356          echo html_writer::end_div();
 357      } else if ($matchcount > 0 && $perpage < $matchcount) {
 358          $perpageurl = new moodle_url($baseurl, array('perpage' => SHOW_ALL_PAGE_SIZE));
 359          echo html_writer::start_div('', array('id' => 'showall'));
 360          echo html_writer::link($perpageurl, get_string('showall', '', $matchcount));
 361          echo html_writer::end_div();
 362      }
 363  
 364      if (!empty($CFG->messaging)) {
 365          echo '<div class="selectbuttons btn-group">';
 366          if ($perpage >= $matchcount) {
 367              $checknos = new \core\output\checkbox_toggleall('participants-table no', true, [
 368                  'id' => 'select-nos',
 369                  'name' => 'select-nos',
 370                  'label' => get_string('selectnos'),
 371                  'selectall' => get_string('selectnos'),
 372                  'deselectall' => get_string('deselectnos'),
 373              ], true);
 374              echo $OUTPUT->render($checknos);
 375          }
 376          echo '</div>';
 377          echo '<div class="py-3">';
 378          echo html_writer::label(get_string('withselectedusers'), 'formactionid');
 379          $displaylist['#messageselect'] = get_string('messageselectadd');
 380          $withselectedparams = array(
 381              'id' => 'formactionid',
 382              'data-action' => 'toggle',
 383              'data-togglegroup' => 'participants-table',
 384              'data-toggle' => 'action',
 385              'disabled' => true
 386          );
 387          echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), $withselectedparams);
 388          echo '</div>';
 389          echo '</div>'."\n";
 390          echo '</form>'."\n";
 391  
 392          $options = new stdClass();
 393          $options->courseid = $course->id;
 394          $options->noteStateNames = note_get_state_names();
 395          $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
 396          $PAGE->requires->js_call_amd('report_participation/participants', 'init', [$options]);
 397      }
 398      echo '</div>'."\n";
 399  }
 400  
 401  echo $OUTPUT->footer();