-
Notifications
You must be signed in to change notification settings - Fork 481
/
script.rb
1524 lines (1320 loc) · 52.8 KB
/
script.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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# == Schema Information
#
# Table name: scripts
#
# id :integer not null, primary key
# name :string(255) not null
# created_at :datetime
# updated_at :datetime
# wrapup_video_id :integer
# hidden :boolean default(FALSE), not null
# user_id :integer
# login_required :boolean default(FALSE), not null
# properties :text(65535)
# new_name :string(255)
# family_name :string(255)
#
# Indexes
#
# index_scripts_on_family_name (family_name)
# index_scripts_on_name (name) UNIQUE
# index_scripts_on_new_name (new_name) UNIQUE
# index_scripts_on_wrapup_video_id (wrapup_video_id)
#
require 'cdo/script_constants'
require 'cdo/shared_constants'
TEXT_RESPONSE_TYPES = [TextMatch, FreeResponse]
# A sequence of Levels
class Script < ActiveRecord::Base
include ScriptConstants
include SharedConstants
include Rails.application.routes.url_helpers
include Seeded
has_many :levels, through: :script_levels
has_many :script_levels, -> {order('chapter ASC')}, dependent: :destroy, inverse_of: :script # all script levels, even those w/ stages, are ordered by chapter, see Script#add_script
has_many :stages, -> {order('absolute_position ASC')}, dependent: :destroy, inverse_of: :script
has_many :users, through: :user_scripts
has_many :user_scripts
has_many :hint_view_requests
has_one :plc_course_unit, class_name: 'Plc::CourseUnit', inverse_of: :script, dependent: :destroy
belongs_to :wrapup_video, foreign_key: 'wrapup_video_id', class_name: 'Video'
belongs_to :user
has_many :course_scripts
has_many :courses, through: :course_scripts
scope :with_associated_models, -> do
includes(
[
{
script_levels: [
{levels: [:game, :concepts, :level_concept_difficulty]},
:stage,
:callouts
]
},
{
stages: [{script_levels: [:levels]}]
},
:course_scripts
]
)
end
attr_accessor :skip_name_format_validation
include SerializedToFileValidation
before_validation :hide_pilot_scripts
def hide_pilot_scripts
self.hidden = true unless pilot_experiment.blank?
end
# As we read and write to files with the script name, to prevent directory
# traversal (for security reasons), we do not allow the name to start with a
# tilde or dot or contain a slash.
validates :name,
presence: true,
format: {
without: /\A~|\A\.|\//,
message: 'cannot start with a tilde or dot or contain slashes'
}
include SerializedProperties
after_save :generate_plc_objects
SCRIPT_DIRECTORY = 'config/scripts'.freeze
def self.script_directory
SCRIPT_DIRECTORY
end
def generate_plc_objects
if professional_learning_course?
course = Course.find_by_name(professional_learning_course)
unless course
course = Course.new(name: professional_learning_course)
course.plc_course = Plc::Course.create!(course: course)
course.save!
end
unit = Plc::CourseUnit.find_or_initialize_by(script_id: id)
unit.update!(
plc_course_id: course.plc_course.id,
unit_name: I18n.t("data.script.name.#{name}.title"),
unit_description: I18n.t("data.script.name.#{name}.description")
)
stages.reload
stages.each do |stage|
lm = Plc::LearningModule.find_or_initialize_by(stage_id: stage.id)
lm.update!(
plc_course_unit_id: unit.id,
name: stage.name,
module_type: stage.flex_category.try(:downcase) || Plc::LearningModule::REQUIRED_MODULE,
plc_tasks: []
)
stage.script_levels.each do |sl|
task = Plc::Task.find_or_initialize_by(script_level_id: sl.id)
task.name = sl.level.name
task.save!
lm.plc_tasks << task
end
end
end
end
serialized_attrs %w(
hideable_stages
peer_reviews_to_complete
professional_learning_course
redirect_to
student_detail_progress_view
project_widget_visible
project_widget_types
exclude_csf_column_in_legend
teacher_resources
stage_extras_available
has_verified_resources
has_lesson_plan
curriculum_path
script_announcements
version_year
is_stable
supported_locales
pilot_experiment
)
def self.twenty_hour_script
Script.get_from_cache(Script::TWENTY_HOUR_NAME)
end
def self.hoc_2014_script
Script.get_from_cache(Script::HOC_NAME)
end
def self.starwars_script
Script.get_from_cache(Script::STARWARS_NAME)
end
def self.minecraft_script
Script.get_from_cache(Script::MINECRAFT_NAME)
end
def self.starwars_blocks_script
Script.get_from_cache(Script::STARWARS_BLOCKS_NAME)
end
def self.frozen_script
Script.get_from_cache(Script::FROZEN_NAME)
end
def self.course1_script
Script.get_from_cache(Script::COURSE1_NAME)
end
def self.course2_script
Script.get_from_cache(Script::COURSE2_NAME)
end
def self.course3_script
Script.get_from_cache(Script::COURSE3_NAME)
end
def self.course4_script
Script.get_from_cache(Script::COURSE4_NAME)
end
def self.infinity_script
Script.get_from_cache(Script::INFINITY_NAME)
end
def self.flappy_script
Script.get_from_cache(Script::FLAPPY_NAME)
end
def self.playlab_script
Script.get_from_cache(Script::PLAYLAB_NAME)
end
def self.artist_script
Script.get_from_cache(Script::ARTIST_NAME)
end
def self.stage_extras_script_ids
@@stage_extras_scripts ||= Script.all.select(&:stage_extras_available?).pluck(:id)
end
# Get the set of scripts that are valid for the current user, ignoring those
# that are hidden based on the user's permission.
# @param [User] user
# @return [Script[]]
def self.valid_scripts(user)
has_any_course_experiments = Course.has_any_course_experiments?(user)
with_hidden = !has_any_course_experiments && user.hidden_script_access?
scripts = with_hidden ? all_scripts : visible_scripts
if has_any_course_experiments
scripts = scripts.map do |script|
alternate_script = script.alternate_script(user)
alternate_script.presence || script
end
end
if !with_hidden && has_any_pilot_access?(user)
scripts = scripts.concat(all_scripts.select {|s| s.has_pilot_access?(user)})
end
scripts
end
class << self
private
def all_scripts
Rails.cache.fetch('valid_scripts/all') do
Script.all
end
end
def visible_scripts
Rails.cache.fetch('valid_scripts/valid') do
Script.all.reject(&:hidden)
end
end
end
# @param [User] user
# @param script_id [String] id of the script we're checking the validity of
# @return [Boolean] Whether this is a valid script ID
def self.valid_script_id?(user, script_id)
valid_scripts(user).any? {|script| script[:id] == script_id.to_i}
end
# @return [Array<Script>] An array of modern elementary scripts.
def self.modern_elementary_courses
Script::CATEGORIES[:csf].map {|name| Script.get_from_cache(name)}
end
# @param locale [String] An "xx-YY" locale string.
# @return [Boolean] Whether all the modern elementary courses are available in the given locale.
def self.modern_elementary_courses_available?(locale)
@modern_elementary_courses_available = modern_elementary_courses.all? do |script|
supported_languages = script.supported_locales || []
supported_languages.any? {|s| locale.casecmp?(s)}
end
end
def starting_level
raise "Script #{name} has no level to start at" if script_levels.empty?
candidate_level = script_levels.first.or_next_progression_level
raise "Script #{name} has no valid progression levels (non-unplugged) to start at" unless candidate_level
candidate_level
end
# Find the lockable or non-locakble stage based on its relative position.
# Raises `ActiveRecord::RecordNotFound` if no matching stage is found.
def stage_by_relative_position(position, lockable = false)
stages.where(lockable: lockable).find_by!(relative_position: position)
end
# For all scripts, cache all related information (levels, etc),
# indexed by both id and name. This is cached both in a class
# variable (ie. in memory in the worker process) and in a
# distributed cache (Rails.cache)
@@script_cache = nil
SCRIPT_CACHE_KEY = 'script-cache'.freeze
# Caching is disabled when editing scripts and levels or running unit tests.
def self.should_cache?
return false if Rails.application.config.levelbuilder_mode
return false unless Rails.application.config.cache_classes
return false if ENV['UNIT_TEST'] || ENV['CI']
true
end
def self.script_cache_to_cache
Rails.cache.write(SCRIPT_CACHE_KEY, script_cache_from_db)
end
def self.script_cache_from_cache
[
ScriptLevel, Level, Game, Concept, Callout, Video, Artist, Blockly, CourseScript
].each(&:new) # make sure all possible loaded objects are completely loaded
Rails.cache.read SCRIPT_CACHE_KEY
end
def self.script_cache_from_db
{}.tap do |cache|
Script.with_associated_models.find_each do |script|
cache[script.name] = script
cache[script.id.to_s] = script
end
end
end
def self.script_cache
return nil unless should_cache?
@@script_cache ||=
script_cache_from_cache || script_cache_from_db
end
# Returns a cached map from script level id to script_level, or nil if in level_builder mode
# which disables caching.
def self.script_level_cache
return nil unless should_cache?
@@script_level_cache ||= {}.tap do |cache|
script_cache.values.each do |script|
cache.merge!(script.script_levels.index_by(&:id))
end
end
end
# Returns a cached map from level id and level name to level, or nil if in
# level_builder mode which disables caching.
def self.level_cache
return nil unless should_cache?
@@level_cache ||= {}.tap do |cache|
script_level_cache.values.each do |script_level|
level = script_level.level
next unless level
cache[level.id] = level unless cache.key? level.id
cache[level.name] = level unless cache.key? level.name
end
end
end
# Find the script level with the given id from the cache, unless the level build mode
# is enabled in which case it is always fetched from the database. If we need to fetch
# the script and we're not in level mode (for example because the script was created after
# the cache), then an entry for the script is added to the cache.
def self.cache_find_script_level(script_level_id)
script_level = script_level_cache[script_level_id] if should_cache?
# If the cache missed or we're in levelbuilder mode, fetch the script level from the db.
if script_level.nil?
script_level = ScriptLevel.find(script_level_id)
# Cache the script level, unless it wasn't found.
@@script_level_cache[script_level_id] = script_level if script_level && should_cache?
end
script_level
end
# Find the level with the given id or name from the cache, unless the level
# build mode is enabled in which case it is always fetched from the database.
# If we need to fetch the level and we're not in level mode (for example
# because the level was created after the cache), then an entry for the level
# is added to the cache.
# @param level_identifier [Integer | String] the level ID or level name to
# fetch
# @return [Level] the (possibly cached) level
# @raises [ActiveRecord::RecordNotFound] if the level cannot be found
def self.cache_find_level(level_identifier)
level = level_cache[level_identifier] if should_cache?
return level unless level.nil?
# If the cache missed or we're in levelbuilder mode, fetch the level from
# the db. Note the field trickery is to allow passing an ID as a string,
# which some tests rely on (unsure about non-tests).
field = level_identifier.to_i.to_s == level_identifier.to_s ? :id : :name
level = Level.find_by!(field => level_identifier)
# Cache the level by ID and by name, unless it wasn't found.
@@level_cache[level.id] = level if level && should_cache?
@@level_cache[level.name] = level if level && should_cache?
level
end
def cached
self.class.get_from_cache(id)
end
def self.get_without_cache(id_or_name, version_year: nil)
# Also serve any script by its new_name, if it has one.
script = id_or_name && Script.find_by(new_name: id_or_name)
return script if script
# a bit of trickery so we support both ids which are numbers and
# names which are strings that may contain numbers (eg. 2-3)
is_id = id_or_name.to_i.to_s == id_or_name.to_s
find_by = is_id ? :id : :name
script = Script.with_associated_models.find_by(find_by => id_or_name)
return script if script
unless is_id
# We didn't find a script matching id_or_name. Next, look for a script
# in the id_or_name script family to redirect to, e.g. csp1 --> csp1-2017.
script_with_redirect = Script.get_script_family_redirect(id_or_name, version_year: version_year)
return script_with_redirect if script_with_redirect
end
raise ActiveRecord::RecordNotFound.new("Couldn't find Script with id|name|family_name=#{id_or_name}")
end
# Returns the script with the specified id, or a script with the specified
# name, or a redirect to the latest version of the script within the specified
# family. Also populates the script cache so that future responses will be cached.
# For example:
# get_from_cache('11') --> script_cache['11'] = <Script id=11, name=...>
# get_from_cache('frozen') --> script_cache['frozen'] = <Script name="frozen", id=...>
# get_from_cache('csp1') --> script_cache['csp1'] = <Script redirect_to="csp1-2018">
# get_from_cache('csp1', version_year: '2017') --> script_cache['csp1/2017'] = <Script redirect_to="csp1-2017">
#
# @param id_or_name [String|Integer] script id, script name, or script family name.
# @param version_year [String] If specified, when looking for a script to redirect
# to within a script family, redirect to this version rather than the latest.
def self.get_from_cache(id_or_name, version_year: nil)
return get_without_cache(id_or_name, version_year: version_year) unless should_cache?
cache_key_suffix = version_year ? "/#{version_year}" : ''
cache_key = "#{id_or_name}#{cache_key_suffix}"
script_cache.fetch(cache_key) do
# Populate cache on miss.
script_cache[cache_key] = get_without_cache(id_or_name, version_year: version_year)
end
end
# Given a script family name, return a dummy Script with redirect_to field
# pointing toward the latest stable script in that family, or to a specific
# version_year if one is specified.
# @param family_name [String] The name of the script family to search in.
# @param version_year [String] Version year to return. Optional.
# @return [Script|nil] A dummy script object, not persisted to the database,
# with only the redirect_to field set.
def self.get_script_family_redirect(family_name, version_year: nil)
script_name = Script.latest_stable_version(family_name, version_year: version_year).try(:name)
script_name ? Script.new(redirect_to: script_name) : nil
end
# @param user [User]
# @param locale [String] User or request locale. Optional.
# @return [String|nil] URL to the script overview page the user should be redirected to (if any).
def redirect_to_script_url(user, locale: nil)
# No redirect unless script belongs to a family.
return nil unless family_name
# Only redirect students.
return nil unless user && user.student?
# No redirect unless user is allowed to view this script version and they are not already assigned to this script
# or the course it belongs to.
return nil unless can_view_version?(user, locale: locale) && !user.assigned_script?(self)
# No redirect if script or its course are not versioned.
current_version_year = version_year || course&.version_year
return nil unless current_version_year.present?
# Redirect user to the latest assigned script in this family,
# if one exists and it is newer than the current script.
latest_assigned_version = Script.latest_assigned_version(family_name, user)
latest_assigned_version_year = latest_assigned_version&.version_year || latest_assigned_version&.course&.version_year
return nil unless latest_assigned_version_year && latest_assigned_version_year > current_version_year
latest_assigned_version.link
end
def link
Rails.application.routes.url_helpers.script_path(self)
end
# @param user [User]
# @param locale [String] User or request locale. Optional.
# @return [Boolean] Whether the user can view the script.
def can_view_version?(user, locale: nil)
# Users can view any course not in a family.
return true unless family_name
latest_stable_version = Script.latest_stable_version(family_name)
latest_stable_version_in_locale = Script.latest_stable_version(family_name, locale: locale)
is_latest = latest_stable_version == self || latest_stable_version_in_locale == self
# All users can see the latest script version in English and in their locale.
return true if is_latest
# Restrictions only apply to students and logged out users.
return false if user.nil?
return true unless user.student?
# A student can view the script version if they have progress in it or the course it belongs to.
has_progress = user.scripts.include?(self) || course&.has_progress?(user)
return true if has_progress
# A student can view the script version if they are assigned to it.
user.assigned_script?(self)
end
# @param family_name [String] The family name for a script family.
# @param version_year [String] Version year to return. Optional.
# @param locale [String] User or request locale. Optional.
# @return [Script|nil] Returns the latest version in a script family.
def self.latest_stable_version(family_name, version_year: nil, locale: 'en-us')
return nil unless family_name.present?
script_versions = Script.
where(family_name: family_name).
order("properties -> '$.version_year' DESC")
# Only select stable, supported scripts (ignore supported locales if locale is an English-speaking locale).
# Match on version year if one is supplied.
locale_str = locale&.to_s
supported_stable_scripts = script_versions.select do |script|
is_supported = script.supported_locales&.include?(locale_str) || locale_str&.start_with?('en')
if version_year
script.is_stable && is_supported && script.version_year == version_year
else
script.is_stable && is_supported
end
end
supported_stable_scripts&.first
end
# @param family_name [String] The family name for a script family.
# @param user [User]
# @return [Script|nil] Returns the latest version in a family that the user is assigned to.
def self.latest_assigned_version(family_name, user)
return nil unless family_name && user
assigned_script_ids = user.section_scripts.pluck(:id)
Script.
# select only scripts assigned to this user.
where(id: assigned_script_ids).
# select only scripts in the same family.
where(family_name: family_name).
# order by version year descending.
order("properties -> '$.version_year' DESC")&.
first
end
def text_response_levels
return @text_response_levels if Script.should_cache? && @text_response_levels
@text_response_levels = text_response_levels_without_cache
end
def text_response_levels_without_cache
text_response_levels = []
script_levels.map do |script_level|
script_level.levels.map do |level|
next if level.contained_levels.empty? ||
!TEXT_RESPONSE_TYPES.include?(level.contained_levels.first.class)
text_response_levels << {
script_level: script_level,
levels: [level.contained_levels.first]
}
end
end
text_response_levels.concat(
script_levels.includes(:levels).
where('levels.type' => TEXT_RESPONSE_TYPES).
map do |script_level|
{
script_level: script_level,
levels: script_level.levels
}
end
)
text_response_levels
end
def to_param
name
end
# Legacy levels have different video and title logic in LevelsHelper.
def legacy_curriculum?
[TWENTY_HOUR_NAME, HOC_2013_NAME, EDIT_CODE_NAME, TWENTY_FOURTEEN_NAME, FLAPPY_NAME, JIGSAW_NAME].include? name
end
def twenty_hour?
ScriptConstants.script_in_category?(:twenty_hour, name)
end
def hoc?
ScriptConstants.script_in_category?(:hoc, name)
end
def flappy?
ScriptConstants.script_in_category?(:flappy, name)
end
def minecraft?
ScriptConstants.script_in_category?(:minecraft, name)
end
def k5_draft_course?
ScriptConstants.script_in_category?(:csf2_draft, name)
end
def csf_international?
ScriptConstants.script_in_category?(:csf_international, name)
end
def k5_course?
(
Script::CATEGORIES[:csf_international] +
Script::CATEGORIES[:csf] +
Script::CATEGORIES[:csf_2018]
).include? name
end
def csf?
k5_course? || twenty_hour?
end
def cs_in_a?
name.match(Regexp.union('algebra', 'Algebra'))
end
def k1?
[
Script::COURSEA_DRAFT_NAME,
Script::COURSEB_DRAFT_NAME,
Script::COURSEA_NAME,
Script::COURSEB_NAME,
Script::COURSE1_NAME
].include?(name)
end
def localize_long_instructions?
# Don't ever show non-English markdown instructions for Course 1 - 4, the
# 20-hour course, or the pre-2017 minecraft courses.
!(
csf_international? ||
twenty_hour? ||
[
ScriptConstants::MINECRAFT_NAME,
ScriptConstants::MINECRAFT_DESIGNER_NAME
].include?(name)
)
end
def beta?
Script.beta? name
end
def self.beta?(name)
name == Script::EDIT_CODE_NAME || ScriptConstants.script_in_category?(:csf2_draft, name)
end
def get_script_level_by_id(script_level_id)
script_levels.find {|sl| sl.id == script_level_id.to_i}
end
def get_script_level_by_relative_position_and_puzzle_position(relative_position, puzzle_position, lockable)
relative_position ||= 1
script_levels.to_a.find do |sl|
sl.stage.lockable? == lockable &&
sl.stage.relative_position == relative_position.to_i &&
sl.position == puzzle_position.to_i &&
!sl.bonus
end
end
def get_script_level_by_chapter(chapter)
chapter = chapter.to_i
return nil if chapter < 1 || chapter > script_levels.to_a.size
script_levels[chapter - 1] # order is by chapter
end
def get_bonus_script_levels(current_stage)
unless @all_bonus_script_levels
@all_bonus_script_levels = stages.map do |stage|
{
stageNumber: stage.relative_position,
levels: stage.script_levels.select(&:bonus).map(&:summarize_as_bonus)
}
end
@all_bonus_script_levels.select! {|stage| stage[:levels].any?}
end
@all_bonus_script_levels.select {|stage| stage[:stageNumber] <= current_stage.absolute_position}
end
private def csf_tts_level?
k5_course?
end
private def csd_tts_level?
[
Script::CSD2_NAME,
Script::CSD3_NAME,
Script::CSD4_NAME,
Script::CSD6_NAME,
Script::CSD2_2018_NAME,
Script::CSD3_2018_NAME,
Script::CSD4_2018_NAME,
Script::CSD6_2018_NAME,
].include?(name)
end
private def csp_tts_level?
[
Script::CSP17_UNIT3_NAME,
Script::CSP17_UNIT5_NAME,
Script::CSP17_POSTAP_NAME,
Script::CSP3_2018_NAME,
Script::CSP5_2018_NAME,
Script::CSP_POSTAP_2018_NAME,
].include?(name)
end
private def hoc_tts_level?
[
Script::APPLAB_INTRO,
Script::DANCE_PARTY_NAME,
Script::DANCE_PARTY_EXTRAS_NAME,
Script::ARTIST_NAME,
Script::SPORTS_NAME,
Script::BASKETBALL_NAME
].include?(name)
end
def text_to_speech_enabled?
csf_tts_level? || csd_tts_level? || csp_tts_level? || hoc_tts_level? || name == Script::TTS_NAME
end
def hint_prompt_enabled?
[
Script::COURSE2_NAME,
Script::COURSE3_NAME,
Script::COURSE4_NAME
].include?(name)
end
def hide_solutions?
name == 'algebra'
end
def banner_image
if has_banner?
"banner_#{name}.jpg"
end
end
def has_lesson_pdf?
return false if ScriptConstants.script_in_category?(:csf, name) || ScriptConstants.script_in_category?(:csf_2018, name)
has_lesson_plan?
end
def has_banner?
# Temporarily remove Course A-F banner (wrong size) - Josh L.
return false if ScriptConstants.script_in_category?(:csf, name) || ScriptConstants.script_in_category?(:csf_2018, name)
k5_course? || [
Script::CSP17_UNIT1_NAME,
Script::CSP17_UNIT2_NAME,
Script::CSP17_UNIT3_NAME,
Script::CSP_UNIT1_NAME,
Script::CSP_UNIT2_NAME,
Script::CSP_UNIT3_NAME,
].include?(name)
end
def freeplay_links
if cs_in_a?
['calc', 'eval']
else
[]
end
end
def has_peer_reviews?
peer_reviews_to_complete.try(:>, 0)
end
# Is age 13+ required for logged out users
# @return {bool}
def logged_out_age_13_required?
return false if login_required
# hard code some exceptions. ideally we'd get rid of these and just make our
# UI tests deal with the 13+ requirement
return false if %w(allthethings allthehiddenthings allthettsthings).include?(name)
script_levels.any? {|script_level| script_level.levels.any?(&:age_13_required?)}
end
# @param user [User]
# @return [Boolean] Whether the user has progress on another version of this script.
def has_older_version_progress?(user)
return nil unless user && family_name && version_year
user_script_ids = user.user_scripts.pluck(:script_id)
Script.
# select only scripts in the same script family.
where(family_name: family_name).
# select only older versions.
where("properties -> '$.version_year' < ?", version_year).
# exclude the current script.
where.not(id: id).
# select only scripts which the user has progress in.
where(id: user_script_ids).
count > 0
end
# Create or update any scripts, script levels and stages specified in the
# script file definitions. If new_suffix is specified, create a copy of the
# script and any associated levels, appending new_suffix to the name when
# copying. Any new_properties are merged into the properties of the new script.
def self.setup(custom_files, new_suffix: nil, new_properties: {})
transaction do
scripts_to_add = []
custom_i18n = {}
# Load custom scripts from Script DSL format
custom_files.map do |script|
name = File.basename(script, '.script')
base_name = Script.base_name(name)
name = "#{base_name}-#{new_suffix}" if new_suffix
script_data, i18n = ScriptDSL.parse_file(script, name)
stages = script_data[:stages]
custom_i18n.deep_merge!(i18n)
# TODO: below is duplicated in update_text. and maybe can be refactored to pass script_data?
scripts_to_add << [{
id: script_data[:id],
name: name,
hidden: script_data[:hidden].nil? ? true : script_data[:hidden], # default true
login_required: script_data[:login_required].nil? ? false : script_data[:login_required], # default false
wrapup_video: script_data[:wrapup_video],
new_name: script_data[:new_name],
family_name: script_data[:family_name],
properties: Script.build_property_hash(script_data).merge(new_properties)
}, stages]
end
# Stable sort by ID then add each script, ensuring scripts with no ID end up at the end
added_scripts = scripts_to_add.sort_by.with_index {|args, idx| [args[0][:id] || Float::INFINITY, idx]}.map do |options, raw_stages|
add_script(options, raw_stages, new_suffix: new_suffix)
end
[added_scripts, custom_i18n]
end
end
# if new_suffix is specified, copy the script, hide it, and copy all its
# levelbuilder-defined levels.
def self.add_script(options, raw_stages, new_suffix: nil)
raw_script_levels = raw_stages.map {|stage| stage[:scriptlevels]}.flatten
script = fetch_script(options)
script.update!(hidden: true) if new_suffix
chapter = 0
stage_position = 0; script_level_position = Hash.new(0)
script_stages = []
script_levels_by_stage = {}
levels_by_key = script.levels.index_by(&:key)
lockable_count = 0
non_lockable_count = 0
# Overwrites current script levels
script.script_levels = raw_script_levels.map do |raw_script_level|
raw_script_level.symbolize_keys!
assessment = nil
named_level = nil
bonus = nil
stage_flex_category = nil
stage_lockable = nil
levels = raw_script_level[:levels].map do |raw_level|
raw_level.symbolize_keys!
# Concepts are comma-separated, indexed by name
raw_level[:concept_ids] = (concepts = raw_level.delete(:concepts)) && concepts.split(',').map(&:strip).map do |concept_name|
(Concept.by_name(concept_name) || raise("missing concept '#{concept_name}'"))
end
raw_level_data = raw_level.dup
assessment = raw_level.delete(:assessment)
named_level = raw_level.delete(:named_level)
bonus = raw_level.delete(:bonus)
stage_flex_category = raw_level.delete(:stage_flex_category)
stage_lockable = !!raw_level.delete(:stage_lockable)
key = raw_level.delete(:name)
if raw_level[:level_num] && !key.starts_with?('blockly')
# a levels.js level in a old style script -- give it the same key that we use for levels.js levels in new style scripts
key = ['blockly', raw_level.delete(:game), raw_level.delete(:level_num)].join(':')
end
level =
if new_suffix && !key.starts_with?('blockly')
Level.find_by_name(key).clone_with_suffix("_#{new_suffix}")
else
levels_by_key[key] || Level.find_by_key(key)
end
if key.starts_with?('blockly')
# this level is defined in levels.js. find/create the reference to this level
level = Level.
create_with(name: 'blockly').
find_or_create_by!(Level.key_to_params(key))
level = level.with_type(raw_level.delete(:type) || 'Blockly') if level.type.nil?
if level.video_key && !raw_level[:video_key]
raw_level[:video_key] = nil
end
level.update(raw_level)
elsif raw_level[:video_key]
level.update(video_key: raw_level[:video_key])
end
unless level
raise ActiveRecord::RecordNotFound, "Level: #{raw_level_data.to_json}, Script: #{script.name}"
end
level
end
stage_name = raw_script_level.delete(:stage)
properties = raw_script_level.delete(:properties) || {}
if new_suffix && properties[:variants]
properties[:variants] = properties[:variants].map do |old_level_name, value|
["#{old_level_name}_#{new_suffix}", value]
end.to_h
end
script_level_attributes = {
script_id: script.id,
chapter: (chapter += 1),
named_level: named_level,
bonus: bonus,
assessment: assessment
}
script_level_attributes[:properties] = properties.with_indifferent_access
script_level = script.script_levels.detect do |sl|
script_level_attributes.all? {|k, v| sl.send(k) == v} &&
sl.levels == levels
end || ScriptLevel.create!(script_level_attributes) do |sl|
sl.levels = levels
end
# Set/create Stage containing custom ScriptLevel
if stage_name
stage = script.stages.detect {|s| s.name == stage_name} ||
Stage.find_or_create_by(
name: stage_name,
script: script,
) do |s|
s.relative_position = 0 # will be updated below, but cant be null
end
stage.assign_attributes(flex_category: stage_flex_category, lockable: stage_lockable)
stage.save! if stage.changed?
script_level_attributes[:stage_id] = stage.id
script_level_attributes[:position] = (script_level_position[stage.id] += 1)
script_level.reload
script_level.assign_attributes(script_level_attributes)
script_level.save! if script_level.changed?
(script_levels_by_stage[stage.id] ||= []) << script_level
unless script_stages.include?(stage)
if stage_lockable
stage.assign_attributes(relative_position: (lockable_count += 1))
else
stage.assign_attributes(relative_position: (non_lockable_count += 1))
end
stage.assign_attributes(absolute_position: (stage_position += 1))
stage.save! if stage.changed?
script_stages << stage
end
end
script_level.assign_attributes(script_level_attributes)
script_level.save! if script_level.changed?
script_level
end
script_stages.each do |stage|
# make sure we have an up to date view
stage.reload
stage.script_levels = script_levels_by_stage[stage.id]
# Go through all the script levels for this stage, 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 stage can be a multi-page
# assessment.
stage.script_levels.each do |script_level|
if !script_level.end_of_stage? && script_level.long_assessment?
raise "Only the final level in a stage may be a multi-page assessment. Script: #{script.name}"