user selection: MDL-16995 Improve the user selector used on the assign roles and group memebers pages - Implement search.php which does the work for ajax requests.

This commit is contained in:
tjhunt 2008-10-27 08:37:50 +00:00
parent 64d2d01cfe
commit f05c20fb44
3 changed files with 100 additions and 6 deletions

View File

@ -58,11 +58,13 @@ abstract class user_selector_base {
// Public API ==============================================================
/**
* Constructor
* Constructor. Each subclass must have a constructor with this signature.
*
* @param string $name the control name/id for use in the HTML.
* @param array $options other options needed to construct this selector.
* You must be able to clone a userselector by doing new get_class($us)($us->get_name(), $us->get_options());
*/
public function __construct($name) {
public function __construct($name, $options = array()) {
global $CFG;
$this->name = $name;
if (empty($CFG->extrauserselectorfields)) {
@ -70,6 +72,9 @@ abstract class user_selector_base {
} else {
$this->extrafields = explode(',', $CFG->extrauserselectorfields);
}
if (isset($options['exclude']) && is_array($options['exclude'])) {
$this->exclude = $options['exclude'];
}
}
/**
@ -116,6 +121,8 @@ abstract class user_selector_base {
* @return mixed if $return is true, returns the HTML as a string, otherwise returns nothing.
*/
public function display($return = false) {
global $USER, $CFG;
// Ensure that the list of previously selected users is up to date.
$this->get_selected_users();
@ -158,6 +165,13 @@ abstract class user_selector_base {
// Use it again, it is rebuilt.
$this->selected = null;
// Put the options into the session for the benefit of the ajax code.
$options = $this->get_options();
$hash = md5(serialize($options));
$USER->userselectors[$hash] = $options;
$output .= '<p><a href="' . $CFG->wwwroot . '/user/selector/search.php?selectorid=' .
$hash . '&amp;' . 'sesskey=' . sesskey() . '&amp;search=">Ajax search script</a></p>'; // DONOTCOMMIT
// Return or output it.
if ($return) {
return $output;
@ -219,11 +233,12 @@ abstract class user_selector_base {
* containing at least the list of fields returned by the method
* required_fields_sql().
*/
protected abstract function find_users($search);
public abstract function find_users($search);
protected function get_options() {
return array(
'class' => get_class($this),
'name' => $this->name,
'exclude' => $this->exclude,
);
}
@ -353,13 +368,13 @@ abstract class user_selector_base {
}
class role_assign_user_selector extends user_selector_base {
protected function find_users($search) {
public function find_users($search) {
return array(); // TODO
}
}
class group_members_user_selector extends user_selector_base {
protected function find_users($search) {
public function find_users($search) {
return array(); // TODO
}
}

66
user/selector/search.php Normal file
View File

@ -0,0 +1,66 @@
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* Code to search for users in response to an ajax call from a user selector.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package userselector
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
// Check access.
require_login();
if (!confirm_sesskey()) {
print_error('invalidsesskey');
}
// Get the search parameter.
$search = required_param('search', PARAM_RAW);
// Get and validate the selectorid parameter.
$selectorhash = required_param('selectorid', PARAM_ALPHANUM);
if (!isset($USER->userselectors[$selectorhash])) {
print_error('unknownuserselector');
}
// Create the appropriate userselector.
$options = $USER->userselectors[$selectorhash];
$classname = $options['class'];
unset($options['class']);
$name = $options['name'];
unset($options['name']);
if (isset($options['file'])) {
require_once($CFG->dirroot . '/' . $options['file']);
unset($options['file']);
}
$userselector = new $classname($name, $options);
// Do the search and output the results.
$users = $userselector->find_users($search);
echo json_encode(array('results' => $users));
?>

View File

@ -1,4 +1,6 @@
<?php
$justdefineclass = defined('MOODLE_INTERNAL');
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
@ -7,7 +9,7 @@ class test_user_selector extends user_selector_base {
parent::__construct($name);
}
protected function find_users($search) {
public function find_users($search) {
global $DB;
list($wherecondition, $params) = $this->search_sql($search, 'u');
$sql = 'SELECT ' . $this->required_fields_sql('u') .
@ -25,6 +27,17 @@ class test_user_selector extends user_selector_base {
}
return $groupedusers;
}
protected function get_options() {
$options = parent::get_options();
$options['file'] = 'user/selector/test.php';
return $options;
}
}
if ($justdefineclass) {
return;
}
print_header();