/** * save * Saves a collection of attendance records * @Route("/admin/schedule/attendance/save", options={"expose"=true}) * @Template("TSKScheduleBundle:Default:attendance_save.html.twig") * @Method("POST") */ public function saveAction(Request $request) { $form = $this->createForm(new AttendanceFormType(), new Attendance(), array('csrf_protection' => false)); $form->bind($request); $attendance = $form->getData(); $roster = $attendance->getRosters()->first(); if ($request->isMethod('POST')) { if ($form->isValid()) { // save the attendance $em = $this->getDoctrine()->getManager(); $statuses = $attendance->getStatuses(); foreach ($attendance->getRosters() as $r) { $sa = new ScheduleAttendance(); $sa->setSchool($r->getSchedule()->getSchool()); $sa->setClass($r->getClass()); $sa->setSchedule($r->getSchedule()); $sa->setStudent($r->getStudent()); $sa->setAttDate($attendance->getAttDate()); $sa->setStatus('present'); $em->persist($sa); try { $em->flush(); } catch (\Exception $e) { print $e->getMessage(); if ($e->getCode() == '23000') { // ignore ... } else { // print $e->getCode(); // throw $e; } } } if ($this->getRequest()->isXmlHttpRequest()) { return new Response(1); } else { $this->get('session')->getFlashBag()->add('success', 'Attendance saved!'); } } else { $errors = $this->get('validator')->validate($roster); $errmsg = ''; foreach ($errors as $error) { $errmsg = $error->getMessage(); } if ($this->getRequest()->isXmlHttpRequest()) { return new Response('some error'); } else { $this->get('session')->getFlashBag()->add('error', 'Error saving attendance ' . $errmsg); } } if ($this->getRequest()->isXmlHttpRequest()) { return new Response(1); } else { return $this->redirect($this->getRequest()->headers->get('referer')); } return $this->redirect($this->generateUrl('tsk_schedule_roster_index', array('id' => $roster->getSchedule()->getId(), 'class' => $roster->getClass()->getId()))); } }
protected function processAttendance($row) { if ($row) { $attendanceRepo = $this->manager->getRepository('TSK\\ScheduleBundle\\Entity\\ScheduleAttendance'); $attDate = new \DateTime($row[3]); $classRepo = $this->manager->getRepository('TSK\\ClassBundle\\Entity\\Classes'); $studentRepo = $this->manager->getRepository('TSK\\StudentBundle\\Entity\\Student'); $class = $classRepo->find($row[1]); if (!$class) { print "Can't find class " . $row[1] . "\n"; exit; } $student = $studentRepo->findOneBy(array('legacyStudentId' => $row[2])); if (!$student) { print "Can't find student " . $row[2] . "\n"; exit; } $oldAttendance = $attendanceRepo->findOneBy(array('attDate' => $attDate, 'class' => $class, 'student' => $student)); if (!$oldAttendance) { $attendance = new ScheduleAttendance(); $attendance->setSchool($this->school); $attendance->setSchedule($this->dummyScheduleEntity); $attendance->setClass($class); $attendance->setStudent($student); $attendance->setAttDate($attDate); $attendance->setStatus('present'); $attendance->setNotes($row[4]); try { $this->manager->persist($attendance); $this->manager->flush(); } catch (DBALException $e) { } catch (\Exception $e) { ld($e); print $e->getMessage(); exit; } } } }