diff --git a/Gemfile b/Gemfile index b9e1b4e2..f4697bf5 100644 --- a/Gemfile +++ b/Gemfile @@ -62,6 +62,9 @@ gem 'elasticsearch-rails' gem 'oauth2' +# xlsx +gem 'axlsx_rails', '0.3.0' + #Ruby 2.2+ has removed test/unit from the core library. if RUBY_VERSION>='2.2' gem 'test-unit', '~> 3.0' diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 685f9f74..344976ab 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -467,7 +467,9 @@ class ChallengesController < ApplicationController render_404 return else - if (@shixun.status == 0 && !User.current.manager_of_shixun?(@shixun)) || @shixun.status == -1 + logger.info("operable: ##{@shixun.operable?}") + logger.info("@shixun.status: ##{@shixun.status}") + if (@shixun.status == 0 && !User.current.manager_of_shixun?(@shixun)) || (!@shixun.operable? && !User.current.admin?) render_403 return end diff --git a/app/controllers/managements/schools_controller.rb b/app/controllers/managements/schools_controller.rb index 6dfe84fb..381c7493 100644 --- a/app/controllers/managements/schools_controller.rb +++ b/app/controllers/managements/schools_controller.rb @@ -7,8 +7,13 @@ class Managements::SchoolsController < Managements::BaseController params[:sort_by] ||= :teacher_count params[:sort_direction] ||= :desc - schools = Management::SchoolReportService.new(params).call + schools = Management::SchoolReportService.new(params).call @schools = paginateHelper schools + + respond_to do |format| + format.html { load_statistic_total } + format.js + end end def yesterday_data @@ -31,6 +36,13 @@ class Managements::SchoolsController < Managements::BaseController raise '参数错误' end + def statistics_xlsx + @schools = Management::SchoolReportService.new(params).call + + filename = ["学校统计总表", params[:keyword], Time.zone.now.strftime('%Y%m%d%H%M%S')].join('-') << ".xlsx" + render xlsx: "statistics_xlsx", filename: filename + end + private def set_navigation_bar @menu_type = 1 @@ -41,4 +53,13 @@ class Managements::SchoolsController < Managements::BaseController [I18n.t("school_daily_report.#{column}"), column] end end + + def load_statistic_total + @teacher_total = User.teacher.count + @student_total = User.student.count + @course_total = Course.count + @active_course_total = Course.where(is_end: false).count + @shixun_homework_total = HomeworkCommon.where(homework_type: 4).count + @other_homework_total = HomeworkCommon.where(homework_type: [1, 3]).count + end end diff --git a/app/controllers/oauth_controller.rb b/app/controllers/oauth_controller.rb index 930f2b75..16f83291 100644 --- a/app/controllers/oauth_controller.rb +++ b/app/controllers/oauth_controller.rb @@ -202,7 +202,11 @@ class OauthController < ApplicationController self.logged_user = openi.user if openi.present? && openi.user.present? original_url = params[:original_url] - redirect_to original_url + if current_user.logged? + redirect_to original_url + else + redirect_to signin_path + end end def get_token_callback diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index 0b7aa420..abe09e08 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -63,33 +63,21 @@ class HomeworkCommon < ActiveRecord::Base #作业类型 def homework_type_ch - type = self.homework_type - case type - when 1 - type = "普通" - when 2 - type = "编程" - when 3 - type = "分组" - when 4 - type = "实训" + case homework_type + when 1 then '普通' + when 2 then '编程' + when 3 then '分组' + when 4 then '实训' end - type end def homework_type_module - type = self.homework_type - case type - when 1 - type = "common_homework" - when 2 - type = "code_homework" - when 3 - type = "group_homework" - when 4 - type = "shixun_homework" + case homework_type + when 1 then 'common_homework' + when 2 then 'code_homework' + when 3 then 'group_homework' + when 4 then 'shixun_homework' end - type end def act_as_activity diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 54882be1..439c0e44 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -55,20 +55,27 @@ class Shixun < ActiveRecord::Base scope :visible, lambda{where(status: [2,3])} - scope :min, lambda { select([:id, :name, :gpid, :modify_time, :reset_time, :language, :propaedeutics, :status, :identifier, :test_set_permission, :hide_code, :forbid_copy, - :webssh, :user_id, :code_hidden, :task_pass, :exec_time, :multi_webssh]) } + scope :min, lambda { select([:id, :name, :gpid, :modify_time, :reset_time, :language, :propaedeutics, :status, :identifier, + :test_set_permission, :hide_code, :forbid_copy, :hidden, :webssh, :user_id, :code_hidden, + :task_pass, :exec_time, :multi_webssh]) } scope :published, lambda{where(status: 2)} scope :field_for_recommend, lambda{ select([:id, :name, :identifier, :myshixuns_count]) } include ApplicationHelper has_many :tidings, :as => :container, :dependent => :destroy - scope :visible, -> { where(status: -1) } + #scope :visible, -> { where(status: -1) } after_create :send_tiding def should_compile? self.mirror_repositories.published_main_mirror.first.try(:should_compile) end + # 可供使用的实训 + def operable? + logger.info("####") + self.status != -1 && !self.hidden + end + def is_published? self.status > 1 ? true : false end diff --git a/app/services/games_service.rb b/app/services/games_service.rb index bddb03b7..f172edbd 100644 --- a/app/services/games_service.rb +++ b/app/services/games_service.rb @@ -17,7 +17,8 @@ class GamesService myshixun = Myshixun.min.find(game.myshixun_id) shixun = Shixun.min.find(myshixun.shixun_id) - unless (myshixun.user_id == current_user.id || current_user.admin? || current_user.business? || current_user.id == shixun.try(:user_id) || current_user.is_certification_teacher) + unless ((myshixun.user_id == current_user.id || current_user.business? || current_user.id == shixun.try(:user_id) || + current_user.is_certification_teacher) && (shixun.operable?)) || current_user.admin? return{:status => 403} end game_challenge = Challenge.min.find(game.challenge_id) diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 5a962bd5..e9ee1bae 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -39,7 +39,7 @@ class Management::SchoolReportService ( SELECT COUNT(*) FROM homework_commons hc LEFT JOIN courses ON courses.id = hc.course_id - WHERE courses.school_id = schools.id + WHERE courses.school_id = schools.id AND hc.homework_type = 4 ) homework_count, ( SELECT COUNT(*) FROM homework_commons hc diff --git a/app/views/layouts/base_management.html.erb b/app/views/layouts/base_management.html.erb index e038f1c2..201e26f1 100644 --- a/app/views/layouts/base_management.html.erb +++ b/app/views/layouts/base_management.html.erb @@ -44,10 +44,6 @@
  • <%= link_to "课堂列表", classroom_classment_managements_path %>
  • <%= link_to "实训作业", shixun_task_managements_path%>
  • <%= link_to "项目列表", project_list_managements_path%>
  • - - - -
  • 精选实训+ diff --git a/app/views/managements/schools/_data_contrast_list.html.erb b/app/views/managements/schools/_data_contrast_list.html.erb index af63aee8..c2699601 100644 --- a/app/views/managements/schools/_data_contrast_list.html.erb +++ b/app/views/managements/schools/_data_contrast_list.html.erb @@ -35,20 +35,20 @@ percentage = report['total'].zero? ? increase.to_f * 100 : (increase / report['total'].to_f) * 100 %> <% if increase > 0 %> - + +<%= increase %> - +<%= percentage.round(5) %>% + +<%= percentage.round(5) %>% <% elsif increase.zero? %> <%= increase %> <%= percentage.round(5) %>% <% else %> - + <%= increase %> - <%= percentage.round(5) %>% + <%= percentage.round(5) %>% <% end %> <% end %> diff --git a/app/views/managements/schools/_statistics_list.html.erb b/app/views/managements/schools/_statistics_list.html.erb index 609a1cca..cffb316c 100644 --- a/app/views/managements/schools/_statistics_list.html.erb +++ b/app/views/managements/schools/_statistics_list.html.erb @@ -17,7 +17,7 @@ <% @schools.each_with_index do |school, index| %> - <% p school %><%= (@obj_pages.page - 1) * @obj_pages.per_page + index + 1 %> + <%= (@obj_pages.page - 1) * @obj_pages.per_page + index + 1 %> <%= school['id'] %> <%= school['name'] %> <%= school['teacher_count'] %> diff --git a/app/views/managements/schools/statistics.html.erb b/app/views/managements/schools/statistics.html.erb index 1597fe45..5a68f75c 100644 --- a/app/views/managements/schools/statistics.html.erb +++ b/app/views/managements/schools/statistics.html.erb @@ -4,7 +4,26 @@ class: 'fl task-form-30 task-height-30 mt10', style: 'margin: 10px 10px 10px 25px;' %> <%= link_to '搜索', 'javascript:void(0)', class: 'fl task-btn task-btn-orange ml5 mt10', onclick: "$('#school_report_search_form').submit();" %> <%= link_to '清除', 'javascript:clearSearchCondition()', class: 'fl task-btn ml5 mt2 mt10' %> + +
    + <%= link_to '导出Excel', school_statistics_xlsx_managements_path(format: :xlsx), + class: 'fl task-btn task-btn-orange mt10 mr20 export-statistic-btn', + onclick: 'exportSchoolStatistic();' %> +
    <% end %> + + + +
    +
    + 统计总计: + 教师总人数<%= @teacher_total %>人, + 学生总人数<%= @student_total %>人, + 课堂总数<%= @course_total %>个, + 正在进行课堂总数<%= @active_course_total %>个, + 实训作业总数<%= @shixun_homework_total %>个, + 其它作业总数<%= @other_homework_total %>个, +
    @@ -16,4 +35,11 @@ $("#school_report_search_form input[name='keyword']").val(""); $('#school_report_search_form').submit(); } + + function exportSchoolStatistic(){ + var form = $("#school_report_search_form") + var exportLink = form.find(".export-statistic-btn"); + var keyword = form.find("input[name='keyword'").val(); + exportLink.attr("href", exportLink.attr("href") + "?keyword=" + keyword); + } \ No newline at end of file diff --git a/app/views/managements/schools/statistics_xlsx.xlsx.axlsx b/app/views/managements/schools/statistics_xlsx.xlsx.axlsx new file mode 100644 index 00000000..b68a817c --- /dev/null +++ b/app/views/managements/schools/statistics_xlsx.xlsx.axlsx @@ -0,0 +1,20 @@ +wb = xlsx_package.workbook +wb.add_worksheet(name: "统计总表") do |sheet| + sheet.add_row %w(ID 单位名称 教师总人数 学生总人数 课堂总数 正在进行课堂数 实训作业总数 其它作业总数 动态时间) + + total = @schools.count + buffer_size = 500 + times = total % 500 > 0 ? total / 500 + 1 : total / 500 + + times.times do |index| + schools = @schools.limit(buffer_size).offset(index * buffer_size) + + schools.each do |school| + sheet.add_row([ + school['id'].to_s, school['name'].to_s, school['teacher_count'].to_s, school['student_count'].to_s, + school['course_count'].to_s, school['active_course_count'].to_s, school['homework_count'].to_s, + school['other_homework_count'].to_s, format_time(school['nearly_course_time']) + ]) + end + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 26cddf96..85ec25c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -728,6 +728,7 @@ RedmineApp::Application.routes.draw do ## oauth相关 get :school_report, controller: 'managements::schools', action: 'statistics' get :school_yesterday_data, controller: 'managements::schools', action: 'yesterday_data' get :school_data_contrast, controller: 'managements::schools', action: 'data_contrast' + get :school_statistics_xlsx, controller: 'managements::schools', action: 'statistics_xlsx' end end # Enable Grack support