From 89f09b24542c97ec96aeb82b30c565638fa13241 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 16 Dec 2011 15:51:39 +0000 Subject: [PATCH] MDL-30760 question engine: question summary can be longer than 64k! 1. So we will truncate the question summary to 65000 chars if necessary. 2. Also, fix one minor error in mutlianswer save_question_options. question_bank::MAX_SUMMARY_LENGTH is not the most logical class to add the constant to, but it needs to be accessible during upgrade, so I was lazy and put it there. --- question/engine/bank.php | 2 ++ question/engine/datalib.php | 5 +++++ question/engine/upgrade/upgradelib.php | 5 +++++ question/type/multianswer/questiontype.php | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/question/engine/bank.php b/question/engine/bank.php index 656531510df..f4404ebb946 100644 --- a/question/engine/bank.php +++ b/question/engine/bank.php @@ -42,6 +42,8 @@ require_once(dirname(__FILE__) . '/../type/questiontypebase.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ abstract class question_bank { + const MAX_SUMMARY_LENGTH = 65000; + /** @var array question type name => question_type subclass. */ private static $questiontypes = array(); diff --git a/question/engine/datalib.php b/question/engine/datalib.php index 2f41b12d7ff..9d94731f57a 100644 --- a/question/engine/datalib.php +++ b/question/engine/datalib.php @@ -89,6 +89,11 @@ class question_engine_data_mapper { $record->minfraction = $qa->get_min_fraction(); $record->flagged = $qa->is_flagged(); $record->questionsummary = $qa->get_question_summary(); + if (textlib::strlen($record->questionsummary) > question_bank::MAX_SUMMARY_LENGTH) { + // It seems some people write very long quesions! MDL-30760 + $record->questionsummary = textlib::substr($record->questionsummary, + 0, question_bank::MAX_SUMMARY_LENGTH - 3) . '...'; + } $record->rightanswer = $qa->get_right_answer_summary(); $record->responsesummary = $qa->get_response_summary(); $record->timemodified = time(); diff --git a/question/engine/upgrade/upgradelib.php b/question/engine/upgrade/upgradelib.php index 6f0aac88c08..7e682f0ed9c 100644 --- a/question/engine/upgrade/upgradelib.php +++ b/question/engine/upgrade/upgradelib.php @@ -245,6 +245,11 @@ class question_engine_attempt_upgrader { $qa = $qas[$questionid]; $qa->questionusageid = $attempt->uniqueid; $qa->slot = $i; + if (textlib::strlen($qa->questionsummary) > question_bank::MAX_SUMMARY_LENGTH) { + // It seems some people write very long quesions! MDL-30760 + $qa->questionsummary = textlib::substr($qa->questionsummary, + 0, question_bank::MAX_SUMMARY_LENGTH - 3) . '...'; + } $this->insert_record('question_attempts', $qa); $layout[$questionkeys[$questionid]] = $qa->slot; diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index e1748b439fc..2d746928d14 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -407,7 +407,7 @@ function qtype_multianswer_extract_question($text) { && preg_match('~'.NUMERICAL_ALTERNATIVE_REGEX.'~', $altregs[ANSWER_ALTERNATIVE_REGEX_ANSWER], $numregs)) { $wrapped->answer[] = $numregs[NUMERICAL_CORRECT_ANSWER]; - if ($numregs[NUMERICAL_ABS_ERROR_MARGIN]) { + if (array_key_exists(NUMERICAL_ABS_ERROR_MARGIN, $numregs)) { $wrapped->tolerance["$answerindex"] = $numregs[NUMERICAL_ABS_ERROR_MARGIN]; } else {