forked from tmuras/Moodle-Flashcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backuplib.php
268 lines (229 loc) · 9.92 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
261
262
263
264
265
266
267
268
<?PHP //$Id: backuplib.php,v 1.4 2008/12/28 15:44:55 diml Exp $
/**
* This php script contains all the stuff to backup/restore
* flashcard mods
*
* @package mod-flashcard
* @category mod
* @author Valery Fremaux ([email protected])
* @version Moodle 2.0
*
*/
//This is the "graphical" structure of the flashcard mod:
//
// flashcard
// (CL,pk->id)
// |
// +-------------------------------------------+
// | |
// flashcard_card flashcard_deckdata
// (UL,pk->id, fk->flashcardid, fk->entryid) (IL, pk->id, fk->flashcardid,files)
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// IL->instance level info
// CL->course level info
// UL->user level info
// files->table may have files
//
//-----------------------------------------------------------
function flashcard_backup_mods($bf, $preferences) {
global $CFG, $DB;
$status = true;
//Iterate over flashcard table
$flashcards = $DB->get_records('flashcard', array('course' => $preferences->backup_course), 'id');
if ($flashcards) {
foreach ($flashcards as $flashcard) {
$status = $status && flashcard_backup_one_mod($bf, $preferences, $flashcard);
}
}
return $status;
}
function flashcard_backup_one_mod($bf, $preferences, $flashcard) {
global $CFG, $DB;
if (is_numeric($flashcard)) {
$flashcard = $DB->get_record('flashcard', array('id' => $flashcard));
}
$status = true;
fwrite ($bf, start_tag('MOD', 3, true));
//Print choice data
fwrite ($bf,full_tag('ID', 4, false, $flashcard->id));
fwrite ($bf,full_tag('MODTYPE', 4, false, 'flashcard'));
fwrite ($bf,full_tag('NAME', 4, false, $flashcard->name));
fwrite ($bf,full_tag('SUMMARY', 4, false, $flashcard->summary));
fwrite ($bf,full_tag('SUMMARYFORMAT', 4, false, $flashcard->summaryformat));
fwrite ($bf,full_tag('TIMEMODIFIED', 4, false, $flashcard->timemodified));
fwrite ($bf,full_tag('STARTTIME', 4, false, $flashcard->starttime));
fwrite ($bf,full_tag('ENDTIME', 4, false, $flashcard->endtime));
fwrite ($bf,full_tag('QUESTIONID', 4, false, $flashcard->questionid));
fwrite ($bf,full_tag('AUTODOWNGRADE', 4, false, $flashcard->autodowngrade));
fwrite ($bf,full_tag('DECKS', 4, false, $flashcard->decks));
fwrite ($bf,full_tag('DECK2_RELEASE', 4, false, $flashcard->deck2_release));
fwrite ($bf,full_tag('DECK3_RELEASE', 4, false, $flashcard->deck3_release));
fwrite ($bf,full_tag('DECK4_RELEASE', 4, false, $flashcard->deck4_release));
fwrite ($bf,full_tag('DECK1_DELAY', 4, false, $flashcard->deck1_delay));
fwrite ($bf,full_tag('DECK2_DELAY', 4, false, $flashcard->deck2_delay));
fwrite ($bf,full_tag('DECK3_DELAY', 4, false, $flashcard->deck3_delay));
fwrite ($bf,full_tag('DECK4_DELAY', 4, false, $flashcard->deck4_delay));
fwrite ($bf,full_tag('QUESTIONSMEDIATYPE', 4, false, $flashcard->questionsmediatype));
fwrite ($bf,full_tag('ANSWERSMEDIATYPE', 4, false, $flashcard->answersmediatype));
fwrite ($bf,full_tag('FLIPDECK', 4, false, $flashcard->flipdeck));
$status = $status && backup_flashcard_deck($bf, $preferences, $flashcard);
$status = $status && backup_flashcard_files_instance($bf, $preferences, $flashcard->id);
if ($preferences->mods['flashcard']->userinfo) {
$status = $status && backup_flashcard_cards($bf, $preferences, $flashcard);
}
/// End mod
$status = $status && fwrite ($bf, end_tag('MOD', 3, true));
return $status;
}
/**
* Backup flashcard deck constitution (executed from flashcard_backup_mods)
*/
function backup_flashcard_deck($bf, $preferences, &$flashcard) {
global $CFG, $DB;
$status = true;
/// Write start tag
$status = $status && fwrite ($bf, start_tag('DECK', 4, true));
$cards = $DB->get_records('flashcard_deckdata', array('flashcardid' => $flashcard->id));
/// If there is card
if ($cards) {
/// Iterate over each card of the deck
foreach ($cards as $card) {
/// Start card
$status = $status && fwrite ($bf, start_tag('CARD', 5, true));
/// Print card data
fwrite ($bf, full_tag('ID', 6, false, $card->id));
fwrite ($bf, full_tag('FLASHCARDID', 6, false, $card->flashcardid));
fwrite ($bf, full_tag('QUESTIONTEXT', 6, false, $card->questiontext));
fwrite ($bf, full_tag('ANSWERTEXT', 6, false, $card->answertext));
/// End card
$status = $status && fwrite ($bf, end_tag('CARD', 5, true));
}
}
/// Write end tag
$status = $status && fwrite($bf, end_tag('DECK', 4, true));
return $status;
}
/**
* Backup flashcard_cards (executed from flashcard_backup_mods)
*/
function backup_flashcard_cards($bf, $preferences, &$flashcard) {
global $CFG, $DB;
$status = true;
/// Write start tag
$status = $status && fwrite ($bf, start_tag('CARDS', 4, true));
$cards = $DB->get_records('flashcard_card', array('flashcardid' => $flashcard->id));
/// If there is card
if ($cards) {
/// Iterate over each card
foreach ($cards as $card) {
/// Start card
$status = $status && fwrite ($bf, start_tag('CARD', 5, true));
/// Print card data
fwrite ($bf, full_tag('ID', 6, false, $card->id));
fwrite ($bf, full_tag('FLASHCARDID', 6, false, $card->flashcardid));
fwrite ($bf, full_tag('USERID', 6, false, $card->userid));
fwrite ($bf, full_tag('ENTRYID', 6, false, $card->entryid));
fwrite ($bf, full_tag('DECK', 6, false, $card->deck));
fwrite ($bf, full_tag('LASTACCESSED', 6, false, $card->lastaccessed));
fwrite ($bf, full_tag('ACCESSCOUNT', 6, false, $card->accesscount));
/// End card
$status = $status && fwrite ($bf, end_tag('CARD', 5, true));
}
}
/// Write end tag
$status = $status && fwrite($bf, end_tag('CARDS', 4, true));
return $status;
}
/// Backup flashcard files for images or sounds
function backup_flashcard_files_instance($bf, $preferences, $instanceid) {
global $CFG;
$status = true;
//First we check to moddata exists and create it as necessary
//in temp/backup/$backup_code dir
$status = check_and_create_moddata_dir($preferences->backup_unique_code);
$status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/flashcard/",true);
//Now copy the flashcard dir
if ($status) {
//Only if it exists !! Thanks to Daniel Miksik.
if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/flashcard/".$instanceid)) {
$status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/flashcard/".$instanceid,
$CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/flashcard/".$instanceid);
}
}
return $status;
}
/// Return an array of info (name,value)
function flashcard_check_backup_mods($course, $user_data = false, $backup_unique_code) {
// First the course data
$info[0][0] = get_string('modulenameplural', 'flashcard');
if ($ids = flashcard_ids($course)) {
$info[0][1] = count($ids);
} else {
$info[0][1] = 0;
}
$info[1][0] = get_string('deck', 'flashcard');
if ($ids = flashcard_deckdata_ids($course)) {
$info[1][1] = count($ids);
} else {
$info[1][1] = 0;
}
if ($user_data){
$info[2][0] = get_string('userdecks', 'flashcard');
if ($ids = flashcard_cards_ids($course)) {
$info[2][1] = count($ids);
} else {
$info[2][1] = 0;
}
}
return $info;
}
// Returns an array of flashcard id
function flashcard_ids ($course) {
global $CFG, $DB;
$query = "
SELECT
f.id,
f.course
FROM
{flashcard} f
WHERE
f.course = ?
";
return $DB->get_records_sql($query, array($course));
}
// Returns an array of flashcard card id in deck
function flashcard_deckdata_ids($course) {
global $CFG, $DB;
$query = "
SELECT
dd.id,
f.course
FROM
{flashcard} f,
{flashcard_deckdata} dd
WHERE
f.id = dd.flashcardid AND
f.course = ?
";
return $DB->get_records_sql($query, array($course));
}
// Returns an array of flashcard card id
function flashcard_cards_ids($course) {
global $CFG, $DB;
$query = "
SELECT
c.id,
f.course
FROM
{flashcard} f,
{flashcard_card} c
WHERE
f.id = c.flashcardid AND
f.course = ?
";
return $DB->get_records_sql($query, array($course));
}
?>