class Admin::SelectedCoursesController < OrbitMemberController include Admin::SelectedCoursesHelper layout "member_plugin" before_action :set_course, only: [:show, :edit , :update, :destroy] before_action :set_plugin before_action :get_settings,:only => [:new, :edit, :setting] before_action :need_access_right before_action :allow_admin_only, :only => [:index, :setting] def index @selected_courses = SelectedCourse.order_by(:created_at=>'desc').page(params[:page]).per(10) end def new @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil @selected_course = SelectedCourse.new end def create selected_course = SelectedCourse.create(selected_course_params) redirect_to params[:referer_url] end def show end 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="selected_courses.xlsx"' } end end def edit end def destroy @selected_course.destroy redirect_to admin_selected_courses_path(:page => params[:page]) end def update @selected_course.update_attributes(selected_course_params) @selected_course.save redirect_to params[:referer_url] end def setting end def frontend_setting @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil @intro = SelectedCourseIntro.find_by(:member_profile_id=>@member.id) rescue nil @intro = @intro.nil? ? SelectedCourseIntro.new({:member_profile_id=>@member.id}) : @intro end def update_frontend_setting @member = MemberProfile.find(intro_params['member_profile_id']) rescue nil @intro = SelectedCourseIntro.find_by(:member_profile_id=>@member.id) rescue nil @intro = @intro.nil? ? SelectedCourseIntro.new({:member_profile_id=>@member.id}) : @intro @intro.update_attributes(intro_params) @intro.save redirect_to URI.encode('/admin/members/'+@member.to_param+'/SelectedCourse') end def toggle_hide if params[:ids] @projects = SelectedCourse.any_in(_id: params[:ids]) @projects.each do |project| project.is_hidden = params[:disable] project.save end end render json: {"success"=>true} end private def selected_course_params selected_course_params = params.require(:selected_course).permit! if selected_course_params["member_profile_id"].nil? selected_course_params["member_profile_id"] = nil end return selected_course_params end def intro_params params.require(:selected_course_intro).permit! rescue nil end def get_settings @selected_course_semesters = SelectedCourseSemester.all @selected_course_categories = SelectedCourseCategory.all.order_by(:sort_position => "asc") end def set_plugin @plugin = OrbitApp::Plugin::Registration.all.select{|plugin| plugin.app_name.eql? 'SelectedCourse'}.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 @selected_course = SelectedCourse.find_by(:uid => uid) rescue SelectedCourse.find(params[:id]) end end