fixed log action inserts
[moodle.git] / backuplib.php
CommitLineData
02039379
PS
1<?PHP // $Id: backuplib.php,v 1.1 2006/03/12 18:39:59 skodak Exp $
2 //This php script contains all the stuff to backup/restore
3 //book mods
4
5 //This is the 'graphical' structure of the book mod:
6 //
7 // book
8 // (CL,pk->id)
9 // |
10 // |
11 // |
12 // book_chapters
13 // (CL,pk->id, fk->bookid)
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 book_backup_mods($bf,$preferences) {
26 global $CFG;
27
28 $status = true;
29
30 ////Iterate over book table
31 if ($books = get_records ('book', 'course', $preferences->backup_course, 'id')) {
32 foreach ($books as $book) {
33 if (backup_mod_selected($preferences,'book',$book->id)) {
34 $status = book_backup_one_mod($bf,$preferences,$book);
35 }
36 }
37 }
38 return $status;
39 }
40
41 function book_backup_one_mod($bf,$preferences,$book) {
42
43 global $CFG;
44
45 if (is_numeric($book)) {
46 $book = get_record('book','id',$book);
47 }
48
49 $status = true;
50
51 //Start mod
52 fwrite ($bf,start_tag('MOD',3,true));
53 //Print book data
54 fwrite ($bf,full_tag('ID',4,false,$book->id));
55 fwrite ($bf,full_tag('MODTYPE',4,false,'book'));
56 fwrite ($bf,full_tag('NAME',4,false,$book->name));
57 fwrite ($bf,full_tag('SUMMARY',4,false,$book->summary));
58 fwrite ($bf,full_tag('NUMBERING',4,false,$book->numbering));
59 fwrite ($bf,full_tag('DISABLEPRINTING',4,false,$book->disableprinting));
60 fwrite ($bf,full_tag('CUSTOMTITLES',4,false,$book->customtitles));
61 fwrite ($bf,full_tag('TIMECREATED',4,false,$book->timecreated));
62 fwrite ($bf,full_tag('TIMEMODIFIED',4,false,$book->timemodified));
63 //back up the chapters
64 $status = backup_book_chapters($bf,$preferences,$book);
65 //End mod
66 $status = fwrite($bf,end_tag('MOD',3,true));
67
68 return $status;
69 }
70
71 //Backup book_chapters contents (executed from book_backup_mods)
72 function backup_book_chapters($bf,$preferences,$book) {
73
74 global $CFG;
75
76 $status = true;
77 //Print book's chapters
78 if ($chapters = get_records('book_chapters', 'bookid', $book->id, 'id')) {
79 //Write start tag
80 $status =fwrite ($bf,start_tag('CHAPTERS',4,true));
81 foreach ($chapters as $ch) {
82 //Start chapter
83 fwrite ($bf,start_tag('CHAPTER',5,true));
84 //Print chapter data
85 fwrite ($bf,full_tag('ID',6,false,$ch->id));
86 fwrite ($bf,full_tag('PAGENUM',6,false,$ch->pagenum));
87 fwrite ($bf,full_tag('SUBCHAPTER',6,false,$ch->subchapter));
88 fwrite ($bf,full_tag('TITLE',6,false,$ch->title));
89 fwrite ($bf,full_tag('CONTENT',6,false,$ch->content));
90 fwrite ($bf,full_tag('HIDDEN',6,false,$ch->hidden));
91 fwrite ($bf,full_tag('TIMECREATED',6,false,$ch->timecreated));
92 fwrite ($bf,full_tag('TIMEMODIFIED',6,false,$ch->timemodified));
93 fwrite ($bf,full_tag('IMPORTSRC',6,false,$ch->importsrc));
94 //End chapter
95 $status = fwrite ($bf,end_tag('CHAPTER',5,true));
96 }
97 //Write end tag
98 $status = fwrite ($bf,end_tag('CHAPTERS',4,true));
99 }
100 return $status;
101 }
102
103 //Return a content encoded to support interactivities linking. Every module
104 //should have its own. They are called automatically from the backup procedure.
105 function book_encode_content_links ($content,$preferences) {
106
107 global $CFG;
108
109 $base = preg_quote($CFG->wwwroot,"/");
110
111 $result = $content;
112
113 //Link to the list of books
114 $buscar="/(".$base."\/mod\/book\/index.php\?id\=)([0-9]+)/";
115 $result= preg_replace($buscar,'$@BOOKINDEX*$2@$',$result);
116
117 //Link to book's specific chapter
118 $buscar="/(".$base."\/mod\/book\/view.php\?id\=)([0-9]+)\&chapterid\=([0-9]+)/";
119 $result= preg_replace($buscar,'$@BOOKCHAPTER*$2*$3@$',$result);
120
121 //Link to book's first chapter
122 $buscar="/(".$base."\/mod\/book\/view.php\?id\=)([0-9]+)/";
123 $result= preg_replace($buscar,'$@BOOKSTART*$2@$',$result);
124
125 return $result;
126 }
127
128
129 ////Return an array of info (name,value)
130 function book_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
131
132 if (!empty($instances) && is_array($instances) && count($instances)) {
133 $info = array();
134 foreach ($instances as $id => $instance) {
135 $info += book_check_backup_mods_instances($instance,$backup_unique_code);
136 }
137 return $info;
138 }
139
140 //First the course data
141 $info[0][0] = get_string('modulenameplural','book');
142 $info[0][1] = count_records('book', 'course', $course);
143
144 //No user data for books ;-)
145
146 return $info;
147 }
148
149 ////Return an array of info (name,value)
150 function book_check_backup_mods_instances($instance,$backup_unique_code) {
151 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
152 $info[$instance->id.'0'][1] = '';
153
154 return $info;
155 }
156
157?>