]> git.mjollnir.org Git - moodle.git/commitdiff
Finished first version of add.php - to add multiple attendance instances
authorjungwirr <jungwirr>
Wed, 17 Sep 2003 23:46:53 +0000 (23:46 +0000)
committerjungwirr <jungwirr>
Wed, 17 Sep 2003 23:46:53 +0000 (23:46 +0000)
at the same time.

lang/en/attendance.php
mod/attendance/add.php
mod/attendance/lib.php
mod/attendance/mod.html

index af7bc2970d89b14484bc26da70be6bd903b4d25b..6171af12c17e188b8efe1c2fbfd3c3dcf62e8d16 100755 (executable)
@@ -5,6 +5,7 @@ $string['modulename'] = "Attendance";
 $string['allmodulename'] = "All Attendance Rolls";
 $string['modulenameplural'] = "Attendance Rolls";
 $string['addmultiple'] = "Add Multiple Attendance Rolls";
+$string['addingmultiple'] = "Adding Multiple Attendance Rolls";
 #------------------------------------------------------------
 
 
@@ -13,7 +14,7 @@ $string['for'] = "for";
 $string['dayofroll'] = "Date to take roll for";
 $string['notes'] = "Exceptional information";
 $string['notesfor'] = "Exceptional information for";
-$string['hoursinclass'] = "Number of Hours in a Class Period";
+$string['hoursinclass'] = "Number of hours in a class period";
 $string['presentshort'] = "X";
 $string['tardyshort'] = "T";
 $string['absentshort'] = "A";
@@ -38,5 +39,14 @@ $string['startmulti'] = "Date for first attendance roll";
 $string['endmulti'] = "Date for last attendance roll";
 $string['choosedays'] = "Days of each week to take attendance";
 $string['dynamicsectionmulti'] = "Place rolls in their weekly section automatically";
-
+$string['hoursineachclass'] = "Number of Hours in each class period";
+$string['sunday'] = "Sun";
+$string['monday'] = "Mon";
+$string['tuesday'] = "Tue";
+$string['wednesday'] = "Wed";
+$string['thursday'] = "Thu";
+$string['friday'] = "Fri";
+$string['saturday'] = "Sat";
+$string['endbeforestart'] = "The end date is before the start date";
+$string['startafterend'] = "The start date is after the end of the course";
 ?>
index 6136c7ead6195609062a041a53b97c3b6a7e065b..c53c5b8301378aa0c30969aace5f2a3ca955d37e 100644 (file)
     require("../../config.php");
     require("lib.php");
 
