Skip to content

Commit

Permalink
Merge pull request #1 from AmurSU/plans
Browse files Browse the repository at this point in the history
Поддержка учебных планов и автогенерации карт нагрузок
  • Loading branch information
Envek committed Mar 13, 2012
2 parents 5d2b2ec + dac4118 commit dcdaabb
Show file tree
Hide file tree
Showing 39 changed files with 690 additions and 34 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem "pdfkit"
gem "pg"
gem 'whenever'
gem 'unicode'
gem 'nokogiri'

group :production do
gem 'unicorn'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GEM
net-ssh (2.1.4)
net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
nokogiri (1.5.0)
open4 (1.3.0)
pdfkit (0.5.2)
pg (0.12.2)
Expand Down Expand Up @@ -94,6 +95,7 @@ DEPENDENCIES
mail
net-scp (~> 1.0.4)
net-ssh (~> 2.1.4)
nokogiri
pdfkit
pg
rails (~> 2.3.8)
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/dept_head/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,25 @@ class DeptHead::GroupsController < DeptHead::BaseController
config.columns = [:name, :forming_year, :course, :population]
config.create.columns.exclude :course
config.update.columns.exclude :course
config.action_links.add :teaching_plan, :label => "Учебный план", :type => :member, :page => true
end

def teaching_plan
@group = Group.find(params[:id])
@charge_cards = @group.charge_cards
@lesson_types = LessonType.all(:order => :id)
@teaching_plans = TeachingPlan.find_all_by_speciality_id_and_course_and_semester(
@group.speciality_id, @group.course, TAURUS_CONFIG["semester"]["current"]["number"]
)
discipline_ids = (@charge_cards.map{|cc| cc.discipline_id} + @teaching_plans.map{|tp| tp.discipline_id}).uniq
@disciplines = Discipline.find(discipline_ids, :order => :name)
render "application/groups/teaching_plans/show"
end

protected

def current_user
return current_dept_head
end

end
67 changes: 66 additions & 1 deletion app/controllers/dept_head/specialities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
class DeptHead::SpecialitiesController < DeptHead::BaseController
include GosinspParser

active_scaffold do |config|
config.actions << :delete
config.columns = [:code, :name]
config.nested.add_link('Группы', [:groups])
config.action_links.add :teaching_plan, :label => "Учебный план", :type => :member, :page => true
config.action_links.add :teaching_plan_import, :label => "Импорт учебного плана", :type => :collection, :page => true
config.action_links.add :add_charge_cards, :label => "Автосоздание карт нагрузки", :type => :member, :page => true
end

def teaching_plan
@speciality = Speciality.find(params[:id])
@teaching_plans = TeachingPlan.find_all_by_speciality_id(@speciality.id)
discipline_ids = @teaching_plans.map{|tp| tp.discipline_id}.uniq
@disciplines = Discipline.find(discipline_ids, :order => :name)
@courses = @teaching_plans.map{|tp| tp.course}.uniq.sort
render "application/specialities/teaching_plans/show"
end

def teaching_plan_import
if params[:plan] and params[:plan].class == Tempfile
@specialities = current_dept_head.department.specialities
@speciality, @results, @errors = parse_and_fill_teaching_plan(params[:plan].read, @specialities)
render "supervisor/teaching_plans/fill"
return
end
end

def add_charge_cards
@speciality = Speciality.find(params[:id])
discipline_ids = current_dept_head.department.disciplines.all(:select => "id").map{|d| d.id}
to_remove_ids = @speciality.groups.map{|g| g.jets}.flatten.map{|j| j.charge_card_id}.uniq
@cards_to_remove = ChargeCard.count(:conditions => {:id => to_remove_ids, :discipline_id => discipline_ids})
end

def create_charge_cards
@speciality = Speciality.find(params[:id])
discipline_ids = current_dept_head.department.disciplines.all(:select => "id").map{|d| d.id}
conditions = {:speciality_id => @speciality.id, :discipline_id => discipline_ids}
if params[:remove]
ids = @speciality.groups.map{|g| g.jets}.flatten.map{|j| j.charge_card_id}.uniq
deleted = (ChargeCard.destroy_all(:id => ids, :discipline_id => discipline_ids)).count
end
created = 0
@courses = TeachingPlan.all(:conditions => {:speciality_id => @speciality.id}, :select => "DISTINCT(course)").map{|p| p.course}.sort
@courses.each do |course|
groups = @speciality.groups.select{|g| g.course == course}
if groups.any?
conditions.merge!({:course => course, :semester => TAURUS_CONFIG["semester"]["current"]["number"]})
plans = TeachingPlan.all(:conditions => conditions)
plans.each do |plan|
created += (plan.create_charge_cards_for(groups)).count
end
end
end
redirect_to "/dept_head/specialities", :notice => "Создано карт нагрузок: #{created}#{", удалено: #{deleted}" if deleted}"
end

