Skip to content

Commit

Permalink
mod/assignment MDL-22609 Checking in Aparup's patch fixing the file u…
Browse files Browse the repository at this point in the history
…pgrade (itemid now is submission->id, not user->id). Also fixed some buggy uses of $OUTPUT->notification in uploadsingle->upload() (redirect works better)
  • Loading branch information
moodler committed Jul 1, 2010
1 parent 484617d commit d992f53
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
28 changes: 16 additions & 12 deletions mod/assignment/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,39 @@ function xmldb_assignment_upgrade($oldversion) {
$context = get_context_instance(CONTEXT_MODULE, $submission->cmid);

// migrate submitted files first
$path = $basepath;
$newpath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->id/";
if (!file_exists($newpath)) {
mkdir($newpath);
}
$filearea = 'assignment_submission';
$items = new DirectoryIterator($path);
$items = new DirectoryIterator($basepath);
foreach ($items as $item) {
if (!$item->isFile()) {
continue;
}
if (!$item->isReadable()) {
echo $OUTPUT->notification(" File not readable, skipping: ".$path.$item->getFilename());
echo $OUTPUT->notification(" File not readable, skipping: ".$basepath.$item->getFilename());
continue;
}
$filename = clean_param($item->getFilename(), PARAM_FILE);
if ($filename === '') {
continue;
}
if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
$file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
unlink($path.$item->getFilename());
$file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
if ($fs->create_file_from_pathname($file_record, $basepath.$item->getFilename())) {
unlink($basepath.$item->getFilename());
}
}
}
unset($items); //release file handles

