MDL-40266 improve emulate_bound_params() for mysqli
[moodle.git] / comment / locallib.php
CommitLineData
36051c9e 1<?php
24048d09
DC
2
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.
14//
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/>.
17
35453657
DC
18/**
19 * Functions and classes for comments management
20 *
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
24 */
25defined('MOODLE_INTERNAL') || die();
26
36051c9e
DC
27/**
28 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
29 *
35453657
DC
30 * @package core
31 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
36051c9e
DC
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class comment_manager {
c1951ea9 35
35453657 36 /** @var int The number of comments to display per page */
34e20eb4 37 private $perpage;
c1951ea9
DC
38
39 /**
40 * Constructs the comment_manage object
41 */
42 public function __construct() {
34e20eb4
DC
43 global $CFG;
44 $this->perpage = $CFG->commentsperpage;
45 }
36051c9e
DC
46
47 /**
48 * Return comments by pages
c1951ea9
DC
49 *
50 * @global moodle_database $DB
36051c9e 51 * @param int $page
c1951ea9 52 * @return array An array of comments
36051c9e
DC
53 */
54 function get_comments($page) {
c1951ea9
DC
55 global $DB;
56
36051c9e
DC
57 if ($page == 0) {
58 $start = 0;
59 } else {
c1951ea9 60 $start = $page * $this->perpage;
36051c9e 61 }
36051c9e 62 $comments = array();
c1951ea9
DC
63
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);
367a75fa 70 $formatoptions = array('overflowdiv' => true);
c1951ea9
DC
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;
36051c9e 82 }
c1951ea9 83 $rs->close();
36051c9e
DC
84
85 return $comments;
86 }
87
c1951ea9
DC
88 /**
89 * Records the course object
90 *
91 * @global moodle_page $PAGE
92 * @global moodle_database $DB
93 * @param int $courseid
c1951ea9 94 */
05fe3ab5 95 private function setup_course($courseid) {
1d51d1e0 96 global $PAGE, $DB;
437637a1 97 if (!empty($this->course) && $this->course->id == $courseid) {
36051c9e
DC
98 // already set, stop
99 return;
100 }
1d51d1e0
DC
101 if ($courseid == $PAGE->course->id) {
102 $this->course = $PAGE->course;
c1951ea9 103 } else if (!$this->course = $DB->get_record('course', array('id' => $courseid))) {
36051c9e
DC
104 $this->course = null;
105 }
106 }
107
c1951ea9
DC
108 /**
109 * Sets up the module or block information for a comment
110 *
111 * @global moodle_database $DB
112 * @param stdClass $comment
113 * @return bool
114 */
05fe3ab5 115 private function setup_plugin($comment) {
36051c9e
DC
116 global $DB;
117 $this->context = get_context_instance_by_id($comment->contextid);
c1951ea9
DC
118 if (!$this->context) {
119 return false;
36051c9e 120 }
c1951ea9
DC
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;
128 }
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;
36051c9e 137 }
c1951ea9 138 return true;
36051c9e
DC
139 }
140
141 /**
142 * Print comments
143 * @param int $page
35453657 144 * @return bool return false if no comments available
36051c9e 145 */
c1951ea9
DC
146 public function print_comments($page = 0) {
147 global $OUTPUT, $CFG, $OUTPUT, $DB;
148
149 $count = $DB->count_records('comments');
36051c9e 150 $comments = $this->get_comments($page);
c1951ea9
DC
151 if (count($comments) == 0) {
152 echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
153 return false;
154 }
155
36051c9e 156 $table = new html_table();
c1951ea9
DC
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')
162 );
36051c9e 163 $table->align = array ('left', 'left', 'left', 'left');
f36b47ef 164 $table->attributes = array('class'=>'generaltable commentstable');
36051c9e 165 $table->data = array();
c1951ea9
DC
166
167 $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey()));
36051c9e 168 foreach ($comments as $c) {
05fe3ab5 169 $this->setup_plugin($c);
36051c9e 170 if (!empty($this->plugintype)) {
c1951ea9 171 $context_url = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'url', array($c));
36051c9e 172 }
34e20eb4 173 $checkbox = html_writer::checkbox('comments', $c->id, false);
c1951ea9 174 $action = html_writer::link(new moodle_url($link, array('commentid' => $c->id)), get_string('delete'));
34e20eb4 175 if (!empty($context_url)) {
c1951ea9 176 $action .= html_writer::empty_tag('br');
34e20eb4 177 $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
36051c9e 178 }
2be0842e 179 $table->data[] = array($checkbox, $c->fullname, $c->content, $action);
36051c9e 180 }
16be8974 181 echo html_writer::table($table);
36051c9e 182 echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php');
c1951ea9 183 return true;
36051c9e
DC
184 }
185
186 /**
c1951ea9
DC
187 * Delete a comment
188 *
36051c9e 189 * @param int $commentid
c1951ea9 190 * @return bool
36051c9e
DC
191 */
192 public function delete_comment($commentid) {
193 global $DB;
c1951ea9
DC
194 if ($DB->record_exists('comments', array('id' => $commentid))) {
195 $DB->delete_records('comments', array('id' => $commentid));
b9a689e8 196 return true;
36051c9e
DC
197 }
198 return false;
199 }
200 /**
c1951ea9
DC
201 * Delete comments
202 *
203 * @param string $list A list of comment ids separated by hyphens
204 * @return bool
36051c9e
DC
205 */
206 public function delete_comments($list) {
207 global $DB;
208 $ids = explode('-', $list);
209 foreach ($ids as $id) {
c1951ea9
DC
210 $id = (int)$id;
211 if ($DB->record_exists('comments', array('id' => $id))) {
212 $DB->delete_records('comments', array('id' => $id));
36051c9e
DC
213 }
214 }
215 return true;
216 }
217}