PageRenderTime 138ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/webservice/forms.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 307 lines | 204 code | 61 blank | 42 comment | 25 complexity | e91b61bcb7932ce78733b3a94efe1365 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
  56. $mform->setType('enabled', PARAM_BOOL);
  57. $mform->addElement('advcheckbox', 'restrictedusers',
  58. get_string('restrictedusers', 'webservice'));
  59. $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
  60. $mform->setType('restrictedusers', PARAM_BOOL);
  61. // Can users download files?
  62. $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
  63. $mform->setAdvanced('downloadfiles');
  64. $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
  65. $mform->setType('downloadfiles', PARAM_BOOL);
  66. // Can users upload files?
  67. $mform->addElement('advcheckbox', 'uploadfiles', get_string('uploadfiles', 'webservice'));
  68. $mform->setAdvanced('uploadfiles');
  69. $mform->addHelpButton('uploadfiles', 'uploadfiles', 'webservice');
  70. /// needed to select automatically the 'No required capability" option
  71. $currentcapabilityexist = false;
  72. if (empty($service->requiredcapability)) {
  73. $service->requiredcapability = "norequiredcapability";
  74. $currentcapabilityexist = true;
  75. }
  76. // Prepare the list of capabilities to choose from
  77. $systemcontext = context_system::instance();
  78. $allcapabilities = $systemcontext->get_capabilities();
  79. $capabilitychoices = array();
  80. $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
  81. 'webservice');
  82. foreach ($allcapabilities as $cap) {
  83. $capabilitychoices[$cap->name] = $cap->name . ': '
  84. . get_capability_string($cap->name);
  85. if (!empty($service->requiredcapability)
  86. && $service->requiredcapability == $cap->name) {
  87. $currentcapabilityexist = true;
  88. }
  89. }
  90. $mform->addElement('searchableselector', 'requiredcapability',
  91. get_string('requiredcapability', 'webservice'), $capabilitychoices);
  92. $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
  93. $mform->setAdvanced('requiredcapability');
  94. $mform->setType('requiredcapability', PARAM_RAW);
  95. /// display notification error if the current requiredcapability doesn't exist anymore
  96. if (empty($currentcapabilityexist)) {
  97. global $OUTPUT;
  98. $mform->addElement('static', 'capabilityerror', '',
  99. $OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
  100. 'webservice', $service->requiredcapability)));
  101. $service->requiredcapability = "norequiredcapability";
  102. }
  103. $mform->addElement('hidden', 'id');
  104. $mform->setType('id', PARAM_INT);
  105. if (!empty($service->id)) {
  106. $buttonlabel = get_string('savechanges');
  107. } else {
  108. $buttonlabel = get_string('addaservice', 'webservice');
  109. }
  110. $this->add_action_buttons(true, $buttonlabel);
  111. $this->set_data($service);
  112. }
  113. function definition_after_data() {
  114. $mform = $this->_form;
  115. $service = $this->_customdata;
  116. if (!empty($service->component)) {
  117. // built-in components must not be modified except the enabled flag!!
  118. $mform->hardFreeze('name,requiredcapability,restrictedusers');
  119. }
  120. }
  121. function validation($data, $files) {
  122. $errors = parent::validation($data, $files);
  123. return $errors;
  124. }
  125. }
  126. class external_service_functions_form extends moodleform {
  127. function definition() {
  128. global $CFG;
  129. $mform = $this->_form;
  130. $data = $this->_customdata;
  131. $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));
  132. require_once($CFG->dirroot . "/webservice/lib.php");
  133. $webservicemanager = new webservice();
  134. $functions = $webservicemanager->get_not_associated_external_functions($data['id']);
  135. //we add the descriptions to the functions
  136. foreach ($functions as $functionid => $functionname) {
  137. //retrieve full function information (including the description)
  138. $function = external_function_info($functionname);
  139. $functions[$functionid] = $function->name . ':' . $function->description;
  140. }
  141. $mform->addElement('searchableselector', 'fids', get_string('name'),
  142. $functions, array('multiple'));
  143. $mform->addRule('fids', get_string('required'), 'required', null, 'client');
  144. $mform->addElement('hidden', 'id');
  145. $mform->setType('id', PARAM_INT);
  146. $mform->addElement('hidden', 'action');
  147. $mform->setType('action', PARAM_ALPHANUMEXT);
  148. $this->add_action_buttons(true, get_string('addfunctions', 'webservice'));
  149. $this->set_data($data);
  150. }
  151. }
  152. class web_service_token_form extends moodleform {
  153. function definition() {
  154. global $USER, $DB, $CFG;
  155. $mform = $this->_form;
  156. $data = $this->_customdata;
  157. $mform->addElement('header', 'token', get_string('token', 'webservice'));
  158. if (empty($data->nouserselection)) {
  159. //check if the number of user is reasonable to be displayed in a select box
  160. $usertotal = $DB->count_records('user',
  161. array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1));
  162. if ($usertotal < 500) {
  163. list($sort, $params) = users_order_by_sql('u');
  164. // User searchable selector - return users who are confirmed, not deleted, not suspended and not a guest.
  165. $sql = 'SELECT u.id, ' . get_all_user_name_fields(true, 'u') . '
  166. FROM {user} u
  167. WHERE u.deleted = 0
  168. AND u.confirmed = 1
  169. AND u.suspended = 0
  170. AND u.id != :siteguestid
  171. ORDER BY ' . $sort;
  172. $params['siteguestid'] = $CFG->siteguest;
  173. $users = $DB->get_records_sql($sql, $params);
  174. $options = array();
  175. foreach ($users as $userid => $user) {
  176. $options[$userid] = fullname($user);
  177. }
  178. $mform->addElement('searchableselector', 'user', get_string('user'), $options);
  179. $mform->setType('user', PARAM_INT);
  180. } else {
  181. //simple text box for username or user id (if two username exists, a form error is displayed)
  182. $mform->addElement('text', 'user', get_string('usernameorid', 'webservice'));
  183. $mform->setType('user', PARAM_RAW_TRIMMED);
  184. }
  185. $mform->addRule('user', get_string('required'), 'required', null, 'client');
  186. }
  187. //service selector
  188. $services = $DB->get_records('external_services');
  189. $options = array();
  190. $systemcontext = context_system::instance();
  191. foreach ($services as $serviceid => $service) {
  192. //check that the user has the required capability
  193. //(only for generation by the profile page)
  194. if (empty($data->nouserselection)
  195. || empty($service->requiredcapability)
  196. || has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
  197. $options[$serviceid] = $service->name;
  198. }
  199. }
  200. $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
  201. $mform->addRule('service', get_string('required'), 'required', null, 'client');
  202. $mform->setType('service', PARAM_INT);
  203. $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
  204. $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
  205. $mform->addElement('date_selector', 'validuntil',
  206. get_string('validuntil', 'webservice'), array('optional' => true));
  207. $mform->setType('validuntil', PARAM_INT);
  208. $mform->addElement('hidden', 'action');
  209. $mform->setType('action', PARAM_ALPHANUMEXT);
  210. $this->add_action_buttons(true);
  211. $this->set_data($data);
  212. }
  213. function get_data() {
  214. global $DB;
  215. $data = parent::get_data();
  216. if (!empty($data) && !is_numeric($data->user)) {
  217. //retrieve username
  218. $user = $DB->get_record('user', array('username' => $data->user), 'id');
  219. $data->user = $user->id;
  220. }
  221. return $data;
  222. }
  223. function validation($data, $files) {
  224. global $DB;
  225. $errors = parent::validation($data, $files);
  226. if (is_numeric($data['user'])) {
  227. $searchtype = 'id';
  228. } else {
  229. $searchtype = 'username';
  230. //check the username is valid
  231. if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) {
  232. $errors['user'] = get_string('invalidusername');
  233. }
  234. }
  235. if (!isset($errors['user'])) {
  236. $users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id');
  237. //check that the user exists in the database
  238. if (count($users) == 0) {
  239. $errors['user'] = get_string('usernameoridnousererror', 'webservice');
  240. } else if (count($users) > 1) { //can only be a username search as id are unique
  241. $errors['user'] = get_string('usernameoridoccurenceerror', 'webservice');
  242. }
  243. }
  244. return $errors;
  245. }
  246. }