Description:
fix problem edit page
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r590:0ef720704122 - - 2 files changed: 4 inserted, 4 deleted
@@ -1,177 +1,177 | |||
|
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 | 10 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
11 | 11 | end |
|
12 | 12 | |
|
13 | 13 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
14 | 14 | verify :method => :post, :only => [ :destroy, |
|
15 | 15 | :create, :quick_create, |
|
16 | 16 | :do_manage, |
|
17 | 17 | :do_import, |
|
18 |
- |
|
|
18 | + ], | |
|
19 | 19 | :redirect_to => { :action => :index } |
|
20 | 20 | |
|
21 | 21 | def show |
|
22 | 22 | @problem = Problem.find(params[:id]) |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | def new |
|
26 | 26 | @problem = Problem.new |
|
27 | 27 | @description = nil |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | 30 | def create |
|
31 | 31 | @problem = Problem.new(params[:problem]) |
|
32 | 32 | @description = Description.new(params[:description]) |
|
33 | 33 | if @description.body!='' |
|
34 | 34 | if !@description.save |
|
35 | 35 | render :action => new and return |
|
36 | 36 | end |
|
37 | 37 | else |
|
38 | 38 | @description = nil |
|
39 | 39 | end |
|
40 | 40 | @problem.description = @description |
|
41 | 41 | if @problem.save |
|
42 | 42 | flash[:notice] = 'Problem was successfully created.' |
|
43 | 43 | redirect_to action: :index |
|
44 | 44 | else |
|
45 | 45 | render :action => 'new' |
|
46 | 46 | end |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def quick_create |
|
50 | 50 | @problem = Problem.new(params[:problem]) |
|
51 | 51 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
52 | 52 | @problem.full_score = 100 |
|
53 | 53 | @problem.available = false |
|
54 | 54 | @problem.test_allowed = true |
|
55 | 55 | @problem.output_only = false |
|
56 | 56 | @problem.date_added = Time.new |
|
57 | 57 | if @problem.save |
|
58 | 58 | flash[:notice] = 'Problem was successfully created.' |
|
59 | 59 | redirect_to action: :index |
|
60 | 60 | else |
|
61 | 61 | flash[:notice] = 'Error saving problem' |
|
62 | 62 | redirect_to action: :index |
|
63 | 63 | end |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def edit |
|
67 | 67 | @problem = Problem.find(params[:id]) |
|
68 | 68 | @description = @problem.description |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def update |
|
72 | 72 | @problem = Problem.find(params[:id]) |
|
73 | 73 | @description = @problem.description |
|
74 |
- if @description |
|
|
74 | + if @description.nil? and params[:description][:body]!='' | |
|
75 | 75 | @description = Description.new(params[:description]) |
|
76 | 76 | if !@description.save |
|
77 | 77 | flash[:notice] = 'Error saving description' |
|
78 | 78 | render :action => 'edit' and return |
|
79 | 79 | end |
|
80 | 80 | @problem.description = @description |
|
81 |
- elsif @description |
|
|
81 | + elsif @description | |
|
82 | 82 | if !@description.update_attributes(params[:description]) |
|
83 | 83 | flash[:notice] = 'Error saving description' |
|
84 | 84 | render :action => 'edit' and return |
|
85 | 85 | end |
|
86 | 86 | end |
|
87 | 87 | if params[:file] and params[:file].content_type != 'application/pdf' |
|
88 | 88 | flash[:notice] = 'Error: Uploaded file is not PDF' |
|
89 | 89 | render :action => 'edit' and return |
|
90 | 90 | end |
|
91 | 91 | if @problem.update_attributes(params[:problem]) |
|
92 | 92 | flash[:notice] = 'Problem was successfully updated.' |
|
93 | 93 | unless params[:file] == nil or params[:file] == '' |
|
94 | 94 | flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' |
|
95 | 95 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
96 | 96 | if not FileTest.exists? out_dirname |
|
97 | 97 | Dir.mkdir out_dirname |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
101 | 101 | if FileTest.exists? out_filename |
|
102 | 102 | File.delete out_filename |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | File.open(out_filename,"wb") do |file| |
|
106 | 106 | file.write(params[:file].read) |
|
107 | 107 | end |
|
108 | 108 | @problem.description_filename = "#{@problem.name}.pdf" |
|
109 | 109 | @problem.save |
|
110 | 110 | end |
|
111 | 111 | redirect_to :action => 'show', :id => @problem |
|
112 | 112 | else |
|
113 | 113 | render :action => 'edit' |
|
114 | 114 | end |
|
115 | 115 | end |
|
116 | 116 | |
|
117 | 117 | def destroy |
|
118 | 118 | Problem.find(params[:id]).destroy |
|
119 | 119 | redirect_to action: :index |
|
120 | 120 | end |
|
121 | 121 | |
|
122 | 122 | def toggle |
|
123 | 123 | @problem = Problem.find(params[:id]) |
|
124 | 124 | @problem.update_attributes(available: !(@problem.available) ) |
|
125 | 125 | respond_to do |format| |
|
126 | 126 | format.js { } |
|
127 | 127 | end |
|
128 | 128 | end |
|
129 | 129 | |
|
130 | 130 | def toggle_test |
|
131 | 131 | @problem = Problem.find(params[:id]) |
|
132 | 132 | @problem.update_attributes(test_allowed: !(@problem.test_allowed?) ) |
|
133 | 133 | respond_to do |format| |
|
134 | 134 | format.js { } |
|
135 | 135 | end |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def turn_all_off |
|
139 | 139 | Problem.find(:all, |
|
140 | 140 | :conditions => "available = 1").each do |problem| |
|
141 | 141 | problem.available = false |
|
142 | 142 | problem.save |
|
143 | 143 | end |
|
144 | 144 | redirect_to action: :index |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | def turn_all_on |
|
148 | 148 | Problem.find(:all, |
|
149 | 149 | :conditions => "available = 0").each do |problem| |
|
150 | 150 | problem.available = true |
|
151 | 151 | problem.save |
|
152 | 152 | end |
|
153 | 153 | redirect_to action: :index |
|
154 | 154 | end |
|
155 | 155 | |
|
156 | 156 | def stat |
|
157 | 157 | @problem = Problem.find(params[:id]) |
|
158 | 158 | unless @problem.available or session[:admin] |
|
159 | 159 | redirect_to :controller => 'main', :action => 'list' |
|
160 | 160 | return |
|
161 | 161 | end |
|
162 | 162 | @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id) |
|
163 | 163 | |
|
164 | 164 | #stat summary |
|
165 | 165 | range =65 |
|
166 | 166 | @histogram = { data: Array.new(range,0), summary: {} } |
|
167 | 167 | user = Hash.new(0) |
|
168 | 168 | @submissions.find_each do |sub| |
|
169 | 169 | d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60 |
|
170 | 170 | @histogram[:data][d.to_i] += 1 if d < range |
|
171 | 171 | user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max |
|
172 | 172 | end |
|
173 | 173 | @histogram[:summary][:max] = [@histogram[:data].max,1].max |
|
174 | 174 | |
|
175 | 175 | @summary = { attempt: user.count, solve: 0 } |
|
176 | 176 | user.each_value { |v| @summary[:solve] += 1 if v == 1 } |
|
177 | 177 | end |
@@ -1,9 +1,9 | |||
|
1 | 1 | <h1>Editing problem</h1> |
|
2 | 2 | |
|
3 |
- <%= form_ |
|
|
3 | + <%= form_for @problem,url:{action: 'update'},html: {multipart: true} do %> | |
|
4 | 4 | <%= render :partial => 'form' %> |
|
5 | 5 | <%= submit_tag 'Edit' %> |
|
6 | 6 | <% end %> |
|
7 | 7 | |
|
8 | 8 | <%= link_to 'Show', :action => 'show', :id => @problem %> | |
|
9 | 9 | <%= link_to 'Back', :action => 'list' %> |
You need to be logged in to leave comments.
Login now