Merge branch 'MDL-32082_messaging_readability_21' of git://github.com/andyjdavis...
[moodle.git] / notes / externallib.php
blobd5b9c5334d7d52272033db915fa54eb1085f1c73
1 <?php
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.
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/>.
18 /**
19 * External notes API
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
26 require_once("$CFG->libdir/externallib.php");
28 class moodle_notes_external extends external_api {
30 /**
31 * Returns description of method parameters
32 * @return external_function_parameters
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),
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 array $notes An array of notes to create.
58 * @return array (success infos and fail infos)
60 public static function create_notes($notes = array()) {
61 global $CFG, $DB;
62 require_once($CFG->dirroot . "/notes/lib.php");
64 $params = self::validate_parameters(self::create_notes_parameters(), array('notes' => $notes));
66 //check if note system is enabled
67 if (!$CFG->enablenotes) {
68 throw new moodle_exception('notesdisabled', 'notes');
71 //retrieve all courses
72 $courseids = array();
73 foreach($params['notes'] as $note) {
74 $courseids[] = $note['courseid'];
76 $courses = $DB->get_records_list("course", "id", $courseids);
78 //retrieve all users of the notes
79 $userids = array();
80 foreach($params['notes'] as $note) {
81 $userids[] = $note['userid'];
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);
86 $resultnotes = array();
87 foreach ($params['notes'] as $note) {
89 $success = true;
90 $resultnote = array(); //the infos about the success of the operation
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);
103 //check the user exists
104 if (empty($users[$note['userid']])) {
105 $success = false;
106 $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
109 //build the resultnote
110 if (isset($note['clientnoteid'])) {
111 $resultnote['clientnoteid'] = $note['clientnoteid'];
114 if ($success) {
115 //now we can create the note
116 $dbnote = new stdClass;
117 $dbnote->courseid = $note['courseid'];
118 $dbnote->userid = $note['userid'];
119 //clean param text and set format accordingly
120 switch (strtolower($note['format'])) {
121 case 'html':
122 $dbnote->content = clean_param($note['text'], PARAM_CLEANHTML);
123 $dbnote->format = FORMAT_HTML;
124 break;
125 case 'text':
126 default:
127 $dbnote->content = clean_param($note['text'], PARAM_TEXT);
128 $dbnote->format = FORMAT_PLAIN;
129 break;
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;
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;
156 $resultnote['noteid'] = $success;
157 } else {
158 $resultnote['noteid'] = -1;
159 $resultnote['errormessage'] = $errormessage;
162 $resultnotes[] = $resultnote;
165 return $resultnotes;
169 * Returns description of method result value
170 * @return external_description
172 public static function create_notes_returns() {
173 return new external_multiple_structure(
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)