[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/message/output/airnotifier/ -> externallib.php (source)

   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 functions
  19   *
  20   * @package    message_airnotifier
  21   * @category   external
  22   * @copyright  2012 Jerome Mouneyrac <jerome@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since Moodle 2.7
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  require_once("$CFG->libdir/externallib.php");
  30  
  31  /**
  32   * External API for airnotifier web services
  33   *
  34   * @package    message_airnotifier
  35   * @category   external
  36   * @copyright  2012 Jerome Mouneyrac <jerome@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @since Moodle 2.7
  39   */
  40  class message_airnotifier_external extends external_api {
  41  
  42      /**
  43       * Returns description of method parameters
  44       *
  45       * @since Moodle 2.7
  46       */
  47      public static function is_system_configured_parameters() {
  48          return new external_function_parameters(
  49                  array()
  50          );
  51      }
  52  
  53      /**
  54       * Tests whether the airnotifier settings have been configured
  55       *
  56       * @since Moodle 2.7
  57       */
  58      public static function is_system_configured() {
  59          global $DB;
  60  
  61          // First, check if the plugin is disabled.
  62          $processor = $DB->get_record('message_processors', array('name' => 'airnotifier'), '*', MUST_EXIST);
  63          if (!$processor->enabled) {
  64              return 0;
  65          }
  66  
  67          // Then, check if the plugin is completly configured.
  68          $manager = new message_airnotifier_manager();
  69          return (int) $manager->is_system_configured();
  70      }
  71  
  72      /**
  73       * Returns description of method result value
  74       *
  75       * @return external_single_structure
  76       * @since Moodle 2.7
  77       */
  78      public static function is_system_configured_returns() {
  79          return new external_value( PARAM_INT, '0 if the system is not configured, 1 otherwise');
  80      }
  81  
  82      /**
  83       * Returns description of method parameters
  84       *
  85       * @since Moodle 2.7
  86       */
  87      public static function are_notification_preferences_configured_parameters() {
  88          return new external_function_parameters(
  89                  array(
  90                      'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
  91                  )
  92          );
  93      }
  94  
  95      /**
  96       * Check if the users have notification preferences configured for the airnotifier plugin
  97       *
  98       * @param array $userids Array of user ids
  99       * @since Moodle 2.7
 100       */
 101      public static function are_notification_preferences_configured($userids) {
 102          global $CFG, $USER, $DB;
 103  
 104          require_once($CFG->dirroot . '/message/lib.php');
 105          $params = self::validate_parameters(self::are_notification_preferences_configured_parameters(),
 106                  array('userids' => $userids));
 107  
 108          list($sqluserids, $params) = $DB->get_in_or_equal($params['userids'], SQL_PARAMS_NAMED);
 109          $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
 110          $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
 111          $params['contextlevel'] = CONTEXT_USER;
 112          $usersql = "SELECT u.* $uselect
 113                        FROM {user} u $ujoin
 114                       WHERE u.id $sqluserids";
 115          $users = $DB->get_recordset_sql($usersql, $params);
 116  
 117          $result = array(
 118              'users' => array(),
 119              'warnings' => array()
 120          );
 121          $hasuserupdatecap = has_capability('moodle/user:update', context_system::instance());
 122          foreach ($users as $user) {
 123  
 124              $currentuser = ($user->id == $USER->id);
 125  
 126              if ($currentuser or $hasuserupdatecap) {
 127  
 128                  if (!empty($user->deleted)) {
 129                      $warning = array();
 130                      $warning['item'] = 'user';
 131                      $warning['itemid'] = $user->id;
 132                      $warning['warningcode'] = '1';
 133                      $warning['message'] = "User $user->id was deleted";
 134                      $result['warnings'][] = $warning;
 135                      continue;
 136                  }
 137  
 138                  $preferences = array();
 139                  $preferences['userid'] = $user->id;
 140                  $preferences['configured'] = 0;
 141  
 142                  // Now we get for all the providers and all the states
 143                  // the user preferences to check if at least one is enabled for airnotifier plugin.
 144                  $providers = message_get_providers_for_user($user->id);
 145                  $configured = false;
 146  
 147                  foreach ($providers as $provider) {
 148                      if ($configured) {
 149                          break;
 150                      }
 151  
 152                      foreach (array('loggedin', 'loggedoff') as $state) {
 153  
 154                          $prefstocheck = array();
 155                          $prefname = 'message_provider_'.$provider->component.'_'.$provider->name.'_'.$state;
 156  
 157                          // First get forced settings.
 158                          if ($forcedpref = get_config('message', $prefname)) {
 159                              $prefstocheck = array_merge($prefstocheck, explode(',', $forcedpref));
 160                          }
 161  
 162                          // Then get user settings.
 163                          if ($userpref = get_user_preferences($prefname, '', $user->id)) {
 164                              $prefstocheck = array_merge($prefstocheck, explode(',', $userpref));
 165                          }
 166  
 167                          if (in_array('airnotifier', $prefstocheck)) {
 168                              $preferences['configured'] = 1;
 169                              $configured = true;
 170                              break;
 171                          }
 172  
 173                      }
 174                  }
 175  
 176                  $result['users'][] = $preferences;
 177              } else if (!$hasuserupdatecap) {
 178                  $warning = array();
 179                  $warning['item'] = 'user';
 180                  $warning['itemid'] = $user->id;
 181                  $warning['warningcode'] = '2';
 182                  $warning['message'] = "You don't have permissions for view user $user->id preferences";
 183                  $result['warnings'][] = $warning;
 184              }
 185  
 186          }
 187          $users->close();
 188  
 189          return $result;
 190      }
 191  
 192      /**
 193       * Returns description of method result value
 194       *
 195       * @return external_single_structure
 196       * @since Moodle 2.7
 197       */
 198      public static function are_notification_preferences_configured_returns() {
 199          return new external_single_structure(
 200              array(
 201                  'users' => new external_multiple_structure(
 202                      new external_single_structure(
 203                          array (
 204                              'userid'     => new external_value(PARAM_INT, 'userid id'),
 205                              'configured' => new external_value(PARAM_INT,
 206                                  '1 if the user preferences have been configured and 0 if not')
 207                          )
 208                      ),
 209                      'list of preferences by user'),
 210                  'warnings' => new external_warnings()
 211              )
 212          );
 213      }
 214  
 215  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1