From 8a1e7b7756db53d2b49a1ff202c2089d15229ca6 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 30 Jul 2012 19:59:48 +0100 Subject: [PATCH] MDL-34640 quesion reponse files: PARTIAL SOLUTION to allowing resonse files to be graded automatically. --- question/engine/datalib.php | 84 ++++++++++++++++++++++++- question/engine/questionattemptstep.php | 12 ++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/question/engine/datalib.php b/question/engine/datalib.php index b58c05e0374..c742312c2ce 100644 --- a/question/engine/datalib.php +++ b/question/engine/datalib.php @@ -1199,6 +1199,21 @@ class question_engine_unit_of_work implements question_usage_observer { } +/** + * The interface implemented by {@link question_file_saver} and {@link question_file_loader}. + * + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +interface question_response_files { + /** + * Get the files that were submitted. + * @return array of stored_files objects. + */ + public function get_files(); +} + + /** * This class represents the promise to save some files from a particular draft * file area into a particular file area. It is used beause the necessary @@ -1210,7 +1225,7 @@ class question_engine_unit_of_work implements question_usage_observer { * @copyright 2011 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class question_file_saver { +class question_file_saver implements question_response_files { /** @var int the id of the draft file area to save files from. */ protected $draftitemid; /** @var string the owning component name. */ @@ -1290,6 +1305,73 @@ class question_file_saver { file_save_draft_area_files($this->draftitemid, $context->id, $this->component, $this->filearea, $itemid); } + + /** + * Get the files that were submitted. + * @return array of stored_files objects. + */ + public function get_files() { + global $USER; + + $fs = get_file_storage(); + $usercontext = context_user::instance($USER->id); + + return $fs->get_area_files($usercontext->id, 'user', 'draft', + $this->draftitemid, 'sortorder, filepath, filename', false); + } +} + + +/** + * This class is the mirror image of {@link question_file_saver}. It allows + * files to be accessed again later (e.g. when re-grading) using that same + * API as when doing the original grading. + * + * @copyright 2012 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class question_file_loader implements question_response_files { + /** @var question_attempt_step the step that these files belong to. */ + protected $step; + + /** @var string the field name for these files - which is used to construct the file area name. */ + protected $name; + + /** + * @var string the value to stored in the question_attempt_step_data to + * represent these files. + */ + protected $value; + + /** @var int the context id that the files belong to. */ + protected $contextid; + + /** + * Constuctor. + * @param question_attempt_step $step the step that these files belong to. + * @param string $name string the field name for these files - which is used to construct the file area name. + * @param string $value the value to stored in the question_attempt_step_data to + * represent these files. + * @param int $contextid the context id that the files belong to. + */ + public function __construct(question_attempt_step $step, $name, $value, $contextid) { + $this->draftitemid = $draftitemid; + $this->component = $component; + $this->filearea = $filearea; + $this->value = $this->compute_value($draftitemid, $text); + } + + public function __toString() { + return $this->value; + } + + /** + * Get the files that were submitted. + * @return array of stored_files objects. + */ + public function get_files() { + return $this->step->get_qt_files($this->name, $this->contextid); + } } diff --git a/question/engine/questionattemptstep.php b/question/engine/questionattemptstep.php index 5cbd5c8a65c..6145f4158bb 100644 --- a/question/engine/questionattemptstep.php +++ b/question/engine/questionattemptstep.php @@ -403,6 +403,18 @@ class question_attempt_step { if (!is_null($record->fraction)) { $step->fraction = $record->fraction + 0; } + + // This next chunk of code requires getting $contextid and $qtype here. + // Somehow, we need to get that information to this point by modifying + // all the paths by which this method can be called. + foreach (question_bank::get_qtype($qtype)->response_file_areas() as $area) { + if (empty($step->data[$area])) { + continue; + } + + $step->data[$area] = new question_file_loader($this, $area, $step->data[$area], $contextid) + } + return $step; } }