[virtualcommons-svn] SF.net SVN: virtualcommons:[39] vcweb/trunk/virtualcommons/experiment
Status: Beta
Brought to you by:
alllee
From: <al...@us...> - 2008-08-13 09:32:26
|
Revision: 39 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=39&view=rev Author: alllee Date: 2008-08-13 09:32:34 +0000 (Wed, 13 Aug 2008) Log Message: ----------- refactoring forestry_v2 module and experiment.inc classes heavily. still need to fix the generate_results_table and getAllStageData() methods. Modified Paths: -------------- vcweb/trunk/virtualcommons/experiment/experiment.inc vcweb/trunk/virtualcommons/experiment/games/forestry_v2/forestry_v2.module vcweb/trunk/virtualcommons/experiment/student_login/student_login.module vcweb/trunk/virtualcommons/experiment/teacher_dashboard/teacher_dashboard.module Modified: vcweb/trunk/virtualcommons/experiment/experiment.inc =================================================================== --- vcweb/trunk/virtualcommons/experiment/experiment.inc 2008-08-11 23:16:36 UTC (rev 38) +++ vcweb/trunk/virtualcommons/experiment/experiment.inc 2008-08-13 09:32:34 UTC (rev 39) @@ -18,7 +18,16 @@ } } +class TableData { + public $header; + public $rows; + public function __construct($header, $rows) { + $this->header = $header; + $this->rows = $rows; + } +} + class Game { private $game_id; private $teacher_id; @@ -48,6 +57,18 @@ return $this->no_of_students; } + public function getAllStageIds() { + $result = db_query('SELECT s.id FROM vc_stage s + INNER JOIN vc_experiment e ON s.vc_experiment_id=e.id + INNER JOIN vc_game g ON e.id=g.vc_experiment_id + WHERE g.id=%d ORDER BY s.stage_no', $this->game_id); + $stage_ids = array(); + while ($fetch_object = db_fetch_object($result)) { + $stage_ids[] = $fetch_object->id; + } + return $stage_ids; + } + // FIXME: remove current_stage_no from this session object since it is // unsafe if the browser exits public function getStageId($current_stage_no) { @@ -206,23 +227,23 @@ public function isFinished() { $max_stage_number = db_result(db_query('SELECT max(stage_no) - FROM vc_stage s inner join vc_game g on s.vc_experiment_id=g.vc_experiment_id - WHERE g.id=%d', $this->game_id)); + FROM vc_stage s inner join vc_game g on s.vc_experiment_id=g.vc_experiment_id + WHERE g.id=%d', $this->game_id)); return $this->isStageOver() && ($max_stage_number == $this->getCurrentStageNumber()); } public function isStageOver() { - drupal_set_message(t('Current round no is: ' .$this->getCurrentRoundNo() . ' and no of rounds are: ' .$this->getNumberOfRounds())); - return ($this->getCurrentRoundNo() - 1 == $this->getNumberOfRounds()); + //drupal_set_message(t('Current round no is: ' .$this->getCurrentRoundNo() . ' and no of rounds are: ' .$this->getNumberOfRounds())); + return ($this->getCurrentRoundNo() > $this->getNumberOfRounds()); } public function getTurnSequence($group_id) { return db_result(db_query('select turn_sequence - FROM vc_forestry_turn_sequence s, vc_group gr, vc_game ga - WHERE s.vc_group_id=%d AND ga.id=gr.vc_game_id AND ga.current_round_no=s.round_no AND s.vc_rules_id=gr.vc_selected_rules_id', - $group_id)); + FROM vc_forestry_turn_sequence s, vc_group gr, vc_game ga + WHERE s.vc_group_id=%d AND ga.id=gr.vc_game_id AND ga.current_round_no=s.round_no AND s.vc_rules_id=gr.vc_selected_rules_id', + $group_id)); } public function getValidStudentIds($group_id) { @@ -236,7 +257,7 @@ $valid_ids[] = $data->id; drupal_set_message(t($data->id. ' , ')); } - + return $valid_ids; } @@ -245,9 +266,9 @@ $result = db_query('SELECT fs.id, fs.harvest_decision, fs.total_points FROM vc_forestry_student fs INNER JOIN vc_student s ON fs.vc_student_id=s.id WHERE fs.harvest_decision > 0 - AND s.vc_group_id=%d - AND fs.round_no=%d - AND s.turn_no NOT IN (%s)', $group_id, $this->getCurrentRoundNo(), $turn_sequence); + AND s.vc_group_id=%d + AND fs.round_no=%d + AND s.turn_no NOT IN (%s)', $group_id, $this->getCurrentRoundNo(), $turn_sequence); while ($data = db_fetch_object($result)) { $fine = $data->harvest_decision + 3; $dice = 6; @@ -289,17 +310,7 @@ // FIXME: preprocess the groups and insert default decisions for // all students that haven't made a harvest decision. $group_id=$data->group_id; - $current_rule_number = $this->getCurrentRuleNumber($group_id); - if ($voting_enabled) { - // roll the dice - $dice = rand(1, 6); - $dice = 6; - if ($dice == 6) { - // check to see if we need to assess fines - $this->assessFines($group_id, $current_rule_number); - - } - } + $resources_before=$crp=$data->crp; $max_resources=$data->max_r; $total_harvest_decision = db_result(db_query("select sum(harvest_decision) @@ -313,9 +324,32 @@ } //insert into group-round info - db_query("Insert into {vc_forestry_group_round_info} (round_no,vc_game_id,vc_group_id,vc_stage_id,total_harvest_decision,resources_before,resources_after) - values(%d,%d,%d,%d,%d,%d,%d)", - $round_no,$this->game_id,$group_id,$stage_id,$total_harvest_decision,$resources_before,$resources_available); + $current_rule_number = $this->getCurrentRuleNumber($group_id); + if ($voting_enabled) + { + // roll the dice + $dice = rand(1, 6); + // $dice = 6; + if ($dice == 6) + { + // check to see if we need to assess fines + $this->assessFines($group_id, $current_rule_number); + + } + $selected_rule_id=db_result(db_query("select vc_selected_rules_id from vc_group where id=%d",$group_id)); + // db_query("insert into {vc_forestry_group_round_info}(vc_rules_id,dice) values (%d,%d) where vc_game_id=%d and vc_stage_id=%d and vc_group_id=%d",$selected_rule_id,$dice,$this->game_id,$stage_id,$group_id); + $this->initRules(); + db_query("Insert into {vc_forestry_group_round_info} (round_no,vc_game_id,vc_group_id,vc_stage_id,total_harvest_decision,resources_before,resources_after,vc_rules_id,dice) + values(%d,%d,%d,%d,%d,%d,%d,%d,%d)", + $round_no,$this->game_id,$group_id,$stage_id,$total_harvest_decision,$resources_before,$resources_available,$selected_rule_id,$dice); + + } + else + { + db_query("Insert into {vc_forestry_group_round_info} (round_no,vc_game_id,vc_group_id,vc_stage_id,total_harvest_decision,resources_before,resources_after) + values(%d,%d,%d,%d,%d,%d,%d)", + $round_no,$this->game_id,$group_id,$stage_id,$total_harvest_decision,$resources_before,$resources_available); + } //update forestry common pool db_query("update {vc_forestry_common_resource_pool} set common_resource_pool=%d where vc_game_id=%d and vc_group_id=%d",$resources_available,$this->game_id,$group_id); } @@ -348,14 +382,19 @@ $this->current_round_no = $round_no; }*/ db_query("UPDATE {vc_game} SET current_round_no=%d WHERE id=%d", $this->current_round_no, $this->game_id); + if ($voting_enabled) { + //initialise the rule settings for next round.If game is finished don't initialise anything + if(!($this->isFinished())) + $this->initRules(); + } /*if($this->isStageOver()) - $this->setRoundNo($this->getCurrentRoundNo());*/ + $this->setRoundNo($this->getCurrentRoundNo());*/ } public function setRoundNo($round_no) { - //drupal_set_mesage(t('Round is : ' . $round_no)); + //drupal_set_mesage(t('Round is : ' . $round_no)); if ($round_no > ($this->getNumberOfRounds())) { // reset round number to 1 $this->current_round_no = 1; @@ -388,19 +427,21 @@ { //reset the forestry common resource pool + $result_resource=db_query("select max_resources,vc_group_id from vc_forestry_common_resource_pool where vc_game_id=%d",$this->game_id); while($data=db_fetch_object($result_resource)) { - db_query("update vc_forestry_common_resource_pool set common_resource_pool=%d where vc_group_id=%d and vc_game_id=%d",$data->max_resources,$data->vc_group_id,$this>game_id); - + db_query("update {vc_forestry_common_resource_pool} set common_resource_pool=%d where vc_group_id=%d and vc_game_id=%d",$data->max_resources,$data->vc_group_id,$this>game_id); + drupal_set_message("vc_forestry_common_resource_pool si updated for group id, max resources: " . $data->vc_group_id . ', ' . $data->max_resources . ', ' . $this->game_id ); } + //determine the rule for a group - - $result_group=db_query("select id from vc_group where vc_game_id=%d",$this->game_id); - while($data=db_fetch_object($result_group)) - { + + $result_group=db_query("select id from vc_group where vc_game_id=%d",$this->game_id); + while($data=db_fetch_object($result_group)) + { $group_id=$data->id; //$result_voting=db_query("select vc_rules_id,count(vc_rules_id) as rule_cnt from vc_student_voting where vc_student_id in (select id from vc_student where vc_group_id=%d) group by vc_rules_id",$group_id); $result_voting=db_query(" select vc_rules_id, count(vc_rules_id) as rule_cnt from vc_student_voting v @@ -428,7 +469,7 @@ { $max_rule_cnt=$rule_cnt[$j]; $max_rule_id=$rule_id[$j]; - drupal_set_message(t( '$max_rule_id is : ' . $rule_id[$j])); + drupal_set_message(t( '$max_rule_id is : ' . $rule_id[$j])); } } else @@ -439,13 +480,13 @@ //echo '<br/>Equal $i ' . $i. ' $j ' .$j; array_push($tie_rules,$rule_id[$i],$rule_id[$j]); $tie_rule_cnt=$rule_cnt[$i]; - drupal_set_message(t('rule cnt i: '. $tie_rules[$i] . 'rule cnt j: '.$tie_rules[$j])); + drupal_set_message(t('rule cnt i: '. $tie_rules[$i] . 'rule cnt j: '.$tie_rules[$j])); } else if($max < $rule_cnt[$i]) { $max_rule_cnt=$rule_cnt[$i]; $max_rule_id=$rule_id[$i]; - drupal_set_message(t( '$max_rule_id is : ' . $rule_id[$i])); + drupal_set_message(t( '$max_rule_id is : ' . $rule_id[$i])); } } @@ -469,8 +510,8 @@ // drupal_set_message(t('Rule id : ' . $final_rule_id.'Group id is: ' . $group_id)); }//end of while - + }//end of init function // several ways to implement this - @@ -478,6 +519,7 @@ // 2. use shuffle and then take an array_slice from 0 to $how_many private function generateTurnSequence($how_many, $no_of_students) { $students = range(1,$no_of_students); + drupal_set_message("no of students need to be selected for this round are :" . $how_many); $keys = array_rand( range(1, $no_of_students), $how_many ); $turn_sequence = array(); foreach ($keys as $key) { @@ -546,11 +588,16 @@ private $group_no; private $no_of_students; private $experiment_id; + private $student; public function getNumberOfStudents() { return $this->no_of_students; } + public function getStudent() { + return $this->student; + } + public function getGroupId() { return $this->group_id; } @@ -569,7 +616,22 @@ } } + public function generate_harvest_table() { + $number_of_players = $this->no_of_students; + $result = db_query("SELECT min_resource_level,max_resource_level,individual_harvest_level from {vc_forestry_${number_of_players}player_harvest_table}"); + while($harvest_data=db_fetch_object($result)) + { + $min=$harvest_data->min_resource_level; + $max=$harvest_data->max_resource_level; + $ind=$harvest_data->individual_harvest_level; + $data[]=array($min.' - '.$max,$ind); + } + return new TableData(array('Resource Level', 'Maximum Harvest Decision'), $data); + } + + + public function getTurnSequence() { return $this->game->getTurnSequence($this->group_id); } @@ -599,8 +661,8 @@ $group->game = new Game($group->game_id); $group->group_id = $fetch_object->group_id; $group->no_of_students = db_result(db_query("SELECT count(*) FROM vc_student WHERE vc_group_id=%d", $group->group_id)); - drupal_set_message( t('generating a new game for group with game_id: '.$group->game->getGameId() . ':'.$group->group_id) ); - return $group; + $group->student = new ForestryStudent($studentId, $group); + return $group->student; } return false; } @@ -673,42 +735,50 @@ } //drupal_set_message(t('Rule no : ' . $final_rule_no[0].' ' .$final_rule_no[1]. 'Group id is: ' . $this->group_id)); return $final_rule_no; + } + public function getResourcesAvailable() { + return db_result(db_query('SELECT common_resource_pool + FROM vc_forestry_common_resource_pool + WHERE vc_group_id=%d', $this->group_id)); } -public function isGameFinished() -{ -return $this->game->isFinished(); + public function getMaximumResourcesAvailable() { + return db_result(db_query('SELECT max_resources + FROM vc_forestry_common_resource_pool + WHERE vc_group_id=%d', $this->group_id)); + } -} + public function isGameFinished() + { + return $this->game->isFinished(); + } -public function isStageOver() -{ -drupal_set_message(t('$this->game->getCurrentRoundNo(): ' . $this->game->getCurrentRoundNo() . ' $this->game->getNumberOfRounds()'. $this->game->getNumberOfRounds())); - return ($this->game->getCurrentRoundNo() -1 == $this->game->getNumberOfRounds()); -} -public function getRoundNo() -{ + public function getRoundNo() + { return $this->game->getCurrentRoundNo(); -} + } /*public function getRuleDescription() { db_result(db_query("select description select vc_selected_rules_id from vc_group where id=%d",$this->group_id)); }*/ -public function isTurn($stud_id) + public function isTurn($stud_id) { - $valid_id=$this->game->getValidStudentIds($this->group_id); - $flag=FALSE; - drupal_set_message(t('Group id is : '. $this->group_id. 'Student Id is : '. $stud_id . ' Valid student ids are: ' . $valid_id[0] . ',' . $valid_id[1])); - if(in_array($stud_id,$valid_id)) - { - $flag= TRUE; - } - else - $flag= FALSE; - drupal_set_message(t('flag for student turn : '. $flag)); - return $flag; + $flag=FALSE; + if(!($this->isGameFinished())) + { + $valid_id=$this->game->getValidStudentIds($this->group_id); + drupal_set_message(t('Group id is : '. $this->group_id. 'Student Id is : '. $stud_id . ' Valid student ids are: ' . $valid_id[0] . ',' . $valid_id[1])); + if(in_array($stud_id,$valid_id)) + { + $flag= TRUE; + } + } + else + $flag= FALSE; + drupal_set_message(t('flag for student turn : '. $flag)); + return $flag; } }//end of Group @@ -718,12 +788,14 @@ */ class ForestryStudent { - private $round_no; private $stud_id; - private $stage_id; - private $harvest_decision; private $turn_no; + private $group; + public function __construct($student_id, $group) { + $this->stud_id = $student_id; + $this->group = $group; + } public function getLatestResource($group_id,$game_id) { @@ -735,33 +807,70 @@ return $this->turn_no; } - public function setStudentId($stud_id) - { - $this->stud_id=$stud_id; - } - public function setStageId($stage_id) - { - $this->stage_id=$stage_id; + public function getGroup() { + return $this->group; } - - /* public function getRoundNo($game_id,$stage_id) - { - //$this-> - return db_result(db_query("select current_round_no from vc_game where id=%d and vc_stage_id=%d",$game_id,$stage_id)); - //drupal_set_message(t('round no is:' .$stage_id . $this->round_no . $game_id)); -// return $this->round_no; + /* public function getRoundNo($game_id,$stage_id) + { + //$this-> + return db_result(db_query("select current_round_no from vc_game where id=%d and vc_stage_id=%d",$game_id,$stage_id)); + + //drupal_set_message(t('round no is:' .$stage_id . $this->round_no . $game_id)); + // return $this->round_no; }*/ + public function getAllStageData() { + $game = $this->group->getGame(); + $stage_ids = $game->getAllStageIds(); + $all_data = array(); + foreach ($stage_ids as $stage_id) { + $all_data[] = $this->getStageData($stage_id); + } + return $all_data; + } + + public function getStageData($stage_id) { + $student_decision = db_query("select round_no,harvest_decision,fine,total_points from vc_forestry_student where vc_student_id='%s' and vc_stage_id=%d order by round_no",$this->stud_id,$stage_id); + $decision_table = array(); + $voting_enabled=db_result(db_query("select voting from vc_stage where id=%d",$stage_id)); + + while($student_data=db_fetch_object($student_decision)) + { + $round_no=$student_data->round_no; + $harvest_decision=$student_data->harvest_decision; + $fine=$student_data->fine; + $total_points=$student_data->total_points; + //get the resource info from vc_forestry_group_round_information + $fetch_object = db_fetch_object(db_query("select resources_before,resources_after from vc_forestry_group_round_info where vc_group_id=%d and vc_game_id=%d and round_no=%d and vc_stage_id=%d", + $this->group->getGroupId(),$this->group->getGame()->getGameId(),$round_no,$stage_id)); + if ($fetch_object) { + $resources_before=$fetch_object->resources_before; + $resources_after=$fetch_object->resources_after; + } + $round_header = array('Round Number', 'Harvest Decision', 'Resources Before', 'Resources After'); + $round_data = array($round_no,$harvest_decision,$resources_before,$resources_after); + if($voting_enabled) { + array_push($round_data, $fine, $total_points); + array_push($round_header, 'Penalty', 'Total Points'); + } + $decision_table[] = new TableData($round_header, $round_data); + } + return $decision_table; + } + + /* public function getForestryStudentInfo($group_id,$game_id,$stage_id) { - $student_decision=db_query("select round_no,harvest_decision from vc_forestry_student where vc_student_id= '%s' and vc_stage_id=%d ",$this->stud_id,$stage_id); + $student_decision=db_query("select round_no,harvest_decision,fine,total_points from vc_forestry_student where vc_student_id= '%s' and vc_stage_id=%d ",$this->stud_id,$stage_id); while($student_data=db_fetch_object($student_decision)) { $round_no=$student_data->round_no; $harvest_decision=$student_data->harvest_decision; - $round_no=(int)$round_no; + $round_no=(int)$round_no; + $fine=$student_data->fine; + $total_points=$student_data->total_points; //get the resource info from vc_forestry_group_round_information $resource_info= db_query("select resources_before,resources_after from vc_forestry_group_round_info where vc_group_id=%d and vc_game_id=%d and round_no=%d and vc_stage_id=%d",$group_id,$game_id,$round_no,$stage_id); while($data_resource=db_fetch_object($resource_info)) @@ -769,23 +878,39 @@ $resources_before=$data_resource->resources_before; $resources_after=$data_resource->resources_after; } - $final_row[]=array($round_no,$harvest_decision,$resources_before,$resources_after); + + $flag=db_result(db_query("select voting from vc_stage where id=%d",$stage_id)); + drupal_set_message(t('flag for voting is: '. $flag)); + drupal_set_message(t('fine is: '. $fine . ' and total_points are: '.$total_points)); + if($flag) + $final_row[]=array($round_no,$harvest_decision,$resources_before,$resources_after,$fine,$total_points); + else + $final_row[]=array($round_no,$harvest_decision,$resources_before,$resources_after); + //array_push($final_row,$fine,$total_points); } $this->round_no=$round_no; + /* foreach($final_row as $key=>$value) + { + foreach($value as $key1=>$value1) + + drupal_set_message(t('data for the prev round is: '. $value1)); + } + return $final_row; - } + */ - public function saveForestryStudentInfo($round_no,$stud_harvest_decision) + public function save($round_no, $harvest_decision) { + $stage_id = $this->getGroup()->getGame()->getCurrentStageId(); $total = db_result(db_query("SELECT sum(harvest_decision) FROM {vc_forestry_student} WHERE vc_student_id='%s' - AND vc_stage_id=%d", - $this->stud_id, - $this->stage_id)); - db_query("Insert into {vc_forestry_student} (round_no,vc_student_id,vc_stage_id,harvest_decision, total_points) values(%d,'%s',%d,%d, %d)", - $round_no,$this->stud_id,$this->stage_id,$stud_harvest_decision, $total); + AND vc_stage_id=%d", + $this->stud_id, + $stage_id)); + db_query("INSERT INTO {vc_forestry_student} (round_no,vc_student_id,vc_stage_id,harvest_decision, total_points) VALUES (%d,'%s',%d,%d, %d)", + $round_no,$this->stud_id,$stage_id,$harvest_decision, $total); } public function updateStartFlag($flag) @@ -793,17 +918,19 @@ db_query("update vc_student set start_flag=%d where id ='%s' ",$flag,$this->stud_id); } - public function getStartFlag() + public function canStart() { return db_result(db_query("select start_flag from vc_student where id ='%s' ",$this->stud_id)); } + /* XXX: unsafe operation, should only be set by the facilitator (teacher) public function updateStageId($stage_no,$experiment_id,$game_id) { db_query("update vc_game set vc_stage_id=(select id from vc_stage where stage_no= %d and vc_experiment_id=%d) where id=%d;",$stage_no,$experiment_id,$game_id); } - + */ + } class Rules @@ -811,7 +938,7 @@ public function getRules($stage_id) { - drupal_set_message(t('current stage is : ' . $stage_id)); + drupal_set_message(t('current stage is : ' . $stage_id)); $result=db_query("Select rule_no,description from vc_rules where vc_stage_id=%d",$stage_id ); while($data=db_fetch_object($result)) { Modified: vcweb/trunk/virtualcommons/experiment/games/forestry_v2/forestry_v2.module =================================================================== --- vcweb/trunk/virtualcommons/experiment/games/forestry_v2/forestry_v2.module 2008-08-11 23:16:36 UTC (rev 38) +++ vcweb/trunk/virtualcommons/experiment/games/forestry_v2/forestry_v2.module 2008-08-13 09:32:34 UTC (rev 39) @@ -15,29 +15,20 @@ function forestry_v2_menu($may_cache) { $items = array(); if ($may_cache) { - $items[] = array('path' => 'forestry', + $items[] = array('path' => 'forestry/intro', 'title' => t('Forestry Game'), - 'callback' => 'forestry_v2_page', + 'callback' => 'forestry_intro', 'type'=>MENU_CALLBACK, 'access' => TRUE//user_access('Only Teachers') ); - - //forestry stage 1 menu - $items[]=array( 'path' => 'forestry_stage1', - 'title' => t('Forestry Game - Stage 1'), - 'callback' => 'forestry_game_stage1', + $items[]=array( 'path' => 'forestry/play', + 'title' => t('Forestry Game - Round Active'), + 'callback' => 'forestry_play', //'callback arguments'=>array(forestry_game_stage1_multiform), 'type'=>MENU_CALLBACK, 'access' => TRUE//user_access('Only Teachers') ); - $items[]=array( 'path' => 'forestry_stage1_part2', - 'title' => t('Forestry Game - Stage 1'), - 'callback'=>'forestry_game_stage1_part2', - // 'callback argument' => array('forestry_game_stage1_part2'), - 'type'=>MENU_CALLBACK, - 'access' => TRUE//user_access('Only Teachers') - ); } else @@ -59,247 +50,138 @@ } /** - * Called when user goes to example.com/?q=forestry + * */ -function forestry_v2_page() { - $output = t('Please note the following maximum harvest table.'); +function forestry_intro() { + $output = t('Welcome to the Forestry game! (Add more instructions here?). The following table lists the maximum harvest decisions you can make associated with a given resource level range.'); // Return the HTML generated from the $form data structure. - $output .= drupal_get_form('forestry_v2_nameform'); + $output .= generate_harvest_table(); + $output .= drupal_get_form('forestry_intro_form'); return $output; } + /** - * Defines a form. + * Returns the initial harvest table. */ -function forestry_v2_nameform() { - //$form['#method']='post'; - //+$form['#action']='http://localhost/drupal/?q=studentgroup'; - $form['#id']='forestry_v2'; - $header = array('Current Resource Level', 'Individual Maximum Harvest Level'); - - $stud_id=$_SESSION['student_id']; - $group = Session::get('group'); - drupal_set_message(' group id: ' . $group->getGroupId() ); - // FIXME: merge harvest tables into single table - - $game_id=$group->getGameId(); - $group_id=$group->getGroupId(); - $result_group=$group->getNumberOfStudents(); - drupal_set_message(t('Group no'. $result_group)); - $result = db_query("SELECT min_resource_level,max_resource_level,individual_harvest_level from {vc_forestry_${result_group}player_harvest_table}"); - /* - - if($result_group==5) - { - $result=db_query(db_rewrite_sql("Select min_resource_level,max_resource_level,individual_harvest_level from {virtualcommons_forestry_5player_harvest_table}")); - - } - else if($result_group==4) - { - $result=db_query(db_rewrite_sql("Select min_resource_level,max_resource_level,individual_harvest_level from {virtualcommons_forestry_4player_harvest_table}")); - } - else if($result_group==3) - { - $result=db_query(db_rewrite_sql("Select min_resource_level,max_resource_level,individual_harvest_level from virtualcommons_forestry_3player_harvest_table")); - } - } - */ - -while($harvest_data=db_fetch_object($result)) -{ - $min=$harvest_data->min_resource_level; - $max=$harvest_data->max_resource_level; - $ind=$harvest_data->individual_harvest_level; - $row[]=array($min.' - '.$max,$ind); - //drupal_set_message(t($min . " " . $max . " " . $ind)); +function forestry_intro_form() { + $form['#id']='forestry_intro'; + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Next') + ); + return $form; } -$_SESSION['harvest_table']=$row; - -$form['harvest_table']=array( - '#type'=>'markup', - '#value'=>theme('table',$header,$row) - ); - -$form['submit'] = array( - '#type' => 'submit', - '#value' => t('Next') - ); -return $form; -} /** * Invoked after the student has successfully logged in and is trying to click * "Next" to start the game. */ -function forestry_v2_nameform_submit($form_id, $form_values) +function forestry_intro_form_submit($form_id, $form_values) { - $stud_id=$_SESSION['student_id']; - $group = Session::get('group'); - $flag=$group->getStartFlag($stud_id); - $game_id = $group->getGameId(); - drupal_set_message(t('Student Flag is: '.$flag)); - if($flag) { - $current_stage_no = $group->getCurrentStageNumber(); - $current_stage_no = (int) $current_stage_no; - drupal_set_message(t('current stage is: '. $current_stage_no)); - return 'forestry_stage1'; - /* - else if($current_stage_no==2) - { - $current_rule_no=$group->getCurrentRuleNumber(); - switch($current_rule_no) - { - case 1: - return 'forestry_stage2_rule1'; - case 2: - return 'forestry_stage2_rule2'; - case 3: - return 'forestry_stage2_rule3'; - } + return 'forestry/play'; +} - } - */ +function forestry_end() { + $output = '<h3>The experiment is now over. Thanks for participating!</h3>'; + $output .= generate_results_table(); + return $output; +} + +function generate_harvest_table() { + $student = Session::get('student'); + $harvest_table = $student->getGroup()->generate_harvest_table(); + $_SESSION['harvest_table']=$harvest_table->rows; + return '<fieldset><legend><b>Maximum Harvest Table</b></legend>'.theme_table($harvest_table->header, $harvest_table->rows).'</fieldset>'; +} + + +function generate_results_table() { + $student = Session::get('student'); + $table_data_array = $student->getAllStageData(); + $result = '<fieldset><legend><b>Round Results</b></legend>'; + if ($student->getGroup()->getGame()->getCurrentRoundNo() == 1) { + $result .= 'No results available yet for the first round'; } else { - return 'forestry'; + foreach ($table_data_array as $table_data) { + $result .= theme_table($table_data->header, $table_data->rows); + } } + $result .= '</fieldset>'; + return $result; } -//Forestry Stage 1 Begins - -function forestry_game_stage1() { - +// primary forestry controller +function forestry_play() { // Return the HTML generated from the $form data structure. - $output = drupal_get_form('forestry_game_stage1_multiform'); + $student = Session::get('student'); + // check if user is properly authenticated + if (! $student ) { + return drupal_get_form('student_login_nameform'); + } + $group = $student->getGroup(); + if ($group->getGame()->isFinished()) { + $output = 'The experiment is now over. Your results are listed below. Thanks for participating!'; + $output .= generate_results_table(); + } + else { + $output .= generate_harvest_table(); + $output .= generate_results_table(); + $output .= ( $student->canStart() ) ? drupal_get_form('forestry_play_multiform') : drupal_get_form('forestry_wait_form'); + } return $output; } - - -function forestry_game_stage1_multiform() +function forestry_play_multiform() { + $form['#id']='forestry_play_multiform'; + $form['#tree'] = TRUE; - $form['#id']='forestry_game_stage1_multiform'; - $group = Session::get('group'); - $student=new ForestryStudent(); + $student = Session::get('student'); + $group = $student->getGroup(); - $form['#tree'] = TRUE; $resource_after=0; $resource_before=0; - $stud_id=$_SESSION['student_id']; - $resource_available=0; - $round_no=0; - $student->setStudentId($stud_id); - //get the availble resource from the database + $round_no = $group->getGame()->getCurrentRoundNo(); - $group_id=$group->getGroupId(); - $game_id=$group->getGameId(); - $round_no=$group->getRoundNo(); - drupal_set_message(t('group id : '. $group_id . ' Game id: '. $game_id)); - $resource_available= $student->getLatestResource($group_id,$game_id); - - drupal_set_message(t('reosurce available : '. $resource_available)); - $resource_available =(int) $resource_available; - //FIXME:later on use session objects to do all session activity - if(!isset($_SESSION['prev_resource'])) - { - $_SESSION['prev_resource']=$resource_available; - drupal_set_message(t('PREV resource is NOW set to : ' . $resource_available)); - } - $_SESSION['prev_resource']=$resource_available; - $current_stage_id=$group->getCurrentStageId(); - drupal_set_message(t('current stage id: '. $current_stage_id)); - $game = $group->getGame(); - if ( $game->isVotingEnabled() ) { - $header=array("Round Number","Your Harvest Decision","Resources Before","Resources After",'Penalty', 'Net Gain', ); - if($group->isTurn($stud_id)) - $form['turn']=array( - '#type'=>'item', - '#value'=>t('You have chosen for this round to harvest. You can harvest for this round.') - ); - - } - else { - $header=array("Round Number","Your Harvest Decision","Resources Before","Resources After"); - } - - $row=$student->getForestryStudentInfo($group_id,$game_id,$current_stage_id); - - // $stage_id=$group->getCurrentStageId(); - - - - drupal_set_message(t('round no: ' .$round_no .' '. gettype($round_no) )); - - //store the round no in hidden field - $form['round_no']=array( '#type'=>'hidden', - '#value'=>$round_no + '#value'=>$round_no ); - - // $no_rounds = $group->getNumberOfRounds(); - drupal_set_message(t('game id:' . $group->getGameId())); - $no_rounds=$group->getNumberOfRounds(); - drupal_set_message(t('no of rounds are: '.$no_rounds)); - if($round_no <= $no_rounds) - { + $form['indicator']=array( + '#type'=>'fieldset', + '#title'=>t('<b>Round @number</b>',array('@number'=>$round_no)) + ); - $form['resources_available']=array( +$form['indicator']['resources_available']=array( + '#type'=>'item', + '#title'=>t('Resources Available'), + '#value'=>$group->getResourcesAvailable() + ); - '#type'=>'item', - '#title'=>t('Resources Available'), - '#value'=>$resource_available - ); - $form['indicator']=array( - '#type'=>'fieldset', - '#title'=>t('<b>Round @number</b>',array('@number'=>$round_no)) - ); - - $form['indicator']['harvest_decision']=array( - '#type'=>'textfield', - '#title'=>t('Your Harvest Decision'), - '#default_value'=>'0', - '#size'=>2, - '#maxlength'=>2, - '#required'=>TRUE - ); - $form['indicator']['submit'] = array( - '#type' => 'submit', - '#value' => t('Submit') - ); - - } - else - { -// $stage_no=$group->getCurrentStageNumber(); - // drupal_set_message(t('Stage no is : '.$stage_no)); - $form['indicator']['submit'] = array( - '#type' => 'submit', - '#value' => t('Next Stage') - ); - } - - $form['resource']=array( - '#type'=>'fieldset', - '#title'=>t('<b>Resources</b>'), - + $form['indicator']['harvest_decision']=array( + '#type'=>'textfield', + '#title'=>t('Your Harvest Decision'), + '#default_value'=>'0', + '#size'=>2, + '#maxlength'=>2, + '#required'=>TRUE ); - $form['resource']['table']=array( - '#type'=>'markup', - '#value'=>theme('table',$header,$row) + $form['indicator']['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit') ); - Session::put('forestry_student',$student); return $form; } -function forestry_game_stage1_multiform_validate($form_id,$form_values) +function forestry_play_multiform_validatewrong($form_id,$form_values) { $harvest_decision=$form_values['indicator']['harvest_decision']; $resource=(int) $_SESSION['prev_resource']; @@ -312,6 +194,10 @@ } else { + // FIXME: Seema, you have got to be kidding me. You are using this to + // determine whether or not the harvest decision is between the min and + // max??? I am going to leave this here to force you to figure out a + // better solution, which is very easy to come up with. $harvest_table= $_SESSION['harvest_table']; @@ -376,9 +262,10 @@ } -function forestry_game_stage1_multiform_submit($form_id,$form_values) +function forestry_play_multiform_submit($form_id,$form_values) { - $group = Session::get('group'); + $student = Session::get('student'); + $group = $student->getGroup(); $harvest_table=$_SESSION['harvest_table']; //$harvest_tbl=explode("-",$harvest_table); @@ -391,47 +278,47 @@ $round_no=$form_values['round_no']; - $round_no=(int) $round_no; + /* //get the student and set it $stud_id=$_SESSION['student_id']; $student=new ForestryStudent(); $student->setStudentId($stud_id); $stage_id=$group->getCurrentStageId(); $resources_before=(int) $_SESSION['prev_resource']; - drupal_set_message(t('resource_before:'. $resources_before)); - $student->setStageId($stage_id); - $student->saveForestryStudentInfo($round_no,$stud_harvest_decision); - $flag=false; - $student->updateStartFlag($flag); + */ + + + $student->save($round_no,$stud_harvest_decision); + $student->updateStartFlag(false); + return 'forestry/play'; + + /* $no_rounds=$group->getNumberOfRounds(); - - $no_rounds=(int) $no_rounds; - drupal_set_message(t('$actual_no_rounds' . $no_rounds . '$current_round_no' . $round_no)); if($form_values['indicator']['submit']=="Next Stage") { - $stage_no=2; - $exp_id=$group->getExperimentId(); - $game_id=$group->getGameId(); + //$stage_no=2; + // $exp_id=$group->getExperimentId(); + //$game_id=$group->getGameId(); + drupal_set_message("in next stage button functionality"); unset($_SESSION['prev_resource']); return 'rules'; } else return 'forestry_stage1_part2'; - - + */ } -function theme_forestry_game_stage1_multiform($form) +function theme_forestry_play_multiform($form) { $output='<div class="indicator"><table style="text-align: center; width: 100%;" border="0" cellpadding="0" cellspacing="0">'; - $output.='<h2 align="center" style="color:#990000">'; - $output.=drupal_render($form['turn']); - $output.='</h2> + $output.='<h2 align="center" style="color:#990000">'; + $output.=drupal_render($form['turn']); + $output.='</h2> <tbody> <tr> <td style="width: 80%;"> </td> @@ -471,119 +358,48 @@ } -function forestry_game_stage1_part2() +function forestry_wait_form() { - - // $output=t('Please click the Next button for next round'); - $output.=drupal_get_form('forestry_game_stage1_part2form'); - return $output; -} - -function forestry_game_stage1_part2form() -{ - -$student=Session::get('forestry_student'); -$stage_flag=FALSE; -$group=Session::get('group'); -$group_id=$group->getGroupId(); + $student = Session::get('student'); + $group = $student->getGroup(); + $group_id=$group->getGroupId(); $game_id=$group->getGameId(); - if ($group->isGameFinished()) - { - drupal_set_message('The game is over. Thanks for participating!'); -// $_SESSION['finished']='game'; - } - else - { - $stage_flag=$group->isStageOver(); - } - if($stage_flag) - { - $header=array("Round Number","Your Harvest Decision","Resources Before","Resources After"); - $row=$student->getForestryStudentInfo($group_id,$game_id); - - $form['stage_info']=array( - '#type'=>'item', - '#value'=>t(' Following is the summary of your harvest decision for this stage. This stage is over.') - ); - - $form['resource']=array( - '#type'=>'fieldset', - '#title'=>t('<b>Resources</b>'), - - ); - $form['resource']['table']=array( - '#type'=>'markup', - '#value'=>theme('table',$header,$row) - ); - - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Next Stage') - ); - unset($_SESSION['finished']); - } - - - else - { - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Next Round') - ); - } + // FIXME: hack only in place for Drupal 5, can get rid of in Drupal 6 in + // favor of the $repeat parameter in drupal_set_message + drupal_get_messages(); + drupal_set_message( + t('Please wait until the rest of the participants finish + making their decisions and the facilitator instructs you to click on + the next button. Clicking on the next button before the facilitator is + ready will have no effect.'), + 'warning', FALSE); + if ($group->isGameFinished()) + { + drupal_set_message(t('The game is over. Thanks for participating.'), + 'warning', FALSE); + } + else if($group->getGame()->isStageOver()) + { + drupal_set_message(t('This stage is now over.'), 'warning', FALSE); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Go to the next stage') + ); + } + else + { + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Next Round') + ); + } return $form; - + } -function forestry_game_stage1_part2form_submit($form_id,$form_values) +function forestry_wait_form_submit($form_id,$form_values) { - - $stud_id=$_SESSION['student_id']; - $student=Session::get('forestry_student'); - $group=Session::get('group'); - - - $stage_flag=FALSE; - drupal_set_message(t($form_values['submit'])); - $flag=$student->getStartFlag(); -//if($round_no >= $no_rounds) { } - - /*if($form_values['submit']=="Next Round") - { - $flag=$student->getStartFlag(); -drupal_set_message(t('flag'.$flag)); - if ($group->isGameFinished()) - { - drupal_set_message('The game is over. Thanks for participating!'); -// $_SESSION['finished']='game'; - } - else - { - $stage_flag=$group->isStageOver(); -// if($stage_flag) - //if(!(isset($_SESSION['finished']))) - //$_SESSION['finished']='stage'; - drupal_set_message(t('stage flag is: ' . $stage_flag)); - }*/ - if($flag) - { - // if($stage_flag) - return 'forestry_stage1'; - } - else { - return 'forestry_stage1_part2'; - } - - - - - - + return 'forestry/play'; } - - - - - Modified: vcweb/trunk/virtualcommons/experiment/student_login/student_login.module =================================================================== --- vcweb/trunk/virtualcommons/experiment/student_login/student_login.module 2008-08-11 23:16:36 UTC (rev 38) +++ vcweb/trunk/virtualcommons/experiment/student_login/student_login.module 2008-08-13 09:32:34 UTC (rev 39) @@ -58,26 +58,20 @@ function student_login_nameform_validate($form_id,$form_values) { $stud_id=$form_values['id']; - $group = Group::findByStudentId($stud_id); - if ($group) { + $student = Group::findByStudentId($stud_id); + if ($student) { // set the game object into the session, currently only contains the // game_id - Session::put('group',$group); + Session::put('student',$student); } else { form_set_error('id',t('Invalid Student ID. Please contact your instructor.')); } } - - function student_login_nameform_submit($form_id,$form_values) { $_SESSION['student_id']=$form_values['id']; - return 'forestry'; + return 'forestry/intro'; } - - - - Modified: vcweb/trunk/virtualcommons/experiment/teacher_dashboard/teacher_dashboard.module =================================================================== --- vcweb/trunk/virtualcommons/experiment/teacher_dashboard/teacher_dashboard.module 2008-08-11 23:16:36 UTC (rev 38) +++ vcweb/trunk/virtualcommons/experiment/teacher_dashboard/teacher_dashboard.module 2008-08-13 09:32:34 UTC (rev 39) @@ -76,7 +76,7 @@ // display all stage data $all_stage_data = $game->getAllGroupRoundInfo(); $header_novoting=array('Round No','Group No', 'Group Harvest Decision','Resources Before','Resources After'); - $header_voting=array('Round No','Group No', 'Group Harvest Decision','Resources Before','Resources After', 'Dice', 'Rule #'); + $header_voting=array('Round No','Group No', 'Group Harvest Decision','Resources Before','Resources After', 'Rule #', 'Dice'); foreach ($all_stage_data as $stage_id => $stage_data) { $stage_no = $game->getStageNumber($stage_id); @@ -84,7 +84,7 @@ '#type'=>'fieldset', '#title'=>t("<b>Stage ${stage_no}</b>") ); - $header = ($game->isVotingEnabledForStage($stageId) ? $header_voting : $header_novoting); + $header = ($game->isVotingEnabledForStage($stage_id) ? $header_voting : $header_novoting); $form['stage_'.$stage_no]['table']=array( '#type'=>'markup', '#value'=>theme('table',$header,$stage_data) @@ -93,7 +93,7 @@ if ($game->isFinished()) { // $output = t('The game is over.'); - drupal_set_message('The game is over. Thanks for participating!'); + drupal_set_message(t('<h2 align="center" style="color:#990000">The game is over. Thanks for participating!</h2>')); /* $form['final_msg']=array( '#type'=>'markup', @@ -116,7 +116,7 @@ } - drupal_set_message(t('Stage flag is:' .$stage_flag)); + //drupal_set_message(t('Stage flag is:' .$stage_flag)); $form['submit'] = array( '#type' => 'submit', @@ -156,7 +156,6 @@ //set flag, when flag=TRUE it means round is even , when flag=FALSE it means //that round is uneven. Ja! $game=Session::get('game'); - $game->setRoundNo($game->getCurrentRoundNo()); if($form_values['submit']=="Next Round") { // $no_rounds = $game->getNumberOfRounds(); @@ -190,4 +189,4 @@ } -?> \ No newline at end of file +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |