/
lib.php
863 lines (792 loc) · 32.9 KB
/
lib.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
<?php
/**
*
* @package mahara
* @subpackage export
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
require_once('view.php');
require_once(get_config('docroot') . '/artefact/lib.php');
/**
* Helper interface to hold PluginExport's abstract static methods
*/
interface IPluginExport {
/**
* A human-readable title for the export
*/
public static function get_title();
/**
* A human-readable description for the export
*/
public static function get_description();
}
/**
* Base class for all Export plugins.
*
* This class does some basic setup for export plugins, as well as interfacing
* with the Mahara Plugin API. Mostly, the work of generating exports is
* delegated to the plugins themselves.
*
* TODO: split generation of an archive file from the export() method,
* implement zipping the export in a method in this class to reduce
* duplication.
*/
abstract class PluginExport extends Plugin implements IPluginExport {
/**
* Export all views owned by this user
*/
const EXPORT_ALL_VIEWS = -1;
/**
* Export only certain views - used internally when a list of views is
* passed to the constructor
*/
const EXPORT_LIST_OF_VIEWS = -2;
/**
* Export all artefacts owned by this user
*/
const EXPORT_ALL_ARTEFACTS = -3;
/**
* Export artefacts that are part of the views to be exported
*/
const EXPORT_ARTEFACTS_FOR_VIEWS = -4;
/**
* Export only certain artefacts - used internally when a list of artefacts
* is passed to the constructor
*/
const EXPORT_LIST_OF_ARTEFACTS = -5;
/*
* Export only certain collections and their artefacts
*/
const EXPORT_COLLECTIONS = -6;
/**
* Maximum filename length in UTF-8 encoding characters
* Most file systems (FAT, FAT32, NTFS, ext2, ext3, ext4) support the filename length of 255 bytes
* UTF-8 needs at most 3 bytes to encode
*/
const MAX_FILENAME_LENGTH = 80;
public static function get_plugintype_name() {
return 'export';
}
/**
* Where the theme assets for export plugins live. Usually it's in the normal location,
* but they can also have assets that live under artefacts.
*
* @param string $pluginname
* @return string
*/
public static function get_theme_path($pluginname) {
if (strpos($pluginname, '/')) {
// Path for export plugins that sit under an artefact plugin
// i.e. "export:html/file:index.tpl"
list($exportname, $artefactname) = explode('/', $pluginname, 2);
return 'artefact/' . $artefactname . '/export/' . $exportname;
}
else {
return parent::get_theme_path($pluginname);
}
}
/**
* Perform the export and return the path to the resulting file.
*
* @return string path to the resulting file (relative to dataroot)
*/
abstract public function export();
/**
* Perform the checks to see if there is enough space for the
* export() resulting file before trying to write it.
*
* @return bool
*/
abstract public function is_diskspace_available();
// MAIN CLASS DEFINITION
/**
* List of artefacts to export. Set up by constructor.
*/
public $artefacts = array();
/**
* List of views to export. Set up by constructor.
*/
public $views = array();
/**
* Whether the user requested to export feedback as well
*/
public $includefeedback = false;
/**
* User object for the user being exported.
*/
protected $user;
/**
* Represents the mode for exporting views - one of the class consts
* defined above
*/
protected $viewexportmode;
/**
* Represents the mode for exporting artefacts - one of the class consts
* defined above
*/
protected $artefactexportmode;
/**
* The time the export was generated.
*
* Technically, this is the time at which the export object was created,
* not the time at which export() was called.
*/
protected $exporttime;
/**
* Callback to notify when progress is made
*/
private $progresscallback = null;
/**
* Establishes exactly what views and artefacts are to be exported, and
* sets up temporary export directories
*
* Subclasses can override this if they need to do anything else, but
* they must call parent::__construct.
*
* @param User $user The user to export data for
* @param mixed $views can be:
* - PluginExport::EXPORT_ALL_VIEWS
* - array, containing:
* - int - view ids
* - stdclass objects - db rows
* - View objects
* @param mixed $artefacts can be:
* - PluginExport::EXPORT_ALL_ARTEFACTS
* - PluginExport::EXPORT_ARTEFACTS_FOR_VIEWS
* - PluginExport::EXPORT_COLLECTIONS
* - array, containing:
* - int - artefact ids
* - stdclass objects - db rows
* - ArtefactType subclasses
*/
public function __construct(User $user, $views, $artefacts, $progresscallback=null) {
if (!is_null($progresscallback)) {
if (is_callable($progresscallback)) {
$this->progresscallback = $progresscallback;
}
else {
throw new SystemException("The specified progress callback isn't callable");
}
}
$this->notify_progress_callback(0, 'Starting');
$this->exporttime = time();
$this->user = $user;
$userid = $this->user->get('id');
$tmpviews = array();
$tmpartefacts = array();
// Get the list of views to export
if ($views == self::EXPORT_ALL_VIEWS) {
$tmpviews = get_column_sql('SELECT id FROM {view} WHERE owner = ? ORDER BY id', array($userid));
$this->viewexportmode = $views;
}
else if (is_array($views) && $artefacts == self::EXPORT_COLLECTIONS) {
$tmpviews = $views;
$this->viewexportmode = self::EXPORT_COLLECTIONS;
}
else if (is_array($views)) {
$tmpviews = $views;
$this->viewexportmode = self::EXPORT_LIST_OF_VIEWS;
}
foreach ($tmpviews as $v) {
$view = null;
if ($v instanceof View) {
$view = $v;
}
else if (is_object($v)) {
$view = new View($v->id, $v);
}
else if (is_numeric($v)) {
$view = new View($v);
}
if (is_null($view)) {
throw new ParamOutOfRangeException("Invalid view $v");
}
if ($view->get('owner') != $userid) {
throw new UserException("User $userid does not own view " . $view->get('id'));
}
$this->views[$view->get('id')] = $view;
}
// Get the list of artefacts to export
if ($artefacts == self::EXPORT_ALL_ARTEFACTS) {
$query = 'SELECT id FROM {artefact} WHERE "owner" = ?';
$args = array($userid);
if (sizeof($tmpviews)) {
$query .= 'UNION
SELECT artefact
FROM {view_artefact}
WHERE "view" IN (SELECT id FROM {view} WHERE "owner" = ?)
ORDER BY id';
$args[] = $userid;
$this->artefactexportmode = $tmpartefacts = get_column_sql($query, $args);
}
}
else {
if ($tmpviews) {
$sql = "SELECT DISTINCT va.artefact
FROM {view_artefact} va
LEFT JOIN {view} v ON v.id = va.view
WHERE v.owner = ?
AND va.view IN ( " . implode(',', array_keys($this->views)) . ")
ORDER BY va.artefact";
$tmpartefacts = (array)get_column_sql($sql, array($userid));
// Some artefacts are not inside the view, but still need to be exported with it
$tmpartefacts = array_unique(array_merge($tmpartefacts, $this->get_view_extra_artefacts()));
$tmpartefacts = artefact_get_descendants($tmpartefacts);
$tmpartefacts = array_unique(array_merge($tmpartefacts, $this->get_artefact_extra_artefacts($tmpartefacts)));
}
if ($artefacts == self::EXPORT_ARTEFACTS_FOR_VIEWS) {
$this->artefactexportmode = $artefacts;
}
else if ($artefacts == self::EXPORT_COLLECTIONS) {
$this->artefactexportmode = self::EXPORT_ARTEFACTS_FOR_VIEWS;
}
else {
$tmpartefacts = array_unique(array_merge($tmpartefacts, $artefacts));
$this->artefactexportmode = self::EXPORT_LIST_OF_ARTEFACTS;
}
}
$typestoplugins = get_records_assoc('artefact_installed_type');
$ids_to_get = array();
foreach ($tmpartefacts as $a) {
if ($a instanceof ArtefactType) {
continue;
}
else if (is_object($a) && isset($a->id)) {
$ids_to_get[] = $a->id;
}
else if (is_numeric($a)) {
$ids_to_get[] = $a;
}
}
$artefacts = artefact_instances_from_ids($ids_to_get);
foreach ($tmpartefacts as $a) {
$artefact = null;
if ($a instanceof ArtefactType) {
$artefact = $a;
}
else if (is_object($a) && isset($a->id)) {
$artefact = $artefacts[$a->id];
}
else if (is_numeric($a)) {
$artefact = $artefacts[$a];
}
if (is_null($artefact)) {
throw new ParamOutOfRangeException("Invalid artefact $a");
}
// This check won't work, at the _least_ because at the time of
// writing, can_view_artefact does not support normal users viewing
// site files. This check is also pretty damn slow. So think twice
// before uncommenting it. I presume if you _are_ uncommenting it,
// it's because you're trying to isloate a security vulnerability
// where a user can export another user's files or something. In
// which case you'll be being careful anyway, I hope.
//if (!$this->user->can_view_artefact($artefact)) {
// throw new SystemException("User $userid does not own artefact " . $artefact->get('id'));
//}
if ($artefact->exportable()) {
$this->artefacts[$artefact->get('id')] = $artefact;
}
}
$this->collections = array();
if ($views === -1) {
$collections = FALSE;
}
else {
$collections = get_records_sql_assoc('
SELECT * FROM {collection} WHERE id IN (
SELECT collection
FROM {collection_view}
WHERE view IN (' . join(',', array_keys($this->views)) . ')
)',
array());
}
if ($collections) {
require_once('collection.php');
foreach ($collections as &$c) {
$this->collections[$c->id] = new Collection(0, $c);
}
}
// Now set up the temporary export directories
$this->exportdir = get_config('dataroot')
. 'export/'
. $this->user->get('id') . '/'
. $this->exporttime . '/';
if (!check_dir_exists($this->exportdir)) {
throw new SystemException("Couldn't create the temporary export directory $this->exportdir");
}
$this->messages = array();
$this->notify_progress_callback(10, 'Setup');
}
/**
* Accessor
*
* @param string $field The field to get (see the class definition to find
* which fields are available)
*/
public function get($field) {
if (!property_exists($this, $field)) {
throw new ParamOutOfRangeException("Field $field wasn't found in class " . get_class($this));
}
return $this->{$field};
}
/**
* Notifies the registered progress callback about the progress in generating the export.
*
* This is provided as exports can take a long time to generate. Export
* plugins are encouraged to call this at least after performing some major
* operation, and should always call it saying when the execution of
* export() is done. However, it is unnecessary to call it too often.
*
* For testing purposes, you may find it useful to register a progress
* callback that simply log_debug()s the data, so you can check that the
* percentage is always increasing, for example.
*
* @param int $percent The total percentage of the way through generating
* the export. The base class constructor hands over
* control claiming 10% of the work is done.
* @param string $status A string describing the current status of the
* export - e.g. 'Exporting Artefact (20/75)'
*/
protected function notify_progress_callback($percent, $status) {
if ($this->progresscallback) {
call_user_func_array($this->progresscallback, array(
$percent, $status
));
}
}
/**
* Returns embedded artefacts in view description and
* additional artefacts required for view export from artefact plugins
*/
protected function get_view_extra_artefacts() {
$extra = View::get_embedded_artefacts(array_keys($this->views));
$plugins = plugins_installed('artefact');
foreach ($plugins as &$plugin) {
safe_require('artefact', $plugin->name);
$classname = generate_class_name('artefact', $plugin->name);
if (is_callable($classname . '::view_export_extra_artefacts')) {
if ($artefacts = call_static_method($classname, 'view_export_extra_artefacts', array_keys($this->views))) {
$extra = array_unique(array_merge($extra, $artefacts));
}
}
}
return $extra;
}
protected function get_artefact_extra_artefacts(&$artefactids) {
if (empty($artefactids)) {
return array();
}
$extra = array();
$plugins = plugins_installed('artefact');
foreach ($plugins as &$plugin) {
safe_require('artefact', $plugin->name);
$classname = generate_class_name('artefact', $plugin->name);
if (is_callable($classname . '::artefact_export_extra_artefacts')) {
if ($artefacts = call_static_method($classname, 'artefact_export_extra_artefacts', $artefactids)) {
$extra = array_unique(array_merge($extra, $artefacts));
}
}
}
return $extra;
}
}
/**
* Looks in the export staging area in dataroot and deletes old, unneeded
* exports.
*/
function export_cleanup_old_exports() {
require_once('file.php');
$basedir = get_config('dataroot') . 'export/';
// If the export directory hasn't been created yet, there's no point
// running the cron.
if(!is_dir($basedir)) {
return;
}
$exportdir = new DirectoryIterator($basedir);
$mintime = time() - (24 * 60 * 60); // delete exports older than 24 hours
// The export dir contains one directory for each user who has created
// an export, named after their UID
foreach ($exportdir as $userdir) {
if ($userdir->isDot()) continue;
// Each user's directory contains one directory for each export
// they made, named as the unix timestamp of the time they
// generated it
$udir = new DirectoryIterator($basedir . $userdir->getFilename());
foreach ($udir as $dir) {
if ($dir->isDot()) continue;
if ($dir->getCTime() < $mintime) {
rmdirr($basedir . $userdir->getFilename() . '/' . $dir->getFilename());
}
}
}
// Remove any rows from the export_archive that are older than 24 hours and are not exports for submissions
delete_records_sql('DELETE FROM {export_archive} WHERE submission = 0 AND ctime < ?', array(date('Y-m-d H:i:s', $mintime)));
}
/**
* Add an export item's information to the export_queue and export_queue_items tables
*
* @param $objectarray Array of objects. Currently handles collection or view objects
* @param $external Name of the external connection
* @param $submitter Name of the submitter if different to owner
* @param $type Specify what type of export is to be done. Valid options are: 'collections', 'views', 'all'
*
* @return bool If adds to queue successfully
*/
function export_add_to_queue($object, $external = null, $submitter = null, $type = null) {
if (!is_array($object)) {
if (!($object instanceof Collection) && !($object instanceof View)) {
// not supported object type
return true;
}
$objectarray[] = $object;
}
else {
$objectarray = $object;
}
$queue = new stdClass();
if ($submitter && $submitter->get('id')) {
$queue->submitter = $submitter->get('id');
}
$owner = (!empty($objectarray[0])) ? $objectarray[0]->get('owner') : null;
if ($owner) {
$queue->usr = $objectarray[0]->get('owner');
}
else {
$queue->usr = $queue->submitter;
}
$queue->exporttype = 'leap';
if (!empty($type)) {
$queue->type = $type;
}
$queue->ctime = db_format_timestamp(time());
if ($external) {
$queue->externalid = $external->id;
}
db_begin();
$queueid = insert_record('export_queue', $queue, 'id', true);
$counter = (!empty($type) && $type == 'all') ? 1 : 0;
foreach ($objectarray as $key => $object) {
if (!($object instanceof Collection) && !($object instanceof View)) {
// not supported object type
continue;
}
if ($object instanceof Collection) {
require_once(get_config('docroot') . 'lib/view.php');
$views = $object->views();
foreach ($views['views'] as $view) {
$v = new View($view->id);
$queueitems = new stdClass();
$queueitems->exportqueueid = $queueid;
$queueitems->collection = $object->get('id');
$queueitems->view = $view->id;
insert_record('export_queue_items', $queueitems);
$counter++;
}
}
else if ($object instanceof View) {
$queueitems = new stdClass();
$queueitems->exportqueueid = $queueid;
$queueitems->view = $object->get('id');
insert_record('export_queue_items', $queueitems);
$counter++;
}
}
if (empty($counter)) {
db_rollback();
}
else {
db_commit();
}
return true;
}
/**
* cron job to process the export queue
* @param string $id Specify which row of export_queue table you want to run - could be used for debugging purposes
*/
function export_process_queue($id = false) {
$where = 'starttime IS NULL';
$values = array();
if ($id) {
$where .= ' AND id = ?';
$values = array($id);
}
// Try getting the first 100 items in queue - TODO; work out a good number to get at once
if (!$ready = get_records_select_array('export_queue', $where, $values, 'ctime', '*', 0, 100)) {
return true;
}
$now = date('Y-m-d H:i:s', time());
foreach ($ready as $row) {
// If there server is getting too busy we abort and wait for next cron run.
if (server_busy()) {
log_info('too busy');
return true;
}
$errors = array();
// update the item with start process time
execute_sql('UPDATE {export_queue} SET starttime = ? WHERE id = ?', array($now, $row->id));
$items = get_records_select_array('export_queue_items', 'exportqueueid = ?', array($row->id), 'id');
if (!$items && $row->type == 'all') {
$items = array();
$row->what = 'all';
}
$views = array();
// To make sure we process the item with this id only once we keep a track of the $lastid
// We don't know if the $item will be a collection or view (or artefact possibly in the future)
// In the case of a user exporting to leap2a there can be a number of collections/views to deal
// with so we want to deal with each collection or view only once.
$lastid = '';
$submitted = false;
foreach ($items as $key => $item) {
if (!empty($item->collection) && $lastid != 'collection_' . $item->collection) {
$row->what = 'collections';
$lastid = 'collection_' . $item->collection;
$views = array_merge($views, get_column('collection_view', 'view', 'collection', $item->collection));
$submitted = get_record('collection', 'id', $item->collection);
}
else if (empty($item->collection) && !empty($item->view) && $lastid != 'view_' . $item->view) {
$row->what = 'views';
$lastid = 'view_' . $item->view;
$views = array_merge($views, array($item->view));
$submitted = get_record('view', 'id', $item->view);
}
}
$views = array_unique($views);
// Bail if we don't have enough data to do an export
if (!isset($row->exporttype)
|| !isset($row->what)
|| !isset($views)) {
$errors[] = get_string('unabletogenerateexport', 'export');
log_warn(get_string('unabletogenerateexport', 'export'));
continue;
}
safe_require('export', $row->exporttype);
$user = new User();
$user->find_by_id($row->usr);
$class = generate_class_name('export', $row->exporttype);
switch($row->what) {
case 'all':
$exporter = new $class($user, PluginExport::EXPORT_ALL_VIEWS, PluginExport::EXPORT_ALL_ARTEFACTS);
break;
case 'views':
$exporter = new $class($user, $views, PluginExport::EXPORT_ARTEFACTS_FOR_VIEWS);
break;
case 'collections':
$exporter = new $class($user, $views, PluginExport::EXPORT_COLLECTIONS);
break;
default:
$errors[] = get_string('unabletoexportportfoliousingoptionsadmin', 'export');
log_warn(get_string('unabletoexportportfoliousingoptionsadmin', 'export'));
}
$exporter->includefeedback = false; // currently only doing leap2a exports and they can't handle feedback
// Get an estimate of how big the unzipped export file would be
// so we can check that we have enough disk space for it
$space = $exporter->is_diskspace_available();
if (!$space) {
$errors[] = get_string('exportfiletoobig', 'mahara');
log_warn(get_string('exportfiletoobig', 'mahara'));
}
try {
$zipfile = $exporter->export();
}
catch (SystemException $e) {
$errors[] = get_string('exportzipfileerror', 'export', $e->getMessage());
log_warn($e->getMessage());
}
$filepath = $exporter->get('exportdir');
// If export is a submission we need to save this from being deleted by the export_cleanup_old_exports cron
// so we need to put it somewhere safe
if (!empty($submitted->submittedtime)) {
// Now set up the export submission directories
$submissiondir = get_config('dataroot')
. 'submission/'
. $row->usr . '/';
if (!check_dir_exists($submissiondir)) {
$errors[] = get_string('submissiondirnotwritable', 'export', $submissiondir);
}
else {
copy($filepath . $zipfile, $submissiondir . $zipfile);
$filepath = $submissiondir;
}
}
$filetitle = '';
if (!empty($row->type)) {
switch ($row->type) {
case 'all':
$filetitle = get_string('allmydata', 'export');
break;
default:
$filetitle = get_string('exporting' . $row->type, 'export');
}
}
else {
$filetitle = !empty($submitted->name) ? $submitted->name : $submitted->title;
}
$externalhost = !empty($submitted->submittedhost) ? $submitted->submittedhost : null;
db_begin();
// Need to record this in the export_archive table so one can fetch the file via a download link
$archiveid = insert_record('export_archive', (object) array('usr' => $row->usr,
'filename' => $zipfile,
'filetitle' => $filetitle,
'filepath' => $filepath,
'submission' => ((!empty($submitted->submittedtime)) ? 1 : 0),
'ctime' => db_format_timestamp(time()),
), 'id', true);
if (!$archiveid) {
$errors[] = get_string('exportarchivesavefailed', 'export');
}
// If the export row is for a submitted view/collection
if (!empty($submitted->submittedtime)) {
$inserted = insert_record('archived_submissions', (object) array('archiveid' => $archiveid,
'group' => $submitted->submittedgroup,
'externalhost' => $externalhost,
'externalid' => $row->externalid,
));
if (!$inserted) {
$errors[] = get_string('archivedsubmissionfailed', 'export');
}
require_once(get_config('docroot') . 'lib/view.php');
if ($submitted->submittedstatus == View::PENDING_RELEASE) {
// we are running this export as part of the releasing submission process
if ($row->what == 'collections') {
require_once(get_config('docroot') . 'lib/collection.php');
$id = substr($lastid, strlen('collection_'));
$collection = new Collection($id);
try {
$collection->release($row->submitter);
}
catch (SystemException $e) {
$errors[] = get_string('submissionreleasefailed', 'export');
log_warn($e->getMessage());
}
}
else if ($row->what == 'views') {
$id = substr($lastid, strlen('view_'));
$view = new View($id);
try {
$view->release($row->submitter);
}
catch (SystemException $e) {
$errors[] = get_string('submissionreleasefailed', 'export');
log_warn($e->getMessage());
}
}
else {
$errors[] = get_string('submissionreleasefailed', 'export');
}
}
}
else {
// Need to send emails with the download link in them - so we add the data to the activity_queue table
$arg = display_name($row->usr);
$data = (object) array(
'subject' => false,
'message' => false,
'strings' => (object) array(
'subject' => (object) array(
'key' => 'exportdownloademailsubject',
'section' => 'admin',
'args' => array($filetitle),
),
'message' => (object) array(
'key' => 'exportdownloademailmessage',
'section' => 'admin',
'args' => array(hsc($arg), $filetitle),
),
'urltext' => (object) array(
'key' => 'exportdownloadurl',
'section' => 'admin',
),
),
'users' => array($row->usr),
'url' => get_config('webroot') . 'downloadarchive.php?id=' . $archiveid,
);
activity_occurred('maharamessage', $data);
}
// finally delete the queue item
if (!delete_records('export_queue_items', 'exportqueueid', $row->id)) {
$errors[] = get_string('deleteexportqueueitems', 'export', $row->id);
log_warn('Unable to delete export queue items for ID: ' . $row->id);
}
if (!delete_records('export_queue', 'id', $row->id)) {
$errors[] = get_string('deleteexportqueuerow', 'export', $row->id);
log_warn('Unable to delete export queue row ID: ' . $row->id);
}
// if there are any errors then we need to alert the site and institution admins
if (!empty($errors)) {
$admins = get_column('usr', 'id', 'admin', 1, 'deleted', 0);
$institutions = $user->get('institutions');
if (!empty($institutions)) {
foreach ($institutions as $key => $value) {
require_once(get_config('docroot') . 'lib/institution.php');
$institution = new Institution($key);
$admins = array_merge($admins, $institution->admins());
}
}
$arg = "\n\n -" . implode("\n - ", $errors);
$data = (object) array(
'subject' => false,
'message' => false,
'strings' => (object) array(
'subject' => (object) array(
'key' => 'exportqueueerrorsadminsubject',
'section' => 'export',
),
'message' => (object) array(
'key' => 'exportqueueerrorsadminmessage',
'section' => 'export',
'args' => array(hsc($row->id), hsc($arg)),
),
'urltext' => (object) array(
'key' => 'exportdownloadurl',
'section' => 'admin',
),
),
'users' => $admins,
'url' => get_config('webroot') . 'admin/users/exportqueue.php',
);
activity_occurred('maharamessage', $data);
db_rollback();
}
else {
db_commit();
}
}
return true;
}
/**
* In between function for adding thing to export queue that are submitted items.
* Would be useful if we need to do special checking/handling of these compared to normal exports.
* Currently only passes thru the variables.
*/
function add_submission_to_export_queue($object, $submitter) {
return export_add_to_queue($object, null, $submitter);
}
/**
* Check to see if any archived submission leap2a files have been removed from server
* and if so then remove the corresponding database information.
*/
function submissions_delete_removed_archive() {
$remove = array();
// Get all the items in the archived_submissions table
$results = get_records_sql_assoc("SELECT * FROM {export_archive} e JOIN {archived_submissions} a ON e.id = a.archiveid", array());
if (!empty($results)) {
foreach ($results as $key => $result) {
// make sure the archive file is still on server (not removed by server admin)
if (!file_exists($result->filepath . $result->filename)) {
$remove[] = $result->archiveid;
}
}
}
if (!empty($remove)) {
// we have some export_archive row ids so lets remove the rows
$idstr = join(',', $remove);
db_begin();
delete_records_select('archived_submissions', 'archiveid IN (' . $idstr . ')');
delete_records_select('export_archive', 'id IN (' . $idstr . ')');
db_commit();
}
}