Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/allow save blueprint content #3

Merged
merged 5 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
30 changes: 30 additions & 0 deletions model/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,36 @@ 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) {
$value = intval($value);
if (! $this->getResource($selection)->exists() || $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
*
Expand Down
12 changes: 12 additions & 0 deletions model/storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions model/storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
13 changes: 13 additions & 0 deletions model/storage/implementation/JsonStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions scripts/update/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ public function update($initialVersion)

$this->setVersion('0.2.0');
}

$this->skip('0.2.0', '0.3.0');
}
}