lib.php 11 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-wall
 * @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.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *
 */

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

class PluginBlocktypeWall extends SystemBlocktype {

    public static function get_title() {
        return get_string('title', 'blocktype.wall');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.wall');
    }

    public static function single_only() {
        return true;
    }

    public static function get_categories() {
        return array('internal');
    }

    public static function get_viewtypes() {
        return array('profile');
    }

    public static function render_instance(BlockInstance $instance, $editing=false) {
Penny Leach's avatar
Penny Leach committed
37
38
        global $USER;
        $owner = $instance->get_view()->get('owner');
39
40
41
        if (!$owner) {
            return '';
        }
Penny Leach's avatar
Penny Leach committed
42
        $userid = (!empty($USER) ? $USER->get('id') : 0);
Aaron Wells's avatar
Aaron Wells committed
43

44
45
46
47
48
49
        $returnstr = '';
        if (!$editing && $userid != 0) {
            $returnstr .= self::wallpost_form($instance);
            $returnstr .= self::wallpost_js();
        }

50
        $smarty = smarty_core();
51
        $smarty->assign('instanceid', $instance->get('id'));
Penny Leach's avatar
Penny Leach committed
52
        $smarty->assign('ownwall', (!empty($USER) && $USER->get('id') == $owner));
Aaron Wells's avatar
Aaron Wells committed
53
        if ($posts = self::fetch_posts($instance)) {
54
            $smarty->assign('wallposts', $posts);
55
56
57
58
59
        }
        else {
            $smarty->assign('wallmessage', get_string('noposts', 'blocktype.wall'));
        }

60
        return $returnstr . $smarty->fetch('blocktype:wall:inlineposts.tpl');
61
62
63
64
65
66
    }

    public static function has_instance_config() {
        return false;
    }

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
        $elements = array();
        $elements['sizelimitfieldset'] = array(
            'type' => 'fieldset',
            'legend' => get_string('postsizelimit', 'blocktype.wall'),
            'elements' => array(
                'postsizelimitdescription' => array(
                    'value' => get_string('postsizelimitdescription', 'blocktype.wall')
                ),
                'defaultpostsizelimit' => array(
                    'title'        => get_string('postsizelimitmaxcharacters', 'blocktype.wall'),
                    'type'         => 'text',
                    'defaultvalue' => get_config_plugin('blocktype', 'wall', 'defaultpostsizelimit'),
                    'rules' => array( 'maxlength' => 6 )
                )
            ),
        );
        return array(
            'elements' => $elements,
        );

    }

    public static function validate_config_options($form, $values) {
        if (!is_numeric($values['defaultpostsizelimit'])) {
            $form->set_error('defaultpostsizelimit', get_string('postsizelimitinvalid', 'blocktype.wall'));
        }
        else if ($values['defaultpostsizelimit'] < 0) {
            $form->set_error('defaultpostsizelimit', get_string('postsizelimittoosmall', 'blocktype.wall'));
        }
    }

    public static function save_config_options($values) {
        set_config_plugin('blocktype', 'wall', 'defaultpostsizelimit', (int)$values['defaultpostsizelimit']);
    }

    public static function postinst($prevversion) {
        if ($prevversion == 0) {
            set_config_plugin('blocktype', 'wall', 'defaultpostsizelimit', 1500); // 1500 characters
        }
    }

113
114
115
116
    public static function delete_instance(BlockInstance $instance) {
        return delete_records('blocktype_wall_post', 'instance', $instance->get('id'));
    }

117
118
119
120
121
122
123
    public function wallpost_form(BlockInstance $instance, $replyto='', $replyuser='') {
        if ($replyuser) {
            $walltoreplyto = self::get_wall_id_for_user($replyuser);
        }
        else {
            $walltoreplyto = $instance->get('id');
        }
124
        require_once('pieforms/pieform.php');
125
        return pieform(array(
126
            'name'      => 'wallpost_'.$instance->get('id'),
127
128
            'renderer'  => 'maharatable',
            'autofocus' => false,
129
            'jsform'    => true,
130
            'template'  => 'wallpost.php',
131
            'templatedir' => pieform_template_dir('wallpost.php', 'blocktype/wall'),
132
            'validatecallback' => array('PluginBlocktypeWall', 'wallpost_validate'),
133
            'successcallback' => array('PluginBlocktypeWall', 'wallpost_submit'),
134
            'jssuccesscallback' => 'wallpost_success',
135
136
137
            'elements' => array(
                'text' => array(
                    'type' => 'textarea',
138
139
                    'title' => get_string('Post', 'blocktype.wall'),
                    'hiddenlabel' => true,
140
                    'description' => bbcode_format_post_message(),
141
142
143
                    'rows' => 3,
                    'cols' => 50,
                    'defaultvalue' => '',
144
                    'width' => '100%',
145
146
147
148
                    'rules' => array(
                        'required' => true,
                        'maxlength' => get_config_plugin('blocktype', 'wall', 'defaultpostsizelimit'),
                    ),
149
                ),
150
151
152
153
                'postsizelimit' => array(
                    'type' => 'html',
                    'value' => get_string('maxcharacters', 'blocktype.wall', get_config_plugin('blocktype', 'wall', 'defaultpostsizelimit'))
                ),
154
155
                'private' => array(
                    'type' => 'checkbox',
156
                    'title' => get_string('makeyourpostprivate', 'blocktype.wall'),
157
                ),
158
159
160
161
162
163
164
165
                'instance' => array(
                    'type' => 'hidden',
                    'value' => $walltoreplyto,
                ),
                'replyto' => array(
                    'type' => 'hidden',
                    'value' => $replyto,
                ),
166
167
                'submit' => array(
                    'type' => 'submit',
168
                    'value' => get_string('Post', 'blocktype.wall'),
169
170
171
172
173
174
                ),
            ),
        ));
        // TODO if replying here, add select element for replyto other wall or own wall
        // depending on if the user we're replying to has a wall
    }
