/** * Creates a personalized exam file. * * @param unknown $examid * @return NULL */ function emarking_download_exam($examid, $multiplepdfs = false, $groupid = null, progress_bar $pbar = null, $sendprintorder = false, $idprinter = null, $printanswersheet = false, $debugprinting = false) { global $DB, $CFG, $USER, $OUTPUT; require_once $CFG->dirroot . '/mod/emarking/lib/openbub/ans_pdf_open.php'; // Validate emarking exam object if (!($downloadexam = $DB->get_record('emarking_exams', array('id' => $examid)))) { throw new Exception(get_string("invalidexamid", "mod_emarking")); } // Contexto del curso para verificar permisos $context = context_course::instance($downloadexam->course); if (!has_capability('mod/emarking:downloadexam', $context)) { throw new Exception(get_string("invalidaccess", "mod_emarking")); } // Verify that remote printing is enable, otherwise disable a printing order if ($sendprintorder && (!$CFG->emarking_enableprinting || $idprinter == null)) { throw new Exception('Printing is not enabled or printername was absent ' . $idprinter); } // Validate course if (!($course = $DB->get_record('course', array('id' => $downloadexam->course)))) { throw new Exception(get_string("invalidcourse", "mod_emarking")); } // Validate course category if (!($coursecat = $DB->get_record('course_categories', array('id' => $course->category)))) { throw new Exception(get_string("invalidcategoryid", "mod_emarking")); } // We tell the user we are setting up the printing if ($pbar) { $pbar->update(0, 1, get_string('settingupprinting', 'mod_emarking')); } // Default value for enrols that will be included if ($CFG->emarking_enrolincludes && strlen($CFG->emarking_enrolincludes) > 1) { $enrolincludes = $CFG->emarking_enrolincludes; } // If the exam sets enrolments, we use those if (isset($downloadexam->enrolments) && strlen($downloadexam->enrolments) > 1) { $enrolincludes = $downloadexam->enrolments; } // Convert enrolments to array $enrolincludes = explode(",", $enrolincludes); // Produce all PDFs first separatedly $filedir = $CFG->dataroot . "/temp/emarking/{$context->id}"; $fileimg = $filedir . "/qr"; $userimgdir = $filedir . "/u"; $pdfdir = $filedir . "/pdf"; emarking_initialize_directory($filedir, true); emarking_initialize_directory($fileimg, true); emarking_initialize_directory($userimgdir, true); emarking_initialize_directory($pdfdir, true); // Get all the files uploaded as forms for this exam $fs = get_file_storage(); $files = $fs->get_area_files($context->id, 'mod_emarking', 'exams', $examid); // We filter only the PDFs $pdffileshash = array(); foreach ($files as $filepdf) { if ($filepdf->get_mimetype() === 'application/pdf') { $pdffileshash[] = array('hash' => $filepdf->get_pathnamehash(), 'filename' => $filepdf->get_filename(), 'path' => emarking_get_path_from_hash($filedir, $filepdf->get_pathnamehash())); } } // Verify that at least we have a PDF if (count($pdffileshash) < 1) { throw new Exception(get_string("examhasnopdf", "mod_emarking")); } $students = emarking_get_students_for_printing($downloadexam->course); $studentinfo = array(); $currenttemplate = 0; // Fill studentnames with student info (name, idnumber, id and picture) foreach ($students as $student) { $studentenrolments = explode(",", $student->enrol); // Verifies that the student is enrolled through a valid enrolment and that we haven't added her yet if (count(array_intersect($studentenrolments, $enrolincludes)) == 0 || isset($studentinfo[$student->id])) { continue; } // We create a student info object $studentobj = new stdClass(); $studentobj->name = substr("{$student->lastname}, {$student->firstname}", 0, 65); $studentobj->idnumber = $student->idnumber; $studentobj->id = $student->id; $studentobj->picture = emarking_get_student_picture($student, $userimgdir); // Store student info in hash so every student is stored once $studentinfo[$student->id] = $studentobj; } // We validate the number of students as we are filtering by enrolment // type after getting the data $numberstudents = count($studentinfo); if ($numberstudents == 0) { throw new Exception('No students to print/create the exam'); } // Add the extra students to the list for ($i = $numberstudents; $i < $numberstudents + $downloadexam->extraexams; $i++) { $studentobj = new stdClass(); $studentobj->name = '..............................................................................'; $studentobj->idnumber = 0; $studentobj->id = 0; $studentobj->picture = $CFG->dirroot . "/pix/u/f1.png"; $studentinfo[] = $studentobj; } // Check if there is a logo file $logofilepath = emarking_get_logo_file($filedir); // If asked to do so we create a PDF witht the students list if ($downloadexam->printlist == 1) { $pdf = new FPDI(); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); emarking_draw_student_list($pdf, $logofilepath, $downloadexam, $course, $studentinfo); $studentlistpdffile = $pdfdir . "/000-studentslist.pdf"; $pdf->Output($studentlistpdffile, "F"); // se genera el nuevo pdf $pdf = null; } // Here we produce a PDF file for each student $currentstudent = 0; foreach ($studentinfo as $stinfo) { // If we have a progress bar, we notify the new PDF being created if ($pbar) { $pbar->update($currentstudent + 1, count($studentinfo), $stinfo->name); } // We create the PDF file $pdf = new FPDI(); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); // We use the next form available from the list of PDF forms sent if ($currenttemplate >= count($pdffileshash) - 1) { $currenttemplate = 0; } else { $currenttemplate++; } // Load the PDF from the filesystem as template $path = $pdffileshash[$currenttemplate]['path']; $originalpdfpages = $pdf->setSourceFile($path); $pdf->SetAutoPageBreak(false); // Add all pages in the template, adding the header if it corresponds for ($pagenumber = 1; $pagenumber <= $originalpdfpages + $downloadexam->extrasheets; $pagenumber++) { // Adding a page $pdf->AddPage(); // If the page is not an extra page, we import the page from the template if ($pagenumber <= $originalpdfpages) { $template = $pdf->importPage($pagenumber); $pdf->useTemplate($template, 0, 0, 0, 0, true); } // If we have a personalized header, we add it if ($downloadexam->headerqr) { emarking_draw_header($pdf, $stinfo, $downloadexam->name, $pagenumber, $fileimg, $logofilepath, $course, $originalpdfpages + $downloadexam->extrasheets); } } // The filename will be the student id - course id - page number $qrstringtmp = $stinfo->id > 0 ? "{$stinfo->id}-{$course->id}-{$pagenumber}" : "NN{$currentstudent}+1-{$course->id}-{$pagenumber}"; // Create the PDF file for the student $pdffile = $pdfdir . "/" . $qrstringtmp . ".pdf"; $pdf->Output($pdffile, "F"); // Store the exam file for printing later $stinfo->examfile = $pdffile; $stinfo->number = $currentstudent + 1; $stinfo->pdffilename = $qrstringtmp; $currentstudent++; } $sqlprinter = "SELECT id, name, command\n\t\t\tFROM {emarking_printers}\n\t\t\tWHERE id = ?"; $printerinfo = $DB->get_record_sql($sqlprinter, array($idprinter)); // If we have to print directly $debugprintingmsg = ''; if ($sendprintorder) { // Check if we have to print the students list if ($downloadexam->printlist == 1) { $printresult = emarking_print_file($printerinfo->name, $printerinfo->command, $studentlistpdffile, $debugprinting); if (!$printresult) { $debugprintingmsg .= 'Problems printing ' . $studentlistpdffile . '<hr>'; } else { $debugprintingmsg .= $printresult . '<hr>'; } } // Print each student $currentstudent = 0; foreach ($studentinfo as $stinfo) { $currentstudent++; if ($pbar != null) { $pbar->update($currentstudent, count($studentinfo), get_string('printing', 'mod_emarking') . ' ' . $stinfo->name); } if (!isset($stinfo->examfile) || !file_exists($stinfo->examfile)) { continue; } $printresult = emarking_print_file($printerinfo->name, $printerinfo->command, $stinfo->examfile, $debugprinting); if (!$printresult) { $debugprintingmsg .= 'Problems printing ' . $stinfo->examfile . '<hr>'; } else { $debugprintingmsg .= $printresult . '<hr>'; } } if ($CFG->debug || $debugprinting) { echo $debugprintingmsg; } // Notify everyone that the exam was printed emarking_send_examprinted_notification($downloadexam, $course); $downloadexam->status = EMARKING_EXAM_SENT_TO_PRINT; $downloadexam->printdate = time(); $DB->update_record('emarking_exams', $downloadexam); return true; } $examfilename = emarking_clean_filename($course->shortname, true) . "_" . emarking_clean_filename($downloadexam->name, true); $zipdebugmsg = ''; if ($multiplepdfs) { $zip = new ZipArchive(); $zipfilename = $filedir . "/" . $examfilename . ".zip"; if ($zip->open($zipfilename, ZipArchive::CREATE) !== true) { throw new Exception('Could not create zip file'); } // Check if we have to print the students list if ($downloadexam->printlist == 1) { $zip->addFile($studentlistpdffile); } // Add every student PDF to zip file $currentstudent = 0; foreach ($studentinfo as $stinfo) { $currentstudent++; if ($pbar != null) { $pbar->update($currentstudent, count($studentinfo), get_string('printing', 'mod_emarking') . ' ' . $stinfo->name); } if (!isset($stinfo->examfile) || !file_exists($stinfo->examfile)) { continue; } if (!$zip->addFile($stinfo->examfile, $stinfo->pdffilename . '.pdf')) { $zipdebugmsg .= "Problems adding {$stinfo->examfile} to ZIP file using name {$stinfo->pdffilename} <hr>"; } } $zip->close(); if ($CFG->debug || $debugprinting) { echo $zipdebugmsg; } // Notify everyone that the exam was downloaded emarking_send_examdownloaded_notification($downloadexam, $course); $downloadexam->status = EMARKING_EXAM_SENT_TO_PRINT; $downloadexam->printdate = time(); $DB->update_record('emarking_exams', $downloadexam); // Read zip file from disk and send to the browser $file_name = basename($zipfilename); header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=" . $examfilename . ".zip"); header("Content-Length: " . filesize($zipfilename)); readfile($zipfilename); exit; } // We create the final big PDF file $pdf = new FPDI(); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); // We import the students list if required if ($downloadexam->printlist) { emarking_import_pdf_into_pdf($pdf, $studentlistpdffile); } // Add every student PDF to zip file $currentstudent = 0; foreach ($studentinfo as $stinfo) { $currentstudent++; if (!isset($stinfo->examfile) || !file_exists($stinfo->examfile)) { continue; } emarking_import_pdf_into_pdf($pdf, $stinfo->examfile); } // Notify everyone that the exam was downloaded emarking_send_examdownloaded_notification($downloadexam, $course); $downloadexam->status = EMARKING_EXAM_SENT_TO_PRINT; $downloadexam->printdate = time(); $DB->update_record('emarking_exams', $downloadexam); $pdf->Output($examfilename . '.pdf', 'D'); }
/** * * @param unknown $pdf * @param unknown $filedir * @param unknown $stinfo * @param unknown $logofilepath * @param unknown $path * @param unknown $fileimg * @param unknown $course * @param unknown $examname * @param unknown $answers * @param unknown $attemptid */ function emarking_add_answer_sheet($pdf, $filedir, $stinfo, $logofilepath, $path, $fileimg, $course, $examname, $answers, $attemptid) { global $CFG; require_once $CFG->dirroot . '/mod/assign/feedback/editpdf/fpdi/fpdi2tcpdf_bridge.php'; require_once $CFG->dirroot . '/mod/assign/feedback/editpdf/fpdi/fpdi.php'; if ($answers == null) { return; } $answerspdffilename = $filedir . '/answer' . random_string(15) . '.pdf'; $answerspdf = emarking_create_omr_answer_sheet($answers); $answerspdf->Output($answerspdffilename, 'F'); $pdf->setSourceFile($answerspdffilename); $tplidx = $pdf->ImportPage(1); $s = $pdf->getTemplatesize($tplidx); $pdf->AddPage('P', array($s['w'], $s['h'])); $pdf->useTemplate($tplidx); if ($path) { $pdf->setSourceFile($path); } $top = 50; $left = 10; $width = $s['w'] - 20; $height = $s['h'] - 65; // Corners $style = array('width' => 0.25, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)); $pdf->Circle($left, $top, 9, 0, 360, 'F', $style, array(0, 0, 0)); $pdf->Circle($left, $top, 4, 0, 360, 'F', $style, array(255, 255, 255)); $pdf->Circle($left + $width, $top, 9, 0, 360, 'F', $style, array(0, 0, 0)); $pdf->Circle($left + $width, $top, 4, 0, 360, 'F', $style, array(255, 255, 255)); $pdf->Circle($left, $top + $height, 9, 0, 360, 'F', $style, array(0, 0, 0)); $pdf->Circle($left, $top + $height, 4, 0, 360, 'F', $style, array(255, 255, 255)); $pdf->Circle($left + $width, $top + $height, 9, 0, 360, 'F', $style, array(0, 0, 0)); $pdf->Circle($left + $width, $top + $height, 4, 0, 360, 'F', $style, array(255, 255, 255)); emarking_draw_header($pdf, $stinfo, $examname, 1, $fileimg, $logofilepath, $course, null, false, true, $attemptid); }