Ejemplo n.º 1
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);
}
Ejemplo n.º 2
0
 /**
  * Returns an SQL WHERE and/or JOIN clause for an array of filters.  The
  * filters are ANDed together
  *
  * @param array $filters array of filters
  * @param bool $use_join whether or not a JOIN query should be generated
  * (if relevant)
  * @param string $tablename the name or alias of the base table
  * @param moodle_database $db the database that the query will be executed
  * on
  *
  * @return array same as the get_sql method
  */
 public static function get_combined_sql(array $filters = array(), $use_join = false, $tablename = null, $paramtype = SQL_PARAMS_QM, moodle_database $db = null)
 {
     $filter = new AND_filter($filters);
     return $filter->get_sql($use_join, $tablename, $paramtype, $db);
 }
Ejemplo n.º 3
0
 /**
  * Delete the records corresponding to some criteria.
  *
  * @param mixed $filter a filter or an array of filter objects.  (Note:
  * unlike in the find and count methods, this parameter is not optional)
  * @param moodle_database $db database object to use
  */
 public static function delete_records($filter, moodle_database $db = null)
 {
     require_once elis::lib('data/data_filter.class.php');
     global $DB;
     if (!empty(static::$delete_is_complex)) {
         // deleting involves more than just removing the DB records
         $items = static::find($filter, array(), 0, 0, $db);
         foreach ($items as $item) {
             $item->delete();
         }
         return;
     }
     $tablename = static::TABLE;
     if ($db === null) {
         $db = $DB;
     }
     if (is_object($filter)) {
         $sql_clauses = $filter->get_sql(false, "{{$tablename}}", SQL_PARAMS_QM, $db);
     } else {
         $sql_clauses = AND_filter::get_combined_sql($filter, false, null, SQL_PARAMS_QM, $db);
     }
     if (!isset($sql_clauses['where'])) {
         $sql_clauses['where'] = '';
         $sql_clauses['where_parameters'] = null;
     }
     return $db->delete_records_select($tablename, $sql_clauses['where'], $sql_clauses['where_parameters']);
 }