Moodle

Adding group element in function lib/formslib.php/repeat_elements()

Details

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

Description

Adding group element to the function lib/formslib.php/repeat_elements() would improve its flexibility.
Developping an improved question units interface MDL-4474 shows the limit of the actual function.
The group element sructure is more complex than the single element (i.e. text ) and the first difficulty is related to the creation of the multiple elements step
Actual code use clone() to create the copy.
Internal element are not copied and all the cloned group element refers to the original internal element.
clone()should be replaced by fullclone().
RCS file: /cvsroot/moodle/moodle/lib/formslib.php,v
retrieving revision 1.160
diff -u -r1.160 formslib.php
— formslib.php 23 Sep 2008 14:45:58 -0000 1.160
+++ formslib.php 9 Oct 2008 04:59:26 -0000
@@ -736,7 +736,7 @@
$mform->setConstants(array($repeathiddenname=>$repeats));
for ($i=0; $i<$repeats; $i++) {
foreach ($elementobjs as $elementobj){

  • $elementclone = clone($elementobj);
    + $elementclone = fullclone($elementobj);
    $name = $elementclone->getName();
    if (!empty($name)){
    $elementclone->setName($name."[$i]");
    Other repeated elements functions should also be tested.
    The work on question units will be used to test if other code improvments are necessary.

Issue Links

Activity

Hide
Pierre Pichet added a comment -

A typical element structure
[47] => MoodleQuickForm_text Object
(
[_helpbutton] =>
[_hiddenLabel] =>
[_label] => Answer
[_type] => text
[_flagFrozen] =>
[_persistantFreeze] => 1
[_attributes] => Array
(
[name] => answer[7]
[type] => text
)

[_tabOffset] => 0
[_tab] =>
[_lineEnd] =>

[_comment] =>
)

A typical group element structure
[55] => MoodleQuickForm_group Object
(
[_helpbutton] =>
[_name] => forceregenerationgrp[0]
[_elements] => Array
(
[0] => MoodleQuickForm_radio Object
(
[_helpbutton] =>
[_text] => [[]]
[_label] =>
[_type] => radio
[_flagFrozen] =>
[_persistantFreeze] => 1
[_attributes] => Array
(
[name] => mult
[value] => 1
[type] => radio
[id] => id_6748d6
)

[_tabOffset] => 0
[_tab] =>
[_lineEnd] =>

[_comment] =>
)

[1] => MoodleQuickForm_text Object
(
[_helpbutton] =>
[_hiddenLabel] => 1
[_label] =>
[_type] => text
[_flagFrozen] =>
[_persistantFreeze] => 1
[_attributes] => Array
(
[name] => multiplier
[type] => text
[value] => 1
)

[_tabOffset] => 0
[_tab] =>
[_lineEnd] =>

[_comment] =>
)

[2] => MoodleQuickForm_static Object
(
[_elementTemplateType] => static
[_helpbutton] =>
[_text] => [[Penalty = 1.0 i.e. the unit is mandatory ]]
[_label] =>
[_type] => static
[_flagFrozen] =>
[_persistantFreeze] =>
[_attributes] => Array
(
[name] => pen
)

[_tabOffset] => 0
[_tab] =>
[_lineEnd] =>

[_comment] =>
)

)

[_separator] =>
[_required] => Array
(
)

[_appendName] => 1
[_label] =>
[_type] => group
[_flagFrozen] =>
[_persistantFreeze] =>
[_attributes] =>
[_tabOffset] => 0
[_tab] =>
[_lineEnd] =>

[_comment] =>
)
The internal group elements are refered as
if (!empty($units)) {
foreach ($units as $key => $unit){
$default_values['unit['.$key.']'] = $unit->unit;
if ($unit->multiplier < (1.0-0.0001) ){ $default_values['forceregenerationgrp['.$key.'][mult]'] = 1; }else { $default_values['forceregenerationgrp['.$key.'][mult]'] = 0; }
$default_values['forceregenerationgrp['.$key.'][multiplier]'] = $unit->multiplier;
if(!isset($unit->units_penalty) || $unit->units_penalty == '') { $default_values['forceregenerationgrp['.$key.'][penalty]'] = ($key+1) * 0.1 ;//0.0 ; } else { $default_values['forceregenerationgrp['.$key.'][penalty]'] = $unit->units_penalty; }
}
}
they should be build like
$minmaxgrp = array();
$minmaxgrp[] =& $mform->createElement('radio', "mult",null ,get_string('', 'qtype_datasetdependent'), 1);
$minmaxgrp[] =& $mform->createElement('text', "multiplier", "", '', false);
$minmaxgrp[] =& $mform->createElement('static','pen', "", get_string('Penalty = 1.0 i.e. the unit is mandatory ', 'qtype_datasetdependent'));
$repeated[] =& $mform->createElement('group','forceregenerationgrp',"",$minmaxgrp, "", true);
The fifth element should be set to true i.e. the groupname is appended to the interan elements name.
When set to false so that their name does not include the group name, the actual code does not renamed them correctly i.e does not append the [0] ,[1] etc index.
In save_question_options() they need to be retrieved as
if(isset($question->forceregenerationgrp) && is_array($question->forceregenerationgrp)){
foreach ($question->forceregenerationgrp as $key => $forceregeneration) {
if($forceregeneration['mult'] == 1) { $question->multiplier[$key] = $forceregeneration['multiplier'] ; $question->units_penalty[$key]= 1.0 ; }else { $question->multiplier[$key]= 1.0 ; $question->units_penalty[$key]= $forceregeneration['penalty'] ; }
}
}

Show
Pierre Pichet added a comment - A typical element structure [47] => MoodleQuickForm_text Object ( [_helpbutton] => [_hiddenLabel] => [_label] => Answer [_type] => text [_flagFrozen] => [_persistantFreeze] => 1 [_attributes] => Array ( [name] => answer[7] [type] => text ) [_tabOffset] => 0 [_tab] => [_lineEnd] => [_comment] => ) A typical group element structure [55] => MoodleQuickForm_group Object ( [_helpbutton] => [_name] => forceregenerationgrp[0] [_elements] => Array ( [0] => MoodleQuickForm_radio Object ( [_helpbutton] => [_text] => [[]] [_label] => [_type] => radio [_flagFrozen] => [_persistantFreeze] => 1 [_attributes] => Array ( [name] => mult [value] => 1 [type] => radio [id] => id_6748d6 ) [_tabOffset] => 0 [_tab] => [_lineEnd] => [_comment] => ) [1] => MoodleQuickForm_text Object ( [_helpbutton] => [_hiddenLabel] => 1 [_label] => [_type] => text [_flagFrozen] => [_persistantFreeze] => 1 [_attributes] => Array ( [name] => multiplier [type] => text [value] => 1 ) [_tabOffset] => 0 [_tab] => [_lineEnd] => [_comment] => ) [2] => MoodleQuickForm_static Object ( [_elementTemplateType] => static [_helpbutton] => [_text] => [[Penalty = 1.0 i.e. the unit is mandatory ]] [_label] => [_type] => static [_flagFrozen] => [_persistantFreeze] => [_attributes] => Array ( [name] => pen ) [_tabOffset] => 0 [_tab] => [_lineEnd] => [_comment] => ) ) [_separator] => [_required] => Array ( ) [_appendName] => 1 [_label] => [_type] => group [_flagFrozen] => [_persistantFreeze] => [_attributes] => [_tabOffset] => 0 [_tab] => [_lineEnd] => [_comment] => ) The internal group elements are refered as if (!empty($units)) { foreach ($units as $key => $unit){ $default_values['unit['.$key.']'] = $unit->unit; if ($unit->multiplier < (1.0-0.0001) ){ $default_values['forceregenerationgrp['.$key.'][mult]'] = 1; }else { $default_values['forceregenerationgrp['.$key.'][mult]'] = 0; } $default_values['forceregenerationgrp['.$key.'][multiplier]'] = $unit->multiplier; if(!isset($unit->units_penalty) || $unit->units_penalty == '') { $default_values['forceregenerationgrp['.$key.'][penalty]'] = ($key+1) * 0.1 ;//0.0 ; } else { $default_values['forceregenerationgrp['.$key.'][penalty]'] = $unit->units_penalty; } } } they should be build like $minmaxgrp = array(); $minmaxgrp[] =& $mform->createElement('radio', "mult",null ,get_string('', 'qtype_datasetdependent'), 1); $minmaxgrp[] =& $mform->createElement('text', "multiplier", "", '', false); $minmaxgrp[] =& $mform->createElement('static','pen', "", get_string('Penalty = 1.0 i.e. the unit is mandatory ', 'qtype_datasetdependent')); $repeated[] =& $mform->createElement('group','forceregenerationgrp',"",$minmaxgrp, "", true); The fifth element should be set to true i.e. the groupname is appended to the interan elements name. When set to false so that their name does not include the group name, the actual code does not renamed them correctly i.e does not append the [0] ,[1] etc index. In save_question_options() they need to be retrieved as if(isset($question->forceregenerationgrp) && is_array($question->forceregenerationgrp)){ foreach ($question->forceregenerationgrp as $key => $forceregeneration) { if($forceregeneration['mult'] == 1) { $question->multiplier[$key] = $forceregeneration['multiplier'] ; $question->units_penalty[$key]= 1.0 ; }else { $question->multiplier[$key]= 1.0 ; $question->units_penalty[$key]= $forceregeneration['penalty'] ; } } }
Hide
Jamie Pratt added a comment -

Used suggested change - changed clone to fullclone.

Show
Jamie Pratt added a comment - Used suggested change - changed clone to fullclone.
Hide
Petr Škoda (skodak) added a comment -

thanks, closing

Show
Petr Škoda (skodak) added a comment - thanks, closing

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: