<commit_before># encoding: utf-8

namespace :fix do

  #Usage
  #Development:   bundle exec rake fix:resetScormTimestamps
  #In production: bundle exec rake fix:resetScormTimestamps RAILS_ENV=production
  task :resetScormTimestamps => :environment do
    printTitle("Reset SCORM timestamps")

    Excursion.all.map { |ex| 
      ex.update_column :scorm2004_timestamp, nil
      ex.update_column :scorm12_timestamp, nil
    }

    printTitle("Task Finished")
  end

  #Usage
  #Development:   bundle exec rake fix:fillExcursionsLanguage
  #In production: bundle exec rake fix:fillExcursionsLanguage RAILS_ENV=production
  task :fillExcursionsLanguage => :environment do

    printTitle("Filling Excursions language")

    validLanguageCodes = ["de","en","es","fr","it","pt","ru"]
    #"ot" is for "other"

    Excursion.all.map { |ex|
      eJson = JSON(ex.json)

      lan = eJson["language"]
      emptyLan = (lan.nil? or !lan.is_a? String or lan=="independent")

      if emptyLan and !Vish::Application.config.APP_CONFIG["languageDetectionAPIKEY"].nil?
        #Try to infer language
        #Use https://github.com/detectlanguage/detect_language gem

        stringToTestLanguage = ""
        if ex.title.is_a? String and !ex.title.blank?
          stringToTestLanguage = stringToTestLanguage + ex.title + " "
        end
        if ex.description.is_a? String and !ex.description.blank?
          stringToTestLanguage = stringToTestLanguage + ex.description + " "
        end

        if stringToTestLanguage.is_a? String and !stringToTestLanguage.blank?
          detectionResult = (DetectLanguage.detect(stringToTestLanguage) rescue [])
          detectionResult.each do |result|
            if result["isReliable"] == true
              detectedLanguageCode = result["language"]
              if validLanguageCodes.include? detectedLanguageCode
                lan = detectedLanguageCode
              else
                lan = "ot"
              end
              emptyLan = false
              break
            end
          end
        end
      end

      if !emptyLan
        ao = ex.activity_object
        if ao.language != lan
          ao.update_column :language, lan
        end

        if eJson["language"] != lan
          eJson["language"] = lan
          ex.update_column :json, eJson.to_json
        end
      end

    }

    printTitle("Task Finished")
  end

  #Usage
  #Development:   bundle exec rake fix:updateScormPackages
  #In production: bundle exec rake fix:updateScormPackages RAILS_ENV=production
  task :updateScormPackages => :environment do
    printTitle("Updating SCORM Packages")
    Scormfile.record_timestamps=false
    ActivityObject.record_timestamps=false

    Scormfile.all.each do |scormfile|
      begin
        scormfile.updateScormfile
      rescue Exception => e
        puts "Exception in Scormfile with id '" + scormfile.id.to_s + "'. Exception message: " + e.message
      end
    end

    Rake::Task["fix:resetScormTimestamps"].invoke

    Scormfile.record_timestamps=true
    ActivityObject.record_timestamps=true
    printTitle("Task finished")
  end

  #Usage
  #Development:   bundle exec rake fix:updateImscPackages
  #In production: bundle exec rake fix:updateImscPackages RAILS_ENV=production
  task :updateImscPackages => :environment do
    printTitle("Updating IMS Content Packages")
    Imscpfile.record_timestamps=false
    ActivityObject.record_timestamps=false

    Imscpfile.all.each do |imscpfile|
      begin
        imscpfile.updateImscpfile
      rescue Exception => e
        puts "Exception in Imscpfile with id '" + imscpfile.id.to_s + "'. Exception message: " + e.message
      end
    end

    Imscpfile.record_timestamps=true
    ActivityObject.record_timestamps=true
    printTitle("Task finished")
  end

  #Usage
  #Development:   bundle exec rake fix:updateWebapps
  #In production: bundle exec rake fix:updateWebapps RAILS_ENV=production
  task :updateWebapps => :environment do
    printTitle("Updating Web Applications")
    Webapp.record_timestamps=false
    ActivityObject.record_timestamps=false

    Webapp.all.each do |webapp|
      begin
        webapp.updateWebapp
      rescue Exception => e
        puts "Exception in Webapp with id '" + webapp.id.to_s + "'. Exception message: " + e.message
      end
    end

    Webapp.record_timestamps=true
    ActivityObject.record_timestamps=true
    printTitle("Task finished")
  end

  #Usage
  #Development:   bundle exec rake fix:codeResourcesExcursions
  #In production: bundle exec rake fix:codeResourcesExcursions RAILS_ENV=production
  task :codeResourcesExcursions => :environment do

    printTitle("Fixing code resources in excursions")

    #Get all excursions
    excursions = Excursion.all
    excursions.each do |excursion|
      begin
        jsonChange = false
        eJson = JSON(excursion.json)
        eJson["slides"].each do |slide|
          sElements = slide["elements"]
          if sElements != nil
            sElements.each do |el|
              if el["type"]=="object" and el["body"].class == String and el["body"].include?("vishubcode.global.dit.upm.es") and el["body"].include?("</iframe>")
                newBody = el["body"].gsub("vishubcode.global.dit.upm.es",Vish::Application.config.APP_CONFIG['code_domain'])
                el["body"] = newBody 
                jsonChange = true
              end
            end
          end
        end
        if jsonChange<commit_msg>More fixes for code resources<commit_after># encoding: utf-8

