Made multiuser.
authorviric@mandarina
Sun, 28 Dec 2008 16:57:53 +0000
changeset 3 f3e55c2386a1
parent 2 7c6c888b2fed
child 4 fcb273554da6
Made multiuser.
controllers/.opiniarbo.php.swp
controllers/opiniarbo.php
libraries/aulib.php
models/.opiniarbo.php.swp
models/opiniarbo.php
models/opiniarbo_model.php
views/arbo.php
views/assertions.php
views/context.php
views/new_assertion.php
views/page_bottom.php
views/page_head.php
views/users.php
Binary file controllers/.opiniarbo.php.swp has changed
--- a/controllers/opiniarbo.php	Fri Dec 26 01:01:11 2008 +0000
+++ b/controllers/opiniarbo.php	Sun Dec 28 16:57:53 2008 +0000
@@ -2,6 +2,10 @@
 
 class Opiniarbo extends Controller {
 
+    private $mainassert;
+    private $userid;
+    private $userid2;
+
 	function Opiniarbo()
     {
         parent::Controller();
@@ -14,138 +18,38 @@
 
 	function index()
     {
-        $mainassert = $this->uri->segment(3, -1);
-        $parserdata['logindata'] = $this->aulib->getLoginLink();
-        if ($mainassert == -1)
-        {
-            // Managing heads
-            if ($this->input->post('add'))
-            {
-                $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
-                $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
+        // NEW INDEX
+        // Show users
+        $this->mainassert = $this->uri->segment(3, 0);
+        $this->userid = $this->aulib->getSessionId();
+        $this->userid2 = $this->uri->segment(4, 0);
 
-                // Add the new assertion if added succesfully
-                if ($this->form_validation->run())
-                {
-                    $dbdata['assert'] = $this->input->post('text');
-                    $dbdata['value'] = $this->input->post('value')/20.+0.5;
-                    if ($this->input->post('context'))
-                        $dbdata['context'] = $this->input->post('context');
-                    $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
-                        $dbdata);
-                    $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
-                }
-            }
-            else if ($this->input->post('modify'))
-            {
-                $this->form_validation->set_rules('id', 'Valoro', 'required');
-                $this->form_validation->set_rules('vid', 'Valoro', 'required');
-                $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
-
-                // Add the new assertion if added succesfully
-                if ($this->form_validation->run())
-                {
-                    $dbdata['assertid'] = $this->input->post('id');
-                    $dbdata['valueid'] = $this->input->post('vid');
-                    $dbdata['value'] = $this->input->post('value')/20.+0.5;
-                    $this->opiniarbo_model->modify_values($this->aulib->getSessionId(),
-                        $dbdata);
-                    $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
-                }
-            }
-
-            // Show the head assertions
-            $parserdata['asserts'] = $this->opiniarbo_model->get_my_heads($this->aulib->getSessionId());
-            foreach($parserdata['asserts'] as &$row)
-            {
-                if ($row['value'] != NULL)
-                    $row['value'] = ($row['value'] - 0.5) * 20;
-                if ($row['calc_value'] != NULL)
-                    $row['calc_value'] = ($row['calc_value'] - 0.5)*20;
-            }
-            $parserdata['asserts_other'] = $this->opiniarbo_model->get_other_heads($this->aulib->getSessionId());
-            $this->parser->parse('arbo_heads', $parserdata);
+        // If a form told to update userid2, do it
+        if ($this->input->post('other_user'))
+        {
+            $this->userid2 = $this->input->post('other_user');
+        }
+        $this->load->view('page_head');
+        $this->_show_users();
+        if ($this->userid == 0)
+        {
+            // No user logged in
+        }
+        else if ($this->mainassert == 0)
+        {
+            $this->_process_new_assertion(NULL);
+            $this->_show_assertions(NULL, "Bazaj asertoj");
+            $this->_form_new_assertion(NULL, "Nova baza aserto");
         }
         else
         {
-            // Assert chosen
-            $parserdata['mainassert'] = $mainassert;
-
-            if ($this->input->post('add'))
-            {
-                $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
-                $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
-                $this->form_validation->set_rules('influence', 'Influo', 'required');
-
-                // Add the subassertion if possible
-                if ($this->form_validation->run())
-                {
-                    $dbdata['assert'] = $this->input->post('text');
-                    $dbdata['value'] = $this->input->post('value')/20. + 0.5;
-                    $dbdata['influence'] = $this->input->post('influence')/10.;
-                    if ($this->input->post('context'))
-                        $dbdata['context'] = $this->input->post('context');
-                    $dbdata['parent'] = $mainassert;
-                    $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
-                        $dbdata);
-                    $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
-                }
-            }
-            else if ($this->input->post('modify'))
-            {
-                $this->form_validation->set_rules('id', 'Valoro', 'required');
-                $this->form_validation->set_rules('vid', 'Valoro', 'required');
-                $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
-                $this->form_validation->set_rules('influence', 'Influo', 'required');
-
-                // Modify the values if possible
-                if ($this->form_validation->run())
-                {
-                    $dbdata['assertid'] = $this->input->post('id');
-                    $dbdata['valueid'] = $this->input->post('vid');
-                    $dbdata['value'] = $this->input->post('value')/20.+0.5;
-                    $dbdata['influence'] = $this->input->post('influence')/10;
-                    $this->opiniarbo_model->modify_values($this->aulib->getSessionId(),
-                        $dbdata);
-                    $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
-                }
-            }
-            $assertdata = $this->opiniarbo_model->get_assert($mainassert, $this->aulib->getSessionId());
-            $parserdata['maintext'] = $assertdata['assert'];
-            $parserdata['mainvalue'] = $assertdata['value'];
-            if ($parserdata['mainvalue'] != NULL)
-                $parserdata['mainvalue'] = ($parserdata['mainvalue'] - 0.5)*20;
-            $parserdata['maininfluence'] = $assertdata['influence'];
-            if ($parserdata['maininfluence'] != NULL)
-                $parserdata['maininfluence'] = $parserdata['maininfluence'] * 10;
-            $parserdata['maincontext'] = $assertdata['context'];
-            $parserdata['maincalc_value'] = $assertdata['calc_value'];
-            if ($parserdata['maincalc_value'] != NULL)
-                $parserdata['maincalc_value'] = ($parserdata['maincalc_value'] - 0.5)*20;
-
-            $parserdata['subasserts'] = $this->opiniarbo_model->get_subasserts($mainassert, $this->aulib->getSessionId());
-            foreach($parserdata['subasserts'] as &$row)
-            {
-                if ($row['value'] != NULL)
-                    $row['value'] = ($row['value'] - 0.5) * 20;
-                if ($row['influence'] != NULL)
-                    $row['influence'] = $row['influence'] * 10;
-                if ($row['calc_value'] != NULL)
-                    $row['calc_value'] = ($row['calc_value'] - 0.5)*20;
-            }
-
-            $parserdata['preasserts'] = $this->opiniarbo_model->get_preasserts($mainassert, $this->aulib->getSessionId());
-            foreach($parserdata['preasserts'] as &$row)
-            {
-                if ($row['value'] != NULL)
-                    $row['value'] = ($row['value'] - 0.5) * 20;
-                if ($row['influence'] != NULL)
-                    $row['influence'] = $row['influence'] * 10;
-                if ($row['calc_value'] != NULL)
-                    $row['calc_value'] = ($row['calc_value'] - 0.5)*20;
-            }
-            $this->parser->parse('arbo', $parserdata);
+            $this->_process_new_assertion($this->mainassert);
+            $this->_show_preassertions($this->mainassert);
+            $this->_show_assertion($this->mainassert);
+            $this->_show_assertions($this->mainassert, "Subasertoj");
+            $this->_form_new_assertion($this->mainassert, "Nova subaserto");
         }
+        $this->load->view('page_bottom');
     }
 
     function recalculate()
@@ -169,4 +73,191 @@
         $this->form_validation->set_message("_influence_check", "Skribu influon inter -10 kaj 10");
         return FALSE;
     }
