protected static function prepare_submission_for_external($submission, workshop $workshop, $canviewauthorpublished = null,
            $canviewauthornames = null, $canviewallsubmissions = null) {
        global $USER;

        if (is_null($canviewauthorpublished)) {
            $canviewauthorpublished = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
        }
        if (is_null($canviewauthornames)) {
            $canviewauthornames = has_capability('mod/workshop:viewauthornames', $workshop->context);
        }
        if (is_null($canviewallsubmissions)) {
            $canviewallsubmissions = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
        }

        $ownsubmission = $submission->authorid == $USER->id;
        if (!$canviewauthornames && !$ownsubmission) {
            $submission->authorid = 0;
        }

        // Remove grade, gradeover, gradeoverby, feedbackauthor and timegraded for non-teachers or invalid phase.
        // WS mod_workshop_external::get_grades should be used for retrieving grades by students.
        if ($workshop->phase < workshop::PHASE_EVALUATION || !$canviewallsubmissions) {
            $properties = submission_exporter::properties_definition();
            foreach ($properties as $attribute => $settings) {
                // Special case, the feedbackauthor (and who did it) should be returned if the workshop is closed and
                // the user can view it.
                if (($attribute == 'feedbackauthor' || $attribute == 'gradeoverby') &&
                        $workshop->phase == workshop::PHASE_CLOSED && $ownsubmission) {
                    continue;
                }
                if (!empty($settings['optional'])) {
                    unset($submission->{$attribute});
                }
            }
        }
        return $submission;
    }