/**
  * Get a list of the clusters assigned to this curriculum.
  *
  * @uses           $CURMAN
  * @param   int    $curriculumid     The cluster id
  * @param   int    $parentclusterid  If non-zero, a required direct-parent cluster
  * @param   int    $startrecord      The index of the record to start with
  * @param   int    $perpage          The number of records to include
  * @param   array  $extrafilters     Additional filters to apply to the listing
  * @return  array                    The appropriate cluster records
  */
 public static function get_clusters($curriculumid = 0, $parentclusterid = 0, $sort = 'name', $dir = 'ASC', $startrec = 0, $perpage = 0, $extrafilters = array())
 {
     global $DB;
     if (empty($DB)) {
         return NULL;
     }
     //require plugin code if enabled
     $plugins = get_plugin_list('elisprogram');
     $display_priority_enabled = isset($plugins['usetdisppriority']);
     if ($display_priority_enabled) {
         require_once elis::plugin_file('elisprogram_usetdisppriority', 'lib.php');
     }
     $select = 'SELECT clstcur.id, clstcur.clusterid, clst.name, clst.display, clstcur.autoenrol ';
     $tables = 'FROM {' . self::TABLE . '} clstcur ';
     $join = 'LEFT JOIN {' . userset::TABLE . '} clst ' . 'ON clst.id = clstcur.clusterid ';
     //handle empty sort case
     if (empty($sort)) {
         $sort = 'name';
         $dir = 'ASC';
     }
     //get the fields we are sorting
     $sort_fields = explode(',', $sort);
     //convert the fields into clauses
     $sort_clauses = array();
     foreach ($sort_fields as $key => $value) {
         $new_value = trim($value);
         if ($display_priority_enabled && $new_value == 'priority') {
             $sort_clauses[$key] = $new_value . ' DESC';
         } else {
             $sort_clauses[$key] = $new_value . ' ' . $dir;
         }
     }
     //determine if we are handling the priority field for ordering
     if ($display_priority_enabled && in_array('priority', $sort_fields)) {
         userset_display_priority_append_sort_data('clst.id', $select, $join);
     }
     $where = 'WHERE clstcur.curriculumid = :curriculumid ';
     $params = array('curriculumid' => $curriculumid);
     //apply the parent-cluster condition if applicable
     if (!empty($parentclusterid)) {
         $where .= " AND clst.parent = :parentclusterid ";
         $params['parentclusterid'] = $parentclusterid;
     }
     if (!empty($extrafilters['contexts'])) {
         //apply a filter related to filtering on particular PM cluster contexts
         $filter_object = $extrafilters['contexts']->get_filter('id', 'cluster');
         $filter_sql = $filter_object->get_sql(false, 'clst', SQL_PARAMS_NAMED);
         if (!empty($filter_sql)) {
             //user does not have access at the system context
             $where .= 'AND (' . $filter_sql['where'] . ") ";
             $params = array_merge($params, $filter_sql['where_parameters']);
         }
     }
     $group = 'GROUP BY clstcur.id ';
     $sort_clause = 'ORDER BY ' . implode($sort_clauses, ', ') . ' ';
     $sql = $select . $tables . $join . $where . $group . $sort_clause;
     return $DB->get_records_sql($sql, $params, $startrec, $perpage);
 }
 /**
  * Test contexts in userset_display_priority_append_sort_data
  *
  * Covers:
  * local/elisprogram/plugins/usetdisppriority/lib.php:43
  */
 public function test_usersetdisplaypriority()
 {
     require_once elispm::file('plugins/usetdisppriority/lib.php');
     $select = '';
     $join = '';
     userset_display_priority_append_sort_data('id', $select, $join);
 }
Example #3
0
/**
 * Gets a cluster listing with specific sort and other filters.
 *
 * @param string $sort Field to sort on.
 * @param string $dir Direction of sort.
 * @param int $startrec Record number to start at.
 * @param int $perpage Number of records per page.
 * @param string $namesearch Search string for cluster name.
 * @param string $descsearch Search string for cluster description.
 * @param string $alpha Start initial of cluster name filter.
 * @param int $userid User who you are assigning clusters to
 * @return object array Returned records.
 */
