如何在moodle中获取选择形式的选定项目

时间:2016-01-20 09:06:03

标签: php forms moodle multi-select

我有两个php页面。第一个是chart1.php。在这个页面中,我创建了一个getitems_form形式的对象,该对象在chartreport_getitems_form.php中定义,并处理该表单中的提交值。这段代码是这样的:

<?php
require('../../config.php');
require_once('chartreport_getitems_form.php');
.
.
$getvariables=new getvariables;
if (!isset($getvariables->avalue)) {
  $getvariables->svalue=array();
  $getvariables->avalue=$coursegroups['name'];
  $getvariables->id=$id;
}
//create form
$mform = new getitems_form(null,$getvariables);

//Form processing and displaying is done here
if ($mform->is_cancelled()) { 
...
} else if ($fromform = $mform->get_data()) {

     if(!empty($fromform ->submitbutton)){

       echo "!!!!!!!","<pre>",var_dump($fromform ->Getfirstvalue),"</pre>";
       echo "******","<pre>",var_dump($fromform ->Setfirstvalue),"</pre>";

     }
     // reset the form selections
     $mform = new getitems_form(null,$getvariables);
} else { // first call
       if (!isset($getvariables->avalue)) {
           $getvariables->svalue=array();
           $getvariables->avalue=$coursegroups['name'];
           $getvariables->id=$id;
        }
}

//===========
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();

在第二页,它的名字是chartreport_getitems_form.php,我有两个多选形式,我用两个按钮“add1”和“remove1”用javascript更改它们的选项,用于客户端进程。 在这段代码的末尾,我有一个submition按钮,可以将for的值发送到chart1.php。 我向这个按钮添加一个jquery脚本,当这个按钮提交时,在这两个选择器中选择所有选项。这项工作是为了我可以在chart1.php中获得这些选项的值。

<?php
require_once("{$CFG->libdir}/formslib.php");
require_once('lib/JEasyUI.php');

class getitems_form extends moodleform {
//Add elements to form
public function definition() {
   global $PAGE, $CFG;
    $easy_ui=new JEasyUI();
    $block_name='chartreport';  
    $js_root="/report/{$block_name}/js/jquery-easyui";
    $PAGE->requires->js("/report/{$block_name}/js/jquery-2.1.4.min.js",true);

    $mform =& $this->_form;
    $a=& $this->_customdata;

    $avalue=$a->avalue;
    $svalue=$a->svalue;
    $courseid=$a->id;


    $mform->addElement('hidden', 'id',$courseid);
    $mform->setType('id', PARAM_INT);

    $mform->addElement('header', 'firstvalue', get_string('GetGroups','report_chartreport'));

    $objs1[0] =& $mform->createElement('select', 'Getfirstvalue', get_string('Groups','report_chartreport'),$avalue,'size="15"');
    $objs1[0]->setMultiple(true);

    $objs1[1] =& $mform->createElement('select', 'Setfirstvalue', get_string('selectedGroups','report_chartreport'),$svalue,'size="15"');
    $objs1[1]->setMultiple(true);


    $grp =& $mform->addElement('group', 'group1', get_string('Groups','report_chartreport'), $objs1, array(' ', '<br />'), false);
    $mform->addGroupRule('group1', array('value' => array(array(get_string('maximumchars', '', 255), 'maxlength', 255, 'client'))));

    //====
    $script="
    function test(){
        $('select[name*=\"Getfirstvalue\"]  option:selected').appendTo('select[name*=\"Setfirstvalue\"]');
        return false;
    }";
    $objs2 = array();
    $objs2[] =& $mform->createElement('submit', 'add1', get_string('add','report_chartreport'),
    ' onclick="test(); return false;"' );
    $mform->addElement('html', html_writer::tag('script', $script));


    $scriptremove="
    function removeitem(){
        $('select[name*=\"Setfirstvalue\"]  option:selected').appendTo('select[name*=\"Getfirstvalue\"]');
        return false;
    }";
    $objs2[] =& $mform->createElement('submit', 'remove1', get_string('remove','report_chartreport'),
    ' onclick="removeitem(); return false;"');
    $mform->addElement('html', html_writer::tag('script', $scriptremove));


    $grp =& $mform->addElement('group', 'buttonsgrp', get_string('selectedlist1','report_chartreport'), $objs2, array(' ', '<br />'), false);

    $renderer =& $mform->defaultRenderer();
    $template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
    $renderer->setGroupElementTemplate($template, 'group1');

    //--------------------- 
 $scriptbutton= '$(document).ready(function(){
                        $("#id_submitbutton").click(function(){
                            $(\'select[name*=\"Getfirstvalue\"]  option\').prop(\'selected\', true);
                        });

                        $("#id_submitbutton").click(function(){
                            $(\'select[name*=\"Setfirstvalue\"]  option\').prop(\'selected\', true);
                        });

                    });';

$mform->addElement('html', html_writer::tag('script', $scriptbutton));
    $this->add_action_buttons($cancel = true, $submitlabel=get_string('DrawChart','report_chartreport'));       
}

}

这两个选择器中的不同之处只是第一个具有初始选项($ avalue),但第二个选项($ svalue)的initioal选项为null。 当我提交按钮,我想处理它们的价值。我可以获得具有initioal选项($ avalue)的选择器的值,但对于另一个我看到错误:

Notice:  Undefined property: stdClass::$Setfirstvalue in C:\MoodleWindowsInstaller-latest\server\moodle\report\chartreport\Chart1.php on line 72
NULL

所以我的问题是,为什么我不能选择“$ svalue”? 以及如何获得这些选择?

感谢

1 个答案:

答案 0 :(得分:1)

您可以获得初始选项中的选项。 你可以在chart1.php中这样做:

$getvariables->svalue=array_map(function($input) { return ''; }, $coursegroups['name']);

并在chartreport_getitems_form.php中写下:

$scriptdeleteinitialoptions='$(document).one(\'ready\', function () {
                                $("#id_Setfirstvalue").empty();
                          });';
    $mform->addElement('html', html_writer::tag('script', $scriptdeleteinitialoptions));