/**
* lowers a node within its own branch setting it as 
* sub node of the previous sibling. The first son cannot be lowered.
* @param project the current project
* @param group the current group
* @param id the node to be lowered
* @param table the table-tree name
*/
function brainstorm_tree_right($brainstormid, $userid, $groupid, $id)
{
    global $CFG;
    $accessClause = brainstorm_get_accessclauses($userid, $groupid, false);
    /// get ordering and parent for the moving node
    $sql = "\r\n\t    SELECT \r\n\t        itemdest, \r\n\t        intvalue\r\n\t    FROM \r\n\t        {$CFG->prefix}brainstorm_operatordata AS od\r\n\t    WHERE \r\n\t        id = {$id}\r\n\t";
    $res = get_record_sql($sql);
    $ordering = $res->intvalue;
    $fatherid = $res->itemdest;
    /// get previous record if not first. It will become our parent.
    if ($ordering > 1) {
        $orderingbis = $ordering - 1;
        $sql = "\r\n\t\t    SELECT \r\n\t\t        id,\r\n\t\t        id\r\n\t\t    FROM \r\n\t\t        {$CFG->prefix}brainstorm_operatordata AS od\r\n    \t\tWHERE \r\n    \t        brainstormid = {$brainstormid} AND\r\n    \t        operatorid = 'hierarchize' AND\r\n    \t\t    intvalue = {$orderingbis} AND \r\n    \t\t    itemdest = {$fatherid}\r\n    \t\t    {$accessClause}\r\n        ";
        $resid = get_record_sql($sql);
        $newfatherid = $resid->id;
        /// get our upward brothers. They should be ordered back from ordering
        $sql = "\r\n\t\t    SELECT \r\n\t\t        id, \r\n\t\t        intvalue\r\n\t\t    FROM \r\n\t\t        {$CFG->prefix}brainstorm_operatordata AS od\r\n\t\t    WHERE \r\n    \t        brainstormid = {$brainstormid} AND\r\n    \t        operatorid = 'hierarchize' AND\r\n\t\t        intvalue > {$ordering} AND \r\n\t\t        itemdest = {$fatherid} \r\n\t\t        {$accessClause}\r\n\t\t    ORDER BY \r\n\t\t        intvalue\r\n\t\t";
        $newbrotherordering = $ordering;
        /// order back all upward brothers
        if ($resbrothers = get_records_sql($sql)) {
            foreach ($resbrothers as $resbrother) {
                $objet->id = $resbrother->id;
                $objet->intvalue = $newbrotherordering;
                update_record('brainstorm_operatordata', $objet);
                $newbrotherordering = $newbrotherordering + 1;
            }
        }
        $maxordering = brainstorm_tree_get_max_ordering($brainstormid, null, $groupid, true, $newfatherid);
        $newordering = $maxordering + 1;
        // assigning father's id
        $objet->id = $id;
        $objet->itemdest = $newfatherid;
        $objet->intvalue = $newordering;
        update_record('brainstorm_operatordata', $objet);
    }
}
/**
* refreshes the tree data if new responses where in in the meanwhile
*
*/
function hierarchize_refresh_tree($brainstormid, $groupid = 0)
{
    global $CFG, $USER;
    // get those responses who are new
    $sql = "\r\n        SELECT\r\n            r.id,r.id\r\n        FROM\r\n            {$CFG->prefix}brainstorm_responses as r\r\n        WHERE\r\n            r.brainstormid = {$brainstormid} AND\r\n            r.groupid = {$groupid} AND\r\n            r.id NOT IN\r\n        (SELECT\r\n            od.itemsource\r\n        FROM\r\n            {$CFG->prefix}brainstorm_operatordata as od\r\n        WHERE\r\n            od.brainstormid = {$brainstormid} AND\r\n            operatorid = 'hierarchize' AND\r\n            od.groupid = {$groupid} AND\r\n            od.userid = {$USER->id})\r\n    ";
    // echo $sql;
    $diff = get_records_sql($sql);
    $maxordering = brainstorm_tree_get_max_ordering($brainstormid, null, $groupid, 1, 0);
    if ($diff) {
        $treerecord->brainstormid = $brainstormid;
        $treerecord->userid = $USER->id;
        $treerecord->groupid = $groupid;
        $treerecord->operatorid = 'hierarchize';
        $treerecord->itemdest = 0;
        $treerecord->intvalue = $maxordering + 1;
        $treerecord->timemodified = time();
        foreach ($diff as $adif) {
            $treerecord->itemsource = $adif->id;
            if (!insert_record('brainstorm_operatordata', $treerecord)) {
                error("Could not insert tree regeneration records");
            }
            $treerecord->intvalue++;
        }
    }
}