Beispiel #1
0
function dob_elect_contents($post_id, $bEcho = false)
{
    #echo '<pre>';
    global $wpdb;
    $dob_elect_cmb_vote = get_post_meta($post_id, 'dob_elect_cmb_vote', true);
    $vm_type = empty($dob_elect_cmb_vote['type']) ? 'updown' : $dob_elect_cmb_vote['type'];
    $vm_data = empty($dob_elect_cmb_vote['data']) ? array() : $dob_elect_cmb_vote['data'];
    $vm_begin = empty($dob_elect_cmb_vote['begin']) ? '' : $dob_elect_cmb_vote['begin'];
    $vm_end = empty($dob_elect_cmb_vote['end']) ? '' : $dob_elect_cmb_vote['end'];
    $user_id = get_current_user_id();
    $LOGIN_IP = empty($_SESSION['LOGIN_IP']) ? '' : $_SESSION['LOGIN_IP'];
    $bVote = false;
    if (is_single() && $user_id) {
        $debug = '';
        if (!empty($_POST) && $LOGIN_IP == dob_get_real_ip()) {
            $bVote = true;
            if ((int) $_POST['dob_form_cart']) {
                $debug = dob_common_cart($user_id, $post_id, 'elect');
            } else {
                $debug = dob_common_update($user_id, $post_id, 'elect');
            }
        }
        if ($debug) {
            echo "<pre>{$debug}</pre>";
        }
    }
    // load CACHE
    $ts_struct = $ts_all = $ts_stat = '';
    $ts_post = get_the_modified_time('Y-m-d H:i:s');
    $cached_struct = dob_common_cache(-1, 'all', false, $ts_struct);
    $cached_all = dob_common_cache($post_id, 'all', false, $ts_all);
    $cached_stat_json = dob_common_cache($post_id, 'stat', false, $ts_stat, false);
    $ttids = $elect_latest = $result_stat = $stat = null;
    if (is_array($cached_all)) {
        extract($cached_all);
    }
    $nTotal = null;
    $nDirect = null;
    if (!empty($stat)) {
        extract($stat);
    }
    // ttids: 신규, 포스트변경, 계층변경
    $bTtids = false;
    if ($ttids === null || $ts_all < $ts_post || $ts_all < $ts_struct) {
        $ttids = dob_common_get_selected_hierarchy_leaf_ttids($post_id);
        $bTtids = true;
    }
    // nTotal: 신규, 포스트변경, 계층변경
    if ($nTotal === null || $ts_all < $ts_post || $ts_all < $ts_struct) {
        $nTotal = dob_common_get_users_count($ttids);
        // get all user count
    }
    // elect_latest, stat_detail: 신규, 투표, 포스트변경, 계층변경
    $bLatest = false;
    if ($elect_latest === null || $bVote || $ts_all < $ts_post || $ts_all < $ts_struct) {
        $elect_latest = dob_common_get_latest_by_ttids($post_id, $ttids, 'elect');
        // user_id => rows	// for login_name
        $nDirect = is_array($elect_latest) ? count($elect_latest) : 0;
        $bLatest = true;
    }
    // build final vote results.
    $myinfo = $user_id ? dob_common_get_user_info($user_id) : null;
    $myval = isset($elect_latest[$user_id]) ? (int) $elect_latest[$user_id]['value'] : '';
    #print_r($elect_latest);
    // Cache STAT, 통계: 신규, 실제 통계값 변경
    $bStat = false;
    $stat = ['nDirect' => $nDirect, 'nTotal' => $nTotal];
    $stat_json = json_encode($stat, JSON_UNESCAPED_UNICODE);
    if (empty($cached_stat_json) || $cached_stat_json != $stat_json) {
        $ts_now = date('Y-m-d H:i:s');
        dob_common_cache($post_id, 'stat', $stat_json, $ts_now, false);
        $bStat = true;
    }
    $bResult = false;
    if (strtotime($vm_end) < time() && ($result_stat === null || $bTtids || $bLatest || $bStat)) {
        // AFTER voting period
        $result_stat = array();
        foreach ($elect_latest as $uid => $v) {
            dob_elect_accum_stat($result_stat, $vm_type, $v['value'], 1);
        }
        $bResult = true;
    }
    if ($bTtids || $bLatest || $bStat || $bResult) {
        // Cache Results, 결과: 신규, 포스트변경, 계층변경
        $data = compact('ttids', 'elect_latest', 'result_stat', 'stat');
        dob_common_cache($post_id, 'all', $data);
    }
    ## build HTML /*{{{*/
    //$label_title		= '균형 투표';	//__('Balance Voting', DOBslug);
    $label_result = '투표결과';
    //__('My Vote', DOBslug);
    $label_before = '시작전';
    //__('Statistics', DOBslug);
    $label_ing = '진행중';
    //__('Statistics', DOBslug);
    $label_after = '종료됨';
    //__('Statistics', DOBslug);
    $label_chart = '결과 차트';
    //__('Direct voter', DOBslug);
    $label_my = '내 투표';
    //__('My Vote', DOBslug);
    $label_history = '기록';
    //__('My Vote', DOBslug);
    $label_login = '******';
    //__('Please Login', DOBslug);
    /*}}}*/
    $html_timer = $html_chart = $html_form = $html_history = '';
    if (is_single()) {
        $vm_label = $vm_type == 'updown' ? array(-1 => '반대', 0 => '기권') : array(-1 => '모두반대', 0 => '기권');
        if ($vm_type == 'updown') {
            $vm_label[1] = '찬성';
        } else {
            // choice, plural
            foreach ($vm_data as $k => $v) {
                $vm_label[$k + 1] = $v;
            }
        }
        /*}}}*/
        $ts = time();
        $html_timer = dob_elect_html_timer($ts);
        if ($ts < strtotime($vm_begin)) {
            // BEFORE
            $label_result .= ' : ' . $label_before;
        } elseif (strtotime($vm_begin) < $ts && $ts < strtotime($vm_end)) {
            // VOTING /*{{{*/
            $label_result .= ' : ' . $label_ing;
            $content_form = "<a href='" . wp_login_url($_SERVER['REQUEST_URI']) . "' style='color:red; font-weight:bold'>{$label_login}</a>";
            if ($user_id) {
                if (is_null($ttids)) {
                    $content_form = '선거대상 계층이 지정되지 않았습니다.';
                    //__('Election Hierarchy does not selected.', DOBslug);
                } elseif (empty($ttids) && empty($myinfo->term_taxonomy_id) || is_array($ttids) && !empty($ttids) && !in_array($myinfo->term_taxonomy_id, $ttids)) {
                    $label_restrict = '선거대상 계층이 아닙니다.';
                    //__('Your hierarchy is not available in this voting.', DOBslug);
                    $content_form = "<span style='color:red; font-weight:bold'>{$label_restrict}</span>";
                } elseif (isset($_SESSION['LOGIN_IP']) && $_SESSION['LOGIN_IP'] == dob_get_real_ip()) {
                    $content_form = dob_elect_display_mine($post_id, $vm_type, $vm_label, $myval, $user_id);
                } else {
                    $content_form = '로그인 이후 1시간이 지났거나, 네트워크가 변경되었으니, 다시 로그인해 주세요<br>투표시에는 네트워크(WIFI,LTE,3G)를 변경하지 마세요.';
                    //__('You passed 1-hours after login, or Your network was Changed. Please Login AGAIN.', DOBslug);
                }
            }
            $html_form = <<<HTML
    <div class="panel panel-default">
      <div class="panel-heading" data-toggle="collapse" data-target="#dob_elect_my">
        <span class="panel-title">{$label_my}</span>
      </div>
      <div id="dob_elect_my" class="panel-collapse collapse in">
\t\t\t\t{$content_form} 
      </div>
    </div>
HTML;
            /*}}}*/
        } else {
            // AFTER /*{{{*/
            $label_result .= ' : ' . $label_after;
            $content_chart = dob_elect_html_chart($result_stat, $vm_label, $nTotal, $nDirect, $vm_type == 'plural');
            $html_chart = <<<HTML
    <div class="panel panel-default">
      <div class="panel-heading" data-toggle="collapse" data-target="#dob_elect_html_chart">
        <span class="panel-title">{$label_chart}</span>
      </div>
      <div id="dob_elect_html_chart" class="panel-collapse collapse in">
        {$content_chart}
      </div>
    </div>
HTML;
        }
        /*}}}*/
    }
    $html_stat = dob_elect_html_stat($nDirect, $nTotal, $vm_begin, $vm_end, false);
    $dob_elect = <<<HTML
\t{$html_stat}
\t{$html_chart}
\t{$html_form}
\t{$html_history}
HTML;
    if ($bEcho) {
        echo $dob_elect;
    } else {
        return $dob_elect;
    }
}
Beispiel #2
0
function dob_vote_contents($vm_type, $post_id, $dob_vm_data, $bEcho = false)
{
    #echo '<pre>';
    global $wpdb;
    $user_id = get_current_user_id();
    $bVote = false;
    if (is_single() && $user_id) {
        $debug = '';
        $LOGIN_IP = empty($_SESSION['LOGIN_IP']) ? '' : $_SESSION['LOGIN_IP'];
        if (!empty($_POST) && $LOGIN_IP == dob_get_real_ip()) {
            $bVote = true;
            #echo '<pre>'.print_r($_POST,true).'</pre>';
            if ((int) $_POST['dob_form_cart']) {
                $debug = dob_common_cart($user_id, $post_id, 'offer');
            } else {
                $debug = dob_common_update($user_id, $post_id, 'offer');
            }
        }
        if ($debug) {
            echo "<pre>{$debug}</pre>";
        }
    }
    // load CACHE
    $ts_struct = $ts_all = $ts_stat = '';
    $ts_post = get_the_modified_time('Y-m-d H:i:s');
    $cached_struct = dob_common_cache(-1, 'all', false, $ts_struct);
    $cached_all = dob_common_cache($post_id, 'all', false, $ts_all);
    $cached_stat_json = dob_common_cache($post_id, 'stat', false, $ts_stat, false);
    $gr_vals = $ttids = $hier_voter = $stat_detail = $stat_sum = null;
    if (is_array($cached_all)) {
        extract($cached_all);
    }
    $nTotal = null;
    $nDirect = $nGroup = $nFixed = 0;
    if (!empty($stat_sum)) {
        extract($stat_sum);
    }
    // ttids: 신규, 포스트변경, 계층변경
    if (!is_array($ttids) || $ts_all < $ts_post || $ts_all < $ts_struct) {
        $ttids = dob_common_get_selected_hierarchy_leaf_ttids($post_id);
    }
    // nTotal: 신규, 포스트변경, 계층변경
    if (is_null($nTotal) || $ts_all < $ts_post || $ts_all < $ts_struct) {
        $nTotal = dob_common_get_users_count($ttids);
        // get all user count
    }
    // gr_vals: 신규, 투표, 계층변경
    if (is_null($gr_vals) || $bVote || $ts_all < $ts_struct) {
        $gr_vals = dob_common_get_all_group_ttid_values($post_id);
    }
    #$bVote = true;
    #echo '<pre>'.print_r(compact('nTotal','nDirect','nFixed','nGroup'),true).'</pre>';
    // hier_voter, stat_detail: 신규, 투표, 포스트변경, 계층변경
    if (!is_array($hier_voter) || $bVote || $ts_all < $ts_post || $ts_all < $ts_struct) {
        // RESET count
        $nDirect = $nGroup = $nFixed = 0;
        #$ts = microtime(true);
        $hier_voter = dob_vote_get_hierarchy_voter($post_id, $ttids);
        // order by lft
        #echo '<pre>hi:'.(microtime(true)-$ts).'</pre>';
        #$ts = microtime(true);
        foreach ($hier_voter as $ttid => $v) {
            /*{{{*/
            $all_ids = dob_vote_get_user_hierarchy($ttid);
            if (empty($all_ids)) {
                continue;
                // no hierarchy member
            }
            $uid_vals = $v['uid_vals'];
            $uvc_voted = count($uid_vals);
            $uv_valid = array_filter($uid_vals, function ($v) {
                return !empty($v);
            });
            $uvc_valid = count($uv_valid);
            $uv_null = array_diff($all_ids, array_keys($uv_valid));
            // check leaf's direct voting
            if (empty($v['chl'])) {
                // check null-user's group delegator
                $uv_group = array();
                $uv_tmp = $uv_null;
                /*if ( $user_id && ! in_array($user_id,$uv_null) 
                    && $myinfo && $ttid == $myinfo->term_taxonomy_id
                  ) {
                    $uv_tmp[] = $user_id;
                  } TODO: WHY??? */
                foreach ($uv_tmp as $uid) {
                    // get only available group values
                    $tmp_gtid_vals = dob_vote_get_user_group_ttid_values($uid, $gr_vals, $vm_type, false);
                    if (!empty($tmp_gtid_vals) && !empty($tmp_gtid_vals['value'])) {
                        $uv_group[$uid] = $tmp_gtid_vals;
                    }
                    /*{{{*/
                    /*$gtid_vals = array();
                      // RESET count
                      foreach ( $gr_ttids as $gtid ) {
                        $gtid_vals[$gtid] = $gr_vals[$gtid]->value;
                      }
                      if ( !empty($gtid_vals) ) {
                        // TODO: cache this
                        if ( $value = dob_vote_aggregate_value($vm_type,$gtid_vals) ) {
                          $uv_group[$uid] = array (
                            'gtid_vals' => $gtid_vals,
                            'value' => $value,
                          );
                        }
                      }*/
                    /*}}}*/
                }
                // deduct last-ancestor's influences
                $uvc_group_reflected = count(array_diff(array_keys($uv_group), array_keys($uv_valid)));
                foreach (array_reverse($v['anc']) as $a_ttid) {
                    if (isset($hier_voter[$a_ttid])) {
                        // only exists
                        $hier_voter[$a_ttid]['inf'] -= $uvc_valid + $uvc_group_reflected;
                        if ($hier_voter[$a_ttid]['value']) {
                            break;
                        }
                    }
                }
                // self added leaf data
                $hier_voter[$ttid]['uv_group'] = $uv_group;
                $hier_voter[$ttid]['value'] = null;
            } else {
                ## BRANCH NODE ##
                // decision value
                $value = dob_vote_aggregate_value($vm_type, $uid_vals, count($all_ids));
                // deduct last-ancestor's influences
                if ($value) {
                    // not 0
                    foreach (array_reverse($v['anc']) as $a_ttid) {
                        if (isset($hier_voter[$a_ttid])) {
                            $hier_voter[$a_ttid]['inf'] -= $v['inf'];
                            if ($hier_voter[$a_ttid]['value']) {
                                break;
                            }
                        }
                    }
                }
                // deduct last-ancestor's influences by uvc_valid(Direct-voting)
                if ($uvc_valid) {
                    foreach (array_reverse($v['anc']) as $a_ttid) {
                        if (isset($hier_voter[$a_ttid])) {
                            $hier_voter[$a_ttid]['inf'] -= $uvc_valid;
                            if ($hier_voter[$a_ttid]['value']) {
                                break;
                            }
                        }
                    }
                }
                // self added non-leaf data
                $hier_voter[$ttid]['value'] = $value;
                $hier_voter[$ttid]['all_ids'] = $all_ids;
            }
            // self added common data
            $hier_voter[$ttid]['uv_valid'] = $uv_valid;
            #$hier_voter[$ttid]['uv_null'] = $uv_null;
            #$hier_voter[$ttid]['all_ids'] = $all_ids;
        }
        /*}}}*/
        #echo '<pre>hv:'.(microtime(true)-$ts).'</pre>';
        #$ts = microtime(true);
        $stat_detail = array();
        // with $nDirect,$nFixed,$nGroup
        // build final stat_detail. /*{{{*/
        foreach ($hier_voter as $ttid => $v) {
            if (empty($v['chl'])) {
                // leaf
                // final decision by Direct-Voting
                foreach ($v['uv_valid'] as $uid => $val) {
                    dob_vote_make_stat($stat_detail, $vm_type, $val, 1, 'di');
                    if (!empty($val)) {
                        ++$nDirect;
                    }
                }
                // decision by group-voting
                foreach ($v['uv_group'] as $uid => $info) {
                    if (empty($v['uv_valid'][$uid])) {
                        #echo '<pre>'.print_r([$uid,$info,$v['uv_valid']],true).'</pre>';
                        dob_vote_make_stat($stat_detail, $vm_type, $info['value'], 1, 'gr');
                        ++$nGroup;
                    }
                }
            } else {
                // non-leaf
                if ($v['value']) {
                    // Delegator's decision value is NOT 0
                    $nFixed += $inf = $v['inf'];
                    // accum inf.(deducted nLow)
                    dob_vote_make_stat($stat_detail, $vm_type, $v['value'], $inf, 'hi');
                }
                if (!empty($v['uid_vals'])) {
                    // Delegator's private value
                    foreach ($v['uid_vals'] as $uid => $val) {
                        if ($val) {
                            $nDirect += 1;
                            dob_vote_make_stat($stat_detail, $vm_type, $val, 1, 'di');
                        }
                    }
                }
            }
        }
        /*}}}*/
        #echo '<pre>st:'.(microtime(true)-$ts).'</pre>';
    }
    #echo '<pre>'.print_r($hier_voter,true).'</pre>';
    #file_put_contents('/tmp/hv.'.date('His').'.php',print_r($hier_voter,true));
    $stat_sum = compact('nTotal', 'nDirect', 'nFixed', 'nGroup');
    #echo '<pre>'.print_r($stat_sum,true).'</pre>';
    // Cache STAT, 통계: 신규, 실제 통계값 변경
    $stat_json = json_encode(compact('stat_sum', 'stat_detail'), JSON_UNESCAPED_UNICODE);
    if (empty($cached_stat_json) || $cached_stat_json != $stat_json) {
        $ts_now = date('Y-m-d H:i:s');
        dob_common_cache($post_id, 'stat', $stat_json, $ts_now, false);
    }
    // Cache Results, 결과: 신규, 투표행위, 포스트변경, 계층변경
    if (!is_array($cached_all) || $bVote || $ts_all < $ts_post || $ts_all < $ts_struct) {
        $data = ['gr_vals' => $gr_vals, 'ttids' => $ttids, 'hier_voter' => $hier_voter, 'stat_detail' => $stat_detail, 'stat_sum' => $stat_sum];
        dob_common_cache($post_id, 'all', $data);
    }
    #echo '</pre>';
    $myinfo = $user_id ? dob_common_get_user_info($user_id) : null;
    ## build HTML
    # labels /*{{{*/
    $label_for_devel = '개발자용';
    //__('Balance Voting', DOBslug);
    $label_total = '전체';
    //__('Total Users', DOBslug);
    $label_valid = '유효';
    //__('Total Users', DOBslug);
    $label_hierarchy = '계층';
    //__('Hierarchy voter', DOBslug);
    $label_group = '단체';
    //__('Delegate voter', DOBslug);
    $label_direct = '직접';
    //__('Direct voter', DOBslug);
    $label_result = '결과';
    //__('Direct voter', DOBslug);
    $label_chart = '차트';
    //__('Direct voter', DOBslug);
    $label_my = '내';
    //__('My Vote', DOBslug);
    $label_other = '다른';
    //__('My Vote', DOBslug);
    $label_history = '기록';
    //__('My Vote', DOBslug);
    $label_vote = '투표';
    //__('Vote', DOBslug);
    $label_influence = '영향';
    //__('Direct voter', DOBslug);
    $label_no_pos = '계층이 지정되지 않아,';
    //__('Direct voter', DOBslug);
    $label_no_vote = '투표할 수 없습니다.';
    //__('Direct voter', DOBslug);
    $label_no_analysis = '분석할 수 없습니다.';
    //__('Direct voter', DOBslug);
    $label_invalid_pos = '소속계층이 투표대상이 아닙니다.';
    //__('Direct voter', DOBslug);
    $label_login = '******';
    //__('Please Login', DOBslug);
    $label_analysis = '분석';
    //__('Direct voter', DOBslug);
    $label_3rd = '3순위';
    //__('3rd Priority', DOBslug);
    $label_2rd = '2순위';
    //__('2rd Priority', DOBslug);
    $label_1rd = '1순위';
    //__('1rd Priority', DOBslug);
    $label_no = '없음';
    //__('1rd Priority', DOBslug);
    /*}}}*/
    $html_stat = '';
    // dob_vote_html_stat($stat_sum);
    $html_chart = $html_analysis = $html_myvote = $html_history = '';
    if (is_single()) {
        /*{{{*/
        $vm_legend = $vm_type == 'updown' ? array(-1 => '반대', 0 => '기권') : array(-1 => '모두반대', 0 => '기권');
        if ($vm_type == 'updown') {
            $vm_legend[1] = '찬성';
        } else {
            // choice, plural
            foreach ($dob_vm_data as $k => $v) {
                $vm_legend[$k + 1] = $v;
            }
        }
        #echo '<pre>'.print_r($stat_detail,true).'</pre>';
        $content_chart = dob_vote_html_chart($stat_detail, $vm_legend, $nTotal);
        $html_chart = <<<HTML
    <div class="panel panel-default" style="clear:both;">
      <div class="panel-heading" data-toggle="collapse" data-target="#dob_vote_html_chart">
        <span class="panel-title">{$label_result} {$label_chart}</span>
      </div>
      <div id="dob_vote_html_chart" class="panel-collapse collapse in">
        {$content_chart}
      </div>
    </div>
HTML;
        ############################
        # build html vote analysis #
        ############################
        $content_analysis_all = '';
        /*{{{*/
        $vote_latest = dob_common_get_latest_by_ttids($post_id, $ttids, 'offer');
        // user_id => rows	// for login_name
        $myval = empty($vote_latest[$user_id]) ? null : (int) $vote_latest[$user_id]['value'];
        #echo '<pre>'.print_r($myinfo,true).'</pre>';
        $hierarchies = $h_tr_obj = [];
        /*{{{*/
        $all_group_vals = array();
        foreach ($gr_vals as $gtid => $gr) {
            $group_value_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $gr['value'], false);
            $all_group_vals[$gtid] = $gr['name'] . ':' . $group_value_tooltip;
        }
        $content_analysis_all = "## {$label_total} {$label_group} {$label_vote}<br> &nbsp; " . (empty($all_group_vals) ? $label_no : implode(', ', $all_group_vals)) . '<br>';
        $hierarchies[] = " ## {$label_hierarchy} {$label_influence} {$label_chart}";
        foreach ($hier_voter as $ttid => $v) {
            $uv_valid = $v['uv_valid'];
            $indent = ' &nbsp; ' . str_repeat(' -- ', $v['lvl']);
            $inherit = 0;
            foreach ($v['anc'] as $a_ttid) {
                if (!empty($hier_voter[$a_ttid]['value'])) {
                    $inherit = $hier_voter[$a_ttid]['value'];
                }
            }
            // get affected parent
            $tmp_anc = $v['anc'];
            $parent = empty($tmp_anc) ? 0 : array_pop($tmp_anc);
            while (!empty($tmp_anc) && !isset($h_tr_obj[$parent])) {
                $parent = array_pop($tmp_anc);
            }
            if (empty($v['chl'])) {
                // leaf
                $str_mine = '';
                $str_group = $grname_vals = array();
                // info of myval and mygroup
                if ($myinfo && $ttid == $myinfo->term_taxonomy_id) {
                    $mygroup = isset($v['uv_group'][$user_id]) ? $v['uv_group'][$user_id] : dob_vote_get_user_group_ttid_values($user_id, $gr_vals, $vm_type, true);
                    #echo '<pre>'.var_export([$user_id,$gr_vals,$mygroup],true).'</pre>';
                    if (!empty($mygroup)) {
                        $grname_vals[] = "<span style='background-color:yellow;padding:0;'>[ {$mygroup['value']} ]</span>";
                        foreach ($mygroup['gtid_vals'] as $gtid => $val) {
                            $gr_val_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $val, false);
                            $grname_vals[] = isset($gr_vals[$gtid]) ? $gr_vals[$gtid]['name'] . ':' . $gr_val_tooltip : '';
                        }
                        $str_group = '// ' . implode(', ', $grname_vals);
                    }
                    $myval_tooltip = is_null($myval) ? 'null' : dob_common_value_tooltip($vm_type, $vm_legend, $myval, false);
                    $str_mine = "<span style='color:red;padding:0;'>@{$myinfo->user_nicename}:" . $myval_tooltip . "</span>";
                    $str_mine .= $str_group;
                }
                $uvc_valid = count($v['uv_valid']);
                $hierarchies[] = $indent . $v['tname'] . "({$v['inf']}-{$uvc_valid}) : <u>{$inherit}</u> {$str_mine}";
                $value_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $v['value'], false);
                $inherit_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $inherit, true);
                $decision = empty($v['value']) ? $inherit_tooltip : $value_tooltip;
                $h_tr_obj[$ttid] = ['lvl' => $v['lvl'], 'name' => $v['tname'], 'inf' => "{$v['inf']}-{$uvc_valid}", 'decision' => '', 'inherit' => $inherit_tooltip, 'detail' => $str_mine, 'parent' => $parent];
            } else {
                // branch
                $yes = $no = array();
                foreach ($uv_valid as $uid => $val) {
                    $str = $vote_latest[$uid]['user_nicename'] . ':' . dob_common_value_tooltip($vm_type, $vm_legend, $val, false);
                    $yes[] = $uid == $user_id ? "<span style='color:red'>@{$str}</span>" : $str;
                }
                $yes = implode(', ', $yes);
                $no = array_diff($v['all_ids'], array_keys($uv_valid));
                $no_ids = dob_vote_get_user_nicenames($no);
                $no = empty($no_ids) ? '' : '<strike>' . implode(', ', $no_ids) . '</strike>';
                $val = empty($v['value']) ? "<u>{$inherit}</u>" : "<b>{$v['value']}</b>";
                $hierarchies[] = $indent . $v['tname'] . "({$v['inf']}) : {$val} <span style='background-color:yellow;padding:0;'>[ {$v['value']} ]</span> ({$yes}) {$no}";
                $value_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $v['value'], false);
                $inherit_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $inherit, true);
                $decision = empty($v['value']) ? $inherit_tooltip : $value_tooltip;
                $h_tr_obj[$ttid] = ['lvl' => $v['lvl'], 'name' => $v['tname'], 'inf' => $v['inf'], 'decision' => $decision, 'inherit' => $inherit_tooltip, 'detail' => $yes . ($no ? "// {$no}" : ''), 'parent' => $parent];
            }
        }
        /*}}}*/
        //$content_analysis_all .= implode('<br>',$hierarchies);
        $content_analysis_all .= dob_vote_get_allheir_table('table_analysis_allhier', $h_tr_obj);
        /*}}}*/
        $content_analysis_myhier = $content_analysis_mygroup = "{$label_no_pos} {$label_no_analysis}";
        if ($myinfo) {
            // content_analysis_myhier /*{{{*/
            $vote_latest = dob_common_get_latest_by_ttids($post_id, $ttids, 'offer');
            // user_id => rows, for login_name
            $h_tr_obj = [];
            #echo '<pre>'.print_r([$myinfo->anc],true).'</pre>';
            $my_ttids = explode(',', $myinfo->anc);
            $my_ttids[] = $myinfo->term_taxonomy_id;
            $inherit = 0;
            foreach ($my_ttids as $ttid) {
                $v = empty($hier_voter[$ttid]) ? null : $hier_voter[$ttid];
                #echo '<pre>'.print_r($v['anc'],true).'</pre>';
                #echo '<pre>'.print_r($v['uv_group'],true).'</pre>';
                if (empty($v)) {
                    // get only ttid's info
                    list($v) = dob_common_get_hierarchy_info([$ttid]);
                    // get only one tt info.
                    #echo '<pre>'.print_r([$ttid,$v],true).'</pre>';
                    $inherit_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $inherit, true);
                    $h_tr_obj[$ttid] = ['lvl' => $v->lvl, 'name' => $v->name, 'inf' => $v->inf, 'decision' => $inherit_tooltip, 'inherit' => $inherit_tooltip, 'parent' => $v->parent];
                } elseif (!empty($v['chl'])) {
                    // branch
                    #echo '<pre>'.print_r([$ttid,$v],true).'</pre>';
                    foreach ($v['anc'] as $a_ttid) {
                        if (!empty($hier_voter[$a_ttid]['value'])) {
                            $inherit = $hier_voter[$a_ttid]['value'];
                        }
                    }
                    $uv_valid = $v['uv_valid'];
                    $yes = $no = array();
                    foreach ($uv_valid as $uid => $val) {
                        $val_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $val, false);
                        $str = $vote_latest[$uid]['user_nicename'] . ":{$val_tooltip}";
                        $yes[] = $uid == $user_id ? "<span style='color:red'>@{$str}</span>" : $str;
                    }
                    $yes = implode(', ', $yes);
                    $no = array_diff($v['all_ids'], array_keys($uv_valid));
                    $no_ids = dob_vote_get_user_nicenames($no);
                    $no = empty($no_ids) ? '' : '<strike>' . implode(', ', $no_ids) . '</strike>';
                    // jquery treetable
                    $value_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $v['value'], false);
                    $inherit_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $inherit, true);
                    $decision = empty($v['value']) ? $inherit_tooltip : $value_tooltip;
                    $h_tr_obj[$ttid] = ['lvl' => $v['lvl'], 'name' => $v['tname'], 'inf' => $v['inf'], 'decision' => $decision, 'inherit' => $inherit_tooltip, 'yes' => $yes, 'no' => $no, 'parent' => empty($v['anc']) ? 0 : $v['anc'][count($v['anc']) - 1]];
                }
            }
            $content_analysis_myhier = dob_vote_get_myheir_table('table_analysis_myhier', $h_tr_obj);
            /*}}}*/
            // content_analysis_mygroup /*{{{*/
            // info of myval and mygroup
            $ttid = $myinfo->term_taxonomy_id;
            $mygroup = empty($hier_voter[$ttid]['uv_group'][$user_id]) ? null : $hier_voter[$ttid]['uv_group'][$user_id];
            $my_group_final = $html_group_my = $html_group_other = '';
            $arr_group_other = [];
            if (isset($mygroup['value']) && isset($mygroup['gtid_vals'])) {
                $my_group_final = $mygroup['value'];
                $arr_group_my = [];
                foreach ($mygroup['gtid_vals'] as $gtid => $val) {
                    if (!empty($all_group_vals[$gtid])) {
                        $arr_group_my[] = $all_group_vals[$gtid];
                        unset($all_group_vals[$gtid]);
                    }
                }
                $html_group_my = implode(', ', $arr_group_my);
            }
            $my_group_final_tooltip = dob_common_value_tooltip($vm_type, $vm_legend, $my_group_final, false);
            foreach ($all_group_vals as $gtid => $group) {
                $arr_group_other[] = $group;
            }
            $html_group_other = implode(', ', $arr_group_other);
            $content_analysis_mygroup = <<<HTML
            {$label_my} {$label_group} {$label_result} : <b>{$my_group_final_tooltip}</b> <br>
            {$label_my} {$label_group} {$label_vote} : {$html_group_my} <br>
            {$label_other} {$label_group} {$label_vote} : {$html_group_other}
