<?php

/*
 * STUDENTS.CSV - Read Only
 * HISTORY.CSV - Read & Write
 */
 
function getStudents() {
    $students = fopen('students.csv', 'r');
    while(!feof($students)) {
        $line = fgets($students);
        echo $line;
    }
    fclose($students);
}

function getHistory() {
    $history = fopen("history.csv", "r");
    while(!feof($history)) {
        $line = fgets($history);
        echo $line . "|";
    }
    fclose($history);
}

function getShuffle() {
    $shuffle = fopen("shuffle.csv", "r");
    while(!feof($shuffle)) {
        $line = fgets($shuffle);
        echo $line . "|";
    }
    fclose($shuffle);
}

function setHistory($value) {
    $history = fopen("history.csv", "a");
    fputcsv($history, array($value));
    fclose($history);
}

if($_GET["data"] == "students") {
    getStudents();
} else if($_GET["data"] == "history") {
    getHistory();
} else if($_GET["data"] == "shuffle") {
    getShuffle();
} else {
    setHistory($_GET["data"]);
}

?>