Example #1
0
 /**
  * This function will generate and return a list of the page images from a pdf.
  * @param int|\assign $assignment
  * @param int $userid
  * @param int $attemptnumber (-1 means latest attempt)
  * @return array(stored_file)
  */
 public static function generate_page_images_for_attempt($assignment, $userid, $attemptnumber)
 {
     global $CFG;
     require_once $CFG->libdir . '/pdflib.php';
     $assignment = self::get_assignment_from_param($assignment);
     if (!$assignment->can_view_submission($userid)) {
         \print_error('nopermission');
     }
     // Need to generate the page images - first get a combined pdf.
     $file = self::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
     if (!$file) {
         throw new \moodle_exception('Could not generate combined pdf.');
     }
     $tmpdir = \make_temp_directory('assignfeedback_editpdf/pageimages/' . self::hash($assignment, $userid, $attemptnumber));
     $combined = $tmpdir . '/' . self::COMBINED_PDF_FILENAME;
     $file->copy_content_to($combined);
     // Copy the file.
     $pdf = new pdf();
     $pdf->set_image_folder($tmpdir);
     $pagecount = $pdf->set_pdf($combined);
     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
     $record = new \stdClass();
     $record->contextid = $assignment->get_context()->id;
     $record->component = 'assignfeedback_editpdf';
     $record->filearea = self::PAGE_IMAGE_FILEAREA;
     $record->itemid = $grade->id;
     $record->filepath = '/';
     $fs = \get_file_storage();
     // Remove the existing content of the filearea.
     $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid);
     $files = array();
     for ($i = 0; $i < $pagecount; $i++) {
         $image = $pdf->get_image($i);
         $record->filename = basename($image);
         $files[$i] = $fs->create_file_from_pathname($record, $tmpdir . '/' . $image);
         @unlink($tmpdir . '/' . $image);
     }
     $pdf->Close();
     // PDF loaded and never saved/outputted needs to be closed.
     @unlink($combined);
     @rmdir($tmpdir);
     return $files;
 }