// 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)) {
} else {
$this->extrafields = explode(',', $CFG->extrauserselectorfields);
}
+ if (isset($options['exclude']) && is_array($options['exclude'])) {
+ $this->exclude = $options['exclude'];
+ }
}
/**
* @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();
// 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 . '&' . 'sesskey=' . sesskey() . '&search=">Ajax search script</a></p>'; // DONOTCOMMIT
+
// Return or output it.
if ($return) {
return $output;
* 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,
);
}
}
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
}
}
--- /dev/null
+<?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));
+?>
\ No newline at end of file
<?php
+$justdefineclass = defined('MOODLE_INTERNAL');
+
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
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') .
}
return $groupedusers;
}
+
+ protected function get_options() {
+ $options = parent::get_options();
+ $options['file'] = 'user/selector/test.php';
+ return $options;
+ }
+
+}
+
+if ($justdefineclass) {
+ return;
}
print_header();