2009-07-24 02:44:44 +00:00
|
|
|
<?php
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle 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.
|
|
|
|
//
|
|
|
|
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2009-07-24 02:44:44 +00:00
|
|
|
/**
|
2010-03-15 07:59:28 +00:00
|
|
|
* Comment is helper class to add/delete comments anywhere in moodle
|
2009-07-24 02:44:44 +00:00
|
|
|
*
|
2010-03-15 07:59:28 +00:00
|
|
|
* @package comment
|
2010-07-04 18:36:34 +00:00
|
|
|
* @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
|
2009-07-24 02:44:44 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
class comment {
|
2009-07-24 02:44:44 +00:00
|
|
|
/**
|
2010-03-15 07:59:28 +00:00
|
|
|
* @var integer
|
2009-07-24 02:44:44 +00:00
|
|
|
*/
|
2010-03-15 07:59:28 +00:00
|
|
|
private $page;
|
|
|
|
/**
|
|
|
|
* there may be several comment box in one page
|
|
|
|
* so we need a client_id to recognize them
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $cid;
|
|
|
|
private $contextid;
|
|
|
|
/**
|
|
|
|
* commentarea is used to specify different
|
|
|
|
* parts shared the same itemid
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $commentarea;
|
|
|
|
/**
|
|
|
|
* itemid is used to associate with commenting content
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $itemid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this html snippet will be used as a template
|
|
|
|
* to build comment content
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $template;
|
|
|
|
private $context;
|
2010-04-09 09:03:51 +00:00
|
|
|
private $courseid;
|
2010-03-15 07:59:28 +00:00
|
|
|
/**
|
2010-04-09 09:03:51 +00:00
|
|
|
* course module object, only be used to help find pluginname automatically
|
|
|
|
* if pluginname is specified, it won't be used at all
|
2010-03-15 07:59:28 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $cm;
|
|
|
|
private $plugintype;
|
2010-04-09 09:03:51 +00:00
|
|
|
/**
|
|
|
|
* When used in module, it is recommended to use it
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-03-15 07:59:28 +00:00
|
|
|
private $pluginname;
|
|
|
|
private $viewcap;
|
|
|
|
private $postcap;
|
|
|
|
/**
|
|
|
|
* to tell comments api where it is used
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $env;
|
|
|
|
/**
|
|
|
|
* to costomize link text
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $linktext;
|
2010-04-09 09:03:51 +00:00
|
|
|
|
|
|
|
// static variable will be used by non-js comments UI
|
2010-03-15 07:59:28 +00:00
|
|
|
private static $nonjs = false;
|
|
|
|
private static $comment_itemid = null;
|
|
|
|
private static $comment_context = null;
|
|
|
|
private static $comment_area = null;
|
2010-08-25 03:50:19 +00:00
|
|
|
private static $comment_page = null;
|
2010-08-25 07:47:22 +00:00
|
|
|
private static $comment_component = null;
|
2010-03-15 07:59:28 +00:00
|
|
|
/**
|
2010-04-09 09:03:51 +00:00
|
|
|
* Construct function of comment class, initialise
|
2010-03-15 07:59:28 +00:00
|
|
|
* class members
|
|
|
|
* @param object $options
|
|
|
|
*/
|
|
|
|
public function __construct($options) {
|
|
|
|
global $CFG, $DB;
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-03-29 05:29:53 +00:00
|
|
|
if (empty($CFG->commentsperpage)) {
|
|
|
|
$CFG->commentsperpage = 15;
|
|
|
|
}
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
$this->viewcap = false;
|
|
|
|
$this->postcap = false;
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup client_id
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($options->client_id)) {
|
|
|
|
$this->cid = $options->client_id;
|
|
|
|
} else {
|
|
|
|
$this->cid = uniqid();
|
|
|
|
}
|
2010-07-04 18:36:34 +00:00
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup context
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($options->context)) {
|
|
|
|
$this->context = $options->context;
|
|
|
|
$this->contextid = $this->context->id;
|
|
|
|
} else if(!empty($options->contextid)) {
|
|
|
|
$this->contextid = $options->contextid;
|
|
|
|
$this->context = get_context_instance_by_id($this->contextid);
|
|
|
|
} else {
|
|
|
|
print_error('invalidcontext');
|
|
|
|
}
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-08-25 03:50:19 +00:00
|
|
|
if (!empty($options->component)) {
|
|
|
|
$this->set_component($options->component);
|
|
|
|
}
|
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup course
|
|
|
|
// course will be used to generate user profile link
|
|
|
|
if (!empty($options->course)) {
|
|
|
|
$this->courseid = $options->course->id;
|
|
|
|
} else if (!empty($options->courseid)) {
|
|
|
|
$this->courseid = $options->courseid;
|
2010-04-19 03:05:28 +00:00
|
|
|
} else {
|
2010-05-03 16:14:05 +00:00
|
|
|
$this->courseid = SITEID;
|
2010-04-09 09:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup coursemodule
|
|
|
|
if (!empty($options->cm)) {
|
|
|
|
$this->cm = $options->cm;
|
|
|
|
} else {
|
|
|
|
$this->cm = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup commentarea
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($options->area)) {
|
|
|
|
$this->commentarea = $options->area;
|
|
|
|
}
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup itemid
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($options->itemid)) {
|
|
|
|
$this->itemid = $options->itemid;
|
2010-04-14 05:01:43 +00:00
|
|
|
} else {
|
|
|
|
$this->itemid = 0;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup env
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($options->env)) {
|
|
|
|
$this->env = $options->env;
|
2009-07-24 02:44:44 +00:00
|
|
|
} else {
|
2010-03-19 04:53:54 +00:00
|
|
|
$this->env = '';
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup customized linktext
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($options->linktext)) {
|
|
|
|
$this->linktext = $options->linktext;
|
|
|
|
} else {
|
|
|
|
$this->linktext = get_string('comments');
|
|
|
|
}
|
2010-04-09 09:03:51 +00:00
|
|
|
|
2010-08-25 07:47:22 +00:00
|
|
|
if (!empty($options->ignore_permission)) {
|
|
|
|
$this->ignore_permission = true;
|
|
|
|
} else {
|
|
|
|
$this->ignore_permission = false;
|
|
|
|
}
|
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
if (!empty($options->showcount)) {
|
|
|
|
$count = $this->count();
|
|
|
|
if (empty($count)) {
|
|
|
|
$this->count = '';
|
|
|
|
} else {
|
|
|
|
$this->count = '('.$count.')';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->count = '';
|
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
|
2010-04-09 09:03:51 +00:00
|
|
|
// setup options for callback functions
|
|
|
|
$this->args = new stdclass;
|
|
|
|
$this->args->context = $this->context;
|
|
|
|
$this->args->courseid = $this->courseid;
|
|
|
|
$this->args->cm = $this->cm;
|
|
|
|
$this->args->commentarea = $this->commentarea;
|
|
|
|
$this->args->itemid = $this->itemid;
|
2010-03-15 07:59:28 +00:00
|
|
|
|
2010-08-25 03:50:19 +00:00
|
|
|
// setting post and view permissions
|
|
|
|
$this->check_permissions();
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
// load template
|
|
|
|
$this->template = <<<EOD
|
|
|
|
<div class="comment-userpicture">___picture___</div>
|
|
|
|
<div class="comment-content">
|
|
|
|
___name___ - <span>___time___</span>
|
|
|
|
<div>___content___</div>
|
|
|
|
</div>
|
|
|
|
EOD;
|
|
|
|
if (!empty($this->plugintype)) {
|
2010-04-09 09:03:51 +00:00
|
|
|
$this->template = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'template', $this->args, $this->template);
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
unset($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive nonjs comment parameters
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
global $PAGE, $CFG;
|
|
|
|
// setup variables for non-js interface
|
|
|
|
self::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHA);
|
|
|
|
self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT);
|
|
|
|
self::$comment_context = optional_param('comment_context', '', PARAM_INT);
|
2010-08-25 07:47:22 +00:00
|
|
|
self::$comment_page = optional_param('comment_page', '', PARAM_INT);
|
2010-03-15 07:59:28 +00:00
|
|
|
self::$comment_area = optional_param('comment_area', '', PARAM_ALPHAEXT);
|
|
|
|
|
|
|
|
$PAGE->requires->string_for_js('addcomment', 'moodle');
|
|
|
|
$PAGE->requires->string_for_js('deletecomment', 'moodle');
|
|
|
|
$PAGE->requires->string_for_js('comments', 'moodle');
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 03:50:19 +00:00
|
|
|
public function set_component($component) {
|
2010-08-25 06:24:17 +00:00
|
|
|
$this->component = $component;
|
2010-08-25 03:50:19 +00:00
|
|
|
list($this->plugintype, $this->pluginname) = normalize_component($component);
|
|
|
|
return null;
|
|
|
|
}
|
2010-04-09 09:03:51 +00:00
|
|
|
|
2010-08-25 03:50:19 +00:00
|
|
|
public function set_view_permission($value) {
|
|
|
|
$this->viewcap = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set_post_permission($value) {
|
|
|
|
$this->postcap = $value;
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-15 07:59:28 +00:00
|
|
|
* check posting comments permission
|
|
|
|
* It will check based on user roles and ask modules
|
|
|
|
* If you need to check permission by modules, a
|
|
|
|
* function named $pluginname_check_comment_post must be implemented
|
2009-07-24 02:44:44 +00:00
|
|
|
*/
|
2010-03-29 05:44:48 +00:00
|
|
|
private function check_permissions() {
|
2010-03-15 07:59:28 +00:00
|
|
|
global $CFG;
|
|
|
|
$this->postcap = has_capability('moodle/comment:post', $this->context);
|
|
|
|
$this->viewcap = has_capability('moodle/comment:view', $this->context);
|
|
|
|
if (!empty($this->plugintype)) {
|
2010-08-25 03:50:19 +00:00
|
|
|
$permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', array($this->args), array('post'=>true, 'view'=>true));
|
2010-08-25 07:47:22 +00:00
|
|
|
if ($this->ignore_permission) {
|
|
|
|
$this->postcap = $permissions['post'];
|
|
|
|
$this->viewcap = $permissions['view'];
|
2010-08-25 08:20:46 +00:00
|
|
|
} else {
|
|
|
|
$this->postcap = $this->postcap && $permissions['post'];
|
|
|
|
$this->viewcap = $this->viewcap && $permissions['view'];
|
2010-08-25 07:47:22 +00:00
|
|
|
}
|
2009-11-01 16:48:45 +00:00
|
|
|
}
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-15 07:59:28 +00:00
|
|
|
* Prepare comment code in html
|
|
|
|
* @param boolean $return
|
|
|
|
* @return mixed
|
2009-07-24 02:44:44 +00:00
|
|
|
*/
|
2010-03-15 07:59:28 +00:00
|
|
|
public function output($return = true) {
|
2010-04-07 06:42:17 +00:00
|
|
|
global $PAGE, $OUTPUT;
|
|
|
|
static $template_printed;
|
2010-03-15 07:59:28 +00:00
|
|
|
|
2010-03-29 03:54:14 +00:00
|
|
|
$this->link = $PAGE->url;
|
2010-03-15 07:59:28 +00:00
|
|
|
$murl = new moodle_url($this->link);
|
|
|
|
$murl->remove_params('nonjscomment');
|
|
|
|
$murl->param('nonjscomment', 'true');
|
2010-04-09 09:03:51 +00:00
|
|
|
$murl->param('comment_itemid', $this->itemid);
|
|
|
|
$murl->param('comment_context', $this->context->id);
|
|
|
|
$murl->param('comment_area', $this->commentarea);
|
2010-04-08 05:38:44 +00:00
|
|
|
$murl->remove_params('comment_page');
|
2010-03-15 07:59:28 +00:00
|
|
|
$this->link = $murl->out();
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
$options = new stdclass;
|
|
|
|
$options->client_id = $this->cid;
|
|
|
|
$options->commentarea = $this->commentarea;
|
|
|
|
$options->itemid = $this->itemid;
|
|
|
|
$options->page = 0;
|
2010-04-09 09:03:51 +00:00
|
|
|
$options->courseid = $this->courseid;
|
2010-03-15 07:59:28 +00:00
|
|
|
$options->contextid = $this->contextid;
|
2010-03-18 09:42:30 +00:00
|
|
|
$options->env = $this->env;
|
2010-08-25 06:24:17 +00:00
|
|
|
$options->component = $this->component;
|
2010-03-15 07:59:28 +00:00
|
|
|
if ($this->env == 'block_comments') {
|
|
|
|
$options->notoggle = true;
|
2010-08-25 06:15:01 +00:00
|
|
|
$options->autostart = true;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$PAGE->requires->js_init_call('M.core_comment.init', array($options), true);
|
|
|
|
|
|
|
|
if (!empty(self::$nonjs)) {
|
2010-03-18 09:42:30 +00:00
|
|
|
// return non js comments interface
|
2010-04-08 05:38:44 +00:00
|
|
|
return $this->print_comments(self::$comment_page, $return, true);
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-07-09 03:57:41 +00:00
|
|
|
$strsubmit = get_string('savecomment');
|
2010-03-15 07:59:28 +00:00
|
|
|
$strcancel = get_string('cancel');
|
2010-08-02 08:16:51 +00:00
|
|
|
$strshowcomments = get_string('showcommentsnonjs');
|
2010-03-15 07:59:28 +00:00
|
|
|
$sesskey = sesskey();
|
2010-04-07 06:42:17 +00:00
|
|
|
$html = '';
|
2010-03-15 07:59:28 +00:00
|
|
|
// print html template
|
2010-03-18 09:42:30 +00:00
|
|
|
// Javascript will use the template to render new comments
|
2010-04-07 06:42:17 +00:00
|
|
|
if (empty($template_printed) && !empty($this->viewcap)) {
|
|
|
|
$html .= '<div style="display:none" id="cmt-tmpl">' . $this->template . '</div>';
|
|
|
|
$template_printed = true;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->viewcap)) {
|
|
|
|
// print commenting icon and tooltip
|
2010-03-29 06:07:31 +00:00
|
|
|
$icon = $OUTPUT->pix_url('t/collapsed');
|
2010-04-07 06:42:17 +00:00
|
|
|
$html .= <<<EOD
|
2010-03-29 04:06:13 +00:00
|
|
|
<div class="mdl-left">
|
2010-08-02 08:16:51 +00:00
|
|
|
<noscript>
|
|
|
|
<a href="{$this->link}">{$strshowcomments}</a>
|
|
|
|
</noscript>
|
|
|
|
<a id="comment-link-{$this->cid}" class="comment-link" href="#">
|
2010-03-29 06:07:31 +00:00
|
|
|
<img id="comment-img-{$this->cid}" src="$icon" alt="{$this->linktext}" title="{$this->linktext}" />
|
2010-03-18 09:42:30 +00:00
|
|
|
<span id="comment-link-text-{$this->cid}">{$this->linktext} {$this->count}</span>
|
2010-03-15 07:59:28 +00:00
|
|
|
</a>
|
|
|
|
<div id="comment-ctrl-{$this->cid}" class="comment-ctrl">
|
|
|
|
<ul id="comment-list-{$this->cid}" class="comment-list">
|
2010-03-18 09:42:30 +00:00
|
|
|
EOD;
|
|
|
|
// in comments block, we print comments list right away
|
|
|
|
if ($this->env == 'block_comments') {
|
|
|
|
$html .= $this->print_comments(0, true, false);
|
2010-03-18 09:57:32 +00:00
|
|
|
$html .= '</ul>';
|
|
|
|
$html .= $this->get_pagination(0);
|
|
|
|
} else {
|
|
|
|
$html .= <<<EOD
|
2010-03-15 07:59:28 +00:00
|
|
|
</ul>
|
|
|
|
<div id="comment-pagination-{$this->cid}" class="comment-pagination"></div>
|
|
|
|
EOD;
|
2010-03-18 09:57:32 +00:00
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
|
|
|
|
// print posting textarea
|
|
|
|
if (!empty($this->postcap)) {
|
|
|
|
$html .= <<<EOD
|
|
|
|
<div class='comment-area'>
|
|
|
|
<div class="bd">
|
2010-03-18 09:42:30 +00:00
|
|
|
<textarea name="content" rows="2" id="dlg-content-{$this->cid}"></textarea>
|
2010-03-15 07:59:28 +00:00
|
|
|
</div>
|
|
|
|
<div class="fd" id="comment-action-{$this->cid}">
|
2010-04-07 09:09:32 +00:00
|
|
|
<a href="#" id="comment-action-post-{$this->cid}"> {$strsubmit} </a>
|
2010-03-15 07:59:28 +00:00
|
|
|
EOD;
|
|
|
|
if ($this->env != 'block_comments') {
|
|
|
|
$html .= <<<EOD
|
|
|
|
<span> | </span>
|
2010-04-07 09:09:32 +00:00
|
|
|
<a href="#" id="comment-action-cancel-{$this->cid}"> {$strcancel} </a>
|
2010-03-15 07:59:28 +00:00
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
$html .= <<<EOD
|
|
|
|
</div>
|
|
|
|
</div>
|
2010-03-29 04:06:13 +00:00
|
|
|
<div class="clearer"></div>
|
2010-03-15 07:59:28 +00:00
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
$html .= <<<EOD
|
2010-03-18 09:42:30 +00:00
|
|
|
</div><!-- end of comment-ctrl -->
|
2010-03-15 07:59:28 +00:00
|
|
|
</div>
|
|
|
|
EOD;
|
|
|
|
} else {
|
|
|
|
$html = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return) {
|
|
|
|
return $html;
|
|
|
|
} else {
|
|
|
|
echo $html;
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
|
2009-07-24 02:44:44 +00:00
|
|
|
/**
|
2010-03-15 07:59:28 +00:00
|
|
|
* Return matched comments
|
2010-07-12 08:49:11 +00:00
|
|
|
*
|
2010-03-15 07:59:28 +00:00
|
|
|
* @param int $page
|
|
|
|
* @return mixed
|
2009-07-24 02:44:44 +00:00
|
|
|
*/
|
2010-03-15 07:59:28 +00:00
|
|
|
public function get_comments($page = '') {
|
|
|
|
global $DB, $CFG, $USER, $OUTPUT;
|
|
|
|
if (empty($this->viewcap)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_numeric($page)) {
|
|
|
|
$page = 0;
|
|
|
|
}
|
|
|
|
$this->page = $page;
|
|
|
|
$params = array();
|
|
|
|
$start = $page * $CFG->commentsperpage;
|
2010-08-25 09:11:36 +00:00
|
|
|
$ufields = user_picture::fields('u');
|
2010-07-05 08:53:42 +00:00
|
|
|
$sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated
|
2010-07-04 18:36:34 +00:00
|
|
|
FROM {comments} c
|
|
|
|
JOIN {user} u ON u.id = c.userid
|
|
|
|
WHERE c.contextid = :contextid AND c.commentarea = :commentarea AND c.itemid = :itemid
|
|
|
|
ORDER BY c.timecreated DESC";
|
|
|
|
$params['contextid'] = $this->contextid;
|
|
|
|
$params['commentarea'] = $this->commentarea;
|
|
|
|
$params['itemid'] = $this->itemid;
|
2010-03-15 07:59:28 +00:00
|
|
|
|
|
|
|
$comments = array();
|
|
|
|
$candelete = has_capability('moodle/comment:delete', $this->context);
|
2010-07-04 18:36:34 +00:00
|
|
|
$rs = $DB->get_recordset_sql($sql, $params, $start, $CFG->commentsperpage);
|
2010-07-05 08:53:42 +00:00
|
|
|
foreach ($rs as $u) {
|
2010-07-04 18:36:34 +00:00
|
|
|
$c = new object();
|
|
|
|
$c->id = $u->cid;
|
|
|
|
$c->content = $u->ccontent;
|
|
|
|
$c->format = $u->cformat;
|
|
|
|
$c->timecreated = $u->ctimecreated;
|
2010-07-13 09:07:19 +00:00
|
|
|
$url = new moodle_url('/user/view.php', array('id'=>$u->id, 'course'=>$this->courseid));
|
2010-08-25 07:47:22 +00:00
|
|
|
$c->profileurl = $url->out();
|
2010-07-12 08:49:11 +00:00
|
|
|
$c->fullname = fullname($u);
|
2010-07-04 18:36:34 +00:00
|
|
|
$c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig'));
|
|
|
|
$c->content = format_text($c->content, $c->format);
|
|
|
|
|
|
|
|
$c->avatar = $OUTPUT->user_picture($u, array('size'=>18));
|
|
|
|
if (($USER->id == $u->id) || !empty($candelete)) {
|
|
|
|
$c->delete = true;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
2010-07-04 18:36:34 +00:00
|
|
|
$comments[] = $c;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
2010-07-04 18:36:34 +00:00
|
|
|
$rs->close();
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!empty($this->plugintype)) {
|
|
|
|
// moodle module will filter comments
|
2010-04-09 09:03:51 +00:00
|
|
|
$comments = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'display', array($comments, $this->args), $comments);
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count() {
|
2009-07-24 02:44:44 +00:00
|
|
|
global $DB;
|
2010-03-15 07:59:28 +00:00
|
|
|
if ($count = $DB->count_records('comments', array('itemid'=>$this->itemid, 'commentarea'=>$this->commentarea, 'contextid'=>$this->context->id))) {
|
|
|
|
return $count;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_pagination($page = 0) {
|
|
|
|
global $DB, $CFG, $OUTPUT;
|
|
|
|
$count = $this->count();
|
|
|
|
$pages = (int)ceil($count/$CFG->commentsperpage);
|
|
|
|
if ($pages == 1 || $pages == 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (!empty(self::$nonjs)) {
|
|
|
|
// used in non-js interface
|
2010-04-08 05:38:44 +00:00
|
|
|
return $OUTPUT->paging_bar($count, $page, $CFG->commentsperpage, $this->link, 'comment_page');
|
2010-03-15 07:59:28 +00:00
|
|
|
} else {
|
|
|
|
// return ajax paging bar
|
|
|
|
$str = '';
|
2010-03-18 09:57:32 +00:00
|
|
|
$str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid.'">';
|
2010-03-15 07:59:28 +00:00
|
|
|
for ($p=0; $p<$pages; $p++) {
|
|
|
|
if ($p == $page) {
|
2010-03-29 05:29:53 +00:00
|
|
|
$class = 'curpage';
|
|
|
|
} else {
|
|
|
|
$class = 'pageno';
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
2010-04-07 09:09:32 +00:00
|
|
|
$str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid.'-'.$p.'">'.($p+1).'</a> ';
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
$str .= '</div>';
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new comment
|
|
|
|
* @param string $content
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function add($content, $format = FORMAT_MOODLE) {
|
|
|
|
global $CFG, $DB, $USER, $OUTPUT;
|
|
|
|
if (empty($this->postcap)) {
|
|
|
|
throw new comment_exception('nopermissiontocomment');
|
|
|
|
}
|
|
|
|
$now = time();
|
|
|
|
$newcmt = new stdclass;
|
|
|
|
$newcmt->contextid = $this->contextid;
|
|
|
|
$newcmt->commentarea = $this->commentarea;
|
|
|
|
$newcmt->itemid = $this->itemid;
|
|
|
|
$newcmt->content = $content;
|
|
|
|
$newcmt->format = $format;
|
|
|
|
$newcmt->userid = $USER->id;
|
|
|
|
$newcmt->timecreated = $now;
|
|
|
|
|
|
|
|
if (!empty($this->plugintype)) {
|
|
|
|
// moodle module will check content
|
2010-04-09 09:03:51 +00:00
|
|
|
$ret = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'add', array(&$newcmt, $this->args), true);
|
2010-03-15 07:59:28 +00:00
|
|
|
if (!$ret) {
|
|
|
|
throw new comment_exception('modulererejectcomment');
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
|
|
|
|
$cmt_id = $DB->insert_record('comments', $newcmt);
|
|
|
|
if (!empty($cmt_id)) {
|
|
|
|
$newcmt->id = $cmt_id;
|
|
|
|
$newcmt->time = userdate($now, get_string('strftimerecent', 'langconfig'));
|
2010-07-12 08:49:11 +00:00
|
|
|
$newcmt->fullname = fullname($USER);
|
|
|
|
$url = new moodle_url('/user/view.php', array('id'=>$USER->id, 'course'=>$this->courseid));
|
|
|
|
$newcmt->profileurl = $url->out();
|
2010-08-02 07:44:45 +00:00
|
|
|
$newcmt->content = format_text($newcmt->content, $format);
|
2010-03-15 07:59:28 +00:00
|
|
|
$newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16));
|
|
|
|
return $newcmt;
|
|
|
|
} else {
|
|
|
|
throw new comment_exception('dbupdatefailed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete by context, commentarea and itemid
|
2010-09-16 07:38:52 +00:00
|
|
|
* @param object $param {
|
|
|
|
* contextid => int the context in which the comments exist [required]
|
|
|
|
* commentarea => string the comment area [optional]
|
|
|
|
* itemid => int comment itemid [optional]
|
|
|
|
* }
|
2010-08-02 07:44:45 +00:00
|
|
|
* @return boolean
|
2010-03-15 07:59:28 +00:00
|
|
|
*/
|
2010-08-02 07:44:45 +00:00
|
|
|
public function delete_comments($param) {
|
2010-03-15 07:59:28 +00:00
|
|
|
global $DB;
|
2010-08-02 07:44:45 +00:00
|
|
|
$param = (array)$param;
|
|
|
|
if (empty($param['contextid'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$DB->delete_records('comments', $param);
|
2010-03-30 07:38:47 +00:00
|
|
|
return true;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
2010-09-16 07:38:52 +00:00
|
|
|
/**
|
|
|
|
* Delete page_comments in whole course, used by course reset
|
|
|
|
* @param object $context course context
|
|
|
|
*/
|
|
|
|
public function reset_course_page_comments($context) {
|
|
|
|
global $DB;
|
|
|
|
$contexts = array();
|
|
|
|
$contexts[] = $context->id;
|
|
|
|
$children = get_child_contexts($context);
|
|
|
|
foreach ($children as $c) {
|
|
|
|
$contexts[] = $c->id;
|
|
|
|
}
|
|
|
|
list($ids, $params) = $DB->get_in_or_equal($contexts);
|
|
|
|
$DB->delete_records_select('comments', "commentarea='page_comments' AND contextid $ids", $params);
|
|
|
|
}
|
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
/**
|
|
|
|
* Delete a comment
|
|
|
|
* @param int $commentid
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function delete($commentid) {
|
|
|
|
global $DB, $USER;
|
|
|
|
$candelete = has_capability('moodle/comment:delete', $this->context);
|
|
|
|
if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) {
|
|
|
|
throw new comment_exception('dbupdatefailed');
|
|
|
|
}
|
|
|
|
if (!($USER->id == $comment->userid || !empty($candelete))) {
|
|
|
|
throw new comment_exception('nopermissiontocomment');
|
|
|
|
}
|
2010-03-30 07:38:47 +00:00
|
|
|
$DB->delete_records('comments', array('id'=>$commentid));
|
|
|
|
return true;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 09:42:30 +00:00
|
|
|
/**
|
|
|
|
* Print comments
|
|
|
|
* @param int $page
|
|
|
|
* @param boolean $return return comments list string or print it out
|
|
|
|
* @param boolean $nonjs print nonjs comments list or not?
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function print_comments($page = 0, $return = true, $nonjs = true) {
|
2010-03-29 03:54:14 +00:00
|
|
|
global $DB, $CFG, $PAGE;
|
2010-03-15 07:59:28 +00:00
|
|
|
$html = '';
|
2010-04-09 09:03:51 +00:00
|
|
|
if (!(self::$comment_itemid == $this->itemid &&
|
|
|
|
self::$comment_context == $this->context->id &&
|
|
|
|
self::$comment_area == $this->commentarea)) {
|
2010-03-15 07:59:28 +00:00
|
|
|
$page = 0;
|
|
|
|
}
|
|
|
|
$comments = $this->get_comments($page);
|
|
|
|
|
2010-03-18 09:42:30 +00:00
|
|
|
$html = '';
|
|
|
|
if ($nonjs) {
|
|
|
|
$html .= '<h3>'.get_string('comments').'</h3>';
|
|
|
|
$html .= "<ul id='comment-list-$this->cid' class='comment-list'>";
|
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
$results = array();
|
|
|
|
$list = '';
|
2010-03-18 09:42:30 +00:00
|
|
|
|
2010-03-15 07:59:28 +00:00
|
|
|
foreach ($comments as $cmt) {
|
2010-03-18 09:42:30 +00:00
|
|
|
$list = '<li id="comment-'.$cmt->id.'-'.$this->cid.'">'.$this->print_comment($cmt, $nonjs).'</li>' . $list;
|
2010-03-15 07:59:28 +00:00
|
|
|
}
|
|
|
|
$html .= $list;
|
2010-03-18 09:42:30 +00:00
|
|
|
if ($nonjs) {
|
|
|
|
$html .= '</ul>';
|
2010-03-18 09:57:32 +00:00
|
|
|
$html .= $this->get_pagination($page);
|
2010-03-18 09:42:30 +00:00
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
$sesskey = sesskey();
|
2010-03-29 03:54:14 +00:00
|
|
|
$returnurl = $PAGE->url;
|
2010-03-15 07:59:28 +00:00
|
|
|
$strsubmit = get_string('submit');
|
2010-03-18 09:42:30 +00:00
|
|
|
if ($nonjs) {
|
2010-03-15 07:59:28 +00:00
|
|
|
$html .= <<<EOD
|
|
|
|
<form method="POST" action="{$CFG->wwwroot}/comment/comment_post.php">
|
2010-03-18 09:42:30 +00:00
|
|
|
<textarea name="content" rows="2"></textarea>
|
2010-03-15 07:59:28 +00:00
|
|
|
<input type="hidden" name="contextid" value="$this->contextid" />
|
|
|
|
<input type="hidden" name="action" value="add" />
|
|
|
|
<input type="hidden" name="area" value="$this->commentarea" />
|
2010-08-25 07:47:22 +00:00
|
|
|
<input type="hidden" name="component" value="$this->component" />
|
2010-03-15 07:59:28 +00:00
|
|
|
<input type="hidden" name="itemid" value="$this->itemid" />
|
2010-04-09 09:03:51 +00:00
|
|
|
<input type="hidden" name="courseid" value="{$this->courseid}" />
|
2010-03-15 07:59:28 +00:00
|
|
|
<input type="hidden" name="sesskey" value="{$sesskey}" />
|
|
|
|
<input type="hidden" name="returnurl" value="{$returnurl}" />
|
|
|
|
<input type="submit" value="{$strsubmit}" />
|
|
|
|
</form>
|
|
|
|
EOD;
|
2010-03-18 09:42:30 +00:00
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
if ($return) {
|
|
|
|
return $html;
|
|
|
|
} else {
|
|
|
|
echo $html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-18 09:42:30 +00:00
|
|
|
public function print_comment($cmt, $nonjs = true) {
|
|
|
|
global $OUTPUT;
|
2010-03-15 07:59:28 +00:00
|
|
|
$patterns = array();
|
|
|
|
$replacements = array();
|
|
|
|
|
2010-03-18 09:42:30 +00:00
|
|
|
if (!empty($cmt->delete) && empty($nonjs)) {
|
2010-04-07 09:09:32 +00:00
|
|
|
$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;
|
2010-03-18 09:42:30 +00:00
|
|
|
// add the button
|
|
|
|
}
|
2010-03-15 07:59:28 +00:00
|
|
|
$patterns[] = '___picture___';
|
|
|
|
$patterns[] = '___name___';
|
|
|
|
$patterns[] = '___content___';
|
|
|
|
$patterns[] = '___time___';
|
|
|
|
$replacements[] = $cmt->avatar;
|
2010-07-12 08:49:11 +00:00
|
|
|
$replacements[] = html_writer::link($cmt->profileurl, $cmt->fullname);
|
2010-03-15 07:59:28 +00:00
|
|
|
$replacements[] = $cmt->content;
|
|
|
|
$replacements[] = userdate($cmt->timecreated, get_string('strftimerecent', 'langconfig'));
|
|
|
|
|
|
|
|
// use html template to format a single comment.
|
|
|
|
return str_replace($patterns, $replacements, $this->template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class comment_exception extends moodle_exception {
|
|
|
|
public $message;
|
|
|
|
function __construct($errorcode) {
|
|
|
|
$this->errorcode = $errorcode;
|
|
|
|
$this->message = get_string($errorcode, 'error');
|
2009-07-24 02:44:44 +00:00
|
|
|
}
|
|
|
|
}
|