Skip to content

Add resources to collections lessons that were missing them - #51359

Merged
bethanyaconnor merged 1 commit into
stagingfrom
bethany/fix-collections-resources-take-2
Apr 14, 2023
Merged

Add resources to collections lessons that were missing them#51359
bethanyaconnor merged 1 commit into
stagingfrom
bethany/fix-collections-resources-take-2

Conversation

@bethanyaconnor

@bethanyaconnor bethanyaconnor commented Apr 14, 2023

Copy link
Copy Markdown
Contributor

Some lessons in collections were missing resources. Very long conversation here. I wrote a script to copy all of the resources and vocabulary over from the units in the CSD courses to their respective lessons in collections. Dan and Angelina did a spot check of pairs of lessons I used and gave it a 👍

I'd love a quick review to make sure I'm only affecting resources, lessons_resources, vocabularies, and lesson_vocabularies (and serialized_at). Anything else would be unintentional.

Note: some of the resource markdown didn't get updated correctly here even though I tried to update it. This should be solved by https://codedotorg.atlassian.net/browse/TEACH-395, so I elected to not worry about it here.

Code used to do copy these over. This looks like a lot but it's mostly copied from Lesson.copy_to_unit

#!/usr/bin/env ruby

require_relative '../../deployment'
raise unless [:development, :adhoc, :levelbuilder].include? rack_env

# Wait until after initial error checking before loading the rails environment.
def require_rails_env
  puts "loading rails environment..."
  start_time = Time.now
  require_relative '../../dashboard/config/environment'
  puts "rails environment loaded in #{(Time.now - start_time).to_i} seconds."
end

require_rails_env

include Rails.application.routes.url_helpers
MARKDOWN_FIELDS = %w(overview student_overview preparation assessment_opportunities purpose)

csd_collections = UnitGroup.where(name: ['focus-on-data-2023', 'focus-on-creativity-2023', 'focus-on-hardware-2023', 'focus-on-design-with-purpose-2023', 'focus-on-coding-2023', 'focus-on-impact-in-society-2023'])
puts csd_collections.count
puts csd_collections.map(&:name).inspect
csd_course_lessons = UnitGroup.find_by_name('csd-2023').default_units.map(&:lessons).flatten

lesson_pairs = []
csd_collections.each do |coll|
  coll.default_units.each do |u|
    should_write_script_json = false
    u.lessons.each do |l|
      next if l.name ==  "Post-Project Test"
      course_lesson = csd_course_lessons.find {|cl| l.name == cl.name }
      next unless course_lesson
      next unless l.resources.empty?
      next if course_lesson.resources.empty?
      lesson_pairs += ["levelbuilder-studio.code.org#{script_lesson_path(l.script, l)},levelbuilder-studio.code.org#{script_lesson_path(course_lesson.script, course_lesson)}"]

      should_write_script_json = true
      course_version = l.script.get_course_version

      copied_resource_map = {}
      l.resources = course_lesson.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 = {}
      l.vocabularies = course_lesson.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 l.try(field)
        Services::MarkdownPreprocessor.sub_resource_links!(l.try(field), update_resource_link_on_clone)
        Services::MarkdownPreprocessor.sub_vocab_definitions!(l.try(field), update_vocab_definition_on_clone)
      end

      l.save!
    end
    u.write_script_json if should_write_script_json
  end
end

lesson_pairs.each do |pair|
  puts pair
end

@bethanyaconnor
bethanyaconnor marked this pull request as ready for review April 14, 2023 21:03
@bethanyaconnor
bethanyaconnor requested a review from a team April 14, 2023 21:03

@hannahbergam hannahbergam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Spot checked several of the diffs and ✅

}
}
],
"lessons_resources": [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that this is represented later!

"scripts_student_resources": [

],
"vocabularies": [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for checking these too- I'm convinced they're all properly accounted for!

@bethanyaconnor
bethanyaconnor merged commit 67c788f into staging Apr 14, 2023
@bethanyaconnor
bethanyaconnor deleted the bethany/fix-collections-resources-take-2 branch April 14, 2023 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants