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

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: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent 268425bd
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -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'));
+55 −2
Original line number Diff line number Diff line
@@ -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)) {
+6 −2
Original line number Diff line number Diff line
@@ -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';
$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';
+34 −1
Original line number Diff line number Diff line
@@ -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);
+9 −19
Original line number Diff line number Diff line
@@ -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);
Loading