3 require_once($CFG->libdir
.'/formslib.php');
4 require_once($CFG->libdir
. '/datalib.php');
6 class user_bulk_form
extends moodleform
{
8 * Quickform select object for the available users.
12 * Quickform select object for the selected users.
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);
26 $objs[] = &$this->ausers
;
27 $objs[] = &$this->susers
;
29 $mform->addElement('group', 'usersgrp', get_string('users'), $objs, ' ', false);
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);
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() {
50 $this->_updateSelection($this->get_data());
51 $this->setSelectedUsers($SESSION->bulk_susers
);
52 if(empty($SESSION->bulk_susers
)) {
53 $this->_form
->removeElement('actionsgrp');
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) {
63 if(!is_array(@$SESSION->bulk_susers
)) {
64 $SESSION->bulk_susers
= array();
66 // if the forms was not submited, then quit
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
);
89 * Sets the available users list, based on their ids
90 * @param array $ausers array of user ids
92 function setAvailableUsers($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) {
106 if(is_null($sqlwhere) ||
($users =& $this->getUserData($sqlwhere))===false) {
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) {
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) {
131 if(is_null($sqlwhere) ||
($users =& $this->getUserData($sqlwhere))===false) {
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() {
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
)){
164 return $data->action
;