Class: DeliverablesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/deliverables_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#robot?

Instance Method Details

#createObject

POST /deliverables POST /deliverables.xml


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/controllers/deliverables_controller.rb', line 171

def create
  # Make sure that a file was specified
  @deliverable = Deliverable.new(params[:deliverable])
  @deliverable.creator = current_user
  @deliverable.is_team_deliverable ? @deliverable.update_team : @deliverable.team = nil

  if @deliverable.is_team_deliverable && @deliverable.team == nil
    flash[:error] = "You are not on a team in this course, so you can't submit a team deliverable"
    respond_to do |format|
      format.html { render :action => "new" }
      format.xml { render :xml => @deliverable.errors, :status => :unprocessable_entity }
    end
    return
  end

  if !params[:deliverable_attachment][:attachment]
    flash[:error] = 'Must specify a file to upload'
    respond_to do |format|
      format.html { render :action => "new" }
      format.xml { render :xml => @deliverable.errors, :status => :unprocessable_entity }
    end
    return
  end
  @attachment = DeliverableAttachment.new(params[:deliverable_attachment])
  @attachment.submitter = @deliverable.creator
  @deliverable.attachment_versions << @attachment
  @attachment.deliverable = @deliverable

  respond_to do |format|
    if @attachment.valid? and @deliverable.valid? and @deliverable.save
      @deliverable.send_deliverable_upload_email(url_for(@deliverable))
      flash[:notice] = 'Deliverable was successfully created.'
      format.html { redirect_to(@deliverable) }
      format.xml { render :xml => @deliverable, :status => :created, :location => @deliverable }
    else
      if not @attachment.valid?
        flash[:notice] = 'Attachment not valid'
      elsif not @deliverable.valid?
        flash[:notice] = 'Deliverable not valid'
      else
        flash[:notice] = 'Something else went wrong'
      end
      format.html { render :action => "new" }
      format.xml { render :xml => @deliverable.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /deliverables/1 DELETE /deliverables/1.xml


261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/controllers/deliverables_controller.rb', line 261

def destroy
  @deliverable = Deliverable.find(params[:id])

  unless @deliverable.editable?(current_user)
    flash[:error] = I18n.t(:not_your_deliverable)
    redirect_to root_path and return
  end
  @deliverable.destroy

  respond_to do |format|
    format.html { redirect_to(deliverables_url) }
    format.xml { head :ok }
  end
end

#editObject

GET /deliverables/1/edit


157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/deliverables_controller.rb', line 157

def edit
  @deliverable = Deliverable.find(params[:id])

  @courses = current_user.registered_for_these_courses_during_current_semester
  @assignments = @deliverable.course.assignments.where(:is_submittable => true)

  unless @deliverable.editable?(current_user)
    flash[:error] = I18n.t(:not_your_deliverable)
    redirect_to root_path and return
  end
end

#edit_feedbackObject


276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'app/controllers/deliverables_controller.rb', line 276

def edit_feedback
  if @course.grading_rule.nil?
    flash[:error] = I18n.t(:no_grading_rule_for_course)
    redirect_to course_path(@course) and return
  end

  @deliverable = Deliverable.find(params[:id])

#    if [email protected]?(current_user)
#      flash[:error] = "Only faculty teaching this course can provide feedback on deliverables. #{current_user.human_name}"
#      redirect_to :controller => "welcome", :action => "index"
#      return
#    end
end

#get_assignments_for_studentObject

Todo: rename this method get_assginemments_for_course shouldn't this be in the assignments controller?


347
348
349
350
351
352
353
354
355
# File 'app/controllers/deliverables_controller.rb', line 347

def get_assignments_for_student
  unless params[:course_id].nil?
    @assignments = Course.find(params[:course_id]).assignments.all(:conditions => ["is_submittable = ?", true])
    @assignments_array = @assignments.collect { |assignment| {:assignment => assignment.attributes.merge({:name_with_type => assignment.name_with_type})} }
    respond_to do |format|
      format.json { render json: @assignments_array }
    end
  end
end

#get_deliverablesObject


50
51
52
53
54
55
56
# File 'app/controllers/deliverables_controller.rb', line 50

def get_deliverables
  filter_options = params[:filter_options] || Hash.new
  @deliverables = filter_deliverables(params[:course_id], filter_options)
  respond_to do |format|
    format.js
  end
end

#grading_queue_for_courseObject


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
# File 'app/controllers/deliverables_controller.rb', line 18

def grading_queue_for_course

  @course = Course.find(params[:course_id])

  if @course.grading_rule.nil?
    flash[:error] = I18n.t(:no_grading_rule_for_course)
    redirect_to course_path(@course) and return
  end
  if @course.grading_rule.default_values?
    flash.now[:error] = I18n.t(:default_grading_rule_for_course)
  end

  # Retrieving assignments names for the course to be able to filter by deliverable name later on.
  @assignments = Assignment.where(:course_id => @course.id).all
  # Team Turing: this fails when the task is nil.
  # @assignments = Assignment.where(:course_id => @course.id).all.sort_by(&:task_number)


  if @course.faculty.include?(current_user)
    # Get all deliverables for this team/student
    @deliverables = Deliverable.get_deliverables(params[:course_id], current_user.id, {:is_my_team => 1})

    @deliverables = @deliverables.select { |deliverable| deliverable.grade_status == "ungraded" ||
        deliverable.grade_status == "drafted" }

    @deliverables = @deliverables.sort { |a, b| b.assignment.assignment_order <=> a.assignment.assignment_order }
  else
    has_permissions_or_redirect(:admin, root_path)
  end

end

#indexObject

GET /deliverables GET /deliverables.xml


14
15
16
# File 'app/controllers/deliverables_controller.rb', line 14

def index
  redirect_to my_deliverables_path(current_user)
end

#individual_index_for_courseObject

temporary for mel


69
70
71
72
73
74
75
76
# File 'app/controllers/deliverables_controller.rb', line 69

def individual_index_for_course
  @course = Course.find(params[:course_id])
  if (current_user.is_admin? || @course.faculty.include?(current_user))
    @deliverables = Deliverable.where("team_id is null").find_all_by_course_id(@course.id)
  else
    has_permissions_or_redirect(:admin, root_path)
  end
end

#my_deliverablesObject


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/deliverables_controller.rb', line 78

def my_deliverables
  user = User.find_by_param(params[:id])
  if (current_user.id != user.id)
    unless (current_user.is_staff?)||(current_user.is_admin?)
      flash[:error] = I18n.t(:not_your_deliverable)
      redirect_to root_path
      return
    end
  end
  @current_deliverables = Deliverable.find_current_by_user(user)
  @past_deliverables = Deliverable.find_past_by_user(user)
  @current_assignments = Assignment.list_assignments_for_student(user.id, :current)
  @current_courses = user.registered_for_these_courses_during_current_semester()
  @past_assignments = Assignment.list_assignments_for_student(user.id, :past)
  @past_courses = user.registered_for_these_courses_during_past_semesters()
  respond_to do |format|
    format.html { render :action => "index" }
    format.xml { render :xml => @deliverables }
  end
end

#newObject

GET /deliverables/new GET /deliverables/new.xml


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/deliverables_controller.rb', line 133

def new
  # If we aren't on this deliverable's team, you can't see it.
  @deliverable = Deliverable.new(:creator => current_user)
  @courses = current_user.registered_for_these_courses_during_current_semester

  if params[:course_id]
    @deliverable.course_id = params[:course_id]
    @assignments = Course.find(params[:course_id]).assignments.where(:is_submittable => true)
  else
    if @courses.empty?
      @assignments = []
    else
      @assignments = @courses[0].assignments.where(:is_submittable => true)
    end
  end


  respond_to do |format|
    format.html # new.html.erb
    format.xml { render :xml => @deliverable }
  end
end

#render_grade_book_menuObject


8
9
10
# File 'app/controllers/deliverables_controller.rb', line 8

def render_grade_book_menu
  @is_in_grade_book = true if (current_user.is_staff?)||(current_user.is_admin?)
end

#showObject

GET /deliverables/1 GET /deliverables/1.xml


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
# File 'app/controllers/deliverables_controller.rb', line 101

def show
  @deliverable = Deliverable.find(params[:id])
  @course = @deliverable.course
  @hostname_with_port = request.host_with_port
  unless @deliverable.editable?(current_user)
    flash[:error] = I18n.t(:not_your_deliverable)
    redirect_to root_path and return
  end

  if (current_user.is_admin? || @course.faculty.include?(current_user))
    if @course.grading_rule.nil?
      flash[:error] = I18n.t(:no_grading_rule_for_course)
      redirect_to course_path(@course) and return
    end
    if @course.grading_rule.default_values?
      flash.now[:error] = I18n.t(:default_grading_rule_for_course)
    end
  end

  respond_to do |format|
    if (current_user.is_admin? || @course.faculty.include?(current_user))
      format.html { render layout: false }
    else
      format.html # show.html.erb
      format.xml { render :xml => @deliverable }
    end

  end
end

#team_index_for_courseObject

temporary for mel


59
60
61
62
63
64
65
66
# File 'app/controllers/deliverables_controller.rb', line 59

def team_index_for_course
  @course = Course.find(params[:course_id])
  if (current_user.is_admin? || @course.faculty.include?(current_user))
    @deliverables = Deliverable.where("team_id is not null").find_all_by_course_id(@course.id)
  else
    has_permissions_or_redirect(:admin, root_path)
  end
end

#updateObject

PUT /deliverables/1 PUT /deliverables/1.xml


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/controllers/deliverables_controller.rb', line 221

def update
  @deliverable = Deliverable.find(params[:id])

#    params[:is_team_deliverable] ? @deliverable.update_team : @deliverable.team = nil
  @deliverable.is_team_deliverable ? @deliverable.update_team : @deliverable.team = nil

  unless @deliverable.editable?(current_user)
    flash[:error] = I18n.t(:not_your_deliverable)
    redirect_to root_path and return
  end

  there_is_an_attachment = params[:deliverable_attachment][:attachment]
  if there_is_an_attachment

    @attachment = DeliverableAttachment.new(params[:deliverable_attachment])
    @attachment.submitter = current_user
    @deliverable.attachment_versions << @attachment
    @attachment.deliverable = @deliverable

    if @attachment.valid? and @deliverable.valid? and @deliverable.update_attributes(params[:deliverable])
      @deliverable.send_deliverable_upload_email(url_for(@deliverable))
      flash[:notice] = 'Deliverable was successfully updated.'
      redirect_to(@deliverable)
    else
      render :action => "edit"
    end
  else
    if @deliverable.valid? and @deliverable.update_attributes(params[:deliverable])
      flash[:notice] = 'Deliverable was successfully updated.'
      redirect_to(@deliverable)
    else
      render :action => "edit"
    end
  end


end

#update_feedbackObject


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'app/controllers/deliverables_controller.rb', line 292

def update_feedback
  @deliverable = Deliverable.find(params[:id])

  unless (current_user.is_admin? || @deliverable.course.faculty.include?(current_user))
    flash[:error] = I18n.t(:not_your_deliverable)
    redirect_to root_path and return
  end

  # check is save and email is clicked or save as draft is clicked
  is_student_visible=true
  if params[:submit]
    is_student_visible=true
    @deliverable.update_attributes(:grade_status => "graded")
  elsif params[:draft]
    is_student_visible=false
    @deliverable.update_attributes(:grade_status => "drafted")
  end


  flash[:error] = @deliverable.update_grade(params, is_student_visible, current_user.id)
  other_email = nil
  if params[:send_copy_to_myself] == "1"
    other_email = current_user.email
  end
  if @deliverable.update_feedback_and_notes(params[:deliverable])
    if is_student_visible == true
      @deliverable.send_deliverable_feedback_email(url_for(@deliverable), other_email)
    end
  else
    flash[:error] << 'Unable to save feedback'
  end

  #Obtain current selected filters to update the queue accordingly
  if params[:deliverable][:current_filter_options].present?
    @selected_filter_options = JSON.parse(params[:deliverable][:current_filter_options])
  else
    @selected_filter_options = Hash.new
  end
  @deliverables = filter_deliverables(@deliverable.course_id, @selected_filter_options)

  respond_to do |format|
    if flash[:error].blank?
      flash[:error] = nil
      flash[:notice] = 'Feedback successfully saved.'
      format.js
    else
      flash[:error] = flash[:error].join("<br>")
      format.html { redirect_to(@deliverable) }
    end
  end
end