How to ask parameters from user in import questions module

How to ask parameters from user in import questions module

by Joseph Rézeau -
Number of replies: 3
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

I am currently implementing a new export/import format module, to export all the questions of a question category and re-import them into a single cloze/embedded question. In my import process I would like to include an input screen to ask the teacher to select a few choices as to the final layout of the cloze question (line-breaks or running text; numbered sub-questions or not, etc.). This would take the form of a couple of boxes to be ticked (yes or no) with a submit button.

I do not know how to this, although I'm sure it is basic stuff for Moodle gurus. I would like to place my "input screen" in my \question\format\mynewformat\format.php file, somewhere at the beginning of  function readquestions($lines). What do I need to implement? How do I stop the flow of the program there, so that I can recuperate the user's choices and incorporate them into my parameters?

Any help appreciated, preferably with examples or references to other Moodle files, etc.

Joseph

Average of ratings: -
In reply to Joseph Rézeau

Re: How to ask parameters from user in import questions module

by Jan Dierckx -

You don't need to stop the flow of the program to wait for user input as would be the case in other programming languages. When the page is displayed to the user all processing has already stopped. All user input sent to the server will be processed by a completely new page. (This is what all the Ajax buzz is about: how can you send a new request to the server without asking for a completely new page, but let us leave that aside to keep things simple)

This is exactly what made it difficult for me to grasp PHP programming. I believe programmers refer to this as maintaining state (Look for it on Google especially in combination with PHP as a search term)
As a mere mortal I'd like to call it: How does one Moodle page knows which selections were made on the previous Moodle page?

Simple answer: they use parameters in the url or they send parameters through forms. (There is another way: sessions, but we will leave those aside for the moment.)

  1. Using url parameters is the easiest way: Write a lot of links for the different choices:

    <a href="./format.php?linebreaks=false&amp;numberedsubq=true">
       No linebreaks, Numbered subquestions</a> 
    <a href="./format.php?linebreaks=true&amp;numberedsubq=true">
       Linebreaks, Numbered subquestions</a> 
    etc...
    

    Alas, this is ugly! So we will go for the harder way...

  2. Passing parameters through forms. The best way to understand it, is to look at existing forms. You will quickly notice 2 things:

  • Moodle forms contain a lot of hidden fields. That's precisely to pass around information from one page to the other: it's likely that the question category would in your code be passed using a hidden field.

  • Moodle forms come in pairs: edit.php is accompanied by edit.html / post.php by post.html / etc...
    The html file contains the form and the php file has two parts: the first part processes the information that was submitted. (Look at the data_submitted() function). The second part is used to setup some defaults before displaying the html file with the form.

I hope carefully studying some examples of these *.html / *.php pairs will make it clearer.

Just one warning. Now that you know how Moodle passes around information from page to page, you'll understand that it is very easy for a user to enter other information (Just add a new parameter to the url or use the Firefox Web Developer Toolbar to change the value of a hidden field). That's why the receiving page should never trust information entered by the user. (eg in your case you might add a check whether the user is really allowed to export that particular question category). This information is often neglected in PHP tutorials or books. Some of the examples in those books or on the web are even dangerous to use on a real server connected to the internet.

Some other things to pay attention at when you grasp a PHP book or tutorial:

  • You can skip big grin the part about MySQL database connections. Moodle's database functions are way easier to use.

  • Do read about $_GET (the PHP way of passing around values through urls) and $_POST (passing around values through forms) work, but afterwards you can use the simpler Moodle functions for those: data_submitted() and optional_param() and required_param()

Hope this saves you some time. I know I spent quite a bit of time trying to grasp this, coming from a Commodore 64 Basic background, where one would simply write

    3060 INPUT "Numbered subquestions?", NUM
    3070 IF NUM = "Y" THEN GOTO 3120
    ... 
In reply to Joseph Rézeau

Re: How to ask parameters from user in import questions module

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
There is no provision in the import/export scripts for aditional user parameters. Perhaps there should be....

However, it would not be too difficult to add a new function into the class to display the screen you require returning the new value(s) as parameters. Read question simply needs to check for the existence of these parameters. If they have not been set, call the function to display the page, if they have check them (security!!) and use them.

The new page's form's action will need to recall the question/export.php with all it's existing parameters plus your new ones - this will ensure that it calls the readquestion method again (which can then proceed with the new data).

Does this make sense? It's easier to do it than to explain it smile

If you are stuck (and not in too much of a rush), I would be happy to rough out an example for you.
In reply to Howard Miller

Re: How to ask parameters from user in import questions module

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Jan and Howard,

Thanks for your help, I am beginning to see the light, or at least a gleam of hope.smile

I'll ponder what you have explained and might come back to you soon,

Joseph