]> git.mjollnir.org Git - moodle.git/commitdiff
user selection: MDL-16995 Improve the user selector used on the assign roles and...
authortjhunt <tjhunt>
Mon, 27 Oct 2008 08:37:50 +0000 (08:37 +0000)
committertjhunt <tjhunt>
Mon, 27 Oct 2008 08:37:50 +0000 (08:37 +0000)
user/selector/lib.php
user/selector/search.php [new file with mode: 0644]
user/selector/test.php

index a8f4b724aa0d597b784a9345a9ffd56a6eced118..e43300023366a0ad6a42b6dd1a3101e18ee83d52 100644 (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
     }
 }
diff --git a/user/selector/search.php b/user/selector/search.php
new file mode 100644 (file)
index 0000000..13a7caf
--- /dev/null
@@ -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));
+?>
\ No newline at end of file
index e7f6d461e2b8e7e6c836b76f26a2874725ac1c9d..2d19161a613f810b02f129b6d975318f7850cd28 100644 (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();