forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackuplib.php
249 lines (205 loc) · 9.13 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
<?php
//This php script contains all the stuff to backup/restore
//choice mods
//This is the "graphical" structure of the choice mod:
//
// choice
// (CL,pk->id)----------|
// | |
// | |
// | |
// choice_options |
// (UL,pk->id, fk->choiceid) |
// | |
// | |
// | |
// choice_answers |
// (UL,pk->id, fk->choiceid, 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 choice_backup_mods($bf,$preferences) {
global $CFG, $DB;
$status = true;
//Iterate over choice table
$choices = $DB->get_records("choice", array("course" => $preferences->backup_course), "id");
if ($choices) {
foreach ($choices as $choice) {
if (backup_mod_selected($preferences,'choice',$choice->id)) {
$status = choice_backup_one_mod($bf,$preferences,$choice);
}
}
}
return $status;
}
function choice_backup_one_mod($bf,$preferences,$choice) {
global $CFG, $DB;
if (is_numeric($choice)) {
$choice = $DB->get_record('choice',array('id' => $choice));
}
$status = true;
//Start mod
fwrite ($bf,start_tag("MOD",3,true));
//Print choice data
fwrite ($bf,full_tag("ID",4,false,$choice->id));
fwrite ($bf,full_tag("MODTYPE",4,false,"choice"));
fwrite ($bf,full_tag("NAME",4,false,$choice->name));
fwrite ($bf,full_tag("TEXT",4,false,$choice->intro));
fwrite ($bf,full_tag("FORMAT",4,false,$choice->introformat));
fwrite ($bf,full_tag("PUBLISH",4,false,$choice->publish));
fwrite ($bf,full_tag("SHOWRESULTS",4,false,$choice->showresults));
fwrite ($bf,full_tag("DISPLAY",4,false,$choice->display));
fwrite ($bf,full_tag("ALLOWUPDATE",4,false,$choice->allowupdate));
fwrite ($bf,full_tag("SHOWUNANSWERED",4,false,$choice->showunanswered));
fwrite ($bf,full_tag("LIMITANSWERS",4,false,$choice->limitanswers));
fwrite ($bf,full_tag("TIMEOPEN",4,false,$choice->timeopen));
fwrite ($bf,full_tag("TIMECLOSE",4,false,$choice->timeclose));
fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$choice->timemodified));
//Now backup choice_options
$status = backup_choice_options($bf,$preferences,$choice->id);
//if we've selected to backup users info, then execute backup_choice_answers
if (backup_userdata_selected($preferences,'choice',$choice->id)) {
$status = backup_choice_answers($bf,$preferences,$choice->id);
}
//End mod
$status =fwrite ($bf,end_tag("MOD",3,true));
return $status;
}
//Backup choice_answers contents (executed from choice_backup_mods)
function backup_choice_answers ($bf,$preferences,$choice) {
global $CFG, $DB;
$status = true;
$choice_answers = $DB->get_records("choice_answers", array("choiceid" => $choice),"id");
//If there is answers
if ($choice_answers) {
//Write start tag
$status =fwrite ($bf,start_tag("ANSWERS",4,true));
//Iterate over each answer
foreach ($choice_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("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 choice_options contents (executed from choice_backup_mods)
function backup_choice_options ($bf,$preferences,$choice) {
global $CFG, $DB;
$status = true;
$choice_options = $DB->get_records("choice_options", array("choiceid" => $choice),"id");
//If there is options
if (!empty($choice_options)) {
//Write start tag
$status =fwrite ($bf,start_tag("OPTIONS",4,true));
//Iterate over each answer
foreach ($choice_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("TEXT",6,false,$cho_opt->text));
fwrite ($bf,full_tag("MAXANSWERS",6,false,$cho_opt->maxanswers));
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 choice_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 += choice_check_backup_mods_instances($instance,$backup_unique_code);
}
return $info;
}
//First the course data
$info[0][0] = get_string("modulenameplural","choice");
if ($ids = choice_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","choice");
if ($ids = choice_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 choice_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","choice");
if ($ids = choice_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 choice_encode_content_links ($content,$preferences) {
global $CFG;
$base = preg_quote($CFG->wwwroot,"/");
//Link to the list of choices
$buscar="/(".$base."\/mod\/choice\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHOICEINDEX*$2@$',$content);
//Link to choice view by moduleid
$buscar="/(".$base."\/mod\/choice\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHOICEVIEWBYID*$2@$',$result);
return $result;
}
// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
//Returns an array of choices id
function choice_ids ($course) {
global $CFG, $DB;
return $DB->get_records_sql ("SELECT a.id, a.course
FROM {choice} a
WHERE a.course = ?", array($course));
}
//Returns an array of choice_answers id
function choice_answer_ids_by_course ($course) {
global $CFG, $DB;
return $DB->get_records_sql ("SELECT s.id , s.choiceid
FROM {choice_answers} s,
{choice} a
WHERE a.course = ? AND
s.choiceid = a.id", array($course));
}
//Returns an array of choice_answers id
function choice_answer_ids_by_instance ($instanceid) {
global $CFG, $DB;
return $DB->get_records_sql ("SELECT s.id , s.choiceid
FROM {choice_answers} s
WHERE s.choiceid = ?", array($instanceid));
}