From 8ef49a3347c5d06bcaf5db52e39d2aa48d6ddc03 Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Wed, 20 Dec 2023 13:14:26 +0100 Subject: [PATCH] Moves the card sorting logic from index.php into the datavbase to allow easy modification based on what type of data is being served from the db. --- README.md | 2 +- db.sql | 22 +++++++++++++--------- index.php | 33 +++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ad84609..f435507 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This is a single-file php application for training writing using flash cards sorted into boxes. The UI is optimized for mobile viewing. Added cards are automatically sorted into different boxes based on successfully typing in the correct answer or not. -The provided database contains cards for learning German for english speakers but it can be adapted to learn anything. Editing is also possible throug the UI (except for word classes) so adding an empty database is also an option. Please tweak db.sql before creating the sqlite3 database. +The provided database contains cards for learning German for english speakers but it can be adapted to learn anything. Editing of words is also possible throug the UI adding an empty database is also an option. To change the flow of boxes, add/remove boxes or add/remove word classes, please tweak db.sql before creating the sqlite3 database. ## Why build it? I wrote this small application to help my wife learn german but then thought of publishing it in case someone else is looking for a simple solution. I know there are plenty of language learning programs out there but i wanted something offline and with boxes the cards are sorted into the traditional way. Contributions and improvements are most welcome. diff --git a/db.sql b/db.sql index 4a7dd7e..05a5559 100644 --- a/db.sql +++ b/db.sql @@ -2,16 +2,20 @@ PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE boxes ( id INTEGER PRIMARY KEY, - name TEXT NOT NULL + name TEXT NOT NULL, + class_id_on_correct INTEGER NOT NULL, + class_id_on_incorrect INTEGER NOT NULL, + FOREIGN KEY(class_id_on_correct) REFERENCES boxes(id) ON UPDATE CASCADE, + FOREIGN KEY(class_id_on_incorrect) REFERENCES boxes(id) ON UPDATE CASCADE ); -INSERT INTO boxes VALUES(0,'Failed Thrice'); -INSERT INTO boxes VALUES(1,'Failed Twice'); -INSERT INTO boxes VALUES(2,'Failed Once'); -INSERT INTO boxes VALUES(3,'New'); -INSERT INTO boxes VALUES(4,'Correct Once'); -INSERT INTO boxes VALUES(5,'Correct Twice'); -INSERT INTO boxes VALUES(6,'Correct Thrice'); -INSERT INTO boxes VALUES(7,'Mastered'); +INSERT INTO boxes VALUES(0,'Failed Thrice',4,0); +INSERT INTO boxes VALUES(1,'Failed Twice',4,0); +INSERT INTO boxes VALUES(2,'Failed Once',4,1); +INSERT INTO boxes VALUES(3,'New',4,2); +INSERT INTO boxes VALUES(4,'Correct Once',5,2); +INSERT INTO boxes VALUES(5,'Correct Twice',6,2); +INSERT INTO boxes VALUES(6,'Correct Thrice',7,2); +INSERT INTO boxes VALUES(7,'Mastered',7,2); CREATE TABLE classes ( id INTEGER PRIMARY KEY, name TEXT NOT NULL diff --git a/index.php b/index.php index 1fa9c0b..32b04a1 100644 --- a/index.php +++ b/index.php @@ -64,11 +64,19 @@ class DB { return $this->db->query("SELECT * FROM boxes"); } - public function getBoxNameByID($boxID) { + public function getBoxNameByBoxID($boxID) { return $this->db->query("SELECT name FROM boxes WHERE id='".$boxID."' LIMIT 1")->fetchColumn(); } - public function getClassNameByID($classID) { + public function getNewBoxIDOnCorrectByBoxID($boxID) { + return $this->db->query("SELECT class_id_on_correct FROM boxes WHERE id='".$boxID."' LIMIT 1")->fetchColumn(); + } + + public function getNewBoxIDOnIncorrectByBoxID($boxID) { + return $this->db->query("SELECT class_id_on_incorrect FROM boxes WHERE id='".$boxID."' LIMIT 1")->fetchColumn(); + } + + public function getClassNameByClassID($classID) { return $this->db->query("SELECT name FROM classes WHERE id='".$classID."' LIMIT 1")->fetchColumn(); } @@ -87,7 +95,7 @@ class DB { } } - public function getCardByID($card_id) { + public function getCardByCardID($card_id) { $res = $this->db->query("SELECT * FROM cards WHERE id = '" . $card_id . "' LIMIT 1")->fetchAll(); if (count($res) > 0) { $row = $res[0]; @@ -107,7 +115,7 @@ class DB { } public function updateCard($card) { - $origCard = $this->getCardByID($card->id); + $origCard = $this->getCardByCardID($card->id); if($origCard->boxID != $card->boxID) { $res = $this->db->query("UPDATE cards SET box_id='".$card->boxID."' WHERE id='".$card->id."'"); } @@ -157,7 +165,7 @@ class App { /* if($this->selectedCard != null) { // Give the user some info... - $this->message = $this->selectedCard->front.' | '.$this->db->getClassNameByID($this->selectedCard->classID); + $this->message = $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID); } */ } @@ -180,25 +188,30 @@ class App { $this->message = 'Correct'; } else { - $this->message = 'Almost Correct'; + $this->message = 'Correct spelling but wrong upper/lower case'; } - + /* if($this->selectedCard->boxID >= 0 && $this->selectedCard->boxID < 3) { $this->selectedCard->boxID = 4; } else if($this->selectedCard->boxID >= 3 && $this->selectedCard->boxID < $this->db->getNumBoxes()-1) { $this->selectedCard->boxID += 1; } + */ + $this->selectedCard->boxID = $this->db->getNewBoxIDOnCorrectByBoxID($this->selectedCard->boxID); } //Answer wrong: else { $this->message = 'Wrong - Correct answer:
'.$this->selectedCard->back; + /* if($this->selectedCard->boxID > 0 && $this->selectedCard->boxID <= 3) { $this->selectedCard->boxID -= 1; } else if($this->selectedCard->boxID > 3 && $this->selectedCard->boxID < $this->db->getNumBoxes()) { $this->selectedCard->boxID = 2; } + */ + $this->selectedCard->boxID = $this->db->getNewBoxIDOnIncorrectByBoxID($this->selectedCard->boxID); } $this->db->updateCard($this->selectedCard); $this->testInput = ''; @@ -213,7 +226,7 @@ class App { private function renderTest() { echo '
' . "\n"; echo '
' . $this->message . "
\n"; - echo '
'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByID($this->selectedCard->classID))."
\n"; + echo '
'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID))."
\n"; echo '
' . "\n"; echo 'testInput == null ? '' : ' value="' . $this->testInput . '"') @@ -281,7 +294,7 @@ class App { if (isset($_REQUEST['card_id']) && $_REQUEST['card_id'] >= 0) { - $this->selectedCard = $this->db->getCardByID($_REQUEST['card_id']); + $this->selectedCard = $this->db->getCardByCardID($_REQUEST['card_id']); } if (isset($_REQUEST['check_case']) && $_REQUEST['check_case'] == 1) @@ -313,7 +326,7 @@ class App { $card = new Card($this->selectedCard->id, $this->selectedCard->boxID, $_REQUEST['class_id'], trim($_REQUEST['card_front']), trim($_REQUEST['card_back'])); $this->db->updateCard($card); // Reload the updated card - $this->selectedCard = $this->db->getCardByID($card->id); + $this->selectedCard = $this->db->getCardByCardID($card->id); } // Delete a card from the database else if (isset($_REQUEST['action_delete']) &&