247 function get_user_calc_value($user, $parent, $orig_value) |
247 function get_user_calc_value($user, $parent, $orig_value) |
248 { |
248 { |
249 $this->db->select('asertoj.id,values.id as vid,values.value,values.influence'); |
249 $this->db->select('asertoj.id,values.id as vid,values.value,values.influence'); |
250 $this->db->from('values'); |
250 $this->db->from('values'); |
251 $this->db->join('asertoj','values.assertion = asertoj.id'); |
251 $this->db->join('asertoj','values.assertion = asertoj.id'); |
|
252 $this->db->where('values.user', $user); |
252 $this->db->where('asertoj.parent',$parent); |
253 $this->db->where('asertoj.parent',$parent); |
253 $this->db->where('values.user', $user); |
|
254 $query = $this->db->get(); |
254 $query = $this->db->get(); |
255 |
255 |
256 $val = NULL; |
256 $val = NULL; |
257 if ($parent != NULL) |
257 if ($query->num_rows() > 0) |
258 { |
258 { |
259 if ($query->num_rows() > 0) |
259 $qresult = $query->result_array(); |
260 { |
260 |
261 $qresult = $query->result_array(); |
261 $sum_influence = 0.; |
262 |
262 |
263 $sum_influence = 0; |
263 foreach($qresult as $row) |
264 |
264 { |
265 foreach($qresult as $row) |
265 $sum_influence += abs($row['influence']); |
266 { |
266 } |
267 $sum_influence += abs($row['influence']); |
267 |
268 } |
268 // Calculate the calc_value |
269 |
269 $val = 0; |
270 // Calculate the calc_value |
270 |
271 $val = 0; |
271 foreach($qresult as $row) |
272 foreach($qresult as $row) |
272 { |
|
273 // for $parent == NULL we only need the recursive call. |
|
274 // the $row['influence'] will have no meaning in a head assertion, |
|
275 // so we avoid the division_by_zero error writting a special case. |
|
276 if ($parent != NULL) |
273 { |
277 { |
274 if ($row['influence'] >= 0) |
278 if ($row['influence'] >= 0) |
275 $val += $row['influence'] * $this->get_user_calc_value($user, |
279 $val += $row['influence'] * $this->get_user_calc_value($user, |
276 $row['id'], $row['value']) / $sum_influence; |
280 $row['id'], $row['value']) / $sum_influence; |
277 else |
281 else |
278 $val += $row['influence'] * ($this->get_user_calc_value($user, |
282 $val += $row['influence'] * ($this->get_user_calc_value($user, |
279 $row['id'], $row['value']) - 1) / $sum_influence; |
283 $row['id'], $row['value']) - 1) / $sum_influence; |
280 } |
284 } |
281 } |
285 else |
282 |
286 $this->get_user_calc_value($user, $row['id'], $row['value']); |
|
287 } |
|
288 } |
|
289 |
|
290 if ($parent != NULL) |
|
291 { |
283 // Set the NULL calc_value to those without subassertions, |
292 // Set the NULL calc_value to those without subassertions, |
284 // and the proper value to those with subassertions. |
293 // and the proper value to those with subassertions. |
285 $query2 = $this->db->where('assertion', $parent); |
294 $query2 = $this->db->where('assertion', $parent); |
286 $query2 = $this->db->where('user', $user); |
295 $query2 = $this->db->where('user', $user); |
287 $query2->update('values', array('calc_value' => $val)); |
296 $query2->update('values', array('calc_value' => $val)); |