/**
  * Dummy can_do method for viewing a curriculum report (needed for the
  * cluster tree parameter for reports)
  */
 function can_do_viewreport()
 {
     global $CFG, $CURMAN;
     $id = $this->required_param('id', PARAM_INT);
     //needed for execution mode constants
     require_once $CFG->dirroot . '/blocks/php_report/php_report_base.php';
     //check if we're scheduling or viewing
     $execution_mode = $this->optional_param('execution_mode', php_report::EXECUTION_MODE_SCHEDULED, PARAM_INT);
     //check the correct capability
     $capability = $execution_mode == php_report::EXECUTION_MODE_SCHEDULED ? 'block/php_report:schedule' : 'block/php_report:view';
     if ($this->_has_capability($capability)) {
         return true;
     }
     /*
      * Start of cluster hierarchy extension
      */
     $viewable_clusters = cluster::get_viewable_clusters($capability);
     $cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
     $like = sql_ilike();
     $parent_path = sql_concat('parent_context.path', "'/%'");
     //if the user has no additional access through parent clusters, then they can't view this cluster
     if (empty($viewable_clusters)) {
         return false;
     }
     $cluster_filter = implode(',', $viewable_clusters);
     //determine if this cluster is the parent of some accessible child cluster
     $sql = "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                  AND parent_context.instanceid = {$id}";
     return record_exists_sql($sql);
     /*
      * End of cluster hierarchy extension
      */
 }
Example #2
0
function cluster_count_records($namesearch = '', $alpha = '', $extrafilters = array())
{
    global $CURMAN;
    $select = array();
    $LIKE = $CURMAN->db->sql_compare();
    if (!empty($namesearch)) {
        $namesearch = trim($namesearch);
        $select[] = "(name {$LIKE} '%{$namesearch}%')";
    }
    if ($alpha) {
        $select[] = "(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 = "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('id', 'cluster');
        //extend the basic context filter by potentially enabling access to parent clusters
        $select[] = "({$context_filter} OR {$sql_condition})";
    }
    if (isset($extrafilters['parent'])) {
        $select[] = "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));
        $select[] = "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})";
    }
    $select = implode(' AND ', $select);
    return $CURMAN->db->count_records_select(CLSTTABLE, $select);
}