diff --git a/app/controllers/admin/courses_controller.rb b/app/controllers/admin/courses_controller.rb
index adf255c..741e4f4 100644
--- a/app/controllers/admin/courses_controller.rb
+++ b/app/controllers/admin/courses_controller.rb
@@ -2,6 +2,7 @@ class Admin::CoursesController < OrbitMemberController
include Admin::CoursesHelper
layout "member_plugin"
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]
@@ -24,7 +25,17 @@ class Admin::CoursesController < OrbitMemberController
def show
end
-
+ 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
def analysis_report
role = params[:role_id]
year_start = params[:year_start].to_i
@@ -61,6 +72,17 @@ class Admin::CoursesController < OrbitMemberController
redirect_to params[:referer_url]
end
+ def destroy_assignment
+ @course_assignment.destroy
+ redirect_to course_assignments_admin_courses_path(:page => params[:page])
+ end
+
+ def update_assignment
+ @course_assignment.update_attributes(course_assignment_params)
+ @course_assignment.save
+ redirect_to params[:referer_url]
+ end
+
def setting
end
@@ -91,13 +113,34 @@ class Admin::CoursesController < OrbitMemberController
render json: {"success"=>true}
end
+ 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
+ def create_assignment
+ #render :html => params and return
+ course_assignment = CourseAssignment.create(course_assignment_params)
+ redirect_to params[:referer_url]
+ end
private
def course_params
- params.require(:course).permit!
+ 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
end
-
def intro_params
params.require(:course_intro).permit! rescue nil
end
@@ -122,4 +165,15 @@ class Admin::CoursesController < OrbitMemberController
end
@course = Course.find_by(:uid => uid) rescue Course.find(params[:id])
end
+ 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
end
\ No newline at end of file
diff --git a/app/models/course.rb b/app/models/course.rb
index 01e6b04..50c9bd9 100644
--- a/app/models/course.rb
+++ b/app/models/course.rb
@@ -16,6 +16,7 @@ class Course
has_many :course_material_files, :dependent => :destroy, :autosave => true
has_many :course_supplement_files, :dependent => :destroy, :autosave => true
has_many :course_evaluation_files, :dependent => :destroy, :autosave => true
+ has_many :course_assignments, :dependent => :destroy, :autosave => true
belongs_to :course_semester
belongs_to :course_category
@@ -32,18 +33,26 @@ class Course
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year=>'desc') }
before_save do |record|
- if !record.student_ids.empty?
- selected_course = SelectedCourse rescue nil
- if selected_course
- record.student_ids.each do |student_id|
- selected_course = SelectedCourse.where(:member_profile_id => student_id,:course_id=>self.id).first
- if selected_course.nil?
- SelectedCourse.create(:member_profile => MemberProfile.find(student_id) , :course_id=>self.id , :year=>self.year,:course_title_translations=>self.title_translations,:course_objective_translations=>self.objective_translations) rescue nil
- else
- selected_course.update(:year=>self.year,:course_title_translations=>self.title_translations,:course_objective_translations=>self.objective_translations)
- end
+ selected_course = SelectedCourse rescue nil
+ if selected_course
+ student_ids_remove = record.student_ids_was.to_a - record.student_ids
+ record.student_ids.each do |student_id|
+ selected_course = SelectedCourse.where(:member_profile_id => student_id,:course_id=>record.id).first
+ if selected_course.nil?
+ SelectedCourse.create(:member_profile => MemberProfile.find(student_id) , :course_id=>record.id , :year=>record.year,:course_title_translations=>record.title_translations,:course_objective_translations=>record.objective_translations)
+ else
+ selected_course.update(:year=>record.year,:course_title_translations=>record.title_translations,:course_objective_translations=>record.objective_translations)
end
end
+ student_ids_remove.each do |student_id|
+ SelectedCourse.where(:member_profile_id => student_id,:course_id=>record.id).destroy
+ end
+ end
+ end
+ before_destroy do |record|
+ selected_course = SelectedCourse rescue nil
+ if selected_course
+ SelectedCourse.where(:course_id=>record.id).destroy
end
end
def students
diff --git a/app/models/course_assignment.rb b/app/models/course_assignment.rb
new file mode 100644
index 0000000..47ee5e3
--- /dev/null
+++ b/app/models/course_assignment.rb
@@ -0,0 +1,29 @@
+class CourseAssignment
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include Slug
+ field :name, as: :slug_title, type: String, localize: true, default: ""
+ field :deadline, type: DateTime, default: DateTime.now
+ field :assign_date, type: DateTime, default: DateTime.now
+ field :detail, type: String, localize: true, default: ""
+ has_many :course_attachments, :dependent => :destroy, :autosave => true
+ accepts_nested_attributes_for :course_attachments, :allow_destroy => true
+ belongs_to :course
+ scope :enabled_for_student, ->{where(:assign_date.lte=>DateTime.now)}
+ def display_attachments
+ self.course_attachments.map{|f|
+ next if f.file.file.nil?
+ title = (f.title.blank? ? f.file.file.original_filename : f.title)
+ "#{title}"
+ }.join("
").html_safe
+ end
+ def display_deadline
+ self.deadline.strftime("%Y-%m-%d %H:%M")
+ end
+ def display_assign_date
+ self.assign_date.strftime("%Y-%m-%d %H:%M")
+ end
+ def deliver_count
+ StudentAssignment.where(:course_assignment_id => self.id,:member_profile_id.ne=>nil).count rescue 0
+ end
+end
\ No newline at end of file
diff --git a/app/models/course_attachment.rb b/app/models/course_attachment.rb
new file mode 100644
index 0000000..886e824
--- /dev/null
+++ b/app/models/course_attachment.rb
@@ -0,0 +1,12 @@
+class CourseAttachment
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :title, localize: true
+ field :description, localize: true
+ field :should_destroy, :type => Boolean
+
+ mount_uploader :file, AssetUploader
+
+ belongs_to :course_assignment
+end
\ No newline at end of file
diff --git a/app/views/admin/courses/_courses.html.erb b/app/views/admin/courses/_courses.html.erb
index f4ea125..772c7c4 100644
--- a/app/views/admin/courses/_courses.html.erb
+++ b/app/views/admin/courses/_courses.html.erb
@@ -10,6 +10,7 @@
+
+ <%= t(:add) %> +
+ +| <%= t("personal_course.name") %> | +<%= t("personal_course.detail") %> | +<%= t("personal_course.course_attachment") %> | +
+ <%= t("personal_course.assign_date") %>
+
+
+
+
+
+
+ |
+
+ <%= t("personal_course.deadline") %>
+
+
+
+
+
+
+ |
+ <% if !student_assignment.nil? %>
+ <%=t("personal_course.already_deliver")%> | + <% end %> +
|---|---|---|---|---|---|
|
+ <%= course_assignment.name %>
+
+
+
+ |
+ <%= course_assignment.detail %> | +<%= course_assignment.display_attachments %> | +<%= course_assignment.display_assign_date %> | +<%= course_assignment.display_deadline %> | + <% if !student_assignment.nil? %> +<%= link_to course_assignment.deliver_count, show_assignments_admin_courses_path(:name=>course_assignment.name, :uid => course_assignment.uid ) %> | + <% end %> +