MDL-31636 redirect debugging to error log for ajax
[moodle.git] / comment / comment_ajax.php
CommitLineData
1bcb7eb5 1<?php
1bcb7eb5 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/*
18 * Handling all ajax request for comments API
35453657
DC
19 *
20 * @package core
21 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1bcb7eb5 23 */
2696b976 24define('AJAX_SCRIPT', true);
6c847bbb 25define('NO_DEBUG_DISPLAY', true);
2696b976 26
1bcb7eb5 27require_once('../config.php');
36051c9e 28require_once($CFG->dirroot . '/comment/lib.php');
1bcb7eb5 29
1bcb7eb5 30$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
c1951ea9
DC
31$action = optional_param('action', '', PARAM_ALPHA);
32
33if (empty($CFG->usecomments)) {
34 throw new comment_exception('commentsnotenabled', 'moodle');
35}
36
57588523 37list($context, $course, $cm) = get_context_info_array($contextid);
1bcb7eb5 38
cff7e83d
AB
39if ( $contextid == SYSCONTEXTID ) {
40 $course = $SITE;
41}
42
467c85d7
DC
43$PAGE->set_url('/comment/comment_ajax.php');
44
c1951ea9
DC
45// Allow anonymous user to view comments providing forcelogin now enabled
46require_course_login($course, true, $cm);
47$PAGE->set_context($context);
48if (!empty($cm)) {
49 $PAGE->set_cm($cm, $course);
50} else if (!empty($course)) {
51 $PAGE->set_course($course);
52}
18fb4d75 53
df796bf0 54if (!confirm_sesskey()) {
c1951ea9 55 $error = array('error'=>get_string('invalidsesskey', 'error'));
df796bf0
DC
56 die(json_encode($error));
57}
58
c1951ea9 59$client_id = required_param('client_id', PARAM_ALPHANUM);
aff24313 60$area = optional_param('area', '', PARAM_AREA);
467c85d7
DC
61$commentid = optional_param('commentid', -1, PARAM_INT);
62$content = optional_param('content', '', PARAM_RAW);
63$itemid = optional_param('itemid', '', PARAM_INT);
64$page = optional_param('page', 0, PARAM_INT);
aff24313 65$component = optional_param('component', '', PARAM_COMPONENT);
1bcb7eb5 66
467c85d7 67// initilising comment object
c1951ea9
DC
68$args = new stdClass;
69$args->context = $context;
70$args->course = $course;
71$args->cm = $cm;
72$args->area = $area;
73$args->itemid = $itemid;
74$args->client_id = $client_id;
75$args->component = $component;
76$manager = new comment($args);
77
78echo $OUTPUT->header(); // send headers
866354a9 79
467c85d7 80// process ajax request
1bcb7eb5 81switch ($action) {
2696b976 82 case 'add':
c1951ea9
DC
83 if ($manager->can_post()) {
84 $result = $manager->add($content);
85 if (!empty($result) && is_object($result)) {
86 $result->count = $manager->count();
87 $result->client_id = $client_id;
88 echo json_encode($result);
89 die();
90 }
15894c65 91 }
2696b976
PS
92 break;
93 case 'delete':
c1951ea9
DC
94 $comment_record = $DB->get_record('comments', array('id'=>$commentid));
95 if ($manager->can_delete($commentid) || $comment_record->userid == $USER->id) {
96 if ($manager->delete($commentid)) {
97 $result = array(
98 'client_id' => $client_id,
99 'commentid' => $commentid
100 );
101 echo json_encode($result);
102 die();
103 }
15894c65 104 }
2696b976
PS
105 break;
106 case 'get':
107 default:
c1951ea9
DC
108 if ($manager->can_view()) {
109 $comments = $manager->get_comments($page);
110 $result = array(
111 'list' => $comments,
112 'count' => $manager->count(),
113 'pagination' => $manager->get_pagination($page),
114 'client_id' => $client_id
115 );
116 echo json_encode($result);
117 die();
118 }
119 break;
120}
121
122if (!isloggedin()) {
123 // tell user to log in to view comments
124 echo json_encode(array('error'=>'require_login'));
1bcb7eb5 125}
c1951ea9
DC
126// ignore request
127die;