summaryrefslogtreecommitdiff
path: root/lib/study
diff options
context:
space:
mode:
Diffstat (limited to 'lib/study')
-rw-r--r--lib/study/deck.php89
-rw-r--r--lib/study/deckadd.php14
-rw-r--r--lib/study/deckrm.php17
-rw-r--r--lib/study/setcard.php37
-rw-r--r--lib/study/setrate.php19
5 files changed, 170 insertions, 6 deletions
diff --git a/lib/study/deck.php b/lib/study/deck.php
index d7f8e9f..9b9c71f 100644
--- a/lib/study/deck.php
+++ b/lib/study/deck.php
@@ -1,5 +1,88 @@
<?php
-$title = "olol";
-$message = 'Placeholder. Go to <a href="deck">study decks</a>.';
-require("tpl/general/empty.php");
+assert_redir(count($args) == 3, 'study');
+$studyid = intval($args[2]);
+
+$study = mysql_fetch_assoc(sql(
+ "SELECT decks.id AS deckid, decks.name AS deckname, account.login AS deckowner, ".
+ " deck_study.learn_rate AS learn_rate, deck_study.user AS learn_user, deck_study.need_check AS need_check, deck_study.last_card AS last_card ".
+ "FROM deck_study LEFT JOIN decks ON deck_study.deck = decks.id LEFT JOIN account ON account.id = decks.owner ".
+ "WHERE deck_study.id = $studyid"));
+assert_error($study && $study['learn_user'] == $user['id'], "You are not at the right place here.");
+
+if ($study['need_check']) {
+ /* Algorithm :
+ - Check that deck_study.last_card = max(card_study.card.number)
+ - Check that foreach (card where card.deck = deck_study.deck && card.number < deck_study.last_card),
+ exists corresponding card_study, if not exist, create level 0 (skipped)
+ */
+ $mcn = mysql_fetch_assoc(sql("SELECT MAX(cards.number) AS mcn FROM deck_study ".
+ "LEFT JOIN card_study ON card_study.deck_study = deck_study.id LEFT JOIN cards ON cards.id = card_study.card ".
+ "WHERE deck_study.id = $studyid"));
+ $hasmoar = false;
+ if ($mcn) {
+ $mcn = $mcn['mcn'];
+ if ($study['last_card'] != $mcn) sql('UPDATE deck_study SET last_card = ' . $mcn . ' WHERE id = ' . $studyid);
+
+ $cs = array();
+ for($i = 1; $i < $mcn; $i++) $cs[$i] = 0;
+
+ $d = sql("SELECT cards.number AS n, card_study.id AS i FROM card_study LEFT JOIN cards ON cards.id = card_study.card ".
+ "WHERE card_study.deck_study = $studyid");
+ while($r = mysql_fetch_assoc($d)) $cs[$r['n']] = $r['i'];
+
+ for ($i = 1; $i < $mcn; $i++) {
+ if ($cs[$i] == 0) {
+ $n = mysql_fetch_assoc(sql("SELECT cards.id AS id FROM deck_study LEFT JOIN cards ON cards.deck = deck_study.deck AND cards.number = $i ".
+ "WHERE deck_study.id = $studyid"));
+ assert_error($n, "Fucking deck inconsistency.");
+ sql("INSERT INTO card_study(deck_study, card, level, next_review) ".
+ "VALUES($studyid, " . $n['id'] . ", 0, ADDDATE(CURDATE(), INTERVAL 999999 DAY))");
+ $hasmoar = true;
+ }
+ }
+ }
+ sql("UPDATE deck_study SET need_check = 0 WHERE id = $studyid");
+ $message = "This deck has been checked. " . ($hasmoar ? "Some cards you hadn't studied before were added to your skipped list." : "");
+}
+
+
+$load = mysql_fetch_assoc(sql("SELECT SUM(4 - level) AS l FROM card_study WHERE deck_study = $studyid AND level < 4 AND level > 0"));
+$load = intval($load['l']);
+
+if ($load < $study['learn_rate']) {
+ $next_card = mysql_fetch_assoc(sql("SELECT * FROM cards WHERE deck = " . $study['deckid'] . " AND number = " . ($study['last_card'] + 1)));
+}
+
+
+
+$filters = array(
+ "what" => array(
+ "level > 0 AND next_review <= CURDATE()" => "study today",
+ "level > 0 AND level < 4" => "learning",
+ "level > 0" => "all except skipped",
+ "level = 0" => "skipped",
+ ),
+ "order" => array(
+ "level" => "level",
+ "number" => "card number",
+ ),
+ "way" => $ord_ways,
+);
+$fdefaults = array(
+ "what" => "level > 0 AND level < 4",
+ "order" => "level",
+ "way" => "ASC",
+);
+
+$study_cards = array();
+$s = sql(
+ "SELECT cards.id AS id, cards.name AS name, cards.text_html AS text, cards.number AS number, ".
+ "card_study.level AS level, card_study.next_review <= CURDATE() AS must_study ".
+ "FROM card_study LEFT JOIN cards ON card_study.card = cards.id WHERE deck_study = $studyid AND " . get_filter("what") .
+ " ORDER BY " . get_filter("order") . " " . get_filter('way'));
+while ($ss = mysql_fetch_assoc($s)) $study_cards[] = $ss;
+
+
+
+require("tpl/study/deck.php");
diff --git a/lib/study/deckadd.php b/lib/study/deckadd.php
index d7f8e9f..b95648c 100644
--- a/lib/study/deckadd.php
+++ b/lib/study/deckadd.php
@@ -1,5 +1,13 @@
<?php
-$title = "olol";
-$message = 'Placeholder. Go to <a href="deck">study decks</a>.';
-require("tpl/general/empty.php");
+assert_redir(count($args) == 3, 'deck');
+$deckid = intval($args[2]);
+$deck = mysql_fetch_assoc(sql("SELECT id FROM decks WHERE id = $deckid"));
+assert_error($deck, "This deck does not exist.");
+
+assert_error(!mysql_fetch_assoc(sql("SELECT id FROM deck_study WHERE deck = $deckid AND user = " . $user['id'])),
+ "You are already studying this deck.");
+
+sql("INSERT INTO deck_study(user, deck) VALUES(" . $user['id'] . ", $deckid)");
+header("Location: deck-study-".mysql_insert_id());
+die();
diff --git a/lib/study/deckrm.php b/lib/study/deckrm.php
new file mode 100644
index 0000000..6b4e803
--- /dev/null
+++ b/lib/study/deckrm.php
@@ -0,0 +1,17 @@
+<?php
+
+assert_redir(count($args) >= 3, 'study');
+$studyid = intval($args[2]);
+
+$study = mysql_fetch_assoc(sql(
+ "SELECT ".
+ " deck_study.user AS learn_user ".
+ "FROM deck_study ".
+ "WHERE deck_study.id = $studyid"));
+assert_error($study && $study['learn_user'] == $user['id'], "You are not at the right place here.");
+
+token_validate("Do you really want to remove this deck from your studies ? ALL YOUR PROGRESS WILL BE LOST!", "deck-study-$studyid");
+sql("DELETE FROM card_study WHERE deck_study = $studyid");
+sql("DELETE FROM deck_study WHERE id = $studyid");
+header("Location: study");
+die();
diff --git a/lib/study/setcard.php b/lib/study/setcard.php
new file mode 100644
index 0000000..13f3fc0
--- /dev/null
+++ b/lib/study/setcard.php
@@ -0,0 +1,37 @@
+<?php
+
+assert_redir(count($args) == 4, 'study');
+$cardid = intval($args[2]);
+
+$info = mysql_fetch_assoc(sql(
+ "SELECT deck_study.id AS studyid, deck_study.last_card AS last_card, deck_study.need_check AS need_check, ".
+ "card_study.id AS csid, cards.id AS cardid, cards.number AS cardnumber ".
+ "FROM cards LEFT JOIN deck_study ON deck_study.deck = cards.deck AND deck_study.user = " . $user['id'] . " ".
+ "LEFT JOIN card_study ON card_study.deck_study = deck_study.id AND card_study.card = $cardid ".
+ "WHERE cards.id = $cardid"
+ ));
+assert_error($info, "This card does not exist.");
+
+$studyid = intval($info['studyid']);
+assert_error($studyid > 0, "You are not studying this deck.");
+
+assert_error($info['need_check'] == 0,
+ 'This deck needs checking first. Please return <a href="deck-study-'.$studyid.'">here</a> first, it should do it.');
+
+assert_error($info['cardnumber'] <= $info['last_card'] + 1, "You must first add previous cards to your study list.");
+
+$level = intval($args[3]);
+assert_error($level >= 0 && $level <= 7, "That level is invalid.");
+$intervals = array(999999, 1, 3, 5, 8, 16, 24, 999999);
+
+if ($info['cardnumber'] == $info['last_card'] + 1) {
+ sql("INSERT INTO card_study(deck_study, card, level, next_review) ".
+ "VALUES($studyid, $cardid, $level, ADDDATE(CURDATE(), INTERVAL " . $intervals[$level] . " DAY))");
+ sql("UPDATE deck_study SET last_card = last_card + 1 WHERE id = $studyid");
+} else {
+ assert_error($info['csid'] > 0, "Deck is inconsistent.");
+ sql("UPDATE card_study SET level = $level, next_review = ADDDATE(CURDATE(), INTERVAL " . $intervals[$level] . " DAY) WHERE id = " . $info['csid']);
+}
+
+header("Location: deck-study-$studyid");
+die();
diff --git a/lib/study/setrate.php b/lib/study/setrate.php
new file mode 100644
index 0000000..f893eb9
--- /dev/null
+++ b/lib/study/setrate.php
@@ -0,0 +1,19 @@
+<?php
+
+assert_redir(count($args) >= 3, 'study');
+$studyid = intval($args[2]);
+
+$study = mysql_fetch_assoc(sql(
+ "SELECT decks.id AS deckid, decks.name AS deckname, account.login AS deckowner, ".
+ " deck_study.learn_rate AS learn_rate, deck_study.user AS learn_user ".
+ "FROM deck_study LEFT JOIN decks ON deck_study.deck = decks.id LEFT JOIN account ON account.id = decks.owner ".
+ "WHERE deck_study.id = $studyid"));
+assert_error($study && $study['learn_user'] == $user['id'], "You are not at the right place here.");
+
+if (isset($args[3]) && ($rate = intval($args[3])) > 0) {
+ sql("UPDATE deck_study SET learn_rate = $rate WHERE id = $studyid");
+ header("Location: deck-study-$studyid");
+ die();
+}
+
+include ("tpl/study/setrate.php");