lib.php 28.5 KB
Newer Older
1
2
3
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage export-leap
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
 *
 */

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

/**
31
32
* LEAP export plugin.  See http://wiki.cetis.ac.uk/LEAP2A_specification and
* http://wiki.mahara.org/Developer_Area/Import%2f%2fExport/LEAP_Export
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
*/
class PluginExportLeap extends PluginExport {

    /**
    * xml string to build up.  Maybe later
    * this could change to an open file buffer
    * for performance.
    */
    protected $xml;

    /**
    * smarty object for main xml object
    * at the moment individual entries have their own
    * as well, because I can't figure out whether to unset
    * each entry template vars each time, or to create a new one
    * is more performant.
    */
    protected $smarty;

    /**
    * array of attachment objects
    * like this: (object)array('file' => '/path/to/file', 'name' => 'something.jpg');
    */
    protected $attachments = array();

    /**
    * filename to use for the feed
    */
    protected $leapfile    = 'leap2a.xml';

    /**
    * attachment directory for files
    */
    protected $filedir     = 'files/';

    /**
    * name of resultant zipfile
    */
    protected $zipfile;

    /**
    * special cases - artefact plugins that want to override stuff PER PLUGIN
    * rather than per type or per instance.
    */
    protected $specialcases = array();

    /**
    * extra person data injected by plugins other than internal
    */
    protected $extrapersondata = array();

    /**
    * constructor.  overrides the parent class
    * to set up smarty and the attachment directory
    */
88
89
    public function __construct(User $user, $views, $artefacts, $progresshandler=null) {
        parent::__construct($user, $views, $artefacts, $progresshandler);
90
91
92
93
94
95
        $this->smarty = smarty_core();

        if (!check_dir_exists($this->exportdir . '/' . $this->filedir)) {
            throw new SystemException("Couldn't create the temporary export directory $this->exportdir");
        }
        $this->zipfile = 'mahara-export-leap-user'
96
            . $this->get('user')->get('id') . '-' . $this->exporttime . '.zip';
97
        // some plugins might want to do their own special thing
98
        foreach (plugins_installed('artefact', true) as $plugin) {
99
100
            $plugin = $plugin->name;
            if (safe_require('export', 'leap/' . $plugin, 'lib.php', 'require_once', true)) {
101
102
103
104
105
106
                $classname = 'LeapExport' . ucfirst($plugin);
                if (class_exists($classname) && call_static_method($classname, 'override_entire_export')) {
                    $this->specialcases[$plugin] = array();
                }
            }
        }
107
108
109
110

        $outputfilter = LeapExportOutputFilter::singleton();
        $outputfilter->set_artefactids(array_keys($this->artefacts));

111
        $this->notify_progress_callback(15, 'Setup complete');
112
113
    }

114
115
116
117
118
119
120
121
    public static function get_title() {
        return get_string('title', 'export.leap');
    }

    public static function get_description() {
        return get_string('description', 'export.leap');
    }

122
123
124
125
126
127
    /**
    * main export routine
    */
    public function export() {
        // the xml stuff
        $this->export_header();
128
        $this->notify_progress_callback(20, 'Exporting Views');
129
        $this->export_views();
130
        $this->notify_progress_callback(30, 'Exporting artefacts');
131
132
        $this->export_artefacts();

133
        $this->notify_progress_callback(70, 'Exporting artefact plugin data');
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        $internal = null;
        foreach ($this->specialcases as $plugin => $artefacts) {
            if ($plugin == 'internal') {
                $internal = $artefacts;
                continue; // do it last so other plugins can inject persondata
            }
            $classname = 'LeapExport' . ucfirst($plugin);
            $pluginexport = new $classname($this, $artefacts);
            $this->xml .= $pluginexport->get_export_xml();
        }

        if (!empty($internal)) {
            $pluginexport = new LeapExportInternal($this, $internal);
            $this->xml .= $pluginexport->get_export_xml();
        }
149
        $this->notify_progress_callback(75, 'Exporting footer');
150
151

        $this->export_footer();
152
        $this->notify_progress_callback(80, 'Writing files');
153
154
155
156
157
158
159
160
161
162
163
164

        // write out xml to a file
        if (!file_put_contents($this->exportdir . $this->leapfile, $this->xml)) {
            throw new SystemException("Couldn't write LEAP data to the file");
        }

        // copy attachments over
        foreach ($this->attachments as $id => $fileinfo) {
            $existingfile = $fileinfo->file;
            $desiredname  = $fileinfo->name;
            copy($existingfile, $this->exportdir . $this->filedir . $id . '-' . $desiredname);
        }
165
        $this->notify_progress_callback(85, 'Creating zipfile');
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

        // zip everything up
        $cwd = getcwd();
        $command = sprintf('%s %s %s %s %s',
            get_config('pathtozip'),
            get_config('ziprecursearg'),
            escapeshellarg($this->exportdir .  $this->zipfile),
            escapeshellarg($this->leapfile),
            escapeshellarg($this->filedir)
        );
        $output = array();
        chdir($this->exportdir);
        exec($command, $output, $returnvar);
        chdir($cwd);
        if ($returnvar != 0) {
181
            throw new SystemException('Failed to zip the export file: return code ' . $returnvar);
182
        }
183
        $this->notify_progress_callback(100, 'Done');
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        return $this->zipfile;
    }