+
+    function _show_users()
+    {
+        $viewdata['assertionid'] = $this->mainassert;
+        $viewdata['other_user'] = $this->userid2;
+        $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
+        $viewdata['other_users'] = array_merge(array(array('id' => 0, 'name' => 'Neniu')),
+            $this->opiniarbo_model->get_other_users($this->userid));
+        $viewdata['loginlink'] = $this->aulib->getLoginLink();
+        $this->load->view('users', $viewdata);
+    }
+
+    function _show_preassertions($id)
+    {
+        $viewdata['title'] = "Antaŭasertoj";
+        $viewdata['assertionid'] = $this->mainassert;
+        $viewdata['other_user'] = $this->userid2;
+        $viewdata['user_name'] = $this->opiniarbo_model->get_user_name($this->userid);
+        $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
+
+        // Show the assertions
+        $viewdata['assertions'] = $this->opiniarbo_model->get_preassertions($this->userid, $this->userid2, $id);
+
+        if (count($viewdata['assertions']) > 0 )
+        {
+            foreach($viewdata['assertions'] as &$row)
+            {
+                if (isset($row['value1']))
+                    $row['value1'] = ($row['value1'] - 0.5) * 20;
+                if (isset($row['calc_value1']))
+                    $row['calc_value1'] = ($row['calc_value1'] - 0.5)*20;
+                if (isset($row['value2']))
+                    $row['value2'] = ($row['value2'] - 0.5) * 20;
+                if (isset($row['calc_value2']))
+                    $row['calc_value2'] = ($row['calc_value2'] - 0.5)*20;
+                if ($row['parent'] != NULL)
+                    $viewdata['show_influences'] = TRUE;
+            }
+            $this->load->view('assertions', $viewdata);
+        }
+    }
+
+    function _show_assertion($id)
+    {
+        $viewdata['title'] = "Ĉefa asserto";
+        $viewdata['assertionid'] = $this->mainassert;
+        $viewdata['other_user'] = $this->userid2;
+        $viewdata['user_name'] = $this->opiniarbo_model->get_user_name($this->userid);
+        $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
+        $viewdata['assertions'] = array($this->opiniarbo_model->get_assertion($this->userid, $this->userid2, $id));
+        foreach($viewdata['assertions'] as &$row)
+        {
+            if (isset($row['value1']))
+                $row['value1'] = ($row['value1'] - 0.5) * 20;
+            if (isset($row['calc_value1']))
+                $row['calc_value1'] = ($row['calc_value1'] - 0.5)*20;
+            if (isset($row['value2']))
+                $row['value2'] = ($row['value2'] - 0.5) * 20;
+            if (isset($row['calc_value2']))
+                $row['calc_value2'] = ($row['calc_value2'] - 0.5)*20;
+        }
+        $viewdata['context'] = $viewdata['assertions'][0]['context'];
+        if ($viewdata['assertions'][0]['parent'] != NULL)
+            $viewdata['show_influences'] = TRUE;
+        $this->load->view('assertions', $viewdata);
+        $this->load->view('context', $viewdata);
+    }
+
+    function _show_assertions($parent, $title)
+    {
+        if ($parent != NULL)
+        {
+            $viewdata['show_influences'] = TRUE;
+        }
+        $viewdata['title'] = $title;
+        $viewdata['assertionid'] = $this->mainassert;
+        $viewdata['other_user'] = $this->userid2;
+        $viewdata['user_name'] = $this->opiniarbo_model->get_user_name($this->userid);
+        $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
+
+        // Show the assertions
+        $viewdata['assertions'] = $this->opiniarbo_model->get_assertions($this->userid, $this->userid2, $parent);
+
+        if (count($viewdata['assertions']) > 0 )
+        {
+            foreach($viewdata['assertions'] as &$row)
+            {
+                if (isset($row['value1']))
+                    $row['value1'] = ($row['value1'] - 0.5) * 20;
+                if (isset($row['influence1']))
+                    $row['influence1'] = $row['influence1']*10;
+                if (isset($row['calc_value1']))
+                    $row['calc_value1'] = ($row['calc_value1'] - 0.5)*20;
+                if (isset($row['value2']))
+                    $row['value2'] = ($row['value2'] - 0.5) * 20;
+                if (isset($row['influence2']))
+                    $row['influence2'] = $row['influence2']*10;
+                if (isset($row['calc_value2']))
+                    $row['calc_value2'] = ($row['calc_value2'] - 0.5)*20;
+            }
+            $this->load->view('assertions', $viewdata);
+        }
+    }
+
+    function _process_new_assertion($parent)
+    {
+        if ($this->input->post('add'))
+        {
+            $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
+            $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
+            if ($this->input->post('parent'))
+                $this->form_validation->set_rules('influence', 'Influo', 'required');
+
+            // Add the subassertion if possible
+            if ($this->form_validation->run())
+            {
+                $dbdata['assert'] = $this->input->post('text');
+                $dbdata['value'] = $this->input->post('value')/20. + 0.5;
+                if ($this->input->post('parent'))
+                    $dbdata['influence'] = $this->input->post('influence')/10.;
+                if ($this->input->post('context'))
+                    $dbdata['context'] = $this->input->post('context');
+                $dbdata['parent'] = $this->mainassert;
+                $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
+                    $dbdata);
+                $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
+            }
+        }
+        else if ($this->input->post('modify'))
+        {
+            $this->form_validation->set_rules('id', 'Aserto ID', 'required');
+            $this->form_validation->set_rules('vid', 'Valoro ID', 'required');
+            $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
+            if ($this->input->post('parent'))
+                $this->form_validation->set_rules('influence', 'Influo', 'required');
+
+            // Modify the values if possible
+            if ($this->form_validation->run())
+            {
+                $dbdata['assertid'] = $this->input->post('id');
+                $dbdata['valueid'] = $this->input->post('vid');
+                $dbdata['value'] = $this->input->post('value')/20.+0.5;
+                if ($this->input->post('parent'))
+                    $dbdata['influence'] = $this->input->post('influence')/10;
+                $this->opiniarbo_model->modify_values($this->aulib->getSessionId(),
+                    $dbdata);
+                $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
+            }
+        }
+        else if ($this->input->post('delete'))
+        {
+            $this->form_validation->set_rules('vid', 'Valoro ID', 'required');
+            if ($this->form_validation->run())
+            {
+                $dbdata['valueid'] = $this->input->post('vid');
+                $this->opiniarbo_model->remove_value($this->aulib->getSessionId(),
+                    $dbdata);
+                $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
+            }
+        }
+        else if ($this->input->post('addvalue'))
+        {
+            $this->form_validation->set_rules('id', 'Valoro', 'required');
+            if ($this->form_validation->run())
+            {
+                $dbdata['assertid'] = $this->input->post('id');
+                $dbdata['value'] = $this->input->post('value')/20.+0.5;
+                if ($this->input->post('parent'))
+                    $dbdata['influence'] = $this->input->post('influence')/10;
+                $this->opiniarbo_model->add_value($this->aulib->getSessionId(),
+                        $dbdata);
+                $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
+            }
+        }
+    }
+
+    function _form_new_assertion($parent, $title)
+    {
+        if ($parent != NULL)
+        {
+            $viewdata['parent_assertion_id'] = $parent;
+        }
+        $viewdata['title'] = $title;
+        $viewdata['assertionid'] = $this->mainassert;
+        $viewdata['other_user'] = $this->userid2;
+        $this->load->view('new_assertion', $viewdata);
+    }
 }
