Skip to content

Commit

Permalink
MDL-39261 mod_lesson: Move essay answer files to new file area
Browse files Browse the repository at this point in the history
* Moved answers to a different file area
* Added backup and restore calls
* Include privacy/reset related calls similar to essay responses
  • Loading branch information
Peter committed Apr 11, 2019
1 parent 676eca7 commit a4606b7
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions mod/lesson/backup/moodle2/backup_lesson_stepslib.php
Expand Up @@ -197,6 +197,7 @@ protected function define_structure() {
$answer->annotate_files('mod_lesson', 'page_answers', 'id');
$answer->annotate_files('mod_lesson', 'page_responses', 'id');
$attempt->annotate_files('mod_lesson', 'essay_responses', 'id');
$attempt->annotate_files('mod_lesson', 'essay_answers', 'id');

// Prepare and return the structure we have just created for the lesson module.
return $this->prepare_activity_structure($lesson);
Expand Down
1 change: 1 addition & 0 deletions mod/lesson/backup/moodle2/restore_lesson_stepslib.php
Expand Up @@ -253,6 +253,7 @@ protected function after_execute() {
$this->add_related_files('mod_lesson', 'page_answers', 'lesson_answer');
$this->add_related_files('mod_lesson', 'page_responses', 'lesson_answer');
$this->add_related_files('mod_lesson', 'essay_responses', 'lesson_attempt');
$this->add_related_files('mod_lesson', 'essay_answers', 'lesson_attempt');

// Remap all the restored prevpageid and nextpageid now that we have all the pages and their mappings
$rs = $DB->get_recordset('lesson_pages', array('lessonid' => $this->task->get_activityid()),
Expand Down
20 changes: 19 additions & 1 deletion mod/lesson/classes/privacy/provider.php
Expand Up @@ -445,6 +445,7 @@ public static function delete_data_for_all_users_in_context(context $context) {

$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses');
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers');
}

/**
Expand Down Expand Up @@ -485,6 +486,7 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
$cmid = $lessonidstocmids[$record->lessonid];
$context = context_module::instance($cmid);
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses', $record->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers', $record->id);
}
$recordset->close();

Expand Down Expand Up @@ -522,6 +524,7 @@ public static function delete_data_for_users(approved_userlist $userlist) {
$recordset = $DB->get_recordset_select('lesson_attempts', $sql, $params, '', 'id, lessonid');
foreach ($recordset as $record) {
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses', $record->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers', $record->id);
}
$recordset->close();

Expand Down Expand Up @@ -616,7 +619,21 @@ protected static function transform_attempt(stdClass $data, context_module $cont
if ($data->page_qtype == LESSON_PAGE_ESSAY) {
// Essay questions serialise data in the answer field.
$info = \lesson_page_type_essay::extract_useranswer($answer);
$answer = format_text($info->answer, $info->answerformat, $options);
$answerfilespath = [get_string('privacy:path:essayanswers', 'mod_lesson'), $data->attempt_id];
$answer = format_text(
writer::with_context($context)->rewrite_pluginfile_urls(
$answerfilespath,
'mod_lesson',
'essay_answers',
$data->attempt_id,
$info->answer
),
$info->answerformat,
$options
);
writer::with_context($context)->export_area_files($answerfilespath, 'mod_lesson',
'essay_answers', $data->page_id);

if ($info->response !== null) {
// We export the files in a subfolder to avoid conflicting files, and tell the user
// where those files were exported. That is because we are not using a subfolder for
Expand All @@ -636,6 +653,7 @@ protected static function transform_attempt(stdClass $data, context_module $cont
);
writer::with_context($context)->export_area_files($responsefilespath, 'mod_lesson',
'essay_responses', $data->page_id);

}

} else if ($data->page_qtype == LESSON_PAGE_MULTICHOICE && $data->page_qoption) {
Expand Down
4 changes: 2 additions & 2 deletions mod/lesson/essay.php
Expand Up @@ -248,7 +248,7 @@
$currentpage = $lesson->load_page($attempt->pageid);
$a->question = format_text($currentpage->contents, $currentpage->contentsformat, $formattextdefoptions);
$answer = file_rewrite_pluginfile_urls($essayinfo->answer, 'pluginfile.php', $context->id,
'mod_lesson', 'essay_responses', $attempt->id);
'mod_lesson', 'essay_answers', $attempt->id);
$a->response = format_text($answer, $essayinfo->answerformat,
array('context' => $context, 'para' => true));
$a->comment = $essayinfo->response;
Expand Down Expand Up @@ -457,7 +457,7 @@
// Expects the following to be set: $attemptid, $answer, $user, $page, $attempt
$essayinfo = lesson_page_type_essay::extract_useranswer($attempt->useranswer);
$answer = file_rewrite_pluginfile_urls($essayinfo->answer, 'pluginfile.php', $context->id,
'mod_lesson', 'essay_responses', $attempt->id);
'mod_lesson', 'essay_answers', $attempt->id);
$currentpage = $lesson->load_page($attempt->pageid);

$mform = new essay_grading_form(null, array('scoreoptions'=>$scoreoptions, 'user'=>$user));
Expand Down
1 change: 1 addition & 0 deletions mod/lesson/lang/en/lesson.php
Expand Up @@ -474,6 +474,7 @@
$string['privacy:metadata:overrides'] = 'A record of overrides per lesson';
$string['privacy:metadata:userpref:lessonview'] = 'The preferred display mode when editing lessons';
$string['privacy:path:essayresponses'] = 'Essay responses';
$string['privacy:path:essayanswers'] = 'Essay answers';
$string['privacy:path:pages'] = 'Pages';
$string['processerror'] = 'Error occurred during processing!';
$string['progressbar'] = 'Progress bar';
Expand Down
4 changes: 3 additions & 1 deletion mod/lesson/lib.php
Expand Up @@ -1088,6 +1088,7 @@ function lesson_reset_userdata($data) {
}
$context = context_module::instance($cm->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses');
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers');
}
}

Expand Down Expand Up @@ -1366,7 +1367,7 @@ function lesson_pluginfile($course, $cm, $context, $filearea, $args, $forcedownl
}
$fullpath = "/$context->id/mod_lesson/$filearea/$itemid/".implode('/', $args);

} else if ($filearea === 'essay_responses') {
} else if ($filearea === 'essay_responses' || $filearea === 'essay_answers') {
$itemid = (int)array_shift($args);
if (!$attempt = $DB->get_record('lesson_attempts', array('id' => $itemid))) {
return false;
Expand Down Expand Up @@ -1408,6 +1409,7 @@ function lesson_get_file_areas() {
$areas['page_answers'] = get_string('pageanswers', 'mod_lesson');
$areas['page_responses'] = get_string('pageresponses', 'mod_lesson');
$areas['essay_responses'] = get_string('essayresponses', 'mod_lesson');
$areas['essay_answers'] = get_string('essayresponses', 'mod_lesson');
return $areas;
}

Expand Down
1 change: 1 addition & 0 deletions mod/lesson/locallib.php
Expand Up @@ -3923,6 +3923,7 @@ final public function delete() {
if ($attempts = $DB->get_records('lesson_attempts', array("pageid" => $this->properties->id))) {
foreach ($attempts as $attempt) {
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses', $attempt->id);
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_answers', $attempt->id);
}
}

Expand Down
10 changes: 5 additions & 5 deletions mod/lesson/pagetypes/essay.php
Expand Up @@ -91,7 +91,7 @@ public function display($renderer, $attempt) {
}

$data = file_prepare_standard_editor($data, 'answer', $options['editoroptions'],
$context, 'mod_lesson', 'essay_responses');
$context, 'mod_lesson', 'essay_answers');
$mform->set_data($data);

// Trigger an event question viewed.
Expand Down Expand Up @@ -147,7 +147,7 @@ public function on_after_write_attempt($attempt, $result) {
);

$formdata = file_postupdate_standard_editor($formdata, 'answer', $editoroptions,
$editoroptions['context'], 'mod_lesson', 'essay_responses', $attempt->id);
$editoroptions['context'], 'mod_lesson', 'essay_answers', $attempt->id);

// Update the student response to have the modified link.
$useranswer = unserialize($attempt->useranswer);
Expand All @@ -174,7 +174,7 @@ public function on_after_write_attempt($attempt, $result) {
*/
public function format_answer($answer, $context, $answerformat, $options = []) {
$answer = file_rewrite_pluginfile_urls($answer, 'pluginfile.php', $context->id,
'mod_lesson', 'essay_responses', $options->attemptid);
'mod_lesson', 'essay_answers', $options->attemptid);
return parent::format_answer($answer, $context, $answerformat, $options);
}

Expand Down Expand Up @@ -303,7 +303,7 @@ public function report_answers($answerpage, $answerdata, $useranswer, $pagestats
if ($useranswer != null) {
$essayinfo = self::extract_useranswer($useranswer->useranswer);
$essayinfo->answer = file_rewrite_pluginfile_urls($essayinfo->answer, 'pluginfile.php',
$context->id, 'mod_lesson', 'essay_responses', $useranswer->id);
$context->id, 'mod_lesson', 'essay_answers', $useranswer->id);

if ($essayinfo->response == null) {
$answerdata->response = get_string("nocommentyet", "lesson");
Expand Down Expand Up @@ -426,7 +426,7 @@ public function definition() {
$mform->setType('answer', PARAM_RAW);
$mform->addElement('html', $OUTPUT->container(get_string('youranswer', 'lesson'), 'youranswer'));
$useranswer = file_rewrite_pluginfile_urls($useranswer, 'pluginfile.php', $editoroptions['context']->id,
'mod_lesson', 'essay_responses', $this->_customdata['attemptid']);
'mod_lesson', 'essay_answers', $this->_customdata['attemptid']);
$mform->addElement('html', $OUTPUT->container($useranswer, 'reviewessay'));
$this->add_action_buttons(null, get_string("nextpage", "lesson"));
} else {
Expand Down

0 comments on commit a4606b7

Please sign in to comment.