protected
Expand All @@ -15,9 +68,21 @@ def before_create_save(record)

def conditions_for_collection
if dept = current_dept_head.department
{:department_id => dept.id}
discipline_ids = current_dept_head.department.disciplines.all(:select => "id").map{|d| d.id}
conditions = {:discipline_id => discipline_ids, :semester => TAURUS_CONFIG["semester"]["current"]["number"]}
ids = TeachingPlan.all(:conditions => conditions, :select => "DISTINCT(speciality_id)").map{ |tp| tp.speciality_id }
["department_id = :department_id OR id IN (:id)", {:department_id => dept.id, :id => ids }]
else
{:department_id => nil}
end
end

def custom_finder_options
{:order => "department_id = #{current_dept_head.department_id} DESC, code ASC"}
end

def current_user
return current_dept_head
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ def index
def show
@group = Group.find(params[:group_id])
@charge_cards = @group.charge_cards
@disciplines = Discipline.find(@charge_cards.map{|cc| cc.discipline_id}, :order => :name)
@lesson_types = LessonType.all(:order => :id)
@teaching_plans = TeachingPlan.find_all_by_speciality_id_and_course_and_semester(
@group.speciality_id, @group.course, TAURUS_CONFIG["semester"]["current"]["number"]
)
discipline_ids = (@charge_cards.map{|cc| cc.discipline_id} + @teaching_plans.map{|tp| tp.discipline_id}).uniq
@disciplines = Discipline.find(discipline_ids, :order => :name)
render "application/groups/teaching_plans/show"
end

end
2 changes: 1 addition & 1 deletion app/controllers/supervisor/departments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Supervisor::DepartmentsController < Supervisor::BaseController
active_scaffold do |config|
config.actions << :delete
config.columns = [:name, :short_name]
config.columns = [:name, :short_name, :gosinsp_code]
config.nested.add_link('Специальности', [:specialities])
end
end
20 changes: 20 additions & 0 deletions app/controllers/supervisor/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,25 @@ class Supervisor::GroupsController < Supervisor::BaseController
config.create.columns.exclude :course
config.update.columns.exclude :course
config.columns[:speciality].form_ui = :select
config.action_links.add :teaching_plan, :label => "Учебный план", :type => :member, :page => true
end

def teaching_plan
@group = Group.find(params[:id])
@charge_cards = @group.charge_cards
@lesson_types = LessonType.all(:order => :id)
@teaching_plans = TeachingPlan.find_all_by_speciality_id_and_course_and_semester(
@group.speciality_id, @group.course, TAURUS_CONFIG["semester"]["current"]["number"]
)
discipline_ids = (@charge_cards.map{|cc| cc.discipline_id} + @teaching_plans.map{|tp| tp.discipline_id}).uniq
@disciplines = Discipline.find(discipline_ids, :order => :name)
render "application/groups/teaching_plans/show"
end

protected

def current_user
return nil
end

end
16 changes: 16 additions & 0 deletions app/controllers/supervisor/specialities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ class Supervisor::SpecialitiesController < Supervisor::BaseController
config.columns = [:code, :name, :department]
config.nested.add_link('Группы', [:groups])
config.columns[:department].form_ui = :select
config.action_links.add :teaching_plan, :label => "Учебный план", :type => :member, :page => true
end

def teaching_plan
@speciality = Speciality.find(params[:id])
@teaching_plans = TeachingPlan.find_all_by_speciality_id(@speciality.id)
discipline_ids = @teaching_plans.map{|tp| tp.discipline_id}.uniq
@disciplines = Discipline.find(discipline_ids, :order => :name)
@courses = @teaching_plans.map{|tp| tp.course}.uniq.sort
render "application/specialities/teaching_plans/show"
end

protected

def current_user
return nil
end

end
14 changes: 14 additions & 0 deletions app/controllers/supervisor/teaching_plans_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Supervisor::TeachingPlansController < Supervisor::BaseController

include GosinspParser

def new
end

def fill
if params[:plan] and params[:plan].class == Tempfile
@speciality, @results, @errors = parse_and_fill_teaching_plan(params[:plan].read)
end
end

end
5 changes: 5 additions & 0 deletions app/helpers/dept_head/specialities_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
module DeptHead::SpecialitiesHelper

def list_row_class(record)
'alien-speciality' if record.department_id != current_dept_head.department_id
end