    public function cleanup() {
        // @todo remove temporary files and directories
        // @todo maybe move the zip file somewhere else - like to files/export or something
    }
    /**
    * create the feed header and author info
    */
    private function export_header() {
        $this->smarty->assign('userid', $this->get('user')->get('id'));
        $this->smarty->assign('name', full_name($this->get('user')));
        $this->smarty->assign('email', $this->get('user')->get('email'));
198
199
        $this->smarty->assign('export_time', $this->exporttime);
        $this->smarty->assign('export_time_rfc3339', PluginExportLeap::format_rfc3339_date($this->exporttime));
200
201
202
203
204
205
        require(get_config('docroot') . 'export/leap/version.php');
        $this->smarty->assign('leap_export_version', $config->version);
        $this->xml .= $this->smarty->fetch('export:leap:header.tpl');
    }

    /**
206
207
     * Export the views
     */
208
    private function export_views() {
209
210
        $layouts = get_records_assoc('view_layout');

211
        foreach ($this->get('views') as $view) {
212
            $config = $this->rewrite_artefact_ids($view->export_config('leap'));
213
214
215
216
            $this->smarty->assign('title',       $config['title']);
            $this->smarty->assign('id',          'portfolio:view' . $view->get('id'));
            $this->smarty->assign('updated',     self::format_rfc3339_date(strtotime($view->get('mtime'))));
            $this->smarty->assign('created',     self::format_rfc3339_date(strtotime($view->get('ctime'))));
217
            $this->smarty->assign('summarytype', 'html');
218
            $this->smarty->assign('summary',     $config['description']);
219
            $this->smarty->assign('contenttype', 'html');
220
221
            $this->smarty->assign('content',     $view->build_columns());
            $this->smarty->assign('viewdata',    $config['columns']);
222
223
224
225
226
227
228
229
230
231
232
233
234
            if (isset($layouts[$config['layout']])) {
                $layout = $config['layout'];
            }
            else {
                // This line ruthlessly stolen from lib/upgrade.php - the only
                // place where view layouts are canonically specified
                $layout = ($config['numcolumns'] == 2 ? 1 : ($config['numcolumns'] == 3 ? 4 : 7));
            }
            if (isset($layouts[$layout])) {
                $this->smarty->assign('layout',  $layouts[$layout]->widths);
            }
            $ownerformat = ($config['ownerformat']) ? $config['ownerformat'] : FORMAT_NAME_DISPLAYNAME;
            $this->smarty->assign('ownerformat', $ownerformat);
235
            $this->smarty->assign('leaptype',    'selection');
Nigel McNie's avatar
Nigel McNie committed
236
237
238
239
240
241
242
243
244
245

            $tags = array();
            if ($config['tags']) {
                $tags = array_map(create_function('$a',
                    'return array(
                        \'term\' => LeapExportElement::normalise_tag($a),
                        \'label\' => $a
                    );'), $config['tags']);
            }
            $this->smarty->assign('categories', array_merge(array(
246
247
248
249
                array(
                    'scheme' => 'selection_type',
                    'term' => 'Webpage',
                )
Nigel McNie's avatar
Nigel McNie committed
250
251
            ), $tags));

252
            $this->smarty->assign('links', $this->get_links_for_view($view->get('id')));
253
            $this->xml .= $this->smarty->fetch("export:leap:view.tpl");
254
255
256
        }
    }

257
258
259
260
261
262
263
264
265
266
267
268
269
    /**
     * Looks at all blockinstance configurations, and rewrites the artefact IDs
     * found to be IDs in the generated export.
     *
     * This only works for the 'artefactid' and 'artefactids' fields, which is
     * somewhat of a limitation, as it makes it hard for blocks that want to
     * store artefact ids in other configdata fields. We might have to address
     * this limitation later.
     */
    private function rewrite_artefact_ids($config) {
        foreach ($config['columns'] as &$column) {
            foreach ($column as &$blockinstance) {
                if (isset($blockinstance['config']['artefactid'])) {
270
271
272
273
274
275
276
                    // json_encoded value of null is the string 'null', don't include these
                    if ($blockinstance['config']['artefactid'] != 'null') {
                        $blockinstance['config']['artefactid'] = 'portfolio:artefact' . $blockinstance['config']['artefactid'];
                    }
                    else {
                        $blockinstance['config']['artefactid'] = null;
                    }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
                }
                else if (isset($blockinstance['config']['artefactids'])) {
                    $ids = json_decode($blockinstance['config']['artefactids']);
                    $blockinstance['config']['artefactids'] = json_encode(array_map(array($this, 'prepend_artefact_identifier'), $ids));
                }
            }
        }
        return $config;
    }

    private function prepend_artefact_identifier($artefactid) {
        return 'portfolio:artefact' . $artefactid;
    }

291
292
    private function get_links_for_view($viewid) {
        static $viewartefactdata = null;
293
        static $vaextra = null;
294
295
296
        if (is_null($viewartefactdata)) {
            $viewartefactdata = get_records_select_array('view_artefact', 'view IN (' . join(', ', array_keys($this->views)) . ')');
        }
297
298
299
        if (is_null($vaextra)) {
            $vaextra = $this->get_view_extra_artefacts(true);
        }
300
301
302
303
304
305
306
307
308
309

        $links = array();
        foreach ($viewartefactdata as $va) {
            if ($va->view == $viewid) {
                $links[] = (object)array(
                    'type' => 'has_part',
                    'id'   => 'portfolio:artefact' . $va->artefact,
                );
            }
        }
310
311
312
313
314
315
316
317
318

        if (isset($vaextra[$viewid])) {
            foreach ($vaextra[$viewid] as $artefactid) {
                $links[] = (object)array(
                    'type' => 'is_evidence_of', // Fix this
                    'id'   => 'portfolio:artefact' . $artefactid,
                );
            }
        }
319
320
321
        return $links;
    }

322
    /**
323
324
     * Export the artefacts
     */
325
    private function export_artefacts() {
326
327
328
329
330
331
332
333
334
335
        $progressstart = 30;
        $progressend   = 70;
        $artefacts     = $this->get('artefacts');
        $artefactcount = count($artefacts);
        $i = 0;
        foreach ($artefacts as $artefact) {
            if ($i++ % 10 == 1) {
                $percent = intval($progressstart + ($i / $artefactcount) * ($progressend - $progressstart));
                $this->notify_progress_callback($percent, "Exporting artefacts: $i/$artefactcount");
            };
336
337
            $element = null;
            // go see if we have to do anything special for this artefact type.
338
339
340
341
342
            if (safe_require('export', 'leap/' . $artefact->get_plugin_name(), 'lib.php', 'require_once', true)) {
                $classname = 'LeapExportElement' . ucfirst($artefact->get('artefacttype'));
                if (class_exists($classname)) {
                    $element = new $classname($artefact, $this);
                }
343

344
345
346
347
348
349
350
351
352
353
354
355
356
                if (is_null($element)) {
                    $element = new LeapExportElement($artefact, $this);
                }
                if (array_key_exists($artefact->get_plugin_name(), $this->specialcases) && !$element->override_plugin_specialcase()) {
                    $this->specialcases[$artefact->get_plugin_name()][] = $artefact;
                    continue;
                }
                if (!$element->is_leap()) {
                    continue;
                }
                $element->add_attachments();
                $element->assign_smarty_vars();
                $this->xml .= $element->get_export_xml();
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
            }
        }
    }

    /**
    * somewhat hacky way for other plugins to inject data into persondata
    * which is what artefact/internal creates
    *
    * @param array $data array of of keyed arrays. required keys are:
    *                     artefacttype, artefactplugin field, label,  value.
    *                     optional keys are service, displayorder and mahara (non leap persondata)
    */
    public function inject_persondata($data) {
        $this->extrapersondata = array_merge($this->extrapersondata, $data);
    }

    /**
    * export the feed footer
    */
    private function export_footer() {
        $this->xml .= $this->smarty->fetch('export:leap:footer.tpl');
    }


    /**
    * entry point for adding attachments into this export
    * USE THIS FUNCTION, and keep the return variable for the filename
    *
    * @param string $filepath path to file to add
    * @param string $newname proper resulting filename
    *
    * @return filename string use this to substitute into <content src="">
    */
    public function add_attachment($filepath, $newname) {
        if (!file_exists($filepath)) {
            throw new ParamOutOfRangeException("Tried to add non existant file $filepath");
        }
        if (empty($newname)) {
            throw new ParamOutOfRangeException("Tried to add non existant file $filepath");
        }
397
        $newname = substr(str_replace('/', '_', $newname), 0, 245);
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
        $this->attachments[] = (object)array('file' => $filepath, 'name' => $newname);
        return (count($this->attachments) -1) . '-' . $newname;
    }

    /**
     * format a date to the w3 datetime format
     *
     * @param integer unix timestamp to format
     * @return string W3 Date format
     */
    public static function format_rfc3339_date($date) {
        $d = format_date($date, 'strftimew3cdatetime');
        return substr($d, 0, -2) . ':' . substr($d, -2);
    }
}

/**
* LEAP Element class
* one per entry
*/
class LeapExportElement {

