Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / attendance / viewweek.php
blobed51b553655ee6e9433b78931093ed8d33617e37
1 <?php // $Id$
2 /// This page prints all instances of attendance in a given week
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 (! $attendance = get_record("attendance", "id", $id)) {
15 error("Course module is incorrect");
17 if (! $course = get_record("course", "id", $attendance->course)) {
18 error("Course is misconfigured");
20 if (! $cm = get_coursemodule_from_instance("attendance", $attendance->id, $course->id)) {
21 error("Course Module ID was incorrect");
23 if ($scope == "section") {
24 if (! $attendances = get_attendance_for_section($attendance->id, $course->id)) {
25 error("Course module is incorrect");
27 } else {
28 if (! $attendances = get_attendance_for_week($attendance->id, $course->id)) {
29 error("Course module is incorrect");
34 require_login($course->id);
36 add_to_log($course->id, "attendance", "viewweek", "viewweek.php?scope=".$scope."&amp;id=$course->id");
39 if ($attendances) {
40 if ( !(isteacher($course->id) || isstudent($course->id)) ) {
41 attendance_print_header();
42 notice(get_string("noviews", "attendance"));
43 print_footer($course); exit;
49 /// create an array of all the attendance objects for the week
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)) {
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
83 if ($download == "xls") {
84 require_once("$CFG->libdir/excel/Worksheet.php");
85 require_once("$CFG->libdir/excel/Workbook.php");
86 // HTTP headers
87 attendance_HeaderingExcel($course->shortname."_Attendance_Week.xls");
88 // Creating a workbook
89 $workbook = new Workbook("-");
90 // Creating the first worksheet
91 $myxls =& $workbook->add_worksheet('Weekly Attendance');
93 // print the date headings at the top of the table
94 // for each day of attendance
95 $myxls->write_string(3,0,get_string("lastname"));
96 $myxls->write_string(3,1,get_string("firstname"));
97 $myxls->write_string(3,2,get_string("idnumber"));
98 $pos=3;
99 if ($dlsub== "all") {
100 for($k=0;$k<$numatt;$k++) {
101 // put notes for the date in the date heading
102 $myxls->write_string(1,$pos,userdate($atts[$k]->attendance->day,"%m/%0d"));
103 $myxls->set_column($pos,$pos,5);
104 $myxls->write_string(2,$pos,$atts[$k]->attendance->notes);
105 for ($i=1;$i<=$atts[$k]->attendance->hours;$i++) {
106 $myxls->write_number(3,$pos,$i);
107 $myxls->set_column($pos,$pos,1);
108 $pos++;
111 } // if dlsub==all
112 $myxls->write_string(3,$pos,get_string("total"));
113 $myxls->set_column($pos,$pos,5);
115 /// generate the attendance rolls for the body of the spreadsheet
116 if (isstudent($course->id)) {
117 $students[0] = $USER;
118 } else { // must be a teacher
119 $students = attendance_get_course_students($attendance->course, "u.lastname ASC");
121 $i=0;
122 $A = get_string("absentshort","attendance");
123 $T = get_string("tardyshort","attendance");
124 $P = get_string("presentshort","attendance");
125 $row=4;
126 if ($students) foreach ($students as $student) {
127 $myxls->write_string($row,0,$student->lastname);
128 $myxls->write_string($row,1,$student->firstname);
129 $studentid=(($student->idnumber != "") ? $student->idnumber : " ");
130 $myxls->write_string($row,2,$studentid);
131 $pos=3;
132 if ($dlsub== "all") {
133 for($k=0;$k<$numatt;$k++) { // for each day of attendance for the student
134 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
135 // set the attendance defaults for each student
136 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status=$T;}
137 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status=$A;}
138 else {$status=$P;}
139 $myxls->write_string($row,$pos,$status);
140 $pos++;
141 } /// for loop
144 $abs=$tar=0;
145 for($k=0;$k<$numatt;$k++) { // for eacj day of attendance for the student
146 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
147 // set the attendance defaults for each student
148 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {;$tar++;}
149 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {;$abs++;}
150 } /// for loop
151 } // outer for for each day of attendance
152 $tot=attendance_tally_overall_absences_decimal($abs,$tar);
153 $myxls->write_number($row,$pos,$tot);
154 $row++;
156 $workbook->close();
158 exit;
161 if ($download == "txt") {
163 header("Content-Type: application/download\n");
164 header("Content-Disposition: attachment; filename=\"".$course->shortname."_Attendance_Week.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)) {
182 $students[0] = get_user_info_from_db("id", $USER->id);
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
232 if (isstudent($course->id)) {
233 $onepage=true;
234 $multipage=false;
235 } else if (!(isset($onepage))){
236 $onepage=false;
237 $multipage=true;
238 } else if ($onepage) {
239 $multipage=false;
240 } else { // if onepage is set to false
241 $multilpage=true;
244 // adjust the width for the report for students
246 if (($onetable) || ($CFG->attendance_hours_in_full_report == 0)) {
247 $hoursinreport = 100+$numhours;
248 } else if (isstudent($course->id)) {
249 $hoursinreport = $CFG->attendance_hours_in_full_report + 15;
250 } else {
251 $hoursinreport = $CFG->attendance_hours_in_full_report;
253 while (($multipage || $onepage) && (!$endonepage)) {
254 // this makes for a one iteration loop for multipage
255 $multipage = false;
258 if ($numhours>=$hoursinreport) {
259 if (!isset($pagereport)) {
260 // $pagereport is used to determine whether the report needs to be paged at all
261 $pagereport=true;
262 $endatt=0;
263 $page=1;
265 // find the last hour to have on this page of the report
266 // go to the next (or first) page
267 // $endatt++;
268 // $startatt=$endatt;
269 $curpage=1;
270 $endatt=0;
271 for($curpage=1;true;$curpage++) { // the for loop is broken from the inside
272 $pagehours=$atts[$endatt]->attendance->hours;
273 $startatt=$endatt;
274 while(($pagehours<$hoursinreport)) {
275 if ($endatt>=$numatt) { break 2; } // end the page number calculations and trigger the end of a multi-page report!
276 $endatt++;
277 $pagehours=$pagehours+$atts[$endatt]->attendance->hours;
279 // if this is the page we're on, save the info
280 if ($curpage == $page) {$endatt_target = $endatt; $startatt_target = $startatt; }
281 } // hopefully at this point, startatt and endatt are set correctly for the current page
282 if ($curpage == $page) {$endatt_target = $endatt; $startatt_target = $startatt; } else {
283 $endatt=$endatt_target; $startatt=$startatt_target; }
284 $maxpages = $curpage;
285 } else {$pagereport=false;}
287 $minatt=($pagereport ? $startatt : 0);
288 $maxatt=($pagereport ? $endatt : $numatt);
290 if ((!$pagereport) || ($page == $maxpages)) {$endonepage = true;} // end a one page display
294 // ALL PRELIMINARY STUFF DONE - MAKE THE MEAT OF THE PAGE
297 attendance_print_header();
300 // print other links at top of page
301 $strviewsection = get_string("viewsection", "attendance");
302 $strviewweek = get_string("viewweek", "attendance");
303 $strviewall = get_string("viewall", "attendance");
304 $strviewone = get_string("viewone", "attendance");
305 $strviewtable = get_string("viewtable", "attendance");
306 $strviewmulti = get_string("viewmulti", "attendance");
309 echo "<p align=\"right\"><a href=\"viewall.php?id=".$course->id."\">";
310 echo "$strviewall</a><br />";
311 if ($onepage) { // one page for all tables
312 echo "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."\">";
313 echo "$strviewmulti</a><br />";
314 echo "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;onetable=1\">";
315 echo "$strviewtable</a><br />";
316 } else if ($onetable) { // one table for all
317 echo "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."\">";
318 echo "$strviewmulti</a><br />";
319 echo "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;onepage=1\">";
320 echo "$strviewone</a><br />";
321 } else { // multiple pages
322 echo "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;onepage=1\">";
323 echo "$strviewone</a><br />";
324 echo "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;onetable=1\">";
325 echo "$strviewtable</a><br />";
327 if ($scope=="week") { // week view for scope
328 echo "<a href=\"viewweek.php?scope=section&amp;id=".$id."\">";
329 echo "$strviewsection</a></p>";
330 } else { // section view for scope
331 echo "<a href=\"viewweek.php?scope=week&amp;id=".$id."\">";
332 echo "$strviewweek</a></p>";
336 if (!$onepage) {
338 attendance_print_pagenav();
343 // build the table for attendance roll
344 // this is the wrapper table
345 echo "<table align=\"center\" width=\"80\" class=\"generalbox\"".
346 "border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>".
347 "<td bgcolor=\"#ffffff\" class=\"generalboxcontent\">";
348 // this is the main table
349 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ".
350 "cellpadding=\"5\" cellspacing=\"1\" class=\"generaltable\">";
351 if (isteacher($course->id)) {
352 echo "<tr><th valign=\"top\" align=\"right\" colspan=\"3\" nowrap class=\"generaltableheader\">".
353 "&nbsp;</th>\n";
355 // $minpage=0;$maxpage=$numatt;
356 // print the date headings at the top of the table
357 // for each day of attendance
358 for($k=$minatt;$k<$maxatt;$k++) {
359 // put notes for the date in the date heading
360 $notes = ($atts[$k]->attendance->notes != "") ? ":<br />".$atts[$k]->attendance->notes : "";
361 $auto = ($atts[$k]->attendance->autoattend == 1) ? "(".get_string("auto","attendance").")" : "";
362 echo "<th valign=\"top\" align=\"left\" colspan=\"" .$atts[$k]->attendance->hours. "\" nowrap class=\"generaltableheader\">".
363 "<a href=\"view.php?id=".$atts[$k]->attendance->cmid."\">".userdate($atts[$k]->attendance->day,"%m/%0d")."</a>".$auto.
364 $notes."</th>\n";
366 // if we're at the end of the report
367 if ($maxatt==$numatt || !$pagereport) {
368 echo "<th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">&nbsp;</th>\n";
370 echo "</tr>\n";
371 // print the second level headings with name and possibly hour numbers
372 if (isteacher($course->id)) {
373 echo "<tr><th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">Last Name</th>\n";
374 echo "<th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">First Name</th>\n";
375 echo "<th valign=\"top\" align=\"left\" nowrap class=\"generaltableheader\">ID</th>\n";
377 // generate the headers for the attendance hours
378 for($k=$minatt;$k<$maxatt;$k++) {
379 if ($atts[$k]->attendance->hours > 1) {
380 for($i=1;$i<=$atts[$k]->attendance->hours;$i++) {
381 echo "<th valign=\"top\" align=\"center\" nowrap class=\"generaltableheader\">".$i."</th>\n";
383 } else { echo "<th valign=\"top\" align=\"center\" nowrap class=\"generaltableheader\">&nbsp;</th>\n"; }
385 // if we're at the end of the report
386 if ($maxatt==$numatt || !$pagereport) {
387 echo "<th valign=\"top\" align=\"center\" nowrap class=\"generaltableheader\">total</th>";
389 echo "</tr>\n";
391 // get the list of students along with student ID field
392 // get back array of stdclass objects in sorted order, with members:
393 // id, username,firstname,lastname,maildisplay,mailformat,email,city,country,
394 // lastaccess,lastlogin,picture (picture is null, 0, or 1), idnumber
397 if (isstudent($course->id)) {
398 $students[0] = get_user_info_from_db("id", $USER->id);
399 } else { // must be a teacher
400 $students = attendance_get_course_students($attendance->course, "u.lastname ASC");
402 $i=0;
403 $A = get_string("absentshort","attendance");
404 $T = get_string("tardyshort","attendance");
405 $P = get_string("presentshort","attendance");
406 if ($students) foreach ($students as $student) {
407 if (isteacher($course->id)) {
408 echo "<tr><td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-top: 1px solid;\">".$student->lastname."</td>\n";
409 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-top: 1px solid;\">".$student->firstname."</td>\n";
410 $studentid=(($student->idnumber != "") ? $student->idnumber : "&nbsp");
411 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-top: 1px solid;\">".$studentid."</td>\n";
413 for($k=$minatt;$k<$maxatt;$k++) { // for eacj day of attendance for the student
414 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
415 // set the attendance defaults for each student
416 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status=$T;}
417 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status=$A;}
418 else {$status=$P;}
419 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-left: 1px dotted; border-top: 1px solid;\">".$status."</td>\n";
420 } /// for loop
422 if ($maxatt==$numatt || !$pagereport) {
423 // tally total attendances for the students
424 $abs=$tar=0;
425 for($k=0;$k<$numatt;$k++) { // for eacj day of attendance for the student
426 for($j=1;$j<=$atts[$k]->attendance->hours;$j++) {
427 // set the attendance defaults for each student
428 if ($atts[$k]->sroll[$student->id][$j]->status == 1) {;$tar++;}
429 elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {;$abs++;}
430 } /// for loop
431 } // outer for for each day of attendance
432 $tot=attendance_tally_overall_absences_fraction($abs,$tar);
433 echo "<td align=\"left\" nowrap class=\"generaltablecell\" style=\"border-left: 1px dotted; border-top: 1px solid;\">".$tot."</td></tr>\n";
435 } // foreach
436 /// doing the table manually now
437 /// print_table($table);
438 /// ending for the table
439 echo "</table></td></tr></table>\n";
441 if ($onepage) {$page++; echo "<br /> <br />\n"; }
442 } // while loop for multipage/one page printing
444 if (!$onepage) { attendance_print_pagenav(); }
447 echo "<center><table border=0 align=CENTER><tr>";
448 echo "<td>";
449 if (($numhours-4) > 255) {
450 echo "<form><input type=\"button\" value=\"".get_string("downloadexcelfull", "attendance").
451 "\" onclick=\"alert('Sorry, you have more than 251 days on this report. This will not fit into an Excel Spreadsheet. ".
452 " Please try downloading the report week by week instead.')\" /></form>";
453 } else {
454 $options["id"] = "$id";
455 $options["download"] = "xls";
456 $options["dlsub"] = "all";
457 print_single_button("viewweek.php", $options, get_string("downloadexcelfull", "attendance"));
459 echo "</td><td>";
460 $options["id"] = "$id";
461 $options["download"] = "xls";
462 $options["dlsub"] = "totals";
463 print_single_button("viewweek.php", $options, get_string("downloadexceltotals", "attendance"));
464 echo "</td><td>";
465 $options["download"] = "txt";
466 $options["dlsub"] = "all";
467 print_single_button("viewweek.php", $options, get_string("downloadtextfull", "attendance"));
468 echo "</td><td>";
469 $options["dlsub"] = "totals";
470 print_single_button("viewweek.php", $options, get_string("downloadtexttotals", "attendance"));
471 echo "</td></table></center>";
476 } else { error("There are no attendance rolls in this course.");} // for no attendance rolls
477 /// Finish the page
478 print_footer($course);
481 function attendance_print_pagenav() {
482 global $pagereport, $minatt, $maxatt, $course, $page, $numatt, $maxpages, $attendance,$scope,$id;
483 if ($pagereport) {
484 $of = get_string('of','attendance');
485 $pg = get_string('page');
487 echo "<center><table align=\"center\" width=\"80\" class=\"generalbox\"".
488 "border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>".
489 "<td bgcolor=\"#ffffff\" class=\"generalboxcontent\">";
490 // this is the main table
491 echo "<table width=\"100%\" border=\"0\" valign=\"top\" align=\"center\" ".
492 "cellpadding=\"5\" cellspacing=\"1\" class=\"generaltable\">";
493 echo "<tr>";
494 if ($minatt!=0) {
495 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">".
496 "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;pagereport=1&amp;page=".($page-1)."\">&lt;</a>&nbsp;\n";
497 "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;pagereport=1&amp;page=1\">&lt;&lt;</a></th>\n";
498 } else {
499 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">&lt;&lt;&nbsp;&lt;</th>\n";
501 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">".
502 "$pg $page $of $maxpages</th>\n";
503 if ($maxatt!=$numatt) {
504 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">".
505 "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;pagereport=1&amp;page=". ($page+1)."\">&gt;</a>&nbsp;".
506 "<a href=\"viewweek.php?scope=".$scope."&amp;id=".$id ."&amp;pagereport=1&amp;page=$maxpages\">&gt;&gt;</a></th>";
507 } else {
508 echo "<th valign=\"top\" align=\"right\" nowrap class=\"generaltableheader\">&gt;&nbsp;&gt;&gt;</th>\n";
510 echo "</tr></table></td></tr></table></center>\n";
514 function attendance_print_header() {
515 global $course, $cm;
518 /// Print the page header
520 $strattendances = get_string("modulenameplural", "attendance");
521 $strattendance = get_string("modulename", "attendance");
522 $strweekattendance = get_string("weekmodulename", "attendance");
523 print_header_simple("$strallattendance", "",
524 "<a href=index.php?id=$course->id>$strattendances</a> -> $strweekattendance",
525 "", "", true, "&nbsp;",
526 navmenu($course, $cm));
529 function attendance_HeaderingExcel($filename) {
530 header("Content-type: application/vnd.ms-excel");
531 header("Content-Disposition: attachment; filename=$filename" );
532 header("Expires: 0");
533 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
534 header("Pragma: public");