function map_get_cells($brainstormid, $userid = null, $groupid = 0, $configdata = null)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid);
    $sql = "\r\n        SELECT\r\n            id,\r\n            itemsource,\r\n            itemdest,\r\n            intvalue,\r\n            floatvalue,\r\n            blobvalue\r\n         FROM\r\n            {$CFG->prefix}brainstorm_operatordata AS od\r\n         WHERE\r\n            brainstormid = {$brainstormid} AND\r\n            operatorid = 'map'\r\n            {$accessClause}\r\n    ";
    $map = array();
    if ($maprecords = get_records_sql($sql)) {
        foreach ($maprecords as $record) {
            if (!$configdata || !@$configdata->quantified) {
                $map[$record->itemsource][$record->itemdest] = 1;
            } else {
                switch ($configdata->quantifiertype) {
                    case 'integer':
                        $map[$record->itemsource][$record->itemdest] = $record->intvalue;
                        break;
                    case 'float':
                        $map[$record->itemsource][$record->itemdest] = $record->floatvalue;
                        break;
                    case 'multiple':
                        $map[$record->itemsource][$record->itemdest] = unserialize($record->blobvalue);
                        break;
                    default:
                }
            }
        }
    }
    return $map;
}
/**
* checks if there are ordering data for the given user context
* @param int $brainstormid
* @param int $userid
* @param int $groupid
* @param boolean $excludemyself
*/
function has_ordering_data($brainstormid, $userid = null, $groupid = 0, $excludemyself = false)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, $excludemyself);
    $sql = "\r\n        SELECT\r\n            COUNT(*)\r\n        FROM\r\n            {$CFG->prefix}brainstorm_responses as r,\r\n            {$CFG->prefix}brainstorm_operatordata as od\r\n        WHERE\r\n            r.brainstormid = {$brainstormid} AND\r\n            r.id = od.itemsource AND\r\n            (od.operatorid = 'order'\r\n            {$accessClause})\r\n    ";
    return count_records_sql($sql);
}
/**
*
* @uses CFG, USER
* @param int $brainstormid
* @param int $slotid
* @param int $userid
* @param int $groupid
* @param boolean $excludemyself
*/
function merge_get_customentries($brainstormid, $slotid, $userid = null, $groupid = 0, $excludemyself = false)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, $excludemyself);
    $select = "\r\n        brainstormid = {$brainstormid} AND\r\n        operatorid = 'merge' AND\r\n        intvalue = {$slotid} AND\r\n        itemsource = 0\r\n        {$accessClause}\r\n    ";
    $records = get_records_select('brainstorm_operatordata AS od', $select);
    return $records;
}
/**
*
* @uses CFG, USER
* @param int $brainstormid
* @param int $userid
* @param int $groupid 
* @param boolean $excludemyself
*/
function filter_get_status($brainstormid, $userid = null, $groupid = 0, $excludemyself = false)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, $excludemyself);
    $sql = "\r\n        SELECT\r\n            itemsource,\r\n            intvalue,\r\n            od.userid,\r\n            od.groupid,\r\n            response\r\n         FROM\r\n            {$CFG->prefix}brainstorm_operatordata AS od,\r\n            {$CFG->prefix}brainstorm_responses AS r\r\n         WHERE\r\n            od.brainstormid = {$brainstormid} AND\r\n            od.itemsource = r.id AND\r\n            operatorid = 'filter'\r\n            {$accessClause}\r\n    ";
    if (!($statusrecords = get_records_sql($sql))) {
        $statusrecords = array();
    }
    return $statusrecords;
}
/**
* returns an array of matching indicators by response.
* @uses CFG, USER
* @param int $braintormid
* @param int $userid
*/
function categorize_get_matchings($brainstormid, $userid = null, $groupid = 0)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, true);
    /// get interesting responses
    $select = "\r\n       brainstormid = '{$brainstormid}' AND\r\n       operatorid = 'categorize'\r\n       {$accessClause}\r\n    ";
    $allcategorizations = get_records_select('brainstorm_operatordata AS od', $select);
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, false);
    /// get interesting responses
    $select = "\r\n       brainstormid = '{$brainstormid}' AND\r\n       operatorid = 'categorize'\r\n       {$accessClause}\r\n    ";
    $mycategorizations = get_records_select('brainstorm_operatordata AS od', $select);
    /// compile our values first
    $reference = array();
    if ($mycategorizations) {
        foreach ($mycategorizations as $cat) {
            $reference[$cat->itemsource][] = $cat->itemdest;
        }
    }
    if ($allcategorizations && !empty($reference)) {
        /// compile values for other users
        foreach ($allcategorizations as $cat) {
            if (!in_array($cat->itemsource, array_keys($reference))) {
                continue;
            }
            // discard those responses we did not give any assignation
            if (@in_array($cat->itemdest, $reference[$cat->itemsource])) {
                @$match[$cat->itemsource]++;
            } else {
                @$unmatch[$cat->itemsource]++;
            }
        }
        $matchings->match =& $match;
        $matchings->unmatch =& $unmatch;
    } else {
        $matchings->match = array();
        $matchings->unmatch = array();
    }
    return $matchings;
}
/**
*
*
*/
function scale_get_scalebounds($brainstormid, $userid = null, $groupid = 0, $excludemyself = false, $configdata)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, $excludemyself);
    switch (@$configdata->quantifierype) {
        case 'moodlescale':
            $field = 'blobvalue';
            break;
        case 'integer':
            $field = 'intvalue';
            break;
        default:
            $field = 'floatvalue';
            break;
    }
    if (isset($field)) {
        $sql = "\r\n            SELECT\r\n                MAX({$field}) as maxvalue,\r\n                MIN({$field}) as minvalue\r\n            FROM\r\n                {$CFG->prefix}brainstorm_operatordata as od\r\n            WHERE\r\n                od.brainstormid = {$brainstormid} AND\r\n                operatorid = 'scale'\r\n                {$accessClause}\r\n            GROUP BY\r\n                brainstormid\r\n        ";
        $bounds = get_record_sql($sql);
        if ($bounds) {
            if ($bounds->minvalue > 0) {
                $bounds->minvalue = 0;
            }
            $bounds->range = $bounds->maxvalue - $bounds->minvalue;
            return $bounds;
        }
    } else {
        if (isset($configdata->scale)) {
            if ($scale = get_record('scale', 'id', $configdata->scale)) {
                $bounds->minvalue = 0;
                $bounds->maxvalue = count(explode(',', $scale->scale)) - 1;
                return $bounds;
            }
        }
    }
    $bounds->minvalue = 0;
    $bounds->maxvalue = 0;
    return $bounds;
}
/**
* calculates bounds of record set given for any responses
* @param int $brainstormid
* @param int $userid
* @param int $groupid
*/
function locate_get_bounds($brainstormid, $userid = null, $groupid = 0, $excludemyself = false)
{
    global $CFG;
    $operator = new BrainstormOperator($brainstormid, 'locate');
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, $excludemyself);
    $select = "\r\n        brainstormid = {$brainstormid} AND\r\n        operatorid = 'locate'\r\n        {$accessClause}\r\n    ";
    if (!($locations = get_records_select('brainstorm_operatordata AS od', $select, '', 'itemsource,blobvalue'))) {
        $locations = array();
    }
    $maxs = array();
    $mins = array();
    /// calculate bounds
    foreach ($locations as $responseid => $locationblob) {
        $locationdata = unserialize($locationblob->blobvalue);
        // $locationdatas[$responseid][] = $locationdata;
        if (!isset($maxs[$responseid]['x'])) {
            $maxs[$responseid]['x'] = $operator->configdata->xminrange;
        }
        if (!isset($mins[$responseid]['x'])) {
            $mins[$responseid]['x'] = $operator->configdata->xmaxrange;
        }
        if (!isset($maxs[$responseid]['y'])) {
            $maxs[$responseid]['y'] = $operator->configdata->yminrange;
        }
        if (!isset($mins[$responseid]['y'])) {
            $mins[$responseid]['y'] = $operator->configdata->ymaxrange;
        }
        $maxs[$responseid]['x'] = $maxs[$responseid]['x'] - $locationdata->x < 0 ? $locationdata->x : $maxs[$responseid]['x'];
        $mins[$responseid]['x'] = $mins[$responseid]['x'] - $locationdata->x > 0 ? $locationdata->x : $mins[$responseid]['x'];
        $maxs[$responseid]['y'] = $maxs[$responseid]['y'] - $locationdata->y < 0 ? $locationdata->y : $maxs[$responseid]['y'];
        $mins[$responseid]['y'] = $mins[$responseid]['y'] - $locationdata->y < 0 ? $locationdata->y : $mins[$responseid]['y'];
    }
    $result->max =& $maxs;
    $result->min =& $mins;
    return $result;
}
/**
* get the max ordering available in sequence at a specified node
* @param int $brainstormid the current brainstorm context
* @param int $groupid the current group
* @param boolean $istree true id the entity is table-tree rather than table-list
* @param fatherid the parent node
* @return integer the max ordering found
*/
function brainstorm_tree_get_max_ordering($brainstormid, $userid = null, $groupid = 0, $istree = false, $fatherid = 0)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, false);
    $operator = $istree ? 'hierarchize' : 'order';
    $treeClause = $istree ? "AND itemdest = {$fatherid}" : '';
    $sql = "\r\n\t    SELECT \r\n\t        MAX(intvalue) as position\r\n\t    FROM \r\n\t        {$CFG->prefix}brainstorm_operatordata AS od\r\n\t    WHERE \r\n\t        brainstormid = {$brainstormid} AND\r\n\t        operatorid = '{$operator}'\r\n\t        {$accessClause}\r\n\t        {$treeClause}\r\n\t";
    if (!($result = get_record_sql($sql))) {
        $result->position = 1;
    }
    return $result->position;
}
/**
*
* @param int $brainstormid
* @param int $userid
* @param int $groupid
* @param boolean $excludemyself
* @param string $sort
* @returns array of responses
*/
function brainstorm_count_operatorinputs($brainstormid, $userid = null, $groupid = 0, $excludemyself = false)
{
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, $excludemyself);
    $select = "\r\n        brainstormid = {$brainstormid}\r\n        {$accessClause}\r\n    ";
    return count_records_select('brainstorm_operatordata AS od', $select);
}
/**
*
* @param int $brainstormid
* @param object $cm
* @param int $userid
* @param int $groupid
* @param boolean $excludemyself
* @param string $previouslevel
*/
function hierarchize_print_levelindeepness($brainstormid, $cm, $userid = null, $groupid = 0, $excludemyself = false, $fatherid = 0)
{
    global $CFG;
    $subs = hierarchize_get_childs($brainstormid, $userid, $groupid, $excludemyself, $fatherid);
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, false);
    if (!empty($subs)) {
        // get column spanning counts
        $idlist = join("','", array_keys($subs));
        echo '<tr valign="top">';
        foreach ($subs as $sub) {
            echo '<td class="subtree">';
            echo $sub->response;
            echo '<br/><table width="100%">';
            hierarchize_print_levelindeepness($brainstormid, $cm, $userid, $groupid, $excludemyself, $sub->odid);
            echo '</table>';
            echo '</td>';
        }
        echo '</tr>';
    }
}