lib.php 33 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage export
 * @author     Catalyst IT Ltd
7
8
 * @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.
9
10
11
12
13
14
15
16
 *
 */

defined('INTERNAL') || die();

require_once('view.php');
require_once(get_config('docroot') . '/artefact/lib.php');

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * 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();
}

32
/**
33
34
 * Base class for all Export plugins.
 *
Aaron Wells's avatar
Aaron Wells committed
35
36
 * 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
37
38
 * delegated to the plugins themselves.
 *
Aaron Wells's avatar
Aaron Wells committed
39
40
 * TODO: split generation of an archive file from the export() method,
 * implement zipping the export in a method in this class to reduce
41
42
 * duplication.
 */
43
abstract class PluginExport extends Plugin implements IPluginExport {
44
45

    /**
46
47
48
49
50
     * Export all views owned by this user
     */
    const EXPORT_ALL_VIEWS = -1;

    /**
Aaron Wells's avatar
Aaron Wells committed
51
     * Export only certain views - used internally when a list of views is
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
     * 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;

    /**
Aaron Wells's avatar
Aaron Wells committed
67
     * Export only certain artefacts - used internally when a list of artefacts
68
69
70
71
     * is passed to the constructor
     */
    const EXPORT_LIST_OF_ARTEFACTS = -5;

72
73
74
75
76
    /*
     * Export only certain collections and their artefacts
     */
    const EXPORT_COLLECTIONS = -6;

77
78
79
80
81
82
83
    /**
     * 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;

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    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);
        }
    }

107
108
109
    /**
     * Perform the export and return the path to the resulting file.
     *
110
     * @return string path to the resulting file (relative to dataroot)
111
     */
112
113
    abstract public function export();

114
115
116
117
118
119
120
121
    /**
     * 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();

122
123
124
    //  MAIN CLASS DEFINITION

    /**
125
     * List of artefacts to export. Set up by constructor.
126
     */
127
    public $artefacts = array();
128
129

    /**
130
     * List of views to export. Set up by constructor.
131
     */
132
    public $views = array();
133

134
135
136
137
138
    /**
     * Whether the user requested to export feedback as well
     */
    public $includefeedback = false;

139
    /**
140
141
     * User object for the user being exported.
     */
142
143
    protected $user;

144
    /**
Aaron Wells's avatar
Aaron Wells committed
145
     * Represents the mode for exporting views - one of the class consts
146
     * defined above
147
148
149
150
     */
    protected $viewexportmode;

    /**
Aaron Wells's avatar
Aaron Wells committed
151
     * Represents the mode for exporting artefacts - one of the class consts
152
     * defined above
153
154
155
     */
    protected $artefactexportmode;

156
157
158
    /**
     * The time the export was generated.
     *
Aaron Wells's avatar
Aaron Wells committed
159
     * Technically, this is the time at which the export object was created,
160
     * not the time at which export() was called.
161
     */
162
    protected $exporttime;
163

164
    /**
165
     * Callback to notify when progress is made
166
167
168
     */
    private $progresscallback = null;

169
    /**
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
     * 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
186
     *                         - PluginExport::EXPORT_COLLECTIONS
187
188
189
190
191
     *                         - array, containing:
     *                             - int - artefact ids
     *                             - stdclass objects - db rows
     *                             - ArtefactType subclasses
     */
192
    public function __construct(User $user, $views, $artefacts, $progresscallback=null) {
193
194
195
196
197
198
199
200
201
202
        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');

203
        $this->exporttime = time();
204
        $this->user = $user;
205

206
207
208
        $userid = $this->user->get('id');
        $tmpviews = array();
        $tmpartefacts = array();
209
210

        // Get the list of views to export
211
        if ($views == self::EXPORT_ALL_VIEWS) {
212
            $tmpviews = get_column_sql('SELECT id FROM {view} WHERE owner = ? ORDER BY id', array($userid));
213
            $this->viewexportmode = $views;
214
        }
215
216
217
218
        else if (is_array($views) && $artefacts == self::EXPORT_COLLECTIONS) {
            $tmpviews = $views;
            $this->viewexportmode = self::EXPORT_COLLECTIONS;
        }
219
220
        else if (is_array($views)) {
            $tmpviews = $views;
221
            $this->viewexportmode = self::EXPORT_LIST_OF_VIEWS;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        }
        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");
            }
237
238
239
            if ($view->get('owner') != $userid) {
                throw new UserException("User $userid does not own view " . $view->get('id'));
            }
240
241
242
            $this->views[$view->get('id')] = $view;
        }

