Created
June 19, 2011 07:34
-
-
Save jontonsoup/1033878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//so the csv inputs correctly | |
ini_set("auto_detect_line_endings", "1"); | |
/** | |
* Statistics Controller | |
* | |
*/ | |
class StatisticsController extends AppController { | |
/** | |
* index method | |
* | |
* @return void | |
*/ | |
public function index() { | |
$this->Statistic->recursive = 0; | |
$this->set('statistics', $this->paginate()); | |
$school = $this->Statistic->findByAffVotes('34'); | |
$this->set('wow', $school['School']['name']); | |
} | |
/** | |
* view method | |
* | |
* @param string $id | |
* @return void | |
*/ | |
public function view($id = null) { | |
$this->Statistic->id = $id; | |
if (!$this->Statistic->exists()) { | |
throw new NotFoundException(__('Invalid statistic')); | |
} | |
$this->set('statistic', $this->Statistic->read(null, $id)); | |
} | |
/** | |
* add method | |
* | |
* @return void | |
*/ | |
public function add() { | |
if ($this->request->is('post')) { | |
$this->Statistic->create(); | |
if ($this->Statistic->save($this->request->data)) { | |
$this->Session->setFlash(__('The statistic has been saved')); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash(__('The statistic could not be saved. Please, try again.')); | |
} | |
} | |
$judges = $this->Statistic->Judge->find('list'); | |
$schools = $this->Statistic->School->find('list'); | |
$debaters = $this->Statistic->Debater->find('list'); | |
$this->set(compact('judges', 'schools', 'debaters')); | |
} | |
/** | |
* edit method | |
* | |
* @param string $id | |
* @return void | |
*/ | |
public function edit($id = null) { | |
$this->Statistic->id = $id; | |
if (!$this->Statistic->exists()) { | |
throw new NotFoundException(__('Invalid statistic')); | |
} | |
if ($this->request->is('post') || $this->request->is('put')) { | |
if ($this->Statistic->save($this->request->data)) { | |
$this->Session->setFlash(__('The statistic has been saved')); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash(__('The statistic could not be saved. Please, try again.')); | |
} | |
} else { | |
$this->request->data = $this->Statistic->read(null, $id); | |
} | |
$judges = $this->Statistic->Judge->find('list'); | |
$schools = $this->Statistic->School->find('list'); | |
$debaters = $this->Statistic->Debater->find('list'); | |
$this->set(compact('judges', 'schools', 'debaters')); | |
} | |
/** | |
* delete method | |
* | |
* @param string $id | |
* @return void | |
*/ | |
public function delete($id = null) { | |
if (!$this->request->is('post')) { | |
throw new MethodNotAllowedException(); | |
} | |
$this->Statistic->id = $id; | |
if (!$this->Statistic->exists()) { | |
throw new NotFoundException(__('Invalid statistic')); | |
} | |
if ($this->Statistic->delete()) { | |
$this->Session->setFlash(__('Statistic deleted')); | |
$this->redirect(array('action' => 'index')); | |
} | |
$this->Session->setFlash(__('Statistic was not deleted')); | |
$this->redirect(array('action' => 'index')); | |
} | |
/* * ************************* */ | |
/* csv to array functions */ | |
/* * ************************ */ | |
private function convert_csv_to_array($path, $file, &$csv_array) { | |
$file = fopen($path, "r"); | |
$i = 0; | |
while (!feof($file)) { | |
$csv_array[$i] = fgetcsv($file); | |
$i++; | |
} | |
return TRUE; | |
} | |
/* * *********************** */ | |
/* clean up data functions */ | |
/* * *********************** */ | |
private function clean_up_data(&$csv_array, $number_of_rows_in_block) { | |
//fix loop if csv has headers | |
if ($csv_array[0][0] == "info") { | |
$start = 3; | |
} else { | |
$start = 2; | |
} | |
for ($i = $start; $i < count($csv_array); $i = $i + $number_of_rows_in_block) { | |
for ($j = 1; $j < count($csv_array[$i]); $j++) { | |
//replace extranous characters | |
$csv_array[$i][$j] = str_replace(array(",", "/", "*", "'", "."), "", $csv_array[$i][$j]); | |
} | |
} | |
} | |
/* * *************************** */ | |
/* functions to save the data */ | |
/* * *************************** */ | |
private function save_to_database($csv_array) { | |
//fix loop if csv has headers | |
if ($csv_array[0][0] == "info") { | |
$start = 1; | |
} else { | |
$start = 0; | |
} | |
for ($i = $start; $i < count($csv_array); $i = $i + $number_of_rows_in_block) { | |
//get values | |
$debater_name = $csv_array[$i + 3][0]; | |
$school = $csv_array[$i + 1][0]; | |
for ($j = 1; $j < count($csv_array[$i]); $j++) { | |
//get values | |
$outcome = $csv_array[$i][$j]; | |
$judge = $csv_array[$i + 2][$j]; | |
$speaker_points = $csv_array[$i + 3][$j]; | |
$speaker_point_column_name = $this->determine_speakerpoint_bucket($speaker_points); | |
//check if school is in database | |
if ($this->School->findByname($school) != NULL) { | |
} else { | |
$this->School->create(); | |
$this->School->save(array("School" => array('name' => $school))); | |
} | |
//check if debater is in database | |
if ($this->Debater->findByname($debater) != NULL) { | |
} else { | |
$this->Debater->create(); | |
$this->Debater->save(array("Debater" => array('name' => $debater_name, 'school_id' => $this->School->id))); | |
} | |
//check if statistic is in database | |
if ($this->Statistic->findByname($statistic) != NULL) { | |
} else { | |
$flag = true; | |
$rounds_judged_column_name = $this->determine_aff_or_neg_for_rounds_judged($outcome); | |
$rounds_won_column_name = $this->determine_aff_or_neg_for_rounds_won($outcome, $flag); | |
$rounds_won_number = 0; | |
if ($flag = true) { | |
$rounds_won_number = 1; | |
} | |
$this->Statistic->create(); | |
$this->Statistic->save(array("Statistic" => array('judge_id' => $this->Judge->id, | |
'school_id' => $this->School->id, | |
'debater_id' => $this->debater->id, | |
'rounds_judged' => 1, | |
$rounds_judged_column_name => 1, | |
$rounds_won_column_name => $rounds_won_number, | |
'total_speaker_points' => $speaker_points, | |
'processed' => 'false', | |
$speaker_point_column_name => 1))); | |
} | |
} | |
} | |
} | |
private function determine_speakerpoint_bucket($speaker_points) { | |
if ($speaker_points == 20) { | |
return 'total_20'; | |
} | |
if ($speaker_points == 20.5) { | |
return 'total_20_5'; | |
} | |
if ($speaker_points == 21) { | |
return 'total_21'; | |
} | |
if ($speaker_points == 21.5) { | |
return 'total_21_5'; | |
} | |
if ($speaker_points == 22) { | |
return 'total_22'; | |
} | |
if ($speaker_points == 22.5) { | |
return 'total_22_5'; | |
} | |
if ($speaker_points == 23) { | |
return 'total_23'; | |
} | |
if ($speaker_points == 23.5) { | |
return 'total_23_5'; | |
} | |
if ($speaker_points == 24) { | |
return 'total_24'; | |
} | |
if ($speaker_points == 24.) { | |
return 'total_24_5'; | |
} | |
if ($speaker_points == 25) { | |
return 'total_25'; | |
} | |
if ($speaker_points == 25.5) { | |
return 'total_25_5'; | |
} | |
if ($speaker_points == 26) { | |
return 'total_26'; | |
} | |
if ($speaker_points == 26.5) { | |
return 'total_26_5'; | |
} | |
if ($speaker_points == 27) { | |
return 'total_27'; | |
} | |
if ($speaker_points == 27.5) { | |
return 'total_27_5'; | |
} | |
if ($speaker_points == 28) { | |
return 'total_28'; | |
} | |
if ($speaker_points == 28.5) { | |
return 'total_28_5'; | |
} | |
if ($speaker_points == 29) { | |
return 'total_29'; | |
} | |
if ($speaker_points == 29.5) { | |
return 'total_29_5'; | |
} | |
if ($speaker_points == 30) { | |
return 'total_30'; | |
} | |
} | |
private function determine_aff_or_neg_for_rounds_judged($outcome) { | |
if ($outcome == 'W PRO' || 'W AFF' || 'w aff' || 'w pro' || 'L PRO' || 'L AFF' || 'L aff' || 'L pro') { | |
return 'aff_rounds_judged'; | |
} else { | |
return 'neg_rounds_judged'; | |
} | |
} | |
private function determine_aff_or_neg_for_rounds_won($outcome, &$flag) { | |
if ($outcome == 'W PRO' || 'W AFF' || 'w aff' || 'w pro') { | |
return 'aff_votes'; | |
} else if ($outcome == 'W Con' || 'W Neg' || 'w con' || 'w neg') { | |
return 'neg_votes'; | |
} else { | |
$flag = false; | |
return 'neg_votes'; | |
} | |
} | |
/* * ************************************** */ | |
/* general functions | |
/* * ************************************** */ | |
/* * ********************************************* */ | |
/* the main function that procresses everything */ | |
/* * ********************************************* */ | |
public function process() { | |
/* This determines the tournament type. | |
* TRUE for LD, False for Policy | |
* | |
*/ | |
$is_ld = true; | |
//logic for ld vs policy | |
if ($is_ld == true) { | |
$number_of_rows_in_block = 4; | |
} else { | |
$number_of_rows_in_block = 5; | |
} | |
//do all the processing | |
$csv_array = array(); | |
//if the user has submitted a form | |
if ($this->data == true) { | |
$path = $this->data["File"]["tmp_name"]; | |
$file = fopen($path, "r"); | |
$this->convert_csv_to_array($path, $file, &$csv_array); | |
//save the file on the server | |
$name = "/Applications/MAMP/htdocs/Fantasy-Debate-Statistics-Tracker/app/webroot/files/" . $this->data["File"]["name"]; | |
copy($path, $name); | |
//done with file, so close it | |
fclose($file); | |
$this->set("array", $csv_array); | |
$this->clean_up_data($csv_array, $number_of_rows_in_block); | |
$this->set("array_csv", $csv_array); | |
$this->save_to_database($csv_array); | |
$this->set("pretty", json_encode($csv_array)); | |
} else { | |
$this->set("array", "First array"); | |
$this->set("array_csv", "Output goes here"); | |
$this->set("pretty", "Output goes here"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment