Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Todos programme, activities and action index pages #4192

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/controllers/activity_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ def index
@pupil_categories = ActivityCategory.pupil.by_name
@activity_categories = ActivityCategory.featured.by_name.select { |activity_category| activity_category.activity_types.active.count >= 4 }
@activity_count = ActivityType.active_and_not_custom.count
@programme_types = ProgrammeType.featured
if Flipper.enabled?(:todos, current_user)
@programme_types = ProgrammeType.featured.with_task_type(ActivityType)
else
@programme_types = ProgrammeType.featured
end
end

def show
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/intervention_type_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class InterventionTypeGroupsController < ApplicationController

def index
@intervention_type_groups = @intervention_type_groups.by_name
if Flipper.enabled?(:todos, current_user)
@programme_types = ProgrammeType.featured.with_task_type(InterventionType)
end
end

def show
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/programme_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ def index
end

def show
route_not_found unless @programme_type.active
if Flipper.enabled?(:todos)
route_not_found unless @programme_type.active && @programme_type.has_todos?
else
route_not_found unless @programme_type.active
end
end

private

def user_progress
@user_progress = Programmes::UserProgress.new(current_user)
if !Flipper.enabled?(:todos) || Flipper.enabled?(:todos_old)
@user_progress = Programmes::UserProgress.new(current_user)
end
end
end
12 changes: 12 additions & 0 deletions app/models/concerns/todos/assignable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ module Assignable

accepts_nested_attributes_for :activity_type_todos, allow_destroy: true
accepts_nested_attributes_for :intervention_type_todos, allow_destroy: true

scope :with_task_type, ->(task_type) {
where(id: ProgrammeType.joins(:todos).where(todos: { task_type: task_type.name }).distinct)
}
end

def has_todos?
todos.any?
end

def no_todos?
todos.none?
end

# Has the provided school already completed all activity & intervention types this year?
Expand Down
24 changes: 4 additions & 20 deletions app/models/concerns/todos/completable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,24 @@ module Completable
scope :completable, -> { raise NoMethodError, 'Implement completable scope in subclass!' }
end

def assignable_todos
assignable.todos
end

def assignable_todo_ids
assignable_todos.ids
end

def completed_todo_ids
completed_todos.pluck(:todo_id)
end

def has_todos?
assignable_todo_ids.any?
end

def nothing_todo?
assignable_todo_ids.none?
end

def completable?
has_todos? && todos_complete?
assignable.has_todos? && todos_complete?
end

def todos_incomplete?
(assignable_todo_ids - completed_todo_ids).any?
(assignable.todos.ids - completed_todo_ids).any?
end

def todos_complete?
(assignable_todo_ids - completed_todo_ids).empty?
(assignable.todos.ids - completed_todo_ids).empty?
end

def uncompleted_todos
assignable_todos - todos_completed
assignable.todos - todos_completed
end

def uncompleted_tasks
Expand Down
6 changes: 4 additions & 2 deletions app/models/programme_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ProgrammeType < ApplicationRecord
translates :document_link, type: :string, fallbacks: { cy: :en }

t_has_one_attached :image

## these two relationships to be removed when todos feature removed
has_many :programme_type_activity_types
has_many :activity_types, through: :programme_type_activity_types

Expand Down Expand Up @@ -98,8 +100,8 @@ def activity_types_and_school_activity(school)
end

def repeatable?(school)
# Does the school have any programmes for this programme type completed before the beginning of this academic year?
school.programmes.where(programme_type: self).completed.where(ended_on: ..school.current_academic_year.start_date).any?
# Only allow a repeat if the school hasn't completed this programe type this academic year
school.programmes.where(programme_type: self).completed.where(ended_on: school.current_academic_year.start_date..).none?
end

# Provide a list of activity types a school has already completed this year for this programme type
Expand Down
7 changes: 7 additions & 0 deletions app/models/school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,18 @@ class ProcessDataError < StandardError; end
has_many :school_target_events, inverse_of: :school
has_many :audits, inverse_of: :school

# relationships to be removed when :todos removed
has_many :audit_activity_types, -> { distinct }, through: :audits, source: :activity_types
has_many :audit_intervention_types, -> { distinct }, through: :audits, source: :intervention_types

has_many :audit_todos, through: :audits, source: :todos
has_many :audit_activity_type_tasks, through: :audit_todos, source: :task, source_type: 'ActivityType'
has_many :audit_intervention_type_tasks, through: :audit_todos, source: :task, source_type: 'InterventionType'

has_many :programmes, inverse_of: :school
has_many :programme_types, through: :programmes

