/** * Save & preprocess the files and trigger plagiarism plugin, if enabled, to scan the uploaded files via events trigger * * @global stdClass $USER * @global moodle_database $DB * @param stdClass $submission * @param stdClass $data * @return bool */ public function save(stdClass $submission, stdClass $data) { global $USER, $DB, $SESSION; // Pre-process all files to convert to useful PDF format. $fileoptions = $this->get_file_options(); file_postupdate_standard_filemanager($data, 'pdfs', $fileoptions, $this->assignment->get_context(), 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_DRAFT, $submission->id); $pdfsubmission = $this->get_pdf_submission($submission->id); // Plagiarism code event trigger when files are uploaded. $fs = get_file_storage(); $files = $fs->get_area_files($this->assignment->get_context()->id, 'assignsubmission_pdf', ASSIGNSUBMISSION_PDF_FA_DRAFT, $submission->id, "id", false); // Check all files are PDF v1.4 or less. $submissionok = true; foreach ($files as $key => $file) { if (!AssignPDFLib::ensure_pdf_compatible($file)) { $filename = $file->get_filename(); $file->delete(); unset($files[$key]); if (!isset($SESSION->assignsubmission_pdf_invalid)) { $SESSION->assignsubmission_pdf_invalid = array(); } $SESSION->assignsubmission_pdf_invalid[] = $filename; $submissionok = false; } } if (!$submissionok) { return false; } $templatedata = null; if (isset($data->pdf_template)) { $templatedata = serialize($data->pdf_template); } $count = $this->count_files($submission->id, ASSIGNSUBMISSION_PDF_FA_DRAFT); // Send files to event system. // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection). $eventdata = new stdClass(); $eventdata->modulename = 'assign'; $eventdata->cmid = $this->assignment->get_course_module()->id; $eventdata->itemid = $submission->id; $eventdata->courseid = $this->assignment->get_course()->id; $eventdata->userid = $USER->id; if ($count > 1) { $eventdata->files = $files; // This is depreceated - please use pathnamehashes instead! } $eventdata->file = $files; // This is depreceated - please use pathnamehashes instead! $eventdata->pathnamehashes = array_keys($files); events_trigger('assessable_file_uploaded', $eventdata); if ($pdfsubmission) { $pdfsubmission->numpages = 0; $pdfsubmission->templatedata = $templatedata; $DB->update_record('assignsubmission_pdf', $pdfsubmission); } else { $pdfsubmission = new stdClass(); $pdfsubmission->submission = $submission->id; $pdfsubmission->assignment = $this->assignment->get_instance()->id; $pdfsubmission->numpages = 0; $pdfsubmission->templatedata = $templatedata; $DB->insert_record('assignsubmission_pdf', $pdfsubmission); } if (!$this->assignment->get_instance()->submissiondrafts) { // No 'submit assignment' button - need to submit immediately. $this->submit_for_grading($submission); } return true; }
public function test_ensure_pdf_compatible() { $fs = get_file_storage(); // Test a file that is already PDF version 1.4. $pdf = new AssignPDFLib(); $filerecord = array('contextid' => context_system::instance()->id, 'component' => 'assignfeedback_pdf', 'filearea' => 'phpunit', 'itemid' => 0, 'filepath' => '/', 'filename' => 'testfileok.pdf'); $fileok = $fs->create_file_from_pathname($filerecord, self::pdf_path('1')); $result = $pdf->ensure_pdf_compatible($fileok); $this->assertEquals(true, $result); // Checking has gone OK. $outfile = $this->tempdir . '/pdfout.pdf'; $fileok->copy_content_to($outfile); $pagecount = $pdf->load_pdf($outfile); $this->assertEquals(17, $pagecount); // File can be loaded by the pdf library. // Test a file that is PDF version 1.5. $pdf = new AssignPDFLib(); $filerecord = array('contextid' => context_system::instance()->id, 'component' => 'assignfeedback_pdf', 'filearea' => 'phpunit', 'itemid' => 0, 'filepath' => '/', 'filename' => 'testfileconvert.pdf'); $fileconvert = $fs->create_file_from_pathname($filerecord, self::pdf_path('convert')); $result = $pdf->ensure_pdf_compatible($fileconvert); $this->assertEquals(true, $result); // Checking has gone OK. $outfile = $this->tempdir . '/pdfout.pdf'; $fileconvert->copy_content_to($outfile); $pagecount = $pdf->load_pdf($outfile); $this->assertEquals(3, $pagecount); // File can be loaded by the pdf library. }