问题
I want to create a custom form in moodle and store the form data in database table. I've been studying moodle form library but it's too complex for me.
Any help or guidance or tutorial or reference guide or ebook on how to create custom forms in moodle and store the forms data in the database will be much appreciated.
回答1:
You have several ways to do it. The cleaner is to use the Form API (http://docs.moodle.org/dev/Form_API).
By the way, you can easily create your own form using PHP in a local plugin, using the Page API (http://docs.moodle.org/dev/Page_API).
Here is a simple example :
<?php
require_once('../../config.php');
global $CFG, $PAGE;
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title('Form name');
$PAGE->set_heading('Form name');
$PAGE->set_url($CFG->wwwroot.'/local/yourform/index.php');
echo $OUTPUT->header();
?>
<form method="post" action="post.php">
... Your form code goes here
</form>
<?php
... Your PHP data handling code
echo $OUTPUT->footer();
?>
Put this code into a new directory inside the "local" directory from Moodle root. Example :
/moodle/local/yourform/index.php
Then, access your form by adding local/yourform/index.php
at the end of your Moodle root URL.
回答2:
It is preferred to use the Form API. This handles input validation, prefilling the form, etc.
For detailed information, see: https://docs.moodle.org/dev/Form_API
Highlights Tested and optimised for use on major screen-readers Dragon and JAWS. Tableless layout. Process form data securely, with required_param, optional_param and session key. Supports client-side validation Facility to add Moodle help buttons to forms. Support for file repository using File_API Support for many custom moodle specific and non-specific form elements. Addition for repeated elements. Addition for form elements in advance group Usage For creating a form in moodle, you have to create class extending moodleform class and override definition for including form elements.
//moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");
class simplehtml_form extends moodleform {
//Add elements to form
public function definition() {
global $CFG;
$mform = $this->_form; // Don't forget the underscore!
$mform->addElement('text', 'email', get_string('email')); // Add elements to your form
$mform->setType('email', PARAM_NOTAGS); //Set type of element
$mform->setDefault('email', 'Please enter email'); //Default value
...
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
Then instantiate form (in this case simplehtml_form) on your page.
//include simplehtml_form.php
require_once('PATH_TO/simplehtml_form.php');
//Instantiate simplehtml_form
$mform = new simplehtml_form();
//Form processing and displaying is done here
if ($mform->is_cancelled()) {
//Handle form cancel operation, if cancel button is present on form
} else if ($fromform = $mform->get_data()) {
//In this case you process validated data. $mform->get_data() returns data posted in form.
} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.
//Set default data (if any)
$mform->set_data($toform);
//displays the form
$mform->display();
}
回答3:
There's a plugin for creating forms for Moodle that lets you easily create forms for registration, feedback, surveys, and contact. We can do customization according to our own needs. We can embed form anywhere on Moodle using the shortcode.
https://edwiser.org/forms/
来源:https://stackoverflow.com/questions/24617350/how-to-create-a-custom-form-in-moodle