243
        // Get the list of artefacts to export
244
        if ($artefacts == self::EXPORT_ALL_ARTEFACTS) {
245
246
247
248
249
250
            $query = 'SELECT id FROM {artefact} WHERE "owner" = ?';
            $args = array($userid);
            if (sizeof($tmpviews)) {
                $query .= 'UNION
                    SELECT artefact
                    FROM {view_artefact}
Francois Marier's avatar
Francois Marier committed
251
                WHERE "view" IN (SELECT id FROM {view} WHERE "owner" = ?)
252
253
254
255
                    ORDER BY id';
                $args[] = $userid;
                $this->artefactexportmode = $tempartefacts = get_column_sql($query, $args);
            }
256
        }
257
        else {
258
            if ($tmpviews) {
259
                $sql = "SELECT DISTINCT va.artefact
260
261
262
                    FROM {view_artefact} va
                    LEFT JOIN {view} v ON v.id = va.view
                    WHERE v.owner = ?
263
                    AND va.view IN ( " . implode(',', array_keys($this->views)) . ")
264
                    ORDER BY va.artefact";
265
                $tmpartefacts = (array)get_column_sql($sql, array($userid));
266
267
268

                // 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()));
269
                $tmpartefacts = artefact_get_descendants($tmpartefacts);
270
                $tmpartefacts = array_unique(array_merge($tmpartefacts, $this->get_artefact_extra_artefacts($tmpartefacts)));
271
            }
272
273
274
            if ($artefacts == self::EXPORT_ARTEFACTS_FOR_VIEWS) {
                $this->artefactexportmode = $artefacts;
            }
275
276
277
            else if ($artefacts == self::EXPORT_COLLECTIONS) {
                $this->artefactexportmode = self::EXPORT_ARTEFACTS_FOR_VIEWS;
            }
278
            else {
279
280
                $tmpartefacts = array_unique(array_merge($tmpartefacts, $artefacts));
                $this->artefactexportmode = self::EXPORT_LIST_OF_ARTEFACTS;
281
            }
282
283
        }
        $typestoplugins = get_records_assoc('artefact_installed_type');
284
285
286
287
288
289
290
291
292
293
294
295
296
        $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);
297
298
        foreach ($tmpartefacts as $a) {
            $artefact = null;
299
            if ($a instanceof ArtefactType) {
300
301
                $artefact = $a;
            }
302
            else if (is_object($a) && isset($a->id)) {
303
                $artefact = $artefacts[$a->id];
304
305
            }
            else if (is_numeric($a)) {
306
                $artefact = $artefacts[$a];
307
308
309
310
            }
            if (is_null($artefact)) {
                throw new ParamOutOfRangeException("Invalid artefact $a");
            }
311
312
313
314
315
316
317
318
319
320
            // 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'));
            //}
321
322
323
            if ($artefact->exportable()) {
                $this->artefacts[$artefact->get('id')] = $artefact;
            }
324
        }
325

326
        $this->collections = array();
327
328
329
330
331
332
333
334
335
336
337
338
339
        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());
        }

340
341
342
343
344
345
346
        if ($collections) {
            require_once('collection.php');
            foreach ($collections as &$c) {
                $this->collections[$c->id] = new Collection(0, $c);
            }
        }

