/
lesson.rb
734 lines (644 loc) · 28.3 KB
/
lesson.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# == Schema Information
#
# Table name: stages
#
# id :integer not null, primary key
# name :string(255) not null
# absolute_position :integer
# script_id :integer not null
# created_at :datetime
# updated_at :datetime
# lockable :boolean default(FALSE), not null
# relative_position :integer not null
# properties :text(65535)
# lesson_group_id :integer
# key :string(255) not null
# has_lesson_plan :boolean not null
#
# Indexes
#
# index_stages_on_lesson_group_id_and_key (lesson_group_id,key) UNIQUE
# index_stages_on_script_id_and_key (script_id,key) UNIQUE
#
require 'cdo/shared_constants'
# Ordered partitioning of script levels within a script
# (Intended to replace most of the functionality in Game, due to the need for multiple app types within a single Lesson)
class Lesson < ApplicationRecord
include LevelsHelper
include SharedConstants
include Rails.application.routes.url_helpers
include SerializedProperties
belongs_to :script, inverse_of: :lessons
belongs_to :lesson_group
has_many :lesson_activities, -> {order(:position)}, dependent: :destroy
has_many :script_levels, -> {order(:chapter)}, foreign_key: 'stage_id', dependent: :destroy
has_many :levels, through: :script_levels
has_and_belongs_to_many :resources, join_table: :lessons_resources
has_and_belongs_to_many :vocabularies, join_table: :lessons_vocabularies
has_and_belongs_to_many :programming_expressions, join_table: :lessons_programming_expressions
has_many :objectives, dependent: :destroy
# join tables needed for seeding logic
has_many :lessons_resources
has_many :lessons_vocabularies
has_many :lessons_programming_expressions
has_one :plc_learning_module, class_name: 'Plc::LearningModule', inverse_of: :lesson, foreign_key: 'stage_id', dependent: :destroy
has_and_belongs_to_many :standards, foreign_key: 'stage_id'
has_many :lessons_standards, foreign_key: 'stage_id' # join table. we need this association for seeding logic
# the dependent: :destroy clause is needed to ensure that the associated join
# models are deleted when this lesson is deleted. in order for this to work,
# the join model must have an :id column.
has_many :lessons_opportunity_standards, dependent: :destroy
has_many :opportunity_standards, through: :lessons_opportunity_standards, source: :standard
self.table_name = 'stages'
serialized_attrs %w(
overview
student_overview
unplugged
creative_commons_license
assessment
purpose
preparation
announcements
visible_after
assessment_opportunities
)
# A lesson has an absolute position and a relative position. The difference between the two is that relative_position
# numbers the lessons in order in two groups 1. lessons that are numbered on the script overview page (lockable false OR has_lesson_plan true)
# 2. lessons that are not numbered on the script overview page (lockable true AND has_lesson_plan false)
# if we have two lessons without lesson plans that are lockable followed by a
# lesson that is not lockable, the third lesson will have an absolute_position of 3 but a relative_position of 1
acts_as_list scope: :script, column: :absolute_position
validates_uniqueness_of :key, scope: :script_id
include CodespanOnlyMarkdownHelper
def self.add_lessons(script, lesson_group, raw_lessons, counters, new_suffix, editor_experiment)
script.lessons.reload
raw_lessons.map do |raw_lesson|
Lesson.prevent_blank_display_name(raw_lesson)
Lesson.prevent_changing_stable_i18n_key(script, raw_lesson)
lesson = script.lessons.detect {|l| l.key == raw_lesson[:key]} ||
Lesson.find_or_create_by(
key: raw_lesson[:key],
script: script
) do |l|
l.name = "" # will be updated below, but cant be null
l.relative_position = 0 # will be updated below, but cant be null
l.has_lesson_plan = true # will be reset below if specified
end
numbered_lesson = !!raw_lesson[:has_lesson_plan] || !raw_lesson[:lockable]
lesson.assign_attributes(
name: raw_lesson[:name],
absolute_position: (counters.lesson_position += 1),
lesson_group: lesson_group,
lockable: !!raw_lesson[:lockable],
has_lesson_plan: !!raw_lesson[:has_lesson_plan],
visible_after: raw_lesson[:visible_after],
unplugged: !!raw_lesson[:unplugged],
relative_position: numbered_lesson ? (counters.numbered_lesson_count += 1) : (counters.unnumbered_lesson_count += 1)
)
lesson.save! if lesson.changed?
lesson.script_levels = ScriptLevel.add_script_levels(
script, lesson_group, lesson, raw_lesson[:script_levels], counters, new_suffix, editor_experiment
)
lesson.save!
lesson.reload
Lesson.prevent_multi_page_assessment_outside_final_level(lesson)
lesson
end
end
def self.prevent_changing_stable_i18n_key(script, raw_lesson)
if script.is_stable && ScriptConstants.i18n?(script.name) && I18n.t("data.script.name.#{script.name}.lessons.#{raw_lesson[:key]}").include?('translation missing:')
raise "Adding new keys or update existing keys for lessons in scripts that are marked as stable and included in the i18n sync is not allowed. Offending Lesson Key: #{raw_lesson[:key]}"
end
end
def self.prevent_blank_display_name(raw_lesson)
if raw_lesson[:name].blank?
raise "Expect all lessons to have display names. The following lesson does not have a display name: #{raw_lesson[:key]}"
end
end
# Go through all the script levels for this lesson, except the last one,
# and raise an exception if any of them are a multi-page assessment.
# (That's when the script level is marked assessment, and the level itself
# has a pages property and more than one page in that array.)
# This is because only the final level in a lesson can be a multi-page
# assessment.
def self.prevent_multi_page_assessment_outside_final_level(lesson)
lesson.script_levels.each do |script_level|
if lesson.script_levels.last != script_level && script_level.long_assessment?
raise "Only the final level in a lesson may be a multi-page assessment. Lesson: #{lesson.name}"
end
end
if lesson.lockable && !lesson.script_levels.last.assessment?
raise "Expect lockable lessons to have an assessment as their last level. Lesson: #{lesson.name}"
end
end
def script
return Script.get_from_cache(script_id) if Script.should_cache?
super
end
def to_param
relative_position.to_s
end
def unplugged_lesson?
script_levels = script.script_levels.select {|sl| sl.stage_id == id}
return false unless script_levels.first
script_levels.first.oldest_active_level.unplugged?
end
def spelling_bee?
script_levels = script.script_levels.select {|sl| sl.stage_id == id}
return false unless script_levels.first
script_levels.first.oldest_active_level.spelling_bee?
end
# We number lessons that either have lesson plans or are not lockable
def numbered_lesson?
!!has_lesson_plan || !lockable
end
def has_lesson_pdf?
return false if ScriptConstants.script_in_category?(:csf, script.name) || ScriptConstants.script_in_category?(:csf_2018, script.name)
!!has_lesson_plan
end
def localized_title
# The standard case for localized_title is something like "Lesson 1: Maze".
# In the case of lockable lessons without lesson plans, we don't want to include the Lesson 1
return localized_name unless numbered_lesson?
if script.lessons.to_a.many?
I18n.t('stage_number', number: relative_position) + ': ' + localized_name
else # script only has one lesson, use the script name
script.title_for_display
end
end
def localized_name
if script.lessons.many?
I18n.t "data.script.name.#{script.name}.lessons.#{key}.name"
else
I18n.t "data.script.name.#{script.name}.title"
end
end
def localized_lesson_plan
return script_lesson_path(script, self) if script.is_migrated
if script.curriculum_path?
path = script.curriculum_path.gsub('{LESSON}', relative_position.to_s)
if path.include? '{LOCALE}'
CDO.curriculum_url I18n.locale, path.split('{LOCALE}/').last
else
path
end
end
end
def lesson_plan_html_url
localized_lesson_plan || "#{lesson_plan_base_url}/Teacher"
end
def lesson_plan_pdf_url
if script.is_migrated && has_lesson_plan
Services::CurriculumPdfs.get_lesson_plan_url(self)
else
"#{lesson_plan_base_url}/Teacher.pdf"
end
end
def student_lesson_plan_pdf_url
if script.is_migrated && script.include_student_lesson_plans && has_lesson_plan
Services::CurriculumPdfs.get_lesson_plan_url(self, true)
end
end
def lesson_plan_base_url
CDO.code_org_url "/curriculum/#{script.name}/#{relative_position}"
end
def course_version_standards_url
script.get_course_version&.all_standards_url
end
def summarize(include_bonus_levels = false, for_edit: false)
lesson_summary = Rails.cache.fetch("#{cache_key}/lesson_summary/#{I18n.locale}/#{include_bonus_levels}") do
cached_levels = include_bonus_levels ? cached_script_levels : cached_script_levels.reject(&:bonus)
description_student = I18n.t('description_student', scope: [:data, :script, :name, script.name, :lessons, key], smart: true, default: '')
description_student = render_codespan_only_markdown(description_student) unless script.is_migrated?
description_teacher = I18n.t('description_teacher', scope: [:data, :script, :name, script.name, :lessons, key], smart: true, default: '')
description_teacher = render_codespan_only_markdown(description_teacher) unless script.is_migrated?
lesson_data = {
script_id: script.id,
script_name: script.name,
num_script_lessons: script.lessons.to_a.size,
id: id,
position: absolute_position,
relative_position: relative_position,
name: localized_name,
key: key,
assessment: !!assessment,
title: localized_title,
lesson_group_display_name: lesson_group&.localized_display_name,
lockable: !!lockable,
hasLessonPlan: has_lesson_plan,
numberedLesson: numbered_lesson?,
levels: cached_levels.map {|sl| sl.summarize(false, for_edit: for_edit)},
description_student: description_student,
description_teacher: description_teacher,
unplugged: unplugged,
lessonEditPath: edit_lesson_path(id: id)
}
# Use to_a here so that we get access to the cached script_levels.
# Without it, script_levels.last goes back to the database.
last_script_level = script_levels.to_a.last
# The last level in a lesson might be a long assessment, so add extra information
# related to that. This might include information for additional pages if it
# happens to be a multi-page long assessment.
if last_script_level&.long_assessment?
last_level_summary = lesson_data[:levels].last
extra_levels = ScriptLevel.summarize_extra_puzzle_pages(last_level_summary)
lesson_data[:levels] += extra_levels
last_level_summary[:uid] = "#{last_level_summary[:ids].first}_0"
last_level_summary[:url] << "/page/1"
last_level_summary[:page_number] = 1
end
if has_lesson_plan
lesson_data[:lesson_plan_html_url] = lesson_plan_html_url
lesson_data[:lesson_plan_pdf_url] = lesson_plan_pdf_url
if script.include_student_lesson_plans && script.is_migrated
lesson_data[:student_lesson_plan_html_url] = script_lesson_student_path(script, self)
end
end
if script.hoc?
lesson_data[:finishLink] = script.hoc_finish_url
lesson_data[:finishText] = I18n.t('nav.header.finished_hoc')
end
lesson_data[:lesson_extras_level_url] = script_lesson_extras_url(script.name, lesson_position: relative_position) unless unplugged_lesson?
lesson_data
end
lesson_summary.freeze
end
def summarize_for_calendar
{
id: id,
title: localized_title,
duration: lesson_activities.map(&:summarize).sum {|activity| activity[:duration] || 0},
assessment: !!assessment,
unplugged: unplugged,
url: script_lesson_path(script, self)
}
end
# Provides data about this lesson needed by the script edit page.
#
# TODO: [PLAT-369] trim down to only include those fields needed on the
# script edit page
def summarize_for_script_edit
summary = summarize(true, for_edit: true).dup
# Do not let script name override lesson name when there is only one lesson
summary[:name] = name
summary[:lesson_group_display_name] = lesson_group&.display_name
summary.freeze
end
# Provides all the editable data related to this lesson and its activities for
# display on the lesson edit page, excluding any lesson attributes which can
# be edited on the script edit page (e.g. name and key).
#
# The only non-editable data included are the ids of activities and activity
# sections, which are needed to identify those objects but cannot themselves
# be edited.
#
# Key names are converted to camelCase here so they can easily be consumed by
# the client.
def summarize_for_lesson_edit
lesson_standards = standards.sort_by {|s| [s.framework.name, s.shortcode]}
{
id: id,
name: name,
overview: overview,
studentOverview: student_overview,
assessmentOpportunities: assessment_opportunities,
assessment: assessment,
unplugged: unplugged,
lockable: lockable,
hasLessonPlan: has_lesson_plan,
creativeCommonsLicense: creative_commons_license,
purpose: purpose,
preparation: preparation,
announcements: announcements,
activities: lesson_activities.map(&:summarize_for_lesson_edit),
resources: resources.map(&:summarize_for_lesson_edit),
vocabularies: vocabularies.map(&:summarize_for_lesson_edit),
programmingEnvironments: ProgrammingEnvironment.all.map(&:summarize_for_lesson_edit),
programmingExpressions: programming_expressions.map(&:summarize_for_lesson_edit),
objectives: objectives.map(&:summarize_for_edit),
standards: lesson_standards.map(&:summarize_for_lesson_edit),
frameworks: Framework.all.map(&:summarize_for_lesson_edit),
opportunityStandards: opportunity_standards.map(&:summarize_for_lesson_edit),
courseVersionId: lesson_group.script.get_course_version&.id,
scriptIsVisible: !script.hidden,
scriptPath: script_path(script),
lessonPath: script_lesson_path(script, self),
lessonExtrasAvailableForScript: script.lesson_extras_available
}
end
def summarize_for_lesson_show(user, can_view_teacher_markdown)
{
unit: script.summarize_for_lesson_show,
position: relative_position,
lockable: lockable,
key: key,
displayName: localized_name,
overview: Services::MarkdownPreprocessor.process(overview || ''),
announcements: announcements,
purpose: Services::MarkdownPreprocessor.process(purpose || ''),
preparation: Services::MarkdownPreprocessor.process(preparation || ''),
activities: lesson_activities.map {|la| la.summarize_for_lesson_show(can_view_teacher_markdown)},
resources: resources_for_lesson_plan(user&.authorized_teacher?),
vocabularies: vocabularies.map(&:summarize_for_lesson_show),
programmingExpressions: programming_expressions.map(&:summarize_for_lesson_show),
objectives: objectives.map(&:summarize_for_lesson_show),
standards: standards.map(&:summarize_for_lesson_show),
opportunityStandards: opportunity_standards.map(&:summarize_for_lesson_show),
is_teacher: user&.teacher?,
assessmentOpportunities: Services::MarkdownPreprocessor.process(assessment_opportunities),
lessonPlanPdfUrl: lesson_plan_pdf_url,
courseVersionStandardsUrl: course_version_standards_url,
isVerifiedTeacher: user&.authorized_teacher?,
hasVerifiedResources: lockable || lesson_plan_has_verified_resources
}
end
def summarize_for_rollup(user)
{
key: key,
position: relative_position,
displayName: localized_name,
preparation: Services::MarkdownPreprocessor.process(preparation || ''),
resources: resources_for_lesson_plan(user&.authorized_teacher?),
vocabularies: vocabularies.map(&:summarize_for_lesson_show),
programmingExpressions: programming_expressions.map(&:summarize_for_lesson_show),
objectives: objectives.map(&:summarize_for_lesson_show),
standards: standards.map(&:summarize_for_lesson_show),
link: script_lesson_path(script, self)
}
end
def summarize_for_student_lesson_plan
all_resources = resources_for_lesson_plan(false)
{
unit: script.summarize_for_lesson_show(true),
position: relative_position,
key: key,
displayName: localized_name,
overview: student_overview || '',
announcements: (announcements || []).select {|announcement| announcement['visibility'] != "Teacher-only"},
resources: (all_resources['Student'] || []).concat(all_resources['All'] || []),
vocabularies: vocabularies.map(&:summarize_for_lesson_show),
programmingExpressions: programming_expressions.map(&:summarize_for_lesson_show),
studentLessonPlanPdfUrl: student_lesson_plan_pdf_url
}
end
def summarize_for_lesson_dropdown(is_student = false)
{
key: key,
displayName: localized_name,
link: is_student ? script_lesson_student_path(script, self) : script_lesson_path(script, self),
position: relative_position
}
end
# Provides a JSON summary of a particular lesson, that is consumed by tools used to
# build lesson plans (Curriculum Builder)
def summary_for_lesson_plans
{
# TODO: should be renamed after we combine CurriculumBuilder into LevelBuilder, if we still need this.
stageName: localized_name,
lockable: lockable?,
levels: script_levels.map do |script_level|
level = script_level.level
level_json = {
id: script_level.id,
position: script_level.position,
named_level: script_level.named_level?,
bonus_level: !!script_level.bonus,
assessment: script_level.assessment,
progression: script_level.progression,
path: script_level.path,
}
level_json.merge!(level.summary_for_lesson_plans)
level_json
end
}
end
# Returns a hash representing i18n strings in scripts.en.yml which may need
# to be updated after this object was updated. Currently, this only updates
# the lesson name and overviews.
def i18n_hash
{
script.name => {
'lessons' => {
key => {
'name' => name,
'description_student' => student_overview,
'description_teacher' => overview
}
}
}
}
end
# For a given set of students, determine when the given lesson is locked for
# each student.
# The design of a lockable lesson is that there is (optionally) some number of
# non-LevelGroup levels, followed by a single LevelGroup. This last one is the
# only one which is truly locked/unlocked. The lesson is considered locked if
# and only if the final assessment level is locked. When in this state, the UI
# will show the entire lesson as being locked, but if you know the URL of the other
# levels, you're still able to go to them and submit answers.
def lockable_state(students)
return unless lockable?
script_level = script_levels.last
unless script_level.assessment?
raise 'Expect lockable lessons to have an assessment as their last level'
end
return students.map do |student|
user_level = student.last_attempt_for_any script_level.levels, script_id: script.id
# user_level_data is provided so that we can get back to our user_level
# when updating. in some cases we don't yet have a user_level, and need
# to provide enough data to create one
# if we don't have a user level, consider ourselves locked
locked = user_level.nil? || user_level.show_as_locked?(self)
# if we don't have a user level, we can't be readonly
readonly = user_level.present? && user_level.show_as_readonly?(self)
{
user_level_data: {
user_id: student.id,
level_id: user_level.try(:level).try(:id) || script_level.oldest_active_level.id,
script_id: script_level.script.id
},
name: student.name,
locked: locked,
readonly_answers: readonly
}
end
end
# Ensures we get the cached ScriptLevels if they are being cached, vs hitting the db.
def cached_script_levels
return script_levels unless Script.should_cache?
script_levels.map {|sl| Script.cache_find_script_level(sl.id)}
end
def last_progression_script_level
script_levels.reverse.find(&:valid_progression_level?)
end
def next_level_for_lesson_extras(user)
level_to_follow = script_levels.last.next_level
level_to_follow = level_to_follow.next_level while level_to_follow.try(:locked_or_hidden?, user)
level_to_follow
end
def next_level_path_for_lesson_extras(user)
next_level = next_level_for_lesson_extras(user)
next_level ?
build_script_level_path(next_level) : script_completion_redirect(script)
end
def next_level_number_for_lesson_extras(user)
next_level = next_level_for_lesson_extras(user)
next_level ? next_level.lesson.relative_position : nil
end
def published?(user)
return true if user&.levelbuilder?
return true unless visible_after
Time.parse(visible_after) <= Time.now
end
# Updates this lesson's lesson_activities to match the activities represented
# by the provided data, preserving existing objects in cases where ids match.
# @param activities [Array<Hash>] - Array of hashes representing
# LessonActivity objects.
def update_activities(activities)
return unless activities
# use assignment to delete any missing activities.
self.lesson_activities = activities.map do |activity|
lesson_activity = fetch_activity(activity)
lesson_activity.update!(
position: activity['position'],
name: activity['name'],
duration: activity['duration']
)
lesson_activity.update_activity_sections(activity['activitySections'])
lesson_activity
end
# It's too messy to keep track of all 3 position values for scripts during
# this update, so just set activity_section_position as the source of truth
# and then fix chapter and position values after.
script.fix_script_level_positions
# Reload the lesson to make sure the positions information we have is all up
# to date
reload
end
def update_objectives(objectives)
return unless objectives
self.objectives = objectives.map do |objective|
persisted_objective = objective['id'].blank? ? Objective.new(key: SecureRandom.uuid) : Objective.find(objective['id'])
persisted_objective.description = objective['description']
persisted_objective.save!
persisted_objective
end
end
# Used for seeding from JSON. Returns the full set of information needed to
# uniquely identify this object as well as any other objects it belongs to.
# If the attributes of this object alone aren't sufficient, and associated objects are needed, then data from
# the seeding_keys of those objects should be included as well.
# Ideally should correspond to a unique index for this model's table.
# See comments on ScriptSeed.seed_from_hash for more context.
#
# @param [ScriptSeed::SeedContext] seed_context - contains preloaded data to use when looking up associated objects
# @return [Hash<String, String] all information needed to uniquely identify this object across environments.
def seeding_key(seed_context)
my_key = {'lesson.key': key}
my_lesson_group = seed_context.lesson_groups.select {|lg| lg.id == lesson_group_id}.first
raise "No LessonGroup found for #{self.class}: #{my_key}, LessonGroup ID: #{lesson_group_id}" unless my_lesson_group
lesson_group_seeding_key = my_lesson_group.seeding_key(seed_context)
my_key.merge!(lesson_group_seeding_key) {|key, _, _| raise "Duplicate key when generating seeding_key: #{key}"}
my_key.stringify_keys
end
# Finds all other lessons which match the following criteria:
# 1. the other lesson is in the same course offering as this lesson. Or, if
# this lesson is in a CSF course offering, the other lesson may also be in
# any other CSF course offering.
# 2. same lesson key (untranslated lesson name)
# The results are sorted first by version year and then by script name.
#
# This method is intended only to be used in levelbuilder mode, when script
# caching is disabled.
#
# The purpose of this method is to help curriculum writers find lessons
# related to the one they are currently editing in which they might want to
# make similar edits. The heuristic used by this method is that the lesson key
# will not change when a script is deep-copied into a new version year, or
# when a lesson is shared across CSF courses within the same version year. If
# this heuristic proves to be inadequate, we could consider adding an explicit
# link between related lessons.
#
# @return [Array<Lesson>]
def related_lessons
return related_csf_lessons if script&.curriculum_umbrella == 'CSF'
course_offering = script&.get_course_version&.course_offering
return [] unless course_offering
# all units in this course offering, including this lesson's unit
related_units = course_offering.course_versions.map(&:units).flatten
# to minimize the query count, make the initial query load any associations
# that may be used in the sort block below.
load_params = {
script: [
:course_version,
{
unit_group_units: {
unit_group: :course_version
}
}
]
}
lessons = Lesson.eager_load(load_params).
where(script: related_units).
where(key: key).to_a
# we cannot do the sort in the SQL query, because we don't know which
# association get_course_version will use to find the course version.
lessons.sort_by! do |lesson|
version_year = lesson.script.get_course_version.version_year
[version_year, lesson.script.name]
end
lessons - [self]
end
def related_csf_lessons
# because curriculum umbrella is stored on the Script model, take a big
# shortcut and look only at curriculum umbrella, ignoring course version
# and course offering. In the future, when curriulum_umbrella moves to
# CourseOffering, this implementation will need to change to be more like
# related_lessons.
lessons = Lesson.eager_load(script: :course_version).
where("scripts.properties -> '$.curriculum_umbrella' = ?", script.curriculum_umbrella).
where(key: key).
order("scripts.properties -> '$.version_year'", 'scripts.name')
lessons - [self]
end
# @return [Array<Hash>]
def summarize_related_lessons
related_lessons.map do |lesson|
{
scriptTitle: lesson.script.localized_title,
versionYear: lesson.script.get_course_version&.version_year,
lockable: lesson.lockable,
relativePosition: lesson.relative_position,
id: lesson.id,
editUrl: edit_lesson_path(id: lesson.id)
}
end
end
def resources_for_lesson_plan(verified_teacher)
grouped_resources = resources.map(&:summarize_for_lesson_plan).group_by {|r| r[:audience]}
if verified_teacher && grouped_resources.key?('Verified Teacher')
grouped_resources['Teacher'] ||= []
grouped_resources['Teacher'] += grouped_resources['Verified Teacher']
end
grouped_resources.delete('Verified Teacher')
grouped_resources
end
def lesson_plan_has_verified_resources
resources.any? {|r| r.audience == 'Verified Teacher'}
end
private
# Finds the LessonActivity by id, or creates a new one if id is not specified.
# @param activity [Hash]
# @returns [LessonActivity]
def fetch_activity(activity)
if activity['id']
lesson_activity = lesson_activities.find(activity['id'])
return lesson_activity if lesson_activity
raise ActiveRecord::RecordNotFound.new("LessonActivity id #{activity['id']} not found in Lesson id #{id}")
end
lesson_activities.create(
position: activity['position'],
key: SecureRandom.uuid
)
end
end