lib.php 50 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;
136
137
    protected $author;
    protected $authorname;
138
    protected $allowcomments;
139
    protected $approvecomments;
Richard Mansfield's avatar
Richard Mansfield committed
140
    protected $rolepermissions;
141
    protected $mtimemanuallyset;
142

Penny Leach's avatar
Penny Leach committed
143
144
145
146
147
148
149
    protected $viewsinstances;
    protected $viewsmetadata;
    protected $childreninstances;
    protected $childrenmetadata;
    protected $parentinstance;
    protected $parentmetadata;

150
151
152
153
154
155
156
157
    /** 
     * 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
158
    public function __construct($id=0, $data=null) {
159
        if (!empty($id)) {
Penny Leach's avatar
Penny Leach committed
160
161
            if (empty($data)) {
                if (!$data = get_record('artefact','id',$id)) {
162
                    throw new ArtefactNotFoundException(get_string('artefactnotfound', 'error', $id));
Penny Leach's avatar
Penny Leach committed
163
                }
164
            }
165
            $this->id = $id;
166
167
        }
        else {
168
169
            $this->ctime = $this->mtime = time();
            $this->dirty = true;
170
        }
171
172
173
174
175
        if (empty($data)) {
            $data = array();
        }
        foreach ((array)$data as $field => $value) {
            if (property_exists($this, $field)) {
176
177
178
                if (in_array($field, array('atime', 'ctime', 'mtime'))) {
                    $value = strtotime($value);
                } 
Richard Mansfield's avatar
Richard Mansfield committed
179
                if ($field == 'tags' && !is_array($value)) {
180
181
                    $value = preg_split("/\s*,\s*/", trim($value));
                }
182
183
184
185
                $this->{$field} = $value;
            }
        }

186
187
188
189
190
191
192
        $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));
            }
        }

193
194
195
196
197
198
199
200
        // 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
201
        // load group permissions
202
        if ($this->group && !is_array($this->rolepermissions)) {
Richard Mansfield's avatar
Richard Mansfield committed
203
204
205
            $this->load_rolepermissions();
        }

206
        $this->atime = time();
207
208
209
    }

    public function get_views_instances() {
210
211
212
213
214
215
216
217
218
219
220
        // @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;
221
    }
222

223
    public function get_views_metadata() {
224
225
226
227
        if (!isset($this->viewsmetadata)) {
            $this->viewsmetadata = get_records_array('view_artefact', 'artefact', $this->id);
        }
        return $this->viewsmetadata;
228
229
    }

Richard Mansfield's avatar
Richard Mansfield committed
230
231
232
233
    public function count_children() {
        return count_records('artefact', 'parent', $this->get('id'));
    }

234
235
236
237
238
239
240
    public function has_children() {
        if ($this->get_children_metadata()) {
            return true;
        }
        return false;
    }

241
242
243
244
    public function get_plugin_name() {
        return get_field('artefact_installed_type', 'plugin', 'name', $this->get('artefacttype'));
    }

Penny Leach's avatar
Penny Leach committed
245
246
247
248
249
250
251
252
253
    /** 
     * 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.
     */

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

269
270
271
272
273
274
275
276
277
    /**
     * 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
     */
278
    public function get_children_metadata() {
Penny Leach's avatar
Penny Leach committed
279
        if (!isset($this->childrenmetadata)) {
280
            $this->childrenmetadata = get_records_array('artefact', 'parent', $this->id);
Penny Leach's avatar
Penny Leach committed
281
282
        }
        return $this->childrenmetadata;
283
    }
Penny Leach's avatar
Penny Leach committed
284
285
286
287
288
289
290
291
292

    /**
     * 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
     */
293
    public function get_parent_instance() {
Penny Leach's avatar
Penny Leach committed
294
295
296
        if (!isset($this->parentinstance)) {
            $this->parentinstance = false;
            if ($parent = $this->get_parent_metadata()) {
297
                $classname = generate_artefact_class_name($parent->artefacttype);
Penny Leach's avatar
Penny Leach committed
298
299
300
301
                $this->parentinstance = new $classname($parent->id, $parent);
            }
        }
        return $this->parentinstance;
302
    }
303
304
305
306
307
308
309
310

    /** 
     * 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.
     * 
311
     * @return object - db row
312
     */
313
    public function get_parent_metadata() {
314
315
316
        if (empty($this->parent)) {
            return false;
        }
317
        return get_record('artefact','id',$this->parent);
318
319
    }

320
321
322
323
324
325
326
327
328
329
330
331
    /**
     * 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;
    }

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

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

391
392
        db_begin();

393
394
395
396
397
398
399
400
401
        $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
402
403
404
            if ($this->can_be_logged()) {
                $this->log('created');
            }
405
406
407
            if (!empty($this->parent)) {
                $this->parentdirty = true;
            }
408
409
        }
        else {
Richard Mansfield's avatar
Richard Mansfield committed
410
411
412
            if ($this->can_be_logged()) {
                $this->log('edited');
            }
413
414
            update_record('artefact', $fordb, 'id');
        }
415

Richard Mansfield's avatar
Richard Mansfield committed
416
417
418
419
        if (!empty($this->group)) {
            $this->save_rolepermissions();
        }

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

436
        artefact_watchlist_notification(array($this->id));
437

438
439
        handle_event('saveartefact', $this);

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

        db_commit();
463
464
465
    }

    /** 
466
467
468
469
470
471
472
     * 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.
473
     */
