private function giveBadges() { $user = $this->getUser(); $em = $this->getDoctrine()->getManager(); $threads = $em->createQueryBuilder()->select('t')->from('EtuModuleForumBundle:Thread', 't')->where('t.author = :user')->setParameter('user', $user)->getQuery()->getResult(); $nbThreads = count($threads); if ($nbThreads >= 1) { BadgesManager::userAddBadge($user, 'mysterion', 1); BadgesManager::userPersistBadges($user); } if ($nbThreads >= 10) { BadgesManager::userAddBadge($user, 'mysterion', 2); BadgesManager::userPersistBadges($user); } if ($nbThreads >= 20) { BadgesManager::userAddBadge($user, 'mysterion', 3); BadgesManager::userPersistBadges($user); } if ($nbThreads >= 40) { BadgesManager::userAddBadge($user, 'mysterion', 4); BadgesManager::userPersistBadges($user); } $messages = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.author = :user')->setParameter('user', $user)->getQuery()->getResult(); $nbMessages = count($messages); if ($nbMessages >= 1) { BadgesManager::userAddBadge($user, 'monkey', 1); BadgesManager::userPersistBadges($user); } if ($nbMessages >= 20) { BadgesManager::userAddBadge($user, 'monkey', 2); BadgesManager::userPersistBadges($user); } if ($nbMessages >= 50) { BadgesManager::userAddBadge($user, 'monkey', 3); BadgesManager::userPersistBadges($user); } if ($nbMessages >= 100) { BadgesManager::userAddBadge($user, 'monkey', 4); BadgesManager::userPersistBadges($user); } if ($nbMessages >= 500) { BadgesManager::userAddBadge($user, 'monkey', 5); BadgesManager::userPersistBadges($user); } }
/** * @Route("/user/create", name="admin_user_create") * @Template() */ public function userCreateAction() { if (!$this->getUserLayer()->isUser() || !$this->getUser()->getIsAdmin()) { return $this->createAccessDeniedResponse(); } /** @var $user User */ $user = new User(); $privacyChoice = array('choices' => array(User::PRIVACY_PUBLIC => 'user.privacy.public', User::PRIVACY_PRIVATE => 'user.privacy.private'), 'attr' => array('class' => 'profileEdit-privacy-select'), 'required' => false); $form = $this->createFormBuilder($user)->add('login', null, array('required' => true))->add('fullName', null, array('required' => true))->add('mail', null, array('required' => true))->add('password', null, array('required' => true))->add('studentId', null, array('required' => false))->add('phoneNumber', null, array('required' => false))->add('phoneNumberPrivacy', 'choice', $privacyChoice)->add('sex', 'choice', array('choices' => array(User::SEX_MALE => 'base.user.sex.male', User::SEX_FEMALE => 'base.user.sex.female'), 'required' => false))->add('sexPrivacy', 'choice', $privacyChoice)->add('nationality', null, array('required' => false))->add('nationalityPrivacy', 'choice', $privacyChoice)->add('adress', null, array('required' => false))->add('adressPrivacy', 'choice', $privacyChoice)->add('postalCode', null, array('required' => false))->add('postalCodePrivacy', 'choice', $privacyChoice)->add('city', null, array('required' => false))->add('cityPrivacy', 'choice', $privacyChoice)->add('country', null, array('required' => false))->add('countryPrivacy', 'choice', $privacyChoice)->add('birthday', 'birthday', array('widget' => 'single_text', 'format' => 'dd/MM/yyyy', 'required' => false))->add('birthdayPrivacy', 'choice', $privacyChoice)->add('birthdayDisplayOnlyAge', null, array('required' => false))->add('personnalMail', null, array('required' => false))->add('personnalMailPrivacy', 'choice', $privacyChoice)->add('website', null, array('required' => false))->add('facebook', null, array('required' => false))->add('twitter', null, array('required' => false))->add('linkedin', null, array('required' => false))->add('viadeo', null, array('required' => false))->getForm(); $request = $this->getRequest(); if ($request->getMethod() == 'POST' && $form->bind($request)->isValid()) { $em = $this->getDoctrine()->getManager(); if ($user->getProfileCompletion() == 100) { BadgesManager::userAddBadge($user, 'profile_completed'); } else { BadgesManager::userRemoveBadge($user, 'profile_completed'); } if ($user->getTrombiCompletion() == 100) { BadgesManager::userAddBadge($user, 'trombi_completed'); } else { BadgesManager::userRemoveBadge($user, 'trombi_completed'); } BadgesManager::userPersistBadges($user); $user->setPassword($this->get('etu.user.crypting')->encrypt($user->getPassword())); $user->setKeepActive(true); $em->persist($user); $em->flush(); $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'user.admin.userCreate.confirm')); return $this->redirect($this->generateUrl('user_view', array('login' => $user->getLogin()))); } return array('user' => $user, 'form' => $form->createView()); }
/** * Update the element in the database * * @return DbUser */ public function update() { /* * Update: * - formation * - niveau * - filiere * - uvs * - semesters history */ $persist = false; $user = $this->database; $history = $user->addCureentSemesterToHistory(); $niveau = null; $branch = $this->ldap->getNiveau(); preg_match('/^[^0-9]+/i', $this->ldap->getNiveau(), $match); if (isset($match[0])) { $branch = $match[0]; $niveau = str_replace($branch, '', $this->ldap->getNiveau()); } // Updates if (ucfirst(strtolower($this->ldap->getFormation())) != $this->database->getFormation()) { $persist = true; $user->setFormation(ucfirst(strtolower($this->ldap->getFormation()))); } if ($niveau != $this->database->getNiveau()) { $persist = true; $user->setNiveau($niveau); } if ($branch != $this->database->getBranch()) { $persist = true; $user->setBranch($branch); } if ($this->ldap->getFiliere() != $this->database->getFiliere()) { $persist = true; $user->setFiliere($this->ldap->getFiliere()); } if (implode('|', $this->ldap->getUvs()) != $this->database->getUvs()) { $persist = true; $user->setUvs(implode('|', $this->ldap->getUvs())); } /* * Add badges */ if (substr($history['niveau'], 0, 2) == 'TC' && substr($user->getNiveau(), 0, 2) != 'TC') { BadgesManager::userAddBadge($user, 'tc_survivor'); BadgesManager::userPersistBadges($user); } if ($persist) { $this->doctrine->getManager()->persist($user); } return $persist; }
/** * @Route("/{slug}-{name}/send-review", name="uvs_view_send_review") * @Template() */ public function sendReviewAction(Request $request, $slug, $name) { if (!$this->getUserLayer()->isUser()) { return $this->createAccessDeniedResponse(); } /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); /** @var UV $uv */ $uv = $em->getRepository('EtuModuleUVBundle:UV')->findOneBy(array('slug' => $slug)); if (!$uv) { throw $this->createNotFoundException(sprintf('UV for slug %s not found', $slug)); } if (StringManipulationExtension::slugify($uv->getName()) != $name) { return $this->redirect($this->generateUrl('uvs_view_send_review', array('slug' => $uv->getSlug(), 'name' => StringManipulationExtension::slugify($uv->getName()))), 301); } $review = new Review(); $review->setUv($uv)->setSender($this->getUser())->setSemester(User::currentSemester()); $form = $this->createFormBuilder($review)->add('type', 'choice', array('choices' => Review::$types, 'required' => true))->add('semester', 'choice', array('choices' => Review::availableSemesters(), 'required' => true))->add('file', null, array('required' => true))->getForm(); if ($request->getMethod() == 'POST' && $form->submit($request)->isValid()) { $review->upload(); $em->persist($review); $em->flush(); // Notify subscribers $notif = new Notification(); $review->file = null; $notif->setModule($this->getCurrentBundle()->getIdentifier())->setHelper('uv_new_review')->setAuthorId($this->getUser()->getId())->setEntityType('uv')->setEntityId($uv->getId())->addEntity($review); $this->getNotificationsSender()->send($notif); // Add badges $count = $em->createQueryBuilder()->select('COUNT(r) as nb')->from('EtuModuleUVBundle:Review', 'r')->where('r.sender = :user')->setParameter('user', $this->getUser()->getId())->getQuery()->getSingleScalarResult(); $user = $this->getUser(); if ($count >= 1) { BadgesManager::userAddBadge($user, 'uvs_reviews', 1); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 1); } if ($count >= 2) { BadgesManager::userAddBadge($user, 'uvs_reviews', 2); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 2); } if ($count >= 4) { BadgesManager::userAddBadge($user, 'uvs_reviews', 3); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 3); } if ($count >= 10) { BadgesManager::userAddBadge($user, 'uvs_reviews', 4); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 4); } BadgesManager::userPersistBadges($user); $em->persist($user); $em->flush(); $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'uvs.main.sendReview.confirm')); return $this->redirect($this->generateUrl('uvs_view', array('slug' => $slug, 'name' => $name))); } return array('uv' => $uv, 'form' => $form->createView()); }
/** * Get badges (generate some of them on the fly) * * @return array */ public function getBadgesList() { if ($this->testingContext) { return []; } $badges = $this->badges ? $this->badges->toArray() : []; foreach ($badges as &$badge) { $badge = $badge->getBadge(); } $count = count($badges); if ($this->getBranch() == 'TC' && $this->getNiveau() == '1') { BadgesManager::userAddBadge($this, 'tc01'); $count++; } if ($this->getBranch() == 'TC' && $this->getNiveau() == '6') { BadgesManager::userAddBadge($this, 'tc06'); $count++; } if (BadgesManager::userHasBadge($this, 'subscriber') && BadgesManager::userHasBadge($this, 'forum_message') && BadgesManager::userHasBadge($this, 'profile_completed')) { BadgesManager::userAddBadge($this, 'starter'); $count++; } /** @var Member[] $memberships */ $memberships = $this->getMemberships() ? $this->getMemberships()->toArray() : []; if (count($memberships) > 0) { BadgesManager::userAddBadge($this, 'orga_member', 1); $count++; } foreach ($memberships as $member) { if ($member->isFromBureau()) { BadgesManager::userAddBadge($this, 'orga_member', 2); $count++; } if ($member->getRole() == Member::ROLE_PRESIDENT) { BadgesManager::userAddBadge($this, 'orga_member', 3); $count++; if ($member->getOrganization()->getLogin() == 'bde') { BadgesManager::userAddBadge($this, 'orga_member', 4); $count++; } } } if ($count >= 10) { BadgesManager::userAddBadge($this, 'challenge', 1); } if ($count >= 20) { BadgesManager::userAddBadge($this, 'challenge', 2); } if ($count >= 30) { BadgesManager::userAddBadge($this, 'challenge', 3); } if ($count >= 40) { BadgesManager::userAddBadge($this, 'challenge', 4); } if ($count >= 50) { BadgesManager::userAddBadge($this, 'challenge', 5); } if ($count >= 60) { BadgesManager::userAddBadge($this, 'challenge', 6); } /* * Create a usable list */ $list = array(); foreach ((array) BadgesManager::findBadgesList() as $serie => $badges) { foreach ((array) $badges as $level => $badge) { if ($badge->getCountLevels() > 1) { $list[$serie][$level] = array('owned' => false, 'badge' => $badge); } else { $list['singles'][$serie] = array('owned' => false, 'badge' => $badge); } } } $badges = $this->badges ? $this->badges->toArray() : []; /** @var $userBadge UserBadge */ foreach ($badges as $userBadge) { $badge = $userBadge->getBadge(); if ($badge->getCountLevels() > 1) { $list[$badge->getSerie()][$badge->getLevel()]['owned'] = true; $list[$badge->getSerie()][$badge->getLevel()]['createdAt'] = $userBadge->getCreatedAt(); } else { $list['singles'][$badge->getSerie()]['owned'] = true; $list['singles'][$badge->getSerie()]['createdAt'] = $userBadge->getCreatedAt(); } } return $list; }
/** * @Route( * "/unfollow/{entityType}/{entityId}", * requirements={"entityType"="[a-z\-_]+", "entityId" = "\d+"}, * defaults={"_format"="json"}, * name="notifs_unsubscribe", * options={"expose"=true} * ) */ public function unsubscribeAction($entityType, $entityId) { if (!$this->getUserLayer()->isUser()) { return new Response(json_encode(array('status' => 403, 'message' => 'You are not allowed to access this URL as anonymous or organization.')), 403); } $user = $this->getUser(); if (!$user->testingContext) { $this->getSubscriptionsManager()->unsubscribe($user, $entityType, $entityId); } /** @var $em EntityManager */ $em = $this->getDoctrine()->getManager(); $count = $em->createQueryBuilder()->select('COUNT(s) as nb')->from('EtuCoreBundle:Subscription', 's')->where('s.user = :user')->setParameter('user', $user->getId())->getQuery()->getSingleScalarResult(); $count = (int) $count; if ($count >= 10) { BadgesManager::userAddBadge($user, 'subscriber', 1); } else { BadgesManager::userRemoveBadge($user, 'subscriber', 1); } if ($count >= 20) { BadgesManager::userAddBadge($user, 'subscriber', 2); } else { BadgesManager::userRemoveBadge($user, 'subscriber', 2); } if ($count >= 30) { BadgesManager::userAddBadge($user, 'subscriber', 3); } else { BadgesManager::userRemoveBadge($user, 'subscriber', 3); } BadgesManager::userPersistBadges($user); $em->persist($user); $em->flush(); return new Response(json_encode(array('status' => 200, 'message' => $this->get('translator')->trans('core.subscriptions.api.confirm_unfollow')))); }
/** * @Route("/user/trombi/edit", name="user_trombi_edit") * @Template() */ public function trombiEditAction() { if (!$this->getUserLayer()->isUser()) { return $this->createAccessDeniedResponse(); } /** @var $user User */ $user = $this->getUser(); $form = $this->createFormBuilder($user)->add('surnom', null, array('required' => false))->add('jadis', null, array('required' => false, 'attr' => array('class' => 'trombi-textarea')))->add('passions', null, array('required' => false, 'attr' => array('class' => 'trombi-textarea')))->getForm(); $request = $this->getRequest(); if ($request->getMethod() == 'POST' && $form->bind($request)->isValid()) { $em = $this->getDoctrine()->getManager(); if ($user->getTrombiCompletion() == 100) { BadgesManager::userAddBadge($user, 'trombi_completed'); } else { BadgesManager::userRemoveBadge($user, 'trombi_completed'); } BadgesManager::userPersistBadges($user); $em->persist($user); $em->flush(); $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'user.profile.trombiEdit.confirm')); return $this->redirect($this->generateUrl('user_profile')); } return array('form' => $form->createView()); }