From 9c89e342896af6c7b6708415d474ee589990bcd8 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 9 May 2022 17:33:56 +0200
Subject: [PATCH] wip alumni

---
 .../university/person/alumni_controller.rb    | 22 +++++----
 .../admin/education/teachers/edit.html.erb    | 34 ++++++-------
 .../person/alumni/_cohort_fields.html.erb     | 32 +++++++++++++
 .../university/person/alumni/_list.html.erb   | 13 -----
 .../person/alumni/edit_cohorts.html.erb       | 32 +++++++++++++
 .../university/person/alumni/index.html.erb   |  1 +
 .../university/person/alumni/new.html.erb     |  3 ++
 .../university/person/alumni/show.html.erb    | 14 ++++++
 .../person/alumni/show/_cohorts.html.erb      | 48 +++++++++++++++++++
 config/locales/university/en.yml              |  3 ++
 config/locales/university/fr.yml              |  3 ++
 config/routes/admin/university.rb             |  9 +++-
 12 files changed, 174 insertions(+), 40 deletions(-)
 create mode 100644 app/views/admin/university/person/alumni/_cohort_fields.html.erb
 create mode 100644 app/views/admin/university/person/alumni/edit_cohorts.html.erb
 create mode 100644 app/views/admin/university/person/alumni/new.html.erb
 create mode 100644 app/views/admin/university/person/alumni/show/_cohorts.html.erb

diff --git a/app/controllers/admin/university/person/alumni_controller.rb b/app/controllers/admin/university/person/alumni_controller.rb
index 80931367b..0ac640f00 100644
--- a/app/controllers/admin/university/person/alumni_controller.rb
+++ b/app/controllers/admin/university/person/alumni_controller.rb
@@ -14,23 +14,25 @@ class Admin::University::Person::AlumniController < Admin::University::Applicati
   end
 
   def show
+    @cohorts = @alumnus.cohorts.ordered.page(params[:cohorts_page])
     breadcrumb
   end
 
-  def edit
+  def edit_cohorts
     breadcrumb
     add_breadcrumb t('edit')
   end
 
-  def update
-    if @alumnus.update(alumnus_params)
-      redirect_to [:admin, @alumnus],
-                  notice: t('admin.successfully_updated_html', model: @alumnus.to_s)
-    else
-      render :edit
-      breadcrumb
-      add_breadcrumb t('edit')
-    end
+  def update_cohorts
+    # TODO
+    # if @alumnus.update(alumnus_params)
+    #   redirect_to [:admin, @alumnus],
+    #               notice: t('admin.successfully_updated_html', model: @alumnus.to_s)
+    # else
+    #   render :edit
+    #   breadcrumb
+    #   add_breadcrumb t('edit')
+    # end
   end
 
   protected
diff --git a/app/views/admin/education/teachers/edit.html.erb b/app/views/admin/education/teachers/edit.html.erb
index 68677371f..8d3adb005 100644
--- a/app/views/admin/education/teachers/edit.html.erb
+++ b/app/views/admin/education/teachers/edit.html.erb
@@ -4,27 +4,29 @@
 
 <%= simple_form_for [:admin, @teacher], url: admin_education_teacher_path(@teacher) do |f| %>
   <%= f.error_notification %>
-  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %><div class="row">
+  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
 
-  <div class="col-md-2">
-    <%= link_to_add_association t('add'), f, :involvements,
-                                class: button_classes,
-                                data: {
-                                  'association-insertion-method': 'append',
-                                  'association-insertion-node':   '#involvements',
-                                } %>
+  <div class="row">
+
+    <div class="col-md-2">
+      <%= link_to_add_association t('add'), f, :involvements,
+                                  class: button_classes,
+                                  data: {
+                                    'association-insertion-method': 'append',
+                                    'association-insertion-node':   '#involvements',
+                                  } %>
 
-  </div>
-  <div class="col-md-10">
-    <div id="involvements">
-      <% sorted_teacher_involvements = @teacher.involvements.select(&:teacher?).sort_by { |involvement| involvement.created_at || Time.zone.now } %>
-      <%= f.simple_fields_for :involvements, sorted_teacher_involvements, include_id: false do |involvement_f| %>
-        <%= render 'admin/education/teachers/involvement_fields', f: involvement_f, include_id: true %>
-      <% end %>
     </div>
 
+    <div class="col-md-10">
+      <div id="involvements">
+        <% sorted_teacher_involvements = @teacher.involvements.select(&:teacher?).sort_by { |involvement| involvement.created_at || Time.zone.now } %>
+        <%= f.simple_fields_for :involvements, sorted_teacher_involvements, include_id: false do |involvement_f| %>
+          <%= render 'admin/education/teachers/involvement_fields', f: involvement_f, include_id: true %>
+        <% end %>
+      </div>
+    </div>
   </div>
-</div>
   <% content_for :action_bar_right do %>
     <%= submit f %>
   <% end %>
