module settings - how to store them?

module settings - how to store them?

by Cristi A. -
Number of replies: 11
I've created a new module based on NEWMODULE structure.

I've added a text field, and I really don't know how can I save what it is entered there, and display on the users page for that module...

can I keep that information in the database? if so - how?


thanks,
Cristi
Average of ratings: -
In reply to Cristi A.

Re: module settings - how to store them?

by Francois COJEAN -
Are you talking about the instance module configuration page (mod_form.php)? or any other page of your module ?

instance module configuration page use formlib and modedit.php.

So just define your textarea name with the same name as in your database, and value will be save and reloaed correctly.
In reply to Francois COJEAN

Re: module settings - how to store them?

by Cristi A. -
thanks smile

solved that...

now I have another question:

how I do a form on the page that the users see it, with a text field that will show what is in a field [from the module's table] called "age" [let's say] and when the student changes that field, the field should update in the database...
In reply to Cristi A.

Re: module settings - how to store them?

by Francois COJEAN -
In reply to Francois COJEAN

Re: module settings - how to store them?

by Cristi A. -
I looked, I tried doing something, but the script can't pass this line:

$mform = new yourmod_formfunction_form();//name of the form you defined in file above.
instead of "yourmod_formfunction_form()" I put: mymodule_form()

where mymodule is the name of my module :|

and in mod_form.php the main class is:

class mod_mymodule_mod_form extends moodleform_mod {

function definition() {

global $COURSE;
$mform =& $this->_form;
..........



what is wrong here? mixed

In reply to Cristi A.

Re: module settings - how to store them?

by Cristi A. -
no one knows? sad
In reply to Cristi A.

Re: module settings - how to store them?

by Dan Poltawski -
Well, what is the error message you receive ensuring that the debugging level is set to Developer?
In reply to Dan Poltawski

Re: module settings - how to store them?

by Cristi A. -
I solved that error...


now - how can I make in view.php a form that initially takes data from a table [module's table] and when submitted sends the data to the database and updates the fields? :|
In reply to Cristi A.

Re: module settings - how to store them?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Often the quickest way to work thing out is to look at how other modules do things.
In reply to Cristi A.

Re: module settings - how to store them?

by Francois COJEAN -
ok there is two different things here.

mod_form.php is linked to mod_edit.php.
So you have very few control on this form.
The only control i get, is to add control with exact name as database field name in the table : PREFIX_MODULE_NAME.

Then i guess what you want to do is to make your own form that will react to your own script smile

I was in the same case as you few weeks before, don't worry there is a solution.

This is how i proceed :

1) i create two files :
- my_page.php
- my_page_form.php

2) In 'my_page_form.php' i define my form as explain in formslib doc :
[code]
require_once ($CFG->dirroot.'/course/moodleform_mod.php');

class mod_MODULE_NAME_MY_PAGE_form extends moodleform {
// form definition
function definition() {

global $COURSE;
$mform =& $this->_form;

//-------------------------------------------------------------------------------
$mform->addElement('header', 'header1', get_string('header1', 'MODULE_NAME'));
/* a text field */
$mform->addElement('text', 'textfield1', get_string('textfield1', 'MODULE_NAME'), array('size'=>'64'));
$mform->addRule('textfield1', get_string('required'), 'required', null, 'client');
//-------------------------------------------------------------------------------
// add standard buttons, common to all modules
$this->add_action_buttons();
}

// form validation
function validation($data) {
$errors= array();

return $errors;
}
}
[/code]

3) In 'my_page.php', define how the form must react with the database

[code]
require_once('./my_page_form.php');

//you'll process some page parameters at the top here and get the info about
//what instance of your module and what course you're in etc. Make sure you
//include hidden variable in your forms which have their defaults set in set_data
//which pass these variables from page to page

$mform = new mod_MODULE_NAME_MY_PAGE_form('./my_page.php', null, 'post');

if ($mform->is_cancelled())
{
//you need this section if you have a cancel button on your form
//here you tell php what to do if your user presses cancel
//probably a redirect is called for!

redirect('./view.php?id='.$cm->id, '',0);
}
else if ($fromform=$mform->get_data())
{
//this branch is where you process validated data.

/* Prepare dataobject to submit to the database */
$data_object->my_text = $fromform->textfield1;
/* Submit object to the database */
update_record(TABLE_NAME, $data_object);
/* Redirection */
redirect('LINK_TO_THE_PAGE_IT_SHOULD_GO, '',0);
}
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.
//put data you want to fill out in the form into array $toform here then :

if(isset($toform))
{
$mform->set_data($toform);
}
// give focus to the first form element
$mform->focus();

// display form
$mform->display();

}
[/code]
4) Adapt this method to your needs smile
In reply to Francois COJEAN

Re: module settings - how to store them?

by Cristi A. -
this is how I did it:


if ($form = data_submitted()) {
update_record('mYmodulename', $form);
}