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