Advertisement
nadeemjq

webuntis.rb

Jan 5th, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 7.03 KB | None | 0 0
  1. # encoding: UTF-8
  2. class Webuntis
  3.  
  4.   require_dependency "#{Rails.root}/lib/api/webuntis/lib_webuntis.rb"
  5.  
  6.   class << self
  7.  
  8.     # Method below are called by Events controller
  9.  
  10.     def events_for resource, identifier, params
  11.  
  12.       client = Client.find(params[:client_id])
  13.  
  14.  
  15.       case resource
  16.       when "course"
  17.  
  18.         events = Lib_webuntis::Untis.get_events_for_course identifier, params
  19.  
  20.       when "user"
  21.  
  22.         events = Lib_webuntis::Untis.get_events_for_user identifier, params
  23.  
  24.       when "group"
  25.  
  26.         events = Lib_webuntis::Untis.get_events_for_group identifier, params
  27.  
  28.       else
  29.  
  30.         events = Event.none
  31.  
  32.       end
  33.  
  34.       from = params[:from] || 1
  35.       to      = params[:to]     || 4
  36.  
  37.       events = events
  38.               .joins(:schedules)
  39.               .where('schedules.start_time >= ? AND schedules.start_time <= ?', from.to_i.days.ago, to.to_i.days.from_now)
  40.               .joins(:users)
  41.               .includes(:schedules)
  42.               .includes(:course)
  43.               .includes(:attachments)
  44.               .includes(:programs)
  45.               .includes(:groups)
  46.               .order('schedules.start_time')
  47.               .limit(params[:limit] || 100)
  48.               .distinct
  49.  
  50.       events_without_fragments = Lib_webuntis::Tools.fragment_cleaner events
  51.  
  52.     end
  53.  
  54.     # Method for getting teachers
  55.     def get_teachers client
  56.  
  57.       params = {
  58.                           school: client.api_data[:school_name],
  59.                           ajaxCommand: 'getPageConfig',
  60.                           type: 2
  61.                         }
  62.  
  63.       url  = 'https://romres.ist-asp.com/WebUntis/Timetable.do'
  64.  
  65.       req  = RestClient.post url, params #, {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
  66.       json = JSON.parse req
  67.  
  68.       teachers = json['elements']
  69.  
  70.       teachers.each do |teacher|
  71.  
  72.         full_name = teacher['forename'] + ' ' + teacher['longName']
  73.  
  74.         if teacher['externKey'].include? '@'
  75.  
  76.           email = teacher['externKey']
  77.  
  78.         else
  79.  
  80.           email = full_name.split(' ').join('.').downcase + '@' + client.main_domain
  81.  
  82.         end
  83.  
  84.         # The name attribute seems to be both unique and permanent
  85.         # We need to use it instead of ext_id
  86.         user = client.users.
  87.                       where(email: email).
  88.                       first_or_create(
  89.                         first_name: teacher['forename'],
  90.                         last_name: teacher['longName'],
  91.                         full_name: full_name,
  92.                         username: teacher['name'],
  93.                         email: email,
  94.                         ext_id: teacher['id']
  95.                       )
  96.  
  97.         # the id attribute is open to change, so we must update it.
  98.         user.ext_id = teacher['id']
  99.         user.save!
  100.  
  101.         client.users << user unless client.users.include? user
  102.         client.users
  103.       end
  104.       nil
  105.     end
  106.  
  107.     # Gets timetable for specified teach
  108.     # Date must look like: 20150121
  109.     def get_timetable_for_teacher id, date, client
  110.  
  111.       params = {
  112.                           school: client.api_data[:school_name],
  113.                           ajaxCommand: 'getWeeklyTimetable',
  114.                           elementType: 2,
  115.                           elementId: id,
  116.                           date: date,
  117.                           "filter.klasseId" => -1,
  118.                           "filter.restypeId" => -1,
  119.                           "filter.buildingId" => -1,
  120.                           "filter.roomGroupId" => -1,
  121.                           "filter.departmentId" => -1,
  122.                           formatId: 0
  123.                         }
  124.  
  125.       url  = 'https://romres.ist-asp.com/WebUntis/Timetable.do'
  126.  
  127.       req  = RestClient.post url, params #, {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
  128.       json = JSON.parse req
  129.  
  130.       unless json['result'] == nil
  131.         data = json['result']['data']
  132.         Lib_webuntis::JSON.parse_elements_feed data, client
  133.       end
  134.       nil
  135.     end # /get_timetable_for_teacher
  136.  
  137.     def get_groups client
  138.  
  139.       params = {
  140.                           school: client.api_data[:school_name],
  141.                           ajaxCommand: 'getPageConfig',
  142.                           type: 1,
  143.                         }
  144.  
  145.       url  = 'https://romres.ist-asp.com/WebUntis/Timetable.do'
  146.  
  147.       req  = RestClient.post url, params # , {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
  148.       json = JSON.parse req
  149.  
  150.       groups = json['elements']
  151.  
  152.       groups.each do |group|
  153.  
  154.         group = client.groups.
  155.                       where(ext_id: group['id']).
  156.                       first_or_create(
  157.                         name: group['name'],
  158.                         longName: group['longName']
  159.                       )
  160.  
  161.         client.groups << group unless client.groups.include? group
  162.         client.groups
  163.       end
  164.       nil
  165.  
  166.     end # /get_groups
  167.  
  168.     def get_timetable_for_group id, date, client
  169.  
  170.       params = {
  171.                           school: client.api_data[:school_name],
  172.                           ajaxCommand: 'getWeeklyTimetable',
  173.                           elementType: 1,
  174.                           elementId: id,
  175.                           date: date,
  176.                           "filter.klasseId" => -1,
  177.                           "filter.restypeId" => -1,
  178.                           "filter.buildingId" => -1,
  179.                           "filter.roomGroupId" => -1,
  180.                           "filter.departmentId" => -1,
  181.                           formatId: 0
  182.                         }
  183.  
  184.       url  = 'https://romres.ist-asp.com/WebUntis/Timetable.do?'
  185.  
  186.       req  = RestClient.post url, params #, {cookies: {schoolname: '_QWthZGVtaWV0X0RyYW1tZW4'}}
  187.       json = JSON.parse req
  188.  
  189.       unless json['result'] == nil
  190.         data = json['result']['data']
  191.         Lib_webuntis::JSON.parse_elements_feed data, client
  192.       end
  193.       nil
  194.     end # /get_timetable_for_group
  195.  
  196.     def sync_everything params
  197.  
  198.       client = Client.find(params[:id])
  199.       from = params['from'].to_i || 0
  200.       to = params["to"].to_i || 5
  201.       dates = []
  202.  
  203.       (from..to).each do |day|
  204.         dates.push day.days.from_now.strftime("%Y%m%d")
  205.       end
  206.  
  207.       # Get teachers
  208.       Webuntis.get_teachers client
  209.  
  210.       # Get classes
  211.       # webuntis.get_groups # DISABLED: Appears to be buggy (non-deterministic)
  212.  
  213.       # Iterate users
  214.       client.users.where(auxillary: false).find_in_batches(batch_size: 500).each do |user_batch|
  215.         user_batch.each do |user|
  216.           dates.each do |date|
  217.             Webuntis.get_timetable_for_teacher user.ext_id, date, client
  218.           end
  219.         end
  220.       end # /client.users.each
  221.  
  222.       # Iterate groups
  223.       client.groups.find_in_batches(batch_size: 500).each do |group_batch|
  224.         group_batch.each do |group|
  225.           dates.each do |date|
  226.             Webuntis.get_timetable_for_group group.ext_id, date, client
  227.           end
  228.  
  229.         end
  230.       end # /client.groups.each
  231.       nil
  232.     end # /def sync_everything
  233.  
  234.   end # /class << self
  235.  
  236. end # /class Webuntis
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement