PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/webservice/forms.php

http://github.com/moodle/moodle
PHP | 340 lines | 227 code | 67 blank | 46 comment | 37 complexity | 74825bdaee27cdf6fcb9c66f3b79e888 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Web services admin UI forms
  18. *
  19. * @package webservice
  20. * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once $CFG->libdir . '/formslib.php';
  24. /**
  25. * Display the authorised user settings form
  26. * Including IP Restriction, Valid until and (TODO) capability
  27. */
  28. class external_service_authorised_user_settings_form extends moodleform {
  29. function definition() {
  30. $mform = $this->_form;
  31. $data = $this->_customdata;
  32. $mform->addElement('header', 'serviceusersettings',
  33. get_string('serviceusersettings', 'webservice'));
  34. $mform->addElement('text', 'iprestriction',
  35. get_string('iprestriction', 'webservice'));
  36. $mform->addHelpButton('iprestriction', 'iprestriction', 'webservice');
  37. $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
  38. $mform->addElement('date_selector', 'validuntil',
  39. get_string('validuntil', 'webservice'), array('optional' => true));
  40. $mform->addHelpButton('validuntil', 'validuntil', 'webservice');
  41. $mform->setType('validuntil', PARAM_INT);
  42. $this->add_action_buttons(true, get_string('updateusersettings', 'webservice'));
  43. $this->set_data($data);
  44. }
  45. }
  46. class external_service_form extends moodleform {
  47. function definition() {
  48. $mform = $this->_form;
  49. $service = isset($this->_customdata) ? $this->_customdata : new stdClass();
  50. $mform->addElement('header', 'extservice',
  51. get_string('externalservice', 'webservice'));
  52. $mform->addElement('text', 'name', get_string('name'));
  53. $mform->addRule('name', get_string('required'), 'required', null, 'client');
  54. $mform->setType('name', PARAM_TEXT);
  55. $mform->addElement('text', 'shortname', get_string('shortname'), 'maxlength="255" size="20"');
  56. $mform->setType('shortname', PARAM_TEXT);
  57. if (!empty($service->id)) {
  58. $mform->hardFreeze('shortname');
  59. $mform->setConstants('shortname', $service->shortname);
  60. }
  61. $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
  62. $mform->setType('enabled', PARAM_BOOL);
  63. $mform->addElement('advcheckbox', 'restrictedusers',
  64. get_string('restrictedusers', 'webservice'));
  65. $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
  66. $mform->setType('restrictedusers', PARAM_BOOL);
  67. // Can users download files?
  68. $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
  69. $mform->setAdvanced('downloadfiles');
  70. $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
  71. $mform->setType('downloadfiles', PARAM_BOOL);
  72. // Can users upload files?
  73. $mform->addElement('advcheckbox', 'uploadfiles', get_string('uploadfiles', 'webservice'));
  74. $mform->setAdvanced('uploadfiles');
  75. $mform->addHelpButton('uploadfiles', 'uploadfiles', 'webservice');
  76. /// needed to select automatically the 'No required capability" option
  77. $currentcapabilityexist = false;
  78. if (empty($service->requiredcapability)) {
  79. $service->requiredcapability = "norequiredcapability";
  80. $currentcapabilityexist = true;
  81. }
  82. // Prepare the list of capabilities to choose from
  83. $systemcontext = context_system::instance();
  84. $allcapabilities = $systemcontext->get_capabilities();
  85. $capabilitychoices = array();
  86. $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
  87. 'webservice');
  88. foreach ($allcapabilities as $cap) {
  89. $capabilitychoices[$cap->name] = $cap->name . ': '
  90. . get_capability_string($cap->name);
  91. if (!empty($service->requiredcapability)
  92. && $service->requiredcapability == $cap->name) {
  93. $currentcapabilityexist = true;
  94. }
  95. }
  96. $mform->addElement('searchableselector', 'requiredcapability',
  97. get_string('requiredcapability', 'webservice'), $capabilitychoices);
  98. $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
  99. $mform->setAdvanced('requiredcapability');
  100. $mform->setType('requiredcapability', PARAM_RAW);
  101. /// display notification error if the current requiredcapability doesn't exist anymore
  102. if (empty($currentcapabilityexist)) {
  103. global $OUTPUT;
  104. $mform->addElement('static', 'capabilityerror', '',
  105. $OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
  106. 'webservice', $service->requiredcapability)));
  107. $service->requiredcapability = "norequiredcapability";
  108. }
  109. $mform->addElement('hidden', 'id');
  110. $mform->setType('id', PARAM_INT);
  111. if (!empty($service->id)) {
  112. $buttonlabel = get_string('savechanges');
  113. } else {
  114. $buttonlabel = get_string('addaservice', 'webservice');
  115. }
  116. $this->add_action_buttons(true, $buttonlabel);
  117. $this->set_data($service);
  118. }
  119. function definition_after_data() {
  120. $mform = $this->_form;
  121. $service = $this->_customdata;
  122. if (!empty($service->component)) {
  123. // built-in components must not be modified except the enabled flag!!
  124. $mform->hardFreeze('name,requiredcapability,restrictedusers');
  125. }
  126. }
  127. function validation($data, $files) {
  128. global $DB;
  129. $errors = parent::validation($data, $files);
  130. // Add field validation check for duplicate name.
  131. if ($webservice = $DB->get_record('external_services', array('name' => $data['name']))) {
  132. if (empty($data['id']) || $webservice->id != $data['id']) {
  133. $errors['name'] = get_string('nameexists', 'webservice');
  134. }
  135. }
  136. // Add field validation check for duplicate shortname.
  137. // Allow duplicated "empty" shortnames.
  138. if (!empty($data['shortname'])) {
  139. if ($service = $DB->get_record('external_services', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
  140. if (empty($data['id']) || $service->id != $data['id']) {
  141. $errors['shortname'] = get_string('shortnametaken', 'webservice', $service->name);
  142. }
  143. }
  144. }
  145. return $errors;
  146. }
  147. }
  148. class external_service_functions_form extends moodleform {
  149. function definition() {
  150. global $CFG;
  151. $mform = $this->_form;
  152. $data = $this->_customdata;
  153. $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));
  154. require_once($CFG->dirroot . "/webservice/lib.php");
  155. $webservicemanager = new webservice();
  156. $functions = $webservicemanager->get_not_associated_external_functions($data['id']);
  157. //we add the descriptions to the functions
  158. foreach ($functions as $functionid => $functionname) {
  159. //retrieve full function information (including the description)
  160. $function = external_api::external_function_info($functionname);
  161. if (empty($function->deprecated)) {
  162. $functions[$functionid] = $function->name . ':' . $function->description;
  163. } else {
  164. // Exclude the deprecated ones.
  165. unset($functions[$functionid]);
  166. }
  167. }
  168. $mform->addElement('searchableselector', 'fids', get_string('name'),
  169. $functions, array('multiple'));
  170. $mform->addRule('fids', get_string('required'), 'required', null, 'client');
  171. $mform->addElement('hidden', 'id');
  172. $mform->setType('id', PARAM_INT);
  173. $mform->addElement('hidden', 'action');
  174. $mform->setType('action', PARAM_ALPHANUMEXT);
  175. $this->add_action_buttons(true, get_string('addfunctions', 'webservice'));
  176. $this->set_data($data);
  177. }
  178. }
  179. class web_service_token_form extends moodleform {
  180. function definition() {
  181. global $USER, $DB, $CFG;
  182. $mform = $this->_form;
  183. $data = $this->_customdata;
  184. $mform->addElement('header', 'token', get_string('token', 'webservice'));
  185. if (empty($data->nouserselection)) {
  186. //check if the number of user is reasonable to be displayed in a select box
  187. $usertotal = $DB->count_records('user',
  188. array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1));
  189. if ($usertotal < 500) {
  190. list($sort, $params) = users_order_by_sql('u');
  191. // User searchable selector - return users who are confirmed, not deleted, not suspended and not a guest.
  192. $sql = 'SELECT u.id, ' . get_all_user_name_fields(true, 'u') . '
  193. FROM {user} u
  194. WHERE u.deleted = 0
  195. AND u.confirmed = 1
  196. AND u.suspended = 0
  197. AND u.id != :siteguestid
  198. ORDER BY ' . $sort;
  199. $params['siteguestid'] = $CFG->siteguest;
  200. $users = $DB->get_records_sql($sql, $params);
  201. $options = array();
  202. foreach ($users as $userid => $user) {
  203. $options[$userid] = fullname($user);
  204. }
  205. $mform->addElement('searchableselector', 'user', get_string('user'), $options);
  206. $mform->setType('user', PARAM_INT);
  207. } else {
  208. //simple text box for username or user id (if two username exists, a form error is displayed)
  209. $mform->addElement('text', 'user', get_string('usernameorid', 'webservice'));
  210. $mform->setType('user', PARAM_RAW_TRIMMED);
  211. }
  212. $mform->addRule('user', get_string('required'), 'required', null, 'client');
  213. }
  214. //service selector
  215. $services = $DB->get_records('external_services');
  216. $options = array();
  217. $systemcontext = context_system::instance();
  218. foreach ($services as $serviceid => $service) {
  219. //check that the user has the required capability
  220. //(only for generation by the profile page)
  221. if (empty($data->nouserselection)
  222. || empty($service->requiredcapability)
  223. || has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
  224. $options[$serviceid] = $service->name;
  225. }
  226. }
  227. $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
  228. $mform->addRule('service', get_string('required'), 'required', null, 'client');
  229. $mform->setType('service', PARAM_INT);
  230. $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
  231. $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
  232. $mform->addElement('date_selector', 'validuntil',
  233. get_string('validuntil', 'webservice'), array('optional' => true));
  234. $mform->setType('validuntil', PARAM_INT);
  235. $mform->addElement('hidden', 'action');
  236. $mform->setType('action', PARAM_ALPHANUMEXT);
  237. $this->add_action_buttons(true);
  238. $this->set_data($data);
  239. }
  240. function get_data() {
  241. global $DB;
  242. $data = parent::get_data();
  243. if (!empty($data) && !is_numeric($data->user)) {
  244. //retrieve username
  245. $user = $DB->get_record('user', array('username' => $data->user), 'id');
  246. $data->user = $user->id;
  247. }
  248. return $data;
  249. }
  250. function validation($data, $files) {
  251. global $DB;
  252. $errors = parent::validation($data, $files);
  253. if (is_numeric($data['user'])) {
  254. $searchtype = 'id';
  255. } else {
  256. $searchtype = 'username';
  257. //check the username is valid
  258. if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) {
  259. $errors['user'] = get_string('invalidusername');
  260. }
  261. }
  262. if (!isset($errors['user'])) {
  263. $users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id');
  264. //check that the user exists in the database
  265. if (count($users) == 0) {
  266. $errors['user'] = get_string('usernameoridnousererror', 'webservice');
  267. } else if (count($users) > 1) { //can only be a username search as id are unique
  268. $errors['user'] = get_string('usernameoridoccurenceerror', 'webservice');
  269. }
  270. }
  271. return $errors;
  272. }
  273. }