From 11876a777c42ff43a18e856c0b34962a9d090fdb Mon Sep 17 00:00:00 2001 From: bohung Date: Sun, 20 Sep 2020 17:20:43 +0800 Subject: [PATCH] Add course assignment. --- app/controllers/admin/courses_controller.rb | 60 +++++- app/models/course.rb | 29 ++- app/models/course_assignment.rb | 29 +++ app/models/course_attachment.rb | 12 ++ app/views/admin/courses/_courses.html.erb | 1 + .../admin/courses/_form_assignment.html.erb | 203 ++++++++++++++++++ .../admin/courses/course_assignments.html.erb | 82 +++++++ .../admin/courses/edit_assignment.html.erb | 5 + app/views/admin/courses/index.html.erb | 3 +- .../admin/courses/new_assignment.html.erb | 5 + .../admin/courses/show_assignments.html.erb | 52 +++++ .../plugin/personal_course/_profile.html.erb | 2 + config/locales/en.yml | 11 + config/locales/zh_tw.yml | 11 + config/routes.rb | 10 + 15 files changed, 501 insertions(+), 14 deletions(-) create mode 100644 app/models/course_assignment.rb create mode 100644 app/models/course_attachment.rb create mode 100644 app/views/admin/courses/_form_assignment.html.erb create mode 100644 app/views/admin/courses/course_assignments.html.erb create mode 100644 app/views/admin/courses/edit_assignment.html.erb create mode 100644 app/views/admin/courses/new_assignment.html.erb create mode 100644 app/views/admin/courses/show_assignments.html.erb 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 @@ + <%= link_to course.course_assignments.count,course_assignments_admin_courses_path(:id=> course.id) %> <%= course.member_profile.name rescue "" %> <%= course.display_students rescue "" %> diff --git a/app/views/admin/courses/_form_assignment.html.erb b/app/views/admin/courses/_form_assignment.html.erb new file mode 100644 index 0000000..7d997f9 --- /dev/null +++ b/app/views/admin/courses/_form_assignment.html.erb @@ -0,0 +1,203 @@ +<% # encoding: utf-8 %> +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> + <%= stylesheet_link_tag "lib/fileupload" %> + <%= stylesheet_link_tag "lib/main-list" %> + <%= stylesheet_link_tag "lib/main-form-col2" %> + +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> + <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %> + <%= javascript_include_tag "lib/bootstrap-fileupload" %> + <%= javascript_include_tag "lib/file-type" %> + <%= javascript_include_tag "lib/module-area" %> +<% end %> + + +
+ + + + + + +
+ + <% @site_in_use_locales.each_with_index do |locale, i| %> + +
"> + + +
+ +
+ <%= f.fields_for :name_translations do |f| %> + <%= f.text_field locale, class: "input-block-level", placeholder: t("personal_course.name"), value: (@course_assignment.name_translations[locale] rescue nil) %> + <% end %> +
+
+ +
+ +
+ <%= f.fields_for :detail_translations do |f| %> + <%= f.text_field locale, class: "input-block-level", placeholder: t("personal_course.detail"), value: (@course_assignment.detail_translations[locale] rescue nil) %> + <% end %> +
+
+
+ <% end %> +
+ + + + +
+ + +
+ <% if params[:id].present? %> + <%= f.hidden_field :course_id, :value => params[:id] %> + <% end %> + +
+ +
+ <%= f.datetime_picker :assign_date, :no_label => true, :new_record => @course_assignment.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %> +
+
+ +
+ +
+ <%= f.datetime_picker :deadline, :no_label => true, :new_record => @course_assignment.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %> +
+
+ + <% + files_hash = {} + ["course_attachment"].each do |file| + hash = {} + hash["html"] = add_attribute("form_file", f, file.pluralize.to_sym) + hash["count"] = @course_assignment.send(file.pluralize).count rescue 0 + files_hash[file] = hash + %> +
+ +
+ + + <% if !@course_assignment.new_record? && hash["count"] > 0 %> +
+ <% @course_assignment.send(file.pluralize).each_with_index do |obj, i| %> + <% if !obj.new_record? %> + <%= f.fields_for file.pluralize.to_sym, obj do |f| %> + <%= render :partial => "form_file", :object => obj, :locals => {:f => f, :i => i} %> + <% end %> + <% end %> + <% end %> +
+
+ <% end %> + + +
+
+

+ <%= t(:add) %> +

+ +
+
+ <% end %> +
+ + + +
+
+ +
+
+ +
+ <%= f.hidden_field :user_id, :value => params[:user_id] if !params[:user_id].blank? %> + + <%= f.submit t('submit'), class: 'btn btn-primary' %> + <%= link_to t('cancel'), request.referer, :class=>"btn" %> +
+ \ No newline at end of file diff --git a/app/views/admin/courses/course_assignments.html.erb b/app/views/admin/courses/course_assignments.html.erb new file mode 100644 index 0000000..78063c6 --- /dev/null +++ b/app/views/admin/courses/course_assignments.html.erb @@ -0,0 +1,82 @@ +

<%=t("personal_course.assignment_management")%>-<%=@course.title rescue "" %>

