forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfoliolib.php
2200 lines (1972 loc) · 73.5 KB
/
portfoliolib.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* this file contains:
* {@link portfolio_add_button} -entry point for callers
* {@link class portfolio_plugin_base} - class plugins extend
* {@link class portfolio_caller_base} - class callers extend
* {@link class portfolio_admin_form} - base moodleform class for plugin administration
* {@link class portfolio_user_form} - base moodleform class for plugin instance user config
* {@link class portfolio_export_form} - base moodleform class for during-export configuration (eg metadata)
* {@link class portfolio_exporter} - class used during export process
*
* and some helper functions:
* {@link portfolio_instances - returns an array of all configured instances
* {@link portfolio_instance - returns an instance of the right class given an id
* {@link portfolio_instance_select} - returns a drop menu of available instances
* {@link portfolio_static_function - requires the file, and calls a static function on the given class
" {@link portfolio_plugin_sanity_check - polls given (or all) portfolio_plugins for sanity and disables insane ones
" {@link portfolio_instance_sanity_check - polls given (or all) portfolio instances for sanity and disables insane ones
* {@link portfolio_report_instane} - returns a table of insane plugins and the reasons (used for plugins or instances thereof)
* {@link portfolio_supported_formats - returns array of all available formats for plugins and callers to use
* {@link portfolio_handle_event} - event handler for queued transfers that get triggered on cron
*
*/
require_once ($CFG->libdir.'/formslib.php');
// **** EXPORT STAGE CONSTANTS **** //
/**
* display a form to the user
* this one might not be used if neither
* the plugin, or the caller has any config.
*/
define('PORTFOLIO_STAGE_CONFIG', 1);
/**
* summarise the form and ask for confirmation
* if we skipped PORTFOLIO_STAGE_CONFIG,
* just confirm the send.
*/
define('PORTFOLIO_STAGE_CONFIRM', 2);
/**
* either queue the event and skip to PORTFOLIO_STAGE_FINISHED
* or continue to PORTFOLIO_STAGE_PACKAGE
*/
define('PORTFOLIO_STAGE_QUEUEORWAIT', 3);
/**
* package up the various bits
* during this stage both the caller
* and the plugin get their package methods called
*/
define('PORTFOLIO_STAGE_PACKAGE', 4);
/*
* the portfolio plugin must send the file
*/
define('PORTFOLIO_STAGE_SEND', 5);
/**
* cleanup the temporary area
*/
define('PORTFOLIO_STAGE_CLEANUP', 6);
/**
* display the "finished notification"
*/
define('PORTFOLIO_STAGE_FINISHED', 7);
// **** EXPORT FORMAT CONSTANTS **** //
// these should always correspond to a string
// in the portfolio module, called format_{$value}
// **** **** //
/**
* file - the most basic fallback format.
* this should always be supported
* in remote system.s
*/
define('PORTFOLIO_FORMAT_FILE', 'file');
/**
* moodle backup - the plugin needs to be able to write a complete backup
* the caller need to be able to export the particular XML bits to insert
* into moodle.xml (?and the file bits if necessary)
*/
define('PORTFOLIO_FORMAT_MBKP', 'mbkp');
// **** EXPORT TIME LEVELS **** //
// these should correspond to a string
// in the portfolio module, called time_{$value}
/**
* no delay. don't even offer the user the option
* of not waiting for the transfer
*/
define('PORTFOLIO_TIME_LOW', 'low');
/**
* a small delay. user can still easily opt to
* watch this transfer and wait.
*/
define('PORTFOLIO_TIME_MODERATE', 'moderate');
/**
* slow. the user really should not be given the option
* to choose this.
*/
define('PORTFOLIO_TIME_HIGH', 'high');
/**
* entry point to add an 'add to portfolio' button somewhere in moodle
* this function does not check permissions. the caller must check permissions first.
* later, during the export process, the caller class is instantiated and the check_permissions method is called
* but not in this function.
*
* @param string $callbackclass name of the class containing the callback functions
* activity modules should ALWAYS use their name_portfolio_caller
* other locations must use something unique
* @param mixed $callbackargs this can be an array or hash of arguments to pass
* back to the callback functions (passed by reference)
* these MUST be primatives to be added as hidden form fields.
* and the values get cleaned to PARAM_ALPHAEXT or PARAM_NUMBER or PARAM_PATH
* @param string $callbackfile this can be autodetected if it's in the same file as your caller,
* but more often, the caller is a script.php and the class in a lib.php
* so you can pass it here if necessary.
* this path should be relative (ie, not include) dirroot
* @param boolean $fullform either display the fullform with the dropmenu of available instances
* or just a small icon (which will trigger instance selection in a new screen)
* optional, defaults to true.
* @param boolean $return whether to echo or return content (optional defaults to false (echo)
*/
function portfolio_add_button($callbackclass, $callbackargs, $callbackfile=null, $fullform=true, $return=false) {
global $SESSION, $CFG, $COURSE, $USER;
if (empty($CFG->portfolioenabled)) {
return;
}
if (!$instances = portfolio_instances()) {
return;
}
if (defined('PORTFOLIO_INTERNAL')) {
// something somewhere has detected a risk of this being called during inside the preparation
// eg forum_print_attachments
return;
}
if (isset($SESSION->portfolioexport)) {
print_error('alreadyexporting', 'portfolio');
}
if (empty($callbackfile)) {
$backtrace = debug_backtrace();
if (!array_key_exists(0, $backtrace) || !array_key_exists('file', $backtrace[0]) || !is_readable($backtrace[0]['file'])) {
debugging(get_string('nocallbackfile', 'portfolio'));
return;
}
$callbackfile = substr($backtrace[0]['file'], strlen($CFG->dirroot));
} else {
if (!is_readable($CFG->dirroot . $callbackfile)) {
debugging(get_string('nocallbackfile', 'portfolio'));
return;
}
}
require_once($CFG->dirroot . $callbackfile);
$callersupports = call_user_func(array($callbackclass, 'supported_formats'));
$output = '<form method="post" action="' . $CFG->wwwroot . '/portfolio/add.php" id="portfolio-add-button">' . "\n";
foreach ($callbackargs as $key => $value) {
if (!empty($value) && !is_string($value) && !is_numeric($value)) {
$a->key = $key;
$a->value = print_r($value, true);
debugging(get_string('nonprimative', 'portfolio', $a));
return;
}
$output .= "\n" . '<input type="hidden" name="ca_' . $key . '" value="' . $value . '" />';
}
$output .= "\n" . '<input type="hidden" name="callbackfile" value="' . $callbackfile . '" />';
$output .= "\n" . '<input type="hidden" name="callbackclass" value="' . $callbackclass . '" />';
$output .= "\n" . '<input type="hidden" name="course" value="' . (!empty($COURSE) ? $COURSE->id : 0) . '" />';
$selectoutput = '';
if (count($instances) == 1) {
$instance = array_shift($instances);
if (count(array_intersect($callersupports, $instance->supported_formats())) == 0) {
// bail. no common formats.
debugging(get_string('nocommonformats', 'portfolio', $callbackclass));
return;
}
if ($error = portfolio_instance_sanity_check($instance)) {
// bail, plugin is misconfigured
debugging(get_string('instancemisconfigured', 'portfolio', get_string($error[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))));
return;
}
$output .= "\n" . '<input type="hidden" name="instance" value="' . $instance->get('id') . '" />';
}
else {
$selectoutput = portfolio_instance_select($instances, $callersupports, $callbackclass, 'instance', true);
}
if ($fullform) {
$output .= $selectoutput;
$output .= "\n" . '<input type="submit" value="' . get_string('addtoportfolio', 'portfolio') .'" />';
} else {
$output .= "\n" . '<input type="image" src="' . $CFG->pixpath . '/t/portfolio.gif" alt=' . get_string('addtoportfolio', 'portfolio') .'" />';
//@todo replace this with a little icon
}
$output .= "\n" . '</form>';
if ($return) {
return $output;
} else {
echo $output;
}
return true;
}
/**
* returns a drop menu with a list of available instances.
*
* @param array $instances the instances to put in the menu
* @param array $callerformats the formats the caller supports
(this is used to filter plugins)
* @param array $callbackclass the callback class name
*
* @return string the html, from <select> to </select> inclusive.
*/
function portfolio_instance_select($instances, $callerformats, $callbackclass, $selectname='instance', $return=false, $returnarray=false) {
global $CFG;
if (empty($CFG->portfolioenabled)) {
return;
}
$insane = portfolio_instance_sanity_check();
$count = 0;
$selectoutput = "\n" . '<select name="' . $selectname . '">' . "\n";
foreach ($instances as $instance) {
if (count(array_intersect($callerformats, $instance->supported_formats())) == 0) {
// bail. no common formats.
continue;
}
if (array_key_exists($instance->get('id'), $insane)) {
// bail, plugin is misconfigured
debugging(get_string('instancemisconfigured', 'portfolio', get_string($insane[$instance->get('id')], 'portfolio_' . $instance->get('plugin'))));
continue;
}
$count++;
$selectoutput .= "\n" . '<option value="' . $instance->get('id') . '">' . $instance->get('name') . '</option>' . "\n";
$options[$instance->get('id')] = $instance->get('name');
}
if (empty($count)) {
// bail. no common formats.
debugging(get_string('nocommonformats', 'portfolio', $callbackclass));
return;
}
$selectoutput .= "\n" . "</select>\n";
if (!empty($returnarray)) {
return $options;
}
if (!empty($return)) {
return $selectoutput;
}
echo $selectoutput;
}
/**
* return all portfolio instances
*
* @param boolean visibleonly don't include hidden instances (defaults to true and will be overridden to true if the next parameter is true)
* @param boolean useronly check the visibility preferences and permissions of the logged in user
* @return array of portfolio instances (full objects, not just database records)
*/
function portfolio_instances($visibleonly=true, $useronly=true) {
global $DB, $USER;
$values = array();
$sql = 'SELECT * FROM {portfolio_instance}';
if ($visibleonly || $useronly) {
$values[] = 1;
$sql .= ' WHERE visible = ?';
}
if ($useronly) {
$sql .= ' AND id NOT IN (
SELECT instance FROM {portfolio_instance_user}
WHERE userid = ? AND name = ? AND value = ?
)';
$values = array_merge($values, array($USER->id, 'visible', 0));
}
$sql .= ' ORDER BY name';
$instances = array();
foreach ($DB->get_records_sql($sql, $values) as $instance) {
$instances[$instance->id] = portfolio_instance($instance->id, $instance);
}
// @todo check capabilities here - see MDL-15768
return $instances;
}
/**
* supported formats that portfolio plugins and callers
* can use for exporting content
*
* @return array of all the available export formats
*/
function portfolio_supported_formats() {
return array(
PORTFOLIO_FORMAT_FILE,
/*PORTFOLIO_FORMAT_MBKP, */ // later
/*PORTFOLIO_FORMAT_PIOP, */ // also later
);
}
/**
* helper function to return an instance of a plugin (with config loaded)
*
* @param int $instance id of instance
* @param array $record database row that corresponds to this instance
* this is passed to avoid unnecessary lookups
*
* @return subclass of portfolio_plugin_base
*/
function portfolio_instance($instanceid, $record=null) {
global $DB, $CFG;
if ($record) {
$instance = $record;
} else {
if (!$instance = $DB->get_record('portfolio_instance', array('id' => $instanceid))) {
return false; // @todo throw exception?
}
}
require_once($CFG->dirroot . '/portfolio/type/'. $instance->plugin . '/lib.php');
$classname = 'portfolio_plugin_' . $instance->plugin;
return new $classname($instanceid, $instance);
}
/**
* helper function to call a static function on a portfolio plugin class
* this will figure out the classname and require the right file and call the function.
* you can send a variable number of arguments to this function after the first two
* and they will be passed on to the function you wish to call.
*
* @param string $plugin name of plugin
* @param string $function function to call
*/
function portfolio_static_function($plugin, $function) {
global $CFG;
$pname = null;
if (is_object($plugin) || is_array($plugin)) {
$plugin = (object)$plugin;
$pname = $plugin->name;
} else {
$pname = $plugin;
}
$args = func_get_args();
if (count($args) <= 2) {
$args = array();
}
else {
array_shift($args);
array_shift($args);
}
require_once($CFG->dirroot . '/portfolio/type/' . $plugin . '/lib.php');
return call_user_func_array(array('portfolio_plugin_' . $plugin, $function), $args);
}
/**
* helper function to check all the plugins for sanity and set any insane ones to invisible.
*
* @param array $plugins to check (if null, defaults to all)
* one string will work too for a single plugin.
*
* @return array array of insane instances (keys= id, values = reasons (keys for plugin lang)
*/
function portfolio_plugin_sanity_check($plugins=null) {
global $DB;
if (is_string($plugins)) {
$plugins = array($plugins);
} else if (empty($plugins)) {
$plugins = get_list_of_plugins('portfolio/type');
}
$insane = array();
foreach ($plugins as $plugin) {
if ($result = portfolio_static_function($plugin, 'plugin_sanity_check')) {
$insane[$plugin] = $result;
}
}
if (empty($insane)) {
return array();
}
list($where, $params) = $DB->get_in_or_equal(array_keys($insane));
$where = ' plugin ' . $where;
$DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params);
return $insane;
}
/**
* helper function to check all the instances for sanity and set any insane ones to invisible.
*
* @param array $instances to check (if null, defaults to all)
* one instance or id will work too
*
* @return array array of insane instances (keys= id, values = reasons (keys for plugin lang)
*/
function portfolio_instance_sanity_check($instances=null) {
global $DB;
if (empty($instances)) {
$instances = portfolio_instances(false);
} else if (!is_array($instances)) {
$instances = array($instances);
}
$insane = array();
foreach ($instances as $instance) {
if (is_object($instance) && !($instance instanceof portfolio_plugin_base)) {
$instance = portfolio_instance($instance->id, $instance);
} else if (is_numeric($instance)) {
$instance = portfolio_instance($instance);
}
if (!($instance instanceof portfolio_plugin_base)) {
debugging('something weird passed to portfolio_instance_sanity_check, not subclass or id');
continue;
}
if ($result = $instance->instance_sanity_check()) {
$insane[$instance->get('id')] = $result;
}
}
if (empty($insane)) {
return array();
}
list ($where, $params) = $DB->get_in_or_equal(array_keys($insane));
$where = ' id ' . $where;
$DB->set_field_select('portfolio_instance', 'visible', 0, $where, $params);
return $insane;
}
/**
* helper function to display a table of plugins (or instances) and reasons for disabling
*
* @param array $insane array of insane plugins (key = plugin (or instance id), value = reason)
* @param array $instances if reporting instances rather than whole plugins, pass the array (key = id, value = object) here
*
*/
function portfolio_report_insane($insane, $instances=false, $return=false) {
if (empty($insane)) {
return;
}
static $pluginstr;
if (empty($pluginstr)) {
$pluginstr = get_string('plugin', 'portfolio');
}
if ($instances) {
$headerstr = get_string('someinstancesdisabled', 'portfolio');
} else {
$headerstr = get_string('somepluginsdisabled', 'portfolio');
}
$output = notify($headerstr, 'notifyproblem', 'center', true);
$table = new StdClass;
$table->head = array($pluginstr, '');
$table->data = array();
foreach ($insane as $plugin => $reason) {
if ($instances) {
// @todo this isn't working
// because it seems the new recordset object
// doesn't implement the key correctly.
// see MDL-15798
$instance = $instances[$plugin];
$plugin = $instance->get('plugin');
$name = $instance->get('name');
} else {
$name = $plugin;
}
$table->data[] = array($name, get_string($reason, 'portfolio_' . $plugin));
}
$output .= print_table($table, true);
$output .= '<br /><br /><br />';
if ($return) {
return $output;
}
echo $output;
}
/**
* fake the url to portfolio/add.php from data from somewhere else
* you should use portfolio_add_button instead 99% of the time
*
* @param int $instanceid instanceid (optional, will force a new screen if not specified)
* @param string $classname callback classname
* @param string $classfile file containing the callback class definition
* @param array $callbackargs arguments to pass to the callback class
*/
function portfolio_fake_add_url($instanceid, $classname, $classfile, $callbackargs) {
global $CFG;
$url = $CFG->wwwroot . '/portfolio/add.php?instance=' . $instanceid . '&callbackclass=' . $classname . '&callbackfile=' . $classfile;
if (is_object($callbackargs)) {
$callbackargs = (array)$callbackargs;
}
if (!is_array($callbackargs) || empty($callbackargs)) {
return $url;
}
foreach ($callbackargs as $key => $value) {
$url .= '&ca_' . $key . '=' . urlencode($value);
}
return $url;
}
/**
* base class for the caller
* places in moodle that want to display
* the add form should subclass this for their callback.
*/
abstract class portfolio_caller_base {
/**
* stdclass object
* course that was active during the caller
*/
protected $course;
/**
* named array of export config
* use{@link set_export_config} and {@link get_export_config} to access
*/
protected $exportconfig;
/**
* stdclass object
* user currently exporting content
*/
protected $user;
/**
* a reference to the exporter object
*/
protected $exporter;
/**
*
*/
private $stage;
/**
* if this caller wants any additional config items
* they should be defined here.
*
* @param array $mform moodleform object (passed by reference) to add elements to
* @param object $instance subclass of portfolio_plugin_base
* @param integer $userid id of user exporting content
*/
public function export_config_form(&$mform, $instance) {}
/**
* whether this caller wants any additional
* config during export (eg options or metadata)
*
* @return boolean
*/
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.
* @return array keyvalue pairs - form element => error string
*/
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 exmaple even if a huge file is being sent,
* the download portfolio plugin doesn't care )
*
* @return string (see PORTFOLIO_TIME_* constants)
*/
public abstract function expected_time();
/**
* used for displaying the navigation during the export screens.
*
* this function must be implemented, but can really return anything.
* an Exporting.. string will be added on the end.
* @return array of $extranav and $cm
*
* to pass to build_navigation
*
*/
public abstract function get_navigation();
/**
*
*/
public abstract function get_sha1();
/*
* generic getter for properties belonging to this instance
* <b>outside</b> the subclasses
* like name, visible etc.
*
* @todo determine what to return in the error case
*/
public function get($field) {
if (property_exists($this, $field)) {
return $this->{$field};
}
return false; // @todo throw exception?
}
/**
* generic setter for properties belonging to this instance
* <b>outside</b> the subclass
* like name, visible, etc.
*
* @todo determine what to return in the error case
*/
public final function set($field, &$value) {
if (property_exists($this, $field)) {
$this->{$field} =& $value;
$this->dirty = true;
return true;
}
return false; // @todo throw exception?
}
/**
* stores the config generated at export time.
* subclasses can retrieve values using
* {@link 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)) {
continue; // @ todo throw exception
}
$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
* @todo figure out the error cases (item not found or not allowed)
*/
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)) {
return false; // @todo throw exception?
}
if (!array_key_exists($key, $this->exportconfig)) {
return null; // @todo what to return|
}
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 array array of config items.
*/
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 {@see copy_existing_file}
* or {@see write_new_file}
*/
public abstract function prepare_package();
/**
* array of formats this caller supports
* the intersection of what this function returns
* and what the selected portfolio plugin supports
* will be used
* use the constants PORTFOLIO_FORMAT_*
*
* @return array list of formats
*/
public static function supported_formats() {
return array(PORTFOLIO_FORMAT_FILE);
}
/**
* this is the "return to where you were" url
*
* @return string 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();
/**
* nice name to display to the user about this caller location
*/
public abstract static function display_name();
}
abstract class portfolio_module_caller_base extends portfolio_caller_base {
protected $cm;
protected $course;
public function get_navigation() {
$extranav = array('name' => $this->cm->name, 'link' => $this->get_return_url());
return array($extranav, $this->cm);
}
public function get_return_url() {
global $CFG;
return $CFG->wwwroot . '/mod/' . $this->cm->modname . '/view.php?id=' . $this->cm->id;
}
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;
}
}
/**
* the base class for portfolio plguins
* all plugins must subclass this.
*/
abstract class portfolio_plugin_base {
/**
* boolean
* whether this object needs writing out to the database
*/
protected $dirty;
/**
* integer
* id of instance
*/
protected $id;
/**
* string
* name of instance
*/
protected $name;
/**
* string
* plugin this instance belongs to
*/
protected $plugin;
/**
* boolean
* whether this instance is visible or not
*/
protected $visible;
/**
* named array
* admin configured config
* use {@link set_config} and {@get_config} to access
*/
protected $config;
/**
*
* user config cache
* named array of named arrays
* keyed on userid and then on config field => value
* use {@link get_user_config} and {@link set_user_config} to access.
*/
protected $userconfig;
/**
* named array
* export config during export
* use {@link get_export_config} and {@link set export_config} to access.
*/
protected $exportconfig;
/**
* stdclass object
* user currently exporting data
*/
protected $user;
/**
* a reference to the exporter object
*/
protected $exporter;
/**
* array of formats this portfolio supports
* the intersection of what this function returns
* and what the caller supports will be used
* use the constants PORTFOLIO_FORMAT_*
*
* @return array list of formats
*/
public static function supported_formats() {
return array(PORTFOLIO_FORMAT_FILE);
}
/**
* 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
*
* @param string $callertime - what the caller thinks
* the portfolio plugin instance
* is given the final say
* because it might be (for example) download.
* @return string (see PORTFOLIO_TIME_* constants)
*/
public abstract function expected_time($callertime);
/**
* check sanity of plugin
* if this function returns something non empty, ALL instances of your plugin
* will be set to invisble and not be able to be set back until it's fixed
*
* @return mixed - string = error string KEY (must be inside plugin_$yourplugin) or 0/false if you're ok
*/
public static function plugin_sanity_check() {
return 0;
}
/**
* check sanity of instances
* if this function returns something non empty, the instance will be
* set to invislbe and not be able to be set back until it's fixed.
*
* @return mixed - string = error string KEY (must be inside plugin_$yourplugin) or 0/false if you're ok
*/
public function instance_sanity_check() {
return 0;
}
/**
* does this plugin need any configuration by the administrator?
*
* if you override this to return true,
* you <b>must</b> implement {@link} admin_config_form
*/
public static function has_admin_config() {
return false;
}
/**
* can this plugin be configured by the user in their profile?
*
* if you override this to return true,
* you <b>must</b> implement {@link user_config_form
*/
public function has_user_config() {
return false;
}
/**
* does this plugin need configuration during export time?
*
* if you override this to return true,
* you <b>must</b> implement {@link export_config_form}
*/
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.
* @return array keyvalue pairs - form element => error string
*/
public function export_config_validation() {}
/**
* 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.
* @return array keyvalue pairs - form element => error string
*/
public function user_config_validation() {}
/**
* sets the export time config from the moodle form.
* you can also use this to set export config that
* isn't actually controlled by the user
* eg things that your subclasses want to keep in state
* across the export.
* keys must be in {@link get_allowed_export_config}
*
* this is deliberately not final (see boxnet plugin)
*
* @param array $config named array of config items to set.
*/
public 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)) {
continue; // @ todo throw exception
}
$this->exportconfig[$key] = $value;
}
}
/**
* gets an export time config value.
* subclasses should not override this.
*
* @param string key field to fetch
*
* @return string config value
*
* @todo figure out the error cases
*/
public final function get_export_config($key) {
$allowed = array_merge(