/**
 * Queue file for sending to URKUND
 *
 * @param int $cmid - course module id
 * @param int $userid - user id
 * @param varied $file string if path to temp file or full Moodle file object.
 * @return boolean
 */
function urkund_queue_file($cmid, $userid, $file)
{
    global $DB;
    $plagiarismfile = urkund_get_plagiarism_file($cmid, $userid, $file);
    $plagiarismvalues = $DB->get_records_menu('plagiarism_urkund_config', array('cm' => $cmid), '', 'name, value');
    // Check if $plagiarismfile actually needs to be submitted.
    if ($plagiarismfile->statuscode != 'pending') {
        return '';
    }
    if (is_string($file)) {
        $filename = basename($file);
    } else {
        $filename = !empty($file->filename) ? $file->filename : $file->get_filename();
    }
    if ($plagiarismfile->filename !== $filename) {
        // This is a file that was previously submitted and not sent to urkund but the filename has changed so fix it.
        $plagiarismfile->filename = $filename;
        $DB->update_record('plagiarism_urkund_files', $plagiarismfile);
    }
    // Check to see if this is a valid file.
    $mimetype = urkund_check_file_type($filename);
    if (empty($mimetype)) {
        $plagiarismfile->statuscode = URKUND_STATUSCODE_UNSUPPORTED;
        $DB->update_record('plagiarism_urkund_files', $plagiarismfile);
        return '';
    }
    // Check to see if configured to only send certain file-types and if this file matches.
    if (isset($plagiarismvalues['urkund_allowallfile']) && empty($plagiarismvalues['urkund_allowallfile'])) {
        $allowedtypes = explode(',', $plagiarismvalues['urkund_selectfiletypes']);
        $pathinfo = pathinfo($filename);
        if (!empty($pathinfo['extension'])) {
            $ext = strtolower($pathinfo['extension']);
            if (!in_array($ext, $allowedtypes)) {
                // This file is not allowed, delete it from the table.
                $DB->delete_records('plagiarism_urkund_files', array('id' => $plagiarismfile->id));
                debugging("File submitted to cm:" . $cmid . " with an extension " . $ext . " This assignment is configured to ignore this filetype, " . "only files of type:" . $plagiarismvalues['urkund_selectfiletypes'] . " are accepted");
                return '';
            }
        } else {
            // No path found - this shouldn't happen but ignore this file.
            debugging("Could not obtain the extension for a file submitted to cm:" . $cmid . " with the filename " . $filename . "only files of type:" . $plagiarismvalues['urkund_selectfiletypes'] . " are accepted");
            $DB->delete_records('plagiarism_urkund_files', array('id' => $plagiarismfile->id));
            return '';
        }
    }
    return $plagiarismfile;
}
Esempio n. 2
0
function urkund_send_file($cmid, $userid, $file, $plagiarismsettings)
{
    global $DB;
    $plagiarismfile = urkund_get_plagiarism_file($cmid, $userid, $file);
    // Check if $plagiarismfile actually needs to be submitted.
    if ($plagiarismfile->statuscode != 'pending') {
        return true;
    }
    $filename = !empty($file->filename) ? $file->filename : $file->get_filename();
    if ($plagiarismfile->filename !== $filename) {
        // This is a file that was previously submitted and not sent to urkund but the filename has changed so fix it.
        $plagiarismfile->filename = $filename;
        $DB->update_record('plagiarism_urkund_files', $plagiarismfile);
    }
    // Check to see if this is a valid file.
    $mimetype = urkund_check_file_type($filename);
    if (empty($mimetype)) {
        $plagiarismfile->statuscode = URKUND_STATUSCODE_UNSUPPORTED;
        $DB->update_record('plagiarism_urkund_files', $plagiarismfile);
        return true;
    }
    // Increment attempt number.
    $plagiarismfile->attempt = $plagiarismfile->attempt++;
    $DB->update_record('plagiarism_urkund_files', $plagiarismfile);
    return urkund_send_file_to_urkund($plagiarismfile, $plagiarismsettings, $file);
}