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/workspace.xml b/.idea/workspace.xml index 32715c1..88a2226 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,17 +2,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -<<<<<<< HEAD -<<<<<<< HEAD -======= + - ->>>>>>> flyingwaters-master -======= + @@ -20,7 +51,7 @@ ->>>>>>> huluobu-master + @@ -49,92 +80,110 @@ - -<<<<<<< HEAD + + + + + + + + + + + + + + + + -======= + ->>>>>>> 35b6d0a7f00e66e3dd0d8e7ff14d2ceed8776b54 + -<<<<<<< HEAD + + + + + + + -======= + ->>>>>>> 35b6d0a7f00e66e3dd0d8e7ff14d2ceed8776b54 + -<<<<<<< HEAD + -======= + ->>>>>>> 35b6d0a7f00e66e3dd0d8e7ff14d2ceed8776b54 + -<<<<<<< HEAD + -======= -<<<<<<< HEAD + -======= + ->>>>>>> 35b6d0a7f00e66e3dd0d8e7ff14d2ceed8776b54 ->>>>>>> huluobu-master + @@ -150,81 +199,105 @@ -<<<<<<< HEAD + -======= -<<<<<<< HEAD + ->>>>>>> huluobu-master + + + + + + + + -<<<<<<< HEAD + -======= + -======= + ->>>>>>> 35b6d0a7f00e66e3dd0d8e7ff14d2ceed8776b54 ->>>>>>> huluobu-master + -<<<<<<< HEAD + + + + + + + + -======= -<<<<<<< HEAD + ->>>>>>> huluobu-master + + + + + + + + -<<<<<<< HEAD -<<<<<<< HEAD + -======= + ->>>>>>> flyingwaters-master -======= + -======= + + + + + + + + + @@ -240,8 +313,7 @@ ->>>>>>> 35b6d0a7f00e66e3dd0d8e7ff14d2ceed8776b54 ->>>>>>> huluobu-master + @@ -249,6 +321,13 @@ + + + @@ -263,11 +342,12 @@ @@ -356,6 +457,31 @@ \ 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 262808e..8979078 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -62,15 +62,15 @@ class CoursesController < ApplicationController end def excel_in - @course1 = Course.find_by_id(params[:id]) - if @course1.update_attributes(course_params) - flash={:info => " 导入成功"} - else - flash={:warning => "导入失败"} - end - @course=current_user.teaching_courses - @params=params - redirect_to courses_path, flash: flash + + + @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 @@ -165,7 +165,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 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 460b0bf..00f57ec 100644 --- a/app/controllers/grades_controller.rb +++ b/app/controllers/grades_controller.rb @@ -15,6 +15,35 @@ class GradesController < ApplicationController 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 038e9dc..e037c31 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -11,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/views/courses/_form.html.erb b/app/views/courses/_form.html.erb index f4e6b50..b89cfb6 100644 --- a/app/views/courses/_form.html.erb +++ b/app/views/courses/_form.html.erb @@ -115,6 +115,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 283c833..db23736 100644 --- a/app/views/courses/index.html.erb +++ b/app/views/courses/index.html.erb @@ -45,6 +45,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' %> @@ -74,14 +75,21 @@ <% elsif student_logged_in? %> <%= link_to "删除", quit_course_path(course), class: 'btn-sm btn-danger' %> + + >下载最新资料 + <% if course.degree %> <%= link_to "否", nodegree_course_path(course), class: 'btn btn-xs btn-danger' %> <% else %> <%= link_to "是",isdegree_course_path(course), class: 'btn btn-xs btn-info' %> <% end %> + >下载最新资料 <%= course.excel %> + <% end %> + + <% end %> @@ -91,4 +99,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 44cfbc6..d89d246 100644 --- a/app/views/shared/_sidenav.html.erb +++ b/app/views/shared/_sidenav.html.erb @@ -66,6 +66,7 @@ <% if teacher_logged_in? %> 成绩管理 + <% end %> <% if student_logged_in? %> diff --git a/config/routes.rb b/config/routes.rb index fd8bddd..198f500 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". 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 eed474d..4b3d2f5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,9 +12,11 @@ # It's strongly recommended that you check this file into your version control system. + ActiveRecord::Schema.define(version: 20161228082748) do + # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" 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