}
    $i = 0;
    foreach ($unique_attempts as $attempt) {
        $attempts[$i] = $attempt;
        $attempts[$i]->status = JOB_STATE_WAITING;
        $attempts[$i]->timestamp = $timestamp;
        if (!($unique_jobs = get_records_sql('SELECT * FROM ' . "{$CFG->prefix}blended_jobs WHERE attempt_id= '{$attempt->id}'"))) {
            error("Encountered a problem trying to get job.");
        }
        foreach ($unique_jobs as $job) {
            $jobs[$i] = $job;
            $jobs[$i]->status = JOB_STATE_WAITING;
            $jobs[$i]->timestamp = $timestamp;
        }
        $q = $job->quiz;
        $opts = array_explode('=', ',', $jobs[$i]->identifylabel);
        $identifyLabel = $opts['identifyLabel'];
        $i++;
    }
    $quiz = get_record("quiz", "id", $q);
    $numberpaperquiz = count($attempts);
} else {
    $q = $quizid;
    $quiz = get_record("quiz", "id", $q);
    if (!$quiz) {
        error("Refereced quiz is not found: id=" . $q);
    }
    //TODO: cambiar por question_create_uniqueid_attempt
    // Get number for the next or unfinished attempt
    /*if(!$attemptnumber = (int)get_field_sql('SELECT MAX(attempt)+1 FROM ' .
    		"{$CFG->prefix}blended_attempts WHERE quiz = '{$quiz->id}' AND " .
Esempio n. 2
0
/**
 * Generate the PDF from a job
 * if no job->id is passed then groups all attempts with the same:
 * userid, quiz and timestamp with status=JOB_STATUS_WAITING in a multipage PDF
 * returns the number of attempts rendered and the pdf path produced.
 * @param unknown_type $job
 */
function generateJobs($job)
{
    global $CFG;
    try {
        /**
         * Avoid empty quizzes
         */
        $quiz = get_record("quiz", "id", $job->quiz);
        if (!$quiz->questions) {
            throw new PDFError("The quiz is empty!!", PDFError::QUIZ_IS_EMPTY);
        }
        if (isset($job->id)) {
            $query = "id = '{$job->id}' and quiz = '{$job->quiz}' and status = '" . JOB_STATE_WAITING . "'";
        } else {
            $query = "timestamp = '{$job->timestamp}' and quiz = '{$job->quiz}' and userid='{$job->userid}' and status = '" . JOB_STATE_WAITING . "'";
        }
        print "Getting pdf jobs with query: {$query}\n";
        if (!($jobs = get_records_select('blended_jobs', $query))) {
            echo "<p>There are no jobs: job's timestamp: {$job->timestamp}  job's quiz: {$job->quiz}</p>\n";
            return array(0, null);
        }
        // Mark all jobs as BUSY
        if (isset($job->id)) {
            set_field('blended_jobs', 'status', JOB_STATE_BUSY, 'id', $job->id, 'quiz', $job->quiz);
        } else {
            set_field('blended_jobs', 'status', JOB_STATE_BUSY, 'timestamp', $job->timestamp, 'quiz', $job->quiz, 'userid', $job->userid);
        }
        $numberpaperquiz = count($jobs);
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetPrintHeader(false);
        $pdf->SetPrintFooter(false);
        $pdfFile = blended_prepare_and_get_paths($job, $quiz);
        debugging("<p>  Processing job from user {$job->userid}. Quiz with id={$job->quiz}</p>\n");
        echo "<p>Generating {$numberpaperquiz} quizes out of <a href=\"{$CFG->wwwroot}/mod/quiz/view.php?q={$quiz->id}\">\"{$quiz->name}\"</a></p>\n";
        debugging("<p>PDFfile: {$pdfFile->Path}</p>");
        foreach ($jobs as $quiz_job) {
            // parse formatting options into an array
            $options = array_explode('=', ',', $quiz_job->options);
            $blended = get_records('blended', 'id', $quiz_job->blended);
            if (!($attemptdata = get_records('blended_attempts', 'id', $quiz_job->attempt_id))) {
                echo "Error ";
            }
            $a = 0;
            foreach ($attemptdata as $attempt) {
                echo "<p>Generating quiz number " . ($a + 1) . "...\n";
                try {
                    if (!$attempt->layout) {
                        throw new PDFError("El cuestionario no contiene ninguna pregunta", PDFError::QUIZ_IS_EMPTY);
                    }
                    $start = microtime(true);
                    /**
                     * Actually generate the PDF document
                     */
                    $pdf = blended_generate_quiz($attempt, $quiz, $pdf, $blended["{$quiz_job->blended}"], $options, $pdfFile);
                    $printtime = (microtime(true) - $start) * 1000;
                    mtrace(" done in {$printtime} (ms)</p>");
                    $attempt->status = JOB_STATE_FINISHED;
                    $quiz_job->status = JOB_STATE_FINISHED;
                } catch (PDFError $e) {
                    echo "Error creating a page for quiz " . $quiz_job->attempt_id . " Continuing\n";
                    echo "<BR>" . $e->getMessage() . "";
                    $attempt->status = JOB_STATE_IN_ERROR;
                    $quiz_job->status = JOB_STATE_IN_ERROR;
                }
                update_record('blended_attempts', $attempt);
                update_record('blended_jobs', $quiz_job);
                $a = $a + 1;
            }
        }
        $pdf->Output($pdfFile->Path, 'F');
    } catch (PDFError $e) {
        debugging("Fatal PDF error: " . $e->getMessage());
        foreach ($jobs as $quiz_job) {
            $quiz_job->status = JOB_STATE_IN_ERROR;
            update_record('blended_jobs', $quiz_job);
            delete_records('blended_attempts', 'status', JOB_STATE_IN_ERROR, 'id', $quiz_job->attempt_id);
        }
        return array(0, null);
    }
    return array($a, $pdfFile);
}