Skip to content
Browse files

MDL-40013 Easier to override saving question hints

This adds 3 new methods, called from question_type::save_hints,
to make it easier for different question types to overload this
functionality without duplicating code.

Question types that will benefit from this change include
oumultiresponse, ddmarker, preg and correctwriting.
  • Loading branch information...
1 parent f36066f commit 14e5c999601f384c1ce5a168c0184d7372afc8cb @oasychev oasychev committed Jun 5, 2013
Showing with 62 additions and 20 deletions.
  1. +62 −20 question/type/questiontypebase.php
View
82 question/type/questiontypebase.php
@@ -473,25 +473,8 @@ public function save_hints($formdata, $withparts = false) {
$oldhints = $DB->get_records('question_hints',
array('questionid' => $formdata->id), 'id ASC');
- if (!empty($formdata->hint)) {
- $numhints = max(array_keys($formdata->hint)) + 1;
- } else {
- $numhints = 0;
- }
- if ($withparts) {
- if (!empty($formdata->hintclearwrong)) {
- $numclears = max(array_keys($formdata->hintclearwrong)) + 1;
- } else {
- $numclears = 0;
- }
- if (!empty($formdata->hintshownumcorrect)) {
- $numshows = max(array_keys($formdata->hintshownumcorrect)) + 1;
- } else {
- $numshows = 0;
- }
- $numhints = max($numhints, $numclears, $numshows);
- }
+ $numhints = $this->count_hints_on_form($formdata, $withparts);
for ($i = 0; $i < $numhints; $i += 1) {
if (html_is_blank($formdata->hint[$i]['text'])) {
@@ -503,8 +486,7 @@ public function save_hints($formdata, $withparts = false) {
$shownumcorrect = !empty($formdata->hintshownumcorrect[$i]);
}
- if (empty($formdata->hint[$i]['text']) && empty($clearwrong) &&
- empty($shownumcorrect)) {
+ if ($this->is_hint_empty_in_form_data($formdata, $i, $withparts)) {
continue;
}
@@ -524,6 +506,7 @@ public function save_hints($formdata, $withparts = false) {
$hint->clearwrong = $clearwrong;
$hint->shownumcorrect = $shownumcorrect;
}
+ $hint->options = $this->save_hint_options($formdata, $i, $withparts);
$DB->update_record('question_hints', $hint);
}
@@ -536,6 +519,65 @@ public function save_hints($formdata, $withparts = false) {
}
/**
+ * Count number of hints on the form.
+ * Overload if you use custom hint controls.
+ * @param object $formdata the data from the form.
+ * @param bool $withparts whether to take into account clearwrong and shownumcorrect options.
+ * @return int count of hints on the form.
+ */
+ protected function count_hints_on_form($formdata, $withparts) {
+ if (!empty($formdata->hint)) {
+ $numhints = max(array_keys($formdata->hint)) + 1;
+ } else {
+ $numhints = 0;
+ }
+
+ if ($withparts) {
+ if (!empty($formdata->hintclearwrong)) {
+ $numclears = max(array_keys($formdata->hintclearwrong)) + 1;
+ } else {
+ $numclears = 0;
+ }
+ if (!empty($formdata->hintshownumcorrect)) {
+ $numshows = max(array_keys($formdata->hintshownumcorrect)) + 1;
+ } else {
+ $numshows = 0;
+ }
+ $numhints = max($numhints, $numclears, $numshows);
+ }
+ return $numhints;
+ }
+
+ /**
+ * Determine if the hint with specified number is not empty and should be saved.
+ * Overload if you use custom hint controls.
+ * @param object $formdata the data from the form.
+ * @param int $number number of hint under question.
+ * @param bool $withparts whether to take into account clearwrong and shownumcorrect options.
+ * @return bool is this particular hint data empty.
+ */
+ protected function is_hint_empty_in_form_data($formdata, $number, $withparts) {
+ if ($withparts) {
+ return empty($formdata->hint[$number]['text']) && empty($formdata->hintclearwrong[$number]) &&
+ empty($formdata->hintshownumcorrect[$number]);
+ } else {
+ return empty($formdata->hint[$number]['text']);
+ }
+ }
+
+ /**
+ * Save additional question type data into the hint optional field.
+ * Overload if you use custom hint information.
+ * @param object $formdata the data from the form.
+ * @param int $number number of hint to get options from.
+ * @param bool $withparts whether question have parts.
+ * @return string value to save into the options field of question_hints table.
+ */
+ protected function save_hint_options($formdata, $number, $withparts) {
+ return null; // By default, options field is unused.
+ }
+
+ /**
* Can be used to {@link save_question_options()} to transfer the combined
* feedback fields from $formdata to $options.
* @param object $options the $question->options object being built.

0 comments on commit 14e5c99

Please sign in to comment.
Something went wrong with that request. Please try again.