Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / course / report / participation / index.php
blob1acad29f7574e59bbae0cffb009150e96b79a8c8
1 <?php // $Id$
3 require_once('../../../config.php');
4 require_once($CFG->dirroot.'/lib/tablelib.php');
6 define('DEFAULT_PAGE_SIZE', 20);
7 define('SHOW_ALL_PAGE_SIZE', 5000);
9 $id = required_param('id', PARAM_INT); // course id.
10 $roleid = optional_param('roleid', 0, PARAM_INT); // which role to show
11 $instanceid = optional_param('instanceid', 0, PARAM_INT); // instance we're looking at.
12 $timefrom = optional_param('timefrom', 0, PARAM_INT); // how far back to look...
13 $action = optional_param('action', '', PARAM_ALPHA);
14 $page = optional_param('page', 0, PARAM_INT); // which page to show
15 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
17 if ($action != 'view' and $action != 'post') {
18 $action = ''; // default to all (don't restrict)
21 if (!$course = get_record('course', 'id', $id)) {
22 print_error('invalidcourse');
25 if ($roleid != 0 and !$role = get_record('role', 'id', $roleid)) {
26 print_error('invalidrole');
29 require_login($course);
30 $context = get_context_instance(CONTEXT_COURSE, $course->id);
31 require_capability('moodle/site:viewreports', $context);
33 add_to_log($course->id, "course", "report participation", "report/participation/index.php?id=$course->id", $course->id);
35 $strparticipation = get_string('participationreport');
36 $strviews = get_string('views');
37 $strposts = get_string('posts');
38 $strview = get_string('view');
39 $strpost = get_string('post');
40 $strallactions = get_string('allactions');
41 $strreports = get_string('reports');
43 $actionoptions = array('' => $strallactions,
44 'view' => $strview,
45 'post' => $strpost,);
46 if (!in_array($action, $actionoptions)) {
47 $action = '';
50 $navlinks = array();
51 $navlinks[] = array('name' => $strreports, 'link' => "../../report.php?id=$course->id", 'type' => 'misc');
52 $navlinks[] = array('name' => $strparticipation, 'link' => null, 'type' => 'misc');
53 $navigation = build_navigation($navlinks);
54 print_header("$course->shortname: $strparticipation", $course->fullname, $navigation);
56 $modinfo = get_fast_modinfo($course);
58 $modules = get_records_select('modules', "visible = 1 AND name <> 'label'", 'name ASC');
60 $instanceoptions = array();
61 foreach ($modules as $module) {
62 if (empty($modinfo->instances[$module->name])) {
63 continue;
65 $agroup = get_string('modulenameplural', $module->name);
66 $instanceoptions[$agroup] = array();
67 foreach ($modinfo->instances[$module->name] as $cm) {
68 $instanceoptions[$agroup][$cm->id] = format_string($cm->name);
72 $timeoptions = array();
73 // get minimum log time for this course
74 $minlog = get_field_sql('SELECT min(time) FROM '.$CFG->prefix.'log WHERE course = '.$course->id);
76 $now = usergetmidnight(time());
78 // days
79 for ($i = 1; $i < 7; $i++) {
80 if (strtotime('-'.$i.' days',$now) >= $minlog) {
81 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
84 // weeks
85 for ($i = 1; $i < 10; $i++) {
86 if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
87 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
90 // months
91 for ($i = 2; $i < 12; $i++) {
92 if (strtotime('-'.$i.' months',$now) >= $minlog) {
93 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
96 // try a year
97 if (strtotime('-1 year',$now) >= $minlog) {
98 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
101 $roleoptions = array();
102 if ($roles = get_roles_used_in_context($context)) {
103 foreach ($roles as $r) {
104 $roleoptions[$r->id] = $r->name;
107 $guestrole = get_guest_role();
108 if (empty($roleoptions[$guestrole->id])) {
109 $roleoptions[$guestrole->id] = $guestrole->name;
112 $roleoptions = role_fix_names($roleoptions, $context);
114 // print first controls.
115 echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n".
116 '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
117 echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
118 choose_from_menu_nested($instanceoptions,'instanceid',$instanceid);
119 echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
120 choose_from_menu($timeoptions,'timefrom',$timefrom);
121 echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
122 choose_from_menu($roleoptions,'roleid',$roleid,'');
123 echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
124 choose_from_menu($actionoptions,'action',$action,'');
125 helpbutton('participationreport',get_string('participationreport'));
126 echo '<input type="submit" value="'.get_string('go').'" />'."\n</div></form>\n";
128 $baseurl = $CFG->wwwroot.'/course/report/participation/index.php?id='.$course->id.'&amp;roleid='
129 .$roleid.'&amp;instanceid='.$instanceid.'&amp;timefrom='.$timefrom.'&amp;action='.$action.'&amp;perpage='.$perpage;
131 if (!empty($instanceid) && !empty($roleid)) {
132 // from here assume we have at least the module we're using.
133 $cm = $modinfo->cms[$instanceid];
134 $modulename = get_string('modulename', $cm->modname);
136 include_once($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php');
138 $viewfun = $cm->modname.'_get_view_actions';
139 $postfun = $cm->modname.'_get_post_actions';
141 if (!function_exists($viewfun) || !function_exists($postfun)) {
142 print_error('modulemissingcode', 'error', $baseurl, $cm->modname);
145 $viewnames = $viewfun();
146 $postnames = $postfun();
148 $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
149 $table->course = $course;
151 $table->define_columns(array('fullname','count','select'));
152 $table->define_headers(array(get_string('user'),((!empty($action)) ? get_string($action) : get_string('allactions')),get_string('select')));
153 $table->define_baseurl($baseurl);
155 $table->set_attribute('cellpadding','5');
156 $table->set_attribute('class', 'generaltable generalbox reporttable');
158 $table->sortable(true,'lastname','ASC');
159 $table->no_sorting('select');
161 $table->set_control_variables(array(
162 TABLE_VAR_SORT => 'ssort',
163 TABLE_VAR_HIDE => 'shide',
164 TABLE_VAR_SHOW => 'sshow',
165 TABLE_VAR_IFIRST => 'sifirst',
166 TABLE_VAR_ILAST => 'silast',
167 TABLE_VAR_PAGE => 'spage'
169 $table->setup();
171 switch ($action) {
172 case 'view':
173 $actionsql = 'l.action IN (\''.implode('\',\'', $viewnames).'\' )';
174 break;
175 case 'post':
176 $actionsql = 'l.action IN (\''.implode('\',\'', $postnames).'\' )';
177 break;
178 default:
179 // some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
180 $actionsql = 'l.action IN (\''.implode('\',\'', array_merge($viewnames, $postnames)).'\' )';
183 $relatedcontexts = get_related_contexts_string($context);
185 $sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, COUNT(l.action) AS count
186 FROM {$CFG->prefix}role_assignments ra
187 JOIN {$CFG->prefix}user u ON u.id = ra.userid
188 LEFT OUTER JOIN {$CFG->prefix}log l ON l.userid = ra.userid
189 WHERE ra.contextid $relatedcontexts AND ra.roleid = $roleid AND
190 (l.id IS NULL OR
191 (l.cmid = $instanceid AND l.time > $timefrom AND $actionsql)
194 if ($table->get_sql_where()) {
195 $sql .= ' AND '.$table->get_sql_where(); //initial bar
198 $sql .= " GROUP BY ra.userid, u.firstname, u.lastname, u.idnumber";
200 if ($table->get_sql_sort()) {
201 $sql .= ' ORDER BY '.$table->get_sql_sort();
204 $countsql = "SELECT COUNT(DISTINCT(ra.userid))
205 FROM {$CFG->prefix}role_assignments ra
206 JOIN {$CFG->prefix}user u ON u.id = ra.userid
207 LEFT OUTER JOIN {$CFG->prefix}log l ON l.userid = ra.userid
208 WHERE ra.contextid $relatedcontexts AND ra.roleid = $roleid AND
209 (l.id IS NULL OR
210 (l.cmid = $instanceid AND l.time > $timefrom AND $actionsql)
213 $totalcount = count_records_sql($countsql);
215 if ($table->get_sql_where()) {
216 $matchcount = count_records_sql($countsql.' AND '.$table->get_sql_where());
217 } else {
218 $matchcount = $totalcount;
221 echo '<div id="participationreport">' . "\n";
222 echo '<p class="modulename">'.$modulename . ' ' . $strviews.': '.implode(', ',$viewnames).'<br />'."\n"
223 . $modulename . ' ' . $strposts.': '.implode(', ',$postnames).'</p>'."\n";
225 $table->initialbars($totalcount > $perpage);
226 $table->pagesize($perpage, $matchcount);
228 if (!$users = get_records_sql($sql, $table->get_page_start(), $table->get_page_size())) {
229 $users = array(); // tablelib will handle saying 'Nothing to display' for us.
232 $data = array();
234 $a->count = $totalcount;
235 $a->items = $role->name;
237 if ($matchcount != $totalcount) {
238 $a->count = $matchcount.'/'.$a->count;
241 echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
242 echo '
243 <script type="text/javascript">
244 //<![CDATA[
245 function checksubmit(form) {
246 var destination = form.formaction.options[form.formaction.selectedIndex].value;
247 if (destination == "" || !checkchecked(form)) {
248 form.formaction.selectedIndex = 0;
249 return false;
250 } else {
251 return true;
255 function checkchecked(form) {
256 var inputs = document.getElementsByTagName(\'INPUT\');
257 var checked = false;
258 inputs = filterByParent(inputs, function() {return form;});
259 for(var i = 0; i < inputs.length; ++i) {
260 if(inputs[i].type == \'checkbox\' && inputs[i].checked) {
261 checked = true;
264 return checked;
267 function checknos() {
268 void(d=document);
269 void(el=d.getElementsByTagName(\'INPUT\'));
270 for(i=0;i<el.length;i++) {
271 if (el[i].value == 0) {
272 void(el[i].checked=1)
277 //]]>
278 </script>
280 echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="studentsform" onsubmit="return checksubmit(this);">'."\n";
281 echo '<div>'."\n";
282 echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
283 echo '<input type="hidden" name="returnto" value="'. format_string($_SERVER['REQUEST_URI']) .'" />'."\n";
284 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />'."\n";
286 foreach ($users as $u) {
287 $data = array('<a href="'.$CFG->wwwroot.'/user/view.php?id='.$u->userid.'&amp;course='.$course->id.'">'.fullname($u,true).'</a>'."\n",
288 ((!empty($u->count)) ? get_string('yes').' ('.$u->count.') ' : get_string('no')),
289 '<input type="checkbox" name="user'.$u->userid.'" value="'.$u->count.'" />'."\n",
291 $table->add_data($data);
294 $table->print_html();
296 if ($perpage == SHOW_ALL_PAGE_SIZE) {
297 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>'."\n";
299 else if ($matchcount > 0 && $perpage < $matchcount) {
300 echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>'."\n";
303 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> '."\n";
304 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> '."\n";
305 if ($perpage >= $matchcount) {
306 echo '<input type="button" onclick="checknos()" value="'.get_string('selectnos').'" />'."\n";
308 $displaylist['messageselect.php'] = get_string('messageselectadd');
309 choose_from_menu ($displaylist, "formaction", "", get_string("withselectedusers"), "if(checksubmit(this.form))this.form.submit();", "");
310 helpbutton("participantswithselectedusers", get_string("withselectedusers"));
311 echo '<input type="submit" value="' . get_string('ok') . '" />'."\n";
312 echo '</div>'."\n";
313 echo '</form>'."\n";
314 echo '</div>'."\n";
318 print_footer();