我有 2 个选择列表
- ausers - 列出所有可用用户
- susers - 显示选定的用户
我使用moodlesglobal $SESSION
变量来存储用户选择项,然后我使用该变量将选定项加载到susers 列表中$SESSION->susers
。当用户单击“添加用户”按钮时会发生这种情况。
我正在使用会话变量来保存选择,因为我需要多次发布表单。(表格将有另一个课程选择列表)
我无法使用 javascript 来完成此操作,因为我的客户希望脚本在没有 js 支持的情况下运行。
笔记 :
我的代码正确加载了选项,但在选择列表中显示了当前
$SESSION
数据和以前的$SESSION
数据。我认为问题可能是选择元素的刷新问题。我使用以下语句在 susers 元素中加载选定的用户$susers_el->load($susers);
为了更好地说明问题:
在“添加用户”按钮上单击
选定用户 (ausers) 1.Joe 2.James 选择列表 (susers) 1.Joe 2.James
当我想将另一个用户添加到 susers 列表时,会发生以下情况:在“添加用户”按钮上单击
选定用户 (ausers) 3.Lance 选择列表 (susers) 1.Joe 2.James 1.Joe 2.James 3.Lance
以下是当前代码,非常感谢任何帮助或建议。
class learner_progress_form extends moodleform {
function definition() {
global $SESSION, $CFG;
fb('Create form');
$mform = & $this->_form;
//User Section
$mform->addElement('header', 'site_users', 'Site Users');
$objs[] = array();
$ausers[] = array();
//All users & courses sessions
$SESSION->all_users = $this->get_all_users();
//Available users $ courses
$ausers = $SESSION->all_users;
//Form design
$objs[0] =& $mform->createElement('select', 'ausers', 'Available users', $ausers, 'size="15"');
$objs[0]->setMultiple(true);
$objs[1] =& $mform->createElement('select', 'susers', 'Selected users', null, 'size="15"');
$objs[1]->setMultiple(true);
$grp =& $mform->addElement('group', 'usersgrp', 'Users', $objs, ' ', false);
$objs = array();
$objs[] =& $mform->createElement('submit', 'uaddsel','Add');
$objs[] =& $mform->createElement('submit', 'uremovesel', 'Remove');
$grp =& $mform->addElement('group', 'ubuttonsgrp', null, $objs, array(' ', '<br />'), false);
$renderer =& $mform->defaultRenderer();
$template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
$renderer->setGroupElementTemplate($template, 'usersgrp');
//Button Section
$mform->addElement('header','actions','Generate report');
$objs = array();
$objs[] =& $mform->createElement('submit', 'submit', 'Go');
$mform->addElement('group', 'actionsgrp', 'Generate report with selected data', $objs, ' ', false);
}
function definition_after_data() {
global $SESSION;
fb('definition after data');
$mform =& $this->_form;
//Get users group and elements
$usersgrp =& $mform->getElement('usersgrp')->getElements();
//Selected Users
$susers_el =& $usersgrp[1]; //selected users
$susers = $SESSION->susers;
$susers_el->load($susers);
}
function get_all_users() {
global $SESSION, $CFG;
$ausers = array();
$userlist = get_records_sql(
"select id, firstname, lastname, email
from {$CFG->prefix}user
where id NOT IN (0,1,2)"); //exclude empty,admin & guest
foreach($userlist as $user){
if (!empty($user) ) {
$ausers[$user->id] = $user->firstname." ".$user->lastname;
}
}
//sort($ausers);
return $ausers;
}
/**
* Add user(s) selections to list
* $SESSION->s_users : Stores previous selections
* TBD : Seems to be a refresh bug
* TOFIX : Duplication - possible refresh bug
**/
function set_user_selections() {
global $SESSION, $CFG;
$usergrp =& $this->_form->getElement('usersgrp')->getElements();
$ausers_el =& $usergrp[0];
$susers_el =& $usergrp[1];
$susers = $ausers_el->getSelected();
$ausers = $SESSION->all_users;
$users = array();
//build selected users array
foreach ($susers as $user=>$id) {
$users[$id] = $ausers[$id];
}
//Check selected user sessions
if (isset($SESSION->susers) )
{
//Remove duplicate selections
foreach ($SESSION->susers as $id=>$val) {
if (in_array($id, $users) )
{
unset($SESSION->susers[$id]);
}
}
$users = $SESSION->susers + $users;
}
$SESSION->susers = $users;
//asort($users);
fb('User being loaded in select list');
fb($users);
$susers_el->load($users);
}