<commit_before># == Schema Information
#
# Table name: events
#
#  id                 :integer          not null, primary key
#  date               :datetime
#  duration           :float
#  teaching_module_id :integer
#  promotion_id       :integer
#  kind               :integer
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  student_hours      :float
#  teacher_hours      :float
#  label              :string
#  description        :text
#  project_id         :integer
#

class Event < ActiveRecord::Base
  belongs_to :teaching_module
  belongs_to :promotion
  belongs_to :project
  has_and_belongs_to_many :users

  scope :in_semester, -> (semester) { where(teaching_module: semester.teaching_modules) }
  scope :ordered, -> { order(:date) }

  enum kind: [:cm, :td, :tp]

  before_save :compute_student_hours

  def self.create_with(calendar_event, promotion)
    date = calendar_event.dtstart
    date_end = calendar_event.dtend
    if date.class == Icalendar::Values::Date
      # All day event, used for holidays
      duration = 0
      return
    else
      duration = (date_end - date) / 60 / 60
    end
    teaching_module = nil
    hashtags = calendar_event.description.scan(/#(\w+)/).flatten
    kind = Event.kinds[:cm] # default
    hashtags.each do |hashtag|
      case hashtag.downcase
      when 'cm'
        kind = Event.kinds[:cm]
      when 'td'
        kind = Event.kinds[:td]
      when 'tp'
        kind = Event.kinds[:tp]
      else
        teaching_module = TeachingModule.with_code(hashtag).first
      end
    end
    project = Project.at_date_for_promotion(date, promotion)
    event = Event.create  promotion: promotion,
                          duration: duration,
                          date: date,
                          kind: kind,
                          project: project,
                          teaching_module: teaching_module,
                          label: calendar_event.summary,
                          description: calendar_event.description
    calendar_event.attendee.each do |attendee|
      email = attendee.to_s.remove 'mailto:'
      user = User.with_email(email).first
      event.users << user unless user.nil?
    end
    if event.users.none?
      event.users << User.temporary
    end
    # Compute hours now that users are set
    event.save
    event<commit_msg>fix events with no users<commit_after># == Schema Information
#
# Table name: events
#
#  id                 :integer          not null, primary key
#  date               :datetime
#  duration           :float
#  teaching_module_id :integer
#  promotion_id       :integer
#  kind               :integer
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  student_hours      :float
#  teacher_hours      :float
#  label              :string
#  description        :text
#  project_id         :integer
#

class Event < ActiveRecord::Base
  belongs_to :teaching_module
  belongs_to :promotion
  belongs_to :project
  has_and_belongs_to_many :users

  scope :in_semester, -> (semester) { where(teaching_module: semester.teaching_modules) }
  scope :ordered, -> { order(:date) }

  enum kind: [:cm, :td, :tp]

  before_save :compute_student_hours

  def self.create_with(calendar_event, promotion)
    date = calendar_event.dtstart
    date_end = calendar_event.dtend
    if date.class == Icalendar::Values::Date
      # All day event, used for holidays
      duration = 0
      return
    else
      duration = (date_end - date) / 60 / 60
    end
    teaching_module = nil
    hashtags = calendar_event.description.scan(/#(\w+)/).flatten
    kind = Event.kinds[:cm] # default
    hashtags.each do |hashtag|
      case hashtag.downcase
      when 'cm'
        kind = Event.kinds[:cm]
      when 'td'
        kind = Event.kinds[:td]
      when 'tp'
        kind = Event.kinds[:tp]
      else
        teaching_module = TeachingModule.with_code(hashtag).first
      end
    end
    project = Project.at_date_for_promotion(date, promotion)
    event = Event.create  promotion: promotion,
                          duration: duration,
                          date: date,
                          kind: kind,
                          project: project,
                          teaching_module: teaching_module,
                          label: calendar_event.summary,
                          description: calendar_event.description
    calendar_event.attendee.each do |attendee|
      email = attendee.to_s.remove 'mailto:'
      user = User.with_email(email).first
      event.users << user unless user.nil?
    end
    # Do not count time scheduled with no teacher, it might be exams or anything, but not lessons
    # event.users << User.temporary if event.users.none?
    # Compute hours now that users are set
    event.save
    event
{'fraction_non_alphanumeric': 0.08379544054220579, 'fraction_numerical': 0.0020538098172109263, 'mean_word_length': 1.9822412737293325, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '6468214', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1404, 'n_words': 357}