Retrieve values to repeated form elements

Retrieve values to repeated form elements

by Lavanya Manne -
Number of replies: 6
Picture of Plugin developers
How do I retrieve values from database to repeated input text and attachment form elements .
I was able to retrieve the last record. The last record is repeating for title and attachment. 

edit_active_form.php
<?php
    require('config.php');
    require_once($CFG->libdir.'/formslib.php');
    $sid = required_param('id', PARAM_INT);    
    class edit_active_form extends moodleform {
        
        function definition() {
            global $DB, $sid;
                    
            $mform = $this->_form;      
            $fileoptions = $this->_customdata['fileoptions'];
                
            $repeatarray = array();
            $repeatarray[] = $mform->createElement('text', 'title', get_string('titleno', 'local_statistics')); 
            $repeatarray[] = $mform->createElement('filemanager', 'attachment', get_string('attachmentno', 'local_statistics'), null, $fileoptions); 
            $repeatarray[] = $mform->createElement('hidden', 'titleid', 0);
            $repeatarray[] = $mform->createElement('hidden', 'attachmentid', 0);    
         
            if ($instance){
                $repeatno = $DB->count_records('statistics', array('statid'=>$instance));
                $repeatno += 2; 
            } else {
                $repeatno = 1;
            }
                
            $repeateloptions = array();
            // Get the title and attachment values from database.
            $get_title = $DB->get_recordset_sql('SELECT * FROM {statistics} WHERE sid = '.$sid.''); 
            foreach($get_title as $gt) {
                $repeateloptions['title['.$gt->id.']']['default'] = $gt->title;
                $repeateloptions['attachment['.$gt->id.']']['default'] = $gt->attachment;
            }
            
            $repeateloptions['title']['type'] = PARAM_RAW;              
            $repeateloptions['attachment']['type'] = PARAM_INT;
                
            $mform->setType('title', PARAM_CLEANHTML);
            $mform->setType('titleid', PARAM_INT);
            $mform->setType('attachment', PARAM_CLEANHTML);
            $mform->setType('attachmentid', PARAM_INT);
                
            $this->repeat_elements($repeatarray, $repeatno, $repeateloptions, null, true);
            $this->add_action_buttons();        
        }
        
        function validation($data, $files) {
            $errors = parent::validation($data, $files);
            return $errors;
        }
    }

$mform = new active_form();

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

Average of ratings: -
In reply to Lavanya Manne

Re: Retrieve values to repeated form elements

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I suggest you look at mod/choice/mod_form.php and the code in choice_add_instance in mod/choice/lib.php which processes the data. (I was trying to think of the simplest example fo this in Moodle core.)
In reply to Tim Hunt

Re: Retrieve values to repeated form elements

by Lavanya Manne -
Picture of Plugin developers
Thanks Tim Hunt!

In mod/choice/lib.php, the code for add_instance is for adding a new instance to the form. I was looking to retrieve the inserted values from database back to repeated form elements.
Is there a way to set Default value to dynamic. (like the one in my code in loop ).
In reply to Lavanya Manne

Re: Retrieve values to repeated form elements

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, the choice activity must do that somehow, because you can edit an existing Choice. However, I don't remember how exactly that happens. I would need to look at the code. But, you are the one that needs to understand this, so can I suggest that you do the necessary reverse engineering?

(Or wait for someone else who remembers how this works to answer.)
In reply to Tim Hunt

Re: Retrieve values to repeated form elements

by Lavanya Manne -
Picture of Plugin developers
Tim Hunt,

I was able to retrieve the values with last id. What I thought is to have an id set to default value, and is not working though.
ex: $repeateloptions['title['.$gt->id.']']['default'] = $gt->title;

I don't see syntax code for repeated elements, with default values on forums.
In reply to Lavanya Manne

Re: Retrieve values to repeated form elements

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Ah! I have now remebered that there there is something complex about defaults for repeated elements, probably best explained by this comment here: https://github.com/moodle/moodle/blob/master/question/type/edit_question_form.php#L628. I hope that helps.

(I did not point you at the question forms before, even though they use repeat elements a lot, because they are generally quite complex. However, for this detail, they would have been more helpfule.)
In reply to Tim Hunt

Re: Retrieve values to repeated form elements

by Lavanya Manne -
Picture of Plugin developers
Thank You Tim Hunt!

The issue was solved!

The syntax for editing repeated form element would be like:

$i=0;
foreach($get_title as $gt) {
$mform->setDefault('attachment['.$i.']', $gt->attachment);
$i++;
}

Hope this would help someone. Many Thanks!