end
2 changes: 2 additions & 0 deletions app/helpers/supervisor/teaching_plans_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Supervisor::TeachingPlansHelper
end
22 changes: 20 additions & 2 deletions app/models/charge_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ChargeCard < ActiveRecord::Base
has_many :groups, :through => :jets
has_many :pairs, :dependent => :destroy

validates_presence_of :discipline, :lesson_type, :teaching_place, :weeks_quantity, :hours_per_week
validates_presence_of :discipline, :lesson_type, :weeks_quantity, :hours_per_week
validates_numericality_of :weeks_quantity, :hours_per_week

named_scope :with_recommended_first_for, lambda { |department|
Expand All @@ -33,7 +33,7 @@ def name
end

def name_for_pair_edit
[teaching_place.name, assistant_teaching_place.try(:to_label), name].compact.join(", ")
[teaching_place.try(:name), assistant_teaching_place.try(:to_label), name].compact.join(", ")
end

def hours_quantity
Expand Down Expand Up @@ -62,6 +62,24 @@ def editor_name_with_recommendation
"#{editor_name} #{"(рекомендуется)" if recommended}"
end

def self.for_autocreation(discipline_id, lesson_type_id, groups)
groups = [groups].flatten # In case of single group make it look like an array
pretendents = all(:joins => :jets, :conditions => {
:discipline_id => discipline_id,
:lesson_type_id => lesson_type_id,
:jets => {:group_id => groups}
}
)
pretendents = pretendents.find_all {|cc| cc.groups == groups }
if pretendents.empty?
return new(:discipline_id => discipline_id, :lesson_type_id => lesson_type_id)
else
card = pretendents.first
card.instance_variable_set("@readonly", false) # Very dirty hack to avoid ActiveRecord::ReadOnlyRecord exception
return card
end
end

private

def remove_pairs
Expand Down
3 changes: 3 additions & 0 deletions app/models/department.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ class Department < ActiveRecord::Base
has_many :lecturers, :through => :teaching_places
has_many :specialities
has_many :dept_heads

validates_numericality_of :gosinsp_code, :allow_nil => true

end
1 change: 1 addition & 0 deletions app/models/discipline.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Discipline < ActiveRecord::Base
belongs_to :department
has_many :charge_cards
has_many :teaching_plans

validates_presence_of :department, :name, :short_name
validates_uniqueness_of :name, :scope => :department_id
Expand Down
13 changes: 13 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,18 @@ def descriptive_name
def self.escape_name(name)
name.to_s.gsub('%', '\%').gsub('_', '\_') + '%'
end

protected

def authorized_for_update?
return true unless current_user
self.speciality.try(:department_id) == current_user.department_id
end

def authorized_for_delete?
return true unless current_user
self.speciality.try(:department_id) == current_user.department_id
end

end

14 changes: 14 additions & 0 deletions app/models/speciality.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
class Speciality < ActiveRecord::Base
belongs_to :department
has_many :groups
has_many :teaching_plans

validates_presence_of :department

def to_label
"#{name} (#{code})"
end

protected

def authorized_for_update?
return true unless current_user
self.department_id == current_user.department_id
end

def authorized_for_delete?
return true unless current_user
self.department_id == current_user.department_id
end

end
46 changes: 46 additions & 0 deletions app/models/teaching_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class TeachingPlan < ActiveRecord::Base
belongs_to :speciality
belongs_to :discipline

validates_presence_of :speciality_id, :discipline_id, :course, :semester
validates_numericality_of :course, :semester
validates_numericality_of :lections, :practics, :lab_works, :allow_nil => true
validates_inclusion_of :semester, :in => [1, 2]

def create_charge_cards_for(groups)
created = []
if self.lections
card = ChargeCard.for_autocreation(self.discipline_id, 1, groups)
card.weeks_quantity = 18
card.hours_per_week = self.lections / 18
if card.save
created << card
card.groups = groups unless card.groups == groups
end
end
if self.practics
groups.each do |group|
card = ChargeCard.for_autocreation(self.discipline_id, 2, group)
card.weeks_quantity = 18
card.hours_per_week = self.practics / 18
if card.save
created << card
card.groups = [group] unless card.groups == [group]
end
end
end
if self.lab_works
groups.each do |group|
card = ChargeCard.for_autocreation(self.discipline_id, 3, group)
card.weeks_quantity = 18
card.hours_per_week = self.lab_works / 18
if card.save
created << card
card.groups = [group] unless card.groups == [group]
end
end
end
return created
end

end
Loading

0 comments on commit dcdaabb

Please sign in to comment.