[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/question/type/ -> questiontype.php (summary)

The default questiontype class.

Author: Martin Dougiamas and many others. This has recently been completely
License: http://www.gnu.org/copyleft/gpl.html GNU Public License
File Size: 1613 lines (74 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

default_questiontype:: (51 methods):
  name()
  menu_name()
  is_manual_graded()
  is_usable_by_random()
  has_html_answers()
  extra_question_fields()
  extra_answer_fields()
  create_editing_form()
  plugin_dir()
  plugin_baseurl()
  display_question_editing_page()
  get_heading()
  set_default_options()
  save_question()
  save_question_options()
  replace_question_in_attempts()
  get_question_options()
  delete_states()
  delete_question()
  actual_number_of_questions()
  create_session_and_responses()
  restore_session_and_responses()
  save_session_and_responses()
  get_correct_responses()
  get_all_responses()
  get_actual_response()
  get_fractional_grade()
  check_response()
  get_html_head_contributions()
  print_question()
  history()
  print_question_grading_details()
  print_question_formulation_and_controls()
  print_question_submit_buttons()
  response_summary()
  get_texsource()
  compare_responses()
  test_response()
  grade_responses()
  get_config_options()
  finished_edit_wizard()
  print_replacement_options()
  format_text()
  find_file_links()
  replace_file_links()
  error_link()
  backup()
  restore()
  restore_map()
  restore_recode_answer()
  generate_test()


Class: default_questiontype  - X-Ref

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.

name()   X-Ref
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.

menu_name()   X-Ref
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.

is_manual_graded()   X-Ref

return: boolean true if this question can only be graded manually.

is_usable_by_random()   X-Ref

return: boolean true if this question type can be used by the random question type.

has_html_answers()   X-Ref

return: whether the question_answers.answer field needs to have

extra_question_fields()   X-Ref
If your question type has a table that extends the question table, and
you want the base class to automatically save, backup and restore the extra fields,
override this method to return an array wherer the first element is the table name,
and the subsequent entries are the column names (apart from id and questionid).

return: mixed array as above, or null to tell the base class to do nothing.

extra_answer_fields()   X-Ref
If your question type has a table that extends the question_answers table,
make this method return an array wherer the first element is the table name,
and the subsequent entries are the column names (apart from id and answerid).

return: mixed array as above, or null to tell the base class to do nothing.

create_editing_form($submiturl, $question, $category, $contexts, $formeditable)   X-Ref
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.

plugin_dir()   X-Ref

return: string the full path of the folder this plugin's files live in.

plugin_baseurl()   X-Ref

return: string the URL of the folder this plugin's files live in.

display_question_editing_page(&$mform, $question, $wizardnow)   X-Ref
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.

get_heading($adding = false)   X-Ref
Method called by display_question_editing_page and by question.php to get heading for breadcrumbs.

return: array a string heading and the langmodule in which it was found.

set_default_options(&$question)   X-Ref

param: $question

save_question($question, $form, $course)   X-Ref
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,

save_question_options($question)   X-Ref
Saves question-type specific options

This is called by {@link save_question()} to save the question-type specific data
param: object $question  This holds the information from the editing form,
return: object $result->error or $result->noticeyesno or $result->notice

replace_question_in_attempts($oldquestionid, $newquestion, $attemtps)   X-Ref
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

get_question_options(&$question)   X-Ref
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.
param: object $question The question object for the question. This object
return: bool            Indicates success or failure.

delete_states($stateslist)   X-Ref
Deletes states from the question-type specific tables

param: string $stateslist  Comma separated list of state ids to be deleted

delete_question($questionid)   X-Ref
Deletes a question from the question-type specific tables

param: object $question  The question being deleted
return: boolean Success/Failure

actual_number_of_questions($question)   X-Ref
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.
param: object $question The question whose length is to be determined.
return: integer         The number of question numbers which should be

create_session_and_responses(&$question, &$state, $cmoptions, $attempt)   X-Ref
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.
param: object $question The question for which the session is to be
param: object $state    The state to create the session for. Note that
param: object $cmoptions
param: object $attempt  The attempt for which the session is to be
return: bool            Indicates success or failure.

restore_session_and_responses(&$question, &$state)   X-Ref
Restores the session data and most recent responses for the given state

This function loads any session data associated with the question
session in the given state from the database into the state object.
In particular it loads the responses that have been saved for the given
state into the ->responses member of the state object.

Question types with only a single form field for the student's response
will not need not restore the responses; the value of the answer
field in the question_states table is restored to ->responses['']
before this function is called. Question types with more response fields
should override this method and set the ->responses field to an
associative array of responses.
param: object $question The question object for the question including any
param: object $state    The saved state to load the session for. This
return: bool            Indicates success or failure.

save_session_and_responses(&$question, &$state)   X-Ref
Saves the session data and responses for the given question and state

This function saves the question type specific session data from the
state object to the database. In particular for most question types it saves the
responses from the ->responses member of the state object. The question type
non-specific data for the state has already been saved in the question_states
table and the state object contains the corresponding id and
sequence number which may be used to index a question type specific table.

Question types with only a single form field for the student's response
which is contained in ->responses[''] will not have to save this response,
it will already have been saved to the answer field of the question_states table.
Question types with more response fields should override this method to convert
the data the ->responses array into a single string field, and save it in the
database. The implementation in the multichoice question type is a good model to follow.
http://cvs.moodle.org/contrib/plugins/question/type/opaque/questiontype.php?view=markup
has a solution that is probably quite generally applicable.
param: object $question The question object for the question including
param: object $state    The state for which the question type specific
return: bool            Indicates success or failure.

get_correct_responses(&$question, &$state)   X-Ref
Returns an array of values which will give full marks if graded as
the $state->responses field

The correct answer to the question in the given state, or an example of
a correct answer if there are many, is returned. This is used by some question
types in the {@link grade_responses()} function but it is also used by the
question preview screen to fill in correct responses.
param: object $question The question for which the correct answer is to
param: object $state    The state of the question, for which a correct answer is
return: mixed           A response array giving the responses corresponding

get_all_responses(&$question, &$state)   X-Ref
Return an array of values with the texts for all possible responses stored
for the question

All answers are found and their text values isolated
param: object $question The question for which the answers are to
return: object          A mixed object

get_actual_response($question, $state)   X-Ref
Return the actual response to the question in a given state
for the question.

param: object $question The question for which the correct answer is to
param: object $state    The state object that corresponds to the question,
return: mixed           An array containing the response or reponses (multiple answer, match)

get_fractional_grade(&$question, &$state)   X-Ref
No description

check_response(&$question, &$state)   X-Ref
Checks if the response given is correct and returns the id

param: object $question The question for which the correct answer is to
param: object $state    The state object that corresponds to the question,
return: int             The ide number for the stored answer that matches the response

get_html_head_contributions(&$question, &$state)   X-Ref
If this question type requires extra CSS or JavaScript to function,
then this method will return an array of <link ...> tags that reference
those stylesheets. This function will also call require_js()
from ajaxlib.php, to get any necessary JavaScript linked in too.

The two parameters match the first two parameters of print_question.

param: object $question The question object.
param: object $state    The state object.
return: an array of bits of HTML to add to the head of pages where

print_question(&$question, &$state, $number, $cmoptions, $options)   X-Ref
Prints the question including the number, grading details, content,
feedback and interactions

This function prints the question including the question number,
grading details, content for the question, any feedback for the previously
submitted responses and the interactions. The default implementation calls
various other methods to print each of these parts and most question types
will just override those methods.
param: object $question The question to be rendered. Question type
param: object $state    The state to render the question in. The grading
param: integer $number  The number for this question.
param: object $cmoptions
param: object $options  An object describing the rendering options.

history($question, $state, $number, $cmoptions, $options)   X-Ref
No description

print_question_grading_details(&$question, &$state, $cmoptions, $options)   X-Ref
Prints the score obtained and maximum score available plus any penalty
information

This function prints a summary of the scoring in the most recently
graded state (the question may not have been submitted for marking at
the current state). The default implementation should be suitable for most
question types.
param: object $question The question for which the grading details are
param: object $state    The state. In particular the grading information
param: object $cmoptions
param: object $options  An object describing the rendering options.

print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options)   X-Ref
Prints the main content of the question including any interactions

This function prints the main content of the question including the
interactions for the question in the state given. The last graded responses
are printed or indicated and the current responses are selected or filled in.
Any names (eg. for any form elements) are prefixed with $question->name_prefix.
This method is called from the print_question method.
param: object $question The question to be rendered. Question type
param: object $state    The state to render the question in. The grading
param: object $cmoptions
param: object $options  An object describing the rendering options.

print_question_submit_buttons(&$question, &$state, $cmoptions, $options)   X-Ref
Prints the submit button(s) for the question in the given state

This function prints the submit button(s) for the question in the
given state. The name of any button created will be prefixed with the
unique prefix for the question in $question->name_prefix. The suffix
'submit' is reserved for the single question submit button and the suffix
'validate' is reserved for the single question validate button (for
question types which support it). Other suffixes will result in a response
of that name in $state->responses which the printing and grading methods
can then use.
param: object $question The question for which the submit button(s) are to
param: object $state    The state to render the buttons for. The
param: object $cmoptions
param: object $options  An object describing the rendering options.

response_summary($question, $state, $length=80)   X-Ref
Return a summary of the student response

This function returns a short string of no more than a given length that
summarizes the student's response in the given $state. This is used for
example in the response history table. This string should already be,
for output.
param: object $question
param: object $state   The state whose responses are to be summarized
param: int $length     The maximum length of the returned string
return: string         The summary of the student response

get_texsource(&$question, &$state, $cmoptions, $type)   X-Ref
Renders the question for printing and returns the LaTeX source produced

This function should render the question suitable for a printed problem
or solution sheet in LaTeX and return the rendered output.
param: object $question The question to be rendered. Question type
param: object $state    The state to render the question in. The
param: object $cmoptions
param: string $type     Indicates if the question or the solution is to be
return: string          The LaTeX output.

compare_responses(&$question, $state, $teststate)   X-Ref
Compares two question states for equivalence of the student's responses

The responses for the two states must be examined to see if they represent
equivalent answers to the question by the student. This method will be
invoked for each of the previous states of the question before grading
occurs. If the student is found to have already attempted the question
with equivalent responses then the attempt at the question is ignored;
grading does not occur and the state does not change. Thus they are not
penalized for this case.
param: object $question  The question for which the states are to be
param: object $state     The state of the question. The responses are in
param: object $teststate The state whose responses are to be
return: boolean

test_response(&$question, &$state, $answer)   X-Ref
Checks whether a response matches a given answer

This method only applies to questions that use teacher-defined answers

return: boolean

grade_responses(&$question, &$state, $cmoptions)   X-Ref
Performs response processing and grading

This function performs response processing and grading and updates
the state accordingly.
param: object $question The question to be graded. Question type
param: object $state    The state of the question to grade. The current
param: object $cmoptions
return: boolean         Indicates success or failure.

get_config_options()   X-Ref
Includes configuration settings for the question type on the quiz admin
page

TODO: It makes no sense any longer to do the admin for question types
from the quiz admin page. This should be changed.
Returns an array of objects describing the options for the question type
to be included on the quiz module admin page.
Configuration options can be included by setting the following fields in
the object:
->name           The name of the option within this question type.
The full option name will be constructed as
"quiz_{$this->name()}_$name", the human readable name
will be displayed with get_string($name, 'quiz').
->code           The code to display the form element, help button, etc.
i.e. the content for the central table cell. Be sure
to name the element "quiz_{$this->name()}_$name" and
set the value to $CFG->{"quiz_{$this->name()}_$name"}.
->help           Name of the string from the quiz module language file
to be used for the help message in the third column of
the table. An empty string (or the field not set)
means to leave the box empty.
Links to custom settings pages can be included by setting the following
fields in the object:
->name           The name of the link text string.
get_string($name, 'quiz') will be called.
->link           The filename part of the URL for the link. The full URL
is contructed as
"$CFG->wwwroot/question/type/{$this->name()}/$link?sesskey=$sesskey"
[but with the relavant calls to the s and rawurlencode
functions] where $sesskey is the sesskey for the user.
return: array    Array of objects describing the configuration options to

finished_edit_wizard(&$form)   X-Ref
Returns true if the editing wizard is finished, false otherwise.

The default implementation returns true, which is suitable for all question-
types that only use one editing form. This function is used in
question.php to decide whether we can regrade any states of the edited
question and redirect to edit.php.

The dataset dependent question-type, which is extended by the calculated
question-type, overwrites this method because it uses multiple pages (i.e.
a wizard) to set up the question and associated datasets.

param: object $form  The data submitted by the previous page.
return: boolean      Whether the wizard's last page was submitted or not.

print_replacement_options($question, $course, $cmid='0')   X-Ref
Prints a table of course modules in which the question is used

TODO: This should be made quiz-independent

This function is used near the end of the question edit forms in all question types
It prints the table of quizzes in which the question is used
containing checkboxes to allow the teacher to replace the old question version

param: object $question
param: object $course
param: integer $cmid optional The id of the course module currently being edited

format_text($text, $textformat, $cmoptions = NULL)   X-Ref
Call format_text from weblib.php with the options appropriate to question types.

param: string $text the text to format.
param: integer $text the type of text. Normally $question->questiontextformat.
param: object $cmoptions the context the string is being displayed in. Only $cmoptions->course is used.
return: string the formatted text.

find_file_links($question, $courseid)   X-Ref
No description

replace_file_links($question, $fromcourseid, $tocourseid, $url, $destination)   X-Ref
No description

error_link($cmoptions)   X-Ref

param: $cmoptions as passed in from outside.
return: the best link to pass to print_error.

backup($bf,$preferences,$question,$level=6)   X-Ref
No description

restore($old_question_id,$new_question_id,$info,$restore)   X-Ref
No description

restore_map($old_question_id,$new_question_id,$info,$restore)   X-Ref
No description

restore_recode_answer($state, $restore)   X-Ref
No description

generate_test($name, $courseid=null)   X-Ref
Abstract function implemented by each question type. It runs all the code
required to set up and save a question of any type for testing purposes.
Alternate DB table prefix may be used to facilitate data deletion.




Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7