lib.php 25.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
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage artefact-blog
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 *
 */

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

/** 
 * Users can create blogs and blog posts using this plugin.
 */
class PluginArtefactBlog extends PluginArtefact {

    public static function get_artefact_types() {
        return array(
            'blog',
            'blogpost',
        );
    }
40
41
42
43
    
    public static function get_block_types() {
        return array();
    }
44
45
46
47
48
49
50

    public static function get_plugin_name() {
        return 'blog';
    }

    public static function menu_items() {
        return array(
Martyn Smith's avatar
Martyn Smith committed
51
            array(
52
53
                'path'   => 'myportfolio/blogs',
                'url'    => 'artefact/blog/',
54
                'title'  => get_string('myblogs', 'artefact.blog'),
55
56
                'weight' => 30,
            ),
57
58
        );
    }
59

60
    public static function get_cron() {
61
        return array();
62
63
    }

64

65
66
67
68
69
70
71
72
73
74
    public static function get_event_subscriptions() {
        return array(
            (object)array(
                'plugin'       => 'blog',
                'event'        => 'createuser',
                'callfunction' => 'create_default_blog',
            ),
        );
    }

75
76
77
78
79
80
81
    public static function block_advanced_options_element($configdata, $artefacttype) {
        $strartefacttype = get_string($artefacttype, 'artefact.blog');
        return array(
            'type' => 'fieldset',
            'name' => 'advanced',
            'collapsible' => true,
            'collapsed' => false,
82
            'legend' => get_string('moreoptions', 'artefact.blog'),
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
            'elements' => array(
                'copytype' => array(
                    'type' => 'select',
                    'title' => get_string('blockcopypermission', 'view'),
                    'description' => get_string('blockcopypermissiondesc', 'view'),
                    'defaultvalue' => isset($configdata['copytype']) ? $configdata['copytype'] : 'nocopy',
                    'options' => array(
                        'nocopy' => get_string('copynocopy', 'artefact.blog'),
                        'reference' => get_string('copyreference', 'artefact.blog', $strartefacttype),
                        'full' => get_string('copyfull', 'artefact.blog', $strartefacttype),
                    ),
                ),
            ),
        );
    }

99
100
101
102
103
104
105
106
    public static function create_default_blog($event, $user) {
        $name = display_name($user, null, true);
        $blog = new ArtefactTypeBlog(0, (object) array(
            'title'       => get_string('defaultblogtitle', 'artefact.blog', $name),
            'owner'       => $user['id'],
        ));
        $blog->commit();
    }
107
108
109
110
111
112

    public static function get_artefact_type_content_types() {
        return array(
            'blogpost' => array('text'),
        );
    }
113
114
115
116
117
118
119
}

/**
 * A Blog artefact is a collection of BlogPost artefacts.
 */
class ArtefactTypeBlog extends ArtefactType {

Alastair Pharo's avatar
Alastair Pharo committed
120
121
122
123
    /**
     * This constant gives the per-page pagination for listing blogs.
     */
    const pagination = 10;
124
125


126
127
128
129
130
131
132
133
    /**
     * We override the constructor to fetch the extra data.
     *
     * @param integer
     * @param object
     */
    public function __construct($id = 0, $data = null) {
        parent::__construct($id, $data);
134
135
136
137
138
139
140
141

        if (empty($this->id)) {
            $this->container = 1;
        }
    }

    public function is_container() {
        return true;
142
    }
Alastair Pharo's avatar
Alastair Pharo committed
143
144

