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