Example #1
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);
}
 }
 if (has_capability('mod/blended:deletejob', $context)) {
     $imgdelete = "<img src=\"delete.gif\"/>";
     $link = "<a href=\"edit_paperquiz.php?delete={$job->timestamp}&a={$blended->id}\">{$imgdelete}</a>";
     $delete = $link;
 }
 //print $estado;
 if ($estado !== 'terminado') {
     if (has_capability('mod/blended:launchjob', $context)) {
         $link_assignment_page = "<a href=\"generateQuizesPDF.php?jobid={$job->id}&launch={$job->timestamp}&a={$blended->id}\">" . get_string('runTask', 'blended') . "</a>";
         $launch = $link_assignment_page;
     }
 } else {
     //$quiz = $DB->get_record('quiz', "id", $job->quiz);
     $quiz = $DB->get_record('quiz', array('id' => $job->quiz));
     $pdfFile = blended_prepare_and_get_paths($job, $quiz);
     $launch = "<a href=\"{$CFG->wwwroot}/file.php/{$pdfFile->RelativePath}?forcedownload=1\">" . get_string('viewPDF', 'blended') . "</a>";
 }
 $fecha = date("Y-m-d-H-i-s", $job->timestamp);
 //$user_reg = $DB->get_record($table='user', $field='id', $value=$job->userid);
 $user_reg = $DB->get_record('user', array('id' => $USER->id));
 $fullname = fullname($user_reg);
 $jobrow = array();
 $jobrow["name"] = $job->quiz_name;
 $jobrow["date"] = $fecha;
 $jobrow["estado"] = $statusMsg;
 $jobrow["userid"] = $fullname;
 $jobrow["delete"] = $delete;
 $jobrow["launch"] = $launch;
 $jobs[$i] = $jobrow;
 unset($status);