weiphp 投票插件的主控制器部分代码3

  1 <?php
  2 
  3 function show() {
  4         $vote_id = I ( 'id', 0, 'intval' );        //接受id传参
  5         $openid = get_openid ();    //获取当前用户的OpenId
  6         $token = get_token ();        //获取当前用户的Token
  7         
  8         $info = $this->_getVoteInfo ( $vote_id );    //获取投票插件的信息
  9         
 10         $canJoin = ! empty ( $openid ) && ! empty ( $token ) && ! ($this->_is_overtime ( $vote_id )) && ! ($this->_is_join ( $vote_id, $this->mid, $token ));
 11         $this->assign ( 'canJoin', $canJoin );
 12         // dump ( $canJoin );
 13         // dump(! empty ( $openid ));dump(! empty ( $token ));dump(! ($this->_is_overtime ( $vote_id )));dump(! ($this->_is_join ( $vote_id, $openid, $token )));
 14         
 15         $test_id = intval ( $_REQUEST ['test_id'] );    //转换全局变量的值
 16         $this->assign ( 'event_url', event_url ( '投票', $vote_id ) );        //提交到模版
 17         
 18         $this->display ( T ( 'Addons://Vote@Vote/show' ) );        //调用show模版显示
 19     }
 20     function _getVoteInfo($id) {        //获取投票插件的信息
 21         // 检查ID是否合法
 22         if (empty ( $id ) || 0 == $id) {
 23             $this->error ( "错误的投票ID" );
 24         }
 25         
 26         $map ['id'] = $map2 ['vote_id'] = intval ( $id );        //将参数id转换为整形,并赋值
 27         $info = M ( 'vote' )->where ( $map )->find ();        //查表获得信息
 28         // dump(M ( 'vote' )->getLastSql());
 29         $this->assign ( 'info', $info );        //将信息传给模版
 30         
 31         // dump($info);
 32         $opts = M ( 'vote_option' )->where ( $map2 )->order ( '`order` asc' )->select ();
 33         //查表获得结果集并按正序排列
 34         foreach ( $opts as $p ) {    //设置total
 35             $total += $p ['opt_count'];
 36 
 37         }
 38         foreach ( $opts as &$vo ) {        //设置数组¥vo
 39             $vo ['percent'] = round ( $vo ['opt_count'] * 100 / $total, 1 );    //
 40         }
 41         // dump($opts);
 42         $this->assign ( 'opts', $opts );
 43         $this->assign ( 'num_total', $total );
 44         return $info;
 45     }
 46     // 用户投票信息
 47     function join() {
 48         $token = get_token ();        //获取token
 49         $opts_ids = array_filter ( I ( 'post.optArr' ) );    //转换参数
 50         
 51         $vote_id = intval ( $_POST ["vote_id"] );    //设置转换参数为整形
 52         // 检查ID是否合法
 53         if (empty ( $vote_id ) || 0 == $vote_id) {
 54             $this->error ( "错误的投票ID" );
 55         }
 56         if ($this->_is_overtime ( $vote_id )) {
 57             $this->error ( "请在指定的时间内投票" );
 58         }
 59         if ($this->_is_join ( $vote_id, $this->mid, $token )) {
 60             $this->error ( "您已经投过,请不要重复投" );
 61         }
 62         if (empty ( $_POST ['optArr'] )) {
 63             $this->error ( "请先选择投票项" );
 64         }
 65         // 如果没投过,就添加
 66         $data ["user_id"] = $this->mid;
 67         $data ["vote_id"] = $vote_id;
 68         $data ["token"] = $token;
 69         $data ["options"] = implode ( ',', $opts_ids );
 70         $data ["cTime"] = time ();
 71         $addid = M ( "vote_log" )->add ( $data );
 72         // 投票选项信息的num+1
 73         foreach ( $opts_ids as $v ) {
 74             $v = intval ( $v );
 75             $res = M ( "vote_option" )->where ( 'id=' . $v )->setInc ( "opt_count" );
 76         }
 77         
 78         // 投票信息的vote_count+1
 79         $res = M ( "vote" )->where ( 'id=' . $vote_id )->setInc ( "vote_count" );
 80         
 81         // 增加积分
 82         add_credit ( 'vote' );
 83         
 84         // 连续投票
 85         $next_id = M ( "vote" )->where ( 'id=' . $vote_id )->getField ( "next_id" );
 86         if (! empty ( $next_id )) {
 87             $vote_id = $next_id;
 88         }
 89         
 90         redirect ( U ( 'show', 'id=' . $vote_id ) );
 91     }
 92     //已过期返回 true ,否则返回 false
 93     private function _is_overtime($vote_id) {
 94         // 先看看投票期限过期与否
 95         $the_vote = M ( "vote" )->where ( "id=$vote_id" )->find ();
 96         
 97         if(!empty($the_vote['start_date']) && $the_vote ['start_date'] > NOW_TIME) return ture;
 98         
 99         $deadline = $the_vote ['end_date'] + 86400;        //过期时间
100         if(!empty($the_vote['end_date']) && $deadline <= NOW_TIME) return ture;        //判断
101         
102         return false;
103     }
104     private function _is_join($vote_id, $user_id, $token) {
105         // $vote_limit = M ( 'vote' )->where ( 'id=' . $vote_id )->getField ( 'vote_limit' );
106         $vote_limit = 1;
107         $list = M ( "vote_log" )->where ( "vote_id=$vote_id AND user_id='$user_id' AND token='$token' AND options <>''" )->select ();        //查表
108         $count = count ( $list );    //获取个数
109         $info = array_pop ( $list );    //删除
110         if ($info) {    //若info有值
111             $joinData =  ( ',', $info ['options'] );    //设置$joinData
112             $this->assign ( 'joinData', $joinData );    //赋值给模版
113         }
114         if ($count >= $vote_limit) {        //如果数量大于限制数量,并返回数true
115             return true;
116         }
117         return false;
118     }

 

posted on 2015-07-02 02:02  dennr  阅读(380)  评论(0编辑  收藏  举报