Search moodle.org's
Developer Documentation


   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  
  18  /**
  19   * External notes API
  20   *
  21   * @package    core_notes
  22   * @category   external
  23   * @copyright  2011 Jerome Mouneyrac
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  require_once("$CFG->libdir/externallib.php");
  28  
  29  /**
  30   * Notes external functions
  31   *
  32   * @package    core_notes
  33   * @category   external
  34   * @copyright  2011 Jerome Mouneyrac
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @since Moodle 2.2
  37   */
  38  class core_notes_external extends external_api {
  39  
  40      /**
  41       * Returns description of method parameters
  42       *
  43       * @return external_function_parameters
  44       * @since Moodle 2.2
  45       */
  46      public static function create_notes_parameters() {
  47          return new external_function_parameters(
  48              array(
  49                  'notes' => new external_multiple_structure(
  50                      new external_single_structure(
  51                          array(
  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_format_value('text', VALUE_DEFAULT),
  57                              '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),
  58                          )
  59                      )
  60                  )
  61              )
  62          );
  63      }
  64  
  65      /**
  66       * Create notes about some users
  67       * Note: code should be matching the /notes/edit.php checks
  68       * and the /user/addnote.php checks. (they are similar cheks)
  69       *
  70       * @param array $notes  An array of notes to create.
  71       * @return array (success infos and fail infos)
  72       * @since Moodle 2.2
  73       */
  74      public static function create_notes($notes = array()) {
  75          global $CFG, $DB;
  76          require_once($CFG->dirroot . "/notes/lib.php");
  77  
  78          $params = self::validate_parameters(self::create_notes_parameters(), array('notes' => $notes));
  79  
  80          // Check if note system is enabled.
  81          if (!$CFG->enablenotes) {
  82              throw new moodle_exception('notesdisabled', 'notes');
  83          }
  84  
  85          // Retrieve all courses.
  86          $courseids = array();
  87          foreach ($params['notes'] as $note) {
  88              $courseids[] = $note['courseid'];
  89          }
  90          $courses = $DB->get_records_list("course", "id", $courseids);
  91  
  92          // Retrieve all users of the notes.
  93          $userids = array();
  94          foreach ($params['notes'] as $note) {
  95              $userids[] = $note['userid'];
  96          }
  97          list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid_');
  98          $users = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
  99  
 100          $resultnotes = array();
 101          foreach ($params['notes'] as $note) {
 102  
 103              $success = true;
 104              $resultnote = array(); // The infos about the success of the operation.
 105  
 106              // Check the course exists.
 107              if (empty($courses[$note['courseid']])) {
 108                  $success = false;
 109                  $errormessage = get_string('invalidcourseid', 'error');
 110              } else {
 111                  // Ensure the current user is allowed to run this function.
 112                  $context = context_course::instance($note['courseid']);
 113                  self::validate_context($context);
 114                  require_capability('moodle/notes:manage', $context);
 115              }
 116  
 117              // Check the user exists.
 118              if (empty($users[$note['userid']])) {
 119                  $success = false;
 120                  $errormessage = get_string('invaliduserid', 'notes', $note['userid']);
 121              }
 122  
 123              // Build the resultnote.
 124              if (isset($note['clientnoteid'])) {
 125                  $resultnote['clientnoteid'] = $note['clientnoteid'];
 126              }
 127  
 128              if ($success) {
 129                  // Now we can create the note.
 130                  $dbnote = new stdClass;
 131                  $dbnote->courseid = $note['courseid'];
 132                  $dbnote->userid = $note['userid'];
 133                  // Need to support 'html' and 'text' format values for backward compatibility.
 134                  switch (strtolower($note['format'])) {
 135                      case 'html':
 136                          $textformat = FORMAT_HTML;
 137                          break;
 138                      case 'text':
 139                          $textformat = FORMAT_PLAIN;
 140                      default:
 141                          $textformat = external_validate_format($note['format']);
 142                          break;
 143                  }
 144                  $dbnote->content = $note['text'];
 145                  $dbnote->format = $textformat;
 146  
 147                  // Get the state ('personal', 'course', 'site').
 148                  switch ($note['publishstate']) {
 149                      case 'personal':
 150                          $dbnote->publishstate = NOTES_STATE_DRAFT;
 151                          break;
 152                      case 'course':
 153                          $dbnote->publishstate = NOTES_STATE_PUBLIC;
 154                          break;
 155                      case 'site':
 156                          $dbnote->publishstate = NOTES_STATE_SITE;
 157                          $dbnote->courseid = SITEID;
 158                          break;
 159                      default:
 160                          break;
 161                  }
 162  
 163                  // TODO MDL-31119 performance improvement - if possible create a bulk functions for saving multiple notes at once
 164                  if (note_save($dbnote)) { // Note_save attribut an id in case of success.
 165                      $success = $dbnote->id;
 166                  }
 167  
 168                  $resultnote['noteid'] = $success;
 169              } else {
 170                  // WARNINGS: for backward compatibility we return this errormessage.
 171                  //          We should have thrown exceptions as these errors prevent results to be returned.
 172                  // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side .
 173                  $resultnote['noteid'] = -1;
 174                  $resultnote['errormessage'] = $errormessage;
 175              }
 176  
 177              $resultnotes[] = $resultnote;
 178          }
 179  
 180          return $resultnotes;
 181      }
 182  
 183      /**
 184       * Returns description of method result value
 185       *
 186       * @return external_description
 187       * @since Moodle 2.2
 188       */
 189      public static function create_notes_returns() {
 190          return new external_multiple_structure(
 191              new external_single_structure(
 192                  array(
 193                      'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
 194                      'noteid' => new external_value(PARAM_INT, 'ID of the created note when successful, -1 when failed'),
 195                      'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
 196                  )
 197              )
 198          );
 199      }
 200  
 201      /**
 202       * Returns description of delete_notes parameters
 203       *
 204       * @return external_function_parameters
 205       * @since Moodle 2.5
 206       */
 207      public static function delete_notes_parameters() {
 208          return new external_function_parameters(
 209              array(
 210                  "notes"=> new external_multiple_structure(
 211                      new external_value(PARAM_INT, 'ID of the note to be deleted'), 'Array of Note Ids to be deleted.'
 212                  )
 213              )
 214          );
 215      }
 216  
 217      /**
 218       * Delete notes about users.
 219       * Note: code should be matching the /notes/delete.php checks.
 220       *
 221       * @param array $notes An array of ids for the notes to delete.
 222       * @return null
 223       * @since Moodle 2.5
 224       */
 225      public static function delete_notes($notes = array()) {
 226          global $CFG;
 227          require_once($CFG->dirroot . "/notes/lib.php");
 228  
 229          $params = self::validate_parameters(self::delete_notes_parameters(), array('notes' => $notes));
 230  
 231          // Check if note system is enabled.
 232          if (!$CFG->enablenotes) {
 233              throw new moodle_exception('notesdisabled', 'notes');
 234          }
 235          $warnings = array();
 236          foreach ($params['notes'] as $noteid) {
 237              $note = note_load($noteid);
 238              if (isset($note->id)) {
 239                  // Ensure the current user is allowed to run this function.
 240                  $context = context_course::instance($note->courseid);
 241                  self::validate_context($context);
 242                  require_capability('moodle/notes:manage', $context);
 243                  note_delete($note);
 244              } else {
 245                  $warnings[] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
 246              }
 247          }
 248          return $warnings;
 249      }
 250  
 251      /**
 252       * Returns description of delete_notes result value.
 253       *
 254       * @return external_description
 255       * @since Moodle 2.5
 256       */
 257      public static function delete_notes_returns() {
 258          return  new external_warnings('item is always \'note\'',
 259                              'When errorcode is savedfailed the note could not be modified.' .
 260                              'When errorcode is badparam, an incorrect parameter was provided.' .
 261                              'When errorcode is badid, the note does not exist',
 262                              'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
 263  
 264      }
 265  
 266      /**
 267       * Returns description of get_notes parameters.
 268       *
 269       * @return external_function_parameters
 270       * @since Moodle 2.5
 271       */
 272      public static function get_notes_parameters() {
 273          return new external_function_parameters(
 274              array(
 275                  "notes"=> new external_multiple_structure(
 276                      new external_value(PARAM_INT, 'ID of the note to be retrieved'), 'Array of Note Ids to be retrieved.'
 277                  )
 278              )
 279          );
 280      }
 281  
 282      /**
 283       * Get notes about users.
 284       *
 285       * @param array $notes An array of ids for the notes to retrieve.
 286       * @return null
 287       * @since Moodle 2.5
 288       */
 289      public static function get_notes($notes) {
 290          global $CFG;
 291          require_once($CFG->dirroot . "/notes/lib.php");
 292  
 293          $params = self::validate_parameters(self::get_notes_parameters(), array('notes' => $notes));
 294          // Check if note system is enabled.
 295          if (!$CFG->enablenotes) {
 296              throw new moodle_exception('notesdisabled', 'notes');
 297          }
 298          $resultnotes = array();
 299          foreach ($params['notes'] as $noteid) {
 300              $resultnote = array();
 301  
 302              $note = note_load($noteid);
 303              if (isset($note->id)) {
 304                  // Ensure the current user is allowed to run this function.
 305                  $context = context_course::instance($note->courseid);
 306                  self::validate_context($context);
 307                  require_capability('moodle/notes:view', $context);
 308                  list($gotnote['text'], $gotnote['format']) = external_format_text($note->content,
 309                                                                                    $note->format,
 310                                                                                    $context->id,
 311                                                                                    'notes',
 312                                                                                    '',
 313                                                                                    '');
 314                  $gotnote['noteid'] = $note->id;
 315                  $gotnote['userid'] = $note->userid;
 316                  $gotnote['publishstate'] = $note->publishstate;
 317                  $gotnote['courseid'] = $note->courseid;
 318                  $resultnotes["notes"][] = $gotnote;
 319              } else {
 320                  $resultnotes["warnings"][] = array('item' => 'note',
 321                                                     'itemid' => $noteid,
 322                                                     'warningcode' => 'badid',
 323                                                     'message' => 'Note does not exist');
 324              }
 325          }
 326          return $resultnotes;
 327      }
 328  
 329      /**
 330       * Returns description of get_notes result value.
 331       *
 332       * @return external_description
 333       * @since Moodle 2.5
 334       */
 335      public static function get_notes_returns() {
 336          return new external_single_structure(
 337              array(
 338                  'notes' => new external_multiple_structure(
 339                      new external_single_structure(
 340                          array(
 341                              'noteid' => new external_value(PARAM_INT, 'id of the note', VALUE_OPTIONAL),
 342                              'userid' => new external_value(PARAM_INT, 'id of the user the note is about', VALUE_OPTIONAL),
 343                              'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\'', VALUE_OPTIONAL),
 344                              'courseid' => new external_value(PARAM_INT, 'course id of the note', VALUE_OPTIONAL),
 345                              'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML', VALUE_OPTIONAL),
 346                              'format' => new external_format_value('text', VALUE_OPTIONAL),
 347                          ), 'note'
 348                      )
 349                   ),
 350                   'warnings' => new external_warnings('item is always \'note\'',
 351                          'When errorcode is savedfailed the note could not be modified.' .
 352                          'When errorcode is badparam, an incorrect parameter was provided.' .
 353                          'When errorcode is badid, the note does not exist',
 354                          'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)')
 355              )
 356          );
 357      }
 358  
 359      /**
 360       * Returns description of update_notes parameters.
 361       *
 362       * @return external_function_parameters
 363       * @since Moodle 2.5
 364       */
 365      public static function update_notes_parameters() {
 366          return new external_function_parameters(
 367              array(
 368                  'notes' => new external_multiple_structure(
 369                      new external_single_structure(
 370                          array(
 371                              'id' => new external_value(PARAM_INT, 'id of the note'),
 372                              'publishstate' => new external_value(PARAM_ALPHA, '\'personal\', \'course\' or \'site\''),
 373                              'text' => new external_value(PARAM_RAW, 'the text of the message - text or HTML'),
 374                              'format' => new external_format_value('text', VALUE_DEFAULT),
 375                          )
 376                      ), "Array of Notes", VALUE_DEFAULT, array()
 377                  )
 378              )
 379          );
 380      }
 381  
 382      /**
 383       * Update notes about users.
 384       *
 385       * @param array $notes An array of ids for the notes to update.
 386       * @return array fail infos.
 387       * @since Moodle 2.2
 388       */
 389      public static function update_notes($notes = array()) {
 390          global $CFG, $DB;
 391          require_once($CFG->dirroot . "/notes/lib.php");
 392  
 393          $params = self::validate_parameters(self::update_notes_parameters(), array('notes' => $notes));
 394  
 395          // Check if note system is enabled.
 396          if (!$CFG->enablenotes) {
 397              throw new moodle_exception('notesdisabled', 'notes');
 398          }
 399  
 400          $warnings = array();
 401          foreach ($params['notes'] as $note) {
 402              $notedetails = note_load($note['id']);
 403              if (isset($notedetails->id)) {
 404                  // Ensure the current user is allowed to run this function.
 405                  $context = context_course::instance($notedetails->courseid);
 406                  self::validate_context($context);
 407                  require_capability('moodle/notes:manage', $context);
 408  
 409                  $dbnote = new stdClass;
 410                  $dbnote->id = $note['id'];
 411                  $dbnote->content = $note['text'];
 412                  $dbnote->format = external_validate_format($note['format']);
 413                  // Get the state ('personal', 'course', 'site').
 414                  switch ($note['publishstate']) {
 415                      case 'personal':
 416                          $dbnote->publishstate = NOTES_STATE_DRAFT;
 417                          break;
 418                      case 'course':
 419                          $dbnote->publishstate = NOTES_STATE_PUBLIC;
 420                          break;
 421                      case 'site':
 422                          $dbnote->publishstate = NOTES_STATE_SITE;
 423                          $dbnote->courseid = SITEID;
 424                          break;
 425                      default:
 426                          $warnings[] = array('item' => 'note',
 427                                              'itemid' => $note["id"],
 428                                              'warningcode' => 'badparam',
 429                                              'message' => 'Provided publishstate incorrect');
 430                          break;
 431                  }
 432                  if (!note_save($dbnote)) {
 433                      $warnings[] = array('item' => 'note',
 434                                          'itemid' => $note["id"],
 435                                          'warningcode' => 'savedfailed',
 436                                          'message' => 'Note could not be modified');
 437                  }
 438              } else {
 439                  $warnings[] = array('item' => 'note',
 440                                      'itemid' => $note["id"],
 441                                      'warningcode' => 'badid',
 442                                      'message' => 'Note does not exist');
 443              }
 444          }
 445          return $warnings;
 446      }
 447  
 448      /**
 449       * Returns description of update_notes result value.
 450       *
 451       * @return external_description
 452       * @since Moodle 2.5
 453       */
 454      public static function update_notes_returns() {
 455          return new external_warnings('item is always \'note\'',
 456                              'When errorcode is savedfailed the note could not be modified.' .
 457                              'When errorcode is badparam, an incorrect parameter was provided.' .
 458                              'When errorcode is badid, the note does not exist',
 459                              'errorcode can be badparam (incorrect parameter), savedfailed (could not be modified), or badid (note does not exist)');
 460      }
 461  }
 462  
 463  /**
 464   * Deprecated notes external functions
 465   *
 466   * @package    core_notes
 467   * @copyright  2011 Jerome Mouneyrac
 468   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 469   * @since Moodle 2.1
 470   * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
 471   * @see core_notes_external
 472   */
 473  class moodle_notes_external extends external_api {
 474  
 475      /**
 476       * Returns description of method parameters
 477       *
 478       * @return external_function_parameters
 479       * @since Moodle 2.1
 480       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
 481       * @see core_notes_external::create_notes_parameters()
 482       */
 483      public static function create_notes_parameters() {
 484          return core_notes_external::create_notes_parameters();
 485      }
 486  
 487      /**
 488       * Create notes about some users
 489       * Note: code should be matching the /notes/edit.php checks
 490       * and the /user/addnote.php checks. (they are similar cheks)
 491       *
 492       * @param array $notes  An array of notes to create.
 493       * @return array (success infos and fail infos)
 494       * @since Moodle 2.1
 495       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
 496       * @see core_notes_external::create_notes()
 497       */
 498      public static function create_notes($notes = array()) {
 499          return core_notes_external::create_notes($notes);
 500      }
 501  
 502      /**
 503       * Returns description of method result value
 504       *
 505       * @return external_description
 506       * @since Moodle 2.1
 507       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
 508       * @see core_notes_external::create_notes_returns()
 509       */
 510      public static function create_notes_returns() {
 511          return core_notes_external::create_notes_returns();
 512      }
 513  
 514  }

Search This Site: