Esempio n. 1
0
 /**
  * Provide options to the javascript.
  *
  * @return array An array of options.
  */
 public function get_js_opts()
 {
     global $CFG;
     $usersetid = required_param('id', PARAM_INT);
     $usersetclassification = usersetclassification::get_for_cluster($usersetid);
     $autoenroldefault = !empty($usersetclassification->param_autoenrol_tracks) ? 1 : 0;
     $opts = parent::get_js_opts();
     $opts['condition'] = $this->condition;
     $opts['opts']['actionurl'] = $this->endpoint;
     $opts['opts']['autoenroldefault'] = $autoenroldefault;
     $opts['opts']['desc_single'] = $this->descsingle;
     $opts['opts']['desc_multiple'] = $this->descmultiple;
     $opts['opts']['mode'] = 'assign';
     $opts['opts']['lang_bulk_confirm'] = get_string('ds_bulk_confirm', 'local_elisprogram');
     $opts['opts']['lang_working'] = get_string('ds_working', 'local_elisprogram');
     $opts['opts']['langautoenrol'] = get_string('usersettrack_auto_enrol', 'local_elisprogram');
     $opts['opts']['langyes'] = get_string('yes', 'moodle');
     $opts['opts']['langno'] = get_string('no', 'moodle');
     return $opts;
 }
Esempio n. 2
0
/**
 * Run all upgrade steps from before elis 2.6.
 *
 * @param int $oldversion The currently installed version of the old component.
 * @return bool Success/Failure.
 */
