public function sendemailAction()
 {
     $quizset = $this->admin->getSubject();
     if (!$quizset) {
         throw new NotFoundHttpException(sprintf('unable to find the object with id : %s'));
     }
     if (!$quizset->isActiveNow()) {
         $this->addFlash('sonata_flash_error', 'Ten etap jest nie aktywny');
         return new RedirectResponse($this->admin->generateUrl('list'));
     }
     $em = $this->getDoctrine()->getManager();
     $users = $em->getRepository('AppBundle:Users')->findAll();
     foreach ($users as $user) {
         $plainPassword = $this->random_str(8);
         $encoder = $this->container->get('security.password_encoder');
         $encoded = $encoder->encodePassword($user, $plainPassword);
         $user->setPass($encoded);
         $UsersToQuizset = $em->getRepository('AppBundle:UsersToQuizset')->findOneBy(array('idUser' => $user->getId(), 'idSet' => $quizset->getId()));
         if (!$UsersToQuizset) {
             $UsersToQuizset = UsersToQuizset::createUserSet($user, $quizset);
         }
         $template = $em->getRepository('AppBundle:EmailTemplate')->findOneById(1);
         $status = $this->sendEmail($user->getEmail(), $plainPassword, $quizset, $template->getTemplate());
         $UsersToQuizset->setIsEmailSent($status);
         $em->persist($UsersToQuizset);
         $em->flush();
     }
     $this->addFlash('sonata_flash_success', 'Wysłano wiadomości');
     return new RedirectResponse($this->admin->generateUrl('list'));
 }
 public static function createUserSet(Users $user, Quizset $set)
 {
     $UserQuizSet = new UsersToQuizset();
     $UserQuizSet->setIdSet($set->getId());
     $UserQuizSet->setIdUser($user->getId());
     $UserQuizSet->setMasterHash(md5($user->getId() . $user->getEmail() . time()));
     return $UserQuizSet;
 }
 public static function createUserQuestions(Users $User, $Questions, UsersToQuizset $UsersToQuizset)
 {
     $return = array();
     $masterHash = $UsersToQuizset->getMasterHash();
     $setId = $UsersToQuizset->getIdSet();
     $userId = $User->getId();
     foreach ($Questions as $Question) {
         $qUserSet = new QuestionToUserSet();
         $qUserSet->setIdSet($setId);
         $qUserSet->setIdQuestion($Question->getId());
         $qUserSet->setIdUser($userId);
         $qUserSet->setHashQuestion(md5($Question->getId() . $masterHash . $Question->getContent()));
         $qUserSet->setHashAns1(md5($Question->getId() . $masterHash . $Question->getAns1()));
         $qUserSet->setHashAns2(md5($Question->getId() . $masterHash . $Question->getAns2()));
         $qUserSet->setHashAns3(md5($Question->getId() . $masterHash . $Question->getAns3()));
         $return[] = $qUserSet;
     }
     return $return;
 }