Own mod_form.php

Own mod_form.php

by Silvan Voser -
Number of replies: 4

Hi everybody,

I need to create an own edit page for an activity. So if I click in the course on "Update", I get to this page.

I am new to moodle and it would be great if you can give me some hints, documentation or advices how I can implements my functions in the best way. Of course I don't expect a full solution wink.

These are my questions:

  • How can I define own / new functions, not listed here? http://docs.moodle.org/dev/lib/formslib.php_Form_Definition. For example I can make $mform->add_myown_customized_editor();
  • If I save the formular, to where will the information from the fields go? (Later I need to process the values there and put them into MySQL database)
  • I need to check the values in the input fields, how can I check them with a moodle function?
  • How can I get values from the MySQL database for already existing exercises? For example I have a textarea with text. If I would like to change the text now, I need somehow to get the existing text from the MySQL database

Thanks for your help and regards,

power08

Average of ratings: Useful (1)
In reply to Silvan Voser

Re: Own mod_form.php

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you are wanting to create your own settings form, then take a look at the file mod_form.php in any of the existing Moodle activities (mod/page/mod_form.php is a fairly straightforward example).

Validation of input can be done via a 'validation()' function in the form (search for examples) or can be handled via 'addRule' calls in the form definition (again, search for examples)

The data is returned to your activity via the ACTIVITYNAME_add_instance and ACTIVITYNAME_update_instance functions in your activity's lib.php. e.g. mod/page/lib.php: page_add_instance() and page_update_instance().

If you really need a non-standard form element, then you can copy what is done in lib/form/*.php, but you will also need to somewhere call MoodleQuickForm::registerElementType (see lib/formslib.php - scroll down to the bottom). However, I'd recommend not doing this unless you really have to (the built-in types cover 95% of use cases).

Moodle handles initialising the form elements for you, assuming you give the fields in the database the same name as the form elements.

In reply to Davo Smith

Re: Own mod_form.php

by Silvan Voser -

Thanks for your very fast reply, it helped me to go further.

I defined now a button to repeat the form. Did it according to: http://docs.moodle.org/dev/lib/formslib.php_repeat_elements

My question is now, if there are already values for the forms in the MySQL database, how can I display them in the forms? Is there already a function in moodle for this?

Because I cannot display them now as I expected to do it, because repeat elements start automatically the index at 0. If I have already a form from the MySQL database with the ID 0, then I have duplicated IDs, if I would repeat the new form.

Thanks and regards,

power08

Average of ratings: Useful (1)
In reply to Silvan Voser

Populating repeat_elements fields with db data

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Did you ever come up with an answer to this? I am trying to work out how to populate fields that are generated from a call to repeat_elements with the associated data. I have generated the fields from count of the records but I cannot work out how to put the data in. I can see this is very similar to how adding hints or other arbitrary repeated fields works but I cannot see where the data is poked in.

 

 

In reply to Marcus Green

Re: Populating repeat_elements fields with db data

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The question editing forms give examples of this.