namespace :fix do

  #Usage
  #Development:   bundle exec rake fix:resetScormTimestamps
  #In production: bundle exec rake fix:resetScormTimestamps RAILS_ENV=production
  task :resetScormTimestamps => :environment do
    printTitle("Reset SCORM timestamps")

    Excursion.all.map { |ex| 
      ex.update_column :scorm2004_timestamp, nil
      ex.update_column :scorm12_timestamp, nil
    }

    printTitle("Task Finished")
  end

  #Usage
  #Development:   bundle exec rake fix:fillExcursionsLanguage
  #In production: bundle exec rake fix:fillExcursionsLanguage RAILS_ENV=production
  task :fillExcursionsLanguage => :environment do

    printTitle("Filling Excursions language")

    validLanguageCodes = ["de","en","es","fr","it","pt","ru"]
    #"ot" is for "other"

    Excursion.all.map { |ex|
      eJson = JSON(ex.json)

      lan = eJson["language"]
      emptyLan = (lan.nil? or !lan.is_a? String or lan=="independent")

      if emptyLan and !Vish::Application.config.APP_CONFIG["languageDetectionAPIKEY"].nil?
        #Try to infer language
        #Use https://github.com/detectlanguage/detect_language gem

        stringToTestLanguage = ""
        if ex.title.is_a? String and !ex.title.blank?
          stringToTestLanguage = stringToTestLanguage + ex.title + " "
        end
        if ex.description.is_a? String and !ex.description.blank?
          stringToTestLanguage = stringToTestLanguage + ex.description + " "
        end

        if stringToTestLanguage.is_a? String and !stringToTestLanguage.blank?
          detectionResult = (DetectLanguage.detect(stringToTestLanguage) rescue [])
          detectionResult.each do |result|
            if result["isReliable"] == true
              detectedLanguageCode = result["language"]
              if validLanguageCodes.include? detectedLanguageCode
                lan = detectedLanguageCode
              else
                lan = "ot"
              end
              emptyLan = false
              break
            end
          end
        end
      end

      if !emptyLan
        ao = ex.activity_object
        if ao.language != lan
          ao.update_column :language, lan
        end

        if eJson["language"] != lan
          eJson["language"] = lan
          ex.update_column :json, eJson.to_json
        end
      end

    }

    printTitle("Task Finished")
  end

  #Usage
  #Development:   bundle exec rake fix:updateScormPackages
  #In production: bundle exec rake fix:updateScormPackages RAILS_ENV=production
  task :updateScormPackages => :environment do
    printTitle("Updating SCORM Packages")
    Scormfile.record_timestamps=false
    ActivityObject.record_timestamps=false

    Scormfile.all.each do |scormfile|
      begin
        scormfile.updateScormfile
      rescue Exception => e
        puts "Exception in Scormfile with id '" + scormfile.id.to_s + "'. Exception message: " + e.message
      end
    end

    Rake::Task["fix:resetScormTimestamps"].invoke

    Scormfile.record_timestamps=true
    ActivityObject.record_timestamps=true
    printTitle("Task finished")
  end

  #Usage
  #Development:   bundle exec rake fix:updateImscPackages
  #In production: bundle exec rake fix:updateImscPackages RAILS_ENV=production
  task :updateImscPackages => :environment do
    printTitle("Updating IMS Content Packages")
    Imscpfile.record_timestamps=false
    ActivityObject.record_timestamps=false

    Imscpfile.all.each do |imscpfile|
      begin
        imscpfile.updateImscpfile
      rescue Exception => e
        puts "Exception in Imscpfile with id '" + imscpfile.id.to_s + "'. Exception message: " + e.message
      end
    end

    Imscpfile.record_timestamps=true
    ActivityObject.record_timestamps=true
    printTitle("Task finished")
  end

  #Usage
  #Development:   bundle exec rake fix:updateWebapps
  #In production: bundle exec rake fix:updateWebapps RAILS_ENV=production
  task :updateWebapps => :environment do
    printTitle("Updating Web Applications")
    Webapp.record_timestamps=false
    ActivityObject.record_timestamps=false

    Webapp.all.each do |webapp|
      begin
        webapp.updateWebapp
      rescue Exception => e
        puts "Exception in Webapp with id '" + webapp.id.to_s + "'. Exception message: " + e.message
      end
    end

    Webapp.record_timestamps=true
    ActivityObject.record_timestamps=true
    printTitle("Task finished")
  end

  #Usage
  #Development:   bundle exec rake fix:codeResourcesExcursions
  #In production: bundle exec rake fix:codeResourcesExcursions RAILS_ENV=production
  task :codeResourcesExcursions => :environment do

    printTitle("Fixing code resources in excursions")

    #Get all excursions
    excursions = Excursion.all
    excursions.each do |excursion|
      begin
        jsonChange = false
        eJson = JSON(excursion.json)
        eJson["slides"].each do |slide|
          sElements = []
          if slide["type"] == "standard"
            sElements << slide["elements"]
          else
            if slide["slides"].is_a? Array
              slide["slides"].each do |subslide|
                sElements << subslide["elements"]
              end
            end
          end
          sElements.each do |elArray|
            elArray.each do |el|
              if el["type"]=="object" and el["body"].class == String and el["body"].include?("vishubcode.global.dit.upm.es") and el["body"].include?("</iframe>")
                newBody = el["body"].gsub("vishubcode.global.dit.upm.es",Vish::Application.config.APP_CONFIG['code_domain'])
                el["body"] = newBody 
                jsonChange = true
              end
            end
          end
        end
        if jsonChange
{'fraction_non_alphanumeric': 0.09035714285714286, 'fraction_numerical': 0.00125, 'mean_word_length': 2.6857518920697596, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7275875', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3265, 'n_words': 763}