Adding autocomplete feature to Quiz edit form

Adding autocomplete feature to Quiz edit form

by v k -
Number of replies: 1

Hi all,

My goal is simple:  to add autocomplete feature for my custom fields in 'mod/quiz/mod_form.php', so that when I type some text, the possible options are retrieved from a text file on the server

I'm pretty sure it must be simple as we see it often on websites, but I need some help here, since I basically code Moodle with PHP, almost no experience with AJAX/JS. (I know that sounds silly...)

From the documentation - https://docs.moodle.org/dev/lib/formslib.php_Form_Definition#autocomplete - we know there is a class 

MoodleQuickForm_autocomplete.
It is recommended to create a subclass of MoodleQuickForm_autocomplete when using $mform.

In 'mod/quiz/mod_form.php' we see:
 class mod_wscits_mod_form extends moodleform_mod{...}
How do I add MoodleQuickForm_autocomplete here, I just cannot understand. 
Or should I go totally another way to add autocomplete feature to some field(s) in  'mod/quiz/mod_form.php'?

Thanks for reading. Any help and especially sample of autocomplete feature for some custom field would be very grateful!

Thank all!
Average of ratings: -
In reply to v k

Re: Adding autocomplete feature to Quiz edit form

by v k -

UPD:

Now I have an issue saving the autocomplete form value into DB.

I use my own class extending MoodleQuickForm_autocomplete:

class mod_form_myclass_autocomplete extends MoodleQuickForm_autocomplete {

    public function __construct($elementName = null, $elementLabel = null, $options = [], $attributes = []) {

        $elementLabel= get_string('blablabla', 'bla');        $lines=file('file.txt',FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);

        foreach ($lines as $line_num=>$line) {

            $options[$line_num]=$line;

        }        

        parent::__construct($elementName, $elementLabel, $options);

    }

File.txt contains of lines like "Option1", "Option2" etc.

I tried foreach like  foreach ($lines as $line_num=>$line) and just like foreach ($lines as $line).

This results in that when filling the form on my site, the autocomplete field shows options as they are in the file.txt: "Option1", "Option2" etc.

But when I submit and save form data to DB, not "Option1", "Option2" values are inserted into table, but their line number in file.txt, like 0,1,2..


How can I fix it? Thanks all!