survey MDL-23324 survey form generation was assuming that subquestions would always...
[moodle.git] / mod / assignment / backuplib.php
1 <?php
2     //This php script contains all the stuff to backup/restore
3     //assignment mods
5     //This is the "graphical" structure of the assignment mod:
6     //
7     //                     assignment
8     //                    (CL,pk->id)
9     //                        |
10     //                        |
11     //                        |
12     //                 assignment_submisions
13     //           (UL,pk->id, fk->assignment,files)
14     //
15     // Meaning: pk->primary key field of the table
16     //          fk->foreign key to link with parent
17     //          nt->nested field (recursive data)
18     //          CL->course level info
19     //          UL->user level info
20     //          files->table may have files)
21     //
22     //-----------------------------------------------------------
24     //This function executes all the backup procedure about this mod
25     function assignment_backup_mods($bf,$preferences) {
26         global $CFG, $DB;
28         $status = true;
30         //Iterate over assignment table
31         $assignments = $DB->get_records ("assignment", array("course"=>$preferences->backup_course),"id");
32         if ($assignments) {
33             foreach ($assignments as $assignment) {
34                 if (backup_mod_selected($preferences,'assignment',$assignment->id)) {
35                     $status = assignment_backup_one_mod($bf,$preferences,$assignment);
36                     // backup files happens in backup_one_mod now too.
37                 }
38             }
39         }
40         return $status;
41     }
43     function assignment_backup_one_mod($bf,$preferences,$assignment) {
44         global $CFG, $DB;
46         if (is_numeric($assignment)) {
47             $assignment = $DB->get_record('assignment', array('id'=>$assignment));
48         }
50         $status = true;
52         //Start mod
53         fwrite ($bf,start_tag("MOD",3,true));
54         //Print assignment data
55         fwrite ($bf,full_tag("ID",4,false,$assignment->id));
56         fwrite ($bf,full_tag("MODTYPE",4,false,"assignment"));
57         fwrite ($bf,full_tag("NAME",4,false,$assignment->name));
58         fwrite ($bf,full_tag("DESCRIPTION",4,false,$assignment->intro));
59         fwrite ($bf,full_tag("FORMAT",4,false,$assignment->introformat));
60         fwrite ($bf,full_tag("RESUBMIT",4,false,$assignment->resubmit));
61         fwrite ($bf,full_tag("PREVENTLATE",4,false,$assignment->preventlate));
62         fwrite ($bf,full_tag("EMAILTEACHERS",4,false,$assignment->emailteachers));
63         fwrite ($bf,full_tag("VAR1",4,false,$assignment->var1));
64         fwrite ($bf,full_tag("VAR2",4,false,$assignment->var2));
65         fwrite ($bf,full_tag("VAR3",4,false,$assignment->var3));
66         fwrite ($bf,full_tag("VAR4",4,false,$assignment->var4));
67         fwrite ($bf,full_tag("VAR5",4,false,$assignment->var5));
68         fwrite ($bf,full_tag("ASSIGNMENTTYPE",4,false,$assignment->assignmenttype));
69         fwrite ($bf,full_tag("MAXBYTES",4,false,$assignment->maxbytes));
70         fwrite ($bf,full_tag("TIMEDUE",4,false,$assignment->timedue));
71         fwrite ($bf,full_tag("TIMEAVAILABLE",4,false,$assignment->timeavailable));
72         fwrite ($bf,full_tag("GRADE",4,false,$assignment->grade));
73         fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$assignment->timemodified));
75         $class = 'assignment_' . $assignment->assignmenttype;
76         require_once($CFG->dirroot . '/mod/assignment/lib.php');
77         require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
78         call_user_func(array($class, 'backup_one_mod'), $bf, $preferences, $assignment);
80         //if we've selected to backup users info, then execute backup_assignment_submisions and
81         //backup_assignment_files_instance
82         if (backup_userdata_selected($preferences,'assignment',$assignment->id)) {
83             $status = backup_assignment_submissions($bf,$preferences,$assignment);
84             if ($status) {
85                 $status = backup_assignment_files_instance($bf,$preferences,$assignment->id);
86             }
87         }
88         //End mod
89         $status =fwrite ($bf,end_tag("MOD",3,true));
91         return $status;
92     }
94     //Backup assignment_submissions contents (executed from assignment_backup_mods)
95     function backup_assignment_submissions ($bf,$preferences,$assignment) {
96         global $CFG, $DB;
98         $status = true;
100         $assignment_submissions = $DB->get_records("assignment_submissions", array("assignment"=>$assignment->id),"id");
101         //If there is submissions
102         if ($assignment_submissions) {
103             //Write start tag
104             $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
105             //Iterate over each submission
106             foreach ($assignment_submissions as $ass_sub) {
107                 //Start submission
108                 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
109                 //Print submission contents
110                 fwrite ($bf,full_tag("ID",6,false,$ass_sub->id));
111                 fwrite ($bf,full_tag("USERID",6,false,$ass_sub->userid));
112                 fwrite ($bf,full_tag("TIMECREATED",6,false,$ass_sub->timecreated));
113                 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$ass_sub->timemodified));
114                 fwrite ($bf,full_tag("NUMFILES",6,false,$ass_sub->numfiles));
115                 fwrite ($bf,full_tag("DATA1",6,false,$ass_sub->data1));
116                 fwrite ($bf,full_tag("DATA2",6,false,$ass_sub->data2));
117                 fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
118                 fwrite ($bf,full_tag("SUBMISSIONCOMMENT",6,false,$ass_sub->submissioncomment));
119                 fwrite ($bf,full_tag("FORMAT",6,false,$ass_sub->format));
120                 fwrite ($bf,full_tag("TEACHER",6,false,$ass_sub->teacher));
121                 fwrite ($bf,full_tag("TIMEMARKED",6,false,$ass_sub->timemarked));
122                 fwrite ($bf,full_tag("MAILED",6,false,$ass_sub->mailed));
124                 $class = 'assignment_' . $assignment->assignmenttype;
125                 require_once($CFG->dirroot . '/mod/assignment/lib.php');
126                 require_once($CFG->dirroot . '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php');
127                 call_user_func(array($class, 'backup_one_submission'), $bf, $preferences, $assignment, $ass_sub);
128                 //End submission
129                 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
130             }
131             //Write end tag
132             $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
133         }
134         return $status;
135     }
137     //Backup assignment files because we've selected to backup user info
138     //and files are user info's level
139     function backup_assignment_files($bf,$preferences) {
140         global $CFG, $DB;
142         $status = true;
144         //First we check to moddata exists and create it as necessary
145         //in temp/backup/$backup_code  dir
146         $status = check_and_create_moddata_dir($preferences->backup_unique_code);
147         //Now copy the assignment dir
148         if ($status) {
149             //Only if it exists !! Thanks to Daniel Miksik.
150             if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment")) {
151                 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment",
152                                            $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment");
153             }
154         }
156         return $status;
158     }
160     function backup_assignment_files_instance($bf,$preferences,$instanceid) {
161         global $CFG, $DB;
163         $status = true;
165         //First we check to moddata exists and create it as necessary
166         //in temp/backup/$backup_code  dir
167         $status = check_and_create_moddata_dir($preferences->backup_unique_code);
168         $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/",true);
169         //Now copy the assignment dir
170         if ($status) {
171             //Only if it exists !! Thanks to Daniel Miksik.
172             if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid)) {
173                 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid,
174                                            $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/".$instanceid);
175             }
176         }
178         return $status;
180     }
182     //Return an array of info (name,value)
183     function assignment_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
184         if (!empty($instances) && is_array($instances) && count($instances)) {
185             $info = array();
186             foreach ($instances as $id => $instance) {
187                 $info += assignment_check_backup_mods_instances($instance,$backup_unique_code);
188             }
189             return $info;
190         }
191         //First the course data
192         $info[0][0] = get_string("modulenameplural","assignment");
193         if ($ids = assignment_ids ($course)) {
194             $info[0][1] = count($ids);
195         } else {
196             $info[0][1] = 0;
197         }
199         //Now, if requested, the user_data
200         if ($user_data) {
201             $info[1][0] = get_string("submissions","assignment");
202             if ($ids = assignment_submission_ids_by_course ($course)) {
203                 $info[1][1] = count($ids);
204             } else {
205                 $info[1][1] = 0;
206             }
207         }
208         return $info;
209     }
211     //Return an array of info (name,value)
212     function assignment_check_backup_mods_instances($instance,$backup_unique_code) {
213         $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
214         $info[$instance->id.'0'][1] = '';
215         if (!empty($instance->userdata)) {
216             $info[$instance->id.'1'][0] = get_string("submissions","assignment");
217             if ($ids = assignment_submission_ids_by_instance ($instance->id)) {
218                 $info[$instance->id.'1'][1] = count($ids);
219             } else {
220                 $info[$instance->id.'1'][1] = 0;
221             }
222         }
223         return $info;
224     }
226     //Return a content encoded to support interactivities linking. Every module
227     //should have its own. They are called automatically from the backup procedure.
228     function assignment_encode_content_links ($content,$preferences) {
229         global $CFG;
231         $base = preg_quote($CFG->wwwroot,"/");
233         //Link to the list of assignments
234         $buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
235         $result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content);
237         //Link to assignment view by moduleid
238         $buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
239         $result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result);
241         return $result;
242     }
244     // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
246     //Returns an array of assignments id
247     function assignment_ids ($course) {
248         global $CFG, $DB;
250         return $DB->get_records_sql ("SELECT a.id, a.course
251                                         FROM {assignment} a
252                                        WHERE a.course = ?", array($course));
253     }
255     //Returns an array of assignment_submissions id
256     function assignment_submission_ids_by_course ($course) {
257         global $CFG, $DB;
259         return $DB->get_records_sql ("SELECT s.id , s.assignment
260                                         FROM {assignment_submissions} s,
261                                              {assignment} a
262                                        WHERE a.course = ? AND
263                                              s.assignment = a.id", array($course));
264     }
266     //Returns an array of assignment_submissions id
267     function assignment_submission_ids_by_instance ($instanceid) {
268         global $CFG, $DB;
270         return $DB->get_records_sql ("SELECT s.id , s.assignment
271                                         FROM {assignment_submissions} s
272                                        WHERE s.assignment = ?", array($instanceid));
273     }