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; }