libdir . '/questionlib.php');
/**
* 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 {
/**
* 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';
}
/**
* 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();
$menu_name = get_string($name, 'qtype_' . $name);
if ($menu_name[0] == '[') {
// Legacy behavior, if the string was not in the proper qtype_name
// language file, look it up in the quiz one.
$menu_name = get_string($this->name(), 'quiz');
}
return $menu_name;
}
/**
* @return boolean true if this question can only be graded manually.
*/
function is_manual_graded() {
return false;
}
/**
* @return boolean true if this question type can be used by the random question type.
*/
function is_usable_by_random() {
return true;
}
/**
* 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");
$definition_file = $CFG->dirroot.'/question/type/'.$this->name().'/edit_'.$this->name().'_form.php';
if (!(is_readable($definition_file) && is_file($definition_file))) {
return null;
}
require_once($definition_file);
$classname = 'question_edit_'.$this->name().'_form';
if (!class_exists($classname)) {
return null;
}
return new $classname($submiturl, $question);
}
/**
* 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){
print_heading_with_help(get_string("editing".$question->qtype, "quiz"), $question->qtype, "quiz");
$mform->display();
}
/**
*
*
* @param $question
*/
function set_default_options(&$question) {
}
/**
* 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
// question types.
// First, save the basic question itself
$question->name = trim($form->name);
$question->questiontext = trim($form->questiontext);
$question->questiontextformat = $form->questiontextformat;
$question->parent = isset($form->parent)? $form->parent : 0;
$question->length = $this->actual_number_of_questions($question);
$question->penalty = isset($form->penalty) ? $form->penalty : 0;
if (empty($form->image)) {
$question->image = "";
} else {
$question->image = $form->image;
}
if (empty($form->generalfeedback)) {
$question->generalfeedback = '';
} else {
$question->generalfeedback = trim($form->generalfeedback);
}
if (empty($question->name)) {
$question->name = substr(strip_tags($question->questiontext), 0, 15);
if (empty($question->name)) {
$question->name = '-';
}
}
if ($question->penalty > 1 or $question->penalty < 0) {
$question->errors['penalty'] = get_string('invalidpenalty', 'quiz');
}
if (isset($form->defaultgrade)) {
$question->defaultgrade = $form->defaultgrade;
}
if (!empty($question->id)) { // Question already exists
// keep existing unique stamp code
$question->stamp = get_field('question', 'stamp', 'id', $question->id);
if (!update_record("question", $question)) {
error("Could not update question!");
}
} else { // Question is a new one
// Set the unique code
$question->stamp = make_unique_id_code();
if (!$question->id = insert_record("question", $question)) {
error("Could not insert new question!");
}
}
// Now to save all the answers and type-specific options
$form->id = $question->id;
$form->qtype = $question->qtype;
$form->category = $question->category;
$form->questiontext = $question->questiontext;
$result = $this->save_question_options($form);
if (!empty($result->error)) {
error($result->error);
}
if (!empty($result->notice)) {
notice($result->notice, "question.php?id=$question->id");
}
if (!empty($result->noticeyesno)) {
notice_yesno($result->noticeyesno, "question.php?id=$question->id&courseid={$course->id}",
"edit.php?courseid={$course->id}");
print_footer($course);
exit;
}
// Give the question a unique version stamp determined by question_hash()
if (!set_field('question', 'version', question_hash($question), 'id', $question->id)) {
error('Could not update question version field');
}
return $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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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.
* @return bool Indicates success or failure.
* @param object $question The question object for the question including any
* question type specific information.
* @param object $state The saved state to load the session for. This
* object should be updated to include the question
* type specific session information and responses
* (it is passed by reference).
*/
function restore_session_and_responses(&$question, &$state) {
// The default implementation does nothing (successfully)
return true;
}
/**
* 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 and save
* the responses in their own database tables.
* @return bool Indicates success or failure.
* @param object $question The question object for the question including
* the question type specific information.
* @param object $state The state for which the question type specific
* data and responses should be saved.
*/
function save_session_and_responses(&$question, &$state) {
// The default implementation does nothing (successfully)
return true;
}
/**
* 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.
* @return mixed A response array giving the responses corresponding
* to the (or a) correct answer to the question. If there is
* no correct answer that scores 100% then null is returned.
* @param object $question The question for which the correct answer is to
* be retrieved. Question type specific information is
* available.
* @param object $state The state of the question, for which a correct answer is
* needed. Question type specific information is included.
*/
function get_correct_responses(&$question, &$state) {
/* The default implementation returns the response for the first answer
that gives full marks. */
if ($question->options->answers) {
foreach ($question->options->answers as $answer) {
if (((int) $answer->fraction) === 1) {
return array('' => $answer->answer);
}
}
}
return null;
}
/**
* 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
* @return object A mixed object
* ->id question id. Needed to manage random questions:
* it's the id of the actual question presented to user in a given attempt
* ->responses An array of values giving the responses corresponding
* to all answers to the question. Answer ids are used as keys.
* The text and partial credit are the object components
* @param object $question The question for which the answers are to
* be retrieved. Question type specific information is
* available.
*/
// ULPGC ecastro
function get_all_responses(&$question, &$state) {
if (isset($question->options->answers) && is_array($question->options->answers)) {
$answers = array();
foreach ($question->options->answers as $aid=>$answer) {
$r = new stdClass;
$r->answer = $answer->answer;
$r->credit = $answer->fraction;
$answers[$aid] = $r;
}
$result = new stdClass;
$result->id = $question->id;
$result->responses = $answers;
return $result;
} else {
return null;
}
}
/**
* Return the actual response to the question in a given state
* for the question
*
* @return mixed An array containing the response or reponses (multiple answer, match)
* given by the user in a particular attempt.
* @param object $question The question for which the correct answer is to
* be retrieved. Question type specific information is
* available.
* @param object $state The state object that corresponds to the question,
* for which a correct answer is needed. Question
* type specific information is included.
*/
// ULPGC ecastro
function get_actual_response($question, $state) {
// change length to truncate responses here if you want
$lmax = 40;
if (!empty($state->responses)) {
$responses[] = (strlen($state->responses['']) > $lmax) ?
substr($state->responses[''], 0, $lmax).'...' : $state->responses[''];
} else {
$responses[] = '';
}
return $responses;
}
// ULPGC ecastro
function get_fractional_grade(&$question, &$state) {
$maxgrade = $question->maxgrade;
$grade = $state->grade;
if ($maxgrade) {
return (float)($grade/$maxgrade);
} else {
return (float)$grade;
}
}
/**
* Checks if the response given is correct and returns the id
*
* @return int The ide number for the stored answer that matches the response
* given by the user in a particular attempt.
* @param object $question The question for which the correct answer is to
* be retrieved. Question type specific information is
* available.
* @param object $state The state object that corresponds to the question,
* for which a correct answer is needed. Question
* type specific information is included.
*/
// ULPGC ecastro
function check_response(&$question, &$state){
return false;
}
/**
* 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
* specific information is included. The
* maximum possible grade is in ->maxgrade. The name
* prefix for any named elements is in ->name_prefix.
* @param object $state The state to render the question in. The grading
* information is in ->grade, ->raw_grade and
* ->penalty. The current responses are in
* ->responses. This is an associative array (or the
* empty string or null in the case of no responses
* submitted). The last graded state is in
* ->last_graded (hence the most recently graded
* responses are in ->last_graded->responses). The
* question type specific information is also
* included.
* @param integer $number The number for this question.
* @param object $cmoptions
* @param object $options An object describing the rendering options.
*/
function print_question(&$question, &$state, $number, $cmoptions, $options) {
/* The default implementation should work for most question types
provided the member functions it calls are overridden where required.
The layout is determined by the template question.html */
global $CFG;
$isgraded = question_state_is_graded($state->last_graded);
// If this question is being shown in the context of a quiz
// get the context so we can determine whether some extra links
// should be shown. (Don't show these links during question preview.)
$cm = get_coursemodule_from_instance('quiz', $cmoptions->id);
if (!empty($cm->id)) {
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
} else if (!empty($cm->course)) {
$context = get_context_instance(CONTEXT_COURSE, $cm->course);
} else {
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
}
// For editing teachers print a link to an editing popup window
$editlink = '';
if ($context && has_capability('moodle/question:manage', $context)) {
$stredit = get_string('edit');
$linktext = '
';
$editlink = link_to_popup_window('/question/question.php?inpopup=1&id='.$question->id, 'editquestion', $linktext, 450, 550, $stredit, '', true);
}
$generalfeedback = '';
if ($isgraded && $options->generalfeedback) {
$generalfeedback = $this->format_text($question->generalfeedback,
$question->questiontextformat, $cmoptions);
}
$grade = '';
if ($question->maxgrade and $options->scores) {
if ($cmoptions->optionflags & QUESTION_ADAPTIVE) {
$grade = !$isgraded ? '--/' : round($state->last_graded->grade, $cmoptions->decimalpoints).'/';
}
$grade .= $question->maxgrade;
}
$comment = stripslashes($state->manualcomment);
$commentlink = '';
if (isset($options->questioncommentlink) && $context && has_capability('mod/quiz:grade', $context)) {
$strcomment = get_string('commentorgrade', 'quiz');
$commentlink = '
| $strquizname | \n"; echo "$strdoreplace | \n"; echo "$straffectedstudents | \n"; echo "
|---|---|---|
| ".format_string($quiz->name)." | \n"; echo "id}replace\" type=\"checkbox\" ".$checked." /> | \n"; echo "".(($studentcount) ? $studentcount.' '.$strstudents : '-')." | \n"; echo "