# relationships to be removed when :todos removed
has_many :programme_activity_types, through: :programmes, source: :activity_types

has_many :alerts, inverse_of: :school
Expand Down
2 changes: 1 addition & 1 deletion app/services/completables/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def uncompleted_scores
end

def available_count
completable.assignable_todos.count
completable.assignable.todos.count
end

def completed_count
Expand Down
6 changes: 5 additions & 1 deletion app/services/recommendations/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ def alert_tasks(alert)
end

def audit_tasks
school.audit_intervention_types
if Flipper.enabled?(:todos)
school.audit_intervention_type_tasks
else
school.audit_intervention_types
end
end

def task_tasks(task)
Expand Down
6 changes: 5 additions & 1 deletion app/services/recommendations/activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def alert_tasks(alert)
end

def audit_tasks
with_key_stage(school.audit_activity_types)
if Flipper.enabled?(:todos)
with_key_stage(school.audit_activity_type_tasks)
else
with_key_stage(school.audit_activity_types)
end
end

def task_tasks(task)
Expand Down
14 changes: 0 additions & 14 deletions app/views/activity_categories/_programme_types.html.erb

This file was deleted.

16 changes: 1 addition & 15 deletions app/views/activity_categories/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,7 @@
<%= render 'pupil_categories', activity_categories: @pupil_categories.first(4) %>
<% end %>

<% if !@programme_types.blank? %>
<div class="row mt-3 bg-light">
<div class="col">
<div class="d-flex justify-content-between align-items-center">
<h4><%= t('activity_categories.our_programmes.title') %></h4>
<div>
<%= link_to t('activity_categories.view_all_programmes'), programme_types_path,
class: 'btn btn-default' %>
</div>
</div>
<p><%= t('activity_categories.our_programmes.introduction') %></p>
</div>
</div>
<%= render 'programme_types', programme_types: @programme_types.first(4) %>
<% end %>
<%= render 'shared/tasks/programme_types', programme_types: @programme_types %>

<% @activity_categories.each do |activity_category| %>
<div class="row mt-3">
Expand Down
4 changes: 4 additions & 0 deletions app/views/intervention_type_groups/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

<%= render 'schools/prompt_recommended', scope: :adult %>

<% if Flipper.enabled?(:todos, current_user) %>
<%= render 'shared/tasks/programme_types', programme_types: @programme_types %>
<% end %>

<% @intervention_type_groups.each do |intervention_type_group| %>
<div class="row mt-3">
<div class="col">
Expand Down
7 changes: 7 additions & 0 deletions app/views/programme_types/_intention.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if programme_type.activity_type_todos.none? %>
<%= render IconComponent.new(name: 'chalkboard-user') %> <%= t('programme_types.intention.adults') %>
urbanwide marked this conversation as resolved.
Show resolved Hide resolved
<% elsif programme_type.intervention_type_todos.none? %>
<%= render IconComponent.new(name: 'book-open-reader') %> <%= t('programme_types.intention.pupils') %>
<% else %>
<%= render IconComponent.new(name: 'users') %> <%= t('programme_types.intention.all') %>
urbanwide marked this conversation as resolved.
Show resolved Hide resolved
<% end %>
45 changes: 45 additions & 0 deletions app/views/programme_types/_programme_type.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<% programme = programme_type.programme_for_school(current_user_school) if current_user_school %>

<div class="row mt-4">
<div class="col-sm-2">
<%= link_to programme_type_path(programme_type) do %>
<%= render 'programme_types/image', programme_type: programme_type, css_class: 'activity-card-img' %>
<% end %>
</div>
<div class="col-sm-8">
<h4><strong>
<%= link_to programme_type.title, programme_type_path(programme_type), style: 'text-decoration: none;' %>
</strong></h4>
<% if programme&.completed? %>
<p>
<span class="h4 check">
<%= fa_icon('flag-checkered text-success') %>
</span>
<%= t('programme_types.cards.you_have_already_completed_this_programme') %>
</p>
<% elsif programme&.started? %>
<p>
<span class="h4 check">
<%= far_icon('check-circle text-success') %>
</span>
<%= t('programme_types.cards.you_have_already_started_this_programme') %>
</p>
<% end %>
<p>
<%= programme_type.short_description %>
</p>
<p>
<%= render 'intention', programme_type: %>
</p>
<p><%= t('programme_types.cards.bonus_points_html',
bonus_score: tag.span(programme_type.bonus_score, class: 'badge badge-success')) %></p>
</div>
<div class="col-sm-2 align-self-center">
<% if programme&.started? %>
<%= link_to t('programme_types.cards.continue'), programme_type_path(programme_type),
class: 'btn btn-primary btn-lg' %>
<% else %>
<%= link_to t('programme_types.cards.view'), programme_type_path(programme_type), class: 'btn btn-primary btn-lg' %>
<% end %>
</div>
</div>
8 changes: 7 additions & 1 deletion app/views/programme_types/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
</div>
</div>

