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)
|
|
12 |
return "Neniu";
|
|
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 |
{
|
|
210 |
$this->db->select('values.id,values.influence');
|
|
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;
|
|
238 |
$query2 = $this->db->where('id', $row['id']);
|
|
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 |
{
|
|
249 |
$this->db->select('values.id,values.value,values.influence');
|
|
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 |
|
|
256 |
if ($query->num_rows() > 0)
|
|
257 |
{
|
|
258 |
$qresult = $query->result_array();
|
|
259 |
|
|
260 |
// Calculate the calc_value
|
|
261 |
$val = 0;
|
|
262 |
foreach($qresult as $row)
|
|
263 |
{
|
|
264 |
if ($row['influence'] >= 0)
|
|
265 |
$val += $row['influence'] * $this->get_user_calc_value($user,
|
|
266 |
$row['id'], $row['value']);
|
|
267 |
else
|
|
268 |
$val += $row['influence'] * ($this->get_user_calc_value($user,
|
|
269 |
$row['id'], $row['value']) - 1);
|
|
270 |
}
|
|
271 |
}
|
|
272 |
else
|
|
273 |
{
|
|
274 |
$val = NULL;
|
|
275 |
}
|
|
276 |
|
|
277 |
// Set the NULL calc_value to those without subassertions,
|
|
278 |
// and the proper value to those with subassertions.
|
|
279 |
if ($parent != NULL)
|
|
280 |
{
|
|
281 |
$query2 = $this->db->where('id', $parent);
|
|
282 |
$query2->update('values', array(
|
|
283 |
'calc_value' => $val, 'user' => $user));
|
|
284 |
}
|
|
285 |
|
|
286 |
if ($val == NULL)
|
|
287 |
return $orig_value;
|
|
288 |
else
|
|
289 |
return $val;
|
|
290 |
}
|
|
291 |
|
|
292 |
function recalculate($user)
|
|
293 |
{
|
|
294 |
// Get all heads calculated
|
|
295 |
$this->recalc_user_influences($user, NULL);
|
|
296 |
$this->get_user_calc_value($user, NULL, 0);
|
|
297 |
}
|
|
298 |
|
|
299 |
function insert_assertion($user, $inputdata)
|
|
300 |
{
|
|
301 |
$assertdata = array('assert' => $inputdata['assert']);
|
|
302 |
// concat the 'context', if provided.
|
|
303 |
if (isset($inputdata['context']))
|
|
304 |
$assertdata += array('context' => $inputdata['context']);
|
|
305 |
// concat the 'parent', if provided.
|
|
306 |
if (isset($inputdata['parent']))
|
|
307 |
$assertdata += array('parent' => $inputdata['parent']);
|
|
308 |
$this->db->insert('asertoj', $assertdata);
|
|
309 |
$this->db->select('LAST_INSERT_ID()');
|
|
310 |
$query = $this->db->get();
|
|
311 |
$lastid = array_values($query->row_array());
|
|
312 |
$lastid = $lastid[0];
|
|
313 |
|
|
314 |
$valuesdata = array('assertion' => $lastid,
|
|
315 |
'value' => $inputdata['value'],
|
|
316 |
'user' => $user);
|
|
317 |
// concat the 'influence', if provided.
|
|
318 |
if (isset($inputdata['influence']))
|
|
319 |
$valuesdata += array('influence' => $inputdata['influence']);
|
|
320 |
$this->db->insert('values', $valuesdata);
|
|
321 |
}
|
|
322 |
|
|
323 |
function modify_values($user, $inputdata)
|
|
324 |
{
|
|
325 |
print_r($inputdata);
|
|
326 |
$valuesdata = array('value' => $inputdata['value']);
|
|
327 |
if (isset($inputdata['influence']))
|
|
328 |
$valuesdata = array_merge($valuesdata,array('influence' => $inputdata['influence']));
|
|
329 |
|
|
330 |
$this->db->where('id', $inputdata['valueid']);
|
|
331 |
$this->db->update('values', $valuesdata);
|
|
332 |
}
|
|
333 |
|
|
334 |
function remove_value($user, $inputdata)
|
|
335 |
{
|
|
336 |
$this->db->where('id', $inputdata['valueid']);
|
|
337 |
$this->db->delete('values');
|
|
338 |
}
|
|
339 |
|
|
340 |
function add_value($user, $inputdata)
|
|
341 |
{
|
|
342 |
$valuesdata = array('value' => $inputdata['value']);
|
|
343 |
$valuesdata += array('assertion' => $inputdata['assertid']);
|
|
344 |
$valuesdata += array('user' => $user);
|
|
345 |
if (isset($inputdata['influence']))
|
|
346 |
$valuesdata += array('influence' => $inputdata['influence']);
|
|
347 |
|
|
348 |
$this->db->insert('values', $valuesdata);
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
?>
|