--- a/libraries/aulib.php	Fri Dec 26 01:01:11 2008 +0000
+++ b/libraries/aulib.php	Sun Dec 28 16:57:53 2008 +0000
@@ -133,6 +133,14 @@
 	}
 
 	/*
+	 * Return TRUE if user has specified role
+	 */
+	function getUserName($userid) 
+	{
+        return $this->obj->aumodel->userInfo($userid, 'name');
+	}
+
+	/*
 	 * Show "deny access" page
 	 */
 	function denyAccess()
Binary file models/.opiniarbo.php.swp has changed
--- a/models/opiniarbo.php	Fri Dec 26 01:01:11 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,85 +0,0 @@
-<?php
-class Opiniarbo_model extends Model {
-
-    function Opiniarbo_model()
-    {
-        parent::Model();
-    }
-
-    function get_my_heads($user)
-    {
-        $query = $this->db->get_where('asertoj',
-            array('user' => $user, 'parent' => NULL));
-        return $query->result();
-    }
-
-    function get_other_heads($user)
-    {
-        $query = $this->db->get_where('asertoj',
-            array('user !=' => $user, 'parent' => NULL));
-        return $query->result();
-    }
-
-    function get_my_subasserts($parent, $user)
-    {
-        $query = $this->db->get_where('asertoj',
-            array('user' => $user,
-                  'parent' => $parent));
-        return $query->result();
-    }
-
-    function get_other_subasserts($parent, $user)
-    {
-        $query = $this->db->get_where('asertoj',
-            array('user !=' => $user,
-                  'parent' => $parent));
-        return $query->result();
-    }
-
-    function get_calc_value($assert, $orig_value)
-    {
-        $query = $this->db->get_where('asertoj',
-            array('parent' => $assert));
-
-        if ($query->num_rows() > 0)
-        {
-            $val = 0;
-            foreach($query->result() as $row)
-            {
-                $val += get_calc_value($row->id, $row->value);
-            }
-            $query2 = $this->db->where('id', $row->id);
-            $query2->update('asertoj', array(
-                'calc_value' => $val));
-
-            return $val;
-        }
-
-        return $orig_value;
-    }
-
-    function recalculate()
-    {
-        // Get all heads
-        get_calc_value(NULL, 0);
-    }
-
-    function insert_assertion()
-    {
-        if (isset($_POST['parent']))
-        {
-            $data['parent'] = $_POST['parent'];
-            $data['influence'] = $_POST['influence'];
-        }
-        else
-        {
-            $data['parent'] = NULL;
-            $data['influence'] = $_POST['influence'];
-        }
-        $data['assert'] = $_POST['assert'];
-        $data['context'] = $_POST['context'];
-        $data['value'] = float($_POST['value']);
-    }
-}
-
-?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/models/opiniarbo_model.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,352 @@
+<?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);
+    }
+}
+
+?>
--- a/views/arbo.php	Fri Dec 26 01:01:11 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,142 +0,0 @@
-<html>
-<head>
-<title>Opiniarbo</title>
-<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
-<meta http-equiv="cache-control" content="no-cache">
-
-<style type="text/css">
-
-body {
- background-color: #fff;
- margin: 40px;
- font-family: Lucida Grande, Verdana, Sans-serif;
- font-size: 14px;
- color: #4F5155;
-}
-
-a {
- color: #003399;
- background-color: transparent;
- font-weight: normal;
-}
-
-h1 {
- color: #444;
- background-color: transparent;
- border-bottom: 1px solid #D0D0D0;
- font-size: 16px;
- font-weight: bold;
- margin: 24px 0 2px 0;
- padding: 5px 0 6px 0;
-}
-
-code {
- font-family: Monaco, Verdana, Sans-serif;
- font-size: 12px;
- background-color: #f9f9f9;
- border: 1px solid #D0D0D0;
- color: #002166;
- display: block;
- margin: 14px 0 14px 0;
- padding: 12px 10px 12px 10px;
-}
-
-</style>
-</head>
-<body>
-
-<h1>Opiniarbo</h1>
-
-<h2>Uzantoj</h2>
-
-{logindata}
-
-<h2>Antaŭasertoj</h2>
-
-<td><?php echo anchor('opiniarbo/index/', 'Ĉiuj bazaj asertoj'); ?>
-
-<table border=1>
-<tr>
-<th>Id
-<th>Valoro
-<th>Influo
-<th>Kalkulita
-<th>Teksto
-{preasserts}
-<tr>
-<td>{id}
-<td>{value}
-<td>{influence}
-<td>{calc_value}
-<td><?php echo anchor('opiniarbo/index/{id}', '{assert}'); ?>
-{/preasserts}
-</table>
-
-<h2>Ĉefaserto</h2>
-
-<table border=1>
-<tr>
-<th>Id
-<th>Valoro
-<th>Influo
-<th>Kalkulita
-<th>Aserto
-<tr>
-<td>{mainassert}
-<td>{mainvalue}
-<td>{maininfluence}
-<td>{maincalc_value}
-<td>{maintext}
-</table>
-
-<h3>Kunteksto</h3>
-<p>{maincontext}</p>
-
-<h2>Subasertoj</h2>
-
-<table border=1>
-<tr>
-<th>Id
-<th>Valoro
-<th>Influo
-<th>Kalkulita
-<th>Aserto
-<th>Ŝanĝi
-{subasserts}
-<?php echo form_open('opiniarbo/index/{mainassert}'); ?>
-<input type="hidden" name="mainassert" id="mainassert" value="{parent}">
-<input type="hidden" name="id" id="id" value="{id}">
-<input type="hidden" name="vid" id="vid" value="{vid}">
-<tr>
-<td>{id}
-<td><input type="text" name="value" id="value" value="{value}" maxlength="5" size="3" />
-<td><input type="text" name="influence" id="influence" value="{influence}" maxlength="5" size="3" />
-<td>{calc_value}
-<td><?php echo anchor('opiniarbo/index/{id}', '{assert}'); ?>
-<td><input type="submit" name="modify" value="Ŝanĝi"/>
-</form>
-{/subasserts}
-</table>
-
-<h2>Nova subaserto</h2>
-
-<?php echo validation_errors(); ?>
-
-<?php echo form_open('opiniarbo/index/{mainassert}'); ?>
-<input type="hidden" name="mainassert" id="mainassert" value="{parent}">
-<ul>
-<li>Aserto: <input type="text" name="text" id="text" maxlength="500" size="100" />
-<li>Valoro de aserto: <input type="text" name="value" id="value" maxlength="5" size="3" />
-<li>Influo al la ĉefaserto: <input type="text" name="influence" id="influence" maxlength="5" size="3" />
-</ul>
-
-<h3>Kunteksto</h3>
-<p><textarea rows="10" cols="50" name="context" id="context"></textarea></p>
-
-<p><input type="submit" name="add" value="Aldoni"/></p>
-</form>
-
-<p><br />Page rendered in {elapsed_time} seconds</p>
-
-</body>
-</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/assertions.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,99 @@
+<?php if(isset($title))
+    echo "<h2>$title</h2>";
+?>
+<table border=1>
+<tr>
+<th>
+<?php if(isset($show_influences))
+        $colspan=3;
+      else
+        $colspan=2;?>
+<th colspan="<?=$colspan?>"><?=$user_name?>
+<th colspan="<?=$colspan?>"><?=$other_user_name?>
+<tr>
+<th>Id
+<?php if(isset($show_influences)):?>
+<th>Influo
+<?php endif;?>
+<th>Valoro
+<th>Kalkulita
+<?php if(isset($show_influences)):?>
+<th>Influo
+<?php endif;?>
+<th>Valoro
+<th>Kalkulita
+<th>Teksto
+<th>Ŝanĝi
+<?php foreach($assertions as $assertion):?>
+<?=form_open("opiniarbo/index/$assertionid/$other_user");?>
+<tr>
+<?="<input type=\"hidden\" name=\"id\" id=\"id\" value=\"{$assertion['id']}\"/>";?>
+<?php
+    if(isset($assertion['parent']))
+        echo "<input type=\"hidden\" name=\"parent\" id=\"parent\" value=\"{$assertion['parent']}\"/>";
+    echo "<td>{$assertion['id']}";
+    if(isset($assertion['value1']))
+    {
+        $value1 = $assertion['value1'];
+        // We suppose any value1 comes with vid
+        echo "<input type=\"hidden\" name=\"vid\" id=\"vid\" value=\"{$assertion['vid']}\">";
+    }
+    else
+        $value1 = "";
+    if (isset($show_influences))
+    {
+        if (isset($assertion['parent']))
+        {
+            if(isset($assertion['influence1']))
+                $influence1 = $assertion['influence1'];
+            else
+                $influence1 = "";
+            echo "<td><input type=\"text\" name=\"influence\" id=\"influence\" value=\"{$influence1}\" maxlength=\"5\" size=\"3\" />";
+        }
+        else
+            echo "<td>";
+    }
+
+    echo "<td><input type=\"text\" name=\"value\" id=\"value\" value=\"{$value1}\" maxlength=\"5\" size=\"3\" />";
+
+    if (isset($assertion['calc_value1']))
+        echo "<td>{$assertion['calc_value1']}";
+    else
+        echo "<td>";
+
+    if (isset($show_influences))
+    {
+        if (isset($assertion['parent']))
+        {
+            if(isset($assertion['influence2']))
+                $influence2 = $assertion['influence2'];
+            else
+                $influence2 = "";
+            echo "<td>{$influence2}";
+        }
+        else
+            echo "<td>";
+    }
+
+    if (isset($assertion['value2']))
+        echo "<td>{$assertion['value2']}";
+    else
+        echo "<td>";
+
+    if (isset($assertion['calc_value2']))
+        echo "<td>{$assertion['value2']}";
+    else
+        echo "<td>";
+    echo "<td>" . anchor("opiniarbo/index/{$assertion['id']}/{$other_user}",
+        $assertion['text']);
+?>
+<td>
+<?php  if(isset($assertion['value1'])):?>
+    <input type="submit" name="modify" value="Ŝanĝi"/>
+    <input type="submit" name="delete" value="Forigi"/>
+<?php  else:?>
+    <input type="submit" name="addvalue" value="Taksi"/>
+<?php  endif;?>
+</form>
+<?php endforeach;?>
+</table>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/context.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,2 @@
+<h3>Kunteksto</h3>
+<pre><?=$context?></pre>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/new_assertion.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,21 @@
+<?php if(isset($title))
+    echo "<h2>$title</h2>";
+?>
+
+<?php echo validation_errors(); ?>
+
+<?=form_open("opiniarbo/index/$assertionid/$other_user");?>
+<input type="hidden" name="parent" id="parent" value="<?=$assertionid?>">
+<ul>
+<li>Aserto: <input type="text" name="text" id="text" maxlength="500" size="100" />
+<li>Valoro de aserto [-10 falsa, 10 vera]: <input type="text" name="value" id="value" maxlength="5" size="3" />
+<?php if(isset($parent_assertion_id)):?>
+<li>Influo al la patra aserto [-10 mala plena influo, 10 plena influo]: <input type="text" name="influence" id="influence" maxlength="5" size="3" />
+<?php endif;?>
+</ul>
+
+<h3>Kunteksto</h3>
+<p><textarea rows="10" cols="50" name="context" id="context"></textarea></p>
+
+<p><input type="submit" name="add" value="Aldoni"/></p>
+</form>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/page_bottom.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,4 @@
+<p><br />Page rendered in {elapsed_time} seconds</p>
+
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/page_head.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,47 @@
+<html>
+<head>
+<title>Opiniarbo</title>
+<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
+<meta http-equiv="cache-control" content="no-cache">
+
+<style type="text/css">
+
+body {
+ background-color: #fff;
+ margin: 40px;
+ font-family: Lucida Grande, Verdana, Sans-serif;
+ font-size: 14px;
+ color: #4F5155;
+}
+
+a {
+ color: #003399;
+ background-color: transparent;
+ font-weight: normal;
+}
+
+h1 {
+ color: #444;
+ background-color: transparent;
+ border-bottom: 1px solid #D0D0D0;
+ font-size: 16px;
+ font-weight: bold;
+ margin: 24px 0 2px 0;
+ padding: 5px 0 6px 0;
+}
+
+code {
+ font-family: Monaco, Verdana, Sans-serif;
+ font-size: 12px;
+ background-color: #f9f9f9;
+ border: 1px solid #D0D0D0;
+ color: #002166;
+ display: block;
+ margin: 14px 0 14px 0;
+ padding: 12px 10px 12px 10px;
+}
+
+</style>
+</head>
+<body>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/users.php	Sun Dec 28 16:57:53 2008 +0000
@@ -0,0 +1,17 @@
+<h2>Uzantoj</h2>
+<?=$loginlink?>
+
+<h3>Komparite kun <?=$other_user_name?></h3>
+
+<?=form_open("opiniarbo/index/$assertionid/$other_user"); ?>
+    <select name="other_user">
+<?php foreach($other_users as $user):
+    if ($user['id'] != $other_user)
+        echo "<option value=\"{$user['id']}\">{$user['name']}</option>";
+    else
+        echo "<option value=\"{$user['id']}\" selected>{$user['name']}</option>";
+    endforeach;?>
+    <input type="submit" name="compare" value="Kompari kun">
+</form>
+
+<?=anchor("opiniarbo/index/0/$other_user",'Montri la bazajn asertojn');