1.课程control添加create方法

2.课程model添加部分新列及相关逻辑
This commit is contained in:
nwb 2014-06-04 15:47:09 +08:00
parent 4404222437
commit fb36a0d5ba
6 changed files with 160 additions and 86 deletions

View File

@ -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]<activity_count[j+1]
courses[j],courses[j+1]=courses[j+1],courses[j]
activity_count[j],activity_count[j+1]=activity_count[j+1],activity_count[j]
courses[j], courses[j+1]=courses[j+1], courses[j]
activity_count[j], activity_count[j+1]=activity_count[j+1], activity_count[j]
end
end
end
@ -125,49 +181,49 @@ class CoursesController < ApplicationController
case params[:course_sort_type]
when '0'
@courses = @courses_all.order("created_at desc")
@courses = @courses_all.order("created_at desc")
@s_type = 0
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
#gcm
@course_activity_count=get_course_activity @courses,@course_activity_count
@course_activity_count=get_course_activity @courses, @course_activity_count
#gcmend
when '1'
@courses = @courses_all.order("course_ac_para desc")
@s_type = 1
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
#gcm
@course_activity_count=get_course_activity @courses,@course_activity_count
@course_activity_count=get_course_activity @courses, @course_activity_count
#gcmend
when '2'
@courses = @courses_all.order("watchers_count desc")
@s_type = 2
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
#gcm
@course_activity_count=get_course_activity @courses,@course_activity_count
@course_activity_count=get_course_activity @courses, @course_activity_count
#gcmend
#gcm
when '3'
#gcm
@course_activity_count=get_course_activity @courses_all,@course_activity_count
@course_activity_count=get_course_activity @courses_all, @course_activity_count
#gcmend
@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
@s_type = 0
@courses = @courses_all.order("created_at desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
#gcm
@course_activity_count=get_course_activity @courses,@course_activity_count
@course_activity_count=get_course_activity @courses, @course_activity_count
#gcmend
end
@ -184,7 +240,7 @@ class CoursesController < ApplicationController
end
def get_course_activity courses,activities
def get_course_activity courses, activities
@course_ids=activities.keys()
days = Setting.activity_days_default.to_i
@ -192,14 +248,14 @@ class CoursesController < ApplicationController
date_from = date_to - days-1.years
#file_count
Attachment.where(container_id: @course_ids, container_type: Project).where("created_on>?",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

View File

@ -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

View File

@ -17,21 +17,21 @@
<% object = [] %>
<% object << 'project' %>
<% object << 'course' %>
<% object << 'course' %>
<%= error_messages_for object %>
<!--[form:project]-->
<% unless @project.new_record? %>
<p><%= render :partial=>"avatar/avatar_form",:locals=> {source:@project} %></p>
<!--[form:course]-->
<% unless @course.new_record? %>
<p><%= render :partial=>"avatar/avatar_form",:locals=> {source:@course} %></p>
<% end %>
<!-- <p><%= f.text_field :name, :required => true, :size => 60, :style => "width:490px;" %></p> -->
<p><label for="project_name" style="font-size: 13px;" ><%=l(:label_tags_course_name)%><span class="required", > *&nbsp;&nbsp;</span></label><input id="project_name" type="text" value="<%=@project.name%>" style="width:490px;" size="60" name="project[name]"></p>
<p><label for="course_name" style="font-size: 13px;" ><%=l(:label_tags_course_name)%><span class="required" > *&nbsp;&nbsp;</span></label><input id="course_name" type="text" value="<%=@course.name%>" style="width:490px;" size="60" name="course[name]"></p>
<!-- <p><%= f.text_field :identifier, :required => true, :size => 60, :style => "width:488px;", :disabled => @project.identifier_frozen?, :maxlength => Project::IDENTIFIER_MAX_LENGTH %>
<% unless @project.identifier_frozen? %>
<em class="info"><%= l(:text_length_between, :min => 1, :max => Project::IDENTIFIER_MAX_LENGTH) %> <%= l(:text_project_identifier_info).html_safe %></em>
<!-- <p><%= f.text_field :extra, :required => true, :size => 60, :style => "width:488px;", :disabled => @course.extra_frozen?, :maxlength => Project::IDENTIFIER_MAX_LENGTH %>
<% unless @course.extra_frozen? %>
<em class="info"><%= l(:text_length_between, :min => 1, :max => Project::IDENTIFIER_MAX_LENGTH) %> <%= l(:text_course_identifier_info).html_safe %></em>
<% end %></p> -->
<!-- added by bai 新增开课时间、结课时间、课时 -->
@ -294,46 +294,33 @@
<!-- added by huang -->
<!-- <p style="margin-left:-10px;"><%= m.text_field :password, :required => true, :size => 60, :style => "width:488px;margin-left: 10px;" %></p> -->
<p style="margin-left:-10px;"><label for="project[course]_password" style="font-size: 13px;" ><%=l(:label_new_course_password)%><span class="required"> *</span></label><input id="project_course_password" type="text" style="width:488px;margin-left: 10px;" value="<%=@project.course_extra.password if @project.course_extra %>" size="60" name="project[course][password]"></p>
<p style="margin-left:-10px;"><label for="course[course]_password" style="font-size: 13px;" ><%=l(:label_new_course_password)%><span class="required"> *</span></label><input id="course_course_password" type="text" style="width:488px;margin-left: 10px;" value="<%=@course.password %>" size="60" name="course[password]"></p>
<em class="info" style="margin-left:95px;"><%= l(:text_command) %></em>
<% end %>
<!-- <p style="margin-left:-10px;padding-right: 20px;"><%= f.text_area :description, :rows => 8, :class => 'wiki-edit', :style => "font-size:small;width:490px;margin-left:10px;" %></p> -->
<!--new added -->
<p style="margin-left:-20px;padding-right: 20px;"><label for="project_description" style="font-size: 13px;"><%=l(:label_new_course_description)%></label><span class="jstEditor"><textarea id="project_description" class="wiki-edit" style="font-size:small;width:490px;margin-left:10px;" rows="8" name="project[description]" cols="40" ><%=@project.description%></textarea></span></p>
<p style="margin-left:-20px;padding-right: 20px;"><label for="course_description" style="font-size: 13px;"><%=l(:label_new_course_description)%></label><span class="jstEditor"><textarea id="course_description" class="wiki-edit" style="font-size:small;width:490px;margin-left:10px;" rows="8" name="course[description]" cols="40" ><%=@course.description%></textarea></span></p>
<p style="margin-left:-10px;"><em style ="color: #888888;display: block;font-size: 90%;font-style: normal;"><%= f.check_box :is_public, :style => "margin-left:10px;" %><%= l(:label_public_info) %></em></p><!-- modified by bai -->
<p style="display:none;"><%= f.text_field :project_type, :value => 1 %></p>
<p style="display:none;"><%= f.text_field :course_type, :value => 1 %></p>
<%= wikitoolbar_for 'project_description' %>
<%= wikitoolbar_for 'course_description' %>
<% @project.custom_field_values.each do |value| %>
<p><%= custom_field_tag_with_label :project, value %></p>
<% @course.custom_field_values.each do |value| %>
<p><%= custom_field_tag_with_label :course, value %></p>
<% end %>
<%= call_hook(:view_projects_form, :project => @project, :form => f) %>
<%= call_hook(:view_courses_form, :course => @course, :form => f) %>
<!--[eoform:project]-->
<!--[eoform:course]-->
<% 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 %>

View File

@ -7,7 +7,7 @@
<% else %>
<p class="font_description">
<!--teacher could create the course directly-->
<%= 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' %>
</p>
<% end %>
<% else %>

View File

@ -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

View File

@ -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