public function upload_preview()
 {
     global $CFG;
     $imagefolder = $CFG->dataroot . '/temp/assignsubmission_pdf/img';
     if (!check_dir_exists($imagefolder, true, true)) {
         echo "Unable to create temporary image folder";
         die;
     }
     $mform = new edit_templates_form();
     $fname = $mform->save_temp_file('preview');
     if (!$fname) {
         return;
     }
     $pdf = new AssignPDFLib();
     $pdf->load_pdf($fname);
     $pdf->set_image_folder($imagefolder);
     $imgname = $pdf->get_image(1);
     $context = context_course::instance($this->courseid);
     $fs = get_file_storage();
     if ($oldfile = $fs->get_file($context->id, 'assignsubmission_pdf', 'previewimage', 0, '/', 'preview.png')) {
         $oldfile->delete();
     }
     $imginfo = array('contextid' => $context->id, 'component' => 'assignsubmission_pdf', 'filearea' => 'previewimage', 'itemid' => 0, 'filepath' => '/', 'filename' => 'preview.png');
     $fs->create_file_from_pathname($imginfo, $imagefolder . '/' . $imgname);
     // Copy the image into the file storage.
     // Delete the temporary files.
     unlink($fname);
     unlink($imagefolder . '/' . $imgname);
     $this->imagetime = time();
 }
Example #2
0
 /**
  * Generate a page image (if it doesn't exist already) and return the details
  * @param int $pageno
  * @param object $submission
  * @return array|mixed - [ image url, width, height, total page count ]
  */
 protected function get_page_image($pageno, $submission)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/mod/assign/feedback/pdf/mypdflib.php';
     require_once $CFG->dirroot . '/mod/assign/submission/pdf/lib.php';
     $pagefilename = 'page' . $pageno . '.png';
     $pdf = new AssignPDFLib();
     $pagecount = $submission->numpages;
     $context = $this->assignment->get_context();
     $fs = get_file_storage();
     $subfile = $fs->get_file($context->id, 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_FINAL, $submission->id, $this->get_subfolder(), ASSIGNSUBMISSION_PDF_FILENAME);
     if (!$subfile) {
         throw new moodle_exception('errornosubmission', 'assignfeedback_pdf');
     }
     // If pagecount is 0, then we need to skip down to the next stage to find the real page count.
     if ($pagecount && ($file = $fs->get_file($context->id, 'assignfeedback_pdf', ASSIGNFEEDBACK_PDF_FA_IMAGE, $submission->id, $this->get_subfolder(), $pagefilename))) {
         if ($file->get_timemodified() < $subfile->get_timemodified()) {
             // Check the image file was last generated before the most recent PDF was generated.
             $file->delete();
         } else {
             if ($ret = self::get_image_details($file, $pagecount)) {
                 return $ret;
             }
         }
         // If the image is bad in some way, try to create a new image instead.
     }
     // Generate the image.
     $tempfolder = $this->get_temp_folder($submission->id);
     $imagefolder = $tempfolder . 'img';
     if (!file_exists($imagefolder)) {
         if (!mkdir($imagefolder, 0777, true)) {
             $errdata = (object) array('temparea' => $imagefolder);
             throw new moodle_exception('errortempfolder', 'assignfeedback_pdf', '', null, $errdata);
         }
     }
     $pdffolder = $tempfolder . 'sub';
     $pdffile = $pdffolder . '/submission.pdf';
     if (!file_exists($pdffolder)) {
         if (!mkdir($pdffolder, 0777, true)) {
             $errdata = (object) array('temparea' => $pdffolder);
             throw new moodle_exception('errortempfolder', 'assignfeedback_pdf', '', null, $errdata);
         }
     }
     $subfile->copy_content_to($pdffile);
     // Copy the PDF out of the file storage, into the temp area.
     $pagecount = $pdf->set_pdf($pdffile, $pagecount);
     // Only loads the PDF if the pagecount is unknown (0).
     if (!$submission->numpages && $pagecount) {
         // Save the pagecount for future reference.
         $submission->numpages = $pagecount;
         $DB->set_field('assignsubmission_pdf', 'numpages', $pagecount, array('submission' => $submission->id));
     }
     if ($pageno > $pagecount) {
         @unlink($pdffile);
         @rmdir($imagefolder);
         @rmdir($pdffolder);
         @rmdir($tempfolder);
         return array(null, 0, 0, $pagecount);
     }
     $pdf->set_image_folder($imagefolder);
     if (!($imgname = $pdf->get_image($pageno))) {
         // Generate the image in the temp area.
         throw new moodle_exception('errorgenerateimage', 'assignfeedback_pdf');
     }
     $imginfo = array('contextid' => $context->id, 'component' => 'assignfeedback_pdf', 'filearea' => ASSIGNFEEDBACK_PDF_FA_IMAGE, 'itemid' => $submission->id, 'filepath' => $this->get_subfolder(), 'filename' => $pagefilename);
     $subfile = $fs->create_file_from_pathname($imginfo, $imagefolder . '/' . $imgname);
     // Copy the image into the file storage.
     // Delete the temporary files.
     @unlink($pdffile);
     @unlink($imagefolder . '/' . $imgname);
     @rmdir($imagefolder);
     @rmdir($pdffolder);
     @rmdir($tempfolder);
     if ($ret = self::get_image_details($subfile, $pagecount)) {
         return $ret;
     }
     return array(null, 0, 0, $pagecount);
 }
Example #3
0
 public static function test_gs_path($generateimage = true)
 {
     global $CFG;
     $ret = (object) array('status' => self::GSPATH_OK, 'message' => null);
     $gspath = get_config('assignfeedback_pdf', 'gspath');
     if (empty($gspath)) {
         $ret->status = self::GSPATH_EMPTY;
         return $ret;
     }
     if (!file_exists($gspath)) {
         $ret->status = self::GSPATH_DOESNOTEXIST;
         return $ret;
     }
     if (is_dir($gspath)) {
         $ret->status = self::GSPATH_ISDIR;
         return $ret;
     }
     if (!is_executable($gspath)) {
         $ret->status = self::GSPATH_NOTEXECUTABLE;
         return $ret;
     }
     $testfile = $CFG->dirroot . '/mod/assign/feedback/pdf/testgs.pdf';
     if (!file_exists($testfile)) {
         $ret->status = self::GSPATH_NOTESTFILE;
         return $ret;
     }
     if (!$generateimage) {
         return $ret;
     }
     $testimagefolder = $CFG->dataroot . '/temp/assignfeedback_pdf_test';
     @unlink($testimagefolder . '/image_page1.png');
     // Delete any previous test images.
     check_dir_exists($testimagefolder, true, true);
     $pdf = new AssignPDFLib();
     $pdf->set_pdf($testfile);
     $pdf->set_image_folder($testimagefolder);
     try {
         $pdf->get_image(1);
     } catch (moodle_exception $e) {
         $ret->status = self::GSPATH_ERROR;
         $ret->message = $e->getMessage();
     }
     return $ret;
 }
 public function test_create_image()
 {
     $pdf = new AssignPDFLib();
     $pdf->set_pdf(self::pdf_path('1'));
     $pdf->set_image_folder($this->tempdir);
     $imagefile = $pdf->get_image(1);
     // First page.
     $this->assertFileExists($this->tempdir . '/' . $imagefile);
     $imagefile = $pdf->get_image(10);
     // Somewhere in the middle page.
     $this->assertFileExists($this->tempdir . '/' . $imagefile);
     $imagefile = $pdf->get_image(17);
     // Last page.
     $this->assertFileExists($this->tempdir . '/' . $imagefile);
 }