author | viric@mandarina |
Sun, 28 Dec 2008 20:48:10 +0000 | |
changeset 5 | cb13c07e7e5d |
parent 4 | fcb273554da6 |
child 8 | 971cc5994467 |
permissions | -rw-r--r-- |
3 | 1 |
<?php |
2 |
class Opiniarbo_model extends Model { |
|
3 |
||
4 |
function Opiniarbo_model() |
|
5 |
{ |
|
6 |
parent::Model(); |
|
7 |
} |
|
8 |
||
9 |
function get_user_name($userid) |
|
10 |
{ |
|
11 |
if ($userid == 0) |
|
5 | 12 |
return lang("noone_str"); |
3 | 13 |
$this->db->select('id,name'); |
14 |
$this->db->from('auser'); |
|
15 |
$this->db->where('id', $userid); |
|
16 |
$query = $this->db->get(); |
|
17 |
||
18 |
return $query->row()->name; |
|
19 |
} |
|
20 |
||
21 |
function get_other_users($userid) |
|
22 |
{ |
|
23 |
$this->db->select('id,name'); |
|
24 |
$this->db->from('auser'); |
|
25 |
$this->db->where('id !=', $userid); |
|
26 |
$query = $this->db->get(); |
|
27 |
||
28 |
return $query->result_array(); |
|
29 |
} |
|
30 |
||
31 |
function get_assertions($userid1, $userid2, $parent) |
|
32 |
{ |
|
33 |
$heads = array(); |
|
34 |
// contains: id, assert, value1, value2, calc_value1, calc_value2 |
|
35 |
$user1_heads = $this->get_my_heads_values($userid1, $parent); |
|
36 |
if ($userid2 != 0) |
|
37 |
$user2_heads = $this->get_my_heads_values($userid2, $parent); |
|
38 |
else |
|
39 |
$user2_heads = array(); |
|
40 |
foreach($user1_heads as $row1) |
|
41 |
{ |
|
42 |
$found = FALSE; |
|
43 |
foreach($user2_heads as $row2) |
|
44 |
{ |
|
45 |
if($row2['id'] == $row1['id']) |
|
46 |
{ |
|
47 |
$value2 = $row2['value']; |
|
48 |
if ($parent != NULL) |
|
49 |
$influence2 = $row['influence']; |
|
50 |
$calc_value2 = $row2['calc_value']; |
|
51 |
$found = TRUE; |
|
52 |
break; |
|
53 |
} |
|
54 |
} |
|
55 |
if ($found) |
|
56 |
{ |
|
57 |
$heads[$row1['id']] = array('id' => $row1['id'], |
|
58 |
'vid' => $row1['vid'], |
|
59 |
'text' => $row1['assert'], |
|
60 |
'value1' => $row1['value'], |
|
61 |
'parent' => $row1['parent'], |
|
62 |
'value2' => $value2); |
|
63 |
if ($parent != NULL) |
|
64 |
{ |
|
65 |
$heads[$row1['id']] += array('influence1' => $row1['influence']); |
|
66 |
$heads[$row1['id']] += array('influence2' => $influence2); |
|
67 |
} |
|
68 |
if ($row1['calc_value'] != NULL) |
|
69 |
$heads[$row1['id']] += array('calc_value1' => $row1['calc_value']); |
|
70 |
if ($calc_value2 != NULL) |
|
71 |
$heads[$row1['id']] += array('calc_value2' => $calc_value2); |
|
72 |
||
73 |
} |
|
74 |
else |
|
75 |
{ |
|
76 |
$heads[$row1['id']] = array('id' => $row1['id'], |
|
77 |
'vid' => $row1['vid'], |
|
78 |
'text' => $row1['assert'], |
|
79 |
'parent' => $row1['parent'], |
|
80 |
'value1' => $row1['value']); |
|
81 |
if ($parent != NULL) |
|
82 |
{ |
|
83 |
$heads[$row1['id']] += array('influence1' => $row1['influence']); |
|
84 |
} |
|
85 |
if ($row1['calc_value'] != NULL) |
|
86 |
$heads[$row1['id']] += array('calc_value1' => $row1['calc_value']); |
|
87 |
} |
|
88 |
} |
|
89 |
foreach($user2_heads as $row2) |
|
90 |
{ |
|
91 |
if (!isset($heads[$row2['id']])) |
|
92 |
{ |
|
93 |
$heads[$row2['id']] = array('id' => $row2['id'], |
|
94 |
'text' => $row2['assert'], |
|
95 |
'parent' => $row2['parent'], |
|
96 |
'value2' => $row2['value']); |
|
97 |
if ($row2['calc_value'] != NULL) |
|
98 |
$heads[$row2['id']] += array('calc_value2' => $row2['calc_value']); |
|
99 |
if ($parent != NULL) |
|
100 |
{ |
|
101 |
$heads[$row2['id']] += array('influence2' => $row2['influence']); |
|
102 |
} |
|
103 |
||
104 |
} |
|
105 |
} |
|
106 |
return $heads; |
|
107 |
} |
|
108 |
||
109 |
function get_my_heads_values($user, $parent) |
|
110 |
{ |
|
111 |
$this->db->select('asertoj.id,asertoj.assert,asertoj.parent,values.value,values.influence,' |
|
112 |
.'values.calc_value, values.id as vid'); |
|
113 |
$this->db->from('asertoj'); |
|
114 |
$this->db->join('values', 'values.assertion = asertoj.id'); |
|
115 |
$this->db->where('values.user', $user); |
|
116 |
$this->db->where('asertoj.parent', $parent); |
|
117 |
||
118 |
$query = $this->db->get(); |
|
119 |
return $query->result_array(); |
|
120 |
} |
|
121 |
||
122 |
function get_assertion($userid1, $userid2, $id) |
|
123 |
{ |
|
124 |
$this->db->select('assert,context,parent'); |
|
125 |
$this->db->from('asertoj'); |
|
126 |
$this->db->where('id', $id); |
|
127 |
$query = $this->db->get(); |
|
128 |
$qresult = $query->row_array(); |
|
129 |
||
130 |
$result['id'] = $id; |
|
131 |
$result['text'] = $qresult['assert']; |
|
132 |
$result['context'] = $qresult['context']; |
|
133 |
$result['parent'] = $qresult['parent']; |
|
134 |
||
135 |
$this->db->select('id,value,calc_value,influence'); |
|
136 |
$this->db->from('values'); |
|
137 |
$this->db->where('assertion', $id); |
|
138 |
$this->db->where('user', $userid1); |
|
139 |
$query = $this->db->get(); |
|
140 |
if ($query->num_rows() > 0) |
|
141 |
{ |
|
142 |
$qresult = $query->row_array(); |
|
143 |
$result['vid'] = $qresult['id']; |
|
144 |
if ($qresult['value'] != NULL) |
|
145 |
$result['value1'] = $qresult['value']; |
|
146 |
if ($qresult['influence'] != NULL) |
|
147 |
$result['influence1'] = $qresult['influence']; |
|
148 |
if ($qresult['calc_value'] != NULL) |
|
149 |
$result['calc_value1'] = $qresult['calc_value']; |
|
150 |
} |
|
151 |
||
152 |
$this->db->select('value,calc_value,influence'); |
|
153 |
$this->db->from('values'); |
|
154 |
$this->db->where('assertion', $id); |
|
155 |
$this->db->where('user', $userid2); |
|
156 |
$query = $this->db->get(); |
|
157 |
if ($query->num_rows() > 0) |
|
158 |
{ |
|
159 |
$qresult = $query->row_array(); |
|
160 |
if ($qresult['value'] != NULL) |
|
161 |
$result['value2'] = $qresult['value']; |
|
162 |
if ($qresult['influence'] != NULL) |
|
163 |
$result['influence2'] = $qresult['influence']; |
|
164 |
if ($qresult['calc_value'] != NULL) |
|
165 |
$result['calc_value2'] = $qresult['calc_value']; |
|
166 |
} |
|
167 |
||
168 |
return $result; |
|
169 |
} |
|
170 |
||
171 |
function get_subasserts($parent, $user) |
|
172 |
{ |
|
173 |
$this->db->select('asertoj.id,asertoj.assert,values.value,' |
|
174 |
.'values.calc_value,values.influence, values.id as vid'); |
|
175 |
$this->db->from('asertoj'); |
|
176 |
$this->db->join('values', 'values.assertion = asertoj.id'); |
|
177 |
$this->db->where('values.user', $user); |
|
178 |
$this->db->where('asertoj.parent', $parent); |
|
179 |
||
180 |
$query = $this->db->get(); |
|
181 |
return $query->result_array(); |
|
182 |
} |
|
183 |
||
184 |
function get_preassertions($userid1, $userid2, $id) |
|
185 |
{ |
|
186 |
$result = array(); |
|
187 |
do |
|
188 |
{ |
|
189 |
$assertion = $this->get_assertion($userid1, $userid2, $id); |
|
190 |
$id = $assertion['parent']; |
|
191 |
$result[] = $assertion; |
|
192 |
} while($id != NULL); |
|
193 |
||
194 |
// We already show the main assert, so don't need in the preasserts |
|
195 |
unset($result[0]); |
|
196 |
||
197 |
return array_reverse($result); |
|
198 |
} |
|
199 |
||
200 |
function get_other_subasserts($parent, $user) |
|
201 |
{ |
|
202 |
$query = $this->db->get_where('asertoj', |
|
203 |
array('user !=' => $user, |
|
204 |
'parent' => $parent)); |
|
205 |
return $query->result(); |
|
206 |
} |
|
207 |
||
208 |
function recalc_user_influences($user,$parent) |
|
209 |
{ |
|
4
fcb273554da6
Fixing problems recalculating, and showing values as %2.2f
viric@mandarina
parents:
3
diff
changeset
|
210 |
$this->db->select('values.id as vid,asertoj.id,values.influence'); |
3 | 211 |
$this->db->from('values'); |
212 |
$this->db->join('asertoj','values.assertion = asertoj.id'); |
|
213 |
$this->db->where('asertoj.parent',$parent); |
|
214 |
$this->db->where('values.user', $user); |
|
215 |
$query = $this->db->get(); |
|
216 |
||
217 |
// Remap the influences |
|
218 |
if ($query->num_rows() > 0) |
|
219 |
{ |
|
220 |
$qresult = $query->result_array(); |
|
221 |
||
222 |
$sum_influence = 0; |
|
223 |
||
224 |
if ($parent != NULL) |
|
225 |
{ |
|
226 |
foreach($qresult as $row) |
|
227 |
{ |
|
228 |
$sum_influence += abs($row['influence']); |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
// Remap as needed between a total of -10 and 10 |
|
233 |
foreach($qresult as $row) |
|
234 |
{ |
|
235 |
if ($parent != NULL) |
|
236 |
{ |
|
237 |
$newinfluence = $row['influence'] / $sum_influence; |
|
4
fcb273554da6
Fixing problems recalculating, and showing values as %2.2f
viric@mandarina
parents:
3
diff
changeset
|
238 |
$query2 = $this->db->where('id', $row['vid']); |
3 | 239 |
$query2->update('values', array( |
240 |
'influence' => $newinfluence)); |
|
241 |
} |
|
242 |
$this->recalc_user_influences($user,$row['id']); |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
function get_user_calc_value($user, $parent, $orig_value) |
|
248 |
{ |
|
4
fcb273554da6
Fixing problems recalculating, and showing values as %2.2f
viric@mandarina
parents:
3
diff
changeset
|
249 |
$this->db->select('asertoj.id,values.id as vid,values.value,values.influence'); |
3 | 250 |
$this->db->from('values'); |
251 |
$this->db->join('asertoj','values.assertion = asertoj.id'); |
|
252 |
$this->db->where('asertoj.parent',$parent); |
|
253 |
$this->db->where('values.user', $user); |
|
254 |
$query = $this->db->get(); |
|
255 |
||
5 | 256 |
$val = NULL; |
3 | 257 |
if ($parent != NULL) |
258 |
{ |
|
5 | 259 |
if ($query->num_rows() > 0) |
260 |
{ |
|
261 |
$qresult = $query->result_array(); |
|
262 |
||
263 |
$sum_influence = 0; |
|
264 |
||
265 |
foreach($qresult as $row) |
|
266 |
{ |
|
267 |
$sum_influence += abs($row['influence']); |
|
268 |
} |
|
269 |
||
270 |
// Calculate the calc_value |
|
271 |
$val = 0; |
|
272 |
foreach($qresult as $row) |
|
273 |
{ |
|
274 |
if ($row['influence'] >= 0) |
|
275 |
$val += $row['influence'] * $this->get_user_calc_value($user, |
|
276 |
$row['id'], $row['value']) / $sum_influence; |
|
277 |
else |
|
278 |
$val += $row['influence'] * ($this->get_user_calc_value($user, |
|
279 |
$row['id'], $row['value']) - 1) / $sum_influence; |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
// Set the NULL calc_value to those without subassertions, |
|
284 |
// and the proper value to those with subassertions. |
|
4
fcb273554da6
Fixing problems recalculating, and showing values as %2.2f
viric@mandarina
parents:
3
diff
changeset
|
285 |
$query2 = $this->db->where('assertion', $parent); |
fcb273554da6
Fixing problems recalculating, and showing values as %2.2f
viric@mandarina
parents:
3
diff
changeset
|
286 |
$query2 = $this->db->where('user', $user); |
fcb273554da6
Fixing problems recalculating, and showing values as %2.2f
viric@mandarina
parents:
3
diff
changeset
|
287 |
$query2->update('values', array('calc_value' => $val)); |
3 | 288 |
} |
289 |
||
290 |
if ($val == NULL) |
|
291 |
return $orig_value; |
|
292 |
else |
|
293 |
return $val; |
|
294 |
} |
|
295 |
||
296 |
function recalculate($user) |
|
297 |
{ |
|
298 |
// Get all heads calculated |
|
5 | 299 |
//$this->recalc_user_influences($user, NULL); |
3 | 300 |
$this->get_user_calc_value($user, NULL, 0); |
301 |
} |
|
302 |
||
303 |
function insert_assertion($user, $inputdata) |
|
304 |
{ |
|
305 |
$assertdata = array('assert' => $inputdata['assert']); |
|
306 |
// concat the 'context', if provided. |
|
307 |
if (isset($inputdata['context'])) |
|
308 |
$assertdata += array('context' => $inputdata['context']); |
|
309 |
// concat the 'parent', if provided. |
|
310 |
if (isset($inputdata['parent'])) |
|
311 |
$assertdata += array('parent' => $inputdata['parent']); |
|
312 |
$this->db->insert('asertoj', $assertdata); |
|
313 |
$this->db->select('LAST_INSERT_ID()'); |
|
314 |
$query = $this->db->get(); |
|
315 |
$lastid = array_values($query->row_array()); |
|
316 |
$lastid = $lastid[0]; |
|
317 |
||
318 |
$valuesdata = array('assertion' => $lastid, |
|
319 |
'value' => $inputdata['value'], |
|
320 |
'user' => $user); |
|
321 |
// concat the 'influence', if provided. |
|
322 |
if (isset($inputdata['influence'])) |
|
323 |
$valuesdata += array('influence' => $inputdata['influence']); |
|
324 |
$this->db->insert('values', $valuesdata); |
|
325 |
} |
|
326 |
||
327 |
function modify_values($user, $inputdata) |
|
328 |
{ |
|
329 |
$valuesdata = array('value' => $inputdata['value']); |
|
330 |
if (isset($inputdata['influence'])) |
|
331 |
$valuesdata = array_merge($valuesdata,array('influence' => $inputdata['influence'])); |
|
332 |
||
333 |
$this->db->where('id', $inputdata['valueid']); |
|
334 |
$this->db->update('values', $valuesdata); |
|
335 |
} |
|
336 |
||
337 |
function remove_value($user, $inputdata) |
|
338 |
{ |
|
339 |
$this->db->where('id', $inputdata['valueid']); |
|
340 |
$this->db->delete('values'); |
|
341 |
} |
|
342 |
||
343 |
function add_value($user, $inputdata) |
|
344 |
{ |
|
345 |
$valuesdata = array('value' => $inputdata['value']); |
|
346 |
$valuesdata += array('assertion' => $inputdata['assertid']); |
|
347 |
$valuesdata += array('user' => $user); |
|
348 |
if (isset($inputdata['influence'])) |
|
349 |
$valuesdata += array('influence' => $inputdata['influence']); |
|
350 |
||
351 |
$this->db->insert('values', $valuesdata); |
|
352 |
} |
|
353 |
} |
|
354 |
||
355 |
?> |