controllers/opiniarbo.php
author viric@mandarina
Fri, 26 Dec 2008 01:01:11 +0000
changeset 2 7c6c888b2fed
parent 1 f08a11b5d207
child 3 f3e55c2386a1
permissions -rw-r--r--
He afegit opinions per usuari.

<?php

class Opiniarbo extends Controller {

	function Opiniarbo()
    {
        parent::Controller();
		$this->load->library('form_validation');
        $this->load->library('aulib');
		$this->load->library('parser');
		$this->load->helper('form');
		$this->load->model('opiniarbo_model');
    }

	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');

                // 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);
        }
        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);
        }
    }

    function recalculate()
    {
        $this->opiniarbo_model->recalculate();
        echo "OK";
    }
    
    function _value_check($str)
    {
        if (strlen($str) > 0 &&  $str <= 10.0 && $str >= -10.0)
            return TRUE;
        $this->form_validation->set_message("_value_check", "Skribu valoron inter -10 kaj 10");
        return FALSE;
    }

    function _influence_check($str)
    {
        if (strlen($str) > 0 &&  $str <= 10.0 && $str >= -10.0)
            return TRUE;
        $this->form_validation->set_message("_influence_check", "Skribu influon inter -10 kaj 10");
        return FALSE;
    }
}