index.php 5.44 KB
Newer Older
Alastair Pharo's avatar
Alastair Pharo committed
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-blog
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Alastair Pharo's avatar
Alastair Pharo committed
9
10
11
12
 *
 */

define('INTERNAL', 1);
13
define('MENUITEM', 'content/blogs');
14
15
16
17
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'blog');
define('SECTION_PAGE', 'view');

Alastair Pharo's avatar
Alastair Pharo committed
18
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
19
define('TITLE', get_string('viewblog','artefact.blog'));
Alastair Pharo's avatar
Alastair Pharo committed
20
safe_require('artefact', 'blog');
21
require_once(get_config('libroot') . 'pieforms/pieform.php');
22
23
24
if (!PluginArtefactBlog::is_active()) {
    throw new AccessDeniedException(get_string('plugindisableduser', 'mahara', get_string('blog','artefact.blog')));
}
25
26
if ($changepoststatus = param_integer('changepoststatus', null)) {
    ArtefactTypeBlogpost::changepoststatus_form($changepoststatus);
27
28
29
30
}
if ($delete = param_integer('delete', null)) {
    ArtefactTypeBlogpost::delete_form($delete);
}
Alastair Pharo's avatar
Alastair Pharo committed
31

32
33
34
35
$id = param_integer('id', null);
if (is_null($id)) {
    if (!$records = get_records_select_array(
            'artefact',
Francois Marier's avatar
Francois Marier committed
36
            "artefacttype = 'blog' AND \"owner\" = ?",
37
38
39
            array($USER->get('id')),
            'id ASC'
        )) {
40
        die_info(get_string('nodefaultblogfound', 'artefact.blog', get_config('wwwroot')));
41
    }
42
43
    $id = $records[0]->id;
    $blog = new ArtefactTypeBlog($id, $records[0]);
44
45
46
47
}
else {
    $blog = new ArtefactTypeBlog($id);
}
48
$blog->check_permission();
Alastair Pharo's avatar
Alastair Pharo committed
49

50
51
52
$limit = param_integer('limit', 5);
$offset = param_integer('offset', 0);

53
54
55
56
57
58
59
60
61
$posts = ArtefactTypeBlogPost::get_posts($id, $limit, $offset);
$template = 'artefact:blog:posts.tpl';
$pagination = array(
    'baseurl'    => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $id,
    'id'         => 'blogpost_pagination',
    'jsonscript' => 'artefact/blog/view/index.json.php',
    'datatable'  => 'postlist',
);
ArtefactTypeBlogPost::render_posts($posts, $template, array(), $pagination);
62
63

$strpublished = json_encode(get_string('published', 'artefact.blog'));
64
65
66
$strdraft = json_encode(get_string('draft', 'artefact.blog'));
$strchangepoststatuspublish = json_encode(get_string('publish', 'artefact.blog'));
$strchangepoststatusunpublish = json_encode(get_string('unpublish', 'artefact.blog'));
67
$js = <<<EOF
68
69
function changepoststatus_success(form, data) {
    if ($('changepoststatus_' + data.id + '_currentpoststatus').value == 0) {
70
71
        removeElementClass($('posttitle_' + data.id), 'draft');
        addElementClass($('posttitle_' + data.id), 'published');
72
73
74
75
        $('poststatus' + data.id).innerHTML = {$strpublished};
        $('changepoststatus_' + data.id + '_submit').value = {$strchangepoststatusunpublish};
    }
    else {
76
77
        removeElementClass($('posttitle_' + data.id), 'published');
        addElementClass($('posttitle_' + data.id), 'draft');
78
79
80
        $('poststatus' + data.id).innerHTML = {$strdraft};
        $('changepoststatus_' + data.id + '_submit').value = {$strchangepoststatuspublish};
    }
81
82
83
84
85
86
87
88
}
function delete_success(form, data) {
    addElementClass('postdetails_' + data.id, 'hidden');
    if ($('postfiles_' + data.id)) {
        addElementClass('postfiles_' + data.id, 'hidden');
    }
    addElementClass('postdescription_' + data.id, 'hidden');
    addElementClass('posttitle_' + data.id, 'hidden');
89
    var results = \$j('#blogpost_pagination div.results').html();
90
    var oldcount = parseInt(results, 10);
91
92
    var newcount = oldcount - 1;
    \$j('#blogpost_pagination div.results').html(results.replace(oldcount, newcount));
93
    progressbarUpdate('blogpost', true);
94
}
95
EOF;
Alastair Pharo's avatar
Alastair Pharo committed
96

97
$smarty = smarty(array('paginator', 'expandable'));
98
99
$smarty->assign('PAGEHEADING', $blog->get('title'));
$smarty->assign('INLINEJAVASCRIPT', $js);
100

101
102
103
104
105
106
107
108
if (!$USER->get_account_preference('multipleblogs')) {
    $blogcount = count_records('artefact', 'artefacttype', 'blog', 'owner', $USER->get('id'));
    if ($blogcount == 1) {
        $smarty->assign('enablemultipleblogstext', 1);
    }
    else if ($blogcount > 1) {
        $smarty->assign('hiddenblogsnotification', 1);
    }
109
110
}

Alastair Pharo's avatar
Alastair Pharo committed
111
$smarty->assign_by_ref('blog', $blog);
112
$smarty->assign_by_ref('posts', $posts);
Alastair Pharo's avatar
Alastair Pharo committed
113
$smarty->display('artefact:blog:view.tpl');
114
115
exit;

116
117
function changepoststatus_submit(Pieform $form, $values) {
    $blogpost = new ArtefactTypeBlogPost((int) $values['changepoststatus']);
118
    $blogpost->check_permission();
119
120
121
122
123
124
125
126
    $newpoststatus = !($values['currentpoststatus']);
    $blogpost->changepoststatus($newpoststatus);
    if ($newpoststatus) {
        $strmessage = get_string('blogpostpublished', 'artefact.blog');
    }
    else {
        $strmessage = get_string('blogpostunpublished', 'artefact.blog');
    }
127
    $form->reply(PIEFORM_OK, array(
128
        'message' => $strmessage,
129
        'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blogpost->get('parent'),
130
        'id' => $values['changepoststatus'],
131
132
133
134
135
136
137
138
139
140
141
142
    ));
}

function delete_submit(Pieform $form, $values) {
    $blogpost = new ArtefactTypeBlogPost((int) $values['delete']);
    $blogpost->check_permission();
    if ($blogpost->get('locked')) {
        $form->reply(PIEFORM_ERR, get_string('submittedforassessment', 'view'));
    }
    $blogpost->delete();
    $form->reply(PIEFORM_OK, array(
        'message' => get_string('blogpostdeleted', 'artefact.blog'),
143
        'goto' => get_config('wwwroot') . 'artefact/blog/view/index.php?id=' . $blogpost->get('parent'),
144
145
146
        'id' => $values['delete'],
    ));
}