"MDL-19118, comments api"
[moodle.git] / comment / lib.php
CommitLineData
1bcb7eb5 1<?php
2
3/**
4 * comment_manager is class to manage moodle comments
5 *
6 * @package moodlecore
7 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
8 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9 */
10class comment_manager {
11
12 /**
13 * Return comments by pages
14 * @param int $page
15 * @return mixed
16 */
17 function get_comments($page) {
18 global $DB, $CFG, $USER;
19 $params = array();
20 if ($page == 0) {
21 $start = 0;
22 } else {
23 $start = $page*$this->perpage;
24 }
25 $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.username, u.firstname, u.lastname, c.timecreated
26 FROM {comments} c, {user} u
27 WHERE u.id=c.userid ORDER BY c.timecreated ASC";
28
29 $comments = array();
30 if ($records = $DB->get_records_sql($sql, array(), $start, $this->perpage)) {
31 foreach ($records as $item) {
32 $item->username = fullname($item);
33 $item->time = userdate($item->timecreated);
34 $item->content = format_text($item->content);
35 $comments[] = $item;
36 unset($item->firstname);
37 unset($item->lastname);
38 unset($item->timecreated);
39 }
40 }
41
42 return $comments;
43 }
44
45 private function _setup_course($courseid) {
46 global $COURSE, $DB;
47 if (!empty($this->course)) {
48 // already set, stop
49 return;
50 }
51 if ($courseid == $COURSE->id) {
52 $this->course = $COURSE;
53 } else if (!$this->course = $DB->get_record('course', array('id'=>$courseid))) {
54 $this->course = null;
55 }
56 }
57
58 private function _setup_plugin($comment) {
59 global $DB;
60 $this->context = get_context_instance_by_id($comment->contextid);
61 if ($this->context->contextlevel == CONTEXT_BLOCK) {
62 if ($block = $DB->get_record('block_instances', array('id'=>$this->context->instanceid))) {
63 $this->plugintype = 'block';
64 $this->pluginname = $block->blockname;
65 }
66 }
67 if ($this->context->contextlevel == CONTEXT_MODULE) {
68 $this->plugintype = 'mod';
69 $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
70 $this->_setup_course($this->cm->course);
71 $this->modinfo = get_fast_modinfo($this->course);
72 $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
73 }
74 }
75
76 /**
77 * Print comments
78 * @param int $page
79 */
80 function print_comments($page=0) {
81 global $CFG, $OUTPUT, $DB;
82 $this->perpage = 10;
83 $count = $DB->count_records_sql('SELECT COUNT(*) FROM {comments} c');
84 $comments = $this->get_comments($page);
85 $table = new stdclass;
86 $table->head = array ('<input type="checkbox" id="comment_select_all"/>', 'author', 'content', 'action');
87 $table->align = array ('left', 'left', 'left', 'left');
88 $table->width = "95%";
89 $table->data = array();
90 foreach ($comments as $c) {
91 $this->_setup_plugin($c);
92 if (!empty($this->plugintype)) {
93 $url = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'url', array($c));
94 }
95 $checkbox = '<input type="checkbox" name="comments" value="'. $c->id .'" />';
96 $action = '';
97 $action .= "<a href='{$CFG->wwwroot}/comment/index.php?action=delete&amp;sesskey=".sesskey()."&amp;commentid={$c->id}'>".get_string('delete').'</a>';
98 $action .= "<br />";
99 if (!empty($url)) {
100 $action .= "<a target='_blank' href='{$url}'>".get_string('commentincontext').'</a>';
101 }
102 $table->data[] = array($checkbox, $c->username, $c->content, $action);
103 }
104 print_table($table);
105 print_paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php?', 'page');
106 }
107
108 /**
109 * delete a comment
110 * @param int $commentid
111 */
112 public function delete_comment($commentid) {
113 global $DB;
114 if ($comment = $DB->get_record('comments', array('id'=>$commentid))) {
115 return $DB->delete_records('comments', array('id'=>$commentid));
116 }
117 return false;
118 }
119 /**
120 * delete comments
121 * @param int $commentid
122 */
123 public function delete_comments($list) {
124 global $DB;
125 $ids = explode('-', $list);
126 foreach ($ids as $id) {
127 if (is_int((int)$id)) {
128 if ($comment = $DB->get_record('comments', array('id'=>$id))) {
129 $DB->delete_records('comments', array('id'=>$comment->id));
130 }
131 }
132 }
133 return true;
134 }
135}