MDL-19756 Showing exceptions only in DEBUG_DEVELOPER mode
[moodle.git] / mod / assignment / backuplib.php
CommitLineData
9a58f7cb 1<?php //$Id$
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
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 //-----------------------------------------------------------
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 }
1ecea976 40 return $status;
41 }
42
43 function assignment_backup_one_mod($bf,$preferences,$assignment) {
607d2b2a 44 global $CFG, $DB;
1ecea976 45
46 if (is_numeric($assignment)) {
607d2b2a 47 $assignment = $DB->get_record('assignment', array('id'=>$assignment));
1ecea976 48 }
49
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));
74 //if we've selected to backup users info, then execute backup_assignment_submisions and
75 //backup_assignment_files_instance
76 if (backup_userdata_selected($preferences,'assignment',$assignment->id)) {
69547cc5 77 $status = backup_assignment_submissions($bf,$preferences,$assignment->id);
1ecea976 78 if ($status) {
79 $status = backup_assignment_files_instance($bf,$preferences,$assignment->id);
d7c93ec8 80 }
81 }
1ecea976 82 //End mod
83 $status =fwrite ($bf,end_tag("MOD",3,true));
84
85 return $status;
d7c93ec8 86 }
87
88 //Backup assignment_submissions contents (executed from assignment_backup_mods)
89 function backup_assignment_submissions ($bf,$preferences,$assignment) {
607d2b2a 90 global $CFG, $DB;
d7c93ec8 91
92 $status = true;
93
69547cc5 94 $assignment_submissions = $DB->get_records("assignment_submissions", array("assignment"=>$assignment),"id");
d7c93ec8 95 //If there is submissions
96 if ($assignment_submissions) {
97 //Write start tag
98 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
99 //Iterate over each submission
100 foreach ($assignment_submissions as $ass_sub) {
101 //Start submission
102 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
103 //Print submission contents
104 fwrite ($bf,full_tag("ID",6,false,$ass_sub->id));
105 fwrite ($bf,full_tag("USERID",6,false,$ass_sub->userid));
106 fwrite ($bf,full_tag("TIMECREATED",6,false,$ass_sub->timecreated));
107 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$ass_sub->timemodified));
108 fwrite ($bf,full_tag("NUMFILES",6,false,$ass_sub->numfiles));
72778c9a 109 fwrite ($bf,full_tag("DATA1",6,false,$ass_sub->data1));
110 fwrite ($bf,full_tag("DATA2",6,false,$ass_sub->data2));
d7c93ec8 111 fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
ea6432fe 112 fwrite ($bf,full_tag("SUBMISSIONCOMMENT",6,false,$ass_sub->submissioncomment));
52436fe1 113 fwrite ($bf,full_tag("FORMAT",6,false,$ass_sub->format));
d7c93ec8 114 fwrite ($bf,full_tag("TEACHER",6,false,$ass_sub->teacher));
115 fwrite ($bf,full_tag("TIMEMARKED",6,false,$ass_sub->timemarked));
116 fwrite ($bf,full_tag("MAILED",6,false,$ass_sub->mailed));
117 //End submission
118 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
119 }
120 //Write end tag
121 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
122 }
123 return $status;
124 }
125
126 //Backup assignment files because we've selected to backup user info
127 //and files are user info's level
128 function backup_assignment_files($bf,$preferences) {
607d2b2a 129 global $CFG, $DB;
d7c93ec8 130
131 $status = true;
132
133 //First we check to moddata exists and create it as necessary
134 //in temp/backup/$backup_code dir
135 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
136 //Now copy the assignment dir
137 if ($status) {
138 //Only if it exists !! Thanks to Daniel Miksik.
139 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment")) {
140 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment",
141 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment");
142 }
143 }
144
145 return $status;
146
147 }
148
1ecea976 149 function backup_assignment_files_instance($bf,$preferences,$instanceid) {
607d2b2a 150 global $CFG, $DB;
1ecea976 151
152 $status = true;
153
154 //First we check to moddata exists and create it as necessary
155 //in temp/backup/$backup_code dir
156 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
157 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/",true);
158 //Now copy the assignment dir
159 if ($status) {
160 //Only if it exists !! Thanks to Daniel Miksik.
161 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid)) {
162 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment/".$instanceid,
163 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment/".$instanceid);
164 }
165 }
166
167 return $status;
168
169 }
170
d7c93ec8 171 //Return an array of info (name,value)
1ecea976 172 function assignment_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
173 if (!empty($instances) && is_array($instances) && count($instances)) {
174 $info = array();
175 foreach ($instances as $id => $instance) {
176 $info += assignment_check_backup_mods_instances($instance,$backup_unique_code);
177 }
178 return $info;
179 }
d7c93ec8 180 //First the course data
181 $info[0][0] = get_string("modulenameplural","assignment");
182 if ($ids = assignment_ids ($course)) {
183 $info[0][1] = count($ids);
184 } else {
185 $info[0][1] = 0;
186 }
187
188 //Now, if requested, the user_data
189 if ($user_data) {
190 $info[1][0] = get_string("submissions","assignment");
191 if ($ids = assignment_submission_ids_by_course ($course)) {
192 $info[1][1] = count($ids);
193 } else {
194 $info[1][1] = 0;
195 }
196 }
197 return $info;
198 }
199
1ecea976 200 //Return an array of info (name,value)
201 function assignment_check_backup_mods_instances($instance,$backup_unique_code) {
202 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
203 $info[$instance->id.'0'][1] = '';
204 if (!empty($instance->userdata)) {
205 $info[$instance->id.'1'][0] = get_string("submissions","assignment");
206 if ($ids = assignment_submission_ids_by_instance ($instance->id)) {
207 $info[$instance->id.'1'][1] = count($ids);
208 } else {
209 $info[$instance->id.'1'][1] = 0;
210 }
211 }
212 return $info;
213 }
214
25ef0370 215 //Return a content encoded to support interactivities linking. Every module
216 //should have its own. They are called automatically from the backup procedure.
217 function assignment_encode_content_links ($content,$preferences) {
25ef0370 218 global $CFG;
219
220 $base = preg_quote($CFG->wwwroot,"/");
d7c93ec8 221
25ef0370 222 //Link to the list of assignments
223 $buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
224 $result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content);
d7c93ec8 225
25ef0370 226 //Link to assignment view by moduleid
227 $buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
228 $result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result);
d7c93ec8 229
25ef0370 230 return $result;
231 }
d7c93ec8 232
233 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
234
235 //Returns an array of assignments id
236 function assignment_ids ($course) {
607d2b2a 237 global $CFG, $DB;
d7c93ec8 238
607d2b2a 239 return $DB->get_records_sql ("SELECT a.id, a.course
240 FROM {assignment} a
241 WHERE a.course = ?", array($course));
d7c93ec8 242 }
243
244 //Returns an array of assignment_submissions id
245 function assignment_submission_ids_by_course ($course) {
607d2b2a 246 global $CFG, $DB;
d7c93ec8 247
607d2b2a 248 return $DB->get_records_sql ("SELECT s.id , s.assignment
249 FROM {assignment_submissions} s,
250 {assignment} a
251 WHERE a.course = ? AND
252 s.assignment = a.id", array($course));
d7c93ec8 253 }
1ecea976 254
255 //Returns an array of assignment_submissions id
256 function assignment_submission_ids_by_instance ($instanceid) {
607d2b2a 257 global $CFG, $DB;
1ecea976 258
607d2b2a 259 return $DB->get_records_sql ("SELECT s.id , s.assignment
260 FROM {assignment_submissions} s
261 WHERE s.assignment = ?", array($instanceid));
1ecea976 262 }
d7c93ec8 263?>