lib.php 24.4 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
61
62
63
    public static function get_cron() {
        return array(
            (object)array(
                'callfunction' => 'clean_post_files',
Richard Mansfield's avatar
Richard Mansfield committed
64
65
                'hour'         => '4',
                'minute'       => '40'
66
67
68
69
70
71
72
73
74
            )
        );
    }

    /**
     * This function cleans out any files that have been uploaded, but which
     * are not associated with a blog, because of an aborted blog creation.
     */
    public static function clean_post_files() {
75

Richard Mansfield's avatar
Richard Mansfield committed
76
77
        $bloguploadbase = get_config('dataroot') . ArtefactTypeBlogPost::$blogattachmentroot;
        if (!$basedir = opendir($bloguploadbase)) {
78
79
80
            // If the directory doesn't exist, it's likely that it hasn't been 
            // created yet (i.e. nobody has uploaded any files)
            return;
Richard Mansfield's avatar
Richard Mansfield committed
81
82
83
84
85
86
87
88
89
        }

        $currenttime = time();

        // Read through all the upload session directories
        while (false !== ($sessionupload = readdir($basedir))) {
            if ($sessionupload != "." && $sessionupload != "..") {
                $sessionupload = $bloguploadbase . $sessionupload;
                $subdir = opendir($sessionupload);
90

Richard Mansfield's avatar
Richard Mansfield committed
91
92
93
94
95
96
97
98
99
100
101
102
103
                // Remove all files older than the session timeout plus two hours.
                while (false !== ($uploadfile = readdir($subdir))) {
                    if ($uploadfile != "." && $uploadfile != "..") {
                        $uploadfile = $sessionupload . '/' . $uploadfile;
                        if ($currenttime - filemtime($uploadfile) > get_config('session_timeout') + 7200) {
                            unlink($uploadfile);
                        }
                    }
                }

                closedir($subdir);
                rmdir($sessionupload);
            }
104
        }
Richard Mansfield's avatar
Richard Mansfield committed
105
106

        closedir($basedir);
107
    }
108
109
110
111
112
113
114
}

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

Alastair Pharo's avatar
Alastair Pharo committed
115
116
117
118
    /**
     * This constant gives the per-page pagination for listing blogs.
     */
    const pagination = 10;
119
120


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

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

    public function is_container() {
        return true;
137
    }
Alastair Pharo's avatar
Alastair Pharo committed
138
139

    /**
140
141
142
     * 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
143
     */
144
    public function commit() {
145
146
147
148
149
150
151
152
153
154
155
156
        // 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;
157
158
    }

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

        // Delete the artefact and all children.
        parent::delete();
170
171
    }

