Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / course / report / participation / index.php
blob9baf30c0fe9e8b31ee6697fdfedc233436b8e19b
1 <?php // $Id$
3 require_once('../../../config.php');
4 require_once($CFG->libdir.'/statslib.php');
6 define('DEFAULT_PAGE_SIZE', 20);
7 define('SHOW_ALL_PAGE_SIZE', 5000);
9 $id = required_param('id', PARAM_INT); // course id.
10 $moduleid = optional_param('moduleid', 0, PARAM_INT); // module id.
11 $oldmod = optional_param('oldmod', 0, PARAM_INT);
12 $roleid = optional_param('roleid',0,PARAM_INT); // which role to show
13 $instanceid = optional_param('instanceid', 0, PARAM_INT); // instance we're looking at.
14 $timefrom = optional_param('timefrom', 0, PARAM_INT); // how far back to look...
15 $action = optional_param('action', '', PARAM_ALPHA);
16 $page = optional_param('page', 0, PARAM_INT); // which page to show
17 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // how many per page
19 if ($action != 'view' && $action != 'post') {
20 $action = ''; // default to all (don't restrict)
23 // reset instance if changing module.
24 if (!empty($moduleid) && !empty($oldmod) && $moduleid != $oldmod) {
25 $instanceid = 0;
28 if (!$course = get_record('course','id',$id)) {
29 print_error('invalidcourse');
32 if ($roleid != 0 && !$role = get_record('role','id',$roleid)) {
33 print_error('invalidrole');
36 require_login($course->id);
37 $context = get_context_instance(CONTEXT_COURSE, $course->id);
39 if (!has_capability('moodle/site:viewreports', $context)) {
40 print_error('mustbeteacher', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
44 add_to_log($course->id, "course", "report participation", "report/participation/index.php?id=$course->id", $course->id);
46 $strparticipation = get_string('participationreport');
47 $strviews = get_string('views');
48 $strposts = get_string('posts');
49 $strview = get_string('view');
50 $strpost = get_string('post');
51 $strallactions = get_string('allactions');
52 $strreports = get_string('reports');
54 $strnav = "<a href=\"../../view.php?id=$course->id\">" . format_string($course->shortname) . "</a> ->
55 <a href=\"../../report.php?id=$course->id\">$strreports</a> -> ". $strparticipation;
57 print_header("$course->shortname: $strparticipation", $course->fullname,
58 "<a href=\"../../view.php?id=$course->id\">$course->shortname</a> ->
59 <a href=\"../../report.php?id=$course->id\">$strreports</a> ->
60 $strparticipation");
62 // print_header($course->fullname.' '.$strparticipation,$strparticipation,$strnav);
64 $allowedmodules = array('assignment','book','chat','choice','exercise','forum','glossary','hotpot',
65 'journal','lesson','questionnaire','quiz','resource','scorm',
66 'survey','wiki','workshop'); // some don't make sense here - eg 'label'
68 if (!$modules = get_records_sql('SELECT DISTINCT module,name FROM '.$CFG->prefix.'course_modules cm JOIN '.
69 $CFG->prefix.'modules m ON cm.module = m.id WHERE course = '.$course->id)) {
70 print_error('noparticipatorycms', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
74 $modoptions = array();
75 foreach ($modules as $m) {
76 if (in_array($m->name,$allowedmodules)) {
77 $modoptions[$m->module] = get_string('modulename',$m->name);
81 $timeoptions = array();
82 // get minimum log time for this course
83 $minlog = get_field_sql('SELECT min(time) FROM '.$CFG->prefix.'log WHERE course = '.$course->id);
85 $now = usergetmidnight(time());
87 // days
88 for ($i = 1; $i < 7; $i++) {
89 if (strtotime('-'.$i.' days',$now) >= $minlog) {
90 $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
93 // weeks
94 for ($i = 1; $i < 10; $i++) {
95 if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
96 $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
99 // months
100 for ($i = 2; $i < 12; $i++) {
101 if (strtotime('-'.$i.' months',$now) >= $minlog) {
102 $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
105 // try a year
106 if (strtotime('-1 year',$now) >= $minlog) {
107 $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
110 $useroptions = array();
111 if ($roles = get_roles_on_exact_context(get_context_instance(CONTEXT_COURSE,$course->id))) {
112 foreach ($roles as $r) {
113 $useroptions[$r->id] = $r->name;
116 $guestrole = get_guest_role();
117 if (empty($useroptions[$guestrole->id])) {
118 $useroptions[$guestrole->id] = $guestrole->name;
120 $actionoptions = array('' => $strallactions,
121 'view' => $strview,
122 'post' => $strpost,
126 // print first controls.
127 echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n".
128 '<input type="hidden" name="id" value="'.$course->id.'" />'."\n".
129 '<input type="hidden" name="oldmod" value="'.$moduleid.'" />'."\n".
130 '<input type="hidden" name="instanceid" value="'.$instanceid.'" />'."\n";
131 echo '<label for="menumoduleid">'.get_string('activitymodule').'</label>'."\n";
132 choose_from_menu($modoptions,'moduleid',$moduleid);
133 echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
134 choose_from_menu($timeoptions,'timefrom',$timefrom);
135 echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
136 choose_from_menu($useroptions,'roleid',$roleid,'');
137 echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
138 choose_from_menu($actionoptions,'action',$action,'');
139 helpbutton('participationreport',get_string('participationreport'));
140 echo '<input type="submit" value="'.get_string('go').'" />'."\n</div></form>\n";
142 if (empty($moduleid)) {
143 notify(get_string('selectamodule'));
144 print_footer();
145 exit;
148 $baseurl = $CFG->wwwroot.'/course/report/participation/index.php?id='.$course->id.'&amp;roleid='
149 .$roleid.'&amp;instanceid='.$instanceid.'&amp;timefrom='.$timefrom.'&amp;moduleid='
150 .$moduleid.'&amp;action='.$action.'&amp;perpage='.$perpage;
153 // from here assume we have at least the module we're using.
154 $module = get_record('modules','id',$moduleid);
155 $modulename = get_string('modulename',$module->name);
157 require_once($CFG->dirroot.'/mod/'.$module->name.'/lib.php');
159 $viewfun = $module->name.'_get_view_actions';
160 $postfun = $module->name.'_get_post_actions';
162 if (!function_exists($viewfun) || !function_exists($postfun)) {
163 error(get_string('modulemissingcode','error',$module->name),$baseurl);
166 $viewnames = $viewfun();
167 $postnames = $postfun();
169 // get all instances of this module in the course.
170 if (!$instances = get_all_instances_in_course($module->name,$course)) {
171 error(get_string('noinstances','error',$modulename));
174 $instanceoptions = array();
176 foreach ($instances as $instance) {
177 $instanceoptions[$instance->id] = $instance->name;
180 if (count($instanceoptions) == 1) { // just display it if there's only one.
181 $instanceid = array_pop(array_keys($instanceoptions));
184 echo '<form action="'.$CFG->wwwroot.'/course/report/participation/index.php" method="post">'. "\n".
185 '<div id="participationreportselector">'."\n".
186 '<input type="hidden" name="id" value="'.$course->id.'" />'."\n".
187 '<input type="hidden" name="oldmod" value="'.$moduleid.'" />'."\n".
188 '<input type="hidden" name="timefrom" value="'.$timefrom.'" />'."\n".
189 '<input type="hidden" name="action" value="'.$action.'" />'."\n".
190 '<input type="hidden" name="roleid" value="'.$roleid.'" />'."\n".
191 '<input type="hidden" name="moduleid" value="'.$moduleid.'" />'."\n";
192 choose_from_menu($instanceoptions,'instanceid',$instanceid);
193 echo '<input type="submit" value="'.get_string('go').'" />'."\n".
194 '</div>'."\n".
195 "</form>\n";
197 if (!empty($instanceid) && !empty($roleid)) {
198 if (!$cm = get_coursemodule_from_instance($module->name,$instanceid,$course->id)) {
199 print_error('cmunknown');
202 require_once($CFG->dirroot.'/lib/tablelib.php');
203 $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
204 $table->course = $course;
206 $table->define_columns(array('fullname','count',''));
207 $table->define_headers(array(get_string('user'),((!empty($action)) ? get_string($action) : get_string('allactions')),get_string('select')));
208 $table->define_baseurl($baseurl);
210 $table->set_attribute('cellpadding','5');
211 $table->set_attribute('class', 'generaltable generalbox reporttable');
213 $table->sortable(true,'lastname','ASC');
215 $table->set_control_variables(array(
216 TABLE_VAR_SORT => 'ssort',
217 TABLE_VAR_HIDE => 'shide',
218 TABLE_VAR_SHOW => 'sshow',
219 TABLE_VAR_IFIRST => 'sifirst',
220 TABLE_VAR_ILAST => 'silast',
221 TABLE_VAR_PAGE => 'spage'
223 $table->setup();
226 $primary_roles = sql_primary_role_subselect(); // In dmllib.php
227 $sql = 'SELECT DISTINCT prs.userid, u.firstname,u.lastname,u.idnumber,count(l.action) as count FROM ('.$primary_roles.') prs'
228 .' JOIN '.$CFG->prefix.'user u ON u.id = prs.userid LEFT JOIN '.$CFG->prefix.'log l ON prs.userid = l.userid '
229 .' AND prs.courseid = l.course AND l.time > '.$timefrom.' AND l.course = '.$course->id.' AND l.module = \''.$module->name.'\' '
230 .' AND l.cmid = '.$cm->id;
231 switch ($action) {
232 case 'view':
233 $sql .= ' AND action IN (\''.implode('\',\'',$viewnames).'\' )';
234 break;
235 case 'post':
236 $sql .= ' AND action IN (\''.implode('\',\'',$postnames).'\' )';
237 break;
238 default:
239 // some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
240 $sql .= ' AND action IN (\''.implode('\',\'',array_merge($viewnames,$postnames)).'\' )';
244 $sql .= ' WHERE prs.courseid = '.$course->id.' AND prs.primary_roleid = '.$roleid.' AND prs.contextlevel = '.CONTEXT_COURSE.' AND prs.courseid = '.$course->id;
246 if ($table->get_sql_where()) {
247 $sql .= ' AND '.$table->get_sql_where(); //initial bar
250 $sql .= ' GROUP BY prs.userid,u.firstname,u.lastname,u.idnumber,l.userid';
252 if ($table->get_sql_sort()) {
253 $sql .= ' ORDER BY '.$table->get_sql_sort();
256 $countsql = 'SELECT COUNT(DISTINCT(prs.userid)) FROM ('.$primary_roles.') prs '
257 .' JOIN '.$CFG->prefix.'user u ON u.id = prs.userid WHERE prs.courseid = '.$course->id
258 .' AND prs.primary_roleid = '.$roleid.' AND prs.contextlevel = '.CONTEXT_COURSE;
260 $totalcount = count_records_sql($countsql);
262 if ($table->get_sql_where()) {
263 $matchcount = count_records_sql($countsql.' AND '.$table->get_sql_where());
264 } else {
265 $matchcount = $totalcount;
268 echo '<div id="participationreport">' . "\n";
269 echo '<p class="modulename">'.$modulename . ' ' . $strviews.': '.implode(', ',$viewnames).'<br />'."\n"
270 . $modulename . ' ' . $strposts.': '.implode(', ',$postnames).'</p>'."\n";
272 $table->initialbars($totalcount > $perpage);
273 $table->pagesize($perpage, $matchcount);
275 if (!$users = get_records_sql($sql, $table->get_page_start(), $table->get_page_size())) {
276 $users = array(); // tablelib will handle saying 'Nothing to display' for us.
279 $data = array();
281 $a->count = $totalcount;
282 $a->items = $role->name;
284 if ($matchcount != $totalcount) {
285 $a->items .= ' ('.get_string('matched').' '.$matchcount.')';
288 echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
289 echo '
290 <script type="text/javascript">
291 //<![CDATA[
292 function checksubmit(form) {
293 var destination = form.formaction.options[form.formaction.selectedIndex].value;
294 if (destination == "" || !checkchecked(form)) {
295 form.formaction.selectedIndex = 0;
296 return false;
297 } else {
298 return true;
302 function checkchecked(form) {
303 var inputs = document.getElementsByTagName(\'INPUT\');
304 var checked = false;
305 inputs = filterByParent(inputs, function() {return form;});
306 for(var i = 0; i < inputs.length; ++i) {
307 if(inputs[i].type == \'checkbox\' && inputs[i].checked) {
308 checked = true;
311 return checked;
314 function checknos() {
315 void(d=document);
316 void(el=d.getElementsByTagName(\'INPUT\'));
317 for(i=0;i<el.length;i++) {
318 if (el[i].value == 0) {
319 void(el[i].checked=1)
324 //]]>
325 </script>
327 echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="studentsform" onsubmit="return checksubmit(this);">'."\n";
328 echo '<div>'."\n";
329 echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
330 echo '<input type="hidden" name="returnto" value="'. format_string($_SERVER['REQUEST_URI']) .'" />'."\n";
331 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />'."\n";
333 foreach ($users as $u) {
334 $data = array('<a href="'.$CFG->wwwroot.'/user/view.php?id='.$u->userid.'&amp;course='.$course->id.'">'.fullname($u,true).'</a>'."\n",
335 ((!empty($u->count)) ? get_string('yes').' ('.$u->count.') ' : get_string('no')),
336 '<input type="checkbox" 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.'&amp;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.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>'."\n";
350 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> '."\n";
351 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> '."\n";
352 if ($perpage >= $matchcount) {
353 echo '<input type="button" onclick="checknos()" value="'.get_string('selectnos').'" />'."\n";
355 $displaylist['messageselect.php'] = get_string('messageselectadd');
356 choose_from_menu ($displaylist, "formaction", "", get_string("withselectedusers"), "if(checksubmit(this.form))this.form.submit();", "");
357 helpbutton("participantswithselectedusers", get_string("withselectedusers"));
358 echo '<input type="submit" value="' . get_string('ok') . '" />'."\n";
359 echo '</div>'."\n";
360 echo '</form>'."\n";
361 echo '</div>'."\n";
365 print_footer();