Skip to content

Commit

Permalink
MDL-42799 Assignment: Min. width/height for widget is set to 1
Browse files Browse the repository at this point in the history
It was possible for user to add widgets with width/height = 0
which were not displayed to user and doesn't have much meaning to it
This patch will set min. width/height to 1
  • Loading branch information
Rajesh Taneja committed Nov 12, 2013
1 parent d214057 commit baf881b
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 32 deletions.
34 changes: 33 additions & 1 deletion mod/assign/feedback/editpdf/classes/pdf.php
Expand Up @@ -64,6 +64,10 @@ class pdf extends \FPDI {
const GSPATH_NOTESTFILE = 'notestfile';
/** Any other error */
const GSPATH_ERROR = 'error';
/** Min. width an annotation should have */
const MIN_ANNOTATION_WIDTH = 5;
/** Min. height an annotation should have */
const MIN_ANNOTATION_HEIGHT = 5;

/**
* Combine the given PDF files into a single PDF. Optionally add a coversheet and coversheet fields.
Expand Down Expand Up @@ -300,13 +304,30 @@ public function add_annotation($sx, $sy, $ex, $ey, $colour = 'yellow', $type = '
$ry = abs($sy - $ey) / 2;
$sx = min($sx, $ex) + $rx;
$sy = min($sy, $ey) + $ry;

// $rx and $ry should be >= min width and height
if ($rx < self::MIN_ANNOTATION_WIDTH) {
$rx = self::MIN_ANNOTATION_WIDTH;
}
if ($ry < self::MIN_ANNOTATION_HEIGHT) {
$ry = self::MIN_ANNOTATION_HEIGHT;
}

$this->Ellipse($sx, $sy, $rx, $ry);
break;
case 'rectangle':
$w = abs($sx - $ex);
$h = abs($sy - $ey);
$sx = min($sx, $ex);
$sy = min($sy, $ey);

// Width or height should be >= min width and height
if ($w < self::MIN_ANNOTATION_WIDTH) {
$w = self::MIN_ANNOTATION_WIDTH;
}
if ($h < self::MIN_ANNOTATION_HEIGHT) {
$h = self::MIN_ANNOTATION_HEIGHT;
}
$this->Rect($sx, $sy, $w, $h);
break;
case 'highlight':
Expand All @@ -316,6 +337,12 @@ public function add_annotation($sx, $sy, $ex, $ey, $colour = 'yellow', $type = '
$sy = min($sy, $ey) + ($h * 0.5);
$this->SetAlpha(0.5, 'Normal', 0.5, 'Normal');
$this->SetLineWidth(8.0 * $this->scale);

// width should be >= min width
if ($w < self::MIN_ANNOTATION_WIDTH) {
$w = self::MIN_ANNOTATION_WIDTH;
}

$this->Rect($sx, $sy, $w, $h);
$this->SetAlpha(1.0, 'Normal', 1.0, 'Normal');
break;
Expand All @@ -326,7 +353,10 @@ public function add_annotation($sx, $sy, $ex, $ey, $colour = 'yellow', $type = '
foreach ($points as $point) {
$scalepath[] = intval($point) * $this->scale;
}
$this->PolyLine($scalepath, 'S');

if (!empty($scalepath)) {
$this->PolyLine($scalepath, 'S');
}
}
break;
case 'stamp':
Expand All @@ -335,6 +365,8 @@ public function add_annotation($sx, $sy, $ex, $ey, $colour = 'yellow', $type = '
$h = abs($sy - $ey);
$sx = min($sx, $ex);
$sy = min($sy, $ey);

// Stamp is always more than 40px, so no need to check width/height.
$this->Image($imgfile, $sx, $sy, $w, $h);
break;
default: // Line.
Expand Down
Expand Up @@ -250,6 +250,44 @@ RECT = function(x, y, width, height) {
// Allow chaining.
return this;
};

/**
* Checks if rect has min width.
* @method has_min_width
* @return bool true if width is more than 5px.
* @public
*/
this.has_min_width = function() {
return (this.width >= 5);
};

/**
* Checks if rect has min height.
* @method has_min_height
* @return bool true if height is more than 5px.
* @public
*/
this.has_min_height = function() {
return (this.height >= 5);
};

/**
* Set min. width of annotation bound.
* @method set_min_width
* @public
*/
this.set_min_width = function() {
this.width = 5;
};

/**
* Set min. height of annotation bound.
* @method set_min_height
* @public
*/
this.set_min_height = function() {
this.height = 5;
};
};

