Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ public static function addNewEvent($data)
file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$newid.".txt", $result, FILE_APPEND);
}

// create new mappings file: result classname|coursename|courseid so we can match them up again for updates.
// may be able to use the sarjat file but don't want to touch it - I don't know what it could break in RG1
for ($i = 1; $i < count($data->mappings); $i++) {
$class = utils::encode_rg_output($data->mappings[$i]->class);
$course = utils::encode_rg_output($data->mappings[$i]->course);
$courseid = utils::encode_rg_output($data->mappings[$i]->courseid);

$classmapping = $class."|".$course."|".$courseid.PHP_EOL;
file_put_contents(KARTAT_DIRECTORY."mappings_".$newid.".txt", $classmapping, FILE_APPEND);
}

if ($write["status_msg"] == "") {
$write["ok"] = true;
$write["status_msg"] = "Event created.";
Expand Down Expand Up @@ -441,7 +452,7 @@ public static function deleteEvent($eventid)

// rename all associated files but don't worry about errors
// safer than deleting them since you can always add the event again
$files = array("kilpailijat_", "kommentit_", "hajontakanta_", "merkinnat_", "radat_", "ratapisteet_", "sarjat_", "sarjojenkoodit_");
$files = array("kilpailijat_", "kommentit_", "hajontakanta_", "merkinnat_", "radat_", "ratapisteet_", "sarjat_", "sarjojenkoodit_", "mappings_");
foreach ($files as $file) {
@rename(KARTAT_DIRECTORY.$file.$eventid.".txt", KARTAT_DIRECTORY."deleted_".$file.$eventid.".txt");
}
Expand Down Expand Up @@ -569,4 +580,4 @@ public static function fixResults($id) {
utils::unlockDatabase();
}
}
}
}
205 changes: 204 additions & 1 deletion app/result.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,214 @@ private static function isDefaultComment($comment)
return false;
}