    /**
    * the artefact this element represents
    */
    protected $artefact;

    /**
    * the exporter object
    */
    protected $exporter;

    /**
    * smarty object to assign variables to
    */
    protected $smarty;

    /**
    * the links this element has to other elements
    */
    protected $links;

    /**
    * constructor.
    *
    * @param ArtefactType $artefact artefact this element represents
    * @param LeapExporter $exporter the exporter object
    */
    public function __construct(ArtefactType $artefact, LeapExporter $exporter) {
        $this->artefact = $artefact;
        $this->exporter = $exporter;
        $this->smarty   = smarty_core();
    }

    /**
    * Return the xml for this element
    *
    * @return string XML
    */
    public function get_export_xml() {
        return $this->smarty->fetch($this->get_template_path());
    }

    /**
    * assign the smarty vars used in this template
    */
    public function assign_smarty_vars() {
        $this->smarty->assign('artefacttype', $this->artefact->get('artefacttype'));
        $this->smarty->assign('artefactplugin', $this->artefact->get_plugin_name());
        $this->smarty->assign('title', $this->artefact->get('title'));
        $this->smarty->assign('id', 'portfolio:artefact' . $this->artefact->get('id'));
        $this->smarty->assign('updated', PluginExportLeap::format_rfc3339_date($this->artefact->get('mtime')));
        $this->smarty->assign('created', PluginExportLeap::format_rfc3339_date($this->artefact->get('ctime')));
        // these are the ones we really need to override
        $this->add_links();
        $this->smarty->assign('content', $this->get_content());
474
        $this->smarty->assign('contenttype', $this->get_content_type());
475
        $this->smarty->assign('leaptype', $this->get_leap_type());
476
        $this->smarty->assign('author', $this->get_entry_author());
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

        if ($tags = $this->artefact->get('tags')) {
            $tags = array_map(create_function('$a',
                'return array(
                    \'term\' => LeapExportElement::normalise_tag($a),
                    \'label\' => $a
                );'), $tags);
        }
        if (!$categories = $this->get_categories()) {
            $categories = array();
        }
        $this->smarty->assign('categories', array_merge($tags, $categories));
        $this->smarty->assign('links', $this->links);
    }

    /**
    * add a link to a view
    * uses get_view_relationship to figure out which LEAP relationship to use
    * use this function, as it makes sure the view we're linking to is included
    * in the feed.
    *
    * @param View $view to link to
    */
500
501
502
    public function add_view_link($viewid) {
        if (array_key_exists($viewid, $this->exporter->get('views'))) {
            $this->add_generic_link('view' . $viewid, $this->get_view_relationship($viewid));
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
        }
    }

    /**
    * add a link to another artefact
    * use this function, as it makes sure the artefact we're linking to is included
    * in the feed.
    *
    * @param ArtefactType $artefact artefact to link to
    * @param string rel the LEAP relationship to use
    */
    public function add_artefact_link(ArtefactType $artefact, $rel) {
        if (array_key_exists($artefact->get('id'), $this->exporter->get('artefacts'))) {
            $this->add_generic_link('artefact' . $artefact->get('id'), $rel);
        }
    }

    /**
    * Adds a link to this element that isn't necessarily to a view or artefact
    *
    * @param string $id id to link to, not including portfolio: ns
    *                                  eg resumecomposite6
    * @param string $rel the LEAP relationship to use
    * @param keyed array $extras any extra bits to go in (eg display_order => 1)
    */
    public function add_generic_link($id, $rel, $extras=null) {
529
        if (!in_array($rel, array('related', 'alternate', 'enclosure'))) {
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
            $rel = 'leap:' . $rel;
        }
        $link = array(
            'id'   => 'portfolio:' . $id,
            'type' => $rel,
        );
        if (is_array($extras)) {
            $link = array_merge($extras, $link);
        }
        $this->links['portfolio:' . $id] = (object)$link;
    }

    /**
    * Add links to other artefacts and views
    * By default just the parents, children, and views.
    * You can override this to add extra links, eg files/blogposts
    *
    * The resulting array is keyed on the LEAP portfolio:id (eg portfolio:artefact2)
    */
    public function add_links() {
        if ($views = $this->artefact->get_views_metadata()) {
            foreach ($views as $view) {
552
                $this->add_view_link($view->view);
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
            }
        }
        if ($parent = $this->artefact->get_parent_instance()) {
            $this->add_artefact_link($parent, $this->get_parent_relationship($parent));
        }
        if ($children = $this->artefact->get_children_instances()) {
            foreach ($children as $child) {
                $this->add_artefact_link($child, $this->get_child_relationship($child));
            }
        }
    }

    /**
    * Path to main entry template.
    * this can be overridden per artefact plugin... eg
    * export:leap/file:imageentry.tpl
    * export:leap/plugin:artefacttypeentry.tpl
    *
    * @return string
    */
    public function get_template_path() {
        return 'export:leap:entry.tpl';
    }

    /**
    * The LEAP element type
    * See http://wiki.cetis.ac.uk/2009-03/LEAP2A_types
    *
    * @return string
    */
    public function get_leap_type() {
        return 'entry'; // default base type that everything inherits from
    }

    /**
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
     * The content type of whatever is going in the <content> element.
     *
     * Can only be one of 'text', 'html' or 'xhtml', and we're currently not 
     * using XHTML in Mahara.
     *
     * @return string
     */
    public function get_content_type() {
        return 'text';
    }


    /**
    * The main content of the element, which goes between <content> tags.
    *
    * Escaping of this value happens in the template, depending on the content 
    * type, which can be set with {@link get_content_type()}.
    *
    * The default is to use the artefact description.
607
    *
608
    * @return string
609
610
    */
    public function get_content() {
611
612
613
614
615
616
617
618
        switch ($this->get_content_type()) {
        case 'text':
        case 'html':
        case 'xhtml':
            return $this->artefact->get('description');
        default:
            throw new SystemException("Unrecognised content type");
        }
619
620
    }

621
622
623
624
625
626
627
628
629
630
    /**
    * The id of the entry's author
    * Override this if the author is different from the portfolio holder
    *
    * @return int
    */
    public function get_entry_author() {
        return;
    }

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
    /**
    * The relationship this artefact has to a view.
    * Almost always is_part_of, but could also be supports or anything else.
    *
    * @return string
    */
    public function get_view_relationship(View $view) {
        return 'is_part_of';
    }

    /**
    * The relationship this artefact has to the given child.
    * By default, has_part
    *
    * @return string
    */
    public function get_child_relationship(ArtefactType $child) {
        return 'has_part';
    }

    /**
    * The relationship this artefact has to the given parent.
    * By default, is_part_of
    *
    * @return string
    */
    public function get_parent_relationship(ArtefactType $parent) {
        return 'is_part_of';
    }

    /**
    * Add the attachments this element has to the export
    * Use the add_attachment method on the exporter object
    * For each attachments
    */
    public function add_attachments() { }

    /**
    * If the entire plugin overrides export
    * but there are individual artefact types that should be exported
    * override this to return true.
    *
    * For example, internal gets overridden to do persondata
    * but industry, introduction & occupation are entries in their own right
    */
    public function override_plugin_specialcase() {
        return false;
    }

    /**
    * is this element even a LEAP element.
    * return false to have this artefact skipped
    * (this is essentially opt-out for artefact types)
    */
    public function is_leap() {
        return true;
    }

    public function get_categories() {
        return array();
    }

    /**
    * Getter
    *
    * @param String key
    */
    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};
    }

