Exemplo n.º 1
0
 protected function create_submission_pdf(stdClass $submission)
 {
     global $DB;
     $fs = get_file_storage();
     $context = $this->assignment->get_context();
     $coversheet = null;
     $templateitems = null;
     // Create a the required temporary folders.
     $temparea = $this->get_temp_folder($submission->id);
     $tempdestarea = $temparea . 'sub';
     $destfile = $tempdestarea . '/' . ASSIGNSUBMISSION_PDF_FILENAME;
     if (!file_exists($temparea) || !file_exists($tempdestarea)) {
         if (!mkdir($temparea, 0777, true) || !mkdir($tempdestarea, 0777, true)) {
             $errdata = (object) array('temparea' => $temparea, 'tempdestarea' => $tempdestarea);
             throw new moodle_exception('errortempfolder', 'assignsubmission_pdf', '', null, $errdata);
         }
     }
     // Get the coversheet details and copy the file to the temporary folder.
     $coversheetfiles = $fs->get_area_files($context->id, 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_COVERSHEET, false, '', false);
     if ($coversheetfiles) {
         $coversheetfile = reset($coversheetfiles);
         // Only ever one coversheet file.
         $coversheet = $tempdestarea . '/coversheet.pdf';
         if (!$coversheetfile->copy_content_to($coversheet)) {
             $errdata = (object) array('coversheet' => $coversheet);
             throw new moodle_exception('errorcoversheet', 'assignsubmission_pdf', '', null, $errdata);
         }
         if ($templateid = $this->get_config('templateid')) {
             $templateitems = $DB->get_records('assignsubmission_pdf_tmplit', array('templateid' => $templateid));
             $templatedata = $DB->get_field('assignsubmission_pdf', 'templatedata', array('submission' => $submission->id));
             if (!empty($templatedata)) {
                 $templatedata = unserialize($templatedata);
             }
             foreach ($templateitems as $item) {
                 if (isset($templatedata[$item->id])) {
                     $item->data = $templatedata[$item->id];
                 } else {
                     $item->data = '';
                 }
             }
         }
     }
     // Copy all the PDFs to the temporary folder.
     $files = $fs->get_area_files($context->id, 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_DRAFT, $submission->id, "sortorder, id", false);
     $combinefiles = array();
     foreach ($files as $file) {
         $destpath = $temparea . '/' . $file->get_contenthash();
         if (!$file->copy_content_to($destpath)) {
             throw new moodle_exception('errorcopyfile', 'assignsubmission_pdf', '', $file->get_filename());
         }
         $combinefiles[] = $destpath;
     }
     // Combine all the submitted files and the coversheet.
     $mypdf = new AssignPDFLib();
     $pagecount = $mypdf->combine_pdfs($combinefiles, $destfile, $coversheet, $templateitems);
     if (!$pagecount) {
         return 0;
         // No pages found in the submitted files - this shouldn't happen.
     }
     // Copy the combined file into the submission area.
     $fs->delete_area_files($context->id, 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_FINAL, $submission->id);
     $fileinfo = array('contextid' => $context->id, 'component' => 'assignsubmission_pdf', 'filearea' => ASSIGNSUBMISSION_PDF_FA_FINAL, 'itemid' => $submission->id, 'filename' => ASSIGNSUBMISSION_PDF_FILENAME, 'filepath' => $this->get_subfolder());
     $fs->create_file_from_pathname($fileinfo, $destfile);
     // Clean up all the temporary files.
     unlink($destfile);
     if ($coversheet) {
         unlink($coversheet);
     }
     foreach ($combinefiles as $combinefile) {
         unlink($combinefile);
     }
     @rmdir($tempdestarea);
     @rmdir($temparea);
     return $pagecount;
 }
Exemplo n.º 2
0
 public function test_combine_pdfs_coversheet_template()
 {
     $pdflist = array(self::pdf_path('1'), self::pdf_path('2'));
     $coversheet = self::pdf_path('cover');
     $outfile = $this->tempdir . '/testout.pdf';
     $templatefields = array((object) array('xpos' => 100, 'ypos' => 100, 'type' => 'text', 'width' => 150, 'data' => 'Testing text'), (object) array('xpos' => 100, 'ypos' => 120, 'type' => 'shorttext', 'data' => 'Testing some shorttext'), (object) array('xpos' => 100, 'ypos' => 140, 'type' => 'date', 'setting' => 'd/m/Y'));
     $this->assertFileNotExists($outfile);
     $pdf = new AssignPDFLib();
     $size = $pdf->combine_pdfs($pdflist, $outfile, $coversheet, $templatefields);
     $this->assertEquals(20, $size);
     $this->assertFileExists($outfile);
 }