Example #1
0
        $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
    }
    foreach ($pages as $id => $pagefile) {
        $index = count($response->pages);
        $page = new stdClass();
        $comments = page_editor::get_comments($grade->id, $index, $draft);
        $page->url = moodle_url::make_pluginfile_url($context->id, 'assignfeedback_editpdf', $filearea, $grade->id, '/', $pagefile->get_filename())->out();
        $page->comments = $comments;
        if ($imageinfo = $pagefile->get_imageinfo()) {
            $page->width = $imageinfo['width'];
            $page->height = $imageinfo['height'];
        } else {
            $page->width = 0;
            $page->height = 0;
        }
        $annotations = page_editor::get_annotations($grade->id, $index, $draft);
        $page->annotations = $annotations;
        array_push($response->pages, $page);
    }
    echo json_encode($response);
    die;
} else {
    if ($action == 'savepage') {
        require_capability('mod/assign:grade', $context);
        $response = new stdClass();
        $response->errors = array();
        $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
        $pagejson = required_param('page', PARAM_RAW);
        $page = json_decode($pagejson);
        $index = required_param('index', PARAM_INT);
        $added = page_editor::set_comments($grade->id, $index, $page->comments);
Example #2
0
 /**
  * Check to see if the grade feedback for the pdf has been modified.
  *
  * @param stdClass $grade Grade object.
  * @param stdClass $data Data from the form submission (not used).
  * @return boolean True if the pdf has been modified, else false.
  */
 public function is_feedback_modified(stdClass $grade, stdClass $data)
 {
     global $USER;
     $pagenumbercount = document_services::page_number_for_attempt($this->assignment, $grade->userid, $grade->attemptnumber);
     for ($i = 0; $i < $pagenumbercount; $i++) {
         // Select all annotations.
         $draftannotations = page_editor::get_annotations($grade->id, $i, true);
         $nondraftannotations = page_editor::get_annotations($grade->id, $i, false);
         // Check to see if the count is the same.
         if (count($draftannotations) != count($nondraftannotations)) {
             // The count is different so we have a modification.
             return true;
         } else {
             $matches = 0;
             // Have a closer look and see if the draft files match all the non draft files.
             foreach ($nondraftannotations as $ndannotation) {
                 foreach ($draftannotations as $dannotation) {
                     foreach ($ndannotation as $key => $value) {
                         if ($key != 'id' && $value != $dannotation->{$key}) {
                             continue 2;
                         }
                     }
                     $matches++;
                 }
             }
             if ($matches !== count($nondraftannotations)) {
                 return true;
             }
         }
         // Select all comments.
         $draftcomments = page_editor::get_comments($grade->id, $i, true);
         $nondraftcomments = page_editor::get_comments($grade->id, $i, false);
         if (count($draftcomments) != count($nondraftcomments)) {
             return true;
         } else {
             // Go for a closer inspection.
             $matches = 0;
             foreach ($nondraftcomments as $ndcomment) {
                 foreach ($draftcomments as $dcomment) {
                     foreach ($ndcomment as $key => $value) {
                         if ($key != 'id' && $value != $dcomment->{$key}) {
                             continue 2;
                         }
                     }
                     $matches++;
                 }
             }
             if ($matches !== count($nondraftcomments)) {
                 return true;
             }
         }
     }
     return false;
 }
Example #3
0
 public function test_page_editor()
 {
     $assign = $this->create_assign_and_submit_pdf();
     $this->setUser($this->teachers[0]);
     $grade = $assign->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';
     $comment2 = new comment();
     $comment2->rawtext = 'Comment text 2';
     $comment2->width = 100;
     $comment2->x = 200;
     $comment2->y = 100;
     $comment2->colour = 'clear';
     page_editor::set_comments($grade->id, 0, array($comment, $comment2));
     $annotation = new annotation();
     $annotation->path = '';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 200;
     $annotation->endy = 200;
     $annotation->type = 'line';
     $annotation->colour = 'red';
     $annotation2 = new annotation();
     $annotation2->path = '';
     $annotation2->x = 100;
     $annotation2->y = 100;
     $annotation2->endx = 200;
     $annotation2->endy = 200;
     $annotation2->type = 'rectangle';
     $annotation2->colour = 'yellow';
     page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
     $notempty = page_editor::has_annotations_or_comments($grade->id, false);
     // Still empty because all edits are still drafts.
     $this->assertFalse($notempty);
     $comments = page_editor::get_comments($grade->id, 0, false);
     $this->assertEmpty($comments);
     $comments = page_editor::get_comments($grade->id, 0, true);
     $this->assertEquals(count($comments), 2);
     $annotations = page_editor::get_annotations($grade->id, 0, false);
     $this->assertEmpty($annotations);
     $annotations = page_editor::get_annotations($grade->id, 0, true);
     $this->assertEquals(count($annotations), 2);
     $comment = reset($comments);
     $annotation = reset($annotations);
     page_editor::remove_comment($comment->id);
     page_editor::remove_annotation($annotation->id);
     $comments = page_editor::get_comments($grade->id, 0, true);
     $this->assertEquals(count($comments), 1);
     $annotations = page_editor::get_annotations($grade->id, 0, true);
     $this->assertEquals(count($annotations), 1);
     page_editor::release_drafts($grade->id);
     $notempty = page_editor::has_annotations_or_comments($grade->id, false);
     $this->assertTrue($notempty);
     page_editor::unrelease_drafts($grade->id);
     $notempty = page_editor::has_annotations_or_comments($grade->id, false);
     $this->assertFalse($notempty);
 }
Example #4
0
 /**
  * Check to see if the grade feedback for the pdf has been modified.
  *
  * @param stdClass $grade Grade object.
  * @param stdClass $data Data from the form submission (not used).
  * @return boolean True if the pdf has been modified, else false.
  */
 public function is_feedback_modified(stdClass $grade, stdClass $data)
 {
     // We only need to know if the source user's PDF has changed. If so then all
     // following users will have the same status. If it's only an individual annotation
     // then only one user will come through this method.
     // Source user id is only added to the form if there was a pdf.
     if (!empty($data->editpdf_source_userid)) {
         $sourceuserid = $data->editpdf_source_userid;
         // Retrieve the grade information for the source user.
         $sourcegrade = $this->assignment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
         $pagenumbercount = document_services::page_number_for_attempt($this->assignment, $sourceuserid, $sourcegrade->attemptnumber);
         for ($i = 0; $i < $pagenumbercount; $i++) {
             // Select all annotations.
             $draftannotations = page_editor::get_annotations($sourcegrade->id, $i, true);
             $nondraftannotations = page_editor::get_annotations($grade->id, $i, false);
             // Check to see if the count is the same.
             if (count($draftannotations) != count($nondraftannotations)) {
                 // The count is different so we have a modification.
                 return true;
             } else {
                 $matches = 0;
                 // Have a closer look and see if the draft files match all the non draft files.
                 foreach ($nondraftannotations as $ndannotation) {
                     foreach ($draftannotations as $dannotation) {
                         foreach ($ndannotation as $key => $value) {
                             if ($key != 'id' && $value != $dannotation->{$key}) {
                                 continue 2;
                             }
                         }
                         $matches++;
                     }
                 }
                 if ($matches !== count($nondraftannotations)) {
                     return true;
                 }
             }
             // Select all comments.
             $draftcomments = page_editor::get_comments($sourcegrade->id, $i, true);
             $nondraftcomments = page_editor::get_comments($grade->id, $i, false);
             if (count($draftcomments) != count($nondraftcomments)) {
                 return true;
             } else {
                 // Go for a closer inspection.
                 $matches = 0;
                 foreach ($nondraftcomments as $ndcomment) {
                     foreach ($draftcomments as $dcomment) {
                         foreach ($ndcomment as $key => $value) {
                             if ($key != 'id' && $value != $dcomment->{$key}) {
                                 continue 2;
                             }
                         }
                         $matches++;
                     }
                 }
                 if ($matches !== count($nondraftcomments)) {
                     return true;
                 }
             }
         }
     }
     return false;
 }
Example #5
0
 /**
  * Test that modifying the annotated pdf form return true when modified
  * and false when not modified.
  */
 public function test_is_feedback_modified()
 {
     global $DB;
     $assign = $this->create_assign_and_submit_pdf();
     $this->setUser($this->teachers[0]);
     $grade = $assign->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);
     page_editor::set_annotations($grade->id, 0, $annotations);
     $plugin = $assign->get_feedback_plugin_by_type('editpdf');
     $data = new stdClass();
     $data->editpdf_source_userid = $this->students[0]->id;
     $this->assertTrue($plugin->is_feedback_modified($grade, $data));
     $plugin->save($grade, $data);
     $annotation = new annotation();
     $annotation->gradeid = $grade->id;
     $annotation->pageno = 0;
     $annotation->path = '';
     $annotation->x = 100;
     $annotation->y = 100;
     $annotation->endx = 200;
     $annotation->endy = 200;
     $annotation->type = 'rectangle';
     $annotation->colour = 'yellow';
     $yellowannotationid = page_editor::add_annotation($annotation);
     // Add a comment as well.
     $comment = new comment();
     $comment->gradeid = $grade->id;
     $comment->pageno = 0;
     $comment->rawtext = 'Second Comment text';
     $comment->width = 100;
     $comment->x = 100;
     $comment->y = 100;
     $comment->colour = 'red';
     page_editor::add_comment($comment);
     $this->assertTrue($plugin->is_feedback_modified($grade, $data));
     $plugin->save($grade, $data);
     // We should have two annotations.
     $this->assertCount(2, page_editor::get_annotations($grade->id, 0, false));
     // And two comments.
     $this->assertCount(2, page_editor::get_comments($grade->id, 0, false));
     // Add one annotation and delete another.
     $annotation = new annotation();
     $annotation->gradeid = $grade->id;
     $annotation->pageno = 0;
     $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';
     page_editor::add_annotation($annotation);
     $annotations = page_editor::get_annotations($grade->id, 0, true);
     page_editor::remove_annotation($yellowannotationid);
     $this->assertTrue($plugin->is_feedback_modified($grade, $data));
     $plugin->save($grade, $data);
     // We should have two annotations.
     $this->assertCount(2, page_editor::get_annotations($grade->id, 0, false));
     // And two comments.
     $this->assertCount(2, page_editor::get_comments($grade->id, 0, false));
     // Add a comment and then remove it. Should not be considered as modified.
     $comment = new comment();
     $comment->gradeid = $grade->id;
     $comment->pageno = 0;
     $comment->rawtext = 'Third Comment text';
     $comment->width = 400;
     $comment->x = 57;
     $comment->y = 205;
     $comment->colour = 'black';
     $comment->id = page_editor::add_comment($comment);
     // We should now have three comments.
     $this->assertCount(3, page_editor::get_comments($grade->id, 0, true));
     // Now delete the newest record.
     page_editor::remove_comment($comment->id);
     // Back to two comments.
     $this->assertCount(2, page_editor::get_comments($grade->id, 0, true));
     // No modification.
     $this->assertFalse($plugin->is_feedback_modified($grade, $data));
 }
Example #6
0
 /**
  * This function takes the combined pdf and embeds all the comments and annotations.
  *
  * This also moves the annotations and comments from drafts to not drafts. And it will
  * copy all the images stored to the readonly area, so that they can be viewed online, and
  * not be overwritten when a new submission is sent.
  *
  * @param int|\assign $assignment
  * @param int $userid
  * @param int $attemptnumber (-1 means latest attempt)
  * @return stored_file
  */
 public static function generate_feedback_document($assignment, $userid, $attemptnumber)
 {
     $assignment = self::get_assignment_from_param($assignment);
     if (!$assignment->can_view_submission($userid)) {
         \print_error('nopermission');
     }
     if (!$assignment->can_grade()) {
         \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/final/' . self::hash($assignment, $userid, $attemptnumber));
     $combined = $tmpdir . '/' . self::COMBINED_PDF_FILENAME;
     $file->copy_content_to($combined);
     // Copy the file.
     $pdf = new pdf();
     $fs = \get_file_storage();
     $stamptmpdir = \make_temp_directory('assignfeedback_editpdf/stamps/' . self::hash($assignment, $userid, $attemptnumber));
     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
     // Copy any new stamps to this instance.
     if ($files = $fs->get_area_files($assignment->get_context()->id, 'assignfeedback_editpdf', 'stamps', $grade->id, "filename", false)) {
         foreach ($files as $file) {
             $filename = $stamptmpdir . '/' . $file->get_filename();
             $file->copy_content_to($filename);
             // Copy the file.
         }
     }
     $pagecount = $pdf->set_pdf($combined);
     $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
     page_editor::release_drafts($grade->id);
     for ($i = 0; $i < $pagecount; $i++) {
         $pdf->copy_page();
         $comments = page_editor::get_comments($grade->id, $i, false);
         $annotations = page_editor::get_annotations($grade->id, $i, false);
         foreach ($comments as $comment) {
             $pdf->add_comment($comment->rawtext, $comment->x, $comment->y, $comment->width, $comment->colour);
         }
         foreach ($annotations as $annotation) {
             $pdf->add_annotation($annotation->x, $annotation->y, $annotation->endx, $annotation->endy, $annotation->colour, $annotation->type, $annotation->path, $stamptmpdir);
         }
     }
     fulldelete($stamptmpdir);
     $filename = self::get_downloadable_feedback_filename($assignment, $userid, $attemptnumber);
     $filename = clean_param($filename, PARAM_FILE);
     $generatedpdf = $tmpdir . '/' . $filename;
     $pdf->save_pdf($generatedpdf);
     $record = new \stdClass();
     $record->contextid = $assignment->get_context()->id;
     $record->component = 'assignfeedback_editpdf';
     $record->filearea = self::FINAL_PDF_FILEAREA;
     $record->itemid = $grade->id;
     $record->filepath = '/';
     $record->filename = $filename;
     // Only keep one current version of the generated pdf.
     $fs->delete_area_files($record->contextid, $record->component, $record->filearea, $record->itemid);
     $file = $fs->create_file_from_pathname($record, $generatedpdf);
     // Cleanup.
     @unlink($generatedpdf);
     @unlink($combined);
     @rmdir($tmpdir);
     self::copy_pages_to_readonly_area($assignment, $grade);
     return $file;
 }