Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (101 sloc) 5.251 kb
716c8e8 Serj L Link for timetable added in toolbar (test added for that). Size of toolb...
authored
1 # encoding: UTF-8
2 class TimetablesController < ApplicationController
fe27710 Serj L Editing/updating one timetable row for school class. Added foreign key f...
authored
3 before_filter :authenticate_school_heads, :only => [ :index, :new, :create, :edit,
4 :update ]
716c8e8 Serj L Link for timetable added in toolbar (test added for that). Size of toolb...
authored
5
6 def index
149d756 Serj L Translations to Russian language for Type of class and day of week.
authored
7 @classes = SchoolClass.order( :class_code ) # List of classes, use this in buttons to create/filter.
2eb2269 Serj L Now index page of Timetable shows absolute every row for chosen school c...
authored
8 @tts = []; @class_code = nil
db8d88a Serj L Timetable showing (alpha version, only for monday now).
authored
9
10 if params.has_key?( :class_code )
11 school_class = SchoolClass.where( "class_code = ?", params[:class_code] ).first
2eb2269 Serj L Now index page of Timetable shows absolute every row for chosen school c...
authored
12 @class_code = school_class.class_code
fe27710 Serj L Editing/updating one timetable row for school class. Added foreign key f...
authored
13 tts = timetable_for_class( school_class ) # Here we get timetable for class only if subject has been choosed.
14 @tt_monday = sorted_timetable_for_day( tts, "Mon" ) # Timetable for monday.
15 @tt_tuesday = sorted_timetable_for_day( tts, "Tue" ) # Timetable for tuesday.
16 @tt_wednesday = sorted_timetable_for_day( tts, "Wed" ) # Timetable for wednesday.
17 @tt_thursday = sorted_timetable_for_day( tts, "Thu") # Timetable for thursday.
18 @tt_friday = sorted_timetable_for_day( tts, "Fri") # Timetable for friday.
db8d88a Serj L Timetable showing (alpha version, only for monday now).
authored
19 end
716c8e8 Serj L Link for timetable added in toolbar (test added for that). Size of toolb...
authored
20 end
8deabd5 Serj L Test for new method of controller added. New/Create methods (empty now) ...
authored
21
22 def new
149d756 Serj L Translations to Russian language for Type of class and day of week.
authored
23 @types_of_lesson = collect_types_of_lesson
24
003ccd5 Serj L Do not allow to create new timetable for class if old one already exists...
authored
25 if ( params.has_key?( :class_code ) ) # If you pushed button to create class and you choosed class.
dc85433 Serj L Timetable saving. Early version.
authored
26 @class = SchoolClass.where( "class_code = ?", params[:class_code] ).first
003ccd5 Serj L Do not allow to create new timetable for class if old one already exists...
authored
27 $global_class = @class # Here we use global var because after redirect we loose our school class.
dc85433 Serj L Timetable saving. Early version.
authored
28 else
29 @class = $global_class
30 end
8deabd5 Serj L Test for new method of controller added. New/Create methods (empty now) ...
authored
31
003ccd5 Serj L Do not allow to create new timetable for class if old one already exists...
authored
32 # Check if timetable for this class already has been created and then redirect or
33 # allowing to create it.
34 unless timetable_exists?( $global_class )
35 @tt = Timetable.new
36 @subjects = collect_subjects_with_curriculums( @class )
37 else
38 flash[:notice] = "Расписание для класса #{$global_class.class_code} уже было " +
39 "создано! Пожалуйста, редактируйте уже имеющееся расписание!"
40 redirect_to timetables_path( :class_code => $global_class.class_code ) # Show created timetable for class.
41 end
8deabd5 Serj L Test for new method of controller added. New/Create methods (empty now) ...
authored
42 end
43
44 def create
dc85433 Serj L Timetable saving. Early version.
authored
45 errors_messages = []
46
47 # Take, validate and save/or collect errors for user's timetable.
48 params[:timetable].each do |i, values| # Where i is the i-th set and values are the user inputs.
49 @tt = Timetable.new( values )
50
51 if not @tt.save
52 errors_messages << @tt.errors.full_messages.to_sentence
53 end
54 end
8deabd5 Serj L Test for new method of controller added. New/Create methods (empty now) ...
authored
55
dc85433 Serj L Timetable saving. Early version.
authored
56 if errors_messages.empty?
57 flash[:success] = "Расписание успешно создано!"
58 redirect_to timetables_path
59 else
60 flash[:error] = errors_messages.to_sentence :last_word_connector => ", ",
61 :two_words_connector => ", "
62 redirect_to new_timetable_path
63 end
8deabd5 Serj L Test for new method of controller added. New/Create methods (empty now) ...
authored
64 end
dc85433 Serj L Timetable saving. Early version.
authored
65
149d756 Serj L Translations to Russian language for Type of class and day of week.
authored
66 def edit
67 @types_of_lesson = collect_types_of_lesson
fe27710 Serj L Editing/updating one timetable row for school class. Added foreign key f...
authored
68 @tt = Timetable.find( params[:id] )
69 @subjects_with_curriculums = collect_subjects_with_curriculums( @tt.school_class )
70 end
71
72 def update
73 @tt = Timetable.find( params[:id] )
dc85433 Serj L Timetable saving. Early version.
authored
74
fe27710 Serj L Editing/updating one timetable row for school class. Added foreign key f...
authored
75 if @tt.update_attributes( params[:timetable] )
76 flash[:success] = "Расписание успешно обновлено!"
77 redirect_to timetables_path
78 else
79 flash.now[:error] = @tt.errors.full_messages.to_sentence :last_word_connector => ", ",
80 :two_words_connector => ", "
81 render 'edit'
82 end
83 end
84
85 private
dc85433 Serj L Timetable saving. Early version.
authored
86 # Collecting subjects names for school class and curriculum_id for each subject.
87 def collect_subjects_with_curriculums( school_class )
88 subjects = school_class.curriculums.collect do |c|
89 [ c.qualification.subject.subject_name, c.id ]
90 end
91 end
db8d88a Serj L Timetable showing (alpha version, only for monday now).
authored
92
0d73b44 Serj L Timetable shows for each day and it sorts by number of lesson. View for ...
authored
93 # Return for school class it's timetable.
db8d88a Serj L Timetable showing (alpha version, only for monday now).
authored
94 def timetable_for_class( school_class )
2eb2269 Serj L Now index page of Timetable shows absolute every row for chosen school c...
authored
95 Timetable.select{|t| t.school_class.class_code == school_class.class_code }.to_a
db8d88a Serj L Timetable showing (alpha version, only for monday now).
authored
96 end
97
98 def subjects_of_class( school_class )
99 subjects = school_class.curriculums.collect do |c|
100 c.qualification.subject.subject_name
101 end
102 end
0d73b44 Serj L Timetable shows for each day and it sorts by number of lesson. View for ...
authored
103
104 # Return sorted by number of lesson tometable for one day.
105 def sorted_timetable_for_day( timetable, day )
106 timetable.select{ |t| t.tt_day_of_week == day }
107 .sort_by{ |e| e[:tt_number_of_lesson] }
108 end
149d756 Serj L Translations to Russian language for Type of class and day of week.
authored
109
003ccd5 Serj L Do not allow to create new timetable for class if old one already exists...
authored
110 # Return russian name for type of lesson.
149d756 Serj L Translations to Russian language for Type of class and day of week.
authored
111 def collect_types_of_lesson
112 [ ["Обязательное занятие", "Primary lesson"], ["Электив", "Extra"] ]
113 end
003ccd5 Serj L Do not allow to create new timetable for class if old one already exists...
authored
114
115 # Check if timetable already has been created for school class.
116 def timetable_exists?( school_class )
117 not timetable_for_class( school_class ).empty?
118 end
716c8e8 Serj L Link for timetable added in toolbar (test added for that). Size of toolb...
authored
119 end
Something went wrong with that request. Please try again.