function elisprogram_usetclassify_pre26upgradesteps($oldversion)
{
    global $CFG, $THEME, $DB;
    $dbman = $DB->get_manager();
    $result = true;
    if ($oldversion < 2011071400) {
        // Rename field.
        $field = field::find(new field_filter('shortname', '_elis_cluster_classification'));
        if ($field->valid()) {
            $field = $field->current();
            $field->shortname = USERSET_CLASSIFICATION_FIELD;
            if ($field->name == 'Cluster classification') {
                // Rhe field name hasn't been changed from the old default.
                $field->name = get_string('classification_field_name', 'elisprogram_usetclassify');
            }
            $field->save();
            $category = $field->category;
            if ($category->name == 'Cluster classification') {
                // The field name hasn't been changed from the old default.
                $category->name = get_string('classification_category_name', 'elisprogram_usetclassify');
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011071400, 'pmplugins', 'userset_classification');
    }
    if ($result && $oldversion < 2011101200) {
        $field = field::find(new field_filter('shortname', USERSET_CLASSIFICATION_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if ($owner = new field_owner(!isset($field->owners) || !isset($field->owners['manual']) ? false : $field->owners['manual'])) {
                $owner->fieldid = $field->id;
                $owner->plugin = 'manual';
                //$owner->exclude = 0; // TBD
                $owner->param_help_file = 'elisprogram_usetclassify/cluster_classification';
                $owner->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101200, 'pmplugins', 'userset_classification');
    }
    if ($result && $oldversion < 2011101800) {
        // Userset -> 'User Set'.
        $field = field::find(new field_filter('shortname', USERSET_CLASSIFICATION_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            if (stripos($field->name, 'Userset') !== false) {
                $field->name = str_ireplace('Userset', 'User Set', $field->name);
                $field->save();
            }
            $category = $field->category;
            if (stripos($category->name, 'Userset') !== false) {
                $category->name = str_ireplace('Userset', 'User Set', $category->name);
                $category->save();
            }
        }
        upgrade_plugin_savepoint($result, 2011101800, 'pmplugins', 'userset_classification');
    }
    if ($result && $oldversion < 2011110300) {
        // Make sure to rename the default classification name from "Cluster" to "User set".
        require_once elispm::file('plugins/usetclassify/usersetclassification.class.php');
        // Make sure there are no custom fields with invalid categories.
        pm_fix_orphaned_fields();
        $field = field::find(new field_filter('shortname', USERSET_CLASSIFICATION_FIELD));
        if ($field->valid()) {
            $field = $field->current();
            $category = $field->category;
            $default = usersetclassification::find(new field_filter('shortname', 'regular'));
            if ($default->valid()) {
                $default = $default->current();
                $default->name = get_string('cluster', 'local_elisprogram');
                $default->save();
            }
            // Upgrade field owner data for the default User Set field.
            $field = field::ensure_field_exists_for_context_level($field, CONTEXT_ELIS_USERSET, $category);
            $owners = field_owner::find(new field_filter('fieldid', $field->id));
            if ($owners->valid()) {
                foreach ($owners as $owner) {
                    if ($owner->plugin == 'cluster_classification') {
                        $owner->plugin = 'userset_classification';
                        $owner->save();
                    } else {
                        if ($owner->plugin == 'manual') {
                            $owner->param_options_source = 'userset_classifications';
                            $owner->param_help_file = 'elisprogram_usetclassify/cluster_classification';
                            $owner->save();
                        }
                    }
                }
            }
            upgrade_plugin_savepoint($result, 2011110300, 'pmplugins', 'userset_classification');
        }
    }
    return $result;
}
Esempio n. 3
0
 /**
  * Get the default autoenrol setting for each userset.
  * @param array $row An array for a single result.
  * @return array The transformed result.
  */
 protected function results_row_transform(array $row)
 {
     $row = parent::results_row_transform($row);
     // Get autoenrol default for the current userset.
     $usersetclassification = usersetclassification::get_for_cluster($row['element_id']);
     $autoenroldefault = !empty($usersetclassification->param_autoenrol_curricula) ? 1 : 0;
     $row['autoenroldefault'] = $autoenroldefault;
     return $row;
 }
 /**
  * Test contexts in usersetclassification data object.
  *
  * Covers:
  * local/elisprogram/plugins/usetclassify/usersetclassification.class.php:132
  */
 public function test_usersetclassification()
 {
     require_once elispm::file('plugins/usetclassify/usersetclassification.class.php');
     require_once elispm::file('plugins/usetclassify/lib.php');
     $cat = $this->create_field_category(CONTEXT_ELIS_USERSET);
     $field = $this->create_field(USERSET_CLASSIFICATION_FIELD, $cat, CONTEXT_ELIS_USERSET);
     $userset = $this->create_userset($field);
     $res = usersetclassification::get_for_cluster($userset);
 }
Esempio n. 5
0
 function get_default_object_for_add()
 {
     $parent = $this->optional_param('parent', 0, PARAM_INT);
     if ($parent) {
         $obj = new stdClass();
         $obj->parent = $parent;
         if ($parent) {
             require_once elis::plugin_file('elisprogram_usetclassify', 'usersetclassification.class.php');
             require_once elis::plugin_file('elisprogram_usetclassify', 'lib.php');
             if ($classification = usersetclassification::get_for_cluster($parent)) {
                 $fieldname = 'field_' . USERSET_CLASSIFICATION_FIELD;
                 if ($classification->param_child_classification) {
                     $obj->{$fieldname} = $classification->param_child_classification;
                 } else {
                     $obj->{$fieldname} = $classification->shortname;
                 }
                 //default groups and groupings settings
                 if ($classification->param_autoenrol_groups) {
                     $obj->field_userset_group = $classification->param_autoenrol_groups;
                 }
                 if ($classification->param_autoenrol_groupings) {
                     $obj->field_userset_groupings = $classification->param_autoenrol_groupings;
                 }
             }
         }
         return $obj;
     } else {
         return NULL;
     }
 }
Esempio n. 6
0
 /**
 * Find a list of userset folders that the user has access to.
 *
 * @param $CFG
 * @param array $opts      The current drop down list to which to add userset folders
 * @return array Alfresco repository folder names.
 */
    function find_userset_folders(&$opts, $createonly) {
        global $CFG, $DB, $USER;

        require_once($CFG->libdir . '/ddllib.php');

        // Ensure that the cluster table actually exists before we query it.
        $manager = $DB->get_manager();
        if (!$manager->table_exists('local_elisprogram_uset')) {
            return false;
        }

        if (!file_exists($CFG->dirroot.'/local/elisprogram/plugins/usetclassify/usersetclassification.class.php')) {
            return false;
        }

        require_once($CFG->dirroot.'/local/elisprogram/plugins/usetclassify/usersetclassification.class.php');
        require_once($CFG->dirroot.'/local/elisprogram/lib/data/userset.class.php');

        // Get cm userid
        $cmuserid = pm_get_crlmuserid($USER->id);

        $timenow = time();

        $capability = 'repository/elisfiles:viewusersetcontent';

        $child_path = $DB->sql_concat('c_parent.path', "'/%'");
        $like = $DB->sql_like('c.path',':child_path');

        // Select clusters and sub-clusters for the current user
        // to which they have the vieworganization capability
        $sql = "SELECT DISTINCT clst.id AS instanceid
            FROM {local_elisprogram_uset} clst
            WHERE EXISTS ( SELECT 'x'
                FROM {context} c
                JOIN {context} c_parent
                  ON {$like}
                  OR c.path = c_parent.path
                JOIN {role_assignments} ra
                  ON ra.contextid = c_parent.id
                  AND ra.userid = :muserid
                JOIN {role_capabilities} rc
                  ON ra.roleid = rc.roleid
                WHERE c_parent.instanceid = clst.id
                  AND c.contextlevel = :contextlevelnum
                  AND c_parent.contextlevel = :contextlevelnum2
                  AND rc.capability = :capability
                )
             OR EXISTS ( SELECT 'x'
                 FROM {local_elisprogram_uset_asign} ca
                WHERE ca.clusterid = clst.id
                  AND ca.userid = :cmuserid)
              ";

        $params = array(
            'capability'       => $capability,
            'child_path'       => $child_path,
            'contextlevelnum'  => CONTEXT_ELIS_USERSET,
            'contextlevelnum2' => CONTEXT_ELIS_USERSET,
            'muserid'          => $USER->id,
            'cmuserid'         => $cmuserid
        );
        $viewable_clusters = $DB->get_recordset_sql($sql, $params);

        // Get user clusters
        $cluster_info = array();
        if ($viewable_clusters) {
            foreach ($viewable_clusters as $cluster) {
                if (!$new_cluster_info = $this->load_cluster_info($cluster->instanceid)) {
                   continue;
                } else {
                    $cluster_info[$cluster->instanceid] = $new_cluster_info;
                }
            }
        } else {
            return false;
        }
        if (empty($cluster_info)) {
            return false;
        }

        // There may be multiple clusters that this user is assigned to...
        foreach ($cluster_info as $cluster) {
            // Get the extra cluster data and ensure it is present before proceeding.
            $clusterdata = usersetclassification::get_for_cluster($cluster);

            if (empty($clusterdata->params)) {
                continue;
            }

            $clusterparams = unserialize($clusterdata->params);

            // Make sure this cluster has the Alfresco shared folder property defined
            if (empty($clusterparams['elis_files_shared_folder'])) {
                continue;
            }

            // Make sure we can get the storage space from Alfresco for this userset.
            if (!$uuid = $this->get_userset_store($cluster->id)) {
                continue;
            }

            // Add to opts array
            $cluster_context = \local_elisprogram\context\userset::instance($cluster->id);
            $system_context = context_system::instance();

            $viewalfuserset = has_capability('repository/elisfiles:viewusersetcontent', $cluster_context) ||
                              has_capability('repository/elisfiles:viewsitecontent', $system_context);
            $editalfuserset = has_capability('repository/elisfiles:createusersetcontent', $cluster_context) ||
                              has_capability('repository/elisfiles:createsitecontent', $system_context);
            if ($editalfuserset) {
                if (!elis_files_has_permission($uuid, $USER->username, true)) {
                    $this->allow_edit($USER->username, $uuid);
                }
            } else if ($viewalfuserset) { // viewalfuserset
                if (!elis_files_has_permission($uuid, $USER->username)) {
                    $this->allow_read($USER->username, $uuid);
                }
            }
            if ((!$createonly && ($editalfuserset || $viewalfuserset)) || ($createonly && $editalfuserset)) {
                $params = array('path'=>$uuid,
                                'shared'=>(boolean)0,
                                'oid'=>(int)$cluster->id,
                                'cid'=>0,
                                'uid'=>0);
                $encodedpath = base64_encode(serialize($params));

                $unbiasedparams = array(
                    'path'   => $uuid,
                    'shared' => false,
                    'oid'    => 0,
                    'cid'    => 0,
                    'uid'    => 0
                );
                $unbiasedpath = base64_encode(serialize($unbiasedparams));
                $opts[] = array('name'=> $cluster->name,
                                'path'=> $encodedpath,
                                'unbiasedpath'=> $unbiasedpath
                                );
            }
        }

        return true;
    }
Esempio n. 7
0
/**
 * Handle the event when a user is unassigned from a user set.
 *
 * @uses $DB
 * @param object $usersetinfo The ELIS crlm_cluster_assignments record object.
 * @return bool True on success or failure (event handlers must always return true).
 */
function elis_files_userset_deassigned($usersetinfo) {
    global $DB;

    // Only proceed here if we have valid userid,clusterid & the Alfresco plug-in is actually enabled.
    if (empty($usersetinfo->userid) || empty($usersetinfo->clusterid) ||
        !($repo = repository_factory::factory('elisfiles'))) {
        return true;
    }

    $user = new user($usersetinfo->userid);

    // Get the Moodle user info from the CM user record.
    if (!$muser = $user->get_moodleuser()) {
        return true;
    }

    if (!($userset = $DB->get_record(userset::TABLE, array('id' => $usersetinfo->clusterid)))) {
        return true;
    }

    if (!file_exists(elispm::file('plugins/usetclassify/usersetclassification.class.php'))) {
        return true;
    }

    require_once(elispm::file('plugins/usetclassify/usersetclassification.class.php'));

    // Get the extra user set data and ensure it is present before proceeding.
    $usersetdata = usersetclassification::get_for_cluster($userset);

    if (empty($usersetdata->params)) {
        return true;
    }

    $usersetparams = unserialize($usersetdata->params);

    // Make sure this user set has the Alfresco shared folder property defined
    if (empty($usersetparams['elis_files_shared_folder'])) {
        return true;
    }

    // Does this organization have an Alfresco storage space?
    if (!$uuid = $repo->get_userset_store($userset->id, false)) {
        return true;
    }

    $context = \local_elisprogram\context\userset::instance($userset->id);

    $sql = 'SELECT rc.*
            FROM {role_assignments} ra
            INNER JOIN {role} r ON ra.roleid = r.id
            INNER JOIN {role_capabilities} rc ON r.id = rc.roleid
            WHERE ra.contextid = :contextid
            AND ra.userid = :userid
            AND rc.capability IN (:cap1, :cap2)
            AND rc.permission = '.CAP_ALLOW;

    $params = array(
        'contextid' => $context->id,
        'userid'    => $muser->id,
        'cap1'      => 'repository/elisfiles:createusersetcontent',
        'cap2'      => 'local/elisprogram:userset_enrol'
    );

    // Check if the user has a specific role assignment on the user set context with the editing capability
    if ($DB->record_exists_sql($sql, $params)) {
        // Remove all non-editing permissions for this user on the organization shared space.
        if ($permissions = elis_files_get_permissions($uuid, $muser->username)) {
            foreach ($permissions as $permission) {
                // Do not remove editing permissions if this user still actually has a user set membership.
                if ($permission == ELIS_FILES_ROLE_COLLABORATOR) {
                    continue;
                }

                elis_files_set_permission($muser->username, $uuid, $permission, ELIS_FILES_CAPABILITY_DENIED);
            }
        }

    // Remove all permissions for this user on the organization shared space.
    } else if ($permissions = elis_files_get_permissions($uuid, $muser->username)) {
        require_once(elispm::lib('data/clusterassignment.class.php'));
        foreach ($permissions as $permission) {
            // Do not remove view permissions if this user still actually has a user set membership.
            $params = array(
                'userid'    => $usersetinfo->userid,
                'clusterid' => $userset->id
            );

            if ($permission == ELIS_FILES_ROLE_CONSUMER && $DB->record_exists(clusterassignment::TABLE, $params)) {
                continue;
            }

            elis_files_set_permission($muser->username, $uuid, $permission, ELIS_FILES_CAPABILITY_DENIED);
        }
    }

    return true;
}
Esempio n. 8
0
    /**
     * Validate that the "find_userset_folders" method respect site files
     * capabilities assigned at the system level
     * @uses $USER, $DB
     * @param string $capability The site files capabilility to assign to the test user
     * @param boolean $createonly The "create only" flag, as needed by the method
     * @param array $names The expected set of userset names that should be returned
     * @dataProvider userset_folders_provider
     */
    public function test_find_userset_folders_respects_site_files($capability, $createonly, $names) {
        if (!class_exists('elispm')) {
            $this->markTestSkipped('local_elisprogram needed for test');
            return false;
        }

        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        global $DB, $USER;
        require_once(elis::lib('data/customfield.class.php'));
        require_once(elispm::lib('data/userset.class.php'));
        require_once(elispm::file('plugins/usetclassify/usersetclassification.class.php'));
        require_once(elispm::file('accesslib.php'));

        // Make sure the test user is not mistaken for a site admin or guest
        set_config('siteadmins', '');
        set_config('siteguest', '');

        $classification = new usersetclassification(array(
            'shortname' => 'testclassification'
        ));
        $classification->param_elis_files_shared_folder = 1;
        $classification->save();

        $userset = new userset(array(
            'name' => 'testusersetname'
        ));
        $userset->save();
        $userset->reset_custom_field_list();
        $userset->load();
        $userset->field__elis_userset_classification = 'testclassification';
        $userset->save();

        $this->setUser(100);

        // Assign the "site files" role to the test user
        $roleid = $this->assign_role_capability($capability);
        $systemcontext = context_system::instance();
        role_assign($roleid, $USER->id, $systemcontext->id);

        // Assign the "view userset content" role to the test user
        $usersetcontext = \local_elisprogram\context\userset::instance($userset->id);
        $roleid = $this->assign_role_capability('repository/elisfiles:viewusersetcontent', 100);
        role_assign($roleid, $USER->id, $usersetcontext->id);

        // Obtain the set of userset folders
        $elisfiles = repository_factory::factory();

        $folders = array();
        $elisfiles->find_userset_folders($folders, $createonly);

        // Validate that the method returned the right number of folders
        $this->assertEquals(count($names), count($folders));

        // Validate the specific names
        foreach ($names as $i => $name) {
            $this->assertEquals($name, $folders[$i]['name']);
        }
    }
 static function get_for_cluster($cluster)
 {
     require_once elis::lib('data/customfield.class.php');
     require_once elispm::file('plugins/usetclassify/lib.php');
     if (is_object($cluster)) {
         $cluster = $cluster->id;
     }
     $context = \local_elisprogram\context\userset::instance($cluster);
     $value = field_data::get_for_context_and_field($context, USERSET_CLASSIFICATION_FIELD);
     if (isset($value) && $value->valid()) {
         $value = $value->current();
         $name = $value->data;
         $newusersetclassification = usersetclassification::find(new field_filter('shortname', $name));
         return $newusersetclassification->current();
     } else {
         return false;
     }
 }