Skip to content

Commit

Permalink
✨ Sync Enrollment data to API server by POST/PUT/DELETE method, #151
Browse files Browse the repository at this point in the history
- Request to original API with HTTP POST/PUT/DELETE method for Enrollment resource
- Add sourced_id to Enrollment
  • Loading branch information
kyoshizaki committed May 3, 2019
1 parent e29269b commit 1f943f6
Show file tree
Hide file tree
Showing 31 changed files with 360 additions and 161 deletions.
7 changes: 5 additions & 2 deletions app/assets/stylesheets/resources/enrollments.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// CSS for course-members-resource
// CSS for enrollment-resource
//

#resource #course-member-resource {
#resource #enrollment-resource {
.edit-group {
a {
color: #fff;
Expand All @@ -24,4 +24,7 @@
}
}
}
.role-info td {
text-align: center;
}
}
4 changes: 2 additions & 2 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def new
def create
@course = Course.new(course_params)
begin
raise unless @course.creatable? session[:id]
raise unless Course.creatable? session[:id]
@course.status = @course.term.status
Course.transaction do
@course.save!
Expand All @@ -26,7 +26,7 @@ def create
response = request_roster_api('/classes/', :post, payload)
@course.update_attributes!(sourced_id: response['class']['sourcedId'])
@course.managers.each do |manager|
payload = {enrollment: Enrollment.to_roster_hash(@course.sourced_id, manager, 'manager')}
payload = {enrollment: Enrollment.new(course_id: @course.id, user_id: manager.id, role: 'manager').to_roster_hash}
request_roster_api('/enrollments/', :post, payload)
end
end
Expand Down
153 changes: 96 additions & 57 deletions app/controllers/enrollments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,95 @@
require 'csv'
class EnrollmentsController < ApplicationController
include RosterApi
include ::UsersController::AllActions
include ::StickiesController::AllActions
# ====================================================================
# Public Functions
# ====================================================================
def new
get_resources
@form_category = ''
@member_role = 'learner'
render 'layouts/renders/main_pane', locals: { resource: 'enrollments/new' }
end

def create
enrollment = Enrollment.find_or_initialize_by(course_id: params[:enrollment][:course_id], user_id: params[:enrollment][:user_id])
role = params[:enrollment][:role]
begin
raise unless enrollment.new_record?
raise unless Enrollment.creatable? enrollment.course_id, session[:id], role
Enrollment.transaction do
enrollment.role = role
enrollment.save!
if SYSTEM_ROSTER_SYNC == :on
payload = {enrollment: enrollment.to_roster_hash}
response = request_roster_api('/enrollments/', :post, payload)
enrollment.update_attributes!(sourced_id: response['enrollment']['sourcedId'])
end
end
rescue => error
notify_error error, t('controllers.enrollments.creation_failed')
end

case params[:form_category]
when 'search'
ajax_search_candidates
when 'csv'
ajax_csv_candidates
else
new
end
end

def edit
@enrollment = Enrollment.find params[:id]
@course = Course.find_enabled_by @enrollment.course_id
@user = @enrollment.user
render 'layouts/renders/main_pane', locals: { resource: 'enrollments/edit' }
end

def update
@enrollment = Enrollment.find params[:id]
@course = Course.find_enabled_by @enrollment.course_id
@user = @enrollment.user
from_role = @enrollment.role
to_role = params[:enrollment][:role]
begin
raise unless @enrollment.updatable? session[:id]
raise t('controllers.enrollments.evaluator_must_be_manager') if (to_role != 'manager') && (@course.evaluator? @user.id)
Enrollment.transaction do
@enrollment.update_attributes!(enrollment_params)
if (SYSTEM_ROSTER_SYNC == :on) && (to_role != from_role)
payload = {enrollment: @enrollment.to_roster_hash}
response = request_roster_api("/enrollments/#{@enrollment.sourced_id}", :put, payload)
end
end
render_index
rescue => error
notify_error error, t('controllers.enrollments.update_failed')
render 'layouts/renders/main_pane', locals: { resource: 'enrollments/edit' }
end
end

def destroy
@enrollment = Enrollment.find params[:id]
@course = Course.find_enabled_by @enrollment.course_id
@user = @enrollment.user
begin
raise t('controllers.enrollments.evaluator_must_be_manager') if (@course.evaluator? @user.id)
raise unless @enrollment.destroyable? session[:id]
Enrollment.transaction do
@enrollment.destroy!
response = request_roster_api("/enrollments/#{@enrollment.sourced_id}", :delete) if (SYSTEM_ROSTER_SYNC == :on)
end
render_index
rescue => error
notify_error error, t('controllers.enrollments.delete_failed')
render 'layouts/renders/main_pane', locals: { resource: 'enrollments/edit' }
end
end

