diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..5555dd2 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index aa9ae2d..0e9674f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,30 +1,5 @@ - - - - - - - - - CSS - - - Probable bugsCSS - - - RELAX NG - - - - - CoffeeScript - - - - - @@ -36,19 +11,4 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vim.iml b/.idea/vim.iml index c40b2a2..8f8657e 100644 --- a/.idea/vim.iml +++ b/.idea/vim.iml @@ -108,6 +108,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a673c5a..b07cda5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,12 +2,24 @@ + + + + + + + + - + + + + - + + @@ -36,88 +48,71 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - + + - - + + - - + + - - - - - - - - - - - - + + - + - - + + - - + + - - + + @@ -125,6 +120,13 @@ + + + @@ -139,22 +141,6 @@ @@ -207,10 +209,35 @@ - + + + + + + + + + CSS + + + Probable bugsCSS + + + RELAX NG + + + + + CoffeeScript + + + + @@ -237,9 +264,7 @@ - - @@ -258,6 +283,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -334,33 +409,93 @@ + + - - + + + + + + + + + + + - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -539,12 +674,18 @@ - + + + + + + + - @@ -605,26 +761,26 @@ - - + + - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -645,141 +801,194 @@ - - + - + - + + + + + + + + - + - + + + + + + + + - + - + - + - + - + - + + + + + + + + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + - + - - - + - + - + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -787,145 +996,181 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - - + + - + - - - + + - + + + + + + + + + + + + + + - - - + + - - + + - - + + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/assets/javascripts/excel.coffee b/app/assets/javascripts/excel.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/excel.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/excel.scss b/app/assets/stylesheets/excel.scss new file mode 100644 index 0000000..8da6b4f --- /dev/null +++ b/app/assets/stylesheets/excel.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the excel controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 9818fbd..33b73e6 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -57,6 +57,20 @@ class CoursesController < ApplicationController redirect_to courses_path, flash: {:success => "已经成功关闭该课程:#{ @course.name}"} end + def excel + @course= current_user.teaching_courses + end + + def excel_in + @course = Course.find_by_id(params[:id]) + if @course.update_attributes(course_params) + flash={:info => " 导入成功"} + else + flash={:warning => "导入失败"} + end + redirect_to excel_courses_path, flash: flash + end + #-------------------------for students---------------------- def list @@ -79,7 +93,19 @@ class CoursesController < ApplicationController redirect_to courses_path, flash: flash end + def excel_out + @course=Course.find_by_id(params[:id]) + if @course.update_attributes(course_params) + flash={:info => " 导出成功"} + else + flash={:warning => "导出失败"} + end + redirect_to excel_courses_path, flash: flash + end + def show + @course = Course.find(params[:id]) + end #-------------------------for both teachers and students---------------------- def index @@ -118,7 +144,7 @@ class CoursesController < ApplicationController def course_params - params.require(:course).permit(:course_code, :avatar, :name, :course_type, :teaching_type, :exam_type, + params.require(:course).permit(:course_code, :excel, :avatar, :name, :course_type, :teaching_type, :exam_type, :credit, :limit_num, :class_room, :course_time, :course_week, :course_introduction) end diff --git a/app/controllers/excel_controller.rb b/app/controllers/excel_controller.rb new file mode 100644 index 0000000..426592c --- /dev/null +++ b/app/controllers/excel_controller.rb @@ -0,0 +1,26 @@ +=begin +Date: 2016.12.28 +创建基于Importex::Base的类 +使用导入excel的ruby插件 importex +gem install importex +或者使用 +./script/plugin install git://github.com/ryanb/importex.git +=end + +class ExcelController < ApplicationController + def parse + #output excel + respond_to :html, :xlsx + def index + @user = User.all + respond_with @users + end + end + + def creat + @excel = Excel.new(excel_params) + @book.excel.url = params[:file]; + end + +end + diff --git a/app/controllers/grades_controller.rb b/app/controllers/grades_controller.rb index a0b5406..1ef96b3 100644 --- a/app/controllers/grades_controller.rb +++ b/app/controllers/grades_controller.rb @@ -12,6 +12,35 @@ class GradesController < ApplicationController redirect_to grades_path(course_id: params[:course_id]), flash: flash end + def excel_in + + require 'importex' + Excel true + column "class",:required=> true + column "Grades",:type=> Integer + column "Date",:type=> Date + column "Discontinued",:type=> Boolean + + #导入excel文件 + Excel.import("course.excel") + + #使用导入数据 + #all代表所有的数据,各个字段作为hash + excel = Excel.all + excel.first["Discontinued"] # => false + + #数据验证和容错处理 + #当excel的数据格式不能处理的时候,或者数据格式不符合时 + begin + Excel.import() + rescue Importex::ImportError => e + puts e.message + end + + + end + def index if teacher_logged_in? @course=Course.find_by_id(params[:course_id]) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a356e55..979c98b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,73 +1,73 @@ class UsersController < ApplicationController - before_action :logged_in, only: :update - before_action :correct_user, only: [:update, :destroy] +before_action :logged_in, only: :update +before_action :correct_user, only: [:update, :destroy] - def new - @user=User.new - end +def new +@user=User.new +end - def create - @user = User.new(user_params) - if @user.save - redirect_to root_url, flash: {success: "新账号注册成功,请登陆"} - else - flash[:warning] = "账号信息填写有误,请重试" - render 'new' - end - end +def create +@user = User.new(user_params) +if @user.save + redirect_to root_url, flash: {success: "新账号注册成功,请登陆"} +else + flash[:warning] = "账号信息填写有误,请重试" + render 'new' +end +end - def edit - @user=User.find_by_id(params[:id]) - end +def edit +@user=User.find_by_id(params[:id]) +end - def update - @user = User.find_by_id(params[:id]) - if @user.update_attributes(user_params) - flash={:info => "更新成功"} - else - flash={:warning => "更新失败"} - end - redirect_to root_path, flash: flash - end +def update +@user = User.find_by_id(params[:id]) +if @user.update_attributes(user_params) + flash={:info => "更新成功"} +else + flash={:warning => "更新失败"} +end +redirect_to root_path, flash: flash +end - def destroy - @user = User.find_by_id(params[:id]) - @user.destroy - redirect_to users_path(new: false), flash: {success: "用户删除"} - end +def destroy +@user = User.find_by_id(params[:id]) +@user.destroy +redirect_to users_path(new: false), flash: {success: "用户删除"} +end #----------------------------------- students function-------------------- - private +private - def user_params - params.require(:user).permit(:name, :email, :major, :department, :password, - :password_confirmation) - end +def user_params +params.require(:user).permit(:name, :email, :major, :department, :password, + :password_confirmation) +end - # Confirms a logged-in user. - def logged_in - unless logged_in? - redirect_to root_url, flash: {danger: '请登陆'} - end - end +# Confirms a logged-in user. +def logged_in +unless logged_in? + redirect_to root_url, flash: {danger: '请登陆'} +end +end - # Confirms the correct user. - def correct_user - @user = User.find(params[:id]) - unless current_user?(@user) - redirect_to root_url, flash: {:warning => '此操作需要管理员身份'} - end - end +# Confirms the correct user. +def correct_user +@user = User.find(params[:id]) +unless current_user?(@user) + redirect_to root_url, flash: {:warning => '此操作需要管理员身份'} +end +end - # Confirms a logged-in user. - def teacher_logged_in - unless teacher_logged_in? - redirect_to root_url, flash: {danger: '请登陆'} - end - end +# Confirms a logged-in user. +def teacher_logged_in +unless teacher_logged_in? + redirect_to root_url, flash: {danger: '请登陆'} +end +end end diff --git a/app/helpers/excel_helper.rb b/app/helpers/excel_helper.rb new file mode 100644 index 0000000..44db5a6 --- /dev/null +++ b/app/helpers/excel_helper.rb @@ -0,0 +1,2 @@ +module ExcelHelper +end diff --git a/app/models/course.rb b/app/models/course.rb index f909956..e037c31 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,7 @@ class Course < ActiveRecord::Base mount_uploader :avatar, AvatarUploader + mount_uploader :excel, ExcelUploader has_many :grades has_many :users, through: :grades @@ -10,5 +11,4 @@ class Course < ActiveRecord::Base validates :name, :course_type, :course_time, :course_week, :class_room, :credit, :teaching_type, :exam_type, presence: true, length: {maximum: 50} - end diff --git a/app/models/excel.rb b/app/models/excel.rb new file mode 100644 index 0000000..a14d67b --- /dev/null +++ b/app/models/excel.rb @@ -0,0 +1,49 @@ +=begin +Date: 2016.12.28 +创建基于Importex::Base的类 +使用导入excel的ruby插件 importex +gem install importex +或者使用 +./script/plugin install git://github.com/ryanb/importex.git +=end + + +require 'importex' +class Excel true + column "class",:required=> true + column "Grades",:type=> Integer + column "Date",:type=> Date + column "Discontinued",:type=> Boolean +end + +#导入excel文件 +Excel.import("course.excel") + +#使用导入数据 +#all代表所有的数据,各个字段作为hash +excel = Excel.all +excel.first["Discontinued"] # => false + +#数据验证和容错处理 +#当excel的数据格式不能处理的时候,或者数据格式不符合时 +begin + Excel.import() +rescue Importex::ImportError => e + puts e.message +end + + +#允许导入的字段指向Ruby对象, +# 例如,下面的例子,可以增加一个字段表示上面导入Product的类型情况 +class Category < ActiveRecord::Base + def self.importex_value(str) + find_by_name!(str) + rescue ActiveRecord::RecordNotFound + raise Importex::InvalidCell, "No category with that name." + end +end + +class Product < Importex::Base + column "Category", :type => Category +end \ No newline at end of file diff --git a/app/models/excelparse.rb b/app/models/excelparse.rb new file mode 100644 index 0000000..7ade93a --- /dev/null +++ b/app/models/excelparse.rb @@ -0,0 +1,53 @@ +=begin +用Ruby读取Excel文件 +Parseexcel是一个ruby端的perl解析excel的插件 +=end + +Spreadsheet::Parseexcel.parse("course.excel") +worksheet = workbook.worksheet(0) + worksheet.each { |row| + j = 0 + i = 0 + if row != nil + row.each { |cell| + if cell != nil + contents = cell.to_s('latin1') + puts "Row: #{j} Cell: #{i} #{contents}" + end + i = i+1 + } + j = j +1 + end + } + +=begin + cell.to_s('latin1') #读取字符串 + cell.to_s('latin1') #读取float值 + cell.to_i #读取int值 + cell.date #读取一个时间值 + cell = row.at(3) #读取特定值 +=end + +require 'parseexcel' + + #从命令行输入要打开的excel文件名 + workbook = Spreadsheet::ParseExcel.parse(ARGV[0]) + + #得到第一个表单 + worksheet = workbook.worksheet(0) + #遍历行 + worksheet.each { |row| + j=0 + i=0 + if row != nil + #遍历该行非空单元格 + row.each { |cell| + if cell != nil + #取得单元格内容为string类型 + contents = cell.to_s('latin1') + puts "Row: #{j} Cell: #{i}> #{contents}" + end + i = i+1 + } + end + } \ No newline at end of file diff --git a/app/uploaders/excel_uploader.rb b/app/uploaders/excel_uploader.rb new file mode 100644 index 0000000..bba3ed9 --- /dev/null +++ b/app/uploaders/excel_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class ExcelUploader < CarrierWave::Uploader::Base + + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + # include CarrierWave::MiniMagick + + # Choose what kind of storage to use for this uploader: + storage :file + # storage :fog + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) + # + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :resize_to_fit => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + # def extension_white_list + # %w(jpg jpeg gif png) + # end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + +end diff --git a/app/views/courses/_form.html.erb b/app/views/courses/_form.html.erb index 0b94a59..2bc54d2 100644 --- a/app/views/courses/_form.html.erb +++ b/app/views/courses/_form.html.erb @@ -113,6 +113,17 @@ +
+ <%= f.label "excel", class: 'col-sm-3 control-label' %> +
+
+
+ <%= f.file_field :excel %> + <%= f.hidden_field :excel_cache %> +
+
+
+ diff --git a/app/views/courses/excel.html.erb b/app/views/courses/excel.html.erb new file mode 100644 index 0000000..068253c --- /dev/null +++ b/app/views/courses/excel.html.erb @@ -0,0 +1,75 @@ +
+
+
+ <%= render "shared/sidenav" %> + +
+
+
+
+ +
+
+ + + + + + + + + + + + + + <% @course.each do |course| %> + + + + + + <%= form_for course, url: excel_in_course_path(course) , html: {class: 'form-horizontal', role: 'form'} do |f| %> +
+ <%= f.label "上传文件", class: 'col-sm-3 control-label' %> +
+
+ <%= f.file_field :excel %> + <%= f.hidden_field :excel_cache %> +
+
+
+ <%= f.submit '提交', class: "btn btn-success btn-block" %> + <% end %> + + + <% end %> + +
课程名称Excel文件导入
<%= link_to course.name, detail_course_url(course), class: 'btn-sm' %> >下载最新资料
+
+
+
+
+
+ + + + + + + + + + diff --git a/app/views/courses/index.html.erb b/app/views/courses/index.html.erb index 33a0b4d..b2f30ee 100644 --- a/app/views/courses/index.html.erb +++ b/app/views/courses/index.html.erb @@ -33,6 +33,7 @@ <%= course.credit %> <%= course.exam_type %> <%= course.teacher.name %> + <%course.excel%> <% if teacher_logged_in? %> <%= link_to "编辑", edit_course_url(course), class: 'btn btn-xs btn-info' %> @@ -46,7 +47,10 @@ <%= link_to "删除", course_path(course), :method => "delete", class: 'btn btn-xs btn-danger', :data => {confirm: '确定要删除此课程?'} %> <% elsif student_logged_in? %> <%= link_to "删除", quit_course_path(course), class: 'btn-sm btn-danger' %> + >下载最新资料 <% end %> + + <% end %> @@ -56,4 +60,4 @@ - \ No newline at end of file + diff --git a/app/views/excel/parse.html.erb b/app/views/excel/parse.html.erb new file mode 100644 index 0000000..8d5a688 --- /dev/null +++ b/app/views/excel/parse.html.erb @@ -0,0 +1,2 @@ +

