get_db_records(" SELECT p.person_id, p.name, p.profname, p.picture, COUNT( c.week ) asked FROM dsg_person p LEFT JOIN dsg_subject s ON p.person_id = s.person_id LEFT JOIN dsg_action a ON a.class_name = '$class' LEFT JOIN dsg_chronology c ON c.user_id = s.user_id AND c.game_id = s.game_id AND c.week = s.week AND c.game_id='{$this->game_id}' AND c.user_id='{$this->user_id}' AND a.action_id = c.action_id GROUP BY p.person_id, p.name, p.profname, p.picture ORDER BY p.person_id "); /* return $this->get_db_records("SELECT p.person_id, p.name, p.profname, p.picture, 'NA' asked FROM dsg_person p"); ---------------------------OK------------------------------ ... SELECT p.person_id, IF(COUNT(c.week) > 0, 1, 0) N, s.*, c.* FROM dsg_subject s RIGHT JOIN dsg_person p ON p.person_id = s.person_id LEFT JOIN dsg_chronology c ON s.week = c.week AND c.user_id = s.user_id AND c.game_id = s.game_id AND c.game_id = '1' AND c.user_id = '1' AND c.action_id = 'P' GROUP BY p.person_id --------------------------------------- SELECT p.person_id, p.name, p.profname, p.picture, COUNT( c.week ) asked FROM dsg_person p LEFT JOIN dsg_subject s ON p.person_id = s.person_id LEFT JOIN dsg_chronology c ON c.user_id = s.user_id AND c.game_id = s.game_id AND c.week = s.week AND c.game_id = '1' AND c.user_id = '1' AND c.action_id = 'P' GROUP BY p.person_id, p.name, p.profname, p.picture ORDER BY p.person_id -------------------------------------- SELECT p.person_id, p.name, p.profname, p.picture, COUNT( c.week ) asked FROM dsg_person p LEFT JOIN dsg_subject s ON p.person_id = s.person_id LEFT JOIN dsg_action a ON a.class_name = 'person' LEFT JOIN dsg_chronology c ON c.user_id = s.user_id AND c.game_id = s.game_id AND c.week = s.week AND c.game_id = '1' AND c.user_id = '1' AND a.action_id = c.action_id GROUP BY p.person_id, p.name, p.profname, p.picture ORDER BY p.person_id ----------------------------------------------------- */ } function set_id($id) { $sql = "SELECT p.person_id, p.name, p.profname, p.picture, 'AA' asked FROM dsg_person p WHERE person_id = '$id'"; if ($rec = $this->get_db_record($sql)) { //kui tuleb tühi rec - evalueeritakse false-na parent::set_id($id); $this->assign_vars($rec); return 1; } else return 0; //kui netist tuleb vale päring, jõuab siia } /** * Kas see on antud inimene? või kas imineme kuulub antud inimeste hulka * * @return boolean */ function is($person_id) { if (!is_array($person_id)) return ($this->id == $person_id); //on array: foreach ($person_id as $id) if ($id == $this->id) return 1; return 0; //$this->get_db_value("SELECT"); } /* * ..kas keegi kasutab seda??????? JAH ruulides * * Võibolla mitu inimest?? ei - see peaks siis olema juba Group obj. meetod * Probleem: prototüüp 2 meetod. */ function adopter($person_id = null) { if (is_null($person_id)) { $person_id = $this->id; } $scs =& $this->owner->get_scoring_system(); $p =& $scs->p($person_id); return $p->in_phase(ADOPTION); //return 1; //$this->get_db_value("SELECT"); } /** * * @access private */ function get_info() { $sql = "SELECT info FROM dsg_person WHERE person_id = '{$this->id}'"; if ($this->info = $this->get_db_value($sql)) { //kui tuleb tühi - evalueeritakse false-na return 1; } else return 0; } /** * * @access public */ function subjects_needed() { $diary =& $this->owner->get_diary(); return ($this->subject_count - count($this->subjects)); } /** */ function save($week) { $sql = "INSERT INTO dsg_chronology (user_id, game_id, week, action_id) VALUES ('{$this->user_id}', '{$this->game_id}', '$week', '{$this->action_id}') "; return $this->exec_sql($sql); } /** * kui eelmisel actionil on vabu subjekti kohti, ollakse eelmine, * kui ei ole, ollakse uus action. * * Võib juhtuda et seda kutsutakse välja korduvalt.. * teistkordsest väljakutsumisest ei tohiks midagi hullu juhtuda - * - teeb täpselt seda mida talt oodatakse, ehk laeb vajadusel * eelmised väärtused uuesti sisse. */ function set_last_action() { $diary =& $this->owner->get_diary(); $a =& $diary->get_last_action_by_type($this->action_id); // $a on ainult info saamiseks, teda ei kasutata muuks. if (isset($a)) { //eelmine action existeerib. if ($a->subjects_needed()) { //kui on vabu kohti $this->load($a->week); //teeme enda eelmiseks. return true; //olen eelmine. } } //olen uus //$this->week = $diary->get_week(); return false; } /** * * @access public */ function commit() { /* see kõik toimub juhul kui see objekt esindab ühte inimest */ if ($this->commited()) { $this->get_info(); return 1; //väljastab et kõik OK. } $failure = 0; $diary =& $this->owner->get_diary(); /* * Idee: kui eelmisel actionil on vabu subjekti kohti, ollakse eelmine, * kui ei ole, ollakse uus action. */ $is_last = $this->set_last_action(); if (!$is_last) { if ($diary->get_weeks_left() < $this->time_cost) { $this->message = 'Mängu lõpuni on vähem aega kui tegevuse sooritamiseks vaja.'; return $failure; } //$this->week = $diary->get_week(); //tehakse Action->init()is if (!$this->save($this->week)) { $this->message = 'Viga: käigu salvestamine ebaõnnestus.'; return $failure; } } //salvestame subjekti if (!$this->add_subjects($this->week, $this->id)) { $this->message = 'Viga subjektide lisamisel. käigu salvestamine ebaõnnestus.'; return $failure; } if (!$is_last) { //kui on esimene inimene 5-st, nihutame nädalat edasi if (!$diary->add_weeks($this->time_cost)) { $this->message = 'Viga: Mängu lõpuni on vähem aega kui tegevuse sooritamiseks vaja.'; return $failure; } } //avalikustame info $this->get_info(); return 1; } /** * Arv, mitu korda seda Actionit on tehtud.. * Praegu võtab 1 parameetri või mitte ühtegi * * Ühel juhul on inimene tõenäoliselt initsialiseerimata, id antakse parameetrina sisse. * arv, mitu korda tehtud ÜHE subjektiga.. * * Juhul kui param pole antud, võiks vaadata AB-st kas ta ise on tehtud (kas commit on tehtud) * Antud juhul peaks väljastama ainult kas 0 või 1 sest seda käiku võib mängu jooksul teha vaid 1 kord. * * @param mixed $with_subject Person'i identifikaator * @return arv, mitu korda tehtud (kui on 0, evalueerub false-ks - seda kasutatakse tõeväärtusena) */ function commited($with_subject = null) { $class = get_class($this); $person_id = (is_null($with_subject)) ? $this->id : $with_subject; //kui parameetrit pole antud return $this->get_db_value("SELECT COUNT(*) FROM dsg_chronology c, dsg_action a, dsg_subject s WHERE a.class_name = '$class' AND person_id='$person_id' AND c.action_id=a.action_id AND c.week=s.week AND c.user_id=s.user_id AND c.game_id=s.game_id AND c.game_id='{$this->game_id}' AND c.user_id='{$this->user_id}' "); } /** * Parameetriks kas üksikväärtus või array * vaatab kas mingi action on tehtud mitme subjektiga -> lihtsalt true või false (1/0) * * @param array $with_subjects või üksik value * @return boolean arv */ function all_commited($with_subjects) { if (!is_array($with_subjects)) $with_subjects = array($with_subjects); $class = get_class($this); $count = count($with_subjects); $arr = array(); foreach ($with_subjects as $id) $arr[] = "'$id'"; $comma_separated = implode(",", $arr); return $this->get_db_value("SELECT COUNT(*) = $count FROM dsg_chronology c, dsg_action a, dsg_subject s WHERE a.class_name = '$class' AND c.action_id=a.action_id AND c.week=s.week AND c.user_id=s.user_id AND c.game_id=s.game_id AND c.game_id='{$this->game_id}' AND c.user_id='{$this->user_id}' AND person_id IN ($comma_separated)"); } //Punktiarvestuse meetodid ^^^^^^^^^^^^^^^^^^^^^^^^^^ /* * ..kas keegi kasutab seda??????? * * Võibolla mitu inimest?? ei - see peaks siis olema juba Group obj. meetod * Probleem: prototüüp 2 meetod. function adopter($person_id = null) { if (is_null($person_id)) { $person_id = $this->id; } return 1; //$this->get_db_value("SELECT"); } */ /* * ei vajata praegu function phase() { //peaks järgima phase_areas() kehtestatud reegleid. $sql = "SELECT IF(s.points = 1, ".AWARENESS.", IF(s.points <= 1 + p.interest, ".INTEREST.", IF(s.points < 1 + p.interest + p.appraisal, ".APPRAISAL.", ".ADOPTION.") ) ) FROM dsg_states s, dsg_person p WHERE s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id = '{$this->id}' "; $phase = $this->get_db_value($sql); return $phase; } */ /** * protected */ function phase_areas() { $phases = array( 0=>array( 'phase'=>UNAFFECTED, 'beg'=>'0', 'end'=>'0' ), 1=>array( 'phase'=>AWARENESS, 'beg'=>'1', 'end'=>'1' ), 2=>array( 'phase'=>INTEREST, 'beg'=>'2', 'end'=>'1+p.interest' ), 3=>array( 'phase'=>APPRAISAL, 'beg'=>'1+p.interest+1', 'end'=>'1+p.interest+p.appraisal-1' ), 4=>array( 'phase'=>ADOPTION, 'beg'=>'1+p.interest+p.appraisal', 'end'=>'1+p.interest+p.appraisal' ) ); return $phases; } /** * protected */ function get_joint_area_by_mask($mask) { //Moodustame maskis sisalduvatest kõrvuti olevatest faasidest ühtse piirkonna. //Kui piirkond on katkendlik, jääb kehtima esimene ühtne osa. $area_beg = NULL; $area_end = NULL; foreach ($this->phase_areas() as $phase_nr => $props) { if ($props['phase'] & $mask) { if (!isset($area_beg)) { //oleme siin esimese faasiga mis maskis sisaldub $area_beg = $props['beg']; $last_existing_phase_nr = $phase_nr - 1; } if ($phase_nr == $last_existing_phase_nr + 1) { $area_end = $props['end']; } $last_existing_phase_nr = $phase_nr; } } return array(0=>$area_beg, 1=>$area_end, 'beg'=>$area_beg, 'end'=>$area_end); //et saaks kasutada list() või key järgi } /** * aread on indekseeritud faasi konstandi järgi * protected */ function get_areas_by_mask($mask) { $arr = array(); foreach ($this->phase_areas() as $phase_nr => $props) { if ($props['phase'] & $mask) { $arr[$props['phase']] = array('beg'=>$props['beg'], 'end'=>$props['end']); } } return $arr; } /* * valesti - vaata Group-ist kuidas on õige, praegu ei vajata * * @param integer * @param integer $mask Punkte lisatakse vaid maskis mainitud lahtritesse!! * kui punkte on liiga palju, pannakse ainult osad, et lahter täis saaks. function add_points($nr = 1, $mask = NULL) { if ($nr < 1) return; 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.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id = '{$this->id}' "; //echo '{',$sql, '}'; $this->exec_sql($sql); }else{ //$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 <= 1+p.interest+p.appraisal, s.points+$nr, 1+p.interest+p.appraisal) WHERE s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id = '{$this->id}' "); } } */ /* * valesti - vaata Group-ist kuidas on õige, praegu ei vajata * ok function add_full_points() { $this->exec_sql(" UPDATE dsg_states s, dsg_person p SET s.points = 1+p.interest+p.appraisal WHERE s.game_id='{$this->game_id}' AND s.user_id='{$this->user_id}' AND s.person_id='{$this->id}' "); } */ //Punktiarvestuse meetodid vvvvvvvvvvvvvvvvvvvvvvvvvvv function get_related_info() { $game =& $this->owner; $relations =& new InfoNode($game); $r = $game->get_action('R'); $rows = $r->get_open_items(); foreach ($rows as $row) { $row['group_relation_id']; } $activities =& new InfoNode($game); $ri =& new InfoNode($game); $ri->add_node($relations); $ri->add_node($activities); return $ri; } } ?>