Skip to content

Commit

Permalink
Merge branch 'MDL-30140-MOODLE_21_STABLE' of git://github.com/mouneyr…
Browse files Browse the repository at this point in the history
…ac/moodle into MOODLE_21_STABLE
  • Loading branch information
Sam Hemelryk committed Dec 14, 2011
2 parents 9822be6 + f39a641 commit 3d2ac80
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
65 changes: 55 additions & 10 deletions admin/webservice/forms.php
Expand Up @@ -182,16 +182,25 @@ function definition() {
$mform->addElement('header', 'token', get_string('token', 'webservice'));

if (empty($data->nouserselection)) {
//user searchable selector - get all users (admin and guest included)
$sql = "SELECT u.id, u.firstname, u.lastname
FROM {user} u
ORDER BY u.lastname";
$users = $DB->get_records_sql($sql, array());
$options = array();
foreach ($users as $userid => $user) {
$options[$userid] = $user->firstname . " " . $user->lastname;

//check if the number of user is reasonable to be displayed in a select box
$usertotal = $DB->count_records('user',
array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1));

if ($usertotal < 500) {
//user searchable selector - get all users (admin and guest included)
$users = $DB->get_records('user',
array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1), 'lastname',
'id, firstname, lastname');
$options = array();
foreach ($users as $userid => $user) {
$options[$userid] = $user->firstname . " " . $user->lastname;
}
$mform->addElement('searchableselector', 'user', get_string('user'), $options);
} else {
//simple text box for username or user id (if two username exists, a form error is displayed)
$mform->addElement('text', 'user', get_string('usernameorid', 'webservice'));
}
$mform->addElement('searchableselector', 'user', get_string('user'), $options);
$mform->addRule('user', get_string('required'), 'required', null, 'client');
}

Expand Down Expand Up @@ -225,8 +234,44 @@ function definition() {
$this->set_data($data);
}

function validation($data, $files) {
function get_data() {
global $DB;
$data = parent::get_data();

if (!empty($data) && !is_numeric($data->user)) {
//retrieve username
$user = $DB->get_record('user', array('username' => $data->user), 'id');
$data->user = $user->id;
}
return $data;
}

function validation(&$data, $files) {
global $DB;

$errors = parent::validation($data, $files);

if (is_numeric($data['user'])) {
$searchtype = 'id';
} else {
$searchtype = 'username';
//check the username is valid
if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) {
$errors['user'] = get_string('invalidusername');
}
}

if (!isset($errors['user'])) {
$users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id');

//check that the user exists in the database
if (count($users) == 0) {
$errors['user'] = get_string('usernameoridnousererror', 'webservice');
} else if (count($users) > 1) { //can only be a username search as id are unique
$errors['user'] = get_string('usernameoridoccurenceerror', 'webservice');
}
}

return $errors;
}

Expand Down
4 changes: 4 additions & 0 deletions lang/en/webservice.php
Expand Up @@ -189,6 +189,10 @@
$string['userasclients'] = 'Users as clients with token';
$string['userasclientsdescription'] = 'The following steps help you to set up the Moodle web service for users as clients. These steps also help to set up the recommended token (security keys) authentication method. In this use case, the user will generate his token from the security keys page via My profile settings.';
$string['usermissingcaps'] = 'Missing capabilities: {$a}';
$string['usernameorid'] = 'Username / User id';
$string['usernameorid_help'] = 'Enter a username or a user id.';
$string['usernameoridnousererror'] = 'No users were found with this username/user id.';
$string['usernameoridoccurenceerror'] = 'More than one user was found with this username. Please enter the user id.';
$string['usernotallowed'] = 'The user is not allowed for this service. First you need to allow this user on the {$a}\'s allowed users administration page.';
$string['usersettingssaved'] = 'User settings saved';
$string['validuntil'] = 'Valid until';
Expand Down

0 comments on commit 3d2ac80

Please sign in to comment.