From 4a1d6adafd429bbfd6d131dd0c07d0af08fb7109 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 21 Jun 2023 11:18:54 +0800 Subject: [PATCH 1/1] wip --- .../privacy/grade_grade_with_history.php | 40 +++++++++++++++++++ grade/classes/privacy/provider.php | 39 ++++++++++-------- 2 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 grade/classes/privacy/grade_grade_with_history.php diff --git a/grade/classes/privacy/grade_grade_with_history.php b/grade/classes/privacy/grade_grade_with_history.php new file mode 100644 index 0000000000..3cdb33579e --- /dev/null +++ b/grade/classes/privacy/grade_grade_with_history.php @@ -0,0 +1,40 @@ +. + +namespace core_grades\privacy; + +use grade_grade; + +/** + * A grade_item which has a reference to its historical content. + * + * @package core_grades + * @category grade + * @copyright 2023 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +class grade_grade_with_history extends grade_grade { + public int $historyid; + + public function __construct(\stdClass $params = null, $fetch = true) { + // The grade history is not a real grade_grade so we remove the ID. + $this->historyid = $params->id; + unset($params->id); + + parent::__construct($params, $fetch); + } +} diff --git a/grade/classes/privacy/provider.php b/grade/classes/privacy/provider.php index 78a84828ff..070af6fde0 100644 --- a/grade/classes/privacy/provider.php +++ b/grade/classes/privacy/provider.php @@ -33,6 +33,7 @@ use grade_item; use grade_grade; use grade_scale; use stdClass; +use core_grades\privacy\grade_grade_with_history; use core_privacy\local\metadata\collection; use core_privacy\local\request\approved_contextlist; use core_privacy\local\request\transform; @@ -53,9 +54,6 @@ class provider implements \core_privacy\local\request\subsystem\provider, \core_privacy\local\request\core_userlist_provider { - /** @var int history identifier. */ - private static $historyid; - /** * Returns metadata. * @@ -546,9 +544,10 @@ class provider implements get_string('feedbackhistoryfiles', 'core_grades') ]; foreach ($data as $key => $grades) { + /** @var grade_grade_with_history */ $gg = $grades['gradeobject']; writer::with_context($gg->get_context())->export_area_files($pathtofiles, GRADE_FILE_COMPONENT, - GRADE_HISTORY_FEEDBACK_FILEAREA, self::$historyid); + GRADE_HISTORY_FEEDBACK_FILEAREA, $gg->historyid); unset($data[$key]['gradeobject']); // Do not want to export this later. } @@ -679,9 +678,10 @@ class provider implements get_string('feedbackhistoryfiles', 'core_grades') ]; foreach ($data as $key => $grades) { + /** @var grade_grade_with_history */ $gg = $grades['gradeobject']; writer::with_context($gg->get_context())->export_area_files($pathtofiles, GRADE_FILE_COMPONENT, - GRADE_HISTORY_FEEDBACK_FILEAREA, self::$historyid); + GRADE_HISTORY_FEEDBACK_FILEAREA, $gg->historyid); unset($data[$key]['gradeobject']); // Do not want to export this later. } @@ -1040,11 +1040,10 @@ class provider implements $prefix = $ishistory ? 'ggh_' : 'gg_'; $ggrecord = static::extract_record($record, $prefix); if ($ishistory) { - // The grade history is not a real grade_grade so we remove the ID. - $historyid = $ggrecord->id; - unset($ggrecord->id); + $gg = new grade_grade_with_history($ggrecord, false); + } else { + $gg = new grade_grade($ggrecord, false); } - $gg = new grade_grade($ggrecord, false); // There is a grade item in the record. if (!empty($record->gi_id)) { @@ -1059,10 +1058,6 @@ class provider implements $gi->scale->load_items(); } - if ($ishistory) { - self::$historyid = $historyid; - } - return $gg; } @@ -1200,9 +1195,15 @@ class provider implements $timemodified = $gg->timemodified ? transform::datetime($gg->timemodified) : null; $timecreated = $gg->timecreated ? transform::datetime($gg->timecreated) : $timemodified; // When null we use timemodified. - $filearea = $ishistory ? GRADE_HISTORY_FEEDBACK_FILEAREA : GRADE_FEEDBACK_FILEAREA; - $itemid = $ishistory ? self::$historyid : $gg->id; - $subpath = $ishistory ? get_string('feedbackhistoryfiles', 'core_grades') : get_string('feedbackfiles', 'core_grades'); + if ($gg instanceof grade_grade_with_history) { + $filearea = GRADE_HISTORY_FEEDBACK_FILEAREA; + $itemid = $gg->historyid; + $subpath = get_string('feedbackhistoryfiles', 'core_grades'); + } else { + $filearea = GRADE_FEEDBACK_FILEAREA; + $itemid = $gg->id; + $subpath = get_string('feedbackfiles', 'core_grades'); + } $pathtofiles = [ get_string('grades', 'core_grades'), @@ -1275,7 +1276,11 @@ class provider implements $grades = $DB->get_recordset_sql($sql, $params); foreach ($grades as $grade) { $gg = static::extract_grade_grade_from_record($grade, $ishistory); - $fileitemid = ($ishistory) ? self::$historyid : $gg->id; + if ($gg instanceof grade_grade_with_history) { + $fileitemid = $gg->historyid; + } else { + $fileitemid = $gg->id; + } $fs->delete_area_files($gg->get_context()->id, GRADE_FILE_COMPONENT, $filearea, $fileitemid); } $grades->close(); -- 2.39.0