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 cesa_notes API
|
|
|
19 |
*
|
|
|
20 |
* @package block_cesa_notes
|
|
|
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/cesa_notes/lib.php');
|
|
|
29 |
$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
|
|
|
30 |
$contextarea = optional_param('contextarea', 'site', PARAM_ALPHA);
|
|
|
31 |
$action = optional_param('action', '', PARAM_ALPHA);
|
|
|
32 |
$page = optional_param('page', 0, PARAM_INT);
|
|
|
33 |
|
|
|
34 |
list($context, $course, $cm) = get_context_info_array($contextid);
|
|
|
35 |
|
|
|
36 |
if ( $contextid == SYSCONTEXTID || $context->contextlevel == CONTEXT_USER) {
|
|
|
37 |
$course = get_site();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$PAGE->set_url('/blocks/cesa_notes/cesa_notes_ajax.php');
|
|
|
41 |
|
|
|
42 |
require_course_login($course, true, $cm);
|
|
|
43 |
|
|
|
44 |
$PAGE->set_context($context);
|
|
|
45 |
if (!empty($cm)) {
|
|
|
46 |
$PAGE->set_cm($cm, $course);
|
|
|
47 |
} else if (!empty($course)) {
|
|
|
48 |
$PAGE->set_course($course);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
if (!confirm_sesskey()) {
|
|
|
52 |
$error = array('error' => get_string('invalidsesskey', 'error'));
|
|
|
53 |
die(json_encode($error));
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if (!isloggedin()) {
|
|
|
57 |
echo json_encode(array('error' => 'require_login'));
|
|
|
58 |
die();
|
|
|
59 |
}
|
|
|
60 |
$config = get_config('block_cesa_notes');
|
|
|
61 |
|
|
|
62 |
echo $OUTPUT->header(); //...send headers
|
|
|
63 |
// process ajax request
|
|
|
64 |
switch ($action) {
|
|
|
65 |
case 'add':
|
|
|
66 |
$content = optional_param('content', '', PARAM_RAW);
|
|
|
67 |
$noteurl = optional_param('noteurl', '', PARAM_RAW);
|
|
|
68 |
$manager = new block_cesa_notes_manager();
|
|
|
69 |
if ($note = $manager->addmynote($context, $contextarea, $course, $content, $noteurl, $cm->id)) {
|
|
|
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_cesa_notes($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_cesa_notes_manager();
|
|
|
86 |
$options = new stdClass();
|
|
|
87 |
$options->page = $page;
|
|
|
88 |
$options->contextarea = $contextarea;
|
|
|
89 |
$count = $manager->count_cesa_notes($options);
|
|
|
90 |
$notes = $manager->get_cesa_notes($options);
|
|
|
91 |
echo json_encode(array('notes' => $notes, 'count' => $count));
|
|
|
92 |
die();
|
|
|
93 |
break;
|
|
|
94 |
case 'edit':
|
|
|
95 |
$content = optional_param('content', '', PARAM_RAW);
|
|
|
96 |
$noteurl = optional_param('noteurl', '', PARAM_RAW);
|
|
|
97 |
$noteid = optional_param('noteToEditIt', '', PARAM_RAW);
|
|
|
98 |
$manager = new block_cesa_notes_manager();
|
|
|
99 |
if ($note = $manager->editmynote($noteid, $context, $contextarea, $course, $content, $noteurl, $cm->id)) {
|
|
|
100 |
$options = new stdClass();
|
|
|
101 |
$options->page = $page;
|
|
|
102 |
$options->courseid = $course->id;
|
|
|
103 |
$options->contextid = $context->id;
|
|
|
104 |
$options->context = $context;
|
|
|
105 |
$options->contextarea = $contextarea;
|
|
|
106 |
unset($options->courseid);
|
|
|
107 |
$count = $manager->count_cesa_notes($options);
|
|
|
108 |
echo json_encode(array('notes' => array($note), 'count' => $count));
|
|
|
109 |
} else {
|
|
|
110 |
echo json_encode(array('error' => 'Unable to edit note'));
|
|
|
111 |
}
|
|
|
112 |
die();
|
|
|
113 |
break;
|
|
|
114 |
case 'delete':
|
|
|
115 |
$noteid = required_param('noteid', PARAM_INT);
|
|
|
116 |
$limitfrom = optional_param('lastnotecounts', 0, PARAM_INT);
|
|
|
117 |
$manager = new block_cesa_notes_manager();
|
|
|
118 |
if ($manager->delete($noteid)) {
|
|
|
119 |
$options = new stdClass();
|
|
|
120 |
$options->page = $page;
|
|
|
121 |
$options->contextarea = $contextarea;
|
|
|
122 |
$count = $manager->count_cesa_notes($options);
|
|
|
123 |
if ($limitfrom) {
|
|
|
124 |
$options->limitfrom = $limitfrom - 1;
|
|
|
125 |
}
|
|
|
126 |
$notes = $manager->get_cesa_notes($options);
|
|
|
127 |
echo json_encode(array('notes' => $notes, 'count' => $count, 'noteid' => $noteid));
|
|
|
128 |
}
|
|
|
129 |
die();
|
|
|
130 |
}
|
|
|
131 |
die();
|