Commit 49a48366 authored by Robert Lyon's avatar Robert Lyon

Bug 1409546: Add framework option to add/edit collection form

And centralising the checks for collection can_have_framework() and
has_framework()

And making sure that one cant change framework if competencies exist
for the current framework

Change-Id: I5918c4e3f463388a0b03511eb9ef7adc24babcec
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 268425bd
......@@ -109,6 +109,7 @@ $form = pieform(array(
'name' => 'edit',
'plugintype' => 'core',
'pluginname' => 'collection',
'validatecallback' => 'validate',
'successcallback' => 'submit',
'elements' => $elements,
));
......@@ -121,9 +122,32 @@ $smarty->assign('headingclass', 'page-header');
$smarty->assign_by_ref('form', $form);
$smarty->display('collection/edit.tpl');
function validate(Pieform $form, $values) {
$rendered = $form->get_property('elements');
if (isset($rendered['framework'])) {
if (!empty($rendered['framework']['defaultvalue']) && $rendered['framework']['defaultvalue'] != $values['framework']) {
// Make sure that if the user is changing the framework that there isn't annotations paired to the old framework
$views = get_records_sql_array("SELECT v.id, v.title FROM {view} v
JOIN {collection_view} cv ON cv.view = v.id
JOIN {framework_evidence} fe ON fe.view = cv.view
WHERE cv.collection = ?", array($rendered['id']['value']));
if (!empty($views)) {
$errorstr = get_string('changeframeworkproblems', 'module.framework');
foreach ($views as $view) {
$errorstr .= " '" . $view->title . "'";
}
$form->set_error('framework', $errorstr);
}
}
}
}
function submit(Pieform $form, $values) {
global $SESSION, $new, $copy, $urlparams;
$values['navigation'] = (int) $values['navigation'];
if (empty($values['framework'])) {
$values['framework'] = null;
}
$collection = Collection::save($values);
if (!$new) {
$SESSION->add_ok_msg(get_string('collectionsaved', 'collection'));
......
......@@ -474,6 +474,20 @@ class Collection {
'defaultvalue' => 1,
),
);
if ($frameworks = $this->can_have_framework()) {
$options = array('' => get_string('noframeworkselected', 'module.framework'));
foreach ($frameworks as $framework) {
$options[$framework->id] = $framework->name;
}
$elements['framework'] = array(
'type' => 'select',
'title' => get_string('Framework', 'module.framework'),
'options' => $options,
'defaultvalue' => $this->framework,
'width' => '280px',
'description' => get_string('frameworkdesc', 'module.framework'),
);
}
// populate the fields with the existing values if any
if (!empty($this->id)) {
......@@ -555,15 +569,54 @@ class Collection {
}
/**
* Check that a collection has a framework
* Check that a collection can have a framework
* - The collection is not owned by a group
* - The framework plugin is active
* - The institution has 'SmartEvidence' turned on
* - There frameworks available for the institution
*
* @return mixed array of available frameworks or false
*/
public function can_have_framework() {
if (!empty($this->group)) {
return false;
}
safe_require('module', 'framework');
if (!PluginModuleFramework::is_active()) {
return false;
}
if ($this->institution) {
$institution = $this->institution;
}
else {
$user = new User();
$user->find_by_id($this->owner);
$institutions = array_keys($user->get('institutions'));
$institution = (!empty($institutions)) ? $institutions[0] : 'mahara';
}
$institution = new Institution($institution);
// Check that smart evidence is enabled for the institution
if (!$institution->allowinstitutionsmartevidence) {
return false;
}
safe_require('module','framework');
$frameworks = Framework::get_frameworks($institution->name, true);
return $frameworks;
}
/**
* Check that a collection has a framework
* - The collection can have a framework
* - It has a framework id
* - It has views in the collection
*
* @return boolean
*/
public function has_framework() {
if (!empty($this->group)) {
if (!$this->can_have_framework()) {
return false;
}
if (empty($this->framework)) {
......
......@@ -11,9 +11,13 @@
defined('INTERNAL') || die();
$string['accessdeniedsmartevidencenotallowed'] = 'SmartEvidence needs to be enabled for the institution "%s".';
$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';
\ No newline at end of file
$string['errorbadmatrixname'] = 'Unable to find the matrix file';
$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';
......@@ -390,12 +390,45 @@ class Framework {
);
}
/**
* Get available frameworks based on institution
*
* @param string $institution If set to 'any' all results returned
* @param boolean $shared Return frameworks that can be viewed by all institutions
*
* @return frameworks
*/
public function get_frameworks($institution = 'any', $shared = false) {
global $USER;
if ($USER->get('admin')) {
$institution = 'any'; // make sure site admin can get full list regardless
}
$sql = "SELECT * FROM {framework}";
$values = array();
if ($institution != 'any') {
// Only get the frameworks available to this institution
$placeholders = '?';
$values[] = $institution;
if ($shared) {
// Include frameworks with institution set to 'all'
$placeholders .= ',?';
$values[] = 'all';
}
$sql .= " WHERE institution IN (" . $placeholders . ")";
}
$sql .= " ORDER BY name";
$frameworks = get_records_sql_array($sql, $values);
return $frameworks;
}
/**
* Get the evidence state for the framework
*
* @param int $userid
*
* @return outcomes
* @return evidence(s)
*/
public function get_evidence() {
$outcomes = get_records_array('framework_evidence', 'framework', $this->id);
......
......@@ -30,31 +30,21 @@ safe_require('artefact', 'comment');
$collectionid = param_integer('id');
$collection = new Collection($collectionid);
$owner = $collection->get('owner');
$institution = $collection->get('institution');
if (!$collection->has_framework()) {
$errorstr = get_string('accessdenied', 'error');
throw new AccessDeniedException($errorstr);
}
if ($owner) {
// Find what institution they belong to
// Use first one if they belong to multiple
$user = new User();
$user->find_by_id($owner);
$institutions = array_keys($user->get('institutions'));
$institution = (!empty($institutions)) ? $institutions[0] : 'mahara';
}
$institution = new Institution($institution);
// Check that smart evidence is enabled for the institution
if (!$institution->allowinstitutionsmartevidence) {
$errorstr = get_string('accessdeniedsmartevidencenotallowed', 'module.framework', $institution->displayname);
$views = $collection->get('views');
if (empty($views)) {
$errorstr = get_string('accessdeniednoviews', 'module.framework');
throw new AccessDeniedException($errorstr);
}
$views = $collection->get('views');
// Get the first view from the collection
$firstview = $views['views'][0];
$view = new View($firstview->id);
if (!$collection->has_framework()) {
// We can't show the matrix page so redirect them to the first page of the collection instead
redirect($view->get_url());
}
if (!can_view_view($view->get('id'))) {
$errorstr = get_string('accessdenied', 'error');
throw new AccessDeniedException($errorstr);
......
@javascript @core @core_administration
Feature: Make sure 'framework' module is installed and active
Feature: Make sure 'framework' module is installed and we can add it
to a collection
In order to use SmartEvidence
As an admin
So I can benefit from the recording/marking of SmartEvidence in a
Mahara institution
Scenario: Installing framework module and actoivating for an institution
Background:
Given the following "pages" exist:
| title | description| ownertype | ownername |
| PageA | This is page A | user | admin |
| PageB | This is page B | user | admin |
| PageC | This is page C | user | admin |
| PageD | This is page D | user | admin |
| PageE | This is page E | user | admin |
| PageF | This is page F | user | admin |
| PageG | This is page G | user | admin |
| PageH | This is page H | user | admin |
And the following "collections" exist:
| title | description| ownertype | ownername | pages |
| CollA | This is collection A | user | admin | PageA, PageB, PageC, PageD, PageE, PageF, PageG, PageH |
Scenario: Installing framework module and activating for an institution
Given I log in as "admin" with password "Kupuhipa1"
And I follow "Administration"
And I choose "Plugin administration" in "Extensions"
......@@ -24,3 +42,11 @@ Scenario: Installing framework module and actoivating for an institution
And I enable the switch "Allow SmartEvidence"
And I press "Submit"
Then I should see "Institution updated successfully."
# Adding framework to existing collection
And I follow "Return to site"
And I choose "Collections" in "Portfolio"
And I follow "Edit title and description"
And I select "SmartEvidence" from "SmartEvidence framework"
And I press "Save"
Then I should see "Collection saved successfully."
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