protected static function delete_files(array $itemids, bool $ishistory, array $userids = null) {
        global $DB;

        list($iteminnsql, $params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
        if (!is_null($userids)) {
            list($userinnsql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
            $params = array_merge($params, $userparams);
        }

        if ($ishistory) {
            $gradefields = static::get_fields_sql('grade_grades_history', 'ggh', 'ggh_');
            $gradetable = 'grade_grades_history';
            $tableprefix = 'ggh';
            $filearea = GRADE_HISTORY_FEEDBACK_FILEAREA;
        } else {
            $gradefields = static::get_fields_sql('grade_grade', 'gg', 'gg_');
            $gradetable = 'grade_grades';
            $tableprefix = 'gg';
            $filearea = GRADE_FEEDBACK_FILEAREA;
        }

        $gifields = static::get_fields_sql('grade_item', 'gi', 'gi_');

        $fs = new \file_storage();
        $sql = "SELECT $gradefields, $gifields
                  FROM {{$gradetable}} $tableprefix
                  JOIN {grade_items} gi
                    ON gi.id = {$tableprefix}.itemid
                 WHERE gi.id $iteminnsql ";
        if (!is_null($userids)) {
            $sql .= "AND {$tableprefix}.userid $userinnsql";
        }

        $grades = $DB->get_recordset_sql($sql, $params);
        foreach ($grades as $grade) {
            $gg = static::extract_grade_grade_from_record($grade, $ishistory);
            $fileitemid = ($ishistory) ? $gg->historyid : $gg->id;
            $fs->delete_area_files($gg->get_context()->id, GRADE_FILE_COMPONENT, $filearea, $fileitemid);
        }
        $grades->close();
    }