+// if form is being submitted from generated form
 if (isset($_POST["course"]))  {
-
-echo "<html><body><pre>";
-echo "submitted form with these values:\n";
-
-    var_dump($_POST);
-
-echo "</pre></body></html>";
-/*
-
-    require_login();
+  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($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 (!isteacheredit($mod->course)) {
+      error("You can't modify this course!");
+  }
+
+  $modlib = "lib.php";
+  if (file_exists($modlib)) {
+      include_once($modlib);
+  } else {
+      error("This module is missing important code! ($modlib)");
+  }
+
+/* // set the information for the new instances
+     $attendance->dynsection = !empty($attendance->dynsection) ? 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;
+        }
+*/
+  $curdate = make_timestamp($mod->startyear, $mod->startmonth, $mod->startday);
+  $stopdate = make_timestamp($mod->endyear, $mod->endmonth, $mod->endday);
+  $enddate = $curdate + $mod->numsections * 604800;  
+  if ($curdate > $stopdate) {
+       error(get_string("endbeforestart", "attendance"));
+       }
+  if ($enddate < $curdate) {
+       error(get_string("startafterend", "attendance"));
+       }
+  if ($stopdate > $enddate) {
+      // if stop date is after end of course, just move it to end of course
+                       $stopdate = $enddate;
+       }
+  while ($curdate <= $stopdate) {
+    $mod->day = $curdate;
+    $mod->name=userdate($mod->day, get_string("strftimedate"));
+         if (isset($mod->notes)) {$mod->name = $mod->name . " - " . $mod->notes;}
+    switch(userdate($curdate, "%u")) {
+      case 1: if (!empty($mod->mon)) {attendance_add_module($mod);}break;
+      case 2: if (!empty($mod->tue)) {attendance_add_module($mod);}break;
+      case 3: if (!empty($mod->wed)) {attendance_add_module($mod);}break;
+      case 4: if (!empty($mod->thu)) {attendance_add_module($mod);}break;
+      case 5: if (!empty($mod->fri)) {attendance_add_module($mod);}break;
+      case 6: if (!empty($mod->sat)) {attendance_add_module($mod);}break;
+      case 7: if (!empty($mod->sun)) {attendance_add_module($mod);}break;
+    } // switch
+    $curdate = $curdate + 86400; // add one day to the date
+  } // while for days
+      
+  if (!empty($SESSION->returnpage)) {
+      $return = $SESSION->returnpage;
+      unset($SESSION->returnpage);
+      redirect($return);
+  } else {
+      redirect("index.php?id=$mod->course");
+  }
+  exit;
+  
+} else {
+/// -----------------------------------------------------------------------------------
+/// ------------------ SECTION FOR MAKING THE FORM TO BE POSTED -----------------------
+/// -----------------------------------------------------------------------------------
 
-    if (isset($cancel)) {  
-        if (!empty($SESSION->returnpage)) {
-            $return = $SESSION->returnpage;
-            unset($SESSION->returnpage);
-            redirect($return);
-        } else {
-            redirect("view.php?id=$mod->course");
-        }
-    }
+/// @include_once("$CFG->dirroot/mod/attendance/lib.php"); 
+/// error_reporting(E_ALL);
 
+        require_variable($id);
+        require_variable($section);
 
-    if (isset($_POST["course"])) {    // add or update form submitted
+        if (! $course = get_record("course", "id", $id)) {
+            error("This course doesn't exist");
+        }
 
-        if (!isteacheredit($mod->course)) {
-            error("You can't modify this course!");
+        if (! $module = get_record("modules", "name", "attendance")) {
+            error("This module type doesn't exist");
         }
 
-        $modlib = "../mod/$mod->modulename/lib.php";
-        if (file_exists($modlib)) {
-            include_once($modlib);
+        $form->section    = $section;         // The section number itself
+        $form->course     = $course->id;
+        $form->module     = $module->id;
+        $form->modulename = $module->name;
+        $form->instance   = "";
+        $form->coursemodule = "";
+        $form->mode       = "add";
+
+        $sectionname    = get_string("name$course->format");
+        $fullmodulename = strtolower(get_string("modulename", $module->name));
+
+        if ($form->section) {
+            $heading->what = $fullmodulename;
+            $heading->to   = "$sectionname $form->section";
+            $pageheading = get_string("addingmultiple", "attendance");
         } else {
-            error("This module is missing important code! ($modlib)");
+            $pageheading = get_string("addingmultiple", "attendance");
         }
-        $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");
 
-        }
+    if (!isteacheredit($course->id)) {
+        error("You can't modify this course!");
+    }
 
-        rebuild_course_cache($mod->course);
+    $streditinga = get_string("editinga", "moodle", $fullmodulename);
+    $strmodulenameplural = get_string("modulenameplural", $module->name);
 
-        if (!empty($SESSION->returnpage)) {
-            $return = $SESSION->returnpage;
-            unset($SESSION->returnpage);
-            redirect($return);
-        } else {
-            redirect("view.php?id=$mod->course");
-        }
-        exit;
+    if ($course->category) {
+        print_header("$course->shortname: $streditinga", "$course->fullname",
+                     "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> 
+                      <A HREF=\"$CFG->wwwroot/mod/$module->name/index.php?id=$course->id\">$strmodulenameplural</A> -> 
+                      $streditinga", "form.name", "", false);
+    } else {
+        print_header("$course->shortname: $streditinga", "$course->fullname",
+                     "$streditinga", "form.name", "", false);
     }
-*/
+
+    unset($SESSION->modform); // Clear any old ones that may be hanging around.
 
 
-} 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($id);    // Course Module ID, or
     optional_variable($a);     // attendance ID
 
 /// populate the appropriate objects
@@ -135,13 +156,13 @@ echo "</pre></body></html>";
         if (! $attendance = get_record("attendance", "course", $id)) {
             error("Course module is incorrect");
         }
-/*        if (! $cm = get_coursemodule_from_instance("attendance", $attendance->id, $id)) {
+        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");
@@ -169,29 +190,20 @@ echo "</pre></body></html>";
     $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",
+    print_header("$course->shortname: $straddmultiple", "$course->fullname",
                  "$navigation <A HREF=index.php?id=$course->id>$strattendances</A> -> $straddmultiple", 
                   "", "", true, "&nbsp;", 
                   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")
+// determine the end date for the course based on the number of sections and the start date
+$course->enddate = $course->startdate + $course->numsections * 604800;
 ?>
 <FORM name="form" method="post" action="<?=$ME ?>">
 <CENTER>
@@ -199,18 +211,37 @@ echo "</pre></body></html>";
 <INPUT type="submit" name="cancel" value="<? print_string("cancel") ?>">
 <TABLE cellpadding=5>
 
-<!-- <? $options[0] = get_string("no"); $options[1] = get_string("yes"); ?> -->
-<!-- <TR valign=top> -->
-<!--     <TD align=right><P><B><? print_string("takeroll", "attendance") ?>:</B></P></TD> -->
-<!--     <TD align=left><? choose_from_menu($options, "roll", $form->roll, "") ?></td> -->
-<!-- </tr> -->
+<TR valign=top>
+    <TD align=right><P><B><?php print_string("startmulti", "attendance") ?>:</B></P></TD>
+    <TD colspan="3"><?php print_date_selector("startday", "startmonth", "startyear",$course->startdate) ?></TD>
+</TR>
+<TR valign=top>
+    <TD align=right><P><B><?php print_string("endmulti", "attendance") ?>:</B></P></TD>
+    <TD colspan="3"><?php print_date_selector("endday", "endmonth", "endyear",$course->enddate) ?></TD>
+</TR>
 
 <TR valign=top>
-    <TD align=right><P><B><?php print_string("dayofroll", "attendance") ?>:</B></P></TD>
-    <TD colspan="3"><?php print_date_selector("theday", "themonth", "theyear", $form->day) ?></TD>
+    <TD align=right><P><B><?php print_string("choosedays", "attendance") ?>:</B></P></TD>
+    <TD colspan="3">
+    <?php print_string("sunday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="sun" >
+    <?php print_string("monday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="mon" "checked">
+    <?php print_string("tuesday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="tue" "checked">
+    <?php print_string("wednesday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="wed" "checked">
+    <?php print_string("thursday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="thu" "checked">
+    <?php print_string("friday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="fri" "checked">
+    <?php print_string("saturday","attendance"); echo ":"; ?>
+    <input type="checkbox" name="sat" >
+    </TD>
 </TR>
+
 <tr valign=top>
-    <TD align="right"><P><B><?php print_string("dynamicsection", "attendance") ?>:</B></P></TD>
+    <TD align="right"><P><B><?php print_string("dynamicsectionmulti", "attendance") ?>:</B></P></TD>
     <TD align="left">
       <input type="checkbox" name="dynsection" <?php echo !empty($form->dynsection) ? 'checked' : '' ?> >
     </TD>
@@ -218,14 +249,8 @@ echo "</pre></body></html>";
 <?php // starting with 2 to allow for the nothing value in choose_from_menu to be the default of 1
 for ($i=2;$i<=24;$i++){ $opt[$i] = $i; } ?>
 <TR valign=top>
-    <TD align=right><P><B><?php print_string("hoursinclass", "attendance") ?>:</B></P></TD>
-    <TD  colspan="3" align="left"><?php choose_from_menu($opt, "hours", $form->hours, "1","","1") ?></td>
-</tr>
-<tr valign=top>
-    <td align=right><p><b><?php print_string("notes", "attendance") ?>:</b></p></td>
-    <td colspan="3">
-        <input type="text" name="notes" size=60 value="<?php p($form->notes) ?>">
-    </td>
+    <TD align=right><P><B><?php print_string("hoursineachclass", "attendance") ?>:</B></P></TD>
+    <TD  colspan="3" align="left"><?php choose_from_menu($opt, "hours", $CFG->attendance_default_hours, "1","","1") ?></td>
 </tr>
 </TABLE>
 <!-- These hidden variables are always the same -->
@@ -236,6 +261,7 @@ for ($i=2;$i<=24;$i++){ $opt[$i] = $i; } ?>
 <INPUT type="hidden" name=modulename    value="<?php p($form->modulename) ?>">
 <INPUT type="hidden" name=instance      value="<?php p($form->instance) ?>">
 <INPUT type="hidden" name=mode          value="<?php p($form->mode) ?>">
+<INPUT type="hidden" name=numsections   value="<?php p($course->numsections) ?>">
 <BR />
 <INPUT type="submit" value="<?php print_string("savechanges") ?>">
 <INPUT type="submit" name="cancel" value="<?php print_string("cancel") ?>">
index 4ed73a57378ec4dc32c07767862495eece9ee702..a4a0dc4380135ac130fbf0fdd1cb99f3d0bd4cd7 100755 (executable)
@@ -4,30 +4,60 @@
 
 // error_reporting(E_ALL);
 
+function attendance_add_module(&$mod) {
+//  global $mod;
+  require("../../course/lib.php");
+
+  if (! $mod->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->day = make_timestamp($attendance->theyear, 
-                       $attendance->themonth, $attendance->theday); 
+     if (empty($attendance->day)) {
+       $attendance->day = make_timestamp($attendance->theyear, 
+                          $attendance->themonth, $attendance->theday);
+     }
      $attendance->name=userdate($attendance->day, get_string("strftimedate"));
-        if ($attendance->notes) { 
+        if (isset($attendance->notes)) { 
                $attendance->name = $attendance->name . " - " . $attendance->notes;
         }
 
-if (($attendance->dynsection) && ($course->format =="weeks")) { 
+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";
+         $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);
@@ -48,20 +78,19 @@ function attendance_update_instance($attendance) {
                $attendance->name = $attendance->name . " - " . 
                  $attendance->notes;
         }  
-  if (($attendance->dynsection) && ($course->format =="weeks")) { 
+  if ($attendance->dynsection) { 
            //get info about the course
-               if ($mod->course) {
-                       if (! $course = get_record("course", "id", $mod->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){
+               if (($attendance->section > $course->numsections) || ($attendance->section < 0)){
                        $attendance->section = 0;
                }
-               $attendance->section = "$attendance->section";
+//             $attendance->section = "$attendance->section";
   }    
        // get the data from the attendance grid
   if ($data = data_submitted()) {      
index a4ce37337b6f3ddc184e8682b7b7859dad928a07..10727d17a713643a4bfd971f18bb6e0385f1db68 100755 (executable)
@@ -139,6 +139,9 @@ if ($form->hours >1) {
 <INPUT type="hidden" name=instance      value="<?php p($form->instance) ?>">
 <INPUT type="hidden" name=mode          value="<?php p($form->mode) ?>">
 <BR />
+<?php
+  echo "<a href=\"../mod/attendance/add.php?id=".$form->course . "&section=".$form->section ."\">Add multiple rolls</a><br /><br />";
+?>
 <INPUT type="submit" value="<?php print_string("savechanges") ?>">
 <INPUT type="submit" name="cancel" value="<?php print_string("cancel") ?>">
 </CENTER>