Skip to content

Commit a9bbbd7

Browse files
author
David Monllao
committed
Merge branch 'MDL-27520_master' of git://github.com/markn86/moodle
2 parents 077a1a2 + 464a9dc commit a9bbbd7

22 files changed

+705
-67
lines changed

backup/moodle2/backup_stepslib.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,9 @@ protected function execute_condition() {
24182418
}
24192419

24202420
protected function define_structure() {
2421+
global $CFG;
2422+
2423+
require_once($CFG->libdir . '/grade/constants.php');
24212424

24222425
// To know if we are including userinfo
24232426
$userinfo = $this->get_setting_value('userinfo');
@@ -2474,6 +2477,7 @@ protected function define_structure() {
24742477
// This only happens if we are including user info
24752478
if ($userinfo) {
24762479
$grade->set_source_table('grade_grades', array('itemid' => backup::VAR_PARENTID));
2480+
$grade->annotate_files(GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, 'id');
24772481
}
24782482

24792483
$letter->set_source_table('grade_letters', array('contextid' => backup::VAR_CONTEXTID));
@@ -2510,6 +2514,9 @@ protected function execute_condition() {
25102514
}
25112515

25122516
protected function define_structure() {
2517+
global $CFG;
2518+
2519+
require_once($CFG->libdir . '/grade/constants.php');
25132520

25142521
// Settings to use.
25152522
$userinfo = $this->get_setting_value('userinfo');
@@ -2537,6 +2544,7 @@ protected function define_structure() {
25372544
JOIN {backup_ids_temp} bi ON ggh.itemid = bi.itemid
25382545
WHERE bi.backupid = ?
25392546
AND bi.itemname = 'grade_item'", array(backup::VAR_BACKUPID));
2547+
$grade->annotate_files(GRADE_FILE_COMPONENT, GRADE_HISTORY_FEEDBACK_FILEAREA, 'id');
25402548
}
25412549

25422550
// Annotations.

backup/moodle2/restore_stepslib.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,6 +3705,10 @@ protected function process_grade_item($data) {
37053705
}
37063706

37073707
protected function process_grade_grade($data) {
3708+
global $CFG;
3709+
3710+
require_once($CFG->libdir . '/grade/constants.php');
3711+
37083712
$data = (object)($data);
37093713
$olduserid = $data->userid;
37103714
$oldid = $data->id;
@@ -3719,7 +3723,16 @@ protected function process_grade_grade($data) {
37193723

37203724
$grade = new grade_grade($data, false);
37213725
$grade->insert('restore');
3722-
$this->set_mapping('grade_grades', $oldid, $grade->id);
3726+
3727+
$this->set_mapping('grade_grades', $oldid, $grade->id, true);
3728+
3729+
$this->add_related_files(
3730+
GRADE_FILE_COMPONENT,
3731+
GRADE_FEEDBACK_FILEAREA,
3732+
'grade_grades',
3733+
null,
3734+
$oldid
3735+
);
37233736
} else {
37243737
debugging("Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'");
37253738
}
@@ -3794,9 +3807,12 @@ protected function define_structure() {
37943807
}
37953808

37963809
protected function process_grade_grade($data) {
3797-
global $DB;
3810+
global $CFG, $DB;
3811+
3812+
require_once($CFG->libdir . '/grade/constants.php');
37983813

37993814
$data = (object) $data;
3815+
$oldhistoryid = $data->id;
38003816
$olduserid = $data->userid;
38013817
unset($data->id);
38023818

@@ -3807,13 +3823,23 @@ protected function process_grade_grade($data) {
38073823
$data->oldid = $this->get_mappingid('grade_grades', $data->oldid);
38083824
$data->usermodified = $this->get_mappingid('user', $data->usermodified, null);
38093825
$data->rawscaleid = $this->get_mappingid('scale', $data->rawscaleid);
3810-
$DB->insert_record('grade_grades_history', $data);
3826+
3827+
$newhistoryid = $DB->insert_record('grade_grades_history', $data);
3828+
3829+
$this->set_mapping('grade_grades_history', $oldhistoryid, $newhistoryid, true);
3830+
3831+
$this->add_related_files(
3832+
GRADE_FILE_COMPONENT,
3833+
GRADE_HISTORY_FEEDBACK_FILEAREA,
3834+
'grade_grades_history',
3835+
null,
3836+
$oldhistoryid
3837+
);
38113838
} else {
38123839
$message = "Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'";
38133840
$this->log($message, backup::LOG_DEBUG);
38143841
}
38153842
}
3816-
38173843
}
38183844

38193845
/**

grade/report/history/classes/output/tablelog.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,20 @@ public function col_feedback(\stdClass $history) {
314314
if ($this->is_downloading()) {
315315
return $history->feedback;
316316
} else {
317-
return format_text($history->feedback, $history->feedbackformat, array('context' => $this->context));
317+
// We need the activity context, not the course context.
318+
$gradeitem = $this->gradeitems[$history->itemid];
319+
$context = $gradeitem->get_context();
320+
321+
$feedback = file_rewrite_pluginfile_urls(
322+
$history->feedback,
323+
'pluginfile.php',
324+
$context->id,
325+
GRADE_FILE_COMPONENT,
326+
GRADE_HISTORY_FEEDBACK_FILEAREA,
327+
$history->id
328+
);
329+
330+
return format_text($feedback, $history->feedbackformat, array('context' => $context));
318331
}
319332
}
320333

grade/report/user/lib.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,16 +685,30 @@ private function fill_table_recursive(&$element) {
685685
$gradeitemdata['feedback'] = '';
686686
$gradeitemdata['feedbackformat'] = $grade_grade->feedbackformat;
687687

688+
if ($grade_grade->feedback) {
689+
$grade_grade->feedback = file_rewrite_pluginfile_urls(
690+
$grade_grade->feedback,
691+
'pluginfile.php',
692+
$grade_grade->get_context()->id,
693+
GRADE_FILE_COMPONENT,
694+
GRADE_FEEDBACK_FILEAREA,
695+
$grade_grade->id
696+
);
697+
}
698+
688699
if ($grade_grade->overridden > 0 AND ($type == 'categoryitem' OR $type == 'courseitem')) {
689700
$data['feedback']['class'] = $classfeedback.' feedbacktext';
690-
$data['feedback']['content'] = get_string('overridden', 'grades').': ' . format_text($grade_grade->feedback, $grade_grade->feedbackformat);
701+
$data['feedback']['content'] = get_string('overridden', 'grades').': ' .
702+
format_text($grade_grade->feedback, $grade_grade->feedbackformat,
703+
['context' => $grade_grade->get_context()]);
691704
$gradeitemdata['feedback'] = $grade_grade->feedback;
692705
} else if (empty($grade_grade->feedback) or (!$this->canviewhidden and $grade_grade->is_hidden())) {
693706
$data['feedback']['class'] = $classfeedback.' feedbacktext';
694707
$data['feedback']['content'] = ' ';
695708
} else {
696709
$data['feedback']['class'] = $classfeedback.' feedbacktext';
697-
$data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
710+
$data['feedback']['content'] = format_text($grade_grade->feedback, $grade_grade->feedbackformat,
711+
['context' => $grade_grade->get_context()]);
698712
$gradeitemdata['feedback'] = $grade_grade->feedback;
699713
}
700714
$data['feedback']['headers'] = "$header_cat $header_row feedback";

lib/filelib.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,6 +4198,9 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin
41984198

41994199
// ========================================================================================================================
42004200
} else if ($component === 'grade') {
4201+
4202+
require_once($CFG->libdir . '/grade/constants.php');
4203+
42014204
if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel == CONTEXT_SYSTEM) {
42024205
// Global gradebook files
42034206
if ($CFG->forcelogin) {
@@ -4213,15 +4216,35 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin
42134216
\core\session\manager::write_close(); // Unlock session during file serving.
42144217
send_stored_file($file, 60*60, 0, $forcedownload, $sendfileoptions);
42154218

4216-
} else if ($filearea === 'feedback' and $context->contextlevel == CONTEXT_COURSE) {
4217-
//TODO: nobody implemented this yet in grade edit form!!
4218-
send_file_not_found();
4219+
} else if ($filearea == GRADE_FEEDBACK_FILEAREA || $filearea == GRADE_HISTORY_FEEDBACK_FILEAREA) {
4220+
if ($context->contextlevel != CONTEXT_MODULE) {
4221+
send_file_not_found;
4222+
}
42194223

4220-
if ($CFG->forcelogin || $course->id != SITEID) {
4221-
require_login($course);
4224+
require_login($course, false);
4225+
4226+
$gradeid = (int) array_shift($args);
4227+
$filename = array_pop($args);
4228+
if ($filearea == GRADE_HISTORY_FEEDBACK_FILEAREA) {
4229+
$grade = $DB->get_record('grade_grades_history', ['id' => $gradeid]);
4230+
} else {
4231+
$grade = $DB->get_record('grade_grades', ['id' => $gradeid]);
42224232
}
42234233

4224-
$fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
4234+
if (!$grade) {
4235+
send_file_not_found();
4236+
}
4237+
4238+
$iscurrentuser = $USER->id == $grade->userid;
4239+
4240+
if (!$iscurrentuser) {
4241+
$coursecontext = context_course::instance($course->id);
4242+
if (!has_capability('moodle/grade:viewall', $coursecontext)) {
4243+
send_file_not_found();
4244+
}
4245+
}
4246+
4247+
$fullpath = "/$context->id/$component/$filearea/$gradeid/$filename";
42254248

42264249
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
42274250
send_file_not_found();

lib/grade/constants.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,18 @@
263263
* GRADE_MIN_MAX_FROM_GRADE_GRADE - Get the grade min/max from the grade grade.
264264
*/
265265
define('GRADE_MIN_MAX_FROM_GRADE_GRADE', 2);
266+
267+
/**
268+
* The component to store grade files.
269+
*/
270+
define('GRADE_FILE_COMPONENT', 'grade');
271+
272+
/**
273+
* The file area to store the associated grade_grades feedback files.
274+
*/
275+
define('GRADE_FEEDBACK_FILEAREA', 'feedback');
276+
277+
/**
278+
* The file area to store the associated grade_grades_history feedback files.
279+
*/
280+
define('GRADE_HISTORY_FEEDBACK_FILEAREA', 'historyfeedback');

lib/grade/grade_grade.php

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ class grade_grade extends grade_object {
172172
*/
173173
public $aggregationweight = null;
174174

175+
/**
176+
* Feedback files to copy.
177+
*
178+
* Example -
179+
*
180+
* [
181+
* 'contextid' => 1,
182+
* 'component' => 'mod_xyz',
183+
* 'filearea' => 'mod_xyz_feedback',
184+
* 'itemid' => 2
185+
* ];
186+
*
187+
* @var array
188+
*/
189+
public $feedbackfiles = [];
190+
175191
/**
176192
* Returns array of grades for given grade_item+users
177193
*
@@ -1017,13 +1033,60 @@ public function insert($source=null) {
10171033
* @return bool success
10181034
*/
10191035
public function update($source=null) {
1020-
$this->rawgrade = grade_floatval($this->rawgrade);
1021-
$this->finalgrade = grade_floatval($this->finalgrade);
1036+
$this->rawgrade = grade_floatval($this->rawgrade);
1037+
$this->finalgrade = grade_floatval($this->finalgrade);
10221038
$this->rawgrademin = grade_floatval($this->rawgrademin);
10231039
$this->rawgrademax = grade_floatval($this->rawgrademax);
10241040
return parent::update($source);
10251041
}
10261042

1043+
1044+
/**
1045+
* Handles adding feedback files in the gradebook.
1046+
*
1047+
* @param int|null $historyid
1048+
*/
1049+
protected function add_feedback_files(int $historyid = null) {
1050+
global $CFG;
1051+
1052+
// We only support feedback files for modules atm.
1053+
if ($this->grade_item && $this->grade_item->is_external_item()) {
1054+
$context = $this->get_context();
1055+
$this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);
1056+
1057+
if (empty($CFG->disablegradehistory) && $historyid) {
1058+
$this->copy_feedback_files($context, GRADE_HISTORY_FEEDBACK_FILEAREA, $historyid);
1059+
}
1060+
}
1061+
1062+
return $this->id;
1063+
}
1064+
1065+
/**
1066+
* Handles updating feedback files in the gradebook.
1067+
*
1068+
* @param int|null $historyid
1069+
*/
1070+
protected function update_feedback_files(int $historyid = null){
1071+
global $CFG;
1072+
1073+
// We only support feedback files for modules atm.
1074+
if ($this->grade_item && $this->grade_item->is_external_item()) {
1075+
$context = $this->get_context();
1076+
1077+
$fs = new file_storage();
1078+
$fs->delete_area_files($context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, $this->id);
1079+
1080+
$this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);
1081+
1082+
if (empty($CFG->disablegradehistory) && $historyid) {
1083+
$this->copy_feedback_files($context, GRADE_HISTORY_FEEDBACK_FILEAREA, $historyid);
1084+
}
1085+
}
1086+
1087+
return true;
1088+
}
1089+
10271090
/**
10281091
* Deletes the grade_grade instance from the database.
10291092
*
@@ -1121,4 +1184,44 @@ function get_aggregation_hint() {
11211184
return array('status' => $this->get_aggregationstatus(),
11221185
'weight' => $this->get_aggregationweight());
11231186
}
1187+
1188+
/**
1189+
* Handles copying feedback files to a specified gradebook file area.
1190+
*
1191+
* @param context $context
1192+
* @param string $filearea
1193+
* @param int $itemid
1194+
*/
1195+
private function copy_feedback_files(context $context, string $filearea, int $itemid) {
1196+
if ($this->feedbackfiles) {
1197+
$filestocopycontextid = $this->feedbackfiles['contextid'];
1198+
$filestocopycomponent = $this->feedbackfiles['component'];
1199+
$filestocopyfilearea = $this->feedbackfiles['filearea'];
1200+
$filestocopyitemid = $this->feedbackfiles['itemid'];
1201+
1202+
$fs = new file_storage();
1203+
if ($filestocopy = $fs->get_area_files($filestocopycontextid, $filestocopycomponent, $filestocopyfilearea,
1204+
$filestocopyitemid)) {
1205+
foreach ($filestocopy as $filetocopy) {
1206+
$destination = [
1207+
'contextid' => $context->id,
1208+
'component' => GRADE_FILE_COMPONENT,
1209+
'filearea' => $filearea,
1210+
'itemid' => $itemid
1211+
];
1212+
$fs->create_file_from_storedfile($destination, $filetocopy);
1213+
}
1214+
}
1215+
}
1216+
}
1217+
1218+
/**
1219+
* Determine the correct context for this grade_grade.
1220+
*
1221+
* @return context
1222+
*/
1223+
public function get_context() {
1224+
$this->load_grade_item();
1225+
return $this->grade_item->get_context();
1226+
}
11241227
}

0 commit comments

Comments
 (0)