lib.php 46.3 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * 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
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
21
 * @subpackage artefact
22
 * @author     Catalyst IT Ltd
23
 * @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
31
32
33
 *
 */

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

/**
 * Base artefact plugin class
 * @abstract
 */
34
abstract class PluginArtefact extends Plugin {
35
36
37
38

    /** 
     * This function returns a list of classnames 
     * of artefact types this plugin provides.
Penny Leach's avatar
Penny Leach committed
39
     * @abstract
40
41
42
43
     * @return array
     */
    public static abstract function get_artefact_types();

44
45
46
47
48
49
50
51
52
53
54
    
    /**
    * This function returns a list of classnames
    * of block types this plugin provides
    * they must match directories inside artefact/$name/blocktype
    * @abstract
    * @return array
    */
    public static abstract function get_block_types();


55
56
    /**
     * This function returns the name of the plugin.
Penny Leach's avatar
Penny Leach committed
57
     * @abstract
58
59
60
61
62
     * @return string
     */
    public static abstract function get_plugin_name();


63
64
65
66
67
68
69
70
71
72
73
    /**
     * This function returns an array of menu items
     * to be displayed
     * Each item should be a StdClass object containing -
     * - name language pack key
     * - url relative to wwwroot
     * @return array
     */
    public static function menu_items() {
        return array();
    }
74
75
76
77
78
79
80
81
82
83
84
85
86


    /**
     * This function returns an array of menu items to be displayed
     * on a group page when viewed by group members.
     * Each item should be a StdClass object containing -
     * - title language pack key
     * - url relative to wwwroot
     * @return array
     */
    public static function group_tabs($groupid) {
        return array();
    }
87
88
89
90
91
92
93
94
95
96
97


    /**
     * Returns any artefacts that are not inside a view
     * but which need to be exported along with it.
     * @param array $viewids
     * @return array of artefact ids
     */
    public static function view_export_extra_artefacts($viewids) {
        return array();
    }
98
99
100
101
102
103
104
105
106
107
108
109


    /**
     * When filtering searches, some artefact types are classified the same way
     * even when they come from different artefact plugins.  This function allows
     * artefact plugins to declare which search filter content type each of their 
     * artefact types belong to.
     * @return array of artefacttype => array of filter content types
     */
    public static function get_artefact_type_content_types() {
        return array();
    }
110
111
112
113
114
115
116
117
}

/** 
 * Base artefact type class
 * @abstract
 */
abstract class ArtefactType {
    
118
119
    protected $dirty;
    protected $parentdirty;
120
    protected $deleted = false;
121
    protected $id;
122
123
    protected $artefacttype;
    protected $owner;
124
    protected $container;
125
    protected $parent;
126
127
    protected $ctime;
    protected $mtime;
128
    protected $atime;
129
130
131
132
    protected $locked;
    protected $title;
    protected $description;
    protected $note;
133
    protected $tags = array();
134
    protected $institution;
135
    protected $group;
Richard Mansfield's avatar
Richard Mansfield committed
136
    protected $rolepermissions;
137
    protected $mtimemanuallyset;
138

Penny Leach's avatar
Penny Leach committed
139
140
141
142
143
144
145
    protected $viewsinstances;
    protected $viewsmetadata;
    protected $childreninstances;
    protected $childrenmetadata;
    protected $parentinstance;
    protected $parentmetadata;

146
147
148
149
150
151
152
153
    /** 
     * Constructer. 
     * If an id is supplied, will query the database
     * to build up the basic information about the object.
     * If an id is not supplied, we just create an empty
     * artefact, ready to be filled up
     * @param int $id artefact.id
     */
Penny Leach's avatar
Penny Leach committed
154
    public function __construct($id=0, $data=null) {
155
        if (!empty($id)) {
Penny Leach's avatar
Penny Leach committed
156
157
            if (empty($data)) {
                if (!$data = get_record('artefact','id',$id)) {
158
                    throw new ArtefactNotFoundException(get_string('artefactnotfound', 'error', $id));
Penny Leach's avatar
Penny Leach committed
159
                }
160
            }
161
            $this->id = $id;
162
163
        }
        else {
164
165
            $this->ctime = $this->mtime = time();
            $this->dirty = true;
166
        }
167
168
169
170
171
        if (empty($data)) {
            $data = array();
        }
        foreach ((array)$data as $field => $value) {
            if (property_exists($this, $field)) {
172
173
174
                if (in_array($field, array('atime', 'ctime', 'mtime'))) {
                    $value = strtotime($value);
                } 
Richard Mansfield's avatar
Richard Mansfield committed
175
                if ($field == 'tags' && !is_array($value)) {
176
177
                    $value = preg_split("/\s*,\s*/", trim($value));
                }
178
179
180
181
                $this->{$field} = $value;
            }
        }

182
183
184
185
186
187
188
        $this->artefacttype = $this->get_artefact_type();
        if (!empty($data->artefacttype)) {
            if ($this->artefacttype != $data->artefacttype) {
                throw new SystemException(get_string('artefacttypemismatch', 'error', $data->artefacttype, $this->artefacttype));
            }
        }

189
190
191
192
193
194
195
196
        // load tags
        if ($this->id) {
            $tags = get_column('artefact_tag', 'tag', 'artefact', $this->id);
            if (is_array($tags)) {
                $this->tags = $tags;
            }
        }

Richard Mansfield's avatar
Richard Mansfield committed
197
        // load group permissions
198
        if ($this->group && !is_array($this->rolepermissions)) {
Richard Mansfield's avatar
Richard Mansfield committed
199
200
201
            $this->load_rolepermissions();
        }

202
        $this->atime = time();
203
204
205
    }

