/**
  * Create a widget for rendering the editor.
  *
  * @param int $userid
  * @param stdClass $grade
  * @param bool $readonly
  * @return seplfeedback_editpdf_widget
  */
 public function get_widget($userid, $grade, $readonly)
 {
     $attempt = -1;
     if ($grade && $grade->attemptnumber) {
         $attempt = $grade->attemptnumber;
     } else {
         $grade = $this->seplment->get_user_grade($userid, true);
     }
     $feedbackfile = document_services::get_feedback_document($this->seplment->get_instance()->id, $userid, $attempt);
     $stampfiles = array();
     $fs = get_file_storage();
     $syscontext = context_system::instance();
     // Copy any new stamps to this instance.
     if ($files = $fs->get_area_files($syscontext->id, 'seplfeedback_editpdf', 'stamps', 0, "filename", false)) {
         foreach ($files as $file) {
             $filename = $file->get_filename();
             if ($filename !== '.') {
                 $existingfile = $fs->get_file($this->seplment->get_context()->id, 'seplfeedback_editpdf', 'stamps', $grade->id, '/', $file->get_filename());
                 if (!$existingfile) {
                     $newrecord = new stdClass();
                     $newrecord->contextid = $this->seplment->get_context()->id;
                     $newrecord->itemid = $grade->id;
                     $fs->create_file_from_storedfile($newrecord, $file);
                 }
             }
         }
     }
     // Now get the full list of stamp files for this instance.
     if ($files = $fs->get_area_files($this->seplment->get_context()->id, 'seplfeedback_editpdf', 'stamps', $grade->id, "filename", false)) {
         foreach ($files as $file) {
             $filename = $file->get_filename();
             if ($filename !== '.') {
                 $url = moodle_url::make_pluginfile_url($this->seplment->get_context()->id, 'seplfeedback_editpdf', 'stamps', $grade->id, '/', $file->get_filename(), false);
                 array_push($stampfiles, $url->out());
             }
         }
     }
     $url = false;
     $filename = '';
     if ($feedbackfile) {
         $url = moodle_url::make_pluginfile_url($this->seplment->get_context()->id, 'seplfeedback_editpdf', document_services::FINAL_PDF_FILEAREA, $grade->id, '/', $feedbackfile->get_filename(), false);
         $filename = $feedbackfile->get_filename();
     }
     // Retrieve total number of pages.
     $pagetotal = document_services::page_number_for_attempt($this->seplment->get_instance()->id, $userid, $attempt, $readonly);
     $widget = new seplfeedback_editpdf_widget($this->seplment->get_instance()->id, $userid, $attempt, $url, $filename, $stampfiles, $readonly, $pagetotal);
     return $widget;
 }
 public function test_document_services()
 {
     $sepl = $this->create_sepl_and_submit_pdf();
     $this->setUser($this->teachers[0]);
     $grade = $sepl->get_user_grade($this->students[0]->id, true);
     $notempty = page_editor::has_annotations_or_comments($grade->id, false);
     $this->assertFalse($notempty);
     $comment = new comment();
     $comment->rawtext = 'Comment text';
     $comment->width = 100;
     $comment->x = 100;
     $comment->y = 100;
     $comment->colour = 'red';
     page_editor::set_comments($grade->id, 0, array($comment));
     $annotations = array();
     $annotation = new annotation();
     $annotation->path = '';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 200;
     $annotation->endy = 200;
     $annotation->type = 'line';
     $annotation->colour = 'red';
     array_push($annotations, $annotation);
     $annotation = new annotation();
     $annotation->path = '';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 200;
     $annotation->endy = 200;
     $annotation->type = 'rectangle';
     $annotation->colour = 'yellow';
     array_push($annotations, $annotation);
     $annotation = new annotation();
     $annotation->path = '';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 200;
     $annotation->endy = 200;
     $annotation->type = 'oval';
     $annotation->colour = 'green';
     array_push($annotations, $annotation);
     $annotation = new annotation();
     $annotation->path = '';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 200;
     $annotation->endy = 116;
     $annotation->type = 'highlight';
     $annotation->colour = 'blue';
     array_push($annotations, $annotation);
     $annotation = new annotation();
     $annotation->path = '100,100:105,105:110,100';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 110;
     $annotation->endy = 105;
     $annotation->type = 'pen';
     $annotation->colour = 'black';
     array_push($annotations, $annotation);
     page_editor::set_annotations($grade->id, 0, $annotations);
     page_editor::release_drafts($grade->id);
     $notempty = page_editor::has_annotations_or_comments($grade->id, false);
     $this->assertTrue($notempty);
     $file = document_services::generate_feedback_document($sepl->get_instance()->id, $grade->userid, $grade->attemptnumber);
     $this->assertNotEmpty($file);
     $file2 = document_services::get_feedback_document($sepl->get_instance()->id, $grade->userid, $grade->attemptnumber);
     $this->assertEquals($file, $file2);
     document_services::delete_feedback_document($sepl->get_instance()->id, $grade->userid, $grade->attemptnumber);
     $file3 = document_services::get_feedback_document($sepl->get_instance()->id, $grade->userid, $grade->attemptnumber);
     $this->assertEmpty($file3);
 }