Development:Import questions

From MoodleDocs

(Redirected from Question import)
Jump to: navigation, search

Contents

Developper docs viewpoint

This page will contain informations specific to the import of question from external files process mostly with a code developper viewpoint.

Question general and optional elements

When importing a question of a given question type implemented on Moodle ( truefalse, numerical etc.) from an external file that contains a similar type but nor necessarily an identical to the Moodle type you need to know what are the elements that are needed by Moodle to correctly import the external question. You need to know their Moodle name and properties so to apply, if necessary, transformation to the the imported data or if they are not in the imported data, to supply acceptable default values. This information is actually not given at a unique site (file, docs) for almost all the actual question types implemented in Moodle. It can be deduced by the database items althoug indirectly and after a close examination of the question type code.

However there is a place where almost all the necessary information is present: the edit HTML page(s) used to create or modify a question of a given type.

The create and modify HTML page

The creation and edition of a question of any Moodle type is handled by the question/question.php file. This file process the general question information, save the question elements by a call to

$question = $QTYPES[$qtype]->save_question($question, $form, $course);

$question object contains some elements the most important is the $question->id whose presence and values are used to know if the question already exist or have been somewhat processed. Almost all the other necessary elements are contained in the $form object. This $form object is the result of the HTML page(s) that the user have filled with the necessary data to obtain a valid question on Moodle. If we know these elements, we have a good starting point to implement new import format.php files. We could use the browser to obtain the input source of the file and retrace those elements but there is a lot of other HTML tags that make it a not easy task.

A TOOL to extract the elements of any question type

I invite you to use the following simple tool to examine the elements on the HTML create and modify page of a question.

How it works

The question/question.php writes the HTML edit page heading and gives the control to the specific editquestion.php.

    require_once('type/'.$qtype.'/editquestion.php');

The $qtype/editquestion.php process the default or saved question data and print the HTML content using a file like editquestion.html. On return the question/question.php writes the necessary javascript code to implement the HTML Area editor if $usehtmleditor is true.

   if ($usehtmleditor ) {
       use_html_editor('questiontext');
   }
   print_footer($course);

This is where the TOOL code is inserted so that it will be on EVERY question type HTML creation or edition by the Moodle users

   print_simple_box_start('center');
   require_once('type/'.$qtype.'/editquestion.php');
   echo '<form><div align="center">
      <input type="button" onclick="return determineform();"  
           value="Form elements of a '.$qtype.' question" />
      </div></form>';
   print_simple_box_end();?>
   <script language="javascript" type="text/javascript">    
   function determineform() {
      text ="Description of the elements of a ;<?php echo $qtype; ?> question :document.theform\n";
   	    for(var i=0;i<document.theform.length;++i)
    {
     text += "Name: " + document.theform.elements[i].name;
     text +=", id : " + document.theform.elements[i].id;
     text +=", type : " + document.theform.elements[i].type;
     text +=", value : " + document.theform.elements[i].value+"\n" ;
     }
      alert(text);
     }
    </script>
    ;<?php      
   if ($usehtmleditor ) {
       use_html_editor('questiontext');
   }
   print_footer($course);

The TOOL results

A button at the end of the HTML page

Image:formelementbutton.jpg

A typical result

Image:formelementdescription.jpg

A text area has been added under the Form elements so that you can copy and paste the description of the question elements.

As this is in a different form that the document.theform, there is no interference with the question creation or edition processing.

As an example the result of a copy and paste:

Description of the elements of a numerical question :document.theform 
Name: category, id : , type : select-one, value : 1 
Name: name, id : , type : text, value : mon age 
Name: , id : , type : select-one, value : Trebuchet MS,Verdana,Arial,Helvetica,sans-serif 
Name: , id : , type : select-one, value : 1 
Name: , id : , type : select-one, value : 
Name: questiontext, id : edit-questiontext, type : textarea, value : Quel est mon âge? 
Name: questiontextformat, id : , type : hidden, value : 1 
Name: defaultgrade, id : , type : text, value : 1 
Name: penalty, id : , type : text, value : 0.1 
Name: answer[], id : correct0, type : text, value : 62 
Name: tolerance[], id : acceptederror0, type : text, value : 1 
Name: fraction[], id : , type : hidden, value : 1 
Name: feedback[], id : , type : textarea, value : 
Name: multiplier[], id : , type : hidden, value : 1.0 
Name: unit[], id : defaultunit, type : text, value : 
Name: multiplier[], id : , type : text, value : 
Name: unit[], id : , type : text, value : 
Name: multiplier[], id : , type : text, value : 
Name: unit[], id : , type : text, value : 
Name: multiplier[], id : , type : text, value : 
Name: unit[], id : , type : text, value : 
Name: multiplier[], id : , type : text, value : 
Name: unit[], id : , type : text, value : 
Name: multiplier[], id : , type : text, value : 
Name: unit[], id : , type : text, value : 
Name: , id : , type : submit, value : Save changes 
Name: makecopy, id : , type : submit, value : Save as new question 
Name: cancel, id : , type : submit, value : Cancel 
Name: sesskey, id : , type : hidden, value : JARvtwJinO 
Name: id, id : , type : hidden, value : 2 
Name: qtype, id : , type : hidden, value : numerical 
Name: versioning, id : , type : hidden, value : on

How to obtain results for other question types

A test site

If you look at the result of the first image, the moodle experimental site where this was implemented is shown.

So I invite you to log to

http://www.chimie.uqam.ca/moodle16/

under user login: moodle password: moodle

You will ge log as a teacher so handle it correctly, there is no backup and restore of the data.

You can create questions of the different types and experiment.

On your homesite

The question.php file used (1.6 +) is posted on the quiz forum. http://moodle.org/mod/forum/discuss.php?d=48227

Improvements

The code can be improved

  • by using the popup window javascript alreday on the HTML pages.
  • by getting more information from the different element (i.e select );

Document you trials and success stories in these docs pages

Use the specific link in the menu at the head of this page and edit the page. --Pierre Pichet 21:02, 25 June 2006 (WST)

Personal tools
Moodle » Documentation » English