我有2个选择列表
- ausers - 列出所有可用用户
- susers - 显示所选用户
醇>
我使用moodles global $SESSION
变量来存储用户选择项
然后我使用$SESSION->susers
将所选项加载到susers列表中
变量。
当用户点击“添加用户”按钮时会发生这种情况。
我正在使用会话变量来保存选择,因为我需要发布 形式不止一次。 (表单将有另一个课程选择列表)
我无法使用javascript来完成此操作,因为我的客户希望脚本在没有js支持的情况下运行。
注意:
我的代码正确加载选项 但会显示当前的
$SESSION
数据 以及之前的$SESSION
数据 选择列表。我认为问题可能是select元素的刷新问题。 我使用以下语句在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);
}
答案 0 :(得分:0)
<强>解决!强>
我基本上必须检查$ _POST变量以在我的选择列表上执行适当的操作。
这必须每次都在definition_after_data
函数中完成
表单已加载(刷新,发布等)。会话变量可以很好地保持用户以前的选择加载。
这些简单的代码解决了我的问题。
//Load users
if (!isset($_POST['uaddsel']) and !isset($_POST['uremovesel']) ) {
$this->load_session_users(); //load previous selected user in susers
}
//Load courses
if (!isset($_POST['caddsel']) and !isset($_POST['cremovesel']) ) {
$this->load_session_courses();
}
在我的index.php中我会打电话:
//Add user
if (isset($_POST['uaddsel']) ) {
//add user
$form->set_user_selections();
}
//remove user
if (isset($_POST['uremovesel']) ) {
$form->remove_selected_users();
}