From 0eecf876878d690bb15c2d36ac3650ce15142c30 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 9 May 2016 13:59:33 +0800 Subject: [PATCH] MDL-53923 mod_assign: Movement of functions to file_storage. --- lang/en/admin.php | 2 +- lib/filestorage/file_storage.php | 106 ++++++++++++++++++ lib/upgradelib.php | 13 ++- mod/assign/feedback/editpdf/classes/pdf.php | 105 ----------------- .../lang/en/assignfeedback_editpdf.php | 12 +- mod/assign/feedback/editpdf/testunoconv.php | 30 ++--- version.php | 2 +- 7 files changed, 140 insertions(+), 130 deletions(-) diff --git a/lang/en/admin.php b/lang/en/admin.php index 76cafeee6e6..4ffb7054d8c 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -1073,7 +1073,7 @@ $string['unbookmarkthispage'] = 'Unbookmark this page'; $string['unicoderequired'] = 'It is required that you store all your data in Unicode format (UTF-8). New installations must be performed into databases that have their default character set as Unicode. If you are upgrading, you should perform the UTF-8 migration process (see the Admin page).'; $string['uninstallplugin'] = 'Uninstall'; $string['unlockaccount'] = 'Unlock account'; -$string['unoconvwarning'] = 'The installed version of your unoconv is not supported, the required version for the assignment grading is 0.7'; +$string['unoconvwarning'] = 'The installed version of your unoconv is not supported, the required version to support assignment grading features is 0.7.'; $string['unsettheme'] = 'Unset theme'; $string['unsupported'] = 'Unsupported'; $string['unsupporteddbstorageengine'] = 'The database storage engine being used is no longer supported.'; diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index e4b1dde181f..9a528d2f16a 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -56,6 +56,24 @@ class file_storage { /** @var array List of formats supported by unoconv */ private $unoconvformats; + // Unoconv constants. + /** No errors */ + const UNOCONVPATH_OK = 'ok'; + /** Not set */ + const UNOCONVPATH_EMPTY = 'empty'; + /** Does not exist */ + const UNOCONVPATH_DOESNOTEXIST = 'doesnotexist'; + /** Is a dir */ + const UNOCONVPATH_ISDIR = 'isdir'; + /** Not executable */ + const UNOCONVPATH_NOTEXECUTABLE = 'notexecutable'; + /** Test file missing */ + const UNOCONVPATH_NOTESTFILE = 'notestfile'; + /** Version not supported */ + const UNOCONVPATH_VERSIONNOTSUPPORTED = 'versionnotsupported'; + /** Any other error */ + const UNOCONVPATH_ERROR = 'error'; + /** * Constructor - do not use directly use {@link get_file_storage()} call instead. @@ -213,6 +231,94 @@ class file_storage { return in_array($sanitized, $this->unoconvformats); } + /** + * Check if the installed version of unoconv is supported. + * + * @return bool true if the present version is supported, false otherwise. + */ + public static function can_convert_documents() { + global $CFG; + $unoconvbin = \escapeshellarg($CFG->pathtounoconv); + $command = "$unoconvbin --version"; + exec($command, $output); + preg_match('/([0-9]+\.[0-9]+)/', $output[0], $matches); + $currentversion = (float)$matches[0]; + $supportedversion = 0.7; + if ($currentversion < $supportedversion) { + return false; + } + + return true; + } + + /** + * If the test pdf has been generated correctly and send it direct to the browser. + */ + public static function send_test_pdf() { + global $CFG; + require_once($CFG->libdir . '/filelib.php'); + + $filerecord = array( + 'contextid' => \context_system::instance()->id, + 'component' => 'test', + 'filearea' => 'assignfeedback_editpdf', + 'itemid' => 0, + 'filepath' => '/', + 'filename' => 'unoconv_test.docx' + ); + + // Get the fixture doc file content and generate and stored_file object. + $fs = get_file_storage(); + $fixturefile = $CFG->libdir . '/tests/fixtures/unoconv-source.docx'; + $fixturedata = file_get_contents($fixturefile); + $testdocx = $fs->get_file($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], + $filerecord['itemid'], $filerecord['filepath'], $filerecord['filename']); + if (!$testdocx) { + $testdocx = $fs->create_file_from_string($filerecord, $fixturedata); + + } + + // Convert the doc file to pdf and send it direct to the browser. + $result = $fs->get_converted_document($testdocx, 'pdf'); + readfile_accel($result, 'application/pdf', true); + } + + /** + * Check if unoconv configured path is correct and working. + * + * @return \stdClass an object with the test status and the UNOCONVPATH_ constant message. + */ + public static function test_unoconv_path() { + global $CFG; + $unoconvpath = $CFG->pathtounoconv; + + $ret = new \stdClass(); + $ret->status = self::UNOCONVPATH_OK; + $ret->message = null; + + if (empty($unoconvpath)) { + $ret->status = self::UNOCONVPATH_EMPTY; + return $ret; + } + if (!file_exists($unoconvpath)) { + $ret->status = self::UNOCONVPATH_DOESNOTEXIST; + return $ret; + } + if (is_dir($unoconvpath)) { + $ret->status = self::UNOCONVPATH_ISDIR; + return $ret; + } + if (!file_is_executable($unoconvpath)) { + $ret->status = self::UNOCONVPATH_NOTEXECUTABLE; + return $ret; + } + if (!\file_storage::can_convert_documents()) { + $ret->status = self::UNOCONVPATH_VERSIONNOTSUPPORTED; + return $ret; + } + + return $ret; + } /** * Perform a file format conversion on the specified document. diff --git a/lib/upgradelib.php b/lib/upgradelib.php index d80f6c0e499..969eeb7ed9d 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -2288,12 +2288,17 @@ function upgrade_install_plugins(array $installable, $confirmed, $heading='', $c * @param environment_results $result object to update, if relevant. * @return environment_results|null updated results or null if unoconv path is not executable. */ -function check_unoconv_version(environment_results $result){ +function check_unoconv_version(environment_results $result) { global $CFG; - if (!during_initial_install() && !empty($CFG->pathtounoconv) && is_executable(trim($CFG->pathtounoconv))) { - - if (assignfeedback_editpdf\pdf::check_unoconv_version_support() === false) { + if (!during_initial_install() && !empty($CFG->pathtounoconv) && file_is_executable(trim($CFG->pathtounoconv))) { + $unoconvbin = \escapeshellarg($CFG->pathtounoconv); + $command = "$unoconvbin --version"; + exec($command, $output); + preg_match('/([0-9]+\.[0-9]+)/', $output[0], $matches); + $currentversion = (float)$matches[0]; + $supportedversion = 0.7; + if ($currentversion < $supportedversion) { $result->setInfo('unoconv version not supported'); $result->setStatus(false); return $result; diff --git a/mod/assign/feedback/editpdf/classes/pdf.php b/mod/assign/feedback/editpdf/classes/pdf.php index 8d0cd5b9594..34db6a14c7a 100644 --- a/mod/assign/feedback/editpdf/classes/pdf.php +++ b/mod/assign/feedback/editpdf/classes/pdf.php @@ -64,23 +64,6 @@ class pdf extends \FPDI { const GSPATH_NOTESTFILE = 'notestfile'; /** Any other error */ const GSPATH_ERROR = 'error'; - /** No errors */ - const UNOCONVPATH_OK = 'ok'; - /** Not set */ - const UNOCONVPATH_EMPTY = 'empty'; - /** Does not exist */ - const UNOCONVPATH_DOESNOTEXIST = 'doesnotexist'; - /** Is a dir */ - const UNOCONVPATH_ISDIR = 'isdir'; - /** Not executable */ - const UNOCONVPATH_NOTEXECUTABLE = 'notexecutable'; - /** Test file missing */ - const UNOCONVPATH_NOTESTFILE = 'notestfile'; - /** Version not supported */ - const UNOCONVPATH_VERSIONNOTSUPPORTED = 'versionnotsupported'; - /** Any other error */ - const UNOCONVPATH_ERROR = 'error'; - /** Min. width an annotation should have */ const MIN_ANNOTATION_WIDTH = 5; /** Min. height an annotation should have */ @@ -610,93 +593,5 @@ class pdf extends \FPDI { die(); } - /** - * If the test pdf has been generated correctly and send it direct to the browser. - */ - public static function send_test_pdf() { - global $CFG; - require_once($CFG->libdir . '/filelib.php'); - - $filerecord = array( - 'contextid' => \context_system::instance()->id, - 'component' => 'test', - 'filearea' => 'assignfeedback_editpdf', - 'itemid' => 0, - 'filepath' => '/', - 'filename' => 'unoconv_test.docx' - ); - - // Get the fixture doc file content and generate and stored_file object. - $fs = get_file_storage(); - $fixturefile = $CFG->libdir . '/tests/fixtures/unoconv-source.docx'; - $fixturedata = file_get_contents($fixturefile); - $testdocx = $fs->get_file($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], - $filerecord['itemid'], $filerecord['filepath'], $filerecord['filename']); - if (!$testdocx) { - $testdocx = $fs->create_file_from_string($filerecord, $fixturedata); - - } - - // Convert the doc file to pdf and send it direct to the browser. - $result = $fs->get_converted_document($testdocx, 'pdf'); - readfile_accel($result, 'application/pdf', true); - } - - /** - * Check if unoconv configured path is correct and working. - * - * @return \stdClass an object with the test status and the UNOCONVPATH_ constant message. - */ - public static function test_unoconv_path() { - global $CFG; - $unoconvpath = $CFG->pathtounoconv; - - $ret = new \stdClass(); - $ret->status = self::UNOCONVPATH_OK; - $ret->message = null; - - if (empty($unoconvpath)) { - $ret->status = self::UNOCONVPATH_EMPTY; - return $ret; - } - if (!file_exists($unoconvpath)) { - $ret->status = self::UNOCONVPATH_DOESNOTEXIST; - return $ret; - } - if (is_dir($unoconvpath)) { - $ret->status = self::UNOCONVPATH_ISDIR; - return $ret; - } - if (!is_executable($unoconvpath)) { - $ret->status = self::UNOCONVPATH_NOTEXECUTABLE; - return $ret; - } - if (self::check_unoconv_version_support() === false) { - $ret->status = self::UNOCONVPATH_VERSIONNOTSUPPORTED; - return $ret; - } - - return $ret; - } - - /** - * Check if the installed version of unoconv is supported. - * - * @return bool true if the present version is supported, false otherwise. - */ - public static function check_unoconv_version_support() { - global $CFG; - $unoconvbin = \escapeshellarg($CFG->pathtounoconv); - $command = "$unoconvbin --version"; - exec($command, $output); - preg_match('/([0-9]+\.[0-9]+)/', $output[0], $matches); - $currentversion = (float)$matches[0]; - $supportedversion = 0.7; - if ($currentversion < $supportedversion) { - return false; - } - - return true; - } } diff --git a/mod/assign/feedback/editpdf/lang/en/assignfeedback_editpdf.php b/mod/assign/feedback/editpdf/lang/en/assignfeedback_editpdf.php index 4318efff736..5f0db7e2b7e 100644 --- a/mod/assign/feedback/editpdf/lang/en/assignfeedback_editpdf.php +++ b/mod/assign/feedback/editpdf/lang/en/assignfeedback_editpdf.php @@ -87,11 +87,13 @@ $string['test_ok'] = 'The ghostscript path appears to be OK - please check you c $string['test_doesnotexist'] = 'The ghostscript path points to a non-existent file'; $string['test_empty'] = 'The ghostscript path is empty - please enter the correct path'; $string['test_unoconv'] = 'Test unoconv path'; -$string['test_unoconv_isdir'] = 'The unoconv path points to a folder, please include the unoconv program in the path you specify'; -$string['test_unoconv_notestfile'] = 'The test DOC is missing'; -$string['test_unoconv_notexecutable'] = 'The unoconv points to a file that is not executable'; -$string['test_unoconv_ok'] = 'The unoconv path appears to properly configured, please click on the button below to download the test pdf file'; -$string['test_unoconv_versionnotsupported'] = 'The minimum supported version for unoconv is 0.7'; +$string['test_unoconvdoesnotexist'] = 'The unoconv path does not point to the unoconv program. Please review your path settings.'; +$string['test_unoconvdownload'] = 'Download converted pdf test file.'; +$string['test_unoconvisdir'] = 'The unoconv path points to a folder, please include the unoconv program in the path you specify'; +$string['test_unoconvnotestfile'] = 'The test DOC is missing'; +$string['test_unoconvnotexecutable'] = 'The unoconv points to a file that is not executable'; +$string['test_unoconvok'] = 'The unoconv path appears to properly configured.'; +$string['test_unoconvversionnotsupported'] = 'The minimum supported version for unoconv is 0.7'; $string['toolbarbutton'] = '{$a->tool} {$a->shortcut}'; $string['tool'] = 'Tool'; $string['viewfeedbackonline'] = 'View annotated PDF...'; diff --git a/mod/assign/feedback/editpdf/testunoconv.php b/mod/assign/feedback/editpdf/testunoconv.php index 6caef6e3c72..c3d4d4c0793 100644 --- a/mod/assign/feedback/editpdf/testunoconv.php +++ b/mod/assign/feedback/editpdf/testunoconv.php @@ -20,8 +20,11 @@ * @copyright 2016 Simey Lameze * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -require('../../../../config.php'); -global $PAGE, $OUTPUT; +require(dirname(__FILE__) . '/../../../../config.php'); +require_once($CFG->libdir . '/filelib.php'); + +$sendpdf = optional_param('sendpdf', 0, PARAM_BOOL); + $PAGE->set_url(new moodle_url('/mod/assign/feedback/editpdf/testunoconv.php')); $PAGE->set_context(context_system::instance()); @@ -34,39 +37,38 @@ $PAGE->navbar->add(get_string('plugins', 'admin')); $PAGE->navbar->add(get_string('assignmentplugins', 'mod_assign')); $PAGE->navbar->add(get_string('feedbackplugins', 'mod_assign')); $PAGE->navbar->add(get_string('pluginname', 'assignfeedback_editpdf'), - new moodle_url('/admin/settings.php?section=assignfeedback_editpdf')); + new moodle_url('/admin/settings.php', array('section' => 'assignfeedback_editpdf'))); $PAGE->navbar->add($strheading); $PAGE->set_heading($strheading); $PAGE->set_title($strheading); -$sendpdf = optional_param('sendpdf', 0, PARAM_BOOL); if ($sendpdf) { + require_sesskey(); // Serve the generated test pdf. - assignfeedback_editpdf\pdf::send_test_pdf(); + file_storage::send_test_pdf(); die(); } -$result = assignfeedback_editpdf\pdf::test_unoconv_path(); +$result = file_storage::test_unoconv_path(); switch ($result->status) { - - case assignfeedback_editpdf\pdf::UNOCONVPATH_OK: - $msg = get_string('test_unoconv_ok', 'assignfeedback_editpdf'); + case file_storage::UNOCONVPATH_OK: + $msg = get_string('test_unoconvok', 'assignfeedback_editpdf'); $msg .= html_writer::empty_tag('br'); - $pdflink = new moodle_url($PAGE->url, array('sendpdf' => 1)); - $msg .= $OUTPUT->single_button($pdflink, get_string('download')); + $pdflink = new moodle_url($PAGE->url, array('sendpdf' => 1, 'sesskey' => sesskey())); + $msg .= html_writer::link($pdflink, get_string('test_unoconvdownload', 'assignfeedback_editpdf')); $msg .= html_writer::empty_tag('br'); break; - case assignfeedback_editpdf\pdf::UNOCONVPATH_ERROR: + case file_storage::UNOCONVPATH_ERROR: $msg = $result->message; break; default: - $msg = get_string("test_unoconv_{$result->status}", 'assignfeedback_editpdf'); + $msg = get_string("test_unoconv{$result->status}", 'assignfeedback_editpdf'); break; } $returl = new moodle_url('/admin/settings.php', array('section' => 'assignfeedback_editpdf')); $msg .= $OUTPUT->continue_button($returl); echo $OUTPUT->header(); -echo $OUTPUT->box($msg, 'generalbox '); +echo $OUTPUT->box($msg, 'generalbox'); echo $OUTPUT->footer(); diff --git a/version.php b/version.php index 334e6251474..d70307e70b3 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2016051300.01; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2016051300.02; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. -- 2.43.0