Skip to content

Commit

Permalink
✨ Create terms from OneRoster API, #148
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshizaki committed Oct 9, 2018
1 parent 802edaf commit d10e2eb
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 23 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gem 'shrine', '~> 2.11.0'
# gem 'therubyracer', platforms: :ruby
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '~> 4.1.14'
gem 'whenever', require: false

group :development, :test do
gem 'sqlite3', '~> 1.3.0'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GEM
chromedriver-helper (2.1.0)
archive-zip (~> 0.10)
nokogiri (~> 1.8)
chronic (0.10.2)
climate_control (0.2.0)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
Expand Down Expand Up @@ -280,6 +281,8 @@ GEM
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
whenever (0.10.0)
chronic (>= 0.6.3)
xpath (3.1.0)
nokogiri (~> 1.8)

Expand Down Expand Up @@ -326,6 +329,7 @@ DEPENDENCIES
uglifier (~> 4.1.14)
unicorn (~> 5.4.0)
web-console (>= 3.6.0)
whenever

BUNDLED WITH
1.16.2
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def course_card_hash(course)
card['header'] = course.title
card['body'] = course.overview
card['summary'] = true
card['footnotes'] = [t('activerecord.models.term') + ' : ' + course.term.title]
card['footnotes'] = [term_display_title(course.term.title)]
card
end

Expand Down
29 changes: 29 additions & 0 deletions app/helpers/terms_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'date'
module TermsHelper
# ====================================================================
# Public Functions
# ====================================================================

def term_display_title term_title
# FIXME: term format condition, xxxx-yyyyy
term_words = term_title.split '-'
t('helpers.term_year', year: term_words[0]) + t("helpers.term_category_#{term_words[1]}")
end

def selectable_terms(category)
terms = Term.all.order(start_at: :desc)
selectables = []
day = Date.today
terms.each do |term|
title = term_display_title term.title
case category
when 'hereafter'
# new course can be created from 10 months prior to term.start_at
selectables.push([title, term.id]) if ((term.start_at - 10.months)..term.end_at).cover? day
when 'all'
selectables.push([title, term.id])
end
end
selectables
end
end
2 changes: 2 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationJob < ActiveJob::Base
end
39 changes: 39 additions & 0 deletions app/jobs/roster_import_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rest-client'
require 'json'

class RosterImportJob < ApplicationJob
# ====================================================================
# Public Functions
# ====================================================================
queue_as :default

def perform(*args)
terms = get_request '/terms'
now = Time.zone.now
open_terms = terms.reject{|t| (now < Time.zone.parse(t['startDate']) - 1.month) || (Time.zone.parse(t['endDate']) <= now)}
open_terms.each do |t|
Term.create_from_roster t
classes = get_request "/terms/#{t['sourcedId']}/classes"
# p classes
end
open_terms
end

# ====================================================================
# Private Functions
# ====================================================================

private

def get_request(endpoint)
url = SYSTEM_ROSTER_URL_PREFIX + endpoint
# FIXME: verify_ssl should be true!
response = RestClient::Request.execute(
:url => url,
:method => :get,
:headers => {Authorization: 'Bearer ' + SYSTEM_ROSTER_TOKEN},
:verify_ssl => false
)
JSON.parse(response.body)
end
end
20 changes: 5 additions & 15 deletions app/models/term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,23 @@
# end_at :date
# created_at :datetime not null
# updated_at :datetime not null
# guid :string
#

require 'date'
class Term < ApplicationRecord
has_many :courses
validates_presence_of :end_at
validates_presence_of :start_at
validates_presence_of :title
validates_uniqueness_of :title
validates_uniqueness_of :guid

# ====================================================================
# Public Functions
# ====================================================================
def self.selectables(category)
terms = Term.all.order(start_at: :desc)
selectables = []
day = Date.today
terms.each do |term|
case category
when 'hereafter'
# new course can be created from 10 months prior to term.start_at
selectables.push([term.title, term.id]) if ((term.start_at - 10.months)..term.end_at).cover? day
when 'all'
selectables.push([term.title, term.id])
end
end
selectables
def self.create_from_roster(roster_term)
return unless where(guid: roster_term['sourcedId']).count.zero?
Term.create(guid: roster_term['sourcedId'], title: roster_term['title'], start_at: roster_term['startDate'], end_at: roster_term['endDate'])
end

def deletable?(user_id)
Expand Down
2 changes: 1 addition & 1 deletion app/uploaders/outcome_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def generate_location(io, context)
class_name = 'users'
directory_name1 = context[:record].outcome.manager_id
directory_name2 = context[:record].outcome.folder_name
# Use original filename instead of UUID
# Use original filename instead of GUID
file_name = context[:metadata]['filename']

