MDL-55459 assignfeedback_editpdf: Add linked comments and markers to PDF

This commit is contained in:
Tony Butler
2017-03-24 09:48:42 +00:00
parent 216ea39be7
commit 2c153c5649
4 changed files with 123 additions and 7 deletions
@@ -634,17 +634,15 @@ EOD;
$grade = $assignment->get_user_grade($userid, true, $attemptnumber);
page_editor::release_drafts($grade->id);
$allcomments = array();
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);
if (!empty($comments)) {
$allcomments[$i] = $comments;
}
foreach ($annotations as $annotation) {
@@ -659,6 +657,18 @@ EOD;
}
}
if (!empty($allcomments)) {
// Append all comments to the end of the document.
$links = $pdf->append_comments($allcomments);
// Add the comment markers with links.
foreach ($allcomments as $pageno => $comments) {
foreach ($comments as $index => $comment) {
$pdf->add_comment_marker($comment->pageno, $index, $comment->x, $comment->y, $links[$pageno][$index],
$comment->colour);
}
}
}
fulldelete($stamptmpdir);
$filename = self::get_downloadable_feedback_filename($assignment, $userid, $attemptnumber);
@@ -50,7 +50,8 @@ class page_editor {
if (!$draft) {
$params['draft'] = 0;
}
$records = $DB->get_records('assignfeedback_editpdf_cmnt', $params);
// Fetch comments ordered by position on the page.
$records = $DB->get_records('assignfeedback_editpdf_cmnt', $params, 'y, x');
foreach ($records as $record) {
array_push($comments, new comment($record));
}
+103
View File
@@ -212,6 +212,109 @@ class pdf extends \FPDI {
}
}
/**
* Append all comments to the end of the document.
*
* @param array $allcomments All comments, indexed by page number (starting at 0).
* @return array|bool An array of links to comments, or false.
*/
public function append_comments($allcomments) {
if (!$this->filename) {
return false;
}
$this->SetFontSize(12 * $this->scale);
$this->SetMargins(100 * $this->scale, 120 * $this->scale, -1, true);
$this->SetAutoPageBreak(true, 100 * $this->scale);
$this->setHeaderFont(array('helvetica', '', 24 * $this->scale));
$this->setHeaderMargin(24 * $this->scale);
$this->setHeaderData('', 0, '', get_string('commentindex', 'assignfeedback_editpdf'));
// Add a new page to the document with an appropriate header.
$this->setPrintHeader(true);
$this->AddPage();
// Add the comments.
$commentlinks = array();
foreach ($allcomments as $pageno => $comments) {
foreach ($comments as $index => $comment) {
// Create a link to the current location, which will be added to the marker.
$commentlink = $this->AddLink();
$this->SetLink($commentlink, -1);
$commentlinks[$pageno][$index] = $commentlink;
// Also create a link back to the marker, which will be added here.
$markerlink = $this->AddLink();
$this->SetLink($markerlink, $comment->y * $this->scale, $pageno + 1);
$label = get_string('commentlabel', 'assignfeedback_editpdf', array('pnum' => $pageno + 1, 'cnum' => $index + 1));
$this->Cell(50 * $this->scale, 0, $label, 0, 0, '', false, $markerlink);
$this->MultiCell(0, 0, $comment->rawtext, 0, 'L');
$this->Ln(12 * $this->scale);
}
// Add an extra line break between pages.
$this->Ln(12 * $this->scale);
}
return $commentlinks;
}
/**
* Add a comment marker to the specified page.
*
* @param int $pageno The page number to add markers to (starting at 0).
* @param int $index The comment index.
* @param int $x The x-coordinate of the marker (in pixels).
* @param int $y The y-coordinate of the marker (in pixels).
* @param int $link The link identifier pointing to the full comment text.
* @param string $colour The fill colour of the marker (red, yellow, green, blue, white, clear).
* @return bool Success status.
*/
public function add_comment_marker($pageno, $index, $x, $y, $link, $colour = 'yellow') {
if (!$this->filename) {
return false;
}
$fill = '';
$fillopacity = 0.9;
switch ($colour) {
case 'red':
$fill = 'rgb(249, 181, 179)';
break;
case 'green':
$fill = 'rgb(214, 234, 178)';
break;
case 'blue':
$fill = 'rgb(203, 217, 237)';
break;
case 'white':
$fill = 'rgb(255, 255, 255)';
break;
case 'clear':
$fillopacity = 0;
break;
default: /* Yellow */
$fill = 'rgb(255, 236, 174)';
}
$marker = '@<svg xmlns="http://www.w3.org/2000/svg" viewBox="-0.5 -0.5 12 12" preserveAspectRatio="xMinYMin meet">' .
'<path d="M11 0H1C.4 0 0 .4 0 1v6c0 .6.4 1 1 1h1v4l4-4h5c.6 0 1-.4 1-1V1c0-.6-.4-1-1-1z" fill="' . $fill . '" ' .
'fill-opacity="' . $fillopacity . '" stroke="rgb(153, 153, 153)" stroke-width="0.5"/></svg>';
$label = get_string('commentlabel', 'assignfeedback_editpdf', array('pnum' => $pageno + 1, 'cnum' => $index + 1));
$x *= $this->scale;
$y *= $this->scale;
$size = 24 * $this->scale;
$this->SetDrawColor(51, 51, 51);
$this->SetFontSize(10 * $this->scale);
$this->setPage($pageno + 1);
// Add the marker image.
$this->ImageSVG($marker, $x - 0.5, $y - 0.5, $size, $size, $link);
// Add the label.
$this->MultiCell($size * 0.95, 0, $label, 0, 'C', false, 1, $x, $y, true, 0, false, true, $size * 0.60, 'M', true);
return true;
}
/**
* Add a comment to the current page
* @param string $text the text of the comment
@@ -33,6 +33,8 @@ $string['clear'] = 'Clear';
$string['colourpicker'] = 'Colour picker';
$string['commentcolour'] = 'Comment colour';
$string['comment'] = 'Comments';
$string['commentindex'] = 'Index of comments';
$string['commentlabel'] = '{$a->pnum}.{$a->cnum}';
$string['command'] = 'Command:';
$string['commentcontextmenu'] = 'Comment context menu';
$string['couldnotsavepage'] = 'Could not save page {$a}';