diff --git a/game_manager_frame.php b/game_manager_frame.php index e5dc3e7..e990caf 100644 --- a/game_manager_frame.php +++ b/game_manager_frame.php @@ -72,6 +72,8 @@ if (!get_autologin_state() || (($user_data["privilege"] !== PRIVILEGE_CREATOR) & + diff --git a/gamemgr.php b/gamemgr.php index b383802..d6e49bd 100644 --- a/gamemgr.php +++ b/gamemgr.php @@ -87,21 +87,43 @@ function get_all_game_data_by_contributor_nickname(string $nickname): array return $game_headers; } -function import_challenges_from_csv(string $csv_path, string $gameid) +const CSV_ENCODINGS = ["Windows-1252", "UTF-8"]; + +function import_challenges_from_csv(string $csv_path, string $gameid): array { $game_data = get_game($gameid); if (count($game_data) === []) { - return; + return ["n" => 0, "encoding" => ""]; } + // convert text encoding into UTF-8 + $data = file_get_contents($csv_path); + $encoding = "UNKNOWN"; + foreach (CSV_ENCODINGS as $enc) { // detect encoding + if (mb_check_encoding($data, $enc)) { + $encoding = $enc; + break; + } + } + + if ($encoding !== "UNKNOWN") { // if encoding has been detected successfully + $data = mb_convert_encoding($data, "UTF-8", $encoding); + file_put_contents($csv_path, $data); + } + + // load challenges $challenges = []; // load filled CSV file $f = fopen($csv_path, "r"); if (!$f) { // failed to open file - return; + return ["n" => 0, "encoding" => $encoding]; } while ($csvline = fgetcsv($f)) { + // skip empty lines + if (trim(implode("", $csvline)) === "") { + continue; + } if (count($csvline) >= 3) { $ch = [ "question" => $csvline[0], @@ -122,6 +144,8 @@ function import_challenges_from_csv(string $csv_path, string $gameid) // update game with game file present $game_data["game_file_present"] = true; update_game($game_data); + + return ["n" => count($challenges), "encoding" => $encoding]; } function is_user_contributor_to_game(string $gameid, string $nickname): bool diff --git a/interface.php b/interface.php index 7fc3f24..0076d14 100644 --- a/interface.php +++ b/interface.php @@ -146,6 +146,8 @@ if (($privilege !== PRIVILEGE_CREATOR) && ($privilege !== PRIVILEGE_QUIZMASTER)) goto print_result; } +$requester_nickname = ($privilege === PRIVILEGE_QUIZMASTER) ? "*" : $nickname; // "*" means every game + switch ($action) { case "create_game": case "update_game": @@ -207,7 +209,8 @@ switch ($action) { // update game file if supplied if (isset($_FILES["game_file"])) { - import_challenges_from_csv($_FILES["game_file"]["tmp_name"], $data["_id"]); + $challenge_import_status = import_challenges_from_csv($_FILES["game_file"]["tmp_name"], $data["_id"]); + $result = json_encode($challenge_import_status); } } } @@ -215,7 +218,6 @@ switch ($action) { break; case "get_all_game_headers": { - $requester_nickname = ($privilege === PRIVILEGE_QUIZMASTER) ? "*" : $nickname; // "*" means every game $game_headers = get_all_game_data_by_contributor_nickname($requester_nickname); foreach ($game_headers as &$game_header) { resolve_groupids($game_header["groups"]); @@ -223,6 +225,15 @@ switch ($action) { $result = json_encode($game_headers); } break; + case "get_challenges": + { + $gameid = ($_REQUEST["gameid"] ?: ""); + $game_data = get_game($gameid); + if ((count($game_data) > 0) && (($requester_nickname === "*") || (is_user_contributor_to_game($gameid, $requester_nickname)))) { + $result = file_get_contents(get_game_file_by_gameid($gameid)); + } + } + break; case "delete_games": { $gameids = explode_list(trim($_REQUEST["ids"] ?: "")); diff --git a/js/gamemgr.js b/js/gamemgr.js index f53e16d..d02b835 100644 --- a/js/gamemgr.js +++ b/js/gamemgr.js @@ -46,6 +46,7 @@ function create_edit_game(game = null) { let download_challenges_btn = document.getElementById("download_challenges_btn"); let show_game_file_upload_btn = document.getElementById("show_game_file_upload"); let cancel_game_file_upload_btn = document.getElementById("cancel_game_file_upload"); + let edit_challenges_btn = document.getElementById("edit_challenges_btn"); let groupF = document.getElementById("game_groups"); let time_limitedChk = document.getElementById("time_limited"); let time_limitF = document.getElementById("time_limit"); @@ -90,6 +91,13 @@ function create_edit_game(game = null) { let game_file_present = updating && game["game_file_present"]; if (game_file_present) { show(download_challenges_btn); + show(edit_challenges_btn); + edit_challenges_btn.onclick = () => { + edit_challenges(game); + }; + } else { + hide(download_challenges_btn); + hide(edit_challenges_btn); } show_hide_gamefile_upload(false); @@ -107,7 +115,7 @@ function create_edit_game(game = null) { contributors: contributorsF.value.trim(), groups: groupF.value.trim(), properties: { - time_limit: updating ? (time_limitedChk.checked ? time_to_seconds(time_limitF.value): 0) : 0, + time_limit: updating ? (time_limitedChk.checked ? time_to_seconds(time_limitF.value) : 0) : 0, repeatable: updating ? repeatableChk.checked : false } }; @@ -118,10 +126,15 @@ function create_edit_game(game = null) { action: updating ? "update_game" : "create_game", data: JSON.stringify(reqData) }; - if (gameFileF.files.length > 0) { // append game file if selected + let uploading_game_file = gameFileF.files.length > 0; + if (uploading_game_file) { // append game file if selected req["game_file"] = gameFileF.files[0]; } request(req).then(resp => { + if (uploading_game_file) { + let file_upload_status = JSON.parse(resp); + alert(`Importálás eredménye:\n${file_upload_status["n"]} kérdés, ${file_upload_status["encoding"]} szövegkódolással`); + } list_all_games(); }); hide("game_editor_window"); @@ -191,6 +204,13 @@ function handle_time_limit_chkbox() { time_limitF.disabled = !time_limitedChk.checked; } +function edit_challenges(game) { + let req = {action: "get_challenges", gameid: game["_id"]}; + request(req).then(resp => { + console.log(JSON.parse(resp)); + }); +} + // function hint_all_groups(target_element_id) { // const hintbox_insert_fn = (record) => { // let targetF = document.getElementById(target_element_id);