2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the assignment mod:
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) {
31 //Iterate over assignment table
32 $assignments = get_records ("assignment","course",$preferences->backup_course
,"id");
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.
44 function assignment_backup_one_mod($bf,$preferences,$assignment) {
48 if (is_numeric($assignment)) {
49 $assignment = get_record('assignment','id',$assignment);
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
);
81 $status = backup_assignment_files_instance($bf,$preferences,$assignment->id
);
85 $status =fwrite ($bf,end_tag("MOD",3,true));
90 //Backup assignment_submissions contents (executed from assignment_backup_mods)
91 function backup_assignment_submissions ($bf,$preferences,$assignment) {
97 $assignment_submissions = get_records("assignment_submissions","assignment",$assignment,"id");
98 //If there is submissions
99 if ($assignment_submissions) {
101 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
102 //Iterate over each submission
103 foreach ($assignment_submissions as $ass_sub) {
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
));
121 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
124 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
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) {
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
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");
153 function backup_assignment_files_instance($bf,$preferences,$instanceid) {
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
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);
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)) {
180 foreach ($instances as $id => $instance) {
181 $info +
= assignment_check_backup_mods_instances($instance,$backup_unique_code);
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);
193 //Now, if requested, the 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);
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);
214 $info[$instance->id
.'1'][1] = 0;
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) {
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);
239 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
241 //Returns an array of assignments id
242 function assignment_ids ($course) {
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) {
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) {
268 return get_records_sql ("SELECT s.id , s.assignment
269 FROM {$CFG->prefix}assignment_submissions s
270 WHERE s.assignment = $instanceid");