public function updateForTPs($input, $tpIds)
 {
     $this->nom = $input['nom'];
     $this->enonce = $input['enonce'];
     $this->baliseCorrection = $input['baliseCorrection'];
     $this->reponse = $input['reponse'];
     $this->sur = $input['sur'];
     if ($this->save()) {
         //TODO: mettre ca dans une transaction
         $allOldTpIds = $this->tps->lists('id')->all();
         $deletedTpIds = array_diff($allOldTpIds, $tpIds);
         $newTpIds = array_diff($tpIds, $allOldTpIds);
         foreach ($deletedTpIds as $tpId) {
             if ($tpId != 0) {
                 TP::find($tpId)->questions()->detach($this->id);
             }
             //TODO resynch l'ordre
         }
         foreach ($newTpIds as $tpId) {
             if ($tpId != 0) {
                 TP::find($tpId)->addQuestion($this);
             }
         }
         return true;
     } else {
         return $this->validationMessages;
     }
 }
 private function displayView($option0, $selectedValue, $item = null, $displayOnlyLinked = null)
 {
     if (isset($item) and isset($displayOnlyLinked)) {
         $lesTPs = $item->tps;
         //affiche seulement les tps associées à cet item. (utile pour show)
     } else {
         //sinon affiche tous.
         $lesTPs = TP::all()->sortby("nom");
     }
     $belongsToList = createSelectOptions($lesTPs, [get_class(), 'createOptionsValue'], $option0);
     if (isset($item)) {
         //si on a un item, on sélectionne seulement ce qui est associées
         $belongsToSelectedIds = $item->TPs->pluck('id')->toArray();
     } else {
         //sinon, on sélectionne ce qui a été passée en paramêtre (si c'est bon, sinon, la première de la liste
         $belongsToSelectedIds = checkLinkedId(array_keys($belongsToList)[0], $selectedValue, 'App\\Models\\TP');
     }
     $filtre1 = createFiltreParClassePourTP($lesTPs, true);
     $question = $item;
     return compact('question', 'belongsToList', 'belongsToSelectedIds', 'filtre1');
 }
 /**
  * Sauvegarde les données présentement à l'écran 
  * 
  * @param unknown $input
  * @param unknown $etudiant_id
  * @param unknown $classe_id
  * @param unknown $tp_id
  * @param unknown $pageCourante
  * @return boolean|string
  */
 public function doRepondre($reponses, $etudiant_id, $classe_id, $tp_id, $pageCourante)
 {
     $return = true;
     $etudiant = User::findorfail($etudiant_id);
     $classe = Classe::findorfail($classe_id);
     $tp = TP::findorfail($tp_id);
     $lesQuestions = $tp->questions()->orderBy('ordre')->get();
     //batit la pagination des questions
     $i = 1;
     foreach ($lesQuestions as $question) {
         $page[$i][] = $question->id;
         if ($question->pivot->breakafter == 1) {
             $i++;
         }
     }
     //choisi la bonne page de questions
     $questions = $lesQuestions->filter(function ($item) use($page, $pageCourante) {
         return in_array($item->id, $page[$pageCourante]);
     });
     //verifie que c'est les bonnes questions qui nous revienne
     $listeIdReponses = array_keys($reponses);
     foreach ($questions as $question) {
         if (!in_array($question->id, $listeIdReponses)) {
             $return = false;
         }
     }
     if ($return) {
         // on a toutes les réponses, on peut les stocker
         foreach ($questions as $question) {
             $note = Note::where('classe_id', '=', $classe_id)->where('tp_id', '=', $tp_id)->where('etudiant_id', '=', $etudiant_id)->where('question_id', '=', $question->id)->first();
             // cette requete devrait toujour fonctionner
             $note->reponse = $reponses[$question->id];
             $note->save();
         }
     }
     return $return;
 }
 public function run()
 {
     $tp1cn2 = TP::where('nom', '=', 'TP1 CN2')->first();
     $tp2cn2 = TP::where('nom', '=', 'TP2 CN2')->first();
     $tp1DM1 = TP::where('nom', '=', 'TP1 DM1')->first();
     DB::table('tps_questions')->delete();
     DB::table('questions')->delete();
     $question = new Question();
     $question->nom = "Q1";
     $question->enonce = "Quel est la couleur du cheval blanc de Napoléon";
     $question->sur = 10;
     $question->baliseCorrection = "blanc = 10, noir =0";
     $question->reponse = "blanc";
     $question->save();
     $tp1cn2->addQuestion($question);
     $question = new Question();
     $question->nom = "Exponentielle";
     $question->enonce = "<p>Si x = 2</p>\n\n<p>et y = 3</p>\n\n<p>Quelle est la valeur de <strong>x<sup>y</sup></strong>?</p>\n\t\t";
     $question->sur = 10;
     $question->baliseCorrection = "<p>un nombre pair = 5p</p>\n\n<p>8 = 10p</p>";
     $question->reponse = "8";
     $question->save();
     $tp1cn2->addQuestion($question);
 }
 /**
  * Sauvegarde la correction 
  * @param unknown $etudiant_id
  * @param unknown $classe_id
  * @param unknown $tp_id
  * @param unknown $questions_id
  * @param unknown $input
  */
 public function doCorriger($etudiant_id, $classe_id, $tp_id, $question_id, $commentaire, $commentaire_visible, $pointage)
 {
     $etudiant = User::findorfail($etudiant_id);
     $classe = Classe::findorfail($classe_id);
     $tp = TP::findorfail($tp_id);
     $question = Question::findorfail($question_id);
     $reponse = Note::where('classe_id', '=', $classe->id)->where('tp_id', '=', $tp->id)->where('etudiant_id', '=', $etudiant->id)->where('question_id', $question->id)->first();
     $reponse->commentaire = $commentaire;
     $reponse->commentaire_visible = $commentaire_visible;
     $reponse->note = $pointage;
     if ($reponse->save()) {
         return true;
     } else {
         return $reponse->validationMessages;
     }
 }