lib.php 31 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage artefact-comment
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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

29 30
require_once('activity.php');

Richard Mansfield's avatar
Richard Mansfield committed
31 32 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();
    }

    public static function postinst($prevversion) {
        if ($prevversion == 0) {
57
            set_config_plugin('artefact', 'comment', 'commenteditabletime', 10);
Richard Mansfield's avatar
Richard Mansfield committed
58 59 60 61 62
            foreach(ArtefactTypeComment::deleted_types() as $type) {
                insert_record('artefact_comment_deletedby', (object)array('name' => $type));
            }
        }
    }
Richard Mansfield's avatar
Richard Mansfield committed
63 64 65 66 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

    public static function view_export_extra_artefacts($viewids) {
        $artefacts = array();
        if (!$artefacts = get_column_sql("
            SELECT artefact
            FROM {artefact_comment_comment}
            WHERE onview IN (" . join(',', $viewids) . ')', array())) {
            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}
            WHERE onartefact IN (" . join(',', $artefactids) . ')', array())) {
            return array();
        }
        if ($attachments = get_column_sql('
            SELECT attachment
            FROM {artefact_attachment}
            WHERE artefact IN (' . join(',', $artefacts). ')')) {
            $artefacts = array_merge($artefacts, $attachments);
        }
        return $artefacts;
    }
Richard Mansfield's avatar
Richard Mansfield committed
96 97 98 99 100 101 102 103
}

class ArtefactTypeComment extends ArtefactType {

    protected $onview;
    protected $onartefact;
    protected $private;
    protected $deletedby;
104
    protected $requestpublic;
Richard Mansfield's avatar
Richard Mansfield committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    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;
                }
            }
        }
    }

    public function commit() {
        if (empty($this->dirty)) {
            return;
        }

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

        db_begin();

        parent::commit();

        $data = (object)array(
130 131 132 133 134 135
            'artefact'      => $this->get('id'),
            'onview'        => $this->get('onview'),
            'onartefact'    => $this->get('onartefact'),
            'private'       => $this->get('private'),
            'deletedby'     => $this->get('deletedby'),
            'requestpublic' => $this->get('requestpublic'),
Richard Mansfield's avatar
Richard Mansfield committed
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
        );

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

        db_commit();
        $this->dirty = false;
    }

    public static function is_singular() {
        return false;
    }

    public static function get_icon($options=null) {
        global $THEME;
        return $THEME->get_url('images/comment.gif', false, 'artefact/comment');
    }

    public function delete() {
        if (empty($this->id)) {
            return;
        }
        db_begin();
        $this->detach();
        parent::delete();
        db_commit();
    }

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    public static function bulk_delete($artefactids) {
        if (empty($artefactids)) {
            return;
        }

        $idstr = join(',', $artefactids);

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

Richard Mansfield's avatar
Richard Mansfield committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199
    public static function get_links($id) {
        return array(
            '_default' => get_config('wwwroot') . 'artefact/comment/view.php?id=' . $this->get('id'),
        );
    }

    public function can_have_attachments() {
        return true;
    }

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

200
    public static function get_comments($limit=10, $offset=0, $showcomment=null, &$view=null, &$artefact=null) {
201 202 203 204 205
        global $USER;
        $userid = $USER->get('id');
        $viewid = $view->get('id');
        if (!empty($artefact)) {
            $canedit = $USER->can_edit_artefact($artefact);
206 207
            $owner = $artefact->get('owner');
            $isowner = $userid && $userid == $owner;
208 209 210 211
            $artefactid = $artefact->get('id');
        }
        else {
            $canedit = $USER->can_edit_view($view);
212 213
            $owner = $view->get('owner');
            $isowner = $userid && $userid == $owner;
214 215
            $artefactid = null;
        }
216 217 218 219 220 221 222

        $result = (object) array(
            'limit'    => $limit,
            'offset'   => $offset,
            'view'     => $viewid,
            'artefact' => $artefactid,
            'canedit'  => $canedit,
223
            'owner'    => $owner,
224
            'isowner'  => $isowner,
225 226 227
            'data'     => array(),
        );

228 229
        if (!empty($artefactid)) {
            $where = 'c.onartefact = ' . $artefactid;
230 231
        }
        else {
232
            $where = 'c.onview = ' . $viewid;
233 234 235 236 237 238 239 240 241 242
        }
        if (!$canedit) {
            $where .= ' AND (c.private = 0 OR a.author = ' . (int) $userid . ')';
        }

        $result->count = count_records_sql('
            SELECT COUNT(*)
            FROM {artefact} a JOIN {artefact_comment_comment} c ON a.id = c.artefact
            WHERE ' . $where);

243
        if ($result->count > 0) {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            if ($showcomment == 'last') { // 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.
                // Fetch everything up to $showcomment to get its rank
                // This will get ugly if there are 1000s of comments
                $ids = get_column_sql('
                SELECT a.id
                FROM {artefact} a JOIN {artefact_comment_comment} c ON a.id = c.artefact
                WHERE ' . $where . ' AND a.id <= ?
                ORDER BY a.ctime', array($showcomment));
                $last = end($ids);
                if ($last == $showcomment) {
                    $rank = key($ids);
                    $result->forceoffset = $offset = ((ceil($rank / $limit) - 1) * $limit);
261
                    $result->showcomment = $showcomment;
262
                }
263
            }
264

265 266
            $comments = get_records_sql_assoc('
                SELECT
267 268
                    a.id, a.author, a.authorname, a.ctime, a.description,
                    c.private, c.deletedby, c.requestpublic,
269 270 271 272 273
                    u.username, u.firstname, u.lastname, u.preferredname, u.email, u.staff, u.admin,
                    u.deleted, u.profileicon
                FROM {artefact} a
                    INNER JOIN {artefact_comment_comment} c ON a.id = c.artefact
                    LEFT JOIN {usr} u ON a.author = u.id
274 275
                WHERE ' . $where . '
                ORDER BY a.ctime', array(), $offset, $limit);
276

277
            $files = ArtefactType::attachments_from_id_list(array_keys($comments));
278

279 280 281 282 283
            if ($files) {
                safe_require('artefact', 'file');
                foreach ($files as &$file) {
                    $comments[$file->artefact]->attachments[] = $file;
                }
284 285
            }

286 287
            $result->data = array_values($comments);
        }
288

289
        self::build_html($result);
290 291 292
        return $result;
    }

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    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];
    }

311 312
    public static function deleted_messages() {
        return array(
313 314 315
            'author' => 'commentremovedbyauthor',
            'owner'  => 'commentremovedbyowner',
            'admin'  => 'commentremovedbyadmin',
Richard Mansfield's avatar
Richard Mansfield committed
316
        );
317 318 319 320 321
    }

    public static function build_html(&$data) {
        global $USER;
        $candelete = $data->canedit || $USER->get('admin');
322 323 324 325
        $deletedmessage = array();
        foreach (self::deleted_messages() as $k => $v) {
            $deletedmessage[$k] = get_string($v, 'artefact.comment');
        }
326
        $authors = array();
327 328
        $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
329
        foreach ($data->data as &$item) {
330 331 332
            $item->ts = strtotime($item->ctime);
            $item->date = format_date($item->ts, 'strftimedatetime');
            $item->isauthor = $item->author && $item->author == $USER->get('id');
333
            if (!empty($item->attachments)) {
Richard Mansfield's avatar
Richard Mansfield committed
334
                if ($data->isowner) {
335 336
                    $item->attachmessage = get_string(
                        'feedbackattachmessage',
337 338
                        'artefact.comment',
                        get_string('feedbackattachdirname', 'artefact.comment')
339 340 341 342 343 344
                    );
                }
                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
345 346
                }
            }
347
            if ($item->private) {
348
                $item->pubmessage = get_string('thiscommentisprivate', 'artefact.comment');
349
            }
350

351 352 353 354
            if (isset($data->showcomment) && $data->showcomment == $item->id) {
                $item->highlight = 1;
            }

Richard Mansfield's avatar
Richard Mansfield committed
355 356 357
            if ($item->deletedby) {
                $item->deletedmessage = $deletedmessage[$item->deletedby];
            }
358
            else if ($candelete || $item->isauthor) {
Richard Mansfield's avatar
Richard Mansfield committed
359 360
                $item->deleteform = pieform(self::delete_comment_form($item->id));
            }
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

            // Comment authors can edit recent comments if they're private or if no one has replied yet.
            if (!$item->deletedby && $item->isauthor
                && ($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)) {
                if (empty($item->requestpublic)
                    || $item->isauthor && $item->requestpublic == 'owner'
                    || $data->isowner && $item->requestpublic == 'author') {
                    $item->makepublicform = pieform(self::make_public_form($item->id));
                }
                else if ($item->isauthor && $item->requestpublic == 'author'
                         || $data->isowner && $item->requestpublic == 'owner') {
                    $item->makepublicrequested = 1;
                }
            }

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
            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,
                    );
                }
            }
