|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00017 /* 00018 * Handling all ajax request for comments API 00019 */ 00020 define('AJAX_SCRIPT', true); 00021 00022 require_once('../config.php'); 00023 require_once($CFG->dirroot . '/comment/lib.php'); 00024 00025 $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT); 00026 $action = optional_param('action', '', PARAM_ALPHA); 00027 00028 if (empty($CFG->usecomments)) { 00029 throw new comment_exception('commentsnotenabled', 'moodle'); 00030 } 00031 00032 list($context, $course, $cm) = get_context_info_array($contextid); 00033 00034 $PAGE->set_url('/comment/comment_ajax.php'); 00035 00036 // Allow anonymous user to view comments providing forcelogin now enabled 00037 require_course_login($course, true, $cm); 00038 $PAGE->set_context($context); 00039 if (!empty($cm)) { 00040 $PAGE->set_cm($cm, $course); 00041 } else if (!empty($course)) { 00042 $PAGE->set_course($course); 00043 } 00044 00045 if (!confirm_sesskey()) { 00046 $error = array('error'=>get_string('invalidsesskey', 'error')); 00047 die(json_encode($error)); 00048 } 00049 00050 $client_id = required_param('client_id', PARAM_ALPHANUM); 00051 $area = optional_param('area', '', PARAM_AREA); 00052 $commentid = optional_param('commentid', -1, PARAM_INT); 00053 $content = optional_param('content', '', PARAM_RAW); 00054 $itemid = optional_param('itemid', '', PARAM_INT); 00055 $page = optional_param('page', 0, PARAM_INT); 00056 $component = optional_param('component', '', PARAM_COMPONENT); 00057 00058 // initilising comment object 00059 $args = new stdClass; 00060 $args->context = $context; 00061 $args->course = $course; 00062 $args->cm = $cm; 00063 $args->area = $area; 00064 $args->itemid = $itemid; 00065 $args->client_id = $client_id; 00066 $args->component = $component; 00067 $manager = new comment($args); 00068 00069 echo $OUTPUT->header(); // send headers 00070 00071 // process ajax request 00072 switch ($action) { 00073 case 'add': 00074 if ($manager->can_post()) { 00075 $result = $manager->add($content); 00076 if (!empty($result) && is_object($result)) { 00077 $result->count = $manager->count(); 00078 $result->client_id = $client_id; 00079 echo json_encode($result); 00080 die(); 00081 } 00082 } 00083 break; 00084 case 'delete': 00085 $comment_record = $DB->get_record('comments', array('id'=>$commentid)); 00086 if ($manager->can_delete($commentid) || $comment_record->userid == $USER->id) { 00087 if ($manager->delete($commentid)) { 00088 $result = array( 00089 'client_id' => $client_id, 00090 'commentid' => $commentid 00091 ); 00092 echo json_encode($result); 00093 die(); 00094 } 00095 } 00096 break; 00097 case 'get': 00098 default: 00099 if ($manager->can_view()) { 00100 $comments = $manager->get_comments($page); 00101 $result = array( 00102 'list' => $comments, 00103 'count' => $manager->count(), 00104 'pagination' => $manager->get_pagination($page), 00105 'client_id' => $client_id 00106 ); 00107 echo json_encode($result); 00108 die(); 00109 } 00110 break; 00111 } 00112 00113 if (!isloggedin()) { 00114 // tell user to log in to view comments 00115 echo json_encode(array('error'=>'require_login')); 00116 } 00117 // ignore request 00118 die;