    /**
     * Converts a tag to a 'normalised' tag, as per 
     * http://wiki.cetis.ac.uk/2009-03/LEAP2A_categories#Plain_tags
     *
     * The method of normalisation isn't specified at
     * the time of this being written.
     */
    public static function normalise_tag($tag) {
        $tag = preg_replace('#[^a-zA-Z0-9-]#', '-', $tag);
        $tag = preg_replace('#\-{2,}#', '-', $tag);
        return $tag;
    }
}

/**
* Class to extend for entire plugin exports
*
* when each artefact isn't just an ordinary entry
* eg internal & resume
*/
abstract class LeapExportArtefactPlugin {

    protected $exporter;
    protected $artefacts;

    public function __construct(LeapExporter $exporter, array $artefacts) {
        $this->exporter = $exporter;
        $this->artefacts = $artefacts;
    }

    /**
    * contract method used to detect whether the entire plugin should override the export
    * here for consistency but there's really no point overriding this to return false
    * the existance of a subclass kind of implies overriding.
    */
    public static function override_entire_export() {
        return true;
    }

    /**
    * export xml for the subclass.
    *
    * @param LeapExporter $exporter the exporter object. Can be used to fetch smarty object.
    * @param array $artefacts the array of selected artefacts that belong to this plugin
    *
    * @return XML string
    */
    abstract public function get_export_xml();

}

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
function export_leap_rewrite_links($html) {
    $outputfilter = LeapExportOutputFilter::singleton();
    return $outputfilter->filter($html);
}
/**
 * Provides a mechanism for converting the HTML generated by views and 
 * artefacts for the LEAP export.
 *
 * This is primarily so that the content of view blocktypes and links to 
 * artefacts get rewritten to point to entries within the export.
 */
