Help with forms

Help with forms

از Jean-Michel Védrine در
Number of replies: 4
Hello to all,
I am trying to upgrade the dragdrop questiontype to work with Moodle 1.9
To do so I need to use the forms library to generate the screen to edit the question.
Each "answer" to a dragdrop question is a "hotspot" (a rectangular area where the student must drag an image or other media)
To define each hotspot the original code use several groups of 4 text fields on the same line (see picture).
I know how to generate a repeating group of items as in other question types using $mform->createElement and $this->repeat_elements.
But I don't understand how to include a group of 4 text fields on the same line in this repeating group of items.
I have read the forms library documentation with no luck.
Any help ? Thanks a lot.
Attachment dragdrops_creen.JPG
میانگین امتیازات:  -
In reply to Jean-Michel Védrine

Re: Help with forms

از Tim Hunt در
عکس Core developers عکس Documentation writers عکس Particularly helpful Moodlers عکس Peer reviewers عکس Plugin developers
You need to use add group: http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition#addGroup.

Alternatively, you can look at what Adriane Boyd did with the Image Target question type, where the editing form uses YUI to let you specify regions on an image by dragging a rectangle.
In reply to Tim Hunt

Re: Help with forms

از Jean-Michel Védrine در

Hello Tim,

Thanks a lot for your help. But I read several time the help on how to add group and I didn't manage to use it in my context of a repeating group. Take a look at my code :

$repeated = array();
... some lines deleted for clarity ...
$repeated[] =& $mform->createElement('text', 'ddmediatargetx', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
$repeated[] =& $mform->createElement('text', 'ddmediatargety', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
$repeated[] =& $mform->createElement('text', 'ddmediadisplaywidth', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
$repeated[] =& $mform->createElement('text', 'ddmediadisplayheight', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
... some lines deleted for clarity
if (isset($this->question->options)){
$countanswers = count($this->question->options->media);
} else {
$countanswers = 0;
}
$repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
$repeatedoptions = array();
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreanswerblanks', 'qtype_dragdrop'));

How can I do to group the 4 text fields on the same line ? Obviously I can't use $mform->addGroup here or I am missing something ?

In reply to Jean-Michel Védrine

Re: Help with forms

از Jamie Pratt در
for a repeated group try something like this with createElement('group':

$repeated = array();
 $repeatedgroup = array();
 ... some lines deleted for clarity ...
 $repeatedgroup[] =& $mform->createElement('text', 'ddmediatargetx', '', array('size' => 4));
 $repeatedgroup[] =& $mform->createElement('text', 'ddmediatargety', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
 $repeatedgroup[] =& $mform->createElement('text', 'ddmediadisplaywidth', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
 $repeatedgroup[] =& $mform->createElement('text', 'ddmediadisplayheight', get_string('imageposition', 'qtype_dragdrop'), array('size' => 4));
$repeated[] =& $mform->createElement('group', 'ddmediagroup', $label, $repeatedgroup);

In reply to Jamie Pratt

Re: Help with forms

از Jean-Michel Védrine در
Hello Jamie,
As expected from a formslib expert your solution works perfectly.
But I still have a problem with the default values of these fields :
$default_values['ddmediagroup['.$key.'][ddmediatargetx]']= $myvalue;
set all ddmediatargetx fields of all ddmediagroup groups irrespective of the $key value to $myvalue !
So I need a little more help !