locallib.php 5.97 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

18
19
20
21
/**
 * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
 *
 * @package   comment
22
 * @copyright  2010 Dongsheng Cai <dongsheng@moodle.com>
23
24
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class comment_manager {
26
27
28
29
30
    private $perpage;
    function __construct() {
        global $CFG;
        $this->perpage = $CFG->commentsperpage;
    }
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    /**
     * Return comments by pages
     * @param int $page
     * @return mixed
     */
    function get_comments($page) {
        global $DB, $CFG, $USER;
        $params = array();
        if ($page == 0) {
            $start = 0;
        } else {
            $start = $page*$this->perpage;
        }
45
        $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.firstname, u.lastname, c.timecreated
46
47
48
49
            FROM {comments} c, {user} u
            WHERE u.id=c.userid ORDER BY c.timecreated ASC";

        $comments = array();
50
        $formatoptions = array('overflowdiv' => true);
51
52
        if ($records = $DB->get_records_sql($sql, array(), $start, $this->perpage)) {
            foreach ($records as $item) {
53
                $item->fullname = fullname($item);
54
                $item->time = userdate($item->timecreated);
55
                $item->content = format_text($item->content, FORMAT_MOODLE, $formatoptions);
56
57
58
59
60
61
62
63
64
65
                $comments[] = $item;
                unset($item->firstname);
                unset($item->lastname);
                unset($item->timecreated);
            }
        }

        return $comments;
    }

66
    private function setup_course($courseid) {
67
        global $PAGE, $DB;
68
69
70
71
        if (!empty($this->course)) {
            // already set, stop
            return;
        }
72
73
        if ($courseid == $PAGE->course->id) {
            $this->course = $PAGE->course;
74
75
76
77
78
        } else if (!$this->course = $DB->get_record('course', array('id'=>$courseid))) {
            $this->course = null;
        }
    }

79
    private function setup_plugin($comment) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        global $DB;
        $this->context = get_context_instance_by_id($comment->contextid);
        if (!is_object($this->context)) {
            return;
        }
        if ($this->context->contextlevel == CONTEXT_BLOCK) {
            if ($block = $DB->get_record('block_instances', array('id'=>$this->context->instanceid))) {
                $this->plugintype = 'block';
                $this->pluginname = $block->blockname;
            }
        }
        if ($this->context->contextlevel == CONTEXT_MODULE) {
            $this->plugintype = 'mod';
            $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
94
            $this->setup_course($this->cm->course);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            $this->modinfo = get_fast_modinfo($this->course);
            $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
        }
    }

    /**
     * Print comments
     * @param int $page
     */
    function print_comments($page=0) {
        global $CFG, $OUTPUT, $DB;
        $count = $DB->count_records_sql('SELECT COUNT(*) FROM {comments} c');
        $comments = $this->get_comments($page);
        $table = new html_table();
109
        $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'));
110
        $table->align = array ('left', 'left', 'left', 'left');
111
        $table->attributes = array('class'=>'generaltable commentstable');
112
        $table->data = array();
113
        $linkbase = $CFG->wwwroot.'/comment/index.php?action=delete&sesskey='.sesskey();
114
        foreach ($comments as $c) {
115
            $link = $linkbase . '&commentid='. $c->id;
116
            $this->setup_plugin($c);
117
            if (!empty($this->plugintype)) {
118
                $context_url = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'url', array($c));
119
            }
120
121
122
123
124
            $checkbox = html_writer::checkbox('comments', $c->id, false);
            $action = html_writer::link($link, get_string('delete'));
            if (!empty($context_url)) {
                $action .= html_writer::tag('br', null);
                $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
125
            }
126
            $table->data[] = array($checkbox, $c->fullname, $c->content, $action);
127
        }
128
        echo html_writer::table($table);
129
130
131
132
133
134
135
136
137
138
        echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php');
    }

    /**
     * delete a comment
     * @param int $commentid
     */
    public function delete_comment($commentid) {
        global $DB;
        if ($comment = $DB->get_record('comments', array('id'=>$commentid))) {
139
140
            $DB->delete_records('comments', array('id'=>$commentid));
            return true;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        }
        return false;
    }
    /**
     * delete comments
     * @param int $commentid
     */
    public function delete_comments($list) {
        global $DB;
        $ids = explode('-', $list);
        foreach ($ids as $id) {
            if (is_int((int)$id)) {
                if ($comment = $DB->get_record('comments', array('id'=>$id))) {
                    $DB->delete_records('comments', array('id'=>$comment->id));
                }
            }
        }
        return true;
    }
}