Commit 6f0e7991 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1984139: Resume add composite to have a validate function



For point 2

- The validation was being done as part of the submission after the
item was created.

We need to have a validation function to do the file attachment check
before the submission so we don't create a record.

Change-Id: I01ab672a677e4c0670f7e85debf68bb6822283e4
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent 41d0132c
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -1329,6 +1329,7 @@ EOF;
                'pluginname' => 'resume',
                'elements' => $elements,
                'jsform' => true,
                'validatecallback' => 'compositeform_validate',
                'successcallback' => 'compositeform_submit',
                'jssuccesscallback' => 'compositeSaveCallback',
            );
@@ -2304,6 +2305,44 @@ function addbook_validate(Pieform $form, $values) {
    }
}

function compositeform_validate(Pieform $form, $values) {
    global $USER;

    // Check if new attachment is already attached to another item for this artefact
    $a = false;
    try {
        $a = artefact_instance_from_type($values['compositetype'], $USER->get('id'));
    }
    catch (ArtefactNotFoundException $e) {
        // no artefact so no attachment clash
    }

    if ($a && array_key_exists('filebrowser', $values)) {
        $is_error = array();
        $new = is_array($values['filebrowser']) ? $values['filebrowser'] : array();
        // only allow the attaching of files that exist and are editable by user
        foreach ($new as $key => $fileid) {
            $file = artefact_instance_from_id($fileid);
            if (!($file instanceof ArtefactTypeFile) || !$USER->can_publish_artefact($file)) {
                $form->set_error(null, get_string('invalidattachment', 'artefact.file'));
            }
            if (record_exists('artefact_attachment', 'artefact', $a->get('id'), 'attachment', $fileid)) {
                $artefactfile = artefact_instance_from_id($fileid);
                $is_error[] = $artefactfile->get('title');
            }
        }
        if (!empty($is_error)) {
            if (sizeof($is_error) > 1) {
                $error = get_string('duplicateattachments', 'artefact.resume', implode('\', \'', $is_error));
            }
            else {
                $error = get_string('duplicateattachment', 'artefact.resume', implode(', ', $is_error));
            }
            $form->set_error(null, $error);
        }
    }
}

function compositeform_submit(Pieform $form, $values) {
    $result = array();
    try {