class LeapExportOutputFilter {

    private static $instance = null;

    private $artefactids = array();

    private function __construct() {
    }

    public static function singleton() {
        if (is_null(self::$instance)) {
            self::$instance = new LeapExportOutputFilter();
        }
        return self::$instance;
    }

    public function set_artefactids(array $artefactids) {
        $this->artefactids = $artefactids;
    }

    public function filter($html) {
        // Links to artefacts
        $html = preg_replace_callback(
            '#<a[^>]+href="(' . preg_quote(get_config('wwwroot')) . ')?/?view/artefact\.php\?artefact=(\d+)(&amp;view=\d+)?(&amp;page=\d+)?"([^>]*)>#',  //  ([^<]*)</a>
            array($this, 'replace_artefact_link'),
            $html
        );

        // Links to download files
        $html = preg_replace_callback(
            array(
798
799
                '#<(a[^>]+)href="(' . preg_quote(get_config('wwwroot')) . ')?/?artefact/file/download\.php\?file=(\d+)(&amp;view=\d+)?"([^>]*)>#',
                '#<(img[^>]+)src="(' . preg_quote(get_config('wwwroot')) . ')?/?artefact/file/download\.php\?file=(\d+)(&amp;view=\d+)?"([^>]*)>#',
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
            ),
            array($this, 'replace_download_link'),
            $html
        );

        return $html;
    }

    /**
     * Callback to replace links to artefact to point to the correct entry 
     * in the LEAP export
     */
    private function replace_artefact_link($matches) {
        $artefactid = $matches[2];
        if (in_array($artefactid, $this->artefactids)) {
            return '<a rel="has_part" href="portfolio:artefact' . hsc($artefactid) . '"' . $matches[5] . '>';
        }

        // If the artefact isn't in the export, then we can't provide an 
        // export-relative link to it
        log_debug("Not providing an export-relative link for $artefactid");
        return $matches[0];
    }

    /**
     * Callback to replace links to artefact/file/download.php to point to the 
     * correct entry in the LEAP export
     */
    private function replace_download_link($matches) {
        $artefactid = $matches[3];
        if (in_array($artefactid, $this->artefactids)) {
            return '<' . $matches[1] . 'rel="has_part" href="portfolio:artefact' . hsc($artefactid) . '"' . $matches[5] . '>';
        }

        log_debug("Not providing an export-relative link for $artefactid");
        return $matches[0];
    }

}

840
?>