diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index ddeb42538..9f823ab9d 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -5,10 +5,9 @@ class CoursesController < ApplicationController menu_item l(:label_sort_by_active), :only => :index menu_item l(:label_sort_by_influence), :only => :index - before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches] - before_filter :authorize, :only => [:show, :settings, :edit, :update, :modules, :close, :reopen,:view_homework_attaches,:course] - before_filter :authorize_global, :only => [:new, :create,:view_homework_attaches] - before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar] + before_filter :authorize, :only => [:show, :settings, :edit, :update, :modules, :close, :reopen, :view_homework_attaches, :course] + before_filter :authorize_global, :only => [:view_homework_attaches] #:new,:create] + before_filter :require_admin, :only => [:copy, :archive, :unarchive, :destroy, :calendar] before_filter :require_login, :only => [:join, :unjoin] before_filter :allow_join, :only => [:join] @@ -22,7 +21,7 @@ class CoursesController < ApplicationController members = [] members << Member.new(:role_ids => [10], :user_id => User.current.id) course.members << members - + StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id]) @state = 0 else @@ -34,13 +33,13 @@ class CoursesController < ApplicationController format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} } end end - + def unjoin if User.current.logged? - + @member = Member.where('project_id = ? and user_id = ?', params[:object_id], User.current.id) @member.first.destroy - + joined = StudentsForCourse.where('student_id = ? and course_id = ?', User.current.id, params[:object_id]) joined.each do |join| join.delete @@ -52,7 +51,7 @@ class CoursesController < ApplicationController end end - def handle_course courses,activities + def handle_course courses, activities course_activity_count_array=activities.values() course_array=[] @@ -62,30 +61,87 @@ class CoursesController < ApplicationController i=i+1 end - courses=desc_sort_course_by_avtivity(course_activity_count_array,course_array) + courses=desc_sort_course_by_avtivity(course_activity_count_array, course_array) return courses end - def new - @course_type = params[:course_type] ||= params[:course] - @issue_custom_fields = IssueCustomField.sorted.all - @trackers = Tracker.sorted.all + def create + if User.current.user_extensions.identity + @course = Course.new + @course.extra='course' + DateTime.parse(Time.now.to_s).strftime('%Y-%m-%d_%H-%M-%S').to_s + @course.safe_attributes = params[:course] + @course.tea_id = User.current.id + # added by bai + @course.term = params[:term] + @course.time = params[:time] + #@course.school_id = params[:occupation] + @course.school_id = User.current.user_extensions.school_id + @course.setup_time = params[:setup_time] + @course.endup_time = params[:endup_time] + @course.class_period = params[:class_period] + end - @course = Course.new - @course.safe_attributes = params[:course] + @issue_custom_fields = IssueCustomField.sorted.all + @trackers = Tracker.sorted.all + + if User.current.user_extensions.identity == 0 + if @course.save + unless User.current.admin? + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => User.current, :roles => [r]) + #course = ProjectInfo.new(:user_id => User.current.id, :course_id => @course.id) + user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) + if params[:course][:is_public] == '1' + course_status = CourseStatus.create(:course_id => @course.id, :watchers_count => 0, :changesets_count => 0, :grade => 0, :course_type => @course_tag) + end + @course.members << m + #@course.course_infos << course + end + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_create) + if params[:continue] + redirect_to new_course_path(attrs, :course => '0') + elsif params[:course_continue] + redirect_to new_course_path(:course => '1') + else + redirect_to settings_course_path(@course, :course_type => 1) + end + } + format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'courses', :action => 'show', :id => @course.id) } + end + else + @course.destroy + respond_to do |format| + format.html { render :action => 'new', :layout => 'base' } #Added by young + format.api { render_validation_errors(@course) } + end + end + end + + end + + + def new + @course_type = params[:course_type] ||= params[:course] + @issue_custom_fields = IssueCustomField.sorted.all + @trackers = Tracker.sorted.all + + @course = Course.new + @course.safe_attributes = params[:course] render :layout => 'base' end - def desc_sort_course_by_avtivity(activity_count,courses) + def desc_sort_course_by_avtivity(activity_count, courses) return courses if activity_count.size<2 (activity_count.size-2).downto(0) do |i| (0..i).each do |j| if activity_count[j]?",date_from).each do |attachment| + Attachment.where(container_id: @course_ids, container_type: Project).where("created_on>?", date_from).each do |attachment| activities[attachment.container_id]+=1 end #message_count Board.where(course_id: @course_ids).each do |board| # activities[board.course_id]+=1 - activities[board.course_id]+=board.messages.where("updated_on>?",date_from).count + activities[board.course_id]+=board.messages.where("updated_on>?", date_from).count end #feedbackc_count @@ -224,21 +280,19 @@ class CoursesController < ApplicationController def allow_join if course_endTime_timeout? Project.find(params[:object_id]) respond_to do |format| - format.js{ + format.js { @state = 2 - render :partial => 'set_join', - :locals => {:user => User.current, - :course => Project.find(params[:object_id]), - :object_id => params[:object_id] - } + render :partial => 'set_join', + :locals => {:user => User.current, + :course => Project.find(params[:object_id]), + :object_id => params[:object_id] + } } end end end - - def index1 per_page_option = 10 @@ -258,13 +312,13 @@ class CoursesController < ApplicationController @course_activity_count[course.id]=0 end - @course_activity_count=get_course_activity @courses_all,@course_activity_count + @course_activity_count=get_course_activity @courses_all, @course_activity_count #gcm end case params[:course_sort_type] when '0' - @courses = @courses_all.order("created_on desc") + @courses = @courses_all.order("created_on desc") @s_type = 0 when '1' @courses = @courses_all.order("grade desc") @@ -276,15 +330,15 @@ class CoursesController < ApplicationController #gcm when '3' #@courses=desc_sort_course_by_avtivity(@course_activity_count_array,@course_all_array) - @courses=handle_course @courses_all,@course_activity_count + @courses=handle_course @courses_all, @course_activity_count @s_type = 3 - @courses = @courses[@course_pages.offset, @course_pages.per_page] + @courses = @courses[@course_pages.offset, @course_pages.per_page] else @courses = @courses = @courses_all.order("grade desc") @s_type = 1 end - @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) + @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) respond_to do |format| format.html { @@ -294,7 +348,7 @@ class CoursesController < ApplicationController # scope = scope.active # end } - format.api { + format.api { # @offset, @limit = api_offset_and_limit # @course_count = Project.visible.count # @courses = Project.visible.offset(@offset).limit(@limit).order('lft').all diff --git a/app/models/course.rb b/app/models/course.rb index b611261cc..d0273fa71 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -23,6 +23,8 @@ class Course < ActiveRecord::Base 'term', 'password' + acts_as_customizable + scope :active, lambda { where(:status => STATUS_ACTIVE) } scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } scope :all_public, lambda { where(:is_public => true) } @@ -43,6 +45,11 @@ class Course < ActiveRecord::Base user.allowed_to?(:view_course, self) end + def extra_frozen? + errors[:extra].blank? && !(new_record? || extra.blank?) + end + + def self.visible_condition(user, options={}) allowed_to_condition(user, :view_course, options) end diff --git a/app/views/courses/_course_form.html.erb b/app/views/courses/_course_form.html.erb index 017c4e6ee..66054a93e 100644 --- a/app/views/courses/_course_form.html.erb +++ b/app/views/courses/_course_form.html.erb @@ -17,21 +17,21 @@ <% object = [] %> -<% object << 'project' %> +<% object << 'course' %> <% object << 'course' %> <%= error_messages_for object %> - -<% unless @project.new_record? %> -

