Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Lib_webuntis
- class Tools
- class << self
- def fragment_cleaner events
- ranges = Hash.new
- result = Hash.new
- result[:schedules_to_remove] = []
- result[:new_end_times] = Hash.new
- schedules = Schedule
- .select(:start_time, :end_time, :id, :event_id, :type_id)
- .where('event_id in (?)', events.ids)
- .includes(:course, :event)
- schedules_by_groups = schedules.group_by { |schedule| schedule.rooms.pluck(:name).sort.join(",").hash.to_s + schedule.event.title }
- schedules_by_groups.each do |groups|
- schedules_by_course = groups.second.group_by { |schedule| schedule.course.name }
- schedules_by_course.each do |schedules_of_course|
- schedules_by_lecturers = schedules_of_course.second.group_by { |schedule| schedule.event.lecturers.pluck(:email).sort.join(",").hash }
- schedules_by_lecturers.each do |lecturer_schedules|
- ranges[groups.first] ||= Hash.new
- ranges[groups.first][schedules_of_course.first] ||= Hash.new
- ranges[groups.first][schedules_of_course.first][lecturer_schedules.first] ||= Array.new
- ranges[groups.first][schedules_of_course.first][lecturer_schedules.first].push lecturer_schedules.second
- current_schedule = nil
- # schedule_ids to remove
- ranges[groups.first][schedules_of_course.first][lecturer_schedules.first].first.each do |schedule|
- putc schedule.id
- if current_schedule == nil
- current_schedule = schedule
- else
- # Check time
- time_difference_in_minutes = (schedule.start_time - current_schedule.end_time)/60
- if time_difference_in_minutes <= 45 && current_schedule != schedule
- current_schedule.end_time = schedule.end_time
- result[:schedules_to_remove].push schedule.id
- result[:new_end_times][current_schedule.id] = current_schedule.end_time
- else
- current_schedule = schedule
- end
- end
- end
- end
- end
- end # // .each do |groups|
- events = events
- .where('schedules.id NOT IN (?)', result[:schedules_to_remove])
- events.each do |event|
- event.schedules.each do |schedule|
- schedule.end_time = result[:new_end_times][schedule.id] || schedule.end_time
- end
- end
- events
- end
- end
- end
- class Untis
- class << self
- # GET /clients/3/courses/:id/events
- def get_events_for_course identifier, params
- if identifier.is_number?
- course = Course.
- where(id: identifier, client_id: params["client_id"]).
- first
- else
- course = Course.
- where(name: URI.decode(identifier), client_id: params["client_id"]).
- first
- end
- if course
- # Event.includes(:schedules).where(
- # course_id: course.id,
- # schedules: {start_time: }
- # )
- course.events
- else
- Event.none
- end
- end
- # GET /clients/3/users/:id/events
- def get_events_for_user identifier, params
- client = Client.find params["client_id"]
- if identifier.is_number?
- user = client.
- users.
- where(id: identifier).
- first
- elsif identifier.include? '@'
- user = client.
- users.
- where(email: URI.decode(identifier)).
- first
- else
- user = client.
- users.
- where(username: URI.decode(identifier)).
- first
- end
- if user
- user.events
- else
- Event.none
- end
- end
- # GET /clients/3/groups/:id/events
- def get_events_for_group identifier, params
- if identifier.is_number?
- group = Group.
- where(id: identifier, client_id: params["client_id"]).
- first
- else
- group = Group.
- where(name: URI.decode(identifier), client_id: params["client_id"]).
- first
- end
- if group
- group.events
- else
- Event.none
- end
- end
- end # / class << self
- end # / class Untis
- class JSON
- class << self
- def parse_elements_feed data, client
- # JSON looks like this
- # - result
- # - elementIds
- # - elementPeriods
- # - ext_id - cotains events
- # - elements
- # - elementRoomLocks
- # - elements
- # Types
- # Type 1 - Class
- # Type 2 - User
- # Type 3 - Subject
- # Type 4 - Room
- # Let's create the elements first.
- data['elements'].each do |element|
- case element['type']
- when 1 # a class
- group = client.groups.
- where(name: element['name']).
- first_or_create(
- ext_id: element['id'],
- longName: element['longName']
- )
- group.ext_id = element['id']
- group.save
- client.groups << group unless client.groups.include? group
- when 2 # a user
- email = (element['displayname'].split(',').map { |e| e.strip }).reverse.join('.').downcase + client.main_domain
- user = client.users.
- where(ext_id: element['id']).
- first_or_create(
- full_name: element['displayname'],
- email: email,
- ext_id: element['id']
- )
- user.ext_id = element['id']
- user.save
- client.users << user unless client.users.include? user
- when 3 # a subject
- course = client.courses.
- where(name: element['name']).
- first_or_create(
- title: element['longName'],
- ext_id: element['id']
- )
- course.ext_id = element['id']
- course.save
- client.courses << course unless client.courses.include? course
- when 4 # a room
- room = client.rooms.
- where(name: element['name']).
- first_or_create(
- name: element['name'],
- longName: element['longName'],
- ext_id: element['id']
- )
- room.ext_id = element['id']
- room.save
- client.rooms << room unless client.rooms.include? room
- else
- false
- end
- end # / data['elements'].each
- # Let's iterate over each lesson to create events and schedules
- data['elementPeriods'].first.second.each do |period|
- # Times
- start_time = (period['startTime'].to_s.length == 4) ? period['startTime'] : '0' + period['startTime'].to_s
- end_time = (period['endTime'].to_s.length == 4) ? period['endTime'] : '0' + period['endTime'].to_s
- date = period['date']
- # Collecting arrays for elements to create associations later
- groups_array = Array.new
- users_array = Array.new
- courses_array = Array.new
- rooms_array = Array.new
- # What elements belong to this period?
- period['elements'].each do |element|
- case element['type']
- when 1 # a class
- group = client.groups.
- where(ext_id: element['id']).
- first
- groups_array.push group
- when 2 # a user
- user = client.users.
- where(ext_id: element['id']).
- first
- users_array.push user
- when 3 # a subject
- course = client.courses.
- where(ext_id: element['id']).
- first
- courses_array.push course
- when 4 # a room
- room = client.rooms.
- where(ext_id: element['id']).
- first
- rooms_array.push room
- else
- false
- end
- end # /period['elements'].each
- if users_array.count < 1
- ghost_user = client.users.where(username: 'admin').first_or_create(
- first_name:'Admin',
- last_name: 'Mode',
- full_name: 'Admin Mode',
- email: 'admin.mode@' + client.main_domain,
- auxillary: true
- )
- client.users << ghost_user unless ghost_user.clients.include? client
- users_array.push ghost_user
- end
- # The ext_id of events is unique only for a given Client and Course combo, or so I hope.
- courses_array.each do |course|
- # Transitionary code:
- # Let's find duplicate schedules in other events.
- # This shouldn't really be neccessary if we started from scratch
- related_course_ids = client.courses.where(name: course.name).pluck(:id)
- related_events_ids = Event.where(course_id: related_course_ids, title: course.name).pluck(:id)
- # Create schedule, but check to see if it already exists
- # Remember: If a and b are in the same place at the same time, then a = b
- schedule = Schedule
- .includes(event: :groups)
- .includes(event: :users)
- .where(
- start_time: Time.zone.parse("#{date} #{start_time}"),
- end_time: Time.zone.parse("#{date} #{end_time}"),
- event_id: related_events_ids,
- groups: {id: (groups_array.map { |group| group.id })},
- users: {id: (users_array.map { |user| user.id })}
- ).first
- if schedule
- event = Event.includes(:schedules).includes(:users).where(id: schedule.event_id).first
- else
- # If doesnt exist already
- # Find event
- event = course.events.create!(
- title: course.name,
- ext_id: period['id']
- )
- event.ext_id = period['id']
- event.save
- schedule = Schedule
- .create(
- start_time: Time.zone.parse("#{date} #{start_time}"),
- end_time: Time.zone.parse("#{date} #{end_time}"),
- event_id: event.id
- )
- event = Event.includes(:schedules).includes(:users).where(id: schedule.event_id).first
- end
- # Add relations
- event.schedules << schedule unless event.schedules.include? schedule
- # Groups
- groups_array.each do |group|
- group.events << event unless group.events.include? event
- end
- # Users
- users_array.each do |user|
- event.users << user unless event.users.include? user
- user.add_role :lecturer, event unless user.has_role? :lecturer, event
- end
- # Rooms
- rooms_array.each do |room|
- room.schedules << schedule unless room.schedules.include? schedule
- end
- # Delete events with empty titles
- if event.title.nil?
- event.destroy
- end
- end # /courses_array.each
- end # / data['elementPeriods'].each
- nil
- end # / parse_elements_feed
- end # class << self
- end # class JSON
- end # module Lib_webuntis
- #Extend class string
- class String
- def is_number?
- true if Float(self) rescue false
- end
- end
Add Comment
Please, Sign In to add comment