    public function get_views_instances() {
206
207
208
209
210
211
212
213
214
215
216
        // @todo test this
        if (!isset($this->viewsinstances)) {
            $this->viewsinstances = false;
            if ($views = $this->get_views_metadata()) {
                $this->viewsinstances = array();
                foreach ($views as $view) {
                    $this->viewsinstances[] = new View($view->id, $view);
                }
            }
        }
        return $this->viewsinstances;
217
    }
218

219
    public function get_views_metadata() {
220
221
222
223
        if (!isset($this->viewsmetadata)) {
            $this->viewsmetadata = get_records_array('view_artefact', 'artefact', $this->id);
        }
        return $this->viewsmetadata;
224
225
    }

Richard Mansfield's avatar
Richard Mansfield committed
226
227
228
229
    public function count_children() {
        return count_records('artefact', 'parent', $this->get('id'));
    }

230
231
232
233
234
235
236
    public function has_children() {
        if ($this->get_children_metadata()) {
            return true;
        }
        return false;
    }

237
238
239
240
    public function get_plugin_name() {
        return get_field('artefact_installed_type', 'plugin', 'name', $this->get('artefacttype'));
    }

Penny Leach's avatar
Penny Leach committed
241
242
243
244
245
246
247
248
249
    /** 
     * This function returns the instances 
     * of all children of this artefact
     * If you just want the basic info, 
     * use {@link get_children_metadata} instead.
     * 
     * @return array of instances.
     */

250
    public function get_children_instances() {
Penny Leach's avatar
Penny Leach committed
251
252
253
254
255
        if (!isset($this->childreninstances)) {
            $this->childreninstances = false;
            if ($children = $this->get_children_metadata()) {
                $this->childreninstances = array();
                foreach ($children as $child) {
256
                    $classname = generate_artefact_class_name($child->artefacttype);
Penny Leach's avatar
Penny Leach committed
257
258
259
260
261
262
                    $instance = new $classname($child->id, $child);
                    $this->childreninstances[] = $instance;
                }
            }
        }
        return $this->childreninstances;
263
264
    }

265
266
267
268
269
270
271
272
273
    /**
     * This function returns the db rows 
     * from the artefact table that have this 
     * artefact as the parent.
     * If you want instances, use {@link get_children_instances}
     * but bear in mind this will have a performance impact.
     * 
     * @return array
     */
274
    public function get_children_metadata() {
Penny Leach's avatar
Penny Leach committed
275
        if (!isset($this->childrenmetadata)) {
276
            $this->childrenmetadata = get_records_array('artefact', 'parent', $this->id);
Penny Leach's avatar
Penny Leach committed
277
278
        }
        return $this->childrenmetadata;
279
    }
Penny Leach's avatar
Penny Leach committed
280
281
282
283
284
285
286
287
288

    /**
     * This function returns the instance relating to the parent
     * of this object, or false if there isn't one.
     * If you just want basic information about it,
     * use {@link get_parent_metadata} instead.
     *
     * @return ArtefactType
     */
289
    public function get_parent_instance() {
Penny Leach's avatar
Penny Leach committed
290
291
292
        if (!isset($this->parentinstance)) {
            $this->parentinstance = false;
            if ($parent = $this->get_parent_metadata()) {
293
                $classname = generate_artefact_class_name($parent->artefacttype);
Penny Leach's avatar
Penny Leach committed
294
295
296
297
                $this->parentinstance = new $classname($parent->id, $parent);
            }
        }
        return $this->parentinstance;
298
    }
299
300
301
302
303
304
305
306

    /** 
     * This function returns the db row 
     * (if there is one) of the parent
     * artefact for this instance.
     * If you want the instance, use 
     * {@link get_parent_instance} instead.
     * 
307
     * @return object - db row
308
     */
309
    public function get_parent_metadata() {
310
311
312
        if (empty($this->parent)) {
            return false;
        }
313
        return get_record('artefact','id',$this->parent);
314
315
    }

316
317
318
319
320
321
322
323
324
325
326
327
    /**
     * Returns how much quota this artefact has used.
     *
     * It should try to not instantiate the artefact, because it is normally 
     * called as part of an expensive cron job
     *
     * @return int Size in bytes that the artefact is taking up in quota
     */
    public static function get_quota_usage($artefact) {
        return 0;
    }

328
    public function get($field) {
329
        if (!property_exists($this, $field)) {
330
            throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
331
332
333
334
335
        }
        return $this->{$field};
    }

    public function set($field, $value) {
336
        if (property_exists($this, $field)) {
337
338
339
340
            if ($this->{$field} != $value) {
                // only set it to dirty if it's changed
                $this->dirty = true;
            }
341
            $this->{$field} = $value;
342
343
344
            if ($field == 'parent') {
                $this->parentdirty = true;
            }
345
346
347
348
349
350
            if ($field == 'mtime') {
                $this->mtimemanuallyset = true;
            }
            else if (!$this->mtimemanuallyset) {
                $this->mtime = time();
            }
351
352
            return true;
        }
353
        throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
354
355
    }
    
356
357
358
    /**
     * Artefact destructor. Calls commit and marks the
     * artefact cache as dirty if necessary.
359
360
361
     *
     * A special case is when the object has just been deleted.  In this case,
     * we do nothing.
362
     */
363
    public function __destruct() {
364
365
366
367
        if ($this->deleted) {
            return;
        }
      
368
        if (!empty($this->dirty) && !defined('MAHARA_CRASHING')) {
369
370
            $this->commit();
        }
371
372
373
374
375
376
    }
    
