Commit 1f9af265 authored by Robert Lyon's avatar Robert Lyon

Bug 1409546: Altering matrix upload to a file upload field

And removing the matrices folder after the plugin is installed

Change-Id: Ife603336ab6a150717353e1c8df995fd36f3e117
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent bb84ffc2
......@@ -15,9 +15,12 @@ $string['Framework'] = 'SmartEvidence framework';
$string['frameworkdesc'] = 'Choose the competency framework that you want to associate with your portfolio.';
$string['taskscompleted'] = 'Tasks completed';
$string['addpages'] = 'Add more pages to this collection if you want them to show up here in the SmartEvidence map.';
$string['matrixname'] = 'Name of matrix file';
$string['matrixnamedesc'] = 'The name of the matrix file that you have added to the "module/framework/matrices/" directory';
$string['errorbadmatrixname'] = 'Unable to find the matrix file';
$string['matrixfile'] = 'Matrix file';
$string['matrixfiledesc'] = 'The .matrix file containing the JSON encoded matrix framework';
$string['notvalidmatrixfile'] = 'Not a valid matrix file';
$string['invalidjson'] = 'Not a valid matrix file. Unable to parse the JSON content.';
$string['jsonmissingvars'] = 'Not a valid matrix file. Missing "framework" and/or "framework name".';
$string['manuallyremovematrices'] = 'Unable to remove install directory "%s". Please remove it manually.';
$string['changeframeworkproblems'] = 'You cannot change the framework. The following pages have evidence connected with them:';
$string['accessdeniednoviews'] = 'You need to add some pages to your collection.';
$string['noframeworkselected'] = 'None';
......
......@@ -50,27 +50,56 @@ class PluginModuleFramework extends PluginModule {
}
// Add in any smart evidence framework data to the framework tables
// based on any existing .matrix files in the matricies directory
$files = glob(get_config('docroot') . 'module/framework/matrices/*.matrix');
$matricesdir = get_config('docroot') . 'module/framework/matrices/';
$files = glob($matricesdir . '*.matrix');
foreach ($files as $file) {
self::add_matrix_to_db($file);
}
require_once('file.php');
if (!@rmdirr($matricesdir)) {
log_warn(get_string('manuallyremovematrices', 'module.framework', $matricesdir), true, false);
}
}
}
public function matrix_is_valid_json($filename) {
$ok = array('error' => false);
$matrix = file_get_contents($filename);
if (!$matrix) {
$ok['error'] = true;
$ok['message'] = get_string('invalidfilename', 'admin', $filename);
}
else {
$content = json_decode($matrix);
if (is_null($content)) {
$ok['error'] = true;
$ok['message'] = get_string('invalidjson', 'module.framework');
}
else {
if (empty($content->framework) || empty($content->framework->name)) {
$ok['error'] = true;
$ok['message'] = get_string('jsonmissingvars', 'module.framework');
}
else {
$ok['content'] = $content;
}
}
}
return $ok;
}
private function add_matrix_to_db($filename) {
if (substr_count($filename, '/') == 0) {
$filename = get_config('docroot') . 'module/framework/matrices/' . $filename;
}
$matrix = file_get_contents($filename);
$content = json_decode($matrix);
if (!$content || empty($content->framework) || empty($content->framework->name)) {
log_warn($file . ' is not a valid JSON file');
$ok = self::matrix_is_valid_json($filename);
if ($ok['error']) {
return false;
}
else {
safe_require('module', 'framework');
$framework = new Framework(null, $content->framework);
$framework = new Framework(null, $ok['content']->framework);
$framework->commit();
}
}
......@@ -82,14 +111,14 @@ class PluginModuleFramework extends PluginModule {
public static function get_config_options() {
$elements = array(
'matrix' => array(
'type' => 'text',
'size' => 50,
'title' => get_string('matrixname', 'module.framework'),
'type' => 'file',
'title' => get_string('matrixfile', 'module.framework'),
'description' => get_string('matrixfiledesc', 'module.framework'),
'accept' => '.matrix',
'rules' => array(
'required' => true,
),
'description' => get_string('matrixnamedesc', 'module.framework'),
)
'required' => true
)
),
);
return array(
......@@ -98,16 +127,27 @@ class PluginModuleFramework extends PluginModule {
}
public static function validate_config_options(Pieform $form, $values) {
if (empty($values['matrix']) ||
preg_match("/\.matrix$/", $values['matrix']) === 0 ||
!file_exists(get_config('docroot') . 'module/framework/matrices/' . $values['matrix'])
) {
$form->set_error('matrix', get_string('errorbadmatrixname', 'module.framework'));
require_once('uploadmanager.php');
$um = new upload_manager('matrix');
if ($error = $um->preprocess_file()) {
$form->set_error('matrix', $error);
}
if (!$um->optionalandnotsupplied) {
$reqext = ".matrix";
$fileext = substr($values['matrix']['name'], (-1 * strlen($reqext)));
if ($fileext !== $reqext) {
$form->set_error('matrix', get_string('notvalidmatrixfile', 'module.framework'));
}
}
$matrixfile = self::matrix_is_valid_json($um->file['tmp_name']);
if ($matrixfile['error']) {
$form->set_error('matrix', $matrixfile['message']);
}
}
public static function save_config_options(Pieform $form, $values) {
self::add_matrix_to_db($values['matrix']);
self::add_matrix_to_db($values['matrix']['tmp_name']);
}
}
......
......@@ -35,7 +35,9 @@ Scenario: Installing framework module and activating for an institution
# Make sure we have a matrix config form
And I scroll to the base of id "module.framework"
And I follow "Configuration for module smartevidence"
Then I should see "Name of matrix file"
And I attach the file "example.matrix" to "Matrix file"
And I press "Save"
Then I should see "Settings saved"
# Activate smartevidence in an institution
And I choose "Institutions" in "Institutions"
......
{"framework":
{"institution":"all",
"name":"SmartEvidence example",
"description":"This is the example SmartEvidence matrix",
"selfassess":true,
"standards":
[
{
"shortname":"Professional knowledge 1.",
"name":"Know the students and how they learn",
"description":"",
"priority":1,
"standardelement":
[
{
"shortname":"1.1",
"name":"one point one",
"description":"This is the first sub standard of standard 1",
"priority":1,
"parent":false
},
{
"shortname":"1.2",
"name":"one point two",
"description":"This is the second sub standard of standard 1",
"priority":2,
"parent":false
},
{
"shortname":"1.2.1",
"name":"one point two point one",
"description":"This is the second sub standard of standard 1 with sub clause one",
"priority":3,
"parent":true
},
{
"shortname":"1.3",
"name":"one point three",
"description":"This is the third sub standard of standard 1",
"priority":4,
"parent":false
}
]
},
{
"shortname":"Professional knowledge 2.",
"name":"Know the content and how to teach it",
"description":"Special descritpion for this one",
"priority":2,
"standardelement":
[
{
"shortname":"2.1",
"name":"two point one",
"description":"This is the first sub standard of standard 2",
"priority":1,
"parent":false
},
{
"shortname":"2.2",
"name":"two point two",
"description":"This is the second sub standard of standard 2",
"priority":2,
"parent":false
},
{
"shortname":"2.3",
"name":"two point three",
"description":"This is the third sub standard of standard 2",
"priority":3,
"parent":false
},
{
"shortname":"2.4",
"name":"two point four",
"description":"This is the fourth sub standard of standard 2",
"priority":4,
"parent":false
},
{
"shortname":"2.5",
"name":"two point five",
"description":"This is the fifth sub standard of standard 2",
"priority":5,
"parent":false
},
{
"shortname":"2.6",
"name":"two point six",
"description":"This is the sixth sub standard of standard 2",
"priority":6,
"parent":false
}
]
},
{
"shortname":"Unused standard 1.",
"name":"Will add standardelements later",
"description":"This is an empty standard",
"priority":3
},
{
"shortname":"Professional practice 1.",
"name":"Plan for and implement effective teaching and learning",
"description":"",
"priority":4,
"standardelement":
[
{
"shortname":"4.1",
"name":"four point one",
"description":"This is the first sub standard of standard 4",
"priority":1,
"parent":false
},
{
"shortname":"4.2",
"name":"four point two",
"description":"This is the second sub standard of standard 4",
"priority":2,
"parent":false
}
]
}
]
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment