1
- # Copyright 2011-2012 Rice University. Licensed under the Affero General Public
1
+ # Copyright 2011-2012 Rice University. Licensed under the Affero General Public
2
2
# License version 3 or later. See the COPYRIGHT file for details.
3
3
4
4
class QuestionsController < ApplicationController
@@ -13,31 +13,31 @@ class QuestionsController < ApplicationController
13
13
before_filter :except => [ :index , :new , :get_started , :search ] do @use_columns = true end
14
14
15
15
# before_filter {@include_autocomplete=true}
16
-
16
+
17
17
autocomplete :tag , :name , :class_name => 'ActsAsTaggableOn::Tag'
18
-
18
+
19
19
def index
20
20
@where ||= 'Published Questions'
21
21
end
22
22
23
23
def show
24
24
@question = Question . from_param ( params [ :id ] )
25
25
@unfinished_solutions = !@question . solutions . where { ( is_visible == false ) & ( creator_id == my { present_user . id } ) } . first . nil?
26
- raise SecurityTransgression unless present_user . can_read? ( @question ) ||
26
+ raise SecurityTransgression unless present_user . can_read? ( @question ) ||
27
27
( ( request . remote_ip == '50.116.31.239' ) )
28
-
28
+
29
29
start_time = Time . now if logger . info?
30
-
30
+
31
31
@question . variate! ( QuestionVariator . new ( params [ :seed ] ) )
32
-
32
+
33
33
logger . info { "Variated question #{ @question . to_param } with seed " +
34
34
"#{ params [ :seed ] || '[unset]' } , duration = #{ Time . now -start_time } " }
35
-
35
+
36
36
respond_to do |format |
37
37
format . json
38
- format . html
39
- format . qti {
40
- render :template => case params [ :version ]
38
+ format . html
39
+ format . qti {
40
+ render :template => case params [ :version ]
41
41
when "1.2" , nil
42
42
"#{ view_dir ( @question ) } /show.1p2"
43
43
else
@@ -60,18 +60,18 @@ def history
60
60
61
61
def new
62
62
end
63
-
63
+
64
64
def get_started
65
65
end
66
-
66
+
67
67
def create_simple
68
68
create ( SimpleQuestion . new )
69
69
end
70
-
70
+
71
71
def create_multipart
72
72
create ( MultipartQuestion . new )
73
73
end
74
-
74
+
75
75
def edit
76
76
@question = Question . from_param ( params [ :id ] )
77
77
raise SecurityTransgression unless present_user . can_update? ( @question )
@@ -83,8 +83,8 @@ def edit
83
83
respond_with ( @question )
84
84
end
85
85
86
-
87
- # We can't use the normal respond_with here b/c the STI we're using confuses it.
86
+
87
+ # We can't use the normal respond_with here b/c the STI we're using confuses it.
88
88
# Rails tries to render simple_questions/edit when there are update errors, but
89
89
# we just want it to go to the questions view.
90
90
def update
@@ -100,7 +100,7 @@ def update
100
100
return
101
101
end
102
102
103
- respond_to do |format |
103
+ respond_to do |format |
104
104
if ( @updated = @question . update_attributes ( params [ :question ] ) )
105
105
flash [ :notice ] = "Your draft has been saved.
106
106
Until you publish this draft, please remember that only members of " +
@@ -144,46 +144,46 @@ def preview
144
144
raise ActiveRecord ::Rollback
145
145
end
146
146
end
147
-
147
+
148
148
def show_part
149
149
@multipart_question = Question . from_param ( params [ :question_id ] )
150
-
150
+
151
151
parts = @multipart_question . child_question_parts
152
152
raise SecurityTransgression unless parts . length >= params [ :part_id ] . to_i
153
-
153
+
154
154
@question = parts [ params [ :part_id ] . to_i -1 ] . child_question
155
-
155
+
156
156
raise SecurityTransgression unless present_user . can_read? ( @question )
157
-
158
-
157
+
158
+
159
159
respond_to do |format |
160
160
format . html { render 'questions/show' }
161
161
end
162
162
end
163
-
163
+
164
164
def tags
165
165
@tags = Question . tag_counts_on ( :tags )
166
166
end
167
-
167
+
168
168
def add_tags
169
169
@question = Question . from_param ( params [ :question_id ] )
170
170
raise SecurityTransgression unless present_user . can_tag? ( @question )
171
-
171
+
172
172
incoming_tags = params [ :tags ] . split ( "," ) . collect { |t | t . strip } . delete_if { |t | t . blank? }
173
-
173
+
174
174
if incoming_tags . empty?
175
175
elsif tags_fail_regex? ( incoming_tags )
176
176
flash [ :alert ] = "Tags can only contain letters, numbers, spaces, hyphens, and apostrophes. Multiple tags should be separated by commas."
177
177
else
178
178
tags = @question . tag_list . concat ( incoming_tags ) . join ( ", " )
179
- @question . update_attribute ( :tag_list , tags )
180
- end
181
-
179
+ @question . update_attribute ( :tag_list , tags )
180
+ end
181
+
182
182
respond_to do |format |
183
183
format . js { render :template => 'questions/tags_action' }
184
- end
184
+ end
185
185
end
186
-
186
+
187
187
def remove_tag
188
188
@question = Question . from_param ( params [ :question_id ] )
189
189
raise SecurityTransgression unless present_user . can_tag? ( @question )
@@ -194,23 +194,23 @@ def remove_tag
194
194
updated_tag_list = @question . tag_list . reject { |t | target_tag == t }
195
195
@question . update_attribute ( :tag_list , updated_tag_list )
196
196
end
197
-
197
+
198
198
respond_to do |format |
199
199
@in_remove_state = true
200
200
format . js { render :template => 'questions/tags_action' }
201
201
end
202
202
end
203
-
203
+
204
204
def tagged
205
205
@tags = params [ :tags ] . gsub ( "_" , " " ) . split ( "+" )
206
206
@per_page = params [ :per_page ] || 20
207
-
207
+
208
208
if tags_fail_regex? ( @tags )
209
209
@questions = [ ]
210
210
flash [ :alert ] = "The provided tags contain invalid characters."
211
211
else
212
212
@questions = Question . tagged_with ( @tags )
213
- . reject { |q | ( q . is_published? && !q . is_latest? ) ||
213
+ . reject { |q | ( q . is_published? && !q . is_latest? ) ||
214
214
!present_user . can_read? ( q ) }
215
215
. paginate ( :page => params [ :page ] , :per_page => @per_page )
216
216
end
@@ -231,17 +231,17 @@ def preview_publish
231
231
run_prepublish_error_checks ( @questions )
232
232
respond_with ( @questions )
233
233
end
234
-
234
+
235
235
def publish
236
236
@questions = Question . find ( params [ :question_ids ] )
237
-
237
+
238
238
@questions . each { |q |
239
239
raise SecurityTransgression unless q . can_be_published_by? ( present_user )
240
240
}
241
241
242
242
run_prepublish_error_checks ( @questions , false )
243
243
error_message = combine_base_error_messages ( @questions )
244
-
244
+
245
245
if !error_message . blank?
246
246
respond_to do |format |
247
247
format . html {
@@ -251,38 +251,38 @@ def publish
251
251
format . any ( :json , :xml ) { raise NotYetImplemented }
252
252
end
253
253
end
254
-
254
+
255
255
if params [ :agreement_checkbox ]
256
256
@questions . each { |q |
257
257
q . publish! ( present_user )
258
258
}
259
-
259
+
260
260
respond_with ( @questions ) do |format |
261
- format . html {
262
- redirect_to @questions . size == 1 ?
261
+ format . html {
262
+ redirect_to @questions . size == 1 ?
263
263
question_path ( @questions . first ) :
264
264
questions_path
265
265
}
266
266
end
267
267
else
268
- flash [ :alert ] = "You must accept the license agreement in order to publish " +
268
+ flash [ :alert ] = "You must accept the license agreement in order to publish " +
269
269
( ( @questions . size == 1 ) ? "this question." : "these questions." )
270
270
respond_with ( @questions ) do |format |
271
271
format . html { redirect_to :back }
272
272
end
273
273
end
274
274
end
275
-
275
+
276
276
def source
277
277
@question = Question . from_param ( params [ :question_id ] )
278
278
raise SecurityTransgression unless @question . can_be_read_by? ( present_user )
279
279
respond_with ( @question )
280
280
end
281
-
281
+
282
282
def new_version
283
283
@source_question = Question . from_param ( params [ :question_id ] )
284
284
raise SecurityTransgression unless @source_question . can_be_new_versioned_by? ( present_user )
285
-
285
+
286
286
begin
287
287
@question = @source_question . new_version! ( present_user )
288
288
respond_to do |format |
@@ -293,7 +293,7 @@ def new_version
293
293
flash [ :alert ] = "We could not create a derived question as requested."
294
294
respond_to do |format |
295
295
format . html { redirect_to question_path ( @source_question ) }
296
- end
296
+ end
297
297
end
298
298
end
299
299
@@ -306,7 +306,7 @@ def derivation_dialog
306
306
307
307
respond_to do |format |
308
308
format . js
309
- end
309
+ end
310
310
end
311
311
312
312
def new_derivation
@@ -315,7 +315,7 @@ def new_derivation
315
315
edit_now = params [ :edit ] == "now"
316
316
raise SecurityTransgression unless ( @source_question . can_be_derived_by? ( present_user ) &&
317
317
( !list . nil? && list . can_be_updated_by? ( present_user ) ) )
318
-
318
+
319
319
begin
320
320
@question = @source_question . new_derivation! ( present_user , list )
321
321
flash [ :notice ] = "Derived question created.
@@ -343,23 +343,23 @@ def new_derivation
343
343
false
344
344
end
345
345
346
- # Originally, the license was set with the other attributes on the
347
- # new/edit/update pages. However, we decided to break that out into a
348
- # separate page b/c it wasn't super critical (and because we are going to
346
+ # Originally, the license was set with the other attributes on the
347
+ # new/edit/update pages. However, we decided to break that out into a
348
+ # separate page b/c it wasn't super critical (and because we are going to
349
349
# start questions with a default license.) These methods support those pages.
350
-
350
+
351
351
def edit_license
352
352
@question = Question . from_param ( params [ :question_id ] )
353
353
raise SecurityTransgression unless present_user . can_update? ( @question )
354
354
respond_with ( @question )
355
355
end
356
-
356
+
357
357
def update_license
358
358
@question = Question . from_param ( params [ :question_id ] )
359
359
raise SecurityTransgression unless present_user . can_update? ( @question )
360
360
361
361
@question . license = License . find ( params [ :question ] [ :license_id ] )
362
-
362
+
363
363
respond_to do |format |
364
364
if @question . save
365
365
format . html { redirect_to question_path ( @question ) }
@@ -370,6 +370,9 @@ def update_license
370
370
end
371
371
372
372
def search
373
+ render text : 'Question search has been temporarily disabled.'
374
+ return
375
+
373
376
@type = params [ :type ]
374
377
@location = params [ :location ]
375
378
@part = params [ :part ]
@@ -403,34 +406,34 @@ def evaluate
403
406
end
404
407
end
405
408
end
406
-
409
+
407
410
protected
408
411
409
- def create ( question )
412
+ def create ( question )
410
413
@question = question
411
414
raise SecurityTransgression unless present_user . can_create? ( @question )
412
-
415
+
413
416
begin
414
417
@question . create! ( current_user )
415
-
418
+
416
419
respond_to do |format |
417
420
format . html { redirect_to edit_question_path ( @question ) }
418
421
end
419
422
rescue ActiveRecord ::RecordInvalid => invalid
420
423
logger . error { "An error occurred when creating a question: #{ invalid . message } " }
421
-
424
+
422
425
respond_to do |format |
423
- format . html {
426
+ format . html {
424
427
begin
425
428
redirect_to :back
426
429
rescue ActionController ::RedirectBackError
427
- redirect_to root_path
430
+ redirect_to root_path
428
431
end
429
432
}
430
- end
433
+ end
431
434
end
432
435
end
433
-
436
+
434
437
def tags_fail_regex? ( tags )
435
438
tags = [ tags ] . flatten
436
439
tags . any? { |tag | ( tag =~ /^[A-Za-z\d \- ' ]+$/ ) . nil? }
0 commit comments