MDL-14945
[moodle-linuxchix.git] / mod / assignment / backuplib.php
blob747df2bb94a39efe9f76445e1fb6d272fcd31166
1 <?php //$Id$
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)
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)
22 //-----------------------------------------------------------
24 //This function executes all the backup procedure about this mod
25 function assignment_backup_mods($bf,$preferences) {
27 global $CFG;
29 $status = true;
31 //Iterate over assignment table
32 $assignments = get_records ("assignment","course",$preferences->backup_course,"id");
33 if ($assignments) {
34 foreach ($assignments as $assignment) {
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.
41 return $status;
44 function assignment_backup_one_mod($bf,$preferences,$assignment) {
46 global $CFG;
48 if (is_numeric($assignment)) {
49 $assignment = get_record('assignment','id',$assignment);
52 $status = true;
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);
84 //End mod
85 $status =fwrite ($bf,end_tag("MOD",3,true));
87 return $status;
90 //Backup assignment_submissions contents (executed from assignment_backup_mods)
91 function backup_assignment_submissions ($bf,$preferences,$assignment) {
93 global $CFG;
95 $status = true;
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));
112 fwrite ($bf,full_tag("DATA1",6,false,$ass_sub->data1));
113 fwrite ($bf,full_tag("DATA2",6,false,$ass_sub->data2));
114 fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
115 fwrite ($bf,full_tag("SUBMISSIONCOMMENT",6,false,$ass_sub->submissioncomment));
116 fwrite ($bf,full_tag("FORMAT",6,false,$ass_sub->format));
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));
123 //Write end tag
124 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
126 return $status;
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) {
133 global $CFG;
135 $status = true;
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");
149 return $status;
153 function backup_assignment_files_instance($bf,$preferences,$instanceid) {
155 global $CFG;
157 $status = true;
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);
172 return $status;
176 //Return an array of info (name,value)
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);
183 return $info;
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;
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;
202 return $info;
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;
217 return $info;
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) {
224 global $CFG;
226 $base = preg_quote($CFG->wwwroot,"/");
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);
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);
236 return $result;
239 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
241 //Returns an array of assignments id
242 function assignment_ids ($course) {
244 global $CFG;
246 return get_records_sql ("SELECT a.id, a.course
247 FROM {$CFG->prefix}assignment a
248 WHERE a.course = '$course'");
251 //Returns an array of assignment_submissions id
252 function assignment_submission_ids_by_course ($course) {
254 global $CFG;
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");
263 //Returns an array of assignment_submissions id
264 function assignment_submission_ids_by_instance ($instanceid) {
266 global $CFG;
268 return get_records_sql ("SELECT s.id , s.assignment
269 FROM {$CFG->prefix}assignment_submissions s
270 WHERE s.assignment = $instanceid");