HTML;
            /*}}}*/
        }
        /*{{{*/
        $html_analysis = <<<HTML
    <div class="panel panel-default" style="clear:both;">
      <div class="panel-heading" data-toggle="collapse" data-target="#dob_vote_html_analysis_section">
        <span class="panel-title">{$label_vote} {$label_analysis}</span>
      </div>
      <div id="dob_vote_html_analysis_section" class="panel-collapse collapse in">
        <div class="panel panel-default" style="clear:both;margin-left:20px;margin-right:20px">
          <div class="panel-heading collapsed" data-toggle="collapse" data-target="#dob_vote_html_analysis_all">
            <span class="panel-title">{$label_total} {$label_analysis} ({$label_for_devel})</span>
          </div>
          <div id="dob_vote_html_analysis_all" class="panel-collapse scrollable collapse">
            {$content_analysis_all}
          </div>
        </div>
        <div class="panel panel-default" style="clear:both;margin-left:20px;margin-right:20px">
          <div class="panel-heading" data-toggle="collapse" data-target="#dob_vote_html_analysis_my_hier">
            <span class="panel-title">{$label_my} {$label_hierarchy} {$label_analysis} ({$label_3rd})</span>
          </div>
          <div id="dob_vote_html_analysis_my_hier" class="panel-collapse scrollable collapse in">
            {$content_analysis_myhier}
          </div>
        </div>
        <div class="panel panel-default" style="clear:both;margin-left:20px;margin-right:20px">
          <div class="panel-heading" data-toggle="collapse" data-target="#dob_vote_html_analysis_my_group">
            <span class="panel-title">{$label_my} {$label_group} {$label_analysis} ({$label_2rd})</span>
          </div>
          <div id="dob_vote_html_analysis_my_group" class="panel-collapse scrollable collapse in">
            {$content_analysis_mygroup}
          </div>
        </div>
      </div>
    </div>
