Skip to content

Commit

Permalink
add exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
anisa kusumadewi committed Nov 9, 2022
1 parent 1a11b2a commit 04f7443
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
3 changes: 3 additions & 0 deletions lang/en/pdfannotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@
$string['error:deleteAnnotation'] = 'An error has occured while deleting an annotation.';
$string['error:editAnnotation'] = 'An error has occurred while editing an annotation.';
$string['error:editcomment'] = 'An error has occured while trying to edit a comment.';
$string['error:findimage'] = 'An error occured while trying to find image {$a}.';
$string['error:forwardquestion'] = 'An error has occured while forwarding the question.';
$string['error:forwardquestionnorecipient'] = 'An error has occured while forwarding the question.: No person in this course has the capability to receive forwarded questions.';
$string['error:getAllQuestions'] = 'An error has occured while getting the questions of this document.';
$string['error:getAnnotation'] = 'An error has occured while getting the annotation.';
$string['error:getAnnotations'] = 'An error has occured while getting all annotations.';
$string['error:getComments'] = 'An error has occured while getting the comments.';
$string['error:getimageheight'] = 'An error has occured while getting image height of {$a}.';
$string['error:getimageheight'] = 'An error has occured while getting image width of {$a}.';
$string['error:getQuestions'] = 'An error has occured while getting the questions for this page.';
$string['error:printComments'] = 'Error with data from server.';
$string['error:hideComment'] = "An error has occured while trying to hide the comment from participants' view.";
Expand Down
75 changes: 49 additions & 26 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function pdfannotator_display_embed($pdfannotator, $cm, $course, $file, $page =
// Load and execute the javascript files.
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/pdf.js?ver=00002"));
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/textclipper.js"));
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/index.js?ver=00035"));
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/index.js?ver=00036"));
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/locallib.js?ver=00006"));

// Pass parameters from PHP to JavaScript.
Expand Down Expand Up @@ -210,42 +210,65 @@ function pdfannotator_split_content_image($content, $res, $itemid, $context=null
}

$tempinfo = [];
$encodedurl = urldecode($url[0]);
foreach($fileinfo as $file) {
$count = substr_count(urldecode($url[0]), $file['filename']);
$count = substr_count($encodedurl, $file['filename']);
if($count) {
$tempinfo = $file;
break;
}
}

if($tempinfo) {
$imagedata = 'data:' . $tempinfo['filemimetype'] . ';base64,' . base64_encode($tempinfo['filecontent']);
$data['image'] = $imagedata;
$data['format'] = $tempinfo['filemimetype'];
$data['fileid'] = $tempinfo['fileid'];
$data['filename'] = $tempinfo['filename'];
$data['filepath'] = $tempinfo['filepath'];
$data['filesize'] = $tempinfo['filesize'];
$data['imagestorage'] = 'intern';
try {
if($tempinfo) {
$imagedata = 'data:' . $tempinfo['filemimetype'] . ';base64,' . base64_encode($tempinfo['filecontent']);
$data['image'] = $imagedata;
$data['format'] = $tempinfo['filemimetype'];
$data['fileid'] = $tempinfo['fileid'];
$data['filename'] = $tempinfo['filename'];
$data['filepath'] = $tempinfo['filepath'];
$data['filesize'] = $tempinfo['filesize'];
$data['imagestorage'] = 'intern';
} else if (!str_contains($CFG->wwwroot, $url[0])){
$data['imagestorage'] = 'extern';
$data['format'] = $format[0];
$imgcontent = @file_get_contents($url[0]);
if ($imgcontent) {
$data['image'] = 'data:image/' . $format[0] . ";base64," . base64_encode($imgcontent);
} else {
throw new Exception(get_string('error:findimage', 'pdfannotator', $encodedurl));
}
} else {
throw new Exception(get_string('error:findimage', 'pdfannotator', $encodedurl));
}

preg_match('/height=[0-9]+/', $imgstr, $height);
$data['imageheight'] = str_replace("\"", "", explode('=', $height[0])[1]);
if ($height) {
$data['imageheight'] = str_replace("\"", "", explode('=', $height[0])[1]);
} else if (!$height && $data['imagestorage'] === 'extern') {
$imagemetadata = getimagesize($url[0]);
$data['imageheight'] = $imagemetadata[1];
} else {
throw new Exception(get_string('error:getimageheight', 'pdfannotator', $encodedurl));
}
preg_match('/width=[0-9]+/', $imgstr, $width);
$data['imagewidth'] = str_replace("\"", "", explode('=', $width[0])[1]);
} else if (!str_contains($CFG->wwwroot, $url[0])){
$data['imagestorage'] = 'extern';
$data['format'] = $format[0];
$imagemetadata = getimagesize($url[0]);
$data['image'] = 'data:image/' . $format[0] . ";base64," . base64_encode(file_get_contents($url[0]));
$data['imagewidth'] = $imagemetadata[0];
$data['imageheight'] = $imagemetadata[1];
} else {
$data['success'] = "error";
$data['message'] = "cannot load image";
if ($width) {
$data['imagewidth'] = str_replace("\"", "", explode('=', $width[0])[1]);
} else if (!$width && $data['imagestorage'] === 'extern') {
$imagemetadata = getimagesize($url[0]);
$data['imagewidth'] = $imagemetadata[0];
} else {
throw new Exception(get_string('error:getimagewidth', 'pdfannotator', $encodedurl));
}
} catch (Exception $ex) {
$data['image'] = "error";
$data['message'] = $ex->getMessage();
} finally {
$res[] = $firststr;
$res[] = $data;
$content = $laststr;
}

$res[] = $firststr;
$res[] = $data;
$content = $laststr;
}
$res[] = $content;

Expand Down
4 changes: 2 additions & 2 deletions shared/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'mod_pdfannotator';
$plugin->version = 2022110900;
$plugin->version = 2022110902;
$plugin->release = 'PDF Annotator v1.4 release 11';
$plugin->requires = 2021051700;
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 04f7443

Please sign in to comment.