3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
22 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 class comment_manager
{
28 * The number of comments to display per page
34 * Constructs the comment_manage object
36 public function __construct() {
38 $this->perpage
= $CFG->commentsperpage
;
42 * Return comments by pages
44 * @global moodle_database $DB
46 * @return array An array of comments
48 function get_comments($page) {
54 $start = $page * $this->perpage
;
58 $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.firstname, u.lastname, c.timecreated
62 ORDER BY c.timecreated ASC";
63 $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage
);
64 $formatoptions = array('overflowdiv' => true);
65 foreach ($rs as $item) {
66 // Set calculated fields
67 $item->fullname
= fullname($item);
68 $item->time
= userdate($item->timecreated
);
69 $item->content
= format_text($item->content
, FORMAT_MOODLE
, $formatoptions);
70 // Unset fields not related to the comment
71 unset($item->firstname
);
72 unset($item->lastname
);
73 unset($item->timecreated
);
83 * Records the course object
85 * @global moodle_page $PAGE
86 * @global moodle_database $DB
87 * @param int $courseid
90 private function setup_course($courseid) {
92 if (!empty($this->course
)) {
96 if ($courseid == $PAGE->course
->id
) {
97 $this->course
= $PAGE->course
;
98 } else if (!$this->course
= $DB->get_record('course', array('id' => $courseid))) {
104 * Sets up the module or block information for a comment
106 * @global moodle_database $DB
107 * @param stdClass $comment
110 private function setup_plugin($comment) {
112 $this->context
= get_context_instance_by_id($comment->contextid
);
113 if (!$this->context
) {
116 switch ($this->context
->contextlevel
) {
118 if ($block = $DB->get_record('block_instances', array('id' => $this->context
->instanceid
))) {
119 $this->plugintype
= 'block';
120 $this->pluginname
= $block->blockname
;
126 $this->plugintype
= 'mod';
127 $this->cm
= get_coursemodule_from_id('', $this->context
->instanceid
);
128 $this->setup_course($this->cm
->course
);
129 $this->modinfo
= get_fast_modinfo($this->course
);
130 $this->pluginname
= $this->modinfo
->cms
[$this->cm
->id
]->modname
;
139 * @return boolean return false if no comments available
141 public function print_comments($page = 0) {
142 global $OUTPUT, $CFG, $OUTPUT, $DB;
144 $count = $DB->count_records('comments');
145 $comments = $this->get_comments($page);
146 if (count($comments) == 0) {
147 echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
151 $table = new html_table();
152 $table->head
= array (
153 html_writer
::checkbox('selectall', '', false, get_string('selectall'), array('id'=>'comment_select_all', 'class'=>'comment-report-selectall')),
154 get_string('author', 'search'),
155 get_string('content'),
158 $table->align
= array ('left', 'left', 'left', 'left');
159 $table->attributes
= array('class'=>'generaltable commentstable');
160 $table->data
= array();
162 $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey()));
163 foreach ($comments as $c) {
164 $this->setup_plugin($c);
165 if (!empty($this->plugintype
)) {
166 $context_url = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'url', array($c));
168 $checkbox = html_writer
::checkbox('comments', $c->id
, false);
169 $action = html_writer
::link(new moodle_url($link, array('commentid' => $c->id
)), get_string('delete'));
170 if (!empty($context_url)) {
171 $action .= html_writer
::empty_tag('br');
172 $action .= html_writer
::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
174 $table->data
[] = array($checkbox, $c->fullname
, $c->content
, $action);
176 echo html_writer
::table($table);
177 echo $OUTPUT->paging_bar($count, $page, $this->perpage
, $CFG->wwwroot
.'/comment/index.php');
184 * @param int $commentid
187 public function delete_comment($commentid) {
189 if ($DB->record_exists('comments', array('id' => $commentid))) {
190 $DB->delete_records('comments', array('id' => $commentid));
198 * @param string $list A list of comment ids separated by hyphens
201 public function delete_comments($list) {
203 $ids = explode('-', $list);
204 foreach ($ids as $id) {
206 if ($DB->record_exists('comments', array('id' => $id))) {
207 $DB->delete_records('comments', array('id' => $id));