* This is the base class for Moodle question types.
*
* There are detailed comments on each method, explaining what the method is
* for, and the circumstances under which you might need to override it.
*
* Note: the questiontype API should NOT be considered stable yet. Very few
* question tyeps have been produced yet, so we do not yet know all the places
* where the current API is insufficient. I would rather learn from the
* experiences of the first few question type implementors, and improve the
* interface to meet their needs, rather the freeze the API prematurely and
* condem everyone to working round a clunky interface for ever afterwards.
*/
class default_questiontype {
function name()
/**
* Name of the question type
*
* The name returned should coincide with the name of the directory
* in which this questiontype is located
*
* @return string the name of this question type.
*/
function name() {
return 'default';
}
function menu_name
/**
* The name this question should appear as in the create new question
* dropdown.
*
* @return mixed the desired string, or false to hide this question type in the menu.
*/
function menu_name() {
$name = $this->name();
...
return $menu_name;
}
function is_manual_graded()
/**
* @return boolean true if this question can only be graded manually.
*/
function is_manual_graded() {
return false;
}
function is_usable_by_random()
/**
* @return boolean true if this question type can be used by the random question type.
*/
function is_usable_by_random() {
return true;
}
function create_editing_form($submiturl, $question)
/**
* Return an instance of the question editing form definition. This looks for a
* class called edit_{$this->name()}_question_form in the file
* {$CFG->docroot}/question/type/{$this->name()}/edit_{$this->name()}_question_form.php
* and if it exists returns an instance of it.
*
* @param string $submiturl passed on to the constructor call.
* @return object an instance of the form definition, or null if one could not be found.
*/
function create_editing_form($submiturl, $question) {
global $CFG;
require_once("{$CFG->dirroot}/question/type/edit_question_form.php");
.....
return new $classname($submiturl, $question);
}
function display_question_editing_page(&$mform, $question, $wizardnow)
/**
* This method should be overriden if you want to include a special heading or some other
* html on a question editing page besides the question editing form.
*
* @param question_edit_form $mform a child of question_edit_form
* @param object $question
* @param string $wizardnow is for first page.
*/
function display_question_editing_page(&$mform, $question, $wizardnow){
/**
* Saves or updates a question after editing by a teacher
*
* Given some question info and some data about the answers
* this function parses, organises and saves the question
* It is used by {@link question.php} when saving new data from
* a form, and also by {@link import.php} when importing questions
* This function in turn calls {@link save_question_options}
* to save question-type specific options
* @param object $question the question object which should be updated
* @param object $form the form submitted by the teacher
* @param object $course the course we are in
* @return object On success, return the new question object. On failure,
* return an object as follows. If the error object has an errors field,
* display that as an error message. Otherwise, the editing form will be
* redisplayed with validation errors, from validation_errors field, which
* is itself an object, shown next to the form fields.
*/
function save_question($question, $form, $course) {
// This default implementation is suitable for most
....
return $question;
}
function save_question_options($question)
/**
* Saves question-type specific options
*
* This is called by {@link save_question()} to save the question-type specific data
* @return object $result->error or $result->noticeyesno or $result->notice
* @param object $question This holds the information from the editing form,
* it is not a standard question object.
*/
function save_question_options($question) {
return null;
}
function replace_question_in_attempts($oldquestionid, $newquestion, $attemtps)
/**
* Changes all states for the given attempts over to a new question
*
* This is used by the versioning code if the teacher requests that a question
* gets replaced by the new version. In order for the attempts to be regraded
* properly all data in the states referring to the old question need to be
* changed to refer to the new version instead. In particular for question types
* that use the answers table the answers belonging to the old question have to
* be changed to those belonging to the new version.
*
* @param integer $oldquestionid The id of the old question
* @param object $newquestion The new question
* @param array $attempts An array of all attempt objects in whose states
* replacement should take place
*/
function replace_question_in_attempts($oldquestionid, $newquestion, $attemtps) {
echo 'Not yet implemented';
return;
}
function get_question_options(&$question)
/**
* Loads the question type specific options for the question.
*
* This function loads any question type specific options for the
* question from the database into the question object. This information
* is placed in the $question->options field. A question type is
* free, however, to decide on a internal structure of the options field.
* @return bool Indicates success or failure.
* @param object $question The question object for the question. This object
* should be updated to include the question type
* specific information (it is passed by reference).
*/
function get_question_options(&$question) {
if (!isset($question->options)) {
$question->options = new object;
}
// The default implementation attaches all answers for this question
$question->options->answers = get_records('question_answers', 'question', $question->id, 'id ASC');
return true;
}
function delete_states($stateslist)
/**
* Deletes states from the question-type specific tables
*
* @param string $stateslist Comma separated list of state ids to be deleted
*/
function delete_states($stateslist) {
/// The default question type does not have any tables of its own
// therefore there is nothing to delete
return true;
}
function delete_question($questionid)
/**
* Deletes a question from the question-type specific tables
*
* @return boolean Success/Failure
* @param object $question The question being deleted
*/
function delete_question($questionid) {
/// The default question type does not have any tables of its own
// therefore there is nothing to delete
return true;
}
function actual_number_of_questions($question)
/**
* Returns the number of question numbers which are used by the question
*
* This function returns the number of question numbers to be assigned
* to the question. Most question types will have length one; they will be
* assigned one number. The 'description' type, however does not use up a
* number and so has a length of zero. Other question types may wish to
* handle a bundle of questions and hence return a number greater than one.
* @return integer The number of question numbers which should be
* assigned to the question.
* @param object $question The question whose length is to be determined.
* Question type specific information is included.
*/
function actual_number_of_questions($question) {
// By default, each question is given one number
return 1;
}
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt)
/**
* Creates empty session and response information for the question
*
* This function is called to start a question session. Empty question type
* specific session data (if any) and empty response data will be added to the
* state object. Session data is any data which must persist throughout the
* attempt possibly with updates as the user interacts with the
* question. This function does NOT create new entries in the database for
* the session; a call to the {@link save_session_and_responses} member will
* occur to do this.
* @return bool Indicates success or failure.
* @param object $question The question for which the session is to be
* created. Question type specific information is
* included.
* @param object $state The state to create the session for. Note that
* this will not have been saved in the database so
* there will be no id. This object will be updated
* to include the question type specific information
* (it is passed by reference). In particular, empty
* responses will be created in the ->responses
* field.
* @param object $cmoptions
* @param object $attempt The attempt for which the session is to be
* started. Questions may wish to initialize the
* session in different ways depending on the user id
* or time available for the attempt.
*/
function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
// The default implementation should work for the legacy question types.
// Most question types with only a single form field for the student's response
// will use the empty string as the index for that one response. This will
// automatically be stored in and restored from the answer field in the
// question_states table.
$state->responses = array(
=> ,
);
return true;
}
/// BACKUP FUNCTIONS ////////////////////////////
/*
* Backup the data in the question
*
* This is used in question/backuplib.php
*/
function backup($bf,$preferences,$question,$level=6) {
// The default type has nothing to back up
return true;
}
/// RESTORE FUNCTIONS /////////////////
/*
* Restores the data in the question
*
* This is used in question/restorelib.php
*/
function restore($old_question_id,$new_question_id,$info,$restore) {
// The default question type has nothing to restore
return true;
}
function restore_map($old_question_id,$new_question_id,$info,$restore) {
// There is nothing to decode
return true;
}
function restore_recode_answer($state, $restore) {
// There is nothing to decode
return $state->answer;
}
//This function restores the question_rqp_states
function restore_state($state_id,$info,$restore) {
// The default question type does not keep its own state information
return true;
}