<%= render "cards", programme_types: @programme_types, user_progress: @user_progress %>
<% if Flipper.enabled?(:todos, current_user) %>
<% @programme_types.each do |programme_type| %>
<%= render('programme_type', programme_type:) if programme_type.has_todos? %>
<% end %>
<% else %>
<%= render 'cards', programme_types: @programme_types, user_progress: @user_progress %>
<% end %>
5 changes: 4 additions & 1 deletion app/views/programme_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<div class="col-md-6">
<h2><%= @programme_type.title %></h2>
<p><%= @programme_type.short_description %></p>
<% if Flipper.enabled?(:todos) %>
<p><%= render 'intention', programme_type: @programme_type %></p>
<% end %>
<p><%= t('programme_types.cards.bonus_points_html',
bonus_score: tag.span(@programme_type.bonus_score, class: 'badge badge-success')) %></p>
</div>
Expand Down Expand Up @@ -43,7 +46,7 @@
<%= render 'programme_types/prompts/prompt', programme_type: @programme_type, school: current_user_school %>
<% elsif programme&.completed? %>
<%= render 'programme_types/prompts/completed', programme: programme %>
<% elsif programme %>
<% elsif programme&.started? %>
<%= render 'programme_types/prompts/started', programme: programme %>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion app/views/schools/audits/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<% if Flipper.enabled?(:todos, current_user) %>
<% if @audit.has_todos? %>
<p>
<%= t('schools.audits.show.completing_all_of_these_activities_will_award_you') %>
<%= t('schools.audits.show.completed_points') %>
<span class="badge badge-success"><%= SiteSettings.current&.audit_activities_bonus_points %></span>
<%= t('schools.audits.show.points', count: SiteSettings.current&.audit_activities_bonus_points) %>
</p>
Expand Down
29 changes: 29 additions & 0 deletions app/views/shared/tasks/_programme_types.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% if programme_types&.any? && programme_types.count >= 4 %>
<div class="row mt-3 bg-light">
<div class="col">
<div class="d-flex justify-content-between align-items-center">
<h4><%= t('activity_categories.our_programmes.title') %></h4>
<div>
<%= link_to t('activity_categories.view_all_programmes'), programme_types_path,
class: 'btn btn-default' %>
</div>
</div>
<p><%= t('activity_categories.our_programmes.introduction') %></p>
</div>
</div>

<div class="card-deck justify-content-center bg-light pb-4 activities-deck" id="programme-types">
<% programme_types.first(4).each do |programme_type| %>
<div class="card col-sm-3">
<%= link_to programme_type_path(programme_type) do %>
<%= render 'programme_types/image', programme_type: programme_type, css_class: '' %>
<% end %>
<div class="card-body">
<p class="card-text">
<%= programme_type.title %>
</p>
</div>
</div>
<% end %>
</div>
<% end %>
4 changes: 4 additions & 0 deletions config/locales/views/programme_types/programme_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ en:
introduction_2: As an Energy Sparks user you can enrol in any of our programmes and record activities to track your progress and score points for your school
page_title: Programmes
title: Our Energy Saving Programmes
intention:
adults: This programme is intended for adults
all: This programme is intended for the whole school
pupils: This programme is intended for pupils
prompt:
completed_message_html:
one: You've completed all the activities in this programme. Mark it done to score <strong>%{count}</strong> bonus point?
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/schools/schools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ en:
show:
completed_message: Congratulations, you completed all recommended actions and activities on %{date}!
completed_on: Completed on
completed_points: Completing all of these tasks will award you
completing_all_of_these_activities_will_award_you: Completing all of these activities will award you
download_report: Download report
points:
Expand Down
12 changes: 12 additions & 0 deletions spec/factories/programme_types_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
end
end

trait :with_activity_type_todos do
after(:create) do |programme_type, _evaluator|
create_list(:activity_type_todo, 3, assignable: programme_type)
end
end

trait :with_intervention_type_todos do
after(:create) do |programme_type, _evaluator|
create_list(:intervention_type_todo, 3, assignable: programme_type)
end
end

# old way - remove when :todos feature removed
factory :programme_type_with_activity_types do
transient do
Expand Down
Loading
Loading