From 7cab0ba37ca4ba365ba2d117196610f92fa07017 Mon Sep 17 00:00:00 2001 From: jungwirr Date: Wed, 17 Sep 2003 14:37:42 +0000 Subject: [PATCH] fixed bugs in view.php, mod.html, viewall.php added add.php for adding mutiple instances at once - not nearly done yet! --- mod/attendance/add.php | 250 +++++++++++++++++++++++++++++++++++++ mod/attendance/lib.php | 117 +++++++++++++---- mod/attendance/mod.html | 62 ++++----- mod/attendance/version.php | 2 +- mod/attendance/view.php | 12 +- mod/attendance/viewall.php | 29 +++-- 6 files changed, 398 insertions(+), 74 deletions(-) create mode 100644 mod/attendance/add.php diff --git a/mod/attendance/add.php b/mod/attendance/add.php new file mode 100644 index 0000000000..6136c7ead6 --- /dev/null +++ b/mod/attendance/add.php @@ -0,0 +1,250 @@ +
";
+echo "submitted form with these values:\n";
+
+    var_dump($_POST);
+
+echo "
"; +/* + + require_login(); +/// ----------------------------------------------------------------------------------- +/// --------------------SECTION FOR PROCESSING THE FORM ON POST ----------------------- +/// ----------------------------------------------------------------------------------- + if (isset($SESSION->modform)) { // Variables are stored in the session + $mod = $SESSION->modform; + unset($SESSION->modform); + } else { + $mod = (object)$_POST; + } + + + if (isset($cancel)) { + if (!empty($SESSION->returnpage)) { + $return = $SESSION->returnpage; + unset($SESSION->returnpage); + redirect($return); + } else { + redirect("view.php?id=$mod->course"); + } + } + + + if (isset($_POST["course"])) { // add or update form submitted + + if (!isteacheredit($mod->course)) { + error("You can't modify this course!"); + } + + $modlib = "../mod/$mod->modulename/lib.php"; + if (file_exists($modlib)) { + include_once($modlib); + } else { + error("This module is missing important code! ($modlib)"); + } + $addinstancefunction = $mod->modulename."_add_instance"; + $updateinstancefunction = $mod->modulename."_update_instance"; + $deleteinstancefunction = $mod->modulename."_delete_instance"; + + switch ($mod->mode) { + case "update": + if (! $updateinstancefunction($mod)) { + error("Could not update the $mod->modulename"); + } + add_to_log($mod->course, "course", "update mod", + "../mod/$mod->modulename/view.php?id=$mod->coursemodule", + "$mod->modulename $mod->instance"); + break; + + case "add": + if (! $mod->instance = $addinstancefunction($mod)) { + error("Could not add a new instance of $mod->modulename"); + } + // 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"); + } + if (! $sectionid = add_mod_to_section($mod) ) { + error("Could not add the new course module to that section"); + } + if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) { + error("Could not update the course module with the correct section"); + } + add_to_log($mod->course, "course", "add mod", + "../mod/$mod->modulename/view.php?id=$mod->coursemodule", + "$mod->modulename $mod->instance"); + break; + case "delete": + if (! $deleteinstancefunction($mod->instance)) { + notify("Could not delete the $mod->modulename (instance)"); + } + if (! delete_course_module($mod->coursemodule)) { + notify("Could not delete the $mod->modulename (coursemodule)"); + } + if (! delete_mod_from_section($mod->coursemodule, "$mod->section")) { + notify("Could not delete the $mod->modulename from that section"); + } + add_to_log($mod->course, "course", "delete mod", + "view.php?id=$mod->course", + "$mod->modulename $mod->instance"); + break; + default: + error("No mode defined"); + + } + + rebuild_course_cache($mod->course); + + if (!empty($SESSION->returnpage)) { + $return = $SESSION->returnpage; + unset($SESSION->returnpage); + redirect($return); + } else { + redirect("view.php?id=$mod->course"); + } + exit; + } +*/ + + +} else { +/// ----------------------------------------------------------------------------------- +/// ------------------ SECTION FOR MAKING THE FORM TO BE POSTED ----------------------- +/// ----------------------------------------------------------------------------------- + +/// @include_once("$CFG->dirroot/mod/attendance/lib.php"); +/// error_reporting(E_ALL); + + optional_variable($id); // Course Module ID, or + optional_variable($a); // attendance ID + +/// populate the appropriate objects + if ($id) { + if (! $course = get_record("course", "id", $id)) { + error("Course is misconfigured"); + } + if (! $attendance = get_record("attendance", "course", $id)) { + error("Course module is incorrect"); + } +/* if (! $cm = get_coursemodule_from_instance("attendance", $attendance->id, $id)) { + error("Course Module ID was incorrect"); + } + if (! $attendances = get_records("attendance", "course", $cm->course)) { + error("Course module is incorrect"); + } +*/ + } else { + if (! $attendance = get_record("attendance", "id", $a)) { + error("Course module is incorrect"); + } + if (! $course = get_record("course", "id", $attendance->course)) { + error("Course is misconfigured"); + } + if (! $cm = get_coursemodule_from_instance("attendance", $attendance->id, $course->id)) { + error("Course Module ID was incorrect"); + } + if (! $attendances = get_records("attendance", "course", $cm->course)) { + error("Course module is incorrect"); + } + } + + require_login($course->id); + + add_to_log($course->id, "attendance", "add", "add.php?id=$course->id"); + +/// Print the page header + if ($course->category) { + $navigation = "id\">$course->shortname ->"; + } + + $strattendances = get_string("modulenameplural", "attendance"); + $strattendance = get_string("modulename", "attendance"); + $straddmultiple = get_string("addmultiple", "attendance"); + $strallattendance = get_string("allmodulename", "attendance"); + + print_header("$course->shortname: $strallattendance", "$course->fullname", + "$navigation id>$strattendances -> $straddmultiple", + "", "", true, " ", + navmenu($course)); + +/// Print the main part of the page + + // adaptation of mod code to view code needs this: + $form = $attendance; + + if (!isteacher($course->id)) { + notice(get_string("cantadd", "attendance")); + print_footer($course); exit; + } +//choose the start date +//choose the end date +//OR +//for the whole course +//choose the days of the week you want attendance + @include_once("$CFG->dirroot/mod/attendance/lib.php"); + //require_once("lib.php") +?> +
+
+"> +"> + + + + + + + + + + + + + + + + + + + + + + + + + +

