forked from Wunderbyte-GmbH/moodle-mod_booking
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackuplib.php
executable file
·260 lines (217 loc) · 8.53 KB
/
backuplib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php //$Id: backuplib.php,v 1.11 2006/02/08 23:46:21 danmarsden Exp $
//This php script contains all the stuff to backup/restore
//booking mods
//This is the "graphical" structure of the booking mod:
//
// booking
// (CL,pk->id)----------|
// | |
// | |
// | |
// booking_options |
// (UL,pk->id, fk->bookingid) |
// | |
// | |
// | |
// booking_answers |
// (UL,pk->id, fk->bookingid, fk->optionid)
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// CL->course level info
// UL->user level info
// files->table may have files)
//
//-----------------------------------------------------------
function booking_backup_mods($bf,$preferences) {
global $CFG;
$status = true;
//Iterate over booking table
$bookings = get_records ("booking","course",$preferences->backup_course,"id");
if ($bookings) {
foreach ($bookings as $booking) {
if (backup_mod_selected($preferences,'booking',$booking->id)) {
$status = booking_backup_one_mod($bf,$preferences,$booking);
}
}
}
return $status;
}
function booking_backup_one_mod($bf,$preferences,$booking) {
global $CFG;
if (is_numeric($booking)) {
$booking = get_record('booking','id',$booking);
}
$status = true;
//Start mod
fwrite ($bf,start_tag("MOD",3,true));
//Print booking data
fwrite ($bf,full_tag("ID",4,false,$booking->id));
fwrite ($bf,full_tag("MODTYPE",4,false,"booking"));
fwrite ($bf,full_tag("NAME",4,false,$booking->name));
fwrite ($bf,full_tag("TEXT",4,false,$booking->text));
fwrite ($bf,full_tag("FORMAT",4,false,$booking->format));
fwrite ($bf,full_tag("BOOKINGMANAGER",4,false,$booking->bookingmanager));
fwrite ($bf,full_tag("SENDMAIL",4,false,$booking->sendmail));
fwrite ($bf,full_tag("COPYMAIL",4,false,$booking->copymail));
fwrite ($bf,full_tag("ALLOWUPDATE",4,false,$booking->allowupdate));
fwrite ($bf,full_tag("bookingpolicy",4,false,$booking->bookingpolicy));
fwrite ($bf,full_tag("TIMEOPEN",4,false,$booking->timeopen));
fwrite ($bf,full_tag("TIMECLOSE",4,false,$booking->timeclose));
fwrite ($bf,full_tag("LIMITANSWERS",4,false,$booking->limitanswers));
fwrite ($bf,full_tag("MAXANSWERS",4,false,$booking->maxanswers));
fwrite ($bf,full_tag("MAXOVERBOOKING",4,false,$booking->maxoverbooking));
fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$booking->timemodified));
//Now backup booking_options
$status = backup_booking_options($bf,$preferences,$booking->id);
//if we've selected to backup users info, then execute backup_booking_answers
if (backup_userdata_selected($preferences,'booking',$booking->id)) {
$status = backup_booking_answers($bf,$preferences,$booking->id);
}
//End mod
$status =fwrite ($bf,end_tag("MOD",3,true));
return $status;
}
//Backup booking_answers contents (executed from booking_backup_mods)
function backup_booking_answers ($bf,$preferences,$booking) {
global $CFG;
$status = true;
$booking_answers = get_records("booking_answers","bookingid",$booking,"id");
//If there is answers
if ($booking_answers) {
//Write start tag
$status =fwrite ($bf,start_tag("ANSWERS",4,true));
//Iterate over each answer
foreach ($booking_answers as $cho_ans) {
//Start answer
$status =fwrite ($bf,start_tag("ANSWER",5,true));
//Print answer contents
fwrite ($bf,full_tag("ID",6,false,$cho_ans->id));
fwrite ($bf,full_tag("BOOKINGID",6,false,$cho_ans->bookingid));
fwrite ($bf,full_tag("USERID",6,false,$cho_ans->userid));
fwrite ($bf,full_tag("OPTIONID",6,false,$cho_ans->optionid));
fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_ans->timemodified));
//End answer
$status =fwrite ($bf,end_tag("ANSWER",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("ANSWERS",4,true));
}
return $status;
}
//backup booking_options contents (executed from booking_backup_mods)
function backup_booking_options ($bf,$preferences,$booking) {
global $CFG;
$status = true;
$booking_options = get_records("booking_options","bookingid",$booking,"id");
//If there is options
if ($booking_options) {
//Write start tag
$status =fwrite ($bf,start_tag("OPTIONS",4,true));
//Iterate over each answer
foreach ($booking_options as $cho_opt) {
//Start option
$status =fwrite ($bf,start_tag("OPTION",5,true));
//Print option contents
fwrite ($bf,full_tag("ID",6,false,$cho_opt->id));
fwrite ($bf,full_tag("BOOKINGID",6,false,$cho_opt->bookingid));
fwrite ($bf,full_tag("TEXT",6,false,$cho_opt->text));
fwrite ($bf,full_tag("MAXANSWERS",6,false,$cho_opt->maxanswers));
fwrite ($bf,full_tag("MAXOVERBOOKING",6,false,$cho_opt->maxoverbooking));
fwrite ($bf,full_tag("BOOKINGCLOSINGTIME",6,false,$cho_opt->bookingclosingtime));
fwrite ($bf,full_tag("COURSEID",6,false,$cho_opt->courseid));
fwrite ($bf,full_tag("COURSESTARTTIME",6,false,$cho_opt->coursestarttime));
fwrite ($bf,full_tag("COURSEENDTIME",6,false,$cho_opt->courseendtime));
fwrite ($bf,full_tag("DESCRIPTION",6,false,$cho_opt->description));
fwrite ($bf,full_tag("LIMITANSWERS",6,false,$cho_opt->limitanswers));
fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_opt->timemodified));
//End answer
$status =fwrite ($bf,end_tag("OPTION",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("OPTIONS",4,true));
}
return $status;
}
////Return an array of info (name,value)
function booking_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
if (!empty($instances) && is_array($instances) && count($instances)) {
$info = array();
foreach ($instances as $id => $instance) {
$info += booking_check_backup_mods_instances($instance,$backup_unique_code);
}
return $info;
}
//First the course data
$info[0][0] = get_string("modulenameplural","booking");
if ($ids = booking_ids ($course)) {
$info[0][1] = count($ids);
} else {
$info[0][1] = 0;
}
//Now, if requested, the user_data
if ($user_data) {
$info[1][0] = get_string("responses","booking");
if ($ids = booking_answer_ids_by_course ($course)) {
$info[1][1] = count($ids);
} else {
$info[1][1] = 0;
}
}
return $info;
}
////Return an array of info (name,value)
function booking_check_backup_mods_instances($instance,$backup_unique_code) {
//First the course data
$info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
$info[$instance->id.'0'][1] = '';
//Now, if requested, the user_data
if (!empty($instance->userdata)) {
$info[$instance->id.'1'][0] = get_string("responses","booking");
if ($ids = booking_answer_ids_by_instance ($instance->id)) {
$info[$instance->id.'1'][1] = count($ids);
} else {
$info[$instance->id.'1'][1] = 0;
}
}
return $info;
}
//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function booking_encode_content_links ($content,$preferences) {
global $CFG;
$base = preg_quote($CFG->wwwroot,"/");
//Link to the list of bookings
$buscar="/(".$base."\/mod\/booking\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@BOOKINGINDEX*$2@$',$content);
//Link to booking view by moduleid
$buscar="/(".$base."\/mod\/booking\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@BOOKINGVIEWBYID*$2@$',$result);
return $result;
}
// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
//Returns an array of bookings id
function booking_ids ($course) {
global $CFG;
return get_records_sql ("SELECT a.id, a.course
FROM {$CFG->prefix}booking a
WHERE a.course = '$course'");
}
//Returns an array of booking_answers id
function booking_answer_ids_by_course ($course) {
global $CFG;
return get_records_sql ("SELECT s.id , s.bookingid
FROM {$CFG->prefix}booking_answers s,
{$CFG->prefix}booking a
WHERE a.course = '$course' AND
s.bookingid = a.id");
}
//Returns an array of booking_answers id
function booking_answer_ids_by_instance ($instanceid) {
global $CFG;
return get_records_sql ("SELECT s.id , s.bookingid
FROM {$CFG->prefix}booking_answers s
WHERE s.bookingid = $instanceid");
}
?>