Пример #1
0
 public function testGetCurrentDurationWithGivenEnd()
 {
     $start = new \DateTime('now');
     $end = new \DateTime('now');
     $end->add(new \DateInterval('PT10H'));
     $Timeslice = new Timeslice();
     $Timeslice->setStartedAt($start);
     $Timeslice->setStoppedAt($end);
     $this->assertEquals(36000, $Timeslice->getCurrentDuration());
 }
Пример #2
0
 /**
  * loads fixtures to database
  *
  * @param  Doctrine\Common\Persistence\ObjectManager $manager
  * @return LoadActivities
  */
 public function load(ObjectManager $manager)
 {
     $baseSlice = new Timeslice();
     $baseSlice->setUser($manager->merge($this->getReference('default-user')));
     foreach ($this->data as $key => $data) {
         $slice = clone $baseSlice;
         if ($data['startedAt'] == null && $data['stoppedAt'] == null) {
             $date = new \DateTime();
             $date->setTime(0, 0, 0);
             $data['startedAt'] = $date;
             $data['stoppedAt'] = $date;
         }
         $slice->setActivity($manager->merge($this->getReference($key)))->setDuration($data['duration'])->setStartedAt($data['startedAt'])->setStoppedAt($data['stoppedAt']);
         $manager->persist($slice);
         $this->addReference('timeslice-' . $key, $slice);
     }
     $manager->flush();
 }
Пример #3
0
 /**
  * create a new activity
  *
  * [POST] /activities
  *
  * @return View
  */
 public function postActivitiesAction()
 {
     // create new activity entity
     $activity = new Activity();
     // convert json to assoc array from request content
     $data = json_decode($this->getRequest()->getContent(), true);
     if (isset($data['parse'])) {
         // Run parser
         $result = $this->parse($data['parse']);
         if (isset($data['date'])) {
             $date = new \DateTime($data['date']);
         } else {
             $date = new \DateTime();
         }
         // create new activity and timeslice entity
         $activity = new Activity();
         $activity->setUser($this->getCurrentUser());
         if (isset($result['customer'])) {
             try {
                 $customer = $this->getCustomerRepository()->createCurrentQueryBuilder('c')->scopeByField('user', $this->getCurrentUser()->getId())->scopeByField('alias', $result['customer'])->getCurrentQueryBuilder()->setMaxResults(1)->getQuery()->getSingleResult();
                 $activity->setCustomer($customer);
             } catch (NoResultException $e) {
             }
         }
         if (isset($result['project'])) {
             try {
                 $project = $this->getProjectRepository()->createCurrentQueryBuilder('p')->scopeByField('user', $this->getCurrentUser()->getId())->scopeByField('alias', $result['project'])->getCurrentQueryBuilder()->setMaxResults(1)->getQuery()->getSingleResult();
                 $activity->setProject($project);
                 // Auto set customer because of direct relation to project
                 if ($activity->getCustomer() == null) {
                     $activity->setCustomer($project->getCustomer());
                 }
             } catch (NoResultException $e) {
             }
         }
         if (isset($result['service'])) {
             try {
                 $service = $this->getServiceRepository()->createCurrentQueryBuilder('p')->scopeByField('user', $this->getCurrentUser()->getId())->scopeByField('alias', $result['service'])->getCurrentQueryBuilder()->setMaxResults(1)->getQuery()->getSingleResult();
                 $activity->setService($service);
             } catch (NoResultException $e) {
             }
         }
         if (isset($result['tags']) && !empty($result['tags'])) {
             foreach ($result['tags'] as $tagname) {
                 try {
                     $tag = $this->getTagRepository()->createCurrentQueryBuilder('t')->scopeByField('user', $this->getCurrentUser()->getId())->scopeByField('name', $tagname)->getCurrentQueryBuilder()->setMaxResults(1)->getQuery()->getSingleResult();
                 } catch (NoResultException $e) {
                     $tag = null;
                 }
                 if ($tag == null) {
                     $tag = new Tag();
                     $tag->setName($tagname);
                     $tag->setUser($this->getCurrentUser());
                 }
                 $activity->addTag($tag);
             }
         }
         if (isset($result['description'])) {
             $activity->setDescription($result['description']);
         }
         // create timeslice
         $timeslice = new Timeslice();
         $timeslice->setActivity($activity);
         $timeslice->setUser($this->getCurrentUser());
         $activity->addTimeslice($timeslice);
         if (isset($result['range']) || isset($result['duration'])) {
             // process time range
             if (isset($result['range'])) {
                 $range = $result['range'];
                 if (empty($range['stop'])) {
                     $start = new \DateTime($range['start']);
                     $stop = new \DateTime('now');
                 } elseif (empty($range['start'])) {
                     $start = new \DateTime('now');
                     $stop = new \DateTime($range['stop']);
                 } elseif (!empty($range['start']) && !empty($range['stop'])) {
                     $start = new \DateTime($range['start']);
                     $stop = new \DateTime($range['stop']);
                 }
                 $start->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
                 $stop->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
                 $timeslice->setStartedAt($start);
                 $timeslice->setStoppedAt($stop);
             } else {
                 // track date for duration
                 $date->setTime(0, 0, 0);
                 $timeslice->setStartedAt($date);
                 $timeslice->setStoppedAt($date);
             }
             // process duration
             if (isset($result['duration'])) {
                 if (empty($result['duration']['sign'])) {
                     $timeslice->setDuration($result['duration']['number']);
                 } else {
                     if ($result['duration']['sign'] == '-') {
                         $timeslice->setDuration($timeslice->getCurrentDuration() - $result['duration']['number']);
                     } else {
                         $timeslice->setDuration($timeslice->getCurrentDuration() + $result['duration']['number']);
                     }
                 }
             }
         } else {
             // start a new timeslice with date 'now'
             $timeslice->setStartedAt(new \DateTime('now'));
         }
         // save change to database
         $em = $this->getDoctrine()->getManager();
         $em->persist($activity);
         $em->flush();
         $em->refresh($activity);
         $view = $this->createView($activity);
     } else {
         // create activity form
         $form = $this->createForm(new ActivityType($this->getDoctrine()->getManager(), $this->getCurrentUser()), $activity);
         $view = $this->saveForm($form, $data);
     }
     return $view;
 }