controllers/opiniarbo.php
changeset 3 f3e55c2386a1
parent 2 7c6c888b2fed
child 5 cb13c07e7e5d
equal deleted inserted replaced
2:7c6c888b2fed 3:f3e55c2386a1
     1 <?php
     1 <?php
     2 
     2 
     3 class Opiniarbo extends Controller {
     3 class Opiniarbo extends Controller {
       
     4 
       
     5     private $mainassert;
       
     6     private $userid;
       
     7     private $userid2;
     4 
     8 
     5 	function Opiniarbo()
     9 	function Opiniarbo()
     6     {
    10     {
     7         parent::Controller();
    11         parent::Controller();
     8 		$this->load->library('form_validation');
    12 		$this->load->library('form_validation');
    12 		$this->load->model('opiniarbo_model');
    16 		$this->load->model('opiniarbo_model');
    13     }
    17     }
    14 
    18 
    15 	function index()
    19 	function index()
    16     {
    20     {
    17         $mainassert = $this->uri->segment(3, -1);
    21         // NEW INDEX
    18         $parserdata['logindata'] = $this->aulib->getLoginLink();
    22         // Show users
    19         if ($mainassert == -1)
    23         $this->mainassert = $this->uri->segment(3, 0);
    20         {
    24         $this->userid = $this->aulib->getSessionId();
    21             // Managing heads
    25         $this->userid2 = $this->uri->segment(4, 0);
    22             if ($this->input->post('add'))
    26 
    23             {
    27         // If a form told to update userid2, do it
    24                 $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
    28         if ($this->input->post('other_user'))
    25                 $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
    29         {
    26 
    30             $this->userid2 = $this->input->post('other_user');
    27                 // Add the new assertion if added succesfully
    31         }
    28                 if ($this->form_validation->run())
    32         $this->load->view('page_head');
    29                 {
    33         $this->_show_users();
    30                     $dbdata['assert'] = $this->input->post('text');
    34         if ($this->userid == 0)
    31                     $dbdata['value'] = $this->input->post('value')/20.+0.5;
    35         {
    32                     if ($this->input->post('context'))
    36             // No user logged in
    33                         $dbdata['context'] = $this->input->post('context');
    37         }
    34                     $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
    38         else if ($this->mainassert == 0)
    35                         $dbdata);
    39         {
    36                     $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
    40             $this->_process_new_assertion(NULL);
    37                 }
    41             $this->_show_assertions(NULL, "Bazaj asertoj");
    38             }
    42             $this->_form_new_assertion(NULL, "Nova baza aserto");
    39             else if ($this->input->post('modify'))
       
    40             {
       
    41                 $this->form_validation->set_rules('id', 'Valoro', 'required');
       
    42                 $this->form_validation->set_rules('vid', 'Valoro', 'required');
       
    43                 $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
       
    44 
       
    45                 // Add the new assertion if added succesfully
       
    46                 if ($this->form_validation->run())
       
    47                 {
       
    48                     $dbdata['assertid'] = $this->input->post('id');
       
    49                     $dbdata['valueid'] = $this->input->post('vid');
       
    50                     $dbdata['value'] = $this->input->post('value')/20.+0.5;
       
    51                     $this->opiniarbo_model->modify_values($this->aulib->getSessionId(),
       
    52                         $dbdata);
       
    53                     $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
    54                 }
       
    55             }
       
    56 
       
    57             // Show the head assertions
       
    58             $parserdata['asserts'] = $this->opiniarbo_model->get_my_heads($this->aulib->getSessionId());
       
    59             foreach($parserdata['asserts'] as &$row)
       
    60             {
       
    61                 if ($row['value'] != NULL)
       
    62                     $row['value'] = ($row['value'] - 0.5) * 20;
       
    63                 if ($row['calc_value'] != NULL)
       
    64                     $row['calc_value'] = ($row['calc_value'] - 0.5)*20;
       
    65             }
       
    66             $parserdata['asserts_other'] = $this->opiniarbo_model->get_other_heads($this->aulib->getSessionId());
       
    67             $this->parser->parse('arbo_heads', $parserdata);
       
    68         }
    43         }
    69         else
    44         else
    70         {
    45         {
    71             // Assert chosen
    46             $this->_process_new_assertion($this->mainassert);
    72             $parserdata['mainassert'] = $mainassert;
    47             $this->_show_preassertions($this->mainassert);
    73 
    48             $this->_show_assertion($this->mainassert);
    74             if ($this->input->post('add'))
    49             $this->_show_assertions($this->mainassert, "Subasertoj");
    75             {
    50             $this->_form_new_assertion($this->mainassert, "Nova subaserto");
    76                 $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
    51         }
    77                 $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
    52         $this->load->view('page_bottom');
    78                 $this->form_validation->set_rules('influence', 'Influo', 'required');
       
    79 
       
    80                 // Add the subassertion if possible
       
    81                 if ($this->form_validation->run())
       
    82                 {
       
    83                     $dbdata['assert'] = $this->input->post('text');
       
    84                     $dbdata['value'] = $this->input->post('value')/20. + 0.5;
       
    85                     $dbdata['influence'] = $this->input->post('influence')/10.;
       
    86                     if ($this->input->post('context'))
       
    87                         $dbdata['context'] = $this->input->post('context');
       
    88                     $dbdata['parent'] = $mainassert;
       
    89                     $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
       
    90                         $dbdata);
       
    91                     $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
    92                 }
       
    93             }
       
    94             else if ($this->input->post('modify'))
       
    95             {
       
    96                 $this->form_validation->set_rules('id', 'Valoro', 'required');
       
    97                 $this->form_validation->set_rules('vid', 'Valoro', 'required');
       
    98                 $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
       
    99                 $this->form_validation->set_rules('influence', 'Influo', 'required');
       
   100 
       
   101                 // Modify the values if possible
       
   102                 if ($this->form_validation->run())
       
   103                 {
       
   104                     $dbdata['assertid'] = $this->input->post('id');
       
   105                     $dbdata['valueid'] = $this->input->post('vid');
       
   106                     $dbdata['value'] = $this->input->post('value')/20.+0.5;
       
   107                     $dbdata['influence'] = $this->input->post('influence')/10;
       
   108                     $this->opiniarbo_model->modify_values($this->aulib->getSessionId(),
       
   109                         $dbdata);
       
   110                     $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
   111                 }
       
   112             }
       
   113             $assertdata = $this->opiniarbo_model->get_assert($mainassert, $this->aulib->getSessionId());
       
   114             $parserdata['maintext'] = $assertdata['assert'];
       
   115             $parserdata['mainvalue'] = $assertdata['value'];
       
   116             if ($parserdata['mainvalue'] != NULL)
       
   117                 $parserdata['mainvalue'] = ($parserdata['mainvalue'] - 0.5)*20;
       
   118             $parserdata['maininfluence'] = $assertdata['influence'];
       
   119             if ($parserdata['maininfluence'] != NULL)
       
   120                 $parserdata['maininfluence'] = $parserdata['maininfluence'] * 10;
       
   121             $parserdata['maincontext'] = $assertdata['context'];
       
   122             $parserdata['maincalc_value'] = $assertdata['calc_value'];
       
   123             if ($parserdata['maincalc_value'] != NULL)
       
   124                 $parserdata['maincalc_value'] = ($parserdata['maincalc_value'] - 0.5)*20;
       
   125 
       
   126             $parserdata['subasserts'] = $this->opiniarbo_model->get_subasserts($mainassert, $this->aulib->getSessionId());
       
   127             foreach($parserdata['subasserts'] as &$row)
       
   128             {
       
   129                 if ($row['value'] != NULL)
       
   130                     $row['value'] = ($row['value'] - 0.5) * 20;
       
   131                 if ($row['influence'] != NULL)
       
   132                     $row['influence'] = $row['influence'] * 10;
       
   133                 if ($row['calc_value'] != NULL)
       
   134                     $row['calc_value'] = ($row['calc_value'] - 0.5)*20;
       
   135             }
       
   136 
       
   137             $parserdata['preasserts'] = $this->opiniarbo_model->get_preasserts($mainassert, $this->aulib->getSessionId());
       
   138             foreach($parserdata['preasserts'] as &$row)
       
   139             {
       
   140                 if ($row['value'] != NULL)
       
   141                     $row['value'] = ($row['value'] - 0.5) * 20;
       
   142                 if ($row['influence'] != NULL)
       
   143                     $row['influence'] = $row['influence'] * 10;
       
   144                 if ($row['calc_value'] != NULL)
       
   145                     $row['calc_value'] = ($row['calc_value'] - 0.5)*20;
       
   146             }
       
   147             $this->parser->parse('arbo', $parserdata);
       
   148         }
       
   149     }
    53     }
   150 
    54 
   151     function recalculate()
    55     function recalculate()
   152     {
    56     {
   153         $this->opiniarbo_model->recalculate();
    57         $this->opiniarbo_model->recalculate();
   167         if (strlen($str) > 0 &&  $str <= 10.0 && $str >= -10.0)
    71         if (strlen($str) > 0 &&  $str <= 10.0 && $str >= -10.0)
   168             return TRUE;
    72             return TRUE;
   169         $this->form_validation->set_message("_influence_check", "Skribu influon inter -10 kaj 10");
    73         $this->form_validation->set_message("_influence_check", "Skribu influon inter -10 kaj 10");
   170         return FALSE;
    74         return FALSE;
   171     }
    75     }
       
    76 
       
    77     function _show_users()
       
    78     {
       
    79         $viewdata['assertionid'] = $this->mainassert;
       
    80         $viewdata['other_user'] = $this->userid2;
       
    81         $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
       
    82         $viewdata['other_users'] = array_merge(array(array('id' => 0, 'name' => 'Neniu')),
       
    83             $this->opiniarbo_model->get_other_users($this->userid));
       
    84         $viewdata['loginlink'] = $this->aulib->getLoginLink();
       
    85         $this->load->view('users', $viewdata);
       
    86     }
       
    87 
       
    88     function _show_preassertions($id)
       
    89     {
       
    90         $viewdata['title'] = "Antaŭasertoj";
       
    91         $viewdata['assertionid'] = $this->mainassert;
       
    92         $viewdata['other_user'] = $this->userid2;
       
    93         $viewdata['user_name'] = $this->opiniarbo_model->get_user_name($this->userid);
       
    94         $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
       
    95 
       
    96         // Show the assertions
       
    97         $viewdata['assertions'] = $this->opiniarbo_model->get_preassertions($this->userid, $this->userid2, $id);
       
    98 
       
    99         if (count($viewdata['assertions']) > 0 )
       
   100         {
       
   101             foreach($viewdata['assertions'] as &$row)
       
   102             {
       
   103                 if (isset($row['value1']))
       
   104                     $row['value1'] = ($row['value1'] - 0.5) * 20;
       
   105                 if (isset($row['calc_value1']))
       
   106                     $row['calc_value1'] = ($row['calc_value1'] - 0.5)*20;
       
   107                 if (isset($row['value2']))
       
   108                     $row['value2'] = ($row['value2'] - 0.5) * 20;
       
   109                 if (isset($row['calc_value2']))
       
   110                     $row['calc_value2'] = ($row['calc_value2'] - 0.5)*20;
       
   111                 if ($row['parent'] != NULL)
       
   112                     $viewdata['show_influences'] = TRUE;
       
   113             }
       
   114             $this->load->view('assertions', $viewdata);
       
   115         }
       
   116     }
       
   117 
       
   118     function _show_assertion($id)
       
   119     {
       
   120         $viewdata['title'] = "Ĉefa asserto";
       
   121         $viewdata['assertionid'] = $this->mainassert;
       
   122         $viewdata['other_user'] = $this->userid2;
       
   123         $viewdata['user_name'] = $this->opiniarbo_model->get_user_name($this->userid);
       
   124         $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
       
   125         $viewdata['assertions'] = array($this->opiniarbo_model->get_assertion($this->userid, $this->userid2, $id));
       
   126         foreach($viewdata['assertions'] as &$row)
       
   127         {
       
   128             if (isset($row['value1']))
       
   129                 $row['value1'] = ($row['value1'] - 0.5) * 20;
       
   130             if (isset($row['calc_value1']))
       
   131                 $row['calc_value1'] = ($row['calc_value1'] - 0.5)*20;
       
   132             if (isset($row['value2']))
       
   133                 $row['value2'] = ($row['value2'] - 0.5) * 20;
       
   134             if (isset($row['calc_value2']))
       
   135                 $row['calc_value2'] = ($row['calc_value2'] - 0.5)*20;
       
   136         }
       
   137         $viewdata['context'] = $viewdata['assertions'][0]['context'];
       
   138         if ($viewdata['assertions'][0]['parent'] != NULL)
       
   139             $viewdata['show_influences'] = TRUE;
       
   140         $this->load->view('assertions', $viewdata);
       
   141         $this->load->view('context', $viewdata);
       
   142     }
       
   143 
       
   144     function _show_assertions($parent, $title)
       
   145     {
       
   146         if ($parent != NULL)
       
   147         {
       
   148             $viewdata['show_influences'] = TRUE;
       
   149         }
       
   150         $viewdata['title'] = $title;
       
   151         $viewdata['assertionid'] = $this->mainassert;
       
   152         $viewdata['other_user'] = $this->userid2;
       
   153         $viewdata['user_name'] = $this->opiniarbo_model->get_user_name($this->userid);
       
   154         $viewdata['other_user_name'] = $this->opiniarbo_model->get_user_name($this->userid2);
       
   155 
       
   156         // Show the assertions
       
   157         $viewdata['assertions'] = $this->opiniarbo_model->get_assertions($this->userid, $this->userid2, $parent);
       
   158 
       
   159         if (count($viewdata['assertions']) > 0 )
       
   160         {
       
   161             foreach($viewdata['assertions'] as &$row)
       
   162             {
       
   163                 if (isset($row['value1']))
       
   164                     $row['value1'] = ($row['value1'] - 0.5) * 20;
       
   165                 if (isset($row['influence1']))
       
   166                     $row['influence1'] = $row['influence1']*10;
       
   167                 if (isset($row['calc_value1']))
       
   168                     $row['calc_value1'] = ($row['calc_value1'] - 0.5)*20;
       
   169                 if (isset($row['value2']))
       
   170                     $row['value2'] = ($row['value2'] - 0.5) * 20;
       
   171                 if (isset($row['influence2']))
       
   172                     $row['influence2'] = $row['influence2']*10;
       
   173                 if (isset($row['calc_value2']))
       
   174                     $row['calc_value2'] = ($row['calc_value2'] - 0.5)*20;
       
   175             }
       
   176             $this->load->view('assertions', $viewdata);
       
   177         }
       
   178     }
       
   179 
       
   180     function _process_new_assertion($parent)
       
   181     {
       
   182         if ($this->input->post('add'))
       
   183         {
       
   184             $this->form_validation->set_rules('text', 'Aserta teksto', 'required|min_length[1]');
       
   185             $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
       
   186             if ($this->input->post('parent'))
       
   187                 $this->form_validation->set_rules('influence', 'Influo', 'required');
       
   188 
       
   189             // Add the subassertion if possible
       
   190             if ($this->form_validation->run())
       
   191             {
       
   192                 $dbdata['assert'] = $this->input->post('text');
       
   193                 $dbdata['value'] = $this->input->post('value')/20. + 0.5;
       
   194                 if ($this->input->post('parent'))
       
   195                     $dbdata['influence'] = $this->input->post('influence')/10.;
       
   196                 if ($this->input->post('context'))
       
   197                     $dbdata['context'] = $this->input->post('context');
       
   198                 $dbdata['parent'] = $this->mainassert;
       
   199                 $this->opiniarbo_model->insert_assertion($this->aulib->getSessionId(),
       
   200                     $dbdata);
       
   201                 $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
   202             }
       
   203         }
       
   204         else if ($this->input->post('modify'))
       
   205         {
       
   206             $this->form_validation->set_rules('id', 'Aserto ID', 'required');
       
   207             $this->form_validation->set_rules('vid', 'Valoro ID', 'required');
       
   208             $this->form_validation->set_rules('value', 'Valoro', 'callback__value_check');
       
   209             if ($this->input->post('parent'))
       
   210                 $this->form_validation->set_rules('influence', 'Influo', 'required');
       
   211 
       
   212             // Modify the values if possible
       
   213             if ($this->form_validation->run())
       
   214             {
       
   215                 $dbdata['assertid'] = $this->input->post('id');
       
   216                 $dbdata['valueid'] = $this->input->post('vid');
       
   217                 $dbdata['value'] = $this->input->post('value')/20.+0.5;
       
   218                 if ($this->input->post('parent'))
       
   219                     $dbdata['influence'] = $this->input->post('influence')/10;
       
   220                 $this->opiniarbo_model->modify_values($this->aulib->getSessionId(),
       
   221                     $dbdata);
       
   222                 $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
   223             }
       
   224         }
       
   225         else if ($this->input->post('delete'))
       
   226         {
       
   227             $this->form_validation->set_rules('vid', 'Valoro ID', 'required');
       
   228             if ($this->form_validation->run())
       
   229             {
       
   230                 $dbdata['valueid'] = $this->input->post('vid');
       
   231                 $this->opiniarbo_model->remove_value($this->aulib->getSessionId(),
       
   232                     $dbdata);
       
   233                 $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
   234             }
       
   235         }
       
   236         else if ($this->input->post('addvalue'))
       
   237         {
       
   238             $this->form_validation->set_rules('id', 'Valoro', 'required');
       
   239             if ($this->form_validation->run())
       
   240             {
       
   241                 $dbdata['assertid'] = $this->input->post('id');
       
   242                 $dbdata['value'] = $this->input->post('value')/20.+0.5;
       
   243                 if ($this->input->post('parent'))
       
   244                     $dbdata['influence'] = $this->input->post('influence')/10;
       
   245                 $this->opiniarbo_model->add_value($this->aulib->getSessionId(),
       
   246                         $dbdata);
       
   247                 $this->opiniarbo_model->recalculate($this->aulib->getSessionId());
       
   248             }
       
   249         }
       
   250     }
       
   251 
       
   252     function _form_new_assertion($parent, $title)
       
   253     {
       
   254         if ($parent != NULL)
       
   255         {
       
   256             $viewdata['parent_assertion_id'] = $parent;
       
   257         }
       
   258         $viewdata['title'] = $title;
       
   259         $viewdata['assertionid'] = $this->mainassert;
       
   260         $viewdata['other_user'] = $this->userid2;
       
   261         $this->load->view('new_assertion', $viewdata);
       
   262     }
   172 }
   263 }