Richard Mansfield's avatar
Richard Mansfield committed
402
        }
403

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

Richard Mansfield's avatar
Richard Mansfield committed
407 408 409 410 411 412 413
        if (!empty($data->artefact)) {
            $data->baseurl = get_config('wwwroot') . 'view/artefact.php?view=' . $data->view . '&artefact=' . $data->artefact;
            $extradata['artefact'] = $data->artefact;
        }
        else {
            $data->baseurl = get_config('wwwroot') . 'view/view.php?id=' . $data->view;
        }
414

Richard Mansfield's avatar
Richard Mansfield committed
415 416 417
        $smarty = smarty_core();
        $smarty->assign_by_ref('data', $data->data);
        $smarty->assign('canedit', $data->canedit);
418
        $smarty->assign('viewid', $data->view);
Richard Mansfield's avatar
Richard Mansfield committed
419
        $smarty->assign('baseurl', $data->baseurl);
420
        $data->tablerows = $smarty->fetch('artefact:comment:commentlist.tpl');
Richard Mansfield's avatar
Richard Mansfield committed
421 422 423 424 425 426 427 428 429
        $pagination = build_pagination(array(
            'id' => 'feedback_pagination',
            'class' => 'center',
            'url' => $data->baseurl,
            'jsonscript' => $data->jsonscript,
            'datatable' => 'feedbacktable',
            'count' => $data->count,
            'limit' => $data->limit,
            'offset' => $data->offset,
430
            'forceoffset' => isset($data->forceoffset) ? $data->forceoffset : null,
431 432
            'resultcounttextsingular' => get_string('comment', 'artefact.comment'),
            'resultcounttextplural' => get_string('comments', 'artefact.comment'),
Richard Mansfield's avatar
Richard Mansfield committed
433 434 435 436 437 438
            'extradata' => $extradata,
        ));
        $data->pagination = $pagination['html'];
        $data->pagination_js = $pagination['javascript'];
    }

