- auto character encoding recognition and conversion to UTF-8 added

This commit is contained in:
Wiesner András 2024-03-18 09:13:03 +01:00
parent c5dbb9ff87
commit c3d008d4f5
4 changed files with 64 additions and 7 deletions

View File

@ -72,6 +72,8 @@ if (!get_autologin_state() || (($user_data["privilege"] !== PRIVILEGE_CREATOR) &
<td> <td>
<input type="button" id="download_challenges_btn" value="Letöltés CSV-ként" shown="false" <input type="button" id="download_challenges_btn" value="Letöltés CSV-ként" shown="false"
onclick="download_challenges()"> onclick="download_challenges()">
<input type="button" id="edit_challenges_btn" value="Szerkesztés"
onclick="edit_challenges()" shown="false">
<input type="button" value="Új feltöltése" id="show_game_file_upload" <input type="button" value="Új feltöltése" id="show_game_file_upload"
onclick="show_hide_gamefile_upload(true)"> onclick="show_hide_gamefile_upload(true)">
<input type="file" id="game_file" shown="false"> <input type="file" id="game_file" shown="false">

View File

@ -87,21 +87,43 @@ function get_all_game_data_by_contributor_nickname(string $nickname): array
return $game_headers; 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); $game_data = get_game($gameid);
if (count($game_data) === []) { 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 = []; $challenges = [];
// load filled CSV file // load filled CSV file
$f = fopen($csv_path, "r"); $f = fopen($csv_path, "r");
if (!$f) { // failed to open file if (!$f) { // failed to open file
return; return ["n" => 0, "encoding" => $encoding];
} }
while ($csvline = fgetcsv($f)) { while ($csvline = fgetcsv($f)) {
// skip empty lines
if (trim(implode("", $csvline)) === "") {
continue;
}
if (count($csvline) >= 3) { if (count($csvline) >= 3) {
$ch = [ $ch = [
"question" => $csvline[0], "question" => $csvline[0],
@ -122,6 +144,8 @@ function import_challenges_from_csv(string $csv_path, string $gameid)
// update game with game file present // update game with game file present
$game_data["game_file_present"] = true; $game_data["game_file_present"] = true;
update_game($game_data); update_game($game_data);
return ["n" => count($challenges), "encoding" => $encoding];
} }
function is_user_contributor_to_game(string $gameid, string $nickname): bool function is_user_contributor_to_game(string $gameid, string $nickname): bool

View File

@ -146,6 +146,8 @@ if (($privilege !== PRIVILEGE_CREATOR) && ($privilege !== PRIVILEGE_QUIZMASTER))
goto print_result; goto print_result;
} }
$requester_nickname = ($privilege === PRIVILEGE_QUIZMASTER) ? "*" : $nickname; // "*" means every game
switch ($action) { switch ($action) {
case "create_game": case "create_game":
case "update_game": case "update_game":
@ -207,7 +209,8 @@ switch ($action) {
// update game file if supplied // update game file if supplied
if (isset($_FILES["game_file"])) { 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; break;
case "get_all_game_headers": 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); $game_headers = get_all_game_data_by_contributor_nickname($requester_nickname);
foreach ($game_headers as &$game_header) { foreach ($game_headers as &$game_header) {
resolve_groupids($game_header["groups"]); resolve_groupids($game_header["groups"]);
@ -223,6 +225,15 @@ switch ($action) {
$result = json_encode($game_headers); $result = json_encode($game_headers);
} }
break; 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": case "delete_games":
{ {
$gameids = explode_list(trim($_REQUEST["ids"] ?: "")); $gameids = explode_list(trim($_REQUEST["ids"] ?: ""));

View File

@ -46,6 +46,7 @@ function create_edit_game(game = null) {
let download_challenges_btn = document.getElementById("download_challenges_btn"); let download_challenges_btn = document.getElementById("download_challenges_btn");
let show_game_file_upload_btn = document.getElementById("show_game_file_upload"); 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 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 groupF = document.getElementById("game_groups");
let time_limitedChk = document.getElementById("time_limited"); let time_limitedChk = document.getElementById("time_limited");
let time_limitF = document.getElementById("time_limit"); 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"]; let game_file_present = updating && game["game_file_present"];
if (game_file_present) { if (game_file_present) {
show(download_challenges_btn); 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); show_hide_gamefile_upload(false);
@ -107,7 +115,7 @@ function create_edit_game(game = null) {
contributors: contributorsF.value.trim(), contributors: contributorsF.value.trim(),
groups: groupF.value.trim(), groups: groupF.value.trim(),
properties: { 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 repeatable: updating ? repeatableChk.checked : false
} }
}; };
@ -118,10 +126,15 @@ function create_edit_game(game = null) {
action: updating ? "update_game" : "create_game", action: updating ? "update_game" : "create_game",
data: JSON.stringify(reqData) 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]; req["game_file"] = gameFileF.files[0];
} }
request(req).then(resp => { 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(); list_all_games();
}); });
hide("game_editor_window"); hide("game_editor_window");
@ -191,6 +204,13 @@ function handle_time_limit_chkbox() {
time_limitF.disabled = !time_limitedChk.checked; 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) { // function hint_all_groups(target_element_id) {
// const hintbox_insert_fn = (record) => { // const hintbox_insert_fn = (record) => {
// let targetF = document.getElementById(target_element_id); // let targetF = document.getElementById(target_element_id);