Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'MDL-27520_master' of git://github.com/markn86/moodle
  • Loading branch information
David Monllao committed Oct 17, 2018
2 parents 077a1a2 + 464a9dc commit a9bbbd7
Show file tree
Hide file tree
Showing 22 changed files with 705 additions and 67 deletions.
8 changes: 8 additions & 0 deletions backup/moodle2/backup_stepslib.php
Expand Up @@ -2418,6 +2418,9 @@ protected function execute_condition() {
}

protected function define_structure() {
global $CFG;

require_once($CFG->libdir . '/grade/constants.php');

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

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

protected function define_structure() {
global $CFG;

require_once($CFG->libdir . '/grade/constants.php');

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

// Annotations.
Expand Down
34 changes: 30 additions & 4 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -3705,6 +3705,10 @@ protected function process_grade_item($data) {
}

protected function process_grade_grade($data) {
global $CFG;

require_once($CFG->libdir . '/grade/constants.php');

$data = (object)($data);
$olduserid = $data->userid;
$oldid = $data->id;
Expand All @@ -3719,7 +3723,16 @@ protected function process_grade_grade($data) {

$grade = new grade_grade($data, false);
$grade->insert('restore');
$this->set_mapping('grade_grades', $oldid, $grade->id);

$this->set_mapping('grade_grades', $oldid, $grade->id, true);

$this->add_related_files(
GRADE_FILE_COMPONENT,
GRADE_FEEDBACK_FILEAREA,
'grade_grades',
null,
$oldid
);
} else {
debugging("Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'");
}
Expand Down Expand Up @@ -3794,9 +3807,12 @@ protected function define_structure() {
}

protected function process_grade_grade($data) {
global $DB;
global $CFG, $DB;

require_once($CFG->libdir . '/grade/constants.php');

$data = (object) $data;
$oldhistoryid = $data->id;
$olduserid = $data->userid;
unset($data->id);

Expand All @@ -3807,13 +3823,23 @@ protected function process_grade_grade($data) {
$data->oldid = $this->get_mappingid('grade_grades', $data->oldid);
$data->usermodified = $this->get_mappingid('user', $data->usermodified, null);
$data->rawscaleid = $this->get_mappingid('scale', $data->rawscaleid);
$DB->insert_record('grade_grades_history', $data);

$newhistoryid = $DB->insert_record('grade_grades_history', $data);

$this->set_mapping('grade_grades_history', $oldhistoryid, $newhistoryid, true);

$this->add_related_files(
GRADE_FILE_COMPONENT,
GRADE_HISTORY_FEEDBACK_FILEAREA,
'grade_grades_history',
null,
$oldhistoryid
);
} else {
$message = "Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'";
$this->log($message, backup::LOG_DEBUG);
}
}

}

/**
Expand Down
15 changes: 14 additions & 1 deletion grade/report/history/classes/output/tablelog.php
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_FEEDBACK_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
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
35 changes: 29 additions & 6 deletions lib/filelib.php
Expand Up @@ -4198,6 +4198,9 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin

// ========================================================================================================================
} else if ($component === 'grade') {

require_once($CFG->libdir . '/grade/constants.php');

if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel == CONTEXT_SYSTEM) {
// Global gradebook files
if ($CFG->forcelogin) {
Expand All @@ -4213,15 +4216,35 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin
\core\session\manager::write_close(); // Unlock session during file serving.
send_stored_file($file, 60*60, 0, $forcedownload, $sendfileoptions);

} else if ($filearea === 'feedback' and $context->contextlevel == CONTEXT_COURSE) {
//TODO: nobody implemented this yet in grade edit form!!
send_file_not_found();
} else if ($filearea == GRADE_FEEDBACK_FILEAREA || $filearea == GRADE_HISTORY_FEEDBACK_FILEAREA) {
if ($context->contextlevel != CONTEXT_MODULE) {
send_file_not_found;
}

if ($CFG->forcelogin || $course->id != SITEID) {
require_login($course);
require_login($course, false);

$gradeid = (int) array_shift($args);
$filename = array_pop($args);
if ($filearea == GRADE_HISTORY_FEEDBACK_FILEAREA) {
$grade = $DB->get_record('grade_grades_history', ['id' => $gradeid]);
} else {
$grade = $DB->get_record('grade_grades', ['id' => $gradeid]);
}

$fullpath = "/$context->id/$component/$filearea/".implode('/', $args);
if (!$grade) {
send_file_not_found();
}

$iscurrentuser = $USER->id == $grade->userid;

if (!$iscurrentuser) {
$coursecontext = context_course::instance($course->id);
if (!has_capability('moodle/grade:viewall', $coursecontext)) {
send_file_not_found();
}
}

$fullpath = "/$context->id/$component/$filearea/$gradeid/$filename";

if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
send_file_not_found();
Expand Down
15 changes: 15 additions & 0 deletions lib/grade/constants.php
Expand Up @@ -263,3 +263,18 @@
* GRADE_MIN_MAX_FROM_GRADE_GRADE - Get the grade min/max from the grade grade.
*/
define('GRADE_MIN_MAX_FROM_GRADE_GRADE', 2);

/**
* The component to store grade files.
*/
define('GRADE_FILE_COMPONENT', 'grade');

/**
* The file area to store the associated grade_grades feedback files.
*/
define('GRADE_FEEDBACK_FILEAREA', 'feedback');

/**
* The file area to store the associated grade_grades_history feedback files.
*/
define('GRADE_HISTORY_FEEDBACK_FILEAREA', 'historyfeedback');
107 changes: 105 additions & 2 deletions lib/grade/grade_grade.php
Expand Up @@ -172,6 +172,22 @@ class grade_grade extends grade_object {
*/
public $aggregationweight = null;

/**
* Feedback files to copy.
*
* Example -
*
* [
* 'contextid' => 1,
* 'component' => 'mod_xyz',
* 'filearea' => 'mod_xyz_feedback',
* 'itemid' => 2
* ];
*
* @var array
*/
public $feedbackfiles = [];

/**
* Returns array of grades for given grade_item+users
*
Expand Down Expand Up @@ -1017,13 +1033,60 @@ public function insert($source=null) {
* @return bool success
*/
public function update($source=null) {
$this->rawgrade = grade_floatval($this->rawgrade);
$this->finalgrade = grade_floatval($this->finalgrade);
$this->rawgrade = grade_floatval($this->rawgrade);
$this->finalgrade = grade_floatval($this->finalgrade);
$this->rawgrademin = grade_floatval($this->rawgrademin);
$this->rawgrademax = grade_floatval($this->rawgrademax);
return parent::update($source);
}


/**
* Handles adding feedback files in the gradebook.
*
* @param int|null $historyid
*/
protected function add_feedback_files(int $historyid = null) {
global $CFG;

// We only support feedback files for modules atm.
if ($this->grade_item && $this->grade_item->is_external_item()) {
$context = $this->get_context();
$this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);

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

return $this->id;
}

/**
* Handles updating feedback files in the gradebook.
*
* @param int|null $historyid
*/
protected function update_feedback_files(int $historyid = null){
global $CFG;

// We only support feedback files for modules atm.
if ($this->grade_item && $this->grade_item->is_external_item()) {
$context = $this->get_context();

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

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

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

return true;
}

/**
* Deletes the grade_grade instance from the database.
*
Expand Down Expand Up @@ -1121,4 +1184,44 @@ function get_aggregation_hint() {
return array('status' => $this->get_aggregationstatus(),
'weight' => $this->get_aggregationweight());
}

/**
* Handles copying feedback files to a specified gradebook file area.
*
* @param context $context
* @param string $filearea
* @param int $itemid
*/
private function copy_feedback_files(context $context, string $filearea, int $itemid) {
if ($this->feedbackfiles) {
$filestocopycontextid = $this->feedbackfiles['contextid'];
$filestocopycomponent = $this->feedbackfiles['component'];
$filestocopyfilearea = $this->feedbackfiles['filearea'];
$filestocopyitemid = $this->feedbackfiles['itemid'];

$fs = new file_storage();
if ($filestocopy = $fs->get_area_files($filestocopycontextid, $filestocopycomponent, $filestocopyfilearea,
$filestocopyitemid)) {
foreach ($filestocopy as $filetocopy) {
$destination = [
'contextid' => $context->id,
'component' => GRADE_FILE_COMPONENT,
'filearea' => $filearea,
'itemid' => $itemid
];
$fs->create_file_from_storedfile($destination, $filetocopy);
}
}
}
}

/**
* Determine the correct context for this grade_grade.
*
* @return context
*/
public function get_context() {
$this->load_grade_item();
return $this->grade_item->get_context();
}
}

0 comments on commit a9bbbd7

Please sign in to comment.