Exemplo n.º 1
0
/**
 *
 * @package mod
 * @subpackage emarking
 * @copyright 2015 Jorge Villalon <*****@*****.**>
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
function emarking_upload_answers($emarking, $fileid, $course, $cm, progress_bar $progressbar = null)
{
    global $CFG, $DB;
    $context = context_module::instance($cm->id);
    // Setup de directorios temporales
    $tempdir = emarking_get_temp_dir_path($emarking->id);
    if (!emarking_unzip($fileid, $tempdir . "/")) {
        return array(false, get_string('errorprocessingextraction', 'mod_emarking'), 0, 0);
    }
    $totalDocumentsProcessed = 0;
    $totalDocumentsIgnored = 0;
    // Read full directory, then start processing
    $files = scandir($tempdir);
    $doubleside = false;
    $pdfFiles = array();
    foreach ($files as $fileInTemp) {
        if (!is_dir($fileInTemp) && strtolower(substr($fileInTemp, -4, 4)) === ".png") {
            $pdfFiles[] = $fileInTemp;
            if (strtolower(substr($fileInTemp, -5, 5)) === "b.png") {
                $doubleside = true;
            }
        }
    }
    $total = count($pdfFiles);
    if ($total == 0) {
        return array(false, get_string('nopagestoprocess', 'mod_emarking'), 0, 0);
    }
    // Process files
    for ($current = 0; $current < $total; $current++) {
        $file = $pdfFiles[$current];
        $filename = explode(".", $file);
        $updatemessage = $filename;
        if ($progressbar) {
            $progressbar->update($current, $total, $updatemessage);
        }
        $parts = explode("-", $filename[0]);
        if (count($parts) != 3) {
            if ($CFG->debug) {
                echo "Ignoring {$file} as it has invalid name";
            }
            $totalDocumentsIgnored++;
            continue;
        }
        $studentid = $parts[0];
        $courseid = $parts[1];
        $pagenumber = $parts[2];
        // Now we process the files according to the emarking type
        if ($emarking->type == EMARKING_TYPE_NORMAL) {
            if (!($student = $DB->get_record('user', array('id' => $studentid)))) {
                $totalDocumentsIgnored++;
                continue;
            }
            if ($courseid != $course->id) {
                $totalDocumentsIgnored++;
                continue;
            }
        } else {
            $student = new stdClass();
            $student->id = $studentid;
        }
        // 1 pasa a 1 1 * 2 - 1 = 1
        // 1b pasa a 2 1 * 2
        // 2 pasa a 3 2 * 2 -1 = 3
        // 2b pasa a 4 2 * 2
        $anonymouspage = false;
        // First clean the page number if it's anonymous
        if (substr($pagenumber, -2) === "_a") {
            $pagenumber = substr($pagenumber, 0, strlen($pagenumber) - 2);
            $anonymouspage = true;
        }
        if ($doubleside) {
            if (substr($pagenumber, -1) === "b") {
                // Detecta b
                $pagenumber = intval($pagenumber) * 2;
            } else {
                $pagenumber = intval($pagenumber) * 2 - 1;
            }
        }
        if ($anonymouspage) {
            continue;
        }
        if (!is_numeric($pagenumber)) {
            if ($CFG->debug) {
                echo "Ignored file: {$filename['0']} page: {$pagenumber} student id: {$studentid} course id: {$courseid}";
            }
            $totalDocumentsIgnored++;
            continue;
        }
        if (emarking_submit($emarking, $context, $tempdir, $file, $student, $pagenumber)) {
            $totalDocumentsProcessed++;
        } else {
            return array(false, get_string('invalidzipnoanonymous', 'mod_emarking'), $totalDocumentsProcessed, $totalDocumentsIgnored);
        }
    }
    emarking_send_processanswers_notification($emarking, $course);
    return array(true, get_string('invalidpdfnopages', 'mod_emarking'), $totalDocumentsProcessed, $totalDocumentsIgnored);
}
Exemplo n.º 2
0
 private function fill_with_magic()
 {
     global $DB, $CFG;
     $students = get_users_by_capability($this->context, 'mod/emarking:submit');
     $emarking = $DB->get_record('emarking', array("id" => $this->cm->instance));
     $context = $this->context;
     $courseid = $this->cm->course;
     $testfolder = $CFG->dirroot . '/mod/emarking/tests/img';
     $tempdir = emarking_get_temp_dir_path($emarking->id);
     emarking_initialize_directory($tempdir, true);
     foreach ($students as $student) {
         copy($testfolder . "/test1.png", $tempdir . '/' . $student->id . '-' . $courseid . '-1.png');
         copy($testfolder . "/test1_a.png", $tempdir . '/' . $student->id . '-' . $courseid . '-1_a.png');
         copy($testfolder . "/test2.png", $tempdir . '/' . $student->id . '-' . $courseid . '-2.png');
         copy($testfolder . "/test2_a.png", $tempdir . '/' . $student->id . '-' . $courseid . '-2_a.png');
         emarking_submit($emarking, $context, $tempdir, $student->id . '-' . $courseid . '-1.png', $student, 1);
         emarking_submit($emarking, $context, $tempdir, $student->id . '-' . $courseid . '-2.png', $student, 2);
     }
 }
Exemplo n.º 3
0
/**
 * Fixes an orphan page by assigning it a student and a page number within
 * an EMarking activity.
 * @param unknown $fileid
 * @param unknown $student
 * @param unknown $emarking
 * @param unknown $context
 * @throws Exception
 * @return number
 */
function emarking_fix_page($fileid, $student, $emarking, $context, $pagenumber)
{
    global $CFG;
    // Obtiene filesystem.
    $fs = get_file_storage();
    if (!($file = $fs->get_file_by_id($fileid))) {
        throw new Exception('Invalid file id');
    }
    // Submit the file to pages.
    if (emarking_submit($emarking, $context, NULL, NULL, $student, $pagenumber, $file)) {
        $file->delete();
        return 1;
    } else {
        throw new Exception('Failed to submit');
    }
    return 1;
}