[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/webservice/ -> forms.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Web services admin UI forms
  20   *
  21   * @package   webservice
  22   * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  require_once $CFG->libdir . '/formslib.php';
  26  
  27  /**
  28   * Display the authorised user settings form
  29   * Including IP Restriction, Valid until and (TODO) capability
  30   */
  31  class external_service_authorised_user_settings_form extends moodleform {
  32  
  33      function definition() {
  34          $mform = $this->_form;
  35          $data = $this->_customdata;
  36  
  37          $mform->addElement('header', 'serviceusersettings',
  38                  get_string('serviceusersettings', 'webservice'));
  39  
  40          $mform->addElement('text', 'iprestriction',
  41                  get_string('iprestriction', 'webservice'));
  42          $mform->addHelpButton('iprestriction', 'iprestriction', 'webservice');
  43          $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
  44  
  45          $mform->addElement('date_selector', 'validuntil',
  46                  get_string('validuntil', 'webservice'), array('optional' => true));
  47          $mform->addHelpButton('validuntil', 'validuntil', 'webservice');
  48          $mform->setType('validuntil', PARAM_INT);
  49  
  50          $this->add_action_buttons(true, get_string('updateusersettings', 'webservice'));
  51  
  52          $this->set_data($data);
  53      }
  54  
  55  }
  56  
  57  class external_service_form extends moodleform {
  58  
  59      function definition() {
  60          $mform = $this->_form;
  61          $service = isset($this->_customdata) ? $this->_customdata : new stdClass();
  62  
  63          $mform->addElement('header', 'extservice',
  64                  get_string('externalservice', 'webservice'));
  65  
  66          $mform->addElement('text', 'name', get_string('name'));
  67          $mform->addRule('name', get_string('required'), 'required', null, 'client');
  68          $mform->setType('name', PARAM_TEXT);
  69  
  70          $mform->addElement('text', 'shortname', get_string('shortname'), 'maxlength="255" size="20"');
  71          $mform->setType('shortname', PARAM_TEXT);
  72          if (!empty($service->id)) {
  73              $mform->hardFreeze('shortname');
  74              $mform->setConstants('shortname', $service->shortname);
  75          }
  76  
  77          $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
  78          $mform->setType('enabled', PARAM_BOOL);
  79          $mform->addElement('advcheckbox', 'restrictedusers',
  80                  get_string('restrictedusers', 'webservice'));
  81          $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
  82          $mform->setType('restrictedusers', PARAM_BOOL);
  83  
  84          // Can users download files?
  85          $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
  86          $mform->setAdvanced('downloadfiles');
  87          $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
  88          $mform->setType('downloadfiles', PARAM_BOOL);
  89  
  90          // Can users upload files?
  91          $mform->addElement('advcheckbox', 'uploadfiles', get_string('uploadfiles', 'webservice'));
  92          $mform->setAdvanced('uploadfiles');
  93          $mform->addHelpButton('uploadfiles', 'uploadfiles', 'webservice');
  94  
  95          /// needed to select automatically the 'No required capability" option
  96          $currentcapabilityexist = false;
  97          if (empty($service->requiredcapability)) {
  98              $service->requiredcapability = "norequiredcapability";
  99              $currentcapabilityexist = true;
 100          }
 101  
 102          // Prepare the list of capabilities to choose from
 103          $systemcontext = context_system::instance();
 104          $allcapabilities = $systemcontext->get_capabilities();
 105          $capabilitychoices = array();
 106          $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
 107                          'webservice');
 108          foreach ($allcapabilities as $cap) {
 109              $capabilitychoices[$cap->name] = $cap->name . ': '
 110                      . get_capability_string($cap->name);
 111              if (!empty($service->requiredcapability)
 112                      && $service->requiredcapability == $cap->name) {
 113                  $currentcapabilityexist = true;
 114              }
 115          }
 116  
 117          $mform->addElement('searchableselector', 'requiredcapability',
 118                  get_string('requiredcapability', 'webservice'), $capabilitychoices);
 119          $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
 120          $mform->setAdvanced('requiredcapability');
 121          $mform->setType('requiredcapability', PARAM_RAW);
 122  /// display notification error if the current requiredcapability doesn't exist anymore
 123          if (empty($currentcapabilityexist)) {
 124              global $OUTPUT;
 125              $mform->addElement('static', 'capabilityerror', '',
 126                      $OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
 127                                      'webservice', $service->requiredcapability)));
 128              $service->requiredcapability = "norequiredcapability";
 129          }
 130  
 131          $mform->addElement('hidden', 'id');
 132          $mform->setType('id', PARAM_INT);
 133  
 134          if (!empty($service->id)) {
 135              $buttonlabel = get_string('savechanges');
 136          } else {
 137              $buttonlabel = get_string('addaservice', 'webservice');
 138          }
 139  
 140          $this->add_action_buttons(true, $buttonlabel);
 141  
 142          $this->set_data($service);
 143      }
 144  
 145      function definition_after_data() {
 146          $mform = $this->_form;
 147          $service = $this->_customdata;
 148  
 149          if (!empty($service->component)) {
 150              // built-in components must not be modified except the enabled flag!!
 151              $mform->hardFreeze('name,requiredcapability,restrictedusers');
 152          }
 153      }
 154  
 155      function validation($data, $files) {
 156          global $DB;
 157  
 158          $errors = parent::validation($data, $files);
 159  
 160          // Add field validation check for duplicate shortname.
 161          // Allow duplicated "empty" shortnames.
 162          if (!empty($data['shortname'])) {
 163              if ($service = $DB->get_record('external_services', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
 164                  if (empty($data['id']) || $service->id != $data['id']) {
 165                      $errors['shortname'] = get_string('shortnametaken', 'webservice', $service->name);
 166                  }
 167              }
 168          }
 169  
 170          return $errors;
 171      }
 172  
 173  }
 174  
 175  class external_service_functions_form extends moodleform {
 176  
 177      function definition() {
 178          global $CFG;
 179  
 180          $mform = $this->_form;
 181          $data = $this->_customdata;
 182  
 183          $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));
 184  
 185          require_once($CFG->dirroot . "/webservice/lib.php");
 186          $webservicemanager = new webservice();
 187          $functions = $webservicemanager->get_not_associated_external_functions($data['id']);
 188  
 189          //we add the descriptions to the functions
 190          foreach ($functions as $functionid => $functionname) {
 191              //retrieve full function information (including the description)
 192              $function = external_function_info($functionname);
 193              $functions[$functionid] = $function->name . ':' . $function->description;
 194          }
 195  
 196          $mform->addElement('searchableselector', 'fids', get_string('name'),
 197                  $functions, array('multiple'));
 198          $mform->addRule('fids', get_string('required'), 'required', null, 'client');
 199  
 200          $mform->addElement('hidden', 'id');
 201          $mform->setType('id', PARAM_INT);
 202  
 203          $mform->addElement('hidden', 'action');
 204          $mform->setType('action', PARAM_ALPHANUMEXT);
 205  
 206          $this->add_action_buttons(true, get_string('addfunctions', 'webservice'));
 207  
 208          $this->set_data($data);
 209      }
 210  
 211  }
 212  
 213  class web_service_token_form extends moodleform {
 214  
 215      function definition() {
 216          global $USER, $DB, $CFG;
 217  
 218          $mform = $this->_form;
 219          $data = $this->_customdata;
 220  
 221          $mform->addElement('header', 'token', get_string('token', 'webservice'));
 222  
 223          if (empty($data->nouserselection)) {
 224  
 225              //check if the number of user is reasonable to be displayed in a select box
 226              $usertotal = $DB->count_records('user',
 227                      array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1));
 228  
 229              if ($usertotal < 500) {
 230                  list($sort, $params) = users_order_by_sql('u');
 231                  // User searchable selector - return users who are confirmed, not deleted, not suspended and not a guest.
 232                  $sql = 'SELECT u.id, ' . get_all_user_name_fields(true, 'u') . '
 233                          FROM {user} u
 234                          WHERE u.deleted = 0
 235                          AND u.confirmed = 1
 236                          AND u.suspended = 0
 237                          AND u.id != :siteguestid
 238                          ORDER BY ' . $sort;
 239                  $params['siteguestid'] = $CFG->siteguest;
 240                  $users = $DB->get_records_sql($sql, $params);
 241                  $options = array();
 242                  foreach ($users as $userid => $user) {
 243                      $options[$userid] = fullname($user);
 244                  }
 245                  $mform->addElement('searchableselector', 'user', get_string('user'), $options);
 246                  $mform->setType('user', PARAM_INT);
 247              } else {
 248                  //simple text box for username or user id (if two username exists, a form error is displayed)
 249                  $mform->addElement('text', 'user', get_string('usernameorid', 'webservice'));
 250                  $mform->setType('user', PARAM_RAW_TRIMMED);
 251              }
 252              $mform->addRule('user', get_string('required'), 'required', null, 'client');
 253          }
 254  
 255          //service selector
 256          $services = $DB->get_records('external_services');
 257          $options = array();
 258          $systemcontext = context_system::instance();
 259          foreach ($services as $serviceid => $service) {
 260              //check that the user has the required capability
 261              //(only for generation by the profile page)
 262              if (empty($data->nouserselection)
 263                      || empty($service->requiredcapability)
 264                      || has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
 265                  $options[$serviceid] = $service->name;
 266              }
 267          }
 268          $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
 269          $mform->addRule('service', get_string('required'), 'required', null, 'client');
 270          $mform->setType('service', PARAM_INT);
 271  
 272          $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
 273          $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
 274  
 275          $mform->addElement('date_selector', 'validuntil',
 276                  get_string('validuntil', 'webservice'), array('optional' => true));
 277          $mform->setType('validuntil', PARAM_INT);
 278  
 279          $mform->addElement('hidden', 'action');
 280          $mform->setType('action', PARAM_ALPHANUMEXT);
 281  
 282          $this->add_action_buttons(true);
 283  
 284          $this->set_data($data);
 285      }
 286  
 287      function get_data() {
 288          global $DB;
 289          $data = parent::get_data();
 290  
 291          if (!empty($data) && !is_numeric($data->user)) {
 292              //retrieve username
 293              $user = $DB->get_record('user', array('username' => $data->user), 'id');
 294              $data->user = $user->id;
 295          }
 296          return $data;
 297      }
 298  
 299      function validation($data, $files) {
 300          global $DB;
 301  
 302          $errors = parent::validation($data, $files);
 303  
 304          if (is_numeric($data['user'])) {
 305              $searchtype = 'id';
 306          } else {
 307              $searchtype = 'username';
 308              //check the username is valid
 309              if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) {
 310                  $errors['user'] = get_string('invalidusername');
 311              }
 312          }
 313  
 314          if (!isset($errors['user'])) {
 315              $users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id');
 316  
 317              //check that the user exists in the database
 318              if (count($users) == 0) {
 319                  $errors['user'] = get_string('usernameoridnousererror', 'webservice');
 320              } else if (count($users) > 1) { //can only be a username search as id are unique
 321                  $errors['user'] = get_string('usernameoridoccurenceerror', 'webservice');
 322              }
 323          }
 324  
 325          return $errors;
 326      }
 327  
 328  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1