From cd3557e64c3fe830b7384ce1a093e8a7ae1ac28b Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 16 Mar 2011 16:28:48 +0000 Subject: [PATCH] MDL-20636 Essay question type, make is_same_response consider files. #216 --- question/engine/datalib.php | 37 ++++++++++++++++++++++++++++++-- question/engine/lib.php | 9 -------- question/type/essay/question.php | 4 +++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/question/engine/datalib.php b/question/engine/datalib.php index b4fe3a63989..2a7e526f925 100644 --- a/question/engine/datalib.php +++ b/question/engine/datalib.php @@ -954,6 +954,12 @@ class question_file_saver { /** @var string the file area name. */ protected $filearea; + /** + * @var string the value to store in the question_attempt_step_data to + * represent these files. + */ + protected $value = null; + /** * Constuctor. * @param int $draftitemid the draft area to save the files from. @@ -966,9 +972,36 @@ class question_file_saver { $this->filearea = $filearea; } + protected function get_value() { + global $USER; + + if (!is_null($this->value)) { + return $this->value; + } + + $fs = get_file_storage(); + $usercontext = get_context_instance(CONTEXT_USER, $USER->id); + + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', + $this->draftitemid, 'sortorder, filepath, filename', false); + + $string = ''; + foreach ($files as $file) { + $string .= $file->get_filepath() . $file->get_filename() . '|' . + $file->get_contenthash() . '|'; + } + + if ($string) { + $this->value = md5($string); + } else { + $this->value = ''; + } + + return $this->value; + } + public function __toString() { - // When stored in the database, we want this value to appear as 1. - return '1'; + return $this->get_value(); } /** diff --git a/question/engine/lib.php b/question/engine/lib.php index c1c8048e29f..8dbc13a340f 100644 --- a/question/engine/lib.php +++ b/question/engine/lib.php @@ -2086,8 +2086,6 @@ class question_attempt { * behaves as if there were no files. */ protected function process_response_files($name, $postdata = null) { - global $USER; - if ($postdata) { // There can be no files with test data (at the moment). return null; @@ -2098,13 +2096,6 @@ class question_attempt { return null; } - $fs = get_file_storage(); - $usercontext = get_context_instance(CONTEXT_USER, $USER->id); - - if ($fs->is_area_empty($usercontext->id, 'user', 'draft', $draftitemid)) { - return null; - } - return new question_file_saver($draftitemid, 'question', 'response_' . str_replace($this->get_field_prefix(), '', $name)); } diff --git a/question/type/essay/question.php b/question/type/essay/question.php index 176aec91acd..436cc1d9738 100644 --- a/question/type/essay/question.php +++ b/question/type/essay/question.php @@ -82,7 +82,9 @@ class qtype_essay_question extends question_with_responses { public function is_same_response(array $prevresponse, array $newresponse) { return question_utils::arrays_same_at_key_missing_is_blank( - $prevresponse, $newresponse, 'answer'); + $prevresponse, $newresponse, 'answer') && ($this->attachments == 0 || + question_utils::arrays_same_at_key_missing_is_blank( + $prevresponse, $newresponse, 'attachments')); } public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {