forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaller.php
576 lines (524 loc) · 18.8 KB
/
caller.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?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/>.
/**
* This file contains the base classes that are extended to create portfolio export functionality.
*
* For places in moodle that want to
* add export functionality to subclass from {@link http://docs.moodle.org/dev/Adding_a_Portfolio_Button_to_a_page}
*
* @package core_portfolio
* @copyright 2008 Penny Leach <[email protected]>, Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Base class for callers
*
* @link See http://docs.moodle.org/dev/Adding_a_Portfolio_Button_to_a_page
* @see also portfolio_module_caller_base
*
* @package core_portfolio
* @category portfolio
* @copyright 2008 Penny Leach <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class portfolio_caller_base {
/** @var stdClass course active during the call */
protected $course;
/** @var array configuration used for export. Use set_export_config and get_export_config to access */
protected $exportconfig = array();
/** @var stdclass user currently exporting content */
protected $user;
/** @var stdClass a reference to the exporter object */
protected $exporter;
/** @var array can be optionally overridden by subclass constructors */
protected $supportedformats;
/** @var stored_file single file exports configuration*/
protected $singlefile;
/** @var stored_file|object set this for multi file exports */
protected $multifiles;
/** @var string set this for generated-file exports */
protected $intendedmimetype;
/**
* Create portfolio_caller object
*
* @param array $callbackargs argument properties
*/
public function __construct($callbackargs) {
$expected = call_user_func(array(get_class($this), 'expected_callbackargs'));
foreach ($expected as $key => $required) {
if (!array_key_exists($key, $callbackargs)) {
if ($required) {
$a = (object)array('arg' => $key, 'class' => get_class($this));
throw new portfolio_caller_exception('missingcallbackarg', 'portfolio', null, $a);
}
continue;
}
$this->{$key} = $callbackargs[$key];
}
}
/**
* If this caller wants any additional config items,
* they should be defined here.
*
* @param moodleform $mform passed by reference, add elements to it.
* @param portfolio_plugin_base $instance subclass of portfolio_plugin_base
*/
public function export_config_form(&$mform, $instance) {}
/**
* Whether this caller wants any additional
* config during export (eg options or metadata)
*
* @return bool
*/
public function has_export_config() {
return false;
}
/**
* Just like the moodle form validation function,
* this is passed in the data array from the form
* and if a non empty array is returned, form processing will stop.
*
* @param array $data data from form.
*/
public function export_config_validation($data) {}
/**
* How long does this reasonably expect to take..
* Should we offer the user the option to wait..?
* This is deliberately nonstatic so it can take filesize into account
* the portfolio plugin can override this.
* (so for example even if a huge file is being sent,
* the download portfolio plugin doesn't care )
*/
public abstract function expected_time();
/**
* Helper method to calculate expected time for multi or single file exports
*
* @return string file time expectation
*/
public function expected_time_file() {
if ($this->multifiles) {
return portfolio_expected_time_file($this->multifiles);
}
else if ($this->singlefile) {
return portfolio_expected_time_file($this->singlefile);
}
return PORTFOLIO_TIME_LOW;
}
/**
* Function to build navigation
*/
public abstract function get_navigation();
/**
* Helper function to get sha1
*/
public abstract function get_sha1();
/**
* Helper function to calculate the sha1 for multi or single file exports
*
* @return string sha1 file exports
*/
public function get_sha1_file() {
if (empty($this->singlefile) && empty($this->multifiles)) {
throw new portfolio_caller_exception('invalidsha1file', 'portfolio', $this->get_return_url());
}
if ($this->singlefile) {
return $this->singlefile->get_contenthash();
}
$sha1s = array();
foreach ($this->multifiles as $file) {
$sha1s[] = $file->get_contenthash();
}
asort($sha1s);
return sha1(implode('', $sha1s));
}
/**
* Generic getter for properties belonging to this instance
* <b>outside</b> the subclasses
* like name, visible etc.
*
* @param string $field property's name
* @return mixed
* @throws portfolio_export_exception
*/
public function get($field) {
if (property_exists($this, $field)) {
return $this->{$field};
}
$a = (object)array('property' => $field, 'class' => get_class($this));
throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a);
}
/**
* Generic setter for properties belonging to this instance
* <b>outside</b> the subclass
* like name, visible, etc.
*
* @param string $field property's name
* @param mixed $value property's value
* @return bool
* @throws moodle_exception
*/
public final function set($field, &$value) {
if (property_exists($this, $field)) {
$this->{$field} =& $value;
$this->dirty = true;
return true;
}
$a = (object)array('property' => $field, 'class' => get_class($this));
throw new portfolio_export_exception($this->get('exporter'), 'invalidproperty', 'portfolio', $this->get_return_url(), $a);
}
/**
* Stores the config generated at export time.
* Subclasses can retrieve values using
* @see get_export_config
*
* @param array $config formdata
*/
public final function set_export_config($config) {
$allowed = array_merge(
array('wait', 'hidewait', 'format', 'hideformat'),
$this->get_allowed_export_config()
);
foreach ($config as $key => $value) {
if (!in_array($key, $allowed)) {
$a = (object)array('property' => $key, 'class' => get_class($this));
throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
}
$this->exportconfig[$key] = $value;
}
}
/**
* Returns a particular export config value.
* Subclasses shouldn't need to override this
*
* @param string $key the config item to fetch
* @return null|mixed of export configuration
*/
public final function get_export_config($key) {
$allowed = array_merge(
array('wait', 'hidewait', 'format', 'hideformat'),
$this->get_allowed_export_config()
);
if (!in_array($key, $allowed)) {
$a = (object)array('property' => $key, 'class' => get_class($this));
throw new portfolio_export_exception($this->get('exporter'), 'invalidexportproperty', 'portfolio', $this->get_return_url(), $a);
}
if (!array_key_exists($key, $this->exportconfig)) {
return null;
}
return $this->exportconfig[$key];
}
/**
* Similar to the other allowed_config functions
* if you need export config, you must provide
* a list of what the fields are.
* Even if you want to store stuff during export
* without displaying a form to the user,
* you can use this.
*
* @return array array of allowed keys
*/
public function get_allowed_export_config() {
return array();
}
/**
* After the user submits their config,
* they're given a confirm screen
* summarising what they've chosen.
* This function should return a table of nice strings => values
* of what they've chosen
* to be displayed in a table.
*
* @return bool
*/
public function get_export_summary() {
return false;
}
/**
* Called before the portfolio plugin gets control.
* This function should copy all the files it wants to
* the temporary directory, using copy_existing_file
* or write_new_file
*
* @see copy_existing_file()
* @see write_new_file()
*/
public abstract function prepare_package();
/**
* Helper function to copy files into the temp area
* for single or multi file exports.
*
* @return stored_file|bool
*/
public function prepare_package_file() {
if (empty($this->singlefile) && empty($this->multifiles)) {
throw new portfolio_caller_exception('invalidpreparepackagefile', 'portfolio', $this->get_return_url());
}
if ($this->singlefile) {
return $this->exporter->copy_existing_file($this->singlefile);
}
foreach ($this->multifiles as $file) {
$this->exporter->copy_existing_file($file);
}
}
/**
* Array of formats this caller supports.
*
* @return array list of formats
*/
public final function supported_formats() {
$basic = $this->base_supported_formats();
if (empty($this->supportedformats)) {
$specific = array();
} else if (!is_array($this->supportedformats)) {
debugging(get_class($this) . ' has set a non array value of member variable supported formats - working around but should be fixed in code');
$specific = array($this->supportedformats);
} else {
$specific = $this->supportedformats;
}
return portfolio_most_specific_formats($specific, $basic);
}
/**
* Base supported formats
*
* @throws coding_exception
*/
public static function base_supported_formats() {
throw new coding_exception('base_supported_formats() method needs to be overridden in each subclass of portfolio_caller_base');
}
/**
* This is the "return to where you were" url
*/
public abstract function get_return_url();
/**
* Callback to do whatever capability checks required
* in the caller (called during the export process
*/
public abstract function check_permissions();
/**
* Clean name to display to the user about this caller location
*/
public static function display_name() {
throw new coding_exception('display_name() method needs to be overridden in each subclass of portfolio_caller_base');
}
/**
* Return a string to put at the header summarising this export.
* By default, it just display the name (usually just 'assignment' or something unhelpful
*
* @return string
*/
public function heading_summary() {
return get_string('exportingcontentfrom', 'portfolio', $this->display_name());
}
/**
* Load data
*/
public abstract function load_data();
/**
* Set up the required files for this export.
* This supports either passing files directly
* or passing area arguments directly through
* to the files api using file_storage::get_area_files
*
* @param mixed $ids one of:
* - single file id
* - single stored_file object
* - array of file ids or stored_file objects
* - null
* @return void
*/
public function set_file_and_format_data($ids=null /* ..pass arguments to area files here. */) {
$args = func_get_args();
array_shift($args); // shift off $ids
if (empty($ids) && count($args) == 0) {
return;
}
$files = array();
$fs = get_file_storage();
if (!empty($ids)) {
if (is_numeric($ids) || $ids instanceof stored_file) {
$ids = array($ids);
}
foreach ($ids as $id) {
if ($id instanceof stored_file) {
$files[] = $id;
} else {
$files[] = $fs->get_file_by_id($id);
}
}
} else if (count($args) != 0) {
if (count($args) < 4) {
throw new portfolio_caller_exception('invalidfileareaargs', 'portfolio');
}
$files = array_values(call_user_func_array(array($fs, 'get_area_files'), $args));
}
switch (count($files)) {
case 0: return;
case 1: {
$this->singlefile = $files[0];
return;
}
default: {
$this->multifiles = $files;
}
}
}
/**
* The button-location always knows best
* what the formats are... so it should be trusted.
*
* @todo MDL-31298 - re-analyze set_formats_from_button comment
* @param array $formats array of PORTFOLIO_FORMAT_XX
* @return void
*/
public function set_formats_from_button($formats) {
$base = $this->base_supported_formats();
if (count($base) != count($formats)
|| count($base) != count(array_intersect($base, $formats))) {
$this->supportedformats = portfolio_most_specific_formats($formats, $base);
return;
}
// in the case where the button hasn't actually set anything,
// we need to run through again and resolve conflicts
// TODO revisit this comment - it looks to me like it's lying
$this->supportedformats = portfolio_most_specific_formats($formats, $formats);
}
/**
* Adds a new format to the list of supported formats.
* This functions also handles removing conflicting and less specific
* formats at the same time.
*
* @param string $format one of PORTFOLIO_FORMAT_XX
* @return void
*/
protected function add_format($format) {
if (in_array($format, $this->supportedformats)) {
return;
}
$this->supportedformats = portfolio_most_specific_formats(array($format), $this->supportedformats);
}
/**
* Gets mimetype
*
* @return string
*/
public function get_mimetype() {
if ($this->singlefile instanceof stored_file) {
return $this->singlefile->get_mimetype();
} else if (!empty($this->intendedmimetype)) {
return $this->intendedmimetype;
}
}
/**
* Array of arguments the caller expects to be passed through to it.
* This must be keyed on the argument name, and the array value is a boolean,
* whether it is required, or just optional
* eg array(
* id => true,
* somethingelse => false
* )
*/
public static function expected_callbackargs() {
throw new coding_exception('expected_callbackargs() method needs to be overridden in each subclass of portfolio_caller_base');
}
/**
* Return the context for this export. used for $PAGE->set_context
*
* @param moodle_page $PAGE global page object
*/
public abstract function set_context($PAGE);
}
/**
* Base class for module callers.
*
* This just implements a few of the abstract functions
* from portfolio_caller_base so that caller authors
* don't need to.
* {@link http://docs.moodle.org/dev/Adding_a_Portfolio_Button_to_a_page}
* @see also portfolio_caller_base
*
* @package core_portfolio
* @category portfolio
* @copyright 2008 Penny Leach <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class portfolio_module_caller_base extends portfolio_caller_base {
/** @var object coursemodule object. set this in the constructor like $this->cm = get_coursemodule_from_instance('forum', $this->forum->id); */
protected $cm;
/** @var int cmid */
protected $id;
/** @var stdclass course object */
protected $course;
/**
* Navigation passed to print_header.
* Override this to do something more specific than the module view page
* like adding more links to the breadcrumb.
*
* @return array
*/
public function get_navigation() {
// No extra navigation by default, link to the course module already included.
$extranav = array();
return array($extranav, $this->cm);
}
/**
* The url to return to after export or on cancel.
* Defaults value is set to the module 'view' page.
* Override this if it's deeper inside the module.
*
* @return string
*/
public function get_return_url() {
global $CFG;
return $CFG->wwwroot . '/mod/' . $this->cm->modname . '/view.php?id=' . $this->cm->id;
}
/**
* Override the parent get function
* to make sure when we're asked for a course,
* We retrieve the object from the database as needed.
*
* @param string $key the name of get function
* @return stdClass
*/
public function get($key) {
if ($key != 'course') {
return parent::get($key);
}
global $DB;
if (empty($this->course)) {
$this->course = $DB->get_record('course', array('id' => $this->cm->course));
}
return $this->course;
}
/**
* Return a string to put at the header summarising this export.
* by default, this function just display the name and module instance name.
* Override this to do something more specific
*
* @return string
*/
public function heading_summary() {
return get_string('exportingcontentfrom', 'portfolio', $this->display_name() . ': ' . $this->cm->name);
}
/**
* Overridden to return the course module context
*
* @param moodle_page $PAGE global PAGE
*/
public function set_context($PAGE) {
$PAGE->set_cm($this->cm);
}
}