Skip to content

Commit

Permalink
MDL-27520 core: Rewrite pluginfile URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese authored and mdjnelson committed Oct 17, 2018
1 parent dd882ba commit c98e9fb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
15 changes: 14 additions & 1 deletion grade/report/history/classes/output/tablelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,20 @@ public function col_feedback(\stdClass $history) {
if ($this->is_downloading()) {
return $history->feedback;
} else {
return format_text($history->feedback, $history->feedbackformat, array('context' => $this->context));
// We need the activity context, not the course context.
$gradeitem = $this->gradeitems[$history->itemid];
$context = $gradeitem->get_context();

$feedback = file_rewrite_pluginfile_urls(
$history->feedback,
'pluginfile.php',
$context->id,
GRADE_FILE_COMPONENT,
GRADE_HISTORY_FILEAREA,
$history->id
);

return format_text($feedback, $history->feedbackformat, array('context' => $context));
}
}

Expand Down
18 changes: 16 additions & 2 deletions grade/report/user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,16 +685,30 @@ private function fill_table_recursive(&$element) {
$gradeitemdata['feedback'] = '';
$gradeitemdata['feedbackformat'] = $grade_grade->feedbackformat;

if ($grade_grade->feedback) {
$grade_grade->feedback = file_rewrite_pluginfile_urls(
$grade_grade->feedback,
'pluginfile.php',
$grade_grade->get_context()->id,
GRADE_FILE_COMPONENT,
GRADE_FEEDBACK_FILEAREA,
$grade_grade->id
);
}

if ($grade_grade->overridden > 0 AND ($type == 'categoryitem' OR $type == 'courseitem')) {
$data['feedback']['class'] = $classfeedback.' feedbacktext';
$data['feedback']['content'] = get_string('overridden', 'grades').': ' . format_text($grade_grade->feedback, $grade_grade->feedbackformat);
$data['feedback']['content'] = get_string('overridden', 'grades').': ' .
format_text($grade_grade->feedback, $grade_grade->feedbackformat,
['context' => $grade_grade->get_context()]);
$gradeitemdata['feedback'] = $grade_grade->feedback;
} else if (empty($grade_grade->feedback) or (!$this->canviewhidden and $grade_grade->is_hidden())) {
$data['feedback']['class'] = $classfeedback.' feedbacktext';
$data['feedback']['content'] = ' ';
} else {
$data['feedback']['class'] = $classfeedback.' feedbacktext';
$data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
$data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat,
['context' => $grade_grade->get_context()]);
$gradeitemdata['feedback'] = $grade_grade->feedback;
}
$data['feedback']['headers'] = "$header_cat $header_row feedback";
Expand Down
28 changes: 17 additions & 11 deletions lib/grade/grade_grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,11 @@ public function insert($source=null) {

// We only support feedback files for modules atm.
if ($this->grade_item && $this->grade_item->is_external_item()) {
$cm = get_coursemodule_from_instance($this->grade_item->itemmodule, $this->grade_item->iteminstance,
0, false, MUST_EXIST);
$modulecontext = context_module::instance($cm->id);
$this->copy_feedback_files($modulecontext, GRADE_FEEDBACK_FILEAREA, $this->id);
$context = $this->get_context();
$this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);

if (empty($CFG->disablegradehistory)) {
$this->copy_feedback_files($modulecontext, GRADE_HISTORY_FILEAREA, $historyid);
$this->copy_feedback_files($context, GRADE_HISTORY_FILEAREA, $historyid);
}
}

Expand Down Expand Up @@ -1103,17 +1101,15 @@ public function update($source=null) {

// We only support feedback files for modules atm.
if ($this->grade_item && $this->grade_item->is_external_item()) {
$cm = get_coursemodule_from_instance($this->grade_item->itemmodule, $this->grade_item->iteminstance,
0, false, MUST_EXIST);
$modulecontext = context_module::instance($cm->id);
$context = $this->get_context();

$fs = new file_storage();
$fs->delete_area_files($modulecontext->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, $this->id);
$fs->delete_area_files($context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, $this->id);

$this->copy_feedback_files($modulecontext, GRADE_FEEDBACK_FILEAREA, $this->id);
$this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);

if (empty($CFG->disablegradehistory)) {
$this->copy_feedback_files($modulecontext, GRADE_HISTORY_FILEAREA, $historyid);
$this->copy_feedback_files($context, GRADE_HISTORY_FILEAREA, $historyid);
}
}

Expand Down Expand Up @@ -1247,4 +1243,14 @@ private function copy_feedback_files(context $context, string $filearea, int $it
}
}
}

/**
* Determine the correct context for this grade_grade.
*
* @return context
*/
public function get_context() {
$this->load_grade_item();
return $this->grade_item->get_context();
}
}
15 changes: 15 additions & 0 deletions lib/grade/grade_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2480,4 +2480,19 @@ protected function notify_changed($deleted) {
\availability_grade\callbacks::grade_item_changed($this->courseid);
}
}

/**
* Helper function to get the accurate context for this grade column.
*
* @return context
*/
public function get_context() {
if ($this->itemtype == 'mod') {
$cm = get_fast_modinfo($this->courseid)->instances[$this->itemmodule][$this->iteminstance];
$context = \context_module::instance($cm->id);
} else {
$context = \context_course::instance($this->courseid);
}
return $context;
}
}
12 changes: 11 additions & 1 deletion lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,17 @@ function grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $use
if (is_null($grade->feedback)) {
$grade->str_feedback = '';
} else {
$grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
$feedback = file_rewrite_pluginfile_urls(
$grade->feedback,
'pluginfile.php',
$grade_grades[$userid]->get_context()->id,
GRADE_FILE_COMPONENT,
GRADE_FEEDBACK_FILEAREA,
$grade_grades[$userid]->id
);

$grade->str_feedback = format_text($feedback, $grade->feedbackformat,
['context' => $grade_grades[$userid]->get_context()]);
}

$item->grades[$userid] = $grade;
Expand Down

0 comments on commit c98e9fb

Please sign in to comment.