/**
  * Get a list of the clusters assigned to this track.
  *
  * @uses            $CURMAN
  * @param  int      $trackid            The track id
  * @param  int      $parent_cluster_id  Cluster that must be the parent of track's clusters
  * @param  string   $sort               The sort field
  * @param  string   $dir                either 'ASC' or 'DESC'
  * @param  int      $startrec           The index of the record to start with
  * @param  int      $perpage            How many records to include
  * @param  array    $extrafilters       Additional filters to apply to the count/listing
  * @param  array                        The appropriate cluster records
  */
 static function get_clusters($trackid = 0, $parent_cluster_id = 0, $sort = 'name', $dir = 'ASC', $startrec = 0, $perpage = 0, $extrafilters = array())
 {
     global $CURMAN;
     if (empty($CURMAN->db)) {
         return NULL;
     }
     //require plugin code if enabled
     $display_priority_enabled = in_array('cluster_display_priority', get_list_of_plugins('curriculum/plugins'));
     if ($display_priority_enabled) {
         require_once CURMAN_DIRLOCATION . '/plugins/cluster_display_priority/lib.php';
     }
     $select = 'SELECT clsttrk.id, clsttrk.clusterid, clst.name, clst.display, clsttrk.autoenrol ';
     $tables = 'FROM ' . $CURMAN->db->prefix_table(CLSTTRKTABLE) . ' clsttrk ';
     $join = 'LEFT JOIN ' . $CURMAN->db->prefix_table(CLSTTABLE) . ' clst ' . 'ON clst.id = clsttrk.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)) {
         cluster_display_priority_append_sort_data('clst.id', $select, $join);
     }
     $where = " WHERE clsttrk.trackid = {$trackid} ";
     if (!empty($parent_cluster_id)) {
         $where .= " AND clst.parent = {$parent_cluster_id} ";
     }
     if (!empty($extrafilters['contexts'])) {
         $where .= ' AND ' . $extrafilters['contexts']->sql_filter_for_context_level('clst.id', 'cluster');
         // TBV
     }
     $group = ' GROUP BY clsttrk.id ';
     $sort_clause = 'ORDER BY ' . implode($sort_clauses, ', ') . ' ';
     $limit = '';
     if (!empty($perpage)) {
         if ($CURMAN->db->_dbconnection->databaseType == 'postgres7') {
             $limit = 'LIMIT ' . $perpage . ' OFFSET ' . $startrec;
         } else {
             $limit = 'LIMIT ' . $startrec . ', ' . $perpage;
         }
     } else {
         $limit = '';
     }
     $sql = $select . $tables . $join . $where . $group . $sort_clause . $limit;
     return $CURMAN->db->get_records_sql($sql);
 }