HTML;
        /*}}}*/
        $content_myform = '';
        /*{{{*/
        if (empty($user_id)) {
            $login_url = wp_login_url($_SERVER['REQUEST_URI']);
            $content_myform = "<a href='{$login_url}' style='color:red; font-weight:bold'>{$label_login}</a>";
        } else {
            if (empty($myinfo->term_taxonomy_id)) {
                $content_myform = "<span style='color:red; font-size:1.2em; font-weight:bold'>{$label_no_pos} {$label_no_vote}</span>";
            } else {
                if (!empty($ttids) && !in_array($myinfo->term_taxonomy_id, $ttids)) {
                    $content_myform = "<span style='color:red; font-size:1.2em; font-weight:bold'>{$label_invalid_pos}</span>";
                } else {
                    $content_myform = dob_vote_display_mine($post_id, $vm_type, $vm_legend, $myval, $user_id);
                }
            }
        }
        $html_myvote = <<<HTML
    <div class="panel panel-default" style="clear:both;">
      <div class="panel-heading" data-toggle="collapse" data-target="#dob_vote_html_myvote">
        <span class="panel-title">{$label_my} {$label_vote} ({$label_1rd})</span>
      </div>
      <div id="dob_vote_html_myvote" class="panel-collapse collapse in">
        {$content_myform}
      </div>
    </div>
