/**
  * @param Request       $request
  * @param SubjectEntity $subject
  * @param string        $studentId
  *
  * @return JsonResponse
  */
 public function saveAction(Request $request, SubjectEntity $subject, $studentId)
 {
     $firstname = $request->get('firstname');
     $lastname = $request->get('lastname');
     $em = $this->get('doctrine.orm.default_entity_manager');
     if (is_numeric($studentId)) {
         $new = false;
         $student = $em->getRepository('SubjectBundle:StudentEntity')->find($studentId);
     } else {
         $new = true;
         $student = new StudentEntity();
         $student->setEducationClass($subject->getEducationClass());
     }
     $student->setFirstname($firstname)->setLastname($lastname);
     $em->persist($student);
     $em->flush();
     return new JsonResponse(['id' => $student->getId(), 'firstname' => $student->getFirstname(), 'lastname' => $student->getLastname(), 'new' => $new]);
 }
 public function newAction(Request $request)
 {
     $subject = $request->get('education_subject');
     $edClass = $request->get('education_class');
     $em = $this->get('doctrine.orm.default_entity_manager');
     $subjectEntity = new SubjectEntity();
     $subjectEntity->setName($subject['key']);
     $edClassEntity = null;
     if (!empty($edClass['val'])) {
         $edClassEntity = $em->getRepository('SubjectBundle:EducationClassEntity')->find($edClass['val']);
     }
     if ($edClassEntity instanceof EducationClassEntity !== true) {
         $edClassEntity = new EducationClassEntity();
         $edClassEntity->setName($edClass['key']);
     }
     $subjectEntity->setEducationClass($edClassEntity);
     $em->persist($subjectEntity);
     $em->persist($edClassEntity);
     $em->flush();
     return new JsonResponse(['data' => ['subject' => $subject, 'edClass' => $edClass, 'error' => '']]);
 }
 public function indexAction(Subject $subject)
 {
     $data = [];
     $students = $subject->getEducationClass()->getStudents();
     $teachingUnits = $subject->getTeachingUnits();
     foreach ($students as $student) {
         $studentData = ['id' => $student->getId(), 'firstname' => $student->getFirstname(), 'lastname' => $student->getLastname(), 'teachingUnits' => []];
         $marks = $student->getMarks();
         foreach ($teachingUnits as $teachingUnit) {
             /** @var $teachingUnit TeachingUnit */
             $studentData['teachingUnits'][$teachingUnit->getId()] = null;
         }
         foreach ($marks as $mark) {
             /** @var MarkEntity $mark */
             $markSubject = $mark->getTeachingUnit()->getSubject();
             if ($markSubject->getId() === $subject->getId()) {
                 $studentData['teachingUnits'][$mark->getTeachingUnit()->getId()] = $mark->getMark();
             }
         }
         $data[] = $studentData;
     }
     return $this->render('MarkBundle:mark:index.html.php', ['data' => $data, 'teachingUnits' => $teachingUnits, 'subject' => $subject]);
 }