<%= render :partial=>"avatar/avatar_form",:locals=> {source:@project} %>

+ +<% unless @course.new_record? %> +

<%= render :partial=>"avatar/avatar_form",:locals=> {source:@course} %>

<% end %> -

+

- @@ -294,46 +294,33 @@ -

+

<%= l(:text_command) %> <% end %> -

+

<%= f.check_box :is_public, :style => "margin-left:10px;" %><%= l(:label_public_info) %>

-

<%= f.text_field :project_type, :value => 1 %>

+

<%= f.text_field :course_type, :value => 1 %>

-<%= wikitoolbar_for 'project_description' %> +<%= wikitoolbar_for 'course_description' %> -<% @project.custom_field_values.each do |value| %> -

<%= custom_field_tag_with_label :project, value %>

+<% @course.custom_field_values.each do |value| %> +

<%= custom_field_tag_with_label :course, value %>

<% end %> -<%= call_hook(:view_projects_form, :project => @project, :form => f) %> +<%= call_hook(:view_courses_form, :course => @course, :form => f) %> - + -<% unless @project.identifier_frozen? %> +<% unless @course.extra_frozen? %> <% content_for :header_tags do %> - <%= javascript_include_tag 'project_identifier' %> + <%= javascript_include_tag 'course_identifier' %> <% end %> <% end %> -<% if !User.current.admin? && @project.inherit_members? && @project.parent && User.current.member_of?(@project.parent) %> - <%= javascript_tag do %> - $(document).ready(function() { - $("#project_inherit_members").change(function(){ - if (!$(this).is(':checked')) { - if (!confirm("<%= escape_javascript(l(:text_own_membership_delete_confirmation)) %>")) { - $("#project_inherit_members").attr("checked", true); - } - } - }); - }); - <% end %> -<% end %> diff --git a/app/views/users/_my_course.html.erb b/app/views/users/_my_course.html.erb index 26cfd48e0..85f455b19 100644 --- a/app/views/users/_my_course.html.erb +++ b/app/views/users/_my_course.html.erb @@ -7,7 +7,7 @@ <% else %>