    /**
145
146
147
     * This function updates or inserts the artefact.  This involves putting
     * some data in the artefact table (handled by parent::commit()), and then
     * some data in the artefact_blog_blog table.
Alastair Pharo's avatar
Alastair Pharo committed
148
     */
149
    public function commit() {
150
151
152
153
154
155
156
157
158
159
160
161
        // Just forget the whole thing when we're clean.
        if (empty($this->dirty)) {
            return;
        }
      
        // We need to keep track of newness before and after.
        $new = empty($this->id);
        
        // Commit to the artefact table.
        parent::commit();

        $this->dirty = false;
162
163
    }

Alastair Pharo's avatar
Alastair Pharo committed
164
    /**
165
166
     * This function extends ArtefactType::delete() by deleting blog-specific
     * data.
Alastair Pharo's avatar
Alastair Pharo committed
167
     */
168
    public function delete() {
169
170
171
172
173
174
        if (empty($this->id)) {
            return;
        }

        // Delete the artefact and all children.
        parent::delete();
175
176
    }

177
178
179
180
181
182
183
184
185
186
187
188
189
    /**
     * Checks that the person viewing this blog is the owner. If not, throws an 
     * AccessDeniedException. Used in the blog section to ensure only the 
     * owners of the blogs can view or change them there. Other people see 
     * blogs when they are placed in views.
     */
    public function check_permission() {
        global $USER;
        if ($USER->get('id') != $this->owner) {
            throw new AccessDeniedException(get_string('youarenottheownerofthisblog', 'artefact.blog'));
        }
    }

190

191
192
    public function describe_size() {
        return $this->count_children() . ' ' . get_string('posts', 'artefact.blog');
193
194
    }

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    /**
     * Renders a blog for a view. This involves using a tablerenderer to paginate the posts.
     *
     * This uses some legacy stuff from the old views interface, including its 
     * dependence on javascript and the table renderer, which would be nice to 
     * fix using the new pagination stuff some time.
     *
     * @param  array  Options for rendering
     * @return array  A two key array, 'html' and 'javascript'.
     */
    public function render_self($options) {
        $this->add_to_render_path($options);

        $smarty = smarty_core();
        if (isset($options['viewid'])) {
210
211
            $smarty->assign('artefacttitle', '<a href="' . get_config('wwwroot') . 'view/artefact.php?artefact='
                                             . $this->get('id') . '&view=' . $options['viewid']
Richard Mansfield's avatar
Richard Mansfield committed
212
                                             . '">' . hsc($this->get('title')) . '</a>');
213
214
        }
        else {
Richard Mansfield's avatar
Richard Mansfield committed
215
            $smarty->assign('artefacttitle', hsc($this->get('title')));
216
217
218
        }

        $smarty->assign('options', $options);
Richard Mansfield's avatar
Richard Mansfield committed
219
        $smarty->assign('description', clean_html($this->get('description')));
220
221
222

        // Remove unnecessary options for blog posts
        unset($options['hidetitle']);
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

        $page = (isset($options['page'])) ? abs(intval($options['page'])) : abs(param_integer('page', 1));
        $offset = $page ? $page * self::pagination - self::pagination : 1;

        $postids = get_column_sql("
            SELECT a.id
            FROM {artefact} a
            LEFT JOIN {artefact_blog_blogpost} bp ON a.id = bp.blogpost
            WHERE a.parent = ?
            AND bp.published = 1
            ORDER BY a.ctime DESC
            LIMIT ? OFFSET ?", array($this->get('id'), self::pagination, $offset));
        $postcount = $this->count_published_posts();

        $data = array();
        foreach($postids as $postid) {
            $blogpost = new ArtefactTypeBlogPost($postid);
            $data[] = array(
                'id' => $postid,
                'content' => $blogpost->render_self($options)
            );
        }
        $smarty->assign('postdata', $data);

        // Pagination
        if ($postcount > self::pagination) {
            $baselink = get_config('wwwroot') . 'view/artefact.php?artefact=' . $this->get('id');
            if (isset($options['viewid'])) {
                $baselink .= '&view=' . $options['viewid'];
            }

            if ($offset + self::pagination < $postcount) {
                $smarty->assign('olderpostslink',  $baselink . '&page=' . ($page + 1));
            }
            if ($offset > 0) {
                $smarty->assign('newerpostslink',  $baselink . '&page=' . ($page - 1));
            }
        }
261
262
263
264

        return array('html' => $smarty->fetch('blocktype:blog:blog_render_self.tpl'), 'javascript' => '');
    }

265
                
266
    public static function get_icon($options=null) {
267
268
    }

Nigel McNie's avatar
Nigel McNie committed
269
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
270
271
272
        return false;
    }

Alastair Pharo's avatar
Alastair Pharo committed
273
    public static function collapse_config() {
274
275
    }

Alastair Pharo's avatar
Alastair Pharo committed
276
277
278
279
280
281
    /**
     * This function returns a list of the given user's blogs.
     *
     * @param User
     * @return array (count: integer, data: array)
     */
282
283
    public static function get_blog_list($limit, $offset) {
        global $USER;
Alastair Pharo's avatar
Alastair Pharo committed
284
        ($result = get_records_sql_array("
285
286
         SELECT * FROM {artefact} WHERE owner = ? AND artefacttype = 'blog'
         ORDER BY title LIMIT ? OFFSET ?", array($USER->get('id'), $limit, $offset)))
Alastair Pharo's avatar
Alastair Pharo committed
287
288
            || ($result = array());

289
        $count = (int)get_field('artefact', 'COUNT(*)', 'owner', $USER->get('id'), 'artefacttype', 'blog');
Alastair Pharo's avatar
Alastair Pharo committed
290
291
292
293

        return array($count, $result);
    }

294
295
296
    public static function build_blog_list_html(&$blogs) {
        $smarty = smarty_core();
        $smarty->assign_by_ref('blogs', $blogs);
297
298
        $blogs->tablerows = $smarty->fetch('artefact:blog:bloglist.tpl');
        $pagination = build_pagination(array(
299
300
301
            'id' => 'bloglist_pagination',
            'class' => 'center',
            'url' => get_config('wwwroot') . 'artefact/blog/index.php',
302
303
            'jsonscript' => 'artefact/blog/index.json.php',
            'datatable' => 'bloglist',
304
305
306
307
308
309
310
311
312
313
314
            'count' => $blogs->count,
            'limit' => $blogs->limit,
            'offset' => $blogs->offset,
            'firsttext' => '',
            'previoustext' => '',
            'nexttext' => '',
            'lasttext' => '',
            'numbersincludefirstlast' => false,
            'resultcounttextsingular' => get_string('blog', 'artefact.blog'),
            'resultcounttextplural' => get_string('blogs', 'artefact.blog'),
        ));
315
316
        $blogs->pagination = $pagination['html'];
        $blogs->pagination_js = $pagination['javascript'];
317
318
    }

Alastair Pharo's avatar
Alastair Pharo committed
319
320
321
322
323
324
    /**
     * This function creates a new blog.
     *
     * @param User
     * @param array
     */
Alastair Pharo's avatar
Alastair Pharo committed
325
    public static function new_blog(User $user, array $values) {
Alastair Pharo's avatar
Alastair Pharo committed
326
327
328
329
        $artefact = new ArtefactTypeBlog();
        $artefact->set('title', $values['title']);
        $artefact->set('description', $values['description']);
        $artefact->set('owner', $user->get('id'));
330
        $artefact->set('tags', $values['tags']);
Alastair Pharo's avatar
Alastair Pharo committed
331
        $artefact->commit();
332
    }
Alastair Pharo's avatar
Alastair Pharo committed
333
334
335
336
337
338
339
340
341
342
343
344
345

    /**
     * This function updates an existing blog.
     *
     * @param User
     * @param array
     */
    public static function edit_blog(User $user, array $values) {
        if (empty($values['id']) || !is_numeric($values['id'])) {
            return;
        }

        $artefact = new ArtefactTypeBlog($values['id']);
346
347
348
349
        if ($user->get('id') != $artefact->get('owner')) {
            return;
        }
        
Alastair Pharo's avatar
Alastair Pharo committed
350
351
        $artefact->set('title', $values['title']);
        $artefact->set('description', $values['description']);
352
        $artefact->set('tags', $values['tags']);
Alastair Pharo's avatar
Alastair Pharo committed
353
354
        $artefact->commit();
    }
355

Martyn Smith's avatar
Martyn Smith committed
356
357
    public static function get_links($id) {
        $wwwroot = get_config('wwwroot');
358

Martyn Smith's avatar
Martyn Smith committed
359
360
361
362
363
        return array(
            '_default'                                  => $wwwroot . 'artefact/blog/view/?id=' . $id,
            get_string('blogsettings', 'artefact.blog') => $wwwroot . 'artefact/blog/settings/?id=' . $id,
        );
    }
364
365
366
367
368

    public function copy_extra($new) {
        $new->set('title', get_string('Copyof', 'mahara', $this->get('title')));
    }

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    /**
     * Returns the number of posts in this blog that have been published.
     *
     * The result of this function looked up from the database each time, so 
     * cache it if you know it's safe to do so.
     *
     * @return int
     */
    public function count_published_posts() {
        return (int)get_field_sql("
            SELECT COUNT(*)
            FROM {artefact} a
            LEFT JOIN {artefact_blog_blogpost} bp ON a.id = bp.blogpost
            WHERE a.parent = ?
            AND bp.published = 1", array($this->get('id')));
    }

386
387
388
389
390
391
392
}

/**
 * BlogPost artefacts occur within Blog artefacts
 */
class ArtefactTypeBlogPost extends ArtefactType {

393
    /**
Alastair Pharo's avatar
Alastair Pharo committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
     * This defines whether the blogpost is published or not.
     *
     * @var boolean
     */
    protected $published = false;

    /**
     * We override the constructor to fetch the extra data.
     *
     * @param integer
     * @param object
     */
    public function __construct($id = 0, $data = null) {
        parent::__construct($id, $data);

409
410
411
412
413
        if ($this->id) {
            if ($bpdata = get_record('artefact_blog_blogpost', 'blogpost', $this->id)) {
                foreach($bpdata as $name => $value) {
                    if (property_exists($this, $name)) {
                        $this->$name = $value;
414
                    }
Alastair Pharo's avatar
Alastair Pharo committed
415
                }
416
417
418
419
            }
            else {
                // This should never happen unless the user is playing around with blog post IDs in the location bar or similar
                throw new ArtefactNotFoundException(get_string('blogpostdoesnotexist', 'artefact.blog'));
420
421
            }
        }
Alastair Pharo's avatar
Alastair Pharo committed
422
423
424
    }

    /**
425
     * This method extends ArtefactType::commit() by adding additional data
Alastair Pharo's avatar
Alastair Pharo committed
426
     * into the artefact_blog_blogpost table.
427
428
429
430
431
     *
     * This method also works out what blockinstances this blogpost is in, and 
     * informs them that they should re-check what artefacts they have in them.
     * The post content may now link to different artefacts. See {@link 
     * PluginBlocktypeBlogPost::get_artefacts for more information}
432
     */
433
    public function commit() {
Alastair Pharo's avatar
Alastair Pharo committed
434
435
436
437
        if (empty($this->dirty)) {
            return;
        }

438
        db_begin();
Alastair Pharo's avatar
Alastair Pharo committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
        $new = empty($this->id);
      
        parent::commit();

        $this->dirty = true;

        $data = (object)array(
            'blogpost'  => $this->get('id'),
            'published' => ($this->get('published') ? 1 : 0)
        );

        if ($new) {
            insert_record('artefact_blog_blogpost', $data);
        }
        else {
            update_record('artefact_blog_blogpost', $data, 'blogpost');
        }

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
        // We want to get all blockinstances that contain this blog post. That is currently:
        // 1) All blogpost blocktypes with this post in it
        // 2) All blog blocktypes with this posts's blog in it
        //
        // With these, we tell them to rebuild what artefacts they have in them, 
        // since the post content could have changed and now have links to 
        // different artefacts in it
        $blockinstanceids = (array)get_column_sql('SELECT block
            FROM {view_artefact}
            WHERE artefact = ?
            OR artefact = ?', array($this->get('id'), $this->get('parent')));
        if ($blockinstanceids) {
            require_once(get_config('docroot') . 'blocktype/lib.php');
            foreach ($blockinstanceids as $id) {
                $instance = new BlockInstance($id);
                $instance->rebuild_artefact_list();
            }
        }

        db_commit();
Alastair Pharo's avatar
Alastair Pharo committed
477
        $this->dirty = false;
478
479
    }

480
    /**
Alastair Pharo's avatar
Alastair Pharo committed
481
482
     * This function extends ArtefactType::delete() by also deleting anything
     * that's in blogpost.
483
     */
484
    public function delete() {
Alastair Pharo's avatar
Alastair Pharo committed
485
486
487
        if (empty($this->id)) {
            return;
        }
488

489
        $this->detach(); // Detach all file attachments
Alastair Pharo's avatar
Alastair Pharo committed
490
491
492
493
        delete_records('artefact_blog_blogpost', 'blogpost', $this->id);
      
        parent::delete();
    }
494
495
496
497
498
499
500
501
502
503
504
505
506

    /**
     * Checks that the person viewing this blog is the owner. If not, throws an 
     * AccessDeniedException. Used in the blog section to ensure only the 
     * owners of the blogs can view or change them there. Other people see 
     * blogs when they are placed in views.
     */
    public function check_permission() {
        global $USER;
        if ($USER->get('id') != $this->owner) {
            throw new AccessDeniedException(get_string('youarenottheownerofthisblogpost', 'artefact.blog'));
        }
    }
507
  
508
509
    public function describe_size() {
        return $this->count_attachments() . ' ' . get_string('attachments', 'artefact.blog');
510
511
    }

512
513
514
515
    public function render_self($options) {
        $smarty = smarty_core();
        if (empty($options['hidetitle'])) {
            if (isset($options['viewid'])) {
516
                $smarty->assign('artefacttitle', '<a href="' . get_config('wwwroot') . 'view/artefact.php?artefact='
517
                     . $this->get('id') . '&amp;view=' . $options['viewid']
Richard Mansfield's avatar
Richard Mansfield committed
518
                     . '">' . hsc($this->get('title')) . '</a>');
519
520
            }
            else {
Richard Mansfield's avatar
Richard Mansfield committed
521
                $smarty->assign('artefacttitle', hsc($this->get('title')));
522
523
524
525
            }
        }

        // We need to make sure that the images in the post have the right viewid associated with them
Richard Mansfield's avatar
Richard Mansfield committed
526
        $postcontent = clean_html($this->get('description'));
527
        if (isset($options['viewid'])) {
528
529
            safe_require('artefact', 'file');
            $postcontent = ArtefactTypeFolder::append_view_url($postcontent, $options['viewid']);
530
531
532
        }
        $smarty->assign('artefactdescription', $postcontent);
        $smarty->assign('artefact', $this);
533
        $attachments = $this->get_attachments();
534
535
        if ($attachments) {
            $this->add_to_render_path($options);
536
            require_once(get_config('docroot') . 'artefact/lib.php');
537
538
539
            foreach ($attachments as &$attachment) {
                $f = artefact_instance_from_id($attachment->id);
                $attachment->size = $f->describe_size();
540
541
                $attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => isset($options['viewid']) ? $options['viewid'] : 0));
                $attachment->viewpath = get_config('wwwroot') . 'view/artefact.php?artefact=' . $attachment->id . '&view=' . (isset($options['viewid']) ? $options['viewid'] : 0);
542
543
                $attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
                if (isset($options['viewid'])) {
544
                    $attachment->downloadpath .= '&view=' . $options['viewid'];
545
546
547
548
549
550
551
552
553
554
555
556
                }
            }
            $smarty->assign('attachments', $attachments);
        }
        $smarty->assign('postedbyon', get_string('postedbyon', 'artefact.blog',
                                                 display_name($this->owner),
                                                 format_date($this->ctime)));
        return array('html' => $smarty->fetch('artefact:blog:render/blogpost_renderfull.tpl'),
                     'javascript' => '');
    }


557
558
    public function can_have_attachments() {
        return true;
Richard Mansfield's avatar
Richard Mansfield committed
559
560
561
    }


562
    public static function get_icon($options=null) {
563
564
    }

Nigel McNie's avatar
Nigel McNie committed
565
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
566
567
568
        return false;
    }

Alastair Pharo's avatar
Alastair Pharo committed
569
    public static function collapse_config() {
570
571
    }

Alastair Pharo's avatar
Alastair Pharo committed
572
573
574
575
576
577
578
579
580
    /**
     * This function returns a list of the current user's blog posts, for the
     * given blog.
     *
     * @param User
     * @param integer
     * @param integer
     */
    public static function get_posts(User $user, $id, $limit = self::pagination, $offset = 0) {
581
        ($result = get_records_sql_assoc("
582
         SELECT a.id, a.title, a.description, " . db_format_tsfield('a.ctime', 'ctime') . ', ' . db_format_tsfield('a.mtime', 'mtime') . ", bp.published
583
584
         FROM {artefact} a
          LEFT OUTER JOIN {artefact_blog_blogpost} bp
Alastair Pharo's avatar
Alastair Pharo committed
585
586
587
588
           ON a.id = bp.blogpost
         WHERE a.parent = ?
          AND a.artefacttype = 'blogpost'
          AND a.owner = ?
589
         ORDER BY bp.published ASC, a.ctime DESC
Alastair Pharo's avatar
Alastair Pharo committed
590
591
592
593
594
595
596
597
         LIMIT ? OFFSET ?;", array(
            $id,
            $user->get('id'),
            $limit,
            $offset
        )))
            || ($result = array());

598
599
600
        $count = (int)get_field('artefact', 'COUNT(*)', 'owner', $user->get('id'), 
                                'artefacttype', 'blogpost', 'parent', $id);

601
        if (count($result) > 0) {
602
            // Get the attached files.
603
            $files = ArtefactType::attachments_from_id_list(array_map(create_function('$a', 'return $a->id;'), $result));
604
            if ($files) {
605
606
607
                safe_require('artefact', 'file');
                foreach ($files as &$file) {
                    $file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', array('id' => $file->attachment));
608
                    $result[$file->artefact]->files[] = $file;
609
610
                }
            }
611
612
613
614
615

            // Format dates properly
            foreach ($result as &$post) {
                $post->ctime = format_date($post->ctime, 'strftimedaydatetime');
                $post->mtime = format_date($post->mtime);
Richard Mansfield's avatar
Richard Mansfield committed
616
                $post->description = clean_html($post->description);
617
            }
618
        }
Alastair Pharo's avatar
Alastair Pharo committed
619

620
        return array($count, array_values($result));
Alastair Pharo's avatar
Alastair Pharo committed
621
622
    }

623
    /** 
Alastair Pharo's avatar
Alastair Pharo committed
624
625
626
627
628
629
630
631
632
633
    /**
     * This function creates a new blog post.
     *
     * @param User
     * @param array
     */
    public static function new_post(User $user, array $values) {
        $artefact = new ArtefactTypeBlogPost();
        $artefact->set('title', $values['title']);
        $artefact->set('description', $values['description']);
Alastair Pharo's avatar
Alastair Pharo committed
634
        $artefact->set('published', $values['published']);
Alastair Pharo's avatar
Alastair Pharo committed
635
        $artefact->set('owner', $user->get('id'));
636
        $artefact->set('parent', $values['parent']);
Alastair Pharo's avatar
Alastair Pharo committed
637
        $artefact->commit();
638
        return true;
639
    }
Alastair Pharo's avatar
Alastair Pharo committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655

