Example #1
0
function emarking_submit($emarking, $context, $path, $filename, $student, $pagenumber = 0)
{
    global $DB, $USER, $CFG;
    // All libraries for grading
    require_once "{$CFG->dirroot}/grade/grading/lib.php";
    require_once $CFG->dirroot . '/grade/lib.php';
    require_once "{$CFG->dirroot}/grade/grading/form/rubric/lib.php";
    // Calculate anonymous file name from original file name
    $filenameparts = explode(".", $filename);
    $anonymousfilename = $filenameparts[0] . "_a." . $filenameparts[1];
    // Verify that both image files (anonymous and original) exist
    if (!file_exists($path . "/" . $filename) || !file_exists($path . "/" . $anonymousfilename)) {
        throw new Exception("Invalid path and/or filename {$path} {$filename}");
    }
    if (!$student) {
        throw new Exception("Invalid student to submit page");
    }
    // Filesystem
    $fs = get_file_storage();
    $userid = isset($student->firstname) ? $student->id : $USER->id;
    $author = isset($student->firstname) ? $student->firstname . ' ' . $student->lastname : $USER->firstname . ' ' . $USER->lastname;
    // Copy file from temp folder to Moodle's filesystem
    $file_record = array('contextid' => $context->id, 'component' => 'mod_emarking', 'filearea' => 'pages', 'itemid' => $emarking->id, 'filepath' => '/', 'filename' => $filename, 'timecreated' => time(), 'timemodified' => time(), 'userid' => $userid, 'author' => $author, 'license' => 'allrightsreserved');
    // If the file already exists we delete it
    if ($fs->file_exists($context->id, 'mod_emarking', 'pages', $emarking->id, '/', $filename)) {
        $previousfile = $fs->get_file($context->id, 'mod_emarking', 'pages', $emarking->id, '/', $filename);
        $previousfile->delete();
    }
    // Info for the new file
    $fileinfo = $fs->create_file_from_pathname($file_record, $path . '/' . $filename);
    // Now copying the anonymous version of the file
    $file_record['filename'] = $anonymousfilename;
    // Check if anoymous file exists and delete it
    if ($fs->file_exists($context->id, 'mod_emarking', 'pages', $emarking->id, '/', $anonymousfilename)) {
        $previousfile = $fs->get_file($context->id, 'mod_emarking', 'pages', $emarking->id, '/', $anonymousfilename);
        $previousfile->delete();
    }
    $fileinfoanonymous = $fs->create_file_from_pathname($file_record, $path . '/' . $anonymousfilename);
    $submission = emarking_get_or_create_submission($emarking, $student, $context);
    // Get the page from previous uploads. If exists update it, if not insert a new page
    $page = $DB->get_record('emarking_page', array('submission' => $submission->id, 'student' => $student->id, 'page' => $pagenumber));
    if ($page != null) {
        $page->file = $fileinfo->get_id();
        $page->fileanonymous = $fileinfoanonymous->get_id();
        $page->timemodified = time();
        $page->teacher = $USER->id;
        $DB->update_record('emarking_page', $page);
    } else {
        $page = new stdClass();
        $page->student = $student->id;
        $page->page = $pagenumber;
        $page->file = $fileinfo->get_id();
        $page->fileanonymous = $fileinfoanonymous->get_id();
        $page->submission = $submission->id;
        $page->timecreated = time();
        $page->timemodified = time();
        $page->teacher = $USER->id;
        $page->id = $DB->insert_record('emarking_page', $page);
    }
    // Update submission info
    $submission->teacher = $page->teacher;
    $submission->timemodified = $page->timemodified;
    $DB->update_record('emarking_submission', $submission);
    return true;
}
Example #2
0
/**
 * Uploads a PDF file as a student's submission for a specific assignment
 *
 * @param object $emarking
 *            the assignment object from dbrecord
 * @param unknown_type $context
 *            the coursemodule
 * @param unknown_type $course
 *            the course object
 * @param unknown_type $path            
 * @param unknown_type $filename            
 * @param unknown_type $student            
 * @param unknown_type $numpages            
 * @param unknown_type $merge            
 * @return boolean
 */
function emarking_submit($emarking, $context, $path, $filename, $student, $pagenumber = 0, $storedfile = NULL)
{
    global $DB, $USER, $CFG;
    // All libraries for grading.
    require_once $CFG->dirroot . '/grade/grading/lib.php';
    require_once $CFG->dirroot . '/grade/lib.php';
    require_once $CFG->dirroot . '/grade/grading/form/rubric/lib.php';
    // Info for the new file.
    $fileinfo = emarking_create_page_file_from_path_or_file($filename, $path, $student, $context, $emarking, $storedfile, $pagenumber);
    // Calculate anonymous file name from original file name.
    $anonymousfilename = emarking_get_anonymous_filename_or_storedfile($filename, $storedfile);
    // Now copying the anonymous version of the file.
    if ($storedfile) {
        if ($anonymousfilename) {
            $fileinfoanonymous = emarking_create_page_file_from_path_or_file(NULL, NULL, $student, $context, $emarking, $storedfile, $pagenumber);
        } else {
            $fileinfoanonymous = emarking_create_anonymous_page_from_storedfile($storedfile, $student);
        }
    } else {
        $fileinfoanonymous = emarking_create_page_file_from_path_or_file($anonymousfilename, $path, $student, $context, $emarking, NULL, $pagenumber);
    }
    if (!$fileinfo || !$fileinfoanonymous) {
        throw new Exception('Could not create file or anonymous');
    }
    // Gets or creates a submission for the student.
    $submission = emarking_get_or_create_submission($emarking, $student, $context);
    // Get the page from previous uploads. If exists update it, if not insert a new page.
    $page = $DB->get_record('emarking_page', array('submission' => $submission->id, 'page' => $pagenumber));
    if ($page != null) {
        $page->file = $fileinfo->get_id();
        $page->fileanonymous = $fileinfoanonymous->get_id();
        $page->timemodified = time();
        $DB->update_record('emarking_page', $page);
    } else {
        $page = new stdClass();
        $page->page = $pagenumber;
        $page->file = $fileinfo->get_id();
        $page->fileanonymous = $fileinfoanonymous->get_id();
        $page->submission = $submission->id;
        $page->timecreated = time();
        $page->timemodified = time();
        $page->id = $DB->insert_record('emarking_page', $page);
    }
    // Update submission info.
    $submission->timemodified = $page->timemodified;
    $DB->update_record('emarking_submission', $submission);
    return true;
}