Merge branch 'MDL-49998' of git://github.com/stronk7/moodle

This commit is contained in:
David Monllao 2015-05-01 15:05:42 +08:00
commit 84d83fe7e1
3 changed files with 14 additions and 19 deletions

View file

@ -193,11 +193,7 @@ class document_services {
$files = self::list_compatible_submission_files_for_attempt($assignment, $userid, $attemptnumber); $files = self::list_compatible_submission_files_for_attempt($assignment, $userid, $attemptnumber);
$pdf = new pdf(); $pdf = new pdf();
if (!$files) { if ($files) {
// No valid submission files - create an empty pdf.
$pdf->AddPage();
} else {
// Create a mega joined PDF. // Create a mega joined PDF.
$compatiblepdfs = array(); $compatiblepdfs = array();
foreach ($files as $file) { foreach ($files as $file) {
@ -221,12 +217,11 @@ class document_services {
if ($pagecount == 0) { if ($pagecount == 0) {
// We at least want a single blank page. // We at least want a single blank page.
debugging('TCPDF did not produce a valid pdf:' . $tmpfile . '. Replacing with a blank pdf.', DEBUG_DEVELOPER); debugging('TCPDF did not produce a valid pdf:' . $tmpfile . '. Replacing with a blank pdf.', DEBUG_DEVELOPER);
$pdf = new pdf();
$pdf->AddPage();
@unlink($tmpfile); @unlink($tmpfile);
$files = false; $files = false;
} }
} }
$pdf->Close(); // No real need to close this pdf, because it has been saved by combine_pdfs(), but for clarity.
$grade = $assignment->get_user_grade($userid, true, $attemptnumber); $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
$record = new \stdClass(); $record = new \stdClass();
@ -243,18 +238,20 @@ class document_services {
// Detect corrupt generated pdfs and replace with a blank one. // Detect corrupt generated pdfs and replace with a blank one.
if ($files) { if ($files) {
$pagecount = $pdf->load_pdf($tmpfile); $verifypdf = new pdf();
$pagecount = $verifypdf->load_pdf($tmpfile);
if ($pagecount <= 0) { if ($pagecount <= 0) {
$files = false; $files = false;
} }
$verifypdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
} }
if (!$files) { if (!$files) {
// This was a blank pdf. // This was a blank pdf.
unset($pdf); $blankpdf = new pdf();
$pdf = new pdf(); $content = $blankpdf->Output(self::COMBINED_PDF_FILENAME, 'S');
$content = $pdf->Output(self::COMBINED_PDF_FILENAME, 'S');
$file = $fs->create_file_from_string($record, $content); $file = $fs->create_file_from_string($record, $content);
$blankpdf->Close(); // No real need to close this pdf, because it has been outputted, but for clarity.
} else { } else {
// This was a combined pdf. // This was a combined pdf.
$file = $fs->create_file_from_pathname($record, $tmpfile); $file = $fs->create_file_from_pathname($record, $tmpfile);
@ -314,6 +311,7 @@ class document_services {
// Get the total number of pages. // Get the total number of pages.
$pdf = new pdf(); $pdf = new pdf();
$pagecount = $pdf->set_pdf($combined); $pagecount = $pdf->set_pdf($combined);
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
// Delete temporary folders and files. // Delete temporary folders and files.
@unlink($combined); @unlink($combined);
@ -375,6 +373,7 @@ class document_services {
$files[$i] = $fs->create_file_from_pathname($record, $tmpdir . '/' . $image); $files[$i] = $fs->create_file_from_pathname($record, $tmpdir . '/' . $image);
@unlink($tmpdir . '/' . $image); @unlink($tmpdir . '/' . $image);
} }
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
@unlink($combined); @unlink($combined);
@rmdir($tmpdir); @rmdir($tmpdir);

View file

@ -485,13 +485,13 @@ class pdf extends \FPDI {
// PDF was not valid - try running it through ghostscript to clean it up. // PDF was not valid - try running it through ghostscript to clean it up.
$pagecount = 0; $pagecount = 0;
} }
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
if ($pagecount > 0) { if ($pagecount > 0) {
// Page is valid and can be read by tcpdf. // Page is valid and can be read by tcpdf.
return $tempsrc; return $tempsrc;
} }
$gsexec = \escapeshellarg($CFG->pathtogs); $gsexec = \escapeshellarg($CFG->pathtogs);
$tempdstarg = \escapeshellarg($tempdst); $tempdstarg = \escapeshellarg($tempdst);
$tempsrcarg = \escapeshellarg($tempsrc); $tempsrcarg = \escapeshellarg($tempsrc);
@ -511,6 +511,8 @@ class pdf extends \FPDI {
// PDF was not valid - try running it through ghostscript to clean it up. // PDF was not valid - try running it through ghostscript to clean it up.
$pagecount = 0; $pagecount = 0;
} }
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
if ($pagecount <= 0) { if ($pagecount <= 0) {
@unlink($tempdst); @unlink($tempdst);
// Could not parse the converted pdf. // Could not parse the converted pdf.
@ -572,6 +574,7 @@ class pdf extends \FPDI {
$ret->status = self::GSPATH_ERROR; $ret->status = self::GSPATH_ERROR;
$ret->message = $e->getMessage(); $ret->message = $e->getMessage();
} }
$pdf->Close(); // PDF loaded and never saved/outputted needs to be closed.
return $ret; return $ret;
} }

View file

@ -52,13 +52,6 @@ class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
parent::setUp(); parent::setUp();
} }
/**
* Tidy up open files that may be left open.
*/
protected function tearDown() {
gc_collect_cycles();
}
protected function create_assign_and_submit_pdf() { protected function create_assign_and_submit_pdf() {
global $CFG; global $CFG;
$assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1, $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1,