+<% student_assignment = (StudentAssignment rescue nil) %> + + + + + + + + + <% if !student_assignment.nil? %> + + <% end %> + + + + <% @course_assignments.each do |course_assignment| %> + + + + + + + <% if !student_assignment.nil? %> + + <% end %> + + <% end %> + +
<%= t("personal_course.name") %><%= t("personal_course.detail") %><%= t("personal_course.course_attachment") %> + <%= t("personal_course.assign_date") %> + + + + <%= t("personal_course.deadline") %> + + + <%=t("personal_course.already_deliver")%>
+ <%= course_assignment.name %> +
+ +
+
<%= course_assignment.detail %><%= course_assignment.display_attachments %><%= course_assignment.display_assign_date %><%= course_assignment.display_deadline %><%= link_to course_assignment.deliver_count, show_assignments_admin_courses_path(:name=>course_assignment.name, :uid => course_assignment.uid ) %>
+ +
+
+ <%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_assignment_admin_courses_path(:id=>params[:id]), :class => 'btn btn-primary' %> +
+ +
+ \ No newline at end of file diff --git a/app/views/admin/courses/edit_assignment.html.erb b/app/views/admin/courses/edit_assignment.html.erb new file mode 100644 index 0000000..a0f8254 --- /dev/null +++ b/app/views/admin/courses/edit_assignment.html.erb @@ -0,0 +1,5 @@ +<%= form_for @course_assignment, url: update_assignment_admin_courses_path(:name=>@course_assignment.name,:uid=>@course_assignment.uid), html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render partial: 'form_assignment', locals: {f: f} %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/courses/index.html.erb b/app/views/admin/courses/index.html.erb index 97c35d9..e5a265c 100644 --- a/app/views/admin/courses/index.html.erb +++ b/app/views/admin/courses/index.html.erb @@ -2,7 +2,8 @@ <%= t('personal_course.year') %> - <%= t('module_name.courses') %> + <%= t('personal_course.title') %> + <%= t('personal_course.course_assignment') %> <%= t("personal_plugins.author") %> <%= t("personal_course.students") %> diff --git a/app/views/admin/courses/new_assignment.html.erb b/app/views/admin/courses/new_assignment.html.erb new file mode 100644 index 0000000..ade35ba --- /dev/null +++ b/app/views/admin/courses/new_assignment.html.erb @@ -0,0 +1,5 @@ +<%= form_for @course_assignment, url: create_assignment_admin_courses_path, html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render partial: 'form_assignment', locals: {f: f} %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/courses/show_assignments.html.erb b/app/views/admin/courses/show_assignments.html.erb new file mode 100644 index 0000000..645dfa6 --- /dev/null +++ b/app/views/admin/courses/show_assignments.html.erb @@ -0,0 +1,52 @@ +

<%=t("personal_selected_course.assignment_delivery_area")%>-<%=@course.title rescue "" %>-<%=@course_assignment.name rescue "" %>

+ +
+ <%= t("personal_selected_course.already_deliver") %> + + + + + + + + + + <% @student_assignments.each do |student_assignment| %> + <% member_profile = student_assignment.member_profile %> + + + + + + <% end %> + +
<%= t("personal_course.students") %><%= t("personal_selected_course.assignment_content") %><%= t("personal_selected_course.student_assignment_file") %>
+ <%= member_profile.name %> +
+ +
+
<%= student_assignment.assignment_content %><%= student_assignment.display_student_assignment_files %>
+
+<% if @student_assignments.to_a.count < 10 %> +
+ <%= t("personal_selected_course.not_yet_deliver") %> +
+ <%= MemberProfile.where(:id.in=>@not_yet_deliver_student_ids).map{|m| link_to( m.name,admin_member_path(m))}.join(",").html_safe %> +
+
+<% end %> +
+
+ <%#= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_assignment_admin_courses_path(:id=>params[:id]), :class => 'btn btn-primary' %> +
+ +
+ \ No newline at end of file diff --git a/app/views/plugin/personal_course/_profile.html.erb b/app/views/plugin/personal_course/_profile.html.erb index 6bfe6f7..44b6645 100644 --- a/app/views/plugin/personal_course/_profile.html.erb +++ b/app/views/plugin/personal_course/_profile.html.erb @@ -30,6 +30,7 @@ <% end -%> <%= t('personal_course.year') %> <%= t('personal_course.title') %> + <%= t('personal_course.course_assignment') %> <%= t('personal_course.course_category') %> @@ -53,6 +54,7 @@ + <%= link_to course.course_assignments.count,course_assignments_admin_courses_path(:id=> course.id) %> <%= course.course_category.title rescue "" %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index fe337a7..ec75212 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,10 +4,21 @@ en: courses: Courses course: Course personal_course: + already_deliver: Already deliver + assignment_management: Assignments management + assign_date_hint: Student can only see this assignment after assign date. + deadline_hint: Student can not modify the his assignment content after deadline. + assign_date: Assgin date + deadline: Deadline + name: Assignment title + detail: Detail + course_attachment: Attachment + delete_assignment_message: Are you sure to delte this assignment? students: Students year: Year title: Course Title objective: Objective + course_assignment: Assignments course_semester: Semester course_category: Category course_syllabus_file: Syllabus Files diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 6752920..781abee 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -4,10 +4,21 @@ zh_tw: courses: 教學資料 course: 教學資料 personal_course: + already_deliver: 已繳交 + assignment_management: 作業管理 + assign_date_hint: 學生使用介面將在指派日期才會出現此筆作業。 + deadline_hint: 截止日期後學生將不能再針對此作業任何變動。 + assign_date: 指派日期 + deadline: 截止日期 + name: 作業名稱 + detail: 說明 + course_attachment: 附件 + delete_assignment_message: 你確定要刪除此則作業? students: 學生 year: 年度 title: 課程名稱 objective: 課程目標 + course_assignment: 作業 course_category: 課程類別 course_semester: 學期 course_syllabus_file: 教學大綱 diff --git a/config/routes.rb b/config/routes.rb index f6f06c9..eaea862 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,16 @@ Rails.application.routes.draw do get 'analysis' get 'analysis_report' get "download_excel" + get 'course_assignments' + post 'create_assignment' + get 'new_assignment' + delete 'destroy_assignment' + scope '(:name-:uid)' do + get 'edit_assignment' + patch 'update_assignment' + put 'update_assignment' + get 'show_assignments' + end end end