Development:Questiontype creation to delete
Материал из База знаний Центра ПУСК МФТИ
Шаблон:Convert question bank Шаблон:Questiontype class
first draw Pierre Pichet 10:45, 17 March 2007 (CDT)
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");
$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);
}
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){
print_heading_with_help(get_string("editing".$question->qtype, "quiz"), $question->qtype, "quiz");
$mform->display();
}
function save_question($question, $form, $course)
/**
* 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; }
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;
}