Merge branch 'MDL-29569' of git://github.com/rwijaya/moodle
[moodle.git] / comment / locallib.php
blobfd62b619347ae924ae1caa26b84d1f2727dfa5a5
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 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
21 * @package comment
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 {
27 /**
28 * The number of comments to display per page
29 * @var int
31 private $perpage;
33 /**
34 * Constructs the comment_manage object
36 public function __construct() {
37 global $CFG;
38 $this->perpage = $CFG->commentsperpage;
41 /**
42 * Return comments by pages
44 * @global moodle_database $DB
45 * @param int $page
46 * @return array An array of comments
48 function get_comments($page) {
49 global $DB;
51 if ($page == 0) {
52 $start = 0;
53 } else {
54 $start = $page * $this->perpage;
56 $comments = array();
58 $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.firstname, u.lastname, c.timecreated
59 FROM {comments} c
60 JOIN {user} u
61 ON u.id=c.userid
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);
74 // Record the comment
75 $comments[] = $item;
77 $rs->close();
79 return $comments;
82 /**
83 * Records the course object
85 * @global moodle_page $PAGE
86 * @global moodle_database $DB
87 * @param int $courseid
88 * @return void
90 private function setup_course($courseid) {
91 global $PAGE, $DB;
92 if (!empty($this->course)) {
93 // already set, stop
94 return;
96 if ($courseid == $PAGE->course->id) {
97 $this->course = $PAGE->course;
98 } else if (!$this->course = $DB->get_record('course', array('id' => $courseid))) {
99 $this->course = null;
104 * Sets up the module or block information for a comment
106 * @global moodle_database $DB
107 * @param stdClass $comment
108 * @return bool
110 private function setup_plugin($comment) {
111 global $DB;
112 $this->context = get_context_instance_by_id($comment->contextid);
113 if (!$this->context) {
114 return false;
116 switch ($this->context->contextlevel) {
117 case CONTEXT_BLOCK:
118 if ($block = $DB->get_record('block_instances', array('id' => $this->context->instanceid))) {
119 $this->plugintype = 'block';
120 $this->pluginname = $block->blockname;
121 } else {
122 return false;
124 break;
125 case CONTEXT_MODULE:
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;
131 break;
133 return true;
137 * Print comments
138 * @param int $page
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'));
148 return false;
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'),
156 get_string('action')
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');
178 return true;
182 * Delete a comment
184 * @param int $commentid
185 * @return bool
187 public function delete_comment($commentid) {
188 global $DB;
189 if ($DB->record_exists('comments', array('id' => $commentid))) {
190 $DB->delete_records('comments', array('id' => $commentid));
191 return true;
193 return false;
196 * Delete comments
198 * @param string $list A list of comment ids separated by hyphens
199 * @return bool
201 public function delete_comments($list) {
202 global $DB;
203 $ids = explode('-', $list);
204 foreach ($ids as $id) {
205 $id = (int)$id;
206 if ($DB->record_exists('comments', array('id' => $id))) {
207 $DB->delete_records('comments', array('id' => $id));
210 return true;