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 * Functions and classes for comments management
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
31 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class comment_manager
{
36 /** @var int The number of comments to display per page */
40 * Constructs the comment_manage object
42 public function __construct() {
44 $this->perpage
= $CFG->commentsperpage
;
48 * Return comments by pages
50 * @global moodle_database $DB
52 * @return array An array of comments
54 function get_comments($page) {
60 $start = $page * $this->perpage
;
64 $usernamefields = get_all_user_name_fields(true, 'u');
65 $sql = "SELECT c.id, c.contextid, c.itemid, c.component, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
69 ORDER BY c.timecreated ASC";
70 $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage
);
71 $formatoptions = array('overflowdiv' => true, 'blanktarget' => true);
72 foreach ($rs as $item) {
73 // Set calculated fields
74 $item->fullname
= fullname($item);
75 $item->time
= userdate($item->timecreated
);
76 $item->content
= format_text($item->content
, FORMAT_MOODLE
, $formatoptions);
77 // Unset fields not related to the comment
78 foreach (get_all_user_name_fields() as $namefield) {
79 unset($item->$namefield);
81 unset($item->timecreated
);
91 * Records the course object
93 * @global moodle_page $PAGE
94 * @global moodle_database $DB
95 * @param int $courseid
97 private function setup_course($courseid) {
99 if (!empty($this->course
) && $this->course
->id
== $courseid) {
103 if ($courseid == $PAGE->course
->id
) {
104 $this->course
= $PAGE->course
;
105 } else if (!$this->course
= $DB->get_record('course', array('id' => $courseid))) {
106 $this->course
= null;
111 * Sets up the module or block information for a comment
113 * @global moodle_database $DB
114 * @param stdClass $comment
117 private function setup_plugin($comment) {
119 $this->context
= context
::instance_by_id($comment->contextid
, IGNORE_MISSING
);
120 if (!$this->context
) {
123 switch ($this->context
->contextlevel
) {
125 if ($block = $DB->get_record('block_instances', array('id' => $this->context
->instanceid
))) {
126 $this->plugintype
= 'block';
127 $this->pluginname
= $block->blockname
;
133 $this->plugintype
= 'mod';
134 $this->cm
= get_coursemodule_from_id('', $this->context
->instanceid
);
135 $this->setup_course($this->cm
->course
);
136 $this->modinfo
= get_fast_modinfo($this->course
);
137 $this->pluginname
= $this->modinfo
->cms
[$this->cm
->id
]->modname
;
146 * @return bool return false if no comments available
148 public function print_comments($page = 0) {
149 global $OUTPUT, $CFG, $OUTPUT, $DB;
151 $count = $DB->count_records('comments');
152 $comments = $this->get_comments($page);
153 if (count($comments) == 0) {
154 echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
158 $table = new html_table();
159 $table->head
= array (
160 html_writer
::checkbox('selectall', '', false, get_string('selectall'), array('id' => 'comment_select_all',
162 get_string('author', 'search'),
163 get_string('content'),
166 $table->colclasses
= array ('leftalign', 'leftalign', 'leftalign', 'leftalign');
167 $table->attributes
= array('class'=>'admintable generaltable');
168 $table->id
= 'commentstable';
169 $table->data
= array();
171 $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey()));
172 foreach ($comments as $c) {
173 $userdata = html_writer
::link(new moodle_url('/user/profile.php', ['id' => $c->userid
]), $c->fullname
);
174 $this->setup_plugin($c);
175 if (!empty($this->plugintype
)) {
176 $context_url = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'url', array($c));
178 $checkbox = html_writer
::checkbox('comments', $c->id
, false);
179 $action = html_writer
::link(new moodle_url($link, array('commentid' => $c->id
)), get_string('delete'));
180 if (!empty($context_url)) {
181 $action .= html_writer
::empty_tag('br');
182 $action .= html_writer
::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
184 $table->data
[] = array($checkbox, $userdata, $c->content
, $action);
186 echo html_writer
::table($table);
187 echo $OUTPUT->paging_bar($count, $page, $this->perpage
, $CFG->wwwroot
.'/comment/index.php');
194 * @param int $commentid
197 public function delete_comment($commentid) {
199 if ($DB->record_exists('comments', array('id' => $commentid))) {
200 $DB->delete_records('comments', array('id' => $commentid));
208 * @param string $list A list of comment ids separated by hyphens
211 public function delete_comments($list) {
213 $ids = explode('-', $list);
214 foreach ($ids as $id) {
216 if ($DB->record_exists('comments', array('id' => $id))) {
217 $DB->delete_records('comments', array('id' => $id));
224 * Get comments created since a given time.
226 * @param stdClass $course course object
227 * @param stdClass $context context object
228 * @param string $component component name
229 * @param int $since the time to check
230 * @param stdClass $cm course module object
231 * @return array list of comments db records since the given timelimit
234 public function get_component_comments_since($course, $context, $component, $since, $cm = null) {
237 $commentssince = array();
238 $where = 'contextid = ? AND component = ? AND timecreated > ?';
239 $comments = $DB->get_records_select('comments', $where, array($context->id
, $component, $since));
240 // Check item by item if we have permissions.
241 $managersviewstatus = array();
242 foreach ($comments as $comment) {
243 // Check if the manager for the item is cached.
244 if (!isset($managersviewstatus[$comment->commentarea
]) or
245 !isset($managersviewstatus[$comment->commentarea
][$comment->itemid
])) {
247 $args = new stdClass
;
248 $args->area
= $comment->commentarea
;
249 $args->itemid
= $comment->itemid
;
250 $args->context
= $context;
251 $args->course
= $course;
252 $args->client_id
= 0;
253 $args->component
= $component;
258 $manager = new comment($args);
259 $managersviewstatus[$comment->commentarea
][$comment->itemid
] = $manager->can_view();
262 if ($managersviewstatus[$comment->commentarea
][$comment->itemid
]) {
263 $commentssince[$comment->id
] = $comment;
266 return $commentssince;