/** * This function will take all of the compatible files for a submission * and combine them into one PDF. * @param int|\assign $assignment * @param int $userid * @param int $attemptnumber (-1 means latest attempt) * @return stored_file */ public static function generate_combined_pdf_for_attempt($assignment, $userid, $attemptnumber) { global $CFG; require_once $CFG->libdir . '/pdflib.php'; $assignment = self::get_assignment_from_param($assignment); if (!$assignment->can_view_submission($userid)) { \print_error('nopermission'); } $files = self::list_compatible_submission_files_for_attempt($assignment, $userid, $attemptnumber); $pdf = new pdf(); if (!$files) { // No valid submission files - create an empty pdf. $pdf->AddPage(); } else { // Create a mega joined PDF. $compatiblepdfs = array(); foreach ($files as $file) { $compatiblepdf = pdf::ensure_pdf_compatible($file); if ($compatiblepdf) { array_push($compatiblepdfs, $compatiblepdf); } } $tmpdir = \make_temp_directory('assignfeedback_editpdf/combined/' . self::hash($assignment, $userid, $attemptnumber)); $tmpfile = $tmpdir . '/' . self::COMBINED_PDF_FILENAME; @unlink($tmpfile); $pagecount = $pdf->combine_pdfs($compatiblepdfs, $tmpfile); if ($pagecount == 0) { // We at least want a single blank page. $pdf->AddPage(); @unlink($tmpfile); $files = false; } } $grade = $assignment->get_user_grade($userid, true, $attemptnumber); $record = new \stdClass(); $record->contextid = $assignment->get_context()->id; $record->component = 'assignfeedback_editpdf'; $record->filearea = self::COMBINED_PDF_FILEAREA; $record->itemid = $grade->id; $record->filepath = '/'; $record->filename = self::COMBINED_PDF_FILENAME; $fs = \get_file_storage(); $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid); if (!$files) { // This was a blank pdf. $content = $pdf->Output(self::COMBINED_PDF_FILENAME, 'S'); $file = $fs->create_file_from_string($record, $content); } else { // This was a combined pdf. $file = $fs->create_file_from_pathname($record, $tmpfile); @unlink($tmpfile); } return $file; }
/** * This function will take all of the compatible files for a submission * and combine them into one PDF. * @param int|\assign $assignment * @param int $userid * @param int $attemptnumber (-1 means latest attempt) * @return stored_file */ public static function generate_combined_pdf_for_attempt($assignment, $userid, $attemptnumber) { global $CFG; require_once $CFG->libdir . '/pdflib.php'; $assignment = self::get_assignment_from_param($assignment); if (!$assignment->can_view_submission($userid)) { \print_error('nopermission'); } $files = self::list_compatible_submission_files_for_attempt($assignment, $userid, $attemptnumber); $pdf = new pdf(); if (!$files) { // No valid submission files - create an empty pdf. $pdf->AddPage(); } else { // Create a mega joined PDF. $compatiblepdfs = array(); foreach ($files as $file) { $compatiblepdf = pdf::ensure_pdf_compatible($file); if ($compatiblepdf) { array_push($compatiblepdfs, $compatiblepdf); } } $tmpdir = \make_temp_directory('assignfeedback_editpdf/combined/' . self::hash($assignment, $userid, $attemptnumber)); $tmpfile = $tmpdir . '/' . self::COMBINED_PDF_FILENAME; @unlink($tmpfile); try { $pagecount = $pdf->combine_pdfs($compatiblepdfs, $tmpfile); } catch (\Exception $e) { debugging('TCPDF could not process the pdf files:' . $e->getMessage(), DEBUG_DEVELOPER); // TCPDF does not recover from errors so we need to re-initialise the class. $pagecount = 0; } if ($pagecount == 0) { // We at least want a single blank page. debugging('TCPDF did not produce a valid pdf:' . $tmpfile . '. Replacing with a blank pdf.', DEBUG_DEVELOPER); $pdf = new pdf(); $pdf->AddPage(); @unlink($tmpfile); $files = false; } } $grade = $assignment->get_user_grade($userid, true, $attemptnumber); $record = new \stdClass(); $record->contextid = $assignment->get_context()->id; $record->component = 'assignfeedback_editpdf'; $record->filearea = self::COMBINED_PDF_FILEAREA; $record->itemid = $grade->id; $record->filepath = '/'; $record->filename = self::COMBINED_PDF_FILENAME; $fs = \get_file_storage(); $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid); // Detect corrupt generated pdfs and replace with a blank one. if ($files) { $pagecount = $pdf->load_pdf($tmpfile); if ($pagecount <= 0) { $files = false; } } if (!$files) { // This was a blank pdf. unset($pdf); $pdf = new pdf(); $content = $pdf->Output(self::COMBINED_PDF_FILENAME, 'S'); $file = $fs->create_file_from_string($record, $content); } else { // This was a combined pdf. $file = $fs->create_file_from_pathname($record, $tmpfile); @unlink($tmpfile); // Test the generated file for correctness. $compatiblepdf = pdf::ensure_pdf_compatible($file); } return $file; }