controllers/opiniarbo.php
changeset 3 f3e55c2386a1
parent 2 7c6c888b2fed
child 5 cb13c07e7e5d
--- 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);
+    }
 }