Merge branch 'master' into master

This commit is contained in:
stormand 2017-01-05 20:46:31 +08:00 committed by GitHub
commit 3782327fa7
31 changed files with 1176 additions and 265 deletions

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value />
</option>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</component>
</project>

View File

@ -1,30 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
<State>
<id>CSS</id>
</State>
<State>
<id>Probable bugsCSS</id>
</State>
<State>
<id>RELAX NG</id>
</State>
</expanded-state>
<selected-state>
<State>
<id>CoffeeScript</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
@ -36,19 +11,4 @@
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-2.2.4" project-jdk-type="RUBY_SDK" />
<component name="masterDetails">
<states>
<state key="ScopeChooserConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -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/

View File

@ -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/

View File

@ -62,15 +62,15 @@ class CoursesController < ApplicationController
end
def excel_in
@course1 = Course.find_by_id(params[:id])
if @course1.update_attributes(course_params)
@course = Course.find_by_id(params[:id])
if @course.update_attributes(course_params)
flash={:info => " 导入成功"}
else
flash={:warning => "导入失败"}
end
@course=current_user.teaching_courses
@params=params
redirect_to courses_path, flash: flash
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

View File

@ -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

View File

@ -15,6 +15,35 @@ class GradesController < ApplicationController
end
def excel_in
require 'importex'
Excel <Importex::Base
column "Name",:required=> 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])

View File

@ -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
def create
@user = User.new(user_params)
if @user.save
redirect_to root_url, flash: {success: "新账号注册成功,请登陆"}
else
else
flash[:warning] = "账号信息填写有误,请重试"
render 'new'
end
end
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)
def update
@user = User.find_by_id(params[:id])
if @user.update_attributes(user_params)
flash={:info => "更新成功"}
else
else
flash={:warning => "更新失败"}
end
redirect_to root_path, flash: flash
end
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,
def user_params
params.require(:user).permit(:name, :email, :major, :department, :password,
:password_confirmation)
end
end
# Confirms a logged-in user.
def logged_in
unless logged_in?
# Confirms a logged-in user.
def logged_in
unless logged_in?
redirect_to root_url, flash: {danger: '请登陆'}
end
end
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
unless current_user?(@user)
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
unless current_user?(@user)
redirect_to root_url, flash: {:warning => '此操作需要管理员身份'}
end
end
end
end
# Confirms a logged-in user.
def teacher_logged_in
unless teacher_logged_in?
# Confirms a logged-in user.
def teacher_logged_in
unless teacher_logged_in?
redirect_to root_url, flash: {danger: '请登陆'}
end
end
end
end
end

View File

@ -0,0 +1,2 @@
module ExcelHelper
end

View File

@ -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

49
app/models/excel.rb Normal file
View File

@ -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 <Importex::Base
column "Name",:required=> 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

53
app/models/excelparse.rb Normal file
View File

@ -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
}

View File

@ -115,6 +115,17 @@
</div>
</div>
<div class="form-group">
<%= f.label "excel", class: 'col-sm-3 control-label' %>
<div class="col-sm-9">
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-arrow-right"></span></div>
<%= f.file_field :excel %>
<%= f.hidden_field :excel_cache %>
</div>
</div>
</div>

View File

@ -0,0 +1,75 @@
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<%= render "shared/sidenav" %>
</div>
<div class="col-sm-10">
<div class="panel panel-primary">
<div class="panel-heading">
</div>
<div class="panel-body" style="min-height: 450px">
<table class="table table-striped table-hover">
<thead>
<caption></caption>
<tr>
<th align="center" >课程名称</th>
<th align="right">Excel文件导入</th>
</tr>
</tablebotder>
</thead>
<tbody>
<% @course.each do |course| %>
<tr>
<td align="left" ><%= link_to course.name, detail_course_url(course), class: 'btn-sm' %></td>
<!--<a href="link_to course.excel"></a>-->
<!--<td align="left"><%= link_to course.excel, detail_course_path(course), class: 'btn-sm' %></td>-->
<%= form_for course, url: excel_in_course_path(course) , html: {class: 'form-horizontal', role: 'form'} do |f| %>
<div class="form-group">
<%= f.label "上传文件", class: 'col-sm-3 control-label' %>
<div class="col-sm-3">
<div class="input-group">
<%= f.file_field :excel %>
<%= f.hidden_field :excel_cache %>
</div>
</div>
</div>
<%= f.submit '提交', class: "btn btn-success btn-block" %>
<% end %>
<td><a class="btn btn-xs btn-success" href=<%="http://localhost:3000#{course.excel}"%> >下载最新资料</a></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<button id="downloadBt" style= "width:80px;height:25px;padding:2px;font-size:13px;color:black;left:90%; top:69%;position:absolute;z-index:100" onclick="downloadSchedule()">下载表格</button>
</div>
<!--<script src="../src/jquery.min.js"></script>-->
<!--<script src="../src/jquery.table2excel.js"></script>-->
<script>
function downloadSchedule() {
$(".table2excel").table2excel({
exclude: ".noExl",
name: "Excel Document Name",
filename: "<%= current_user.name %>课表",
fileext: ".xls",
exclude_img: true,
exclude_links: true,
exclude_inputs: true
});
}
</script>