    /** 
     * This function updates an existing blog post.
     *
     * @param User
     * @param array
     */
    public static function edit_post(User $user, array $values) {
        $artefact = new ArtefactTypeBlogPost($values['id']);
        if ($user->get('id') != $artefact->get('owner')) {
            return false;
        }

        $artefact->set('title', $values['title']);
        $artefact->set('description', $values['description']);
        $artefact->set('published', $values['published']);
656
        $artefact->set('tags', $values['tags']);
Alastair Pharo's avatar
Alastair Pharo committed
657
658
659
660
        $artefact->commit();
        return true;
    }

661

Alastair Pharo's avatar
Alastair Pharo committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    /**
     * This function publishes the blog post.
     *
     * @return boolean
     */
    public function publish() {
        if (!$this->id) {
            return false;
        }
        
        $data = (object)array(
            'blogpost'  => $this->id,
            'published' => 1
        );

        if (get_field('artefact_blog_blogpost', 'COUNT(*)', 'blogpost', $this->id)) {
            update_record('artefact_blog_blogpost', $data, 'blogpost');
        }
        else {
            insert_record('artefact_blog_blogpost', $data);
        }
        return true;
    }
685

686
    
Martyn Smith's avatar
Martyn Smith committed
687
688
689
690
691
692
693
    public static function get_links($id) {
        $wwwroot = get_config('wwwroot');

        return array(
            '_default'                                  => $wwwroot . 'artefact/blog/post.php?blogpost=' . $id,
        );
    }
694
695
696
697
698
699
700
701
702
703