HTML;
        /*}}}*/
    }
    /*}}}*/
    $dob_vote = <<<HTML
  {$html_stat}
  {$html_chart}
  {$html_analysis}
  {$html_myvote}
  {$html_history} 
HTML;
    if ($bEcho) {
        echo $dob_vote;
    } else {
        return $dob_vote;
    }
}
        } else {
            $sql = "UPDATE {$t_term_taxonomy} SET inf = inf + {$r->inf}\n\t\t\t\tWHERE taxonomy='hierarchy' AND lft < {$r->lft} AND rgt > {$r->rgt}";
        }
        $aff = $wpdb->query($sql);
        echo "\n" . preg_replace('/\\s+/', ' ', print_r($r, true)) . " : " . var_export($aff, true);
    }
}
/*}}}*/
function recalc_anc()
{
    /*{{{*/
    global $wpdb;
    $t_term_taxonomy = $wpdb->prefix . 'term_taxonomy';
    // reset all anc
    $sql = "UPDATE {$t_term_taxonomy} SET anc = 0 WHERE taxonomy='hierarchy'";
    $wpdb->query($sql);
    $sql = "SELECT term_taxonomy_id AS ttid, (\n\t\t\t\tSELECT GROUP_CONCAT(term_taxonomy_id) \n\t\t\t\tFROM {$t_term_taxonomy} \n\t\t\t\tWHERE taxonomy='hierarchy' AND lft < tt.lft AND rgt > tt.rgt\n\t\t\t) AS anc\n\t\tFROM {$t_term_taxonomy} tt\n\t\tWHERE taxonomy='hierarchy'";
    //GROUP BY parent";
    $rows = $wpdb->get_results($sql);
    foreach ($rows as $r) {
        $sql = "UPDATE {$t_term_taxonomy} SET anc = '{$r->anc}' WHERE term_taxonomy_id = {$r->ttid}";
        $aff = $wpdb->query($sql);
        //echo "\n".preg_replace('/\s+/',' ',print_r($r,true))." : ".var_export($aff,true);
    }
}
/*}}}*/
recalc_chl();
recalc_inf();
recalc_anc();
dob_common_cache(-1, 'all', 'recalc_inf_chl_anc.php');
echo PHP_EOL;
Beispiel #4
0
    function widget($args, $instance)
    {
        /*{{{*/
        // show only if single post
        if (!is_single()) {
            return;
        }
        $post_id = get_the_ID();
        $cpt = get_post_type($post_id);
        if (!in_array($cpt, ['offer', 'elect'])) {
            return;
        }
        /*{{{*/
        /*$args = [
          [name] => Widget DoBalance
          [id] => widget-dobalance
          [description] => Sidebar of DoBalance Voting
          [class] => 
          [before_widget] => 
          [after_widget] => 
          [before_title] => 
          [after_title] => 
          [widget_id] => dob_vote_result-2
          [widget_name] => 균형투표 결과
        ]; */
        /*}}}*/
        extract($args);
        $stat = dob_common_cache($post_id, 'stat', false);
        # 1. stat
        $html_stat = empty($stat) ? '' : ($cpt == 'offer' ? dob_vote_html_stat($stat['stat_sum']) : $this->get_elect_html_stat($post_id, $stat));
        # 2. history
        $user_id = get_current_user_id();
        $html_history = '';
        if ($user_id) {
            $logs = dob_common_get_log($post_id, $user_id, $cpt);
            $html_history = $this->get_history_html($logs);
        }
        $content = <<<HTML
\t{$html_stat}
\t{$html_history}
HTML;
        #file_put_contents('/tmp/w',$content);
        $title = isset($instance['title']) ? apply_filters('widget_title', $instance['title']) : $this->title;
        echo $before_widget;
        echo $before_title . $title . $after_title;
        echo $content;
        echo $after_widget;
    }