Skip to content

Commit

Permalink
MDL-27857 Export to portfolio support in the assignment module improved
Browse files Browse the repository at this point in the history
Portfolio API code in the assignment module expected that the current
user is the author of the submission. Therefore the "Export to
portfolio" button did not work when the submission was viewed by a
teacher (eg at the page with the list of all submissions in the Advanced
upload assignment).

This patch introduces a new callback argument 'submissionid' that holds
explicit ID of the submission the export deals with. With it available,
we do not need to expect the current user is the author of the
submission.

The patch also cleans some strings used for portfolio callback
exceptions.
  • Loading branch information
mudrd8mz committed Oct 3, 2011
1 parent f289c69 commit 59c8be2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
8 changes: 5 additions & 3 deletions mod/assignment/lang/en/assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@
a zipped web site, or anything you ask them to submit.</p>';
$string['hideintro'] = 'Hide description before available date';
$string['hideintro_help'] = 'If enabled, the assignment description is hidden before the "Available from" date. Only the assignment name is displayed.';
$string['invalidassignment'] = 'incorrect assignment';
$string['invalidid'] = 'assignment ID was incorrect';
$string['invalidtype'] = 'Incorrect assignment type';
$string['invalidassignment'] = 'Invalid assignment';
$string['invalidfileandsubmissionid'] = 'Missing file or submission ID';
$string['invalidid'] = 'Invalid assignment ID';
$string['invalidsubmissionid'] = 'Invalid submission ID';
$string['invalidtype'] = 'Invalid assignment type';
$string['invaliduserid'] = 'Invalid user ID';
$string['itemstocount'] = 'Count';
$string['lastgrade'] = 'Last grade';
Expand Down
4 changes: 2 additions & 2 deletions mod/assignment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1918,15 +1918,15 @@ function print_user_files($userid=0, $return=false) {
$path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_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 ($CFG->enableportfolios && $this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php');
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'submissionid' => $submission->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php');
$button->set_format_by_file($file);
$output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK);
}
$output .= plagiarism_get_links(array('userid'=>$userid, 'file'=>$file, 'cmid'=>$this->cm->id, 'course'=>$this->course, 'assignment'=>$this->assignment));
$output .= '<br />';
}
if ($CFG->enableportfolios && count($files) > 1 && $this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) {
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php');
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'submissionid' => $submission->id), '/mod/assignment/locallib.php');
$output .= '<br />' . $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
}
}
Expand Down
33 changes: 29 additions & 4 deletions mod/assignment/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,29 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
*/
private $assignmentfile;

/** @var int callback arg - the id of submission we export */
protected $submissionid;

/**
* callback arg for a single file export
*/
protected $fileid;

public static function expected_callbackargs() {
return array(
'id' => true,
'fileid' => false,
'id' => true,
'submissionid' => false,
'fileid' => false,
);
}

/**
* Load data needed for the portfolio export
*
* If the assignment type implements portfolio_load_data(), the processing is delegated
* to it. Otherwise, the caller must provide either fileid (to export single file) or
* submissionid (to export all data attached to the given submission) via callback arguments.
*/
public function load_data() {
global $DB, $CFG;

Expand All @@ -75,9 +86,23 @@ public function load_data() {
return $this->assignment->portfolio_load_data($this);
}

$submission = $DB->get_record('assignment_submissions', array('assignment'=>$assignment->id, 'userid'=>$this->user->id));
if (empty($this->fileid)) {
if (empty($this->submissionid)) {
throw new portfolio_caller_exception('invalidfileandsubmissionid', 'mod_assignment');
}

if (! $submission = $DB->get_record('assignment_submissions', array('assignment'=>$assignment->id, 'id'=>$this->submissionid))) {
throw new portfolio_caller_exception('invalidsubmissionid', 'mod_assignment');
}

$submissionid = $submission->id;

} else {
// once we know the file id, we do not need the submission
$submissionid = null;
}

$this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'mod_assignment', 'submission', $submission->id, 'timemodified', false);
$this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'mod_assignment', 'submission', $submissionid, 'timemodified', false);
}

public function prepare_package() {
Expand Down
2 changes: 1 addition & 1 deletion mod/assignment/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct($context, $itemid, $filearea='submission') {
$files = $fs->get_area_files($this->context->id, 'mod_assignment', $filearea, $itemid, "timemodified", false);
if (count($files) >= 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) {
$button = new portfolio_add_button();
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php');
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'submissionid' => $itemid), '/mod/assignment/locallib.php');
$button->reset_formats();
$this->portfolioform = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
}
Expand Down

0 comments on commit 59c8be2

Please sign in to comment.