<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class questionIModel extends X {
    private $tbname = "question";
    private $pkid = "questid";

    
    public function getVolist($where='', $orderby='', $num=0, $limit='') {
        $sql = "SELECT v.*".
            " FROM ".DB_PREFIX."{$this->tbname} AS v".
            " WHERE 1=1";
        $sql .= !empty($where) ? ' AND '.$where : '';
        $sql .= !empty($orderby) ? " ".$orderby : " ORDER BY v.orders ASC";
        if (!empty($limit)) {
            $sql .= " LIMIT {$limit}";
        }
        else {
            $num = intval($num)<1 ? 10 : intval($num);
            $sql .= " LIMIT 0, {$num}";
        }
        $data = parent::$obj->getall($sql);
        if (!empty($data)) {
            $i = 1;
            foreach ($data as $key=>$value) {
                $data[$key]['i'] = $i;
                $i = $i+1;
            }
        }
        return $data;
    }

    
    public function doPolls($queid, $option) {
        $res = false;
        $args = array(
            "polls"=>"[[polls+1]]",
        );
        $res = parent::$obj->update(DB_PREFIX."question_option", $args, "optionid='{$option}'");
        if (true === $res) {
            parent::model("queoption", "am")->updatePolls($queid); 
        }
        return $res;
    }

    
    public function addPollLog($topicid) {
        $logid = parent::$obj->fetch_newid("SELECT MAX(logid) FROM ".DB_PREFIX."question_poll", 1);
        $add_args = array(
            "logid"=>$logid,
            "topicid"=>$topicid,
            "ip"=>XRequest::getip(),
            "addtime"=>time(),
        );
        $res = parent::$obj->insert(DB_PREFIX."question_poll", $add_args);
        return $res;
    }

    
    public function checkCanPoll($topicid) {
        $bool = true;
        $ip = XRequest::getip();
        $_sql = "SELECT * FROM ".DB_PREFIX."question_poll WHERE ip='{$ip}' AND topicid='{$topicid}'";
        $_data = parent::$obj->fetch_first($_sql);
        if (!empty($_data)) {
            $bool = false;
        }
        return $bool;
    }

}
?>
