Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/notes/externallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once("$CFG->libdir/externallib.php");
00027 
00031 class core_notes_external extends external_api {
00032 
00037     public static function create_notes_parameters() {
00038         return new external_function_parameters(
00039             array(
00040                 'notes' => new external_multiple_structure(
00041                     new external_single_structure(
00042                         array(
00043                             'userid' => new external_value(PARAM_INT, 'id of the user the note is about'),
00044                             'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
00045                             '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)'),
00046                             'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
00047                             'format' => new external_value(PARAM_ALPHA, '\'text\' or \'html\'', VALUE_DEFAULT, 'text'),
00048                             '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),
00049                         )
00050                     )
00051                 )
00052             )
00053         );
00054     }
00055 
00063     public static function create_notes($notes = array()) {
00064         global $CFG, $DB;
00065         require_once($CFG->dirroot . "/notes/lib.php");
00066 
00067         $params = self::validate_parameters(self::create_notes_parameters(), array('notes' => $notes));
00068 
00069         //check if note system is enabled
00070         if (!$CFG->enablenotes) {
00071             throw new moodle_exception('notesdisabled', 'notes');
00072         }
00073 
00074         //retrieve all courses
00075         $courseids = array();
00076         foreach($params['notes'] as $note) {
00077             $courseids[] = $note['courseid'];
00078         }
00079         $courses = $DB->get_records_list("course", "id", $courseids);
00080 
00081         //retrieve all users of the notes
00082         $userids = array();
00083         foreach($params['notes'] as $note) {
00084             $userids[] = $note['userid'];
00085         }
00086         list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid_');
00087         $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
00088 
00089         $resultnotes = array();
00090         foreach ($params['notes'] as $note) {
00091 
00092             $success = true;
00093             $resultnote = array(); //the infos about the success of the operation
00094 
00095             //check the course exists
00096             if (empty($courses[$note['courseid']])) {
00097                 $success = false;
00098                 $errormessage = get_string('invalidcourseid', 'notes', $note['courseid']);
00099             } else {
00100                 // Ensure the current user is allowed to run this function
00101                 $context = get_context_instance(CONTEXT_COURSE, $note['courseid']);
00102                 self::validate_context($context);
00103                 require_capability('moodle/notes:manage', $context);
00104             }
00105 
00106             //check the user exists
00107             if (empty($users[$note['userid']])) {
00108                 $success = false;
00109                 $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
00110             }
00111 
00112             //build the resultnote
00113             if (isset($note['clientnoteid'])) {
00114                 $resultnote['clientnoteid'] = $note['clientnoteid'];
00115             }
00116 
00117             if ($success) {
00118                 //now we can create the note
00119                 $dbnote = new stdClass;
00120                 $dbnote->courseid = $note['courseid'];
00121                 $dbnote->userid = $note['userid'];
00122                 //clean param text and set format accordingly
00123                 switch (strtolower($note['format'])) {
00124                     case 'html':
00125                         $dbnote->content = clean_param($note['text'], PARAM_CLEANHTML);
00126                         $dbnote->format = FORMAT_HTML;
00127                         break;
00128                     case 'text':
00129                     default:
00130                         $dbnote->content = clean_param($note['text'], PARAM_TEXT);
00131                         $dbnote->format = FORMAT_PLAIN;
00132                         break;
00133                 }
00134 
00135                 //get the state ('personal', 'course', 'site')
00136                 switch ($note['publishstate']) {
00137                     case 'personal':
00138                         $dbnote->publishstate = NOTES_STATE_DRAFT;
00139                         break;
00140                     case 'course':
00141                         $dbnote->publishstate = NOTES_STATE_PUBLIC;
00142                         break;
00143                     case 'site':
00144                         $dbnote->publishstate = NOTES_STATE_SITE;
00145                         $dbnote->courseid = SITEID;
00146                         break;
00147                     default:
00148                         break;
00149                 }
00150 
00151                 //TODO: performance improvement - if possible create a bulk functions for saving multiple notes at once
00152                 if (note_save($dbnote)) { //note_save attribut an id in case of success
00153                     add_to_log($dbnote->courseid, 'notes', 'add',
00154                             'index.php?course='.$dbnote->courseid.'&amp;user='.$dbnote->userid
00155                             . '#note-' . $dbnote->id , 'add note');
00156                     $success = $dbnote->id;
00157                 }
00158 
00159                 $resultnote['noteid'] = $success;
00160             } else {
00161                 $resultnote['noteid'] = -1;
00162                 $resultnote['errormessage'] = $errormessage;
00163             }
00164 
00165             $resultnotes[] = $resultnote;
00166         }
00167 
00168         return $resultnotes;
00169     }
00170 
00175     public static function create_notes_returns() {
00176         return new external_multiple_structure(
00177             new external_single_structure(
00178                 array(
00179                     'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
00180                     'noteid' => new external_value(PARAM_INT, 'test this to know if it success:  id of the created note when successed, -1 when failed'),
00181                     'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
00182                 )
00183             )
00184         );
00185     }
00186 
00187 }
00188 
00193 class moodle_notes_external extends external_api {
00194 
00200     public static function create_notes_parameters() {
00201         return core_notes_external::create_notes_parameters();
00202     }
00203 
00212     public static function create_notes($notes = array()) {
00213         return core_notes_external::create_notes($notes);
00214     }
00215 
00221     public static function create_notes_returns() {
00222         return core_notes_external::create_notes_returns();
00223     }
00224 
00225 }
 All Data Structures Namespaces Files Functions Variables Enumerations