M.assignfeedback_editpdf = M.assignfeedback_editpdf || {};
Expand Down Expand Up @@ -745,6 +783,7 @@ Y.extend(ANNOTATION, Y.Base, {
* @public
* @method init_from_edit
* @param M.assignfeedback_editpdf.edit edit
* @return bool if width/height is more than min. required.
*/
init_from_edit : function(edit) {
var bounds = new M.assignfeedback_editpdf.rect();
Expand All @@ -758,6 +797,7 @@ Y.extend(ANNOTATION, Y.Base, {
this.endy = bounds.y + bounds.height;
this.colour = edit.annotationcolour;
this.path = '';
return (bounds.has_min_width() && bounds.has_min_height());
}

});
Expand Down Expand Up @@ -860,6 +900,7 @@ Y.extend(ANNOTATIONLINE, M.assignfeedback_editpdf.annotation, {
* @public
* @method init_from_edit
* @param M.assignfeedback_editpdf.edit edit
* @return bool true if line bound is more than min width/height, else false.
*/
init_from_edit : function(edit) {
this.gradeid = this.editor.get('gradeid');
Expand All @@ -870,6 +911,8 @@ Y.extend(ANNOTATIONLINE, M.assignfeedback_editpdf.annotation, {
this.endy = edit.end.y;
this.colour = edit.annotationcolour;
this.path = '';

return !(((this.endx - this.x) === 0) && ((this.endy - this.y) === 0));
}

});
Expand Down Expand Up @@ -956,6 +999,14 @@ Y.extend(ANNOTATIONRECTANGLE, M.assignfeedback_editpdf.annotation, {
bounds.bound([new M.assignfeedback_editpdf.point(edit.start.x, edit.start.y),
new M.assignfeedback_editpdf.point(edit.end.x, edit.end.y)]);

// Set min. width and height of rectangle.
if (!bounds.has_min_width()) {
bounds.set_min_width();
}
if (!bounds.has_min_height()) {
bounds.set_min_height();
}

shape = this.editor.graphic.addShape({
type: Y.Rect,
width: bounds.width,
Expand Down Expand Up @@ -1056,6 +1107,14 @@ Y.extend(ANNOTATIONOVAL, M.assignfeedback_editpdf.annotation, {
bounds.bound([new M.assignfeedback_editpdf.point(edit.start.x, edit.start.y),
new M.assignfeedback_editpdf.point(edit.end.x, edit.end.y)]);

// Set min. width and height of oval.
if (!bounds.has_min_width()) {
bounds.set_min_width();
}
if (!bounds.has_min_height()) {
bounds.set_min_height();
}

shape = this.editor.graphic.addShape({
type: Y.Ellipse,
width: bounds.width,
Expand Down Expand Up @@ -1200,6 +1259,7 @@ Y.extend(ANNOTATIONPEN, M.assignfeedback_editpdf.annotation, {
* @public
* @method init_from_edit
* @param M.assignfeedback_editpdf.edit edit
* @return bool true if pen bound is more than min width/height, else false.
*/
init_from_edit : function(edit) {
var bounds = new M.assignfeedback_editpdf.rect(),
Expand All @@ -1221,6 +1281,8 @@ Y.extend(ANNOTATIONPEN, M.assignfeedback_editpdf.annotation, {
this.endy = bounds.y + bounds.height;
this.colour = edit.annotationcolour;
this.path = pathlist.join(':');

return (bounds.has_min_width() || bounds.has_min_height());
}


Expand Down Expand Up @@ -1318,6 +1380,11 @@ Y.extend(ANNOTATIONHIGHLIGHT, M.assignfeedback_editpdf.annotation, {
bounds.bound([new M.assignfeedback_editpdf.point(edit.start.x, edit.start.y),
new M.assignfeedback_editpdf.point(edit.end.x, edit.end.y)]);

// Set min. width of highlight.
if (!bounds.has_min_width()) {
bounds.set_min_width();
}

highlightcolour = ANNOTATIONCOLOUR[edit.annotationcolour];
// Add an alpha channel to the rgb colour.

Expand Down Expand Up @@ -1348,6 +1415,7 @@ Y.extend(ANNOTATIONHIGHLIGHT, M.assignfeedback_editpdf.annotation, {
* @public
* @method init_from_edit
* @param M.assignfeedback_editpdf.edit edit
* @return bool true if highlight bound is more than min width/height, else false.
*/
init_from_edit : function(edit) {
var bounds = new M.assignfeedback_editpdf.rect();
Expand All @@ -1361,6 +1429,8 @@ Y.extend(ANNOTATIONHIGHLIGHT, M.assignfeedback_editpdf.annotation, {
this.endy = edit.start.y + 16;
this.colour = edit.annotationcolour;
this.page = '';

return (bounds.has_min_width());
}

});
Expand Down Expand Up @@ -1480,6 +1550,7 @@ Y.extend(ANNOTATIONSTAMP, M.assignfeedback_editpdf.annotation, {
* @public
* @method init_from_edit
* @param M.assignfeedback_editpdf.edit edit
* @return bool if width/height is more than min. required.
*/
init_from_edit : function(edit) {
var bounds = new M.assignfeedback_editpdf.rect();
Expand All @@ -1499,6 +1570,9 @@ Y.extend(ANNOTATIONSTAMP, M.assignfeedback_editpdf.annotation, {
this.endy = bounds.y + bounds.height;
this.colour = edit.annotationcolour;
this.path = edit.stamp;

// Min width and height is always more than 40px.
return true;
},

/**
Expand Down Expand Up @@ -2541,6 +2615,7 @@ COMMENT = function(editor, gradeid, pageno, x, y, width, colour, rawtext) {
* @public
* @method init_from_edit
* @param M.assignfeedback_editpdf.edit edit
* @return bool true if comment bound is more than min width/height, else false.
*/
this.init_from_edit = function(edit) {
var bounds = new M.assignfeedback_editpdf.rect();
Expand All @@ -2560,6 +2635,8 @@ COMMENT = function(editor, gradeid, pageno, x, y, width, colour, rawtext) {
this.width = bounds.width;
this.colour = edit.commentcolour;
this.rawtext = '';

return (bounds.has_min_width() && bounds.has_min_height());
};

};
Expand Down Expand Up @@ -3617,24 +3694,25 @@ EDITOR.prototype = {
}
this.currentdrawable = false;
comment = new M.assignfeedback_editpdf.comment(this);
comment.init_from_edit(this.currentedit);
this.pages[this.currentpage].comments.push(comment);
this.drawables.push(comment.draw(true));
this.editingcomment = true;
if (comment.init_from_edit(this.currentedit)) {
this.pages[this.currentpage].comments.push(comment);
this.drawables.push(comment.draw(true));
this.editingcomment = true;
}
} else {
annotation = this.create_annotation(this.currentedit.tool, {});
if (annotation) {
if (this.currentdrawable) {
this.currentdrawable.erase();
}
this.currentdrawable = false;
annotation.init_from_edit(this.currentedit);
this.pages[this.currentpage].annotations.push(annotation);
this.drawables.push(annotation.draw());
if (annotation.init_from_edit(this.currentedit)) {
this.pages[this.currentpage].annotations.push(annotation);
this.drawables.push(annotation.draw());
}
}
}


// Save the changes.
this.save_current_page();

Expand Down

0 comments on commit baf881b

Please sign in to comment.