Moodle HTML Quickform 选择选项未正确加载
我有 2 个选择列表
- ausers - 列出所有可用用户
- susers - 显示选定的用户
我使用moodles global $SESSION
变量来存储用户选择项 然后我使用 $SESSION->susers
将所选项目加载到 susers 列表中 多变的。 当用户单击“添加用户”按钮时,就会发生这种情况。
我正在使用会话变量来保存选择,因为我需要发布 多次填写该表格。 (该表单将有另一个课程选择列表)
我无法使用 javascript 来完成此操作,因为我的客户希望脚本在没有 js 支持的情况下运行。
注意:
我的代码正确加载选项 但显示当前的
$SESSION
数据 以及之前的$SESSION
数据 选择列表。我认为问题可能是选择元素的刷新问题。 我使用以下语句在 susers 元素中加载选定的用户$susers_el->load($susers);
为了更好地说明问题:
在“添加用户”按钮上单击
选定的用户(ausers) 1.乔 2.詹姆斯 选择列表(suser) 1.乔 2.詹姆斯
当我想将另一个用户添加到 susers 列表中时,会发生以下情况: 在“添加用户”按钮上单击
选定的用户(ausers) 3.兰斯 选择列表(suser) 1.乔 2.詹姆斯 1.乔 2.詹姆斯 3.兰斯
下面是当前的代码,非常感谢任何帮助或建议。
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);
}
I have 2 select lists
- ausers - List all available users
- susers - Displays selected users
I use moodles global $SESSION
variable to store the users selection items
and then I load the selected items into the susers list using the $SESSION->susers
variable.
This happens when the user clicks on the "Add user" button.
I am using a session variable to save the selections since I need to post
the form more than once. (The form will have another course selection list)
I cannot use javascript to accomplish this since my client wants the script to function without js support.
NOTE :
My code loads the options correctly
but displays the current$SESSION
data
and the previous$SESSION
data in the
select list.I think the problem might be a refresh issue of the select element.
I use the following statement to load the selected users in the susers element$susers_el->load($susers);
To better illustrate the problem :
On "Add user" button click
Selected users (ausers)
1.Joe
2.James
Select List (susers)
1.Joe
2.James
When I want to add another user to the susers list the following happens:
On "Add user" button click
Selected users (ausers)
3.Lance
Select List (susers)
1.Joe
2.James
1.Joe
2.James
3.Lance
Below is the current code, any help or advice is most appreciated.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决!
我基本上必须检查 $_POST 变量才能对我的选择列表执行适当的操作。
每次都必须在
definition_after_data
函数中完成此操作表单已加载(刷新、发布等)。会话变量可以很好地保持加载用户之前的选择。
这些简单的代码行解决了我的问题。
在我的index.php中我会调用:
SOLVED!
I basically had to check the $_POST variables to perform the appropriate action on my select lists.
This had to be done in the
definition_after_data
function every timethe form is loaded (refreshed, posted etc). The session variables work great to keep load the users previous selections.
These simple lines of code solved my problem.
and in my index.php i would call :