def ajax_index
set_nav_session params[:nav_section], 'enrollments', params[:nav_id].to_i
get_resources
Expand All @@ -28,13 +113,6 @@ def ajax_show_with_transition
ajax_show true
end

def ajax_edit
get_resources
@form_category = ''
@member_role = 'learner'
render 'layouts/renders/main_pane', locals: { resource: 'enrollments/edit' }
end

def ajax_edit_group
get_resources
render 'layouts/renders/main_pane', locals: { resource: 'enrollments/edit_group' }
Expand All @@ -49,7 +127,7 @@ def ajax_csv_candidates
get_resources
manageable = @course.manager_changeable? session[:id]
@candidates = csv_to_member_candidates @candidates_csv, manageable, 'course', @course.id
render 'layouts/renders/resource', locals: { resource: 'enrollments/edit' }
render 'layouts/renders/resource', locals: { resource: 'enrollments/new' }
end

def ajax_search_candidates
Expand Down Expand Up @@ -80,35 +158,7 @@ def ajax_search_candidates
end
@candidates = candidates.zip current_categories, Array.new(candidates.size, @member_role)
end
render 'layouts/renders/resource', locals: { resource: 'enrollments/edit' }
end

def ajax_update_role
if params[:update_to] == 'none'
enrollment = Enrollment.find_by(user_id: params[:user_id], course_id: params[:course_id])
if enrollment && enrollment.deletable?
enrollment.destroy
else
if enrollment.role == 'manager'
flash.now[:message] = 'レッスンの評価担当者、またはコース内でふせんを記載している場合は削除できません。'
else
flash.now[:message] = 'コース内でふせんを記載しているユーザ、または課題を提出済みの学生ユーザは削除できません'
end
flash[:message_category] = 'error'
end
else
update_role params[:user_id], params[:course_id], params[:update_to]
end

# replace page process
case params[:form_category]
when 'search'
ajax_search_candidates
when 'csv'
ajax_csv_candidates
else
ajax_edit
end
render 'layouts/renders/resource', locals: { resource: 'enrollments/new' }
end

def ajax_update_group
Expand Down Expand Up @@ -168,33 +218,22 @@ def autocomplete_manager

private

def enrollment_params
params.require(:enrollment).permit(:role)
end

def get_resources
@course = Course.find_enabled_by session[:nav_id]
@managers = User.sort_by_signin_name @course.managers
@assistants = User.sort_by_signin_name @course.assistants
@learners = User.sort_by_signin_name @course.learners
end

def update_role(user_id, course_id, role)
enrollment = Enrollment.find_by(user_id: user_id, course_id: course_id)
course = Course.find_enabled_by course_id
if enrollment
if (enrollment.role == 'manager') && (course.evaluator? user_id)
flash.now[:message] = 'レッスンの評価担当者は、教師である必要があります'
flash[:message_category] = 'error'
else
unless enrollment.update_attributes(role: role)
flash.now[:message] = 'コース管理者は、コース管理権限のあるユーザのみ登録できます'
flash[:message_category] = 'error'
end
end
else
new_coourse_user = Enrollment.new(user_id: user_id, course_id: course_id, role: role)
unless new_coourse_user.save
flash.now[:message] = 'コース管理者は、コース管理権限のあるユーザのみ登録できます'
flash[:message_category] = 'error'
end
end
def render_index
@managers = User.sort_by_signin_name @course.managers
@assistants = User.sort_by_signin_name @course.assistants
@learners = User.sort_by_signin_name @course.learners
render 'layouts/renders/main_pane', locals: { resource: 'index' }
end

