public function save_hints($formdata, $withparts = false) { global $DB; $context = $formdata->context; $oldhints = $DB->get_records('question_hints', array('questionid' => $formdata->id), 'id ASC'); $numhints = $this->count_hints_on_form($formdata, $withparts); for ($i = 0; $i < $numhints; $i += 1) { if (html_is_blank($formdata->hint[$i]['text'])) { $formdata->hint[$i]['text'] = ''; } if ($withparts) { $clearwrong = !empty($formdata->hintclearwrong[$i]); $shownumcorrect = !empty($formdata->hintshownumcorrect[$i]); } if ($this->is_hint_empty($formdata, $i, $withparts)) { continue; } // Update an existing hint if possible. $hint = array_shift($oldhints); if (!$hint) { $hint = new stdClass(); $hint->questionid = $formdata->id; $hint->hint = ''; $hint->id = $DB->insert_record('question_hints', $hint); } $hint->hint = $this->import_or_save_files($formdata->hint[$i], $context, 'question', 'hint', $hint->id); $hint->hintformat = $formdata->hint[$i]['format']; if ($withparts) { $hint->clearwrong = $clearwrong; $hint->shownumcorrect = $shownumcorrect; } $hint->options = $this->save_hint_options($formdata, $i, $withparts); $DB->update_record('question_hints', $hint); } // Delete any remaining old hints. $fs = get_file_storage(); foreach ($oldhints as $oldhint) { $fs->delete_area_files($context->id, 'question', 'hint', $oldhint->id); $DB->delete_records('question_hints', array('id' => $oldhint->id)); } } /** * Count number of hints on the form. * Overload if you use custom hint controls. */ 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. */ protected function is_hint_empty($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. */ protected function save_hint_options($formdata, $number, $withparts) { return null;//By default, options field is unused. }