survey MDL-23324 survey form generation was assuming that subquestions would always...
[moodle.git] / mod / assignment / backuplib.php
CommitLineData
e7521559 1<?php
d7c93ec8 2 //This php script contains all the stuff to backup/restore
3 //assignment mods
4
5 //This is the "graphical" structure of the assignment mod:
6 //
7 // assignment
e7521559 8 // (CL,pk->id)
d7c93ec8 9 // |
10 // |
11 // |
e7521559 12 // assignment_submisions
d7c93ec8 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 //-----------------------------------------------------------
23
24 //This function executes all the backup procedure about this mod
25 function assignment_backup_mods($bf,$preferences) {
607d2b2a 26 global $CFG, $DB;
d7c93ec8 27
28 $status = true;
29
30 //Iterate over assignment table
607d2b2a 31 $assignments = $DB->get_records ("assignment", array("course"=>$preferences->backup_course),"id");
d7c93ec8 32 if ($assignments) {
33 foreach ($assignments as $assignment) {
1ecea976 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.
d7c93ec8 37 }
d7c93ec8 38 }
39 }
e7521559 40 return $status;
1ecea976 41 }
42
43 function assignment_backup_one_mod($bf,$preferences,$assignment) {
607d2b2a 44 global $CFG, $DB;
e7521559 45
1ecea976 46 if (is_numeric($assignment)) {
607d2b2a 47 $assignment = $DB->get_record('assignment', array('id'=>$assignment));
1ecea976 48 }
e7521559 49
1ecea976 50 $status = true;
51
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));
c119057a 58 fwrite ($bf,full_tag("DESCRIPTION",4,false,$assignment->intro));
59 fwrite ($bf,full_tag("FORMAT",4,false,$assignment->introformat));
1ecea976 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));
3b804d2d 74
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);
79
1ecea976 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)) {
3b804d2d 83 $status = backup_assignment_submissions($bf,$preferences,$assignment);
1ecea976 84 if ($status) {
85 $status = backup_assignment_files_instance($bf,$preferences,$assignment->id);
d7c93ec8 86 }
87 }
1ecea976 88 //End mod
89 $status =fwrite ($bf,end_tag("MOD",3,true));
90
91 return $status;
d7c93ec8 92 }
93
94 //Backup assignment_submissions contents (executed from assignment_backup_mods)
95 function backup_assignment_submissions ($bf,$preferences,$assignment) {
607d2b2a 96 global $CFG, $DB;
d7c93ec8 97
98 $status = true;
99
3b804d2d 100 $assignment_submissions = $DB->get_records("assignment_submissions", array("assignment"=>$assignment->id),"id");
d7c93ec8 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
e7521559
PS
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));
3b804d2d 123
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);
d7c93ec8 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 }
136
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) {
607d2b2a 140 global $CFG, $DB;
e7521559 141
d7c93ec8 142 $status = true;
143
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 }
155
156 return $status;
157
e7521559 158 }
d7c93ec8 159
1ecea976 160 function backup_assignment_files_instance($bf,$preferences,$instanceid) {
607d2b2a 161 global $CFG, $DB;
e7521559 162
1ecea976 163 $status = true;
164
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 }
177
178 return $status;
179
e7521559 180 }
1ecea976 181
d7c93ec8 182 //Return an array of info (name,value)
1ecea976 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 }
d7c93ec8 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 }
198
199 //Now, if requested, the user_data
200 if ($user_data) {
201 $info[1][0] = get_string("submissions","assignment");
e7521559 202 if ($ids = assignment_submission_ids_by_course ($course)) {
d7c93ec8 203 $info[1][1] = count($ids);
204 } else {
205 $info[1][1] = 0;
206 }
207 }
208 return $info;
209 }
210
1ecea976 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 }
225
25ef0370 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) {
25ef0370 229 global $CFG;
230
231 $base = preg_quote($CFG->wwwroot,"/");
d7c93ec8 232
25ef0370 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);
d7c93ec8 236
25ef0370 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);
d7c93ec8 240
25ef0370 241 return $result;
242 }
d7c93ec8 243
244 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
245
e7521559 246 //Returns an array of assignments id
d7c93ec8 247 function assignment_ids ($course) {
607d2b2a 248 global $CFG, $DB;
d7c93ec8 249
607d2b2a 250 return $DB->get_records_sql ("SELECT a.id, a.course
251 FROM {assignment} a
252 WHERE a.course = ?", array($course));
d7c93ec8 253 }
e7521559 254
d7c93ec8 255 //Returns an array of assignment_submissions id
256 function assignment_submission_ids_by_course ($course) {
607d2b2a 257 global $CFG, $DB;
d7c93ec8 258
607d2b2a 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));
d7c93ec8 264 }
1ecea976 265
266 //Returns an array of assignment_submissions id
267 function assignment_submission_ids_by_instance ($instanceid) {
607d2b2a 268 global $CFG, $DB;
1ecea976 269
607d2b2a 270 return $DB->get_records_sql ("SELECT s.id , s.assignment
271 FROM {assignment_submissions} s
272 WHERE s.assignment = ?", array($instanceid));
1ecea976 273 }
e7521559 274