]> git.mjollnir.org Git - moodle.git/commitdiff
Now attendance module is in backup/restore too !!
authorstronk7 <stronk7>
Thu, 18 Sep 2003 18:41:15 +0000 (18:41 +0000)
committerstronk7 <stronk7>
Thu, 18 Sep 2003 18:41:15 +0000 (18:41 +0000)
Please test.

mod/attendance/backuplib.php [new file with mode: 0644]
mod/attendance/restorelib.php [new file with mode: 0644]

diff --git a/mod/attendance/backuplib.php b/mod/attendance/backuplib.php
new file mode 100644 (file)
index 0000000..3fd1997
--- /dev/null
@@ -0,0 +1,138 @@
+<?PHP //$Id$
+    //This php script contains all the stuff to backup/restore
+    //attendance mods
+
+    //This is the "graphical" structure of the attendance mod:
+    //
+    //                    attendance                                      
+    //                    (CL,pk->id)
+    //                        |
+    //                        |
+    //                        |
+    //                  attendance_roll 
+    //               (UL,pk->id, fk->dayid)     
+    //
+    // Meaning: pk->primary key field of the table
+    //          fk->foreign key to link with parent
+    //          nt->nested field (recursive data)
+    //          CL->course level info
+    //          UL->user level info
+    //          files->table may have files)
+    //
+    //-----------------------------------------------------------
+
+    function attendance_backup_mods($bf,$preferences) {
+        
+        global $CFG;
+
+        $status = true;
+
+        //Iterate over attendance table
+        $attendances = get_records ("attendance","course",$preferences->backup_course,"id");
+        if ($attendances) {
+            foreach ($attendances as $attendance) {
+                //Start mod
+                fwrite ($bf,start_tag("MOD",3,true));
+                //Print choice data
+                fwrite ($bf,full_tag("ID",4,false,$attendance->id));
+                fwrite ($bf,full_tag("MODTYPE",4,false,"attendance"));
+                fwrite ($bf,full_tag("NAME",4,false,$attendance->name));
+                fwrite ($bf,full_tag("DAY",4,false,$attendance->day));
+                fwrite ($bf,full_tag("HOURS",4,false,$attendance->hours));
+                fwrite ($bf,full_tag("ROLL",4,false,$attendance->roll));
+                fwrite ($bf,full_tag("NOTES",4,false,$attendance->notes));
+                fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$attendance->timemodified));
+                fwrite ($bf,full_tag("DYNSECTION",4,false,$attendance->dynsection));
+                //if we've selected to backup users info, then execute backup_attendance_roll
+                if ($preferences->mods["attendance"]->userinfo) {
+                    $status = backup_attendance_roll($bf,$preferences,$attendance->id);
+                }
+                //End mod
+                $status =fwrite ($bf,end_tag("MOD",3,true));
+            }
+        }
+        return $status;
+    }
+
+    //Backup attendance_roll contents (executed from attendance_backup_mods)
+    function backup_attendance_roll ($bf,$preferences,$attendance) {
+
+        global $CFG;
+
+        $status = true;
+
+        $attendance_rolls = get_records("attendance_roll","dayid",$attendance,"id");
+        //If there is rolls
+        if ($attendance_rolls) {
+            //Write start tag
+            $status =fwrite ($bf,start_tag("ROLLS",4,true));
+            //Iterate over each roll
+            foreach ($attendance_rolls as $att_rol) {
+                //Start roll
+                $status =fwrite ($bf,start_tag("ROLL",5,true));
+                //Print roll contents
+                fwrite ($bf,full_tag("ID",6,false,$att_rol->id));
+                fwrite ($bf,full_tag("USERID",6,false,$att_rol->userid));
+                fwrite ($bf,full_tag("HOUR",6,false,$att_rol->hour));
+                fwrite ($bf,full_tag("STATUS",6,false,$att_rol->status));
+                fwrite ($bf,full_tag("NOTES",6,false,$att_rol->notes));
+                //End roll
+                $status =fwrite ($bf,end_tag("ROLL",5,true));
+            }
+            //Write end tag
+            $status =fwrite ($bf,end_tag("ROLLS",4,true));
+        }
+        return $status;
+    }
+   
+   ////Return an array of info (name,value)
+   function attendance_check_backup_mods($course,$user_data=false,$backup_unique_code) {
+        //First the course data
+        $info[0][0] = get_string("modulenameplural","attendance");
+        if ($ids = attendance_ids ($course)) {
+            $info[0][1] = count($ids);
+        } else {
+            $info[0][1] = 0;
+        }
+
+        //Now, if requested, the user_data
+        if ($user_data) {
+            $info[1][0] = get_string("rolls","attendance");
+            if ($ids = attendance_roll_ids_by_course ($course)) {
+                $info[1][1] = count($ids);
+            } else {
+                $info[1][1] = 0;
+            }
+        }
+        return $info;
+    }
+
+
+
+
+
+
+    // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
+
+    //Returns an array of attendances id
+    function attendance_ids ($course) {
+
+        global $CFG;
+
+        return get_records_sql ("SELECT a.id, a.course
+                                 FROM {$CFG->prefix}attendance a
+                                 WHERE a.course = '$course'");
+    }
+   
+    //Returns an array of attendance_rolls id
+    function attendance_roll_ids_by_course ($course) {
+
+        global $CFG;
+
+        return get_records_sql ("SELECT r.id , r.dayid
+                                 FROM {$CFG->prefix}attendance_roll r,
+                                      {$CFG->prefix}attendance a
+                                 WHERE a.course = '$course' AND
+                                       r.dayid = a.id");
+    }
+?>
diff --git a/mod/attendance/restorelib.php b/mod/attendance/restorelib.php
new file mode 100644 (file)
index 0000000..e479e83
--- /dev/null
@@ -0,0 +1,139 @@
+<?PHP //$Id$
+    //This php script contains all the stuff to backup/restore
+    //attendance mods
+
+    //This is the "graphical" structure of the attendance mod:
+    //
+    //                    attendance
+    //                    (CL,pk->id)
+    //                        |
+    //                        |
+    //                        |
+    //                  attendance_roll
+    //               (UL,pk->id, fk->dayid)
+    //
+    // Meaning: pk->primary key field of the table
+    //          fk->foreign key to link with parent
+    //          nt->nested field (recursive data)
+    //          CL->course level info
+    //          UL->user level info
+    //          files->table may have files)
+    //
+    //-----------------------------------------------------------
+
+    //This function executes all the restore procedure about this mod
+    function attendance_restore_mods($mod,$restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        //Get record from backup_ids
+        $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
+
+        if ($data) {
+            //Now get completed xmlized object
+            $info = $data->info;
+            //traverse_xmlize($info);                                                                     //Debug
+            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
+            //$GLOBALS['traverse_array']="";                                                              //Debug
+
+            //Now, build the ATTENDANCE record structure
+            $attendance->course = $restore->course_id;
+            $attendance->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
+            $attendance->day = backup_todb($info['MOD']['#']['DAY']['0']['#']);
+            $attendance->hours = backup_todb($info['MOD']['#']['HOURS']['0']['#']);
+            $attendance->roll = backup_todb($info['MOD']['#']['ROLL']['0']['#']);
+            $attendance->notes = backup_todb($info['MOD']['#']['NOTES']['0']['#']);
+            $attendance->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
+            $attendance->dynsection = backup_todb($info['MOD']['#']['DYNSECTION']['0']['#']);
+
+            //The structure is equal to the db, so insert the attendance
+            $newid = insert_record ("attendance",$attendance);
+
+            //Do some output     
+            echo "<ul><li>".get_string("modulename","attendance")." \"".$attendance->name."\"<br>";
+            backup_flush(300);
+
+            if ($newid) {
+                //We have the newid, update backup_ids
+                backup_putid($restore->backup_unique_code,$mod->modtype,
+                             $mod->id, $newid);
+                //Now check if want to restore user data and do it.
+                if ($restore->mods['attendance']->userinfo) {
+                    //Restore attendance_roll
+                    $status = attendance_roll_restore_mods ($newid,$info,$restore);
+                }
+            } else {
+                $status = false;
+            }
+
+            //Finalize ul        
+            echo "</ul>";
+
+        } else {
+            $status = false;
+        }
+
+        return $status;
+    }
+
+    //This function restores the attendance_roll
+    function attendance_roll_restore_mods($attendance_id,$info,$restore) {
+
+        global $CFG;
+
+        $status = true;
+
+        //Get the rolls array
+        $rolls = $info['MOD']['#']['ROLLS']['0']['#']['ROLL'];
+
+        //Iterate over rolls
+        for($i = 0; $i < sizeof($rolls); $i++) {
+            $rol_info = $rolls[$i];
+            //traverse_xmlize($rol_info);                                                                 //Debug
+            //print_object ($GLOBALS['traverse_array']);                                                  //Debug
+            //$GLOBALS['traverse_array']="";                                                              //Debug
+
+            //We'll need this later!!
+            $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
+            $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
+
+            //Now, build the ATTENDANCE_ROLL record structure
+            $roll->dayid = $attendance_id;
+            $roll->userid = backup_todb($rol_info['#']['USERID']['0']['#']);
+            $roll->hour = backup_todb($rol_info['#']['HOUR']['0']['#']);
+            $roll->status = backup_todb($rol_info['#']['STATUS']['0']['#']);
+            $roll->notes = backup_todb($rol_info['#']['NOTES']['0']['#']);
+
+            //We have to recode the userid field
+            $user = backup_getid($restore->backup_unique_code,"user",$roll->userid);
+            if ($user) {
+                $roll->userid = $user->new_id;
+            }
+
+            //The structure is equal to the db, so insert the attendance_roll
+            $newid = insert_record ("attendance_roll",$roll);
+
+            //Do some output
+            if (($i+1) % 50 == 0) {
+                echo ".";
+                if (($i+1) % 1000 == 0) {
+                    echo "<br>";
+                }
+                backup_flush(300);
+            }
+
+            if ($newid) {
+                //We have the newid, update backup_ids
+                backup_putid($restore->backup_unique_code,"attendance_roll",$oldid,
+                             $newid);
+            } else {
+                $status = false;
+            }
+        }
+
+        return $status;
+    }
+
+?>