<commit_before>module GoogleIntegration::Classroom::Parsers::Courses


=begin
example JSON.parse(response.body) :

{"id"=>"5169992618", "name"=>"archive test", "descriptionHeading"=>"archive test", "ownerId"=>"112188393285935083024", "creationTime"=>"2017-03-16T19:09:15.524Z", "updateTime"=>"2017-03-16T19:10:21.493Z", "enrollmentCode"=>"csaous", "courseState"=>"ARCHIVED", "alternateLink"=>"http://classroom.google.com/c/NTE2OTk5MjYxOFpa", "teacherGroupEmail"=>"<EMAIL>", "courseGroupEmail"=>"<EMAIL>", "teacherFolder"=>{"id"=>"<KEY>", "title"=>"archive test", "alternateLink"=>"https://drive.google.com/drive/folders/<KEY>"}, "guardiansEnabled"=>false}

=end

  def self.run(user, course_response)
    if user.role == 'teacher'
      self.parse_courses_for_teacher(course_response, user)
    else
      self.parse_courses_for_student(course_response, user)
    end
  end

  private

  def self.parse_courses_for_teacher(course_response, user)
    courses = []
    if course_response[:courses] && course_response[:courses].any?
      existing_google_classroom_ids = self.existing_google_classroom_ids(user)
      course_response[:courses].each do |course|
        alreadyImported = self.already_imported?(course, existing_google_classroom_ids)
        if alreadyImported
          grade = Classroom.where(google_classroom_id: course[:id], teacher_id: user.id).first.grade
        end
        if self.valid?(course, user, existing_google_classroom_ids)
          name = course[:section] ? "#{course[:name]} #{course[:section]}" : course[:name]
          courses << {id: course[:id].to_i, name: name,
            ownerId: course[:ownerId],
            section: course[:section],
            alreadyImported: alreadyImported,
            grade: grade,
            creationTime: course[:creationTime],}
        end
      end
    end
    courses
  end

  def self.parse_courses_for_student(course_response, user)
    course_ids = []
    if course_response[:courses].any?
      # checking to make sure student is not the owner (teacher) of the course
      course_response[:courses].select{ |c| !own_course(c, user) }.each do |course|
        course_ids << course[:id]
      end
    end
    course_ids
  end

  def self.existing_google_classroom_ids(user)
    User.find(user.id).google_classrooms.map(&:google_classroom_id)
  end

  def self.valid?(course, user, existing_google_classroom_ids)
    # really we should be calling google classrooms api to see if this user is a teacher,<commit_msg>Fix calling any on nil.<commit_after>module GoogleIntegration::Classroom::Parsers::Courses


=begin
example JSON.parse(response.body) :

{"id"=>"5169992618", "name"=>"archive test", "descriptionHeading"=>"archive test", "ownerId"=>"112188393285935083024", "creationTime"=>"2017-03-16T19:09:15.524Z", "updateTime"=>"2017-03-16T19:10:21.493Z", "enrollmentCode"=>"csaous", "courseState"=>"ARCHIVED", "alternateLink"=>"http://classroom.google.com/c/<KEY>", "teacherGroupEmail"=>"<EMAIL>", "courseGroupEmail"=>"<EMAIL>", "teacherFolder"=>{"id"=>"<KEY>", "title"=>"archive test", "alternateLink"=>"https://drive.google.com/drive/folders/<KEY>"}, "guardiansEnabled"=>false}

=end

  def self.run(user, course_response)
    if user.role == 'teacher'
      self.parse_courses_for_teacher(course_response, user)
    else
      self.parse_courses_for_student(course_response, user)
    end
  end

  private

  def self.parse_courses_for_teacher(course_response, user)
    courses = []
    if course_response[:courses] && course_response[:courses].any?
      existing_google_classroom_ids = self.existing_google_classroom_ids(user)
      course_response[:courses].each do |course|
        alreadyImported = self.already_imported?(course, existing_google_classroom_ids)
        if alreadyImported
          grade = Classroom.where(google_classroom_id: course[:id], teacher_id: user.id).first.grade
        end
        if self.valid?(course, user, existing_google_classroom_ids)
          name = course[:section] ? "#{course[:name]} #{course[:section]}" : course[:name]
          courses << {id: course[:id].to_i, name: name,
            ownerId: course[:ownerId],
            section: course[:section],
            alreadyImported: alreadyImported,
            grade: grade,
            creationTime: course[:creationTime],}
        end
      end
    end
    courses
  end

  def self.parse_courses_for_student(course_response, user)
    course_ids = []
    if course_response[:courses] && course_response[:courses].any?
      # checking to make sure student is not the owner (teacher) of the course
      course_response[:courses].select{ |c| !own_course(c, user) }.each do |course|
        course_ids << course[:id]
      end
    end
    course_ids
  end

  def self.existing_google_classroom_ids(user)
    User.find(user.id).google_classrooms.map(&:google_classroom_id)
  end

  def self.valid?(course, user, existing_google_classroom_ids)
    # really we should be calling google classrooms api to see if this user is a teacher,
{'fraction_non_alphanumeric': 0.13652694610778443, 'fraction_numerical': 0.026347305389221556, 'mean_word_length': 4.219791666666667, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 44, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '676042', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1618, 'n_words': 296}