:

day) ?>

:

+ dynsection) ? 'checked' : '' ?> > +

:

hours, "1","","1") ?>

:

+ +
+ + + + + + + + +
+"> +"> +
+
+ + \ No newline at end of file diff --git a/mod/attendance/lib.php b/mod/attendance/lib.php index efbeaece7f..4ed73a5737 100755 --- a/mod/attendance/lib.php +++ b/mod/attendance/lib.php @@ -4,7 +4,8 @@ // error_reporting(E_ALL); -function attendance_add_instance(&$attendance) { +function attendance_add_instance($attendance) { + global $mod; $attendance->timemodified = time(); $attendance->dynsection = !empty($attendance->dynsection) ? 1 : 0; $attendance->day = make_timestamp($attendance->theyear, @@ -13,14 +14,30 @@ function attendance_add_instance(&$attendance) { if ($attendance->notes) { $attendance->name = $attendance->name . " - " . $attendance->notes; } + +if (($attendance->dynsection) && ($course->format =="weeks")) { + if ($mod->course) { + if (! $course = get_record("course", "id", $mod->course)) { + error("Course is misconfigured"); + } + } +// 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) { +function attendance_update_instance($attendance) { + global $mod; $attendance->timemodified = time(); - $attendance->oldid=$attendance->id; +// $attendance->oldid=$attendance->id; $attendance->id = $attendance->instance; $attendance->dynsection = !empty($attendance->dynsection) ? 1 : 0; @@ -31,29 +48,71 @@ function attendance_update_instance(&$attendance) { $attendance->name = $attendance->name . " - " . $attendance->notes; } + if (($attendance->dynsection) && ($course->format =="weeks")) { + //get info about the course + if ($mod->course) { + if (! $course = get_record("course", "id", $mod->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 = "$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 - return update_record("attendance", $attendance); + 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) { @@ -127,6 +186,10 @@ 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(); @@ -140,8 +203,8 @@ function attendance_user_complete($course, $user, $mod, $attendance) { 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="X";} + $status = (($grid[$j] == 1) ? $T : $A); + } else {$status=$P;} echo "".$status."\n"; } /// for loop echo "\n"; diff --git a/mod/attendance/mod.html b/mod/attendance/mod.html index ecfb48f770..a4ce37337b 100755 --- a/mod/attendance/mod.html +++ b/mod/attendance/mod.html @@ -3,56 +3,58 @@ - dirroot/mod/attendance/lib.php") + dirroot/mod/attendance/lib.php") //require_once("lib.php") ?> -
+
-"> -"> +"> +"> - + - - + + - - + + - + - - - + + - +

:

day) ?>

:

day) ?>

:

:

- dynsection) ? 'checked' : '' ?> > + dynsection) ? 'checked' : '' ?> >

:

hours, "1","","1") ?>

:

hours, "1","","1") ?>

:

:

