MDL-69078 questions: check for UTF-8 encoding of the import file

Signed-off-by: Daniel Ziegenberg <[email protected]>
This commit is contained in:
Daniel Ziegenberg
2022-03-17 20:53:50 +01:00
parent 01eb6d2e9b
commit f942a5cedd
10 changed files with 118 additions and 0 deletions
+34
View File
@@ -95,6 +95,40 @@ class qformat_default {
return ($file->get_mimetype() == $this->mime_type());
}
/**
* Validate the given file.
*
* For more expensive or detailed integrity checks.
*
* @param stored_file $file the file to check
* @return string the error message that occurred while validating the given file
*/
public function validate_file(stored_file $file): string {
return '';
}
/**
* Check if the given file has the required utf8 encoding.
*
* @param stored_file $file the file to check
* @return string the error message if the file encoding is not UTF-8
*/
protected function validate_is_utf8_file(stored_file $file): string {
if (!mb_check_encoding($file->get_content(), "UTF-8")) {
return get_string('importwrongfileencoding', 'question', $this->get_name());
}
return '';
}
/**
* Return the localized pluginname string for the question format.
*
* @return string the pluginname string for the question format
*/
protected function get_name(): string {
return get_string('pluginname', get_class($this));
}
// Accessor methods
/**