/**
  * Generate a new PDF with the original submission + annotations
  * @param $submissionid
  * @return bool - true if successful
  */
 public function create_response_pdf($submissionid)
 {
     global $DB;
     $context = $this->assignment->get_context();
     $fs = get_file_storage();
     $file = $fs->get_file($context->id, 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_FINAL, $submissionid, $this->get_subfolder(), ASSIGNSUBMISSION_PDF_FILENAME);
     if (!$file) {
         throw new moodle_exception('errornosubmission2', 'assignfeedback_pdf');
     }
     $temparea = $this->get_temp_folder($submissionid) . 'sub';
     if (!file_exists($temparea)) {
         if (!mkdir($temparea, 0777, true)) {
             throw new moodle_exception('errortempfolder', 'assignfeedback_pdf');
         }
     }
     $sourcefile = $temparea . '/submission.pdf';
     $destfile = $temparea . '/response.pdf';
     $file->copy_content_to($sourcefile);
     $mypdf = new AssignPDFLib();
     $mypdf->load_pdf($sourcefile);
     $comments = $DB->get_records('assignfeedback_pdf_cmnt', array('submissionid' => $submissionid), 'pageno');
     $annotations = $DB->get_records('assignfeedback_pdf_annot', array('submissionid' => $submissionid), 'pageno');
     if ($comments) {
         $comment = current($comments);
     } else {
         $comment = false;
     }
     if ($annotations) {
         $annotation = current($annotations);
     } else {
         $annotation = false;
     }
     while (true) {
         if ($comment) {
             $nextpage = $comment->pageno;
             if ($annotation) {
                 if ($annotation->pageno < $nextpage) {
                     $nextpage = $annotation->pageno;
                 }
             }
         } else {
             if ($annotation) {
                 $nextpage = $annotation->pageno;
             } else {
                 break;
             }
         }
         while ($nextpage > $mypdf->current_page()) {
             if (!$mypdf->copy_page()) {
                 break 2;
             }
         }
         while ($comment && $comment->pageno == $mypdf->current_page()) {
             $mypdf->add_comment($comment->rawtext, $comment->posx, $comment->posy, $comment->width, $comment->colour);
             $comment = next($comments);
         }
         while ($annotation && $annotation->pageno == $mypdf->current_page()) {
             if ($annotation->type == 'freehand') {
                 $path = explode(',', $annotation->path);
                 $mypdf->add_annotation(0, 0, 0, 0, $annotation->colour, 'freehand', $path);
             } else {
                 $mypdf->add_annotation($annotation->startx, $annotation->starty, $annotation->endx, $annotation->endy, $annotation->colour, $annotation->type, $annotation->path);
             }
             $annotation = next($annotations);
         }
     }
     $mypdf->copy_remaining_pages();
     $mypdf->save_pdf($destfile);
     // Delete any previous response file.
     if ($file = $fs->get_file($context->id, 'assignfeedback_pdf', ASSIGNFEEDBACK_PDF_FA_RESPONSE, $submissionid, $this->get_subfolder(), ASSIGNFEEDBACK_PDF_FILENAME)) {
         $file->delete();
     }
     $fileinfo = array('contextid' => $context->id, 'component' => 'assignfeedback_pdf', 'filearea' => ASSIGNFEEDBACK_PDF_FA_RESPONSE, 'itemid' => $submissionid, 'filepath' => $this->get_subfolder(), 'filename' => ASSIGNFEEDBACK_PDF_FILENAME);
     $fs->create_file_from_pathname($fileinfo, $destfile);
     @unlink($sourcefile);
     @unlink($destfile);
     @rmdir($temparea);
     @rmdir(dirname($temparea));
     return true;
 }
 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();
 }
 public function test_annotating_pdf()
 {
     $pdf = new AssignPDFLib();
     $pdf->load_pdf(self::pdf_path('1'));
     // Add comments + annotations to page 1.
     $pdf->copy_page();
     $pdf->add_comment('Test comment', 100, 100, 150, 'yellow');
     $pdf->add_comment('Test comment2', 100, 150, 100, 'green');
     $pdf->add_annotation(90, 90, 110, 110, 'red', 'line');
     $pdf->add_annotation(140, 140, 230, 230, 'yellow', 'oval');
     $pdf->add_annotation(150, 150, 110, 90, 'green', 'rectangle');
     $pdf->add_annotation(160, 160, 170, 170, 'black', 'stamp', 'smile');
     $pdf->add_annotation(200, 200, 250, 250, 'blue', 'highlight');
     $pdf->add_annotation(0, 0, 0, 0, 'white', 'freehand', array(10, 10, 20, 20, 40, 40, 35, 40));
     // Add comments + annotations to page 2.
     $pdf->copy_page();
     $pdf->add_comment('Comment on page 2', 140, 200, 100, 'clear');
     $pdf->add_annotation(40, 40, 250, 250, 'yellow', 'line');
     // Copy the rest of the pages.
     $pdf->copy_remaining_pages();
     // Save the PDF and check it exists.
     $outfile = $this->tempdir . '/outfile.pdf';
     $pdf->save_pdf($outfile);
     $this->assertFileExists($outfile);
     // Load the PDF and check it has the right number of pages.
     $pdf = new AssignPDFLib();
     $size = $pdf->load_pdf($outfile);
     $this->assertEquals(17, $size);
 }