- +
-id) { // get the list of attendance records for all hours of the given day and // put it in the array for use in the attendance table $rolls = get_records("attendance_roll", "dayid", $form->id); - foreach ($rolls as $roll) { - $sroll[$roll->userid][$roll->hour]->status=$roll->status; - $sroll[$roll->userid][$roll->hour]->notes=$roll->notes; - } + if ($rolls) { + foreach ($rolls as $roll) { + $sroll[$roll->userid][$roll->hour]->status=$roll->status; + $sroll[$roll->userid][$roll->hour]->notes=$roll->notes; + } + } // get the list of students along with student ID field // get back array of stdclass objects in sorted order, with members: // id, username,firstname,lastname,maildisplay,mailformat,email,city,country, @@ -129,15 +131,15 @@ if ($form->hours >1) { } // if ($form->id) ?> - - - - - - - + + + + + + +
-"> -"> +"> +">
diff --git a/mod/attendance/version.php b/mod/attendance/version.php index 5a7356808a..380cd3e281 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 = 2003091001; // The current module version (Date: YYYYMMDDXX) +$module->version = 2003090301; // The current module version (Date: YYYYMMDDXX) $module->cron = 0; // Period for cron to check this module (secs) ?> diff --git a/mod/attendance/view.php b/mod/attendance/view.php index fabf3c2cab..67f8d3df2e 100644 --- a/mod/attendance/view.php +++ b/mod/attendance/view.php @@ -1,4 +1,4 @@ -id); } $i=0; + $A = get_string("absentshort","attendance"); + $T = get_string("tardyshort","attendance"); + $P = get_string("presentshort","attendance"); + foreach ($students as $student) { echo "".$student->lastname."\n"; echo "".$student->firstname."\n"; @@ -122,9 +126,9 @@ $abs=$tar=0; for($j=1;$j<=$form->hours;$j++) { // set the attendance defaults for each student - if ($sroll[$student->id][$j]->status == 1) {$status="T";$tar++;} - elseif ($sroll[$student->id][$j]->status == 2) {$status="A";$abs++;} - else {$status="X";} + if ($sroll[$student->id][$j]->status == 1) {$status=$T;$tar++;} + elseif ($sroll[$student->id][$j]->status == 2) {$status=$A;$abs++;} + else {$status=$P;} echo "".$status."\n"; } /// for loop if ($form->hours > 1) { diff --git a/mod/attendance/viewall.php b/mod/attendance/viewall.php index 86e55dbb1c..48624b8923 100644 --- a/mod/attendance/viewall.php +++ b/mod/attendance/viewall.php @@ -1,4 +1,4 @@ -id); - add_to_log($course->id, "attendance", "viewall", "viewall.php?id=$cm->id", "$attendance->id"); + add_to_log($course->id, "attendance", "viewall", "viewall.php?id=$course->id"); /// Print the page header if ($course->category) { @@ -93,10 +93,12 @@ if ($attendances) { } else { // must be a teacher $rolls = get_records("attendance_roll", "dayid", $attendance->id); } - foreach ($rolls as $roll) { - $atts[$numatt]->sroll[$roll->userid][$roll->hour]->status=$roll->status; - $atts[$numatt]->sroll[$roll->userid][$roll->hour]->notes=$roll->notes; - } + if ($rolls) { + foreach ($rolls as $roll) { + $atts[$numatt]->sroll[$roll->userid][$roll->hour]->status=$roll->status; + $atts[$numatt]->sroll[$roll->userid][$roll->hour]->notes=$roll->notes; + } + } $numatt++; } @@ -116,7 +118,7 @@ if ($attendances) { // adjust the width for the report for students if (($onetable) || ($CFG->attendance_hours_in_full_report == 0)) { - $hoursinreport = 10000; + $hoursinreport = 100+$numhours; } else if (isstudent($course->id)) { $hoursinreport = $CFG->attendance_hours_in_full_report + 15; } else { @@ -140,7 +142,7 @@ while (($multipage || $onepage) && (!$endonepage)) { // $startatt=$endatt; $curpage=1; $endatt=0; - for($curpage=1;true;$curpage++) { // the for loop is broken from the inside + for($curpage=1;true;$curpage++) { // the for loop is broken from the inside $pagehours=$atts[$endatt]->attendance->hours; $startatt=$endatt; while(($pagehours<$hoursinreport)) { @@ -230,6 +232,9 @@ while (($multipage || $onepage) && (!$endonepage)) { $students = attendance_get_course_students($attendance->course, "u.lastname ASC"); } $i=0; + $A = get_string("absentshort","attendance"); + $T = get_string("tardyshort","attendance"); + $P = get_string("presentshort","attendance"); foreach ($students as $student) { if (isteacher($course->id)) { echo "".$student->lastname."\n"; @@ -240,9 +245,9 @@ while (($multipage || $onepage) && (!$endonepage)) { for($k=$minatt;$k<$maxatt;$k++) { // for eacj day of attendance for the student for($j=1;$j<=$atts[$k]->attendance->hours;$j++) { // set the attendance defaults for each student - if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status="T";} - elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status="A";} - else {$status="X";} + if ($atts[$k]->sroll[$student->id][$j]->status == 1) {$status=$T;} + elseif ($atts[$k]->sroll[$student->id][$j]->status == 2) {$status=$A;} + else {$status=$P;} echo "".$status."\n"; } /// for loop } -- 2.39.5