2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
23 * @copyright 2011 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
30 * Notes external functions
34 * @copyright 2011 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_notes_external
extends external_api
{
41 * Returns description of method parameters
43 * @return external_function_parameters
46 public static function create_notes_parameters() {
47 return new external_function_parameters(
49 'notes' => new external_multiple_structure(
50 new external_single_structure(
52 'userid' => new external_value(PARAM_INT
, 'id of the user the note is about'),
53 'publishstate' => new external_value(PARAM_ALPHA
, '\'personal\', \'course\' or \'site\''),
54 '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)'),
55 'text' => new external_value(PARAM_RAW
, 'the text of the message - text or HTML'),
56 'format' => new external_value(PARAM_ALPHANUMEXT
, // For backward compatibility it can not be PARAM_INT, so we don't use external_format_value.
57 'text format (' . FORMAT_HTML
. ' = HTML, '
58 . FORMAT_MOODLE
. ' = MOODLE, '
59 . FORMAT_PLAIN
. ' = PLAIN or '
60 . FORMAT_MARKDOWN
. ' = MARKDOWN)', VALUE_DEFAULT
, FORMAT_HTML
),
61 '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
),
70 * Create notes about some users
71 * Note: code should be matching the /notes/edit.php checks
72 * and the /user/addnote.php checks. (they are similar cheks)
74 * @param array $notes An array of notes to create.
75 * @return array (success infos and fail infos)
78 public static function create_notes($notes = array()) {
80 require_once($CFG->dirroot
. "/notes/lib.php");
82 $params = self
::validate_parameters(self
::create_notes_parameters(), array('notes' => $notes));
84 //check if note system is enabled
85 if (!$CFG->enablenotes
) {
86 throw new moodle_exception('notesdisabled', 'notes');
89 //retrieve all courses
91 foreach($params['notes'] as $note) {
92 $courseids[] = $note['courseid'];
94 $courses = $DB->get_records_list("course", "id", $courseids);
96 //retrieve all users of the notes
98 foreach($params['notes'] as $note) {
99 $userids[] = $note['userid'];
101 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED
, 'userid_');
102 $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
104 $resultnotes = array();
105 foreach ($params['notes'] as $note) {
108 $resultnote = array(); //the infos about the success of the operation
110 //check the course exists
111 if (empty($courses[$note['courseid']])) {
113 $errormessage = get_string('invalidcourseid', 'error');
115 // Ensure the current user is allowed to run this function
116 $context = get_context_instance(CONTEXT_COURSE
, $note['courseid']);
117 self
::validate_context($context);
118 require_capability('moodle/notes:manage', $context);
121 //check the user exists
122 if (empty($users[$note['userid']])) {
124 $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
127 //build the resultnote
128 if (isset($note['clientnoteid'])) {
129 $resultnote['clientnoteid'] = $note['clientnoteid'];
133 //now we can create the note
134 $dbnote = new stdClass
;
135 $dbnote->courseid
= $note['courseid'];
136 $dbnote->userid
= $note['userid'];
137 // Need to support 'html' and 'text' format values for backward compatibility.
138 switch (strtolower($note['format'])) {
140 $textformat = FORMAT_HTML
;
143 $textformat = FORMAT_PLAIN
;
145 $textformat = external_validate_format($note['format']);
148 $dbnote->content
= $note['text'];
149 $dbnote->format
= $textformat;
151 //get the state ('personal', 'course', 'site')
152 switch ($note['publishstate']) {
154 $dbnote->publishstate
= NOTES_STATE_DRAFT
;
157 $dbnote->publishstate
= NOTES_STATE_PUBLIC
;
160 $dbnote->publishstate
= NOTES_STATE_SITE
;
161 $dbnote->courseid
= SITEID
;
167 //TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
168 if (note_save($dbnote)) { //note_save attribut an id in case of success
169 add_to_log($dbnote->courseid
, 'notes', 'add',
170 'index.php?course='.$dbnote->courseid
.'&user='.$dbnote->userid
171 . '#note-' . $dbnote->id
, 'add note');
172 $success = $dbnote->id
;
175 $resultnote['noteid'] = $success;
177 // WARNINGS: for backward compatibility we return this errormessage.
178 // We should have thrown exceptions as these errors prevent results to be returned.
179 // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
180 $resultnote['noteid'] = -1;
181 $resultnote['errormessage'] = $errormessage;
184 $resultnotes[] = $resultnote;
191 * Returns description of method result value
193 * @return external_description
196 public static function create_notes_returns() {
197 return new external_multiple_structure(
198 new external_single_structure(
200 'clientnoteid' => new external_value(PARAM_ALPHANUMEXT
, 'your own id for the note', VALUE_OPTIONAL
),
201 'noteid' => new external_value(PARAM_INT
, 'test this to know if it success: id of the created note when successed, -1 when failed'),
202 'errormessage' => new external_value(PARAM_TEXT
, 'error message - if failed', VALUE_OPTIONAL
)
211 * Deprecated notes external functions
213 * @package core_notes
214 * @copyright 2011 Jerome Mouneyrac
215 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
217 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
218 * @todo MDL-31194 This will be deleted in Moodle 2.5.
219 * @see core_notes_external
221 class moodle_notes_external
extends external_api
{
224 * Returns description of method parameters
226 * @return external_function_parameters
228 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
229 * @todo MDL-31194 This will be deleted in Moodle 2.5.
230 * @see core_notes_external::create_notes_parameters()
232 public static function create_notes_parameters() {
233 return core_notes_external
::create_notes_parameters();
237 * Create notes about some users
238 * Note: code should be matching the /notes/edit.php checks
239 * and the /user/addnote.php checks. (they are similar cheks)
241 * @param array $notes An array of notes to create.
242 * @return array (success infos and fail infos)
244 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
245 * @todo MDL-31194 This will be deleted in Moodle 2.5.
246 * @see core_notes_external::create_notes()
248 public static function create_notes($notes = array()) {
249 return core_notes_external
::create_notes($notes);
253 * Returns description of method result value
255 * @return external_description
257 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
258 * @todo MDL-31194 This will be deleted in Moodle 2.5.
259 * @see core_notes_external::create_notes_returns()
261 public static function create_notes_returns() {
262 return core_notes_external
::create_notes_returns();