Example #1
0
/**
 * Generates personalized exams
 *
 * @param unknown $category            
 */
function emarking_generate_personalized_exams($category = NULL)
{
    global $DB;
    $categoryfilter = '';
    if ($category) {
        $categoryfilter = ' AND c.category = ' . $category->id;
    }
    $exams = $DB->get_records_sql('
        SELECT e.*, c.fullname, c.shortname, c.id AS courseid, c.category
        FROM {emarking_exams} e
        INNER JOIN {course} c ON (e.course = c.id)
        WHERE status = :status AND emarking > 0
        ' . $categoryfilter, array('status' => EMARKING_EXAM_UPLOADED));
    $i = 0;
    foreach ($exams as $exam) {
        $examname = "[{$exam->id}] {$exam->fullname} {$exam->name}";
        $newexam = $DB->get_record('emarking_exams', array('id' => $exam->id));
        if ($newexam->status == EMARKING_EXAM_BEING_PROCESSED) {
            mtrace("{$examname} already being generated");
            continue;
        }
        $time = microtime(true);
        $studentsforprinting = emarking_get_students_count_for_printing($exam->course, $exam);
        if ($studentsforprinting <= 0) {
            mtrace("{$examname} skipping, no students enrolled.");
            continue;
        }
        // We start processing the exam.
        $exam->status = EMARKING_EXAM_BEING_PROCESSED;
        $DB->update_record('emarking_exams', $exam);
        // The transaction is for the generation.
        try {
            $result = emarking_download_exam($exam->id, false, false, NULL, false, false, false, false, true);
            $time = round(microtime(true) - $time, 2);
            if ($result) {
                // Update the exam status to success.
                $exam->status = EMARKING_EXAM_PROCESSED;
                $DB->update_record('emarking_exams', $exam);
                $message = 'printed successfully';
            } else {
                $message = 'error printing';
                $e = new moodle_exception('Invalid PDF generation');
                // Update the exam status to error.
                $exam->status = EMARKING_EXAM_ERROR_PROCESSING;
                $DB->update_record('emarking_exams', $exam);
            }
        } catch (Exception $e) {
            $message = 'exception printing';
            // Update the exam status to error.
            $exam->status = EMARKING_EXAM_ERROR_PROCESSING;
            $DB->update_record('emarking_exams', $exam);
        }
        mtrace("{$examname} {$message}: {$studentsforprinting} students ({$time} seconds)");
        $i++;
    }
    mtrace("{$i} exams processed successfully.");
}
Example #2
0
    $tokendate->setTimestamp($_SESSION[$USER->sesskey . "smsdate"]);
    $diff = $now->diff($tokendate);
    if ($diff->i > 5 && false) {
        $PAGE->set_context($contextcourse);
        $PAGE->set_url($url);
        echo $OUTPUT->header();
        echo $OUTPUT->notification(get_string("tokenexpired", "mod_emarking"), "notifyproblem");
        $buttonurl = $incourse ? $courseurl : $coursecategoryurl;
        echo $OUTPUT->single_button($buttonurl, get_string("back"), "get");
        echo $OUTPUT->footer();
        die;
    }
    $item = array("context" => $contextcourse);
    // Add to Moodle log so some auditing can be done
    \mod_emarking\event\successfully_downloaded::create($item)->trigger();
    emarking_download_exam($examid, $multiplepdfs, null, null, null, null, true);
    die;
}
// If the token was not sent, then create new token,
// save data in session variables and send through email or mobile phone
$newtoken = rand(10000, 99999);
// Generate random 5 digits token
$date = new DateTime();
$_SESSION[$USER->sesskey . "smstoken"] = $newtoken;
// Save token in session
$_SESSION[$USER->sesskey . "smsdate"] = $date->getTimestamp();
// Save timestamp to calculate token age
$_SESSION[$USER->sesskey . "examid"] = $examid;
// Save exam id for extra security
if ($CFG->emarking_usesms) {
    // Validate mobile phone number
Example #3
0
            $archive_folder = $rsg;
            // the folder which you archivate
            $dirs[] = $rsg;
            $dir = preg_replace('/[\\/]{2,}/', '/', $archive_folder . "/");
            $dh = opendir($dir);
            while ($file = readdir($dh)) {
                if ($file != '.' && $file != '..') {
                    $files[] = $dir . $file;
                }
            }
            closedir($dh);
        }
        foreach ($files as $f) {
            unlink($f);
        }
        foreach ($dirs as $d) {
            rmdir($d);
        }
    } else {
        if (!emarking_download_exam($exam->id, true, null, $pbar, true, $idprinter, false, $debugprinting)) {
            // Send directly to printer
            print_error('Fatal error trying to print');
        }
    }
    $continue_url = new moodle_url('/mod/emarking/print/printorders.php', array('category' => $course->category));
    echo $OUTPUT->continue_button($continue_url);
} else {
    // Confirm processing and select printer
    echo $form->display();
}
echo $OUTPUT->footer();
Example #4
0
            $archive_folder = $rsg;
            // the folder which you archivate
            $dirs[] = $rsg;
            $dir = preg_replace('/[\\/]{2,}/', '/', $archive_folder . "/");
            $dh = opendir($dir);
            while ($file = readdir($dh)) {
                if ($file != '.' && $file != '..') {
                    $files[] = $dir . $file;
                }
            }
            closedir($dh);
        }
        foreach ($files as $f) {
            unlink($f);
        }
        foreach ($dirs as $d) {
            rmdir($d);
        }
    } else {
        if (!emarking_download_exam($exam->id, true, null, $pbar, true, $printer)) {
            // Send directly to printer
            print_error('Fatal error trying to print');
        }
    }
    $continue_url = new moodle_url('/mod/emarking/printorders.php', array('category' => $course->category));
    echo $OUTPUT->continue_button($continue_url);
} else {
    // Confirm processing and select printer
    echo $form->display();
}
echo $OUTPUT->footer();
Example #5
0
                 header("Pragma: no-cache");
                 header("Expires: 0");
                 readfile($archive_name);
                 foreach ($files as $f) {
                     unlink($f);
                 }
                 foreach ($dirs as $d) {
                     rmdir($d);
                 }
                 unlink($archive_name);
                 exit;
             } else {
                 echo 'Error, can\'t create a zip file!';
             }
         } else {
             emarking_download_exam($examid, $multiplepdfs);
         }
     } else {
         $file = $DB->get_record('files', array('id' => $exam->file));
         $downloadURL = $CFG->wwwroot . '/pluginfile.php/' . $file->contextid . '/mod_emarking/' . $file->filearea . '/' . $file->itemid . '/' . $file->filename . '?sesskey=' . $USER->sesskey . '&token=' . $token;
         $downloadexam = $DB->get_record('emarking_exams', array('id' => $examid));
         $downloadexam->printdate = time();
         $DB->update_record('emarking_exams', $downloadexam);
         redirect($downloadURL, '', 0);
     }
     die;
 } else {
     $item = array('context' => context_module::instance($cm->id), 'objectid' => $cm->id);
     // Add to Moodle log so some auditing can be done
     \mod_emarking\event\invalidtoken_granted::create($item)->trigger();
     print_error('Token not recognized, please go back and try again.');