Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-uoy / lib / uoy.rb
Size: Mime:
# frozen_string_literal: true

require 'uoy/version'
require 'uoy/york_data'

# Provides common functions across Faculty Apps
module Uoy
  module_function

  # Return the common name given to a term number
  def term_name(term)
    case term
    when 1
      'Autumn'
    when 2
      'Spring'
    when 3
      'Summer'
    when 4
      'Summer Vacation'
    end
  end

  # Take nasty SITS formatted classificaitons e.g. MSCR, BSC
  # return classifications as they should be written, MSc (Res), BSc
  def format_classification(input)
    return CLASSIFICATIONS[input] if CLASSIFICATIONS.key?(input)

    input
  end

  # Returns the current academic year, e.g. 2019 for the 2019/20 academic year
  # regardless of the current date. Can also be passed a date and return the
  # correct academic year for that date.
  def current_academic_year(now = Date.today)
    return now.year if UNI_TERM_DATES[now.year.to_s]['Autumn'] <= now

    now.year - 1
  rescue NoMethodError
    raise "Could not return current academic year.
    No academic year data for the date #{now}"
  end

  # Returns the current academic year in SITS format, e.g. 2019/0, 2020/1
  # Can also be passed any date and return the SITS formatted academic year
  def current_academic_year_sits(now = Date.today)
    year = current_academic_year(now)
    "#{year}/#{(year + 1) % 10}"
  end

  # Convert a normal year, e.g. 2019 into a SITS year, e.g. 2019/0
  def year_sits(year = current_academic_year)
    year = year.to_i
    "#{year}/#{(year + 1) % 10}"
  end

  # Return a module assessment code given a module code and a year in normal, or SITS format
  def module_assessment_code(module_code, year)
    case year
    when %r{\d{4}/\d}
      # Year in sits format (YYYY/Y)
      mac_year = year.delete('/').chars.last(3).join
    when /\d{4}/
      mac_year = "#{year.to_i % 100}#{(year.to_i + 1) % 10}"
    else
      raise 'Invalid year supplied.'
    end

    "#{module_code}#{mac_year}"
  end

  # Return the current term number
  def current_term(now = Date.today)
    return 3 if UNI_TERM_DATES[current_academic_year(now).to_s]['Summer'] <= now
    return 2 if UNI_TERM_DATES[current_academic_year(now).to_s]['Spring'] <= now

    1 if UNI_TERM_DATES[current_academic_year(now).to_s]['Autumn'] <= now
  end

  # Return the current week number
  def current_week(now = Date.today)
    year = current_academic_year(now)
    term = current_term(now)
    start_of_term = UNI_TERM_DATES[year.to_s][term_name(term)]
    start_of_term.step(now, 7).count
  end
end