weekly release 2.3dev
[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
19 */
2696b976
PS
20define('AJAX_SCRIPT', true);
21
1bcb7eb5 22require_once('../config.php');
36051c9e 23require_once($CFG->dirroot . '/comment/lib.php');
1bcb7eb5 24
1bcb7eb5 25$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
c1951ea9
DC
26$action = optional_param('action', '', PARAM_ALPHA);
27
28if (empty($CFG->usecomments)) {
29 throw new comment_exception('commentsnotenabled', 'moodle');
30}
31
57588523 32list($context, $course, $cm) = get_context_info_array($contextid);
1bcb7eb5 33
467c85d7
DC
34$PAGE->set_url('/comment/comment_ajax.php');
35
c1951ea9
DC
36// Allow anonymous user to view comments providing forcelogin now enabled
37require_course_login($course, true, $cm);
38$PAGE->set_context($context);
39if (!empty($cm)) {
40 $PAGE->set_cm($cm, $course);
41} else if (!empty($course)) {
42 $PAGE->set_course($course);
43}
18fb4d75 44
df796bf0 45if (!confirm_sesskey()) {
c1951ea9 46 $error = array('error'=>get_string('invalidsesskey', 'error'));
df796bf0
DC
47 die(json_encode($error));
48}
49
c1951ea9 50$client_id = required_param('client_id', PARAM_ALPHANUM);
aff24313 51$area = optional_param('area', '', PARAM_AREA);
467c85d7
DC
52$commentid = optional_param('commentid', -1, PARAM_INT);
53$content = optional_param('content', '', PARAM_RAW);
54$itemid = optional_param('itemid', '', PARAM_INT);
55$page = optional_param('page', 0, PARAM_INT);
aff24313 56$component = optional_param('component', '', PARAM_COMPONENT);
1bcb7eb5 57
467c85d7 58// initilising comment object
c1951ea9
DC
59$args = new stdClass;
60$args->context = $context;
61$args->course = $course;
62$args->cm = $cm;
63$args->area = $area;
64$args->itemid = $itemid;
65$args->client_id = $client_id;
66$args->component = $component;
67$manager = new comment($args);
68
69echo $OUTPUT->header(); // send headers
866354a9 70
467c85d7 71// process ajax request
1bcb7eb5 72switch ($action) {
2696b976 73 case 'add':
c1951ea9
DC
74 if ($manager->can_post()) {
75 $result = $manager->add($content);
76 if (!empty($result) && is_object($result)) {
77 $result->count = $manager->count();
78 $result->client_id = $client_id;
79 echo json_encode($result);
80 die();
81 }
15894c65 82 }
2696b976
PS
83 break;
84 case 'delete':
c1951ea9
DC
85 $comment_record = $DB->get_record('comments', array('id'=>$commentid));
86 if ($manager->can_delete($commentid) || $comment_record->userid == $USER->id) {
87 if ($manager->delete($commentid)) {
88 $result = array(
89 'client_id' => $client_id,
90 'commentid' => $commentid
91 );
92 echo json_encode($result);
93 die();
94 }
15894c65 95 }
2696b976
PS
96 break;
97 case 'get':
98 default:
c1951ea9
DC
99 if ($manager->can_view()) {
100 $comments = $manager->get_comments($page);
101 $result = array(
102 'list' => $comments,
103 'count' => $manager->count(),
104 'pagination' => $manager->get_pagination($page),
105 'client_id' => $client_id
106 );
107 echo json_encode($result);
108 die();
109 }
110 break;
111}
112
113if (!isloggedin()) {
114 // tell user to log in to view comments
115 echo json_encode(array('error'=>'require_login'));
1bcb7eb5 116}
c1951ea9
DC
117// ignore request
118die;