View File

@ -45,6 +45,7 @@
<td><%= course.credit %></td>
<td><%= course.exam_type %></td>
<td><%= course.teacher.name %></td>
<td><%course.excel%></td>
<% if teacher_logged_in? %>
<td><%= link_to "编辑", edit_course_url(course), class: 'btn btn-xs btn-info' %></td>
@ -74,14 +75,21 @@
<% elsif student_logged_in? %>
<td><%= link_to "删除", quit_course_path(course), class: 'btn-sm btn-danger' %></td>
<td><a class="btn btn-xs btn-success" href=<%="http://localhost:3000#{course.excel}"%> >下载最新资料</a></td>
<% if course.degree %>
<td><%= link_to "否", nodegree_course_path(course), class: 'btn btn-xs btn-danger' %></td>
<% else %>
<td><%= link_to "是",isdegree_course_path(course), class: 'btn btn-xs btn-info' %></td>
<% end %>
<td><a class="btn btn-xs btn-success" href=<%="https://courseselectofstormand.herokuapp.com#{course.excel}"%> >下载最新资料</a></td></td>
<td><%= course.excel %></td>
<% end %>
</tr>
<% end %>
</tbody>

View File

@ -0,0 +1,2 @@
<h1>Hello,Rails</h1>
<p>Find me in app/views/excel/parse.html.erb</p>

View File

@ -34,7 +34,65 @@
<td><%= grade.user.department %></td>
<td><%= grade.course.name %></td>
<td><%= grade.grade %></td>
<!--
<td><%= grade.course.excel %></td>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript遍历table</title>
<style type="text/css">
tr td,tr th{
border:#CCC 1px solid;
}
</style>
<script type="text/javascript">
/**
* JavaScript遍历table
*/
function eachTableRow()
{
//获取table序号
var tab=document.getElementById("course.excel");
//获取行数
var rows=tab.rows;
//遍历行
for(var i=1;i<rows.length;i++)
{
//遍历表格列
for(var j=0;j<rows[i].cells.length;j++)
{
//打印某行某列的值
alert("第"+(i+1)+"行,第"+(j+1)+"列的值是:"+rows[i].cells[j].innerHTML);
}
}
}
</script>
</head>
<body>
<table id="tab" cellpadding="0" cellspacing="0" style="width:50%; text-align:center; border:#CCC 1px solid;">
<tr style="border:#CCC 1px solid;">
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<tr>
<td>2014010101</td>
<td>张思思</td>
<td>20</td>
<td>女</td>
</tr>
<tr>
<td>2014010102</td>
<td>立三</td>
<td>21</td>
<td>男</td>
</tr>
</table>
<input type="button" value="遍历" onclick="eachTableRow()"/>
</body>
-->
<% if teacher_logged_in? %>
<%= form_for grade, url: grade_url(id: grade.id, course_id: grade.course.id), method: :patch do |f| %>
<td><%= f.text_field :grade %></td>

View File

@ -66,6 +66,7 @@
<% if teacher_logged_in? %>
<a data-toggle="collapse" data-parent="#accordion" href="#GradeCollapse"><span class="glyphicon glyphicon-user"></span>
成绩管理</a>
<% end %>
<% if student_logged_in? %>

View File

@ -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".

View File

@ -0,0 +1,5 @@
class AddExcelToCourse < ActiveRecord::Migration
def change
add_column :courses, :excel, :string
end
end

View File

@ -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"

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

View File

@ -0,0 +1 @@
RM-162.2380.8

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,9 @@
require 'test_helper'
class ExcelControllerTest < ActionController::TestCase
test "should get parse" do
get :parse
assert_response :success
end
end