Exemplo n.º 1
0
 */
require_once dirname(__FILE__) . '/../../../../config.php';
global $CFG, $PAGE, $OUTPUT;
require_once $CFG->dirroot . '/mod/assign/feedback/pdf/mypdflib.php';
$PAGE->set_url(new moodle_url('/mod/assign/feedback/pdf/testgs.php'));
$PAGE->set_context(context_system::instance());
require_login();
if (!is_siteadmin()) {
    die('Admin only');
}
if (optional_param('sendimage', false, PARAM_BOOL)) {
    // Serve the generated test image.
    AssignPDFLib::send_test_image();
    die;
}
$result = AssignPDFLib::test_gs_path();
switch ($result->status) {
    case AssignPDFLib::GSPATH_OK:
        $msg = get_string('test_ok', 'assignfeedback_pdf');
        $msg .= html_writer::empty_tag('br');
        $imgurl = new moodle_url($PAGE->url, array('sendimage' => 1));
        $msg .= html_writer::empty_tag('img', array('src' => $imgurl));
        break;
    case AssignPDFLib::GSPATH_ERROR:
        $msg = $result->message;
        break;
    default:
        $msg = get_string("test_{$result->status}", 'assignfeedback_pdf');
        break;
}
$returl = new moodle_url('/admin/settings.php', array('section' => 'assignfeedback_pdf'));
Exemplo n.º 2
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;
 }
 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();
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
0
 /**
  * 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;
 }
Exemplo n.º 6
0
 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);
 }