summaryrefslogtreecommitdiff
path: root/lib/study
diff options
context:
space:
mode:
Diffstat (limited to 'lib/study')
-rw-r--r--lib/study/batch.php31
-rw-r--r--lib/study/batchreview.php23
-rw-r--r--lib/study/brresults.php35
-rw-r--r--lib/study/list.php37
-rw-r--r--lib/study/listadd.php13
-rw-r--r--lib/study/listrm.php16
6 files changed, 155 insertions, 0 deletions
diff --git a/lib/study/batch.php b/lib/study/batch.php
new file mode 100644
index 0000000..d8df526
--- /dev/null
+++ b/lib/study/batch.php
@@ -0,0 +1,31 @@
+<?php
+
+assert_redir(count($args) == 3, 'study');
+$batchid = intval($args[2]);
+
+$info = mysql_fetch_assoc(sql(
+ "SELECT list_study.id AS studyid, batches.name AS bname, ".
+ "lists.name AS lname, account.login AS uname, ".
+ "batch_study.id AS bsid, batches.id AS batchid, batches.json_data AS json_data ".
+ "FROM batches LEFT JOIN list_study ON list_study.list = batches.list AND list_study.user = " . $user['id'] . " " .
+ "LEFT JOIN batch_study ON batch_study.batch = $batchid AND batch_study.user = " . $user['id'] . " " .
+ "LEFT JOIN lists ON batches.list = lists.id ".
+ "LEFT JOIN account ON account.id = lists.owner ".
+ "WHERE batches.id = $batchid"));
+assert_error($info, "This batch does not exist.");
+assert_error($info['studyid'] != 0, "You are not studying this list.");
+
+if ($info["bsid"] == 0) {
+ sql("INSERT INTO batch_study(user, batch, last_review) VALUES(" . $user['id'] . ", $batchid, 0)");
+ $info['bsid'] = mysql_insert_id();
+}
+
+$reviews = array();
+$d = sql("SELECT results, score, date, batch FROM batch_review WHERE batch = $batchid AND user = " . $user['id'] ." ORDER BY date ASC");
+while ($r = mysql_fetch_assoc($d)) {
+ $reviews[] = '{"batch": ' . $r['batch'] . ', "date" : "' . $r['date'] . '", '.
+ '"score": ' . $r['score'] . ', "results": ' . $r['results'] . '}';
+}
+$reviews = '[' . implode(', ', $reviews) . ']';
+
+require("tpl/study/batch.php");
diff --git a/lib/study/batchreview.php b/lib/study/batchreview.php
new file mode 100644
index 0000000..8639193
--- /dev/null
+++ b/lib/study/batchreview.php
@@ -0,0 +1,23 @@
+<?php
+
+assert_redir(count($args) == 3, 'study');
+$batchid = intval($args[2]);
+
+$info = mysql_fetch_assoc(sql(
+ "SELECT list_study.id AS studyid, batches.name AS bname, ".
+ "lists.name AS lname, account.login AS uname, ".
+ "batch_study.id AS bsid, batches.id AS batchid, batches.json_data AS json_data ".
+ "FROM batches LEFT JOIN list_study ON list_study.list = batches.list AND list_study.user = " . $user['id'] . " " .
+ "LEFT JOIN batch_study ON batch_study.batch = $batchid AND batch_study.user = " . $user['id'] . " " .
+ "LEFT JOIN lists ON batches.list = lists.id ".
+ "LEFT JOIN account ON account.id = lists.owner ".
+ "WHERE batches.id = $batchid"));
+assert_error($info, "This batch does not exist.");
+assert_error($info['studyid'] != 0, "You are not studying this list.");
+
+if ($info["bsid"] == 0) {
+ sql("INSERT INTO batch_study(user, batch, last_review) VALUES(" . $user['id'] . ", $batchid, 0)");
+ $info['bsid'] = mysql_insert_id();
+}
+
+require("tpl/study/batch_review.php");
diff --git a/lib/study/brresults.php b/lib/study/brresults.php
new file mode 100644
index 0000000..b3e1068
--- /dev/null
+++ b/lib/study/brresults.php
@@ -0,0 +1,35 @@
+<?php
+
+assert_redir(count($args) == 3, 'study');
+$batchid = intval($args[2]);
+
+$info = mysql_fetch_assoc(sql(
+ "SELECT list_study.id AS studyid, ".
+ "batch_study.id AS bsid, batches.id AS batchid, batches.json_data AS json_data ".
+ "FROM batches LEFT JOIN list_study ON list_study.list = batches.list AND list_study.user = " . $user['id'] . " " .
+ "LEFT JOIN batch_study ON batch_study.batch = $batchid AND batch_study.user = " . $user['id'] . " " .
+ "WHERE batches.id = $batchid"));
+if (!($info)) {
+ echo "This batch does not exist";
+ die();
+}
+if (!($info['studyid'] != 0)) {
+ echo "You are not studying this list.";
+ die();
+}
+
+if ($info["bsid"] == 0) {
+ sql("INSERT INTO batch_study(user, batch, last_review) VALUES(" . $user['id'] . ", $batchid, 0)");
+ $info['bsid'] = mysql_insert_id();
+}
+
+if (isset($_POST['results']) && isset($_POST['score'])) {
+ sql("INSERT INTO batch_review(user, batch, results, score, date) ".
+ "VALUES(" . $user['id'] . ", $batchid, '" . escs(esca($_POST['results'])) . "', " . intval($_POST['score']) . ", NOW())");
+ sql("UPDATE batch_study SET last_review = " . mysql_insert_id() . " WHERE id = " . $info['bsid']);
+ echo 'Saved';
+} else {
+ echo 'Error';
+}
+
+die();
diff --git a/lib/study/list.php b/lib/study/list.php
new file mode 100644
index 0000000..2f0031f
--- /dev/null
+++ b/lib/study/list.php
@@ -0,0 +1,37 @@
+<?php
+
+assert_redir(count($args) == 3, 'study');
+$studyid = intval($args[2]);
+
+$study = mysql_fetch_assoc(sql(
+ "SELECT lists.id AS listid, lists.name AS listname, account.login AS listowner, ".
+ " list_study.user AS learn_user ".
+ "FROM list_study LEFT JOIN lists ON list_study.list = lists.id LEFT JOIN account ON account.id = lists.owner ".
+ "WHERE list_study.id = $studyid"));
+assert_error($study && $study['learn_user'] == $user['id'], "You are not at the right place here.");
+
+$filters = array(
+ "order" => array(
+ "name" => "name",
+ "lr_score" => "last score"
+ ),
+ "way" => $ord_ways,
+);
+$fdefaults = array(
+ "order" => "name",
+ "way" => "ASC",
+);
+
+$batches = array();
+$n = sql(
+ "SELECT batches.id AS id, batches.name AS name, ".
+ "batch_study.id AS bs_id, batch_review.date AS lr_date, batch_review.score AS lr_score ".
+ "FROM batches ".
+ "LEFT JOIN batch_study ON batch_study.batch = batches.id AND batch_study.user = " . $user['id'] . " " .
+ "LEFT JOIN batch_review ON batch_review.id = batch_study.last_review ".
+ "WHERE batches.list = " . $study['listid'] . " " .
+ "ORDER BY " . get_filter("order") . " " . get_filter("way")
+ );
+while ($b = mysql_fetch_assoc($n)) $batches[] = $b;
+
+require("tpl/study/list.php");
diff --git a/lib/study/listadd.php b/lib/study/listadd.php
new file mode 100644
index 0000000..536ab1d
--- /dev/null
+++ b/lib/study/listadd.php
@@ -0,0 +1,13 @@
+<?php
+
+assert_redir(count($args) == 3, 'list');
+$listid = intval($args[2]);
+$list = mysql_fetch_assoc(sql("SELECT id FROM lists WHERE id = $listid"));
+assert_error($list, "This list does not exist.");
+
+assert_error(!mysql_fetch_assoc(sql("SELECT id FROM list_study WHERE list = $listid AND user = " . $user['id'])),
+ "You are already studying this list.");
+
+sql("INSERT INTO list_study(user, list) VALUES(" . $user['id'] . ", $listid)");
+header("Location: list-study-".mysql_insert_id());
+die();
diff --git a/lib/study/listrm.php b/lib/study/listrm.php
new file mode 100644
index 0000000..a761e97
--- /dev/null
+++ b/lib/study/listrm.php
@@ -0,0 +1,16 @@
+<?php
+
+assert_redir(count($args) >= 3, 'study');
+$studyid = intval($args[2]);
+
+$study = mysql_fetch_assoc(sql(
+ "SELECT ".
+ " list_study.user AS learn_user ".
+ "FROM list_study ".
+ "WHERE list_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 list from your studies ? Your progress will not be lost.", "list-study-$studyid");
+sql("DELETE FROM list_study WHERE id = $studyid");
+header("Location: study");
+die();