Skip to content

Commit

Permalink
MDL-34640 quesion reponse files: PARTIAL SOLUTION to allowing resonse…
Browse files Browse the repository at this point in the history
… files to be graded automatically.
  • Loading branch information
timhunt committed Mar 30, 2013
1 parent 5957088 commit 8a1e7b7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
84 changes: 83 additions & 1 deletion question/engine/datalib.php
Expand Up @@ -1199,6 +1199,21 @@ public function save(question_engine_data_mapper $dm) {
} }




/**
* 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 * 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 * file area into a particular file area. It is used beause the necessary
Expand All @@ -1210,7 +1225,7 @@ public function save(question_engine_data_mapper $dm) {
* @copyright 2011 The Open University * @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @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. */ /** @var int the id of the draft file area to save files from. */
protected $draftitemid; protected $draftitemid;
/** @var string the owning component name. */ /** @var string the owning component name. */
Expand Down Expand Up @@ -1290,6 +1305,73 @@ public function save_files($itemid, $context) {
file_save_draft_area_files($this->draftitemid, $context->id, file_save_draft_area_files($this->draftitemid, $context->id,
$this->component, $this->filearea, $itemid); $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);
}
} }




Expand Down
12 changes: 12 additions & 0 deletions question/engine/questionattemptstep.php
Expand Up @@ -403,6 +403,18 @@ public static function load_from_records($records, $attemptstepid) {
if (!is_null($record->fraction)) { if (!is_null($record->fraction)) {
$step->fraction = $record->fraction + 0; $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; return $step;
} }
} }
Expand Down

0 comments on commit 8a1e7b7

Please sign in to comment.