instance = attendance_add_instance($mod)) { error("Could not add a new instance of $mod->modulename"); return 0; } // course_modules and course_sections each contain a reference // to each other, so we have to update one of them twice. if (! $mod->coursemodule = add_course_module($mod) ) { error("Could not add a new course module"); return 0; } if (! $sectionid = add_mod_to_section($mod) ) { error("Could not add the new course module to that section"); return 0; } if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) { error("Could not update the course module with the correct section"); return 0; } add_to_log($mod->course, "course", "add mod", "../mod/$mod->modulename/view.php?id=$mod->coursemodule", "$mod->modulename $mod->instance"); rebuild_course_cache($mod->course); } function attendance_add_instance($attendance) { global $mod; $attendance->timemodified = time(); $attendance->dynsection = !empty($attendance->dynsection) ? 1 : 0; $attendance->autoattend = !empty($attendance->autoattend) ? 1 : 0; $attendance->grade = !empty($attendance->grade) ? 1 : 0; if (empty($attendance->day)) { $attendance->day = make_timestamp($attendance->theyear, $attendance->themonth, $attendance->theday); } $attendance->name=userdate($attendance->day, get_string("strftimedate")); if (isset($attendance->notes)) { $attendance->name = $attendance->name . " - " . $attendance->notes; } $attendance->edited = 0; if ($attendance->dynsection) { if ($mod->course) { if (! $course = get_record("course", "id", $mod->course)) { error("Course is misconfigured"); } } if ($course->format =="weeks") { // floor($date_relative / 604800) + 1 $attendance->section = floor(($attendance->day - $course->startdate)/604800) +1; if($attendance->section > $course->numsections){ $attendance->section = 0; } $attendance->section = "$attendance->section"; $mod->section = "$attendance->section"; } } // insert the main record first return $attendance->id = insert_record("attendance", $attendance); } function attendance_update_instance($attendance) { global $mod; $attendance->edited = 1; $attendance->timemodified = time(); // $attendance->oldid=$attendance->id; $attendance->id = $attendance->instance; $attendance->dynsection = !empty($attendance->dynsection) ? 1 : 0; $attendance->autoattend = !empty($attendance->autoattend) ? 1 : 0; $attendance->grade = !empty($attendance->grade) ? 1 : 0; $attendance->day = make_timestamp($attendance->theyear, $attendance->themonth, $attendance->theday); $attendance->name=userdate($attendance->day, get_string("strftimedate")); if ($attendance->notes) { $attendance->name = $attendance->name . " - " . $attendance->notes; } if ($attendance->dynsection) { //get info about the course if ($attendance->course) { if (! $course = get_record("course", "id", $attendance->course)) { error("Course is misconfigured"); } } //work out which section this should be in $attendance->section = floor(($attendance->day - $course->startdate)/604800) +1; if (($attendance->section > $course->numsections) || ($attendance->section < 0)){ $attendance->section = 0; } // $attendance->section = "$attendance->section"; } // get the data from the attendance grid if ($data = data_submitted()) { // Peel out all the data from variable names. $attrec->dayid = $attendance->id; foreach ($data as $key => $val) { $pieces = explode('_',$key); if ($pieces[0] == 'student') { $attrec->userid=$pieces[1]; $attrec->hour=$pieces[2]; $attrec->status=$val; // clear out any old records for the student delete_records("attendance_roll", "dayid",$attrec->dayid, "hour", $attrec->hour, "userid",$attrec->userid); if ($attrec->status != 0) { // student is registered as absent or tardy insert_record("attendance_roll",$attrec, false); } } // if we have a piece of the student roll data } // foreach for all form variables } // if if(!update_record("attendance", $attendance)){ error("Couldn't update record"); } if ($attendance->dynsection) { //get section info $section = get_record("course_sections", "course", $attendance->course, "section", $attendance->section); //remove module from the current section if (! delete_mod_from_section($attendance->coursemodule, $mod->section)) { notify("Could not delete module from existing section"); } //update course with new module location if(! set_field("course_modules", "section", $section->id, "id", $attendance->coursemodule)){ notify("Could not update course module list"); } //add module to the new section if (! add_mod_to_section($attendance, NULL)) { notify("Could not add module to new section"); } rebuild_course_cache($section->course); } return true; } function attendance_delete_instance($id) { if (! $attendance = get_record("attendance", "id", "$id")) { return false; } $result = true; /// delete all the rolls for the day delete_records("attendance_roll", "dayid", "$attendance->id"); if (! delete_records("attendance", "id", "$attendance->id")) { $result = false; } return $result; } function attendance_user_outline($course, $user, $mod, $attendance) { /// Return a small object with summary information about what a /// user has done with a given particular instance of this module /// Used for user activity reports. /// $return->time = the time they did it /// $return->info = a short text description /// for attendance, this would be a list present and tardy for every hour of the day $tardies=count_records("attendance_roll", "dayid", $attendance->id, "userid", $user->id, "status", 1); $absences=count_records("attendance_roll", "dayid", $attendance->id, "userid", $user->id, "status", 2); // build longer string for tardies if ($tardies > 0) { $tardyrecs=attendance_get_records("attendance_roll", "dayid", $attendance->id, "userid", $user->id, "status", 1, "hour ASC"); if ($tardies == 1) { $tardystring = "Tardy in hour " . $tardyrecs[0]->hour . ". "; } elseif ($tardies == $attendance->hours) { $tardystring = "Tardy in all hours. (" . $attendance->hours . ") "; } else { // build array of all tardies $tarr = array(); foreach ($tardyrecs as $tardyrec) { array_push($tarr, $tardyrec->hour); $tardystring = $tardystring . ", " . $tardyrec->hour; } $end=array_pop($tarr); $tardystring = "Tardy in hours " . implode(", ", $tarr) . " and ". $end . ". "; } } else { $tardystring = "";} // build longer string for absences if ($absences > 0) { $absrecs=attendance_get_records("attendance_roll", "dayid", $attendance->id, "userid", $user->id, "status", 2, "hour ASC"); if ($absences == 1) { $absstring = "Absent in hour " . $absrecs[0]->hour . "."; } elseif ($absences == $attendance->hours) { $absstring = "Absent in all hours. (" . $attendance->hours . ")"; } else { // build array of all absences $aarr = array(); foreach ($absrecs as $absrec) { array_push($aarr, $absrec->hour); } $end=array_pop($aarr); $absstring = "Absent in hours " . implode(", ", $aarr) . " and ". $end . "."; } } else { $absstring = "";} $return->info=$tardystring . $absstring; if ($return->info == "") $return->info = "No Tardies or Absences"; return $return; } function attendance_user_complete($course, $user, $mod, $attendance) { /// Print a detailed representation of what a user has done with /// a given particular instance of this module, for user activity reports. // get the attendance record for that day and user $A = get_string("absentshort","attendance"); $T = get_string("tardyshort","attendance"); $P = get_string("presentshort","attendance"); $attrecs=attendance_get_records("attendance_roll", "dayid", $attendance->id, "userid", $user->id, "", "", "hour ASC"); // fill an array with the absences and tardies, as those are the only records actually stored $grid = array(); foreach ($attrecs as $attrec) { $grid[$attrec->hour]=$attrec->status; } echo "
Hour: | \n"; // echo out the table header for($j=1;$j<=$attendance->hours;$j++) { echo "".$j." | \n"; } echo "
---|---|
Status: | "; for($j=1;$j<=$attendance->hours;$j++) { // set the attendance defaults for each student if (isset($grid[$j])) { $status = (($grid[$j] == 1) ? $T : $A); } else {$status=$P;} echo "".$status." | \n"; } /// for loop echo "
$sql \n"; $section = $sectarray->section; /* select cm.section from mdl_course_modules cm, mdl_modules md, mdl_attendance m where cm.course = '7' AND cm.deleted = '0' AND cm.instance = m.id AND md.name = 'attendance' AND md.id = cm.module AND m.id = '119'; */ // then get all the attendance instances in that section $sql = "SELECT a.* FROM {$CFG->prefix}course_modules cm, {$CFG->prefix}modules md, {$CFG->prefix}attendance a WHERE cm.course = '$courseid' AND cm.deleted = '0' AND cm.section = '$section' AND md.name = 'attendance' AND md.id = cm.module AND a.id = cm.instance order by a.day ASC"; // echo "
$sql \n"; return get_records_sql($sql); /* select m.* from mdl_course_modules cm, mdl_modules md, mdl_attendance m where cm.course = '7' AND cm.deleted = '0' AND cm.section = '85' AND md.name = 'attendance' AND md.id = cm.module AND m.id = cm.instance; */ } /** * Return all attendance records that are in the same 7 day span as the instance specified * * This function uses the course and attendance tables together to find all the attendance * records that are for days within the same week span as the instance specified. The week is * determined based NOT on calendar week, but instead on the week span as it occurs in a * weekly formatted course - I find this by starting with the startdate of the course and * then skipping ahead by weeks til I find the range that fits the instance, then I use that * range as min and max to query the attendance table for all the other records. Note that this * function will work with non-weekly formatted courses, though the results won't easily * correlate with the course view. But it will work regardless. * * @param int $id the id of the attendance record we're using as a basis for the query * @param int $courseid the id of the course for which we're getting records * @return (object)recordset associative array of records containing the attendance records we wanted */ function get_attendance_for_week($id, $courseid) { global $CFG; if (! $attendance = get_record("attendance", "id", $id)) { error("Course module is incorrect"); } if (! $course = get_record("course", "id", $courseid)) { error("Course module is incorrect"); } // the offset is for weeks that don't start on Monday $day = $attendance->day; // determine the week range for the select, based on the day for ($maxday=$course->startdate;$day>$maxday;$maxday=$maxday+604800) {;}$minday = $maxday-608400; $sql = "SELECT * FROM {$CFG->prefix}attendance WHERE course = '$courseid' AND day<$maxday AND day>=$minday order by day ASC;"; // echo "
$sql \n"; return get_records_sql($sql); } /** * 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) { // $da = getdate($d); $damon = gmdate("m",$d); $daday = gmdate("d",$d); $dayear = gmdate("Y",$d); // now return midnight of that day // return mktime(0,0,0,$da["mon"], $da["mday"], $da["year"]); return gmmktime(0,0,0,$damon, $daday, $dayear); } /** * 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); } ?>