    public function is_container() {
        return false;
    }

377
    /** 
378
379
380
     * This method updates the contents of the artefact table only.  If your
     * artefact has extra information in other tables, you need to override
     * this method, and call parent::commit() in your own function.
381
     */
382
    public function commit() {
383
384
385
        if (empty($this->dirty)) {
            return;
        }
386
387
388

        db_begin();

389
390
391
392
393
394
395
396
397
        $fordb = new StdClass;
        foreach (get_object_vars($this) as $k => $v) {
            $fordb->{$k} = $v;
            if (in_array($k, array('mtime', 'ctime', 'atime')) && !empty($v)) {
                $fordb->{$k} = db_format_timestamp($v);
            }
        }
        if (empty($this->id)) {
            $this->id = insert_record('artefact', $fordb, 'id', true);
Richard Mansfield's avatar
Richard Mansfield committed
398
399
400
            if ($this->can_be_logged()) {
                $this->log('created');
            }
401
402
403
            if (!empty($this->parent)) {
                $this->parentdirty = true;
            }
404
405
        }
        else {
Richard Mansfield's avatar
Richard Mansfield committed
406
407
408
            if ($this->can_be_logged()) {
                $this->log('edited');
            }
409
410
            update_record('artefact', $fordb, 'id');
        }
411

Richard Mansfield's avatar
Richard Mansfield committed
412
413
414
415
        if (!empty($this->group)) {
            $this->save_rolepermissions();
        }

416
417
        delete_records('artefact_tag', 'artefact', $this->id);
        if (is_array($this->tags)) {
Martyn Smith's avatar
Martyn Smith committed
418
            foreach (array_unique($this->tags) as $tag) {
419
420
421
                if (empty($tag)) {
                    continue;
                }
422
423
424
425
426
427
428
429
430
431
                insert_record(
                    'artefact_tag',
                    (object) array(
                        'artefact' => $this->id,
                        'tag'      => $tag,
                    )
                );
            }
        }

432
        artefact_watchlist_notification(array($this->id));
433

434
435
        handle_event('saveartefact', $this);

436
        if (!empty($this->parentdirty)) {
437
            if ($this->parent) {
438
                // Make sure we have a record for the new parent
439
440
441
442
443
444
                delete_records('artefact_parent_cache', 'artefact', $this->id, 'parent', $this->parent);
                insert_record('artefact_parent_cache', (object)array(
                    'artefact' => $this->id,
                    'parent'   => $this->parent,
                    'dirty'    => 0
                ));
445
446
447
                // Set anything relating to this artefact as dirty
                set_field_select('artefact_parent_cache', 'dirty', 1,
                                 'artefact = ? OR parent = ?', array($this->id, $this->id));
448
449
450
451
            }
            else {
                // No parent - no need for any records in the apc then
                delete_records('artefact_parent_cache', 'artefact', $this->id);
452
453
            }
        }
454
        $this->dirty = false;
455
        $this->deleted = false;
456
        $this->parentdirty = false;
457
458

        db_commit();
459
460
461
    }

