Moving the backup libraries for each module into their new homes.
[moodle.git] / mod / assignment / backuplib.php
CommitLineData
d7c93ec8 1<?PHP //$Id$
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) {
35 //Start mod
36 fwrite ($bf,start_tag("MOD",3,true));
37 //Print assignment data
38 fwrite ($bf,full_tag("ID",4,false,$assignment->id));
39 fwrite ($bf,full_tag("MODTYPE",4,false,"assignment"));
40 fwrite ($bf,full_tag("NAME",4,false,$assignment->name));
41 fwrite ($bf,full_tag("DESCRIPTION",4,false,$assignment->description));
42 fwrite ($bf,full_tag("FORMAT",4,false,$assignment->format));
43 fwrite ($bf,full_tag("RESUBMIT",4,false,$assignment->resubmit));
44 fwrite ($bf,full_tag("TYPE",4,false,$assignment->type));
45 fwrite ($bf,full_tag("MAXBYTES",4,false,$assignment->maxbytes));
46 fwrite ($bf,full_tag("TIMEDUE",4,false,$assignment->timedue));
47 fwrite ($bf,full_tag("GRADE",4,false,$assignment->grade));
48 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$assignment->timemodified));
49 //if we've selected to backup users info, then execute backup_assignment_submisions
50 if ($preferences->mods["assignment"]->userinfo) {
51 $status = backup_assignment_submissions($bf,$preferences,$assignment->id);
52 }
53 //End mod
54 $status =fwrite ($bf,end_tag("MOD",3,true));
55 }
56 }
57 //if we've selected to backup users info, then backup files too
58 if ($status) {
59 if ($preferences->mods["assignment"]->userinfo) {
60 $status = backup_assignment_files($bf,$preferences);
61 }
62 }
63 return $status;
64 }
65
66 //Backup assignment_submissions contents (executed from assignment_backup_mods)
67 function backup_assignment_submissions ($bf,$preferences,$assignment) {
68
69 global $CFG;
70
71 $status = true;
72
73 $assignment_submissions = get_records("assignment_submissions","assignment",$assignment,"id");
74 //If there is submissions
75 if ($assignment_submissions) {
76 //Write start tag
77 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
78 //Iterate over each submission
79 foreach ($assignment_submissions as $ass_sub) {
80 //Start submission
81 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
82 //Print submission contents
83 fwrite ($bf,full_tag("ID",6,false,$ass_sub->id));
84 fwrite ($bf,full_tag("USERID",6,false,$ass_sub->userid));
85 fwrite ($bf,full_tag("TIMECREATED",6,false,$ass_sub->timecreated));
86 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$ass_sub->timemodified));
87 fwrite ($bf,full_tag("NUMFILES",6,false,$ass_sub->numfiles));
88 fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
89 fwrite ($bf,full_tag("COMMENT",6,false,$ass_sub->comment));
90 fwrite ($bf,full_tag("TEACHER",6,false,$ass_sub->teacher));
91 fwrite ($bf,full_tag("TIMEMARKED",6,false,$ass_sub->timemarked));
92 fwrite ($bf,full_tag("MAILED",6,false,$ass_sub->mailed));
93 //End submission
94 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
95 }
96 //Write end tag
97 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
98 }
99 return $status;
100 }
101
102 //Backup assignment files because we've selected to backup user info
103 //and files are user info's level
104 function backup_assignment_files($bf,$preferences) {
105
106 global $CFG;
107
108 $status = true;
109
110 //First we check to moddata exists and create it as necessary
111 //in temp/backup/$backup_code dir
112 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
113 //Now copy the assignment dir
114 if ($status) {
115 //Only if it exists !! Thanks to Daniel Miksik.
116 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment")) {
117 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment",
118 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment");
119 }
120 }
121
122 return $status;
123
124 }
125
126 //Return an array of info (name,value)
127 function assignment_check_backup_mods($course,$user_data=false,$backup_unique_code) {
128 //First the course data
129 $info[0][0] = get_string("modulenameplural","assignment");
130 if ($ids = assignment_ids ($course)) {
131 $info[0][1] = count($ids);
132 } else {
133 $info[0][1] = 0;
134 }
135
136 //Now, if requested, the user_data
137 if ($user_data) {
138 $info[1][0] = get_string("submissions","assignment");
139 if ($ids = assignment_submission_ids_by_course ($course)) {
140 $info[1][1] = count($ids);
141 } else {
142 $info[1][1] = 0;
143 }
144 }
145 return $info;
146 }
147
148
149
150
151
152
153 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
154
155 //Returns an array of assignments id
156 function assignment_ids ($course) {
157
158 global $CFG;
159
160 return get_records_sql ("SELECT a.id, a.course
161 FROM {$CFG->prefix}assignment a
162 WHERE a.course = '$course'");
163 }
164
165 //Returns an array of assignment_submissions id
166 function assignment_submission_ids_by_course ($course) {
167
168 global $CFG;
169
170 return get_records_sql ("SELECT s.id , s.assignment
171 FROM {$CFG->prefix}assignment_submissions s,
172 {$CFG->prefix}assignment a
173 WHERE a.course = '$course' AND
174 s.assignment = a.id");
175 }
176?>