Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| taoQtiTest_actions_XmlEditor | |
0.00% |
0 / 35 |
|
0.00% |
0 / 2 |
110 | |
0.00% |
0 / 1 |
| edit | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
90 | |||
| getXmlEditorService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | use oat\generis\model\OntologyAwareTrait; |
| 24 | use oat\tao\model\resources\ResourceAccessDeniedException; |
| 25 | use oat\taoQtiTest\models\forms\XmlEditForm; |
| 26 | use oat\taoQtiTest\models\xmlEditor\XmlEditorInterface; |
| 27 | use qtism\data\storage\xml\XmlStorageException; |
| 28 | use tao_helpers_form_FormContainer as FormContainer; |
| 29 | |
| 30 | class taoQtiTest_actions_XmlEditor extends tao_actions_ServiceModule |
| 31 | { |
| 32 | use OntologyAwareTrait; |
| 33 | |
| 34 | public function edit(): void |
| 35 | { |
| 36 | if (!$this->hasPostParameter('id')) { |
| 37 | $this->returnError(__('Missed required parameter \'id\'')); |
| 38 | return; |
| 39 | } |
| 40 | $test = $this->getResource($this->getPostParameter('id')); |
| 41 | |
| 42 | if ($this->getXmlEditorService()->isLocked()) { |
| 43 | $this->setData( |
| 44 | 'errorMessage', |
| 45 | __('This functionality is blocked. Please contact with your administrator for more details.') |
| 46 | ); |
| 47 | } else { |
| 48 | try { |
| 49 | $xmlString = $this->getXmlEditorService()->getTestXml($test); |
| 50 | |
| 51 | $formContainer = new XmlEditForm( |
| 52 | $test, |
| 53 | $xmlString, |
| 54 | [FormContainer::CSRF_PROTECTION_OPTION => true] |
| 55 | ); |
| 56 | $form = $formContainer->getForm(); |
| 57 | if ($form->isSubmited() && $form->isValid()) { |
| 58 | $this->getXmlEditorService()->saveStringTest($test, $form->getValues()['xmlString']); |
| 59 | $this->setData('message', __('Saved')); |
| 60 | } |
| 61 | } catch (ResourceAccessDeniedException $e) { |
| 62 | $this->setData('errorMessage', $e->getMessage()); |
| 63 | common_Logger::e($e->getMessage()); |
| 64 | } catch (XmlStorageException $e) { |
| 65 | $errors = $e->getErrors(); |
| 66 | $message = ''; |
| 67 | /** @var LibXMLError $error */ |
| 68 | foreach ($errors->getArrayCopy() as $error) { |
| 69 | $message .= $error->message; |
| 70 | } |
| 71 | $this->setData('errorMessage', $message); |
| 72 | } catch (Throwable $e) { |
| 73 | $this->setData('errorMessage', __('Something went wrong...')); |
| 74 | common_Logger::e($e->getMessage()); |
| 75 | } |
| 76 | $this->setData('form', $form->render()); |
| 77 | $this->setData('formTitle', __('XML Content')); |
| 78 | } |
| 79 | |
| 80 | $this->setView('xml_editor.tpl'); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return XmlEditorInterface |
| 85 | */ |
| 86 | private function getXmlEditorService(): XmlEditorInterface |
| 87 | { |
| 88 | return $this->getServiceLocator()->get(XmlEditorInterface::SERVICE_ID); |
| 89 | } |
| 90 | } |