474
475
    public function delete() {
        if (empty($this->id)) {
476
            $this->dirty = false;
477
478
479
            return;
        }
      
480
        db_begin();
481

482
483
484
485
        // Call delete() on comments (if there are any)
        safe_require('artefact', 'comment');
        ArtefactTypeComment::delete_comments_onartefacts(array($this->id));

486
487
488
489
490
491
        // Call delete() on children (if there are any)
        if ($children = $this->get_children_instances()) {
            foreach ($children as $child) {
                $child->delete();
            }
        }
492

493
        artefact_watchlist_notification(array($this->id));
494

495
        self::_delete_dbrecords(array($this->id));
Richard Mansfield's avatar
Richard Mansfield committed
496
497
498
499

        if ($this->can_be_logged()) {
            $this->log('deleted');
        }
500
      
501
502
        handle_event('deleteartefact', $this);

503
        // Set flags.
504
505
        $this->dirty = false;
        $this->parentdirty = true;
506
        $this->deleted = true;
507

508
        db_commit();
509
510
    }

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
    /**
     * 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);

526
527
528
529
        // Delete comments first
        safe_require('artefact', 'comment');
        ArtefactTypeComment::delete_comments_onartefacts($artefactids);

530
531
        $records = get_records_select_assoc(
            'artefact',
532
            'id IN (' . join(',', array_map('intval', $artefactids)) . ')',
533
534
535
536
537
538
539
            null, 'artefacttype', 'id,parent,artefacttype,container'
        );

        $containers = array();
        $leaves = array();
        foreach ($records as $r) {
            if ($r->container) {
540
                $containers[$r->artefacttype][] = (int)$r->id;
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
            }
            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);
556
557
558
            if (is_mysql()) {
                set_field_select('artefact', 'parent', null, 'id IN (' . join(',', $ids) . ')', array());
            }
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
            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;
        }

603
        $idstr = '(' . join(',', array_map('intval', $artefactids)) . ')';
604
605
606

        db_begin();

607
        // Detach any files from this artefact
608
        delete_records_select('artefact_attachment', "artefact IN $idstr");
609

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

613
614
615
616
617
        // The artefacts should have no 'real' children at this point, but they
        // could still be in the artefact_parent_cache as parents if they had
        // attachments, or if any of their children had attachments.
        delete_records_select('artefact_parent_cache', "parent IN $idstr");

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
        // 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_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();
    }

636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

    /**
     * Initialise artefact author to either the artefact owner, the
     * logged-in user, or the system user.
     */
    private function set_author() {
        global $USER;
        if (isset($this->owner)) {
            $this->author = $this->owner;
        }
        else {
            $this->author = $USER->get('id');
        }
    }

Penny Leach's avatar
Penny Leach committed
651
    /**
652
    * this function provides the way to link to viewing very deeply nested artefacts
653
654
655
    * within a view
    *
    * @todo not sure the comment here is appropriate
656
    */
657
658
659
660
661
662
663
664
665
666
    public function add_to_render_path(&$options) {
        if (empty($options['path'])) {
            $options['path'] = $this->get('id');
        }
        else {
            $options['path'] .= ',' . $this->get('id');
        }
    }


667
668
669
670
671
672
673
674
675
676
    /**
     * 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
677
    /**
678
679
680
681
682
683
684
685
686
     * 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
687
     * @abstract 
688
     * @return string URL for the icon
Penny Leach's avatar
Penny Leach committed
689
     */
690
    public static abstract function get_icon($options=null);
691
692
693
694
695
696
697
698
699
700
701
702
    

    // ******************** 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
703
704
705
706
    /**
     * whether a user will have exactly 0 or 1 of this artefact type
     * @abstract
     */
707
    public static abstract function is_singular();
Penny Leach's avatar
Penny Leach committed
708

