Moodle HTML Quickform 选择选项未正确加载

发布于 2024-11-04 14:28:14 字数 4737 浏览 3 评论 0原文

我有 2 个选择列表

  1. ausers - 列出所有可用用户
  2. 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

  1. ausers - List all available users
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-11-11 14:28:14

已解决!
我基本上必须检查 $_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();
  }

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 time
the 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.

//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(); 
}

and in my index.php i would call :

//Add user
  if (isset($_POST['uaddsel']) ) {
    //add user
    $form->set_user_selections();
  }
  //remove user
  if (isset($_POST['uremovesel']) ) {
    $form->remove_selected_users();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文