From eab709a095b45c0fc39265322abf41db103a16a6 Mon Sep 17 00:00:00 2001 From: jungwirr Date: Thu, 25 Sep 2003 18:54:18 +0000 Subject: [PATCH] Added automatic attendance logging based on date and any activity by student on that day. NOTE - cron must be running for this to work properly Modified multi-page views to use arrows instead of words, added a first page and last page link as well. Cleaned up that nav menu a bit as well. --- lang/en/attendance.php | 5 ++- mod/attendance/db/mysql.php | 3 ++ mod/attendance/db/mysql.sql | 1 + mod/attendance/lib.php | 85 +++++++++++++++++++++++++++++++++++-- mod/attendance/version.php | 4 +- mod/attendance/viewall.php | 27 +++++++----- mod/attendance/viewweek.php | 22 +++++++--- 7 files changed, 123 insertions(+), 24 deletions(-) diff --git a/lang/en/attendance.php b/lang/en/attendance.php index f72d308f27..5051720201 100755 --- a/lang/en/attendance.php +++ b/lang/en/attendance.php @@ -27,7 +27,6 @@ $string['defaultdynamicsection'] = "Whether to move attendance rolls to the corr $string['defaulthoursinclass'] = "How many hours there should be in a default attendance roll"; $string['tardiesperabsence'] = "How many tardies should count as one absence"; $string['hoursinfullreport'] = "The maximum number of hours of attendance that should be displayed on a single page of a report"; -$string['previous'] = "Previous"; $string['pages'] = "Pages"; $string['of'] = "of"; $string['viewall'] = "View All Attendance Rolls"; @@ -57,5 +56,9 @@ $string['downloadexcelfull'] = "Download Full Excel Spreadsheet"; $string['downloadexceltotals'] = "Download Summary Excel Spreadsheet"; $string['downloadtextfull'] = "Download Full Text Report"; $string['downloadtexttotals'] = "Download Summary Text Report"; +$string['autoattend'] = "Automatically take attendance based on user activity logs"; +$string['autoattendmulti'] = "Automatically take attendance for all rolls based on user activity logs"; +$string['auto'] = "auto"; + ?> diff --git a/mod/attendance/db/mysql.php b/mod/attendance/db/mysql.php index 280f4c67ee..10187972ea 100644 --- a/mod/attendance/db/mysql.php +++ b/mod/attendance/db/mysql.php @@ -10,6 +10,9 @@ function attendance_upgrade($oldversion) { execute_sql("ALTER TABLE `{$CFG->prefix}attendance` ADD `edited` TINYINT( 1 ) DEFAULT '0' NOT NULL;"); execute_sql("UPDATE `{$CFG->prefix}attendance` set `edited` = 1;"); } + if ($oldversion < 2003092500) { + execute_sql("ALTER TABLE `{$CFG->prefix}attendance` ADD `autoattend` TINYINT( 1 ) DEFAULT '0' NOT NULL;"); + } return true; } diff --git a/mod/attendance/db/mysql.sql b/mod/attendance/db/mysql.sql index 4750674f66..c559b9256b 100755 --- a/mod/attendance/db/mysql.sql +++ b/mod/attendance/db/mysql.sql @@ -13,6 +13,7 @@ CREATE TABLE prefix_attendance ( timemodified int(10) unsigned NOT NULL default '0', dynsection tinyint(1) NOT NULL default '0', edited tinyint(1) NOT NULL default '0', + autoattend tinyint(1) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM; diff --git a/mod/attendance/lib.php b/mod/attendance/lib.php index 83f5a05515..603e7f42cb 100755 --- a/mod/attendance/lib.php +++ b/mod/attendance/lib.php @@ -6,7 +6,7 @@ function attendance_add_module(&$mod) { // global $mod; - require("../../course/lib.php"); + require_once("../../course/lib.php"); if (! $mod->instance = attendance_add_instance($mod)) { error("Could not add a new instance of $mod->modulename"); return 0; @@ -34,6 +34,7 @@ function attendance_add_instance($attendance) { global $mod; $attendance->timemodified = time(); $attendance->dynsection = !empty($attendance->dynsection) ? 1 : 0; + $attendance->autoattend = !empty($attendance->autoattend) ? 1 : 0; if (empty($attendance->day)) { $attendance->day = make_timestamp($attendance->theyear, $attendance->themonth, $attendance->theday); @@ -71,6 +72,7 @@ function attendance_update_instance($attendance) { // $attendance->oldid=$attendance->id; $attendance->id = $attendance->instance; $attendance->dynsection = !empty($attendance->dynsection) ? 1 : 0; + $attendance->autoattend = !empty($attendance->autoattend) ? 1 : 0; $attendance->day = make_timestamp($attendance->theyear, $attendance->themonth, $attendance->theday); @@ -257,11 +259,41 @@ function attendance_cron () { /// Function to be run periodically according to the moodle cron /// This function searches for things that need to be done, such /// as sending out mail, toggling flags etc ... - global $CFG; +// look for all attendance instances set to autoattend + $attendances = get_records("attendance", "autoattend", 1, "course ASC"); + $td = attendance_find_today(time()); + $tm = attendance_find_tomorrow(time()); + foreach($attendances as $attendance) { + if (($attendance->day >=$td ) && ($attendance->day < $tm)) { + if(!isset($courses[$attendance->course]->students)) { + $courses[$attendance->course]->students = + attendance_get_course_students($attendance->course, "u.lastname ASC"); + } + foreach ($courses[$attendance->course]->students as $student) { + // first, clear out the records that may be there already + delete_records("attendance_roll", + "dayid",$attendance->id, + "userid",$student->id); + $wc = "userid = " . $student->id . " AND course = " . $attendance->course . + " AND time >= " . $td . " AND time < " . $tm; + $count = get_record_select("log",$wc,"COUNT(*) as c"); + if ($count->c == "0") { // then the student hasn't done anything today, so mark him absent + $attrec->dayid = $attendance->id; + $attrec->userid = $student->id; + $attrec->status = 2; // status 2 is absent + // mark ALL hours as absent for first version + for ($i=1;$i<=$attendance->hours;$i++) { + $attrec->hour = $i; + insert_record("attendance_roll",$attrec, false); + } // for loop to mark all hours absent + } // if student has no activity + } // foreach student in the list + } // if the attendance roll is for today + } // for each attendance in the system + return true; +} // function cron - return true; -} function attendance_grades($attendanceid) { /// Must return an array of grades for a given instance of this module, @@ -508,4 +540,49 @@ function attendance_get_participants($attendanceid) { return ($students); } +/** +* Determines if two dates are on the same day +* +* This function takes two unix timestamps and determines if they occur within the same 24 hours +* It does this by comparing the year, month, and day +* +* @param timestamp $d1 The first date to compare +* @param timestamp $d2 The second date to compare +* @return boolean whether the two dates occur on the same day +*/ +function attendance_dates_same_day($d1,$d2) { + $da1 = getdate($d1); + $da2 = getdate($d2); + return (($da1["mday"]==$da2["mday"]) &&($da1["mon"]==$da2["mon"]) && ($da1["year"]==$da2["year"])); +} + +/** +* Finds the beginning of the day for the date specified +* +* This function returns the timestamp for midnight of the day specified in the timestamp +* +* @param timestamp $d The time to find the beginning of the day for +* @return timestamp midnight for that day +*/ +function attendance_find_today($d) { + // add 24 hours to the current time - to solve end of month date issues + $da = getdate($d); + // now return midnight of that day + return mktime(0,0,0,$da["mon"], $da["mday"], $da["year"]); +} + +/** +* Finds the beginning of the day following the date specified +* +* This function returns the timestamp for midnight of the day after the timestamp specified +* +* @param timestamp $d The time to find the next day of +* @return timestamp midnight of the next day +*/ +function attendance_find_tomorrow($d) { + // add 24 hours to the current time - to solve end of month date issues + return attendance_find_today($d+86400); +} + + ?> diff --git a/mod/attendance/version.php b/mod/attendance/version.php index 0d1749c9a8..27ba381e34 100644 --- a/mod/attendance/version.php +++ b/mod/attendance/version.php @@ -5,7 +5,7 @@ /// This fragment is called by moodle_needs_upgrading() and /admin/index.php ///////////////////////////////////////////////////////////////////////////////// -$module->version = 2003092400; // The current module version (Date: YYYYMMDDXX) -$module->cron = 0; // Period for cron to check this module (secs) +$module->version = 2003092500; // The current module version (Date: YYYYMMDDXX) +$module->cron = 3600; // Period for cron to check this module (secs) ?> diff --git a/mod/attendance/viewall.php b/mod/attendance/viewall.php index ed5e0bc5de..0574b52221 100644 --- a/mod/attendance/viewall.php +++ b/mod/attendance/viewall.php @@ -1,4 +1,4 @@ -id, $course->id); + $attendance->cmid = $cm->id; $atts[$numatt]->attendance=$attendance; // tally the hours for possible paging of the report $numhours=$numhours+$attendance->hours; @@ -85,7 +87,7 @@ if ($download == "xls") { // Creating a workbook $workbook = new Workbook("-"); // Creating the first worksheet - $myxls =& $workbook->add_worksheet('Grades'); + $myxls =& $workbook->add_worksheet('Attendance'); // print the date headings at the top of the table // for each day of attendance @@ -268,7 +270,7 @@ while (($multipage || $onepage) && (!$endonepage)) { for($curpage=1;true;$curpage++) { // the for loop is broken from the inside $pagehours=$atts[$endatt]->attendance->hours; $startatt=$endatt; - while(($pagehours<$hoursinreport)) { + while(($pagehours<=$hoursinreport)) { if ($endatt>=$numatt) { break 2; } // end the page number calculations and trigger the end of a multi-page report! $endatt++; $pagehours=$pagehours+$atts[$endatt]->attendance->hours; @@ -331,7 +333,6 @@ while (($multipage || $onepage) && (!$endonepage)) { attendance_print_pagenav(); } - // build the table for attendance roll // this is the wrapper table echo "attendance->notes != "") ? ":
".$atts[$k]->attendance->notes : ""; - echo "\n"; + $auto = ($atts[$k]->attendance->autoattend == 1) ? "(".get_string("auto","attendance").")" : ""; + echo "\n"; } // if we're at the end of the report if ($maxatt==$numatt || !$pagereport) { @@ -489,8 +492,6 @@ function attendance_print_pagenav() { if ($pagereport) { $of = get_string('of','attendance'); $pg = get_string('page'); - $next = get_string('next'); - $prev = get_string('previous', 'attendance'); echo "
attendance->hours. "\" nowrap class=\"generaltableheader\">". - userdate($atts[$k]->attendance->day,"%m/%0d").$notes."attendance->hours. "\" nowrap class=\"generaltableheader\">". + "attendance->cmid."\">".userdate($atts[$k]->attendance->day,"%m/%0d")."".$auto. + $notes."
". @@ -501,13 +502,19 @@ function attendance_print_pagenav() { echo ""; if ($minatt!=0) { echo "\n"; + "id ."&pagereport=1&page=1\"><< \n". + "id ."&pagereport=1&page=".($page-1)."\"><\n"; + } else { + echo "\n"; } echo "\n"; if ($maxatt!=$numatt) { echo ""; + "id ."&pagereport=1&page=". ($page+1)."\">> ". + "id ."&pagereport=1&page=$maxpages\">>>"; + } else { + echo "\n"; } echo "
". - "id ."&pagereport=1&page=".($page-1)."\">$prev $pg<< <". "$pg $page $of $maxpages". - "id ."&pagereport=1&page=". ($page+1)."\">$next $pg> >>
\n"; } diff --git a/mod/attendance/viewweek.php b/mod/attendance/viewweek.php index c8f11e4935..a58ede777f 100644 --- a/mod/attendance/viewweek.php +++ b/mod/attendance/viewweek.php @@ -51,6 +51,8 @@ if ($attendances) { $numhours=0; foreach ($attendances as $attendance){ // store the raw attendance object + $cm = get_coursemodule_from_instance("attendance", $attendance->id, $course->id); + $attendance->cmid = $cm->id; $atts[$numatt]->attendance=$attendance; // tally the hours for possible paging of the report $numhours=$numhours+$attendance->hours; @@ -86,7 +88,7 @@ if ($download == "xls") { // Creating a workbook $workbook = new Workbook("-"); // Creating the first worksheet - $myxls =& $workbook->add_worksheet('Grades'); + $myxls =& $workbook->add_worksheet('Weekly Attendance'); // print the date headings at the top of the table // for each day of attendance @@ -356,8 +358,10 @@ while (($multipage || $onepage) && (!$endonepage)) { for($k=$minatt;$k<$maxatt;$k++) { // put notes for the date in the date heading $notes = ($atts[$k]->attendance->notes != "") ? ":
".$atts[$k]->attendance->notes : ""; - echo "attendance->hours. "\" nowrap class=\"generaltableheader\">". - userdate($atts[$k]->attendance->day,"%m/%0d").$notes."\n"; + $auto = ($atts[$k]->attendance->autoattend == 1) ? "(".get_string("auto","attendance").")" : ""; + echo "attendance->hours. "\" nowrap class=\"generaltableheader\">". + "attendance->cmid."\">".userdate($atts[$k]->attendance->day,"%m/%0d")."".$auto. + $notes."\n"; } // if we're at the end of the report if ($maxatt==$numatt || !$pagereport) { @@ -479,8 +483,6 @@ function attendance_print_pagenav() { if ($pagereport) { $of = get_string('of','attendance'); $pg = get_string('page'); - $next = get_string('next'); - $prev = get_string('previous', 'attendance'); echo "
". @@ -491,13 +493,19 @@ function attendance_print_pagenav() { echo ""; if ($minatt!=0) { echo "\n"; + "< \n"; + "<<\n"; + } else { + echo "\n"; } echo "\n"; if ($maxatt!=$numatt) { echo ""; + "> ". + ">>"; + } else { + echo "\n"; } echo "
". - "$prev $pg<< <". "$pg $page $of $maxpages". - "$next $pg> >>
\n"; } -- 2.39.5