175

176
177
178
179
180
181
182
    public function wallpost_js() {
        $js = <<<EOF
function wallpost_success(form, data) {
    if ($('wall') && data.posts && data.block) {
        var wall = getFirstElementByTagAndClassName('div', 'wall', 'blockinstance_' + data.block);
        var temp = DIV();
        temp.innerHTML = data.posts;
183
        newposts = getElementsByTagAndClassName('li', 'wallpost', temp);
184
185
186
        replaceChildNodes(wall, newposts);
        if ($('wallpost_' + data.block + '_text')) {
            $('wallpost_' + data.block + '_text').value = '';
187
        }
188
189
190
191
192
        formSuccess(form, data);
    }
}
EOF;
        return "<script>$js</script>";
193
194
    }

195
196
197
198
199
200
201
202
    public static function wallpost_validate(Pieform $form, $values) {
        require_once(get_config('libroot') . 'antispam.php');
        $result = probation_validate_content($values['text']);
        if ($result !== true) {
            $form->set_error('text', get_string('newuserscantpostlinksorimages'));
        }
    }

203
204
205
206
207
208
209
210
211
212
    public static function wallpost_submit(Pieform $form, $values) {
        global $USER;
        $record = (object)array(
            'instance' => $values['instance'],
            'from'     => $USER->get('id'),
            'replyto'  => ($values['replyto']) ? $values['replyto'] : null,
            'private'  => (int)(bool)$values['private'],
            'postdate' => db_format_timestamp(time()),
            'text'     => $values['text'],
        );
213

214
        insert_record('blocktype_wall_post', $record);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

        $instance = new BlockInstance($values['instance']);
        $owner = $instance->get_view()->get('owner');
        $smarty = smarty_core();
        $smarty->assign('instanceid', $instance->get('id'));
        $smarty->assign('ownwall', (!empty($USER) && $USER->get('id') == $owner));
        if ($posts = self::fetch_posts($instance)) {
            $smarty->assign('wallposts', $posts);
        }
        $renderedposts = $smarty->fetch('blocktype:wall:inlineposts.tpl');

        $form->reply(PIEFORM_OK, array(
            'message'  => get_string('addpostsuccess', 'blocktype.wall'),
            'posts'    => $renderedposts,
            'block'    => $values['instance'],
230
            'goto'     => profile_url($owner),
231
        ));
232
    }
233

234
    public static function fetch_posts(BlockInstance $instance, $nolimit=false ) {
235
236
237
238
        global $USER;
        $owner = $instance->get_view()->get('owner');
        $userid = (!empty($USER) ? $USER->get('id') : 0);

Aaron Wells's avatar
Aaron Wells committed
239
240
241
        // We select u.id because display_name uses the 'id' field to get
        // information (we really should be passing objects with just user
        // information to it, for safety). We select it again as 'userid' to
242
        // avoid confusion in the templates
243
        $sql = '
Max's avatar
Max committed
244
            SELECT bwp.id AS postid, bwp.instance, bwp.from, bwp.replyto, bwp.private, bwp.postdate, bwp.text,' . db_format_tsfield('postdate') . ',
245
                u.id, u.id AS userid, u.username, u.firstname, u.lastname, u.preferredname, u.staff, u.admin, u.email, u.profileicon, u.urlid
Aaron Wells's avatar
Aaron Wells committed
246
                FROM {blocktype_wall_post} bwp
247
248
                JOIN {usr} u ON bwp.from = u.id
                WHERE bwp.instance = ? AND u.deleted = 0
Aaron Wells's avatar
Aaron Wells committed
249
        ' . (($owner != $userid)  ? '
250
                AND (bwp.private = 0 OR bwp.from = ' . db_quote($userid) . ') ' : '' ) . '
251
252
253
254
                ORDER BY bwp.postdate DESC
        ';
        $params = array($instance->get('id'));

255
        if ($records = get_records_sql_array($sql, $params, $nolimit ? '' : 0, $nolimit ? '' : 10)) {
256
257
            return array_map(
                create_function(
Aaron Wells's avatar
Aaron Wells committed
258
                    '$item',
259
                    '$item->displayname = display_name($item);
260
                    $item->profileurl = profile_url($item);
261
262
                    $item->deletable = PluginBlocktypeWall::can_delete_wallpost($item->from, ' . intval($owner) .');
                    return $item;'),
263
264
265
266
267
                $records
            );
        }
        return false;
    }
268
269
270
271
272

    public static function default_copy_type() {
        return 'shallow';
    }

273
274
275
276
277
278
279
    /**
     * Wall only makes sense on profile viewtypes
     */
    public static function allowed_in_view(View $view) {
        return $view->get('type') == 'profile';
    }

280
281
282
    public static function override_instance_title(BlockInstance $instance) {
        global $USER;
        $ownerid = $instance->get_view()->get('owner');
283
        if ($ownerid === null || $ownerid == $USER->get('id')) {
284
285
286
287
288
            return get_string('title', 'blocktype.wall');
        }
        return get_string('otherusertitle', 'blocktype.wall', display_name($ownerid, null, true));
    }

289
290
291
292
293
294
295
    public static function can_delete_wallpost($poster, $wallowner) {
        global $USER;
        return $USER->is_admin_for_user($wallowner) ||
            $poster == $USER->get('id') ||
            $wallowner == $USER->get('id');
    }

296
}