function cluster_get_listing($sort = 'name', $dir = 'ASC', $startrec = 0, $perpage = 0, $namesearch = '', $alpha = '', $extrafilters = array(), $userid = 0)
{
    global $USER, $DB;
    //require plugin code if enabled
    $plugins = get_plugin_list('elisprogram');
    $display_priority_enabled = isset($plugins['usetdisppriority']);
    if ($display_priority_enabled) {
        require_once elis::plugin_file('elisprogram_usetdisppriority', 'lib.php');
        $priority_field = field::get_for_context_level_with_name(CONTEXT_ELIS_USERSET, USERSET_DISPLAY_PRIORITY_FIELD);
        if (empty($priority_field->id)) {
            $display_priority_enabled = false;
        }
    }
    $select = 'SELECT clst.* ';
    $tables = 'FROM {' . userset::TABLE . '} clst';
    $join = '';
    $filters = array();
    if (!empty($namesearch)) {
        $namesearch = trim($namesearch);
        $filters[] = new field_filter('name', "%{$namesearch}%", field_filter::LIKE);
    }
    if ($alpha) {
        $filters[] = new field_filter('name', "{$alpha}%", field_filter::LIKE);
    }
    if (!empty($extrafilters['contexts'])) {
        /*
         * Start of cluster hierarchy extension
         */
        $sql_condition = new select_filter('FALSE');
        if (userset::all_clusters_viewable()) {
            //user has capability at system level so allow access to any cluster
            $sql_condition = new select_filter('TRUE');
        } else {
            //user does not have capability at system level, so filter
            $viewable_clusters = userset::get_viewable_clusters();
            if (empty($viewable_clusters)) {
                //user has no access to any clusters, so do not allow additional access
                $sql_condition = new select_filter('FALSE');
            } else {
                //user has additional access to some set of clusters, so "enable" this access
                //use the context path to find parent clusters
                $path = $DB->sql_concat('parent_context.path', "'/%'");
                list($IN, $inparams) = $DB->get_in_or_equal($viewable_clusters);
                $sql_condition = new select_filter("clst.id IN (SELECT parent_context.instanceid\n                              FROM {context} parent_context\n                              JOIN {context} child_context\n                                ON child_context.path LIKE {$path}\n                               AND parent_context.contextlevel = " . CONTEXT_ELIS_USERSET . "\n                               AND child_context.contextlevel = " . CONTEXT_ELIS_USERSET . "\n                               AND child_context.instanceid {$IN}\n                           )", $inparams);
            }
        }
        /*
         * End of cluster hierarchy extension
         */
        $context_filter = $extrafilters['contexts']->get_filter('id', 'cluster');
        //extend the basic context filter by potentially enabling access to parent clusters
        $filters[] = new OR_filter(array($context_filter, $sql_condition));
    }
    if (isset($extrafilters['parent'])) {
        $filters[] = new field_filter('parent', $extrafilters['parent']);
    }
    if (isset($extrafilters['classification'])) {
        require_once elispm::file('plugins/usetclassify/lib.php');
        $field = new field(field::get_for_context_level_with_name(CONTEXT_ELIS_USERSET, USERSET_CLASSIFICATION_FIELD));
        $filters[] = new elis_field_filter($field, 'id', CONTEXT_ELIS_USERSET, $extrafilters['classification']);
    }
    if (!empty($userid)) {
        //get the context for the "indirect" capability
        $context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:userset_enrol_userset_user', $USER->id);
        $clusters = cluster_get_user_clusters($userid);
        $allowed_clusters = $context->get_allowed_instances($clusters, 'cluster', 'clusterid');
        $curriculum_context = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:userset_enrol', $USER->id);
        $curriculum_filter = $curriculum_context->get_filter('id');
        if (empty($allowed_clusters)) {
            $filters[] = $curriculum_filter;
        } else {
            $allowed_clusters_list = implode(',', $allowed_clusters);
            $path = $DB->sql_concat('parentctxt.path', "'/%'");
            //this allows both the indirect capability and the direct curriculum filter to work
            $subcluster_filter = new select_filter("clst.id IN (SELECT childctxt.instanceid\n                               FROM {" . userset::TABLE . "} clst\n                               JOIN {context} parentctxt\n                                 ON clst.id = parentctxt.instanceid\n                                AND parentctxt.contextlevel = " . CONTEXT_ELIS_USERSET . "\n                               JOIN {context} childctxt\n                                 ON childctxt.path LIKE {$path}\n                                AND childctxt.contextlevel = " . CONTEXT_ELIS_USERSET . "\n                              WHERE parentctxt.instanceid IN ({$allowed_clusters_list}))");
            $filters[] = new OR_filter(array($subcluster_filter, $curriculum_filter));
        }
    }
    //handle empty sort case
    if (empty($sort)) {
        $sort = 'name';
        $dir = 'ASC';
    }
    //get the fields we are sorting
    $sort_fields = explode(',', $sort);
    //convert the fields into clauses
    $sort_clauses = array();
    foreach ($sort_fields as $field) {
        $field = trim($field);
        if ($field == 'priority') {
            if ($display_priority_enabled) {
                $sort_clauses[] = $field . ' DESC';
            }
        } else {
            $sort_clauses[] = $field . ' ' . $dir;
        }
    }
    if (empty($sort_clauses)) {
        $sort_clauses = array('name ASC');
    }
    //determine if we are handling the priority field for ordering
    if ($display_priority_enabled && in_array('priority', $sort_fields)) {
        userset_display_priority_append_sort_data('clst.id', $select, $join);
    }
    $filter = new AND_filter($filters);
    $filtersql = $filter->get_sql(true, 'clst');
    $params = array();
    $where = '';
    if (isset($filtersql['join'])) {
        $join .= ' JOIN ' . $filtersql['join'];
        $params = array_merge($params, $filtersql['join_parameters']);
    }
    if (isset($filtersql['where'])) {
        $where = ' WHERE ' . $filtersql['where'];
        $params = array_merge($params, $filtersql['where_parameters']);
    }
    $sort_clause = ' ORDER BY ' . implode($sort_clauses, ', ') . ' ';
    $sql = $select . $tables . $join . $where . $sort_clause;
    $recordset = $DB->get_recordset_sql($sql, $params, $startrec, $perpage);
    return new data_collection($recordset, 'userset', null, array(), true);
}