-
Notifications
You must be signed in to change notification settings - Fork 479
/
lesson.rb
922 lines (813 loc) · 37 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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
# == 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 unit
# (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, class_name: 'Unit', inverse_of: :lessons, optional: true
belongs_to :lesson_group, optional: true
has_many :lesson_activities, -> {order(:position)}, dependent: :destroy
has_many :activity_sections, through: :lesson_activities
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
has_one :rubric, dependent: :destroy
self.table_name = 'stages'
serialized_attrs %w(
overview
student_overview
unplugged
creative_commons_license
assessment
purpose
preparation
announcements
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 unit overview page (lockable false OR
# has_lesson_plan true)
# 2. lessons that are not numbered on the unit 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, case_sensitive: true, message: ->(object, _data) do
"lesson with key #{object.key.inspect} is already taken within unit #{object.script&.name.inspect}"
end
include CodespanOnlyMarkdownHelper
MARKDOWN_FIELDS = %w(overview student_overview preparation assessment_opportunities purpose)
def self.update_lessons_in_migrated_unit(unit, lesson_group, raw_lessons, counters)
raw_lessons.map do |raw_lesson|
lesson = fetch_lesson(raw_lesson, unit)
lesson.assign_attributes(
lesson_group: lesson_group,
absolute_position: (counters.lesson_position += 1),
relative_position: lesson.numbered_lesson? ? (counters.numbered_lesson_count += 1) : (counters.unnumbered_lesson_count += 1)
)
lesson.save! if lesson.changed?
lesson
end
end
def self.fetch_lesson(raw_lesson, unit)
if raw_lesson[:id]
return Lesson.find_by!(script: unit, id: raw_lesson[:id], key: raw_lesson[:key])
end
Lesson.prevent_blank_display_name(raw_lesson)
Lesson.create!(
key: raw_lesson[:key],
script: unit,
name: raw_lesson[:name],
relative_position: 0, # will be updated by the caller, but can't be nil
has_lesson_plan: true
)
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
def script
return Unit.get_from_cache(script_id) if Unit.should_cache?
super
end
def to_param
relative_position.to_s
end
def get_script_level_by_id
# if Scripts are cached, then we do in-memory filtering to avoid a database
# hit. If Scripts are NOT cached, then we want to find by a query in order
# to _minimize_ the database hit.
if Unit.should_cache?
script_levels = script.script_levels.select {|sl| sl.stage_id == id}
return script_levels.first
else
return script.script_levels.find_by(stage_id: id)
end
end
def unplugged_lesson?
script_level = get_script_level_by_id
return false if script_level.blank?
script_level.oldest_active_level.unplugged?
end
def spelling_bee?
script_level = get_script_level_by_id
return false if script_level.blank?
script_level.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 Unit.unit_in_category?('csf', script.name) && ['2017', '2018'].include?(script.version_year)
!!has_lesson_plan
end
# Returns a version of the named property which is fully ready for
# user-facing rendering. Currently does localization and markdown
# preprocessing, could in the future be expanded to do more.
def render_property(property_name)
raise "Rendering #{property_name} which is not in MARKDOWN_FIELDS" unless MARKDOWN_FIELDS.include?(property_name.to_s)
result = get_localized_property(property_name)
result = Services::MarkdownPreprocessor.process(result || '')
return result
end
# If there is a script_level, build_script_level_path will provide the correct url,
# even if it's a lockable lesson. Otherwise, we give the url to the student resources
# page, and the lesson plan pdf as a backup.
def start_url
if script_levels.first
return url_from_path(build_script_level_path(script_levels.first), CDO.default_scheme)
elsif script.include_student_lesson_plans && script.is_migrated
return url_from_path(script_lesson_student_path(script, self), CDO.default_scheme)
elsif student_lesson_plan_pdf_url
return student_lesson_plan_pdf_url
end
end
# A simple helper function to encapsulate creating a unique key, since this
# model does not have a unique identifier field of its own.
def get_localized_property(property_name)
key = Services::GloballyUniqueIdentifiers.build_lesson_key(self)
Services::I18n::CurriculumSyncUtils.get_localized_property(self, property_name, key)
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 # unit only has one lesson, use the unit name
script.title_for_display
end
end
def localized_name
get_localized_property(:name) || ''
end
def localized_lesson_plan
return script_lesson_path(script, self) if script.is_migrated? && !script.use_legacy_lesson_plans?
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_feedback_url
url = "https://studio.code.org/form/teacher_lesson_feedback?survey_data[script_name]=#{script.name}&survey_data[lesson_number]=#{relative_position}&survey_data[lesson_name]=#{CGI.escape(localized_name)}"
url += if script.unit_group
"&survey_data[course_name]=#{CGI.escape(script.unit_group.localized_title)}&survey_data[unit_name]=#{CGI.escape(script.localized_title)}&survey_data[unit_number]=#{script.unit_group_units&.first&.position}"
else
"&survey_data[course_name]=#{CGI.escape(script.localized_title)}"
end
url
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, student_facing: 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 = get_localized_property('student_overview') || ''
description_student = render_codespan_only_markdown(description_student) unless script.is_migrated?
description_teacher = get_localized_property('overview') || ''
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: get_uncached_edit_path,
lessonStartUrl: start_url
}
# 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
# only collect lesson feedback on the most recent stable english version of the course
lesson_data[:lesson_feedback_url] = lesson_feedback_url if script.get_course_version&.recommended?
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 get_uncached_edit_path
# for hoc scripts, everything under /s/[script-name]/lessons/* is cached,
# and user-identifying cookies are stripped. this means we can't tell if
# a user trying to edit a lesson plan via /s/[script-name]/lessons/1/edit
# has sufficient permissions or not. therefore, use a different path
# when editing lesson plans in hoc scripts.
has_lesson_plan && ScriptConfig.hoc_scripts.exclude?(script.name) ?
script_lesson_edit_path(script, self) :
edit_lesson_path(id: id)
end
def get_uncached_show_path
# use a custom path for viewing hoc lesson plans on levelbuilder, so that
# levelbuilders can see the gray "extra links" box with a link to edit
# the lesson. this also sidesteps some weird problems where visiting a
# a path like levelbuilder-studio.code.org/s/dance/lessons/1 messes up
# the user's login session and requires them to reauthenticate before
# accessing other pages which require levelbuilder credentials.
ScriptConfig.hoc_scripts.include?(script.name) ? lesson_path(id: id) : script_lesson_path(script, self)
end
def total_lesson_duration
lesson_activities.map(&:summarize).sum {|activity| activity[:duration] || 0}
end
def summarize_for_calendar
{
id: id,
lessonNumber: relative_position,
title: localized_title,
duration: total_lesson_duration,
assessment: !!assessment,
unplugged: unplugged,
url: script_lesson_path(script, self)
}
end
# Provides data about this lesson needed by the edit page for unmigrated units.
def summarize_for_unit_edit
summary = summarize(true, for_edit: true).dup
# Do not let unit 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
def summarize_for_migrated_unit_edit
{
id: id,
name: name,
key: key,
assessment: !!assessment,
lockable: !!lockable,
hasLessonPlan: has_lesson_plan,
unplugged: unplugged,
lessonEditPath: get_uncached_edit_path
}
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 unit 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.sort_by(&:word).map(&:summarize_for_lesson_edit),
programmingExpressions: programming_expressions.sort_by {|pe| pe.syntax || ''}.map(&:summarize_for_lesson_edit),
objectives: objectives.sort_by(&:description).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),
lessonPath: get_uncached_show_path,
rubric: rubric,
}
end
def summarize_for_lesson_show(user, can_view_teacher_markdown)
{
id: id,
unit: script.summarize_for_lesson_show,
position: relative_position,
lockable: lockable,
key: key,
duration: total_lesson_duration,
displayName: localized_name,
overview: render_property(:overview),
announcements: announcements,
purpose: render_property(:purpose),
preparation: render_property(:preparation),
activities: lesson_activities.map {|la| la.summarize_for_lesson_show(can_view_teacher_markdown, user)},
resources: resources_for_lesson_plan(user&.verified_instructor?),
vocabularies: vocabularies.sort_by(&:word).map(&:summarize_for_lesson_show),
programmingExpressions: programming_expressions.sort_by {|pe| pe.syntax || ''}.map(&:summarize_for_lesson_show),
objectives: objectives.sort_by(&:description).map(&:summarize_for_lesson_show),
standards: standards.map(&:summarize_for_lesson_show),
opportunityStandards: opportunity_standards.map(&:summarize_for_lesson_show),
is_instructor: script.can_be_instructor?(user),
assessmentOpportunities: render_property(:assessment_opportunities),
lessonPlanPdfUrl: lesson_plan_pdf_url,
courseVersionStandardsUrl: course_version_standards_url,
isVerifiedInstructor: user&.verified_instructor?,
hasVerifiedResources: lockable || lesson_plan_has_verified_resources,
scriptResourcesPdfUrl: script.get_unit_resources_pdf_url
}
end
def summarize_for_rollup(user)
{
key: key,
position: relative_position,
displayName: localized_name,
preparation: render_property(:preparation),
resources: resources_for_lesson_plan(user&.verified_instructor?),
vocabularies: vocabularies.sort_by(&:word).map(&:summarize_for_lesson_show),
programmingExpressions: programming_expressions.sort_by {|pe| pe.syntax || ''}.map(&:summarize_for_lesson_show),
objectives: objectives.sort_by(&:description).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)
{
id: id,
unit: script.summarize_for_lesson_show(true),
position: relative_position,
key: key,
displayName: localized_name,
overview: get_localized_property(:student_overview) || '',
announcements: (announcements || []).select {|announcement| announcement['visibility'] != "Teacher-only"},
resources: (all_resources['Student'] || []).concat(all_resources['All'] || []),
vocabularies: vocabularies.sort_by(&:word).map(&:summarize_for_lesson_show),
programmingExpressions: programming_expressions.sort_by {|pe| pe.syntax || ''}.map(&:summarize_for_lesson_show),
studentLessonPlanPdfUrl: student_lesson_plan_pdf_url
}
end
def summarize_for_lesson_dropdown(is_student = false)
{
id: id,
key: key,
displayName: localized_name,
link: is_student ? script_lesson_student_path(script, self) : script_lesson_path(script, self),
position: relative_position
}
end
def summarize_for_rubric_edit
{
id: id,
unitName: script.title_for_display,
lessonNumber: relative_position,
lessonName: name,
levels: levels
}
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
# 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 Unit.should_cache?
script_levels.map {|sl| Unit.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)
if script.show_unit_overview_between_lessons?
return script_path(script)
end
next_level = next_level_for_lesson_extras(user)
next_level ?
build_script_level_path(next_level) : script_completion_redirect(user, script)
end
def next_level_number_for_lesson_extras(user)
next_level = next_level_for_lesson_extras(user)
next_level&.lesson&.relative_position
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 units 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.filter_map do |objective|
next nil if objective['description'].blank?
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 unit 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 unit 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 Unit 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).
# This SQL string is not at risk for injection vulnerabilites because
# it's not actually using any user-provided values, just
# levelbuilder-defined ones, so it's safe to wrap in Arel.sql
order(Arel.sql("scripts.properties -> '$.version_year'"), 'scripts.name')
lessons - [self]
end
# @return [Array<Hash>]
def summarize_related_lessons
related_lessons.map do |lesson|
{
unitTitle: 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.sort_by(&:name).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
# Makes a copy of the lesson and adds it to the end last lesson group
# in destination_unit. It does not clone levels.
# Both destination_unit and the unit this lesson is in must:
# - be migrated
# - be in a course version
# - be in course versions from the same version year
def copy_to_unit(destination_unit, new_level_suffix = nil)
return if script == destination_unit
raise 'Both lesson and unit must be migrated' unless script.is_migrated? && destination_unit.is_migrated?
raise 'Destination unit and lesson must be in a course version' if destination_unit.get_course_version.nil? && !destination_unit.old_professional_learning_course?
if !destination_unit.old_professional_learning_course? && script.old_professional_learning_course?
raise 'Deeper learning lesson must be copied to deeper learning courses.'
end
copied_lesson = dup
# scripts.en.yml cannot handle the '.' character in key names
copied_lesson.key = copied_lesson.name.delete('.')
copied_lesson.script_id = destination_unit.id
destination_lesson_group = destination_unit.lesson_groups.last
unless destination_lesson_group
destination_lesson_group = LessonGroup.create!(script: destination_unit, position: 1, user_facing: false, key: 'new-lesson-group')
Unit.merge_and_write_i18n(destination_lesson_group.i18n_hash, destination_unit.name)
end
copied_lesson.lesson_group_id = destination_lesson_group.id
copied_lesson.absolute_position = destination_unit.lessons.count + 1
copied_lesson.relative_position =
destination_unit.lessons.count {|l| copied_lesson.numbered_lesson? == l.numbered_lesson?} + 1
copied_lesson.save!
unless destination_unit.old_professional_learning_course?
# Copy objects that require course version, i.e. resources and vocab
course_version = destination_unit.get_course_version
copied_resource_map = {}
copied_lesson.resources = resources.map do |original_resource|
copied_resource = original_resource.copy_to_course_version(course_version)
copied_resource_map[original_resource.key] = copied_resource
copied_resource
end.uniq
copied_vocab_map = {}
copied_lesson.vocabularies = vocabularies.map do |original_vocab|
copied_vocab = original_vocab.copy_to_course_version(course_version)
copied_vocab_map[original_vocab.key] = copied_vocab
copied_vocab
end.uniq
update_resource_link_on_clone = proc do |resource|
new_resource = copied_resource_map[resource.key] || resource.copy_to_course_version(course_version)
"[r #{new_resource ? Services::GloballyUniqueIdentifiers.build_resource_key(new_resource) : Services::GloballyUniqueIdentifiers.build_resource_key(resource)}]"
end
update_vocab_definition_on_clone = proc do |vocab|
new_vocab = copied_vocab_map[vocab.key] || vocab.copy_to_course_version(course_version)
"[v #{new_vocab ? Services::GloballyUniqueIdentifiers.build_vocab_key(new_vocab) : Services::GloballyUniqueIdentifiers.build_vocab_key(vocab)}]"
end
MARKDOWN_FIELDS.each do |field|
next unless copied_lesson.try(field)
Services::MarkdownPreprocessor.sub_resource_links!(copied_lesson.try(field), update_resource_link_on_clone)
Services::MarkdownPreprocessor.sub_vocab_definitions!(copied_lesson.try(field), update_vocab_definition_on_clone)
end
end
copied_lesson.save! if copied_lesson.changed?
# Copy lesson activities, activity sections, and script levels
copied_lesson.lesson_activities = lesson_activities.map do |original_lesson_activity|
copied_lesson_activity = original_lesson_activity.dup
copied_lesson_activity.key = SecureRandom.uuid
copied_lesson_activity.lesson_id = copied_lesson.id
copied_lesson_activity.save!
copied_lesson_activity.activity_sections = original_lesson_activity.activity_sections.map do |original_activity_section|
copied_activity_section = original_activity_section.dup
copied_activity_section.key = SecureRandom.uuid
copied_activity_section.lesson_activity_id = copied_lesson_activity.id
unless destination_unit.old_professional_learning_course?
if copied_activity_section.description
Services::MarkdownPreprocessor.sub_resource_links!(copied_activity_section.description, update_resource_link_on_clone)
Services::MarkdownPreprocessor.sub_vocab_definitions!(copied_activity_section.description, update_vocab_definition_on_clone)
end
if copied_activity_section.tips.present?
copied_activity_section.tips.each do |tip|
next unless tip && tip['markdown']
Services::MarkdownPreprocessor.sub_resource_links!(tip['markdown'], update_resource_link_on_clone)
Services::MarkdownPreprocessor.sub_vocab_definitions!(tip['markdown'], update_vocab_definition_on_clone)
end
end
end
copied_activity_section.save!
sl_data = original_activity_section.script_levels.map.with_index(1) do |original_script_level, pos|
# Only include active level and discard variants
original_active_level = original_script_level.oldest_active_level
copied_level = new_level_suffix.blank? ? original_active_level : original_active_level.clone_with_suffix(new_level_suffix)
{
"activitySectionPosition" => pos,
"assessment" => original_script_level.assessment,
"bonus" => original_script_level.bonus,
"challenge" => original_script_level.challenge,
"levels" => [copied_level]
}
end
copied_activity_section.update_script_levels(sl_data) if sl_data.present?
copied_activity_section
end
copied_lesson_activity
end
# Copy objectives
copied_lesson.objectives = objectives.map do |original_objective|
copied_objective = original_objective.dup
copied_objective.key = SecureRandom.uuid
copied_objective
end
# Copy programming expressions and standards associations
copied_lesson.programming_expressions = programming_expressions
copied_lesson.standards = standards
copied_lesson.opportunity_standards = opportunity_standards
destination_unit.fix_script_level_positions
destination_unit.write_script_json
copied_lesson
end
def report_bug_url(request)
message = "Bug in Lesson #{name}\n#{request.url}\n#{request.user_agent}\n"
"https://support.code.org/hc/en-us/requests/new?&tf_description=#{CGI.escape(message)}"
end
# Finds the LessonActivity by id, or creates a new one if id is not specified.
# @param activity [Hash]
# @returns [LessonActivity]
private 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