Made multiuser.
<?php
class Opiniarbo_model extends Model {
function Opiniarbo_model()
{
parent::Model();
}
function get_user_name($userid)
{
if ($userid == 0)
return "Neniu";
$this->db->select('id,name');
$this->db->from('auser');
$this->db->where('id', $userid);
$query = $this->db->get();
return $query->row()->name;
}
function get_other_users($userid)
{
$this->db->select('id,name');
$this->db->from('auser');
$this->db->where('id !=', $userid);
$query = $this->db->get();
return $query->result_array();
}
function get_assertions($userid1, $userid2, $parent)
{
$heads = array();
// contains: id, assert, value1, value2, calc_value1, calc_value2
$user1_heads = $this->get_my_heads_values($userid1, $parent);
if ($userid2 != 0)
$user2_heads = $this->get_my_heads_values($userid2, $parent);
else
$user2_heads = array();
foreach($user1_heads as $row1)
{
$found = FALSE;
foreach($user2_heads as $row2)
{
if($row2['id'] == $row1['id'])
{
$value2 = $row2['value'];
if ($parent != NULL)
$influence2 = $row['influence'];
$calc_value2 = $row2['calc_value'];
$found = TRUE;
break;
}
}
if ($found)
{
$heads[$row1['id']] = array('id' => $row1['id'],
'vid' => $row1['vid'],
'text' => $row1['assert'],
'value1' => $row1['value'],
'parent' => $row1['parent'],
'value2' => $value2);
if ($parent != NULL)
{
$heads[$row1['id']] += array('influence1' => $row1['influence']);
$heads[$row1['id']] += array('influence2' => $influence2);
}
if ($row1['calc_value'] != NULL)
$heads[$row1['id']] += array('calc_value1' => $row1['calc_value']);
if ($calc_value2 != NULL)
$heads[$row1['id']] += array('calc_value2' => $calc_value2);
}
else
{
$heads[$row1['id']] = array('id' => $row1['id'],
'vid' => $row1['vid'],
'text' => $row1['assert'],
'parent' => $row1['parent'],
'value1' => $row1['value']);
if ($parent != NULL)
{
$heads[$row1['id']] += array('influence1' => $row1['influence']);
}
if ($row1['calc_value'] != NULL)
$heads[$row1['id']] += array('calc_value1' => $row1['calc_value']);
}
}
foreach($user2_heads as $row2)
{
if (!isset($heads[$row2['id']]))
{
$heads[$row2['id']] = array('id' => $row2['id'],
'text' => $row2['assert'],
'parent' => $row2['parent'],
'value2' => $row2['value']);
if ($row2['calc_value'] != NULL)
$heads[$row2['id']] += array('calc_value2' => $row2['calc_value']);
if ($parent != NULL)
{
$heads[$row2['id']] += array('influence2' => $row2['influence']);
}
}
}
return $heads;
}
function get_my_heads_values($user, $parent)
{
$this->db->select('asertoj.id,asertoj.assert,asertoj.parent,values.value,values.influence,'
.'values.calc_value, values.id as vid');
$this->db->from('asertoj');
$this->db->join('values', 'values.assertion = asertoj.id');
$this->db->where('values.user', $user);
$this->db->where('asertoj.parent', $parent);
$query = $this->db->get();
return $query->result_array();
}
function get_assertion($userid1, $userid2, $id)
{
$this->db->select('assert,context,parent');
$this->db->from('asertoj');
$this->db->where('id', $id);
$query = $this->db->get();
$qresult = $query->row_array();
$result['id'] = $id;
$result['text'] = $qresult['assert'];
$result['context'] = $qresult['context'];
$result['parent'] = $qresult['parent'];
$this->db->select('id,value,calc_value,influence');
$this->db->from('values');
$this->db->where('assertion', $id);
$this->db->where('user', $userid1);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$qresult = $query->row_array();
$result['vid'] = $qresult['id'];
if ($qresult['value'] != NULL)
$result['value1'] = $qresult['value'];
if ($qresult['influence'] != NULL)
$result['influence1'] = $qresult['influence'];
if ($qresult['calc_value'] != NULL)
$result['calc_value1'] = $qresult['calc_value'];
}
$this->db->select('value,calc_value,influence');
$this->db->from('values');
$this->db->where('assertion', $id);
$this->db->where('user', $userid2);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$qresult = $query->row_array();
if ($qresult['value'] != NULL)
$result['value2'] = $qresult['value'];
if ($qresult['influence'] != NULL)
$result['influence2'] = $qresult['influence'];
if ($qresult['calc_value'] != NULL)
$result['calc_value2'] = $qresult['calc_value'];
}
return $result;
}
function get_subasserts($parent, $user)
{
$this->db->select('asertoj.id,asertoj.assert,values.value,'
.'values.calc_value,values.influence, values.id as vid');
$this->db->from('asertoj');
$this->db->join('values', 'values.assertion = asertoj.id');
$this->db->where('values.user', $user);
$this->db->where('asertoj.parent', $parent);
$query = $this->db->get();
return $query->result_array();
}
function get_preassertions($userid1, $userid2, $id)
{
$result = array();
do
{
$assertion = $this->get_assertion($userid1, $userid2, $id);
$id = $assertion['parent'];
$result[] = $assertion;
} while($id != NULL);
// We already show the main assert, so don't need in the preasserts
unset($result[0]);
return array_reverse($result);
}
function get_other_subasserts($parent, $user)
{
$query = $this->db->get_where('asertoj',
array('user !=' => $user,
'parent' => $parent));
return $query->result();
}
function recalc_user_influences($user,$parent)
{
$this->db->select('values.id,values.influence');
$this->db->from('values');
$this->db->join('asertoj','values.assertion = asertoj.id');
$this->db->where('asertoj.parent',$parent);
$this->db->where('values.user', $user);
$query = $this->db->get();
// Remap the influences
if ($query->num_rows() > 0)
{
$qresult = $query->result_array();
$sum_influence = 0;
if ($parent != NULL)
{
foreach($qresult as $row)
{
$sum_influence += abs($row['influence']);
}
}
// Remap as needed between a total of -10 and 10
foreach($qresult as $row)
{
if ($parent != NULL)
{
$newinfluence = $row['influence'] / $sum_influence;
$query2 = $this->db->where('id', $row['id']);
$query2->update('values', array(
'influence' => $newinfluence));
}
$this->recalc_user_influences($user,$row['id']);
}
}
}
function get_user_calc_value($user, $parent, $orig_value)
{
$this->db->select('values.id,values.value,values.influence');
$this->db->from('values');
$this->db->join('asertoj','values.assertion = asertoj.id');
$this->db->where('asertoj.parent',$parent);
$this->db->where('values.user', $user);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$qresult = $query->result_array();
// Calculate the calc_value
$val = 0;
foreach($qresult as $row)
{
if ($row['influence'] >= 0)
$val += $row['influence'] * $this->get_user_calc_value($user,
$row['id'], $row['value']);
else
$val += $row['influence'] * ($this->get_user_calc_value($user,
$row['id'], $row['value']) - 1);
}
}
else
{
$val = NULL;
}
// Set the NULL calc_value to those without subassertions,
// and the proper value to those with subassertions.
if ($parent != NULL)
{
$query2 = $this->db->where('id', $parent);
$query2->update('values', array(
'calc_value' => $val, 'user' => $user));
}
if ($val == NULL)
return $orig_value;
else
return $val;
}
function recalculate($user)
{
// Get all heads calculated
$this->recalc_user_influences($user, NULL);
$this->get_user_calc_value($user, NULL, 0);
}
function insert_assertion($user, $inputdata)
{
$assertdata = array('assert' => $inputdata['assert']);
// concat the 'context', if provided.
if (isset($inputdata['context']))
$assertdata += array('context' => $inputdata['context']);
// concat the 'parent', if provided.
if (isset($inputdata['parent']))
$assertdata += array('parent' => $inputdata['parent']);
$this->db->insert('asertoj', $assertdata);
$this->db->select('LAST_INSERT_ID()');
$query = $this->db->get();
$lastid = array_values($query->row_array());
$lastid = $lastid[0];
$valuesdata = array('assertion' => $lastid,
'value' => $inputdata['value'],
'user' => $user);
// concat the 'influence', if provided.
if (isset($inputdata['influence']))
$valuesdata += array('influence' => $inputdata['influence']);
$this->db->insert('values', $valuesdata);
}
function modify_values($user, $inputdata)
{
print_r($inputdata);
$valuesdata = array('value' => $inputdata['value']);
if (isset($inputdata['influence']))
$valuesdata = array_merge($valuesdata,array('influence' => $inputdata['influence']));
$this->db->where('id', $inputdata['valueid']);
$this->db->update('values', $valuesdata);
}
function remove_value($user, $inputdata)
{
$this->db->where('id', $inputdata['valueid']);
$this->db->delete('values');
}
function add_value($user, $inputdata)
{
$valuesdata = array('value' => $inputdata['value']);
$valuesdata += array('assertion' => $inputdata['assertid']);
$valuesdata += array('user' => $user);
if (isset($inputdata['influence']))
$valuesdata += array('influence' => $inputdata['influence']);
$this->db->insert('values', $valuesdata);
}
}
?>