returns participant ID:Int postMessageOnFriendWall() returns boolean getGrandWinner() returns ParticipantDTO getDayWinners() returns array of ParticipantDTO getParticipant(participantId:Int) => returns ParticipantDTO getWok(participantId:Int) => returns WokDTO getParticipantCount() => returns count of participants class ParticipantDTO { public $destination; public $user_fb_id; public $user_fb_destination; public $user_email; public $friend_fb_name; public $friend_fb_id; public $wok_name; public $created; public $day_winner; public $grand_winner; } class WokDTO { public $wok_name; public $vegetables; public $flavors; public $participant_id; } */ class Competition { // database connection protected $db; // hold facebook object protected $facebook; // create connection on construct function __construct(){ $this->db = new dbConn(); } // save participant public function setParticipant( $vegetables, $flavors, $wok_name, $destination, $user_email, $user_fbid, $user_fbname, $friend_fbid, $friend_fbname, $newsletter, $mobile ){ // create participant $sql = "insert into participant_en ( destination, wok_name, user_email, newsletter, user_fbid, user_fbname, friend_fbname, friend_fbid, mobile_submission, created ) values ( :destination, :wok_name, :user_email, :newsletter, :user_fbid, :user_fbname, :friend_fbname, :friend_fbid, :mobile_submission, now() ) "; $data = array( 'destination' => $destination, 'wok_name' => $wok_name, 'user_email' => $user_email, 'newsletter' => $newsletter, 'user_fbid' => $user_fbid, 'user_fbname' => $user_fbname, 'friend_fbname' => $friend_fbname, 'friend_fbid' => $friend_fbid, 'mobile_submission' => $mobile ); $this->db->query($sql, $data); // get last insert id $participantId = $this->db->lastInsertId(); //print_r($vegetables); // create vegetables for this participant foreach ($vegetables as $vegetable) { //foreach ($values as $value ) { //$value = str_replace('vegetables[]', '', $value); $sql = "insert into participant_vegetable_en (participant_id, vegetable_id) values (:participant_id, :vegetable_id)"; $data = array( 'participant_id' => $participantId, 'vegetable_id' => $vegetable ); $this->db->query($sql, $data); //} } // create flavors [sic] for participant //print_r($flavors); foreach ($flavors as $flavor) { //foreach ($values as $value ) { // echo $value; //$value = str_replace('flavours[]', '', $value); $sql = "insert into participant_flavor_en (participant_id, flavor_id) values (:participant_id, :flavor_id)"; $data = array( 'participant_id' => $participantId, 'flavor_id' => $flavor ); $this->db->query($sql, $data); //} } // return participant id so website can create share link return $participantId; } // posts a facebook message on a person's wall public function postWallMessage($fbid, $message, $link, $name, $caption, $description, $picture) { // Create our Application instance (replace this with your appId and secret). $this->facebook = new Facebook(array( 'appId' => '535064963187915', 'secret' => '613bd05e1349806422e6ae7978b53e78', )); // echo fred($this->facebook->api('/me')); // Try post on friend's wall try { $this->facebook->api('/'.$fbid.'/feed', 'post', array( 'message' => $message, 'link' => $link, 'name' => $name, 'caption' => $caption, 'description' => $description, 'picture' => $picture )); return true; } catch (Exception $e) { // probably not allowed by user return false; } } // get grand winner. returns ParticipantDTO public function getGrandWinner(){ // get winner $query = $this->db->fetch(" select id, destination, user_fbid, user_fbname, user_email, friend_fbname, friend_fbid, wok_name, date(created) as created, day_winner, grand_winner from participant_en where grand_winner = 1 "); // popupate return object $participant = new ParticipantDTO(); if ($query) { $participant->id = $query['id']; $participant->destination = $query['destination']; $participant->user_fbid = $query['user_fbid']; $participant->user_fbname = $query['user_fbname']; $participant->user_email = $query['user_email']; $participant->friend_fbname = $query['friend_fbname']; $participant->friend_fbid = $query['friend_fbid']; $participant->wok_name = $query['wok_name']; $participant->created = $query['created']; $participant->day_winner = $query['day_winner']; $participant->grand_winner = $query['grand_winner']; } return $participant; } // get day winner. returns ParticipantDTO // date input format YYYY-MM-DD public function getDayWinner($date){ // get winner $query = $this->db->fetch(" select id, destination, user_fbid, user_fbname, user_email, friend_fbname, friend_fbid, wok_name, date(created) as created, day_winner, grand_winner from participant_en where day_winner = 1 and date(created) = :date ", array("date" => $date )); // popupate return object $participant = new ParticipantDTO(); if ($query) { $participant->id = $query['id']; $participant->destination = $query['destination']; $participant->user_fbid = $query['user_fbid']; $participant->user_fbname = $query['user_fbname']; $participant->user_email = $query['user_email']; $participant->friend_fbname = $query['friend_fbname']; $participant->friend_fbid = $query['friend_fbid']; $participant->wok_name = $query['wok_name']; $participant->created = $query['created']; $participant->day_winner = $query['day_winner']; $participant->grand_winner = $query['grand_winner']; } return $participant; } // get day winners (array). returns array of ParticipantDTO public function getDayWinners(){ // get all day winners $query = $this->db->fetchAll(" select id, destination, user_fbid, user_fbname, user_email, friend_fbname, friend_fbid, wok_name, date(created) as created, day_winner, grand_winner from participant_en where day_winner = 1 order by date(created) desc "); // return array $participants = array(); // popupate return object if ($query) { foreach ($query as $row) { $participant = new ParticipantDTO(); $participant->id = $row['id']; $participant->destination = $row['destination']; $participant->user_fbid = $row['user_fbid']; $participant->user_fbname = $row['user_fbname']; $participant->user_email = $row['user_email']; $participant->friend_fbname = $row['friend_fbname']; $participant->friend_fbid = $row['friend_fbid']; $participant->wok_name = $row['wok_name']; $participant->created = $row['created']; $participant->day_winner = $row['day_winner']; $participant->grand_winner = $row['grand_winner']; $participants[] = $participant; } } return $participants; } // get participant public function getParticipant($participant_id){ // get participant $query = $this->db->fetch(" select id, destination, user_fbid, user_fbname, user_email, friend_fbname, friend_fbid, wok_name, date(created) as created, day_winner, grand_winner from participant_en where id=:id ", array("id" => $participant_id)); // popupate return object $participant = new ParticipantDTO(); if ($query) { $participant->id = $query['id']; $participant->destination = $query['destination']; $participant->user_fbid = $query['user_fbid']; $participant->user_fbname = $query['user_fbname']; $participant->user_email = $query['user_email']; $participant->friend_fbname = $query['friend_fbname']; $participant->friend_fbid = $query['friend_fbid']; $participant->wok_name = $query['wok_name']; $participant->created = $query['created']; $participant->day_winner = $query['day_winner']; $participant->grand_winner = $query['grand_winner']; } return $participant; } // get wok by participant id public function getWok($participant_id) { // create wok object $wok = new WokDTO; /*// get vegetables $query = $this->db->fetchAll("select vegetable_id from participant_vegetable where participant_id=:id", array("id" => $participant_id)); foreach ($query as $item) { $wok->vegetables[] = $item['vegetable_id']; } // get flavors $query = $this->db->fetchAll("select flavor_id from participant_flavor where participant_id=:id", array("id" => $participant_id)); foreach ($query as $item) { $wok->flavors[] = $item['flavor_id']; }*/ // get vegetables $query = $this->db->fetchAll(" select p.vegetable_id, w.vegetable_en as vegetable, w.description_en as description, w.class, w.size from participant_vegetable p left join wok_vegetables w on p.vegetable_id = w.id where p.participant_id=:id", array("id" => $participant_id)); $wok->vegetables = $query; /*fred($wok); exit(); foreach ($query as $item) { $wok->vegetables_wok = '
'; $wok->vegetables_backdrop .= '
'; $wok->vegetables_list .= '
' . $item['vegetable'] . '
'; }*/ // get flavors $query = $this->db->fetchAll(" select p.flavor_id, w.flavor_en as flavor, w.description_en as description, w.class, w.size from participant_flavor p left join wok_flavors w on p.flavor_id = w.id where p.participant_id=:id", array("id" => $participant_id)); $wok->flavors = $query; //fred($wok); /* foreach ($query as $item) { $wok->flavors_wok .= '
'; $wok->flavors_backdrop .= '
'; $wok->flavors_list .= '
' . $item['vegetable'] . '
'; }*/ return $wok; } // get count of all participants public function getParticipantCount() { return $this->db->querySingleColumn("select count(*) from participant_en"); } // kill db connection on deconstruct public function __destruct(){ $this->db = NULL; } }