Richard Mansfield's avatar
Richard Mansfield committed
439 440 441 442
    public function render_self() {
        return clean_html($this->get('description'));
    }

443
    public static function add_comment_form($defaultprivate=false) {
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        global $USER;
        $form = array(
            'name'            => 'add_feedback_form',
            'method'          => 'post',
            'class'           => 'js-hidden',
            'plugintype'      => 'artefact',
            'pluginname'      => 'comment',
            'jsform'          => true,
            'autofocus'       => false,
            'elements'        => array(),
            'jssuccesscallback' => 'addFeedbackSuccess',
        );
        if (!$USER->is_logged_in()) {
            $form['elements']['authorname'] = array(
                'type'  => 'text',
                'title' => get_string('name'),
                'rules' => array(
                    'required' => true,
                ),
            );
        }
        $form['elements']['message'] = array(
            'type'  => 'wysiwyg',
            'title' => get_string('message'),
            'rows'  => 5,
            'cols'  => 80,
        );
        $form['elements']['ispublic'] = array(
            'type'  => 'checkbox',
            'title' => get_string('makepublic', 'artefact.comment'),
474
            'defaultvalue' => !$defaultprivate,
475
        );
476
        if ($USER->is_logged_in()) {
477 478 479 480
            $form['elements']['attachments'] = array(
                'type'         => 'files',
                'title'        => get_string('attachfile', 'artefact.comment'),
                'defaultvalue' => array(),
481 482
            );
        }
483 484 485 486 487 488 489 490
        if (!$USER->is_logged_in()) {
            $form['elements']['captcha'] = array(
                'type'  => 'captcha',
                'title' => get_string('captchatitle'),
                'description' => get_string('captchadescription'),
                'rules' => array('required' => true)
            );
        }
491 492
        $form['elements']['submit'] = array(
            'type'  => 'submitcancel',
493
            'value' => array(get_string('placefeedback', 'artefact.comment'), get_string('cancel')),
494 495 496
        );
        return $form;
    }
