public function testGetSuggestions()
 {
     //
     // Set up tags with the following level/recency/frequency:
     //
     // low level:
     //     bravo (now, 2)
     //     alpha (now, 1)
     //     delta (yesterday, 2)
     //     charlie (yesterday, 1)
     //     echo (last week, 3)
     // high level:
     //     yankee (now, 2)
     //     zebra (now, 1)
     //     x-ray (yesterday, 2)
     //     whiskey (yesterday, 1)
     //     victor (last week, 3)
     //
     $this->generateTags(array(array('level' => 0, 'texts' => array('alpha', 'bravo'), 'time' => new \DateTime()), array('level' => 1, 'texts' => array('bravo'), 'time' => new \DateTime()), array('level' => 2, 'texts' => array('delta', 'charlie'), 'time' => new \DateTime('yesterday')), array('level' => 3, 'texts' => array('delta'), 'time' => new \DateTime('2 days ago')), array('level' => 4, 'texts' => array('echo'), 'time' => new \DateTime('last week')), array('level' => 0, 'texts' => array('echo'), 'time' => new \DateTime('last week')), array('level' => 1, 'texts' => array('echo'), 'time' => new \DateTime('last week')), array('level' => 5, 'texts' => array('zebra', 'yankee'), 'time' => new \DateTime()), array('level' => 6, 'texts' => array('yankee'), 'time' => new \DateTime()), array('level' => 7, 'texts' => array('x-ray', 'whiskey'), 'time' => new \DateTime('yesterday')), array('level' => 8, 'texts' => array('x-ray'), 'time' => new \DateTime('2 days ago')), array('level' => 9, 'texts' => array('victor'), 'time' => new \DateTime('last week')), array('level' => 10, 'texts' => array('victor'), 'time' => new \DateTime('last week')), array('level' => 9, 'texts' => array('victor'), 'time' => new \DateTime('last week'))));
     $suggestions = $this->tagManager->getSuggestions($this->user, array('limit' => 5));
     $this->assertCount(2, $suggestions);
     $expectedLow = array('bravo', 'alpha', 'delta', 'charlie', 'echo');
     $this->assertEquals($expectedLow, $suggestions['low']);
     $expectedHigh = array('yankee', 'zebra', 'x-ray', 'whiskey', 'victor');
     $this->assertEquals($expectedHigh, $suggestions['high']);
     // When too few previous tags exist,
     // suggestions should be padded out with defaults.
     $suggestions = $this->tagManager->getSuggestions($this->user, array('limit' => 10));
     $this->assertCount(10, $suggestions['high']);
     $this->assertCount(10, $suggestions['low']);
 }
Esempio n. 2
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $user = $this->tokenStorage->getToken()->getUser();
     if (!$user) {
         throw new \LogicException(__CLASS__ . ' cannot be used without an authenticated user!');
     }
     $tags = $this->tagManager->getTextsByUser($user);
     $tag_ops = array_combine($tags, $tags);
     $builder->add('level', HiddenType::class, array('label' => 'Stress level'))->add('factorTexts', ChoiceType::class, array('choices' => $tag_ops, 'required' => false, 'label' => 'Factors', 'multiple' => true))->add('notes', TextAreaType::class, array('required' => false))->add('localtime', DateTimeType::class, array('date_widget' => 'single_text', 'label' => 'Local time'))->add('timezone', TimezoneType::class);
     // Callback function for adding tags to the select list,
     // used by event handlers below.
     $addTagsToForm = function (FormInterface $form, array $texts) {
         // Get the existing field & info
         $field = $form->get('factorTexts');
         $type = $field->getConfig()->getType()->getInnerType();
         $options = $field->getConfig()->getOptions();
         // Update the options
         foreach ($texts as $text) {
             $options['choices'][$text] = $text;
         }
         // Replace the field.
         $form->add('factorTexts', get_class($type), $options);
     };
     // Called before form data is populated.
     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($addTagsToForm) {
         $log = $event->getData();
         $form = $event->getForm();
         // Make sure all the *assigned* tags are included in the select list
         // (even if they haven't been persisted to the database yet).
         $addTagsToForm($form, $log->getFactorTexts());
     });
     // Called when form is submitted, before the submit is processed.
     $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($addTagsToForm) {
         $data = $event->getData();
         // Make sure all *submitted* tags are included in the select list,
         // ex. for re-rendering form on error.
         if (array_key_exists('factorTexts', $data)) {
             $addTagsToForm($event->getForm(), $data['factorTexts']);
         }
     });
 }