forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
435 lines (397 loc) · 16.3 KB
/
locallib.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library of functions and constants for module glossary
* outside of what is required for the core moodle api
*
* @package mod-glossary
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . '/portfolio/caller.php');
/**
* class to handle exporting an entire glossary database
*/
class glossary_full_portfolio_caller extends portfolio_module_caller_base {
private $glossary;
private $exportdata;
private $keyedfiles = array(); // keyed on entry
/**
* return array of expected call back arguments
* and whether they are required or not
*
* @return array
*/
public static function expected_callbackargs() {
return array(
'id' => true,
);
}
/**
* load up all data required for this export.
*
* @return void
*/
public function load_data() {
global $DB;
if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) {
throw new portfolio_caller_exception('invalidid', 'glossary');
}
if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
throw new portfolio_caller_exception('invalidid', 'glossary');
}
$entries = $DB->get_records('glossary_entries', array('glossaryid' => $this->glossary->id));
list($where, $params) = $DB->get_in_or_equal(array_keys($entries));
$aliases = $DB->get_records_select('glossary_alias', 'entryid ' . $where, $params);
$categoryentries = $DB->get_records_sql('SELECT ec.entryid, c.name FROM {glossary_entries_categories} ec
JOIN {glossary_categories} c
ON c.id = ec.categoryid
WHERE ec.entryid ' . $where, $params);
$this->exportdata = array('entries' => $entries, 'aliases' => $aliases, 'categoryentries' => $categoryentries);
$fs = get_file_storage();
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
$this->multifiles = array();
foreach (array_keys($entries) as $entry) {
$this->keyedfiles[$entry] = array_merge(
$fs->get_area_files($context->id, 'glossary_attachment', $entry, "timemodified", false),
$fs->get_area_files($context->id, 'glossary_entry', $entry, "timemodified", false)
);
$this->multifiles = array_merge($this->multifiles, $this->keyedfiles[$entry]);
}
}
/**
* how long might we expect this export to take
*
* @return constant one of PORTFOLIO_TIME_XX
*/
public function expected_time() {
$filetime = portfolio_expected_time_file($this->multifiles);
$dbtime = portfolio_expected_time_db(count($this->exportdata['entries']));
return ($filetime > $dbtime) ? $filetime : $dbtime;
}
/**
* return the sha1 of this content
*
* @return string
*/
public function get_sha1() {
$file = '';
if ($this->multifiles) {
$file = $this->get_sha1_file();
}
return sha1(serialize($this->exportdata) . $file);
}
/**
* prepare the package ready to be passed off to the portfolio plugin
*
* @return void
*/
public function prepare_package() {
$entries = $this->exportdata['entries'];
$aliases = array();
$categories = array();
if (is_array($this->exportdata['aliases'])) {
foreach ($this->exportdata['aliases'] as $alias) {
if (!array_key_exists($alias->entryid, $aliases)) {
$aliases[$alias->entryid] = array();
}
$aliases[$alias->entryid][] = $alias->alias;
}
}
if (is_array($this->exportdata['categoryentries'])) {
foreach ($this->exportdata['categoryentries'] as $cat) {
if (!array_key_exists($cat->entryid, $categories)) {
$categories[$cat->entryid] = array();
}
$categories[$cat->entryid][] = $cat->name;
}
}
if ($this->get('exporter')->get('formatclass') == PORTFOLIO_FORMAT_SPREADSHEET) {
$csv = glossary_generate_export_csv($entries, $aliases, $categories);
$this->exporter->write_new_file($csv, clean_filename($this->cm->name) . '.csv', false);
return;
} else if ($this->get('exporter')->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
$ids = array(); // keep track of these to make into a selection later
global $USER, $DB;
$writer = $this->get('exporter')->get('format')->leap2a_writer($USER);
$format = $this->exporter->get('format');
$filename = $this->get('exporter')->get('format')->manifest_name();
foreach ($entries as $e) {
$content = glossary_entry_portfolio_caller::entry_content(
$this->course,
$this->cm,
$this->glossary,
$e,
(array_key_exists($e->id, $aliases) ? $aliases[$e->id] : array()),
$format
);
$entry = new portfolio_format_leap2a_entry('glossaryentry' . $e->id, $e->concept, 'entry', $content);
$entry->author = $DB->get_record('user', array('id' => $e->userid), 'id,firstname,lastname,email');
$entry->published = $e->timecreated;
$entry->updated = $e->timemodified;
if (!empty($this->keyedfiles[$e->id])) {
$entry->add_attachments($this->keyedfiles[$e->id]);
foreach ($this->keyedfiles[$e->id] as $file) {
$this->exporter->copy_existing_file($file);
}
}
if (!empty($categories[$e->id])) {
foreach ($categories[$e->id] as $cat) {
// this essentially treats them as plain tags
// leap has the idea of category schemes
// but I think this is overkill here
$entry->add_category($cat);
}
}
$writer->add_entry($entry);
$ids[] = $entry->id;
}
$selection = new portfolio_format_leap2a_entry('wholeglossary' . $this->glossary->id, get_string('modulename', 'glossary'), 'selection');
$writer->add_entry($selection);
$writer->make_selection($selection, $ids, 'Grouping');
$content = $writer->to_xml();
}
$this->exporter->write_new_file($content, $filename, true);
}
/**
* make sure that the current user is allowed to do this
*
* @return boolean
*/
public function check_permissions() {
return has_capability('mod/glossary:export', get_context_instance(CONTEXT_MODULE, $this->cm->id));
}
/**
* return a nice name to be displayed about this export location
*
* @return string
*/
public static function display_name() {
return get_string('modulename', 'glossary');
}
/**
* what formats this function *generally* supports
*
* @return array
*/
public static function base_supported_formats() {
return array(PORTFOLIO_FORMAT_SPREADSHEET, PORTFOLIO_FORMAT_LEAP2A);
}
}
/**
* class to export a single glossary entry
*
* @package mod-glossary
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class glossary_entry_portfolio_caller extends portfolio_module_caller_base {
private $glossary;
private $entry;
protected $entryid;
/*
* @return array
*/
public static function expected_callbackargs() {
return array(
'entryid' => true,
'id' => true,
);
}
/**
* load up all data required for this export.
*
* @return void
*/
public function load_data() {
global $DB;
if (!$this->cm = get_coursemodule_from_id('glossary', $this->id)) {
throw new portfolio_caller_exception('invalidid', 'glossary');
}
if (!$this->glossary = $DB->get_record('glossary', array('id' => $this->cm->instance))) {
throw new portfolio_caller_exception('invalidid', 'glossary');
}
if ($this->entryid) {
if (!$this->entry = $DB->get_record('glossary_entries', array('id' => $this->entryid))) {
throw new portfolio_caller_exception('noentry', 'glossary');
}
// in case we don't have USER this will make the entry be printed
$this->entry->approved = true;
}
$this->categories = $DB->get_records_sql('SELECT ec.entryid, c.name FROM {glossary_entries_categories} ec
JOIN {glossary_categories} c
ON c.id = ec.categoryid
WHERE ec.entryid = ?', array($this->entryid));
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
if ($this->entry->sourceglossaryid == $this->cm->instance) {
if ($maincm = get_coursemodule_from_instance('glossary', $this->entry->glossaryid)) {
$context = get_context_instance(CONTEXT_MODULE, $maincm->id);
}
}
$this->aliases = $DB->get_records('glossary_alias', array('entryid'=>$this->entryid));
$fs = get_file_storage();
$this->multifiles = array_merge(
$fs->get_area_files($context->id, 'glossary_attachment', $this->entry->id, "timemodified", false),
$fs->get_area_files($context->id, 'glossary_entry', $this->entry->id, "timemodified", false)
);
}
/**
* how long might we expect this export to take
*
* @return constant one of PORTFOLIO_TIME_XX
*/
public function expected_time() {
return PORTFOLIO_TIME_LOW;
}
/**
* make sure that the current user is allowed to do this
*
* @return boolean
*/
public function check_permissions() {
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
return has_capability('mod/glossary:exportentry', $context)
|| ($this->entry->userid == $this->user->id && has_capability('mod/glossary:exportownentry', $context));
}
/**
* return a nice name to be displayed about this export location
*
* @return string
*/
public static function display_name() {
return get_string('modulename', 'glossary');
}
/**
* prepare the package ready to be passed off to the portfolio plugin
*
* @return void
*/
public function prepare_package() {
global $DB;
$format = $this->exporter->get('format');
$content = self::entry_content($this->course, $this->cm, $this->glossary, $this->entry, $this->aliases, $format);
$filename = clean_filename($this->entry->concept) . '.html';
if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
global $USER;
$writer = $this->get('exporter')->get('format')->leap2a_writer($USER);
$filename = $this->get('exporter')->get('format')->manifest_name();
$entry = new portfolio_format_leap2a_entry('glossaryentry' . $this->entry->id, $this->entry->concept, 'entry', $content);
$entry->author = $DB->get_record('user', array('id' => $this->entry->userid), 'id,firstname,lastname,email');
$entry->published = $this->entry->timecreated;
$entry->updated = $this->entry->timemodified;
if ($this->multifiles) {
$entry->add_attachments($this->multifiles);
}
if ($this->categories) {
foreach ($this->categories as $cat) {
// this essentially treats them as plain tags
// leap has the idea of category schemes
// but I think this is overkill here
$entry->add_category($cat->name);
}
}
$writer->add_entry($entry);
$content = $writer->to_xml();
}
if ($this->multifiles) {
foreach ($this->multifiles as $file) {
$this->exporter->copy_existing_file($file);
}
}
return $this->exporter->write_new_file($content, $filename, !empty($this->multifiles));
}
/**
* return the sha1 of this content
*
* @return string
*/
public function get_sha1() {
if ($this->multifiles) {
return sha1(serialize($this->entry) . $this->get_sha1_file());
}
return sha1(serialize($this->entry));
}
/**
* what formats this function *generally* supports
*
* @return array
*/
public static function base_supported_formats() {
return array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_LEAP2A);
}
/**
* helper function to get the html content of an entry
* for both this class and the full glossary exporter
* this is a very simplified version of the dictionary format output,
* but with its 500 levels of indirection removed
* and file rewriting handled by the portfolio export format.
*
* @param stdclass $course
* @param stdclass $cm
* @param stdclass $glossary
* @param stdclass $entry
*
* @return string
*/
public static function entry_content($course, $cm, $glossary, $entry, $aliases, $format) {
global $OUTPUT, $DB;
$entry = clone $entry;
$options = new object();
$options->para = false;
$options->trusted = $entry->definitiontrust;
$output = '<table class="glossarypost dictionary" cellspacing="0">' . "\n";
$output .= '<tr valign="top">' . "\n";
$output .= '<td class="entry">' . "\n";
$output .= '<div class="concept">';
$output .= format_text($OUTPUT->heading('<span class="nolink">' . $entry->concept . '</span>', 3, 'nolink'), FORMAT_MOODLE, $options);
$output .= '</div> ' . "\n";
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$entry->definition = portfolio_rewrite_pluginfile_urls($entry->definition, $context->id, 'glossary_entry', $entry->id, $format);
$output .= format_text($entry->definition, $entry->definitionformat, $options);
if (isset($entry->footer)) {
$output .= $entry->footer;
}
$output .= '</td></tr>' . "\n";
if (!empty($aliases)) {
$output .= '<tr valign="top"><td class="entrylowersection">';
$key = (count($aliases) == 1) ? 'alias' : 'aliases';
$output .= get_string($key, 'glossary') . ': ';
foreach ($aliases as $alias) {
$output .= s($alias) . ',';
}
$output .= substr($output, 0, -1);
$output .= '</td></tr>' . "\n";
}
if ($entry->sourceglossaryid == $cm->instance) {
if (!$maincm = get_coursemodule_from_instance('glossary', $entry->glossaryid)) {
return '';
}
$filecontext = get_context_instance(CONTEXT_MODULE, $maincm->id);
} else {
$filecontext = $context;
}
$fs = get_file_storage();
if ($files = $fs->get_area_files($filecontext->id, 'glossary_attachment', $entry->id, "timemodified", false)) {
$output .= '<table border="0" width="100%"><tr><td>' . "\n";
foreach ($files as $file) {
$output .= $format->file_output($file);
}
$output .= '</td></tr></table>' . "\n";
}
$output .= '</table>' . "\n";
return $output;
}
}