    /** 
462
463
464
465
466
467
468
     * This function provides basic delete functionality.  It gets rid of the
     * artefact's row in the artefact table, and the tables that reference the
     * artefact table.  It also recursively deletes child artefacts.
     *
     * If your artefact has additional data in another table, you should
     * override this function, but you MUST call parent::delete() after you
     * have done your own thing.
469
     */
470
471
    public function delete() {
        if (empty($this->id)) {
472
            $this->dirty = false;
473
474
475
            return;
        }
      
476
        db_begin();
477

478
479
480
481
482
483
        // Call delete() on children (if there are any)
        if ($children = $this->get_children_instances()) {
            foreach ($children as $child) {
                $child->delete();
            }
        }
484

485
        artefact_watchlist_notification(array($this->id));
486

487
        self::_delete_dbrecords(array($this->id));
Richard Mansfield's avatar
Richard Mansfield committed
488
489
490
491

        if ($this->can_be_logged()) {
            $this->log('deleted');
        }
492
      
493
494
        handle_event('deleteartefact', $this);

495
        // Set flags.
496
497
        $this->dirty = false;
        $this->parentdirty = true;
498
        $this->deleted = true;
499

500
        db_commit();
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
    /**
     * Does a bulk_delete on a list of artefacts, grouping artefacts of
     * the same type.
     *
     * Currently only tested for folders and their contents.
     */
    public static function delete_by_artefacttype($artefactids) {
        if (empty($artefactids)) {
            return;
        }

        db_begin();

        artefact_watchlist_notification($artefactids);

        $records = get_records_select_assoc(
            'artefact',
            'id IN (' . join(',', $artefactids) . ')',
            null, 'artefacttype', 'id,parent,artefacttype,container'
        );

        $containers = array();
        $leaves = array();
        foreach ($records as $r) {
            if ($r->container) {
                $containers[$r->artefacttype][] = $r->id;
            }
            else {
                $leaves[$r->artefacttype][] = $r->id;
            }
        }

        // Delete non-containers grouped by artefacttype
        foreach ($leaves as $artefacttype => $ids) {
            $classname = generate_artefact_class_name($artefacttype);
            call_static_method($classname, 'bulk_delete', $ids);
        }

        // Delete containers grouped by artefacttype
        foreach ($containers as $artefacttype => $ids) {
            $classname = generate_artefact_class_name($artefacttype);
            call_static_method($classname, 'bulk_delete', $ids);
        }

        handle_event('deleteartefacts', $artefactids);

        db_commit();
    }

    /**
     * Faster delete for multiple artefacts.
     *
     * Should only be called on artefacts with no children, after
     * additional data in other tables has already been deleted.
     */
    public static function bulk_delete($artefactids, $log=false) {
        db_begin();

        self::_delete_dbrecords($artefactids);

        // Logging must be triggered by the caller because it's
        // slow to go through each artefact and ask it if it should
        // be logged.
        if ($log) {
            global $USER;
            $entry = (object) array(
                'usr'      => $USER->get('id'),
                'time'     => db_format_timestamp(time()),
                'deleted'  => 1,
            );
            foreach ($artefactids as $id) {
                $entry->artefact = $id;
                insert_record('artefact_log', $entry);
            }
        }

        db_commit();
    }


    private static function _delete_dbrecords($artefactids) {
        if (empty($artefactids)) {
            return;
        }

        $idstr = '(' . join(',', $artefactids) . ')';

        db_begin();

        // Delete any references to these artefacts from non-artefact places.
        delete_records_select('artefact_parent_cache', "artefact IN $idstr");

        // Make sure that the artefacts are removed from any view blockinstances
        if ($records = get_records_sql_array("
            SELECT va.block, va.artefact, bi.configdata
            FROM {view_artefact} va JOIN {block_instance} bi ON va.block = bi.id
            WHERE va.artefact IN $idstr", array())) {
            require_once(get_config('docroot') . 'blocktype/lib.php');
            BlockInstance::bulk_delete_artefacts($records);
        }
        delete_records_select('view_artefact', "artefact IN $idstr");
        delete_records_select('artefact_feedback', "artefact IN $idstr");
        delete_records_select('artefact_tag', "artefact IN $idstr");
        delete_records_select('artefact_access_role', "artefact IN $idstr");
        delete_records_select('artefact_access_usr', "artefact IN $idstr");

        delete_records_select('artefact', "id IN $idstr");

        db_commit();
    }

Penny Leach's avatar
Penny Leach committed
614
    /**
615
    * this function provides the way to link to viewing very deeply nested artefacts
616
617
618
    * within a view
    *
    * @todo not sure the comment here is appropriate
619
    */
620
621
622
623
624
625
626
627
628
629
    public function add_to_render_path(&$options) {
        if (empty($options['path'])) {
            $options['path'] = $this->get('id');
        }
        else {
            $options['path'] .= ',' . $this->get('id');
        }
    }


630
631
632
633
634
635
636
637
638
639
    /**
     * By default public feedback can be placed on all artefacts.
     * Artefact types which don't want to allow public feedback should
     * redefine this function.
     */
    public function public_feedback_allowed() {
        return true;
    }


640
641
642
643
644
645
646
647
648
649
    /**
     * By default users are notified of all feedback on artefacts
     * which they own.  Artefact types which want to allow this
     * notification to be turned off should redefine this function.
     */
    public function feedback_notify_owner() {
        return true;
    }


Penny Leach's avatar
Penny Leach committed
650
    /**
651
652
653
654
655
656
657
658
659
     * Returns a URL for an icon for the appropriate artefact
     *
     * @param array $options Options for the artefact. The array MUST have the 
     *                       'id' key, representing the ID of the artefact for 
     *                       which the icon is being generated. Other keys 
     *                       include 'size' for a [width]x[height] version of 
     *                       the icon, as opposed to the default 20x20, and 
     *                       'view' for the id of the view in which the icon is 
     *                       being displayed.
Penny Leach's avatar
Penny Leach committed
660
     * @abstract 
661
     * @return string URL for the icon
Penny Leach's avatar
Penny Leach committed
662
     */
663
    public static abstract function get_icon($options=null);
664
665
666
667
668
669
670
671
672
673
674
675
    

    // ******************** STATIC FUNCTIONS ******************** //

    public static function get_instances_by_userid($userid, $order, $offset, $limit) {
        // @todo
    }

    public static function get_metadata_by_userid($userid, $order, $offset, $limit) {
        // @todo
    }

Penny Leach's avatar
Penny Leach committed
676
677
678
679
    /**
     * whether a user will have exactly 0 or 1 of this artefact type
     * @abstract
     */
680
    public static abstract function is_singular();
Penny Leach's avatar
Penny Leach committed
681

682
683
684
685
686
687
688
    /**
     * Whether the 'note' field is for the artefact's private use
     */
    public static function is_note_private() {
        return false;
    }

Martyn Smith's avatar
Martyn Smith committed
689
690
691
692
693
694
695
696
697
    /**
     * Returns a list of key => value pairs where the key is either '_default'
     * or a langauge string, and value is a URL linking to that behaviour for
     * this artefact type
     * 
     * @param integer This is the ID of the artefact being linked to
     */
    public static abstract function get_links($id);

Penny Leach's avatar
Penny Leach committed
698
699
700
701
    // @TODO maybe uncomment this later and implement it everywhere
    // when we know a bit more about what blocks we want.
    //public abstract function render_self($options);

702
703
704
705
706
707
708
709
710

    /**
    * Returns the printable name of this artefact
    * (used in lists and such)
    */
    public function get_name() {
        return $this->get('title');
    }

711
712
713
714
715
716
717
718
719
720
721
722
    /**
    * Should the artefact be linked to from the listing on my views?
    */
    public function in_view_list() {
        return true;
    }

    /**
    * Returns a short name for the artefact to be used in a list of artefacts in a view 
    */
    public function display_title($maxlen=null) {
        if ($maxlen) {
723
            return str_shorten_text($this->get('title'), $maxlen, true);
724
725
726
727
        }
        return $this->get('title');
    }

728
729
730
731
732
733
734
    public function display_owner() {
        if ($owner = $this->get('owner')) {
            return display_name($owner);
        }
        if ($group = $this->get('group')) {
            return get_field('group', 'name', 'id', $group);
        }
735
736
737
738
739
740
        if ($institution = $this->get('institution')) {
            if ($institution == 'mahara') {
                return get_config('sitename');
            }
            return get_field('institution', 'displayname', 'name', $institution);
        }
741
742
743
744
        return null;
    }


745
746
747
748
749
750
751
    // ******************** HELPER FUNCTIONS ******************** //

    protected function get_artefact_type() {
        $classname = get_class($this);
        
        $type = strtolower(substr($classname, strlen('ArtefactType')));

752
        if (!artefact_type_installed($type)) {
753
754
755
756
757
            throw new InvalidArgumentException("Classname $classname not a valid artefact type");
        }

        return $type;
    }
758

759
760
761
762
    public function to_stdclass() {
       return (object)get_object_vars($this); 
    }

763
764
765
766
767
768
769
770
771
772
773
    public static function has_config() {
        return false;
    }

    public static function get_config_options() {
        return array();
    }

    public static function collapse_config() {
        return false;
    }
Richard Mansfield's avatar
Richard Mansfield committed
774
775

    private function save_rolepermissions() {
776
        if (!$this->group) {
Richard Mansfield's avatar
Richard Mansfield committed
777
778
            return;
        }
779
        require_once(get_config('libroot') . 'group.php');
780
        if (!isset($this->rolepermissions)) {
781
            $this->rolepermissions = group_get_default_artefact_permissions($this->group);
782
        }
Richard Mansfield's avatar
Richard Mansfield committed
783
784
785
        $id = $this->get('id');
        db_begin();
        delete_records('artefact_access_role', 'artefact', $id);
786
        foreach ($this->rolepermissions as $role => $permissions) {
Richard Mansfield's avatar
Richard Mansfield committed
787
788
789
            insert_record('artefact_access_role', (object) array(
                'artefact'      => $id,
                'role'          => $role,
790
791
792
                'can_view'      => (int) !empty($permissions->view),
                'can_edit'      => (int) !empty($permissions->edit),
                'can_republish' => (int) !empty($permissions->republish),
Richard Mansfield's avatar
Richard Mansfield committed
793
794
795
796
797
798
            ));
        }
        db_commit();
    }

    private function load_rolepermissions() {
799
800
801
        if (!$this->group) {
            return;
        }
Richard Mansfield's avatar
Richard Mansfield committed
802
803
        $records = get_records_array('artefact_access_role', 'artefact', $this->get('id'));
        if ($records) {
804
            $this->rolepermissions = array();
Richard Mansfield's avatar
Richard Mansfield committed
805
            foreach ($records as $r) {
806
                $this->rolepermissions[$r->role] = (object) array(
Richard Mansfield's avatar
Richard Mansfield committed
807
808
809
810
811
812
                    'view' => (bool) $r->can_view,
                    'edit' => (bool) $r->can_edit,
                    'republish' => (bool) $r->can_republish,
                );
            }
        }
813
        else {
814
            $this->rolepermissions = group_get_default_artefact_permissions($this->group);
815
        }
Richard Mansfield's avatar
Richard Mansfield committed
816
817
    }

Richard Mansfield's avatar
Richard Mansfield committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
    public function copy_data() {
        $ignore = array(
            'dirty' => 1,
            'parentdirty' => 1,
            'deleted' => 1,
            'id' => 1,
            'locked' => 1,
            'rolepermissions' => 1,
            'viewsinstances' => 1,
            'viewsmetadata' => 1,
            'childreninstances' => 1,
            'childrenmetadata' => 1,
            'parentinstance' => 1,
            'parentmetadata' => 1
        );
833
        $data = new StdClass;
Richard Mansfield's avatar
Richard Mansfield committed
834
        foreach (get_object_vars($this) as $k => $v) {
835
            if (in_array($k, array('atime', 'ctime', 'mtime'))) {
836
                $data->$k = db_format_timestamp($v);
837
838
            }
            else if (!isset($ignore[$k])) {
839
                $data->$k = $v;
Richard Mansfield's avatar
Richard Mansfield committed
840
841
842
843
844
845
846
847
848
849
            }
        }
        return $data;
    }

    public function copy_extra($new) {
    }

    public function copy_for_new_owner($user, $group, $institution) {
        $data = $this->copy_data();
850
851
852
853
854
855
856
        $data->owner = $user;
        $data->group = $group;
        $data->institution = $institution;
        $data->parent = null;
        $classname = generate_artefact_class_name($data->artefacttype);
        safe_require('artefact', get_field('artefact_installed_type', 'plugin', 'name', $data->artefacttype));
        $copy = new $classname(0, (object) $data);
Richard Mansfield's avatar
Richard Mansfield committed
857
        $this->copy_extra($copy);
858
        $copy->commit();
Richard Mansfield's avatar
Richard Mansfield committed
859
860
        return $copy->get('id');
    }
Richard Mansfield's avatar
Richard Mansfield committed
861

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
    /**
     * Called after a view has been copied to rewrite all artefact
     * references in the new artefact (which still point to the
     * original artefacts) so that they also point to new artefacts
     * that were copied during the view copy.
     *
     * @param View $view The newly copied view
     * @param View $template The old view
     * @param array $artefactcopies The mapping between old artefact ids and new ones (created in blockinstance copy)
     * @param integer $oldid id of the artefact this artefact was copied from
     */
    public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
        $copyinfo = $artefactcopies[$oldid];
        if (isset($artefactcopies[$copyinfo->oldparent])) {
            $this->set('parent', $artefactcopies[$copyinfo->oldparent]->newid);
        }
        else {
879
            $this->set('parent', $this->default_parent_for_copy($view, $template, array_map(create_function('$a', 'return $a->newid;'), $artefactcopies)));
880
881
882
        }
    }

883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
    /**
     * Returns the ID of the artefact that should be the parent for copied 
     * artefacts - e.g. the folder that files should be placed in.
     *
     * The $artefactstoignore is used to work around bug #3106
     *
     * @param View $view The new view being created by the copy
     * @param View $template The view being copied from
     * @param array $artefacttoignore A list of artefact IDs to ignore. In 
     *                                particular, it's a list of artefact IDs 
     *                                that have been created by a view being 
     *                                copied. This is so we don't accidentally 
     *                                try to use a new artefact as the parent 
     *                                for all of the artefacts, else we can get 
     *                                into a nasty infinite loop (e.g. when a 
     *                                folder called 'viewfiles' is being 
     *                                copied).
     */
    public function default_parent_for_copy(&$view, &$template, $artefactstoignore) {
902
903
904
        return null;
    }

Richard Mansfield's avatar
Richard Mansfield committed
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
    public function can_be_logged() {
        return false;
    }

    public function log($action) {
        global $USER;
        $entry = (object) array(
            'artefact' => $this->id,
            'usr'      => $USER->get('id'),
            'time'     => db_format_timestamp($this->mtime),
            $action    => 1,
        );
        if ($action == 'deleted') {
            insert_record('artefact_log', $entry);
            return;
        }
        $loggedfields = array('title', 'description', 'parent');
        if ($action == 'edited') {
            $old = get_record('artefact', 'id', $this->id);
            foreach ($loggedfields as $key) {
                if ($this->$key != $old->$key) {
                    $entry->$key = $this->$key;
                    $changed = true;
                }                
            }
            if (isset($changed)) {
                insert_record('artefact_log', $entry);
            }
        }
        if ($action == 'created') {
            foreach ($loggedfields as $key) {
                $entry->$key = $this->$key;
            }
            insert_record('artefact_log', $entry);
        }
    }
941

942
943
944
945
946
947
948
949
    public function can_have_attachments() {
        return false;
    }

    public function count_attachments() {
        return count_records('artefact_attachment', 'artefact', $this->get('id'));
    }

950
    public function attachment_id_list() {
951
952
953
954
955
956
957
        // During view copying, attachment_id_list can get called on artefacts of any type; don't call
        // get_column here unless it might actually return something.
        if ($this->can_have_attachments()) {
            if ($list = get_column('artefact_attachment', 'attachment', 'artefact', $this->get('id'))) {
                return $list;
            }
        }
958
959
        return array();
    }
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976

    public function attachments_from_id_list($artefactids) {
        if (empty($artefactids)) {
            return array();
        }
        $attachments = get_records_sql_array('
            SELECT
                aa.artefact, aa.attachment, a.artefacttype, a.title, a.description
            FROM {artefact_attachment} aa
                INNER JOIN {artefact} a ON aa.attachment = a.id
            WHERE aa.artefact IN (' . join(', ', $artefactids) . ')', '');
        if (!$attachments) {
            return array();
        }
        return $attachments;
    }

977
    public function get_attachments($assoc=false) {
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
        $list = get_records_sql_assoc('SELECT a.id, a.artefacttype, a.title, a.description 
            FROM {artefact_attachment} aa
            INNER JOIN {artefact} a ON a.id = aa.attachment
            WHERE aa.artefact = ?
            ORDER BY a.title', array($this->id));

        // load tags
        if ($list) {
            $tags = get_records_select_array('artefact_tag', 'artefact IN (' . join(',', array_keys($list)) . ')');
            if ($tags) {
                foreach ($tags as $t) {
                    $list[$t->artefact]->tags[] = $t->tag;
                }
                foreach ($list as &$attachment) {
                    if (!empty($attachment->tags)) {
                        $attachment->tags = join(', ', $attachment->tags);
                    }
                }
            }
        }
998
999
1000
1001
1002
1003
1004
        else {
            return array();
        }

        if ($assoc) {          // Remove once tablerenderers are gone.
            return $list;
        }
1005
1006
1007
1008
        return array_values($list);
    }

    public function attach($attachmentid) {
1009
1010
1011
        if (record_exists('artefact_attachment', 'artefact', $this->get('id'), 'attachment', $attachmentid)) {
            return;
        }
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
        $data = new StdClass;
        $data->artefact = $this->get('id');
        $data->attachment = $attachmentid;
        insert_record('artefact_attachment', $data);

        $data = new StdClass;
        $data->artefact = $attachmentid;
        $data->parent = $this->get('id');
        $data->dirty = true;
        insert_record('artefact_parent_cache', $data);

        // Ensure the attachment is recorded as being related to the parent as well
        if ($this->get('parent')) {
            $data = new StdClass;
            $data->artefact = $attachmentid;
            $data->parent = $this->get('parent');
            $data->dirty = 0;

            $where = $data;
            unset($where->dirty);
            ensure_record_exists('artefact_parent_cache', $where, $data);
        }
    }

    public function detach($attachmentid=null) {
        if (is_null($attachmentid)) {
            delete_records('artefact_attachment', 'artefact', $this->id);
            return;
        }
        delete_records('artefact_attachment', 'artefact', $this->get('id'), 'attachment', $attachmentid);
        delete_records('artefact_parent_cache', 'parent', $this->get('id'), 'artefact', $attachmentid);
        if ($this->get('parent')) {
            // Remove the record relating the attachment with the parent
            delete_records('artefact_parent_cache', 'parent', $this->get('parent'), 'artefact', $attachmentid);
        }
    }

    // Interface:
    public static function attached_id_list($attachmentid) {
        return get_column('artefact_attachment', 'artefact', 'attachment', $attachmentid);
    }
1053
1054
}

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
/**
 * Given an artefact plugin name, this function will test if 
 * it's installable or not.  If not, InstallationException will be thrown.
 */
function artefact_check_plugin_sanity($pluginname) {
    $classname = generate_class_name('artefact', $pluginname);
    safe_require('artefact', $pluginname);
    if (!is_callable(array($classname, 'get_artefact_types'))) {
        throw new InstallationException(get_string('artefactpluginmethodmissing', 'error', $classname, 'get_artefact_types'));
    }
    if (!is_callable(array($classname, 'get_block_types'))) {
        throw new InstallationException(get_string('artefactpluginmethodmissing', 'error', $classname, 'get_block_types'));
    }
    $types = call_static_method($classname, 'get_artefact_types');
    foreach ($types as $type) {
        $typeclassname = generate_artefact_class_name($type);
        if (get_config('installed')) {
            if ($taken = get_record_select('artefact_installed_type', 'name = ? AND plugin != ?', 
                                           array($type, $pluginname))) {
1074
1075
1076
1077
1078
1079
                // Check the other plugin's code in case the duplicate type is being removed from it at the same time
                $otherclass = generate_class_name('artefact', $taken->plugin);
                safe_require('artefact', $taken->plugin);
                if (in_array($type, call_static_method($otherclass, 'get_artefact_types'))) {
                    throw new InstallationException(get_string('artefacttypenametaken', 'error', $type, $taken->plugin));
                }
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
            }
        }
        if (!class_exists($typeclassname)) {
            throw new InstallationException(get_string('classmissing', 'error', $typeclassname, $type, $plugin));
        }
    }
    $types = call_static_method($classname, 'get_block_types');
    foreach ($types as $type) {
        $pluginclassname = generate_class_name('blocktype', 'image');
        if (get_config('installed')) {
            if (table_exists(new XMLDBTable('blocktype_installed')) && $taken = get_record_select('blocktype_installed', 
                'name = ? AND artefactplugin != ? ',
                array($type, $pluginname))) {
                throw new InstallationException(get_string('blocktypenametaken', 'error', $type,
                    ((!empty($taken->artefactplugin)) ? $taken->artefactplugin : get_string('system'))));
            }
        }
        // go look for the lib file to include
        try {
            safe_require('blocktype', $pluginname . '/' . $type);
        }
        catch (Exception $_e) {
            throw new InstallationException(get_string('blocktypelibmissing', 'error', $type, $pluginname));
        }
        if (!class_exists($pluginclassname)) {
            throw new InstallationException(get_string('classmissing', 'error', $pluginclassname, $type, $pluginname));
        }
    }
}

1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
function rebuild_artefact_parent_cache_dirty() {
    // this will give us a list of artefacts, as the first returned column
    // is not unqiue, but that's ok, it's what we want.
    if (!$dirty = get_records_array('artefact_parent_cache', 'dirty', 1, '', 'DISTINCT(artefact)')) {
        return;
    }
    db_begin();
    delete_records('artefact_parent_cache', 'dirty', 1);
    foreach ($dirty as $d) {
        $parentids = array();
        $current = $d->artefact;
        delete_records('artefact_parent_cache', 'artefact', $current);
        $parentids = array_keys(artefact_get_parents_for_cache($current));
        foreach ($parentids as $p) {
            $apc = new StdClass;
            $apc->artefact = $d->artefact;
            $apc->parent   = $p;
            $apc->dirty    = 0;
            insert_record('artefact_parent_cache', $apc);
        }
    }
    db_commit();
}

function rebuild_artefact_parent_cache_complete() {
    db_begin();
    delete_records('artefact_parent_cache');
    if ($artefactids = get_column('artefact', 'id')) {
        foreach ($artefactids as $id) {
            $parentids = array_keys(artefact_get_parents_for_cache($id));
            foreach ($parentids as $p) {
                $apc = new StdClass;
                $apc->artefact = $id;
                $apc->parent   = $p;
                $apc->dirty    = 0;
                insert_record('artefact_parent_cache', $apc);
            }
        }
    }
    db_commit();
}

1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
function artefact_get_attachment_types() {
    static $artefacttypes = null;
    if (is_null($artefacttypes)) {
        $artefacttypes = array();
        foreach (require_artefact_plugins() as $plugin) {
            $classname = generate_class_name('artefact', $plugin->name);
            if (!is_callable($classname . '::get_attachment_types')) {
                continue;
            }
            $artefacttypes = array_merge($artefacttypes, call_static_method($classname, 'get_attachment_types'));
        }
    }
    return $artefacttypes;
}
1166

1167
function artefact_get_parents_for_cache($artefactids, &$parentids=false) {
1168
    if (!is_array($artefactids)) {
1169
1170
1171
        $artefactids = array($artefactids);
    }
    $current = $artefactids;
1172
1173
1174
1175
    if (empty($parentids)) { // first call
        $parentids = array();
    }
    while (true) {
1176
        if (!$parents = get_records_select_array('artefact', 'id IN (' . join(',',$current) . ')')) {
1177
1178
            break;
        }
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202

        // get any blog posts these artefacts may be attached to
        $checkattachments = array();
        foreach ($parents as $p) {
            if (in_array($p->artefacttype, artefact_get_attachment_types())) {
                $checkattachments[] = $p->id;
            }
        }
        if (!empty($checkattachments)) {
            if ($associated = get_records_select_assoc('artefact_attachment', 'attachment IN (' . join(',', $checkattachments) . ')')) {
                $associated = array_keys($associated);
                foreach ($associated as $a) {
                    $parentids[$a] = 1;
                }
                artefact_get_parents_for_cache($associated, $parentids);
            }
        }

        // check parents
        $current = array();
        foreach ($parents as $p) {
            if ($p->parent) {
                $parentids[$p->parent] = 1;
                $current[] = $p->parent;
1203
1204
            }
        }
1205
        if (empty($current)) {
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
            break;
        }
    }
    return $parentids;
}

function artefact_can_render_to($type, $format) {
    return in_array($format, call_static_method(generate_artefact_class_name($type), 'get_render_list'));
}

function artefact_instance_from_id($id) {
    $sql = 'SELECT a.*, i.plugin 
            FROM {artefact} a 
            JOIN {artefact_installed_type} i ON a.artefacttype = i.name
            WHERE a.id = ?';
    if (!$data = get_record_sql($sql, array($id))) {
        throw new ArtefactNotFoundException(get_string('artefactnotfound', 'mahara', $id));
    }
    $classname = generate_artefact_class_name($data->artefacttype);
    safe_require('artefact', $data->plugin);
    return new $classname($id, $data);
}

/**
 * This function will return an instance of any "0 or 1" artefact. That is any
 * artefact that each user will have at most one instance of (e.g. profile
 * fields).
 *
 * @param string Is the type of artefact to return
 * @param string The user_id who owns the fetched artefact. (defaults to the
 * current user)
 *
 * @returns ArtefactType Instance of the artefact.
 */
function artefact_instance_from_type($artefact_type, $user_id=null) {
    global $USER;

    if ($user_id === null) {
        $user_id = $USER->get('id');
    }

    safe_require('artefact', get_field('artefact_installed_type', 'plugin', 'name', $artefact_type));

    if (!call_static_method(generate_artefact_class_name($artefact_type), 'is_singular')) {
        throw new ArtefactNotFoundException("This artefact type is not a 'singular' artefact type");
    }

    // email is special (as in the user can have more than one of them, but
    // it's treated as a 0 or 1 artefact and the primary is returned
    if ($artefact_type == 'email') {
        $id = get_field('artefact_internal_profile_email', 'artefact', 'owner', $user_id, 'principal', 1);

        if (!$id) {
            throw new ArtefactNotFoundException("Artefact of type '${artefact_type}' doesn't exist");
        }

        $classname = generate_artefact_class_name($artefact_type);
        safe_require('artefact', 'internal');
        return new $classname($id);
    }
    else {
        $sql = 'SELECT a.*, i.plugin 
                FROM {artefact} a 
                JOIN {artefact_installed_type} i ON a.artefacttype = i.name
                WHERE a.artefacttype = ? AND a.owner = ?';
        if (!$data = get_record_sql($sql, array($artefact_type, $user_id))) {
            throw new ArtefactNotFoundException("Artefact of type '${artefact_type}' doesn't exist");
        }

        $classname = generate_artefact_class_name($artefact_type);
        safe_require('artefact', $data->plugin);
        return new $classname($data->id, $data);
    }

    throw new ArtefactNotFoundException("Artefact of type '${artefact_type}' doesn't exist");
}
1282

1283
function artefact_watchlist_notification($artefactids) {
1284
    // gets all the views containing this artefact or a parent of this artefact and creates a watchlist activity for each view
1285
    if ($views = get_column_sql('SELECT DISTINCT view FROM {view_artefact} WHERE artefact IN (' . implode(',', array_merge(array_keys(artefact_get_parents_for_cache($artefactids)), $artefactids)) . ')')) {
1286
        require_once('activity.php');
1287
1288
1289
1290
1291
1292
        foreach ($views as $view) {
            activity_occurred('watchlist', (object)array('view' => $view));
        }
    }
}

Richard Mansfield's avatar
Richard Mansfield committed
1293
1294
function artefact_get_descendants($new) {
    $seen = array();
1295
1296
1297
1298
1299
1300
1301
    $new = array_combine($new, $new);
    while (!empty($new)) {
        $seen = $seen + $new;
        $children = get_column_sql('
            SELECT id
            FROM {artefact}
            WHERE parent IN (' . implode(',', $new) . ') AND id NOT IN (' . implode(',', $seen) . ')', array());
Richard Mansfield's avatar
Richard Mansfield committed
1302
        if ($children) {
1303
1304
1305
1306
1307
            $new = array_diff($children, $seen);
            $new = array_combine($new, $new);
        }
        else {
            $new = array();
Richard Mansfield's avatar
Richard Mansfield committed
1308
1309
        }
    }
1310
    return array_values($seen);
Richard Mansfield's avatar
Richard Mansfield committed
1311
1312
}

1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
function artefact_owner_sql($userid=null, $groupid=null, $institution=null) {
    if ($institution) {
        return 'institution = ' . db_quote($institution);
    }
    if ($groupid) {
        return '"group" = ' . (int)$groupid;
    }
    if ($userid) {
        return 'owner = ' . (int)$userid;
    }
    return null;
}

1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
/**
 * Given a string of html, look for references to artefacts in it and return a 
 * list of artefact IDs found
 *
 * @return array List of artefact IDs found
 */
function artefact_get_references_in_html($html) {
    $matches = array();

    // Look for links to artefacts
    preg_match_all('#<a[^>]+href="[^>]+artefact/file/download\.php\?file=(\d+)#', $html, $matches);
    $artefacts = $matches[1];

    // Look for images sourcing artefacts
    preg_match_all('#<img[^>]+src="[^>]+artefact/file/download\.php\?file=(\d+)#', $html, $matches);
    $artefacts = array_unique(array_merge($artefacts, $matches[1]));

    // TODO: might have to look for object tags etc. later

    return $artefacts;
}

1348
1349
1350
1351
1352
1353
1354
1355
1356
function artefact_get_records_by_id($ids) {
    if (!empty($ids)) {
        if ($records = get_records_select_assoc('artefact', 'id IN (' . join(',', $ids) . ')')) {
            return $records;
        }
    }
    return array();
}

1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
function artefact_type_installed($type) {
    static $types = array();

    if (!$types) {
        $types = get_records_assoc('artefact_installed_type');
    }

    return isset($types[$type]);
}

1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
function require_artefact_plugins() {
    static $plugins = null;
    if (is_null($plugins)) {
        $plugins = plugins_installed('artefact');
        foreach ($plugins as $plugin) {
            safe_require('artefact', $plugin->name);
        }
    }
    return $plugins;
}

1378
1379
1380
1381
1382
function artefact_get_types_from_filter($filter) {
    static $contenttype_artefacttype = null;

    if (is_null($contenttype_artefacttype)) {
        $contenttype_artefacttype = array();
1383
        foreach (require_artefact_plugins() as $plugin) {
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
            $classname = generate_class_name('artefact', $plugin->name);
            if (!is_callable($classname . '::get_artefact_type_content_types')) {
                continue;
            }
            $artefacttypetypes = call_static_method($classname, 'get_artefact_type_content_types');
            foreach ($artefacttypetypes as $artefacttype => $contenttypes) {
                if (!empty($contenttypes)) {
                    foreach ($contenttypes as $ct) {
                        $contenttype_artefacttype[$ct][] = $artefacttype;
                    }
                }
            }
        }
    }

    if (empty($contenttype_artefacttype[$filter])) {
        return null;
    }

    return $contenttype_artefacttype[$filter];
}


1407
?>