diff --git a/externallib.php b/externallib.php index e6dd8547ff001d3e0eaf38b82a8e03e65cb1b3db..fd070212c7cd1d3ddf61ed64e4c176ec28e943ce 100644 --- a/externallib.php +++ b/externallib.php @@ -42,7 +42,7 @@ class qtype_moopt_external extends external_api { 'taskuuid' => new external_value(PARAM_RAW, 'task\'s uuid', VALUE_OPTIONAL), 'maxscoregradinghints' => new external_value(PARAM_FLOAT, 'maximum score', VALUE_OPTIONAL), 'filesdisplayedingeneralfeedback' => new external_value(PARAM_RAW, 'general feedback', VALUE_OPTIONAL), - 'enablefileinput' => new external_value(PARAM_BOOL, 'Enable file submissions'), + 'enablefileinput' => new external_value(PARAM_BOOL, 'Enable file submissions', VALUE_OPTIONAL), 'freetextfilesettings' => new external_multiple_structure( new external_single_structure( array( @@ -56,7 +56,6 @@ class qtype_moopt_external extends external_api { ) ,'Free text settings', VALUE_OPTIONAL), 'moodleValidationProformaNamespace' => new external_value(PARAM_TEXT, 'detected namespace', VALUE_OPTIONAL), - 'moodleValidationWarningInvalidNamespace' => new external_value(PARAM_TEXT, 'warning message in case of invalid XML namespace', VALUE_OPTIONAL), 'moodleValidationWarnings' => new external_multiple_structure( new external_single_structure( array( @@ -86,6 +85,8 @@ class qtype_moopt_external extends external_api { $unzipinfo = unzip_task_file_in_draft_area($draftid, $usercontext); if ($unzipinfo == null) { return ['error' => 'Error extracting zip file']; + } else if (isset($unzipinfo['error'])) { + return $unzipinfo; } $taskxmlfilename = $unzipinfo['xml']; $taskzipfilename = $unzipinfo['zip'] ?? null; @@ -96,8 +97,7 @@ class qtype_moopt_external extends external_api { $returnval = array(); if ($namespace == null) { - - $returnval['moodleValidationWarningInvalidNamespace'] = get_string('invalidproformanamespace', 'qtype_moopt', + $returnval['error'] = get_string('invalidproformanamespace', 'qtype_moopt', implode(", ", PROFORMA_TASK_XML_NAMESPACES)); } else { @@ -195,6 +195,9 @@ class qtype_moopt_external extends external_api { $encoding = $child->attributes->getNamedItem('encoding')->nodeValue; $filecontent = get_text_content_from_file($usercontext, $draftid, $keepfilename, $pathinfo['dirname'] . '/', $pathinfo['basename'], true, $encoding); + if($filecontent === null){ + $returnval['error'] = "Encoding of file ".$pathinfo['basename']." couldn't be detected."; + } $defaultfilename = basename($child->nodeValue); $fileid = $file->attributes->getNamedItem('id')->nodeValue; $enablefreetext = true; diff --git a/locallib.php b/locallib.php index 20143722152c43d118da0d425f368be9005f4ae1..8280b24d16199cdb5a8753b562e5a395811626bb 100644 --- a/locallib.php +++ b/locallib.php @@ -120,6 +120,7 @@ use qtype_moopt\utility\proforma_xml\separate_feedback_handler; /* * Unzips the task zip file in the given draft area into the area + * moodle doesn't display thrown exceptions, so we handle them as array with key 'error' in calling function * * @param type $draftareaid * @param type $usercontext @@ -129,7 +130,6 @@ use qtype_moopt\utility\proforma_xml\separate_feedback_handler; * 'xml' => (string) the name of the xml file (mandatory) * ] * Returns false, if there is no file in the given draft area. - * @throws invalid_parameter_exception */ function unzip_task_file_in_draft_area($draftareaid, $usercontext) { global $USER; @@ -141,8 +141,8 @@ function unzip_task_file_in_draft_area($draftareaid, $usercontext) { if ($area['filecount'] == 0) { return false; } else if ($area['filecount'] > 1 || $area['foldercount'] != 0) { - throw new invalid_parameter_exception( - 'Only one file is allowed to be in this draft area: A ProFormA-Task as either ZIP or XML file.'); + $error = 'Only one file is allowed to be in this draft area: A ProFormA-Task as either ZIP or XML file. Check for additional folders as well.'; + return array('error' => $error); } // Get name of the file. @@ -164,7 +164,8 @@ function unzip_task_file_in_draft_area($draftareaid, $usercontext) { return array('xml' => $filename); } if ($filetype != 'zip') { - throw new invalid_parameter_exception('Supplied file must be a xml or zip file.'); + $error = 'Supplied file must be a xml or zip file.'; + return array('error' => $error); } $zipfilename = $filename; $result = array('zip' => $zipfilename); @@ -215,7 +216,8 @@ function unzip_task_file_in_draft_area($draftareaid, $usercontext) { } if (!array_key_exists('xml', $result)) { - throw new invalid_parameter_exception('Supplied zip file must contain the file task.xml.'); + $error = 'Supplied zip file must contain the file task.xml.'; + return array('error' => $error); } return $result; @@ -292,19 +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 ) - //check if encoding of attached file is utf-8 else convert + //check if encoding of attached file is utf-8 else convert if encoding could be detectet $content = $file->get_content(); + $test_encodings = array('UTF-8', 'ISO-8859-1', 'ISO-8859-15', 'Windows-1252', 'UTF-16'); if($attached){ if($encoding!=null){ $enc=$encoding; } else { - $enc = mb_detect_encoding($content, null, true); - if($enc===false){ - throw new invalid_parameter_exception('Encoding of attached file ' . $filepath . $filename . ' could\'nt be detectet.'); + $enc = mb_detect_encoding($content, null, false); + if ($enc==false){ + $enc = mb_detect_encoding($content, $test_encodings, true); } } - if($enc!=='UTF-8'){ - $content = mb_convert_encoding($content, 'UTF-8', $enc); + if($enc==false){ + return null; + } else if($enc!=='UTF-8'){ + $content = mb_convert_encoding($content, 'UTF-8', $enc); } }