Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / report / participation / index.php
blob5e3684658c4fbb776491e0de4d732070919e25d9
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');
29 define('DEFAULT_PAGE_SIZE', 20);
30 define('SHOW_ALL_PAGE_SIZE', 5000);
32 $id = required_param('id', PARAM_INT); // course id.
33 $roleid = optional_param('roleid', 0, PARAM_INT); // which role to show
34 $instanceid = optional_param('instanceid', 0, PARAM_INT); // instance we're looking at.
35 $timefrom = optional_param('timefrom', 0, PARAM_INT); // how far back to look...
36 $action = optional_param('action', '', PARAM_ALPHA);
37 $page = optional_param('page', 0, PARAM_INT); // which page to show
38 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
40 $url = new moodle_url('/report/participation/index.php', array('id'=>$id));
41 if ($roleid !== 0) $url->param('roleid');
42 if ($instanceid !== 0) $url->param('instanceid');
43 if ($timefrom !== 0) $url->param('timefrom');
44 if ($action !== '') $url->param('action');
45 if ($page !== 0) $url->param('page');
46 if ($perpage !== DEFAULT_PAGE_SIZE) $url->param('perpage');
47 $PAGE->set_url($url);
48 $PAGE->set_pagelayout('admin');
50 if ($action != 'view' and $action != 'post') {
51 $action = ''; // default to all (don't restrict)
54 if (!$course = $DB->get_record('course', array('id'=>$id))) {
55 print_error('invalidcourse');
58 if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
59 print_error('invalidrole');
62 require_login($course);
63 $context = context_course::instance($course->id);
64 require_capability('report/participation:view', $context);
66 add_to_log($course->id, "course", "report participation", "report/participation/index.php?id=$course->id", $course->id);
68 $strparticipation = get_string('participationreport');
69 $strviews = get_string('views');
70 $strposts = get_string('posts');
71 $strview = get_string('view');
72 $strpost = get_string('post');
73 $strallactions = get_string('allactions');
74 $strreports = get_string('reports');
76 $actionoptions = array('' => $strallactions,
77 'view' => $strview,
78 'post' => $strpost,);
79 if (!array_key_exists($action, $actionoptions)) {
80 $action = '';
83 $PAGE->set_title($course->shortname .': '. $strparticipation);
84 $PAGE->set_heading($course->fullname);
85 echo $OUTPUT->header();
87 $modinfo = get_fast_modinfo($course);
89 $modules = $DB->get_records_select('modules', "visible = 1", null, 'name ASC');
91 $instanceoptions = array();
92 foreach ($modules as $module) {
93 if (empty($modinfo->instances[$module->name])) {
94 continue;
96 $instances = array();
97 foreach ($modinfo->instances[$module->name] as $cm) {
98 // Skip modules such as label which do not actually have links;
99 // this means there's nothing to participate in
100 if (!$cm->has_view()) {
101 continue;
103 $instances[$cm->id] = format_string($cm->name);
105 if (count($instances) == 0) {
106 continue;
108 $instanceoptions[] = array(get_string('modulenameplural', $module->name)=>$instances);
111 $timeoptions = array();
112 // get minimum log time for this course
113 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
115 $now = usergetmidnight(time());
117 // days
118 for ($i = 1; $i < 7; $i++) {
119 if (strtotime('-'.$i.' days',$now) >= $minlog) {
120 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
123 // weeks
124 for ($i = 1; $i < 10; $i++) {
125 if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
126 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
129 // months
130 for ($i = 2; $i < 12; $i++) {
131 if (strtotime('-'.$i.' months',$now) >= $minlog) {
132 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
135 // try a year
136 if (strtotime('-1 year',$now) >= $minlog) {
137 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
140 // TODO: we need a new list of roles that are visible here
141 $roles = get_roles_used_in_context($context);
142 $guestrole = get_guest_role();
143 $roles[$guestrole->id] = $guestrole;
144 $roleoptions = role_fix_names($roles, $context, ROLENAME_ALIAS, true);
146 // print first controls.
147 echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n".
148 '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
149 echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
150 echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
151 echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
152 echo html_writer::select($timeoptions,'timefrom',$timefrom);
153 echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
154 echo html_writer::select($roleoptions,'roleid',$roleid,false);
155 echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
156 echo html_writer::select($actionoptions,'action',$action,false);
157 echo '<input type="submit" value="'.get_string('go').'" />'."\n</div></form>\n";
159 $baseurl = $CFG->wwwroot.'/report/participation/index.php?id='.$course->id.'&amp;roleid='
160 .$roleid.'&amp;instanceid='.$instanceid.'&amp;timefrom='.$timefrom.'&amp;action='.$action.'&amp;perpage='.$perpage;
162 if (!empty($instanceid) && !empty($roleid)) {
163 // from here assume we have at least the module we're using.
164 $cm = $modinfo->cms[$instanceid];
165 $modulename = get_string('modulename', $cm->modname);
167 include_once($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php');
169 $viewfun = $cm->modname.'_get_view_actions';
170 $postfun = $cm->modname.'_get_post_actions';
172 if (!function_exists($viewfun) || !function_exists($postfun)) {
173 print_error('modulemissingcode', 'error', $baseurl, $cm->modname);
176 $viewnames = $viewfun();
177 $postnames = $postfun();
179 $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
180 $table->course = $course;
182 $table->define_columns(array('fullname','count','select'));
183 $table->define_headers(array(get_string('user'),((!empty($action)) ? get_string($action) : get_string('allactions')),get_string('select')));
184 $table->define_baseurl($baseurl);
186 $table->set_attribute('cellpadding','5');
187 $table->set_attribute('class', 'generaltable generalbox reporttable');
189 $table->sortable(true,'lastname','ASC');
190 $table->no_sorting('select');
192 $table->set_control_variables(array(
193 TABLE_VAR_SORT => 'ssort',
194 TABLE_VAR_HIDE => 'shide',
195 TABLE_VAR_SHOW => 'sshow',
196 TABLE_VAR_IFIRST => 'sifirst',
197 TABLE_VAR_ILAST => 'silast',
198 TABLE_VAR_PAGE => 'spage'
200 $table->setup();
202 switch ($action) {
203 case 'view':
204 $actions = $viewnames;
205 break;
206 case 'post':
207 $actions = $postnames;
208 break;
209 default:
210 // some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
211 $actions = array_merge($viewnames, $postnames);
214 list($actionsql, $params) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action');
215 $actionsql = "action $actionsql";
217 $relatedcontexts = get_related_contexts_string($context);
219 $sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, l.actioncount AS count
220 FROM (SELECT * FROM {role_assignments} WHERE contextid $relatedcontexts AND roleid = :roleid ) ra
221 JOIN {user} u ON u.id = ra.userid
222 LEFT JOIN (
223 SELECT userid, COUNT(action) AS actioncount FROM {log} WHERE cmid = :instanceid AND time > :timefrom AND $actionsql GROUP BY userid
224 ) l ON (l.userid = ra.userid)";
225 $params['roleid'] = $roleid;
226 $params['instanceid'] = $instanceid;
227 $params['timefrom'] = $timefrom;
229 list($twhere, $tparams) = $table->get_sql_where();
230 if ($twhere) {
231 $sql .= ' WHERE '.$twhere; //initial bar
232 $params = array_merge($params, $tparams);
235 if ($table->get_sql_sort()) {
236 $sql .= ' ORDER BY '.$table->get_sql_sort();
239 $countsql = "SELECT COUNT(DISTINCT(ra.userid))
240 FROM {role_assignments} ra
241 JOIN {user} u ON u.id = ra.userid
242 WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid";
244 $totalcount = $DB->count_records_sql($countsql, $params);
246 if ($twhere) {
247 $matchcount = $DB->count_records_sql($countsql.' AND '.$twhere, $params);
248 } else {
249 $matchcount = $totalcount;
252 echo '<div id="participationreport">' . "\n";
253 echo '<p class="modulename">'.$modulename . ' ' . $strviews.': '.implode(', ',$viewnames).'<br />'."\n"
254 . $modulename . ' ' . $strposts.': '.implode(', ',$postnames).'</p>'."\n";
256 $table->initialbars($totalcount > $perpage);
257 $table->pagesize($perpage, $matchcount);
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.
263 $data = array();
265 $a = new stdClass();
266 $a->count = $totalcount;
267 $a->items = $role->name;
269 if ($matchcount != $totalcount) {
270 $a->count = $matchcount.'/'.$a->count;
273 echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
275 echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="studentsform">'."\n";
276 echo '<div>'."\n";
277 echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
278 echo '<input type="hidden" name="returnto" value="'. s($PAGE->url) .'" />'."\n";
279 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'."\n";
281 foreach ($users as $u) {
282 $data = array('<a href="'.$CFG->wwwroot.'/user/view.php?id='.$u->userid.'&amp;course='.$course->id.'">'.fullname($u,true).'</a>'."\n",
283 ((!empty($u->count)) ? get_string('yes').' ('.$u->count.') ' : get_string('no')),
284 '<input type="checkbox" class="usercheckbox" name="user'.$u->userid.'" value="'.$u->count.'" />'."\n",
286 $table->add_data($data);
289 $table->print_html();
291 if ($perpage == SHOW_ALL_PAGE_SIZE) {
292 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>'."\n";
294 else if ($matchcount > 0 && $perpage < $matchcount) {
295 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>'."\n";
298 echo '<div class="selectbuttons">';
299 echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> '."\n";
300 echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> '."\n";
301 if ($perpage >= $matchcount) {
302 echo '<input type="button" id="checknos" value="'.get_string('selectnos').'" />'."\n";
304 echo '</div>';
305 echo '<div>';
306 echo html_writer::label(get_string('withselectedusers'), 'formactionselect');
307 $displaylist['messageselect.php'] = get_string('messageselectadd');
308 echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionselect'));
309 echo $OUTPUT->help_icon('withselectedusers');
310 echo '<input type="submit" value="' . get_string('ok') . '" />'."\n";
311 echo '</div>';
312 echo '</div>'."\n";
313 echo '</form>'."\n";
314 echo '</div>'."\n";
316 $PAGE->requires->js_init_call('M.report_participation.init');
319 echo $OUTPUT->footer();