Commit | Line | Data |
---|---|---|
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 | 21 | * @package comment |
3a11c09f | 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 | 26 | class 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; | |
866354a9 | 57 | private $courseid; |
36051c9e | 58 | /** |
866354a9 DC |
59 | * course module object, only be used to help find pluginname automatically |
60 | * if pluginname is specified, it won't be used at all | |
36051c9e DC |
61 | * @var string |
62 | */ | |
63 | private $cm; | |
64 | private $plugintype; | |
866354a9 DC |
65 | /** |
66 | * When used in module, it is recommended to use it | |
67 | * @var string | |
68 | */ | |
36051c9e DC |
69 | private $pluginname; |
70 | private $viewcap; | |
71 | private $postcap; | |
72 | /** | |
73 | * to tell comments api where it is used | |
74 | * @var string | |
75 | */ | |
76 | private $env; | |
77 | /** | |
78 | * to costomize link text | |
79 | * @var string | |
80 | */ | |
81 | private $linktext; | |
866354a9 DC |
82 | |
83 | // static variable will be used by non-js comments UI | |
36051c9e | 84 | private static $nonjs = false; |
36051c9e DC |
85 | private static $comment_itemid = null; |
86 | private static $comment_context = null; | |
87 | private static $comment_area = null; | |
d846488e | 88 | private static $comment_page = null; |
36051c9e | 89 | /** |
866354a9 | 90 | * Construct function of comment class, initialise |
36051c9e DC |
91 | * class members |
92 | * @param object $options | |
93 | */ | |
94 | public function __construct($options) { | |
95 | global $CFG, $DB; | |
76951100 | 96 | |
c794595d DC |
97 | if (empty($CFG->commentsperpage)) { |
98 | $CFG->commentsperpage = 15; | |
99 | } | |
100 | ||
36051c9e DC |
101 | $this->viewcap = false; |
102 | $this->postcap = false; | |
76951100 | 103 | |
866354a9 | 104 | // setup client_id |
36051c9e DC |
105 | if (!empty($options->client_id)) { |
106 | $this->cid = $options->client_id; | |
107 | } else { | |
108 | $this->cid = uniqid(); | |
109 | } | |
3a11c09f | 110 | |
866354a9 | 111 | // setup context |
36051c9e DC |
112 | if (!empty($options->context)) { |
113 | $this->context = $options->context; | |
114 | $this->contextid = $this->context->id; | |
115 | } else if(!empty($options->contextid)) { | |
116 | $this->contextid = $options->contextid; | |
117 | $this->context = get_context_instance_by_id($this->contextid); | |
118 | } else { | |
119 | print_error('invalidcontext'); | |
120 | } | |
76951100 | 121 | |
d846488e DC |
122 | if (!empty($options->component)) { |
123 | $this->set_component($options->component); | |
124 | } | |
125 | ||
866354a9 DC |
126 | // setup course |
127 | // course will be used to generate user profile link | |
128 | if (!empty($options->course)) { | |
129 | $this->courseid = $options->course->id; | |
130 | } else if (!empty($options->courseid)) { | |
131 | $this->courseid = $options->courseid; | |
5b95daec | 132 | } else { |
fe40925a | 133 | $this->courseid = SITEID; |
866354a9 DC |
134 | } |
135 | ||
866354a9 DC |
136 | // setup coursemodule |
137 | if (!empty($options->cm)) { | |
138 | $this->cm = $options->cm; | |
139 | } else { | |
140 | $this->cm = null; | |
141 | } | |
142 | ||
143 | // setup commentarea | |
36051c9e DC |
144 | if (!empty($options->area)) { |
145 | $this->commentarea = $options->area; | |
146 | } | |
76951100 | 147 | |
866354a9 | 148 | // setup itemid |
36051c9e DC |
149 | if (!empty($options->itemid)) { |
150 | $this->itemid = $options->itemid; | |
467c85d7 DC |
151 | } else { |
152 | $this->itemid = 0; | |
36051c9e | 153 | } |
76951100 | 154 | |
866354a9 | 155 | // setup env |
36051c9e DC |
156 | if (!empty($options->env)) { |
157 | $this->env = $options->env; | |
1bcb7eb5 | 158 | } else { |
08a22be6 | 159 | $this->env = ''; |
1bcb7eb5 | 160 | } |
1bcb7eb5 | 161 | |
866354a9 | 162 | // setup customized linktext |
36051c9e DC |
163 | if (!empty($options->linktext)) { |
164 | $this->linktext = $options->linktext; | |
165 | } else { | |
166 | $this->linktext = get_string('comments'); | |
167 | } | |
866354a9 DC |
168 | |
169 | if (!empty($options->showcount)) { | |
170 | $count = $this->count(); | |
171 | if (empty($count)) { | |
172 | $this->count = ''; | |
173 | } else { | |
174 | $this->count = '('.$count.')'; | |
175 | } | |
176 | } else { | |
177 | $this->count = ''; | |
178 | } | |
36051c9e | 179 | |
866354a9 DC |
180 | // setup options for callback functions |
181 | $this->args = new stdclass; | |
182 | $this->args->context = $this->context; | |
183 | $this->args->courseid = $this->courseid; | |
184 | $this->args->cm = $this->cm; | |
185 | $this->args->commentarea = $this->commentarea; | |
186 | $this->args->itemid = $this->itemid; | |
36051c9e | 187 | |
d846488e DC |
188 | // setting post and view permissions |
189 | $this->check_permissions(); | |
190 | ||
36051c9e DC |
191 | // load template |
192 | $this->template = <<<EOD | |
193 | <div class="comment-userpicture">___picture___</div> | |
194 | <div class="comment-content"> | |
195 | ___name___ - <span>___time___</span> | |
196 | <div>___content___</div> | |
197 | </div> | |
198 | EOD; | |
199 | if (!empty($this->plugintype)) { | |
866354a9 | 200 | $this->template = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'template', $this->args, $this->template); |
1bcb7eb5 | 201 | } |
202 | ||
36051c9e DC |
203 | unset($options); |
204 | } | |
205 | ||
206 | /** | |
207 | * Receive nonjs comment parameters | |
208 | */ | |
209 | public static function init() { | |
210 | global $PAGE, $CFG; | |
211 | // setup variables for non-js interface | |
212 | self::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHA); | |
213 | self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT); | |
214 | self::$comment_context = optional_param('comment_context', '', PARAM_INT); | |
a5306210 | 215 | self::$comment_page = optional_param('comment_page', '', PARAM_INT); |
36051c9e DC |
216 | self::$comment_area = optional_param('comment_area', '', PARAM_ALPHAEXT); |
217 | ||
218 | $PAGE->requires->string_for_js('addcomment', 'moodle'); | |
219 | $PAGE->requires->string_for_js('deletecomment', 'moodle'); | |
220 | $PAGE->requires->string_for_js('comments', 'moodle'); | |
1bcb7eb5 | 221 | } |
222 | ||
d846488e | 223 | public function set_component($component) { |
7ade777c | 224 | $this->component = $component; |
d846488e DC |
225 | list($this->plugintype, $this->pluginname) = normalize_component($component); |
226 | return null; | |
227 | } | |
866354a9 | 228 | |
d846488e DC |
229 | public function set_view_permission($value) { |
230 | $this->viewcap = $value; | |
231 | } | |
232 | ||
233 | public function set_post_permission($value) { | |
234 | $this->postcap = $value; | |
1bcb7eb5 | 235 | } |
236 | ||
237 | /** | |
36051c9e DC |
238 | * check posting comments permission |
239 | * It will check based on user roles and ask modules | |
240 | * If you need to check permission by modules, a | |
241 | * function named $pluginname_check_comment_post must be implemented | |
1bcb7eb5 | 242 | */ |
05fe3ab5 | 243 | private function check_permissions() { |
36051c9e DC |
244 | global $CFG; |
245 | $this->postcap = has_capability('moodle/comment:post', $this->context); | |
246 | $this->viewcap = has_capability('moodle/comment:view', $this->context); | |
247 | if (!empty($this->plugintype)) { | |
d846488e | 248 | $permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', array($this->args), array('post'=>true, 'view'=>true)); |
36051c9e DC |
249 | $this->postcap = $this->postcap && $permissions['post']; |
250 | $this->viewcap = $this->viewcap && $permissions['view']; | |
6f5e0852 | 251 | } |
1bcb7eb5 | 252 | } |
253 | ||
254 | /** | |
36051c9e DC |
255 | * Prepare comment code in html |
256 | * @param boolean $return | |
257 | * @return mixed | |
1bcb7eb5 | 258 | */ |
36051c9e | 259 | public function output($return = true) { |
4b201bc6 DC |
260 | global $PAGE, $OUTPUT; |
261 | static $template_printed; | |
36051c9e | 262 | |
5dce9d1b | 263 | $this->link = $PAGE->url; |
36051c9e DC |
264 | $murl = new moodle_url($this->link); |
265 | $murl->remove_params('nonjscomment'); | |
266 | $murl->param('nonjscomment', 'true'); | |
866354a9 DC |
267 | $murl->param('comment_itemid', $this->itemid); |
268 | $murl->param('comment_context', $this->context->id); | |
269 | $murl->param('comment_area', $this->commentarea); | |
a5306210 | 270 | $murl->remove_params('comment_page'); |
36051c9e | 271 | $this->link = $murl->out(); |
76951100 | 272 | |
36051c9e DC |
273 | $options = new stdclass; |
274 | $options->client_id = $this->cid; | |
275 | $options->commentarea = $this->commentarea; | |
276 | $options->itemid = $this->itemid; | |
277 | $options->page = 0; | |
866354a9 | 278 | $options->courseid = $this->courseid; |
36051c9e | 279 | $options->contextid = $this->contextid; |
76951100 | 280 | $options->env = $this->env; |
7ade777c | 281 | $options->component = $this->component; |
36051c9e | 282 | if ($this->env == 'block_comments') { |
36051c9e | 283 | $options->notoggle = true; |
e9935592 | 284 | $options->autostart = true; |
36051c9e DC |
285 | } |
286 | ||
287 | $PAGE->requires->js_init_call('M.core_comment.init', array($options), true); | |
288 | ||
289 | if (!empty(self::$nonjs)) { | |
76951100 | 290 | // return non js comments interface |
a5306210 | 291 | return $this->print_comments(self::$comment_page, $return, true); |
36051c9e | 292 | } |
76951100 | 293 | |
326d9aaa | 294 | $strsubmit = get_string('savecomment'); |
36051c9e | 295 | $strcancel = get_string('cancel'); |
b667ba00 | 296 | $strshowcomments = get_string('showcommentsnonjs'); |
36051c9e | 297 | $sesskey = sesskey(); |
4b201bc6 | 298 | $html = ''; |
36051c9e | 299 | // print html template |
76951100 | 300 | // Javascript will use the template to render new comments |
4b201bc6 DC |
301 | if (empty($template_printed) && !empty($this->viewcap)) { |
302 | $html .= '<div style="display:none" id="cmt-tmpl">' . $this->template . '</div>'; | |
303 | $template_printed = true; | |
36051c9e DC |
304 | } |
305 | ||
306 | if (!empty($this->viewcap)) { | |
307 | // print commenting icon and tooltip | |
07eebf20 | 308 | $icon = $OUTPUT->pix_url('t/collapsed'); |
4b201bc6 | 309 | $html .= <<<EOD |
b1d124d1 | 310 | <div class="mdl-left"> |
b667ba00 DC |
311 | <noscript> |
312 | <a href="{$this->link}">{$strshowcomments}</a> | |
313 | </noscript> | |
314 | <a id="comment-link-{$this->cid}" class="comment-link" href="#"> | |
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 |
320 | EOD; |
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> | |
330 | EOD; | |
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 |
342 | EOD; |
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 |
347 | EOD; |
348 | } | |
349 | ||
350 | $html .= <<<EOD | |
351 | </div> | |
352 | </div> | |
b1d124d1 | 353 | <div class="clearer"></div> |
36051c9e DC |
354 | EOD; |
355 | } | |
356 | ||
357 | $html .= <<<EOD | |
76951100 | 358 | </div><!-- end of comment-ctrl --> |
36051c9e DC |
359 | </div> |
360 | EOD; | |
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 | 373 | * Return matched comments |
4df53d1a | 374 | * |
36051c9e DC |
375 | * @param int $page |
376 | * @return mixed | |
1bcb7eb5 | 377 | */ |
36051c9e DC |
378 | public function get_comments($page = '') { |
379 | global $DB, $CFG, $USER, $OUTPUT; | |
380 | if (empty($this->viewcap)) { | |
381 | return false; | |
382 | } | |
36051c9e DC |
383 | if (!is_numeric($page)) { |
384 | $page = 0; | |
385 | } | |
386 | $this->page = $page; | |
387 | $params = array(); | |
388 | $start = $page * $CFG->commentsperpage; | |
4df53d1a | 389 | $ufields = user_picture::fields('u', array('username')); |
25228004 | 390 | $sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated |
3a11c09f PS |
391 | FROM {comments} c |
392 | JOIN {user} u ON u.id = c.userid | |
393 | WHERE c.contextid = :contextid AND c.commentarea = :commentarea AND c.itemid = :itemid | |
394 | ORDER BY c.timecreated DESC"; | |
395 | $params['contextid'] = $this->contextid; | |
396 | $params['commentarea'] = $this->commentarea; | |
397 | $params['itemid'] = $this->itemid; | |
36051c9e DC |
398 | |
399 | $comments = array(); | |
400 | $candelete = has_capability('moodle/comment:delete', $this->context); | |
3a11c09f | 401 | $rs = $DB->get_recordset_sql($sql, $params, $start, $CFG->commentsperpage); |
25228004 | 402 | foreach ($rs as $u) { |
3a11c09f PS |
403 | $c = new object(); |
404 | $c->id = $u->cid; | |
405 | $c->content = $u->ccontent; | |
406 | $c->format = $u->cformat; | |
407 | $c->timecreated = $u->ctimecreated; | |
08a5988a | 408 | $url = new moodle_url('/user/view.php', array('id'=>$u->id, 'course'=>$this->courseid)); |
4df53d1a DC |
409 | $c->username = $u->username; |
410 | $c->profileurl = $url; | |
411 | $c->fullname = fullname($u); | |
3a11c09f PS |
412 | $c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig')); |
413 | $c->content = format_text($c->content, $c->format); | |
414 | ||
415 | $c->avatar = $OUTPUT->user_picture($u, array('size'=>18)); | |
416 | if (($USER->id == $u->id) || !empty($candelete)) { | |
417 | $c->delete = true; | |
36051c9e | 418 | } |
3a11c09f | 419 | $comments[] = $c; |
36051c9e | 420 | } |
3a11c09f PS |
421 | $rs->close(); |
422 | ||
36051c9e DC |
423 | if (!empty($this->plugintype)) { |
424 | // moodle module will filter comments | |
866354a9 | 425 | $comments = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'display', array($comments, $this->args), $comments); |
36051c9e DC |
426 | } |
427 | ||
428 | return $comments; | |
429 | } | |
430 | ||
431 | public function count() { | |
1bcb7eb5 | 432 | global $DB; |
36051c9e DC |
433 | if ($count = $DB->count_records('comments', array('itemid'=>$this->itemid, 'commentarea'=>$this->commentarea, 'contextid'=>$this->context->id))) { |
434 | return $count; | |
435 | } else { | |
436 | return 0; | |
437 | } | |
438 | } | |
439 | ||
440 | public function get_pagination($page = 0) { | |
441 | global $DB, $CFG, $OUTPUT; | |
442 | $count = $this->count(); | |
443 | $pages = (int)ceil($count/$CFG->commentsperpage); | |
444 | if ($pages == 1 || $pages == 0) { | |
445 | return ''; | |
446 | } | |
447 | if (!empty(self::$nonjs)) { | |
448 | // used in non-js interface | |
a5306210 | 449 | return $OUTPUT->paging_bar($count, $page, $CFG->commentsperpage, $this->link, 'comment_page'); |
36051c9e DC |
450 | } else { |
451 | // return ajax paging bar | |
452 | $str = ''; | |
3e34183a | 453 | $str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid.'">'; |
36051c9e | 454 | for ($p=0; $p<$pages; $p++) { |
36051c9e | 455 | if ($p == $page) { |
c794595d DC |
456 | $class = 'curpage'; |
457 | } else { | |
458 | $class = 'pageno'; | |
1bcb7eb5 | 459 | } |
1150c1bd | 460 | $str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid.'-'.$p.'">'.($p+1).'</a> '; |
36051c9e DC |
461 | } |
462 | $str .= '</div>'; | |
463 | } | |
464 | return $str; | |
465 | } | |
466 | ||
467 | /** | |
468 | * Add a new comment | |
469 | * @param string $content | |
470 | * @return mixed | |
471 | */ | |
472 | public function add($content, $format = FORMAT_MOODLE) { | |
473 | global $CFG, $DB, $USER, $OUTPUT; | |
474 | if (empty($this->postcap)) { | |
475 | throw new comment_exception('nopermissiontocomment'); | |
476 | } | |
477 | $now = time(); | |
478 | $newcmt = new stdclass; | |
479 | $newcmt->contextid = $this->contextid; | |
480 | $newcmt->commentarea = $this->commentarea; | |
481 | $newcmt->itemid = $this->itemid; | |
482 | $newcmt->content = $content; | |
483 | $newcmt->format = $format; | |
484 | $newcmt->userid = $USER->id; | |
485 | $newcmt->timecreated = $now; | |
486 | ||
487 | if (!empty($this->plugintype)) { | |
488 | // moodle module will check content | |
866354a9 | 489 | $ret = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'add', array(&$newcmt, $this->args), true); |
36051c9e DC |
490 | if (!$ret) { |
491 | throw new comment_exception('modulererejectcomment'); | |
1bcb7eb5 | 492 | } |
493 | } | |
36051c9e DC |
494 | |
495 | $cmt_id = $DB->insert_record('comments', $newcmt); | |
496 | if (!empty($cmt_id)) { | |
497 | $newcmt->id = $cmt_id; | |
498 | $newcmt->time = userdate($now, get_string('strftimerecent', 'langconfig')); | |
4df53d1a DC |
499 | $newcmt->username = $USER->username; |
500 | $newcmt->fullname = fullname($USER); | |
501 | $url = new moodle_url('/user/view.php', array('id'=>$USER->id, 'course'=>$this->courseid)); | |
502 | $newcmt->profileurl = $url->out(); | |
650a0c0a | 503 | $newcmt->content = format_text($newcmt->content, $format); |
36051c9e DC |
504 | $newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16)); |
505 | return $newcmt; | |
506 | } else { | |
507 | throw new comment_exception('dbupdatefailed'); | |
508 | } | |
509 | } | |
510 | ||
511 | /** | |
512 | * delete by context, commentarea and itemid | |
650a0c0a DC |
513 | * @param object $param { |
514 | * contextid => int the context in which the comments exist [required] | |
515 | * commentarea => string the comment area [optional] | |
516 | * itemid => int comment itemid [optional] | |
517 | * } | |
518 | * @return boolean | |
36051c9e | 519 | */ |
650a0c0a | 520 | public function delete_comments($param) { |
36051c9e | 521 | global $DB; |
650a0c0a DC |
522 | $param = (array)$param; |
523 | if (empty($param['contextid'])) { | |
524 | return false; | |
525 | } | |
526 | $DB->delete_records('comments', $param); | |
b9a689e8 | 527 | return true; |
36051c9e DC |
528 | } |
529 | ||
530 | /** | |
531 | * Delete a comment | |
532 | * @param int $commentid | |
533 | * @return mixed | |
534 | */ | |
535 | public function delete($commentid) { | |
536 | global $DB, $USER; | |
537 | $candelete = has_capability('moodle/comment:delete', $this->context); | |
538 | if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) { | |
539 | throw new comment_exception('dbupdatefailed'); | |
540 | } | |
541 | if (!($USER->id == $comment->userid || !empty($candelete))) { | |
542 | throw new comment_exception('nopermissiontocomment'); | |
543 | } | |
b9a689e8 DC |
544 | $DB->delete_records('comments', array('id'=>$commentid)); |
545 | return true; | |
36051c9e DC |
546 | } |
547 | ||
76951100 DC |
548 | /** |
549 | * Print comments | |
550 | * @param int $page | |
551 | * @param boolean $return return comments list string or print it out | |
552 | * @param boolean $nonjs print nonjs comments list or not? | |
553 | * @return mixed | |
554 | */ | |
555 | public function print_comments($page = 0, $return = true, $nonjs = true) { | |
5dce9d1b | 556 | global $DB, $CFG, $PAGE; |
36051c9e | 557 | $html = ''; |
866354a9 DC |
558 | if (!(self::$comment_itemid == $this->itemid && |
559 | self::$comment_context == $this->context->id && | |
560 | self::$comment_area == $this->commentarea)) { | |
36051c9e DC |
561 | $page = 0; |
562 | } | |
563 | $comments = $this->get_comments($page); | |
564 | ||
76951100 DC |
565 | $html = ''; |
566 | if ($nonjs) { | |
567 | $html .= '<h3>'.get_string('comments').'</h3>'; | |
568 | $html .= "<ul id='comment-list-$this->cid' class='comment-list'>"; | |
569 | } | |
36051c9e DC |
570 | $results = array(); |
571 | $list = ''; | |
76951100 | 572 | |
36051c9e | 573 | foreach ($comments as $cmt) { |
76951100 | 574 | $list = '<li id="comment-'.$cmt->id.'-'.$this->cid.'">'.$this->print_comment($cmt, $nonjs).'</li>' . $list; |
36051c9e DC |
575 | } |
576 | $html .= $list; | |
76951100 DC |
577 | if ($nonjs) { |
578 | $html .= '</ul>'; | |
3e34183a | 579 | $html .= $this->get_pagination($page); |
76951100 | 580 | } |
36051c9e | 581 | $sesskey = sesskey(); |
5dce9d1b | 582 | $returnurl = $PAGE->url; |
36051c9e | 583 | $strsubmit = get_string('submit'); |
76951100 | 584 | if ($nonjs) { |
36051c9e DC |
585 | $html .= <<<EOD |
586 | <form method="POST" action="{$CFG->wwwroot}/comment/comment_post.php"> | |
76951100 | 587 | <textarea name="content" rows="2"></textarea> |
36051c9e DC |
588 | <input type="hidden" name="contextid" value="$this->contextid" /> |
589 | <input type="hidden" name="action" value="add" /> | |
590 | <input type="hidden" name="area" value="$this->commentarea" /> | |
591 | <input type="hidden" name="itemid" value="$this->itemid" /> | |
866354a9 | 592 | <input type="hidden" name="courseid" value="{$this->courseid}" /> |
36051c9e DC |
593 | <input type="hidden" name="sesskey" value="{$sesskey}" /> |
594 | <input type="hidden" name="returnurl" value="{$returnurl}" /> | |
595 | <input type="submit" value="{$strsubmit}" /> | |
596 | </form> | |
597 | EOD; | |
76951100 | 598 | } |
36051c9e DC |
599 | if ($return) { |
600 | return $html; | |
601 | } else { | |
602 | echo $html; | |
603 | } | |
604 | } | |
605 | ||
76951100 DC |
606 | public function print_comment($cmt, $nonjs = true) { |
607 | global $OUTPUT; | |
36051c9e DC |
608 | $patterns = array(); |
609 | $replacements = array(); | |
610 | ||
76951100 | 611 | if (!empty($cmt->delete) && empty($nonjs)) { |
1150c1bd | 612 | $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 |
613 | // add the button |
614 | } | |
36051c9e DC |
615 | $patterns[] = '___picture___'; |
616 | $patterns[] = '___name___'; | |
617 | $patterns[] = '___content___'; | |
618 | $patterns[] = '___time___'; | |
619 | $replacements[] = $cmt->avatar; | |
4df53d1a | 620 | $replacements[] = html_writer::link($cmt->profileurl, $cmt->fullname); |
36051c9e DC |
621 | $replacements[] = $cmt->content; |
622 | $replacements[] = userdate($cmt->timecreated, get_string('strftimerecent', 'langconfig')); | |
623 | ||
624 | // use html template to format a single comment. | |
625 | return str_replace($patterns, $replacements, $this->template); | |
626 | } | |
627 | } | |
628 | ||
629 | class comment_exception extends moodle_exception { | |
630 | public $message; | |
631 | function __construct($errorcode) { | |
632 | $this->errorcode = $errorcode; | |
633 | $this->message = get_string($errorcode, 'error'); | |
1bcb7eb5 | 634 | } |
635 | } |