-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratio.ext.php
More file actions
133 lines (101 loc) · 2.9 KB
/
Copy pathratio.ext.php
File metadata and controls
133 lines (101 loc) · 2.9 KB
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
127
128
129
130
131
132
133
<?php
class EXT_Ratio extends SYS_Model_Database
{
//--------------------------------------------------------------------------
public $table = 'ratio';
public $rules = array();
public $uid = 0;
//--------------------------------------------------------------------------
public function init()
{
$this->uid = $this->user->id;
sys::set_config_items(&$this, 'ratio');
$this->fields['ratio'] = array(
'uid' => array(),
'rule' => array(),
'score' => array(),
'postdate' => array(
'default' => time()
),
);
}
//--------------------------------------------------------------------------
public function prepare_row_result(&$row)
{
$row->title = $this->rules[$row->rule]['title'];
$row->score = ($row->score > 0 ? '+' : '') . $row->score;
if ($row->postdate) $row->postdate = date('d.m.Y - H:i', $row->postdate);
return parent::prepare_row_result(&$row);
}
//--------------------------------------------------------------------------
public function up($rule)
{
$price = $this->price($rule);
if ( ! $price) return;
$this->insert($this->table(), array(
'uid' => $this->uid,
'rule' => $rule,
'score' => $price
));
if ($this->uid == $this->user->id)
{
$score = $this->user->score;
}
else
{
$score = $this->db->select('score')->where('users.id=?', $this->uid)->get('users')->row()->score;
}
$this->db->where('id=?', $this->uid);
$this->db->update('users', array(
'score' => $score + $price
));
}
//--------------------------------------------------------------------------
public function down($rule)
{
$price = $this->price($rule);
if ( ! $price) return;
$this->db->where('uid=? AND rule=?', $this->uid, $rule)->limit(1);
$this->delete();
if ($this->uid == $this->user->id)
{
$score = $this->user->score;
}
else
{
$score = $this->db->select('score')->where('users.id=?', $this->uid)->get('users')->row()->score;
}
$this->db->where('id=?', $this->uid);
$this->db->update('users', array(
'score' => $score - $price
));
}
//--------------------------------------------------------------------------
public function price($rule)
{
return isset($this->rules[$rule]) ? $this->rules[$rule]['score'] : 0;
}
//--------------------------------------------------------------------------
public function get()
{
$this->db->where('uid=?', $this->uid);
$this->db->order_by('postdate DESC');
return parent::get();
}
//--------------------------------------------------------------------------
public function validation($rule)
{
$price = $this->price($rule);
if ( ! $price) return TRUE;
if ($this->uid == $this->user->id)
{
$score = $this->user->score;
}
else
{
$score = $this->db->select('score')->where('users.id=?', $this->uid)->get('users')->row()->score;
}
return ($score + $price) >= 0;
}
//--------------------------------------------------------------------------
}