function get_page_image($pageno, $submission)
 {
     global $CFG, $DB;
     $pagefilename = 'page' . $pageno . '.png';
     $pdf = new MyPDFLib();
     $pagecount = $submission->numfiles >> 1;
     // Extract the pagecount from 'numfiles' (may be 0)
     $fs = get_file_storage();
     // If pagecount is 0, then we need to skip down to the next stage to find the real page count
     if ($pagecount && ($file = $fs->get_file($this->context->id, 'mod_assignment', 'image', $submission->id, '/', $pagefilename))) {
         if ($imageinfo = $file->get_imageinfo()) {
             $imgurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/image/' . $submission->id . '/' . $pagefilename);
             // Prevent browser from caching image if it has changed
             if (strpos($imgurl, '?') === false) {
                 $imgurl .= '?ts=' . $file->get_timemodified();
             } else {
                 $imgurl .= '&ts=' . $file->get_timemodified();
             }
             return array($imgurl, $imageinfo['width'], $imageinfo['height'], $pagecount);
         }
         // If the image is bad in some way, try to create a new image instead
     }
     // Generate the image
     $tempfolder = $this->get_temp_folder($submission->id);
     $imagefolder = $tempfolder . 'img';
     if (!file_exists($imagefolder)) {
         if (!mkdir($imagefolder, 0777, true)) {
             echo "Unable to create temporary image folder";
             die;
         }
     }
     $pdffolder = $tempfolder . 'sub';
     $pdffile = $pdffolder . '/submission.pdf';
     if (!file_exists($pdffolder)) {
         if (!mkdir($pdffolder, 0777, true)) {
             echo "Unable to create temporary folder";
             die;
         }
     }
     if (!($file = $fs->get_file($this->context->id, 'mod_assignment', 'submissionfinal', $submission->id, '/', 'submission.pdf'))) {
         print_error('Attempting to display image for non-existant submission');
     }
     $file->copy_content_to($pdffile);
     // Copy the PDF out of the file storage, into the temp area
     $pagecount = $pdf->set_pdf($pdffile, $pagecount);
     // Only loads the PDF if the pagecount is unknown (0)
     if ($pageno > $pagecount) {
         unlink($pdffile);
         return array(null, 0, 0, $pagecount);
     }
     $pdf->set_image_folder($imagefolder);
     if (!($imgname = $pdf->get_image($pageno))) {
         // Generate the image in the temp area
         print_error(get_string('errorgenerateimage', 'assignment_uploadpdf'));
     }
     if (($submission->numfiles & 1) == 0) {
         $submission->numfiles = $pagecount << 1 | 1;
         // Use this as a flag that there are images to delete at some point
         // Maybe switch to just searching the filestorage database to find old images?
         $updated = new stdClass();
         $updated->id = $submission->id;
         $updated->numfiles = $submission->numfiles;
         $DB->update_record('assignment_submissions', $updated);
     }
     $imginfo = array('contextid' => $this->context->id, 'component' => 'mod_assignment', 'filearea' => 'image', 'itemid' => $submission->id, 'filepath' => '/', 'filename' => $pagefilename);
     $file = $fs->create_file_from_pathname($imginfo, $imagefolder . '/' . $imgname);
     // Copy the image into the file storage
     //Delete the temporary files
     unlink($pdffile);
     unlink($imagefolder . '/' . $imgname);
     rmdir($imagefolder);
     rmdir($pdffolder);
     rmdir($tempfolder);
     if ($imageinfo = $file->get_imageinfo()) {
         $imgurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/mod_assignment/image/' . $submission->id . '/' . $pagefilename);
         // Prevent browser from caching image if it has changed
         if (strpos($imgurl, '?') === false) {
             $imgurl .= '?ts=' . $file->get_timemodified();
         } else {
             $imgurl .= '&amp;ts=' . $file->get_timemodified();
         }
         return array($imgurl, $imageinfo['width'], $imageinfo['height'], $pagecount);
     }
     return array(null, 0, 0, $pagecount);
 }
 function get_page_image($userid, $pageno, $submission)
 {
     global $CFG;
     $imagefolder = $CFG->dataroot . '/' . $this->file_area_name($userid) . '/images';
     check_dir_exists($imagefolder, true, true);
     $pdffile = $CFG->dataroot . '/' . $this->file_area_name($userid) . '/submission/submission.pdf';
     // Check folder exists + file exists
     if (!file_exists($pdffile)) {
         error('Attempting to display image for non-existing submission');
     }
     $pdf = new MyPDFLib();
     $pdf->set_image_folder($imagefolder);
     $pagecount = $submission->numfiles >> 1;
     // Extract the pagecount from 'numfiles' (may be 0)
     $pagecount = $pdf->set_pdf($pdffile, $pagecount);
     // Only loads the PDF if the pagecount is unknown (0)
     if ($pageno > $pagecount) {
         return array(null, 0, 0, $pagecount);
     }
     if (!($imgname = $pdf->get_image($pageno))) {
         error(get_string('errorgenerateimage', 'assignment_uploadpdf'));
     }
     if (($submission->numfiles & 1) == 0) {
         $submission->numfiles = $pagecount << 1 | 1;
         /* Use this as a flag that there are images to delete at some point */
         $updated = new Object();
         $updated->id = $submission->id;
         $updated->numfiles = $submission->numfiles;
         update_record('assignment_submissions', $updated);
     }
     $imageurl = $CFG->wwwroot . '/file.php?file=/' . $this->file_area_name($userid) . '/images/' . $imgname;
     list($imgwidth, $imgheight, $imgtype, $imgattr) = getimagesize($CFG->dataroot . '/' . $this->file_area_name($userid) . '/images/' . $imgname);
     return array($imageurl, $imgwidth, $imgheight, $pagecount);
 }
        }
    }
} 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");
        }
        $template->course = $courseid;
        $template->name = $template->name . get_string('templatecopy', 'assignment_uploadpdf');
 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();
 }