lib.php 69.3 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-comment
 * @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.
Richard Mansfield's avatar
Richard Mansfield committed
9
10
11
12
13
 *
 */

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

14
require_once('activity.php');
15
require_once('license.php');
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
define('MIN_RATING', 1);
define('MAX_RATING', 5);

function valid_rating($ratingstr) {
    if (empty($ratingstr)) {
        return null;
    }

    $rating = intval($ratingstr);
    if ($rating < MIN_RATING) {
        return null;
    }

    return max(MIN_RATING, min(MAX_RATING, $rating));
}

Richard Mansfield's avatar
Richard Mansfield committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class PluginArtefactComment extends PluginArtefact {

    public static function get_artefact_types() {
        return array(
            'comment',
        );
    }

    public static function get_block_types() {
        return array();
    }

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

    public static function menu_items() {
        return array();
    }

    public static function get_event_subscriptions() {
        return array();
    }

57
58
59
60
61
62
    public static function get_activity_types() {
        return array(
            (object)array(
                'name' => 'feedback',
                'admin' => 0,
                'delay' => 0,
63
64
                'allownonemethod' => 1,
                'defaultmethod' => 'email',
65
66
67
68
            )
        );
    }

69
70
71
72
    public static function can_be_disabled() {
        return false;
    }

Richard Mansfield's avatar
Richard Mansfield committed
73
74
    public static function postinst($prevversion) {
        if ($prevversion == 0) {
75
            set_config_plugin('artefact', 'comment', 'commenteditabletime', 10);
Richard Mansfield's avatar
Richard Mansfield committed
76
77
78
            foreach(ArtefactTypeComment::deleted_types() as $type) {
                insert_record('artefact_comment_deletedby', (object)array('name' => $type));
            }
Aaron Wells's avatar
Aaron Wells committed
79
80

            set_config_plugin('artefact', 'comment', 'maxindent', '5');
Richard Mansfield's avatar
Richard Mansfield committed
81
82
        }
    }
Richard Mansfield's avatar
Richard Mansfield committed
83
84
85
86
87
88

    public static function view_export_extra_artefacts($viewids) {
        $artefacts = array();
        if (!$artefacts = get_column_sql("
            SELECT artefact
            FROM {artefact_comment_comment}
89
            WHERE deletedby IS NULL AND onview IN (" . join(',', array_map('intval', $viewids)) . ')', array())) {
Richard Mansfield's avatar
Richard Mansfield committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            return array();
        }
        if ($attachments = get_column_sql('
            SELECT attachment
            FROM {artefact_attachment}
            WHERE artefact IN (' . join(',', $artefacts). ')')) {
            $artefacts = array_merge($artefacts, $attachments);
        }
        return $artefacts;
    }

    public static function artefact_export_extra_artefacts($artefactids) {
        if (!$artefacts = get_column_sql("
            SELECT artefact
            FROM {artefact_comment_comment}
105
            WHERE deletedby IS NULL AND onartefact IN (" . join(',', $artefactids) . ')', array())) {
Richard Mansfield's avatar
Richard Mansfield committed
106
107
108
109
110
111
112
113
114
115
            return array();
        }
        if ($attachments = get_column_sql('
            SELECT attachment
            FROM {artefact_attachment}
            WHERE artefact IN (' . join(',', $artefacts). ')')) {
            $artefacts = array_merge($artefacts, $attachments);
        }
        return $artefacts;
    }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    public static function get_cron() {
        return array(
            (object)array(
                'callfunction' => 'clean_feedback_notifications',
                'minute'       => '35',
                'hour'         => '22',
            ),
        );
    }

    public static function clean_feedback_notifications() {
        safe_require('notification', 'internal');
        PluginNotificationInternal::clean_notifications(array('feedback'));
    }
131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    public static function progressbar_link($artefacttype) {
        switch ($artefacttype) {
            case 'feedback':
                return 'view/sharedviews.php';
                break;
        }
    }

    public static function progressbar_additional_items() {
        return array(
            (object)array(
                    'name' => 'feedback',
                    'title' => get_string('placefeedback', 'artefact.comment'),
                    'plugin' => 'comment',
                    'active' => true,
                    'iscountable' => true,
                    'is_metaartefact' => true,
            )
        );
    }

    public static function progressbar_metaartefact_count($name) {
        global $USER;
        $meta = new stdClass();
        $meta->artefacttype = $name;
        $meta->completed = 0;
        switch ($name) {
            case 'feedback':
                $sql = "SELECT COUNT(*) AS completed
                         FROM {artefact}
                       WHERE artefacttype='comment'
                         AND owner <> ? AND author = ?";
                $count = get_records_sql_array($sql, array($USER->get('id'), $USER->get('id')));
                $meta->completed = $count[0]->completed;
                break;
            default:
                return false;
        }
        return $meta;
171
    }
Richard Mansfield's avatar
Richard Mansfield committed
172
173
174
175
176
177
178
179
}

class ArtefactTypeComment extends ArtefactType {

    protected $onview;
    protected $onartefact;
    protected $private;
    protected $deletedby;
180
    protected $requestpublic;
181
    protected $rating;
182
    protected $lastcontentupdate;
Richard Mansfield's avatar
Richard Mansfield committed
183
184
185
186
187
188
189
190
191
192
193
194
195

    public function __construct($id = 0, $data = null) {
        parent::__construct($id, $data);

        if ($this->id && ($extra = get_record('artefact_comment_comment', 'artefact', $this->id))) {
            foreach($extra as $name => $value) {
                if (property_exists($this, $name)) {
                    $this->{$name} = $value;
                }
            }
        }
    }

196
197
198
199
    public static function is_allowed_in_progressbar() {
        return false;
    }

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

    /**
     * For comments, the artefact.mtime property is displayed to users, as the "Update on" date,
     * if it is later than the artefact's creation time. The purpose of this is for transparency
     * in communication, so that people will know that a later comment may be in response to one
     * that no longer exists.
     *
     * So, we don't want the publishing
     * @see ArtefactType::set()
     */
    public function set($field, $value) {
        if (($field == 'title' || $field == 'description') && $this->{$field} != $value) {
            $this->lastcontentupdate = $this->mtime;
        }
        return parent::set($field, $value);
    }

Richard Mansfield's avatar
Richard Mansfield committed
217
218
219
220
221
222
223
224
225
226
227
228
    public function commit() {
        if (empty($this->dirty)) {
            return;
        }

        $new = empty($this->id);

        db_begin();

        parent::commit();

        $data = (object)array(
229
230
231
232
233
234
            'artefact'      => $this->get('id'),
            'onview'        => $this->get('onview'),
            'onartefact'    => $this->get('onartefact'),
            'private'       => $this->get('private'),
            'deletedby'     => $this->get('deletedby'),
            'requestpublic' => $this->get('requestpublic'),
235
            'rating'        => $this->get('rating'),
Richard Mansfield's avatar
Richard Mansfield committed
236
        );
237
238
239
        if ($this->get('lastcontentupdate')) {
            $data->lastcontentupdate = db_format_timestamp($this->get('lastcontentupdate'));
        }
Richard Mansfield's avatar
Richard Mansfield committed
240
241
242
243
244
245
246
247

        if ($new) {
            insert_record('artefact_comment_comment', $data);
        }
        else {
            update_record('artefact_comment_comment', $data, 'artefact');
        }

248
249
        if ($this->get('onview')) {
            set_field('view', 'mtime', db_format_timestamp(time()), 'id', $this->get('onview'));
250
        }
251
252
253
254
        else if ($this->get('onartefact')) {
            execute_sql("UPDATE {view} SET mtime = ? WHERE id IN (SELECT va.view FROM {view_artefact} va WHERE va.artefact = ?)", array(db_format_timestamp(time()), $this->get('onartefact')));
        }

Richard Mansfield's avatar
Richard Mansfield committed
255
256
257
258
259
260
261
262
263
        db_commit();
        $this->dirty = false;
    }

    public static function is_singular() {
        return false;
    }

    public static function get_icon($options=null) {
Pat Kira's avatar
Pat Kira committed
264
265
        // global $THEME;
        // return false;
Richard Mansfield's avatar
Richard Mansfield committed
266
267
268
269
270
271
272
273
    }

    public function delete() {
        if (empty($this->id)) {
            return;
        }
        db_begin();
        $this->detach();
274
        delete_records('artefact_comment_comment', 'artefact', $this->id);
Richard Mansfield's avatar
Richard Mansfield committed
275
276
277
278
        parent::delete();
        db_commit();
    }

279
280
281
282
283
    public static function bulk_delete($artefactids) {
        if (empty($artefactids)) {
            return;
        }

284
        $idstr = join(',', array_map('intval', $artefactids));
285
286
287
288
289
290
291
292
293
294
295
296

        db_begin();
        delete_records_select('artefact_comment_comment', 'artefact IN (' . $idstr . ')');
        parent::bulk_delete($artefactids);
        db_commit();
    }

    public static function delete_view_comments($viewid) {
        $ids = get_column('artefact_comment_comment', 'artefact', 'onview', $viewid);
        self::bulk_delete($ids);
    }

297
298
299
300
301
302
    public static function delete_comments_onartefacts($artefactids) {
        $idstr = join(',', array_map('intval', $artefactids));
        $commentids = get_column_sql("SELECT artefact FROM {artefact_comment_comment} WHERE onartefact IN ($idstr)");
        self::bulk_delete($commentids);
    }

Richard Mansfield's avatar
Richard Mansfield committed
303
304
    public static function get_links($id) {
        return array(
305
            '_default' => get_config('wwwroot') . 'artefact/comment/view.php?id=' . $id,
Richard Mansfield's avatar
Richard Mansfield committed
306
307
308
309
310
311
312
313
314
315
316
        );
    }

    public function can_have_attachments() {
        return true;
    }

    public static function deleted_types() {
        return array('author', 'owner', 'admin');
    }

317
    /**
318
319
320
     * Generates default data object required for displaying comments on the page.
     * The is called before populating with specific data to send to get_comments() as
     * an easy way to add variables to get passed to get_comments.
321
     *
322
323
324
325
326
327
328
329
330
331
332
     * int $limit              The number of comments to display (set to
     *                         0 for disabling pagination and showing all comments)
     * int $offset             The offset of comments used for pagination
     * int|string $showcomment Optionally show page with particular comment
     *                         on it or the last page. $offset will be ignored.
     *                         Specify either comment_id or 'last' respectively.
     *                         Set to null to use $offset for pagination.
     * object $view            The view object
     * object $artefact        Optional artefact object
     * bool   $export          Determines if comments are fetched for html export purposes
     * bool   $onview          Optional - is viewing artefact comments on view page so don't show edit buttons
333
     * string $sort            Optional - the sort order of the comments. Valid options are 'earliest' and 'latest'.
334
     * @return object $options Default comments data object
335
     */
336
337
338
339
340
341
342
343
    public static function get_comment_options() {
        $options = new stdClass();
        $options->limit = 10;
        $options->offset = 0;
        $options->showcomment = null;
        $options->view = null;
        $options->artefact = null;
        $options->export = false;
344
        $options->onview = false;
345
346
        $sortorder = get_user_institution_comment_sort_order();
        $options->sort = (!empty($sortorder)) ? $sortorder : 'earliest';
Aaron Wells's avatar
Aaron Wells committed
347
        $options->threaded = null;
348
349
350
351
352
353
354
355
356
357
358
        return $options;
    }

    /**
     * Generates the data object required for displaying comments on the page.
     *
     * @param   object  $options  Object of comment options
     *                            - defaults can be retrieved from get_comment_options()
     * @return  object $result    Comment data object
     */
    public static function get_comments($options) {
359
        global $USER;
Aaron Wells's avatar
Aaron Wells committed
360
        $allowedoptions = self::get_comment_options();
361
362
        // set the object's key/val pairs as variables
        foreach ($options as $key => $option) {
Aaron Wells's avatar
Aaron Wells committed
363
            if (array_key_exists($key, $allowedoptions));
364
365
            $$key = $option;
        }
366
367
368
369
        $userid = $USER->get('id');
        $viewid = $view->get('id');
        if (!empty($artefact)) {
            $canedit = $USER->can_edit_artefact($artefact);
370
371
            $owner = $artefact->get('owner');
            $isowner = $userid && $userid == $owner;
372
373
374
            $artefactid = $artefact->get('id');
        }
        else {
375
            $canedit = $USER->can_moderate_view($view);
376
377
            $owner = $view->get('owner');
            $isowner = $userid && $userid == $owner;
378
379
            $artefactid = null;
        }
380

Aaron Wells's avatar
Aaron Wells committed
381
382
383
384
385
386
387
388
        // Find out whether the page's owner has threaded comments or not
        if ($owner) {
            $threaded = get_user_institution_comment_threads($owner);
        }
        else {
            $threaded = false;
        }

389
390
391
392
393
394
        $result = (object) array(
            'limit'    => $limit,
            'offset'   => $offset,
            'view'     => $viewid,
            'artefact' => $artefactid,
            'canedit'  => $canedit,
395
            'owner'    => $owner,
396
            'isowner'  => $isowner,
397
            'export'   => $export,
398
            'sort'     => $sort,
Aaron Wells's avatar
Aaron Wells committed
399
            'threaded' => $threaded,
400
401
402
            'data'     => array(),
        );

403
        if (!empty($artefactid)) {
404
            $where = 'c.onartefact = ' . (int)$artefactid;
405
406
        }
        else {
407
            $where = 'c.onview = ' . (int)$viewid;
408
409
        }
        if (!$canedit) {
Aaron Wells's avatar
Aaron Wells committed
410
411
412
413
414
415
416
            $where .= ' AND (';
            $where .= 'c.private = 0 '; // Comment is public
            $where .= 'OR a.author = ' . (int) $userid; // You are the comment author
            if ($threaded) {
                $where .= ' OR p.author = ' . (int) $userid; // you authored the parent
            }
            $where .= ')';
417
418
419
420
        }

        $result->count = count_records_sql('
            SELECT COUNT(*)
Aaron Wells's avatar
Aaron Wells committed
421
422
423
424
425
426
            FROM
                {artefact} a
                JOIN {artefact_comment_comment} c
                    ON a.id = c.artefact
                LEFT JOIN {artefact} p
                    ON a.parent = p.id
427
428
            WHERE ' . $where);

429
        if ($result->count > 0) {
Aaron Wells's avatar
Aaron Wells committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

            // Figure out sortorder
            if (!$threaded) {
                $orderby = 'a.ctime ' . ($sort == 'latest' ? 'DESC' : 'ASC');
            }
            else {
                if ($sort != 'latest') {
                    // Threaded ascending
                    $orderby = 'a.path ASC, a.ctime ASC, a.id';
                }
                else {
                    // Threaded & descending. Sort "root comments" by descending order, and the
                    // comments below them in ascending order. (This is the only sane way to do it.)
                    if (is_mysql()) {
                        $splitfunc = 'SUBSTRING_INDEX';
                    }
                    else {
                        $splitfunc = 'SPLIT_PART';
                    }
                    $orderby = "{$splitfunc}(a.path, '/', 2) DESC, a.path ASC, a.ctime ASC, a.id";
                }
            }

453
454
455
456
457
458
459
460
461
            // If pagination is in use, see if we want to get a page with particular comment
            if ($limit) {
                if ($showcomment == 'last') {
                    // If we have limit (pagination is used) ignore $offset and just get the last page of feedback.
                    $result->forceoffset = $offset = (ceil($result->count / $limit) - 1) * $limit;
                }
                else if (is_numeric($showcomment)) {
                    // Ignore $offset and get the page that has the comment
                    // with id $showcomment on it.
Aaron Wells's avatar
Aaron Wells committed
462
                    // Fetch everything and figure out which page $showcomment is in.
463
464
                    // This will get ugly if there are 1000s of comments
                    $ids = get_column_sql('
Aaron Wells's avatar
Aaron Wells committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
                            SELECT a.id
                            FROM {artefact} a JOIN {artefact_comment_comment} c ON a.id = c.artefact
                                LEFT JOIN {artefact} p ON a.parent = p.id
                            WHERE ' . $where . '
                            ORDER BY ' . $orderby,
                            array()
                    );
                    $found = false;
                    foreach ($ids as $k => $v) {
                        if ($v == $showcomment) {
                            $found = $k;
                            break;
                        }
                    }
                    if ($found !== false) {
480
                        // Add 1 because array index starts from 0 and therefore key value is offset by 1.
Aaron Wells's avatar
Aaron Wells committed
481
                        $rank = $found + 1;
482
483
484
                        $result->forceoffset = $offset = ((ceil($rank / $limit) - 1) * $limit);
                        $result->showcomment = $showcomment;
                    }
485
                }
486
            }
487

488
489
            $comments = get_records_sql_assoc('
                SELECT
490
                    a.id, a.author, a.authorname, a.ctime, a.mtime, a.description, a.group,
491
                    c.private, c.deletedby, c.requestpublic, c.rating, c.lastcontentupdate,
492
                    u.username, u.firstname, u.lastname, u.preferredname, u.email, u.staff, u.admin,
Aaron Wells's avatar
Aaron Wells committed
493
                    u.deleted, u.profileicon, u.urlid, a.path, p.id AS parent, p.author AS parentauthor
494
495
                FROM {artefact} a
                    INNER JOIN {artefact_comment_comment} c ON a.id = c.artefact
Aaron Wells's avatar
Aaron Wells committed
496
497
                    LEFT JOIN {artefact} p
                        ON a.parent = p.id
498
                    LEFT JOIN {usr} u ON a.author = u.id
499
                WHERE ' . $where . '
Aaron Wells's avatar
Aaron Wells committed
500
                ORDER BY ' . $orderby, array(), $offset, $limit);
501

502
            $files = ArtefactType::attachments_from_id_list(array_keys($comments));
503

504
505
506
507
508
            if ($files) {
                safe_require('artefact', 'file');
                foreach ($files as &$file) {
                    $comments[$file->artefact]->attachments[] = $file;
                }
509
510
            }

Aaron Wells's avatar
Aaron Wells committed
511
512
513
514
515
516
517
            // calculate the indent tabs for the comments
            $max_depth = ($threaded ? get_config_plugin('artefact', 'comment', 'maxindent') : 1);

            $usercache = array($userid => $canedit);

            foreach($comments as &$c) {
                // You can post a public reply to a comment if you can see it & the comment is not private
518
                $c->canpublicreply = (int) self::can_public_reply_to_comment($c->private, $c->deletedby);
Aaron Wells's avatar
Aaron Wells committed
519
520
                $c->canprivatereply = (int) self::can_private_reply_to_comment(
                        $c->private,
521
                        $c->deletedby,
Aaron Wells's avatar
Aaron Wells committed
522
523
524
525
526
527
528
529
530
531
532
                        $userid,
                        $c->author,
                        $c->parentauthor,
                        $artefact,
                        $view
                );
                $c->canreply = ($threaded && ($c->canpublicreply || $c->canprivatereply)) ? 1 : 0;
                $c->indent = ($max_depth == 1) ? 1 : min($max_depth, substr_count($c->path, '/'));
                // Count indent levels starting from 0 instead of 1.
                $c->indent -= 1;
            }
533
534
            $result->data = array_values($comments);
        }
535

536
537
        // check to see if the feedback is to be displayed in a block instance
        // or the base of the page
Pat Kira's avatar
Pat Kira committed
538
        $result->position = 'base';
539
        $blocks = get_records_array('block_instance', 'view', $viewid);
Pat Kira's avatar
Pat Kira committed
540
        if (!empty($blocks)) {
541
            foreach ($blocks as $block) {
Pat Kira's avatar
Pat Kira committed
542
                if ($block->blocktype === 'comment') {
543
                    $result->position = 'blockinstance';
544
                }
545
546
547
            }
        }

548
        self::build_html($result, $onview);
549
550
551
        return $result;
    }

Aaron Wells's avatar
Aaron Wells committed
552
553
554
555
    /**
     * Can you post a public reply to this comment?
     * (Made into a separate function so we can re-use the logic)
     * @param boolean $isprivate Is the comment private?
556
     * @param int $deletedby The id of the user who deleted the comment (or null)
Aaron Wells's avatar
Aaron Wells committed
557
558
     * @return boolean
     */
559
560
    public static function can_public_reply_to_comment($isprivate, $deletedby) {
        return !($isprivate || $deletedby);
Aaron Wells's avatar
Aaron Wells committed
561
562
563
564
565
566
    }

    /**
     * Can you post a private reply to this comment?
     * (Made into a separate function so we can re-use the logic)
     * @param boolean $isprivate Is the replied-to comment private?
567
     * @param int $deletedby The id of the user who deleted the comment (or null)
Aaron Wells's avatar
Aaron Wells committed
568
569
570
571
572
573
574
     * @param int $commenter User replying to the comment
     * @param int $author Author of the replied-to comment
     * @param int $parentauthor Author of the replied-to comment's parent
     * @param ArtefactType $artefact The artefact being commented on (or null)
     * @param View $view The view being commented on (or null)
     * @return boolean
     */
575
576
577
578
579
580
581
    public static function can_private_reply_to_comment($isprivate, $deletedby, $commenter, $author, $parentauthor, $artefact=null, $view=null) {

        // Can't post a private reply to a deleted comment
        if ($deletedby) {
            return false;
        }

Aaron Wells's avatar
Aaron Wells committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
        // No private replies to anonymous comments
        // (It would be impossible for the commenter to see!)
        if (!$author) {
            return false;
        }

        // No private replies to your own private comments
        if ($isprivate && $author == $commenter) {
            return false;
        }

        // You can post a private reply to a comment that is a private reply to one of your comments
        if ($isprivate && $parentauthor == $commenter) {
            return true;
        }

        // The page owner can post private replies to others' comments
        if (self::can_moderate_comments($commenter, $artefact, $view)) {
            return true;
        }

        // Other users can post a private reply to a comment by the page owner.
        return self::can_moderate_comments($author, $artefact, $view);
    }

    /**
     * Whether a user can moderate comments on a particular (view or artefact) page
     * @param int $userid
     * @param ArtefactType $artefact
     * @param View $view
     * @return boolean
     */
    public static function can_moderate_comments($userid, $artefact=null, $view=null) {
        static $usercache = array();
        if (array_key_exists($userid, $usercache)) {
            return $usercache[$userid];
        }

        $user = new User();
        $user->find_by_id($userid);
        if ($artefact) {
            $canmod = $user->can_edit_artefact($artefact);
        }
        else {
            $canmod = $user->can_moderate_view($view);
        }
        $usercache[$userid] = $canmod;
        return $canmod;
    }

632
    public static function count_comments($viewids=null, $artefactids=null) {
633
634
635
636
        if (!empty($viewids)) {
            return get_records_sql_assoc('
                SELECT c.onview, COUNT(c.artefact) AS comments
                FROM {artefact_comment_comment} c
637
                WHERE c.onview IN (' . join(',', array_map('intval', $viewids)) . ') AND c.deletedby IS NULL
Richard Mansfield's avatar
Richard Mansfield committed
638
639
640
                GROUP BY c.onview',
                array()
            );
641
642
643
644
645
        }
        if (!empty($artefactids)) {
            return get_records_sql_assoc('
                SELECT c.onartefact, COUNT(c.artefact) AS comments
                FROM {artefact_comment_comment} c
646
                WHERE c.onartefact IN (' . join(',', array_map('intval', $artefactids)) . ') AND c.deletedby IS NULL
Richard Mansfield's avatar
Richard Mansfield committed
647
648
649
                GROUP BY c.onartefact',
                array()
            );
650
651
652
        }
    }

653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
    public static function last_public_comment($view=null, $artefact=null) {
        if (!empty($artefact)) {
            $where = 'c.onartefact = ?';
            $values = array($artefact);
        }
        else {
            $where = 'c.onview = ?';
            $values = array($view);
        }
        $newest = get_records_sql_array('
            SELECT a.id, a.ctime
            FROM {artefact} a INNER JOIN {artefact_comment_comment} c ON a.id = c.artefact
            WHERE c.private = 0 AND ' . $where . '
            ORDER BY a.ctime DESC', $values, 0, 1
        );
        return $newest[0];
    }

671
672
673
    /**
     * Fetching the comments for an artefact to display on a view
     *
674
     * @param   ArtefactType  $artfact  The artefact to display comments for
675
676
677
     * @param   object  $view     The view on which the artefact appears
     * @param   int     $blockid  The id of the block instance that connects the artefact to the view
     * @param   bool    $html     Whether to return the information rendered as html or not
678
     * @param   bool    $editing  Whether we are view edit mode or not
679
680
681
682
     *
     * @return  array   $commentcount, $comments   The count of comments and either the comments
     *                                             or the html to render them.
     */
683
    public function get_artefact_comments_for_view(ArtefactType $artefact, $view, $blockid, $html = true, $editing = false) {
684
685
        global $USER;

686
        if (!is_object($artefact) || !is_object($view)) {
687
688
689
690
691
692
693
694
695
696
697
            throw new MaharaException('we do not have the right information to display the comments');
        }

        $commentoptions = ArtefactTypeComment::get_comment_options();
        $commentoptions->limit = 0;
        $commentoptions->view = $view;
        $commentoptions->artefact = $artefact;
        $commentoptions->onview = true;
        $comments = ArtefactTypeComment::get_comments($commentoptions);
        $commentcount = isset($comments->count) ? $comments->count : 0;

698
699
700
701
702
        // If there are no comments, and comments are not allowed, don't display anything.
        if ($commentcount == 0 && !$artefact->get('allowcomments')) {
            return array(0, '');
        }

703
704
        $artefacttitle = $artefact->title;

705
706
707
708
        $artefacturl = get_config('wwwroot') . 'artefact/artefact.php?view=' . $view->get('id') . '&artefact=' . $artefact->get('id');
        if ($html) {
            $smarty = smarty_core();
            $smarty->assign('artefacturl', $artefacturl);
709
            $smarty->assign('artefacttitle', $artefacttitle);
710
711
712
            $smarty->assign('blockid', $blockid);
            $smarty->assign('commentcount', $commentcount);
            $smarty->assign('comments', $comments);
713
            $smarty->assign('editing', $editing);
714
            $smarty->assign('allowcomments', $artefact->get('allowcomments'));
715
            $smarty->assign('allowcommentsadd', ($artefact->get('allowcomments') && ( $USER->is_logged_in() || (!$USER->is_logged_in() && get_config('anonymouscomments')))));
716
717
718
719
720
721
722
723
            $render = $smarty->fetch('artefact/artefactcommentsview.tpl');
            return array($commentcount, $render);
        }
        else {
            return array($commentcount, $comments);
        }
    }

724
725
    public static function deleted_messages() {
        return array(
726
727
728
            'author' => 'commentremovedbyauthor',
            'owner'  => 'commentremovedbyowner',
            'admin'  => 'commentremovedbyadmin',
Richard Mansfield's avatar
Richard Mansfield committed
729
        );
730
731
    }

732
    public static function build_html(&$data, $onview) {
733
        global $USER, $THEME;
734
735

        require_once('pieforms/pieform.php');
736
        $candelete = $data->canedit || $USER->get('admin');
737
738
739
740
        $deletedmessage = array();
        foreach (self::deleted_messages() as $k => $v) {
            $deletedmessage[$k] = get_string($v, 'artefact.comment');
        }
741
        $authors = array();
742
743
        $lastcomment = self::last_public_comment($data->view, $data->artefact);
        $editableafter = time() - 60 * get_config_plugin('artefact', 'comment', 'commenteditabletime');
Richard Mansfield's avatar
Richard Mansfield committed
744
        foreach ($data->data as &$item) {
745

746
747
            $item->ts = strtotime($item->ctime);
            $item->date = format_date($item->ts, 'strftimedatetime');
748
749
            if ($item->ts < strtotime($item->lastcontentupdate)) {
                $item->updated = format_date(strtotime($item->lastcontentupdate), 'strftimedatetime');
750
            }
751
            $item->isauthor = $item->author && $item->author == $USER->get('id');
752
            if (!empty($item->attachments)) {
Pat Kira's avatar
Pat Kira committed
753
754
755

                $item->filescount = count($item->attachments);

Richard Mansfield's avatar
Richard Mansfield committed
756
                if ($data->isowner) {
757
758
                    $item->attachmessage = get_string(
                        'feedbackattachmessage',
759
760
                        'artefact.comment',
                        get_string('feedbackattachdirname', 'artefact.comment')
761
762
763
764
765
766
                    );
                }
                foreach ($item->attachments as &$a) {
                    $a->attachid    = $a->attachment;
                    $a->attachtitle = $a->title;
                    $a->attachsize  = display_size($a->size);
Richard Mansfield's avatar
Richard Mansfield committed
767
768
                }
            }
769
            if ($item->private) {
770
                $item->pubmessage = get_string('thiscommentisprivate', 'artefact.comment');
771
            }
772

773
774
775
            if (isset($data->showcomment) && $data->showcomment == $item->id) {
                $item->highlight = 1;
            }
776
            $is_export_preview = param_integer('export',0);
Richard Mansfield's avatar
Richard Mansfield committed
777
778
779
            if ($item->deletedby) {
                $item->deletedmessage = $deletedmessage[$item->deletedby];
            }
780
            else if (($candelete || $item->isauthor) && !$is_export_preview) {
Richard Mansfield's avatar
Richard Mansfield committed
781
782
                $item->deleteform = pieform(self::delete_comment_form($item->id));
            }
783
784

            // Comment authors can edit recent comments if they're private or if no one has replied yet.
785
            if (!$item->deletedby && $item->isauthor && !$is_export_preview
786
787
788
789
790
791
792
793
                && ($item->private || $item->id == $lastcomment->id) && $item->ts > $editableafter) {
                $item->canedit = 1;
            }

            // Form to make private comment public, or request that a
            // private comment be made public
            if (!$item->deletedby && $item->private && $item->author && $data->owner
                && ($item->isauthor || $data->isowner)) {
794
                if ((empty($item->requestpublic) && $data->isowner)
795
796
                    || $item->isauthor && $item->requestpublic == 'owner'
                    || $data->isowner && $item->requestpublic == 'author') {
797
798
799
                    if (!$is_export_preview) {
                        $item->makepublicform = pieform(self::make_public_form($item->id));
                    }
800
801
802
803
804
805
                }
                else if ($item->isauthor && $item->requestpublic == 'author'
                         || $data->isowner && $item->requestpublic == 'owner') {
                    $item->makepublicrequested = 1;
                }
            }
806
            else if (!$item->deletedby && $item->private && !$item->author
807
                && $data->owner && $data->isowner && $item->requestpublic == 'author' && !$is_export_preview) {
808
809
                $item->makepublicform = pieform(self::make_public_form($item->id));
            }
810
811
812
813
814
815
816
            else if (!$item->deletedby && $item->private && !$data->owner
                && $item->group && $item->requestpublic == 'author') {
                // no owner as comment is on a group view / artefact
                if ($item->isauthor) {
                    $item->makepublicrequested = 1;
                }
                else {
817
                    if (($data->artefact && $data->canedit) || ($data->view && $data->canedit) && !$is_export_preview) {
818
819
820
821
822
823
824
                        $item->makepublicform = pieform(self::make_public_form($item->id));
                    }
                    else {
                        $item->makepublicrequested = 1;
                    }
                }
            }
825

826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
            if ($item->author) {
                if (isset($authors[$item->author])) {
                    $item->author = $authors[$item->author];
                }
                else {
                    $item->author = $authors[$item->author] = (object) array(
                        'id'            => $item->author,
                        'username'      => $item->username,
                        'firstname'     => $item->firstname,
                        'lastname'      => $item->lastname,
                        'preferredname' => $item->preferredname,
                        'email'         => $item->email,
                        'staff'         => $item->staff,
                        'admin'         => $item->admin,
                        'deleted'       => $item->deleted,
                        'profileicon'   => $item->profileicon,
842
                        'profileurl'    => profile_url($item->author),
843
844
845
                    );
                }
            }
846

847
            if (get_config_plugin('artefact', 'comment', 'commentratings') and $item->rating) {
848
849
850
851
                $item->ratingdata = (object) array(
                    'value' => valid_rating($item->rating),
                    'min_rating' => MIN_RATING,
                    'max_rating' => MAX_RATING,
852
                    'export' => $data->export,
853
                );
854
            }
Richard Mansfield's avatar
Richard Mansfield committed
855
        }
856

Richard Mansfield's avatar
Richard Mansfield committed
857
        $extradata = array('view' => $data->view);
858
859
        $data->jsonscript = 'artefact/comment/comments.json.php';

Richard Mansfield's avatar
Richard Mansfield committed
860
        if (!empty($data->artefact)) {
861
            $data->baseurl = get_config('wwwroot') . 'artefact/artefact.php?view=' . $data->view . '&artefact=' . $data->artefact;
Richard Mansfield's avatar
Richard Mansfield committed
862
863
864
865
866
            $extradata['artefact'] = $data->artefact;
        }
        else {
            $data->baseurl = get_config('wwwroot') . 'view/view.php?id=' . $data->view;
        }
867

Richard Mansfield's avatar
Richard Mansfield committed
868
869
870
        $smarty = smarty_core();
        $smarty->assign_by_ref('data', $data->data);
        $smarty->assign('canedit', $data->canedit);
871
        $smarty->assign('position', $data->position);
872
        $smarty->assign('viewid', $data->view);
Richard Mansfield's avatar
Richard Mansfield committed
873
        $smarty->assign('baseurl', $data->baseurl);
874
        $smarty->assign('onview', $onview);
875

876
        $data->tablerows = $smarty->fetch('artefact:comment:commentlist.tpl');
Pat Kira's avatar
Pat Kira committed
877

Richard Mansfield's avatar
Richard Mansfield committed
878
879
880
881
882
        $pagination = build_pagination(array(
            'id' => 'feedback_pagination',
            'class' => 'center',
            'url' => $data->baseurl,
            'jsonscript' => $data->jsonscript,
883
            'datatable' => 'feedbacktable',
Richard Mansfield's avatar
Richard Mansfield committed
884
885
886
            'count' => $data->count,
            'limit' => $data->limit,
            'offset' => $data->offset,
887
            'forceoffset' => isset($data->forceoffset) ? $data->forceoffset : null,
888
889
            'resultcounttextsingular' => get_string('comment', 'artefact.comment'),
            'resultcounttextplural' => get_string('comments', 'artefact.comment'),
Richard Mansfield's avatar
Richard Mansfield committed
890
891
892
893
894
895
            'extradata' => $extradata,
        ));
        $data->pagination = $pagination['html'];
        $data->pagination_js = $pagination['javascript'];
    }

Richard Mansfield's avatar
Richard Mansfield committed
896
897
898
899
    public function render_self() {
        return clean_html($this->get('description'));
    }

900
    public static function add_comment_form($defaultprivate=false, $moderate=false) {
901
902
903
904
905
906
907
908
909
910
        global $USER;
        $form = array(
            'name'            => 'add_feedback_form',
            'method'          => 'post',
            'plugintype'      => 'artefact',
            'pluginname'      => 'comment',
            'jsform'          => true,
            'autofocus'       => false,
            'elements'        => array(),
            'jssuccesscallback' => 'addFeedbackSuccess',
911
            'jserrorcallback'   => 'addFeedbackError',
912
913
        );
        if (!$USER->is_logged_in()) {
914
915
            $form['spam'] = array(
                'secret'       => get_config('formsecret'),
916
                'mintime'      => 1,
Aaron Wells's avatar
Aaron Wells committed
917
918
919
                // Not hashing the "ispublic" element, so that we can show/hide it with JS when
                // doing threaded comments.
                'hash'         => array('authorname', 'message', 'message', 'submit'),
920
            );
921
922
923
924
925
926
927
928
929
930
931
            $form['elements']['authorname'] = array(
                'type'  => 'text',
                'title' => get_string('name'),
                'rules' => array(
                    'required' => true,
                ),
            );
        }
        $form['elements']['message'] = array(
            'type'  => 'wysiwyg',
            'title' => get_string('message'),
932
            'class' => 'hide-label',
933
934
            'rows'  => 5,
            'cols'  => 80,
935
            'rules' => array('maxlength' => 8192),
936
        );
937
938
        if (get_config_plugin('artefact', 'comment', 'commentratings')) {
            $form['elements']['rating'] = array(
939
                'type'  => 'radio',
940
                'title' => get_string('rating', 'artefact.comment'),
941
942
                'options' => array('1' => '', '2' => '', '3' => '', '4' => '', '5' => ''),
                'class' => 'star',
943
944
            );
        }
945
        $form['elements']['ispublic'] = array(
946
            'type'  => 'switchbox',
947
            'title' => get_string('makepublic', 'artefact.comment'),
948
            'defaultvalue' => !$defaultprivate,
949
        );
950
951
952
953
        if (get_config('licensemetadata')) {
            $form['elements']['license'] = license_form_el_basic(null);
            $form['elements']['licensing_advanced'] = license_form_el_advanced(null);
        }
954
        if ($moderate) {
955
956
957
958
959
960
            $form['elements']['ispublic']['description'] = get_string('approvalrequired', 'artefact.comment');
            $form['elements']['moderate'] = array(
                'type'  => 'hidden',
                'value' => true,
            );
        }
961
        if ($USER->is_logged_in()) {
962
963
964
965
            $form['elements']['attachments'] = array(
                'type'         => 'files',
                'title'        => get_string('attachfile', 'artefact.comment'),
                'defaultvalue' => array(),
966
                'maxfilesize'  => get_max_upload_size(false),
967
968
969
970
            );
        }
        $form['elements']['submit'] = array(
            'type'  => 'submitcancel',
971
            'class' => 'btn-default',
972
            'value' => array(get_string('Comment', 'artefact.comment'), get_string('cancel')),
973
        );
Aaron Wells's avatar
Aaron Wells committed
974
975
976
977
978
979
980
981
982
983
984
985
986
        // This is a placeholder where we can display the parent comment's text
        // And also the strings we display when we are forcing a reply to be public or private
        $snippet = smarty_core();
        $form['elements']['replytoview'] = array(
            'type' => 'html',
            'value' => $snippet->fetch('artefact:comment:replyplaceholder.tpl')
        );
        // This is a placeholder for the parent comment's ID. It'll be populated by Javascript if needed.
        $form['elements']['replyto'] = array(
            'type' => 'hidden',
            'dynamic' => 'true',
            'value' => null
        );
987
988
        return $form;
    }
Richard Mansfield's avatar
Richard Mansfield committed
989

990
991
992
    public static function make_public_form($id) {
        return array(
            'name'            => 'make_public',
993
994
            'renderer'        => 'div',
            'class'           => 'form-as-button',
995
996
997
            'elements'        => array(
                'comment'  => array('type' => 'hidden', 'value' => $id),
                'submit'   => array(
998
999
                    'type'  => 'button',
                    'usebuttontag' => true,
1000
                    'class' => 'btn-link btn-xs',
1001
                    'name'  => 'make_public_submit',
1002
                    'value' => '<span class="icon icon-lock text-default left"></span>' . get_string('makepublic', 'artefact.comment'),
1003
1004
1005
1006
1007
                ),
            ),
        );
    }

Richard Mansfield's avatar
Richard Mansfield committed
1008
    public static function delete_comment_form($id) {
1009
        global $THEME;
Richard Mansfield's avatar
Richard Mansfield committed
1010
1011
        return array(
            'name'     => 'delete_comment',
1012
            'renderer' => 'div',
1013
            'class' => 'form-as-button pull-left delete-comment btn-group-items',
Richard Mansfield's avatar
Richard Mansfield committed
1014
1015
1016
            'elements' => array(
                'comment' => array('type' => 'hidden', 'value' => $id),
                'submit'  => array(
1017
1018
                    'type'  => 'button',
                    'usebuttontag' => true,
1019
                    'class' => 'btn-default btn-sm',
Pat Kira's avatar
Pat Kira committed
1020
                    'value' => '<span class="icon icon-trash icon-lg text-danger"></span> <span class="sr-only">' . get_string('delete') . '</span>',
1021
                    'confirm' => get_string('reallydeletethiscomment', 'artefact.comment'),
1022
                    'name'  => 'delete_comment_submit',
Richard Mansfield's avatar
Richard Mansfield committed
1023
1024
1025
1026
                ),
            ),
        );
    }
1027
1028
1029
1030

    public function exportable() {
        return empty($this->deletedby);
    }
1031

1032
    public function get_view_url($viewid, $showcomment=true, $full=true) {
1033
        if ($artefact = $this->get('onartefact')) {
1034
            $url = 'artefact/artefact.php?view=' . $viewid . '&artefact=' . $artefact;
1035
1036
        }
        else {
1037
            $url = 'view/view.php?id=' . $viewid;
1038
1039
1040
1041
        }
        if ($showcomment) {
            $url .= '&showcomment=' . $this->get('id');
        }
1042
1043
1044
        if ($full) {
            $url = get_config('wwwroot') . $url;
        }
1045
1046
        return $url;
    }
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

    // Check whether the logged-in user can see a comment within the
    // context of a given view.  Does not check whether the user can
    // view the view.
    public function viewable_in($viewid) {
        global $USER;
        if ($this->get('deletedby')) {
            return false;
        }

        if ($USER->is_logged_in()) {
            if ($USER->can_view_artefact($this)) {
                return true;
            }
            if ($this->get('author') == $USER->get('id')) {
                return true;
            }
        }

        if ($this->get('private')) {
            return false;
        }

        if ($onview = $this->get('onview')) {
            return $onview == $viewid;
        }

        if ($onartefact = $this->get('onartefact')) {
            return artefact_in_view($onartefact, $viewid);
        }

        return false;
    }
1080
1081
1082
1083
1084
1085
1086
1087

    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
        $elements =  array(
            'commentratings' => array(
1088
                'type'  => 'switchbox',
1089
1090
1091
1092
1093
1094
1095
1096
                'title' => get_string('commentratings', 'artefact.comment'),
                'defaultvalue' => get_config_plugin('artefact', 'comment', 'commentratings'),
                'help'  => true,
            ),
        );
        return array(
            'name'     => 'commentconfig',
            'elements' => $elements,
1097
            'class'    => 'panel panel-body',
1098
            'renderer' => 'div'
1099
1100
1101
        );
    }

1102
    public static function save_config_options($form, $values) {
1103
1104
1105
1106
        foreach (array('commentratings') as $settingname) {
            set_config_plugin('artefact', 'comment', $settingname, $values[$settingname]);
        }
    }
1107
1108
1109
1110
1111
1112
1113

    /**
     * Fetch all users that are currently watching the view the comment is being added to,
     * in case of comment on artefact it will be the view the artefact sits on,
     * so we can use this array to email affected parties.
     *
     * @param   int    $posterid   If set, the poster's id is ignored from resulting array
1114
     * @return  array  $users      An array of user objects of the users that are watching this page or a page this artefact is on
1115
     */
1116
    public function get_watchlist_users($posterid = null) {
1117
1118
        $ontype = ($onview = $this->get('onview')) ? 'onview' : 'onartefact';
        $values = array();
1119
        $sql = "SELECT DISTINCT u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email FROM {usr} u";
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
        if ($ontype == 'onview') {
            $sql .= " JOIN {usr_watchlist_view} uwv ON uwv.view = ? AND uwv.usr = u.id";
            $values[] = $this->get($ontype);
        }
        else if ($ontype == 'onartefact') {
            $sql .= " JOIN {view_artefact} va ON va.artefact = ?
                      JOIN {usr_watchlist_view} uwv ON uwv.view = va.view AND uwv.usr = u.id";
            $values[] = $this->get($ontype);
        }
        if ($posterid) {
            $sql .= " WHERE u.id != ?";
            $values[] = $posterid;
        }

        $users = get_records_sql_assoc($sql, $values);
1135
1136
1137
1138
1139
1140
        if ($users) {
            return $users;
        }
        else {
            return array();
        }
1141
    }
1142
1143
}

1144
1145
1146
1147
1148
1149
1150
1151
/* To make private comments public, both the author and the owner must agree. */
function make_public_validate(Pieform $form, $values) {
    global $USER;
    $comment = new ArtefactTypeComment((int) $values['comment']);

    $author    = $comment->get('author');
    $owner     = $comment->get('owner');
    $requester = $USER->get('id');
1152
    $group     = $comment->get('group');
1153

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
    if (!$owner && !$group) {
        $form->set_error('comment', get_string('makepublicnotallowed', 'artefact.comment'));
    }
    else if (!$owner && $group) {
        if ($requester) {
            $allowed = false;
            // check to see if the requester is a group admin
            $group_admins = group_get_admin_ids($group);
            if (array_search($requester,$group_admins) === false) {
                $form->set_error('comment', get_string('makepublicnotallowed', 'artefact.comment'));
            }
        }
        else {
            $form->set_error('comment', get_string('makepublicnotallowed', 'artefact.comment'));
        }
    }
    else if (!$owner || !$requester || ($requester != $owner && $requester != $author)) {
1171
1172
1173
1174
1175
1176
1177
1178
1179
        $form->set_error('comment', get_string('makepublicnotallowed', 'artefact.comment'));
    }
}

function make_public_submit(Pieform $form, $values) {
    global $SESSION, $USER, $view;

    $comment = new ArtefactTypeComment((int) $values['comment']);

1180
1181
    $relativeurl = $comment->get_view_url($view->get('id'), true, false);
    $url = get_config('wwwroot') . $relativeurl;
1182
1183
1184

    $author    = $comment->get('author');
    $owner     = $comment->get('owner');
1185
1186
1187
1188
1189
    $groupid   = $comment->get('group');
    $group_admins = array();
    if ($groupid) {
        $group_admins = group_get_admin_ids($groupid);
    }
1190
1191
1192
1193
    $requester = $USER->get('id');

    if (($author == $owner && $requester == $owner)
        || ($requester == $owner  && $comment->get('requestpublic') == 'author')
1194
        || (array_search($requester,$group_admins) !== false && $comment->get('requestpublic') == 'author')
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
        || ($requester == $author && $comment->get('requestpublic') == 'owner')) {
        $comment->set('private', 0);
        $comment->set('requestpublic', null);
        $comment->commit();
        $SESSION->add_ok_msg(get_string('commentmadepublic', 'artefact.comment'));
        redirect($url);
    }

    $subject = 'makepublicrequestsubject';
    if ($requester == $owner) {
        $comment->set('requestpublic', 'owner');
        $message = 'makepublicrequestbyownermessage';
        $arg = display_name($owner, $author);
        $userid = $author;
        $sessionmessage = get_string('makepublicrequestsent', 'artefact.comment', display_name($author));
    }
    else if ($requester == $author) {
        $comment->set('requestpublic', 'author');
        $message = 'makepublicrequestbyauthormessage';
        $arg = display_name($author, $owner);
        $userid = $owner;
        $sessionmessage = get_string('makepublicrequestsent', 'artefact.comment', display_name($owner));
    }
1218
1219
1220
1221
1222
1223
1224
    else if (array_search($requester,$group_admins) !== false) {
        $comment->set('requestpublic', 'owner');
        $message = 'makepublicrequestbyownermessage';
        $arg = display_name($requester, $author);
        $userid = $author;
        $sessionmessage = get_string('makepublicrequestsent', 'artefact.comment', display_name($author));
    }
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
    else {
        redirect($url); // Freak out?
    }

    db_begin();
    $comment->commit();

    $data = (object) array(
        'subject'   => false,
        'message'   => false,
        'strings'   => (object) array(
            'subject' => (object) array(
                'key'     => $subject,
                'section' => 'artefact.comment',
                'args'    => array(),
            ),
            'message' => (object) array(
                'key'     => $message,
                'section' => 'artefact.comment',
                'args'    => array(hsc($arg)),
            ),
1246
1247
1248
1249
            'urltext' => (object) array(
                'key'     => 'Comment',
                'section' => 'artefact.comment',