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

/admin/webservice/forms.php

https://bitbucket.org/moodle/moodle
PHP | 223 lines | 141 code | 45 blank | 37 comment | 20 complexity | 6bd7d287b167feb29c4f721a6f8d89db MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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('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. }