shortanswer qtype: MDL-17706 use extra_question_fields mechanism to save on code. Changes thanks to Oleg Sychev.
This commit is contained in:
@@ -103,6 +103,14 @@ class default_questiontype {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* If you use extra_question_fields, overload this function to return question id field name
|
||||
* in case you table use another name for this column
|
||||
*/
|
||||
function questionid_column_name() {
|
||||
return 'questionid';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
@@ -365,11 +373,12 @@ class default_questiontype {
|
||||
$question_extension_table = array_shift($extra_question_fields);
|
||||
|
||||
$function = 'update_record';
|
||||
$options = get_record($question_extension_table, 'questionid', $question->id);
|
||||
$questionidcolname = $this->questionid_column_name();
|
||||
$options = get_record($question_extension_table, $questionidcolname, $question->id);
|
||||
if (!$options) {
|
||||
$function = 'insert_record';
|
||||
$options = new stdClass;
|
||||
$options->questionid = $question->id;
|
||||
$options->$questionidcolname = $question->id;
|
||||
}
|
||||
foreach ($extra_question_fields as $field) {
|
||||
if (!isset($question->$field)) {
|
||||
@@ -437,7 +446,7 @@ class default_questiontype {
|
||||
$extra_question_fields = $this->extra_question_fields();
|
||||
if (is_array($extra_question_fields)) {
|
||||
$question_extension_table = array_shift($extra_question_fields);
|
||||
$extra_data = get_record($question_extension_table, 'questionid', $question->id, '', '', '', '', implode(', ', $extra_question_fields));
|
||||
$extra_data = get_record($question_extension_table, $this->questionid_column_name(), $question->id, '', '', '', '', implode(', ', $extra_question_fields));
|
||||
if ($extra_data) {
|
||||
foreach ($extra_question_fields as $field) {
|
||||
$question->options->$field = $extra_data->$field;
|
||||
@@ -494,7 +503,8 @@ class default_questiontype {
|
||||
$extra_question_fields = $this->extra_question_fields();
|
||||
if (is_array($extra_question_fields)) {
|
||||
$question_extension_table = array_shift($extra_question_fields);
|
||||
$success = $success && delete_records($question_extension_table, 'questionid', $questionid);
|
||||
$success = $success && delete_records($question_extension_table,
|
||||
$this->questionid_column_name(), $questionid);
|
||||
}
|
||||
|
||||
$extra_answer_fields = $this->extra_answer_fields();
|
||||
|
||||
@@ -22,20 +22,12 @@ class question_shortanswer_qtype extends default_questiontype {
|
||||
return 'shortanswer';
|
||||
}
|
||||
|
||||
function get_question_options(&$question) {
|
||||
// Get additional information from database
|
||||
// and attach it to the question object
|
||||
if (!$question->options = get_record('question_shortanswer', 'question', $question->id)) {
|
||||
notify('Error: Missing question options!');
|
||||
return false;
|
||||
}
|
||||
function extra_question_fields() {
|
||||
return array('question_shortanswer','answers','usecase');
|
||||
}
|
||||
|
||||
if (!$question->options->answers = get_records('question_answers', 'question',
|
||||
$question->id, 'id ASC')) {
|
||||
notify('Error: Missing question answers for shortanswer question ' . $question->id . '!');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
function questionid_column_name() {
|
||||
return 'question';
|
||||
}
|
||||
|
||||
function save_question_options($question) {
|
||||
@@ -82,22 +74,10 @@ class question_shortanswer_qtype extends default_questiontype {
|
||||
}
|
||||
}
|
||||
|
||||
if ($options = get_record("question_shortanswer", "question", $question->id)) {
|
||||
$options->answers = implode(",",$answers);
|
||||
$options->usecase = $question->usecase;
|
||||
if (!update_record("question_shortanswer", $options)) {
|
||||
$result->error = "Could not update quiz shortanswer options! (id=$options->id)";
|
||||
return $result;
|
||||
}
|
||||
} else {
|
||||
unset($options);
|
||||
$options->question = $question->id;
|
||||
$options->answers = implode(",",$answers);
|
||||
$options->usecase = $question->usecase;
|
||||
if (!insert_record("question_shortanswer", $options)) {
|
||||
$result->error = "Could not insert quiz shortanswer options!";
|
||||
return $result;
|
||||
}
|
||||
$question->answers = implode(',', $answers);
|
||||
$parentresult = parent::save_question_options($question);
|
||||
if($parentresult !== null) { // Parent function returns null if all is OK
|
||||
return $parentresult;
|
||||
}
|
||||
|
||||
// delete old answer records
|
||||
@@ -117,17 +97,6 @@ class question_shortanswer_qtype extends default_questiontype {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes question from the question-type specific tables
|
||||
*
|
||||
* @return boolean Success/Failure
|
||||
* @param object $question The question being deleted
|
||||
*/
|
||||
function delete_question($questionid) {
|
||||
delete_records("question_shortanswer", "question", $questionid);
|
||||
return true;
|
||||
}
|
||||
|
||||
function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
|
||||
global $CFG;
|
||||
/// This implementation is also used by question type 'numerical'
|
||||
|
||||
Reference in New Issue
Block a user