-
Notifications
You must be signed in to change notification settings - Fork 20
/
ReviewsPeer.php
executable file
·1174 lines (1023 loc) · 33.7 KB
/
ReviewsPeer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Rtk Kanji Flashcards Peer.
*
* This table stores the review status of all kanji flashcards for all users.
*
* Methods:
* getFlashcardData($userId, $ucsId)
* getFlashcardsByIndex($userId, $filter = null)
* getFlashcardCount()
* getKnownKanji()
* getTodayCount()
* getCountExpired()
* getCountUntested()
* getLeitnerBoxCounts()
* getReviewedFlashcardCount()
* getTotalReviews()
* getMostRecentReviewTimeStamp()
* getSelectForDetailedList()
* getSelectForExport()
* getFlashcardsForReview()
* getHeisigProgressCount()
* getProgressChartData()
*
* getRestudyKanjiCount($userId)
* getRestudyKanjiListSelect($userId)
*
* hasFlashcard($userId, $ucsId)
* isFailedCard($userId, $ucsId)
*
* putFlashcardData($id, $oData)
* failFlashcard($userId, $ucsId)
*
* addSelection()
* deleteSelection()
* filterExistingCards($userId, array $cards)
*
* Private:
* addFlashcards() Add/delete at once with a SQL statement.
* deleteFlashcards()
*
* Helpers:
* filterByRtk() Applies framenum filter to the select object.
* filterByUserId() Applies userid filter to the select object.
*
*
* @author Fabrice Denis
*/
class ReviewsPeer extends coreDatabaseTable
{
protected $tableName = 'reviews';
/**
* This function must be copied in each peer class.
* @return self
*/
public static function getInstance()
{
return coreDatabaseTable::_getInstance(__CLASS__);
}
/**
* Returns flashcard status for given user and character id.
*
* @param int $userId
* @param int $ucsId
* @return array|false Row data
*/
public static function getFlashcardData(int $userId, int $ucsId)
{
assert($ucsId > 0x3000);
$select = self::getInstance()->select([
'*',
'ts_lastreview' => 'UNIX_TIMESTAMP(lastreview)'
])
->where('ucs_id = ?', $ucsId);
$select = self::filterByUserId($select, $userId);
$select->query();
$db = self::getInstance()->getDb();
return $db->fetch();
}
/**
* Return user's kanji card data for use by the front end. Only data for
* existing flashcards is returned! (no empty/default card values)
*
* Optionally filter the returned card data to a set of UCS codes.
*
* @param int $userId
* @param int[]|null $ucsIds a set of flashcard ids, null for all RTK1 cards
* @return array
*/
public static function getUserKanjiCardsJS(int $userId, ?array $ucsIds = null)
{
$select = self::getInstance()->select([
'ucs' => 'reviews.ucs_id',
'box' => 'leitnerbox',
'tot' => 'totalreviews',
]);
$select = self::filterByUserId($select, $userId);
if ($ucsIds !== null) {
$select = KanjisPeer::joinLeftUsingUCS($select);
$select->whereIn('ucs_id', $ucsIds);
}
else
{
// For Old Editions, make sure to include the range #3008-3030 so we can
// show Lesson 58 (RTK Supplement) on the Progress Chart.
// FIXME? includes RTK3 kanjis (lesson 57) which we don't currently display.
$select = self::filterByRtk($select, 'rtk1+3');
}
// DBG::out($select);exit;
$rows = self::getInstance()->getDb()->fetchAll($select);
$cards = [];
// format the data for use as `new Map(data)`
foreach ($rows as $row) {
$ucs = (int) $row['ucs'];
// Leitner box
$box = (int) $row['box'];
// is it a new card? (use 0/1 as boolean shorter JSON output)
$new = (int) ($row['tot'] === '0' && $box === 1);
$cards[] = [
$ucs, [
'ucs' => $ucs,
'box' => $box,
'new' => $new,
]
];
}
return $cards;
}
/**
* Returns true if user has a flashcard for given character UCS code.
*
* @param int $userId
* @param int $ucsId
*
* @return bool
*/
public static function hasFlashcard($userId, $ucsId)
{
$count = self::getInstance()->count('userid = ? AND ucs_id = ?', [$userId, $ucsId]);
return (bool)$count;
}
/**
* Checks if a flashcard is "failed" (in the red stack/box 1)
*
* @param int $userId
* @param int $ucsId UCS-2 code value.
*
* @return boolean
*/
public static function isFailedCard($userId, $ucsId)
{
$cardData = self::getFlashcardData($userId, $ucsId);
return (
is_array($cardData)
&& $cardData['leitnerbox'] == 1
&& $cardData['totalreviews'] > 0
);
}
/**
* Returns an array of flashcard ids in the user's deck, as *indexes*.
*
* Note: if filter is not used, cards outside of the active sequence will be
* returned, which have an *extended frame number* (UCS codes).
*
* @param int $userId
* @param string $filter See filterByRtk()
*
* @return array Array of frame numbers, empty array if no cards
*/
public static function getFlashcardsByIndex($userId, $filter = '')
{
$select = self::getInstance()->select(['seq_nr' => rtkIndex::getSqlCol()]);
// join Kanjis table just once
if ($filter === '')
{
$select = KanjisPeer::joinLeftUsingUCS($select);
}
else
{
$select = self::filterByRtk($select, $filter);
}
$select = self::filterByUserId($select, $userId);
return self::getInstance()->getDb()->fetchCol($select);
}
/**
* Do a COUNT(*) on the select object, return integer result.
*
* @param int $userId User id.
* @param coreDatabaseSelect $select Select object with where clause(s) applied.
*
* @return int
*/
private static function _getFlashcardCount($userId, $select = null)
{
$db = self::getInstance()->getDb();
if (is_null($select))
{
$select = self::getInstance()->select();
}
$select->columns(['count' => 'COUNT(*)']);
$select = self::filterByUserId($select, $userId);
// DBG::printr($select->__toString());exit;
$select->query();
$result = $db->fetchObject();
return (int) $result->count;
}
/**
* Return count of flashcards for user.
*
* @param int $userId
* @param string $filter See filterByRtk()
*
* @return int
*/
public static function getFlashcardCount($userId, $filter = '')
{
$select = self::getInstance()->select();
$select = self::filterByRtk($select, $filter);
return self::_getFlashcardCount($userId, $select);
}
/**
* Returns a string of all kanji known by user. Currently 'known' simply
* means the user has a flashcard for it.
*
* @return string
*/
public static function getKnownKanji($userId)
{
$user = sfContext::getInstance()->getUser();
// get array of known kanji as ucs ids (this simple SELECT uses INDEX)
$select = self::getInstance()->select()->columns('ucs_id');
$select = self::filterByUserId($select, $userId);
$ucs_array = self::getInstance()->getDb()->fetchCol($select);
// convert to utf8 string for storage
$knownKanji = count($ucs_array) ? utf8::fromUnicode($ucs_array) : '';
return $knownKanji;
}
/**
* Return count of flashcards reviewed today (midnight time).
*
*/
public static function getTodayCount($userId)
{
$user = sfContext::getInstance()->getUser();
$select = self::getInstance()->select()->where('lastreview > DATE('.UsersPeer::sqlLocalTime().')');
return self::_getFlashcardCount($userId, $select);
}
/**
* Return count of expired flashcards (except failed cards) for user.
*
* @param
*
* @return
*/
public static function getCountExpired($userId)
{
$user = sfContext::getInstance()->getUser();
$sqlLocalTime = new coreDbExpr(UsersPeer::sqlLocalTime());
$select = self::getInstance()->select()->where('totalreviews>0 AND leitnerbox>1 AND expiredate <= ?', $sqlLocalTime);
return self::_getFlashcardCount($userId, $select);
}
/**
* Return count of untested flashcards for user.
*
* @param string $filter See filterByRtk()
*
* @return
*/
public static function getCountUntested($userId, $filter = '')
{
$user = sfContext::getInstance()->getUser();
$select = self::getInstance()->select()->where('totalreviews <= 0');
$select = self::filterByRtk($select, $filter);
return self::_getFlashcardCount($userId, $select);
}
/**
* Return flashcard counts for Leitner boxes.
*
* Returns
*
* [
* 'expired_cards' => 20,
* 'fresh_cards' => 10,
* 'total_cards' => 30
* ],
*
* @param string $filter Filter by RtK Volume 1, 3, or all (see filterByRtk())
*
* @return array Array of box data (including empty boxes), index 0 = failed/new cards.
*/
public static function getLeitnerBoxCounts($filter = '')
{
$user = sfContext::getInstance()->getUser();
$userId = $user->getUserId();
$select = self::getInstance()->select([
'box' => 'leitnerbox',
'due' => sprintf('(%s >= expiredate)', UsersPeer::sqlLocalTime()),
'count' => 'COUNT(*)'
])
->where('totalreviews > 0')
->group(['leitnerbox', 'due ASC']);
$select = self::filterByUserId($select, $userId);
$select = self::filterByRtk($select, $filter); // FIXME we don't strictly need sequences JOIN here
$rows = self::getInstance()->getDb()->fetchAll($select);
// do not assume a fixed box setting, do assume SQL data is not messed up
$highest_box = count($rows) ? max(array_column($rows, 'box')) : 1;
$boxes = [];
for ($i = 0; $i < $highest_box; $i++) {
$boxes[$i] = ['expired_cards' => 0, 'fresh_cards' => 0, 'total_cards' => 0];
}
// set due & undue counts
foreach ($rows as $row) {
$boxNr = (int) ($row['box'] - 1);
assert($boxNr >= 0, "LeitnerBox number is invalid!");
if ($boxNr < 0) {
throw new sfException("Invalid LeitnerBox number: {$row['box']} for user {$userId}");
}
$pile = $row['due'] ? 'expired_cards' : 'fresh_cards';
$boxes[$boxNr][$pile] += $row['count'];
}
// set totals per box
for ($i = 0; $i < $highest_box; $i++) {
$boxes[$i]['total_cards'] = $boxes[$i]['expired_cards'] + $boxes[$i]['fresh_cards'];
}
return $boxes;
}
/**
* Returns number of kanji flashcards with at least one review.
*
* @param int $minBox Minimum leitnerbox (optional)
*
* @return int
*/
public static function getReviewedFlashcardCount($userId, $minBox = 0)
{
$condition = $minBox ? 'totalreviews > 0 AND leitnerbox >= '.$minBox : 'totalreviews > 0';
$select = self::getInstance()->select()->where($condition);
return self::_getFlashcardCount($userId, $select);
}
/**
* Return total reviews accross all kanji,
* for the Profile page.
*
* @return int
*/
public static function getTotalReviews($userId)
{
$db = self::getInstance()->getDb();
$select = $db->select(['count' => 'SUM(totalreviews)'])->from('reviews');
self::filterByUserId($select, $userId)->query();
$row = $db->fetchObject();
return (int)$row->count;
}
/**
* Return the most recent flash card review timestamp
* (of a single flash card, not a review session).
*
* @param int $userId User id.
* @return mixed Lastest review timestamp, or FALSE
*/
public static function getMostRecentReviewTimeStamp($userId)
{
$select = self::getInstance()->select('MAX(lastreview)');
$select = self::filterByUserId($select, $userId);
$ts_lastreview = self::getInstance()->getDb()->fetchOne($select);
return !is_null($ts_lastreview) ? $ts_lastreview : false;
}
/**
* Returns Select for detailed flashcard lists.
*
* Used on:
* - Detailed Flashcard List
* - Manage Flashcards > Select flashcards to remove (RemoveListTableComponent)
*
* @param
* @return
*/
public static function getSelectForDetailedList($userId)
{
$select = self::getInstance()->select([
'kanjis.ucs_id', 'seq_nr' => rtkIndex::getSqlCol(), 'failurecount', 'successcount', 'leitnerbox',
'ts_lastreview' => 'UNIX_TIMESTAMP(lastreview)', 'kanji', 'onyomi', 'strokecount',
'tsLastReview' => 'UNIX_TIMESTAMP(lastreview)'
]);
$select = KanjisPeer::joinLeftUsingUCS($select);
$select = self::filterByUserId($select, $userId);
$select = CustkeywordsPeer::addCustomKeywordJoin($select, $userId);
return $select;
}
/**
* Returns Select for Manage > Edit Keywords table.
*
* @return coreDatabaseSelect
*/
public static function getSelectForEditKeywordsList($userId)
{
$select = self::getInstance()->select([
'kanjis.ucs_id', 'kanji', 'seq_nr' => rtkIndex::getSqlCol(),
'kanjis.keyword', 'custkeyword' => 'custkeywords.keyword',
'ts_lastreview' => 'UNIX_TIMESTAMP(lastreview)'
]);
$select = KanjisPeer::joinLeftUsingUCS($select);
// don't use addCustomKeywordJoin() here because we want BOTH original & custom keyword
$select->joinLeftUsing('custkeywords', ['ucs_id', 'userid']);
$select = self::filterByUserId($select, $userId);
return $select;
}
/**
* Returns select for flashcard export feature.
*
* @param int $userId
*/
public static function getSelectForExport($userId)
{
// the order of columns must match the ExportCSV call in executeExportflashcards() !
$select = self::getInstance()->select([
'seq_nr' => rtkIndex::getSqlCol(), 'kanji',
'keyword' => CustkeywordsPeer::coalesceExpr(),
'lastreview', 'expiredate', 'leitnerbox', 'failurecount', 'successcount',
// vocab added with PR #287 (works because we currently have 1 word per card limit)
'compound'
]);
$select = KanjisPeer::joinLeftUsingUCS($select);
$select = CustkeywordsPeer::addCustomKeywordJoinUsing($select);
$select = VocabPicksPeer::addVocabPicksLeftJoinUsing($select);
$select->order('seq_nr', 'ASC');
$select = self::filterByUserId($select, $userId);
// DBG::out($select);exit;
return $select;
}
/**
* Get a selection of flashcards for review, as an array of flashcard ids.
*
* Orders flashcards by expiredate (longest expired first), and then
* randomize cards that expire on the same date.
*
* Only works for current user (sqlLocalTime).
*
* @param mixed $box 'all' or a Leitner box number starting from 1
* @param string $type 'expired', 'untested', 'relearned', 'known', anything else means 'fresh' (non-expired)
* @param string $filt 'rtk1', 'rkt3', '' for all kanji
* @param boolean $merge True to merge flashcards from given box with higher boxes
*
* @return array Flashcard ids (normalized as int).
*/
public static function getFlashcardsForReview($box, $type, $filt, $merge = false)
{
$user = sfContext::getInstance()->getUser();
$userId = $user->getUserId();
$sqlLocalTime = new coreDbExpr(UsersPeer::sqlLocalTime());
if ($type === 'relearned')
{
// select cards from relearned kanji selection
$select = LearnedKanjiPeer::getInstance()->select('ucs_id');
$select->where('userid = ?', $userId);
}
else
{
$select = self::getInstance()->select('ucs_id');
$select = self::filterByUserId($select, $userId);
if ($filt !== '') {
$select = self::filterByRtk($select, $filt);
}
}
switch ($type)
{
case 'untested':
$order_by = 'expiredate';
if ($user->getUserSetting('OPT_NO_SHUFFLE')) {
// do not shuffle new cards, order by sequence number
$select->columns(['seq_nr' => rtkIndex::getSqlCol()]);
$select = KanjisPeer::joinLeftUsingUCS($select);
$order_by = $order_by . ', ' . rtkIndex::getSqlCol() . ' ASC';
}
else {
$order_by = $order_by . ', RAND()';
}
$select->where('totalreviews = 0');
$select->order($order_by);
break;
case 'relearned':
$select->order('RAND()');
break;
default:
if ($type == 'known') {
// known cards are all cards reviewed once and not currently in the failed stack
// $box should be 'all' (cf. below)
}
elseif ($type == 'expired') {
// expired cards (orange stacks, due for review)
$select->where('totalreviews > 0 AND expiredate <= ?', $sqlLocalTime);
}
else {
// fresh cards (green stacks, not due for review)
$select->where('totalreviews > 0 AND expiredate > ?', $sqlLocalTime);
}
if ($box == 'all') {
$select->where('leitnerbox > 1');
}
elseif ($merge) {
$select->where('leitnerbox >= ?', $box);
}
else {
$select->where('leitnerbox = ?', $box);
}
// "known" cards (free review mode) don't care about expiry date
$select->order($type == 'known' ? 'RAND()' : 'expiredate, RAND()');
break;
}
// DBG::out($select);exit;
$db = self::getInstance()->getDb();
$ids = $db->fetchCol($select);
return array_map('intval', $ids);
}
/**
* Return array of cards with the day diff for each due card over the next N
* days.
*
* @param int $userId
*
* @return array Array of day diffs (1 column), or empty array
*/
public static function getDueCardsByDay()
{
$user = sfContext::getInstance()->getUser();
$exprLocalTime = UsersPeer::sqlLocalTime();
$exprDayDiff = 'DATEDIFF(expiredate, '.$exprLocalTime.')';
$select = self::getInstance()->select(new coreDbExpr($exprDayDiff))
->where('userid = ?', $user->getUserId())
->where('leitnerbox > 1') // not failed or new cards
->where('expiredate < DATE_ADD('.$exprLocalTime.', INTERVAL '.DueCardsGraphComponent::GRAPH_DAYS.' DAY)');
//echo $select;exit;
return self::getInstance()->getDb()->fetchCol($select);
}
/**
* Returns Select object for the Review Summary page.
*
* Select all flashcards reviewed since the given (localized) timestamp.
*
* @see getLocalizedTimestamp()
*
* @param int $userId
*
* @return coreDatabaseSelect
*/
public static function getReviewSummaryListSelect($userId, $ts_start)
{
$select = self::getInstance()->select([
'reviews.ucs_id',
'seq_nr' => rtkIndex::getSqlCol(),
'failurecount',
'successcount',
'leitnerbox',
'ts_lastreview' => 'UNIX_TIMESTAMP(lastreview)',
'kanji',
'onyomi',
'strokecount'
]);
$select->where('UNIX_TIMESTAMP(lastreview) >= ?', $ts_start);
$select = KanjisPeer::joinLeftUsingUCS($select);
$select = self::filterByUserId($select, $userId);
$select = CustkeywordsPeer::addCustomKeywordJoin($select, $userId);
//DBG::out($select);exit;
return $select;
}
/**
* Returns the last kanji card index in an uninterrupted sequence
* starting at 1 -- any cards after a gap are ignored.
*
* @return int Sequence number of last kanji in sequence (starts at 1), or 0
*/
public static function getSequencePosition($userId)
{
$select = self::getInstance()->select();
$select = self::filterByRtk($select, 'rtk1');
$select = self::filterByUserId($select, $userId);
$select->columns(['idx' => rtkIndex::getSqlCol()]);
$indices = self::getInstance()->getDb()->fetchCol($select);
$count = count($indices);
if (!$count) {
return 0;
}
$indices = array_map('intval', $indices);
sort($indices);
// find where the first gap of the sequence is
$diff = array_diff(range(1, $count), $indices);
// if there is no gap in sequence then position = number of flashcards
$position = count($diff) ? min($diff) - 1 : $count;
// LOG::info($indices);
// LOG::info('pos', $position);
return $position;
}
/**
* Returns progress status for Check Progress page.
*
* All flashcards not in the current sequence will be in lesson 0.
*
* Returns:
* array( <lessNr> => array( <lessonId>, <total>, <pass>, <fail> ), ... )
*
* @return array Array of objects
*/
public static function getProgressChartData($userId)
{
$select = self::getInstance()->select(['ucs_id', 'seq_nr' => rtkIndex::getSqlCol(), 'leitnerbox', 'totalreviews', 'failurecount', 'successcount']);
$select = KanjisPeer::joinLeftUsingUCS($select);
$select = self::filterByUserId($select, $userId);
$select->query();
$lessons = [];
$db = self::getInstance()->getDb();
while ($row = $db->fetchObject())
{
$lessNr = rtkIndex::getLessonForIndex((int)$row->seq_nr);
if (!isset($lessons[$lessNr])) {
$lessons[$lessNr] = (object) ['lessonId' => $lessNr, 'total' => 0, 'pass' => 0, 'fail' => 0];
}
// ref
$lesson =& $lessons[$lessNr];
$lesson->total++;
if ($row->leitnerbox > 1 && $row->totalreviews > 0) {
$lesson->pass++;
}
elseif ($row->leitnerbox == 1 && $row->totalreviews > 0) {
$lesson->fail++;
}
}
/*
$select = self::getInstance()->select(array(
'lessonId' => 'lessonnum',
'total' => 'COUNT(*)',
'pass' => 'SUM(leitnerbox > 1 AND totalreviews > 0)',
'fail' => 'SUM(leitnerbox = 1 AND totalreviews > 0)'
));
$select = KanjisPeer::joinLeftUsingUCS($select);
$select->group('lessonnum ASC');
*/
//DBG::printr($lessons);exit;
return $lessons;
}
/**
* Return count of failed kanji for user.
*
* @param
* @return
*/
public static function getRestudyKanjiCount($userId)
{
return self::getInstance()->count('userid = ? AND leitnerbox=1 AND totalreviews>0', $userId);
}
/**
* Return select object for the Restudy Kanji list.
*
* @param int $userId
*
* @return coreDatabaseSelect
*/
public static function getRestudyKanjiListSelect($userId)
{
$select = self::getInstance()
->select([
'kanji',
'seq_nr' => rtkIndex::getSqlCol(),
'successcount',
'failurecount',
'ts_lastreview' => 'UNIX_TIMESTAMP(lastreview)',
'is_learned' => new coreDbExpr('(lk.ucs_id IS NOT NULL)')
]);
$select->joinLeftUsing(LearnedKanjiPeer::getInstance()->getName().' lk', 'userid,ucs_id');
$select = KanjisPeer::joinLeftUsingUCS($select);
$select = CustkeywordsPeer::addCustomKeywordJoin($select, $userId);
$select->where('leitnerbox=1 AND totalreviews>0');
$select = self::filterByUserId($select, $userId);
return $select;
}
/**
* Return data for Restudy List "quick" view in the side column of the Study page.
*
* Returns all failed kanji that are not currently in the learned list.
*
* @param
*
* @return array<array> Resultset
public static function getRestudyQuickList($userId)
{
$select = self::getInstance()->select(array('seq_nr' => rtkIndex::getSqlCol()));
$select = KanjisPeer::joinLeftUsingUCS($select);
$select->joinLeftUsing('learnedkanji', 'userid, ucs_id');
$select = self::filterByUserId($select, $userId);
$select = CustkeywordsPeer::addCustomKeywordJoin($select, $userId);
$select->where('leitnerbox=1 AND totalreviews>0')
->where('learnedkanji.ucs_id IS NULL') // not in the learned list
->order('seq_nr')
->limit(10);
return self::getInstance()->getDb()->fetchAll($select);
}
*/
/**
* Returns the first kanji in index order,
* which is in the failed stack and not yet "learned".
*
* @return mixed UCS-2 code value (int) of the next restudy kanji, or false
*/
public static function getNextUnlearnedKanji($userId)
{
$tableName = self::getInstance()->getName();
$select = self::getInstance()->select($tableName.'.ucs_id');
$select = KanjisPeer::joinLeftUsingUCS($select);
$select->joinLeftUsing('learnedkanji', 'userid, ucs_id');
$select = self::filterByUserId($select, $userId);
$select->where('leitnerbox=1 AND totalreviews>0')
->where('learnedkanji.ucs_id IS NULL') /* not in the learned list */
->order(rtkIndex::getSqlCol().' ASC')
->limit(1);
$ucsId = self::getInstance()->getDb()->fetchOne($select);
return $ucsId !== false ? intval($ucsId) : false;
}
/**
* FlashcardReview callback for the review page.
*
* Note: must sanitize data!
*
* Flashcard answer data is set by the front end code (review.js):
*
* id Flashcard id = UCS-2 code value
* r Answer (cf. FlashcardReview.php const)
*
* @param int $ucsId Flashcard id (UCS-2 code value)
* @param object $oData Flashcard answer data
*
* @return boolean True if update/skip/delete went succesfully
*/
public static function putFlashcardData($ucsId, $oData)
{
assert($ucsId > 0);
assert(isset($oData->r));
//DBG::printr($oData);
$userId = sfContext::getInstance()->getUser()->getUserId();
if ($oData->r === LeitnerSRS::RATE_SKIP)
{
// skip flashcard : just ignore it (pretend it's been handled)
$result = true;
}
elseif ($oData->r === LeitnerSRS::RATE_DELETE)
{
$result = self::deleteFlashcards($userId, [$ucsId]) > 0;
sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent(null, 'flashcards.update'));
}
else
{
$curData = self::getFlashcardData($userId, $ucsId);
if (!$curData) {
// if the card was somehow deleted, return true so the client can clear the card from sync buffer
return true;
}
// LOG::info("rate {$oData->r}", json_encode($curData));
$update = LeitnerSRS::getInstance()->rateCard($curData, $oData->r);
$result = $update && self::updateFlashcard($userId, $ucsId, $update);
}
// clear relearned kanji if successfull answer
if ($result
&& ($oData->r === LeitnerSRS::RATE_HARD ||
$oData->r === LeitnerSRS::RATE_YES ||
$oData->r === LeitnerSRS::RATE_NO ||
$oData->r === LeitnerSRS::RATE_EASY ||
$oData->r === LeitnerSRS::RATE_DELETE))
{
LearnedKanjiPeer::clearKanji($userId, $ucsId);
}
return $result;
}
/**
* Moves flashcard to the red pile (stack 1) by rating it as "No".
*
* @param int $userId
* @param int $ucsId
*
* @return bool Whether SQL update was successful
*/
public static function failFlashcard($userId, $ucsId)
{
$cardData = self::getFlashcardData($userId, $ucsId);
if ($cardData === false) return false;
// rate card as "not remembered" (No)
$update = LeitnerSRS::getInstance()->rateCard($cardData, LeitnerSRS::RATE_NO);
return $update && self::updateFlashcard($userId, $ucsId, $update);
}
/**
* Update flashcard row data, plus:
*
* - sets `lastreview` to user's local time
* - if `interval_days` is provided, converts it to `expiredate`
*
* @param int $userId
* @param int $ucsId
* @param array $cardUpdate Row data to update, +optional value "interval_days"
*
* @return bool Whether SQL update was successful
*/
public static function updateFlashcard(int $userId, int $ucsId, array $cardUpdate)
{
$sqlLocalTime = UsersPeer::sqlLocalTime();
// update "due" timestamp if interval is provided (cf. LeitnerSRS::rateCard())
if (isset($cardUpdate['interval_days']))
{
$interval_days = $cardUpdate['interval_days'];
unset($cardUpdate['interval_days']);
$user = sfContext::getInstance()->getUser();
$sqlExpireDate = sprintf('DATE_ADD(%s, INTERVAL %d DAY)', $sqlLocalTime, $interval_days);
$cardUpdate['expiredate'] = new coreDbExpr($sqlExpireDate);
}
// always update last review timestamp
$cardUpdate['lastreview'] = new coreDbExpr($sqlLocalTime);
// LOG::info("updateFlashcard() ", $cardUpdate);
// foolproof check : leitnerbox should NEVER be zero or NULL
if (array_key_exists('leitnerbox', $cardUpdate)
&& (
!ctype_digit((string)$cardUpdate['leitnerbox'])
|| $cardUpdate['leitnerbox'] <= 0
)
) {
error_log("updateFlashcard() INVALID! ".json_encode($cardUpdate));
return false;
}
return self::getInstance()->update($cardUpdate, 'userid = ? AND ucs_id = ?', [$userId, $ucsId]);
}
/**
* Add a set of new flashcards to the user's deck.
*
* Note: remove duplicate cards first with filterExistingCards() !
*
* @param int $userId
* @param array $cardSel Array of UCS-2 codes
*
* @return array Array of successfully added flashcard ids
*/
static public function addSelection($userId, array $cardSel)
{
// create new flashcards
$cards = ReviewsPeer::addFlashcards($userId, $cardSel);
if (count($cards))
{
ActiveMembersPeer::updateFlashcardCount($userId);
sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent(null, 'flashcards.update'));
}
return $cards;
}
/**
* Delete all given flashards, returns an array of ids of flashcards
* that were succesfully deleted.
*
* @param int $userId
* @param array $cardSel Array of flashcard ids (UCS-2)
*
* @return array Array of successfully deleted flashcards (ids) or false
*/
static public function deleteSelection($userId, array $cardSel)
{
$cards = ReviewsPeer::deleteFlashcards($userId, $cardSel);
if (is_array($cards) && count($cards))
{
ActiveMembersPeer::updateFlashcardCount($userId);
sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent(null, 'flashcards.update'));
}
return $cards;
}
/**
* Filter out flashcard ids in the selection that are already present in the
* user's deck. Returns an array with only the flashcard ids that don't
* exist in the user's deck.
*
* @param int $userId