Skip to content

Commit

Permalink
MDL-24408 extra_answer_fields: loading data to the question editing form
Browse files Browse the repository at this point in the history
Now question_edit_form can load extra answer fields. If a question
type need to do something more complex for particular field, it
can overload added functions without duplicating other code.
  • Loading branch information
oasychev committed Mar 27, 2014
1 parent 6276672 commit a68207b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions question/type/edit_question_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,68 @@ protected function data_preprocessing_answers($question, $withanswerfiles = fals
$question->feedback[$key]['format'] = $answer->feedbackformat;
$key++;
}

// Now process extra answer fields.
$extraanswerfields = question_bank::get_qtype($question->qtype)->extra_answer_fields();
if (is_array($extraanswerfields)) {
// Omit table name.
array_shift($extraanswerfields);
$question = $this->data_preprocessing_extra_answer_fields($question, $extraanswerfields);
}

return $question;
}

/**
* Perform the necessary preprocessing for the extra answer fields.
*
* Questions that do something not trivial when editing extra answer fields
* will want to override this.
* @param object $question the data being passed to the form.
* @param array $extraanswerfields extra answer fields (without table name).
* @return object $question the modified data.
*/
protected function data_preprocessing_extra_answer_fields($question, $extraanswerfields) {
// Setting $question->$field[$key] won't work in PHP, so we need set an array of answer values to $question->$field.
// As we may have several extra fields with data for several answers in each, we use an array of arrays.
// Index in $extrafieldsdata is an extra answer field name, value - array of it's data for each answer.
$extrafieldsdata = array();
// First, prepare an array if empty arrays for each extra answer fields data.
foreach ($extraanswerfields as $field) {
$extrafieldsdata[$field] = array();
}

// Fill arrays with data from $question->options->answers.
$key = 0;
foreach ($question->options->answers as $answer) {
foreach ($extraanswerfields as $field) {
// See hack comment in {@link data_preprocessing_answers()}.
unset($this->_form->_defaultValues["$field[$key]"]);
$extrafieldsdata[$field][$key] = $this->data_preprocessing_extra_answer_field($answer, $field);
}
$key++;
}

// Set this data in the $question object.
foreach ($extraanswerfields as $field) {
$question->$field = $extrafieldsdata[$field];
}
return $question;
}

/**
* Perfmorm preprocessing for particular extra answer field.
*
* Questions with non-trivial DB - form element relationship will
* want to override this.
* @param object $answer an answer object to get extra field from.
* @param string $field extra answer field name.
* @return field value to be set to the form.
*/
protected function data_preprocessing_extra_answer_field($answer, $field) {
return $answer->$field;
}

/**
* Perform the necessary preprocessing for the fields added by
* {@link add_combined_feedback_fields()}.
Expand Down

0 comments on commit a68207b

Please sign in to comment.