controllers/opiniarbo.php
author viric@mandarina
Sun, 28 Dec 2008 20:48:10 +0000
changeset 5 cb13c07e7e5d
parent 3 f3e55c2386a1
child 6 8fc19b7bd2dc
permissions -rw-r--r--
Made multilanguage. Catalan and Russian.

<?php

class Opiniarbo extends Controller {

    private $mainassert;
    private $userid;
    private $userid2;

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

	function index()
    {
        // NEW INDEX
        // Show users
        $this->mainassert = $this->uri->segment(3, 0);
        $this->userid = $this->aulib->getSessionId();
        $this->userid2 = $this->uri->segment(4, 0);

        // 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, lang("headassertions_str"));
            $this->_form_new_assertion(NULL, lang("newheadassertion_str"));
        }
        else
        {
            $this->_process_new_assertion($this->mainassert);
            $this->_show_preassertions($this->mainassert);
            $this->_show_assertion($this->mainassert);
            $this->_show_assertions($this->mainassert, lang("subassertions_str"));
            $this->_form_new_assertion($this->mainassert, lang("newsubassertion_str"));
        }
        $this->load->view('page_bottom');
    }

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

    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' => lang('noone_str'))),
            $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'] = lang("mainassertion_str");
        $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['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;
            }
            $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', 'callback__influence_check');

            // 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');
                if ($this->input->post('context'))
                    $dbdata['context'] = $this->input->post('context');
                if ($this->mainassert != 0)
                    $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', 'callback__influence_check');

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