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