function create_submission_pdf($submissionid, $template)
 {
     $fs = get_file_storage();
     $mypdf = new MyPDFLib();
     $temparea = $this->get_temp_folder($submissionid);
     $destfile = $temparea . 'sub/submission.pdf';
     if (!file_exists($temparea) || !file_exists($temparea . 'sub')) {
         if (!mkdir($temparea . 'sub', 0777, true)) {
             echo "Unable to create temporary folder";
             die;
         }
     }
     $combine_files = array();
     /** @var $files stored_file[] */
     if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submissionid, 'timemodified', false)) {
         foreach ($files as $key => $file) {
             if ($file->get_mimetype() != 'application/pdf') {
                 $fs->create_file_from_storedfile(array('component' => 'mod_assignment', 'filearea' => 'submissionfinal'), $file);
                 /* Copy any non-PDF files to submission filearea */
                 unset($files[$key]);
             } else {
                 $destpath = $temparea . $file->get_filename();
                 if ($file->copy_content_to($destpath)) {
                     $combine_files[] = $destpath;
                 } else {
                     return 0;
                 }
             }
         }
         if (!empty($combine_files)) {
             /* Should have already checked there is at least 1 PDF */
             $coversheet_path = null;
             $coversheet = $fs->get_area_files($this->context->id, 'mod_assignment', 'coversheet', false, '', false);
             if (!empty($coversheet)) {
                 $coversheet = array_shift(array_values($coversheet));
                 $coversheet_path = $temparea . 'sub/coversheet.pdf';
                 if (!$coversheet->copy_content_to($coversheet_path)) {
                     return 0;
                 }
             }
             if (!($pagecount = $mypdf->combine_pdfs($combine_files, $destfile, $coversheet_path, $template))) {
                 return 0;
             }
             $fileinfo = array('contextid' => $this->context->id, 'component' => 'mod_assignment', 'filearea' => 'submissionfinal', 'itemid' => $submissionid, 'filename' => 'submission.pdf', 'filepath' => '/');
             $fs->create_file_from_pathname($fileinfo, $destfile);
             unlink($destfile);
             if ($coversheet_path != null) {
                 unlink($coversheet_path);
             }
             foreach ($combine_files as $fl) {
                 unlink($fl);
             }
             // Try to clean up the temporary folder.
             @rmdir($temparea . 'sub');
             @rmdir($temparea);
             return $pagecount;
         } else {
             return 0;
         }
     }
     return 0;
 }
 function create_submission_pdf($userid, $template)
 {
     global $CFG;
     $filearea = $CFG->dataroot . '/' . $this->file_area_name($userid);
     $destarea = $filearea . '/submission';
     check_dir_exists($destarea, true, true);
     $destfile = $destarea . '/submission.pdf';
     $mypdf = new MyPDFLib();
     if (is_dir($filearea)) {
         if ($files = get_directory_list($filearea, array('responses', 'submission', 'images'))) {
             foreach ($files as $key => $fl) {
                 if (mimeinfo('type', $fl) != 'application/pdf') {
                     copy($filearea . '/' . $fl, $filearea . '/submission/' . $fl);
                     /* Copy any non-PDF files to submission folder */
                     unset($files[$key]);
                 }
             }
             if (count($files) > 0) {
                 /* Should have already checked there is at least 1 PDF */
                 $coversheet = null;
                 $extra = get_record('assignment_uploadpdf', 'assignment', $this->assignment->id);
                 if ($extra) {
                     if (!$extra->coversheet) {
                         $coversheet = null;
                     } else {
                         $coversheet = $CFG->dataroot . '/' . $this->course->id . '/' . $extra->coversheet;
                         if (!file_exists($coversheet)) {
                             // FIXME - Add a meaningful error message onto the screen at this point!
                             $coversheet = null;
                         }
                     }
                 }
                 return $mypdf->combine_pdfs($filearea, $files, $destfile, $coversheet, $template);
             } else {
                 return 0;
             }
         }
     }
     return 0;
 }