def invalid_autocomplete_request?(param)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/terms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TermsController < ApplicationController
def create
@term = Term.new(term_params)
begin
raise unless @term.creatable? session[:id]
raise unless Term.creatable? session[:id]
Term.transaction do
@term.save!
if SYSTEM_ROSTER_SYNC == :on
Expand Down
14 changes: 7 additions & 7 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def member_candidate_url(category, form_category, user, resource_id, update_to,
when 'content'
{ action: 'ajax_update_role', user_id: user.id, content_id: resource_id, update_to: update_to, form_category: form_category, search_word: search_word, member_role: member_role, candidates_csv: candidates_csv }
when 'course'
{ action: 'ajax_update_role', user_id: user.id, course_id: resource_id, update_to: update_to, form_category: form_category, search_word: search_word, member_role: member_role, candidates_csv: candidates_csv }
{ action: 'create', form_category: form_category, search_word: search_word, member_role: member_role, candidates_csv: candidates_csv }
when 'system'
if update_to == 'suspended'
{ action: 'ajax_update_role', user_id: user.id, update_to: 'suspended', form_category: form_category, search_word: search_word, member_role: member_role, candidates_csv: candidates_csv }
Expand Down Expand Up @@ -524,13 +524,13 @@ def link_to_resource(body, resource_id, html_options, action = 'ajax_show')
def member_role_options(update_model)
case update_model
when 'content_member'
options = [[t('activerecord.others.content_member.role.manager') + t('helpers.candidate'), 'manager']]
options.push [t('activerecord.others.content_member.role.assistant') + t('helpers.candidate'), 'assistant']
options.push [t('activerecord.others.content_member.role.user') + t('helpers.candidate'), 'user']
options = [[t('activerecord.others.content_member.role.manager'), 'manager']]
options.push [t('activerecord.others.content_member.role.assistant'), 'assistant']
options.push [t('activerecord.others.content_member.role.user'), 'user']
when 'enrollment'
options = [[t('activerecord.attributes.course.managers') + t('helpers.candidate'), 'manager']]
options.push [t('activerecord.attributes.course.assistants') + t('helpers.candidate'), 'assistant']
options.push [t('activerecord.attributes.course.learners') + t('helpers.candidate'), 'learner']
options = [[t('activerecord.attributes.course.managers'), 'manager']]
options.push [t('activerecord.attributes.course.assistants'), 'assistant']
options.push [t('activerecord.attributes.course.learners'), 'learner']
when 'system'
options = [[t('activerecord.others.user.role.admin'), 'admin']]
options.push [t('activerecord.others.user.role.manager'), 'manager']
Expand Down
9 changes: 6 additions & 3 deletions app/jobs/roster_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ def get_roster(endpoint)
def sync_with_course cid
rmanagers = get_roster "/classes/#{cid[:sourced_id]}/teachers"
manager_ids = User.sync_roster rmanagers['users']
Enrollment.sync_roster cid[:id], manager_ids, 'manager'
rlearners = get_roster "/classes/#{cid[:sourced_id]}/students"
learner_ids = User.sync_roster rlearners['users']
Enrollment.sync_roster cid[:id], learner_ids, 'learner'
destroyed_ids = Enrollment.destroy_unused cid[:id], manager_ids.concat(learner_ids)
# FIXME: get_roster for aide

school_id = Rails.application.secrets.roster_school_sourced_id
renrollments = get_roster "/schools/#{school_id}/classes/#{cid[:sourced_id]}/enrollments"
enrollment_ids = Enrollment.sync_roster cid[:id], manager_ids.concat(learner_ids), renrollments['enrollments']
destroyed_ids = Enrollment.destroy_unused cid[:id], enrollment_ids
@logger.info("Deleted from enrollments for course_id: #{cid[:id]} => user_id: #{destroyed_ids.join(', ')}") if destroyed_ids.present?
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def self.sync_roster(term_id, term_status, rcourses)

ids = []
rcourses.each do |rc|
next if (rc['schoolSourcedId'] != Rails.application.secrets.roster_school_sourced_id)
# REQUIREMENT: period vaule in OneRoster is [weekday number]-[time period number] format
weekday = rc['periods'].split(',')[0].split('-')[0]
period = rc['periods'].split(',')[0].split('-')[1]
Expand Down Expand Up @@ -226,11 +227,11 @@ def update_lesson_notes
end
end

def creatable?(user_id)
def self.creatable?(user_id)
# Not permitted when SYSTEM_ROSTER_SYNC is :suspended
return false if %i[on off].exclude? SYSTEM_ROSTER_SYNC
user = User.find user_id
user && user.system_staff?
user.system_staff?
end

def destroyable?(user_id)
Expand All @@ -247,7 +248,7 @@ def updatable?(user_id)
return false if SYSTEM_ROSTER_SYNC == :off && sourced_id.present?
return true if staff? user_id
user = User.find user_id
user && user.system_staff?
user.system_staff?
end

def fill_goals
Expand Down
Loading

0 comments on commit 1f943f6

Please sign in to comment.