controllers/opiniarbo.php
changeset 2 7c6c888b2fed
parent 1 f08a11b5d207
child 3 f3e55c2386a1
--- a/controllers/opiniarbo.php	Thu Dec 25 21:39:19 2008 +0000
+++ b/controllers/opiniarbo.php	Fri Dec 26 01:01:11 2008 +0000
@@ -19,27 +19,51 @@
         if ($mainassert == -1)
         {
             // Managing heads
-            $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('context', 'Context', 'required|min_length[0]');
-
-            if ($this->form_validation->run())
+            if ($this->input->post('add'))
             {
-                $dbdata['assert'] = $this->input->post('text');
-                $dbdata['value'] = $this->input->post('value')/20.+0.5;
-                $dbdata['context'] = $this->input->post('context');
-                $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
-                    $dbdata);
-                $this->opiniarbo_model->recalculate();
+                $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
+                $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
+
+                // 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 * 10;
+                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);
         }
         else
@@ -47,21 +71,44 @@
             // Assert chosen
             $parserdata['mainassert'] = $mainassert;
 
-            $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', 'callback__influence_check');
-            $this->form_validation->set_rules('context', 'Context', 'required|min_length[0]');
-
-            if ($this->form_validation->run())
+            if ($this->input->post('add'))
             {
-                $dbdata['assert'] = $this->input->post('text');
-                $dbdata['value'] = $this->input->post('value')/20. + 0.5;
-                $dbdata['influence'] = $this->input->post('influence')/10.;
-                $dbdata['context'] = $this->input->post('context');
-                $dbdata['parent'] = $mainassert;
-                $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
-                    $dbdata);
-                $this->opiniarbo_model->recalculate();
+                $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'];