172
173
174
175
176
177
178
179
180
181
182
183
184
    /**
     * 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'));
        }
    }

185

186
187
    public function describe_size() {
        return $this->count_children() . ' ' . get_string('posts', 'artefact.blog');
188
189
    }

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    /**
     * 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 is because if there are multiple blocks on a page, they need separate
        // js variables.
        $blockid = isset($options['blockid'])
            ? $options['blockid']
            : mt_rand();

        $this->add_to_render_path($options);

        $smarty = smarty_core();
        if (isset($options['viewid'])) {
211
212
            $smarty->assign('artefacttitle', '<a href="' . get_config('wwwroot') . 'view/artefact.php?artefact='
                                             . $this->get('id') . '&view=' . $options['viewid']
213
214
215
216
217
218
219
220
221
222
223
                                             . '">' . $this->get('title') . '</a>');
        }
        else {
            $smarty->assign('artefacttitle', $this->get('title'));
        }

        $smarty->assign('blockid', $blockid);
        $smarty->assign('options', $options);
        $smarty->assign('enc_id', json_encode($this->id));
        $smarty->assign('limit', self::pagination);
        $smarty->assign('loading_img', theme_get_url('images/loading.gif'));
224
        $smarty->assign('description', $this->get('description'));
225
226
227
228
229
230
231
232

        // Remove unnecessary options for blog posts
        unset($options['hidetitle']);
        $smarty->assign('enc_options', json_encode(json_encode($options)));

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

233
                
234
    public static function get_icon($options=null) {
235
236
    }

Nigel McNie's avatar
Nigel McNie committed
237
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
238
239
240
        return false;
    }

Alastair Pharo's avatar
Alastair Pharo committed
241
    public static function collapse_config() {
242
243
    }

Alastair Pharo's avatar
Alastair Pharo committed
244
245
246
247
248
249
250
251
252
    /**
     * This function returns a list of the given user's blogs.
     *
     * @param User
     * @return array (count: integer, data: array)
     */
    public static function get_blog_list(User $user, $limit = self::pagination, $offset = 0) {
        ($result = get_records_sql_array("
         SELECT id, title, description
253
         FROM {artefact}
Alastair Pharo's avatar
Alastair Pharo committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
         WHERE owner = ?
          AND artefacttype = 'blog'
         ORDER BY title
         LIMIT ? OFFSET ?", array($user->get('id'), $limit, $offset)))
            || ($result = array());

        $count = (int)get_field('artefact', 'COUNT(*)', 'owner', $user->get('id'), 'artefacttype', 'blog');

        return array($count, $result);
    }

    /**
     * This function creates a new blog.
     *
     * @param User
     * @param array
     */
Alastair Pharo's avatar
Alastair Pharo committed
271
    public static function new_blog(User $user, array $values) {
Alastair Pharo's avatar
Alastair Pharo committed
272
273
274
275
        $artefact = new ArtefactTypeBlog();
        $artefact->set('title', $values['title']);
        $artefact->set('description', $values['description']);
        $artefact->set('owner', $user->get('id'));
276
        $artefact->set('tags', $values['tags']);
Alastair Pharo's avatar
Alastair Pharo committed
277
        $artefact->commit();
278
    }
Alastair Pharo's avatar
Alastair Pharo committed
279
280
281
282
283
284
285
286
287
288
289
290
291

    /**
     * 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']);
292
293
294
295
        if ($user->get('id') != $artefact->get('owner')) {
            return;
        }
        
Alastair Pharo's avatar
Alastair Pharo committed
296
297
        $artefact->set('title', $values['title']);
        $artefact->set('description', $values['description']);
298
        $artefact->set('tags', $values['tags']);
Alastair Pharo's avatar
Alastair Pharo committed
299
300
        $artefact->commit();
    }
301

Martyn Smith's avatar
Martyn Smith committed
302
303
    public static function get_links($id) {
        $wwwroot = get_config('wwwroot');
304

Martyn Smith's avatar
Martyn Smith committed
305
306
307
308
309
        return array(
            '_default'                                  => $wwwroot . 'artefact/blog/view/?id=' . $id,
            get_string('blogsettings', 'artefact.blog') => $wwwroot . 'artefact/blog/settings/?id=' . $id,
        );
    }
310
311
312
313
314
315
316
}

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

317
    /**
Alastair Pharo's avatar
Alastair Pharo committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
     * 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);

333
        if (!$data) {
334
335
336
337
338
339
            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;
                        }
340
                    }
Alastair Pharo's avatar
Alastair Pharo committed
341
                }
342
343
344
345
                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'));
                }
346
347
            }
        }
Alastair Pharo's avatar
Alastair Pharo committed
348
349
350
351
352
    }

    /**
     * This function extends ArtefactType::commit() by adding additional data
     * into the artefact_blog_blogpost table.
353
     */
354
    public function commit() {
Alastair Pharo's avatar
Alastair Pharo committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        if (empty($this->dirty)) {
            return;
        }

        $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');
        }

        $this->dirty = false;
378
379
    }

380
    /**
Alastair Pharo's avatar
Alastair Pharo committed
381
382
     * This function extends ArtefactType::delete() by also deleting anything
     * that's in blogpost.
383
     */
384
    public function delete() {
Alastair Pharo's avatar
Alastair Pharo committed
385
386
387
        if (empty($this->id)) {
            return;
        }
388

Martyn Smith's avatar
Martyn Smith committed
389
        delete_records('artefact_blog_blogpost_file', 'blogpost', $this->id);
Alastair Pharo's avatar
Alastair Pharo committed
390
391
392
393
        delete_records('artefact_blog_blogpost', 'blogpost', $this->id);
      
        parent::delete();
    }
394
395
396
397
398
399
400
401
402
403
404
405
406

    /**
     * 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'));
        }
    }
407
  
408
409
    public function describe_size() {
        return $this->count_attachments() . ' ' . get_string('attachments', 'artefact.blog');
410
411
    }

412
413
414
415
    public function render_self($options) {
        $smarty = smarty_core();
        if (empty($options['hidetitle'])) {
            if (isset($options['viewid'])) {
416
417
                $smarty->assign('artefacttitle', '<a href="' . get_config('wwwroot') . 'view/artefact.php?artefact='
                     . $this->get('id') . '&view=' . $options['viewid']
418
419
420
421
422
423
424
425
426
427
                     . '">' . $this->get('title') . '</a>');
            }
            else {
                $smarty->assign('artefacttitle', $this->get('title'));
            }
        }

        // We need to make sure that the images in the post have the right viewid associated with them
        $postcontent = $this->get('description');
        if (isset($options['viewid'])) {
428
429
            safe_require('artefact', 'file');
            $postcontent = ArtefactTypeFolder::append_view_url($postcontent, $options['viewid']);
430
431
432
433
434
435
        }
        $smarty->assign('artefactdescription', $postcontent);
        $smarty->assign('artefact', $this);
        $attachments = $this->get_attached_files();
        if ($attachments) {
            $this->add_to_render_path($options);
436
            require_once(get_config('docroot') . 'artefact/lib.php');
437
438
439
440
            foreach ($attachments as &$attachment) {
                $f = artefact_instance_from_id($attachment->id);
                $attachment->size = $f->describe_size();
                $attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => $options['viewid']));
441
                $attachment->viewpath = get_config('wwwroot') . 'view/artefact.php?artefact=' . $attachment->id . '&view=' . $options['viewid'];
442
443
                $attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
                if (isset($options['viewid'])) {
444
                    $attachment->downloadpath .= '&id=' . $options['viewid'];
445
446
447
448
449
450
451
452
453
454
455
456
                }
            }
            $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' => '');
    }


457
458
459
    /**
     * Returns an array of IDs of artefacts attached to this blogpost
     */
460
461
462
463
464
465
466
467
468
469
470
471
    public function attachment_id_list() {
        if (!$list = get_column('artefact_blog_blogpost_file', 'file', 'blogpost', $this->get('id'))) {
            $list = array();
        }
        return $list;
    }

    public function attach_file($artefactid) {
        $data = new StdClass;
        $data->blogpost = $this->get('id');
        $data->file = $artefactid;
        insert_record('artefact_blog_blogpost_file', $data);
472

473
474
475
        $data = new StdClass;
        $data->artefact = $artefactid;
        $data->parent = $this->get('id');
476
477
        $data->dirty = true;
        insert_record('artefact_parent_cache', $data);
478
479
480
481
482
483

        // Ensure the attachment is recorded as being related to the blog as well
        $data = new StdClass;
        $data->artefact = $artefactid;
        $data->parent = $this->get('parent');
        $data->dirty = 0;
484
485
486
487

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

    public function detach_file($artefactid) {
        delete_records('artefact_blog_blogpost_file', 'blogpost', $this->get('id'), 'file', $artefactid);
492
        delete_records('artefact_parent_cache', 'parent', $this->get('id'), 'artefact', $artefactid);
493
494
        // Remove the record relating the attachment with the blog
        delete_records('artefact_parent_cache', 'parent', $this->get('parent'), 'artefact', $artefactid);
495
496
    }

Richard Mansfield's avatar
Richard Mansfield committed
497
498
499
500
501
502

    protected function count_attachments() {
        return count_records('artefact_blog_blogpost_file', 'blogpost', $this->get('id'));
    }


503
    public static function get_icon($options=null) {
504
505
    }

Nigel McNie's avatar
Nigel McNie committed
506
    public static function is_singular() {
Penny Leach's avatar
Penny Leach committed
507
508
509
        return false;
    }

Alastair Pharo's avatar
Alastair Pharo committed
510
    public static function collapse_config() {
511
512
    }

Alastair Pharo's avatar
Alastair Pharo committed
513
514
515
516
517
518
519
520
521
    /**
     * 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) {
522
        ($result = get_records_sql_assoc("
Alastair Pharo's avatar
Alastair Pharo committed
523
         SELECT a.id, a.title, a.description, a.ctime, a.mtime, bp.published
524
525
         FROM {artefact} a
          LEFT OUTER JOIN {artefact_blog_blogpost} bp
Alastair Pharo's avatar
Alastair Pharo committed
526
527
528
529
           ON a.id = bp.blogpost
         WHERE a.parent = ?
          AND a.artefacttype = 'blogpost'
          AND a.owner = ?
530
         ORDER BY bp.published ASC, a.ctime DESC
Alastair Pharo's avatar
Alastair Pharo committed
531
532
533
534
535
536
537
538
         LIMIT ? OFFSET ?;", array(
            $id,
            $user->get('id'),
            $limit,
            $offset
        )))
            || ($result = array());

539
540
541
542
        $count = (int)get_field('artefact', 'COUNT(*)', 'owner', $user->get('id'), 
                                'artefacttype', 'blogpost', 'parent', $id);

        // Get the attached files.
543
        if (count($result) > 0) {
544
545
546
547
            $idlist = implode(', ', array_map(create_function('$a', 'return $a->id;'), $result));
            $files = get_records_sql_array('
               SELECT
                  bf.blogpost, bf.file, a.artefacttype, a.title, a.description
548
549
               FROM {artefact_blog_blogpost_file} bf
                  INNER JOIN {artefact} a ON bf.file = a.id
550
551
552
553
554
555
556
               WHERE bf.blogpost IN (' . $idlist . ')', '');
            if ($files) {
                foreach ($files as $file) {
                    $result[$file->blogpost]->files[] = $file;
                }
            }
        }
Alastair Pharo's avatar
Alastair Pharo committed
557

558
        return array($count, array_values($result));
Alastair Pharo's avatar
Alastair Pharo committed
559
560
    }

561
    /** 
Alastair Pharo's avatar
Alastair Pharo committed
562
563
564
565
566
567
568
569
570
571
    /**
     * 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
572
        $artefact->set('published', $values['published']);
Alastair Pharo's avatar
Alastair Pharo committed
573
        $artefact->set('owner', $user->get('id'));
574
        $artefact->set('parent', $values['parent']);
Alastair Pharo's avatar
Alastair Pharo committed
575
        $artefact->commit();
576
        return true;
577
    }
Alastair Pharo's avatar
Alastair Pharo committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

    /** 
     * 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']);
594
        $artefact->set('tags', $values['tags']);
Alastair Pharo's avatar
Alastair Pharo committed
595
596
597
598
        $artefact->commit();
        return true;
    }

599
600
601
    // Where to store temporary blog post files under dataroot
    static $blogattachmentroot = 'artefact/blog/uploads/';

602
603
604
605
606
607
608

    public static function get_temp_file_path($createid, $uploadnumber) {
        return get_config('dataroot') . self::$blogattachmentroot 
            . session_id() . $createid . '/' . $uploadnumber;
    }


609
610
611
612
613
614
615
616
    /**
     * Returns the size of a temporary attachment
     */
    public static function temp_attachment_size($createid, $uploadnumber) {
        return filesize(self::get_temp_file_path($createid, $uploadnumber));
    }


617
618
619
620
    /** 
     * This function saves an uploaded file to a temporary directory in dataroot
     *
     */
621
    public static function save_attachment_temporary($inputname, $dirname, $filename) {
622
623
        require_once('uploadmanager.php');
        $um = new upload_manager($inputname);
624
625
626
        $result = new StdClass;
        $tempdir = self::$blogattachmentroot . $dirname;
        $result->error = $um->process_file_upload($tempdir, $filename);
627
        $result->oldextension = $um->original_filename_extension();
628
629
630
631
        $tempfile = $tempdir . '/' . $filename;
        safe_require('artefact', 'file');
        $result->type = ArtefactTypeFile::detect_artefact_type($tempfile);
        return $result;
632
633
634
    }


635
636
637
    /**
     * Save a temporary uploaded file to the myfiles area.
     */
638
    public function save_attachment($directory, $filename, $title, $description, $oldextension, $tags) {
639
640
641
642
643
644
645

        // Create the blogfiles folder if it doesn't exist yet.
        $blogfilesid = self::blogfiles_folder_id();
        if (!$blogfilesid) {
            return false;
        }

646
647
648
649
650
651
652
        global $USER;

        safe_require('artefact', 'file');

        $data = new StdClass;
        $data->title = $title;
        $data->description = $description;
653
        $data->tags = $tags;
Richard Mansfield's avatar
Richard Mansfield committed
654
655
        $data->owner = $USER->get('id');
        $data->adminfiles = 0; // No admin blogs yet...
656
        $data->parent = $blogfilesid;
657
        $data->oldextension = $oldextension;
Richard Mansfield's avatar
Richard Mansfield committed
658
659
        
        $path = self::$blogattachmentroot . $directory . '/' . $filename;
660

Richard Mansfield's avatar
Richard Mansfield committed
661
        if (!$fileid = ArtefactTypeFile::save_file($path, $data)) {
662
663
            return false;
        }
664
665

        $this->attach_file($fileid);
666
        return $fileid;
667
668
    }

669
    public static function blogfiles_folder_id($create = true) {
670
671
672
        $name = get_string('blogfilesdirname', 'artefact.blog');
        $description = get_string('blogfilesdirdescription', 'artefact.blog');
        safe_require('artefact', 'file');
673
        return ArtefactTypeFolder::get_folder_id($name, $description, null, null, $create);
674
675
    }

676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
    // Change the name & description of a user's blogfiles folder when the user changes language pref
    public static function change_language($userid, $oldlang, $newlang) {
        $oldname = get_string_from_language($oldlang, 'blogfilesdirname', 'artefact.blog');
        safe_require('artefact', 'file');
        $blogfiles = ArtefactTypeFolder::get_folder_by_name($oldname, null, $userid);
        if (empty($blogfiles)) {
            return;
        }

        $name = get_string_from_language($newlang, 'blogfilesdirname', 'artefact.blog');
        $description = get_string_from_language($newlang, 'blogfilesdirdescription', 'artefact.blog');
        if (!empty($name)) {
            $blogfiles = artefact_instance_from_id($blogfiles->id);
            $blogfiles->set('title', $name);
            $blogfiles->set('description', $description);
            $blogfiles->commit();
        }
    }

Alastair Pharo's avatar
Alastair Pharo committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
    /**
     * 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;
    }
718
719
720
721
722
723
724
725

    /**
     * This function returns a list of files attached to a post to use
     * when displaying or editing a blog post
     *
     * @return array
     */
    public function get_attached_files() {
726
        $list = get_records_sql_array('SELECT a.id, a.artefacttype, a.title, a.description 
727
728
            FROM {artefact_blog_blogpost_file} f
            INNER JOIN {artefact} a ON a.id = f.file
729
730
            WHERE f.blogpost = ?
            ORDER BY a.title', array($this->id));
731
732

        // load tags
Martyn Smith's avatar
Martyn Smith committed
733
734
735
736
        if ($list) {
            foreach ( $list as &$attachment ) {
                $attachment->tags = join(', ', get_column('artefact_tag', 'tag', 'artefact', $attachment->id));
            }
737
738
        }
        return $list;
739
    }
740
    
Martyn Smith's avatar
Martyn Smith committed
741
742
743
744
745
746
747
    public static function get_links($id) {
        $wwwroot = get_config('wwwroot');

        return array(
            '_default'                                  => $wwwroot . 'artefact/blog/post.php?blogpost=' . $id,
        );
    }
748
}
749
750

?>