Moodle

problem with "disabledif" function in "repeat_elements" function (with a solution)

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Duplicate
  • Affects Version/s: 2.0
  • Fix Version/s: None
  • Component/s: Forms Library
  • Labels:
    None
  • Affected Branches:
    MOODLE_20_STABLE

Description

The problem :
when i write this code in a mod_form file :

  1. $typeoptions[0] = get_string('Multiple_Choice','svgquizz');
    $typeoptions[1] = get_string('Compare_Text','svgquizz');
    $typeoptions[2] = get_string('Compare_Number','svgquizz');

$repeatarray=array();
$repeateloptions = array();
...
$repeatarray[] = &MoodleQuickForm::createElement('select', 'type', get_string('type','svgquizz'),$typeoptions);
...
$repeatarray[] = &MoodleQuickForm::createElement('text', 'answer_'."$i", get_string('answer','svgquizz'));
$repeateloptions['answer_'."$i"]['disabledif'] = array('type', 'eq', 0);
...
$this->repeat_elements($repeatarray, $repeatno,
$repeateloptions, 'option_repeats', 'option_add_fields', 1, get_string('repeat_button', 'svgquizz'));
#
the function 'disabledif' doesn't work because type is a repeated element and this case is not impleted in the 'repeat_elements' function.

A solution :
I propose this modification in the 'repeat_elements' function wich works fine :

#
function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,
$addfieldsname, $addfieldsno=5, $addstring=null, $addbuttoninside=false){
if ($addstring===null){ $addstring = get_string('addfields', 'form', $addfieldsno); } else {
$addstring = str_ireplace('{no}', $addfieldsno, $addstring);
}
$repeats = optional_param($repeathiddenname, $repeats, PARAM_INT);
$addfields = optional_param($addfieldsname, '', PARAM_TEXT);
if (!empty($addfields)){ $repeats += $addfieldsno; }
$mform =& $this->_form;
$mform->registerNoSubmitButton($addfieldsname);
$mform->addElement('hidden', $repeathiddenname, $repeats);
//value not to be overridden by submitted value
$mform->setConstants(array($repeathiddenname=>$repeats));
###$namecloned = array();###
for ($i=0; $i<$repeats; $i++) {
foreach ($elementobjs as $elementobj){
$elementclone = clone($elementobj);
$name = $elementclone->getName();
###$namecloned[]=$name;###
if (!empty($name)){ $elementclone->setName($name."[$i]"); }
if (is_a($elementclone, 'HTML_QuickForm_header')){
$value=$elementclone->_text;
$elementclone->setValue(str_replace('{no}', ($i+1), $value));

} else {
$value=$elementclone->getLabel();
$elementclone->setLabel(str_replace('{no}', ($i+1), $value));

}

$mform->addElement($elementclone);
}
}
for ($i=0; $i<$repeats; $i++) {
foreach ($options as $elementname => $elementoptions){
$pos=strpos($elementname, '[');
if ($pos!==FALSE){ $realelementname = substr($elementname, 0, $pos+1)."[$i]"; $realelementname .= substr($elementname, $pos+1); }else { $realelementname = $elementname."[$i]"; }
foreach ($elementoptions as $option => $params){

switch ($option){
case 'default' :
$mform->setDefault($realelementname, $params);
break;
case 'helpbutton' :
$mform->setHelpButton($realelementname, $params);
break;
case 'disabledif' :
###
foreach ($namecloned as $num => $name){
if ($params[0] == $name){ $params[0] = $params[0]."[$i]"; }
}

        1. $params = array_merge(array($realelementname), $params);
          call_user_func_array(array(&$mform, 'disabledIf'), $params);
          break;
          case 'rule' :
          if (is_string($params)){ $params = array(null, $params, null, 'client'); }
          $params = array_merge(array($realelementname), $params);
          call_user_func_array(array(&$mform, 'addRule'), $params);
          break;

}
}
}
}
$mform->addElement('submit', $addfieldsname, $addstring);

if (!$addbuttoninside) { $mform->closeHeaderBefore($addfieldsname); }

return $repeats;
}
#

New code begin by ### and finish by ###

Issue Links

Activity

Hide
Matthieu Nué added a comment -

After reading the 'Guidelines for contributed code', i have put this issue in "Add a project here" of the "contrib" project.
Excuse me for my ignorance.
Perhaps, someone can delete this one.

Amitiés

Matthieu

Show
Matthieu Nué added a comment - After reading the 'Guidelines for contributed code', i have put this issue in "Add a project here" of the "contrib" project. Excuse me for my ignorance. Perhaps, someone can delete this one. Amitiés Matthieu
Hide
Tim Hunt added a comment -

These two look like exact duplicates (filed by the same person too!)

Resolving the lower numbered one as dupe since the other has more useful discussion.

Show
Tim Hunt added a comment - These two look like exact duplicates (filed by the same person too!) Resolving the lower numbered one as dupe since the other has more useful discussion.
Hide
Sam Hemelryk added a comment -

Duplicate issue has now been closed

Show
Sam Hemelryk added a comment - Duplicate issue has now been closed

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: