models/opiniarbo.php
changeset 0 3773f4677c40
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/models/opiniarbo.php	Thu Dec 25 11:25:58 2008 +0000
@@ -0,0 +1,85 @@
+<?php
+class Opiniarbo_model extends Model {
+
+    function Opiniarbo_model()
+    {
+        parent::Model();
+    }
+
+    function get_my_heads($user)
+    {
+        $query = $this->db->get_where('asertoj',
+            array('user' => $user, 'parent' => NULL));
+        return $query->result();
+    }
+
+    function get_other_heads($user)
+    {
+        $query = $this->db->get_where('asertoj',
+            array('user !=' => $user, 'parent' => NULL));
+        return $query->result();
+    }
+
+    function get_my_subasserts($parent, $user)
+    {
+        $query = $this->db->get_where('asertoj',
+            array('user' => $user,
+                  'parent' => $parent));
+        return $query->result();
+    }
+
+    function get_other_subasserts($parent, $user)
+    {
+        $query = $this->db->get_where('asertoj',
+            array('user !=' => $user,
+                  'parent' => $parent));
+        return $query->result();
+    }
+
+    function get_calc_value($assert, $orig_value)
+    {
+        $query = $this->db->get_where('asertoj',
+            array('parent' => $assert));
+
+        if ($query->num_rows() > 0)
+        {
+            $val = 0;
+            foreach($query->result() as $row)
+            {
+                $val += get_calc_value($row->id, $row->value);
+            }
+            $query2 = $this->db->where('id', $row->id);
+            $query2->update('asertoj', array(
+                'calc_value' => $val));
+
+            return $val;
+        }
+
+        return $orig_value;
+    }
+
+    function recalculate()
+    {
+        // Get all heads
+        get_calc_value(NULL, 0);
+    }
+
+    function insert_assertion()
+    {
+        if (isset($_POST['parent']))
+        {
+            $data['parent'] = $_POST['parent'];
+            $data['influence'] = $_POST['influence'];
+        }
+        else
+        {
+            $data['parent'] = NULL;
+            $data['influence'] = $_POST['influence'];
+        }
+        $data['assert'] = $_POST['assert'];
+        $data['context'] = $_POST['context'];
+        $data['value'] = float($_POST['value']);
+    }
+}
+
+?>