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)
21 * @package core_comment
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 class comment_manager
{
27 /** @var int The number of comments to display per page */
31 * Constructs the comment_manage object
33 public function __construct() {
35 $this->perpage
= $CFG->commentsperpage
;
39 * Return comments by pages
41 * @global moodle_database $DB
43 * @return array An array of comments
45 function get_comments($page) {
51 $start = $page * $this->perpage
;
55 $userfieldsapi = \core_user\fields
::for_name();
56 $usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects
;
57 $sql = "SELECT c.id, c.contextid, c.itemid, c.component, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
61 ORDER BY c.timecreated ASC";
62 $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage
);
63 $formatoptions = array('overflowdiv' => true, 'blanktarget' => true);
64 foreach ($rs as $item) {
65 // Set calculated fields
66 $item->fullname
= fullname($item);
67 $item->time
= userdate($item->timecreated
);
68 $item->content
= format_text($item->content
, FORMAT_MOODLE
, $formatoptions);
69 // Unset fields not related to the comment
70 foreach (\core_user\fields
::get_name_fields() as $namefield) {
71 unset($item->$namefield);
73 unset($item->timecreated
);
83 * Records the course object
85 * @global moodle_page $PAGE
86 * @global moodle_database $DB
87 * @param int $courseid
89 private function setup_course($courseid) {
91 if (!empty($this->course
) && $this->course
->id
== $courseid) {
95 if ($courseid == $PAGE->course
->id
) {
96 $this->course
= $PAGE->course
;
97 } else if (!$this->course
= $DB->get_record('course', array('id' => $courseid))) {
103 * Sets up the module or block information for a comment
105 * @global moodle_database $DB
106 * @param stdClass $comment
109 private function setup_plugin($comment) {
111 $this->context
= context
::instance_by_id($comment->contextid
, IGNORE_MISSING
);
112 if (!$this->context
) {
115 switch ($this->context
->contextlevel
) {
117 if ($block = $DB->get_record('block_instances', array('id' => $this->context
->instanceid
))) {
118 $this->plugintype
= 'block';
119 $this->pluginname
= $block->blockname
;
125 $this->plugintype
= 'mod';
126 $this->cm
= get_coursemodule_from_id('', $this->context
->instanceid
);
127 $this->setup_course($this->cm
->course
);
128 $this->modinfo
= get_fast_modinfo($this->course
);
129 $this->pluginname
= $this->modinfo
->cms
[$this->cm
->id
]->modname
;
138 * @return bool return false if no comments available
140 * @deprecated since Moodle 4.2 - please do not use this function any more
142 public function print_comments($page = 0) {
143 global $OUTPUT, $CFG, $OUTPUT, $DB;
145 debugging('The function ' . __FUNCTION__
. '() is deprecated, please do not use it any more. ' .
146 'See \'comments\' system report class for replacement', DEBUG_DEVELOPER
);
148 $count = $DB->count_records('comments');
149 $comments = $this->get_comments($page);
150 if (count($comments) == 0) {
151 echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
155 $table = new html_table();
156 $table->head
= array (
157 html_writer
::checkbox('selectall', '', false, get_string('selectall'), array('id' => 'comment_select_all',
159 get_string('author', 'search'),
160 get_string('content'),
163 $table->colclasses
= array ('leftalign', 'leftalign', 'leftalign', 'leftalign');
164 $table->attributes
= array('class'=>'admintable generaltable');
165 $table->id
= 'commentstable';
166 $table->data
= array();
168 $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey()));
169 foreach ($comments as $c) {
170 $userdata = html_writer
::link(new moodle_url('/user/profile.php', ['id' => $c->userid
]), $c->fullname
);
171 $this->setup_plugin($c);
172 if (!empty($this->plugintype
)) {
173 $context_url = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'url', array($c));
175 $checkbox = html_writer
::checkbox('comments', $c->id
, false);
176 $action = html_writer
::link(new moodle_url($link, array('commentid' => $c->id
)), get_string('delete'));
177 if (!empty($context_url)) {
178 $action .= html_writer
::empty_tag('br');
179 $action .= html_writer
::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
181 $table->data
[] = array($checkbox, $userdata, $c->content
, $action);
183 echo html_writer
::table($table);
184 echo $OUTPUT->paging_bar($count, $page, $this->perpage
, $CFG->wwwroot
.'/comment/index.php');
191 * @param int $commentid
194 public function delete_comment($commentid) {
196 if ($DB->record_exists('comments', array('id' => $commentid))) {
197 $DB->delete_records('comments', array('id' => $commentid));
205 * @param string $list A list of comment ids separated by hyphens
208 public function delete_comments($list) {
210 $ids = explode('-', $list);
211 foreach ($ids as $id) {
213 if ($DB->record_exists('comments', array('id' => $id))) {
214 $DB->delete_records('comments', array('id' => $id));
221 * Get comments created since a given time.
223 * @param stdClass $course course object
224 * @param stdClass $context context object
225 * @param string $component component name
226 * @param int $since the time to check
227 * @param stdClass $cm course module object
228 * @return array list of comments db records since the given timelimit
231 public function get_component_comments_since($course, $context, $component, $since, $cm = null) {
234 $commentssince = array();
235 $where = 'contextid = ? AND component = ? AND timecreated > ?';
236 $comments = $DB->get_records_select('comments', $where, array($context->id
, $component, $since));
237 // Check item by item if we have permissions.
238 $managersviewstatus = array();
239 foreach ($comments as $comment) {
240 // Check if the manager for the item is cached.
241 if (!isset($managersviewstatus[$comment->commentarea
]) or
242 !isset($managersviewstatus[$comment->commentarea
][$comment->itemid
])) {
244 $args = new stdClass
;
245 $args->area
= $comment->commentarea
;
246 $args->itemid
= $comment->itemid
;
247 $args->context
= $context;
248 $args->course
= $course;
249 $args->client_id
= 0;
250 $args->component
= $component;
255 $manager = new comment($args);
256 $managersviewstatus[$comment->commentarea
][$comment->itemid
] = $manager->can_view();
259 if ($managersviewstatus[$comment->commentarea
][$comment->itemid
]) {
260 $commentssince[$comment->id
] = $comment;
263 return $commentssince;