Forums | Mahara Community

Developers /
Pieform : how to add elements to an array in a form


27 June 2017, 12:25 AM

Hi Folks,

I would have your feedback and help on a specific point.

I would like to know how to have several element in a form using foreach.

Here is the context :

  • I'm creating a survey plugin. Each survey is composed with several questions. Each Question is composed with several option. I'm trying to show the page.
  • Every input are already in the database.

I'm using a code structured like this :

"If (!empty)

    foreach..... search each question linked to the current survey

                foreach.... for each question look for each option linked to it."

My tests are not working very well with Pieform. It show only the last question with its options.

Is there something important to do in order to work with this approach ?

Your help would be really appreciated.

Regards

Gregor Anželj's profile picture
Posts: 347

27 June 2017, 7:25 AM

De Chiara hi.

Some time ago I attempted the same thing, that is the survey plugin... You can see it at https://wiki.mahara.org/wiki/Plugins/Artefact/Survey and give it a try.

It works in that way that survey data is stored in separate XML file (to make translating process easier).

Lately I've been thinking to rewrite it and store questions and options/answers in the database - similar to how qualification frameworks work in Mahara. Maybe we could join forces?

To your question (from the top of my head, I would do something like):

function get_question_options($question_id) {
// get options based on $question_id from the DB
$options = ...
$data = array();
foreach ($options as $value => $title) {
$data[$value] = $title;
}
return $data;
}

$elements = array();
$i = 1;
foreach ($questions as $question) {
$elements['question'.$i] = array(
'type' => 'select',
'title' => 'Question text here...',
'options' => get_question_options($question_id),
);
}

$form = array(
'name' => 'formname',
// add other form related data here
...
'elements' => $elements
);
$form = pieform($form);

 

04 July 2017, 1:18 AM

Hello,

Thank you for your reply !

I'll test it on Wednesday. :)

I'll keep you up-to-date !

08 July 2017, 2:04 AM

Hello ! So, it works :

Affichage.PNG

Here my form :

$surveyquestions = array(
'name' => 'sresponseform',
'method' => 'post',
'autofocus' => true,
'elements' => array(
'id' => array(
'type' => 'hidden',
'value' => $_GET['id'],
),
'optional' => array(
'type' => 'fieldset',
'class' => 'view-responses',
'elements' => $elements
),
'submit' => array(
'type' => 'submitcancel',
'class' => 'btn btn-primary',
'value' => array(get_string('submitsurveyform', 'survey'), get_string('submitcancel', 'survey')) // Enter New Survey and Cancel
)
)
);

I can't use an input type hidden in 'optional' for the questions id ? Like ..

$elements['survey_questions_id' . $i] => array(
'type' => 'hidden',
'value' => $id,
),

With the others ?

foreach ($questions as $question)
{
$id = $question->id;
$choices = $survey::selectResponses($id);

if(!empty($choices))
{
$elements['question' . $id] = array(
'type' => 'radio',
'id' => $question->id,
'description' => 'Choisssez une option', // Choose an option
'title' => $question->question,
'options' => $choices,
);
}
}
// endquestions

I'm trying to understand how the forms works with the Mahara system ...

Thanks !!

Robert Lyon's profile picture
Posts: 343

10 July 2017, 6:17 AM

Hi,

There is a bit of code in the fieldset renderer that stops hidden elements being there near the - "You cannot put hidden elements in fieldsets" - The reason for this is twofold.

1) It avoids people making fieldsets only containing hidden fields. This is important as the fieldset has a bunch of HTML markup associated with it relating to layout/display and having hidden fields will break this.

2) Hidden fields by there nature are not shown to the user and so in general should be placed at the beginning or the end of a form.

 

Also to note the 'hidden' element can take an array of values so you could also do

$ids = array(1,2,3,4);
$elements
['survey_questions_ids'] => array(
'type' => 'hidden',
'value' => $ids,
)

at the end of your form if you are needing to pass a bunch of 'id' values as part of your form.

Cheers

Robert

14 July 2017, 12:42 AM

Hello Robert,

Thank you for your answer !

6 results