weekly release 2.2.9+
[moodle.git] / backup / backuplib.php
blobb79646fa8e3d1496870edf324ba81b9c3b3a370d
1 <?php
2 //Prints course's messages info (tables message, message_read and message_contacts)
3 function backup_messages ($bf,$preferences) {
4 global $CFG, $DB;
6 $status = true;
8 /// Check we have something to backup
9 $unreads = $DB->count_records ('message');
10 $reads = $DB->count_records ('message_read');
11 $contacts= $DB->count_records ('message_contacts');
13 if ($unreads || $reads || $contacts) {
14 $counter = 0;
15 /// message open tag
16 fwrite ($bf,start_tag("MESSAGES",2,true));
18 if ($unreads) {
19 $rs_unreads = $DB->get_recordset('message');
20 /// Iterate over every unread
21 foreach ($rs_unreads as $unread) {
22 /// start message
23 fwrite($bf, start_tag("MESSAGE",3,true));
24 fwrite ($bf,full_tag("ID",4,false,$unread->id));
25 fwrite ($bf,full_tag("STATUS",4,false,"UNREAD"));
26 fwrite ($bf,full_tag("USERIDFROM",4,false,$unread->useridfrom));
27 fwrite ($bf,full_tag("USERIDTO",4,false,$unread->useridto));
28 fwrite ($bf,full_tag("MESSAGE",4,false,$unread->message));
29 fwrite ($bf,full_tag("FORMAT",4,false,$unread->format));
30 fwrite ($bf,full_tag("TIMECREATED",4,false,$unread->timecreated));
31 fwrite ($bf,full_tag("MESSAGETYPE",4,false,$unread->messagetype));
32 /// end message
33 fwrite ($bf,end_tag("MESSAGE",3,true));
35 /// Do some output
36 $counter++;
37 if ($counter % 20 == 0) {
38 echo ".";
39 if ($counter % 400 == 0) {
40 echo "<br />";
42 backup_flush(300);
45 $rs_unreads->close();
48 if ($reads) {
49 $rs_reads = $DB->get_recordset('message_read');
50 /// Iterate over every unread
51 foreach ($rs_reads as $read) {
52 /// start message
53 fwrite($bf, start_tag("MESSAGE",3,true));
54 fwrite ($bf,full_tag("ID",4,false,$read->id));
55 fwrite ($bf,full_tag("STATUS",4,false,"READ"));
56 fwrite ($bf,full_tag("USERIDFROM",4,false,$read->useridfrom));
57 fwrite ($bf,full_tag("USERIDTO",4,false,$read->useridto));
58 fwrite ($bf,full_tag("MESSAGE",4,false,$read->message));
59 fwrite ($bf,full_tag("FORMAT",4,false,$read->format));
60 fwrite ($bf,full_tag("TIMECREATED",4,false,$read->timecreated));
61 fwrite ($bf,full_tag("MESSAGETYPE",4,false,$read->messagetype));
62 fwrite ($bf,full_tag("TIMEREAD",4,false,$read->timeread));
63 fwrite ($bf,full_tag("MAILED",4,false,$read->mailed));
64 /// end message
65 fwrite ($bf,end_tag("MESSAGE",3,true));
67 /// Do some output
68 $counter++;
69 if ($counter % 20 == 0) {
70 echo ".";
71 if ($counter % 400 == 0) {
72 echo "<br />";
74 backup_flush(300);
77 $rs_reads->close();
80 if ($contacts) {
81 fwrite($bf, start_tag("CONTACTS",3,true));
82 $rs_contacts = $DB->get_recordset('message_contacts');
83 /// Iterate over every contact
84 foreach ($rs_contacts as $contact) {
85 /// start contact
86 fwrite($bf, start_tag("CONTACT",4,true));
87 fwrite ($bf,full_tag("ID",5,false,$contact->id));
88 fwrite ($bf,full_tag("USERID",5,false,$contact->userid));
89 fwrite ($bf,full_tag("CONTACTID",5,false,$contact->contactid));
90 fwrite ($bf,full_tag("BLOCKED",5,false,$contact->blocked));
91 /// end contact
92 fwrite ($bf,end_tag("CONTACT",4,true));
94 /// Do some output
95 $counter++;
96 if ($counter % 20 == 0) {
97 echo ".";
98 if ($counter % 400 == 0) {
99 echo "<br />";
101 backup_flush(300);
104 $rs_contacts->close();
105 fwrite($bf, end_tag("CONTACTS",3,true));
108 /// messages close tag
109 $status = fwrite ($bf,end_tag("MESSAGES",2,true));
112 return $status;
116 //Print blogs info (post table, module=blog, course=0)
117 function backup_blogs($bf, $preferences) {
118 global $CFG, $DB;
120 $status = true;
122 /// Check we have something to backup
123 $siteblogs = $DB->count_records('post', array('module'=>'blog', 'courseid'=>0));
125 if ($siteblogs) {
126 $counter = 0;
127 /// blogs open tag
128 fwrite ($bf, start_tag("BLOGS",2,true));
130 if ($siteblogs) {
131 $rs_blogs = $DB->get_records('post', array('module'=>'blog', 'courseid'=>0));
132 /// Iterate over every blog
133 foreach ($rs_blogs as $blog) {
134 backup_blog($bf, $blog->id, 3);
136 /// Do some output
137 $counter++;
138 if ($counter % 20 == 0) {
139 echo ".";
140 if ($counter % 400 == 0) {
141 echo "<br />";
143 backup_flush(300);
146 $rs_blogs-close();
148 /// blogs close tag
149 $status = fwrite($bf, end_tag("BLOGS",2,true));
152 return $status;
156 function backup_blog($bf, $blogid, $level) {
157 global $DB;
158 $blog = $DB->get_record('post', array('module'=>'blog', 'id'=>$blogid));
160 /// start blog
161 fwrite($bf, start_tag("BLOG",$level,true));
162 /// blog body
163 fwrite ($bf,full_tag("ID",$level+1,false,$blog->id));
164 fwrite ($bf,full_tag("MODULE",$level+1,false,$blog->module));
165 fwrite ($bf,full_tag("USERID",$level+1,false,$blog->userid));
166 fwrite ($bf,full_tag("COURSEID",$level+1,false,$blog->courseid));
167 fwrite ($bf,full_tag("GROUPID",$level+1,false,$blog->groupid));
168 fwrite ($bf,full_tag("MODULEID",$level+1,false,$blog->moduleid));
169 fwrite ($bf,full_tag("COURSEMODULEID",$level+1,false,$blog->coursemoduleid));
170 fwrite ($bf,full_tag("SUBJECT",$level+1,false,$blog->subject));
171 fwrite ($bf,full_tag("SUMMARY",$level+1,false,$blog->summary));
172 fwrite ($bf,full_tag("CONTENT",$level+1,false,$blog->content));
173 fwrite ($bf,full_tag("UNIQUEHASH",$level+1,false,$blog->uniquehash));
174 fwrite ($bf,full_tag("RATING",$level+1,false,$blog->rating));
175 fwrite ($bf,full_tag("FORMAT",$level+1,false,$blog->format));
176 fwrite ($bf,full_tag("ATTACHMENT",$level+1,false,$blog->attachment));
177 fwrite ($bf,full_tag("PUBLISHSTATE",$level+1,false,$blog->publishstate));
178 fwrite ($bf,full_tag("LASTMODIFIED",$level+1,false,$blog->lastmodified));
179 fwrite ($bf,full_tag("CREATED",$level+1,false,$blog->created));
180 fwrite ($bf,full_tag("USERMODIFIED",$level+1,false,$blog->usermodified));
182 /// Blog tags
183 /// Check if we have blog tags to backup
184 if (!empty($CFG->usetags)) {
185 if ($tags = tag_get_tags('post', $blog->id)) { //This return them ordered by default
186 /// Start BLOG_TAGS tag
187 fwrite ($bf,start_tag("BLOG_TAGS",$level+1,true));
188 /// Write blog tags fields
189 foreach ($tags as $tag) {
190 fwrite ($bf,start_tag("BLOG_TAG",$level+2,true));
191 fwrite ($bf,full_tag("NAME",$level+3,false,$tag->name));
192 fwrite ($bf,full_tag("RAWNAME",$level+3,false,$tag->rawname));
193 fwrite ($bf,end_tag("BLOG_TAG",$level+2,true));
195 /// End BLOG_TAGS tag
196 fwrite ($bf,end_tag("BLOG_TAGS",$level+1,true));
199 /// end blog
200 fwrite($bf, end_tag("BLOG",$level,true));
203 //Prints course's format data (any data the format might want to save).
204 function backup_format_data ($bf,$preferences) {
205 global $CFG, $DB;
207 // Check course format
208 if(!($format = $DB->get_field('course','format', array('id'=>$preferences->backup_course)))) {
209 return false;
211 // Write appropriate tag. Note that we always put this tag there even if
212 // blank, it makes parsing easier
213 fwrite ($bf,start_tag("FORMATDATA",2,true));
215 $file=$CFG->dirroot."/course/format/$format/backuplib.php";
216 if(file_exists($file)) {
217 // If the file is there, the function must be or it's an error.
218 require_once($file);
219 $function=$format.'_backup_format_data';
220 if(!function_exists($function)) {
221 return false;
223 if(!$function($bf,$preferences)) {
224 return false;
228 // This last return just checks the file writing has been ok (ish)
229 return fwrite ($bf,end_tag("FORMATDATA",2,true));
232 function backup_gradebook_categories_history_info($bf, $preferences) {
233 global $CFG, $DB;
235 $status = true;
237 // find all grade categories history
238 if ($chs = $DB->get_records('grade_categories_history', array('courseid'=>$preferences->backup_course))) {
239 fwrite ($bf,start_tag("GRADE_CATEGORIES_HISTORIES",3,true));
240 foreach ($chs as $ch) {
241 fwrite ($bf,start_tag("GRADE_CATEGORIES_HISTORY",4,true));
242 fwrite ($bf,full_tag("ID",5,false,$ch->id));
243 fwrite ($bf,full_tag("ACTION",5,false,$ch->action));
244 fwrite ($bf,full_tag("OLDID",5,false,$ch->oldid));
245 fwrite ($bf,full_tag("SOURCE",5,false,$ch->source));
246 fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$ch->timemodified));
247 fwrite ($bf,full_tag("LOGGEDUSER",5,false,$ch->loggeduser));
248 fwrite ($bf,full_tag("PARENT",5,false,$ch->parent));
249 fwrite ($bf,full_tag("DEPTH",5,false,$ch->depth));
250 fwrite ($bf,full_tag("PATH",5,false,$ch->path));
251 fwrite ($bf,full_tag("FULLNAME",5,false,$ch->fullname));
252 fwrite ($bf,full_tag("AGGRETGATION",5,false,$ch->aggregation));
253 fwrite ($bf,full_tag("KEEPHIGH",5,false,$ch->keephigh));
254 fwrite ($bf,full_tag("DROPLOW",5,false,$ch->droplow));
255 fwrite ($bf,full_tag("AGGREGATEONLYGRADED",5,false,$ch->aggregateonlygraded));
256 fwrite ($bf,full_tag("AGGREGATEOUTCOMES",5,false,$ch->aggregateoutcomes));
257 fwrite ($bf,full_tag("AGGREGATESUBCATS",5,false,$ch->aggregatesubcats));
258 fwrite ($bf,end_tag("GRADE_CATEGORIES_HISTORY",4,true));
260 $status = fwrite ($bf,end_tag("GRADE_CATEGORIES_HISTORIES",3,true));
262 return $status;
265 function backup_gradebook_grades_history_info($bf, $preferences) {
266 global $CFG, $DB;
267 $status = true;
269 // find all grade categories history
270 if ($chs = $DB->get_records_sql("SELECT ggh.*
271 FROM {grade_grades_history} ggh
272 JOIN {grade_item} gi ON gi.id = ggh.itemid
273 WHERE gi.courseid = ?", array($preferences->backup_course))) {
274 fwrite ($bf,start_tag("GRADE_GRADES_HISTORIES",3,true));
275 foreach ($chs as $ch) {
276 /// Grades are only sent to backup if the user is one target user
277 if (backup_getid($preferences->backup_unique_code, 'user', $ch->userid)) {
278 fwrite ($bf,start_tag("GRADE_GRADES_HISTORY",4,true));
279 fwrite ($bf,full_tag("ID",5,false,$ch->id));
280 fwrite ($bf,full_tag("ACTION",5,false,$ch->action));
281 fwrite ($bf,full_tag("OLDID",5,false,$ch->oldid));
282 fwrite ($bf,full_tag("SOURCE",5,false,$ch->source));
283 fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$ch->timemodified));
284 fwrite ($bf,full_tag("LOGGEDUSER",5,false,$ch->loggeduser));
285 fwrite ($bf,full_tag("ITEMID",5,false,$ch->itemid));
286 fwrite ($bf,full_tag("USERID",5,false,$ch->userid));
287 fwrite ($bf,full_tag("RAWGRADE",5,false,$ch->rawgrade));
288 fwrite ($bf,full_tag("RAWGRADEMAX",5,false,$ch->rawgrademax));
289 fwrite ($bf,full_tag("RAWGRADEMIN",5,false,$ch->rawgrademin));
290 fwrite ($bf,full_tag("RAWSCALEID",5,false,$ch->rawscaleid));
291 fwrite ($bf,full_tag("USERMODIFIED",5,false,$ch->usermodified));
292 fwrite ($bf,full_tag("FINALGRADE",5,false,$ch->finalgrade));
293 fwrite ($bf,full_tag("HIDDEN",5,false,$ch->hidden));
294 fwrite ($bf,full_tag("LOCKED",5,false,$ch->locked));
295 fwrite ($bf,full_tag("LOCKTIME",5,false,$ch->locktime));
296 fwrite ($bf,full_tag("EXPORTED",5,false,$ch->exported));
297 fwrite ($bf,full_tag("OVERRIDDEN",5,false,$ch->overridden));
298 fwrite ($bf,full_tag("EXCLUDED",5,false,$ch->excluded));
299 fwrite ($bf,full_tag("FEEDBACK",5,false,$ch->feedback));
300 fwrite ($bf,full_tag("FEEDBACKFORMAT",5,false,$ch->feedbackformat));
301 fwrite ($bf,full_tag("INFORMATION",5,false,$ch->information));
302 fwrite ($bf,full_tag("INFORMATIONFORMAT",5,false,$ch->informationformat));
303 fwrite ($bf,end_tag("GRADE_GRADES_HISTORY",4,true));
306 $status = fwrite ($bf,end_tag("GRADE_GRADES_HISTORIES",3,true));
308 return $status;
311 function backup_gradebook_items_history_info($bf, $preferences) {
312 global $CFG, $DB;
313 $status = true;
315 // find all grade categories history
316 if ($chs = $DB->get_records('grade_items_history', array('courseid'=>$preferences->backup_course))) {
317 fwrite ($bf,start_tag("GRADE_ITEM_HISTORIES",3,true));
318 foreach ($chs as $ch) {
319 fwrite ($bf,start_tag("GRADE_ITEM_HISTORY",4,true));
320 fwrite ($bf,full_tag("ID",5,false,$ch->id));
321 fwrite ($bf,full_tag("ACTION",5,false,$ch->action));
322 fwrite ($bf,full_tag("OLDID",5,false,$ch->oldid));
323 fwrite ($bf,full_tag("SOURCE",5,false,$ch->source));
324 fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$ch->timemodified));
325 fwrite ($bf,full_tag("LOGGEDUSER",5,false,$ch->loggeduser));
326 fwrite ($bf,full_tag("CATEGORYID",5,false,$ch->categoryid));
327 fwrite ($bf,full_tag("ITEMNAME",5,false,$ch->itemname));
328 fwrite ($bf,full_tag("ITEMTYPE",5,false,$ch->itemtype));
329 fwrite ($bf,full_tag("ITEMMODULE",5,false,$ch->itemmodule));
330 fwrite ($bf,full_tag("ITEMINSTANCE",5,false,$ch->iteminstance));
331 fwrite ($bf,full_tag("ITEMNUMBER",5,false,$ch->itemnumber));
332 fwrite ($bf,full_tag("ITEMINFO",5,false,$ch->iteminfo));
333 fwrite ($bf,full_tag("IDNUMBER",5,false,$ch->idnumber));
334 fwrite ($bf,full_tag("CALCULATION",5,false,$ch->calculation));
335 fwrite ($bf,full_tag("GRADETYPE",5,false,$ch->gradetype));
336 fwrite ($bf,full_tag("GRADEMAX",5,false,$ch->grademax));
337 fwrite ($bf,full_tag("GRADEMIN",5,false,$ch->grademin));
338 fwrite ($bf,full_tag("SCALEID",5,false,$ch->scaleid));
339 fwrite ($bf,full_tag("OUTCOMEID",5,false,$ch->outcomeid));
340 fwrite ($bf,full_tag("GRADEPASS",5,false,$ch->gradepass));
341 fwrite ($bf,full_tag("MULTFACTOR",5,false,$ch->multfactor));
342 fwrite ($bf,full_tag("PLUSFACTOR",5,false,$ch->plusfactor));
343 fwrite ($bf,full_tag("AGGREGATIONCOEF",5,false,$ch->aggregationcoef));
344 fwrite ($bf,full_tag("SORTORDER",5,false,$ch->sortorder));
345 //fwrite ($bf,full_tag("DISPLAY",7,false,$ch->display));
346 //fwrite ($bf,full_tag("DECIMALS",7,false,$ch->decimals));
347 fwrite ($bf,full_tag("HIDDEN",5,false,$ch->hidden));
348 fwrite ($bf,full_tag("LOCKED",5,false,$ch->locked));
349 fwrite ($bf,full_tag("LOCKTIME",5,false,$ch->locktime));
350 fwrite ($bf,full_tag("NEEDSUPDATE",5,false,$ch->needsupdate));
351 fwrite ($bf,end_tag("GRADE_ITEM_HISTORY",4,true));
353 $status = fwrite ($bf,end_tag("GRADE_ITEM_HISTORIES",3,true));
356 return $status;
359 function backup_gradebook_outcomes_history($bf, $preferences) {
360 global $CFG, $DB;
361 $status = true;
363 // find all grade categories history
364 if ($chs = $DB->get_records('grade_outcomes_history', array('courseid'=>$preferences->backup_course))) {
365 fwrite ($bf,start_tag("GRADE_OUTCOME_HISTORIES",3,true));
366 foreach ($chs as $ch) {
367 fwrite ($bf,start_tag("GRADE_OUTCOME_HISTORY",4,true));
368 fwrite ($bf,full_tag("ID",5,false,$ch->id));
369 fwrite ($bf,full_tag("OLDID",5,false,$ch->oldid));
370 fwrite ($bf,full_tag("ACTION",5,false,$ch->action));
371 fwrite ($bf,full_tag("SOURCE",5,false,$ch->source));
372 fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$ch->timemodified));
373 fwrite ($bf,full_tag("LOGGEDUSER",5,false,$ch->loggeduser));
374 fwrite ($bf,full_tag("SHORTNAME",5,false,$ch->shortname));
375 fwrite ($bf,full_tag("FULLNAME",5,false,$ch->fullname));
376 fwrite ($bf,full_tag("SCALEID",5,false,$ch->scaleid));
377 fwrite ($bf,full_tag("DESCRIPTION",5,false,$ch->description));
378 fwrite ($bf,end_tag("GRADE_OUTCOME_HISTORY",4,true));
380 $status = fwrite ($bf,end_tag("GRADE_OUTCOME_HISTORIES",3,true));
382 return $status;
385 //Backup events info (course events)
386 function backup_events_info($bf,$preferences) {
387 global $CFG, $DB;
389 $status = true;
391 //Counter, points to current record
392 $counter = 0;
394 //Get events (course events)
395 $events = $DB->get_records("event", array("courseid"=>$preferences->backup_course, 'instance'=>0),"id");
397 //Pring events header
398 if ($events) {
399 //Pring events header
400 fwrite ($bf,start_tag("EVENTS",2,true));
401 //Iterate
402 foreach ($events as $event) {
403 //Begin event tag
404 fwrite ($bf,start_tag("EVENT",3,true));
405 //Output event tag
406 fwrite ($bf,full_tag("ID",4,false,$event->id));
407 fwrite ($bf,full_tag("NAME",4,false,$event->name));
408 fwrite ($bf,full_tag("DESCRIPTION",4,false,$event->description));
409 fwrite ($bf,full_tag("FORMAT",4,false,$event->format));
410 fwrite ($bf,full_tag("GROUPID",4,false,$event->groupid));
411 fwrite ($bf,full_tag("USERID",4,false,$event->userid));
412 fwrite ($bf,full_tag("REPEATID",4,false,$event->repeatid));
413 fwrite ($bf,full_tag("EVENTTYPE",4,false,$event->eventtype));
414 fwrite ($bf,full_tag("MODULENAME",4,false,$event->modulename));
415 fwrite ($bf,full_tag("TIMESTART",4,false,$event->timestart));
416 fwrite ($bf,full_tag("TIMEDURATION",4,false,$event->timeduration));
417 fwrite ($bf,full_tag("VISIBLE",4,false,$event->visible));
418 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$event->timemodified));
419 //End event tag
420 fwrite ($bf,end_tag("EVENT",3,true));
422 //End events tag
423 $status = fwrite ($bf,end_tag("EVENTS",2,true));
425 return $status;