/
course.rb
559 lines (460 loc) · 16 KB
/
course.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# frozen_string_literal: true
# == Schema Information
#
# Table name: courses
#
# id :integer not null, primary key
# title :string(255)
# created_at :datetime
# updated_at :datetime
# start :datetime
# end :datetime
# school :string(255)
# term :string(255)
# character_sum :integer default(0)
# view_sum :bigint default(0)
# user_count :integer default(0)
# article_count :integer default(0)
# revision_count :integer default(0)
# slug :string(255)
# subject :string(255)
# expected_students :integer
# description :text(65535)
# submitted :boolean default(FALSE)
# passcode :string(255)
# timeline_start :datetime
# timeline_end :datetime
# day_exceptions :string(2000) default("")
# weekdays :string(255) default("0000000")
# new_article_count :integer default(0)
# no_day_exceptions :boolean default(FALSE)
# trained_count :integer default(0)
# cloned_status :integer
# type :string(255) default("ClassroomProgramCourse")
# upload_count :integer default(0)
# uploads_in_use_count :integer default(0)
# upload_usages_count :integer default(0)
# syllabus_file_name :string(255)
# syllabus_content_type :string(255)
# syllabus_file_size :bigint
# syllabus_updated_at :datetime
# home_wiki_id :integer
# recent_revision_count :integer default(0)
# needs_update :boolean default(FALSE)
# chatroom_id :string(255)
# flags :text(65535)
# level :string(255)
# private :boolean default(FALSE)
# withdrawn :boolean default(FALSE)
# references_count :integer default(0)
#
require_dependency "#{Rails.root}/lib/course_cache_manager"
require_dependency "#{Rails.root}/lib/course_training_progress_manager"
require_dependency "#{Rails.root}/lib/trained_students_manager"
require_dependency "#{Rails.root}/lib/word_count"
require_dependency "#{Rails.root}/lib/course_meetings_manager"
#= Course model
class Course < ApplicationRecord
######################
# Users for a course #
######################
has_many :courses_users, class_name: 'CoursesUsers', dependent: :destroy
has_many :users, -> { distinct }, through: :courses_users
has_many :students, -> { where('courses_users.role = 0') },
through: :courses_users, source: :user
has_many :nonstudents, -> { where('courses_users.role > 0') },
through: :courses_users, source: :user
has_many :instructors, -> { where('courses_users.role = 1') },
through: :courses_users, source: :user
has_many :volunteers, -> { where('courses_users.role > 1') },
through: :courses_users, source: :user
has_many :staff, -> { where('courses_users.role = 4') },
through: :courses_users, source: :user
has_many :survey_notifications, dependent: :destroy
has_many :requested_accounts, dependent: :destroy
has_many :tickets,
class_name: 'TicketDispenser::Ticket',
foreign_key: 'project_id',
dependent: :destroy
has_one :course_stat, class_name: 'CourseStat', dependent: :destroy
#########################
# Activity by the users #
#########################
# :revisions and :all_revisions have the same default implementation,
# but a course type may override :revisions.
has_many(:revisions, lambda do |course|
where('date >= ?', course.start).where('date <= ?', course.end)
end, through: :students)
has_many(:all_revisions, lambda do |course|
where('date >= ?', course.start).where('date <= ?', course.end)
end, through: :students)
# Same as revisions, but isn't bounded by the course end date
has_many(:recent_revisions, lambda do |course|
where('date >= ?', course.start)
end, through: :students, source: :revisions)
has_many(:uploads, lambda do |course|
where('uploaded_at >= ?', course.start).where('uploaded_at <= ?', course.end)
end, through: :students)
has_many :articles_courses, class_name: 'ArticlesCourses', dependent: :destroy
has_many :articles, -> { distinct }, through: :articles_courses
has_many :pages_edited, -> { distinct }, source: :article, through: :revisions
has_many :sandboxes, -> { distinct.sandbox }, source: :article, through: :revisions
has_many :assignments, dependent: :destroy
has_many :categories_courses, class_name: 'CategoriesCourses', dependent: :destroy
has_many :categories, through: :categories_courses
has_many :alerts
has_many :public_alerts, -> { nonprivate }, class_name: 'Alert'
############
# Metadata #
############
belongs_to :home_wiki, class_name: 'Wiki'
has_many :campaigns_courses, class_name: 'CampaignsCourses', dependent: :destroy
has_many :campaigns, through: :campaigns_courses
has_many :tags, dependent: :destroy
has_many :courses_wikis, class_name: 'CoursesWikis', dependent: :destroy
has_many :wikis, through: :courses_wikis
has_many :course_wiki_namespaces, class_name: 'CourseWikiNamespaces', through: :courses_wikis
serialize :flags, Hash
module ClonedStatus
NOT_A_CLONE = 0
# PENDING is the initial cloned record, where the cloning process was
# initiated, but not completed.
PENDING = 1
COMPLETED = 2
end
##########
# Scopes #
##########
scope :nonprivate, -> { where(private: false) }
# Legacy courses are ones that are imported from the EducationProgram
# MediaWiki extension, not created within the dashboard via the wizard.
scope :legacy, -> { where(type: 'LegacyCourse') }
def self.submitted_but_unapproved
Course.includes(:campaigns).where(campaigns: { id: nil })
.where(submitted: true).references(:campaigns)
.order('timeline_start')
end
def self.unsubmitted
Course.includes(:campaigns).where(campaigns: { id: nil })
.where(submitted: false).references(:campaigns)
end
scope :strictly_current, -> { where('? BETWEEN start AND end', Time.zone.now) }
scope :current, -> { current_and_future.where('start < ?', Time.zone.now) }
scope :ended, -> { where('end < ?', Time.zone.now) }
# A course stays "current" for a while after the end date, during which time
# we still check for new data and update page views. To exclude those courses
# use "strictly_current".
UPDATE_LENGTH = ENV['update_length'].to_i.days.seconds.to_i
scope :current_and_future, lambda {
where('end > ?', Time.zone.now - UPDATE_LENGTH)
}
scope :archived, lambda {
where('end <= ?', Time.zone.now - UPDATE_LENGTH)
}
scope :ready_for_update, -> { current.or(where(needs_update: true)) }
def self.will_be_ready_for_survey(opts)
days_offset, before, relative_to = opts.values_at(:days, :before, :relative_to)
today = Time.zone.now
ready_date = before ? today + days_offset.days : today - days_offset.days
where("courses.#{relative_to} > '#{ready_date}'")
end
def self.ready_for_survey(opts)
days_offset, before, relative_to = opts.values_at(:days, :before, :relative_to)
today = Time.zone.now
ready_date = before ? today + days_offset.days : today - days_offset.days
where("courses.#{relative_to} <= '#{ready_date}'")
end
###########
# Aliases #
###########
alias_attribute :institution, :school
##################
# Course content #
##################
has_many :weeks, dependent: :destroy
has_many :blocks, through: :weeks
has_attached_file :syllabus
validates_attachment_content_type :syllabus,
content_type: %w[application/pdf application/msword]
validates :passcode, presence: true, if: :passcode_required?
validates :start, presence: true
validates :end, presence: true
COURSE_TYPES = %w[
ArticleScopedProgram
BasicCourse
ClassroomProgramCourse
Editathon
FellowsCohort
LegacyCourse
VisitingScholarship
].freeze
validates_inclusion_of :type, in: COURSE_TYPES
#############
# Callbacks #
#############
before_save :ensure_required_params
before_save :reorder_weeks
before_save :set_default_times
before_save :check_course_times
before_save :set_needs_update
after_create :ensure_home_wiki_in_courses_wikis
####################
# Instance methods #
####################
delegate :students_up_to_date_with_training, to: :trained_students_manager
delegate :students_with_overdue_training, to: :trained_students_manager
def to_param
# This method is used by ActiveRecord
slug
end
def legacy?
type == 'LegacyCourse'
end
# Overridden for some course types
def passcode_required?
true
end
def approved?
campaigns.any? && !withdrawn
end
def closed?
flags[:closed_date].present?
end
def tag?(query_tag)
tags.pluck(:tag).include? query_tag
end
# title can be a string or a Regexp
def find_block_by_title(title)
title_matcher = Regexp.new(title)
blocks.find { |block| block.title =~ title_matcher }
end
def training_modules
@training_modules ||= TrainingModule.all.select { |tm| training_module_ids.include?(tm.id) }
end
def training_module_ids
@training_module_ids ||= Block.joins(:week).where(weeks: { course_id: id })
.where.not('training_module_ids = ?', [].to_yaml)
.collect(&:training_module_ids).flatten
end
def tracked_revisions
revisions.where.not(article_id: articles_courses.not_tracked.pluck(:article_id))
.where(wiki_id: wiki_ids)
end
def tracked_namespaces
courses_wikis.map do |course_wiki|
wiki = course_wiki.wiki
wiki_namespaces = course_wiki.course_wiki_namespaces
if wiki_namespaces.length.zero?
{ wiki:, namespace: 0 }
else
wiki_namespaces.map do |wiki_ns|
{ wiki:, namespace: wiki_ns.namespace }
end
end
end.flatten
end
def suspected_plagiarism
revisions.suspected_plagiarism
end
def scoped_article_ids
assigned_article_ids + category_article_ids
end
def assigned_article_ids
assignments.pluck(:article_id)
end
def category_article_ids
categories.inject([]) { |ids, cat| ids + cat.article_ids }
end
def update_until
self.end + UPDATE_LENGTH
end
def wiki_page?
wiki_course_page_enabled? && home_wiki.edits_enabled?
end
def wiki_title
return nil unless wiki_page?
escaped_slug = slug.tr(' ', '_') # follow MediaWiki page name conventions: undescores for spaces
"#{home_wiki.course_prefix}/#{escaped_slug}"
end
def update_wikis(updated_wikis)
update(wikis: updated_wikis)
ensure_home_wiki_in_courses_wikis
end
# The url for the on-wiki version of the course.
def url
# Some courses do not have corresponding on-wiki pages, so they have no wiki_title or url.
return unless wiki_page?
"#{home_wiki.base_url}/wiki/#{wiki_title}"
end
def edited_articles_courses
articles_courses.tracked.live
end
def new_articles_courses
edited_articles_courses.new_article
end
def new_articles_on(wiki)
new_articles_courses.where("articles.wiki_id = #{wiki.id}")
end
def uploads_in_use
uploads.where('usage_count > 0')
end
def word_count
@word_count ||= WordCount.from_characters(character_sum)
end
def average_word_count
return 0 if user_count.zero?
word_count / user_count
end
# Overridden for some course types
def wiki_edits_enabled?
return true unless flags.key?(:wiki_edits_enabled)
flags[:wiki_edits_enabled]
end
def edit_settings
flags['edit_settings']
end
def academic_system
flags['academic_system']
end
def format
flags['format']
end
def edit_settings_present?
flags.key?('edit_settings')
end
def wiki_course_page_enabled?
edit_settings['wiki_course_page_enabled']
end
# Overridden for some course types
def assignment_edits_enabled?
return false unless wiki_edits_enabled?
return true unless edit_settings_present?
edit_settings['assignment_edits_enabled']
end
def enrollment_edits_enabled?
return false unless wiki_edits_enabled?
return true unless edit_settings_present?
edit_settings['enrollment_edits_enabled']
end
def controlled_by_event_center?
flags[:event_sync].present?
end
def peer_review_count
flags[:peer_review_count]
end
# An extra param added to some wiki output.
# Overridden by FellowsCohort.
def wiki_template_param
nil
end
# Overidden by ClassroomProgramCourse
def timeline_enabled?
flags[:timeline_enabled].present?
end
def online_volunteers_enabled?
flags[:online_volunteers_enabled].present?
end
def stay_in_sandbox?
flags[:stay_in_sandbox].present?
end
def retain_available_articles?
flags[:retain_available_articles].present?
end
def disable_student_emails?
flags[:disable_student_emails].present?
end
def review_bibliography?
flags[:review_bibliography].present?
end
def very_long_update?
flags[:very_long_update].present?
end
# Overridden for ClassroomProgramCourse
def progress_tracker_enabled?
false
end
# Overridden for some course types
def cloneable?
!tag?('no_clone')
end
# Overridden for some course types
def training_library_slug
'students'
end
def account_requests_enabled?
return true if flags[:register_accounts].present?
campaigns.exists?(register_accounts: true)
end
def meetings_manager
@meetings_manager ||= CourseMeetingsManager.new(self)
end
def training_progress_manager
@training_progress_manager ||= CourseTrainingProgressManager.new(self)
end
def submitted_at
tags.find_by(tag: 'submitted')&.created_at
end
def approved_at
campaigns_courses.first&.created_at
end
def no_sandboxes?
flags[:no_sandboxes].present?
end
#################
# Cache methods #
#################
def update_cache
CourseCacheManager.new(self).update_cache
end
#################
# Class methods #
#################
def self.update_all_caches
ready_for_update.each(&:update_cache)
end
# Ensures weeks for a course have order 1..weeks.count
# This is dangerous if creating or reordering timeline content except via
# TimelineController, where every week is processed from the submitted params,
# and blocks get resorted to the appropriate week if necessary.
# Weeks are expected to have the same order as their ids.
def reorder_weeks
weeks.each_with_index do |week, i|
week.update(order: i + 1)
end
end
# Makes sure that the home wiki
# is always a part of courses wikis.
def ensure_home_wiki_in_courses_wikis
wikis.push(home_wiki) unless wikis.include? home_wiki
end
private
def trained_students_manager
TrainedStudentsManager.new(self)
end
def ensure_required_params
# Halt the callback chain and do not save if require params are missing
throw :abort unless [title, school, term, slug].count(nil).zero?
self.timeline_start ||= start
self.timeline_end ||= self.end
end
# Override start and end times if the controls are hidden from the interface.
# use_start_and_end_times is true when the times are user-supplied.
def set_default_times
return if use_start_and_end_times
self.start = start.beginning_of_day
self.end = self.end.end_of_day
self.timeline_start = timeline_start.beginning_of_day
self.timeline_end = timeline_end.end_of_day
end
# Check if course times are invalid and if yes, set the end time to be the same
# as that of the start time
def check_course_times
self.end = start if start > self.end
end
# If the start date changed, set needs_update to `true` so that
# the stats will be updated to reflect the new start date
# and the earlier revisions will be fetched.
def set_needs_update
self.needs_update = true if start_changed?
end
end