347
        // Now set up the temporary export directories
348
        $this->exportdir = get_config('dataroot')
349
            . 'export/'
350
            . $this->user->get('id')  . '/'
351
            . $this->exporttime .  '/';
352
353
354
        if (!check_dir_exists($this->exportdir)) {
            throw new SystemException("Couldn't create the temporary export directory $this->exportdir");
        }
355

356
357
        $this->messages = array();

358
        $this->notify_progress_callback(10, 'Setup');
359
360
361
    }

    /**
362
363
     * Accessor
     *
Aaron Wells's avatar
Aaron Wells committed
364
     * @param string $field The field to get (see the class definition to find
365
366
     *                      which fields are available)
     */
367
368
369
370
371
372
373
    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};
    }

374
375
376
    /**
     * Notifies the registered progress callback about the progress in generating the export.
     *
Aaron Wells's avatar
Aaron Wells committed
377
378
379
     * 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
380
381
     * export() is done. However, it is unnecessary to call it too often.
     *
Aaron Wells's avatar
Aaron Wells committed
382
383
     * 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
384
385
     * percentage is always increasing, for example.
     *
Aaron Wells's avatar
Aaron Wells committed
386
387
     * @param int $percent   The total percentage of the way through generating
     *                       the export. The base class constructor hands over
388
     *                       control claiming 10% of the work is done.
Aaron Wells's avatar
Aaron Wells committed
389
     * @param string $status A string describing the current status of the
390
391
     *                       export - e.g. 'Exporting Artefact (20/75)'
     */
392
393
394
395
396
397
398
399
    protected function notify_progress_callback($percent, $status) {
        if ($this->progresscallback) {
            call_user_func_array($this->progresscallback, array(
                $percent, $status
            ));
        }
    }

400
    /**
401
402
     * Returns embedded artefacts in view description and
     * additional artefacts required for view export from artefact plugins
403
     */
404
    protected function get_view_extra_artefacts() {
405
        $extra = View::get_embedded_artefacts(array_keys($this->views));
406
407
408
409
410
411
412
        $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));
413
414
415
                }
            }
        }
416
417
        return $extra;
    }
418

419
    protected function get_artefact_extra_artefacts(&$artefactids) {
420
421
422
        if (empty($artefactids)) {
            return array();
        }
423
424
425
426
427
428
429
430
431
432
        $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));
                }
            }
433
        }
434
        return $extra;
435
    }
436
437
}

438
/**
Aaron Wells's avatar
Aaron Wells committed
439
 * Looks in the export staging area in dataroot and deletes old, unneeded
440
441
442
443
444
 * exports.
 */
function export_cleanup_old_exports() {
    require_once('file.php');
    $basedir = get_config('dataroot') . 'export/';
445
446
447
448
449
450
451

    // If the export directory hasn't been created yet, there's no point
    // running the cron.
    if(!is_dir($basedir)) {
        return;
    }

452
    $exportdir = new DirectoryIterator($basedir);
453
    $mintime = time() - (24 * 60 * 60); // delete exports older than 24 hours
454

Aaron Wells's avatar
Aaron Wells committed
455
    // The export dir contains one directory for each user who has created
456
457
458
459
    // an export, named after their UID
    foreach ($exportdir as $userdir) {
        if ($userdir->isDot()) continue;

Aaron Wells's avatar
Aaron Wells committed
460
461
        // Each user's directory contains one directory for each export
        // they made, named as the unix timestamp of the time they
462
463
464
465
466
467
468
469
470
        // 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());
            }
        }
    }
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

    // 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:
638
639
                $errors[] = get_string('unabletoexportportfoliousingoptionsadmin', 'export');
                log_warn(get_string('unabletoexportportfoliousingoptionsadmin', 'export'));
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
        }

        $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/'
667
            . $row->usr  . '/';
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
            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();
    }
863
}