Class: EffortLogsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#robot?

Instance Method Details

#createObject

POST /effort_logs POST /effort_logs.xml


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/controllers/effort_logs_controller.rb', line 249

def create
  @effort_log = EffortLog.new(params[:effort_log])

  setup_required_datastructures(@effort_log.year, @effort_log.week_number)


  respond_to do |format|
    if @effort_log.save
      course_error_msg = @effort_log.validate_effort_against_registered_courses()
      flash[:notice] = 'EffortLog was successfully created.'
      if (course_error_msg!="")
        flash[:error] = 'You are not on a team in the following course(s) ' + course_error_msg
      end
      format.html { redirect_to(@effort_log) }
      format.xml { render :xml => @effort_log, :status => :created, :location => @effort_log }
    else
      format.html { render :action => "new" }
      format.xml { render :xml => @effort_log.errors, :status => :unprocessable_entity }
    end
  end
end

#create_endweek_faculty_emailObject


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/effort_logs_controller.rb', line 45

def create_endweek_faculty_email
  notify_course_list = Course.remind_about_effort_course_list

  #notify_course_list = [48, 47, 46]  #list all courses that we want to track effort
  last_week = (Date.today - 7).cweek
  last_week_year = (Date.today -7).cwyear

  if (!EffortLog.log_effort_week?(last_week_year, last_week))
    logger.info "There was no class last week, so we won't remind students to log effort"
    return
  end

  notify_course_list.each do |course|
    faculty = {}
    teams = Team.where(:course_id => course.id)
    teams.each do |team|
      faculty[team.primary_faculty_id] = 1 unless team.primary_faculty_id.nil?
      faculty[team.secondary_faculty_id] = 1 unless team.secondary_faculty_id.nil?
    end
    faculty_emails = []
    faculty.each { |faculty_id, value| faculty_emails << User.find_by_id(faculty_id).email }
    EffortLogMailer.endweek_admin_report(course.id, course.name, faculty_emails).deliver
  end
end

#create_midweek_warning_emailObject

Todo: consider moving these email methods to the model(EffortLogs) and update the rake task accordingly


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/effort_logs_controller.rb', line 12

def create_midweek_warning_email
  if (!EffortLog.log_effort_week?(Date.today.cwyear, Date.today.cweek))
    #We skip weeks that students aren't taking courses
    logger.info "There is no class this week, so we won't remind students to log effort"
    return
  end

  random_scotty_saying = ScottyDogSaying.all.sample.saying

  courses = Course.remind_about_effort_course_list
  courses.each do |course_id|
    create_midweek_warning_email_for_course(random_scotty_saying, course_id)
  end

end

#create_midweek_warning_email_for_course(random_scotty_saying, course_id) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/effort_logs_controller.rb', line 28

def create_midweek_warning_email_for_course(random_scotty_saying, course_id)
  year = Date.today.cwyear
  week_number = Date.today.cweek

  course = Course.find(course_id)
  users = course.registered_students | course.teams.collect { |team| team.members }.flatten
  users.each do |user|
    logger.debug "**    user #{user.human_name}"
    effort_log = EffortLog.where(:user_id => user.id, :week_number => week_number, :year => year).first
    if (!user.emailed_recently(:effort_log))
      EffortLogMailer.midweek_warning(random_scotty_saying, user).deliver
      user.effort_log_warning_email = Time.now
      user.save
    end
  end
end

#destroyObject

DELETE /effort_logs/1 DELETE /effort_logs/1.xml


307
308
309
310
311
312
313
314
315
# File 'app/controllers/effort_logs_controller.rb', line 307

def destroy
  @effort_log = EffortLog.find(params[:id])
  @effort_log.destroy

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

#editObject

GET /effort_logs/1/edit


232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/effort_logs_controller.rb', line 232

def edit
  @effort_log = EffortLog.find(params[:id])

