Description:
problem toggle on/off
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r558:9fc5999b7c8e - - 3 files changed: 14 inserted, 21 deleted

@@ -1,196 +1,193
1 1 class ProblemsController < ApplicationController
2 2
3 3 before_filter :authenticate, :authorization
4 4
5 5 in_place_edit_for :problem, :name
6 6 in_place_edit_for :problem, :full_name
7 7 in_place_edit_for :problem, :full_score
8 8
9 9 def index
10 - list
11 - render :action => 'list'
10 + @problems = Problem.find(:all, :order => 'date_added DESC')
12 11 end
13 12
14 13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
15 14 verify :method => :post, :only => [ :destroy,
16 15 :create, :quick_create,
17 16 :do_manage,
18 17 :do_import,
19 18 :update ],
20 - :redirect_to => { :action => :list }
21 -
22 - def list
23 - @problems = Problem.find(:all, :order => 'date_added DESC')
24 - end
19 + :redirect_to => { :action => :index }
25 20
26 21 def show
27 22 @problem = Problem.find(params[:id])
28 23 end
29 24
30 25 def new
31 26 @problem = Problem.new
32 27 @description = nil
33 28 end
34 29
35 30 def create
36 31 @problem = Problem.new(params[:problem])
37 32 @description = Description.new(params[:description])
38 33 if @description.body!=''
39 34 if !@description.save
40 35 render :action => new and return
41 36 end
42 37 else
43 38 @description = nil
44 39 end
45 40 @problem.description = @description
46 41 if @problem.save
47 42 flash[:notice] = 'Problem was successfully created.'
48 - redirect_to :action => 'list'
43 + redirect_to action: :index
49 44 else
50 45 render :action => 'new'
51 46 end
52 47 end
53 48
54 49 def quick_create
55 50 @problem = Problem.new(params[:problem])
56 51 @problem.full_name = @problem.name if @problem.full_name == ''
57 52 @problem.full_score = 100
58 53 @problem.available = false
59 54 @problem.test_allowed = true
60 55 @problem.output_only = false
61 56 @problem.date_added = Time.new
62 57 if @problem.save
63 58 flash[:notice] = 'Problem was successfully created.'
64 - redirect_to :action => 'list'
59 + redirect_to action: :index
65 60 else
66 61 flash[:notice] = 'Error saving problem'
67 - redirect_to :action => 'list'
62 + redirect_to action: :index
68 63 end
69 64 end
70 65
71 66 def edit
72 67 @problem = Problem.find(params[:id])
73 68 @description = @problem.description
74 69 end
75 70
76 71 def update
77 72 @problem = Problem.find(params[:id])
78 73 @description = @problem.description
79 74 if @description == nil and params[:description][:body]!=''
80 75 @description = Description.new(params[:description])
81 76 if !@description.save
82 77 flash[:notice] = 'Error saving description'
83 78 render :action => 'edit' and return
84 79 end
85 80 @problem.description = @description
86 81 elsif @description!=nil
87 82 if !@description.update_attributes(params[:description])
88 83 flash[:notice] = 'Error saving description'
89 84 render :action => 'edit' and return
90 85 end
91 86 end
92 87 if params[:file] and params[:file].content_type != 'application/pdf'
93 88 flash[:notice] = 'Error: Uploaded file is not PDF'
94 89 render :action => 'edit' and return
95 90 end
96 91 if @problem.update_attributes(params[:problem])
97 92 flash[:notice] = 'Problem was successfully updated.'
98 93 unless params[:file] == nil or params[:file] == ''
99 94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
100 95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
101 96 if not FileTest.exists? out_dirname
102 97 Dir.mkdir out_dirname
103 98 end
104 99
105 100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
106 101 if FileTest.exists? out_filename
107 102 File.delete out_filename
108 103 end
109 104
110 105 File.open(out_filename,"wb") do |file|
111 106 file.write(params[:file].read)
112 107 end
113 108 @problem.description_filename = "#{@problem.name}.pdf"
114 109 @problem.save
115 110 end
116 111 redirect_to :action => 'show', :id => @problem
117 112 else
118 113 render :action => 'edit'
119 114 end
120 115 end
121 116
122 117 def destroy
123 118 Problem.find(params[:id]).destroy
124 - redirect_to :action => 'list'
119 + redirect_to action: :index
125 120 end
126 121
127 122 def toggle
128 123 @problem = Problem.find(params[:id])
129 - @problem.available = !(@problem.available)
130 - @problem.save
124 + @problem.update_attributes(available: !(@problem.available) )
125 + respond_to do |format|
126 + format.js {}
127 + end
131 128 end
132 129
133 130 def turn_all_off
134 131 Problem.find(:all,
135 132 :conditions => "available = 1").each do |problem|
136 133 problem.available = false
137 134 problem.save
138 135 end
139 - redirect_to :action => 'list'
136 + redirect_to action: :index
140 137 end
141 138
142 139 def turn_all_on
143 140 Problem.find(:all,
144 141 :conditions => "available = 0").each do |problem|
145 142 problem.available = true
146 143 problem.save
147 144 end
148 - redirect_to :action => 'list'
145 + redirect_to action: :index
149 146 end
150 147
151 148 def stat
152 149 @problem = Problem.find(params[:id])
153 150 unless @problem.available or session[:admin]
154 151 redirect_to :controller => 'main', :action => 'list'
155 152 return
156 153 end
157 154 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
158 155
159 156 #stat summary
160 157 range =65
161 158 @histogram = { data: Array.new(range,0), summary: {} }
162 159 user = Hash.new(0)
163 160 @submissions.find_each do |sub|
164 161 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
165 162 @histogram[:data][d.to_i] += 1 if d < range
166 163 user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max
167 164 end
168 165 @histogram[:summary][:max] = [@histogram[:data].max,1].max
169 166
170 167 @summary = { attempt: user.count, solve: 0 }
171 168 user.each_value { |v| @summary[:solve] += 1 if v == 1 }
172 169 end
173 170
174 171 def manage
175 172 @problems = Problem.find(:all, :order => 'date_added DESC')
176 173 end
177 174
178 175 def do_manage
179 176 if params.has_key? 'change_date_added'
180 177 change_date_added
181 178 elsif params.has_key? 'add_to_contest'
182 179 add_to_contest
183 180 elsif params.has_key? 'enable_problem'
184 181 set_available(true)
185 182 elsif params.has_key? 'disable_problem'
186 183 set_available(false)
187 184 end
188 185 redirect_to :action => 'manage'
189 186 end
190 187
191 188 def import
192 189 @allow_test_pair_import = allow_test_pair_import?
193 190 end
194 191
195 192 def do_import
196 193 old_problem = Problem.find_by_name(params[:name])
@@ -1,27 +1,29
1 1 CafeGrader::Application.routes.draw do
2 2 root :to => 'main#login'
3 3
4 4 get "report/login"
5 5
6 6 resources :contests
7 7
8 8 resources :announcements
9 9 match 'announcements/toggle/:id' => 'announcements#toggle'
10 10
11 11 resources :sites
12 12
13 + resources :problem
14 +
13 15 resources :grader_configuration, controller: 'configurations'
14 16
15 17 match 'tasks/view/:file.:ext' => 'tasks#view'
16 18 match 'tasks/download/:id/:file.:ext' => 'tasks#download'
17 19 match 'heartbeat/:id/edit' => 'heartbeat#edit'
18 20
19 21 #main
20 22 get "main/list"
21 23
22 24 # See how all your routes lay out with "rake routes"
23 25
24 26 # This is a legacy wild controller route that's not recommended for RESTful applications.
25 27 # Note: This route will make all actions in every controller accessible via GET requests.
26 28 match ':controller(/:action(/:id))(.:format)'
27 29 end
deleted file
You need to be logged in to leave comments. Login now