From e588128f9016ee99881c25e6b380ec21dec6db85 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 6 Sep 2019 18:20:03 +0800 Subject: [PATCH 1/6] sms --- app/services/users/apply_professional_auth_service.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/services/users/apply_professional_auth_service.rb b/app/services/users/apply_professional_auth_service.rb index 07afe18ae..a920caf55 100644 --- a/app/services/users/apply_professional_auth_service.rb +++ b/app/services/users/apply_professional_auth_service.rb @@ -38,7 +38,11 @@ class Users::ApplyProfessionalAuthService < ApplicationService move_image_file! unless params[:upload_image].to_s == 'false' - sms_notify_admin + sms_cache = Rails.cache.read("apply_pro_certification") + if sms_cache.nil? + sms_notify_admin + Rails.cache.write("apply_pro_certification", 1) + end end end From 4005ebf768af6a99e3ea7af522a9238504f5e77f Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Mon, 9 Sep 2019 19:08:16 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/users/apply_professional_auth_service.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/services/users/apply_professional_auth_service.rb b/app/services/users/apply_professional_auth_service.rb index a920caf55..3ba9fbafd 100644 --- a/app/services/users/apply_professional_auth_service.rb +++ b/app/services/users/apply_professional_auth_service.rb @@ -38,11 +38,11 @@ class Users::ApplyProfessionalAuthService < ApplicationService move_image_file! unless params[:upload_image].to_s == 'false' - sms_cache = Rails.cache.read("apply_pro_certification") - if sms_cache.nil? - sms_notify_admin + # sms_cache = Rails.cache.read("apply_pro_certification") + # if sms_cache.nil? + # sms_notify_admin Rails.cache.write("apply_pro_certification", 1) - end + # end end end From 3b4f6c1a271d3b5ee4d7bdb5223e75dfdf115524 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Mon, 9 Sep 2019 19:48:27 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=AE=9E=E8=B7=B5=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E7=9A=84=E5=B7=B2=E5=AD=A6=E5=85=B3=E5=8D=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/subjects_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index 7c76748b1..952301053 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -442,7 +442,6 @@ class SubjectsController < ApplicationController # 用户进展和获取的标签 def user_subject_progress challenge_ids pass_games = Game.select(:id, :cost_time, :challenge_id).where(status: 2, user_id: current_user.id, challenge_id: challenge_ids) if current_user.logged? - @all_score = Challenge.where(id: challenge_ids).sum(:score) # 如果没有通关的,没必要再继续统计了 if pass_games.blank? @@ -451,6 +450,7 @@ class SubjectsController < ApplicationController @time = 0 @user_tags = [] else + @all_score = Challenge.where(id: challenge_ids).size pass_challenge_ids = pass_games.map(&:challenge_id).uniq # 按道理是不用去重的,但是历史数据与重复 subject_challenge_count = @subject.shixuns.sum(:challenges_count) # 用户通关获得的标签 @@ -460,7 +460,7 @@ class SubjectsController < ApplicationController subject_challenge_count == 0 ? 0 : ((pass_challenge_ids.size.to_f / subject_challenge_count).round(2) * 100).to_i # 用户通关分数 - @my_score = Challenge.where(id: pass_challenge_ids).pluck(:score).sum + @my_score = Challenge.where(id: pass_challenge_ids).size @time = pass_games.map(&:cost_time).sum end From baba6aa9e95d959ab445e559889d6ab8074bcc14 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 10 Sep 2019 14:31:59 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/course_stage.rb | 9 +++++++++ app/models/course_stage_shixun.rb | 5 +++++ db/migrate/20190910061807_create_course_stages.rb | 13 +++++++++++++ .../20190910062710_create_course_stage_shixuns.rb | 12 ++++++++++++ spec/models/course_stage_shixun_spec.rb | 5 +++++ spec/models/course_stage_spec.rb | 5 +++++ 6 files changed, 49 insertions(+) create mode 100644 app/models/course_stage.rb create mode 100644 app/models/course_stage_shixun.rb create mode 100644 db/migrate/20190910061807_create_course_stages.rb create mode 100644 db/migrate/20190910062710_create_course_stage_shixuns.rb create mode 100644 spec/models/course_stage_shixun_spec.rb create mode 100644 spec/models/course_stage_spec.rb diff --git a/app/models/course_stage.rb b/app/models/course_stage.rb new file mode 100644 index 000000000..27eb9de30 --- /dev/null +++ b/app/models/course_stage.rb @@ -0,0 +1,9 @@ +class CourseStage < ApplicationRecord + belongs_to :course, counter_cache: true + + has_many :course_stage_shixuns, -> { order("course_stage_shixuns.position ASC") }, dependent: :destroy + has_many :shixuns, :through => :course_stage_shixuns + + validates :name, length: { maximum: 60 } + validates :description, length: { maximum: 300 } +end diff --git a/app/models/course_stage_shixun.rb b/app/models/course_stage_shixun.rb new file mode 100644 index 000000000..be5a1bb7d --- /dev/null +++ b/app/models/course_stage_shixun.rb @@ -0,0 +1,5 @@ +class CourseStageShixun < ApplicationRecord + belongs_to :course, counter_cache: :stages_count, counter_cache: :shixuns_count + belongs_to :course_stage + belongs_to :shixun, counter_cache: :shixuns_count +end diff --git a/db/migrate/20190910061807_create_course_stages.rb b/db/migrate/20190910061807_create_course_stages.rb new file mode 100644 index 000000000..c664b7f92 --- /dev/null +++ b/db/migrate/20190910061807_create_course_stages.rb @@ -0,0 +1,13 @@ +class CreateCourseStages < ActiveRecord::Migration[5.2] + def change + create_table :course_stages do |t| + t.references :course, index: true + t.string :name + t.text :description + t.integer :position + t.integer :shixuns_count + + t.timestamps + end + end +end diff --git a/db/migrate/20190910062710_create_course_stage_shixuns.rb b/db/migrate/20190910062710_create_course_stage_shixuns.rb new file mode 100644 index 000000000..04512c7da --- /dev/null +++ b/db/migrate/20190910062710_create_course_stage_shixuns.rb @@ -0,0 +1,12 @@ +class CreateCourseStageShixuns < ActiveRecord::Migration[5.2] + def change + create_table :course_stage_shixuns do |t| + t.references :course, index: true + t.references :course_stage, index: true + t.references :shixun, index: true + t.integer :position + + t.timestamps + end + end +end diff --git a/spec/models/course_stage_shixun_spec.rb b/spec/models/course_stage_shixun_spec.rb new file mode 100644 index 000000000..716af2b97 --- /dev/null +++ b/spec/models/course_stage_shixun_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CourseStageShixun, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/course_stage_spec.rb b/spec/models/course_stage_spec.rb new file mode 100644 index 000000000..06c711908 --- /dev/null +++ b/spec/models/course_stage_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CourseStage, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 4f503270aab596076b7b663a1cc294009d58dd93 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 10 Sep 2019 15:30:49 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 2 ++ app/models/course.rb | 11 +++++++++++ app/models/course_stage.rb | 2 +- app/models/course_stage_shixun.rb | 6 +++--- db/migrate/20190910063345_migrate_course_stages.rb | 14 ++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20190910063345_migrate_course_stages.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 2039dbc6f..c12b80da8 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -166,6 +166,8 @@ class CoursesController < ApplicationController end Inform.create(container: @course, description: @subject.learning_notes, name: "学习须知") + + @course.create_stages @course.subject end course_module_types = params[:course_module_types] diff --git a/app/models/course.rb b/app/models/course.rb index 54d897b07..2722c3b24 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -333,6 +333,17 @@ class Course < ApplicationRecord teacher_power_courses end + def create_stages subject + if subject + subject.stages.each do |stage| + new_stage = CourseStage.create!(course_id: id, name: stage.name, description: stage.description, position: stage.position) + stage.stage_shixuns.each do |stage_shixun| + CourseStageShixun.create!(course_id: id, course_stage_id: new_stage.id, shixun_id: stage_shixun.shixun_id, position: stage_shixun.position) + end + end + end + end + private #创建课程后,给该用户发送消息 diff --git a/app/models/course_stage.rb b/app/models/course_stage.rb index 27eb9de30..f105e25f6 100644 --- a/app/models/course_stage.rb +++ b/app/models/course_stage.rb @@ -1,5 +1,5 @@ class CourseStage < ApplicationRecord - belongs_to :course, counter_cache: true + belongs_to :course has_many :course_stage_shixuns, -> { order("course_stage_shixuns.position ASC") }, dependent: :destroy has_many :shixuns, :through => :course_stage_shixuns diff --git a/app/models/course_stage_shixun.rb b/app/models/course_stage_shixun.rb index be5a1bb7d..6ab2707a4 100644 --- a/app/models/course_stage_shixun.rb +++ b/app/models/course_stage_shixun.rb @@ -1,5 +1,5 @@ class CourseStageShixun < ApplicationRecord - belongs_to :course, counter_cache: :stages_count, counter_cache: :shixuns_count - belongs_to :course_stage - belongs_to :shixun, counter_cache: :shixuns_count + belongs_to :course + belongs_to :course_stage, counter_cache: :shixuns_count + belongs_to :shixun end diff --git a/db/migrate/20190910063345_migrate_course_stages.rb b/db/migrate/20190910063345_migrate_course_stages.rb new file mode 100644 index 000000000..0aae05103 --- /dev/null +++ b/db/migrate/20190910063345_migrate_course_stages.rb @@ -0,0 +1,14 @@ +class MigrateCourseStages < ActiveRecord::Migration[5.2] + def change + Course.where(excellent: 1).each do |course| + if course.subject + course.subject.stages.each do |stage| + new_stage = CourseStage.create!(course_id: course.id, name: stage.name, description: stage.description, position: stage.position) + stage.stage_shixuns.each do |stage_shixun| + CourseStageShixun.create!(course_id: course.id, course_stage_id: new_stage.id, shixun_id: stage_shixun.shixun_id, position: stage_shixun.position) + end + end + end + end + end +end From 51ff15554d764058981b9ad165b5fffb121bef85 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 10 Sep 2019 17:01:15 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E7=9A=84=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/javascripts/course_stages.js | 2 + app/assets/stylesheets/course_stages.scss | 3 + app/controllers/course_stages_controller.rb | 105 ++++++++++++++++++ app/controllers/courses_controller.rb | 4 +- app/helpers/course_stages_helper.rb | 2 + app/helpers/courses_helper.rb | 8 +- app/models/course.rb | 16 +++ app/views/course_stages/edit.json.jbuilder | 11 ++ .../courses/online_learning.json.jbuilder | 4 +- config/routes.rb | 7 ++ .../course_stages_controller_spec.rb | 5 + spec/helpers/course_stages_helper_spec.rb | 15 +++ 12 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 app/assets/javascripts/course_stages.js create mode 100644 app/assets/stylesheets/course_stages.scss create mode 100644 app/controllers/course_stages_controller.rb create mode 100644 app/helpers/course_stages_helper.rb create mode 100644 app/views/course_stages/edit.json.jbuilder create mode 100644 spec/controllers/course_stages_controller_spec.rb create mode 100644 spec/helpers/course_stages_helper_spec.rb diff --git a/app/assets/javascripts/course_stages.js b/app/assets/javascripts/course_stages.js new file mode 100644 index 000000000..dee720fac --- /dev/null +++ b/app/assets/javascripts/course_stages.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/course_stages.scss b/app/assets/stylesheets/course_stages.scss new file mode 100644 index 000000000..faa60b4b0 --- /dev/null +++ b/app/assets/stylesheets/course_stages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the course_stages 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/course_stages_controller.rb b/app/controllers/course_stages_controller.rb new file mode 100644 index 000000000..92e44a336 --- /dev/null +++ b/app/controllers/course_stages_controller.rb @@ -0,0 +1,105 @@ +class CourseStagesController < ApplicationController + before_action :require_login + before_action :find_course, only: [:create] + before_action :find_course_stage, only: [:update, :destroy, :edit, :up_position, :down_position] + before_action :user_course_identity, :teacher_allowed + + def create + ActiveRecord::Base.transaction do + begin + @stage = CourseStage.new(stage_params) + @stage.course_id = @course.id + @stage.position = @course.course_stages.count + 1 + @stage.save! + unless params[:shixun_id].blank? + shixuns = Shixun.where(id: params[:shixun_id]).order("field(id, #{params[:shixun_id].join(",")})") + shixuns.each do |shixun| + CourseStageShixun.create!(course_stage_id: @stage.id, course_id: @course.id, shixun_id: shixun.id, position: @stage.course_stage_shixuns.count + 1) + end + end + normal_status("创建成功") + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) + raise ActiveRecord::Rollback + end + end + end + + def edit + + end + + def update + ActiveRecord::Base.transaction do + begin + @stage.update_attributes!(stage_params) + @stage.course_stage_shixuns.destroy_all + unless params[:shixun_id].blank? + params[:shixun_id].each do |shixun_id| + shixun = Shixun.where(id: shixun_id).first + @stage.course_stage_shixuns.create!(course_id: @course.id, shixun_id: shixun.id, position: @stage.course_stage_shixuns.count + 1) if shixun.present? + end + end + normal_status("更新成功") + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) + raise ActiveRecord::Rollback + end + end + end + + def destroy + ActiveRecord::Base.transaction do + @course.course_stages.where("position > ?", @stage.position).update_all("position = position - 1") + @stage.destroy! + normal_status("删除成功") + end + end + + def up_position + ActiveRecord::Base.transaction do + begin + position = @stage.position + tip_exception("第一章不能向上移动") if @stage.position == 1 + pre_stage = @course.course_stages.where(position: position - 1).first + pre_stage.update_attributes(position: position) + @stage.update_attributes(position: position - 1) + normal_status("更新成功") + rescue Exception => e + uid_logger("stage up failed: #{e.message}") + raise ActiveRecord::Rollback + end + end + end + + def down_position + ActiveRecord::Base.transaction do + begin + position = @stage.position + rails "最后一章不能向下移动" if @stage.position == @course.course_stages.count + next_stage = @course.course_stages.where(position: position + 1).first + next_stage.update_attributes(position: position) + @stage.update_attributes(position: position + 1) + normal_status("更新成功") + rescue Exception => e + uid_logger("stage up failed: #{e.message}") + raise ActiveRecord::Rollback + end + end + end + + private + + def find_course_stage + @stage = CourseStage.find_by!(id: params[:id]) + @course = @stage.course + end + + def stage_params + tip_exception("章节名称不能为空") if params[:name].blank? + params.permit(:name, :description) + end + +end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index c12b80da8..fc16c050d 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -267,9 +267,9 @@ class CoursesController < ApplicationController def online_learning @subject = @course.subject - @stages = @subject&.stages + @stages = @course.course_stages @user = current_user - @start_learning = @user_course_identity == Course::STUDENT && @subject&.learning?(current_user.id) + @start_learning = @user_course_identity == Course::STUDENT && @course.learning?(current_user.id) end def search_course_list diff --git a/app/helpers/course_stages_helper.rb b/app/helpers/course_stages_helper.rb new file mode 100644 index 000000000..7ebb68d9a --- /dev/null +++ b/app/helpers/course_stages_helper.rb @@ -0,0 +1,2 @@ +module CourseStagesHelper +end diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index cdb3225d6..6f6c76d04 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -269,10 +269,10 @@ module CoursesHelper group_info end - def last_subject_shixun user_id, subject - myshixun = Myshixun.where(user_id: user_id, shixun_id: subject&.shixuns).order("updated_at desc").first + def last_subject_shixun user_id, course + myshixun = Myshixun.where(user_id: user_id, shixun_id: course.shixuns).order("updated_at desc").first return "" unless myshixun - stage_shixun = subject&.stage_shixuns.where(shixun_id: myshixun.shixun_id).take - progress = stage_shixun&.stage&.position.to_s + "-" + stage_shixun&.position.to_s + " " + myshixun.shixun&.name + stage_shixun = course.course_stage_shixuns.where(shixun_id: myshixun.shixun_id).take + progress = stage_shixun&.course_stage&.position.to_s + "-" + stage_shixun&.position.to_s + " " + myshixun.shixun&.name end end diff --git a/app/models/course.rb b/app/models/course.rb index 2722c3b24..c66c597d4 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -70,6 +70,11 @@ class Course < ApplicationRecord has_many :course_acts, class_name: 'CourseActivity', as: :course_act, dependent: :destroy has_many :tidings, as: :container, dependent: :destroy + # 开放课堂 + has_many :course_stages, -> { order("course_stages.position ASC") }, dependent: :destroy + has_many :course_stage_shixuns, dependent: :destroy + has_many :shixuns, through: :course_stage_shixuns + # 老版的members弃用 现用course_members has_many :members @@ -344,6 +349,17 @@ class Course < ApplicationRecord end end + def learning? user_id + Myshixun.where(user_id: user_id, shixun_id: shixuns).exists? + end + + def my_subject_progress + my_challenge_count = Game.joins(:challenge).where(user_id: User.current.id, status: 2, challenges: {shixun_id: shixuns.published_closed}). + pluck(:challenge_id).uniq.size + course_challeng_count = course.shixuns.pluck(:challenges_count).sum + count = course_challeng_count == 0 ? 0 : ((my_challenge_count.to_f / course_challeng_count).round(2) * 100).to_i + end + private #创建课程后,给该用户发送消息 diff --git a/app/views/course_stages/edit.json.jbuilder b/app/views/course_stages/edit.json.jbuilder new file mode 100644 index 000000000..f76efb15f --- /dev/null +++ b/app/views/course_stages/edit.json.jbuilder @@ -0,0 +1,11 @@ +json.stage_id @stage.id +json.stage_name @stage.name +json.stage_description @stage.description +json.delete_path "/course_stages/#{@stage.id}" +json.shixuns_list do + json.array! @stage.shixuns.each do |shixun| + json.shixun_identifier shixun.identifier + json.shixun_name shixun.name + json.shixun_id shixun.id + end +end \ No newline at end of file diff --git a/app/views/courses/online_learning.json.jbuilder b/app/views/courses/online_learning.json.jbuilder index bec8ff301..9c87743ee 100644 --- a/app/views/courses/online_learning.json.jbuilder +++ b/app/views/courses/online_learning.json.jbuilder @@ -2,10 +2,10 @@ json.stages @stages do |stage| json.partial! 'stages/stage', locals: {stage: stage, user:@user, subject:@subject} end -json.description @subject&.description +# json.description @subject&.description json.start_learning @start_learning -json.learned @start_learning ? @subject&.my_subject_progress : 0 +json.learned @start_learning ? @course.my_subject_progress : 0 json.last_shixun @start_learning ? last_subject_shixun(@user.id, @subject) : "" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 5cf74c29a..9e6b031be 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -371,6 +371,13 @@ Rails.application.routes.draw do get 'search_slim' end + resources :course_stages, shallow: true do + member do + post :up_position + post :down_position + end + end + resources :polls, only:[:index,:new,:create] do collection do post :publish # 立即发布 diff --git a/spec/controllers/course_stages_controller_spec.rb b/spec/controllers/course_stages_controller_spec.rb new file mode 100644 index 000000000..e63a2b6a4 --- /dev/null +++ b/spec/controllers/course_stages_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CourseStagesController, type: :controller do + +end diff --git a/spec/helpers/course_stages_helper_spec.rb b/spec/helpers/course_stages_helper_spec.rb new file mode 100644 index 000000000..bb1081a10 --- /dev/null +++ b/spec/helpers/course_stages_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the CourseStagesHelper. For example: +# +# describe CourseStagesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe CourseStagesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end