    public function update_artefact_references(&$view, &$template, &$artefactcopies, $oldid) {
        parent::update_artefact_references($view, $template, $artefactcopies, $oldid);
        // Attach copies of the files that were attached to the old post.
        // Update <img> tags in the post body to refer to the new image artefacts.
        $regexp = array();
        $replacetext = array();
        if (isset($artefactcopies[$oldid]->oldattachments)) {
            foreach ($artefactcopies[$oldid]->oldattachments as $a) {
                if (isset($artefactcopies[$a])) {
704
                    $this->attach($artefactcopies[$a]->newid);
705
706
707
708
709
710
711
712
                }
                $regexp[] = '#<img([^>]+)src="' . get_config('wwwroot') . 'artefact/file/download.php\?file=' . $a . '"#';
                $replacetext[] = '<img$1src="' . get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactcopies[$a]->newid . '"';
            }
            $this->set('description', preg_replace($regexp, $replacetext, $this->get('description')));
        }
    }

713
714
715
716
717
    /**
     * During the copying of a view, we might be allowed to copy
     * blogposts but not the containing blog.  We need to create a new
     * blog to hold the copied posts.
     */
718
    public function default_parent_for_copy(&$view, &$template, $artefactstoignore) {
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
        static $blogid;

        if (!empty($blogid)) {
            return $blogid;
        }

        $blogname = get_string('viewposts', 'artefact.blog', $view->get('id'));
        $data = (object) array(
            'title'       => $blogname,
            'description' => get_string('postscopiedfromview', 'artefact.blog', $template->get('title')),
            'owner'       => $view->get('owner'),
            'group'       => $view->get('group'),
            'institution' => $view->get('institution'),
        );
        $blog = new ArtefactTypeBlog(0, $data);
        $blog->commit();

        $blogid = $blog->get('id');

        return $blogid;
    }
740
741
742
743
744
745
746
747

    /**
     * Looks through the blog post text for links to download artefacts, and 
     * returns the IDs of those artefacts.
     */
    public function get_referenced_artefacts_from_postbody() {
        return artefact_get_references_in_html($this->get('description'));
    }
748
}
749

750

751
?>