Skip to content

Commit

Permalink
Add language as tags (#103)
Browse files Browse the repository at this point in the history
* add languages to the user form

* adding the terms

* shows the languages

* fixed view to imploded list

* fixing psr-2 coding standard
  • Loading branch information
gabidavila authored and dragonmantank committed May 31, 2017
1 parent 566e412 commit a67e423
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 2 deletions.
37 changes: 37 additions & 0 deletions data/migrations/20170225230431_add_taxonomy_languages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddTaxonomyLanguages extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function up()
{
$this->execute('INSERT INTO taxonomyVocabulary (`name`, `description`) VALUES ("languages", "Languages")');
}

public function down()
{
$this->execute('DELETE FROM taxonomyVocabulary WHERE `name` = "languages" AND `description` = "Languages")');
}
}
10 changes: 10 additions & 0 deletions src/Mentoring/Account/Form/ProfileForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$rules[] = 'mentorValidation';
}

$rules[] = 'languagesValidation';

return $rules;
},
));
Expand All @@ -63,6 +65,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
])
->addModelTransformer(new TextToTagsTransformer($this->taxonomyService, 'apprentice'));

$languageTags = $builder
->create('languagesTags', TextType::class, [
'required' => false,
'constraints' => new TagConstraint(array('groups' => array('languagesValidation'))),
])
->addModelTransformer(new TextToTagsTransformer($this->taxonomyService, 'languages'));

$builder
->add('name', TextType::class, ['constraints' => new NotBlank()])
->add('email', EmailType::class, [
Expand All @@ -80,6 +89,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => false,
])
->add($menteeTags)
->add($languageTags)
->add('profile', TextareaType::class, [
'required' => false,
])
Expand Down
16 changes: 16 additions & 0 deletions src/Mentoring/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User
protected $profile = '';
protected $mentorTags = [];
protected $apprenticeTags = [];
protected $languagesTags = [];
protected $profileImage = null;
protected $sendNotifications = true;

Expand All @@ -33,11 +34,21 @@ public function addMentorTag(Term $term)
$this->mentorTags[$term->getId()] = $term;
}

public function addLanguagesTag(Term $term)
{
$this->languagesTags[$term->getId()] = $term;
}

public function getApprenticeTags()
{
return $this->apprenticeTags;
}

public function getLanguagesTags()
{
return $this->languagesTags;
}

public function getEmail()
{
return $this->email;
Expand Down Expand Up @@ -98,6 +109,11 @@ public function setApprenticeTags(array $terms)
$this->apprenticeTags = $terms;
}

public function setLanguagesTags(array $terms)
{
$this->languagesTags = $terms;
}

public function setEmail($email)
{
$this->email = $email;
Expand Down
11 changes: 11 additions & 0 deletions src/Mentoring/User/UserHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function extract(User $object)
'profile' => $object->getProfile(),
'apprenticeTags' => $object->getApprenticeTags(),
'mentorTags' => $object->getMentorTags(),
'languagesTags' => $object->getLanguagesTags(),
'imageUrl' => $object->getProfileImage(),
'timezone' => $object->getTimezone(),
'sendNotifications' => $object->hasSendNotifications()
Expand All @@ -59,6 +60,14 @@ public function extract(User $object)
$mentorTags[$key] = $tag;
}
$data['mentorTags'] = $mentorTags;

$languagesTags = $data['languagesTags'];
unset($data['languagesTags']);
foreach ($languagesTags as $key => $tag) {
$tag = $this->termHydrator->extract($tag);
$languagesTags[$key] = $tag;
}
$data['languagesTags'] = $languagesTags;
}

if ($data['timeCreated'] instanceof \DateTime) {
Expand Down Expand Up @@ -132,9 +141,11 @@ public function hydrate(array $data, User $object)

$mentoringTerm = $this->taxonomyService->fetchVocabularyByName('mentor');
$apprenticeTerm = $this->taxonomyService->fetchVocabularyByName('apprentice');
$languagesTerm = $this->taxonomyService->fetchVocabularyByName('languages');

$object->setMentorTags($this->taxonomyService->fetchTermsForUser($object, $mentoringTerm));
$object->setApprenticeTags($this->taxonomyService->fetchTermsForUser($object, $apprenticeTerm));
$object->setLanguagesTags($this->taxonomyService->fetchTermsForUser($object, $languagesTerm));

return $object;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Mentoring/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function saveUser(User $user)
$data['roles'] = serialize($data['roles']);
unset($data['mentorTags']);
unset($data['apprenticeTags']);
unset($data['languagesTags']);
unset($data['imageUrl']);

if (empty($data['id'])) {
Expand Down Expand Up @@ -139,5 +140,10 @@ public function saveUserTags(User $user)
echo 'Saving '.$apprenticeTag->getName();
$this->dbal->insert('userTags', ['user_id' => $user->getId(), 'term_id' => $apprenticeTag->getId()]);
}

foreach ($user->getLanguagesTags() as $languageTag) {
echo 'Saving '.$languageTag->getName();
$this->dbal->insert('userTags', ['user_id' => $user->getId(), 'term_id' => $languageTag->getId()]);
}
}
}
1 change: 1 addition & 0 deletions views/account/profile.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{{ form_row(profile_form.mentorTags, { label: 'I want to mentor on the following topics' }) }}
{{ form_row(profile_form.isMentee, { label: 'I want to be an apprentice' }) }}
{{ form_row(profile_form.apprenticeTags, { label: 'I want to learn about the following topics' }) }}
{{ form_row(profile_form.languagesTags, { label: 'I speak the following languages' }) }}
<div data-ng-app="mentoringApp" class="profile_text">
<div role="tabpanel">
<ul class="nav nav-tabs" role="tablist">
Expand Down
4 changes: 3 additions & 1 deletion views/index/mentors.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<br />

<div class="panel panel-default" data-ng-repeat="mentor in mentors | mentorTags:name | orderBy:random">
<div class="panel-heading"><a href="/profile/{% verbatim %}{{ mentor.id }}{% endverbatim %}">{% verbatim %}{{ mentor.name }}{% endverbatim %}</a></div>
<div class="panel-heading">
<a href="/profile/{% verbatim %}{{ mentor.id }}{% endverbatim %}">{% verbatim %}{{ mentor.name }}{% endverbatim %}</a>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-2">
Expand Down
12 changes: 11 additions & 1 deletion views/index/profile.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@
</ul>
</div>
{% endif %}

</div>
<div class="col-lg-9">
<h1>{{ viewing_user.name }}</h1>

{% if viewing_user.languagesTags|length > 0 %}
<p>
Spoken Languages:
{% for language in viewing_user.languagesTags %}
{{ language.name }}
{% if not loop.last %},{% endif %}
{% endfor %}
</p>
{% endif %}

<p>{{ viewing_user.profile|markdown }}</p>

{% if viewing_user.timezone %}
Expand Down

0 comments on commit a67e423

Please sign in to comment.