class Admin::SelectedCoursesController < OrbitMemberController include Admin::SelectedCoursesHelper layout "member_plugin" before_action :set_course, only: [:show, :edit , :update, :destroy] before_action :set_course_assignment, only: [:new_assignment, :edit_assignment , :update_assignment] 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 show_assignments @course_assignments = CourseAssignment.where(:course_id=>params[:id]).enabled_for_student.page(params[:page]).per(10) @course = Course.find(params[:id]) rescue nil @member_profile = MemberProfile.where(:uid=>params[:member_profile_uid]).first end def edit_assignment @student_assignment = StudentAssignment.find(params[:id]) end def update_assignment @student_assignment = StudentAssignment.find(params[:id]) @student_assignment.update_attributes(student_assignment_params) @student_assignment.save redirect_to params[:referer_url] end def new_assignment @student_assignment = StudentAssignment.new end def create_assignment #render :html => params and return student_assignment = StudentAssignment.create(student_assignment_params) redirect_to params[:referer_url] end def destroy_assignment @student_assignment.destroy redirect_to course_assignments_admin_courses_path(:page => params[:page]) 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 student_assignment_params student_assignment_params = params.require(:student_assignment).permit! return student_assignment_params end def selected_course_params params.require(:selected_course).permit! 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 params[:uid].nil? if path.last.include? '-' uid = path[-1].split("-").last uid = uid.split("?").first else uid = path[-2].split("-").last uid = uid.split("?").first end else uid = params[:uid] end @selected_course = SelectedCourse.find_by(:uid => uid) rescue SelectedCourse.find(params[:id]) end def set_course_assignment path = request.path.split('/') if params[:uid].nil? if path.last.include? '-' uid = path[-1].split("-").last uid = uid.split("?").first else uid = path[-2].split("-").last uid = uid.split("?").first end else uid = params[:uid] end @course_assignment = CourseAssignment.find_by(:uid => uid) rescue CourseAssignment.find(params[:id]) @closed = (@course_assignment.deadline < DateTime.now) rescue false @member_profile = MemberProfile.where(:uid=>params[:member_profile_uid]).first 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) || (course.student_ids.include?(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 elsif( StudentAssignment.find(params[:id]).member_profile_id.to_s == current_user.member_profile_id.to_s rescue false) || (StudentAssignment.find(params[:id]).course_assignment.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) || (course_assignment.course.student_ids.include?(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 end