From 073af36b2e44847039d0889d478ec486d2a04d70 Mon Sep 17 00:00:00 2001 From: Isabel <isabel-andrea.uffinger@stud.hs-hannover.de> Date: Tue, 12 Mar 2024 15:39:50 +0100 Subject: [PATCH] first test to encode to utf8 --- .gitignore | 3 ++- externallib.php | 6 ++++-- locallib.php | 19 +++++++++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index eb88b9c..4ce455e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.min.js.map -.idea/ \ No newline at end of file +.idea/ +.vscode \ No newline at end of file diff --git a/externallib.php b/externallib.php index dccc941..e6dd854 100644 --- a/externallib.php +++ b/externallib.php @@ -192,8 +192,9 @@ class qtype_moopt_external extends external_api { break; } else if ($child->localName == 'attached-txt-file') { $pathinfo = pathinfo('/' . $child->nodeValue); + $encoding = $child->attributes->getNamedItem('encoding')->nodeValue; $filecontent = get_text_content_from_file($usercontext, $draftid, $keepfilename, - $pathinfo['dirname'] . '/', $pathinfo['basename']); + $pathinfo['dirname'] . '/', $pathinfo['basename'], true, $encoding); $defaultfilename = basename($child->nodeValue); $fileid = $file->attributes->getNamedItem('id')->nodeValue; $enablefreetext = true; @@ -259,8 +260,9 @@ class qtype_moopt_external extends external_api { } else if ($child->localName == 'attached-txt-file') { $pathinfo = pathinfo('/' . $child->nodeValue); $filename = basename($child->nodeValue); + $encoding = $child->attributes->getNamedItem('encoding')->nodeValue; $filecontent = get_text_content_from_file($usercontext, $draftid, $keepfilename, - $pathinfo['dirname'] . '/', $pathinfo['basename']); + $pathinfo['dirname'] . '/', $pathinfo['basename'], true, $encoding); } if(!empty($filecontent)) diff --git a/locallib.php b/locallib.php index 05cefab..4a1df0d 100644 --- a/locallib.php +++ b/locallib.php @@ -282,7 +282,7 @@ function create_domdocument_from_task_xml($usercontext, $draftareaid, $xmlfilena * @return string * @throws invalid_parameter_exception */ -function get_text_content_from_file($usercontext, $draftareaid, $keepfilename, $filepath, $filename) +function get_text_content_from_file($usercontext, $draftareaid, $keepfilename, $filepath, $filename, $attached, $encoding) { $fs = get_file_storage(); $file = $fs->get_file($usercontext->id, 'user', 'draft', $draftareaid, $filepath, $filename); @@ -294,7 +294,22 @@ function get_text_content_from_file($usercontext, $draftareaid, $keepfilename, $ // TODO: make sure the mimetype is plain text // even task.xmls may contain mistakes (eg PDF ) - return $file->get_content(); + //check if encoding of attached ist utf-8 else convert + $content = $file->get_content(); + if($encoding!==false){ + $enc=$encoding; + } else { + $enc = mb_detect_encoding($content, mb_detect_order(), true); + if($enc===false){ + throw new invalid_parameter_exception('Encoding of attached file ' . $filepath . $filename . 'could\'nt be detectet.'); + } + } + $content = mb_convert_encoding($content, $enc, 'UTF-8'); + if($enc!=='UTF-8'){ + $content = mb_convert_encoding($content, 'UTF-8', $enc); + } + + return $content; } -- GitLab