emergency release 2.2dev
[moodle.git] / notes / externallib.php
CommitLineData
8d46dabb
JM
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 * External notes API
20 *
21 * @package moodlecore
22 * @subpackage notes
23 * @copyright 2011 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26require_once("$CFG->libdir/externallib.php");
27
28class moodle_notes_external extends external_api {
29
30 /**
31 * Returns description of method parameters
32 * @return external_function_parameters
33 */
34 public static function create_notes_parameters() {
35 return new external_function_parameters(
36 array(
37 'notes' => new external_multiple_structure(
38 new external_single_structure(
39 array(
40 'userid' => new external_value(PARAM_INT, 'id of the user the note is about'),
41 'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
42 'courseid' => new external_value(PARAM_INT, 'course id of the note (in Moodle a note can only be created into a course, even for site and personal notes)'),
43 'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
44 'format' => new external_value(PARAM_ALPHA, '\'text\' or \'html\'', VALUE_DEFAULT, 'text'),
45 'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own client id for the note. If this id is provided, the fail message id will be returned to you', VALUE_OPTIONAL),
46 )
47 )
48 )
49 )
50 );
51 }
52
53 /**
54 * Create notes about some users
55 * Note: code should be matching the /notes/edit.php checks
56 * and the /user/addnote.php checks. (they are similar cheks)
0373264c 57 * @param array $notes An array of notes to create.
8d46dabb
JM
58 * @return array (success infos and fail infos)
59 */
60 public static function create_notes($notes = array()) {
61 global $CFG, $DB;
62 require_once($CFG->dirroot . "/notes/lib.php");
63
64 $params = self::validate_parameters(self::create_notes_parameters(), array('notes' => $notes));
65
66 //check if note system is enabled
67 if (!$CFG->enablenotes) {
68 throw new moodle_exception('notesdisabled', 'notes');
69 }
70
71 //retrieve all courses
72 $courseids = array();
73 foreach($params['notes'] as $note) {
74 $courseids[] = $note['courseid'];
75 }
76 $courses = $DB->get_records_list("course", "id", $courseids);
77
78 //retrieve all users of the notes
79 $userids = array();
80 foreach($params['notes'] as $note) {
81 $userids[] = $note['userid'];
82 }
83 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid_');
84 $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
85
86 $resultnotes = array();
87 foreach ($params['notes'] as $note) {
88
89 $success = true;
90 $resultnote = array(); //the infos about the success of the operation
91
92 //check the course exists
93 if (empty($courses[$note['courseid']])) {
94 $success = false;
95 $errormessage = get_string('invalidcourseid', 'notes', $note['courseid']);
96 } else {
97 // Ensure the current user is allowed to run this function
98 $context = get_context_instance(CONTEXT_COURSE, $note['courseid']);
99 self::validate_context($context);
100 require_capability('moodle/notes:manage', $context);
101 }
102
103 //check the user exists
104 if (empty($users[$note['userid']])) {
105 $success = false;
106 $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
107 }
108
109 //build the resultnote
110 if (isset($note['clientnoteid'])) {
111 $resultnote['clientnoteid'] = $note['clientnoteid'];
112 }
113
114 if ($success) {
115 //now we can create the note
0373264c 116 $dbnote = new stdClass;
8d46dabb 117 $dbnote->courseid = $note['courseid'];
0373264c
SH
118 $dbnote->userid = $note['userid'];
119 //clean param text and set format accordingly
8d46dabb
JM
120 switch (strtolower($note['format'])) {
121 case 'html':
0373264c
SH
122 $dbnote->content = clean_param($note['text'], PARAM_CLEANHTML);
123 $dbnote->format = FORMAT_HTML;
8d46dabb
JM
124 break;
125 case 'text':
8d46dabb 126 default:
0373264c
SH
127 $dbnote->content = clean_param($note['text'], PARAM_TEXT);
128 $dbnote->format = FORMAT_PLAIN;
8d46dabb
JM
129 break;
130 }
131
8d46dabb
JM
132 //get the state ('personal', 'course', 'site')
133 switch ($note['publishstate']) {
134 case 'personal':
135 $dbnote->publishstate = NOTES_STATE_DRAFT;
136 break;
137 case 'course':
138 $dbnote->publishstate = NOTES_STATE_PUBLIC;
139 break;
140 case 'site':
141 $dbnote->publishstate = NOTES_STATE_SITE;
142 $dbnote->courseid = SITEID;
143 break;
144 default:
145 break;
146 }
8d46dabb
JM
147
148 //TODO: performance improvement - if possible create a bulk functions for saving multiple notes at once
149 if (note_save($dbnote)) { //note_save attribut an id in case of success
150 add_to_log($dbnote->courseid, 'notes', 'add',
151 'index.php?course='.$dbnote->courseid.'&amp;user='.$dbnote->userid
152 . '#note-' . $dbnote->id , 'add note');
153 $success = $dbnote->id;
154 }
155
156 $resultnote['noteid'] = $success;
157 } else {
158 $resultnote['noteid'] = -1;
159 $resultnote['errormessage'] = $errormessage;
160 }
161
162 $resultnotes[] = $resultnote;
163 }
164
165 return $resultnotes;
166 }
167
168 /**
169 * Returns description of method result value
170 * @return external_description
171 */
172 public static function create_notes_returns() {
173 return new external_multiple_structure(
0373264c
SH
174 new external_single_structure(
175 array(
176 'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
177 'noteid' => new external_value(PARAM_INT, 'test this to know if it success: id of the created note when successed, -1 when failed'),
178 'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
8d46dabb 179 )
0373264c 180 )
8d46dabb
JM
181 );
182 }
183
184}