예제 #1
0
function calculatePoll($pollid, $parentid = 0, $depth = 0, &$childsValue = array())
{
    global $db;
    $parents = $db->selectList('detailedpoll_questions', '*', "`parentid`=" . $parentid . " AND `detailedpollid` = " . (int) $pollid, "`rank` ASC");
    if ($parents) {
        $newparents = array();
        $childsValue = array('value' => 0, 'percent' => 0);
        foreach ($parents as $parent) {
            $childs = calculatePoll($pollid, $parent['questionid'], $depth + 1, &$childsValue);
            $parent['depth'] = $depth;
            $parent['childs'] = $childs ? count($childs) : 0;
            if ($childs) {
                $parent['value'] = sprintf("%0.2f", $childsValue['value'] / $childsValue['percent']);
                $childsValue['value'] = $parent['value'] * $parent['percent'];
                $childsValue['percent'] = $parent['percent'];
                $newparents[] = $parent;
                $newparents = array_merge($newparents, $childs);
            } else {
                $value = $db->selectOneRow('detailedpoll_answers', 'AVG(`value`) AS value', '`questionid`=' . (int) $parent['questionid']);
                $parent['value'] = sprintf("%0.2f", $value['value']);
                $childsValue['value'] += $value['value'] * $parent['percent'];
                $childsValue['percent'] += $parent['percent'];
                $newparents[] = $parent;
            }
        }
        return $newparents;
    } else {
        return false;
    }
}
예제 #2
0
                    } else {
                        $notify->add($lang->get('detailedpoll'), $lang->get('notify_send_unsuccessfull'));
                    }
                } else {
                    $notify->add($lang->get('detailedpoll'), $lang->get('notify_send_unsuccessfull_fields_missing'));
                    $smarty->assign('values', $_POST['values']);
                }
            }
            if (hasVoted($pollid, $login->currentUserId())) {
                $smarty->assign('values', getMyAnswers($pollid, $login->currentUserId()));
            }
            $smarty->assign('sendAvailable', $poll['state'] == 1);
            $smarty->assign('resultAvailable', $poll['state'] >= 2);
            $smarty->assign('poll', $poll);
            if ($poll['state'] >= 2) {
                $questions = calculatePoll($pollid);
                $smarty->assign('result', getCalculatedPoll($questions));
            } else {
                $questions = getQuestions($pollid);
            }
            $smarty->assign('questions', $questions);
            $colors = array(0 => '#aaa', 1 => '#bbb', 2 => '#ccc', 3 => '#ddd');
            $smarty->assign('color', $colors);
            $smarty->assign('path', $template_dir . '/poll.tpl');
        } else {
            $allpolls = getPolls();
            $smarty->assign('polls', $allpolls);
            $smarty->assign('path', $template_dir . '/polls.tpl');
        }
        break;
}