Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / comment / locallib.php
blobd56d954aef3fcb90b18703443b702de3bde370ff
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Functions and classes for comments management
21 * @package core
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();
27 /**
28 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
30 * @package core
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 */
37 private $perpage;
39 /**
40 * Constructs the comment_manage object
42 public function __construct() {
43 global $CFG;
44 $this->perpage = $CFG->commentsperpage;
47 /**
48 * Return comments by pages
50 * @global moodle_database $DB
51 * @param int $page
52 * @return array An array of comments
54 function get_comments($page) {
55 global $DB;
57 if ($page == 0) {
58 $start = 0;
59 } else {
60 $start = $page * $this->perpage;
62 $comments = array();
64 $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.firstname, u.lastname, c.timecreated
65 FROM {comments} c
66 JOIN {user} u
67 ON u.id=c.userid
68 ORDER BY c.timecreated ASC";
69 $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage);
70 $formatoptions = array('overflowdiv' => true);
71 foreach ($rs as $item) {
72 // Set calculated fields
73 $item->fullname = fullname($item);
74 $item->time = userdate($item->timecreated);
75 $item->content = format_text($item->content, FORMAT_MOODLE, $formatoptions);
76 // Unset fields not related to the comment
77 unset($item->firstname);
78 unset($item->lastname);
79 unset($item->timecreated);
80 // Record the comment
81 $comments[] = $item;
83 $rs->close();
85 return $comments;
88 /**
89 * Records the course object
91 * @global moodle_page $PAGE
92 * @global moodle_database $DB
93 * @param int $courseid
95 private function setup_course($courseid) {
96 global $PAGE, $DB;
97 if (!empty($this->course) && $this->course->id == $courseid) {
98 // already set, stop
99 return;
101 if ($courseid == $PAGE->course->id) {
102 $this->course = $PAGE->course;
103 } else if (!$this->course = $DB->get_record('course', array('id' => $courseid))) {
104 $this->course = null;
109 * Sets up the module or block information for a comment
111 * @global moodle_database $DB
112 * @param stdClass $comment
113 * @return bool
115 private function setup_plugin($comment) {
116 global $DB;
117 $this->context = context::instance_by_id($comment->contextid, IGNORE_MISSING);
118 if (!$this->context) {
119 return false;
121 switch ($this->context->contextlevel) {
122 case CONTEXT_BLOCK:
123 if ($block = $DB->get_record('block_instances', array('id' => $this->context->instanceid))) {
124 $this->plugintype = 'block';
125 $this->pluginname = $block->blockname;
126 } else {
127 return false;
129 break;
130 case CONTEXT_MODULE:
131 $this->plugintype = 'mod';
132 $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
133 $this->setup_course($this->cm->course);
134 $this->modinfo = get_fast_modinfo($this->course);
135 $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
136 break;
138 return true;
142 * Print comments
143 * @param int $page
144 * @return bool return false if no comments available
146 public function print_comments($page = 0) {
147 global $OUTPUT, $CFG, $OUTPUT, $DB;
149 $count = $DB->count_records('comments');
150 $comments = $this->get_comments($page);
151 if (count($comments) == 0) {
152 echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
153 return false;
156 $table = new html_table();
157 $table->head = array (
158 html_writer::checkbox('selectall', '', false, get_string('selectall'), array('id'=>'comment_select_all', 'class'=>'comment-report-selectall')),
159 get_string('author', 'search'),
160 get_string('content'),
161 get_string('action')
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 $this->setup_plugin($c);
171 if (!empty($this->plugintype)) {
172 $context_url = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'url', array($c));
174 $checkbox = html_writer::checkbox('comments', $c->id, false);
175 $action = html_writer::link(new moodle_url($link, array('commentid' => $c->id)), get_string('delete'));
176 if (!empty($context_url)) {
177 $action .= html_writer::empty_tag('br');
178 $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
180 $table->data[] = array($checkbox, $c->fullname, $c->content, $action);
182 echo html_writer::table($table);
183 echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php');
184 return true;
188 * Delete a comment
190 * @param int $commentid
191 * @return bool
193 public function delete_comment($commentid) {
194 global $DB;
195 if ($DB->record_exists('comments', array('id' => $commentid))) {
196 $DB->delete_records('comments', array('id' => $commentid));
197 return true;
199 return false;
202 * Delete comments
204 * @param string $list A list of comment ids separated by hyphens
205 * @return bool
207 public function delete_comments($list) {
208 global $DB;
209 $ids = explode('-', $list);
210 foreach ($ids as $id) {
211 $id = (int)$id;
212 if ($DB->record_exists('comments', array('id' => $id))) {
213 $DB->delete_records('comments', array('id' => $id));
216 return true;