public static function updateResults($eventid, $data){

$date = date("Y-m-dTHis");
$write["status_msg"] = "";

utils::rg2log("Updating result files for ".$eventid.". Old data will have datestamp: ".$date.".txt");


//Archive old kilpailijat file
rename(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt",
KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt");


// create new kilpailijat file: results
for ($i = 0; $i < count($data->results); $i++) {
$a = $data->results[$i];

// save position and status if we got them
if (isset($a->position)) {
$position = $a->position;
} else {
$position = '';
}
if (isset($a->status)) {
$status = utils::abbreviateStatus($a->status);
} else {
$status = '';
}

// course provided by json is actually result class - get correct course name
// based on mapping file, and load to txt file if not "Do not save"
$coursename = "";
$courseid = "";
$fh = fopen(KARTAT_DIRECTORY."mappings_".$eventid.".txt", 'r');
while ($oldrow = fgets($fh)) {
$row = explode("|", $oldrow);
if (utils::encode_rg_output($a->course) == $row[0]) {
$coursename = $row[1];
$courseid = $row[2];
break;
}
}

if($coursename !== "Do not save"){
$result = ($i + 1)."|".trim($courseid)."|".utils::encode_rg_output($coursename);
$result .= "|".utils::encode_rg_output(trim($a->name))."|".$a->starttime."|";
// abusing dbid to save status and position
$result .= utils::encode_rg_output($a->dbid)."_#".$position."#".$status;
$result .= "|".$a->variantid."|".$a->time."|".$a->splits.PHP_EOL;
file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", $result, FILE_APPEND);
}
}


// Get orig and new resultid so we can replace them in comments and routes files
// 1|1|C2|Bridget Anderson|35700|121_#1#OK||00:51:36|

$kilpailijat = array();

$fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt", 'r');
while ($oldrow = fgets($fh)) {

$old = explode("|", $oldrow);

$fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", 'r');
while ($newrow = fgets($fh)) {

$new = explode("|", $newrow);

if ($new[3] == $old[3] && $new[2] == $old[2]){

$row = array();
$row["origresultid"] = $old[0];
$row["newresultid"] = $new[0];
$row["origcourseid"] = $old[1];
$row["newcourseid"] = $new[1];
$row["coursename"] = $old[2];
$row["name"] = $old[3];
$kilpailijat[] = $row;

break;
}
}
}

// Add GPS routes from old file
$fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt", 'r');
while ($oldrow = fgets($fh)) {

$old = explode("|", $oldrow);

if (strpos($old[3], 'GPS ') !== false){

// Get the current Id for the non-GPS record

foreach ($kilpailijat as $kil){

if (substr($old[3], 5) == $kil["name"] &&
$old[2] == $kil["coursename"]){

$row = array();
$row["origresultid"] = $old[0];
$row["newresultid"] = GPS_RESULT_OFFSET + $kil["newresultid"];
$row["origcourseid"] = $old[1];
$row["newcourseid"] = $kil["newcourseid"];
$row["coursename"] = $old[2];
$row["name"] = $old[3];
$kilpailijat[] = $row;

$result = $row["newresultid"]."|".$row["newcourseid"]."|".$row["coursename"];
$result .= "|".$row["name"]."|".$old[4]."|".$old[5]."|".$old[6]."|".$old[7]."|".$old[8]."|".$old[9];

file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", $result, FILE_APPEND);

break;

}
}
}
}

if (file_exists(KARTAT_DIRECTORY."kommentit_".$eventid.".txt")){

// Recreate kommentit file
// Replace old course id and result id with ones from new kilpailijat file
// Kommentit format: 2|34|Jake Hanson||test

$updatedfile = array();
$fh = fopen(KARTAT_DIRECTORY."/kommentit_".$eventid.".txt", 'r');
while ($oldrow = fgets($fh)) {

$olddata = explode("|", $oldrow);

foreach ($kilpailijat as $k){
if ($k["origresultid"] == $olddata[1]){

$row = $k["newcourseid"]."|".$k["newresultid"]."|".$olddata[2]."|".$olddata[3]."|".$olddata[4];

$updatedfile[] = $row;

break;

}
}
}

//Archive old file
rename(KARTAT_DIRECTORY."kommentit_".$eventid.".txt",
KARTAT_DIRECTORY."kommentit_".$eventid."_rm_".$date.".txt");

//Write new file
file_put_contents(KARTAT_DIRECTORY."kommentit_".$eventid.".txt", $updatedfile);
utils::rg2log("Updated route comments file ");
}

if (file_exists(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt")){


// Recreate merkinnat file
// Replace old course id and result id with ones from new kilpaijat file
// Merkinnat format: 2|34|Jake Hanson|null|Semicolon-separated results

$updatedfile = array();
$fh = fopen(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", 'r');
while ($oldrow = fgets($fh)) {
$olddata = explode("|", $oldrow);

foreach ($kilpailijat as $k){
if ($k["origresultid"] == $olddata[1]){

$row = $k["newcourseid"]."|".$k["newresultid"]."|".$olddata[2]."|".$olddata[2]."|".$olddata[4];

$updatedfile[] = $row;

break;

}
}

}

//Archive old file
rename(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt",
KARTAT_DIRECTORY."merkinnat_".$eventid."_rm_".$date.".txt");

//Write updated file
file_put_contents(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", $updatedfile);
utils::rg2log("Updated routes file");

}

if ($write["status_msg"] == "") {
$write["ok"] = true;
$write["status_msg"] = "Results updated.";
} else {
$write["ok"] = false;
}
utils::rg2log("Updated results file ");
return $write;

utils::unlockDatabase();
}

private static function sortResultsByCourseThenTime($a, $b)
{
if (intval($a["courseid"]) !== intval($b["courseid"])) {
return (intval($a["courseid"]) - intval($b["courseid"]));
}
return (intval($a["secs"]) - intval($b["secs"]));
}
}
}
7 changes: 7 additions & 0 deletions html/manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h3 class="no-top-margin">Courses</h3>
<span>Move map and controls together <input type="checkbox" id="btn-move-map-and-controls"></span>
</div>
<div id="rg2-course-allocations"></div>
<button type="button" id="btn-add-fieldmapping">Add class-course pair</button>
<hr class="rg2-hr">
<div>
<span>Read-only (enable drawing via "Edit event" tab) <input type="checkbox" id="chk-read-only"></span>
Expand Down Expand Up @@ -95,6 +96,12 @@ <h3 class="no-top-margin">Delete route</h3>
<br/>
<button type="button" class="manage-file-label" id="btn-delete-route">Delete</button>
</div>
<div>
<hr class="rg2-hr">
<h3 class="no-top-margin">Update results</h3>
<span>Results file<input type='file' accept='.csv, .xml' id='rg2-update-results-file' class="pushright manage-file-input" /></span>
<button type="button" class="manage-file-label" id="btn-update-results">Update results</button>
</div>
<hr class="rg2-hr">
<h3 class="no-top-margin">Delete event</h3>
<div id="manage-delete-event">
Expand Down
Loading