Richard Mansfield's avatar
Richard Mansfield committed
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    public static function make_public_form($id) {
        return array(
            'name'            => 'make_public',
            'renderer'        => 'oneline',
            'class'           => 'makeprivate',
            // 'jsform'          => true,
            'elements'        => array(
                'comment'  => array('type' => 'hidden', 'value' => $id),
                'submit'   => array(
                    'type' => 'submit',
                    'name' => 'make_public_submit',
                    'value' => get_string('makepublic', 'artefact.comment'),
                ),
            ),
        );
    }

Richard Mansfield's avatar
Richard Mansfield committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    public static function delete_comment_form($id) {
        return array(
            'name'     => 'delete_comment',
            'renderer' => 'oneline',
            'class'    => 'makeprivate',
            'elements' => array(
                'comment' => array('type' => 'hidden', 'value' => $id),
                'submit'  => array(
                    'type'  => 'submit',
                    'class' => 'delete',
                    'name'  => 'delete_comment_submit',
                    'value' => get_string('delete'),
                ),
            ),
        );
    }
531 532
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
/* 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');

    if (!$author || !$owner || !$requester || ($requester != $owner && $requester != $author)) {
        $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']);

    $viewid = $view->get('id');
    if ($artefact = $comment->get('onartefact')) {
        $url = get_config('wwwroot') . 'view/artefact.php?view=' . $viewid . '&artefact=' . $artefact;
    }
    else {
        $url = get_config('wwwroot') . 'view/view.php?id=' . $viewid;
    }
559
    $url .= '&showcomment=' . $comment->get('id');
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 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

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

    if (($author == $owner && $requester == $owner)
        || ($requester == $owner  && $comment->get('requestpublic') == 'author')
        || ($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));
    }
    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)),
            ),
        ),
        'users'     => array($userid),
        'url'       => $url,
    );
    activity_occurred('maharamessage', $data);
    db_commit();

    $SESSION->add_ok_msg($sessionmessage);
    redirect($url);
}

Richard Mansfield's avatar
Richard Mansfield committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
function delete_comment_submit(Pieform $form, $values) {
    global $SESSION, $USER, $view;

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

    if ($USER->get('id') == $comment->get('author')) {
        $deletedby = 'author';
    }
    else if ($USER->can_edit_view($view)) {
        $deletedby = 'owner';
    }
    else if ($USER->get('admin')) {
        $deletedby = 'admin';
    }

637 638 639 640 641 642 643 644
    $viewid = $view->get('id');
    if ($artefact = $comment->get('onartefact')) {
        $url = get_config('wwwroot') . 'view/artefact.php?view=' . $viewid . '&artefact=' . $artefact;
    }
    else {
        $url = get_config('wwwroot') . 'view/view.php?id=' . $viewid;
    }

645 646
    db_begin();

Richard Mansfield's avatar
Richard Mansfield committed
647 648 649
    $comment->set('deletedby', $deletedby);
    $comment->commit();

650 651
    if ($deletedby != 'author') {
        // Notify author
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
        if ($artefact) {
            $title = get_field('artefact', 'title', 'id', $artefact);
        }
        else {
            $title = get_field('view', 'title', 'id', $comment->get('onview'));
        }
        $title = hsc($title);
        $data = (object) array(
            'subject'   => false,
            'message'   => false,
            'strings'   => (object) array(
                'subject' => (object) array(
                    'key'     => 'commentdeletednotificationsubject',
                    'section' => 'artefact.comment',
                    'args'    => array($title),
                ),
                'message' => (object) array(
                    'key'     => 'commentdeletedauthornotification',
                    'section' => 'artefact.comment',
                    'args'    => array($title, html2text($comment->get('description'))),
                ),
            ),
            'users'     => array($comment->get('author')),
            'url'       => $url,
        );
        activity_occurred('maharamessage', $data);
678 679 680 681 682 683 684 685 686 687 688 689
    }
    if ($deletedby != 'owner' && $comment->get('owner') != $USER->get('id')) {
        // Notify owner
        $data = (object) array(
            'commentid' => $comment->get('id'),
            'viewid'    => $view->get('id'),
        );
        activity_occurred('feedback', $data, 'artefact', 'comment');
    }

    db_commit();

Richard Mansfield's avatar
Richard Mansfield committed
690
    $SESSION->add_ok_msg(get_string('commentremoved', 'artefact.comment'));
691
    redirect($url);
Richard Mansfield's avatar
Richard Mansfield committed
692 693
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
function add_feedback_form_submit(Pieform $form, $values) {
    global $view, $artefact, $USER;
    $data = (object) array(
        'title'       => get_string('Comment', 'artefact.comment'),
        'description' => $values['message'],
        'private'     => 1 - (int) $values['ispublic'],
    );

    if ($artefact) {
        $data->onartefact  = $artefact->get('id');
        $data->owner       = $artefact->get('owner');
        $data->group       = $artefact->get('group');
        $data->institution = $artefact->get('institution');
    }
    else {
        $data->onview      = $view->get('id');
        $data->owner       = $view->get('owner');
        $data->group       = $view->get('group');
        $data->institution = $view->get('institution');
    }

    if ($author = $USER->get('id')) {
        $data->author = $author;
    }
    else {
        $data->authorname = $values['authorname'];
    }

    db_begin();

    $comment = new ArtefactTypeComment(0, $data);
    $comment->commit();

727 728
    if (!empty($values['attachments']) && is_array($values['attachments']) && !empty($data->author)) {

729 730 731
        require_once(get_config('libroot') . 'uploadmanager.php');
        safe_require('artefact', 'file');

732 733
        $ownerlang = empty($data->owner) ? get_config('lang') : get_user_language($data->owner);
        $folderid = ArtefactTypeFolder::get_folder_id(
734 735
            get_string_from_language($ownerlang, 'feedbackattachdirname', 'artefact.comment'),
            get_string_from_language($ownerlang, 'feedbackattachdirdesc', 'artefact.comment'),
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
            null, true, $data->owner, $data->group, $data->institution
        );

        $attachment = (object) array(
            'owner'         => $data->owner,
            'group'         => $data->group,
            'institution'   => $data->institution,
            'author'        => $data->author,
            'allowcomments' => 0,
            'parent'        => $folderid,
            'description'   => get_string_from_language(
                $ownerlang,
                'feedbackonviewbyuser',
                'artefact.comment',
                $view->get('title'),
                display_name($USER)
            ),
        );

        foreach ($values['attachments'] as $filesindex) {
756

757
            $um = new upload_manager($filesindex);
758 759 760 761
            if ($error = $um->preprocess_file()) {
                throw new UploadException($error);
            }

762 763 764 765 766 767
            $attachment->title = ArtefactTypeFileBase::get_new_file_title(
                $um->file['name'],
                $folderid,
                $data->owner,
                $data->group,
                $data->institution
768
            );
769 770 771
            $attachment->size         = $um->file['size'];
            $attachment->filetype     = $um->file['type'];
            $attachment->oldextension = $um->original_filename_extension();
772 773

            try {
774
                $fileid = ArtefactTypeFile::save_uploaded_file($filesindex, $attachment);
775 776
            }
            catch (QuotaExceededException $e) {}
777 778

            $comment->attach($fileid);
779 780 781 782
        }
    }

    require_once('activity.php');
783 784 785 786
    $data = (object) array(
        'commentid' => $comment->get('id'),
        'viewid'    => $view->get('id')
    );
787
    activity_occurred('feedback', $data, 'artefact', 'comment');
788 789 790 791 792

    db_commit();

    if ($artefact) {
        $goto = get_config('wwwroot') . 'view/artefact.php?artefact=' . $artefact->get('id') . '&view='.$view->get('id');
793
        $newlist = ArtefactTypeComment::get_comments(10, 0, 'last', $view, $artefact);
794 795 796
    }
    else {
        $goto = get_config('wwwroot') . 'view/view.php?id='.$view->get('id');
797
        $newlist = ArtefactTypeComment::get_comments(10, 0, 'last', $view);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    }
    $form->reply(PIEFORM_OK, array(
        'message' => get_string('feedbacksubmitted', 'artefact.comment'),
        'goto' => $goto,
        'data' => $newlist,
    ));
}

function add_feedback_form_cancel_submit(Pieform $form) {
    global $view;
    $form->reply(PIEFORM_OK, array(
        'goto' => '/view/view.php?id=' . $view->get('id'),
    ));
}

813 814
class ActivityTypeArtefactCommentFeedback extends ActivityTypePlugin {

815 816
    protected $viewid;
    protected $commentid;
817 818 819

    /**
     * @param array $data Parameters:
820 821
     *                    - viewid (int)
     *                    - commentid (int)
822 823 824 825
     */
    public function __construct($data, $cron=false) {
        parent::__construct($data, $cron);

826 827 828
        $comment = new ArtefactTypeComment($this->commentid);

        if ($onartefact = $comment->get('onartefact')) { // feedback on artefact
829 830
            $userid = null;
            require_once(get_config('docroot') . 'artefact/lib.php');
831 832 833
            $artefactinstance = artefact_instance_from_id($onartefact);
            if ($artefactinstance->feedback_notify_owner()) {
                $userid = $artefactinstance->get('owner');
834 835 836
            }
            if (empty($this->url)) {
                $this->url = get_config('wwwroot') . 'view/artefact.php?artefact='
837
                    . $onartefact . '&view=' . $this->viewid;
838 839 840
            }
        }
        else { // feedback on view.
841 842 843
            $onview = $comment->get('onview');
            if (!$viewrecord = get_record('view', 'id', $onview)) {
                throw new ViewNotFoundException(get_string('viewnotfound', 'error', $onview));
844
            }
845
            $userid = $viewrecord->owner;
846
            if (empty($this->url)) {
847
                $this->url = get_config('wwwroot') . 'view/view.php?id=' . $onview;
848 849
            }
        }
850 851
        if (empty($userid)) {
            return;
852 853
        }

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
        $this->users = activity_get_users($this->get_id(), array($userid));

        $deletedby = $comment->get('deletedby');
        $subjectkey = $deletedby ? 'commentdeletednotificationsubject' : 'newfeedbacknotificationsubject';
        $title = $onartefact ? $artefactinstance->get('title') : $viewrecord->title;

        $this->strings = (object) array(
            'subject' => (object) array(
                'key'     => $subjectkey,
                'section' => 'artefact.comment',
                'args'    => array($title),
            ),
        );

        if ($deletedby) {
            $deletedmessage = ArtefactTypeComment::deleted_messages();
            $this->strings->deletedmessage = (object) array(
                'key' => $deletedmessage[$deletedby],
                'section' => 'artefact.comment',
                'args' => array(),
            );
875
        }
876 877 878 879 880 881
        $this->message = html2text($comment->get('description'));
    }

    public function get_message($user) {
        if (!empty($this->strings->deletedmessage)) {
            return $this->get_string_for_user($user, 'deletedmessage') . ":\n" . $this->message;
882
        }
883
        return $this->message;
884 885 886 887 888 889 890 891 892 893 894
    }

    public function get_plugintype(){
        return 'artefact';
    }

    public function get_pluginname(){
        return 'comment';
    }

    public function get_required_parameters() {
895
        return array('commentid', 'viewid');
896 897 898
    }
}

Richard Mansfield's avatar
Richard Mansfield committed
899
?>