function create_response_pdf($submissionid) { global $DB; $fs = get_file_storage(); if (!($file = $fs->get_file($this->context->id, 'mod_assignment', 'submissionfinal', $submissionid, '/', 'submission.pdf'))) { print_error('Submitted PDF not found'); return false; } $temparea = $this->get_temp_folder($submissionid) . 'sub'; if (!file_exists($temparea)) { if (!mkdir($temparea, 0777, true)) { echo "Unable to create temporary folder"; die; } } $sourcefile = $temparea . '/submission.pdf'; $destfile = $temparea . '/response.pdf'; $file->copy_content_to($sourcefile); $mypdf = new MyPDFLib(); $mypdf->load_pdf($sourcefile); $comments = $DB->get_records('assignment_uploadpdf_comment', array('assignment_submission' => $submissionid), 'pageno'); $annotations = $DB->get_records('assignment_uploadpdf_annot', array('assignment_submission' => $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($this->context->id, 'mod_assignment', 'response', $submissionid, '/', 'response.pdf')) { $file->delete(); } $fileinfo = array('contextid' => $this->context->id, 'component' => 'mod_assignment', 'filearea' => 'response', 'itemid' => $submissionid, 'filename' => 'response.pdf', 'filepath' => '/'); $fs->create_file_from_pathname($fileinfo, $destfile); @unlink($sourcefile); @unlink($destfile); @rmdir($temparea); @rmdir(dirname($temparea)); return true; }
function create_response_pdf($userid, $submissionid) { global $CFG; $filearea = $CFG->dataroot . '/' . $this->file_area_name($userid); $sourcearea = $filearea . '/submission'; $sourcefile = $sourcearea . '/submission.pdf'; if (!is_dir($sourcearea) || !file_exists($sourcefile)) { error('Submitted PDF not found'); return false; } $destarea = $filearea . '/responses'; $destfile = $destarea . '/response.pdf'; check_dir_exists($destarea, true, true); $mypdf = new MyPDFLib(); $mypdf->load_pdf($sourcefile); $comments = get_records('assignment_uploadpdf_comment', 'assignment_submission', $submissionid, 'pageno'); $annotations = get_records('assignment_uploadpdf_annot', 'assignment_submission', $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()) { error('Ran out of pages - this should not happen! - comment.pageno = ' . $comment->pageno . '; currrentpage = ' . $mypdf->CurrentPage()); return false; } } 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 = next($annotations); } } $mypdf->copy_remaining_pages(); $mypdf->save_pdf($destfile); return true; }
delete_records('assignment_uploadpdf_tmplitm', 'id', $itemid); $itemid = 0; } } } elseif ($uploadpreview) { $partdest = $courseid . IMAGE_PATH; $fulldest = $CFG->dataroot . '/' . $partdest; check_dir_exists($fulldest); require_once $CFG->dirroot . '/lib/uploadlib.php'; $um = new upload_manager('preview', false, false, $course, false, 0, true); if ($um->process_file_uploads($partdest)) { $fp = $um->get_new_filepath(); $fn = $um->get_new_filename(); require_once 'mypdflib.php'; $pdf = new MyPDFLib(); $pdf->load_pdf($fp); $pdf->set_image_folder($fulldest); $imagename = $pdf->get_image(1); unlink($fp); } else { //echo 'No file uploaded'; //die; } } elseif ($duplicatetemplate) { // Should not have access to the 'duplicate' button unless a template is selected // but, just in case, we check here (but just do nothing if that is not the case) if ($templateid != -1) { $template = get_record('assignment_uploadpdf_tmpl', 'id', $templateid); if (!$template) { error("Old template not found"); }
function upload_preview() { global $CFG; $imagefolder = $CFG->dataroot . '/temp/uploadpdf/img'; if (!file_exists($imagefolder)) { if (!mkdir($imagefolder, 0777, 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 MyPDFLib(); $pdf->load_pdf($fname); $pdf->set_image_folder($imagefolder); $imgname = $pdf->get_image(1); $context = get_context_instance(CONTEXT_COURSE, $this->courseid); $fs = get_file_storage(); if ($oldfile = $fs->get_file($context->id, 'mod_assignment', 'previewimage', 0, '/', 'preview.png')) { $oldfile->delete(); } $imginfo = array('contextid' => $context->id, 'component' => 'mod_assignment', 'filearea' => 'previewimage', 'itemid' => 0, 'filepath' => '/', 'filename' => 'preview.png'); $file = $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(); }