diff --git a/app/views/admin/university/person/alumni/_cohort_fields.html.erb b/app/views/admin/university/person/alumni/_cohort_fields.html.erb
new file mode 100644
index 000000000..bf6e5f834
--- /dev/null
+++ b/app/views/admin/university/person/alumni/_cohort_fields.html.erb
@@ -0,0 +1,32 @@
+<% include_id ||= false %>
+<div class="nested-fields">
+  <div class="card mb-3">
+    <div class="card-body">
+      <div class="row">
+        <div class="col-md-5">
+          <%= f.association :program,
+                            collection: collection_tree(current_university.education_programs),
+                            label_method: ->(p) { sanitize p[:label] },
+                            value_method: ->(p) { p[:id] },
+                            label: false,
+                            include_blank: t('simple_form.include_blanks.defaults.program'),
+                            required: true,
+                            wrapper: false %>
+        </div>
+        <div class="col-md-6">
+          <%#= f.input :description,
+                      as: :string,
+                      label: false,
+                      placeholder: University::Person::Involvement.human_attribute_name('description'),
+                      wrapper: false %>
+        </div>
+        <div class="col-md-1 text-end">
+          <%= link_to_remove_association '<i class="fas fa-times"></i>'.html_safe,
+                                         f,
+                                         class: 'btn btn-sm btn-danger' %>
+        </div>
+      </div>
+    </div>
+    <%= f.hidden_field :id if include_id %>
+  </div>
+</div>
diff --git a/app/views/admin/university/person/alumni/_list.html.erb b/app/views/admin/university/person/alumni/_list.html.erb
index bf9a289c1..6844f54b8 100644
--- a/app/views/admin/university/person/alumni/_list.html.erb
+++ b/app/views/admin/university/person/alumni/_list.html.erb
@@ -3,7 +3,6 @@
     <tr>
       <th><%= University::Person.human_attribute_name('last_name') %></th>
       <th><%= University::Person.human_attribute_name('first_name') %></th>
-      <th></th>
     </tr>
   </thead>
   <tbody>
@@ -12,18 +11,6 @@
       <tr>
         <td><%= link_to alumnus.last_name, path %></td>
         <td><%= link_to alumnus.first_name, path %></td>
-        <td class="text-end">
-          <div class="btn-group" role="group">
-            <%= link_to t('edit'),
-                      edit_admin_university_person_path(alumnus),
-                      class: button_classes if can?(:update, alumnus) %>
-            <%= link_to t('delete'),
-                      path,
-                      method: :delete,
-                      data: { confirm: t('please_confirm') },
-                      class: button_classes_danger if can?(:destroy, alumnus) %>
-          </div>
-        </td>
       </tr>
     <% end %>
   </tbody>
diff --git a/app/views/admin/university/person/alumni/edit_cohorts.html.erb b/app/views/admin/university/person/alumni/edit_cohorts.html.erb
new file mode 100644
index 000000000..5cc53c3ef
--- /dev/null
+++ b/app/views/admin/university/person/alumni/edit_cohorts.html.erb
@@ -0,0 +1,32 @@
+<% content_for :title, @alumnus %>
+
+<h2 class="h3"><%= Education::Program.model_name.human(count: 2) %></h2>
+
+<%= simple_form_for [:admin, @alumnus], url: edit_cohorts_admin_university_person_alumnus_path(@alumnus) do |f| %>
+  <%= f.error_notification %>
+  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
+
+  <div class="row">
+
+    <div class="col-md-2">
+      <%= link_to_add_association t('add'), f, :cohorts,
+                                  class: button_classes,
+                                  data: {
+                                    'association-insertion-method': 'append',
+                                    'association-insertion-node':   '#cohorts',
+                                  } %>
+
+    </div>
+
+    <div class="col-md-10">
+      <div id="cohorts">
+        <%= f.simple_fields_for :cohorts, @alumnus.cohorts.ordered, include_id: false do |cohort_f| %>
+          <%= render 'admin/university/person/alumni/cohort_fields', f: cohort_f, include_id: true %>
+        <% end %>
+      </div>
+    </div>
+  </div>
+  <% content_for :action_bar_right do %>
+    <%= submit f %>
+  <% end %>
+<% end %>
diff --git a/app/views/admin/university/person/alumni/index.html.erb b/app/views/admin/university/person/alumni/index.html.erb
index fcc1509bd..6fe0494bd 100644
--- a/app/views/admin/university/person/alumni/index.html.erb
+++ b/app/views/admin/university/person/alumni/index.html.erb
@@ -13,4 +13,5 @@
 <% end %>
 
 <% content_for :action_bar_right do %>
+  <%= link_to t('university.manage_alumni'), admin_university_people_path, class: button_classes if can?(:read, University::Person) %>
 <% end %>
diff --git a/app/views/admin/university/person/alumni/new.html.erb b/app/views/admin/university/person/alumni/new.html.erb
new file mode 100644
index 000000000..5a24f1f3b
--- /dev/null
+++ b/app/views/admin/university/person/alumni/new.html.erb
@@ -0,0 +1,3 @@
+<% content_for :title, University::Person.model_name.human %>
+
+<%= render 'form', person: @person %>
diff --git a/app/views/admin/university/person/alumni/show.html.erb b/app/views/admin/university/person/alumni/show.html.erb
index 920c3ee23..7ee7ad53c 100644
--- a/app/views/admin/university/person/alumni/show.html.erb
+++ b/app/views/admin/university/person/alumni/show.html.erb
@@ -1,6 +1,20 @@
 <% content_for :title, @alumnus %>
 
