2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Participation 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 // Release session lock.
35 \core\session\manager
::write_close();
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.
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');
54 $PAGE->set_pagelayout('admin');
56 if ($action != 'view' and $action != 'post') {
57 $action = ''; // default to all (don't restrict)
60 if (!$course = $DB->get_record('course', array('id'=>$id))) {
61 print_error('invalidcourse');
64 if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
65 print_error('invalidrole');
68 require_login($course);
69 $context = context_course
::instance($course->id
);
70 require_capability('report/participation:view', $context);
72 $strparticipation = get_string('participationreport');
73 $strviews = get_string('views');
74 $strposts = get_string('posts');
75 $strreports = get_string('reports');
77 $actionoptions = report_participation_get_action_options();
78 if (!array_key_exists($action, $actionoptions)) {
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();
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.
90 $logtable = report_participation_get_log_table_name(); // Log table to use for fetaching records.
92 // If no log table, then use legacy records.
93 if (empty($logtable)) {
94 $onlyuselegacyreader = true;
97 $modinfo = get_fast_modinfo($course);
99 // Print first controls.
100 report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid);
102 $baseurl = new moodle_url('/report/participation/index.php', array(
105 'instanceid' => $instanceid,
106 'timefrom' => $timefrom,
108 'perpage' => $perpage,
109 'group' => $currentgroup
111 $select = groups_allgroups_course_menu($course, $baseurl, true, $currentgroup);
113 // User cannot see any group.
114 if (empty($select)) {
115 echo $OUTPUT->heading(get_string("notingroup"));
116 echo $OUTPUT->footer();
122 // Fetch current active group.
123 $groupmode = groups_get_course_groupmode($course);
124 $currentgroup = $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
];
126 if (!empty($instanceid) && !empty($roleid)) {
127 $uselegacyreader = $DB->record_exists('log', ['course' => $course->id
]);
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)));
135 // from here assume we have at least the module we're using.
136 $cm = $modinfo->cms
[$instanceid];
138 // Group security checks.
139 if (!groups_group_visible($currentgroup, $course, $cm)) {
140 echo $OUTPUT->heading(get_string("notingroup"));
141 echo $OUTPUT->footer();
145 $table = new flexible_table('course-participation-'.$course->id
.'-'.$cm->id
.'-'.$roleid);
146 $table->course
= $course;
148 $actionheader = !empty($action) ?
get_string($action) : get_string('allactions');
150 if (empty($CFG->messaging
)) {
151 $table->define_columns(array('fullname', 'count'));
152 $table->define_headers(array(get_string('user'), $actionheader));
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'),
163 $table->define_headers(array(get_string('user'), $actionheader, $OUTPUT->render($mastercheckbox)));
165 $table->define_baseurl($baseurl);
167 $table->set_attribute('class', 'generaltable generalbox reporttable');
169 $table->sortable(true,'lastname','ASC');
170 $table->no_sorting('select');
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'
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;
189 if (!empty($currentgroup)) {
190 $groupsql = "JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid)";
191 $params['groupid'] = $currentgroup;
194 $countsql = "SELECT COUNT(DISTINCT(ra.userid))
195 FROM {role_assignments} ra
196 JOIN {user} u ON u.id = ra.userid
198 WHERE ra.contextid $relatedctxsql AND ra.roleid = :roleid";
200 $totalcount = $DB->count_records_sql($countsql, $params);
202 list($twhere, $tparams) = $table->get_sql_where();
204 $params = array_merge($params, $tparams);
205 $matchcount = $DB->count_records_sql($countsql.' AND '.$twhere, $params);
207 $matchcount = $totalcount;
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";
215 $table->initialbars($totalcount > $perpage);
216 $table->pagesize($perpage, $matchcount);
218 if ($uselegacyreader ||
$onlyuselegacyreader) {
219 list($actionsql, $actionparams) = report_participation_get_action_sql($action, $cm->modname
);
220 $params = array_merge($params, $actionparams);
223 if (!$onlyuselegacyreader) {
224 list($crudsql, $crudparams) = report_participation_get_crud_sql($action);
225 $params = array_merge($params, $crudparams);
228 $usernamefields = get_all_user_name_fields(true, 'u');
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
237 SELECT userid, COUNT(action) AS actioncount
239 WHERE cmid = :instanceid
240 AND time > :timefrom " . $actionsql .
241 " GROUP BY userid) l ON (l.userid = ra.userid)";
243 $sql .= ' WHERE '.$twhere; // Initial bar.
246 if ($table->get_sql_sort()) {
247 $sql .= ' ORDER BY '.$table->get_sql_sort();
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.
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
258 JOIN {role_assignments} ra ON u.id = ra.userid AND ra.contextid $relatedctxsql AND ra.roleid = :roleid
260 LEFT JOIN {" . $logtable . "} l
261 ON l.contextinstanceid = :instanceid
262 AND l.timecreated > :timefrom" . $crudsql ."
263 AND l.edulevel = :edulevel
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";
271 $params['edulevel'] = core\event\base
::LEVEL_PARTICIPATING
;
272 $params['contextlevel'] = CONTEXT_MODULE
;
275 $sql .= ' WHERE '.$twhere; // Initial bar.
278 if ($table->get_sql_sort()) {
279 $sql .= ' ORDER BY '.$table->get_sql_sort();
281 if ($u = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
285 // Merge two users array.
286 foreach ($u as $key => $value) {
287 if (isset($users[$key]) && !empty($users[$key]->count
)) {
289 $users[$key]->count +
= $value->count
;
292 $users[$key] = $value;
304 $a->count
= $totalcount;
305 $a->items
= format_string($role->name
, true, array('context' => $context));
307 if ($matchcount != $totalcount) {
308 $a->count
= $matchcount.'/'.$a->count
;
311 echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
313 if (!empty($CFG->messaging
)) {
314 echo '<form action="'.$CFG->wwwroot
.'/user/action_redir.php" method="post" id="participantsform">'."\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";
321 foreach ($users as $u) {
323 $data[] = html_writer
::link(new moodle_url('/user/view.php', array('id' => $u->userid
, 'course' => $course->id
)),
325 $data[] = !empty($u->count
) ?
get_string('yes').' ('.$u->count
.') ' : get_string('no');
327 if (!empty($CFG->messaging
)) {
328 $togglegroup = 'participants-table';
329 if (empty($u->count
)) {
330 $togglegroup .= ' no';
332 $checkbox = new \core\output\
checkbox_toggleall($togglegroup, false, [
333 'classes' => 'usercheckbox',
334 'name' => 'user' . $u->userid
,
335 'value' => $u->count
,
337 $data[] = $OUTPUT->render($checkbox);
339 $table->add_data($data);
342 $table->print_html();
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();
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'),
366 echo $OUTPUT->render($checknos);
369 echo '<div class="p-y-1">';
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',
379 echo html_writer
::select($displaylist, 'formaction', '', array('' => 'choosedots'), $withselectedparams);
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('core_user/participants', 'init', [$options]);
393 echo $OUTPUT->footer();