"MDL-19118, improve comments management"
[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
36051c9e
DC
18/**
19 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
20 *
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
24 */
25class comment_manager {
34e20eb4
DC
26 private $perpage;
27 function __construct() {
28 global $CFG;
29 $this->perpage = $CFG->commentsperpage;
30 }
36051c9e
DC
31
32 /**
33 * Return comments by pages
34 * @param int $page
35 * @return mixed
36 */
37 function get_comments($page) {
38 global $DB, $CFG, $USER;
39 $params = array();
40 if ($page == 0) {
41 $start = 0;
42 } else {
43 $start = $page*$this->perpage;
44 }
45 $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.username, u.firstname, u.lastname, c.timecreated
46 FROM {comments} c, {user} u
47 WHERE u.id=c.userid ORDER BY c.timecreated ASC";
48
49 $comments = array();
50 if ($records = $DB->get_records_sql($sql, array(), $start, $this->perpage)) {
51 foreach ($records as $item) {
52 $item->username = fullname($item);
53 $item->time = userdate($item->timecreated);
54 $item->content = format_text($item->content);
55 $comments[] = $item;
56 unset($item->firstname);
57 unset($item->lastname);
58 unset($item->timecreated);
59 }
60 }
61
62 return $comments;
63 }
64
05fe3ab5 65 private function setup_course($courseid) {
1d51d1e0 66 global $PAGE, $DB;
36051c9e
DC
67 if (!empty($this->course)) {
68 // already set, stop
69 return;
70 }
1d51d1e0
DC
71 if ($courseid == $PAGE->course->id) {
72 $this->course = $PAGE->course;
36051c9e
DC
73 } else if (!$this->course = $DB->get_record('course', array('id'=>$courseid))) {
74 $this->course = null;
75 }
76 }
77
05fe3ab5 78 private function setup_plugin($comment) {
36051c9e
DC
79 global $DB;
80 $this->context = get_context_instance_by_id($comment->contextid);
81 if (!is_object($this->context)) {
82 return;
83 }
84 if ($this->context->contextlevel == CONTEXT_BLOCK) {
85 if ($block = $DB->get_record('block_instances', array('id'=>$this->context->instanceid))) {
86 $this->plugintype = 'block';
87 $this->pluginname = $block->blockname;
88 }
89 }
90 if ($this->context->contextlevel == CONTEXT_MODULE) {
91 $this->plugintype = 'mod';
92 $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
05fe3ab5 93 $this->setup_course($this->cm->course);
36051c9e
DC
94 $this->modinfo = get_fast_modinfo($this->course);
95 $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
96 }
97 }
98
99 /**
100 * Print comments
101 * @param int $page
102 */
103 function print_comments($page=0) {
104 global $CFG, $OUTPUT, $DB;
36051c9e
DC
105 $count = $DB->count_records_sql('SELECT COUNT(*) FROM {comments} c');
106 $comments = $this->get_comments($page);
107 $table = new html_table();
34e20eb4 108 $table->head = array (html_writer::checkbox('selectall', '', false, get_string('selectall'), array('id'=>'comment_select_all', 'class'=>'comment-report-selectall')), get_string('author', 'search'), get_string('content'), get_string('action'));
36051c9e
DC
109 $table->align = array ('left', 'left', 'left', 'left');
110 $table->width = "95%";
111 $table->data = array();
34e20eb4 112 $linkbase = $CFG->wwwroot.'/comment/index.php?action=delete&sesskey='.sesskey();
36051c9e 113 foreach ($comments as $c) {
34e20eb4 114 $link = $linkbase . '&commentid='. $c->id;
05fe3ab5 115 $this->setup_plugin($c);
36051c9e 116 if (!empty($this->plugintype)) {
34e20eb4 117 $context_url = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'url', array($c));
36051c9e 118 }
34e20eb4
DC
119 $checkbox = html_writer::checkbox('comments', $c->id, false);
120 $action = html_writer::link($link, get_string('delete'));
121 if (!empty($context_url)) {
122 $action .= html_writer::tag('br', null);
123 $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
36051c9e
DC
124 }
125 $table->data[] = array($checkbox, $c->username, $c->content, $action);
126 }
16be8974 127 echo html_writer::table($table);
36051c9e
DC
128 echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php');
129 }
130
131 /**
132 * delete a comment
133 * @param int $commentid
134 */
135 public function delete_comment($commentid) {
136 global $DB;
137 if ($comment = $DB->get_record('comments', array('id'=>$commentid))) {
b9a689e8
DC
138 $DB->delete_records('comments', array('id'=>$commentid));
139 return true;
36051c9e
DC
140 }
141 return false;
142 }
143 /**
144 * delete comments
145 * @param int $commentid
146 */
147 public function delete_comments($list) {
148 global $DB;
149 $ids = explode('-', $list);
150 foreach ($ids as $id) {
151 if (is_int((int)$id)) {
152 if ($comment = $DB->get_record('comments', array('id'=>$id))) {
153 $DB->delete_records('comments', array('id'=>$comment->id));
154 }
155 }
156 }
157 return true;
158 }
159}