rating MDL-21657 changed ratings rendering to avoid a nonconstant element name
[moodle.git] / comment / comment_ajax.php
CommitLineData
1bcb7eb5 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/*
19 * Handling all ajax request for comments API
20 */
21require_once('../config.php');
36051c9e 22require_once($CFG->dirroot . '/comment/lib.php');
1bcb7eb5 23
1bcb7eb5 24$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
57588523 25list($context, $course, $cm) = get_context_info_array($contextid);
1bcb7eb5 26
80555111 27require_login($course, true, $cm);
1bcb7eb5 28
29$err = new stdclass;
30
31if (!confirm_sesskey()) {
32 $err->error = get_string('invalidsesskey');
33 die(json_encode($err));
34}
35
36if (!isloggedin()){
37 $err->error = get_string('loggedinnot');
38 die(json_encode($err));
39}
40
41if (isguestuser()) {
42 $err->error = get_string('loggedinnot');
43 die(json_encode($err));
44}
45
46$action = optional_param('action', '', PARAM_ALPHA);
47$area = optional_param('area', '', PARAM_ALPHAEXT);
48$client_id = optional_param('client_id', '', PARAM_RAW);
49$commentid = optional_param('commentid', -1, PARAM_INT);
50$content = optional_param('content', '', PARAM_RAW);
51$itemid = optional_param('itemid', '', PARAM_INT);
52$page = optional_param('page', 0, PARAM_INT);
53
54if (!empty($client_id)) {
55 $cmt = new stdclass;
56 $cmt->contextid = $contextid;
57588523 57 $cmt->courseid = $course->id;
1bcb7eb5 58 $cmt->area = $area;
59 $cmt->itemid = $itemid;
60 $cmt->client_id = $client_id;
61 $comment = new comment($cmt);
62}
63switch ($action) {
64case 'add':
15894c65 65 try {
66 $cmt = $comment->add($content);
67 $cmt->count = $comment->count();
68 if (!empty($cmt) && is_object($cmt)) {
69 $cmt->client_id = $client_id;
70 echo json_encode($cmt);
71 }
72 } catch (comment_exception $e) {
73 echo json_encode(array('error'=>$e->message));
1bcb7eb5 74 }
75 break;
76case 'delete':
15894c65 77 try {
78 $result = $comment->delete($commentid);
79 if ($result === true) {
80 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
81 }
82 } catch (comment_exception $e) {
83 echo json_encode(array('error'=>$e->message));
1bcb7eb5 84 }
85 break;
86case 'get':
87default:
88 $ret = array();
15894c65 89 try {
90 $comments = $comment->get_comments($page);
91 $ret['list'] = $comments;
92 $ret['count'] = $comment->count();
93 $ret['pagination'] = $comment->get_pagination($page);
94 $ret['client_id'] = $client_id;
95 echo json_encode($ret);
96 } catch (comment_exception $e) {
97 echo json_encode(array('error'=>$e->message));
98 }
1bcb7eb5 99}