## General developer forum

### Retrieve a parameter from mod_form and use it in view.php

Retrieve a parameter from mod_form and use it in view.php

Hi Community,

I'm new to moodle plugin development and on task to develop a plugin with certain functionalities. Breaking it down into small steps my first goal is to create a plugin that can show another Page based on the "select" made in the activation of this activity_plugin inside a course.

Therefore I created a select form in "mod_form.php" to select between 3 Options:

$simaids = Array( 'aid1' => 'Simulation 1', 'aid2' => 'Simulation 2', 'aid3' => 'Simulation 3',);$mform->addElement('select', 'simaids', 'Simulation', $simaids); So if one Option is selected there should be a value iE 'aid2', this value needs to be added to an URL that will be shown in "view.php" via: $simulation = file_get_contents('https://myurlexample.com/'.'$simurl');echo$simulation;
So depending on the option the URL differs.

The current challenge for me is to retrieve the parameter from "mod_form.php" and use it in "view.php".

I checked for a soultion and found many possible solutions which I hadn't had success using until now. These solutions are:

• require_once mod_form.php and stating the variable setting it to be equal to $simurl •$_Session - as I am not sure if it will work for a user who is just inside the course and clicking on the plugin
• POST and GET as far as I understand it works with adding parameters to the URL which should nit work as the POST will only be done once when activating the plugin?
• Saving the Form Select Input to some table which I can access from "view.php"(but I don't know if this requires to write a new function to create a table or if it's already stored somewhere

As you might have guessed I'm not only new to moodle but pretty much programming in php. Currently I just try to find another example to understand how to extract it, but most plugins seem too complex for me right now to get the gist where to look at like (mod_game_moodle).

I'm glad for any advice you mind sharing that will help me to achieve a better understanding of moodle plugin development.

Average of ratings: -
Re: Retrieve a parameter from mod_form and use it in view.php

Hi Jo We,

The first problem you're having is that mod_form is a special kind of form used by the activity modules API to create and edit an instance of the activity.  The process works a bit like this:

1. You select your module form the "Add an activity or resource" menu on a course page
2. Moodle finds the mod_form for that plugin and displays it
3. You enter your details into the form and click "Save and display" or "Save and return to course"
4. Moodle uses the "{modulename}_add_instance" function in the plugin's lib.php to process the form data and creates the new instance.
5. Moodle redirects you either to the course page, or the new module instance (depending on which button you selected).

Because you are redirected at the end of this process, the data submitted in the form isn't available to view.php.  You will probably need to save it in the database and retrieve it using the course module ID (the id parameter passed to view.php).

You can read all about how to create an activity module in the plugin development documentation.  Hopefully that will help make things clearer.

Average of ratings: -
Re: Retrieve a parameter from mod_form and use it in view.php

Hi Mark Johnson,

thanks for the answer. I try to use the DB created on Submiting the "mod_form.php" for the activity plugin. Now I'm trying to define the selected value from my simaids select form. Searching for ways to do this I found this thread an also this one, but the latter hadn't any answer to it. All answers I found so far are way to complex to realte to them.

Again I'm thinking in small steps:

The Plugin would be named "newplugin" so the tables name should be "newplugin".

1. Insert the selected simaids into a db

For this I created a new field  (simaids) in the plugins table using the xmdb editor. My Expectation would be that the simaids value selected in the create instance form would automatically end up in this field on pressing the Submit button.

If this is false some code is needed to define the selected key (i.E. aid01) and "insert" it into the created field called "simaids" in the "newplugin" table. Something like

$DB->insert_record("newplugin",$selectedsimaids, $returnid=true,$bulk=false)

2. Then I should be able to use

$DB->get_field("newplugin",$return, array $conditions,$strictness=IGNORE_MISSING)
but I'm not sure about the right use.

first param should be the tables name

second param what does the $return mean? third param is the array of filedname=>vieldvalue but i just need the value which should be aid02 or aid01... forth param$strictness is nothing I think needs to change

3. I want to show a specific file_content based on the value If aid01 was selected on the instance creation the displayed URL would be ending with ".../aid01" or if aid02 was selected ".../aid02".

For this I need to define the fieldvalue as a variable to use it.

Again I'm glad for any help especially to better understand the insert and get record Syntax and how the selected dropdown entry can be processed.

Average of ratings: -
Re: Retrieve a parameter from mod_form and use it in view.php

I'm not sure I fully understand all your questions, but I think I can offer some information.

The data submitted to the create instance form isn't inserted into your database table automatically.  You need to define the newplugin_add_instance() function in mod/newplugin/lib.php to do this.  Look at the lib.php files of any of the existing activity module plugins for examples.

You are correct that you'll need to use $DB->insert_record(). The first argument is the name of the table, and the second is an object containing all the fields (except the id) that you want to insert for the record. To retrieve the data, you usually use$DB->get_record() rather than $DB->get_field(), unless you specifically want a single value returned. The$return parameter of get_field() is the name of the field you want.  If you use get_record(), you will instead get an object containing all the fields from the record.

The $conditions parameter used in all$DB->get_* functions is an array of field/value pairs to match the records on.  So if you wanted the record where the id was 123, you would pass the array ['id' => 123].

Average of ratings: Useful (1)
Re: Retrieve a parameter from mod_form and use it in view.php

Thanks for the Help I missed to answer in time as I am finally able to understand the use of records at least the simple get, insert, delete and update ones. I got to the point where the whole thing is probably running after some last tests.

So again thanks Mark and this thread can be closed, from my side.

Average of ratings: -