Example #2
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, $CURMAN;
    //require plugin code if enabled
    $display_priority_enabled = in_array('cluster_display_priority', get_list_of_plugins('curriculum/plugins'));
    if ($display_priority_enabled) {
        require_once CURMAN_DIRLOCATION . '/plugins/cluster_display_priority/lib.php';
    }
    $LIKE = $CURMAN->db->sql_compare();
    $select = 'SELECT clst.* ';
    $tables = "FROM {$CURMAN->db->prefix_table(CLSTTABLE)} clst ";
    $join = '';
    $where_conditions = array();
    if (!empty($namesearch)) {
        $namesearch = trim($namesearch);
        $where_conditions[] = "(name {$LIKE} '%{$namesearch}%') ";
    }
    if ($alpha) {
        $where_conditions[] = "(name {$LIKE} '{$alpha}%') ";
    }
    if (!empty($extrafilters['contexts'])) {
        /*
         * Start of cluster hierarchy extension
         */
        $sql_condition = '0=1';
        if (cluster::all_clusters_viewable()) {
            //user has capability at system level so allow access to any cluster
            $sql_condition = '0=0';
        } else {
            //user does not have capability at system level, so filter
            $viewable_clusters = cluster::get_viewable_clusters();
            if (empty($viewable_clusters)) {
                //user has no access to any clusters, so do not allow additional access
                $sql_condition = '0=1';
            } else {
                //user has additional access to some set of clusters, so "enable" this access
                $cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
                //use the context path to find parent clusters
                $like = sql_ilike();
                $parent_path = sql_concat('parent_context.path', "'/%'");
                $cluster_filter = implode(',', $viewable_clusters);
                $sql_condition = "clst.id IN (\n                                      SELECT parent_context.instanceid\n                                      FROM {$CURMAN->db->prefix_table('context')} parent_context\n                                      JOIN {$CURMAN->db->prefix_table('context')} child_context\n                                        ON child_context.path {$like} {$parent_path}\n                                        AND parent_context.contextlevel = {$cluster_context_level}\n                                        AND child_context.contextlevel = {$cluster_context_level}\n                                        AND child_context.instanceid IN ({$cluster_filter})\n                                  )";
            }
        }
        /*
         * End of cluster hierarchy extension
         */
        $context_filter = $extrafilters['contexts']->sql_filter_for_context_level('clst.id', 'cluster');
        //extend the basic context filter by potentially enabling access to parent clusters
        $where_conditions[] = "({$context_filter} OR {$sql_condition})";
    }
    if (isset($extrafilters['parent'])) {
        $where_conditions[] = "parent={$extrafilters['parent']}";
    }
    if (isset($extrafilters['classification'])) {
        require_once CURMAN_DIRLOCATION . '/plugins/cluster_classification/lib.php';
        $contextlevel = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
        $field = new field(field::get_for_context_level_with_name($contextlevel, CLUSTER_CLASSIFICATION_FIELD));
        $where_conditions[] = "clst.id IN (SELECT ctx.instanceid\n                                        FROM {$CURMAN->db->prefix_table('context')} ctx\n                                        JOIN (SELECT ctx.id AS contextid, IFNULL(fdata.data, fdefault.data) AS data\n                                                FROM {$CURMAN->db->prefix_table('context')} ctx\n                                            LEFT JOIN {$CURMAN->db->prefix_table($field->data_table())} fdata ON fdata.contextid = ctx.id AND fdata.fieldid = {$field->id}\n                                            LEFT JOIN {$CURMAN->db->prefix_table($field->data_table())} fdefault ON fdefault.contextid IS NULL AND fdefault.fieldid = {$field->id}) fdata ON fdata.data = '{$extrafilters['classification']}' AND fdata.contextid = ctx.id\n                                        WHERE ctx.contextlevel = {$contextlevel})";
    }
    if (!empty($userid)) {
        //get the context for the "indirect" capability
        $context = cm_context_set::for_user_with_capability('cluster', 'block/curr_admin:cluster:enrol_cluster_user', $USER->id);
        $clusters = cluster_get_user_clusters($userid);
        $allowed_clusters = $context->get_allowed_instances($clusters, 'cluster', 'clusterid');
        $curriculum_context = cm_context_set::for_user_with_capability('cluster', 'block/curr_admin:cluster:enrol', $USER->id);
        $curriculum_filter = $curriculum_context->sql_filter_for_context_level('clst.id', 'cluster');
        if (empty($allowed_clusters)) {
            $where_conditions[] = $curriculum_filter;
        } else {
            $allowed_clusters_list = implode(',', $allowed_clusters);
            $cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
            $path = sql_concat('parentctxt.path', "'/%'");
            $like = sql_ilike();
            //this allows both the indirect capability and the direct curriculum filter to work
            $where_conditions[] = "(\n                                     (\n                                     clst.id IN (\n                                       SELECT childctxt.instanceid\n                                       FROM\n                                         {$CURMAN->db->prefix_table(CLSTTABLE)} clst\n                                         JOIN {$CURMAN->db->prefix_table('context')} parentctxt\n                                           ON clst.id = parentctxt.instanceid\n                                           AND parentctxt.contextlevel = {$cluster_context_level}\n                                         JOIN {$CURMAN->db->prefix_table('context')} childctxt\n                                           ON childctxt.path {$like} {$path}\n                                           AND childctxt.contextlevel = {$cluster_context_level}\n                                       )\n                                     )\n                                     OR\n                                     (\n                                     {$curriculum_filter}\n                                     )\n                                   )";
        }
    }
    //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') {
            $priority_key = $key;
            $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)) {
        cluster_display_priority_append_sort_data('clst.id', $select, $join);
    }
    $where = '';
    if (!empty($where_conditions)) {
        $where = 'WHERE ' . implode(' AND ', $where_conditions) . ' ';
    }
    if (isset($priority_key) && !context_level_base::get_custom_context_level('cluster', 'block_curr_admin')) {
        unset($sort_clauses[$priority_key]);
    }
    $sort_clause = 'ORDER BY ' . implode($sort_clauses, ', ') . ' ';
    //paging
    if (!empty($perpage)) {
        if ($CURMAN->db->_dbconnection->databaseType == 'postgres7') {
            $limit = 'LIMIT ' . $perpage . ' OFFSET ' . $startrec;
        } else {
            $limit = 'LIMIT ' . $startrec . ', ' . $perpage;
        }
    } else {
        $limit = '';
    }
    $sql = $select . $tables . $join . $where . $sort_clause . $limit;
    return $CURMAN->db->get_records_sql($sql);
}