Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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   * External user API
      19   *
      20   * @package    core_user
      21   * @category   external
      22   * @copyright  2009 Petr Skodak
      23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      24   */
      25  
      26  require_once("$CFG->libdir/externallib.php");
      27  
      28  /**
      29   * User external functions
      30   *
      31   * @package    core_user
      32   * @category   external
      33   * @copyright  2011 Jerome Mouneyrac
      34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      35   * @since Moodle 2.2
      36   */
      37  class core_user_external extends external_api {
      38  
      39      /**
      40       * Returns description of method parameters
      41       *
      42       * @return external_function_parameters
      43       * @since Moodle 2.2
      44       */
      45      public static function create_users_parameters() {
      46          global $CFG;
      47  
      48          return new external_function_parameters(
      49              array(
      50                  'users' => new external_multiple_structure(
      51                      new external_single_structure(
      52                          array(
      53                              'username' =>
      54                                  new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.'),
      55                              'password' =>
      56                                  new external_value(PARAM_RAW, 'Plain text password consisting of any characters'),
      57                              'firstname' =>
      58                                  new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
      59                              'lastname' =>
      60                                  new external_value(PARAM_NOTAGS, 'The family name of the user'),
      61                              'email' =>
      62                                  new external_value(PARAM_EMAIL, 'A valid and unique email address'),
      63                              'auth' =>
      64                                  new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT,
      65                                      'manual', NULL_NOT_ALLOWED),
      66                              'idnumber' =>
      67                                  new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
      68                                      VALUE_DEFAULT, ''),
      69                              'lang' =>
      70                                  new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT,
      71                                      $CFG->lang, NULL_NOT_ALLOWED),
      72                              'calendartype' =>
      73                                  new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
      74                                      VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL),
      75                              'theme' =>
      76                                  new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
      77                                      VALUE_OPTIONAL),
      78                              'timezone' =>
      79                                  new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
      80                                      VALUE_OPTIONAL),
      81                              'mailformat' =>
      82                                  new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
      83                                      VALUE_OPTIONAL),
      84                              'description' =>
      85                                  new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
      86                              'city' =>
      87                                  new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
      88                              'country' =>
      89                                  new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
      90                              'firstnamephonetic' =>
      91                                  new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
      92                              'lastnamephonetic' =>
      93                                  new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
      94                              'middlename' =>
      95                                  new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
      96                              'alternatename' =>
      97                                  new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
      98                              'preferences' => new external_multiple_structure(
      99                                  new external_single_structure(
     100                                      array(
     101                                          'type'  => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
     102                                          'value' => new external_value(PARAM_RAW, 'The value of the preference')
     103                                      )
     104                                  ), 'User preferences', VALUE_OPTIONAL),
     105                              'customfields' => new external_multiple_structure(
     106                                  new external_single_structure(
     107                                      array(
     108                                          'type'  => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
     109                                          'value' => new external_value(PARAM_RAW, 'The value of the custom field')
     110                                      )
     111                                  ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL)
     112                          )
     113                      )
     114                  )
     115              )
     116          );
     117      }
     118  
     119      /**
     120       * Create one or more users.
     121       *
     122       * @throws invalid_parameter_exception
     123       * @param array $users An array of users to create.
     124       * @return array An array of arrays
     125       * @since Moodle 2.2
     126       */
     127      public static function create_users($users) {
     128          global $CFG, $DB;
     129          require_once($CFG->dirroot."/lib/weblib.php");
     130          require_once($CFG->dirroot."/user/lib.php");
     131          require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.
     132  
     133          // Ensure the current user is allowed to run this function.
     134          $context = context_system::instance();
     135          self::validate_context($context);
     136          require_capability('moodle/user:create', $context);
     137  
     138          // Do basic automatic PARAM checks on incoming data, using params description.
     139          // If any problems are found then exceptions are thrown with helpful error messages.
     140          $params = self::validate_parameters(self::create_users_parameters(), array('users' => $users));
     141  
     142          $availableauths  = core_component::get_plugin_list('auth');
     143          unset($availableauths['mnet']);       // These would need mnethostid too.
     144          unset($availableauths['webservice']); // We do not want new webservice users for now.
     145  
     146          $availablethemes = core_component::get_plugin_list('theme');
     147          $availablelangs  = get_string_manager()->get_list_of_translations();
     148  
     149          $transaction = $DB->start_delegated_transaction();
     150  
     151          $userids = array();
     152          foreach ($params['users'] as $user) {
     153              // Make sure that the username doesn't already exist.
     154              if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
     155                  throw new invalid_parameter_exception('Username already exists: '.$user['username']);
     156              }
     157  
     158              // Make sure auth is valid.
     159              if (empty($availableauths[$user['auth']])) {
     160                  throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
     161              }
     162  
     163              // Make sure lang is valid.
     164              if (empty($availablelangs[$user['lang']])) {
     165                  throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
     166              }
     167  
     168              // Make sure lang is valid.
     169              if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { // Theme is VALUE_OPTIONAL,
     170                                                                                       // so no default value
     171                                                                                       // We need to test if the client sent it
     172                                                                                       // => !empty($user['theme']).
     173                  throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
     174              }
     175  
     176              $user['confirmed'] = true;
     177              $user['mnethostid'] = $CFG->mnet_localhost_id;
     178  
     179              // Start of user info validation.
     180              // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation().
     181              if (!validate_email($user['email'])) {
     182                  throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
     183              } else if ($DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) {
     184                  throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
     185              }
     186              // End of user info validation.
     187  
     188              // Create the user data now!
     189              $user['id'] = user_create_user($user, true, false);
     190  
     191              // Custom fields.
     192              if (!empty($user['customfields'])) {
     193                  foreach ($user['customfields'] as $customfield) {
     194                      // Profile_save_data() saves profile file it's expecting a user with the correct id,
     195                      // and custom field to be named profile_field_"shortname".
     196                      $user["profile_field_".$customfield['type']] = $customfield['value'];
     197                  }
     198                  profile_save_data((object) $user);
     199              }
     200  
     201              // Trigger event.
     202              \core\event\user_created::create_from_userid($user['id'])->trigger();
     203  
     204              // Preferences.
     205              if (!empty($user['preferences'])) {
     206                  foreach ($user['preferences'] as $preference) {
     207                      set_user_preference($preference['type'], $preference['value'], $user['id']);
     208                  }
     209              }
     210  
     211              $userids[] = array('id' => $user['id'], 'username' => $user['username']);
     212          }
     213  
     214          $transaction->allow_commit();
     215  
     216          return $userids;
     217      }
     218  
     219      /**
     220       * Returns description of method result value
     221       *
     222       * @return external_description
     223       * @since Moodle 2.2
     224       */
     225      public static function create_users_returns() {
     226          return new external_multiple_structure(
     227              new external_single_structure(
     228                  array(
     229                      'id'       => new external_value(PARAM_INT, 'user id'),
     230                      'username' => new external_value(PARAM_USERNAME, 'user name'),
     231                  )
     232              )
     233          );
     234      }
     235  
     236  
     237      /**
     238       * Returns description of method parameters
     239       *
     240       * @return external_function_parameters
     241       * @since Moodle 2.2
     242       */
     243      public static function delete_users_parameters() {
     244          return new external_function_parameters(
     245              array(
     246                  'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
     247              )
     248          );
     249      }
     250  
     251      /**
     252       * Delete users
     253       *
     254       * @throws moodle_exception
     255       * @param array $userids
     256       * @return null
     257       * @since Moodle 2.2
     258       */
     259      public static function delete_users($userids) {
     260          global $CFG, $DB, $USER;
     261          require_once($CFG->dirroot."/user/lib.php");
     262  
     263          // Ensure the current user is allowed to run this function.
     264          $context = context_system::instance();
     265          require_capability('moodle/user:delete', $context);
     266          self::validate_context($context);
     267  
     268          $params = self::validate_parameters(self::delete_users_parameters(), array('userids' => $userids));
     269  
     270          $transaction = $DB->start_delegated_transaction();
     271  
     272          foreach ($params['userids'] as $userid) {
     273              $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST);
     274              // Must not allow deleting of admins or self!!!
     275              if (is_siteadmin($user)) {
     276                  throw new moodle_exception('useradminodelete', 'error');
     277              }
     278              if ($USER->id == $user->id) {
     279                  throw new moodle_exception('usernotdeletederror', 'error');
     280              }
     281              user_delete_user($user);
     282          }
     283  
     284          $transaction->allow_commit();
     285  
     286          return null;
     287      }
     288  
     289      /**
     290       * Returns description of method result value
     291       *
     292       * @return null
     293       * @since Moodle 2.2
     294       */
     295      public static function delete_users_returns() {
     296          return null;
     297      }
     298  
     299  
     300      /**
     301       * Returns description of method parameters
     302       *
     303       * @return external_function_parameters
     304       * @since Moodle 2.2
     305       */
     306      public static function update_users_parameters() {
     307          return new external_function_parameters(
     308              array(
     309                  'users' => new external_multiple_structure(
     310                      new external_single_structure(
     311                          array(
     312                              'id' =>
     313                                  new external_value(PARAM_INT, 'ID of the user'),
     314                              'username' =>
     315                                  new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.',
     316                                      VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
     317                              'password' =>
     318                                  new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL,
     319                                      '', NULL_NOT_ALLOWED),
     320                              'firstname' =>
     321                                  new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',
     322                                      NULL_NOT_ALLOWED),
     323                              'lastname' =>
     324                                  new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
     325                              'email' =>
     326                                  new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',
     327                                      NULL_NOT_ALLOWED),
     328                              'auth' =>
     329                                  new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '',
     330                                      NULL_NOT_ALLOWED),
     331                              'idnumber' =>
     332                                  new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
     333                                      VALUE_OPTIONAL),
     334                              'lang' =>
     335                                  new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server',
     336                                      VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
     337                              'calendartype' =>
     338                                  new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
     339                                      VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
     340                              'theme' =>
     341                                  new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
     342                                      VALUE_OPTIONAL),
     343                              'timezone' =>
     344                                  new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
     345                                      VALUE_OPTIONAL),
     346                              'mailformat' =>
     347                                  new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
     348                                      VALUE_OPTIONAL),
     349                              'description' =>
     350                                  new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
     351                              'city' =>
     352                                  new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
     353                              'country' =>
     354                                  new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
     355                              'firstnamephonetic' =>
     356                                  new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
     357                              'lastnamephonetic' =>
     358                                  new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
     359                              'middlename' =>
     360                                  new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
     361                              'alternatename' =>
     362                                  new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
     363                              'customfields' => new external_multiple_structure(
     364                                  new external_single_structure(
     365                                      array(
     366                                          'type'  => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
     367                                          'value' => new external_value(PARAM_RAW, 'The value of the custom field')
     368                                      )
     369                                  ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
     370                              'preferences' => new external_multiple_structure(
     371                                  new external_single_structure(
     372                                      array(
     373                                          'type'  => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
     374                                          'value' => new external_value(PARAM_RAW, 'The value of the preference')
     375                                      )
     376                                  ), 'User preferences', VALUE_OPTIONAL),
     377                          )
     378                      )
     379                  )
     380              )
     381          );
     382      }
     383  
     384      /**
     385       * Update users
     386       *
     387       * @param array $users
     388       * @return null
     389       * @since Moodle 2.2
     390       */
     391      public static function update_users($users) {
     392          global $CFG, $DB;
     393          require_once($CFG->dirroot."/user/lib.php");
     394          require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.
     395  
     396          // Ensure the current user is allowed to run this function.
     397          $context = context_system::instance();
     398          require_capability('moodle/user:update', $context);
     399          self::validate_context($context);
     400  
     401          $params = self::validate_parameters(self::update_users_parameters(), array('users' => $users));
     402  
     403          $transaction = $DB->start_delegated_transaction();
     404  
     405          foreach ($params['users'] as $user) {
     406              user_update_user($user, true, false);
     407              // Update user custom fields.
     408              if (!empty($user['customfields'])) {
     409  
     410                  foreach ($user['customfields'] as $customfield) {
     411                      // Profile_save_data() saves profile file it's expecting a user with the correct id,
     412                      // and custom field to be named profile_field_"shortname".
     413                      $user["profile_field_".$customfield['type']] = $customfield['value'];
     414                  }
     415                  profile_save_data((object) $user);
     416              }
     417  
     418              // Trigger event.
     419              \core\event\user_updated::create_from_userid($user['id'])->trigger();
     420  
     421              // Preferences.
     422              if (!empty($user['preferences'])) {
     423                  foreach ($user['preferences'] as $preference) {
     424                      set_user_preference($preference['type'], $preference['value'], $user['id']);
     425                  }
     426              }
     427          }
     428  
     429          $transaction->allow_commit();
     430  
     431          return null;
     432      }
     433  
     434      /**
     435       * Returns description of method result value
     436       *
     437       * @return null
     438       * @since Moodle 2.2
     439       */
     440      public static function update_users_returns() {
     441          return null;
     442      }
     443  
     444      /**
     445       * Returns description of method parameters
     446       *
     447       * @return external_function_parameters
     448       * @since Moodle 2.4
     449       */
     450      public static function get_users_by_field_parameters() {
     451          return new external_function_parameters(
     452              array(
     453                  'field' => new external_value(PARAM_ALPHA, 'the search field can be
     454                      \'id\' or \'idnumber\' or \'username\' or \'email\''),
     455                  'values' => new external_multiple_structure(
     456                          new external_value(PARAM_RAW, 'the value to match'))
     457              )
     458          );
     459      }
     460  
     461      /**
     462       * Get user information for a unique field.
     463       *
     464       * @throws coding_exception
     465       * @throws invalid_parameter_exception
     466       * @param string $field
     467       * @param array $values
     468       * @return array An array of arrays containg user profiles.
     469       * @since Moodle 2.4
     470       */
     471      public static function get_users_by_field($field, $values) {
     472          global $CFG, $USER, $DB;
     473          require_once($CFG->dirroot . "/user/lib.php");
     474  
     475          $params = self::validate_parameters(self::get_users_by_field_parameters(),
     476                  array('field' => $field, 'values' => $values));
     477  
     478          // This array will keep all the users that are allowed to be searched,
     479          // according to the current user's privileges.
     480          $cleanedvalues = array();
     481  
     482          switch ($field) {
     483              case 'id':
     484                  $paramtype = PARAM_INT;
     485                  break;
     486              case 'idnumber':
     487                  $paramtype = PARAM_RAW;
     488                  break;
     489              case 'username':
     490                  $paramtype = PARAM_RAW;
     491                  break;
     492              case 'email':
     493                  $paramtype = PARAM_EMAIL;
     494                  break;
     495              default:
     496                  throw new coding_exception('invalid field parameter',
     497                          'The search field \'' . $field . '\' is not supported, look at the web service documentation');
     498          }
     499  
     500          // Clean the values.
     501          foreach ($values as $value) {
     502              $cleanedvalue = clean_param($value, $paramtype);
     503              if ( $value != $cleanedvalue) {
     504                  throw new invalid_parameter_exception('The field \'' . $field .
     505                          '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
     506              }
     507              $cleanedvalues[] = $cleanedvalue;
     508          }
     509  
     510          // Retrieve the users.
     511          $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');
     512  
     513          // Finally retrieve each users information.
     514          $returnedusers = array();
     515          foreach ($users as $user) {
     516              $userdetails = user_get_user_details_courses($user);
     517  
     518              // Return the user only if the searched field is returned.
     519              // Otherwise it means that the $USER was not allowed to search the returned user.
     520              if (!empty($userdetails) and !empty($userdetails[$field])) {
     521                  $returnedusers[] = $userdetails;
     522              }
     523          }
     524  
     525          return $returnedusers;
     526      }
     527  
     528      /**
     529       * Returns description of method result value
     530       *
     531       * @return external_multiple_structure
     532       * @since Moodle 2.4
     533       */
     534      public static function get_users_by_field_returns() {
     535          return new external_multiple_structure(self::user_description());
     536      }
     537  
     538  
     539      /**
     540       * Returns description of get_users() parameters.
     541       *
     542       * @return external_function_parameters
     543       * @since Moodle 2.5
     544       */
     545      public static function get_users_parameters() {
     546          return new external_function_parameters(
     547              array(
     548                  'criteria' => new external_multiple_structure(
     549                      new external_single_structure(
     550                          array(
     551                              'key' => new external_value(PARAM_ALPHA, 'the user column to search, expected keys (value format) are:
     552                                  "id" (int) matching user id,
     553                                  "lastname" (string) user last name (Note: you can use % for searching but it may be considerably slower!),
     554                                  "firstname" (string) user first name (Note: you can use % for searching but it may be considerably slower!),
     555                                  "idnumber" (string) matching user idnumber,
     556                                  "username" (string) matching user username,
     557                                  "email" (string) user email (Note: you can use % for searching but it may be considerably slower!),
     558                                  "auth" (string) matching user auth plugin'),
     559                              'value' => new external_value(PARAM_RAW, 'the value to search')
     560                          )
     561                      ), 'the key/value pairs to be considered in user search. Values can not be empty.
     562                          Specify different keys only once (fullname => \'user1\', auth => \'manual\', ...) -
     563                          key occurences are forbidden.
     564                          The search is executed with AND operator on the criterias. Invalid criterias (keys) are ignored,
     565                          the search is still executed on the valid criterias.
     566                          You can search without criteria, but the function is not designed for it.
     567                          It could very slow or timeout. The function is designed to search some specific users.'
     568                  )
     569              )
     570          );
     571      }
     572  
     573      /**
     574       * Retrieve matching user.
     575       *
     576       * @throws moodle_exception
     577       * @param array $criteria the allowed array keys are id/lastname/firstname/idnumber/username/email/auth.
     578       * @return array An array of arrays containing user profiles.
     579       * @since Moodle 2.5
     580       */
     581      public static function get_users($criteria = array()) {
     582          global $CFG, $USER, $DB;
     583  
     584          require_once($CFG->dirroot . "/user/lib.php");
     585  
     586          $params = self::validate_parameters(self::get_users_parameters(),
     587                  array('criteria' => $criteria));
     588  
     589          // Validate the criteria and retrieve the users.
     590          $users = array();
     591          $warnings = array();
     592          $sqlparams = array();
     593          $usedkeys = array();
     594  
     595          // Do not retrieve deleted users.
     596          $sql = ' deleted = 0';
     597  
     598          foreach ($params['criteria'] as $criteriaindex => $criteria) {
     599  
     600              // Check that the criteria has never been used.
     601              if (array_key_exists($criteria['key'], $usedkeys)) {
     602                  throw new moodle_exception('keyalreadyset', '', '', null, 'The key ' . $criteria['key'] . ' can only be sent once');
     603              } else {
     604                  $usedkeys[$criteria['key']] = true;
     605              }
     606  
     607              $invalidcriteria = false;
     608              // Clean the parameters.
     609              $paramtype = PARAM_RAW;
     610              switch ($criteria['key']) {
     611                  case 'id':
     612                      $paramtype = PARAM_INT;
     613                      break;
     614                  case 'idnumber':
     615                      $paramtype = PARAM_RAW;
     616                      break;
     617                  case 'username':
     618                      $paramtype = PARAM_RAW;
     619                      break;
     620                  case 'email':
     621                      // We use PARAM_RAW to allow searches with %.
     622                      $paramtype = PARAM_RAW;
     623                      break;
     624                  case 'auth':
     625                      $paramtype = PARAM_AUTH;
     626                      break;
     627                  case 'lastname':
     628                  case 'firstname':
     629                      $paramtype = PARAM_TEXT;
     630                      break;
     631                  default:
     632                      // Send back a warning that this search key is not supported in this version.
     633                      // This warning will make the function extandable without breaking clients.
     634                      $warnings[] = array(
     635                          'item' => $criteria['key'],
     636                          'warningcode' => 'invalidfieldparameter',
     637                          'message' =>
     638                              'The search key \'' . $criteria['key'] . '\' is not supported, look at the web service documentation'
     639                      );
     640                      // Do not add this invalid criteria to the created SQL request.
     641                      $invalidcriteria = true;
     642                      unset($params['criteria'][$criteriaindex]);
     643                      break;
     644              }
     645  
     646              if (!$invalidcriteria) {
     647                  $cleanedvalue = clean_param($criteria['value'], $paramtype);
     648  
     649                  $sql .= ' AND ';
     650  
     651                  // Create the SQL.
     652                  switch ($criteria['key']) {
     653                      case 'id':
     654                      case 'idnumber':
     655                      case 'username':
     656                      case 'auth':
     657                          $sql .= $criteria['key'] . ' = :' . $criteria['key'];
     658                          $sqlparams[$criteria['key']] = $cleanedvalue;
     659                          break;
     660                      case 'email':
     661                      case 'lastname':
     662                      case 'firstname':
     663                          $sql .= $DB->sql_like($criteria['key'], ':' . $criteria['key'], false);
     664                          $sqlparams[$criteria['key']] = $cleanedvalue;
     665                          break;
     666                      default:
     667                          break;
     668                  }
     669              }
     670          }
     671  
     672          $users = $DB->get_records_select('user', $sql, $sqlparams, 'id ASC');
     673  
     674          // Finally retrieve each users information.
     675          $returnedusers = array();
     676          foreach ($users as $user) {
     677              $userdetails = user_get_user_details_courses($user);
     678  
     679              // Return the user only if all the searched fields are returned.
     680              // Otherwise it means that the $USER was not allowed to search the returned user.
     681              if (!empty($userdetails)) {
     682                  $validuser = true;
     683  
     684                  foreach ($params['criteria'] as $criteria) {
     685                      if (empty($userdetails[$criteria['key']])) {
     686                          $validuser = false;
     687                      }
     688                  }
     689  
     690                  if ($validuser) {
     691                      $returnedusers[] = $userdetails;
     692                  }
     693              }
     694          }
     695  
     696          return array('users' => $returnedusers, 'warnings' => $warnings);
     697      }
     698  
     699      /**
     700       * Returns description of get_users result value.
     701       *
     702       * @return external_description
     703       * @since Moodle 2.5
     704       */
     705      public static function get_users_returns() {
     706          return new external_single_structure(
     707              array('users' => new external_multiple_structure(
     708                                  self::user_description()
     709                               ),
     710                    'warnings' => new external_warnings('always set to \'key\'', 'faulty key name')
     711              )
     712          );
     713      }
     714  
     715      /**
     716       * Returns description of method parameters
     717       *
     718       * @return external_function_parameters
     719       * @since Moodle 2.2
     720       * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
     721       * @see core_user_external::get_users_by_field_parameters()
     722       */
     723      public static function get_users_by_id_parameters() {
     724          return new external_function_parameters(
     725              array(
     726                  'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
     727              )
     728          );
     729      }
     730  
     731      /**
     732       * Get user information
     733       * - This function is matching the permissions of /user/profil.php
     734       * - It is also matching some permissions from /user/editadvanced.php for the following fields:
     735       *   auth, confirmed, idnumber, lang, theme, timezone, mailformat
     736       *
     737       * @param array $userids  array of user ids
     738       * @return array An array of arrays describing users
     739       * @since Moodle 2.2
     740       * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
     741       * @see core_user_external::get_users_by_field()
     742       */
     743      public static function get_users_by_id($userids) {
     744          global $CFG, $USER, $DB;
     745          require_once($CFG->dirroot . "/user/lib.php");
     746  
     747          $params = self::validate_parameters(self::get_users_by_id_parameters(),
     748                  array('userids' => $userids));
     749  
     750          list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
     751          $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
     752          $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
     753          $params['contextlevel'] = CONTEXT_USER;
     754          $usersql = "SELECT u.* $uselect
     755                        FROM {user} u $ujoin
     756                       WHERE u.id $sqluserids";
     757          $users = $DB->get_recordset_sql($usersql, $params);
     758  
     759          $result = array();
     760          $hasuserupdatecap = has_capability('moodle/user:update', context_system::instance());
     761          foreach ($users as $user) {
     762              if (!empty($user->deleted)) {
     763                  continue;
     764              }
     765              context_helper::preload_from_record($user);
     766              $usercontext = context_user::instance($user->id, IGNORE_MISSING);
     767              self::validate_context($usercontext);
     768              $currentuser = ($user->id == $USER->id);
     769  
     770              if ($userarray  = user_get_user_details($user)) {
     771                  // Fields matching permissions from /user/editadvanced.php.
     772                  if ($currentuser or $hasuserupdatecap) {
     773                      $userarray['auth']       = $user->auth;
     774                      $userarray['confirmed']  = $user->confirmed;
     775                      $userarray['idnumber']   = $user->idnumber;
     776                      $userarray['lang']       = $user->lang;
     777                      $userarray['theme']      = $user->theme;
     778                      $userarray['timezone']   = $user->timezone;
     779                      $userarray['mailformat'] = $user->mailformat;
     780                  }
     781                  $result[] = $userarray;
     782              }
     783          }
     784          $users->close();
     785  
     786          return $result;
     787      }
     788  
     789      /**
     790       * Returns description of method result value
     791       *
     792       * @return external_description
     793       * @since Moodle 2.2
     794       * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
     795       * @see core_user_external::get_users_by_field_returns()
     796       */
     797      public static function get_users_by_id_returns() {
     798          $additionalfields = array (
     799              'enrolledcourses' => new external_multiple_structure(
     800              new external_single_structure(
     801                  array(
     802                      'id'  => new external_value(PARAM_INT, 'Id of the course'),
     803                      'fullname'  => new external_value(PARAM_RAW, 'Fullname of the course'),
     804                      'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
     805                  )
     806              ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL));
     807          return new external_multiple_structure(self::user_description($additionalfields));
     808      }
     809  
     810      /**
     811       * Returns description of method parameters
     812       *
     813       * @return external_function_parameters
     814       * @since Moodle 2.2
     815       */
     816      public static function get_course_user_profiles_parameters() {
     817          return new external_function_parameters(
     818              array(
     819                  'userlist' => new external_multiple_structure(
     820                      new external_single_structure(
     821                          array(
     822                              'userid'    => new external_value(PARAM_INT, 'userid'),
     823                              'courseid'    => new external_value(PARAM_INT, 'courseid'),
     824                          )
     825                      )
     826                  )
     827              )
     828          );
     829      }
     830  
     831      /**
     832       * Get course participant's details
     833       *
     834       * @param array $userlist  array of user ids and according course ids
     835       * @return array An array of arrays describing course participants
     836       * @since Moodle 2.2
     837       */
     838      public static function get_course_user_profiles($userlist) {
     839          global $CFG, $USER, $DB;
     840          require_once($CFG->dirroot . "/user/lib.php");
     841          $params = self::validate_parameters(self::get_course_user_profiles_parameters(), array('userlist' => $userlist));
     842  
     843          $userids = array();
     844          $courseids = array();
     845          foreach ($params['userlist'] as $value) {
     846              $userids[] = $value['userid'];
     847              $courseids[$value['userid']] = $value['courseid'];
     848          }
     849  
     850          // Cache all courses.
     851          $courses = array();
     852          list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids), SQL_PARAMS_NAMED);
     853          $cselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
     854          $cjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
     855          $params['contextlevel'] = CONTEXT_COURSE;
     856          $coursesql = "SELECT c.* $cselect
     857                          FROM {course} c $cjoin
     858                         WHERE c.id $sqlcourseids";
     859          $rs = $DB->get_recordset_sql($coursesql, $params);
     860          foreach ($rs as $course) {
     861              // Adding course contexts to cache.
     862              context_helper::preload_from_record($course);
     863              // Cache courses.
     864              $courses[$course->id] = $course;
     865          }
     866          $rs->close();
     867  
     868          list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
     869          $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
     870          $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
     871          $params['contextlevel'] = CONTEXT_USER;
     872          $usersql = "SELECT u.* $uselect
     873                        FROM {user} u $ujoin
     874                       WHERE u.id $sqluserids";
     875          $users = $DB->get_recordset_sql($usersql, $params);
     876          $result = array();
     877          foreach ($users as $user) {
     878              if (!empty($user->deleted)) {
     879                  continue;
     880              }
     881              context_helper::preload_from_record($user);
     882              $course = $courses[$courseids[$user->id]];
     883              $context = context_course::instance($courseids[$user->id], IGNORE_MISSING);
     884              self::validate_context($context);
     885              if ($userarray = user_get_user_details($user, $course)) {
     886                  $result[] = $userarray;
     887              }
     888          }
     889  
     890          $users->close();
     891  
     892          return $result;
     893      }
     894  
     895      /**
     896       * Returns description of method result value
     897       *
     898       * @return external_description
     899       * @since Moodle 2.2
     900       */
     901      public static function get_course_user_profiles_returns() {
     902          $additionalfields = array(
     903              'groups' => new external_multiple_structure(
     904                  new external_single_structure(
     905                      array(
     906                          'id'  => new external_value(PARAM_INT, 'group id'),
     907                          'name' => new external_value(PARAM_RAW, 'group name'),
     908                          'description' => new external_value(PARAM_RAW, 'group description'),
     909                          'descriptionformat' => new external_format_value('description'),
     910                      )
     911                  ), 'user groups', VALUE_OPTIONAL),
     912              'roles' => new external_multiple_structure(
     913                  new external_single_structure(
     914                      array(
     915                          'roleid'       => new external_value(PARAM_INT, 'role id'),
     916                          'name'         => new external_value(PARAM_RAW, 'role name'),
     917                          'shortname'    => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
     918                          'sortorder'    => new external_value(PARAM_INT, 'role sortorder')
     919                      )
     920                  ), 'user roles', VALUE_OPTIONAL),
     921              'enrolledcourses' => new external_multiple_structure(
     922                  new external_single_structure(
     923                      array(
     924                          'id'  => new external_value(PARAM_INT, 'Id of the course'),
     925                          'fullname'  => new external_value(PARAM_RAW, 'Fullname of the course'),
     926                          'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
     927                      )
     928                  ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
     929          );
     930  
     931          return new external_multiple_structure(self::user_description($additionalfields));
     932      }
     933  
     934      /**
     935       * Create user return value description.
     936       *
     937       * @param array $additionalfields some additional field
     938       * @return single_structure_description
     939       */
     940      public static function user_description($additionalfields = array()) {
     941          $userfields = array(
     942              'id'    => new external_value(PARAM_INT, 'ID of the user'),
     943              'username'    => new external_value(PARAM_RAW, 'The username', VALUE_OPTIONAL),
     944              'firstname'   => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
     945              'lastname'    => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
     946              'fullname'    => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
     947              'email'       => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
     948              'address'     => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
     949              'phone1'      => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
     950              'phone2'      => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
     951              'icq'         => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
     952              'skype'       => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
     953              'yahoo'       => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
     954              'aim'         => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
     955              'msn'         => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
     956              'department'  => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
     957              'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
     958              'idnumber'    => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
     959              'interests'   => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
     960              'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
     961              'lastaccess'  => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
     962              'auth'        => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
     963              'confirmed'   => new external_value(PARAM_INT, 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
     964              'lang'        => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
     965              'calendartype' => new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL),
     966              'theme'       => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
     967              'timezone'    => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
     968              'mailformat'  => new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
     969              'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
     970              'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
     971              'city'        => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
     972              'url'         => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
     973              'country'     => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
     974              'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
     975              'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
     976              'customfields' => new external_multiple_structure(
     977                  new external_single_structure(
     978                      array(
     979                          'type'  => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
     980                          'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
     981                          'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
     982                          'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
     983                      )
     984                  ), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL),
     985              'preferences' => new external_multiple_structure(
     986                  new external_single_structure(
     987                      array(
     988                          'name'  => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
     989                          'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
     990                      )
     991              ), 'Users preferences', VALUE_OPTIONAL)
     992          );
     993          if (!empty($additionalfields)) {
     994              $userfields = array_merge($userfields, $additionalfields);
     995          }
     996          return new external_single_structure($userfields);
     997      }
     998  
     999      /**
    1000       * Returns description of method parameters
    1001       *
    1002       * @return external_function_parameters
    1003       * @since Moodle 2.6
    1004       */
    1005      public static function add_user_private_files_parameters() {
    1006          return new external_function_parameters(
    1007              array(
    1008                  'draftid' => new external_value(PARAM_INT, 'draft area id')
    1009              )
    1010          );
    1011      }
    1012  
    1013      /**
    1014       * Copy files from a draft area to users private files area.
    1015       *
    1016       * @throws invalid_parameter_exception
    1017       * @param int $draftid Id of a draft area containing files.
    1018       * @return array An array of warnings
    1019       * @since Moodle 2.6
    1020       */
    1021      public static function add_user_private_files($draftid) {
    1022          global $CFG, $USER, $DB;
    1023  
    1024          require_once($CFG->dirroot . "/user/lib.php");
    1025          $params = self::validate_parameters(self::add_user_private_files_parameters(), array('draftid' => $draftid));
    1026  
    1027          if (isguestuser()) {
    1028              throw new invalid_parameter_exception('Guest users cannot upload files');
    1029          }
    1030  
    1031          $context = context_user::instance($USER->id);
    1032          require_capability('moodle/user:manageownfiles', $context);
    1033  
    1034          $maxbytes = $CFG->userquota;
    1035          $maxareabytes = $CFG->userquota;
    1036          if (has_capability('moodle/user:ignoreuserquota', $context)) {
    1037              $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
    1038              $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
    1039          }
    1040  
    1041          $options = array('subdirs' => 1,
    1042                           'maxbytes' => $maxbytes,
    1043                           'maxfiles' => -1,
    1044                           'accepted_types' => '*',
    1045                           'areamaxbytes' => $maxareabytes);
    1046  
    1047          file_save_draft_area_files($draftid, $context->id, 'user', 'private', 0, $options);
    1048  
    1049          return null;
    1050      }
    1051  
    1052      /**
    1053       * Returns description of method result value
    1054       *
    1055       * @return external_description
    1056       * @since Moodle 2.2
    1057       */
    1058      public static function add_user_private_files_returns() {
    1059          return null;
    1060      }
    1061  
    1062      /**
    1063       * Returns description of method parameters.
    1064       *
    1065       * @return external_function_parameters
    1066       * @since Moodle 2.6
    1067       */
    1068      public static function add_user_device_parameters() {
    1069          return new external_function_parameters(
    1070              array(
    1071                  'appid'     => new external_value(PARAM_NOTAGS, 'the app id, usually something like com.moodle.moodlemobile'),
    1072                  'name'      => new external_value(PARAM_NOTAGS, 'the device name, \'occam\' or \'iPhone\' etc.'),
    1073                  'model'     => new external_value(PARAM_NOTAGS, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'),
    1074                  'platform'  => new external_value(PARAM_NOTAGS, 'the device platform \'iOS\' or \'Android\' etc.'),
    1075                  'version'   => new external_value(PARAM_NOTAGS, 'the device version \'6.1.2\' or \'4.2.2\' etc.'),
    1076                  'pushid'    => new external_value(PARAM_RAW, 'the device PUSH token/key/identifier/registration id'),
    1077                  'uuid'      => new external_value(PARAM_RAW, 'the device UUID')
    1078              )
    1079          );
    1080      }
    1081  
    1082      /**
    1083       * Add a user device in Moodle database (for PUSH notifications usually).
    1084       *
    1085       * @throws moodle_exception
    1086       * @param string $appid The app id, usually something like com.moodle.moodlemobile.
    1087       * @param string $name The device name, occam or iPhone etc.
    1088       * @param string $model The device model Nexus4 or iPad1.1 etc.
    1089       * @param string $platform The device platform iOs or Android etc.
    1090       * @param string $version The device version 6.1.2 or 4.2.2 etc.
    1091       * @param string $pushid The device PUSH token/key/identifier/registration id.
    1092       * @param string $uuid The device UUID.
    1093       * @return array List of possible warnings.
    1094       * @since Moodle 2.6
    1095       */
    1096      public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) {
    1097          global $CFG, $USER, $DB;
    1098          require_once($CFG->dirroot . "/user/lib.php");
    1099  
    1100          $params = self::validate_parameters(self::add_user_device_parameters(),
    1101                  array('appid' => $appid,
    1102                        'name' => $name,
    1103                        'model' => $model,
    1104                        'platform' => $platform,
    1105                        'version' => $version,
    1106                        'pushid' => $pushid,
    1107                        'uuid' => $uuid
    1108                        ));
    1109  
    1110          $warnings = array();
    1111  
    1112          // Prevent duplicate keys for users.
    1113          if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id))) {
    1114              $warnings['warning'][] = array(
    1115                  'item' => $params['pushid'],
    1116                  'warningcode' => 'existingkeyforthisuser',
    1117                  'message' => 'This key is already stored for this user'
    1118              );
    1119              return $warnings;
    1120          }
    1121  
    1122          // Notice that we can have multiple devices because previously it was allowed to have repeated ones.
    1123          // Since we don't have a clear way to decide which one is the more appropiate, we update all.
    1124          if ($userdevices = $DB->get_records('user_devices', array('uuid' => $params['uuid'],
    1125                  'appid' => $params['appid'], 'userid' => $USER->id))) {
    1126  
    1127              foreach ($userdevices as $userdevice) {
    1128                  $userdevice->version    = $params['version'];   // Maybe the user upgraded the device.
    1129                  $userdevice->pushid     = $params['pushid'];
    1130                  $userdevice->timemodified  = time();
    1131                  $DB->update_record('user_devices', $userdevice);
    1132              }
    1133  
    1134          } else {
    1135              $userdevice = new stdclass;
    1136              $userdevice->userid     = $USER->id;
    1137              $userdevice->appid      = $params['appid'];
    1138              $userdevice->name       = $params['name'];
    1139              $userdevice->model      = $params['model'];
    1140              $userdevice->platform   = $params['platform'];
    1141              $userdevice->version    = $params['version'];
    1142              $userdevice->pushid     = $params['pushid'];
    1143              $userdevice->uuid       = $params['uuid'];
    1144              $userdevice->timecreated  = time();
    1145              $userdevice->timemodified = $userdevice->timecreated;
    1146  
    1147              if (!$DB->insert_record('user_devices', $userdevice)) {
    1148                  throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']);
    1149              }
    1150          }
    1151  
    1152          return $warnings;
    1153      }
    1154  
    1155      /**
    1156       * Returns description of method result value.
    1157       *
    1158       * @return external_multiple_structure
    1159       * @since Moodle 2.6
    1160       */
    1161      public static function add_user_device_returns() {
    1162          return new external_multiple_structure(
    1163             new external_warnings()
    1164          );
    1165      }
    1166  
    1167  }
    1168  
    1169   /**
    1170    * Deprecated user external functions
    1171    *
    1172    * @package    core_user
    1173    * @copyright  2009 Petr Skodak
    1174    * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
    1175    * @since Moodle 2.0
    1176    * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
    1177    * @see core_user_external
    1178    */
    1179  class moodle_user_external extends external_api {
    1180  
    1181      /**
    1182       * Returns description of method parameters
    1183       *
    1184       * @return external_function_parameters
    1185       * @since Moodle 2.0
    1186       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1187       * @see core_user_external::create_users_parameters()
    1188       */
    1189      public static function create_users_parameters() {
    1190          return core_user_external::create_users_parameters();
    1191      }
    1192  
    1193      /**
    1194       * Create one or more users
    1195       *
    1196       * @param array $users  An array of users to create.
    1197       * @return array An array of arrays
    1198       * @since Moodle 2.0
    1199       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1200       * @see core_user_external::create_users()
    1201       */
    1202      public static function create_users($users) {
    1203          return core_user_external::create_users($users);
    1204      }
    1205  
    1206      /**
    1207       * Returns description of method result value
    1208       *
    1209       * @return external_description
    1210       * @since Moodle 2.0
    1211       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1212       * @see core_user_external::create_users_returns()
    1213       */
    1214      public static function create_users_returns() {
    1215          return core_user_external::create_users_returns();
    1216      }
    1217  
    1218  
    1219      /**
    1220       * Returns description of method parameters
    1221       *
    1222       * @return external_function_parameters
    1223       * @since Moodle 2.0
    1224       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1225       * @see core_user_external::delete_users_parameters()
    1226       */
    1227      public static function delete_users_parameters() {
    1228          return core_user_external::delete_users_parameters();
    1229      }
    1230  
    1231      /**
    1232       * Delete users
    1233       *
    1234       * @param array $userids
    1235       * @return null
    1236       * @since Moodle 2.0
    1237       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1238       * @see core_user_external::delete_users()
    1239       */
    1240      public static function delete_users($userids) {
    1241          return core_user_external::delete_users($userids);
    1242      }
    1243  
    1244      /**
    1245       * Returns description of method result value
    1246       *
    1247       * @return null
    1248       * @since Moodle 2.0
    1249       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1250       * @see core_user_external::delete_users_returns()
    1251       */
    1252      public static function delete_users_returns() {
    1253          return core_user_external::delete_users_returns();
    1254      }
    1255  
    1256  
    1257      /**
    1258       * Returns description of method parameters
    1259       *
    1260       * @return external_function_parameters
    1261       * @since Moodle 2.0
    1262       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1263       * @see core_user_external::update_users_parameters()
    1264       */
    1265      public static function update_users_parameters() {
    1266          return core_user_external::update_users_parameters();
    1267      }
    1268  
    1269      /**
    1270       * Update users
    1271       *
    1272       * @param array $users
    1273       * @return null
    1274       * @since Moodle 2.0
    1275       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1276       * @see core_user_external::update_users()
    1277       */
    1278      public static function update_users($users) {
    1279          return core_user_external::update_users($users);
    1280      }
    1281  
    1282      /**
    1283       * Returns description of method result value
    1284       *
    1285       * @return null
    1286       * @since Moodle 2.0
    1287       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1288       * @see core_user_external::update_users_returns()
    1289       */
    1290      public static function update_users_returns() {
    1291          return core_user_external::update_users_returns();
    1292      }
    1293  
    1294      /**
    1295       * Returns description of method parameters
    1296       *
    1297       * @return external_function_parameters
    1298       * @since Moodle 2.0
    1299       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1300       * @see core_user_external::get_users_by_id_parameters()
    1301       */
    1302      public static function get_users_by_id_parameters() {
    1303          return core_user_external::get_users_by_id_parameters();
    1304      }
    1305  
    1306      /**
    1307       * Get user information
    1308       * - This function is matching the permissions of /user/profil.php
    1309       * - It is also matching some permissions from /user/editadvanced.php for the following fields:
    1310       *   auth, confirmed, idnumber, lang, theme, timezone, mailformat
    1311       *
    1312       * @param array $userids  array of user ids
    1313       * @return array An array of arrays describing users
    1314       * @since Moodle 2.0
    1315       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1316       * @see core_user_external::get_users_by_id()
    1317       */
    1318      public static function get_users_by_id($userids) {
    1319          return core_user_external::get_users_by_id($userids);
    1320      }
    1321  
    1322      /**
    1323       * Returns description of method result value
    1324       *
    1325       * @return external_description
    1326       * @since Moodle 2.0
    1327       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1328       * @see core_user_external::get_users_by_id_returns()
    1329       */
    1330      public static function get_users_by_id_returns() {
    1331          return core_user_external::get_users_by_id_returns();
    1332      }
    1333      /**
    1334       * Returns description of method parameters
    1335       *
    1336       * @return external_function_parameters
    1337       * @since Moodle 2.1
    1338       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1339       * @see core_user_external::get_course_user_profiles_parameters()
    1340       */
    1341      public static function get_course_participants_by_id_parameters() {
    1342          return core_user_external::get_course_user_profiles_parameters();
    1343      }
    1344  
    1345      /**
    1346       * Get course participant's details
    1347       *
    1348       * @param array $userlist  array of user ids and according course ids
    1349       * @return array An array of arrays describing course participants
    1350       * @since Moodle 2.1
    1351       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1352       * @see core_user_external::get_course_user_profiles()
    1353       */
    1354      public static function get_course_participants_by_id($userlist) {
    1355          return core_user_external::get_course_user_profiles($userlist);
    1356      }
    1357  
    1358      /**
    1359       * Returns description of method result value
    1360       *
    1361       * @return external_description
    1362       * @since Moodle 2.1
    1363       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1364       * @see core_user_external::get_course_user_profiles_returns()
    1365       */
    1366      public static function get_course_participants_by_id_returns() {
    1367          return core_user_external::get_course_user_profiles_returns();
    1368      }
    1369  
    1370      /**
    1371       * Returns description of method parameters
    1372       *
    1373       * @return external_function_parameters
    1374       * @since Moodle 2.1
    1375       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1376       * @see core_enrol_external::get_enrolled_users_parameters()
    1377       */
    1378      public static function get_users_by_courseid_parameters() {
    1379          global $CFG;
    1380          require_once($CFG->dirroot . '/enrol/externallib.php');
    1381          return core_enrol_external::get_enrolled_users_parameters();
    1382      }
    1383  
    1384      /**
    1385       * Get course participants details
    1386       *
    1387       * @param int $courseid  course id
    1388       * @param array $options options {
    1389       *                                'name' => option name
    1390       *                                'value' => option value
    1391       *                               }
    1392       * @return array An array of users
    1393       * @since Moodle 2.1
    1394       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1395       * @see core_enrol_external::get_enrolled_users()
    1396       */
    1397      public static function get_users_by_courseid($courseid, $options) {
    1398          global $CFG;
    1399          require_once($CFG->dirroot . '/enrol/externallib.php');
    1400          return core_enrol_external::get_enrolled_users($courseid, $options);
    1401      }
    1402      /**
    1403       * Returns description of method result value
    1404       *
    1405       * @return external_description
    1406       * @since Moodle 2.1
    1407       * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
    1408       * @see core_enrol_external::get_enrolled_users_returns()
    1409       */
    1410      public static function get_users_by_courseid_returns() {
    1411          global $CFG;
    1412          require_once($CFG->dirroot . '/enrol/externallib.php');
    1413          return core_enrol_external::get_enrolled_users_returns();
    1414      }
    1415  }
    

    Search This Site: