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.'/report/participation/locallib.php');
30 define('DEFAULT_PAGE_SIZE', 20);
31 define('SHOW_ALL_PAGE_SIZE', 5000);
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
40 $currentgroup = optional_param('group', 0, PARAM_INT); // Get the active group.
42 $url = new moodle_url('/report/participation/index.php', array('id'=>$id));
43 if ($roleid !== 0) $url->param('roleid');
44 if ($instanceid !== 0) $url->param('instanceid');
45 if ($timefrom !== 0) $url->param('timefrom');
46 if ($action !== '') $url->param('action');
47 if ($page !== 0) $url->param('page');
48 if ($perpage !== DEFAULT_PAGE_SIZE) $url->param('perpage');
50 $PAGE->set_pagelayout('admin');
52 if ($action != 'view' and $action != 'post') {
53 $action = ''; // default to all (don't restrict)
56 if (!$course = $DB->get_record('course', array('id'=>$id))) {
57 print_error('invalidcourse');
60 if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
61 print_error('invalidrole');
64 require_login($course);
65 $context = context_course::instance($course->id);
66 require_capability('report/participation:view', $context);
68 $strparticipation = get_string('participationreport');
69 $strviews = get_string('views');
70 $strposts = get_string('posts');
71 $strreports = get_string('reports');
73 $actionoptions = report_participation_get_action_options();
74 if (!array_key_exists($action, $actionoptions)) {
78 $PAGE->set_title($course->shortname .': '. $strparticipation);
79 $PAGE->set_heading($course->fullname);
80 echo $OUTPUT->header();
82 $uselegacyreader = false; // Use legacy reader with sql_internal_reader to aggregate records.
83 $onlyuselegacyreader = false; // Use only legacy log table to aggregate records.
85 $logtable = report_participation_get_log_table_name(); // Log table to use for fetaching records.
87 // If no log table, then use legacy records.
88 if (empty($logtable)) {
89 $onlyuselegacyreader = true;
92 // If no legacy and no logtable then don't proceed.
93 if (!$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();
101 $modinfo = get_fast_modinfo($course);
103 $minloginternalreader = 0; // Time of first record in sql_internal_reader.
105 if ($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));
109 $uselegacyreader = true;
110 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
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;
122 // If timefrom is greater then first record in sql_internal_reader then get record from sql_internal_reader only.
123 if (!empty($timefrom) && ($minloginternalreader < $timefrom)) {
124 $uselegacyreader = false;
128 // Print first controls.
129 report_participation_print_filter_form($course, $timefrom, $minlog, $action, $roleid, $instanceid);
131 $baseurl = $CFG->wwwroot.'/report/participation/index.php?id='.$course->id.'&roleid='
132 .$roleid.'&instanceid='.$instanceid.'&timefrom='.$timefrom.'&action='.$action.'&perpage='.$perpage;
133 $select = groups_allgroups_course_menu($course, $baseurl, true, $currentgroup);
135 // User cannot see any group.
136 if (empty($select)) {
137 echo $OUTPUT->heading(get_string("notingroup"));
138 echo $OUTPUT->footer();
144 // Fetch current active group.
145 $groupmode = groups_get_course_groupmode($course);
146 $currentgroup = $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
148 if (!empty($instanceid) && !empty($roleid)) {
150 // Trigger a report viewed event.
151 $event = \report_participation\event\report_viewed::create(array('context' => $context,
152 'other' => array('instanceid' => $instanceid, 'groupid' => $currentgroup, 'roleid' => $roleid,
153 'timefrom' => $timefrom, 'action' => $action)));
156 // from here assume we have at least the module we're using.
157 $cm = $modinfo->cms[$instanceid];
159 // Group security checks.
160 if (!groups_group_visible($currentgroup, $course, $cm)) {
161 echo $OUTPUT->heading(get_string("notingroup"));
162 echo $OUTPUT->footer();
166 $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
167 $table->course = $course;
169 $table->define_columns(array('fullname','count','select'));
170 $table->define_headers(array(get_string('user'),((!empty($action)) ? get_string($action) : get_string('allactions')),get_string('select')));
171 $table->define_baseurl($baseurl);
173 $table->set_attribute('cellpadding','5');
174 $table->set_attribute('class', 'generaltable generalbox reporttable');
176 $table->sortable(true,'lastname','ASC');
177 $table->no_sorting('select');
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'
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;
196 if (!empty($currentgroup)) {
197 $groupsql = "JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid)";
198 $params['groupid'] = $currentgroup;
201 $countsql = "SELECT COUNT(DISTINCT(ra.userid))
202 FROM {role_assignments} ra
203 JOIN {user} u ON u.id = ra.userid
205 WHERE ra.contextid $relatedctxsql AND ra.roleid = :roleid";
207 $totalcount = $DB->count_records_sql($countsql, $params);
209 list($twhere, $tparams) = $table->get_sql_where();
211 $params = array_merge($params, $tparams);
212 $matchcount = $DB->count_records_sql($countsql.' AND '.$twhere, $params);
214 $matchcount = $totalcount;
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";
222 $table->initialbars($totalcount > $perpage);
223 $table->pagesize($perpage, $matchcount);
225 if ($uselegacyreader || $onlyuselegacyreader) {
226 list($actionsql, $actionparams) = report_participation_get_action_sql($action, $cm->modname);
227 $params = array_merge($params, $actionparams);
230 if (!$onlyuselegacyreader) {
231 list($crudsql, $crudparams) = report_participation_get_crud_sql($action);
232 $params = array_merge($params, $crudparams);
235 $usernamefields = get_all_user_name_fields(true, 'u');
237 // If using legacy log then get users from old table.
238 if ($uselegacyreader || $onlyuselegacyreader) {
240 if ($uselegacyreader && !empty($minloginternalreader)) {
241 $limittime = ' AND time < :tilltime ';
242 $params['tilltime'] = $minloginternalreader;
244 $sql = "SELECT ra.userid, $usernamefields, u.idnumber, l.actioncount AS count
245 FROM (SELECT DISTINCT userid FROM {role_assignments} WHERE contextid $relatedctxsql AND roleid = :roleid ) ra
246 JOIN {user} u ON u.id = ra.userid
249 SELECT userid, COUNT(action) AS actioncount
251 WHERE cmid = :instanceid
252 AND time > :timefrom " . $limittime . $actionsql .
253 " GROUP BY userid) l ON (l.userid = ra.userid)";
255 $sql .= ' WHERE '.$twhere; // Initial bar.
258 if ($table->get_sql_sort()) {
259 $sql .= ' ORDER BY '.$table->get_sql_sort();
261 if (!$users = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
262 $users = array(); // Tablelib will handle saying 'Nothing to display' for us.
266 // Get record from sql_internal_reader and merge with records got from legacy log (if needed).
267 if (!$onlyuselegacyreader) {
268 $sql = "SELECT ra.userid, $usernamefields, u.idnumber, COUNT(l.actioncount) AS count
269 FROM (SELECT DISTINCT userid FROM {role_assignments} WHERE contextid $relatedctxsql AND roleid = :roleid ) ra
270 JOIN {user} u ON u.id = ra.userid
273 SELECT userid, COUNT(crud) AS actioncount
274 FROM {" . $logtable . "}
275 WHERE contextinstanceid = :instanceid
276 AND timecreated > :timefrom" . $crudsql ."
277 AND edulevel = :edulevel
279 AND contextlevel = :contextlevel
280 AND (origin = 'web' OR origin = 'ws')
281 GROUP BY userid,timecreated) l ON (l.userid = ra.userid)";
282 // We add this after the WHERE statement that may come below.
283 $groupbysql = " GROUP BY ra.userid, $usernamefields, u.idnumber";
285 $params['edulevel'] = core\event\base::LEVEL_PARTICIPATING;
286 $params['contextlevel'] = CONTEXT_MODULE;
289 $sql .= ' WHERE '.$twhere; // Initial bar.
292 if ($table->get_sql_sort()) {
293 $sql .= ' ORDER BY '.$table->get_sql_sort();
295 if ($u = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
299 // Merge two users array.
300 foreach ($u as $key => $value) {
301 if (isset($users[$key]) && !empty($users[$key]->count)) {
303 $users[$key]->count += $value->count;
306 $users[$key] = $value;
318 $a->count = $totalcount;
319 $a->items = $role->name;
321 if ($matchcount != $totalcount) {
322 $a->count = $matchcount.'/'.$a->count;
325 echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
327 echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="studentsform">'."\n";
329 echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
330 echo '<input type="hidden" name="returnto" value="'. s($PAGE->url) .'" />'."\n";
331 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'."\n";
333 foreach ($users as $u) {
334 $data = array('<a href="'.$CFG->wwwroot.'/user/view.php?id='.$u->userid.'&course='.$course->id.'">'.fullname($u,true).'</a>'."\n",
335 ((!empty($u->count)) ? get_string('yes').' ('.$u->count.') ' : get_string('no')),
336 '<input type="checkbox" class="usercheckbox" name="user'.$u->userid.'" value="'.$u->count.'" />'."\n",
338 $table->add_data($data);
341 $table->print_html();
343 if ($perpage == SHOW_ALL_PAGE_SIZE) {
344 echo '<div id="showall"><a href="'.$baseurl.'&perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>'."\n";
346 else if ($matchcount > 0 && $perpage < $matchcount) {
347 echo '<div id="showall"><a href="'.$baseurl.'&perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>'."\n";
350 echo '<div class="selectbuttons">';
351 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> '."\n";
352 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> '."\n";
353 if ($perpage >= $matchcount) {
354 echo '<input type="button" id="checknos" value="'.get_string('selectnos').'" />'."\n";
358 echo html_writer::label(get_string('withselectedusers'), 'formactionselect');
359 $displaylist['messageselect.php'] = get_string('messageselectadd');
360 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionselect'));
361 echo $OUTPUT->help_icon('withselectedusers');
362 echo '<input type="submit" value="' . get_string('ok') . '" />'."\n";
368 $PAGE->requires->js_init_call('M.report_participation.init');
371 echo $OUTPUT->footer();