#    if @effort_log.week_number != Date.today.cweek
  if !@effort_log.editable_by(current_user)
    flash[:error] = 'You do not have permission to edit the effort log.'
    redirect_to(effort_logs_url) and return
  end
#    if [email protected]_log.has_permission_to_edit_period(current_user)
#      flash[:error] = 'You are unable to update effort logs from the past.'
#      redirect_to(effort_logs_url) and return
#    end
  setup_required_datastructures(@effort_log.year, @effort_log.week_number)
end

#effort_for_unregistered_coursesObject


317
318
319
320
321
322
323
# File 'app/controllers/effort_logs_controller.rb', line 317

def effort_for_unregistered_courses
  @error_effort_logs_users = EffortLog.users_with_effort_against_unregistered_courses()
  respond_to do |format|
    format.html
    format.xml { render :xml => @error_effort_logs_users }
  end
end

#indexObject

GET /effort_logs GET /effort_logs.xml


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

def index
  @effort_logs = EffortLog.find_effort_logs(current_user)

  if Date.today.cweek == 1 #If the first week of the year, then we set to the last week of previous year
    @prior_week_number = 52
    @year = Date.today.cwyear - 1
  else
    @prior_week_number = Date.today.cweek - 1
    @year = Date.today.cwyear
  end

  # if no effort logs created
  if @effort_logs.empty?
    # show new current link
    @show_new_link = true

    # if no effort logs created and today is Monday, then show new prior link
    if Date.today.cwday == 1
      @show_prior_week = true
    else
      @show_prior_week = false
    end
  else
    # if most recent effort log is for current period, then don't show new current link, else show new current link
    if @effort_logs[0].year == Date.today.cwyear && @effort_logs[0].week_number == Date.today.cweek
      @show_new_link = false
    else
      @show_new_link = true
    end

    # start by always showing prior week link
    @show_prior_week = true

    #if today is Monday, evaluate, otherwise don't show prior week link
    if Date.today == Date.commercial(Date.today.cwyear, Date.today.cweek, 1)
      # if most recent log is a match, then don't show prior week link
      if (@effort_logs[0].year == @year && @effort_logs[0].week_number == @prior_week_number)
        @show_prior_week = false
      end
      # if second most recent entry is a match, then don't show prior week link
      if @effort_logs[1]
        if (@effort_logs[1].year == @year && @effort_logs[1].week_number == @prior_week_number)
          @show_prior_week = false
        end
      end
    else
      # if not Monday, don't ever show the prior week link
      @show_prior_week = false
    end
  end

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

#newObject

GET /effort_logs/new GET /effort_logs/new.xml


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/controllers/effort_logs_controller.rb', line 153

def new
  if params[:prior] == 'true' then
    if Date.today.cweek == 1
      week_number = 52
      year = Date.today.cwyear -1
    else
      week_number = Date.today.cweek - 1
      year = Date.today.cwyear
    end
    error_msg = "There already is an effort log for the previous week"
  else
    week_number = Date.today.cweek
    year = Date.today.cwyear
    error_msg = "There already is an effort log for the current week"
  end
  setup_required_datastructures(year, week_number)

  @effort_log = EffortLog.new
  @effort_log.user_id = current_user.id

  #find the most recent effort log to copy its structure, but not its effort data
  recent_effort_log = EffortLog.where(:user_id => current_user.id).order("year DESC, week_number DESC").first

  # We want to make sure that the user isn't accidentally creating two efforts for the same week.
  # Since students are only able to log effort for this week (or a previous week)
  if recent_effort_log and recent_effort_log.week_number == week_number
    #Yes we already have effort for this week
    duplicate_effort_log = recent_effort_log
  else
    #Do we already have effort for the week we are trying to log effort against?
    duplicate_effort_log = EffortLog.where(:user_id => current_user.id, :week_number => week_number, :year => year).first
  end

  if duplicate_effort_log
    logger.debug "We should not be creating another effort log for week #{week_number}"
    flash[:error] = error_msg
    redirect_to(effort_logs_url) and return
  end

  if recent_effort_log
    logger.debug "Copy effort log from week #{recent_effort_log.week_number}"
    recent_effort_log.effort_log_line_items.each do |line|
      @effort_log.effort_log_line_items << EffortLogLineItem.new(:course_id => line.course_id, :task_type_id => line.task_type_id)
    end
  else
    logger.debug "This is the first effort log for the person in the system"

    course = recent_foundations_or_course
    course_id = course.id

    @task_types.each do |task_type|
      @effort_log.effort_log_line_items << EffortLogLineItem.new(:course_id => course_id, :task_type_id => task_type.id)
    end
  end

  # Ps. if we wanted to have an effort log with a single effort log line item that is blank, then this would do it       @effort_log.effort_log_line_items.build      


  @effort_log.year = year
  @effort_log.week_number = week_number

