SHARE
TWEET

Untitled

a guest Jul 21st, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LearningPlatformService
  2.   include ColorgyCrawler::DateMixin
  3.  
  4.   def initialize(user)
  5.     @user = user
  6.     account = user.learning_platform_datum.account
  7.     password = Base64.decode64(Base64.decode64(user.learning_platform_datum.password))
  8.     lp_klass = LearningPlatform.find!(user.organization_code)
  9.     @lp_instance = lp_klass.new(account, password)
  10.     @lp_data = @user.learning_platform_datum
  11.     @imported_list = []
  12.     @failed_list = []
  13.     @existed_list = []
  14.   end
  15.  
  16.   def can_login
  17.     @lp_instance.can_login
  18.   end
  19.  
  20.   def get_course_list
  21.     courses = @lp_instance.get_course_list
  22.   end
  23.  
  24.   def get_announcement_list
  25.     announcements = @lp_instance.get_announcement_list
  26.   end
  27.  
  28.   def get_file_list
  29.     files = @lp_instance.get_file_list
  30.   end
  31.  
  32.   def get_grade_list
  33.     grades = @lp_instance.get_grade_list
  34.   end
  35.  
  36.   def new_data_available?
  37.     result = []
  38.     courses = @user.enroll_courses
  39.     courses.each do |course|
  40.       matched_course = course.matched_course_datum
  41.       user_matched_course_datum = UserMatchedCourseDatum.find_by(user_id: @user.id, matched_course_datum_id: matched_course.id)
  42.       if matched_course.updated_at > user_matched_course_datum.updated_at
  43.         result << {course_id: course.id, new_data_available: true}
  44.       else
  45.         result << {course_id: course.id, new_data_available: false}
  46.       end
  47.     end
  48.     result
  49.   end
  50.  
  51.   def import_announcement_list
  52.     @new_announcement_size = 0
  53.     @exist_announcement_size = 0
  54.     ActiveRecord::Base.transaction do
  55.       announcement_list = get_announcement_list
  56.       raise StandardError, 'No announcement data!' if !announcement_list || announcement_list.empty?
  57.       announcement_list.each do |announcement|
  58.         create_announcement_to_matched_course_announcement(announcement)
  59.       end
  60.       [1, @new_announcement_size, @exist_announcement_size, ""]
  61.     end
  62.   rescue Exception => e
  63.     [2, @new_announcement_size, @exist_announcement_size, e]
  64.   end
  65.  
  66.   def match_course_list
  67.     calendar = Calendar.find_by(owner_type: 'Organization', owner_id: @user.organization.id, default: true)
  68.     ActiveRecord::Base.transaction do
  69.       list = get_course_list
  70.       raise StandardError, 'No course data import!' if !list || list.empty?
  71.       list.each do |lp_course|
  72.         import_list_to_user(lp_course, @user, calendar, @lp_data)
  73.       end
  74.       if @user.reload.course_not_founds.blank?
  75.         [1, @imported_list, @failed_list, @existed_list, ""]
  76.       else
  77.         [2, @imported_list, @failed_list, @existed_list, ""]
  78.       end
  79.     end
  80.   rescue Exception => e
  81.     [3, [], [], [], e]
  82.   end
  83.  
  84.   private
  85.  
  86.   def find_db_course_by_data(data, calendar)
  87.     sql = if data[:course_lecturer].present?
  88.             <<-sql
  89.               data -> 'course_year' = ? AND
  90.               data -> 'course_term' = ? AND
  91.               data -> 'course_lecturer' LIKE ? AND
  92.               data -> 'course_code' LIKE ?
  93.             sql
  94.           else
  95.             <<-sql
  96.               data -> 'course_year' = ? AND
  97.               data -> 'course_term' = ? AND
  98.               data -> 'course_code' LIKE ?
  99.             sql
  100.           end
  101.     where_params = if data[:course_lecturer].present?
  102.                      [sql, current_year.to_s, current_term.to_s, "%#{data[:course_lecturer]}%", "%#{data[:course_code]}%"]
  103.                    else
  104.                      [sql, current_year.to_s, current_term.to_s, "%#{data[:course_code]}%"]
  105.                    end
  106.  
  107.     course = Course.where(calendar_id: calendar.id).where("name LIKE ?", "%#{data[:course_name]}%").where(*where_params).root.last
  108.   end
  109.  
  110.   def find_matched_course_by_data(data)
  111.     if data[:course_lecturer].present?
  112.       matched_course = MatchedCourseDatum.find_by(lp_course_name: data[:course_name], lp_course_code: data[:course_code], lp_course_lecturer: data[:course_lecturer])
  113.     else
  114.       matched_course = MatchedCourseDatum.find_by(lp_course_name: data[:course_name], lp_course_code: data[:course_code])
  115.     end
  116.   end
  117.  
  118.   def import_list_to_user(course_data, user, calendar, learning_platform_datum)
  119.     course = find_db_course_by_data(course_data, calendar)
  120.     if course.present?
  121.       if !user.enroll_course_ids.include?(course.id)
  122.         user.enroll_courses << course
  123.         @imported_list << course_data
  124.         m = MatchedCourseDatum.create(course_id: course.id, lp_course_name: course_data[:course_name],
  125.           lp_course_code: course_data[:course_code],
  126.           lp_course_lecturer: course_data[:course_lecturer],
  127.           lp_course_url: course_data[:course_url])
  128.         UserMatchedCourseDatum.create(user_id: user.id, matched_course_datum_id: m.id)
  129.       else
  130.         @existed_list << course_data
  131.       end
  132.     else
  133.       if user.course_not_founds.find_by(course_data).nil?
  134.         course_not_found = user.course_not_founds.new(course_data)
  135.         course_not_found.learning_platform_datum_id = learning_platform_datum.id
  136.         course_not_found.save
  137.         @failed_list << course_data
  138.       end
  139.     end
  140.   end
  141.  
  142.   def create_announcement_to_matched_course_announcement(announcement_data)
  143.     matched_course = find_matched_course_by_data(announcement_data)
  144.     course = matched_course.course
  145.     announcement_data[:course_announcement].each do |a|
  146.       announcement = course.matched_course_announcements.where(matched_id: matched_course.id, url: a[:url], published_at: a[:published_at]).first_or_initialize
  147.       announcement.title = a[:title]
  148.       announcement.content = a[:content]
  149.       announcement.published_at = a[:published_at]
  150.       if announcement.new_record?
  151.         matched_course.touch
  152.         @new_announcement_size += 1
  153.       else
  154.         @exist_announcement_size += 1
  155.       end
  156.       announcement.save
  157.     end
  158.   end
  159. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top