Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
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 mynotes API
19
 *
20
 * @package    block_mynotes
21
 * @author     Gautam Kumar Das<gautam.arg@gmail.com>
22
 */
23
define('AJAX_SCRIPT', true);
24
define('NO_DEBUG_DISPLAY', true);
25
 
26
require_once('../../config.php');
27
require_once($CFG->dirroot.'/course/lib.php');
28
require_once($CFG->dirroot . '/blocks/mynotes/lib.php');
29
 
30
$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
31
$contextarea = optional_param('contextarea', 'site', PARAM_ALPHA);
32
$action    = optional_param('action', '', PARAM_ALPHA);
33
$page      = optional_param('page', 0, PARAM_INT);
34
 
35
list($context, $course, $cm) = get_context_info_array($contextid);
36
 
37
if ( $contextid == SYSCONTEXTID || $context->contextlevel == CONTEXT_USER) {
38
    $course = get_site();
39
}
40
 
41
$PAGE->set_url('/blocks/mynotes/mynotes_ajax.php');
42
 
43
require_course_login($course, true, $cm);
44
 
45
$PAGE->set_context($context);
46
if (!empty($cm)) {
47
    $PAGE->set_cm($cm, $course);
48
} else if (!empty($course)) {
49
    $PAGE->set_course($course);
50
}
51
 
52
if (!confirm_sesskey()) {
53
    $error = array('error' => get_string('invalidsesskey', 'error'));
54
    die(json_encode($error));
55
}
56
 
57
if (!isloggedin()) {
58
    echo json_encode(array('error' => 'require_login'));
59
    die();
60
}
61
$config = get_config('block_mynotes');
62
 
63
echo $OUTPUT->header(); //...send headers
64
// process ajax request
65
switch ($action) {
66
    case 'add':
67
        $content   = optional_param('content',   '', PARAM_RAW);
68
        $manager = new block_mynotes_manager();
69
        if ($note = $manager->addmynote($context, $contextarea, $course, $content)) {
70
            $options = new stdClass();
71
            $options->page = $page;
72
            $options->courseid = $course->id;
73
            $options->contextid   = $context->id;
74
            $options->context   = $context;
75
            $options->contextarea = $contextarea;
76
            unset($options->courseid);
77
            $count = $manager->count_mynotes($options);
78
            echo json_encode(array('notes' => array($note), 'count' => $count));
79
        } else {
80
            echo json_encode(array('error' => 'Unable to add note'));
81
        }
82
        die();
83
        break;
84
    case 'get':
85
        $manager = new block_mynotes_manager();
86
        $options = new stdClass();
87
        $options->page = $page;
88
        $options->contextarea = $contextarea;
89
        $count = $manager->count_mynotes($options);
90
        $notes = $manager->get_mynotes($options);
91
        echo json_encode(array('notes' => $notes, 'count' => $count));
92
        die();
93
        break;
94
    case 'delete':
95
        $noteid = required_param('noteid', PARAM_INT);
96
        $limitfrom = optional_param('lastnotecounts', 0, PARAM_INT);
97
        $manager = new block_mynotes_manager();
98
        if ($manager->delete($noteid)) {
99
            $options = new stdClass();
100
            $options->page = $page;
101
            $options->contextarea = $contextarea;
102
            $count = $manager->count_mynotes($options);
103
            if ($limitfrom) {
104
                $options->limitfrom = $limitfrom - 1;
105
            }
106
            $notes = $manager->get_mynotes($options);
107
            echo json_encode(array('notes' => $notes, 'count' => $count, 'noteid' => $noteid));
108
        }
109
        die();
110
        break;
111
}
112
die();