MDL-19118, set $PAGE->context for comment_ajax.php
[moodle.git] / comment / comment_ajax.php
1 <?php
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/>.
18 /*
19  * Handling all ajax request for comments API
20  */
21 define('AJAX_SCRIPT', true);
23 require_once('../config.php');
24 require_once($CFG->dirroot . '/comment/lib.php');
26 $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
27 list($context, $course, $cm) = get_context_info_array($contextid);
29 $PAGE->set_context($context);
30 $PAGE->set_url('/comment/comment_ajax.php');
32 require_login($course, true, $cm);
33 require_sesskey();
35 $action    = optional_param('action',    '', PARAM_ALPHA);
36 $area      = optional_param('area',      '', PARAM_ALPHAEXT);
37 $client_id = optional_param('client_id', '', PARAM_RAW);
38 $commentid = optional_param('commentid', -1, PARAM_INT);
39 $content   = optional_param('content',   '', PARAM_RAW);
40 $itemid    = optional_param('itemid',    '', PARAM_INT);
41 $page      = optional_param('page',      0,  PARAM_INT);
43 // initilising comment object
44 if (!empty($client_id)) {
45     $args = new stdclass;
46     $args->context   = $context;
47     $args->course    = $course;
48     $args->cm        = $cm;
49     $args->area      = $area;
50     $args->itemid    = $itemid;
51     $args->client_id = $client_id;
52     $manager = new comment($args);
53 } else {
54     die;
55 }
57 // process ajax request
58 switch ($action) {
59     case 'add':
60         $result = $manager->add($content);
61         if (!empty($result) && is_object($result)) {
62             $result->count = $manager->count();
63             $result->client_id = $client_id;
64             echo json_encode($result);
65         }
66         break;
67     case 'delete':
68         $result = $manager->delete($commentid);
69         if ($result === true) {
70             echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid));
71         }
72         break;
73     case 'get':
74     default:
75         $result = array();
76         $comments = $manager->get_comments($page);
77         $result['list'] = $comments;
78         $result['count'] = $manager->count();
79         $result['pagination'] = $manager->get_pagination($page);
80         $result['client_id']  = $client_id;
81         echo json_encode($result);
82 }