Calculate scores based on Faith's criteria.

This commit is contained in:
Kim Taylor
2024-09-21 19:49:49 +10:00
parent 2453d550ce
commit e2fbd1b1ef
4 changed files with 89 additions and 7 deletions

View File

@@ -0,0 +1,31 @@
<?php
require_once("parse_generic_csv.php");
$options = getopt("", ["generic-csv:"]);
if (isset($options['generic-csv'])) {
$generic_csv = $options['generic-csv'];
} else {
error_log("Error: Missing required option '--generic-csv'.");
exit(1);
}
$candidate_data = parse_generic_csv($generic_csv);
/* Calculate score for candidate */
foreach ($candidate_data as &$candidate) {
$score = 0;
if ($candidate['Pledge'] === "Yes") $score++;
if ($candidate['q1'] === "Yes") $score++;
if ($candidate['q3'] === "Yes") $score++;
if ($candidate['q4'] === "Yes") $score++;
if ($candidate['q7'] === "Yes") $score++;
$candidate['Score'] = $score;
}
print_r($candidate_data);
exit(0);

View File

@@ -0,0 +1,46 @@
<?php
function parse_generic_csv($generic_csv) {
$candidate_data = [];
if (($handle = fopen($generic_csv, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
$question_no = 0;
$is_question = false;
foreach ($headers as $key => $value) {
/* Override key name for questions */
if ($value === "Verified") {
$is_question = false;
}
if (strstr($value, "candidate photo")) $value = "Photo";
if (strstr($value, "In which Local Government Area")) $value = "LGA";
if (strstr($value, "In which Ward")) $value = "Ward";
if (strstr($value, "Political Party")) $value = "Party";
if ($value === "Pledge") {
if (strstr($data[$key], "I pledge")) $data[$key] = "Yes";
else $data[$key] = "No";
}
if ($is_question) {
$candidate['q'.$question_no++] = $data[$key];
} else {
$candidate[$value] = $data[$key];
}
if ($value === "Pledge") {
$is_question = true;
}
}
$candidate_data[] = $candidate;
}
fclose($handle);
} else {
error_log('Error opening candidates file');
exit(1);
}
return $candidate_data;
}