From b9f6ca7052552f05ecef5775a35aa3f4e0d0613d Mon Sep 17 00:00:00 2001 From: dongsheng Date: Fri, 2 Oct 2009 06:22:17 +0000 Subject: [PATCH] "MDL-19118, add comments counter" --- comment/comment.js | 4 ++++ comment/comment_ajax.php | 2 ++ lib/commentlib.php | 52 ++++++++++++++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/comment/comment.js b/comment/comment.js index 0ee1edaa44..13430641d3 100644 --- a/comment/comment.js +++ b/comment/comment.js @@ -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' }, diff --git a/comment/comment_ajax.php b/comment/comment_ajax.php index 93c9bb232c..87f36bbf72 100644 --- a/comment/comment_ajax.php +++ b/comment/comment_ajax.php @@ -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); diff --git a/lib/commentlib.php b/lib/commentlib.php index 32e64df7db..e5c3ff9df8 100644 --- a/lib/commentlib.php +++ b/lib/commentlib.php @@ -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 = << {$this->linktext} -{$this->linktext} +{$this->linktext} {$this->count}
-
-
    -
-
+
    +
EOD; @@ -337,6 +347,7 @@ EOD; $html .= << +
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 -- 2.39.5