Blame view

libs/questioning.class.php 4.63 KB
42868d70   andryeyev   Создал GIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  <?php
  class Questioning{
   var $db = null;
   var $tpl = null;
   var $error = null;
  //var $id = false;
  
   function Questioning(&$db,&$tpl,&$error){
    $this->db = &$db;
    $this->tpl = &$tpl;
    $this->error = &$error;
   }
   
   function trim(&$data){
    foreach($data as $key=>$value){
     if(!is_array($data[$key]))$data[$key] = trim($value);
    }
   }
  
   function valid($data,$upload = null){
  
    if(isset($data['name'])){
     if( !preg_match("/.{1,250}/i",$data['name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Âîïðîñ, îò 1-250 ñèìâîëîâ.";
    }
    
    if(isset($data['answer_name'])){
     if( !preg_match("/.{1,250}/i",$data['answer_name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Îòâåò, îò 1-250 ñèìâîëîâ.";
     if(!isset($data['questioning_id']) || !preg_match("/[0-9]+/",$data['questioning_id']) ) $this->error[] = "Îøèáêà âûáîðà âîïðà.";
    }
  
    if(isset($upload['pic']['name']) && $upload['pic']['name']!=null){
     $type = substr(strrchr($upload['pic']['name'],"."),1);
     if( !preg_match("/^(jpeg|jpg|gif|png)$/i",$type) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èçîáðàæåíèÿ, òîëüêî jpg,png,gif.";
    }
  
   return ( count($this->error) ) ? true : false;
   }
   
   function SaveQuestioning($data){
    $table_name = "questioning";
    if($data['update_id']>0){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$id = $data['update_id'];$where = "id=$id";}else{$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;$id = $this->db->nextId('mySequenceQuestioning');$where = null;}
  
    $fields_values = array("id"=>$id,"name"=>$data['name']);
    if(!$data['update_id']){$fields_values['mktime'] = mktime();}
  
    $this->db->autoExecute($table_name, $fields_values, $DB_AUTOQUERY,$where);
    return $id;
   }
   
   function deleteQuestioning($id){
    $this->db->query("delete from questioning where id=?",array($id));
    $this->db->query("delete from questioning_answer where questioning_id=?",array($id));
   }
   
   function SaveAnswer($data){
    $table_name = "questioning_answer";
    if($data['update_id']>0){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$id = $data['update_id'];$where = "id=$id";}else{$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;$where = null;}
  
    $fields_values = array("name"=>$data['answer_name'],"questioning_id"=>$data['questioning_id']);
    if(!$data['update_id']){$fields_values['mktime'] = mktime();}
  
    $this->db->autoExecute($table_name, $fields_values, $DB_AUTOQUERY,$where);
   }
   
   function AnswerQuestioning($id){
    $this->db->query("update questioning set rate=rate+1 where id=?",array($id));
   }
   function AnswerCounter($id){
    $this->db->query("update questioning_answer set rate=rate+1 where id=?",array($id));
   }
   function clearRate(){
    $this->db->query("delete from questioning_rate_ip where life_mktime<?",array(mktime()));
   }
   function isIpQuestioning($ip,$questioning_id){
    return $this->db->getOne("select count(*) from questioning_rate_ip where ip=? and questioning_id=?",array($ip,$questioning_id));
   }
   function IpQuestioningSave($ip,$questioning_id,$answer_id){
    $hour = 3;
    $table_name = "questioning_rate_ip";
    $fields_values = array("ip"=>$ip,"questioning_id"=>$questioning_id,"answer_id"=>$answer_id,"life_mktime"=>mktime((date("H")+$hour),date("i"),date("s"),date("m"),date("d"),date("Y")));
    $this->db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT);
   }
   
   function viewQuestioningLast(){
    $sql = "select * from questioning order by mktime desc limit 1";
    $row = $this->db->getRow($sql,array(),DB_FETCHMODE_ASSOC);
    $row['answer'] = $this->getQuestioningAnswer($row['id']);
    $this->tpl->assign('questioning_last', $row);
   }
   
   function viewAllQuestioning(){
    $search = array();
    $sql = "select * from questioning where 1=1 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search)." ";
    $sql .= "order by mktime desc";
    $pagerOptions = Array(
      'mode' => 'Sliding',
      'delta' => 6,
      'perPage' => 20,
      'spacesBeforeSeparator' => 1,
      'spacesAfterSeparator' => 1
    );
    $questioningAllData = Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array());
    foreach($questioningAllData['data'] as $key=>$row){
     $questioningAllData['data'][$key]['answer'] = $this->getQuestioningAnswer($row['id']);
    }
    //print_r($questioningAllData);
    $this->tpl->assign('questioningAllData', $questioningAllData);
   }
   
   function getQuestioningAnswer($questioningID){
    $sql = "select * from questioning_answer where questioning_id=? order by mktime desc";
    return $this->db->getAll($sql,array($questioningID),DB_FETCHMODE_ASSOC);
   }
   
   function admin_infoEditQuestioningOne($id){
    $sql = "select * from questioning where id=? limit 1";
    $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('questioning',$row);
   }
   
   function displayQuestioning(){
    $this->tpl->assign("tpl","questioning.tpl");
   }
  }
  ?>