|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00025 class comment_manager { 00026 00031 private $perpage; 00032 00036 public function __construct() { 00037 global $CFG; 00038 $this->perpage = $CFG->commentsperpage; 00039 } 00040 00048 function get_comments($page) { 00049 global $DB; 00050 00051 if ($page == 0) { 00052 $start = 0; 00053 } else { 00054 $start = $page * $this->perpage; 00055 } 00056 $comments = array(); 00057 00058 $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, u.firstname, u.lastname, c.timecreated 00059 FROM {comments} c 00060 JOIN {user} u 00061 ON u.id=c.userid 00062 ORDER BY c.timecreated ASC"; 00063 $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage); 00064 $formatoptions = array('overflowdiv' => true); 00065 foreach ($rs as $item) { 00066 // Set calculated fields 00067 $item->fullname = fullname($item); 00068 $item->time = userdate($item->timecreated); 00069 $item->content = format_text($item->content, FORMAT_MOODLE, $formatoptions); 00070 // Unset fields not related to the comment 00071 unset($item->firstname); 00072 unset($item->lastname); 00073 unset($item->timecreated); 00074 // Record the comment 00075 $comments[] = $item; 00076 } 00077 $rs->close(); 00078 00079 return $comments; 00080 } 00081 00090 private function setup_course($courseid) { 00091 global $PAGE, $DB; 00092 if (!empty($this->course)) { 00093 // already set, stop 00094 return; 00095 } 00096 if ($courseid == $PAGE->course->id) { 00097 $this->course = $PAGE->course; 00098 } else if (!$this->course = $DB->get_record('course', array('id' => $courseid))) { 00099 $this->course = null; 00100 } 00101 } 00102 00110 private function setup_plugin($comment) { 00111 global $DB; 00112 $this->context = get_context_instance_by_id($comment->contextid); 00113 if (!$this->context) { 00114 return false; 00115 } 00116 switch ($this->context->contextlevel) { 00117 case CONTEXT_BLOCK: 00118 if ($block = $DB->get_record('block_instances', array('id' => $this->context->instanceid))) { 00119 $this->plugintype = 'block'; 00120 $this->pluginname = $block->blockname; 00121 } else { 00122 return false; 00123 } 00124 break; 00125 case CONTEXT_MODULE: 00126 $this->plugintype = 'mod'; 00127 $this->cm = get_coursemodule_from_id('', $this->context->instanceid); 00128 $this->setup_course($this->cm->course); 00129 $this->modinfo = get_fast_modinfo($this->course); 00130 $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname; 00131 break; 00132 } 00133 return true; 00134 } 00135 00141 public function print_comments($page = 0) { 00142 global $OUTPUT, $CFG, $OUTPUT, $DB; 00143 00144 $count = $DB->count_records('comments'); 00145 $comments = $this->get_comments($page); 00146 if (count($comments) == 0) { 00147 echo $OUTPUT->notification(get_string('nocomments', 'moodle')); 00148 return false; 00149 } 00150 00151 $table = new html_table(); 00152 $table->head = array ( 00153 html_writer::checkbox('selectall', '', false, get_string('selectall'), array('id'=>'comment_select_all', 'class'=>'comment-report-selectall')), 00154 get_string('author', 'search'), 00155 get_string('content'), 00156 get_string('action') 00157 ); 00158 $table->align = array ('left', 'left', 'left', 'left'); 00159 $table->attributes = array('class'=>'generaltable commentstable'); 00160 $table->data = array(); 00161 00162 $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey())); 00163 foreach ($comments as $c) { 00164 $this->setup_plugin($c); 00165 if (!empty($this->plugintype)) { 00166 $context_url = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'url', array($c)); 00167 } 00168 $checkbox = html_writer::checkbox('comments', $c->id, false); 00169 $action = html_writer::link(new moodle_url($link, array('commentid' => $c->id)), get_string('delete')); 00170 if (!empty($context_url)) { 00171 $action .= html_writer::empty_tag('br'); 00172 $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank')); 00173 } 00174 $table->data[] = array($checkbox, $c->fullname, $c->content, $action); 00175 } 00176 echo html_writer::table($table); 00177 echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php'); 00178 return true; 00179 } 00180 00187 public function delete_comment($commentid) { 00188 global $DB; 00189 if ($DB->record_exists('comments', array('id' => $commentid))) { 00190 $DB->delete_records('comments', array('id' => $commentid)); 00191 return true; 00192 } 00193 return false; 00194 } 00201 public function delete_comments($list) { 00202 global $DB; 00203 $ids = explode('-', $list); 00204 foreach ($ids as $id) { 00205 $id = (int)$id; 00206 if ($DB->record_exists('comments', array('id' => $id))) { 00207 $DB->delete_records('comments', array('id' => $id)); 00208 } 00209 } 00210 return true; 00211 } 00212 }