From 7841fae8158c2ac15bad0b799ff9dd690e3ef404 Mon Sep 17 00:00:00 2001 From: cmoyon Date: Fri, 2 Dec 2016 09:20:32 +0100 Subject: [PATCH 1/5] Add save content feature --- controller/Blueprints.php | 17 ++++++++++++ model/Service.php | 29 ++++++++++++++++++++ model/storage/FileStorage.php | 12 ++++++++ model/storage/Storage.php | 9 ++++++ model/storage/implementation/JsonStorage.php | 13 +++++++++ 5 files changed, 80 insertions(+) diff --git a/controller/Blueprints.php b/controller/Blueprints.php index 4cd70b4..e91ff09 100644 --- a/controller/Blueprints.php +++ b/controller/Blueprints.php @@ -151,4 +151,21 @@ public function editBlueprintClass() $this->setView('form.tpl', 'tao'); } + public function saveBlueprints() + { + if (! $this->hasRequestParameter('uri') || ! $this->hasRequestParameter('matrix')) { + $this->returnJson(['error' => __('Missing parameters missing.')], 400); + return; + } + + $blueprints = $this->getClass($this->getRequestParameter('uri')); + $matrix = json_decode($this->getRequestParameter('matrix'), trues); + if (! is_array($matrix)) { + $this->returnJson(['error' => __('Blueprints matrix has to be an array.')], 400); + return; + } + + $this->getClassService()->saveBlueprintsMatrix($blueprints, $matrix); + } + } \ No newline at end of file diff --git a/model/Service.php b/model/Service.php index 586872b..3f58f20 100644 --- a/model/Service.php +++ b/model/Service.php @@ -192,6 +192,35 @@ public function deleteResource(\core_kernel_classes_Resource $resource) return $resource->delete(); } + /** + * Check if matrix is saved and store it to blueprint content + * + * @param $uri + * @param array $matrix + * @return \common_report_Report + */ + public function saveBlueprintsMatrix($uri, array $matrix) + { + $blueprints = $this->getResource($uri); + if (! $blueprints->exists()) { + return \common_report_Report::createFailure('Unable to find blueprint to save matrix.'); + } + + foreach ($matrix as $selection => $value) { + if (! $this->getResource($selection)->exists() || intval($value) == 0 ) { + return \common_report_Report::createFailure('Matrix is not correctly set.'); + } + } + + $content = $this->getFileStorage()->getContent($blueprints); + $content['selection'] = $matrix; + if ($this->getFileStorage()->setContent($blueprints, $content)) { + return \common_report_Report::createSuccess('Blueprint successfully saved.'); + } + + return \common_report_Report::createFailure('Error on saving blueprint content successfully saved.'); + } + /** * Get the service to handle blueprint files * diff --git a/model/storage/FileStorage.php b/model/storage/FileStorage.php index c1fac5d..9c7130e 100644 --- a/model/storage/FileStorage.php +++ b/model/storage/FileStorage.php @@ -103,6 +103,18 @@ public function getContent(\core_kernel_classes_Resource $blueprint) return $file->read(); } + /** + * Save a content for a given blueprint + * + * @param \core_kernel_classes_Resource $blueprint + * @param array $content + * @return boolean + */ + public function setContent(\core_kernel_classes_Resource $blueprint, $content) + { + return $this->getFile($blueprint)->write($content); + } + /** * Delete a blueprint * diff --git a/model/storage/Storage.php b/model/storage/Storage.php index b048c8b..b0e0b7c 100644 --- a/model/storage/Storage.php +++ b/model/storage/Storage.php @@ -58,6 +58,15 @@ public function getContent(\core_kernel_classes_Resource $blueprint); */ public function hasContent(\core_kernel_classes_Resource $blueprint); + /** + * Save a content for a given blueprint + * + * @param \core_kernel_classes_Resource $blueprint + * @param $content + * @return boolean + */ + public function setContent(\core_kernel_classes_Resource $blueprint, $content); + /** * Delete a content for a given blueprint * diff --git a/model/storage/implementation/JsonStorage.php b/model/storage/implementation/JsonStorage.php index b082498..2ead7f2 100644 --- a/model/storage/implementation/JsonStorage.php +++ b/model/storage/implementation/JsonStorage.php @@ -43,6 +43,19 @@ public function getContent(\core_kernel_classes_Resource $blueprint) return json_decode($content, true); } + /** + * Save a json_encoded content for a given blueprint + * + * @param \core_kernel_classes_Resource $blueprint + * @param $content + * @return boolean + */ + public function setContent(\core_kernel_classes_Resource $blueprint, $content) + { + $content = json_encode($content, JSON_PRETTY_PRINT); + return parent::setContent($blueprint, $content); + } + /** * Get the default content for a blueprint file * From ce675414ec83f93bfc59087f6398b34c1980a9d6 Mon Sep 17 00:00:00 2001 From: cmoyon Date: Fri, 2 Dec 2016 09:21:32 +0100 Subject: [PATCH 2/5] Bump to version 0.3.0 --- manifest.php | 2 +- scripts/update/Updater.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/manifest.php b/manifest.php index 8082ed9..11dfe79 100644 --- a/manifest.php +++ b/manifest.php @@ -27,7 +27,7 @@ 'label' => 'Blueprints Extension', 'description' => 'Extension to manage Test Blueprints', 'license' => 'GPL-2.0', - 'version' => '0.2.0', + 'version' => '0.3.0', 'author' => 'Open Assessment Technologies SA', 'requires' => [ 'tao' => '>=7.29.0' diff --git a/scripts/update/Updater.php b/scripts/update/Updater.php index e0e7264..8848682 100644 --- a/scripts/update/Updater.php +++ b/scripts/update/Updater.php @@ -39,5 +39,7 @@ public function update($initialVersion) $this->setVersion('0.2.0'); } + + $this->skip('0.2.0', '0.3.0'); } } \ No newline at end of file From 24817a06ed3a1bc899dca3dc40a8bcc7e3190245 Mon Sep 17 00:00:00 2001 From: cmoyon Date: Fri, 2 Dec 2016 09:26:27 +0100 Subject: [PATCH 3/5] Remove controller method --- controller/Blueprints.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/controller/Blueprints.php b/controller/Blueprints.php index e91ff09..4cd70b4 100644 --- a/controller/Blueprints.php +++ b/controller/Blueprints.php @@ -151,21 +151,4 @@ public function editBlueprintClass() $this->setView('form.tpl', 'tao'); } - public function saveBlueprints() - { - if (! $this->hasRequestParameter('uri') || ! $this->hasRequestParameter('matrix')) { - $this->returnJson(['error' => __('Missing parameters missing.')], 400); - return; - } - - $blueprints = $this->getClass($this->getRequestParameter('uri')); - $matrix = json_decode($this->getRequestParameter('matrix'), trues); - if (! is_array($matrix)) { - $this->returnJson(['error' => __('Blueprints matrix has to be an array.')], 400); - return; - } - - $this->getClassService()->saveBlueprintsMatrix($blueprints, $matrix); - } - } \ No newline at end of file From fba12fc2f97f4ad4279d5a89110021ed02eb8059 Mon Sep 17 00:00:00 2001 From: cmoyon Date: Fri, 2 Dec 2016 09:58:40 +0100 Subject: [PATCH 4/5] Translated message --- model/Service.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/model/Service.php b/model/Service.php index 3f58f20..e422d95 100644 --- a/model/Service.php +++ b/model/Service.php @@ -203,22 +203,22 @@ public function saveBlueprintsMatrix($uri, array $matrix) { $blueprints = $this->getResource($uri); if (! $blueprints->exists()) { - return \common_report_Report::createFailure('Unable to find blueprint to save matrix.'); + return \common_report_Report::createFailure(__('Unable to find blueprint to save matrix.')); } foreach ($matrix as $selection => $value) { if (! $this->getResource($selection)->exists() || intval($value) == 0 ) { - return \common_report_Report::createFailure('Matrix is not correctly set.'); + return \common_report_Report::createFailure(__('Matrix is not correctly set.')); } } $content = $this->getFileStorage()->getContent($blueprints); $content['selection'] = $matrix; if ($this->getFileStorage()->setContent($blueprints, $content)) { - return \common_report_Report::createSuccess('Blueprint successfully saved.'); + return \common_report_Report::createSuccess(__('Blueprint successfully saved.')); } - return \common_report_Report::createFailure('Error on saving blueprint content successfully saved.'); + return \common_report_Report::createFailure(__('Error on saving blueprint content successfully saved.')); } /** From 406069817678e882abd7571dc80f5f53713de107 Mon Sep 17 00:00:00 2001 From: cmoyon Date: Fri, 2 Dec 2016 10:03:49 +0100 Subject: [PATCH 5/5] Change selection value to int & allow value as zero or more --- model/Service.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/model/Service.php b/model/Service.php index e422d95..4e0a731 100644 --- a/model/Service.php +++ b/model/Service.php @@ -206,8 +206,9 @@ public function saveBlueprintsMatrix($uri, array $matrix) return \common_report_Report::createFailure(__('Unable to find blueprint to save matrix.')); } - foreach ($matrix as $selection => $value) { - if (! $this->getResource($selection)->exists() || intval($value) == 0 ) { + foreach ($matrix as $selection => &$value) { + $value = intval($value); + if (! $this->getResource($selection)->exists() || $value >= 0) { return \common_report_Report::createFailure(__('Matrix is not correctly set.')); } }