// migrate teacher response files
$path = $basepath.'responses/';
if (file_exists($path)) {
$basepath = $basepath.'responses/';
$newpath = $newpath.'responses/';
if (file_exists($basepath)) {
$filearea = 'assignment_response';
$items = new DirectoryIterator($path);
$items = new DirectoryIterator($basepath);
foreach ($items as $item) {
if (!$item->isFile()) {
continue;
Expand All @@ -106,13 +110,13 @@ function xmldb_assignment_upgrade($oldversion) {
continue;
}
if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
$file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename,
$file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
if ($submission->teacher) {
$file_record['userid'] = $submission->teacher;
}
if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
unlink($path.$item->getFilename());
if ($fs->create_file_from_pathname($file_record, $basepath.$item->getFilename())) {
unlink($basepath.$item->getFilename());
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions mod/assignment/type/upload/assignment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function view() {
$this->view_dates();

if (has_capability('mod/assignment:submit', $this->context)) {
$filecount = $this->count_user_files($USER->id);
$submission = $this->get_submission($USER->id);
$filecount = $this->count_user_files($submission->id);

$this->view_feedback();

Expand Down Expand Up @@ -344,16 +344,16 @@ function print_user_files($userid=0, $return=false) {
$fs = get_file_storage();
$browser = get_file_browser();

if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) {
if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $submission->id, "timemodified", false)) {
$button = new portfolio_add_button();
foreach ($files as $file) {
$filename = $file->get_filename();
$mimetype = $file->get_mimetype();
$path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$userid.'/'.$filename);
$path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/assignment_submission/'.$submission->id.'/'.$filename);
$output .= '<a href="'.$path.'" ><img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />'.s($filename).'</a>';

if ($candelete) {
$delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid={$submission->userid}&amp;mode=$mode&amp;offset=$offset";
$delurl = "$CFG->wwwroot/mod/assignment/delete.php?id={$this->cm->id}&amp;file=".rawurlencode($filename)."&amp;userid=$userid&amp;submissionid={$submission->id}&amp;mode=$mode&amp;offset=$offset";

$output .= '<a href="'.$delurl.'">&nbsp;'
.'<img title="'.$strdelete.'" src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="" /></a> ';
Expand Down Expand Up @@ -575,11 +575,13 @@ function upload_file() {
$fs = get_file_storage();
$filename = $mform->get_new_filename('newfile');
if ($filename !== false) {
if (!$fs->file_exists($this->context->id, 'assignment_submission', $USER->id, '/', $filename)) {
if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $USER->id, '/', $filename, false, $USER->id)) {
$submission = $this->get_submission($USER->id, true); //create new submission if needed
$submission->timemodified = time();
if ($DB->update_record('assignment_submissions', $submission)) {
if (!$fs->file_exists($this->context->id, 'assignment_submission', $submission->id, '/', $filename)) {
if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $submission->id, '/', $filename, false, $USER->id)) {
$updates = new object();
$updates->id = $submission->id;
$updates->timemodified = time();
if ($DB->update_record('assignment_submissions', $updates)) {
add_to_log($this->course->id, 'assignment', 'upload',
'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
$this->update_grade($submission);
Expand Down Expand Up @@ -823,6 +825,7 @@ function delete_file() {

$file = required_param('file', PARAM_FILE);
$userid = required_param('userid', PARAM_INT);
$submissionid = required_param('submissionid', PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$mode = optional_param('mode', '', PARAM_ALPHA);
$offset = optional_param('offset', 0, PARAM_INT);
Expand All @@ -849,7 +852,7 @@ function delete_file() {
}

if (!data_submitted() or !$confirm or !confirm_sesskey()) {
$optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
$optionsyes = array ('id'=>$this->cm->id, 'file'=>$file, 'userid'=>$userid, 'submissionid'=>$submissionid, 'confirm'=>1, 'sesskey'=>sesskey(), 'mode'=>$mode, 'offset'=>$offset, 'sesskey'=>sesskey());
if (empty($mode)) {
$this->view_header(get_string('delete'));
} else {
Expand All @@ -867,7 +870,7 @@ function delete_file() {
}

$fs = get_file_storage();
if ($file = $fs->get_file($this->context->id, 'assignment_submission', $userid, '/', $file)) {
if ($file = $fs->get_file($this->context->id, 'assignment_submission', $submissionid, '/', $file)) {
if ($file->delete()) {
$submission->timemodified = time();
if ($DB->update_record('assignment_submissions', $submission)) {
Expand Down
38 changes: 20 additions & 18 deletions mod/assignment/type/uploadsingle/assignment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ function view() {

$this->view_dates();

$filecount = $this->count_user_files($USER->id);
$filecount = false;

if ($submission = $this->get_submission()) {
if ($submission = $this->get_submission($USER->id)) {
$filecount = $this->count_user_files($submission->id);
if ($submission->timemarked) {
$this->view_feedback();
}
if ($filecount) {
echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter');
echo $OUTPUT->box($this->print_user_files($submission->id, true), 'generalbox boxaligncenter');
}
}

Expand All @@ -82,15 +83,13 @@ function upload() {

require_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));

$this->view_header(get_string('upload'));

$filecount = $this->count_user_files($USER->id);
$submission = $this->get_submission($USER->id);
if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
if ($submission = $this->get_submission($USER->id)) {
//TODO: change later to ">= 0", to prevent resubmission when graded 0
if (($submission->grade > 0) and !$this->assignment->resubmit) {
echo $OUTPUT->notification(get_string('alreadygraded', 'assignment'));
redirect('view.php?id='.$this->cm->id, get_string('alreadygraded', 'assignment'));
}
}

Expand All @@ -99,17 +98,22 @@ function upload() {
$fs = get_file_storage();
$filename = $mform->get_new_filename('newfile');
if ($filename !== false) {
$fs->delete_area_files($this->context->id, 'assignment_submission', $USER->id);
if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $USER->id, '/', $filename, false, $USER->id)) {
$fs->delete_area_files($this->context->id, 'assignment_submission', $submission->id);

if (empty($submission->id)) {
$submission = $this->get_submission($USER->id, true); //create new submission if needed
$submission->timemodified = time();
$submission->numfiles = 1;
if ($DB->update_record('assignment_submissions', $submission)) {
}

if ($file = $mform->save_stored_file('newfile', $this->context->id, 'assignment_submission', $submission->id, '/', $filename, false, $USER->id)) {
$updates = new object(); //just enough data for updating the submission
$updates->timemodified = time();
$updates->numfiles = 1;
$updates->id = $submission->id;
if ($DB->update_record('assignment_submissions', $updates)) {
add_to_log($this->course->id, 'assignment', 'upload',
'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
$this->update_grade($submission);
$this->email_teachers($submission);
echo $OUTPUT->heading(get_string('uploadedfile'));
//trigger event with information about this file.
$eventdata = new object();
$eventdata->component = 'mod/assignment';
Expand All @@ -120,21 +124,19 @@ function upload() {
$eventdata->file = $file;
events_trigger('assignment_file_sent', $eventdata);

redirect('view.php?id='.$this->cm->id);
redirect('view.php?id='.$this->cm->id, get_string('uploadedfile'));
} else {
echo $OUTPUT->notification(get_string("uploadnotregistered", "assignment", $newfile_name) );
$file->delete();
redirect('view.php?id='.$this->cm->id, get_string('uploadnotregistered', 'assignment', $newfile_name));
}
}
}
} else {
echo $OUTPUT->notification(get_string("uploaderror", "assignment")); //submitting not allowed!
redirect('view.php?id='.$this->cm->id, get_string('uploaderror', 'assignment')); //submitting not allowed!
}
}

echo $OUTPUT->continue_button('view.php?id='.$this->cm->id);

$this->view_footer();
redirect('view.php?id='.$this->cm->id);
}

function setup_elements(&$mform) {
Expand Down

0 comments on commit d992f53

Please sign in to comment.