personal-course/app/controllers/admin/courses_controller.rb

215 lines
6.3 KiB
Ruby
Raw Normal View History

2015-12-29 11:35:44 +00:00
class Admin::CoursesController < OrbitMemberController
2022-07-26 11:11:11 +00:00
include Admin::CoursesHelper
2015-12-29 11:35:44 +00:00
layout "member_plugin"
2022-07-26 11:11:11 +00:00
before_action :set_course, only: [:show, :edit , :update, :destroy]
before_action :set_course_assignment, only: [ :edit_assignment , :update_assignment, :destroy_assignment]
before_action :set_plugin
before_action :get_settings,:only => [:new, :edit, :setting]
2015-12-29 11:35:44 +00:00
2022-07-26 11:11:11 +00:00
before_action :need_access_right
before_action :allow_admin_only, :only => [:index, :setting]
2015-12-29 11:35:44 +00:00
def index
2022-07-26 11:11:11 +00:00
@courses = Course.sort_year.page(params[:page]).per(10)
2015-12-29 11:35:44 +00:00
end
def new
@member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
2022-07-26 11:11:11 +00:00
@course = Course.new
2015-12-29 11:35:44 +00:00
end
def create
course = Course.create(course_params)
2016-01-12 10:17:58 +00:00
redirect_to params[:referer_url]
2015-12-29 11:35:44 +00:00
end
def show
end
2020-09-20 09:20:43 +00:00
def show_assignments
@course_assignment = CourseAssignment.where(:uid=>params[:uid]).first
@course = @course_assignment.course
@student_ids = @course.student_ids
@student_assignments = StudentAssignment.where(:course_assignment=>@course_assignment,:member_profile_id.in=>@student_ids).page(params[:page]).per(10) rescue Kaminari.paginate_array([])
@not_yet_deliver_student_ids = []
if @student_assignments.to_a.count < 10
@deliver_student_ids = StudentAssignment.where(:course_assignment=>@course_assignment,:member_profile_id.in=>@student_ids).pluck(:member_profile_id)
@not_yet_deliver_student_ids = @student_ids - @deliver_student_ids
end
end
2016-06-07 07:12:13 +00:00
def analysis_report
role = params[:role_id]
year_start = params[:year_start].to_i
year_end = params[:year_end].to_i
graph_by = params[:graph_by]
@data = get_chart_data(year_start,year_end,role,params[:graph_by])
render :layout => false
end
def download_excel
year_start = params[:year_start].to_i
year_end = params[:year_end].to_i
@data = get_data_for_excel(year_start,year_end)
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="courses.xlsx"'
}
end
end
2015-12-29 11:35:44 +00:00
def edit
end
def destroy
@course.destroy
redirect_to admin_courses_path(:page => params[:page])
end
def update
@course.update_attributes(course_params)
@course.save
redirect_to params[:referer_url]
end
2020-09-20 09:20:43 +00:00
def destroy_assignment
@course_assignment.destroy
if( current_user.is_admin? rescue false)
redirect_to course_assignments_admin_courses_path(:page => params[:page])
else
redirect_to :back
end
2020-09-20 09:20:43 +00:00
end
def update_assignment
@course_assignment.update_attributes(course_assignment_params)
@course_assignment.save
redirect_to params[:referer_url]
end
2015-12-29 11:35:44 +00:00
def setting
end
def frontend_setting
@member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
@intro = CourseIntro.find_by(:member_profile_id=>@member.id) rescue nil
@intro = @intro.nil? ? CourseIntro.new({:member_profile_id=>@member.id}) : @intro
end
def update_frontend_setting
@member = MemberProfile.find(intro_params['member_profile_id']) rescue nil
@intro = CourseIntro.find_by(:member_profile_id=>@member.id) rescue nil
@intro = @intro.nil? ? CourseIntro.new({:member_profile_id=>@member.id}) : @intro
@intro.update_attributes(intro_params)
@intro.save
redirect_to URI.encode('/admin/members/'+@member.to_param+'/Course')
end
def toggle_hide
if params[:ids]
@projects = Course.any_in(_id: params[:ids])
@projects.each do |project|
project.is_hidden = params[:disable]
project.save
end
end
render json: {"success"=>true}
end
2020-09-20 09:20:43 +00:00
def course_assignments
@course = Course.find(params[:id]) rescue nil
@course_assignments = CourseAssignment.where(:course_id=>params[:id]).page(params[:page]).per(10)
end
def new_assignment
@course_assignment = CourseAssignment.new
end
def edit_assignment
end
2015-12-29 11:35:44 +00:00
2020-09-20 09:20:43 +00:00
def create_assignment
#render :html => params and return
course_assignment = CourseAssignment.create(course_assignment_params)
redirect_to params[:referer_url]
end
2015-12-29 11:35:44 +00:00
private
def course_params
2020-09-20 09:20:43 +00:00
course_params = params.require(:course).permit!
if course_params['student_ids'].nil?
course_params['student_ids'] = []
end
return course_params
end
def course_assignment_params
course_assignment_params = params.require(:course_assignment).permit!
return course_assignment_params
2015-12-29 11:35:44 +00:00
end
def intro_params
params.require(:course_intro).permit! rescue nil
end
def get_settings
@course_semesters = CourseSemester.all
@course_categories = CourseCategory.all.order_by(:sort_position => "asc")
end
def set_plugin
@plugin = OrbitApp::Plugin::Registration.all.select{|plugin| plugin.app_name.eql? 'Course'}.first
end
def set_course
path = request.path.split('/')
if path.last.include? '-'
uid = path[-1].split("-").last
uid = uid.split("?").first
else
uid = path[-2].split("-").last
uid = uid.split("?").first
end
@course = Course.find_by(:uid => uid) rescue Course.find(params[:id])
end
2020-09-20 09:20:43 +00:00
def set_course_assignment
path = request.path.split('/')
if path.last.include? '-'
uid = path[-1].split("-").last
uid = uid.split("?").first
else
uid = path[-2].split("-").last
uid = uid.split("?").first
end
@course_assignment = CourseAssignment.find_by(:uid => uid) rescue CourseAssignment.find(params[:id])
end
def has_access?
if @user_has_privileges
return true
else
if !params[:id].nil?
course = Course.find(params[:id]) rescue nil
if course.present? && ( course.member_profile_id.to_s == current_user.member_profile_id.to_s rescue false)
return true
elsif( CourseAssignment.find(params[:id]).course.member_profile_id.to_s == current_user.member_profile_id.to_s rescue false)
return true
else
return false
end
elsif !params[:uid].nil?
course_assignment = CourseAssignment.where(:uid=>params[:uid]).first
if course_assignment.nil?
return false
else
if( course_assignment.course.member_profile_id.to_s == current_user.member_profile_id.to_s rescue false)
return true
else
return false
end
end
elsif( Course.find(course_assignment_params[:course_id]).member_profile_id.to_s == current_user.member_profile_id.to_s rescue false)
return true
else
return false
end
end
end
2015-12-29 11:35:44 +00:00
end