Fixing up a problem in chat login - ip address is lastip not lastIP - member variable...
[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) {
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));
4b46c803 44 fwrite ($bf,full_tag("PREVENTLATE",4,false,$assignment->preventlate));
626138c9 45 fwrite ($bf,full_tag("EMAILTEACHERS",4,false,$assignment->emailteachers));
73097f07 46 fwrite ($bf,full_tag("VAR1",4,false,$assignment->var1));
47 fwrite ($bf,full_tag("VAR2",4,false,$assignment->var2));
48 fwrite ($bf,full_tag("VAR3",4,false,$assignment->var3));
49 fwrite ($bf,full_tag("VAR4",4,false,$assignment->var4));
50 fwrite ($bf,full_tag("VAR5",4,false,$assignment->var5));
b0f2597e 51 fwrite ($bf,full_tag("ASSIGNMENTTYPE",4,false,$assignment->assignmenttype));
d7c93ec8 52 fwrite ($bf,full_tag("MAXBYTES",4,false,$assignment->maxbytes));
53 fwrite ($bf,full_tag("TIMEDUE",4,false,$assignment->timedue));
9b6618b9 54 fwrite ($bf,full_tag("TIMEAVAILABLE",4,false,$assignment->timeavailable));
d7c93ec8 55 fwrite ($bf,full_tag("GRADE",4,false,$assignment->grade));
56 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$assignment->timemodified));
57 //if we've selected to backup users info, then execute backup_assignment_submisions
58 if ($preferences->mods["assignment"]->userinfo) {
59 $status = backup_assignment_submissions($bf,$preferences,$assignment->id);
60 }
61 //End mod
62 $status =fwrite ($bf,end_tag("MOD",3,true));
63 }
64 }
65 //if we've selected to backup users info, then backup files too
66 if ($status) {
67 if ($preferences->mods["assignment"]->userinfo) {
68 $status = backup_assignment_files($bf,$preferences);
69 }
70 }
71 return $status;
72 }
73
74 //Backup assignment_submissions contents (executed from assignment_backup_mods)
75 function backup_assignment_submissions ($bf,$preferences,$assignment) {
76
77 global $CFG;
78
79 $status = true;
80
81 $assignment_submissions = get_records("assignment_submissions","assignment",$assignment,"id");
82 //If there is submissions
83 if ($assignment_submissions) {
84 //Write start tag
85 $status =fwrite ($bf,start_tag("SUBMISSIONS",4,true));
86 //Iterate over each submission
87 foreach ($assignment_submissions as $ass_sub) {
88 //Start submission
89 $status =fwrite ($bf,start_tag("SUBMISSION",5,true));
90 //Print submission contents
91 fwrite ($bf,full_tag("ID",6,false,$ass_sub->id));
92 fwrite ($bf,full_tag("USERID",6,false,$ass_sub->userid));
93 fwrite ($bf,full_tag("TIMECREATED",6,false,$ass_sub->timecreated));
94 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$ass_sub->timemodified));
95 fwrite ($bf,full_tag("NUMFILES",6,false,$ass_sub->numfiles));
72778c9a 96 fwrite ($bf,full_tag("DATA1",6,false,$ass_sub->data1));
97 fwrite ($bf,full_tag("DATA2",6,false,$ass_sub->data2));
d7c93ec8 98 fwrite ($bf,full_tag("GRADE",6,false,$ass_sub->grade));
99 fwrite ($bf,full_tag("COMMENT",6,false,$ass_sub->comment));
52436fe1 100 fwrite ($bf,full_tag("FORMAT",6,false,$ass_sub->format));
d7c93ec8 101 fwrite ($bf,full_tag("TEACHER",6,false,$ass_sub->teacher));
102 fwrite ($bf,full_tag("TIMEMARKED",6,false,$ass_sub->timemarked));
103 fwrite ($bf,full_tag("MAILED",6,false,$ass_sub->mailed));
104 //End submission
105 $status =fwrite ($bf,end_tag("SUBMISSION",5,true));
106 }
107 //Write end tag
108 $status =fwrite ($bf,end_tag("SUBMISSIONS",4,true));
109 }
110 return $status;
111 }
112
113 //Backup assignment files because we've selected to backup user info
114 //and files are user info's level
115 function backup_assignment_files($bf,$preferences) {
116
117 global $CFG;
118
119 $status = true;
120
121 //First we check to moddata exists and create it as necessary
122 //in temp/backup/$backup_code dir
123 $status = check_and_create_moddata_dir($preferences->backup_unique_code);
124 //Now copy the assignment dir
125 if ($status) {
126 //Only if it exists !! Thanks to Daniel Miksik.
127 if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment")) {
128 $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/assignment",
129 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/assignment");
130 }
131 }
132
133 return $status;
134
135 }
136
137 //Return an array of info (name,value)
138 function assignment_check_backup_mods($course,$user_data=false,$backup_unique_code) {
139 //First the course data
140 $info[0][0] = get_string("modulenameplural","assignment");
141 if ($ids = assignment_ids ($course)) {
142 $info[0][1] = count($ids);
143 } else {
144 $info[0][1] = 0;
145 }
146
147 //Now, if requested, the user_data
148 if ($user_data) {
149 $info[1][0] = get_string("submissions","assignment");
150 if ($ids = assignment_submission_ids_by_course ($course)) {
151 $info[1][1] = count($ids);
152 } else {
153 $info[1][1] = 0;
154 }
155 }
156 return $info;
157 }
158
25ef0370 159 //Return a content encoded to support interactivities linking. Every module
160 //should have its own. They are called automatically from the backup procedure.
161 function assignment_encode_content_links ($content,$preferences) {
d7c93ec8 162
25ef0370 163 global $CFG;
164
165 $base = preg_quote($CFG->wwwroot,"/");
d7c93ec8 166
25ef0370 167 //Link to the list of assignments
168 $buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
169 $result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content);
d7c93ec8 170
25ef0370 171 //Link to assignment view by moduleid
172 $buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
173 $result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result);
d7c93ec8 174
25ef0370 175 return $result;
176 }
d7c93ec8 177
178 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
179
180 //Returns an array of assignments id
181 function assignment_ids ($course) {
182
183 global $CFG;
184
185 return get_records_sql ("SELECT a.id, a.course
186 FROM {$CFG->prefix}assignment a
187 WHERE a.course = '$course'");
188 }
189
190 //Returns an array of assignment_submissions id
191 function assignment_submission_ids_by_course ($course) {
192
193 global $CFG;
194
195 return get_records_sql ("SELECT s.id , s.assignment
196 FROM {$CFG->prefix}assignment_submissions s,
197 {$CFG->prefix}assignment a
198 WHERE a.course = '$course' AND
199 s.assignment = a.id");
200 }
201?>