From ed8b42e948b3f09ccb4de8f9e4b9f09e8d2004cf Mon Sep 17 00:00:00 2001 From: Manson Wang Date: Sun, 29 Dec 2013 15:10:49 +0800 Subject: [PATCH] Import teacher data --- lib/tasks/import_teachers.rake | 58 +++++++++++++++++++ .../front_end/member_lists/index.html.erb | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/import_teachers.rake diff --git a/lib/tasks/import_teachers.rake b/lib/tasks/import_teachers.rake new file mode 100644 index 00000000..073e6086 --- /dev/null +++ b/lib/tasks/import_teachers.rake @@ -0,0 +1,58 @@ +#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 \ No newline at end of file diff --git a/vendor/built_in_modules/member/app/views/panel/member/front_end/member_lists/index.html.erb b/vendor/built_in_modules/member/app/views/panel/member/front_end/member_lists/index.html.erb index 80fa140c..0b317286 100644 --- a/vendor/built_in_modules/member/app/views/panel/member/front_end/member_lists/index.html.erb +++ b/vendor/built_in_modules/member/app/views/panel/member/front_end/member_lists/index.html.erb @@ -80,7 +80,7 @@
<%= show_avatar(member) %>