weekly release 2.1dev
[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 */
2696b976
PS
21define('AJAX_SCRIPT', true);
22
1bcb7eb5 23require_once('../config.php');
36051c9e 24require_once($CFG->dirroot . '/comment/lib.php');
1bcb7eb5 25
1bcb7eb5 26$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
57588523 27list($context, $course, $cm) = get_context_info_array($contextid);
1bcb7eb5 28
467c85d7
DC
29$PAGE->set_context($context);
30$PAGE->set_url('/comment/comment_ajax.php');
31
df796bf0 32$action = optional_param('action', '', PARAM_ALPHA);
18fb4d75 33
df796bf0
DC
34if (!confirm_sesskey()) {
35 $error = array('error'=>get_string('invalidsesskey'));
36 die(json_encode($error));
37}
38
39if (!isloggedin()) {
40 // display comments on front page without permission check
41 if ($action == 'get') {
42 if ($context->id == get_context_instance(CONTEXT_COURSE, SITEID)->id) {
43 $ignore_permission = true;
44 } else {
45 // tell user to log in to view comments
46 $ignore_permission = false;
47 echo json_encode(array('error'=>'require_login'));
48 die;
49 }
50 } else {
51 // ignore request
52 die;
53 }
a85265f4 54} else {
df796bf0 55 $ignore_permission = false;
d846488e 56}
1bcb7eb5 57
467c85d7
DC
58$area = optional_param('area', '', PARAM_ALPHAEXT);
59$client_id = optional_param('client_id', '', PARAM_RAW);
60$commentid = optional_param('commentid', -1, PARAM_INT);
61$content = optional_param('content', '', PARAM_RAW);
62$itemid = optional_param('itemid', '', PARAM_INT);
63$page = optional_param('page', 0, PARAM_INT);
7ade777c 64$component = optional_param('component', '', PARAM_ALPHAEXT);
1bcb7eb5 65
bce08d9a
PS
66echo $OUTPUT->header(); // send headers
67
467c85d7 68// initilising comment object
1bcb7eb5 69if (!empty($client_id)) {
6bdfef5d 70 $args = new stdClass();
467c85d7
DC
71 $args->context = $context;
72 $args->course = $course;
73 $args->cm = $cm;
74 $args->area = $area;
75 $args->itemid = $itemid;
76 $args->client_id = $client_id;
7ade777c 77 $args->component = $component;
287ccb31
DC
78 // only for comments in frontpage
79 $args->ignore_permission = $ignore_permission;
467c85d7 80 $manager = new comment($args);
866354a9
DC
81} else {
82 die;
1bcb7eb5 83}
866354a9 84
467c85d7 85// process ajax request
1bcb7eb5 86switch ($action) {
2696b976 87 case 'add':
467c85d7
DC
88 $result = $manager->add($content);
89 if (!empty($result) && is_object($result)) {
90 $result->count = $manager->count();
91 $result->client_id = $client_id;
92 echo json_encode($result);
15894c65 93 }
2696b976
PS
94 break;
95 case 'delete':
467c85d7 96 $result = $manager->delete($commentid);
15894c65 97 if ($result === true) {
98 echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
99 }
2696b976
PS
100 break;
101 case 'get':
102 default:
467c85d7
DC
103 $result = array();
104 $comments = $manager->get_comments($page);
105 $result['list'] = $comments;
106 $result['count'] = $manager->count();
107 $result['pagination'] = $manager->get_pagination($page);
108 $result['client_id'] = $client_id;
109 echo json_encode($result);
1bcb7eb5 110}