+<%= render 'admin/university/people/main_infos', person: @alumnus %>
+
+<%= render 'admin/university/person/alumni/show/cohorts', cohorts: @cohorts, param_name: 'cohorts_page' %>
+
+<%# TODO: remove %>
+<% if @alumnus.cohorts.any? %>
 <h2><%= Education::Cohort.model_name.human(count: @alumnus.cohorts.count) %></h2>
 <% @alumnus.cohorts.each do |cohort| %>
   <%= link_to cohort, [:admin, cohort] %>
 <% end %>
+<% end %>
+<%# end TODO %>
+
+
+<% content_for :action_bar_right do %>
+  <%= edit_link @alumnus %>
+  <%= link_to t('university.manage_cohorts'), edit_cohorts_admin_university_person_alumnus_path(@alumnus), class: button_classes if can?(:update, @alumnus) %>
+<% end %>
diff --git a/app/views/admin/university/person/alumni/show/_cohorts.html.erb b/app/views/admin/university/person/alumni/show/_cohorts.html.erb
new file mode 100644
index 000000000..a85f73d8a
--- /dev/null
+++ b/app/views/admin/university/person/alumni/show/_cohorts.html.erb
@@ -0,0 +1,48 @@
+<% param_name ||= :page %>
+
+<% if cohorts.total_count > 0 %>
+  <div class="card">
+    <div class="card-header">
+      <h2 class="card-title mb-0 h5"><%= "#{Education::Cohort.model_name.human(count: 2)} (#{cohorts.total_count})" %></h2>
+    </div>
+    <table class="table">
+      <thead>
+        <tr>
+          <th><%= Education::Cohort.human_attribute_name('program') %></th>
+          <th><%= Education::Cohort.human_attribute_name('academic_year') %></th>
+          <th></th>
+        </tr>
+      </thead>
+      <tbody>
+        <% cohorts.each do |cohort| %>
+          <% program = cohort.program %>
+          <tr>
+            <td><%= link_to_if can?(:read, program), program, [:admin, program] %></td>
+            <td><%= program.level_i18n %></td>
+            <td><%= cohort.academic_year %></td>
+            <td class="text-end">
+              <div class="btn-group" role="group">
+                <%= link_to t('edit'),
+                          '#',
+                          class: button_classes if can?(:update, cohort) %>
+                <%= link_to t('delete'),
+                          '#',
+                          method: :delete,
+                          data: { confirm: t('please_confirm') },
+                          class: button_classes_danger if can?(:destroy, cohort) %>
+              </div>
+            </td>
+          </tr>
+        <% end %>
+      </tbody>
+    </table>
+
+    <% if cohorts.total_pages > 1 %>
+      <div class="card-footer">
+        <%= paginate cohorts, theme: 'bootstrap-5', param_name: param_name %>
+      </div>
+    <% end %>
+
+  </div>
+  
+<% end %>
diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml
index 821184b44..c453abf00 100644
--- a/config/locales/university/en.yml
+++ b/config/locales/university/en.yml
@@ -139,6 +139,9 @@ en:
   university:
     internal_key: Internal Key
     invoice_informations: Invoice informations
+    manage_alumni: Manage alumni
+    manage_cohorts: Manage cohorts
+    manage_experiences: Manage experiences
     person:
       administrator_roles: Administrator roles
       taught_programs: Taught programs
diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml
index 11e6e050e..00d4d9d97 100644
--- a/config/locales/university/fr.yml
+++ b/config/locales/university/fr.yml
@@ -139,6 +139,9 @@ fr:
   university:
     internal_key: Clé interne
     invoice_informations: Données de facturation
+    manage_alumni: Gérer les alumni
+    manage_cohorts: Gérer les promotions
+    manage_experiences: Gérer les expériences professionnelles
     person:
       administrator_roles: Rôles administratifs
       taught_programs: Formations enseignées
diff --git a/config/routes/admin/university.rb b/config/routes/admin/university.rb
index dedeb7272..9ea5fdfa1 100644
--- a/config/routes/admin/university.rb
+++ b/config/routes/admin/university.rb
@@ -4,7 +4,14 @@ namespace :university do
     resources :imports, only: [:index, :show, :new, :create]
   end
   namespace :person do
-    resources :alumni
+    resources :alumni, only: [:index, :show] do
+      member do
+        get 'edit_cohorts' => 'alumni#edit_cohorts'
+        patch 'edit_cohorts' => 'alumni#update_cohorts'
+        get 'edit_experience' => 'alumni#edit_experiences'
+        patch 'edit_experiences' => 'alumni#update_experiences'
+      end
+    end
     namespace :alumnus do
       resources :imports, only: [:index, :show, :new, :create]
     end
-- 
GitLab