diff --git a/app/assets/javascripts/org_courses.js.coffee b/app/assets/javascripts/org_courses.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/org_courses.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/org_courses.css.scss b/app/assets/stylesheets/org_courses.css.scss new file mode 100644 index 000000000..026af5d01 --- /dev/null +++ b/app/assets/stylesheets/org_courses.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the org_courses controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 1a561006a..fd53660c6 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -29,6 +29,29 @@ class CoursesController < ApplicationController before_filter :require_login, :only => [:join, :unjoin] #before_filter :allow_join, :only => [:join] + #查找组织 + def search_public_orgs_not_in_course + condition = '%%' + if !params[:name].nil? + condition = "%#{params[:name].strip}%".gsub(" ","") + end + course_org_ids = OrgCourse.find_by_sql("select distinct organization_id from org_courses where course_id = #{params[:id]}").map(&:organization_id) + if course_org_ids.empty? + @orgs_not_in_course = Organization.where("(is_public or creator_id =?) and name like ?",User.current.id, condition).page((params[:page].to_i || 1)).per(10) + @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count + else + course_org_ids = "(" + course_org_ids.join(',') + ")" + @orgs_not_in_course = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page((params[:page].to_i || 1)).per(10) + @org_count = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count + end + # @course_count = Project.course_entities.visible.like(params[:name]).page(params[:page]).count + @orgs_page = Paginator.new @org_count, 10,params[:page] + #render :json => {:orgs => @orgs_not_in_course, :count => @org_count}.to_json + respond_to do |format| + format.js + end + end + def join if User.current.logged? cs = CoursesService.new diff --git a/app/controllers/org_courses_controller.rb b/app/controllers/org_courses_controller.rb new file mode 100644 index 000000000..d7e59382a --- /dev/null +++ b/app/controllers/org_courses_controller.rb @@ -0,0 +1,48 @@ +class OrgCoursesController < ApplicationController + def create + org_ids = params[:orgNames] + @course = Course.find(params[:course_id]) + org_ids.each do |org_id| + OrgCourse.create(:organization_id => org_id.to_i, :course_id => params[:course_id].to_i, :created_at => Time.now) + end + + condition = '%%' + if !params[:name].nil? + condition = "%#{params[:name].strip}%".gsub(" ","") + end + course_org_ids = OrgCourse.find_by_sql("select distinct organization_id from org_courses where course_id = #{params[:course_id]}").map(&:organization_id) + if course_org_ids.empty? + @orgs_not_in_course = Organization.where("(is_public or creator_id =?) = 1 and name like ?",User.current.id, condition).page((params[:page].to_i || 1)).per(10) + @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count + else + course_org_ids = "(" + course_org_ids.join(',') + ")" + @orgs_not_in_course = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page((params[:page].to_i || 1)).per(10) + @org_count = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count + end + # @course_count = Course.course_entities.visible.like(params[:name]).page(params[:page]).count + @orgs_page = Paginator.new @org_count, 10,params[:page] + #render :json => {:orgs => @orgs_not_in_course, :count => @org_count}.to_json + respond_to do |format| + format.js + end + end + + def destroy + @course = Course.find(params[:course_id]) + @org_course = OrgCourse.find(params[:id]) + @org_course.destroy + + condition = '%%' + course_org_ids = OrgCourse.find_by_sql("select distinct organization_id from org_courses where course_id = #{params[:course_id]}").map(&:organization_id) + if course_org_ids.empty? + @orgs_not_in_course = Organization.where("(is_public or creator_id =?) = 1 and name like ?",User.current.id, condition).page( 1).per(10) + @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count + else + course_org_ids = "(" + course_org_ids.join(',') + ")" + @orgs_not_in_course = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page( 1).per(10) + @org_count = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count + end + # @course_count = Course.course_entities.visible.like(params[:name]).page(params[:page]).count + @orgs_page = Paginator.new @org_count, 10,1 + end +end diff --git a/app/helpers/org_courses_helper.rb b/app/helpers/org_courses_helper.rb new file mode 100644 index 000000000..28655e52e --- /dev/null +++ b/app/helpers/org_courses_helper.rb @@ -0,0 +1,2 @@ +module OrgCoursesHelper +end diff --git a/app/models/course.rb b/app/models/course.rb index 7288c3b3b..a9f72ad61 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -18,6 +18,8 @@ class Course < ActiveRecord::Base :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE})" has_many :principals, :through => :member_principals, :source => :principal has_many :users, :through => :members + has_many :org_courses + has_many :organizations, :through => :org_courses # has_many :homeworks, :through => :homework_for_courses, :source => :bid, :dependent => :destroy has_many :journals_for_messages, :as => :jour, :dependent => :destroy # has_many :homework_for_courses, :dependent => :destroy diff --git a/app/models/org_course.rb b/app/models/org_course.rb new file mode 100644 index 000000000..8c198794a --- /dev/null +++ b/app/models/org_course.rb @@ -0,0 +1,5 @@ +class OrgCourse < ActiveRecord::Base + #attr_accessible :organization, :course, :created_at + belongs_to :organization + belongs_to :course +end diff --git a/app/models/organization.rb b/app/models/organization.rb index 7778da477..d3755b5ee 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -3,7 +3,9 @@ class Organization < ActiveRecord::Base has_many :org_members, :dependent => :destroy has_many :org_projects ,:dependent => :destroy has_many :projects,:through => :org_projects + has_many :courses, :through => :org_courses has_many :org_document_comments, :dependent => :destroy + has_many :org_courses has_many :users, :through => :org_members validates_uniqueness_of :name after_create :save_as_org_activity diff --git a/app/views/courses/search_public_orgs_not_in_course.js.erb b/app/views/courses/search_public_orgs_not_in_course.js.erb new file mode 100644 index 000000000..76f9bcedb --- /dev/null +++ b/app/views/courses/search_public_orgs_not_in_course.js.erb @@ -0,0 +1,13 @@ +$("#search_orgs_result_list").html(""); +$("#search_orgs_result_list").append('