/
courses_controller.rb
159 lines (133 loc) · 4.82 KB
/
courses_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class CoursesController < ApplicationController
before_filter :require_user
append_before_filter :authorized?
after_filter :expire_cache, :only => [:create, :update, :destroy]
def new
@course = Course.new
@teacher = Teacher.find(current_user)
@school_years = SchoolYear.active
end
def edit
@course = Course.find(params[:id], :include => [:enrollments, :students])
@course_terms = CourseTerm.find_all_by_course_id(@course,
:include => [:supporting_skills, :term], :order => "date_ranges.end_date")
@homerooms = Student.homerooms
@skill_cats = SupportingSkillCategory.active.all( :include => :supporting_skills )
respond_to do |format|
format.html
format.js {
# The format of the params[value] is studentid||header name||type. This
# allows us to load the proper list of students and label the table
# header with something appropriate. It is a hack and I'm sure there
# is a better way to do this.
value = params[:value].split('||')
if value.pop == 'H' then
# Find students by "Home Room"
@students = Student.active.sorted.find_all_by_homeroom(value[0])
else
# Find students by "Class Of"
@students = Student.active.sorted.find_all_by_class_of(value[0])
end
render :partial => "student_list"
}
end
end
def create
@course = Course.new(params[:course])
@school_years = SchoolYear.active
# Force the course to be created by the current user
@course.teacher = current_user
# Insert the grading terms
@course.terms << SchoolYear.find(params[:school_year][:id]).terms
if @course.save
flash[:notice] = "Course '#{@course.name}' was successfully created."
redirect_to(courses_url)
else
render :action => "new"
end
end
def update
# Are we updating the course or assigning a supporting skill?
if params[:skill]
# Add or remove supporting_skills to this course
course_term = CourseTerm.find(params[:id])
course_term.supporting_skills.delete(SupportingSkill.find(params[:skill]["false"])) if params[:skill]["false"]
course_term.supporting_skills << SupportingSkill.find(params[:skill]["true"]) if params[:skill]["true"]
@course = course_term.course
else
@course = Course.find(params[:id])
end
respond_to do |format|
if @course.update_attributes(params[:course])
flash[:notice] = "Course '#{@course.name}' was successfully updated."
format.html { redirect_to(courses_url) }
format.js { render :nothing => true }
else
flash[:error] = "Course '#{@course.name}' failed to update."
format.html { redirect_to(:action => "edit") }
format.js { head :unprocessable_entity }
end
end
end
def destroy
@course = Course.find(params[:id]).destroy
flash[:notice] = "Course '#{@course.name}' was successfully deleted."
redirect_to :action => :index
end
# Add student(s) to a course
def add_student
@course = Course.find(params[:id])
# Are we adding one student or an array of students?
if params[:students]
# Add all the students to the course
students = Student.find(params[:students])
students.each do |student|
begin
@course.students << student
student.homeroom = @course.name if @course.is_homeroom?
student.save!
rescue ActiveRecord::RecordInvalid
# This is here to catch an existing student being added to a course
end
end
@course.save
else
# Add a single student to the course and save it
@student = Student.find(params[:student_id])
@add = true
if @course.students.index(@student) == nil
@course.students << @student
@course.save
# If this course is a homeroom then change this student's homeroom
@student.homeroom = @course.name if @course.is_homeroom?
@student.save!
end
end
render :action => "modify", :locals => { :add => true }
end
# Remove a student from a course
def remove_student
@course = Course.find(params[:id])
@student = Student.find(params[:student_id])
@add = false
if @course.students.index(@student) != nil
@course.students -= [@student]
@course.save
end
render :action => "modify", :locals => { :add => false }
end
# Toggle the accommodation flag on/off.
def toggle_accommodation
@enrollment = Enrollment.find(params[:id])
@enrollment.toggle!(:accommodation)
render :nothing => true
end
private
def expire_cache
# Expire this users cache
# OPTIMIZE: The way I understand it, manually coding these expires is more
# efficient than using a RegEx.
expire_fragment "course_list_#{current_user.id}_assignments"
expire_fragment "course_list_#{current_user.id}_evaluations"
end
end