Should be $COURSE not $course
[moodle-linuxchix.git] / admin / user_bulk_form.php
blob2b3d23bb3eba232b0979ba1fc2936e16275f84df
1 <?php //$Id$
3 require_once($CFG->libdir.'/formslib.php');
4 require_once($CFG->libdir . '/datalib.php');
6 class user_bulk_form extends moodleform {
7 /**
8 * Quickform select object for the available users.
9 */
10 var $ausers;
11 /**
12 * Quickform select object for the selected users.
14 var $susers;
15 // form definition
16 function definition() {
17 $mform =& $this->_form;
18 $mform->addElement('header', 'users', get_string('usersinlist', 'bulkusers'));
20 $this->ausers =& $mform->createElement('select', 'ausers', get_string('filtered', 'bulkusers'), null, 'size="15"');
21 $this->ausers->setMultiple(true);
22 $this->susers =& $mform->createElement('select', 'susers', get_string('selected', 'bulkusers'), null, 'size="15"');
23 $this->susers->setMultiple(true);
25 $objs = array();
26 $objs[] = &$this->ausers;
27 $objs[] = &$this->susers;
29 $mform->addElement('group', 'usersgrp', get_string('users'), $objs, ' ', false);
31 $objs = array();
32 $objs[] =& $mform->createElement('submit', 'addone', get_string('addsel', 'bulkusers'));
33 $objs[] =& $mform->createElement('submit', 'removeone', get_string('removesel', 'bulkusers'));
34 $objs[] =& $mform->createElement('submit', 'addall', get_string('addall', 'bulkusers'));
35 $objs[] =& $mform->createElement('submit', 'removeall', get_string('removeall', 'bulkusers'));
36 $mform->addElement('group', 'buttonsgrp', null, $objs, array(' ', '<br />'), false);
38 $objs = array();
39 $objs[] =& $mform->createElement('select', 'action', get_string('withselected'), @$this->_customdata);
40 $objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));;
41 $mform->addElement('group', 'actionsgrp', get_string('actions'), $objs, ' ', false);
43 $renderer =& $mform->defaultRenderer();
44 $template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
45 $renderer->setGroupElementTemplate($template, 'usersgrp');
48 function definition_after_data() {
49 global $SESSION;
50 $this->_updateSelection($this->get_data());
51 $this->setSelectedUsers($SESSION->bulk_susers);
52 if(empty($SESSION->bulk_susers)) {
53 $this->_form->removeElement('actionsgrp');
57 /**
58 * Updates the user selection based on the data submited to the form.
59 * @param object $data object with data received by the form
61 function _updateSelection($data) {
62 global $SESSION;
63 if(!is_array(@$SESSION->bulk_susers)) {
64 $SESSION->bulk_susers = array();
66 // if the forms was not submited, then quit
67 if(is_null($data)){
68 return;
70 if(@$data->addall) {
71 if(!empty($SESSION->bulk_ausers)) {
72 $SESSION->bulk_susers = array_merge($SESSION->bulk_susers, $SESSION->bulk_ausers);
74 } else if(@$data->addone) {
75 if(!empty($data->ausers)) {
76 $SESSION->bulk_susers = array_merge($SESSION->bulk_susers, array_values($data->ausers));
78 } else if(@$data->removeone) {
79 if(!empty($data->susers)) {
80 $SESSION->bulk_susers = array_diff($SESSION->bulk_susers, array_values($data->susers));
82 } else if(@$data->removeall) {
83 $SESSION->bulk_susers = array();
85 $SESSION->bulk_susers = array_unique($SESSION->bulk_susers);
88 /**
89 * Sets the available users list, based on their ids
90 * @param array $ausers array of user ids
92 function setAvailableUsers($ausers) {
93 $sqlwhere = null;
94 if(!empty($ausers)) {
95 $sqlwhere = 'id IN (' . implode(',', $ausers) . ')';
97 $this->setAvailableUsersSQL($sqlwhere);
101 * Sets the available users list, based on a SQL where condition
102 * @param string $sqlwhere filter for the users
104 function setAvailableUsersSQL($sqlwhere=null) {
105 global $SESSION;
106 if(is_null($sqlwhere) || ($users =& $this->getUserData($sqlwhere))===false) {
107 $users = array();
109 $SESSION->bulk_ausers =& array_keys($users);
110 $this->ausers->load($users);
114 * Sets the selected users list, based on their ids
115 * @param array $ausers array of user ids
117 function setSelectedUsers($susers) {
118 $sqlwhere = null;
119 if(!empty($susers)) {
120 $sqlwhere = 'id IN (' . implode(',', $susers) . ')';
122 $this->setSelectedUsersSQL($sqlwhere);
126 * Sets the selected users list, based on a SQL where condition
127 * @param string $sqlwhere filter for the users
129 function setSelectedUsersSQL($sqlwhere=null) {
130 global $SESSION;
131 if(is_null($sqlwhere) || ($users =& $this->getUserData($sqlwhere))===false) {
132 $users = array();
134 $SESSION->bulk_susers =& array_keys($users);
135 $this->susers->load($users);
139 * Returns information about the users.
140 * @param string $sqlwhere filter for the users
142 function getUserData($sqlwhere) {
143 return get_records_select_menu('user', $sqlwhere, 'fullname', 'id,' . sql_fullname() . ' AS fullname');
147 * Returns an array of ids of selected users.
148 * @return array of selected users' ids
150 function getSelectedUsers() {
151 global $SESSION;
152 return $SESSION->bulk_susers;
156 * Returns an int code of the action to be performed.
157 * @return int code of the action or false if no action should be performed
159 function getAction() {
160 $data =& $this->get_data();
161 if(!$this->is_submitted() || empty($data->doaction)){
162 return false;
164 return $data->action;