Martyn Smith's avatar
Martyn Smith committed
709
710
711
712
713
714
715
716
717
    /**
     * 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
718
719
720
721
    // @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);

722
723
724
725
726
727
728
729
730

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

731
732
733
734
735
736
737
738
739
740
741
742
    /**
    * 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) {
743
            return str_shorten_text($this->get('title'), $maxlen, true);
744
745
746
747
        }
        return $this->get('title');
    }

748
749
750
751
752
753
754
    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);
        }
755
756
757
758
759
760
        if ($institution = $this->get('institution')) {
            if ($institution == 'mahara') {
                return get_config('sitename');
            }
            return get_field('institution', 'displayname', 'name', $institution);
        }
761
762
763
764
        return null;
    }


765
766
767
768
769
770
771
    // ******************** HELPER FUNCTIONS ******************** //

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

772
        if (!artefact_type_installed($type)) {
773
774
775
776
777
            throw new InvalidArgumentException("Classname $classname not a valid artefact type");
        }

        return $type;
    }
778

779
780
781
782
    public function to_stdclass() {
       return (object)get_object_vars($this); 
    }

783
784
785
786
787
788
789
790
791
792
793
    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
794
795

    private function save_rolepermissions() {
796
        if (!$this->group) {
Richard Mansfield's avatar
Richard Mansfield committed
797
798
            return;
        }
799
        require_once(get_config('libroot') . 'group.php');
800
        if (!isset($this->rolepermissions)) {
801
            $this->rolepermissions = group_get_default_artefact_permissions($this->group);
802
        }
Richard Mansfield's avatar
Richard Mansfield committed
803
804
805
        $id = $this->get('id');
        db_begin();
        delete_records('artefact_access_role', 'artefact', $id);
806
        foreach ($this->rolepermissions as $role => $permissions) {
Richard Mansfield's avatar
Richard Mansfield committed
807
808
809
            insert_record('artefact_access_role', (object) array(
                'artefact'      => $id,
                'role'          => $role,
810
811
812
                '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
813
814
815
816
817
818
            ));
        }
        db_commit();
    }

    private function load_rolepermissions() {
819
820
821
        if (!$this->group) {
            return;
        }
Richard Mansfield's avatar
Richard Mansfield committed
822
823
        $records = get_records_array('artefact_access_role', 'artefact', $this->get('id'));
        if ($records) {
824
            $this->rolepermissions = array();
Richard Mansfield's avatar
Richard Mansfield committed
825
            foreach ($records as $r) {
826
                $this->rolepermissions[$r->role] = (object) array(
Richard Mansfield's avatar
Richard Mansfield committed
827
828
829
830
831
832
                    'view' => (bool) $r->can_view,
                    'edit' => (bool) $r->can_edit,
                    'republish' => (bool) $r->can_republish,
                );
            }
        }
833
        else {
834
            $this->rolepermissions = group_get_default_artefact_permissions($this->group);
835
        }
Richard Mansfield's avatar
Richard Mansfield committed
836
837
    }

Richard Mansfield's avatar
Richard Mansfield committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
    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
        );
853
        $data = new StdClass;
Richard Mansfield's avatar
Richard Mansfield committed
854
        foreach (get_object_vars($this) as $k => $v) {
855
            if (in_array($k, array('atime', 'ctime', 'mtime'))) {
856
                $data->$k = db_format_timestamp($v);
857
858
            }
            else if (!isset($ignore[$k])) {
859
                $data->$k = $v;
Richard Mansfield's avatar
Richard Mansfield committed
860
861
862
863
864
865
866
867
868
869
            }
        }
        return $data;
    }

    public function copy_extra($new) {
    }

    public function copy_for_new_owner($user, $group, $institution) {
        $data = $this->copy_data();
870
871
872
873
874
875
876
        $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
877
        $this->copy_extra($copy);
878
        $copy->commit();
Richard Mansfield's avatar
Richard Mansfield committed
879
880
        return $copy->get('id');
    }
Richard Mansfield's avatar
Richard Mansfield committed
881

882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
    /**
     * 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 {
899
            $this->set('parent', $this->default_parent_for_copy($view, $template, array_map(create_function('$a', 'return $a->newid;'), $artefactcopies)));
900
901
902
        }
    }

903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
    /**
     * 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) {
922
923
924
        return null;
    }

Richard Mansfield's avatar
Richard Mansfield committed
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
    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);
        }
    }
961

962
963
964
965
966
967
968
969
    public function can_have_attachments() {
        return false;
    }

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

970
    public function attachment_id_list() {
971
972
973
974
975
976
977
        // 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;
            }
        }
978
979
        return array();
    }
980
981
982
983
984

    public function attachments_from_id_list($artefactids) {
        if (empty($artefactids)) {
            return array();
        }
985
986
987
988
989
        // @todo: Join on artefact_file_files shouldn't happen below.
        // We could either assume all attachments are files and then
        // move all these attachment functions to the artefact file
        // plugin, or we could allow artefact plugins to add stuff
        // to this query.
990
991
        $attachments = get_records_sql_array('
            SELECT
992
                aa.artefact, aa.attachment, a.artefacttype, a.title, a.description, f.size
993
994
            FROM {artefact_attachment} aa
                INNER JOIN {artefact} a ON aa.attachment = a.id
995
                LEFT JOIN {artefact_file_files} f ON a.id = f.artefact
996
            WHERE aa.artefact IN (' . join(', ', array_map('intval', $artefactids)) . ')', '');
997
998
999
1000
        if (!$attachments) {
            return array();
        }
        return $attachments;
For faster browsing, not all history is shown. View entire blame