#          format.html # new.html.erb
#      format.xml  { render :xml => @effort_log }


  respond_to do |format|
    if @effort_log.save
      flash[:notice] = 'EffortLog was successfully created.'
      format.html { redirect_to(edit_effort_log_url(@effort_log.id)) }
      format.xml { render :xml => @effort_log, :status => :created, :location => @effort_log }
    else
      flash[:notice] = 'Unable to create new EffortLog.'
      format.html { redirect_to(effort_logs_url) }
      format.xml { render :xml => @effort_log.errors, :status => :unprocessable_entity }
    end
  end
end

#new_effort_log_line_itemObject

Typically an ajax call


326
327
328
329
330
331
# File 'app/controllers/effort_logs_controller.rb', line 326

def new_effort_log_line_item
  setup_required_datastructures(Date.today.cwyear, Date.today.cweek)
  @effort_log_line_item = EffortLogLineItem.new
  @effort_log_line_item.effort_log_id = params[:effort_log_id]
  @effort_log_line_item.save
end

#showObject

GET /effort_logs/1 GET /effort_logs/1.xml


140
141
142
143
144
145
146
147
148
# File 'app/controllers/effort_logs_controller.rb', line 140

def show
  @effort_log = EffortLog.find(params[:id])
  setup_required_datastructures(@effort_log.year, @effort_log.week_number)

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

#updateObject

PUT /effort_logs/1 PUT /effort_logs/1.xml


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'app/controllers/effort_logs_controller.rb', line 273

def update
  params[:effort_log][:existing_effort_log_line_item_attributes] ||= {}

  @effort_log = EffortLog.find(params[:id])
  if !@effort_log.editable_by(current_user)
    flash[:error] = 'You do not have permission to edit the effort log.'
    redirect_to(effort_logs_url) and return
  end
#    if [email protected]_log.has_permission_to_edit_period(current_user)
#      flash[:error] = 'You are unable to update effort logs from the past.'
#      redirect_to(effort_logs_url) and return
#    end

  setup_required_datastructures(@effort_log.year, @effort_log.week_number)

  respond_to do |format|
    if @effort_log.update_attributes(params[:effort_log])
      #check to see if user is logging effort for unregistered courses
      course_error_msg = @effort_log.validate_effort_against_registered_courses()
      flash[:notice] = 'EffortLog was successfully updated.'
      if (course_error_msg!="")
        flash[:error] = 'You are not on a team in the following course(s)<br/>' + course_error_msg
      end
      format.html { redirect_to(edit_effort_log_url) }
      format.xml { head :ok }
    else
      format.html { render "edit" }
      format.xml { render :xml => @effort_log.errors, :status => :unprocessable_entity }
    end
  end
end

#update_task_type_selectObject


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

def update_task_type_select
  unless params[:task_id].blank?
    logger.debug "updated " + params[:task_id]
    @selected_type = TaskType.find_by_id(params[:task_id])
  end

  render :partial => "description_update"
end