- <%= l(:label_project_course_unadd) %><%= link_to "#{l(:label_course_new)}", {:controller => 'projects', :action => 'new', :course => 1, :project_type => 1}, :class => 'icon icon-add' %> + <%= l(:label_project_course_unadd) %><%= link_to "#{l(:label_course_new)}", {:controller => 'courses', :action => 'new'}, :class => 'icon icon-add' %>

<% end %> <% else %> diff --git a/db/migrate/20140604071623_add_is_public_to_courses.rb b/db/migrate/20140604071623_add_is_public_to_courses.rb new file mode 100644 index 000000000..48aa6b5df --- /dev/null +++ b/db/migrate/20140604071623_add_is_public_to_courses.rb @@ -0,0 +1,6 @@ +class AddIsPublicToCourses < ActiveRecord::Migration + def change + add_column :courses, :is_public, :tinyint,:default => 1 + add_column :courses, :inherit_members, :tinyint,:default => 1 + end +end diff --git a/db/schema.rb b/db/schema.rb index 721945ed4..4f3398eb7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140527060344) do +ActiveRecord::Schema.define(:version => 20140604071624) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -116,6 +116,7 @@ ActiveRecord::Schema.define(:version => 20140527060344) do t.integer "messages_count", :default => 0, :null => false t.integer "last_message_id" t.integer "parent_id" + t.integer "course_id" end add_index "boards", ["last_message_id"], :name => "index_boards_on_last_message_id" @@ -217,6 +218,16 @@ ActiveRecord::Schema.define(:version => 20140527060344) do t.datetime "updated_on", :null => false end + create_table "course_statuses", :force => true do |t| + t.integer "changesets_count" + t.integer "watchers_count" + t.integer "course_id" + t.float "grade", :default => 0.0 + t.integer "course_ac_para", :default => 0 + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "courses", :force => true do |t| t.integer "tea_id" t.string "name" @@ -224,8 +235,8 @@ ActiveRecord::Schema.define(:version => 20140527060344) do t.string "code" t.integer "time" t.string "extra" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "location" t.string "term" t.string "string" @@ -234,6 +245,13 @@ ActiveRecord::Schema.define(:version => 20140527060344) do t.string "endup_time" t.string "class_period" t.integer "school_id" + t.text "description" + t.integer "status", :default => 1 + t.integer "attachmenttype", :default => 2 + t.integer "lft" + t.integer "rgt" + t.boolean "is_public", :default => true + t.integer "inherit_members", :limit => 1, :default => 1 end create_table "custom_fields", :force => true do |t| @@ -296,6 +314,7 @@ ActiveRecord::Schema.define(:version => 20140527060344) do create_table "enabled_modules", :force => true do |t| t.integer "project_id" t.string "name", :null => false + t.integer "course_id" end add_index "enabled_modules", ["project_id"], :name => "enabled_modules_project_id" @@ -499,13 +518,14 @@ ActiveRecord::Schema.define(:version => 20140527060344) do create_table "members", :force => true do |t| t.integer "user_id", :default => 0, :null => false - t.integer "project_id", :default => 0, :null => false + t.integer "project_id", :default => 0 t.datetime "created_on" t.boolean "mail_notification", :default => false, :null => false + t.integer "course_id" end add_index "members", ["project_id"], :name => "index_members_on_project_id" - add_index "members", ["user_id", "project_id"], :name => "index_members_on_user_id_and_project_id", :unique => true + add_index "members", ["user_id", "project_id", "course_id"], :name => "index_members_on_user_id_and_project_id", :unique => true add_index "members", ["user_id"], :name => "index_members_on_user_id" create_table "memos", :force => true do |t| @@ -690,7 +710,7 @@ ActiveRecord::Schema.define(:version => 20140527060344) do end create_table "relative_memos", :force => true do |t| - t.integer "osp_id", :null => false + t.integer "osp_id" t.integer "parent_id" t.string "subject", :null => false t.text "content", :null => false