* @param object $options
*/
public function __construct($options) {
- global $CFG;
+ global $CFG, $DB;
$this->viewcap = false;
$this->postcap = false;
if (!empty($options->client_id)) {
$courseid = SITEID;
$this->_setup_course($courseid);
}
+ if (!empty($options->showcount)) {
+ $count = $this->count();
+ if (empty($count)) {
+ $this->count = '';
+ } else {
+ $this->count = '('.$count.')';
+ }
+ } else {
+ $this->count = '';
+ }
unset($options);
}
$PAGE->requires->js('comment/comment.js')->in_head();
$PAGE->requires->string_for_js('addcomment', 'moodle');
$PAGE->requires->string_for_js('deletecomment', 'moodle');
+ $PAGE->requires->string_for_js('comments', 'moodle');
}
private function _setup_course($courseid) {
if (!empty($this->viewcap)) {
// print commenting icon and tooltip
$html = <<<EOD
+<div style="text-align:left">
<a id="comment-link-{$this->cid}" onclick="return view_comments('{$this->cid}', '{$this->commentarea}', '{$this->itemid}', 0)" href="{$this->link}">
<img id="comment-img-{$this->cid}" src="{$CFG->wwwroot}/pix/t/collapsed.png" alt="{$this->linktext}" title="{$this->linktext}" />
-<span>{$this->linktext}</span>
+<span id="comment-link-text-{$this->cid}">{$this->linktext} {$this->count}</span>
</a>
<div id="comment-ctrl-{$this->cid}" class="comment-ctrl">
- <div class="comment-list-wrapper">
- <ul id="comment-list-{$this->cid}" class="comment-list">
- </ul>
- </div>
+ <ul id="comment-list-{$this->cid}" class="comment-list">
+ </ul>
<div id="comment-pagination-{$this->cid}" class="comment-pagination"></div>
EOD;
$html .= <<<EOD
</div>
+</div>
EOD;
} else {
$html = '';
return $comments;
}
+ public function count() {
+ global $DB;
+ 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;
- if (!$count = $DB->count_records_sql("SELECT COUNT(*) from {comments} c, {user} u WHERE u.id=c.userid AND c.contextid=? AND c.commentarea=? AND c.itemid=?", array($this->contextid, $this->commentarea, $this->itemid))) {
- }
+ $count = $this->count();
$pages = (int)ceil($count/$CFG->commentsperpage);
if ($pages == 1 || $pages == 0) {
return '';
* @param string $content
* @return mixed
*/
- public function add($content) {
+ public function add($content, $format = FORMAT_MOODLE) {
global $CFG, $DB, $USER, $OUTPUT;
if (empty($this->postcap)) {
return COMMENT_ERROR_INSUFFICIENT_CAPS;
$newcmt->commentarea = $this->commentarea;
$newcmt->itemid = $this->itemid;
$newcmt->content = $content;
- $newcmt->format = FORMAT_MOODLE;
+ $newcmt->format = $format;
$newcmt->userid = $USER->id;
$newcmt->timecreated = $now;
}
}
+ /**
+ * delete by context, commentarea and itemid
+ *
+ */
+ public function delete_comments() {
+ global $DB;
+ return $DB->delete_records('comments', array(
+ 'contextid'=>$this->context->id,
+ 'commentarea'=>$this->commentarea,
+ 'itemid'=>$this->itemid)
+ );
+ }
+
/**
* Delete a comment
* @param int $commentid