Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / attendance / viewall.php
blob385417b0c705f9b869910382b49982b2b90b5785
1 <?php // $Id$
2 /// This page prints all instances of attendance in a given course
4 require_once("../../config.php");
5 require_once("lib.php" );
6 /// @include_once("$CFG->dirroot/mod/attendance/lib.php");
7 /// error_reporting(E_ALL);
9 optional_variable($id); // Course Module ID, or
10 optional_variable($a); // attendance ID
12 /// populate the appropriate objects
13 if ($id) {
14 if (! $course = get_record("course", "id", $id)) {
15 error("Course is misconfigured");
17 if (! $attendances = get_records("attendance", "course", $id, "day ASC ")) {
18 error("Course module is incorrect");
20 } else {
21 if (! $attendance = get_record("attendance", "id", $a)) {
22 error("Course module is incorrect");
24 if (! $course = get_record("course", "id", $attendance->course)) {
25 error("Course is misconfigured");
27 if (! $cm = get_coursemodule_from_instance("attendance", $attendance->id, $course->id)) {
28 error("Course Module ID was incorrect");
30 if (! $attendances = get_records("attendance", "course", $cm->course)) {
31 error("Course module is incorrect");
35 require_login($course->id);
37 add_to_log($course->id, "attendance", "viewall", "viewall.php?id=$course->id");
40 /// Print the main part of the page
41 if ($attendances) {
42 if ( isstudent($course->id) && !isteacher($course->id)) {
43 attendance_print_header();
44 notice(get_string("noviews", "attendance"));
45 print_footer($course); exit;
49 /// create an array of all the attendance objects for the entire course
50 $numatt=0;
51 $numhours=0;
52 if ($attendances) foreach ($attendances as $attendance){
53 // store the raw attendance object
54 $cm = get_coursemodule_from_instance("attendance", $attendance->id, $course->id);
55 $attendance->cmid = $cm->id;
56 $atts[$numatt]->attendance=$attendance;
57 // tally the hours for possible paging of the report
58 $numhours=$numhours+$attendance->hours;
59 // get the list of attendance records for all hours of the given day and
60 // put it in the array for use in the attendance table
61 if (isstudent($course->id) && !isteacher($course->id)) {
62 $rolls = get_records("attendance_roll", "dayid", $form->id, "userid", $USER->id);
63 } else { // must be a teacher
64 $rolls = get_records("attendance_roll", "dayid", $attendance->id);
66 if ($rolls) {
67 foreach ($rolls as $roll) {
68 $atts[$numatt]->sroll[$roll->userid][$roll->hour]->status=$roll->status;
69 $atts[$numatt]->sroll[$roll->userid][$roll->hour]->notes=$roll->notes;
72 $numatt++;
78 // IF REQUESTING A DATA FILE DOWNLOAD, GENERATE IT INSTEAD OF THE HTML PAGE, SEND IT, AND EXIT
82 if ($download == "xls") {
83 require_once("$CFG->libdir/excel/Worksheet.php");
84 require_once("$CFG->libdir/excel/Workbook.php");
85 // HTTP headers
86 attendance_HeaderingExcel($course->shortname."_Attendance.xls");
87 // Creating a workbook
88 $workbook = new Workbook("-");
89 // Creating the first worksheet
90 $myxls =& $workbook->add_worksheet('Attendance');
92 // print the date headings at the top of the table
93 // for each day of attendance
94 $myxls->write_string(3,0,get_string("lastname"));
95 $myxls->write_string(3,1,get_string("firstname"));
96 $myxls->write_string(3,2,get_string("idnumber"));
97 $pos=3;
98 if ($dlsub== "all") {
99 for($k=0;$k<$numatt;$k++) {
100 // put notes for the date in the date heading
101 $myxls->write_string(1,$pos,userdate($atts[$k]->attendance->day,"%m/%0d"));
102 $myxls->set_column($pos,$pos,5);
103 $myxls->write_string(2,$pos,$atts[$k]->attendance->notes);
104 for ($i=1;$i<=$atts[$k]->attendance->hours;$i++) {
105 $myxls->write_number(3,$pos,$i);
106 $myxls->set_column($pos,$pos,1);
107 $pos++;
110 } // if dlsub==all
111 $myxls->write_string(3,$pos,get_string("total"));
112 $myxls->set_column($pos,$pos,5);
114 /// generate the attendance rolls for the body of the spreadsheet
115 if (isstudent($course->id) && !isteacher($course->id)) {
116 $students[0] = $USER;
117 } else { // must be a teacher
118 $students = attendance_get_course_students($attendance->course, "u.lastname ASC");
120 $i=0;
121 $A = get_string("absentshort","attendance");
122 $T = get_string("tardyshort","attendance");
123 $P = get_string("presentshort","attendance");
124 $row=4;
125 if ($students) foreach ($students as $student) {
126 $myxls->write_string($row,0,$student->lastname);
127 $myxls->write_string($row,1,$student->firstname);
128 $studentid=(($student->idnumber != "") ? $student->idnumber : " ");
129 $myxls->write_string($row,2,$studentid);
130 $pos=3;
131 if ($dlsub== "all") {
132 for($k=0;$k<$numatt;$k++) { // for each day of attendance for the student
133 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
134 // set the attendance defaults for each student
135 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status=$T;}
136 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status=$A;}
137 else {$status=$P;}
138 $myxls->write_string($row,$pos,$status);
139 $pos++;
140 } /// for loop
143 $abs=$tar=0;
144 for($k=0;$k<$numatt;$k++) { // for eacj day of attendance for the student
145 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
146 // set the attendance defaults for each student
147 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {;$tar++;}
148 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {;$abs++;}
149 } /// for loop
150 } // outer for for each day of attendance
151 $tot=attendance_tally_overall_absences_decimal($abs,$tar);
152 $myxls->write_number($row,$pos,$tot);
153 $row++;
155 $workbook->close();
157 exit;
161 if ($download == "txt") {
163 header("Content-Type: application/download\n");
164 header("Content-Disposition: attachment; filename=\"".$course->shortname."_Attendance.txt\"");
166 /// Print names of all the fields
168 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber");
170 if ($dlsub== "all") {
171 for($k=0;$k<$numatt;$k++) {
172 // put notes for the date in the date heading
173 echo "\t" . userdate($atts[$k]->attendance->day,"%m/%0d");
174 echo (($atts[$k]->attendance->notes != "")?" ".$atts[$k]->attendance->notes:"");
175 for ($i=2;$i<=$atts[$k]->attendance->hours;$i++) { echo "\t$i"; }
177 } // if dlsub==all
178 echo "\t". get_string("total") . "\n";
180 /// generate the attendance rolls for the body of the spreadsheet
181 if (isstudent($course->id) && !isteacher($course->id)) {
182 $students[0] = $USER;
183 } else { // must be a teacher
184 $students = attendance_get_course_students($attendance->course, "u.lastname ASC");
186 $i=0;
187 $A = get_string("absentshort","attendance");
188 $T = get_string("tardyshort","attendance");
189 $P = get_string("presentshort","attendance");
190 $row=3;
191 if ($students) foreach ($students as $student) {
192 echo $student->lastname;
193 echo "\t".$student->firstname;
194 $studentid=(($student->idnumber != "") ? $student->idnumber : " ");
195 echo "\t". $studentid;
196 if ($dlsub== "all") {
197 for($k=0;$k<$numatt;$k++) { // for each day of attendance for the student
198 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
199 // set the attendance defaults for each student
200 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status=$T;}
201 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status=$A;}
202 else {$status=$P;}
203 echo "\t".$status;
204 } /// for loop
207 $abs=$tar=0;
208 for($k=0;$k<$numatt;$k++) { // for eacj day of attendance for the student
209 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
210 // set the attendance defaults for each student
211 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {;$tar++;}
212 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {;$abs++;}
213 } /// for loop
214 } // outer for for each day of attendance
215 $tot=attendance_tally_overall_absences_decimal($abs,$tar);
216 echo "\t".$tot."\n";
217 $row++;
219 exit;
226 // FIGURE OUT THE PAGING LAYOUT FOR THE DATA BASED ON STATUS, PAGE NUMBER REQUESTED, ETC
230 // A LOOP FOR CREATING SINGLE-USER VERSION OF THE REPORT OR A ONE-PAGE REPORT
231 if (isstudent($course->id) && !isteacher($course->id)) {
232 $onepage=true;
233 $multipage=false;
234 } else if (!(isset($onepage))){
235 $onepage=false;
236 $multipage=true;
237 } else if ($onepage) {
238 $multipage=false;
239 } else { // if onepage is set to false
240 $multilpage=true;
243 // adjust the width for the report for students
245 if (($onetable) || ($CFG->attendance_hours_in_full_report == 0)) {
246 $hoursinreport = 100+$numhours;
247 } else if (isstudent($course->id) && !isteacher($course->id)) {
248 $hoursinreport = $CFG->attendance_hours_in_full_report + 15;
249 } else {
250 $hoursinreport = $CFG->attendance_hours_in_full_report;
252 while (($multipage || $onepage) && (!$endonepage)) {
253 // this makes for a one iteration loop for multipage
254 if ($multipage) {$onepage = false; $multipage = false; $endonepage=false;}
257 if ($numhours>=$hoursinreport) {
258 if (!isset($pagereport)) {
259 // $pagereport is used to determine whether the report needs to be paged at all
260 $pagereport=true;
261 $endatt=0;
262 $page=1;
264 // find the last hour to have on this page of the report
265 // go to the next (or first) page
266 // $endatt++;
267 // $startatt=$endatt;
268 $curpage=1;
269 $endatt=0;
270 for($curpage=1;true;$curpage++) { // the for loop is broken from the inside
271 $pagehours=$atts[$endatt]->attendance->hours;
272 $startatt=$endatt;
273 while(($pagehours<=$hoursinreport)) {
274 if ($endatt>=$numatt) { break 2; } // end the page number calculations and trigger the end of a multi-page report!
275 $endatt++;
276 $pagehours=$pagehours+$atts[$endatt]->attendance->hours;
278 // if this is the page we're on, save the info
279 if ($curpage == $page) {$endatt_target = $endatt; $startatt_target = $startatt; }
280 } // hopefully at this point, startatt and endatt are set correctly for the current page
281 if ($curpage == $page) {$endatt_target = $endatt; $startatt_target = $startatt; } else {
282 $endatt=$endatt_target; $startatt=$startatt_target; }
283 $maxpages = $curpage;
284 } else {$pagereport=false;}
286 $minatt=($pagereport ? $startatt : 0);
287 $maxatt=($pagereport ? $endatt : $numatt);
289 if ((!$pagereport) || ($page == $maxpages)) {$endonepage = true;} // end a one page display
298 // ALL PRELIMINARY STUFF DONE - MAKE THE MEAT OF THE PAGE
301 attendance_print_header();
303 // print other links at top of page
304 $strviewone = get_string("viewone", "attendance");
305 $strviewtable = get_string("viewtable", "attendance");
306 $strviewmulti = get_string("viewmulti", "attendance");
307 $strviewweek = get_string("viewweek", "attendance");
308 if ($onepage) { // one page for all tables
309 echo "<p align=\"right\"><a href=\"viewall.php?id=".$course->id."\">";
310 echo "$strviewmulti</a><br />";
311 echo "<a href=\"viewall.php?id=".$course->id."&amp;onetable=1\">";
312 echo "$strviewtable</a><br />";
313 echo "<a href=\"viewweek.php?scope=week&amp;id=".$atts[$minatt]->attendance->id."\">";
314 echo "$strviewweek</a></p>";
315 } else if ($onetable) { // one table for all
316 echo "<p align=\"right\"><a href=\"viewall.php?id=".$course->id."\">";
317 echo "$strviewmulti</a><br />";
318 echo "<a href=\"viewall.php?id=".$course->id."&amp;onepage=1\">";
319 echo "$strviewone</a><br />";
320 echo "<a href=\"viewweek.php?scope=week&amp;id=".$atts[$minatt]->attendance->id."\">";
321 echo "$strviewweek</a></p>";
322 } else { // multiple pages
323 echo "<p align=\"right\"><a href=\"viewall.php?id=".$course->id."&amp;onepage=1\">";
324 echo "$strviewone</a><br />";
325 echo "<a href=\"viewall.php?id=".$course->id."&amp;onetable=1\">";
326 echo "$strviewtable</a><br />";
327 echo "<a href=\"viewweek.php?scope=week&amp;id=".$atts[$minatt]->attendance->id."\">";
328 echo "$strviewweek</a></p>";
332 if (!$onepage) {
334 attendance_print_pagenav();
336 // build the table for attendance roll
337 // this is the wrapper table
338 echo "<table align=\"center\" width=\"80\" class=\"generalbox\"".
339 "border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>".
340 "<td bgcolor=\"#ffffff\" class=\"generalboxcontent\">";
341 // this is the main table
342 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ".
343 "cellpadding=\"5\" cellspacing=\"1\" class=\"generaltable\">";
344 if (isteacher($course->id)) {
345 echo "<tr><th valign=\"top\" align=\"right\" colspan=\"3\" nowrap class=\"generaltableheader\">".
346 "&nbsp;</th>\n";
348 // $minpage=0;$maxpage=$numatt;
349 // print the date headings at the top of the table
350 // for each day of attendance
351 for($k=$minatt;$k<$maxatt;$k++) {
352 // put notes for the date in the date heading
353 $notes = ($atts[$k]->attendance->notes != "") ? ":<br />".$atts[$k]->attendance->notes : "";
354 $auto = ($atts[$k]->attendance->autoattend == 1) ? "(".get_string("auto","attendance").")" : "";
355 echo "<th valign=\"top\" align=\"left\" colspan=\"" .$atts[$k]->attendance->hours. "\" nowrap class=\"generaltableheader\">".
356 "<a href=\"view.php?id=".$atts[$k]->attendance->cmid."\">".userdate($atts[$k]->attendance->day,"%m/%0d")."</a>".$auto.
357 $notes."</th>\n";
359 // if we're at the end of the report
360 if ($maxatt==$numatt || !$pagereport) {
361 echo "<th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">&nbsp;</th>\n";
363 echo "</tr>\n";
364 // print the second level headings with name and possibly hour numbers
365 if (isteacher($course->id)) {
366 echo "<tr><th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">Last Name</th>\n";
367 echo "<th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">First Name</th>\n";
368 echo "<th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">ID</th>\n";
370 // generate the headers for the attendance hours
371 for($k=$minatt;$k<$maxatt;$k++) {
372 if ($atts[$k]->attendance->hours > 1) {
373 for($i=1;$i<=$atts[$k]->attendance->hours;$i++) {
374 echo "<th valign=\"top\" align=\"center\" nowrap class=\"generaltableheader\">".$i."</th>\n";
376 } else { echo "<th valign=\"top\" align=\"center\" nowrap class=\"generaltableheader\">&nbsp;</th>\n"; }
378 // if we're at the end of the report
379 if ($maxatt==$numatt || !$pagereport) {
380 echo "<th valign=\"top\" align=\"center\" nowrap class=\"generaltableheader\">total</th>";
382 echo "</tr>\n";
384 // get the list of students along with student ID field
385 // get back array of stdclass objects in sorted order, with members:
386 // id, username,firstname,lastname,maildisplay,mailformat,email,city,country,
387 // lastaccess,lastlogin,picture (picture is null, 0, or 1), idnumber
390 if (isstudent($course->id) && !isteacher($course->id)) {
391 $students[0] = $USER;
392 } else { // must be a teacher
393 $students = attendance_get_course_students($attendance->course, "u.lastname ASC");
395 $i=0;
396 $A = get_string("absentshort","attendance");
397 $T = get_string("tardyshort","attendance");
398 $P = get_string("presentshort","attendance");
399 if ($students) foreach ($students as $student) {
400 if (isteacher($course->id)) {
401 echo "<tr><td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-top: 1px solid;\">".$student->lastname."</td>\n";
402 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-top: 1px solid;\">".$student->firstname."</td>\n";
403 $studentid=(($student->idnumber != "") ? $student->idnumber : "&nbsp");
404 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-top: 1px solid;\">".$studentid."</td>\n";
406 for($k=$minatt;$k<$maxatt;$k++) { // for eacj day of attendance for the student
407 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
408 // set the attendance defaults for each student
409 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status=$T;}
410 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status=$A;}
411 else {$status=$P;}
412 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-left: 1px dotted; border-top: 1px solid;\">".$status."</td>\n";
413 } /// for loop
415 if ($maxatt==$numatt || !$pagereport) {
416 // tally total attendances for the students
417 $abs=$tar=0;
418 for($k=0;$k<$numatt;$k++) { // for eacj day of attendance for the student
419 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
420 // set the attendance defaults for each student
421 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {;$tar++;}
422 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {;$abs++;}
423 } /// for loop
424 } // outer for for each day of attendance
425 $tot=attendance_tally_overall_absences_fraction($abs,$tar);
426 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-left: 1px dotted; border-top: 1px solid;\">".$tot."</td></tr>\n";
428 } // foreach
429 /// doing the table manually now
430 /// print_table($table);
431 /// ending for the table
432 echo "</table></td></tr></table>\n";
434 if ($onepage) {$page++; echo "<br /> <br />\n"; }
435 } // while loop for multipage/one page printing
437 if (!$onepage) { attendance_print_pagenav(); }
440 echo "<center><table border=0 align=CENTER><tr>";
441 echo "<td>";
442 if (($numhours-4) > 255) {
443 echo "<form><input type=\"button\" value=\"".get_string("downloadexcelfull", "attendance").
444 "\" onclick=\"alert('Sorry, you have more than 251 days on this report. This will not fit into an Excel Spreadsheet. ".
445 " Please try downloading the report week by week instead.')\" /></form>";
446 } else {
447 $options["id"] = "$course->id";
448 $options["download"] = "xls";
449 $options["dlsub"] = "all";
450 print_single_button("viewall.php", $options, get_string("downloadexcelfull", "attendance"));
452 echo "</td><td>";
453 $options["id"] = "$course->id";
454 $options["download"] = "xls";
455 $options["dlsub"] = "totals";
456 print_single_button("viewall.php", $options, get_string("downloadexceltotals", "attendance"));
457 echo "</td><td>";
458 $options["download"] = "txt";
459 $options["dlsub"] = "all";
460 print_single_button("viewall.php", $options, get_string("downloadtextfull", "attendance"));
461 echo "</td><td>";
462 $options["dlsub"] = "totals";
463 print_single_button("viewall.php", $options, get_string("downloadtexttotals", "attendance"));
464 echo "</td></table></center>";
467 } else { error("There are no attendance rolls in this course.");} // for no attendance rolls
468 /// Finish the page
471 print_footer($course);
473 function attendance_print_header() {
474 global $course, $cm;
476 /// Print the page header
478 $strattendances = get_string("modulenameplural", "attendance");
479 $strattendance = get_string("modulename", "attendance");
480 $strallattendance = get_string("allmodulename", "attendance");
481 print_header_simple("$strallattendance", "",
482 "<a href=index.php?id=$course->id>$strattendances</a> -> $strallattendance",
483 "", "", true, "&nbsp;",
484 navmenu($course, $cm));
487 function attendance_print_pagenav() {
488 global $pagereport, $minatt, $maxatt, $course, $page, $numatt, $maxpages;
489 if ($pagereport) {
490 $of = get_string('of','attendance');
491 $pg = get_string('page');
493 echo "<center><table align=\"center\" width=\"80\" class=\"generalbox\"".
494 "border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>".
495 "<td bgcolor=\"#ffffff\" class=\"generalboxcontent\">";
496 // this is the main table
497 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ".
498 "cellpadding=\"5\" cellspacing=\"1\" class=\"generaltable\">";
499 echo "<tr>";
500 if ($minatt!=0) {
501 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">".
502 "<a href=\"viewall.php?id=".$course->id ."&amp;pagereport=1&amp;page=1\">&lt;&lt;</a>&nbsp;\n".
503 "<a href=\"viewall.php?id=".$course->id ."&amp;pagereport=1&amp;page=".($page-1)."\">&lt;</a></th>\n";
504 } else {
505 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">&lt;&lt;&nbsp;&lt;</th>\n";
507 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">".
508 "$pg $page $of $maxpages</th>\n";
509 if ($maxatt!=$numatt) {
510 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">".
511 "<a href=\"viewall.php?id=".$course->id ."&amp;pagereport=1&amp;page=". ($page+1)."\">&gt;</a>&nbsp;".
512 "<a href=\"viewall.php?id=".$course->id ."&amp;pagereport=1&amp;page=$maxpages\">&gt;&gt;</a></th>";
513 } else {
514 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">&gt;&nbsp;&gt;&gt;</th>\n";
516 echo "</tr></table></td></tr></table></center>\n";
520 function attendance_HeaderingExcel($filename) {
521 header("Content-type: application/vnd.ms-excel");
522 header("Content-Disposition: attachment; filename=$filename" );
523 header("Expires: 0");
524 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
525 header("Pragma: public");
530 http://www.alpine-usa.com/company_info/press_release/010804_ipad.html