weekly release 3.5.5+
[moodle.git] / report / participation / index.php
blobef66eef7fd76ec863ef298ea4a48c5bce8ab163e
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.
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/>.
17 /**
18 * Participation report
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
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');
31 define('DEFAULT_PAGE_SIZE', 20);
32 define('SHOW_ALL_PAGE_SIZE', 5000);
34 $id = required_param('id', PARAM_INT); // course id.
35 $roleid = optional_param('roleid', 0, PARAM_INT); // which role to show
36 $instanceid = optional_param('instanceid', 0, PARAM_INT); // instance we're looking at.
37 $timefrom = optional_param('timefrom', 0, PARAM_INT); // how far back to look...
38 $action = optional_param('action', '', PARAM_ALPHA);
39 $page = optional_param('page', 0, PARAM_INT); // which page to show
40 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
41 $currentgroup = optional_param('group', null, PARAM_INT); // Get the active group.
43 $url = new moodle_url('/report/participation/index.php', array('id'=>$id));
44 if ($roleid !== 0) $url->param('roleid');
45 if ($instanceid !== 0) $url->param('instanceid');
46 if ($timefrom !== 0) $url->param('timefrom');
47 if ($action !== '') $url->param('action');
48 if ($page !== 0) $url->param('page');
49 if ($perpage !== DEFAULT_PAGE_SIZE) $url->param('perpage');
50 $PAGE->set_url($url);
51 $PAGE->set_pagelayout('admin');
53 if ($action != 'view' and $action != 'post') {
54 $action = ''; // default to all (don't restrict)
57 if (!$course = $DB->get_record('course', array('id'=>$id))) {
58 print_error('invalidcourse');
61 if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
62 print_error('invalidrole');
65 require_login($course);
66 $context = context_course::instance($course->id);
67 require_capability('report/participation:view', $context);
69 $strparticipation = get_string('participationreport');
70 $strviews = get_string('views');
71 $strposts = get_string('posts');
72 $strreports = get_string('reports');
74 $actionoptions = report_participation_get_action_options();
75 if (!array_key_exists($action, $actionoptions)) {
76 $action = '';
79 $PAGE->set_title(format_string($course->shortname, true, array('context' => $context)) .': '. $strparticipation);
80 $PAGE->set_heading(format_string($course->fullname, true, array('context' => $context)));
81 echo $OUTPUT->header();
83 $uselegacyreader = false; // Use legacy reader with sql_internal_table_reader to aggregate records.
84 $onlyuselegacyreader = false; // Use only legacy log table to aggregate records.
86 $logtable = report_participation_get_log_table_name(); // Log table to use for fetaching records.
88 // If no log table, then use legacy records.
89 if (empty($logtable)) {
90 $onlyuselegacyreader = true;
93 // If no legacy and no logtable then don't proceed.
94 if (!$onlyuselegacyreader && empty($logtable)) {
95 echo $OUTPUT->box_start('generalbox', 'notice');
96 echo get_string('nologreaderenabled', 'report_participation');
97 echo $OUTPUT->box_end();
98 echo $OUTPUT->footer();
99 die();
102 $modinfo = get_fast_modinfo($course);
104 $minloginternalreader = 0; // Time of first record in sql_internal_table_reader.
106 if ($onlyuselegacyreader) {
107 // If no sql_inrenal_reader enabled then get min. time from log table.
108 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
109 } else {
110 $uselegacyreader = true;
111 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
113 // If legacy reader is not logging then get data from new log table.
114 // Get minimum log time for this course from preferred log reader.
115 $minloginternalreader = $DB->get_field_sql('SELECT min(timecreated) FROM {' . $logtable . '}
116 WHERE courseid = ?', array($course->id));
117 // If new log store has oldest data then don't use old log table.
118 if (empty($minlog) || ($minloginternalreader <= $minlog)) {
119 $uselegacyreader = false;
120 $minlog = $minloginternalreader;
123 // If timefrom is greater then first record in sql_internal_table_reader then get record from sql_internal_table_reader only.
124 if (!empty($timefrom) && ($minloginternalreader < $timefrom)) {
125 $uselegacyreader = false;
129 // Print first controls.
130 report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid);
132 $baseurl = new moodle_url('/report/participation/index.php', array(
133 'id' => $course->id,
134 'roleid' => $roleid,
135 'instanceid' => $instanceid,
136 'timefrom' => $timefrom,
137 'action' => $action,
138 'perpage' => $perpage,
139 'group' => $currentgroup
141 $select = groups_allgroups_course_menu($course, $baseurl, true, $currentgroup);
143 // User cannot see any group.
144 if (empty($select)) {
145 echo $OUTPUT->heading(get_string("notingroup"));
146 echo $OUTPUT->footer();
147 exit;
148 } else {
149 echo $select;
152 // Fetch current active group.
153 $groupmode = groups_get_course_groupmode($course);
154 $currentgroup = $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
156 if (!empty($instanceid) && !empty($roleid)) {
158 // Trigger a report viewed event.
159 $event = \report_participation\event\report_viewed::create(array('context' => $context,
160 'other' => array('instanceid' => $instanceid, 'groupid' => $currentgroup, 'roleid' => $roleid,
161 'timefrom' => $timefrom, 'action' => $action)));
162 $event->trigger();
164 // from here assume we have at least the module we're using.
165 $cm = $modinfo->cms[$instanceid];
167 // Group security checks.
168 if (!groups_group_visible($currentgroup, $course, $cm)) {
169 echo $OUTPUT->heading(get_string("notingroup"));
170 echo $OUTPUT->footer();
171 exit;
174 $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
175 $table->course = $course;
177 $actionheader = !empty($action) ? get_string($action) : get_string('allactions');
179 if (empty($CFG->messaging)) {
180 $table->define_columns(array('fullname', 'count'));
181 $table->define_headers(array(get_string('user'), $actionheader));
182 } else {
183 $table->define_columns(array('fullname', 'count', 'select'));
184 $table->define_headers(array(get_string('user'), $actionheader, get_string('select')));
186 $table->define_baseurl($baseurl);
188 $table->set_attribute('class', 'generaltable generalbox reporttable');
190 $table->sortable(true,'lastname','ASC');
191 $table->no_sorting('select');
193 $table->set_control_variables(array(
194 TABLE_VAR_SORT => 'ssort',
195 TABLE_VAR_HIDE => 'shide',
196 TABLE_VAR_SHOW => 'sshow',
197 TABLE_VAR_IFIRST => 'sifirst',
198 TABLE_VAR_ILAST => 'silast',
199 TABLE_VAR_PAGE => 'spage'
201 $table->setup();
203 // We want to query both the current context and parent contexts.
204 list($relatedctxsql, $params) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
205 $params['roleid'] = $roleid;
206 $params['instanceid'] = $instanceid;
207 $params['timefrom'] = $timefrom;
209 $groupsql = "";
210 if (!empty($currentgroup)) {
211 $groupsql = "JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid)";
212 $params['groupid'] = $currentgroup;
215 $countsql = "SELECT COUNT(DISTINCT(ra.userid))
216 FROM {role_assignments} ra
217 JOIN {user} u ON u.id = ra.userid
218 $groupsql
219 WHERE ra.contextid $relatedctxsql AND ra.roleid = :roleid";
221 $totalcount = $DB->count_records_sql($countsql, $params);
223 list($twhere, $tparams) = $table->get_sql_where();
224 if ($twhere) {
225 $params = array_merge($params, $tparams);
226 $matchcount = $DB->count_records_sql($countsql.' AND '.$twhere, $params);
227 } else {
228 $matchcount = $totalcount;
231 $modulename = get_string('modulename', $cm->modname);
232 echo '<div id="participationreport">' . "\n";
233 echo '<p class="modulename">' . $modulename . ' ' . $strviews . '<br />'."\n"
234 . $modulename . ' ' . $strposts . '</p>'."\n";
236 $table->initialbars($totalcount > $perpage);
237 $table->pagesize($perpage, $matchcount);
239 if ($uselegacyreader || $onlyuselegacyreader) {
240 list($actionsql, $actionparams) = report_participation_get_action_sql($action, $cm->modname);
241 $params = array_merge($params, $actionparams);
244 if (!$onlyuselegacyreader) {
245 list($crudsql, $crudparams) = report_participation_get_crud_sql($action);
246 $params = array_merge($params, $crudparams);
249 $usernamefields = get_all_user_name_fields(true, 'u');
250 $users = array();
251 // If using legacy log then get users from old table.
252 if ($uselegacyreader || $onlyuselegacyreader) {
253 $limittime = '';
254 if ($uselegacyreader && !empty($minloginternalreader)) {
255 $limittime = ' AND time < :tilltime ';
256 $params['tilltime'] = $minloginternalreader;
258 $sql = "SELECT ra.userid, $usernamefields, u.idnumber, l.actioncount AS count
259 FROM (SELECT DISTINCT userid FROM {role_assignments} WHERE contextid $relatedctxsql AND roleid = :roleid ) ra
260 JOIN {user} u ON u.id = ra.userid
261 $groupsql
262 LEFT JOIN (
263 SELECT userid, COUNT(action) AS actioncount
264 FROM {log}
265 WHERE cmid = :instanceid
266 AND time > :timefrom " . $limittime . $actionsql .
267 " GROUP BY userid) l ON (l.userid = ra.userid)";
268 if ($twhere) {
269 $sql .= ' WHERE '.$twhere; // Initial bar.
272 if ($table->get_sql_sort()) {
273 $sql .= ' ORDER BY '.$table->get_sql_sort();
275 if (!$users = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
276 $users = array(); // Tablelib will handle saying 'Nothing to display' for us.
280 // Get record from sql_internal_table_reader and merge with records got from legacy log (if needed).
281 if (!$onlyuselegacyreader) {
282 $sql = "SELECT ra.userid, $usernamefields, u.idnumber, COUNT(DISTINCT l.timecreated) AS count
283 FROM {user} u
284 JOIN {role_assignments} ra ON u.id = ra.userid AND ra.contextid $relatedctxsql AND ra.roleid = :roleid
285 $groupsql
286 LEFT JOIN {" . $logtable . "} l
287 ON l.contextinstanceid = :instanceid
288 AND l.timecreated > :timefrom" . $crudsql ."
289 AND l.edulevel = :edulevel
290 AND l.anonymous = 0
291 AND l.contextlevel = :contextlevel
292 AND (l.origin = 'web' OR l.origin = 'ws')
293 AND l.userid = ra.userid";
294 // We add this after the WHERE statement that may come below.
295 $groupbysql = " GROUP BY ra.userid, $usernamefields, u.idnumber";
297 $params['edulevel'] = core\event\base::LEVEL_PARTICIPATING;
298 $params['contextlevel'] = CONTEXT_MODULE;
300 if ($twhere) {
301 $sql .= ' WHERE '.$twhere; // Initial bar.
303 $sql .= $groupbysql;
304 if ($table->get_sql_sort()) {
305 $sql .= ' ORDER BY '.$table->get_sql_sort();
307 if ($u = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
308 if (empty($users)) {
309 $users = $u;
310 } else {
311 // Merge two users array.
312 foreach ($u as $key => $value) {
313 if (isset($users[$key]) && !empty($users[$key]->count)) {
314 if ($value->count) {
315 $users[$key]->count += $value->count;
317 } else {
318 $users[$key] = $value;
322 unset($u);
323 $u = null;
327 $data = array();
329 $a = new stdClass();
330 $a->count = $totalcount;
331 $a->items = format_string($role->name, true, array('context' => $context));
333 if ($matchcount != $totalcount) {
334 $a->count = $matchcount.'/'.$a->count;
337 echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
339 if (!empty($CFG->messaging)) {
340 echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="participantsform">'."\n";
341 echo '<div>'."\n";
342 echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
343 echo '<input type="hidden" name="returnto" value="'. s($PAGE->url) .'" />'."\n";
344 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'."\n";
347 foreach ($users as $u) {
348 $data = array();
349 $data[] = html_writer::link(new moodle_url('/user/view.php', array('id' => $u->userid, 'course' => $course->id)),
350 fullname($u, true));
351 $data[] = !empty($u->count) ? get_string('yes').' ('.$u->count.') ' : get_string('no');
353 if (!empty($CFG->messaging)) {
354 $data[] = '<input type="checkbox" class="usercheckbox" name="user'.$u->userid.'" value="'.$u->count.'" />';
356 $table->add_data($data);
359 $table->print_html();
361 if ($perpage == SHOW_ALL_PAGE_SIZE) {
362 $perpageurl = new moodle_url($baseurl, array('perpage' => DEFAULT_PAGE_SIZE));
363 echo html_writer::start_div('', array('id' => 'showall'));
364 echo html_writer::link($perpageurl, get_string('showperpage', '', DEFAULT_PAGE_SIZE));
365 echo html_writer::end_div();
366 } else if ($matchcount > 0 && $perpage < $matchcount) {
367 $perpageurl = new moodle_url($baseurl, array('perpage' => SHOW_ALL_PAGE_SIZE));
368 echo html_writer::start_div('', array('id' => 'showall'));
369 echo html_writer::link($perpageurl, get_string('showall', '', $matchcount));
370 echo html_writer::end_div();
373 if (!empty($CFG->messaging)) {
374 $buttonclasses = 'btn btn-secondary';
375 echo '<div class="selectbuttons btn-group">';
376 echo '<input type="button" id="checkallonpage" value="'.get_string('selectall').'" class="'. $buttonclasses .'"> '."\n";
377 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" class="'. $buttonclasses .'"> '."\n";
378 if ($perpage >= $matchcount) {
379 echo '<input type="button" id="checkallnos" value="'.get_string('selectnos').'" class="'. $buttonclasses .'">'."\n";
381 echo '</div>';
382 echo '<div class="p-y-1">';
383 echo html_writer::label(get_string('withselectedusers'), 'formactionselect');
384 $displaylist['#messageselect'] = get_string('messageselectadd');
385 echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
386 echo '</div>';
387 echo '</div>'."\n";
388 echo '</form>'."\n";
390 $options = new stdClass();
391 $options->courseid = $course->id;
392 $options->noteStateNames = note_get_state_names();
393 $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
394 $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
396 echo '</div>'."\n";
399 echo $OUTPUT->footer();