Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.75 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. use App\Transformers\QuestionTransformer;
  6. use App\Repositories\QuestionRepository;
  7.  
  8. class QuestionService extends Service
  9. {
  10.     protected $questionRepository, $questionTransformer, $userService, $calculatorService;
  11.  
  12.     public function __construct()
  13.     {
  14.         $this->questionRepository   = new QuestionRepository();
  15.         $this->questionTransformer  = new QuestionTransformer();
  16.     }
  17.  
  18.     //Todas as questões
  19.     public function getAllQuestions()
  20.     {
  21.         $questions = $this->questionRepository->all();
  22.  
  23.         if(!$questions)
  24.             return new \Exception('Questions not found');
  25.  
  26.         $questions = $this->questionTransformer->transformCollection($questions->all());
  27.  
  28.         return $questions;
  29.     }
  30.  
  31.     public function countAllQuestions()
  32.     {
  33.         $count = $this->questionRepository->all()->count();
  34.  
  35.         return $count;
  36.     }
  37.  
  38.     public function getQuestionCollection($id)
  39.     {
  40.         return $this->questionRepository->find($id);
  41.     }
  42.  
  43.     public function getQuestion($id)
  44.     {
  45.         $question = $this->questionRepository->find($id);
  46.  
  47.         if(!$question)
  48.             return new \Exception('Question not found');
  49.  
  50.         $question = $this->questionTransformer->transform($question);
  51.  
  52.         return $question;
  53.     }
  54.  
  55.     public function getQuestionsBySubject($request)
  56.     {
  57.         switch ($request->status){
  58.             case 1: // all
  59.                 $questions = $this->questionRepository->findBySubject($request->id, $request->startYear,
  60.                     $request->endYear, $request->keyWord,  $request->pagination);
  61.                 break;
  62.             case 2: // user not resolved
  63.                 $questions = $this->questionRepository->findBySubjectUserNr($request->id, $request->startYear,
  64.                     $request->endYear, $request->keyWord, $request->pagination, $request->userId);
  65.  
  66.                 break;
  67.             case 3: // user resolved
  68.                 //TODO
  69.                 break;
  70.             case 4: // not reviewed
  71.                 $questions = $this->questionRepository->findBySubjectNotReviewed($request->id, $request->startYear,
  72.                     $request->endYear, $request->keyWord, $request->pagination);
  73.  
  74.                 break;
  75.             default: // error
  76.  
  77.                 break;
  78.         }
  79.  
  80.         return $this->questionTransformer->transformCollectionOrderKey($questions->all());
  81.     }
  82.  
  83.     public function getQuestionsByTopic($status,$idtopic, $startYear, $endYear,
  84.                                         $keyWord, $pagination, $iduser)
  85.     {
  86.  
  87.         switch ($status){
  88.             case 1: // all
  89.                 $questions = $this->questionRepository->findByTopic($idtopic, $startYear,
  90.                     $endYear, $keyWord, $pagination);
  91.                 break;
  92.             case 2: // user not resolved
  93.                 $questions = $this->questionRepository->findByTopicUserNr($idtopic, $startYear,
  94.                     $endYear, $keyWord, $pagination, $iduser);
  95.  
  96.                 break;
  97.  
  98.             case 4: // not reviewed
  99.                 $questions = $this->questionRepository->findByTopicNotReviewed($idtopic, $startYear,
  100.                     $endYear, $keyWord, $pagination);
  101.  
  102.                 break;
  103.             default: // error
  104.  
  105.                 break;
  106.         }
  107.  
  108.         return $this->questionTransformer->transformCollectionOrderKey($questions->all());
  109.     }
  110.  
  111.     public function countQuestionsBySubject($request)
  112.     {
  113.         switch ($request->status){
  114.             case 1: // all
  115.                 $count = $this->questionRepository->countBySubject($request->id, $request->startYear,
  116.                                                                    $request->endYear, $request->keyWord);
  117.                 break;
  118.             case 2: // user not resolved
  119.                 //TODO: Redo user questions comparation
  120.                 $questions     = $this->questionRepository->countBySubject($request->id, $request->startYear,
  121.                                                                            $request->endYear, $request->keyWord);
  122.  
  123.                 $questionsUser = $this->questionRepository->findBySubjectUser($request->id, $request->userId);
  124.  
  125.                 $count1 = $questions;
  126.                 $count2 = $questionsUser->count();
  127.  
  128.                 $count  = $count1 - $count2;
  129.                 break;
  130.  
  131.             case 4: // not reviewed
  132.                 $count = $this->questionRepository->countBySubjectNotReviewed($request->id, $request->startYear,
  133.                     $request->endYear, $request->keyWord);
  134.                 break;
  135.             default: // error
  136.  
  137.                 break;
  138.         }
  139.  
  140.         return $count;
  141.     }
  142.  
  143.     public function countQuestionsByTopic($status,$idtopic, $startYear, $endYear, $keyWord, $iduser)
  144.     {
  145.         $count = 0;
  146.  
  147.         switch ($status){
  148.             case 1: // all
  149.                 $count = $this->questionRepository->countByTopic($idtopic, $startYear,
  150.                     $endYear, $keyWord);
  151.                 break;
  152.             case 2: // user not resolved
  153.                 //TODO: Redo user questions comparation
  154.                 $questions     = $this->questionRepository->countByTopic($idtopic, $startYear,
  155.                     $endYear, $keyWord);
  156.                 $questionsUser = $this->questionRepository->findByTopicUser($idtopic, $iduser);
  157.  
  158.                 $count1 = $questions;
  159.                 $count2 = $questionsUser->count();
  160.                 $count  = $count1 - $count2;
  161.                 break;
  162.             case 4: // not reviewed
  163.                 $count = $this->questionRepository->countByTopicNotReviewed($idtopic, $startYear,
  164.                     $endYear, $keyWord);
  165.                 break;
  166.             default: // error
  167.  
  168.                 break;
  169.         }
  170.  
  171.         return $count;
  172.     }
  173.  
  174.     public function countBySubjectBest($idsubject)
  175.     {
  176.         return $this->questionRepository->countBySubjectBest($idsubject);
  177.     }
  178.  
  179.     public function countBySubject($idsubject)
  180.     {
  181.         return $this->questionRepository->countBySubjectId($idsubject);
  182.     }
  183.  
  184.     public function countByTopic($idtopic)
  185.     {
  186.         return $this->questionRepository->countByTopicId($idtopic);
  187.     }
  188.  
  189.     public function updateSubjetOfQuestion($idquest, $idsubject)
  190.     {
  191.         $question = $this->questionRepository->find($idquest);
  192.  
  193.         $question->subject_idsubject = $idsubject;
  194.         $question->reviewed = 1;
  195.         $question->save();
  196.  
  197.         return $question;
  198.     }
  199.  
  200.     public function updateTopicOfQuestion($idquest, $idtopic)
  201.     {
  202.         $question = $this->questionRepository->find($idquest);
  203.  
  204.         $question->topic_idtopic = $idtopic;
  205.         $question->reviewed = 1;
  206.         $question->save();
  207.  
  208.         return $question;
  209.     }
  210.  
  211.     public function getBestQuestion($idsubject, $iduser, $idquiz)
  212.     {
  213.         $userService       = new UserService();
  214.         $calculatorService = new CalculatorService();
  215.  
  216.         $theta    = $userService->getSubjectUserRelationTheta($idsubject, $iduser);
  217.  
  218.         $question = $calculatorService->getBestQuestion($idsubject, $idquiz, $theta);
  219.  
  220.         $question = $this->questionTransformer->transform($question);
  221.  
  222.         return $question;
  223.     }
  224.  
  225.     public function getFutureBestQuestion($idquiz, $idquest, $idsubject, $iduser, $idquests)
  226.     {
  227.         $userService       = new UserService();
  228.         $calculatorService = new CalculatorService();
  229.  
  230.         $question         = $this->questionRepository->find($idquest);
  231.  
  232.         $futureRightTheta = $userService->updateUserTheta($question, $idsubject, $iduser, 1, false);
  233.         $futureWrongTheta = $userService->updateUserTheta($question, $idsubject, $iduser, 0, false);
  234.  
  235.         $idquests[]    = $idquest;
  236.         $questionRight = $calculatorService->getBestQuestion($idsubject, $idquiz, $futureRightTheta, $idquests);
  237.  
  238.         $idquests[]    = $questionRight->idquest;
  239.         $questionWrong = $calculatorService->getBestQuestion($idsubject, $idquiz, $futureWrongTheta, $idquests);
  240.  
  241.         $data = [
  242.             'futureRightAnswerQuestion' => $this->questionTransformer->transform($questionRight),
  243.             'futureWrongAnswerQuestion' => $this->questionTransformer->transform($questionWrong),
  244.             'futureRightTheta'          => $futureRightTheta,
  245.             'futureWrongTheta'          => $futureWrongTheta,
  246.         ];
  247.  
  248.         return $data;
  249.     }
  250.  
  251.     public function getQuestionParametersBySubject($idsubject)
  252.     {
  253.         return $this->questionRepository->findQuestionParametersBySubject($idsubject);
  254.     }
  255.  
  256.     public function updateQuestionParameters($question, $usersAnswers)
  257.     {
  258.         $var = $this->calculatorService->updateQuestionParameters($usersAnswers, $question);
  259.  
  260.         return $var;
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement