"MDL-19118, add comments counter"

This commit is contained in:
dongsheng 2009-10-02 06:22:17 +00:00
parent 0e2afc47d8
commit b9f6ca7052
3 changed files with 48 additions and 10 deletions

View File

@ -45,6 +45,8 @@ function get_comments(client_id, area, itemid, page) {
if (!comment_check_response(ret)) {
return;
}
var linktext = document.getElementById('comment-link-text-'+ret.client_id);
linktext.innerHTML = mstr.moodle.comments + ' ('+ret.count+')';
var container = document.getElementById('comment-list-'+ret.client_id);
var pagination = document.getElementById('comment-pagination-'+ret.client_id);
if (ret.pagination) {
@ -76,6 +78,8 @@ function post_comment(cid) {
var result = cmt_replace(cid,[resp], true);
container.innerHTML += result.html;
var ids = result.ids;
var linktext = document.getElementById('comment-link-text-'+resp.client_id);
linktext.innerHTML = mstr.moodle.comments + ' ('+resp.count+')';
for(var i in ids) {
var attributes = {
color: { to: '#06e' },

View File

@ -69,6 +69,7 @@ if (!empty($client_id)) {
switch ($action) {
case 'add':
$cmt = $comment->add($content);
$cmt->count = $comment->count();
if (!empty($cmt) && is_object($cmt)) {
$cmt->client_id = $client_id;
echo json_encode($cmt);
@ -100,6 +101,7 @@ default:
$ret = array();
$comments = $comment->get_comments($page);
$ret['list'] = $comments;
$ret['count'] = $comment->count();
$ret['pagination'] = $comment->get_pagination($page);
$ret['client_id'] = $client_id;
echo json_encode($ret);

View File

@ -89,7 +89,7 @@ class comment {
* @param object $options
*/
public function __construct($options) {
global $CFG;
global $CFG, $DB;
$this->viewcap = false;
$this->postcap = false;
if (!empty($options->client_id)) {
@ -156,6 +156,16 @@ EOD;
$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);
}
@ -181,6 +191,7 @@ EOD;
$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) {
@ -290,15 +301,14 @@ EOD;
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;
@ -337,6 +347,7 @@ EOD;
$html .= <<<EOD
</div>
</div>
EOD;
} else {
$html = '';
@ -408,10 +419,18 @@ EOD;
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 '';
@ -440,7 +459,7 @@ EOD;
* @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;
@ -451,7 +470,7 @@ EOD;
$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;
@ -478,6 +497,19 @@ EOD;
}
}
/**
* 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