[class_name, directory_name1, directory_name2, file_name].compact.join("/")
Expand Down
2 changes: 1 addition & 1 deletion app/views/courses/_course_candidates.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= render partial: 'layouts/image', locals: {obj: course, fa_class: 'fa-flag', img_px: '40'} %>
</td>
<td class="name-td">
<%= course.term.title %>
<%= term_display_title course.term.title %>
</td>
<% managers = course_managers_display_list(course) %>
<td class="name-td">
Expand Down
4 changes: 2 additions & 2 deletions app/views/courses/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<%= f.label :term_id, raw("<span class='required'>*</span>") + t('activerecord.attributes.course.term_id'), class: 'col-md-3 col-form-label text-sm-right' %>
<div class="col-md-8">
<% if @course.new_record? %>
<%= f.select :term_id, Term.selectables('hereafter'), {}, {class: 'form-control'} %>
<%= f.select :term_id, selectable_terms('hereafter'), {}, {class: 'form-control'} %>
<% else %>
<%= f.select :term_id, Term.selectables('all'), {}, {class: 'form-control'} %>
<%= f.select :term_id, selectable_terms('all'), {}, {class: 'form-control'} %>
<% end %>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/courses/_option_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<div id="option_form" style="display: none; margin-left:20px;">
<div style="margin-bottom:1rem;">
<h3>1. コースの複製</h3>
<% if Term.selectables("hereafter").size > 0 %>
<% if selectable_terms('hereafter').size > 0 %>
<%= form_for :course, url: {action: 'ajax_duplicate', original_id: @course.id}, remote: true do |f| %>
<p>
<%= t('.duplication_settings') %>
</p>
<div class="form-group form-row" style="width: 100%;">
<%= f.label :term_id, class: 'col-md-2 col-form-label text-sm-right' %>
<%= f.select :term_id, Term.selectables('hereafter'), {}, {class: 'col-md-3 form-control input-lg'} %>
<%= f.select :term_id, selectable_terms('hereafter'), {}, {class: 'col-md-3 form-control input-lg'} %>
<%= f.label :title, class: 'col-md-2 col-form-label text-sm-right' %>
<%= f.text_field :title, {class: 'col-md-4 form-control input-lg', placeholder: @course.title} %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/courses/_search_courses.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="form-group form-row" style="width: 100%;">
<%# --- term --- %>
<%= label :term_id, t('activerecord.models.term'), class: 'col-md-2 col-form-label text-sm-right' %>
<%= select_tag :term_id, options_for_select(select_options_unspecified(Term.selectables("all"))), class: 'col-md-3 form-control input-lg' %>
<%= select_tag :term_id, options_for_select(select_options_unspecified(selectable_terms('all'))), class: 'col-md-3 form-control input-lg' %>
<%# --- title --- %>
<%= label :title, t('activerecord.attributes.course.title'), class: 'col-md-2 col-form-label text-sm-right' %>
<%= text_field_tag :title, @search_word, class: 'col-md-4 form-control input-lg', placeholder: t('.placeholder_title') %>
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
# IP addresses that system staffs can singnin
SYSTEM_STAFF_SIGNIN_IP = ['127.0.0.1'].freeze

# URL for IMS OneRoster API server
SYSTEM_ROSTER_URL_PREFIX = ''

# Access token for IMS OneRoster API
SYSTEM_ROSTER_TOKEN = ''

# ===== Versatile constatns =====
# time delay for autocomplete
AUTOCOMPLETE_DELAY = 100
Expand Down
6 changes: 6 additions & 0 deletions config/locales/helpers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ en:
system_notice_summary: system notices shown in signin page
teacher_evaluation: Teacher Eval.
term: Term
term_category_first: First Term
term_category_full: Fullyear Term
term_category_intensive: Intensive Term
term_category_second: Second Term
term_category_years: Two Years Term
term_summary: school terms for courses
term_year: "%{year}: "
unspecified: Unspecified
update: System Update
update_summary: Correction of DB etc. after updating LePo
Expand Down
6 changes: 6 additions & 0 deletions config/locales/helpers/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ ja:
system_notice_summary: サインイン画面に表示するお知らせ
teacher_evaluation: 教師評価
term: 学期
term_category_first: 前期
term_category_full: 通年
term_category_intensive: 集中
term_category_second: 後期
term_category_years: 年度跨ぎ
term_summary: コースを開講する学期
term_year: "%{year}年度"
unspecified: 指定なし
update: システム更新
update_summary: LePo更新後のDB等の修正
Expand Down
27 changes: 27 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
# command "/usr/bin/some_great_command"
# runner "MyModel.some_method"
# rake "some:great:rake:task"
# end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
# end

# Learn more: http://github.com/javan/whenever

set :output, "#{path}/log/cron_log.log"
set :environment, :production

every 2.hours do
runner 'RosterImportJob.perform_now'
end
6 changes: 6 additions & 0 deletions db/migrate/20181008141151_add_guid_to_terms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddGuidToTerms < ActiveRecord::Migration[5.0]
def change
add_column :terms, :guid, :string, after: :id
add_index :terms, :guid, unique: true
end
end
1 change: 1 addition & 0 deletions test/factories/terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# end_at :date
# created_at :datetime not null
# updated_at :datetime not null
# guid :string
#

FactoryBot.define do
Expand Down
7 changes: 7 additions & 0 deletions test/jobs/roster_import_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class RosterImportJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end
1 change: 1 addition & 0 deletions test/models/term_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# end_at :date
# created_at :datetime not null
# updated_at :datetime not null
# guid :string
#

require 'test_helper'
Expand Down

0 comments on commit d10e2eb

Please sign in to comment.