MDL-18044 - updating missed scalestip string to scalestip2
[moodle.git] / comment / lib.php
CommitLineData
1bcb7eb5 1<?php
2
36051c9e
DC
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
1bcb7eb5 18/**
36051c9e 19 * Comment is helper class to add/delete comments anywhere in moodle
1bcb7eb5 20 *
36051c9e
DC
21 * @package comment
22 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
1bcb7eb5 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
1bcb7eb5 25
36051c9e 26class comment {
1bcb7eb5 27 /**
36051c9e 28 * @var integer
1bcb7eb5 29 */
36051c9e
DC
30 private $page;
31 /**
32 * there may be several comment box in one page
33 * so we need a client_id to recognize them
34 * @var integer
35 */
36 private $cid;
37 private $contextid;
38 /**
39 * commentarea is used to specify different
40 * parts shared the same itemid
41 * @var string
42 */
43 private $commentarea;
44 /**
45 * itemid is used to associate with commenting content
46 * @var integer
47 */
48 private $itemid;
49
50 /**
51 * this html snippet will be used as a template
52 * to build comment content
53 * @var string
54 */
55 private $template;
56 private $context;
57 private $course;
58 /**
59 * course module object
60 * @var string
61 */
62 private $cm;
63 private $plugintype;
64 private $pluginname;
65 private $viewcap;
66 private $postcap;
67 /**
68 * to tell comments api where it is used
69 * @var string
70 */
71 private $env;
72 /**
73 * to costomize link text
74 * @var string
75 */
76 private $linktext;
77 private static $nonjs = false;
78 // will be used by non-js comments UI
79 private static $comment_itemid = null;
80 private static $comment_context = null;
81 private static $comment_area = null;
a5306210 82 private static $comment_page = null;
36051c9e
DC
83 /**
84 * Construct function of comment class, initilize
85 * class members
86 * @param object $options
87 */
88 public function __construct($options) {
89 global $CFG, $DB;
76951100 90
c794595d
DC
91 if (empty($CFG->commentsperpage)) {
92 $CFG->commentsperpage = 15;
93 }
94
36051c9e
DC
95 $this->viewcap = false;
96 $this->postcap = false;
76951100 97
36051c9e
DC
98 if (!empty($options->client_id)) {
99 $this->cid = $options->client_id;
100 } else {
101 $this->cid = uniqid();
102 }
76951100 103
36051c9e
DC
104 if (!empty($options->context)) {
105 $this->context = $options->context;
106 $this->contextid = $this->context->id;
107 } else if(!empty($options->contextid)) {
108 $this->contextid = $options->contextid;
109 $this->context = get_context_instance_by_id($this->contextid);
110 } else {
111 print_error('invalidcontext');
112 }
76951100 113
36051c9e
DC
114 if (!empty($options->area)) {
115 $this->commentarea = $options->area;
116 }
76951100 117
36051c9e
DC
118 if (!empty($options->itemid)) {
119 $this->itemid = $options->itemid;
120 }
76951100 121
36051c9e
DC
122 if (!empty($options->env)) {
123 $this->env = $options->env;
1bcb7eb5 124 } else {
08a22be6 125 $this->env = '';
1bcb7eb5 126 }
1bcb7eb5 127
36051c9e
DC
128 if (!empty($options->linktext)) {
129 $this->linktext = $options->linktext;
130 } else {
131 $this->linktext = get_string('comments');
132 }
133
05fe3ab5 134 $this->setup_plugin();
36051c9e
DC
135
136 $this->options = new stdclass;
137 $this->options->context = $this->context;
138 $this->options->commentarea = $this->commentarea;
139 $this->options->itemid = $this->itemid;
140
141 // load template
142 $this->template = <<<EOD
143<div class="comment-userpicture">___picture___</div>
144<div class="comment-content">
145 ___name___ - <span>___time___</span>
146 <div>___content___</div>
147</div>
148EOD;
149 if (!empty($this->plugintype)) {
150 $this->template = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'template', $this->options, $this->template);
151 }
152
153 // setting post and view permissions
05fe3ab5 154 $this->check_permissions();
36051c9e
DC
155
156 // setup course
157 if (!empty($options->course)) {
158 $this->course = $options->course;
159 } else if (!empty($options->courseid)) {
160 $courseid = $options->courseid;
05fe3ab5 161 $this->setup_course($courseid);
36051c9e
DC
162 } else {
163 $courseid = SITEID;
05fe3ab5 164 $this->setup_course($courseid);
36051c9e 165 }
76951100 166
36051c9e
DC
167 if (!empty($options->showcount)) {
168 $count = $this->count();
169 if (empty($count)) {
170 $this->count = '';
171 } else {
172 $this->count = '('.$count.')';
1bcb7eb5 173 }
36051c9e
DC
174 } else {
175 $this->count = '';
1bcb7eb5 176 }
177
36051c9e
DC
178 unset($options);
179 }
180
181 /**
182 * Receive nonjs comment parameters
183 */
184 public static function init() {
185 global $PAGE, $CFG;
186 // setup variables for non-js interface
187 self::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHA);
188 self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT);
189 self::$comment_context = optional_param('comment_context', '', PARAM_INT);
a5306210 190 self::$comment_page = optional_param('comment_page', '', PARAM_INT);
36051c9e
DC
191 self::$comment_area = optional_param('comment_area', '', PARAM_ALPHAEXT);
192
193 $PAGE->requires->string_for_js('addcomment', 'moodle');
194 $PAGE->requires->string_for_js('deletecomment', 'moodle');
195 $PAGE->requires->string_for_js('comments', 'moodle');
1bcb7eb5 196 }
197
05fe3ab5 198 private function setup_course($courseid) {
1d51d1e0 199 global $PAGE, $DB;
1bcb7eb5 200 if (!empty($this->course)) {
201 // already set, stop
202 return;
203 }
1d51d1e0
DC
204 if ($courseid == $PAGE->course->id) {
205 $this->course = $PAGE->course;
1bcb7eb5 206 } else if (!$this->course = $DB->get_record('course', array('id'=>$courseid))) {
207 $this->course = null;
208 }
209 }
210
36051c9e
DC
211 /**
212 * Setting module info
213 *
214 */
05fe3ab5 215 private function setup_plugin() {
1bcb7eb5 216 global $DB;
36051c9e
DC
217 // blog needs to set env as "blog"
218 if ($this->env == 'blog') {
219 $this->plugintype = 'moodle';
220 $this->pluginname = 'blog';
221 }
222 // tag page needs to set env as "tag"
223 if ($this->env == 'tag') {
224 $this->plugintype = 'moodle';
225 $this->pluginname = 'tag';
226 }
1bcb7eb5 227 if ($this->context->contextlevel == CONTEXT_BLOCK) {
228 if ($block = $DB->get_record('block_instances', array('id'=>$this->context->instanceid))) {
229 $this->plugintype = 'block';
230 $this->pluginname = $block->blockname;
231 }
232 }
233 if ($this->context->contextlevel == CONTEXT_MODULE) {
234 $this->plugintype = 'mod';
235 $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
05fe3ab5 236 $this->setup_course($this->cm->course);
1bcb7eb5 237 $this->modinfo = get_fast_modinfo($this->course);
238 $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
239 }
240 }
241
242 /**
36051c9e
DC
243 * check posting comments permission
244 * It will check based on user roles and ask modules
245 * If you need to check permission by modules, a
246 * function named $pluginname_check_comment_post must be implemented
1bcb7eb5 247 */
05fe3ab5 248 private function check_permissions() {
36051c9e
DC
249 global $CFG;
250 $this->postcap = has_capability('moodle/comment:post', $this->context);
251 $this->viewcap = has_capability('moodle/comment:view', $this->context);
252 if (!empty($this->plugintype)) {
253 $permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', $this->options, array('post'=>true, 'view'=>true));
254 $this->postcap = $this->postcap && $permissions['post'];
255 $this->viewcap = $this->viewcap && $permissions['view'];
6f5e0852 256 }
1bcb7eb5 257 }
258
259 /**
36051c9e
DC
260 * Prepare comment code in html
261 * @param boolean $return
262 * @return mixed
1bcb7eb5 263 */
36051c9e 264 public function output($return = true) {
4b201bc6
DC
265 global $PAGE, $OUTPUT;
266 static $template_printed;
36051c9e 267
5dce9d1b 268 $this->link = $PAGE->url;
36051c9e
DC
269 $murl = new moodle_url($this->link);
270 $murl->remove_params('nonjscomment');
271 $murl->param('nonjscomment', 'true');
272 $murl->param('comment_itemid', $this->options->itemid);
273 $murl->param('comment_context', $this->options->context->id);
274 $murl->param('comment_area', $this->options->commentarea);
a5306210 275 $murl->remove_params('comment_page');
36051c9e 276 $this->link = $murl->out();
76951100 277
36051c9e
DC
278 $options = new stdclass;
279 $options->client_id = $this->cid;
280 $options->commentarea = $this->commentarea;
281 $options->itemid = $this->itemid;
282 $options->page = 0;
283 $options->courseid = $this->course->id;
284 $options->contextid = $this->contextid;
76951100 285 $options->env = $this->env;
36051c9e
DC
286 if ($this->env == 'block_comments') {
287 $options->autostart = true;
288 $options->notoggle = true;
289 }
290
291 $PAGE->requires->js_init_call('M.core_comment.init', array($options), true);
292
293 if (!empty(self::$nonjs)) {
76951100 294 // return non js comments interface
a5306210 295 return $this->print_comments(self::$comment_page, $return, true);
36051c9e 296 }
76951100 297
36051c9e
DC
298 $strsubmit = get_string('submit');
299 $strcancel = get_string('cancel');
300 $sesskey = sesskey();
4b201bc6 301 $html = '';
36051c9e 302 // print html template
76951100 303 // Javascript will use the template to render new comments
4b201bc6
DC
304 if (empty($template_printed) && !empty($this->viewcap)) {
305 $html .= '<div style="display:none" id="cmt-tmpl">' . $this->template . '</div>';
306 $template_printed = true;
36051c9e
DC
307 }
308
309 if (!empty($this->viewcap)) {
310 // print commenting icon and tooltip
07eebf20 311 $icon = $OUTPUT->pix_url('t/collapsed');
4b201bc6 312 $html .= <<<EOD
b1d124d1 313<div class="mdl-left">
36051c9e 314<a id="comment-link-{$this->cid}" href="{$this->link}">
07eebf20 315 <img id="comment-img-{$this->cid}" src="$icon" alt="{$this->linktext}" title="{$this->linktext}" />
76951100 316 <span id="comment-link-text-{$this->cid}">{$this->linktext} {$this->count}</span>
36051c9e
DC
317</a>
318<div id="comment-ctrl-{$this->cid}" class="comment-ctrl">
319 <ul id="comment-list-{$this->cid}" class="comment-list">
76951100
DC
320EOD;
321 // in comments block, we print comments list right away
322 if ($this->env == 'block_comments') {
323 $html .= $this->print_comments(0, true, false);
3e34183a
DC
324 $html .= '</ul>';
325 $html .= $this->get_pagination(0);
326 } else {
327 $html .= <<<EOD
36051c9e
DC
328 </ul>
329 <div id="comment-pagination-{$this->cid}" class="comment-pagination"></div>
330EOD;
3e34183a 331 }
36051c9e
DC
332
333 // print posting textarea
334 if (!empty($this->postcap)) {
335 $html .= <<<EOD
336<div class='comment-area'>
337 <div class="bd">
76951100 338 <textarea name="content" rows="2" id="dlg-content-{$this->cid}"></textarea>
36051c9e
DC
339 </div>
340 <div class="fd" id="comment-action-{$this->cid}">
1150c1bd 341 <a href="#" id="comment-action-post-{$this->cid}"> {$strsubmit} </a>
36051c9e
DC
342EOD;
343 if ($this->env != 'block_comments') {
344 $html .= <<<EOD
345 <span> | </span>
1150c1bd 346 <a href="#" id="comment-action-cancel-{$this->cid}"> {$strcancel} </a>
36051c9e
DC
347EOD;
348 }
349
350 $html .= <<<EOD
351 </div>
352</div>
b1d124d1 353<div class="clearer"></div>
36051c9e
DC
354EOD;
355 }
356
357 $html .= <<<EOD
76951100 358</div><!-- end of comment-ctrl -->
36051c9e
DC
359</div>
360EOD;
361 } else {
362 $html = '';
363 }
364
365 if ($return) {
366 return $html;
367 } else {
368 echo $html;
1bcb7eb5 369 }
1bcb7eb5 370 }
36051c9e 371
1bcb7eb5 372 /**
36051c9e
DC
373 * Return matched comments
374 * @param int $page
375 * @return mixed
1bcb7eb5 376 */
36051c9e
DC
377 public function get_comments($page = '') {
378 global $DB, $CFG, $USER, $OUTPUT;
379 if (empty($this->viewcap)) {
380 return false;
381 }
36051c9e
DC
382 if (!is_numeric($page)) {
383 $page = 0;
384 }
385 $this->page = $page;
386 $params = array();
387 $start = $page * $CFG->commentsperpage;
388 $sql = "SELECT c.id, c.userid, c.content, c.format, c.timecreated, u.picture, u.imagealt, u.username, u.firstname, u.lastname
389 FROM {comments} c, {user} u WHERE u.id=c.userid AND c.contextid=? AND c.commentarea=? AND c.itemid=?
390 ORDER BY c.timecreated DESC";
391 $params[] = $this->contextid;
392 $params[] = $this->commentarea;
393 $params[] = $this->itemid;
394
395 $comments = array();
396 $candelete = has_capability('moodle/comment:delete', $this->context);
397 if ($records = $DB->get_records_sql($sql, $params, $start, $CFG->commentsperpage)) {
398 foreach ($records as &$c) {
399 $url = $CFG->httpswwwroot.'/user/view.php?id='.$c->userid.'&amp;course='.$this->course->id;
400 $c->username = '<a href="'.$url.'">'.fullname($c).'</a>';
401 $c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig'));
402 $user = new stdclass;
403 $user->id = $c->userid;
404 $user->picture = $c->picture;
405 $user->firstname = $c->firstname;
406 $user->lastname = $c->lastname;
407 $user->imagealt = $c->imagealt;
408 $c->content = format_text($c->content, $c->format);
409 $c->avatar = $OUTPUT->user_picture($user, array('size'=>18));
410 if (($USER->id == $c->userid) || !empty($candelete)) {
411 $c->delete = true;
412 }
413 $comments[] = $c;
414 }
415 }
416
417 if (!empty($this->plugintype)) {
418 // moodle module will filter comments
a0e68ffc 419 $comments = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'display', array($comments, $this->options));
36051c9e
DC
420 }
421
422 return $comments;
423 }
424
425 public function count() {
1bcb7eb5 426 global $DB;
36051c9e
DC
427 if ($count = $DB->count_records('comments', array('itemid'=>$this->itemid, 'commentarea'=>$this->commentarea, 'contextid'=>$this->context->id))) {
428 return $count;
429 } else {
430 return 0;
431 }
432 }
433
434 public function get_pagination($page = 0) {
435 global $DB, $CFG, $OUTPUT;
436 $count = $this->count();
437 $pages = (int)ceil($count/$CFG->commentsperpage);
438 if ($pages == 1 || $pages == 0) {
439 return '';
440 }
441 if (!empty(self::$nonjs)) {
442 // used in non-js interface
a5306210 443 return $OUTPUT->paging_bar($count, $page, $CFG->commentsperpage, $this->link, 'comment_page');
36051c9e
DC
444 } else {
445 // return ajax paging bar
446 $str = '';
3e34183a 447 $str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid.'">';
36051c9e 448 for ($p=0; $p<$pages; $p++) {
36051c9e 449 if ($p == $page) {
c794595d
DC
450 $class = 'curpage';
451 } else {
452 $class = 'pageno';
1bcb7eb5 453 }
1150c1bd 454 $str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid.'-'.$p.'">'.($p+1).'</a> ';
36051c9e
DC
455 }
456 $str .= '</div>';
457 }
458 return $str;
459 }
460
461 /**
462 * Add a new comment
463 * @param string $content
464 * @return mixed
465 */
466 public function add($content, $format = FORMAT_MOODLE) {
467 global $CFG, $DB, $USER, $OUTPUT;
468 if (empty($this->postcap)) {
469 throw new comment_exception('nopermissiontocomment');
470 }
471 $now = time();
472 $newcmt = new stdclass;
473 $newcmt->contextid = $this->contextid;
474 $newcmt->commentarea = $this->commentarea;
475 $newcmt->itemid = $this->itemid;
476 $newcmt->content = $content;
477 $newcmt->format = $format;
478 $newcmt->userid = $USER->id;
479 $newcmt->timecreated = $now;
480
481 if (!empty($this->plugintype)) {
482 // moodle module will check content
483 $ret = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'add', array(&$newcmt, $this->options), true);
484 if (!$ret) {
485 throw new comment_exception('modulererejectcomment');
1bcb7eb5 486 }
487 }
36051c9e
DC
488
489 $cmt_id = $DB->insert_record('comments', $newcmt);
490 if (!empty($cmt_id)) {
491 $newcmt->id = $cmt_id;
492 $newcmt->time = userdate($now, get_string('strftimerecent', 'langconfig'));
493 $newcmt->username = fullname($USER);
494 $newcmt->content = format_text($newcmt->content);
495 $newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16));
496 return $newcmt;
497 } else {
498 throw new comment_exception('dbupdatefailed');
499 }
500 }
501
502 /**
503 * delete by context, commentarea and itemid
504 *
505 */
506 public function delete_comments() {
507 global $DB;
b9a689e8 508 $DB->delete_records('comments', array(
36051c9e
DC
509 'contextid'=>$this->context->id,
510 'commentarea'=>$this->commentarea,
511 'itemid'=>$this->itemid)
512 );
b9a689e8 513 return true;
36051c9e
DC
514 }
515
516 /**
517 * Delete a comment
518 * @param int $commentid
519 * @return mixed
520 */
521 public function delete($commentid) {
522 global $DB, $USER;
523 $candelete = has_capability('moodle/comment:delete', $this->context);
524 if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) {
525 throw new comment_exception('dbupdatefailed');
526 }
527 if (!($USER->id == $comment->userid || !empty($candelete))) {
528 throw new comment_exception('nopermissiontocomment');
529 }
b9a689e8
DC
530 $DB->delete_records('comments', array('id'=>$commentid));
531 return true;
36051c9e
DC
532 }
533
76951100
DC
534 /**
535 * Print comments
536 * @param int $page
537 * @param boolean $return return comments list string or print it out
538 * @param boolean $nonjs print nonjs comments list or not?
539 * @return mixed
540 */
541 public function print_comments($page = 0, $return = true, $nonjs = true) {
5dce9d1b 542 global $DB, $CFG, $PAGE;
36051c9e
DC
543 $html = '';
544 if (!(self::$comment_itemid == $this->options->itemid &&
545 self::$comment_context == $this->options->context->id &&
546 self::$comment_area == $this->options->commentarea)) {
547 $page = 0;
548 }
549 $comments = $this->get_comments($page);
550
76951100
DC
551 $html = '';
552 if ($nonjs) {
553 $html .= '<h3>'.get_string('comments').'</h3>';
554 $html .= "<ul id='comment-list-$this->cid' class='comment-list'>";
555 }
36051c9e
DC
556 $results = array();
557 $list = '';
76951100 558
36051c9e 559 foreach ($comments as $cmt) {
76951100 560 $list = '<li id="comment-'.$cmt->id.'-'.$this->cid.'">'.$this->print_comment($cmt, $nonjs).'</li>' . $list;
36051c9e
DC
561 }
562 $html .= $list;
76951100
DC
563 if ($nonjs) {
564 $html .= '</ul>';
3e34183a 565 $html .= $this->get_pagination($page);
76951100 566 }
36051c9e 567 $sesskey = sesskey();
5dce9d1b 568 $returnurl = $PAGE->url;
36051c9e 569 $strsubmit = get_string('submit');
76951100 570 if ($nonjs) {
36051c9e
DC
571 $html .= <<<EOD
572<form method="POST" action="{$CFG->wwwroot}/comment/comment_post.php">
76951100 573<textarea name="content" rows="2"></textarea>
36051c9e
DC
574<input type="hidden" name="contextid" value="$this->contextid" />
575<input type="hidden" name="action" value="add" />
576<input type="hidden" name="area" value="$this->commentarea" />
577<input type="hidden" name="itemid" value="$this->itemid" />
578<input type="hidden" name="courseid" value="{$this->course->id}" />
579<input type="hidden" name="sesskey" value="{$sesskey}" />
580<input type="hidden" name="returnurl" value="{$returnurl}" />
581<input type="submit" value="{$strsubmit}" />
582</form>
583EOD;
76951100 584 }
36051c9e
DC
585 if ($return) {
586 return $html;
587 } else {
588 echo $html;
589 }
590 }
591
76951100
DC
592 public function print_comment($cmt, $nonjs = true) {
593 global $OUTPUT;
36051c9e
DC
594 $patterns = array();
595 $replacements = array();
596
76951100 597 if (!empty($cmt->delete) && empty($nonjs)) {
1150c1bd 598 $cmt->content = '<div class="comment-delete"><a href="#" id ="comment-delete-'.$this->cid.'-'.$cmt->id.'"><img src="'.$OUTPUT->pix_url('t/delete').'" /></a></div>' . $cmt->content;
76951100
DC
599 // add the button
600 }
36051c9e
DC
601 $patterns[] = '___picture___';
602 $patterns[] = '___name___';
603 $patterns[] = '___content___';
604 $patterns[] = '___time___';
605 $replacements[] = $cmt->avatar;
606 $replacements[] = fullname($cmt);
607 $replacements[] = $cmt->content;
608 $replacements[] = userdate($cmt->timecreated, get_string('strftimerecent', 'langconfig'));
609
610 // use html template to format a single comment.
611 return str_replace($patterns, $replacements, $this->template);
612 }
613}
614
615class comment_exception extends moodle_exception {
616 public $message;
617 function __construct($errorcode) {
618 $this->errorcode = $errorcode;
619 $this->message = get_string($errorcode, 'error');
1bcb7eb5 620 }
621}