GameRepository.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Game;
  4. use App\Models\Sentence;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\DB;
  7. class GameRepository extends ResourceRepository {
  8. public function __construct(Game $game) {
  9. $this->game = $game;
  10. }
  11. private function save(Game $game, Array $inputs) {
  12. $game->user_id = $inputs['user_id'];
  13. $game->sentence_index = $inputs['sentence_index'];
  14. $game->save();
  15. }
  16. public function store(Array $inputs) {
  17. debug("store");
  18. if (Auth::check()) {
  19. debug("auth checked");
  20. $user_id = Auth::user()->id;
  21. debug($user_id);
  22. $game = new $this->game;
  23. if ($user_id == 1202) {
  24. $sentences = $this->get_sentences_from_orthal();
  25. } else {
  26. $sentences = $this->get_sentences($user_id);
  27. }
  28. if ($sentences->count() == 0) {
  29. $this->save($game, $inputs);
  30. $game->sentences()->attach($sentences);
  31. /* game is not null !! */
  32. return $game;
  33. } else {
  34. $count = $sentences->count();
  35. debug($count);
  36. if ($count < 3) {
  37. if ($user_id == 1202) {
  38. $sentences = $this->get_sentences_from_orthal()->take($count);
  39. } else {
  40. $sentences = $this->get_sentences($user_id)->take($count);
  41. }
  42. } else {
  43. if ($user_id == 1202) {
  44. $sentences = $this->get_sentences_from_orthal()->random(3);
  45. } else {
  46. $sentences = $this->get_sentences($user_id)->random(3);
  47. }
  48. }
  49. /* get a random sentence from reference (=training?) */
  50. $ref_sentence = $this->get_reference_sentences()->random(1);
  51. $this->save($game, $inputs);
  52. $game->sentences()->attach($sentences);
  53. $game->sentences()->attach($ref_sentence);
  54. return $game;
  55. }
  56. }
  57. }
  58. public function update($id, Array $inputs) {
  59. $this->save($this->getById($id), $inputs);
  60. }
  61. public function destroy($id) {
  62. $this->getById($id)->delete();
  63. }
  64. public function getById($id) {
  65. return $this->game->findOrFail($id);
  66. }
  67. public function getWithUserId($user_id) {
  68. return $this->game->where('user_id', $user_id)->where('is_finished', 0);
  69. }
  70. protected function get_sentences($user_id) {
  71. /* forces user to annotate on sentences he has'nt annotated yet */
  72. $id_annotated_sentences = Sentence::select(DB::raw('sentences.id'))
  73. ->join('words', 'sentences.id', '=', 'words.sentence_id')
  74. ->join('annotations', 'annotations.word_id', '=', 'words.id')
  75. ->whereRaw("annotations.confidence_score<10", Array($user_id))
  76. ->get();
  77. return Sentence::join('corpora', 'corpora.id', '=', 'sentences.corpus_id')
  78. ->select('sentences.*')
  79. ->where('corpora.is_training', 0)
  80. ->where('corpora.is_active', 1)
  81. ->whereNotIn('sentences.id', $id_annotated_sentences)
  82. ->get();
  83. /* forces user to annotate on sentences he hasn't annotated yet */
  84. // $id_annotated_sentences = Sentence::select(DB::raw('sentences.id, count(distinct(users.id)) as count_user '))
  85. // ->join('words', 'sentences.id', '=', 'words.sentence_id')
  86. // ->join('annotations', 'annotations.word_id', '=', 'words.id')
  87. // ->join('users', 'users.id', '=', 'annotations.user_id')
  88. // ->join('corpora', 'corpora.id', '=', 'sentences.corpus_id')
  89. // ->where('corpora.is_training', 0)
  90. // ->where('corpora.is_active', 1)
  91. // ->whereRaw("annotations.confidence_score<10")
  92. // ->groupBy('sentences.id')
  93. // ->having('count_user','>','2')
  94. // ->get();
  95. //
  96. // return Sentence::select('sentences.*')->whereNotIn('sentences.id', $id_annotated_sentences)
  97. // ->get();
  98. }
  99. protected function get_sentences_from_orthal($user_id) {
  100. $name = "orthal";
  101. debug(Sentence::join('corpora', 'corpora.id', '=', 'sentences.corpus_id')->where('corpora.id', 322)
  102. ->select('sentences.*')
  103. ->get());
  104. return Sentence::join('corpora', 'corpora.id', '=', 'sentences.corpus_id')->where('corpora.id', 322)
  105. ->select('sentences.*')
  106. ->get();
  107. }
  108. protected function get_reference_sentences() {
  109. return Sentence::join('corpora', 'corpora.id', '=', 'sentences.corpus_id')
  110. ->select('sentences.*')
  111. ->where('corpora.is_training', 1)
  112. ->get();
  113. }
  114. }