MDL-27565 implement moodle_notes_create_note
[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)
57 * @param $notes An array of notes to create.
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
116 $dbnote = new stdClass();
117 $dbnote->courseid = $note['courseid'];
118 $dbnote->format = FORMAT_PLAIN;
119 //clean param text
120 switch (strtolower($note['format'])) {
121 case 'html':
122 $note['text'] = clean_param($note['text'], PARAM_CLEANHTML);
123 break;
124 case 'text':
125 $note['text'] = clean_param($note['text'], PARAM_TEXT);
126 break;
127 default:
128 $note['text'] = clean_param($note['text'], PARAM_TEXT);
129 break;
130 }
131
132 $dbnote->content = $note['text'];
133 //get the state ('personal', 'course', 'site')
134 switch ($note['publishstate']) {
135 case 'personal':
136 $dbnote->publishstate = NOTES_STATE_DRAFT;
137 break;
138 case 'course':
139 $dbnote->publishstate = NOTES_STATE_PUBLIC;
140 break;
141 case 'site':
142 $dbnote->publishstate = NOTES_STATE_SITE;
143 $dbnote->courseid = SITEID;
144 break;
145 default:
146 break;
147 }
148 $dbnote->userid = $note['userid'];
149
150 //TODO: performance improvement - if possible create a bulk functions for saving multiple notes at once
151 if (note_save($dbnote)) { //note_save attribut an id in case of success
152 add_to_log($dbnote->courseid, 'notes', 'add',
153 'index.php?course='.$dbnote->courseid.'&amp;user='.$dbnote->userid
154 . '#note-' . $dbnote->id , 'add note');
155 $success = $dbnote->id;
156 }
157
158 $resultnote['noteid'] = $success;
159 } else {
160 $resultnote['noteid'] = -1;
161 $resultnote['errormessage'] = $errormessage;
162 }
163
164 $resultnotes[] = $resultnote;
165 }
166
167 return $resultnotes;
168 }
169
170 /**
171 * Returns description of method result value
172 * @return external_description
173 */
174 public static function create_notes_returns() {
175 return new external_multiple_structure(
176 new external_single_structure(
177 array(
178 'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
179 'noteid' => new external_value(PARAM_INT, 'test this to know if it success: id of the created note when successed, -1 when failed'),
180 'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
181 )
182 )
183 );
184 }
185
186}