orbit-4-2/lib/tasks/import_teachers.rake

58 lines
1.5 KiB
Ruby
Raw Normal View History

2014-02-10 08:05:59 +00:00
#encoding: utf-8
namespace :teacher do
task :import, [:department,:category] => :environment do |t, args|
department = "#{args.department}"
teacher_role = Role.where(:key=>"teacher").first
role_fulltime = RoleStatus.where(:key=>"1.fulltime").first
role_adjunct = RoleStatus.where(:key=>"2.adjunct").first
role_category = RoleCategory.where(:key=>"#{args.category}").first
@teacher = []
CSV.foreach("#{Rails.root}/#{department}.csv") do |row|
@teacher.push(row)
end
@teacher.each do |teacher|
sex = "unknown"
if teacher[2]==""
sex = "male"
elsif teacher[2]==""
sex = "female"
end
role_status = ""
if teacher[3].include? "兼任"
role_status = role_adjunct
else
role_status = role_fulltime
end
if teacher[1].blank?
teacher[1] = " "
end
user_data = {
:user_id => teacher[5].split("@")[0],
:password => "password",
:first_name_translations =>{"zh_tw"=>teacher[0].split(teacher[0][0])[1],"en"=>teacher[1].split(" ")[1]},
:last_name_translations => {"zh_tw"=>teacher[0][0],"en"=>teacher[1].split(" ")[0]},
:roles => [teacher_role],
:role_statuses => [role_status],
:role_categorys => [role_category],
:office_tel => teacher[4],
:email => teacher[5],
:confirmation_token => nil,
:confirmed_at => Time.now,
:approved => true,
:sex => sex
}
if User.where(:email=>teacher[5]).count == 0
user = User.new(user_data)
user.save
else
user = User.where(:email=>teacher[5]).first
user.update_attributes(user_data)
end
end
end
end