Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: UTF-8
- class Webuntis
- require_dependency "#{Rails.root}/lib/api/webuntis/lib_webuntis.rb"
- class << self
- # Method below are called by Events controller
- def events_for resource, identifier, params
- client = Client.find(params[:client_id])
- case resource
- when "course"
- events = Lib_webuntis::Untis.get_events_for_course identifier, params
- when "user"
- events = Lib_webuntis::Untis.get_events_for_user identifier, params
- when "group"
- events = Lib_webuntis::Untis.get_events_for_group identifier, params
- else
- events = Event.none
- end
- from = params[:from] || 1
- to = params[:to] || 4
- events = events
- .joins(:schedules)
- .where('schedules.start_time >= ? AND schedules.start_time <= ?', from.to_i.days.ago, to.to_i.days.from_now)
- .joins(:users)
- .includes(:schedules)
- .includes(:course)
- .includes(:attachments)
- .includes(:programs)
- .includes(:groups)
- .order('schedules.start_time')
- .limit(params[:limit] || 100)
- .distinct
- events_without_fragments = Lib_webuntis::Tools.fragment_cleaner events
- end
- # Method for getting teachers
- def get_teachers client
- params = {
- school: client.api_data[:school_name],
- ajaxCommand: 'getPageConfig',
- type: 2
- }
- url = 'https://romres.ist-asp.com/WebUntis/Timetable.do'
- req = RestClient.post url, params #, {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
- json = JSON.parse req
- teachers = json['elements']
- teachers.each do |teacher|
- full_name = teacher['forename'] + ' ' + teacher['longName']
- if teacher['externKey'].include? '@'
- email = teacher['externKey']
- else
- email = full_name.split(' ').join('.').downcase + '@' + client.main_domain
- end
- # The name attribute seems to be both unique and permanent
- # We need to use it instead of ext_id
- user = client.users.
- where(email: email).
- first_or_create(
- first_name: teacher['forename'],
- last_name: teacher['longName'],
- full_name: full_name,
- username: teacher['name'],
- email: email,
- ext_id: teacher['id']
- )
- # the id attribute is open to change, so we must update it.
- user.ext_id = teacher['id']
- user.save!
- client.users << user unless client.users.include? user
- client.users
- end
- nil
- end
- # Gets timetable for specified teach
- # Date must look like: 20150121
- def get_timetable_for_teacher id, date, client
- params = {
- school: client.api_data[:school_name],
- ajaxCommand: 'getWeeklyTimetable',
- elementType: 2,
- elementId: id,
- date: date,
- "filter.klasseId" => -1,
- "filter.restypeId" => -1,
- "filter.buildingId" => -1,
- "filter.roomGroupId" => -1,
- "filter.departmentId" => -1,
- formatId: 0
- }
- url = 'https://romres.ist-asp.com/WebUntis/Timetable.do'
- req = RestClient.post url, params #, {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
- json = JSON.parse req
- unless json['result'] == nil
- data = json['result']['data']
- Lib_webuntis::JSON.parse_elements_feed data, client
- end
- nil
- end # /get_timetable_for_teacher
- def get_groups client
- params = {
- school: client.api_data[:school_name],
- ajaxCommand: 'getPageConfig',
- type: 1,
- }
- url = 'https://romres.ist-asp.com/WebUntis/Timetable.do'
- req = RestClient.post url, params # , {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
- json = JSON.parse req
- groups = json['elements']
- groups.each do |group|
- group = client.groups.
- where(ext_id: group['id']).
- first_or_create(
- name: group['name'],
- longName: group['longName']
- )
- client.groups << group unless client.groups.include? group
- client.groups
- end
- nil
- end # /get_groups
- def get_timetable_for_group id, date, client
- params = {
- school: client.api_data[:school_name],
- ajaxCommand: 'getWeeklyTimetable',
- elementType: 1,
- elementId: id,
- date: date,
- "filter.klasseId" => -1,
- "filter.restypeId" => -1,
- "filter.buildingId" => -1,
- "filter.roomGroupId" => -1,
- "filter.departmentId" => -1,
- formatId: 0
- }
- url = 'https://romres.ist-asp.com/WebUntis/Timetable.do?'
- req = RestClient.post url, params #, {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
- json = JSON.parse req
- unless json['result'] == nil
- data = json['result']['data']
- Lib_webuntis::JSON.parse_elements_feed data, client
- end
- nil
- end # /get_timetable_for_group
- def sync_everything params
- client = Client.find(params[:id])
- from = params['from'].to_i || 0
- to = params["to"].to_i || 5
- dates = []
- (from..to).each do |day|
- dates.push day.days.from_now.strftime("%Y%m%d")
- end
- # Get teachers
- Webuntis.get_teachers client
- # Get classes
- # webuntis.get_groups # DISABLED: Appears to be buggy (non-deterministic)
- # Iterate users
- client.users.where(auxillary: false).find_in_batches(batch_size: 500).each do |user_batch|
- user_batch.each do |user|
- dates.each do |date|
- Webuntis.get_timetable_for_teacher user.ext_id, date, client
- end
- end
- end # /client.users.each
- # Iterate groups
- client.groups.find_in_batches(batch_size: 500).each do |group_batch|
- group_batch.each do |group|
- dates.each do |date|
- Webuntis.get_timetable_for_group group.ext_id, date, client
- end
- end
- end # /client.groups.each
- nil
- end # /def sync_everything
- end # /class << self
- end # /class Webuntis
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement