Skip to content

Commit

Permalink
Merge pull request #2 from macite/AkshatBajaj-feature/rollover-teachi…
Browse files Browse the repository at this point in the history
…ng-period

Akshat bajaj feature/rollover teaching period
  • Loading branch information
AkshatBajaj authored Nov 5, 2018
2 parents ef44945 + a48fc1c commit c1182ed
Show file tree
Hide file tree
Showing 26 changed files with 1,093 additions and 276 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
indent_size = 2
indent_style = space
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ group :development, :test do
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'minitest-around'
gem 'minitest-hyper'
gem 'minitest-osx'
gem 'minitest-rails'
gem 'byebug'
Expand Down
4 changes: 1 addition & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ GEM
minitest (5.9.0)
minitest-around (0.4.0)
minitest (~> 5.0)
minitest-hyper (0.2.0)
minitest-osx (0.1.0)
minitest (~> 5.4)
terminal-notifier (~> 1.6)
Expand Down Expand Up @@ -280,7 +279,6 @@ DEPENDENCIES
hirb
json-jwt (= 1.7.0)
minitest-around
minitest-hyper
minitest-osx
minitest-rails
moss_ruby (= 1.1.2)
Expand All @@ -306,4 +304,4 @@ RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.16.3
1.16.6
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ To run unit tests, execute:
$ rake test
```

A report will be generated under `spec/reports/hyper/index.html`.

Unit tests are located in the `test` directory, where **model** tests are under
the `model` subdirectory and **API** tests are under the `api` subdirectory.

Expand Down
1 change: 1 addition & 0 deletions app/api/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Settings < Grape::API
}
end
result
end

desc 'Return privacy policy details'
get '/settings/privacy' do
Expand Down
2 changes: 1 addition & 1 deletion app/api/task_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class TaskDefinitions < Grape::API
id: t.id,
task_definition_id: t.task_definition_id,
tutorial_id: t.tutorial_id,
status: TaskStatus.find(t.status_id).status_key,
status: TaskStatus.id_to_key(t.status_id),
completion_date: t.completion_date,
submission_date: t.submission_date,
times_assessed: t.times_assessed,
Expand Down
2 changes: 1 addition & 1 deletion app/api/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Tasks < Grape::API
id: r.id,
tutorial_id: r.tutorial_id,
task_definition_id: r.task_definition_id,
status: TaskStatus.find(r.status_id).status_key
status: TaskStatus.id_to_key(r.status_id)
}
end
end
Expand Down
18 changes: 18 additions & 0 deletions app/models/break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ def ensure_break_end_is_within_teaching_period
errors.add(:number_of_weeks, "is exceeding Teaching Period end date")
end
end

def duration
number_of_weeks.weeks
end

def first_monday
return start_date if start_date.wday == 1
return start_date + 1.day if start_date.wday == 0
return start_date + (8 - start_date.wday).days
end

def monday_after_break
first_monday + number_of_weeks.weeks
end

def end_date
start_date + duration
end
end
1 change: 1 addition & 0 deletions app/models/group_set.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class GroupSet < ActiveRecord::Base
belongs_to :unit
has_many :task_definitions
has_many :groups, dependent: :destroy

validates_associated :groups
Expand Down
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def task_details_for_shallow_serializer(user)
t = Task.find(r.id)
{
id: r.id,
status: TaskStatus.find(r.status_id).status_key,
status: TaskStatus.id_to_key(r.status_id),
task_definition_id: r.task_definition_id,
include_in_portfolio: r.include_in_portfolio,
pct_similar: t.pct_similar,
Expand Down
65 changes: 57 additions & 8 deletions app/models/task_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ class TaskDefinition < ActiveRecord::Base

validate :ensure_no_submissions, if: :has_change_group_status?

def copy_to(other_unit)
new_td = self.dup

# change the unit...
new_td.unit_id = other_unit.id # for database
new_td.unit = other_unit # for other operations
other_unit.task_definitions << new_td # so we can see it in unit elsewhere

# Adjust dates
new_td.start_week_and_day = start_week, start_day
new_td.target_week_and_day = target_week, target_day

if self['due_date'].present?
new_td.due_week_and_day = due_week, due_day
end

# Ensure we have the dir for the destination task sheet
FileHelper.task_file_dir_for_unit(other_unit, create = true)

if has_task_sheet?
FileUtils.cp(task_sheet, new_td.task_sheet())
end

if has_task_resources?
FileUtils.cp(task_resources, new_td.task_resources)
end

new_td.save

new_td
end

def has_change_group_status?
group_set_id != group_set_id_was
end
Expand Down Expand Up @@ -277,35 +309,50 @@ def self.to_csv(task_definitions, options = {})
end

def start_week
((start_date - unit.start_date) / 1.week).floor
unit.week_number(start_date)
end

def start_day
Date::ABBR_DAYNAMES[start_date.wday]
end

def start_week_and_day= value
week, day = value
self.start_date = unit.date_for_week_and_day(week, day)
end

def target_week
((target_date - unit.start_date) / 1.week).floor
unit.week_number(target_date)
end

def target_day
Date::ABBR_DAYNAMES[target_date.wday]
end

def target_week_and_day= value
week, day = value
self.target_date = unit.date_for_week_and_day(week, day)
end

# Override due date to return either the final date of the unit, or the set due date
def due_date
return self['due_date'] if self['due_date'].present?
return unit.end_date
end

def due_week
if due_date
((due_date - unit.start_date) / 1.week).floor
if due_date.present?
unit.week_number(due_date)
else
''
end
end

def due_week_and_day= value
week, day = value
self.due_date = unit.date_for_week_and_day(week, day)
end

def due_day
if due_date
Date::ABBR_DAYNAMES[due_date.wday]
Expand Down Expand Up @@ -439,19 +486,21 @@ def self.task_def_for_csv_row(unit, row)
result.plagiarism_warn_pct = row[:plagiarism_warn_pct]
result.plagiarism_checks = row[:plagiarism_checks]

unless row[:group_set].present?
result.group_set = unit.group_sets.where(name: row[:group_set]).first
if row[:group_set].present?
result.group_set = unit.group_sets.where(name: row[:group_set]).first
end

if result.valid? && (row[:group_set].nil? || !result.group_set.nil?)
if result.valid? && (row[:group_set].blank? || result.group_set.present?)
begin
result.save
rescue
result.destroy
return [nil, false, 'Failed to save definition due to data error.']
end
else
if result.group_set.nil? && !row[:group_set].nil?
# delete the task if it was new
result.destroy if new_task
if result.group_set.nil? && row[:group_set].present?
return [nil, false, "Unable to find groupset with name #{row[:group_set]} in unit."]
else
return [nil, false, result.errors.full_messages.join('. ')]
Expand Down
18 changes: 18 additions & 0 deletions app/models/task_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ def self.staff_assigned_statuses
TaskStatus.where('id > 4')
end

def self.id_to_key(id)
case id
when 1 then :not_started
when 2 then :complete
when 3 then :need_help
when 4 then :working_on_it
when 5 then :fix_and_resubmit
when 6 then :do_not_resubmit
when 7 then :redo
when 8 then :discuss
when 9 then :ready_to_mark
when 10 then :demonstrate
when 11 then :fail
when 12 then :time_exceeded
else :not_started
end
end

def status_key
return :complete if self == TaskStatus.complete
return :not_started if self == TaskStatus.not_started
Expand Down
79 changes: 77 additions & 2 deletions app/models/teaching_period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class TeachingPeriod < ActiveRecord::Base
validate :validate_end_date_after_start_date, :validate_active_until_after_end_date

def validate_end_date_after_start_date
if end_date < start_date
if end_date.present? && start_date.present? && end_date < start_date
errors.add(:end_date, "should be after the Start date")
end
end

def validate_active_until_after_end_date
if active_until < end_date
if end_date.present? && active_until.present? && active_until < end_date
errors.add(:active_until, "date should be after the End date")
end
end
Expand All @@ -33,6 +33,81 @@ def add_break(start_date, number_of_weeks)
break_in_teaching_period
end

def week_number(date)
# Calcualte date offset, add 2 so 0-week offset is week 1 not week 0
result = ((date - start_date) / 1.week).floor + 1

for a_break in breaks.all do
if date >= a_break.start_date
# we are in or after the break, so calculated week needs to
# be reduced by this break

if date >= a_break.end_date
# past the end of the break...
result -= a_break.number_of_weeks
elsif date == a_break.start_date
# cant use standard calculation as this give 0 for this exact moment...
result -= 1 if date >= a_break.first_monday
elsif date >= a_break.first_monday
# in break so partial reduction
result -= ((date - a_break.first_monday) / 1.week).ceil
end

# for times just past the break but before start of next week...
if date >= a_break.end_date && date < a_break.monday_after_break
# Need to add 1 as we are now in a new week!
result += 1
end
end
end

result
end

def date_for_week(num)
num = num.floor

# start by switching from 1 based to 0 based
# week 1 is offset 0 weeks from the start
num -= 1

result = start_date + num.weeks

# check breaks
for a_break in breaks do
if result >= a_break.start_date
# we are in or after the break, so calculated date is
# extended by the break period
result += a_break.number_of_weeks.weeks
end
end

result
end

def date_for_week_and_day(week, day)
return nil if week.nil? || day.nil?

week_start = date_for_week(week)

day_num = Date::ABBR_DAYNAMES.index day.titlecase
return nil if day_num.nil?

start_day_num = start_date.wday

result = week_start + (day_num - start_day_num).days

for a_break in breaks do
if result >= a_break.start_date && result < a_break.end_date
# we are in or after the break, so calculated date is
# extended by the break period
result += a_break.number_of_weeks.weeks
end
end

result
end

def rollover(rollover_to)
rollover_to.add_associations(self)
rollover_to.save!
Expand Down
Loading

0 comments on commit c1182ed

Please sign in to comment.