Hello,Rails

+

Find me in app/views/excel/parse.html.erb

diff --git a/app/views/grades/index.html.erb b/app/views/grades/index.html.erb index 388a0b8..f7a60df 100644 --- a/app/views/grades/index.html.erb +++ b/app/views/grades/index.html.erb @@ -34,7 +34,65 @@ <%= grade.user.department %> <%= grade.course.name %> <%= grade.grade %> + <% if teacher_logged_in? %> <%= form_for grade, url: grade_url(id: grade.id, course_id: grade.course.id), method: :patch do |f| %> <%= f.text_field :grade %> diff --git a/app/views/shared/_sidenav.html.erb b/app/views/shared/_sidenav.html.erb index 87a1046..910f8db 100644 --- a/app/views/shared/_sidenav.html.erb +++ b/app/views/shared/_sidenav.html.erb @@ -59,6 +59,7 @@ <% if teacher_logged_in? %> 成绩管理 + <% end %> <% if student_logged_in? %> @@ -72,6 +73,12 @@
<% if teacher_logged_in? %> +
  • + <%= link_to excel_courses_path do %> + excel + <% end %> +
  • + <% current_user.teaching_courses.each do |course| %>
  • <%= link_to grades_path(course_id: course.id), :method => :get do %> diff --git a/config/routes.rb b/config/routes.rb index e744eed..ecfe517 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + get 'excel/parse' + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". @@ -24,9 +26,12 @@ Rails.application.routes.draw do get :detail get :open get :close + patch :excel_in + patch :excel_out end collection do get :list + get :excel end end diff --git a/db/migrate/20161228082748_add_excel_to_course.rb b/db/migrate/20161228082748_add_excel_to_course.rb new file mode 100644 index 0000000..45fd8b8 --- /dev/null +++ b/db/migrate/20161228082748_add_excel_to_course.rb @@ -0,0 +1,5 @@ +class AddExcelToCourse < ActiveRecord::Migration + def change + add_column :courses, :excel, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 26d5883..11c70c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161108164630) do +ActiveRecord::Schema.define(version: 20161228082748) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,10 +29,12 @@ ActiveRecord::Schema.define(version: 20161108164630) do t.string "course_time" t.string "course_week" t.integer "teacher_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "course_introduction" t.string "avatar" + t.boolean "open", default: false + t.string "excel" end create_table "grades", force: :cascade do |t| @@ -46,12 +48,6 @@ ActiveRecord::Schema.define(version: 20161108164630) do add_index "grades", ["course_id"], name: "index_grades_on_course_id", using: :btree add_index "grades", ["user_id"], name: "index_grades_on_user_id", using: :btree - create_table "images", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "image" - end - create_table "users", force: :cascade do |t| t.string "name" t.string "email" diff --git a/public/uploads/course/excel/1/___.xlsx b/public/uploads/course/excel/1/___.xlsx new file mode 100644 index 0000000..32e5f09 Binary files /dev/null and b/public/uploads/course/excel/1/___.xlsx differ diff --git a/public/uploads/course/excel/3/___.xlsx b/public/uploads/course/excel/3/___.xlsx new file mode 100644 index 0000000..32e5f09 Binary files /dev/null and b/public/uploads/course/excel/3/___.xlsx differ diff --git a/public/uploads/tmp/1482915177-15750-0002-3592/2016-11-28_16-09-39____.png b/public/uploads/tmp/1482915177-15750-0002-3592/2016-11-28_16-09-39____.png new file mode 100644 index 0000000..fc3552b Binary files /dev/null and b/public/uploads/tmp/1482915177-15750-0002-3592/2016-11-28_16-09-39____.png differ diff --git a/public/uploads/tmp/1482915623-15750-0003-8279/build.txt b/public/uploads/tmp/1482915623-15750-0003-8279/build.txt new file mode 100644 index 0000000..33cb22d --- /dev/null +++ b/public/uploads/tmp/1482915623-15750-0003-8279/build.txt @@ -0,0 +1 @@ +RM-162.2380.8 \ No newline at end of file diff --git a/public/uploads/tmp/1482915720-16509-0002-9588/2016-11-28_16-12-02____.png b/public/uploads/tmp/1482915720-16509-0002-9588/2016-11-28_16-12-02____.png new file mode 100644 index 0000000..86af168 Binary files /dev/null and b/public/uploads/tmp/1482915720-16509-0002-9588/2016-11-28_16-12-02____.png differ diff --git a/public/uploads/tmp/1482915720-16509-0002-9588/thumb_2016-11-28_16-12-02____.png b/public/uploads/tmp/1482915720-16509-0002-9588/thumb_2016-11-28_16-12-02____.png new file mode 100644 index 0000000..0287265 Binary files /dev/null and b/public/uploads/tmp/1482915720-16509-0002-9588/thumb_2016-11-28_16-12-02____.png differ diff --git a/public/uploads/tmp/1482917457-4739-0001-1935/___.xlsx b/public/uploads/tmp/1482917457-4739-0001-1935/___.xlsx new file mode 100644 index 0000000..32e5f09 Binary files /dev/null and b/public/uploads/tmp/1482917457-4739-0001-1935/___.xlsx differ diff --git a/public/uploads/tmp/1482917587-4739-0003-2543/___.xlsx b/public/uploads/tmp/1482917587-4739-0003-2543/___.xlsx new file mode 100644 index 0000000..32e5f09 Binary files /dev/null and b/public/uploads/tmp/1482917587-4739-0003-2543/___.xlsx differ diff --git a/test/controllers/excel_controller_test.rb b/test/controllers/excel_controller_test.rb new file mode 100644 index 0000000..dce5566 --- /dev/null +++ b/test/controllers/excel_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class ExcelControllerTest < ActionController::TestCase + test "should get parse" do + get :parse + assert_response :success + end + +end