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