Skip to content

Commit

Permalink
fixes LMDL-168 based on original owner of file
Browse files Browse the repository at this point in the history
  • Loading branch information
nexterday committed Feb 22, 2017
1 parent dc4064e commit 5f56083
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
86 changes: 59 additions & 27 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @subpackage fileresponse
* @copyright 2012 Luca Bösch [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -181,24 +182,19 @@ public function formulation_and_controls(question_attempt $qa, question_display_
return $result;
}

/**
* Displays any attached files when the question is in read-only mode.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should
* not be displayed. Used to get the context.
*/
public function files_read_only(question_attempt $qa, question_display_options $options,
$question) {
public function get_uploader_name($userid, $question) {
global $DB, $COURSE;
$files = $qa->get_last_qt_files('attachments', $options->context->id);
$step = $qa->get_last_step_with_qt_var('answer');
$output = array();
$returnfiles = array();
$zipper = new zip_packer();
$student_name_qid_str = '_';
$examination_user = $DB->get_record('user', array('id' => $step->get_user_id()
$student_name_qid_arr = array();
$examination_user = $DB->get_record('user', array('id' => $userid
));
if (!$examination_user) {
$student_name_qid_arr[0] = 'uknown';
$student_name_qid_arr[1] = 'uknown';
$student_name_qid_arr[2] = 'uknown';
$student_name_qid_arr[3] = 0;
$student_name_qid_arr[4] = 'uknown';
return $student_name_qid_arr;
}
// Remove anything which isn't a word, whitespace, number
// or any of the following caracters -_~,;[]().
// If you don't need to handle multi-byte characters
Expand All @@ -212,13 +208,46 @@ public function files_read_only(question_attempt $qa, question_display_options $
// Remove any runs of periods
$cleanedlname = mb_ereg_replace("([\.]{2,})", '', $cleanedlname);

$student_name_qid_str .= $cleanedfname . '_' . $cleanedlname . '_' . $question->id;
$student_name_qid_arr[0] = '_' . $cleanedfname . '_' . $cleanedlname . '_' . $question->id;
$student_name_qid_arr[1] = $examination_user->firstname;
$student_name_qid_arr[2] = $examination_user->lastname;
$student_name_qid_arr[3] = $examination_user->id;
$student_name_qid_arr[4] = $examination_user->email;
return $student_name_qid_arr;
}

$skipfile = 'All_Files' . $student_name_qid_str . '.zip';
/**
* Displays any attached files when the question is in read-only mode.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should
* not be displayed. Used to get the context.
*/
public function files_read_only(question_attempt $qa, question_display_options $options,
$question) {
global $DB, $COURSE;
$files = $qa->get_last_qt_files('attachments', $options->context->id);
$step = $qa->get_last_step_with_qt_var('answer');
$output = array();
$returnfiles = array();
$zipper = new zip_packer();
$infocreated = 0;
foreach ($files as $file) {
// if (strtolower($file->get_filename()) == strtolower($skipfile)) {
if (strpos($file->get_filename(), 'All_Files_') !== false) { // Avoid Restore duplicates
$student_name_qid_arr = $this->get_uploader_name($file->get_userid(), $question);

$skipfile = 'All_Files' . $student_name_qid_arr[0] . '.zip';

if (strpos($file->get_filename(), 'All_Files') !== false) { // Avoid Restore duplicates
// Get old zipped file
$fs = get_file_storage();
$itemid = $file->get_itemid();
$skipzippedfile = $fs->get_file($options->context->id, 'question',
'response_attachments', $itemid, '/qtype_fileresponse_zipped/',
$file->get_filename());

if ($skipzippedfile) {
$skipzippedfile->delete();
}
continue;
}
$output[] = html_writer::tag('p',
Expand All @@ -232,10 +261,10 @@ public function files_read_only(question_attempt $qa, question_display_options $

if (!$infocreated) {
// Create info file.
$infocontent = get_string('user') . ': ' . $examination_user->firstname . ' ' .
$examination_user->lastname . " \r\n";
$infocontent .= get_string('email') . ': ' . $examination_user->email . ' (ID: ' .
$examination_user->id . ') ' . "\r\n";
$infocontent = get_string('user') . ': ' . $student_name_qid_arr[1] . ' ' .
$student_name_qid_arr[2] . " \r\n";
$infocontent .= get_string('email') . ': ' . $student_name_qid_arr[4] . ' (ID: ' .
$student_name_qid_arr[3] . ') ' . "\r\n";
$infocontent .= get_string('question') . ': ' . $question->name . ' (ID: ' .
$question->id . ') ' . "\r\n";
$infocontent .= get_string('course') . ': ' . $COURSE->fullname . ' (ID: ' .
Expand All @@ -258,18 +287,20 @@ public function files_read_only(question_attempt $qa, question_display_options $
// Get info file
$infotxtfile = $fs->get_file($options->context->id, 'question',
'response_attachments', $itemid, '/', '_user_info.txt');

$infocreated = 1;
}
}
if (count($returnfiles) > 0) {

$final_zipped_file = $zipper->archive_to_storage($returnfiles, $options->context->id,
'question', 'response_attachments', $itemid, '/qtype_fileresponse_zipped/',
$skipfile, $step->get_user_id());
$skipfile, $file->get_userid());
// Delete info text if it exists
if ($infotxtfile) {
$infotxtfile->delete();
}

// Add Zipped to all links
$output[] = html_writer::tag('hr', '');
$output[] = html_writer::tag('p',
Expand Down Expand Up @@ -600,8 +631,9 @@ protected function get_filepicker_options($context, $draftitemid) {
return array(
'image' => $this->specific_filepicker_options(array('image'
), $draftitemid, $context),
'media' => $this->specific_filepicker_options(array('video', 'audio'
), $draftitemid, $context),
'media' => $this->specific_filepicker_options(
array('video', 'audio'
), $draftitemid, $context),
'link' => $this->specific_filepicker_options('*', $draftitemid, $context)
);
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'qtype_fileresponse';
$plugin->version = 2016112800;
$plugin->version = 2017022200;
$plugin->requires = 2015050500;
$plugin->maturity = MATURITY_BETA;

0 comments on commit 5f56083

Please sign in to comment.