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.
This commit is contained in:
1 parent
0ac54e0bf5
commit
8ef49a3347
3 files changed
+37
-20
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
This is a single-file php application for training writing using flash cards sorted into boxes.
|
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 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?
|
## 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.
|
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.
|
||||||
|
|||||||
@@ -2,16 +2,20 @@ PRAGMA foreign_keys=OFF;
|
|||||||
BEGIN TRANSACTION;
|
BEGIN TRANSACTION;
|
||||||
CREATE TABLE boxes (
|
CREATE TABLE boxes (
|
||||||
id INTEGER PRIMARY KEY,
|
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(0,'Failed Thrice',4,0);
|
||||||
INSERT INTO boxes VALUES(1,'Failed Twice');
|
INSERT INTO boxes VALUES(1,'Failed Twice',4,0);
|
||||||
INSERT INTO boxes VALUES(2,'Failed Once');
|
INSERT INTO boxes VALUES(2,'Failed Once',4,1);
|
||||||
INSERT INTO boxes VALUES(3,'New');
|
INSERT INTO boxes VALUES(3,'New',4,2);
|
||||||
INSERT INTO boxes VALUES(4,'Correct Once');
|
INSERT INTO boxes VALUES(4,'Correct Once',5,2);
|
||||||
INSERT INTO boxes VALUES(5,'Correct Twice');
|
INSERT INTO boxes VALUES(5,'Correct Twice',6,2);
|
||||||
INSERT INTO boxes VALUES(6,'Correct Thrice');
|
INSERT INTO boxes VALUES(6,'Correct Thrice',7,2);
|
||||||
INSERT INTO boxes VALUES(7,'Mastered');
|
INSERT INTO boxes VALUES(7,'Mastered',7,2);
|
||||||
CREATE TABLE classes (
|
CREATE TABLE classes (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name TEXT NOT NULL
|
name TEXT NOT NULL
|
||||||
|
|||||||
@@ -64,11 +64,19 @@ class DB {
|
|||||||
return $this->db->query("SELECT * FROM boxes");
|
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();
|
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();
|
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();
|
$res = $this->db->query("SELECT * FROM cards WHERE id = '" . $card_id . "' LIMIT 1")->fetchAll();
|
||||||
if (count($res) > 0) {
|
if (count($res) > 0) {
|
||||||
$row = $res[0];
|
$row = $res[0];
|
||||||
@@ -107,7 +115,7 @@ class DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function updateCard($card) {
|
public function updateCard($card) {
|
||||||
$origCard = $this->getCardByID($card->id);
|
$origCard = $this->getCardByCardID($card->id);
|
||||||
if($origCard->boxID != $card->boxID) {
|
if($origCard->boxID != $card->boxID) {
|
||||||
$res = $this->db->query("UPDATE cards SET box_id='".$card->boxID."' WHERE id='".$card->id."'");
|
$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) {
|
if($this->selectedCard != null) {
|
||||||
// Give the user some info...
|
// 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';
|
$this->message = 'Correct';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->message = 'Almost Correct';
|
$this->message = 'Correct spelling but wrong upper/lower case';
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
if($this->selectedCard->boxID >= 0 && $this->selectedCard->boxID < 3) {
|
if($this->selectedCard->boxID >= 0 && $this->selectedCard->boxID < 3) {
|
||||||
$this->selectedCard->boxID = 4;
|
$this->selectedCard->boxID = 4;
|
||||||
}
|
}
|
||||||
else if($this->selectedCard->boxID >= 3 && $this->selectedCard->boxID < $this->db->getNumBoxes()-1) {
|
else if($this->selectedCard->boxID >= 3 && $this->selectedCard->boxID < $this->db->getNumBoxes()-1) {
|
||||||
$this->selectedCard->boxID += 1;
|
$this->selectedCard->boxID += 1;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
$this->selectedCard->boxID = $this->db->getNewBoxIDOnCorrectByBoxID($this->selectedCard->boxID);
|
||||||
}
|
}
|
||||||
//Answer wrong:
|
//Answer wrong:
|
||||||
else {
|
else {
|
||||||
$this->message = 'Wrong - Correct answer:<br>'.$this->selectedCard->back;
|
$this->message = 'Wrong - Correct answer:<br>'.$this->selectedCard->back;
|
||||||
|
/*
|
||||||
if($this->selectedCard->boxID > 0 && $this->selectedCard->boxID <= 3) {
|
if($this->selectedCard->boxID > 0 && $this->selectedCard->boxID <= 3) {
|
||||||
$this->selectedCard->boxID -= 1;
|
$this->selectedCard->boxID -= 1;
|
||||||
}
|
}
|
||||||
else if($this->selectedCard->boxID > 3 && $this->selectedCard->boxID < $this->db->getNumBoxes()) {
|
else if($this->selectedCard->boxID > 3 && $this->selectedCard->boxID < $this->db->getNumBoxes()) {
|
||||||
$this->selectedCard->boxID = 2;
|
$this->selectedCard->boxID = 2;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
$this->selectedCard->boxID = $this->db->getNewBoxIDOnIncorrectByBoxID($this->selectedCard->boxID);
|
||||||
}
|
}
|
||||||
$this->db->updateCard($this->selectedCard);
|
$this->db->updateCard($this->selectedCard);
|
||||||
$this->testInput = '';
|
$this->testInput = '';
|
||||||
@@ -213,7 +226,7 @@ class App {
|
|||||||
private function renderTest() {
|
private function renderTest() {
|
||||||
echo '<div class="test_view">' . "\n";
|
echo '<div class="test_view">' . "\n";
|
||||||
echo '<div class="std_row">' . $this->message . "</div>\n";
|
echo '<div class="std_row">' . $this->message . "</div>\n";
|
||||||
echo '<div class="std_row">'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByID($this->selectedCard->classID))."</div>\n";
|
echo '<div class="std_row">'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID))."</div>\n";
|
||||||
echo '<form method="POST">' . "\n";
|
echo '<form method="POST">' . "\n";
|
||||||
echo '<input type="text" name="test_input"'
|
echo '<input type="text" name="test_input"'
|
||||||
.($this->testInput == null ? '' : ' value="' . $this->testInput . '"')
|
.($this->testInput == null ? '' : ' value="' . $this->testInput . '"')
|
||||||
@@ -281,7 +294,7 @@ class App {
|
|||||||
if (isset($_REQUEST['card_id']) &&
|
if (isset($_REQUEST['card_id']) &&
|
||||||
$_REQUEST['card_id'] >= 0)
|
$_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']) &&
|
if (isset($_REQUEST['check_case']) &&
|
||||||
$_REQUEST['check_case'] == 1)
|
$_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']));
|
$card = new Card($this->selectedCard->id, $this->selectedCard->boxID, $_REQUEST['class_id'], trim($_REQUEST['card_front']), trim($_REQUEST['card_back']));
|
||||||
$this->db->updateCard($card);
|
$this->db->updateCard($card);
|
||||||
// Reload the updated 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
|
// Delete a card from the database
|
||||||
else if (isset($_REQUEST['action_delete']) &&
|
else if (isset($_REQUEST['action_delete']) &&
|
||||||
|
|||||||
Reference in new issue
Block a user