|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00025 require_once $CFG->libdir . '/formslib.php'; 00026 00031 class external_service_authorised_user_settings_form extends moodleform { 00032 00033 function definition() { 00034 $mform = $this->_form; 00035 $data = $this->_customdata; 00036 00037 $mform->addElement('header', 'serviceusersettings', 00038 get_string('serviceusersettings', 'webservice')); 00039 00040 $mform->addElement('text', 'iprestriction', 00041 get_string('iprestriction', 'webservice')); 00042 $mform->addHelpButton('iprestriction', 'iprestriction', 'webservice'); 00043 00044 $mform->addElement('date_selector', 'validuntil', 00045 get_string('validuntil', 'webservice'), array('optional' => true)); 00046 $mform->addHelpButton('validuntil', 'validuntil', 'webservice'); 00047 00048 $this->add_action_buttons(true, get_string('updateusersettings', 'webservice')); 00049 00050 $this->set_data($data); 00051 } 00052 00053 } 00054 00055 class external_service_form extends moodleform { 00056 00057 function definition() { 00058 $mform = $this->_form; 00059 $service = $this->_customdata; 00060 00061 $mform->addElement('header', 'extservice', 00062 get_string('externalservice', 'webservice')); 00063 00064 $mform->addElement('text', 'name', get_string('name')); 00065 $mform->addRule('name', get_string('required'), 'required', null, 'client'); 00066 $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice')); 00067 $mform->addElement('advcheckbox', 'restrictedusers', 00068 get_string('restrictedusers', 'webservice')); 00069 $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice'); 00070 00071 //can users download files 00072 $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice')); 00073 $mform->setAdvanced('downloadfiles'); 00074 $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice'); 00075 00077 $currentcapabilityexist = false; 00078 if (empty($service->requiredcapability)) { 00079 $service->requiredcapability = "norequiredcapability"; 00080 $currentcapabilityexist = true; 00081 } 00082 00083 // Prepare the list of capabilities to choose from 00084 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00085 $allcapabilities = fetch_context_capabilities($systemcontext); 00086 $capabilitychoices = array(); 00087 $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability', 00088 'webservice'); 00089 foreach ($allcapabilities as $cap) { 00090 $capabilitychoices[$cap->name] = $cap->name . ': ' 00091 . get_capability_string($cap->name); 00092 if (!empty($service->requiredcapability) 00093 && $service->requiredcapability == $cap->name) { 00094 $currentcapabilityexist = true; 00095 } 00096 } 00097 00098 $mform->addElement('searchableselector', 'requiredcapability', 00099 get_string('requiredcapability', 'webservice'), $capabilitychoices); 00100 $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice'); 00101 $mform->setAdvanced('requiredcapability'); 00103 if (empty($currentcapabilityexist)) { 00104 global $OUTPUT; 00105 $mform->addElement('static', 'capabilityerror', '', 00106 $OUTPUT->notification(get_string('selectedcapabilitydoesntexit', 00107 'webservice', $service->requiredcapability))); 00108 $service->requiredcapability = "norequiredcapability"; 00109 } 00110 00111 $mform->addElement('hidden', 'id'); 00112 $mform->setType('id', PARAM_INT); 00113 00114 if (!empty($service->id)) { 00115 $buttonlabel = get_string('editaservice', 'webservice'); 00116 } else { 00117 $buttonlabel = get_string('addaservice', 'webservice'); 00118 } 00119 00120 $this->add_action_buttons(true, $buttonlabel); 00121 00122 $this->set_data($service); 00123 } 00124 00125 function definition_after_data() { 00126 $mform = $this->_form; 00127 $service = $this->_customdata; 00128 00129 if (!empty($service->component)) { 00130 // built-in components must not be modified except the enabled flag!! 00131 $mform->hardFreeze('name,requiredcapability,restrictedusers'); 00132 } 00133 } 00134 00135 function validation($data, $files) { 00136 $errors = parent::validation($data, $files); 00137 return $errors; 00138 } 00139 00140 } 00141 00142 class external_service_functions_form extends moodleform { 00143 00144 function definition() { 00145 global $CFG; 00146 00147 $mform = $this->_form; 00148 $data = $this->_customdata; 00149 00150 $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice')); 00151 00152 require_once($CFG->dirroot . "/webservice/lib.php"); 00153 $webservicemanager = new webservice(); 00154 $functions = $webservicemanager->get_not_associated_external_functions($data['id']); 00155 00156 //we add the descriptions to the functions 00157 foreach ($functions as $functionid => $functionname) { 00158 //retrieve full function information (including the description) 00159 $function = external_function_info($functionname); 00160 $functions[$functionid] = $function->name . ':' . $function->description; 00161 } 00162 00163 $mform->addElement('searchableselector', 'fids', get_string('name'), 00164 $functions, array('multiple')); 00165 00166 $mform->addElement('hidden', 'id'); 00167 $mform->setType('id', PARAM_INT); 00168 00169 $mform->addElement('hidden', 'action'); 00170 $mform->setType('action', PARAM_ACTION); 00171 00172 $this->add_action_buttons(true, get_string('addfunctions', 'webservice')); 00173 00174 $this->set_data($data); 00175 } 00176 00177 } 00178 00179 class web_service_token_form extends moodleform { 00180 00181 function definition() { 00182 global $USER, $DB, $CFG; 00183 00184 $mform = $this->_form; 00185 $data = $this->_customdata; 00186 00187 $mform->addElement('header', 'token', get_string('token', 'webservice')); 00188 00189 if (empty($data->nouserselection)) { 00190 00191 //check if the number of user is reasonable to be displayed in a select box 00192 $usertotal = $DB->count_records('user', 00193 array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1)); 00194 00195 if ($usertotal < 500) { 00196 //user searchable selector - get all users (admin and guest included) 00197 //user must be confirmed, not deleted, not suspended, not guest 00198 $sql = "SELECT u.id, u.firstname, u.lastname 00199 FROM {user} u 00200 WHERE u.deleted = 0 AND u.confirmed = 1 AND u.suspended = 0 AND u.id != ? 00201 ORDER BY u.lastname"; 00202 $users = $DB->get_records_sql($sql, array($CFG->siteguest)); 00203 $options = array(); 00204 foreach ($users as $userid => $user) { 00205 $options[$userid] = $user->firstname . " " . $user->lastname; 00206 } 00207 $mform->addElement('searchableselector', 'user', get_string('user'), $options); 00208 } else { 00209 //simple text box for username or user id (if two username exists, a form error is displayed) 00210 $mform->addElement('text', 'user', get_string('usernameorid', 'webservice')); 00211 } 00212 $mform->addRule('user', get_string('required'), 'required', null, 'client'); 00213 } 00214 00215 //service selector 00216 $services = $DB->get_records('external_services'); 00217 $options = array(); 00218 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00219 foreach ($services as $serviceid => $service) { 00220 //check that the user has the required capability 00221 //(only for generation by the profile page) 00222 if (empty($data->nouserselection) 00223 || empty($service->requiredcapability) 00224 || has_capability($service->requiredcapability, $systemcontext, $USER->id)) { 00225 $options[$serviceid] = $service->name; 00226 } 00227 } 00228 $mform->addElement('select', 'service', get_string('service', 'webservice'), $options); 00229 $mform->addRule('service', get_string('required'), 'required', null, 'client'); 00230 00231 00232 $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice')); 00233 00234 $mform->addElement('date_selector', 'validuntil', 00235 get_string('validuntil', 'webservice'), array('optional' => true)); 00236 00237 $mform->addElement('hidden', 'action'); 00238 $mform->setType('action', PARAM_ACTION); 00239 00240 $this->add_action_buttons(true); 00241 00242 $this->set_data($data); 00243 } 00244 00245 function get_data() { 00246 global $DB; 00247 $data = parent::get_data(); 00248 00249 if (!empty($data) && !is_numeric($data->user)) { 00250 //retrieve username 00251 $user = $DB->get_record('user', array('username' => $data->user), 'id'); 00252 $data->user = $user->id; 00253 } 00254 return $data; 00255 } 00256 00257 function validation(&$data, $files) { 00258 global $DB; 00259 00260 $errors = parent::validation($data, $files); 00261 00262 if (is_numeric($data['user'])) { 00263 $searchtype = 'id'; 00264 } else { 00265 $searchtype = 'username'; 00266 //check the username is valid 00267 if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) { 00268 $errors['user'] = get_string('invalidusername'); 00269 } 00270 } 00271 00272 if (!isset($errors['user'])) { 00273 $users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id'); 00274 00275 //check that the user exists in the database 00276 if (count($users) == 0) { 00277 $errors['user'] = get_string('usernameoridnousererror', 'webservice'); 00278 } else if (count($users) > 1) { //can only be a username search as id are unique 00279 $errors['user'] = get_string('usernameoridoccurenceerror', 'webservice'); 00280 } 00281 } 00282 00283 return $errors; 00284 } 00285 00286 }