'A','B'=>'B' var $leader; //var $represents_all; function init() { parent::init(); $this->persons = array(); //$this->represents_all = FALSE; } function save_subjects($week) { $this->load($week); if (empty($this->persons)) { echo ''; } } function load_subjects($week) { $sql = "SELECT person_id FROM dsg_subject WHERE week = '$week' AND user_id = '{$this->user_id}' AND game_id = '{$this->game_id}' "; $this->persons = $this->get_db_vertical_values($sql); return $this->persons; } function add_group($group) { foreach ($group->items() as $id) $this->persons[$id] = $id; //tagab unikaalsuse } function remove_group($group) { foreach ($group->items() as $id) unset($this->persons[$id]); } function set_ids($ids) { $this->persons = array(); foreach ($ids as $id) $this->persons[$id] = $id; //tagab unikaalsuse } function add_ids($ids) { //var_dump($ids); foreach ($ids as $id) { //var_dump($id); $this->persons[$id] = $id; //tagab unikaalsuse } } function remove_ids($ids) { foreach ($ids as $id) unset($this->persons[$id]); } function add_id($id) { $this->persons[$id] = $id; //tagab unikaalsuse } function add_person($person) { $this->persons[$person->id()] = $person->id(); //tagab unikaalsuse } function remove_id($id) { unset($this->persons[$id]); } function remove_person($person) { unset($this->persons[$person->id()]); } /** * vajalik */ function set() { foreach (func_get_args() as $arg) { if (is_scalar($arg)) $this->add_id($arg); elseif (is_array($arg)) $this->add_ids($arg); elseif (is_a($arg, get_class($this))) $this->add_group($arg); elseif (is_a($arg, 'person')) $this->add_person($arg); } } /* function set_all($param = TRUE) { if ($param) $this->represents_all = TRUE; else $this->represents_all = FALSE; } */ function set_all() { $this->add_ids($this->get_db_vertical_values("SELECT person_id FROM dsg_person")); } /** * ei tea kas on tarvis */ function items() { return $this->persons; } /* * ei tea kas on tarvis!!! praegu küll ei ole function get_items() { $ids = $this->items(); $persons = array(); foreach ($ids as $id) { $prsn =& new Person($this->owner); $prsn->set_id($id); $persons[] = $prsn; } return $persons; } */ /** * vajalik */ function size() { return count($this->persons); } /** * Group :: comma_separated() * * Mõeldud kasutamiseks SQL-s, kui objekti array on tühi väljastab 'NULL' (IN (NULL) on ok sql-s) * * @return string */ function comma_separated() { if (empty($this->persons)) return 'NULL'; //et SQL töötaks $arr = array(); foreach ($this->persons as $id) $arr[] = "'$id'"; return implode(",", $arr); } /** * Ei tea kui mõttekas see on * * @return Person function get($person_id) { } */ /** * vajalik //praegu mitte * Kas kõik $this grupi liikmed on ka parameetrina antud grupis? */ function in_group($group) { $others = $group->items(); foreach ($this->persons as $person_id) if (!array_key_exists($person_id, $others)) return FALSE; return TRUE; } /** * vajalik * Selle ja teise hulga ühisosa */ function &intersection($group) { $arr = array_intersect_assoc($this->items(), $group->items()); $gr =& new Group($this->owner); $gr->set_ids($arr); return $gr; } function get_areas_condition($mask) { foreach ($this->get_areas_by_mask($mask) as $phase_nr => $area) { $cond = "(s.points BETWEEN {$area['beg']} AND {$area['end']})"; if ($phase_nr == ADOPTION) $cond = "($cond and p.appraisal > 0)"; $arr[] = $cond; } $areas_disjunction = implode(" or ", $arr); //$condition = "(p.appraisal) and ($areas_disjunction)"; return $areas_disjunction; } /** * vajalik * Kas kõik $this grupi liikmed on antud faasi(de)s? * Mida see teeks initsialiseerimata obj puhul? Mitte midagi - lihtsalt tühi grupp ju. * Erinevalt g-st mille puhul initsialiseerimatus tähendab obj-i puudumist, siin on aga * tegemist tühja arrayga. */ function in_phase($mask) { if (!$this->size()) return FALSE; /* $arr = array(); foreach ($this->phase_areas() as $phase_nr => $props) { if ($props['phase'] & $mask) { $area_beg = $props['beg']; $area_end = $props['end']; $arr[] = "(s.points BETWEEN $area_beg AND $area_end)"; } } */ $condition = $this->get_areas_condition($mask); $sql = "SELECT IF($condition, 1, 0) FROM dsg_states s, dsg_person p WHERE s.person_id = p.person_id AND s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id IN (".$this->comma_separated().") "; $val = $this->get_db_value($sql); return $val; } /** * vajalik */ function add_points($nr = 1, $mask = NULL) { if (isset($mask)) { $sql = "UPDATE dsg_states s, dsg_person p SET s.points = "; list($area_beg, $area_end) = $this->get_joint_area_by_mask($mask); $first = 's.points+1'; $last = "s.points+$nr"; $points = "IF($first BETWEEN $area_beg AND $area_end, IF($area_end < $last, $area_end, $last), s.points)"; $sql .= $points; //echo '{',$sql, '}'; $sql .= " WHERE s.person_id = p.person_id AND s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id IN (".$this->comma_separated().") "; //echo '{',$sql, '}'; $this->exec_sql($sql); }else{ list($adopt_beg, $adopt_end) = $this->get_joint_area_by_mask(ADOPTION); //$adopt_end = $area['end']; //$this->exec_sql("UPDATE dsg_states SET points = points + $nr "); $this->exec_sql(" UPDATE dsg_states s, dsg_person p SET s.points = IF(s.points+$nr <= $adopt_end, s.points+$nr, $adopt_end) WHERE s.person_id = p.person_id AND s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id IN (".$this->comma_separated().") "); } } /** * vajalik */ function add_full_points() { list($adopt_beg, $adopt_end) = $this->get_joint_area_by_mask(ADOPTION); $this->exec_sql(" UPDATE dsg_states s, dsg_person p SET s.points = $adopt_end WHERE s.person_id = p.person_id AND s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id IN (".$this->comma_separated().") "); } /** * Group::get_leader() * * @return */ function &get_leader() { $prsn =& new Person($this->owner); $prsn->set_id($this->leader); return $prsn; } /** * Peab igal juhul väljastama obj-i */ function &get_by_mask($mask) { $condition = $this->get_areas_condition($mask); $sql = "SELECT p.person_id FROM dsg_states s, dsg_person p WHERE s.person_id = p.person_id AND s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND ($condition) ". //($this->represents_all ? '' : " AND s.person_id IN (".$this->comma_separated().") "); " AND p.person_id IN (".$this->comma_separated().") "; $grp =& new Group($this->owner); $grp->add_ids($this->get_db_vertical_values($sql)); return $grp; } /** * ////Väljastab kas Person (1) või Group (rohkem) * Väljastab alati Group (1 või rohkem liiget) * Kui selle grupi seas on vähem persoone kui nõutud, antakse nii palju kui on...? ehk siis kõik? * sest üks liige korduda ei saa, aga kas ei või juhtuda, et mingi tõenäosusega jääb keegi ikkagi välja: * teostan valikut 3x 3-e seast (a,b,c). 1)a 2)c 3)a => jääb (a,c). Seda tuleks vältida. */ function &get_random($nr = 1) { if (count($this->persons) < $nr) { $nr = count($this->persons); } $grp =& new Group($this->owner); $id_s = array_rand($this->persons, $nr); if ($nr > 1) $grp->add_ids($id_s); else $grp->add_id($id_s); return $grp; } } ?>