function save_question_options($question) { global $DB; $result = new stdClass(); if (!($oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'))) { $oldanswers = array(); } $answers = array(); $maxfraction = -1; // Insert all the new answers foreach ($question->answer as $key => $dataanswer) { // Check for, and ingore, completely blank answer from the form. if (trim($dataanswer) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key])) { continue; } if ($oldanswer = array_shift($oldanswers)) { // Existing answer, so reuse it $answer = $oldanswer; $answer->answer = trim($dataanswer); $answer->fraction = $question->fraction[$key]; $answer->feedback = $question->feedback[$key]; $DB->update_record("question_answers", $answer); } else { // This is a completely new answer $answer = new stdClass(); $answer->answer = trim($dataanswer); $answer->question = $question->id; $answer->fraction = $question->fraction[$key]; $answer->feedback = $question->feedback[$key]; $answer->id = $DB->insert_record("question_answers", $answer); } $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; } } $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 if (!empty($oldanswers)) { foreach ($oldanswers as $oa) { $DB->delete_records('question_answers', array('id' => $oa->id)); } } /// Perform sanity checks on fractional grades if ($maxfraction != 1) { $maxfraction = $maxfraction * 100; $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction); return $result; } else { return true; } }
function save_question_options($question) { $result = new stdClass(); $conditions = array(); $conditions_uncleaned = explode("\r\n", $question->conditions); foreach ($conditions_uncleaned as $condition_uncleaned) { if (strlen(trim($condition_uncleaned)) > 0) { $conditions[] = trim($condition_uncleaned); } } $question->conditions = implode("\r\n", $conditions); $parentresult = parent::save_question_options($question); if ($parentresult !== null) { // Parent function returns null if all is OK return $parentresult; } }
/** * Saves question-type specific options * * This is called by {@link save_question()} to save the question-type specific data from a * submitted form. This method takes the form data and formats into the correct format for * writing to the database. It then calls the parent method to actually write the data. * * @param object $question This holds the information from the editing form, * it is not a standard question object. * @return object $result->error or $result->noticeyesno or $result->notice */ function save_question_options($question) { // Start a try block to catch any exceptions generated when we attempt to parse and // then add the answers and variables to the database try { // Loop over all the answers in the question form and parse them to generate // a parser string. This ensures a constant formatting is stored in the database foreach ($question->answer as &$answer) { $expr = $this->parse_expression($answer); $answer = $expr->sage(); } // Now we need to write out all the answers to the question to the database $answers = $this->save_question_answers($question); $question->answers = implode(',', $answers); // The next task is to write out all the variables associated with the question $variables = $this->save_question_variables($question); $question->variables = implode(',', $variables); } catch (Exception $e) { // Error when adding answers orvariables to the database so create a result class // and put the error string in the error member funtion and then return the class // This keeps us compatible with the existing save_question_options methods. $result = new stdClass(); $result->error = $e->getMessage(); return $result; } // Process the allowed functions field. This code just sets up the variable, it is saved // in the parent class' save_question_options method called at the end of this method // Look for the 'all' option. If we find it then set the string to an empty value if (array_key_exists('all', $question->allowedfuncs)) { $question->allowedfuncs = ''; } else { // Create a comma separated string of the function names which are stored in the // keys of the array $question->allowedfuncs = implode(',', array_keys($question->allowedfuncs)); } // Call the parent method to write the extensions fields to the database. This either returns null // or an error object so if we get anything then return it otherwise return our existing if ($res = parent::save_question_options($question)) { return $res; } else { return true; } }
function save_question_options($question) { global $DB; $result = new stdClass(); $context = $question->context; $oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'); // Insert all the new answers $answers = array(); $maxfraction = -1; foreach ($question->answer as $key => $answerdata) { // Check for, and ignore, completely blank answer from the form. if (trim($answerdata) == '' && $question->fraction[$key] == 0 && html_is_blank($question->feedback[$key]['text'])) { continue; } // Update an existing answer if possible. $answer = array_shift($oldanswers); if (!$answer) { $answer = new stdClass(); $answer->question = $question->id; $answer->answer = ''; $answer->feedback = ''; $answer->id = $DB->insert_record('question_answers', $answer); } $answer->answer = trim($answerdata); $answer->fraction = $question->fraction[$key]; $answer->feedback = $this->import_or_save_files($question->feedback[$key], $context, 'question', 'answerfeedback', $answer->id); $answer->feedbackformat = $question->feedback[$key]['format']; $DB->update_record('question_answers', $answer); $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; } } // Delete any left over old answer records. $fs = get_file_storage(); foreach ($oldanswers as $oldanswer) { $fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id); $DB->delete_records('question_answers', array('id' => $oldanswer->id)); } $question->answers = implode(',', $answers); $parentresult = parent::save_question_options($question); if ($parentresult !== null) { // Parent function returns null if all is OK return $parentresult; } // Perform sanity checks on fractional grades if ($maxfraction != 1) { $result->noticeyesno = get_string('fractionsnomax', 'quiz', $maxfraction * 100); return $result; } return true; }