From 5d36f8c9ed04451717d8483be14a33f7c5eb017b Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 14 Nov 2019 19:49:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/weapps/courses_controller.rb | 14 +++++++-- app/forms/weapps/update_course_form.rb | 15 ++++++++++ app/services/weapps/create_course_service.rb | 7 +++++ app/services/weapps/update_course_service.rb | 31 ++++++++++++++++++++ app/views/weapps/courses/edit.json.jbuilder | 2 ++ 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 app/forms/weapps/update_course_form.rb create mode 100644 app/services/weapps/update_course_service.rb create mode 100644 app/views/weapps/courses/edit.json.jbuilder diff --git a/app/controllers/weapps/courses_controller.rb b/app/controllers/weapps/courses_controller.rb index c7ecb89c9..38f1b99f9 100644 --- a/app/controllers/weapps/courses_controller.rb +++ b/app/controllers/weapps/courses_controller.rb @@ -1,5 +1,6 @@ class Weapps::CoursesController < Weapps::BaseController # before_action :require_wechat_login! + before_action :teacher_allowed, except: [:create] def create return render_error("只有老师身份才能创建课堂") unless current_user.is_teacher? @@ -12,11 +13,12 @@ class Weapps::CoursesController < Weapps::BaseController end def edit - + @course = current_course end def update - + Weapps::UpdateCourseService.call(current_course, course_params) + render_ok end private @@ -24,4 +26,12 @@ class Weapps::CoursesController < Weapps::BaseController def course_params params.permit(:name, :course_list_name, :credit, course_module_types: []) end + + def current_course + @_current_course = Course.find params[:id] + end + + def teacher_allowed + return render_forbidden unless current_user.course_identity(current_course) < Course::STUDENT + end end \ No newline at end of file diff --git a/app/forms/weapps/update_course_form.rb b/app/forms/weapps/update_course_form.rb new file mode 100644 index 000000000..6fb1d81d9 --- /dev/null +++ b/app/forms/weapps/update_course_form.rb @@ -0,0 +1,15 @@ +class Weapps::UpdateCourseForm + include ActiveModel::Model + + attr_accessor :course + attr_accessor :name, :course_list_name, :credit + + validates :name, presence: true + validates :course_list_name, presence: true + + validate :course_name_prefix + + def course_name_prefix + raise '课堂名称应以课程名称开头' unless name.index(course_list_name) && name.index(course_list_name) == 0 + end +end \ No newline at end of file diff --git a/app/services/weapps/create_course_service.rb b/app/services/weapps/create_course_service.rb index d7dd81ffe..196533bb6 100644 --- a/app/services/weapps/create_course_service.rb +++ b/app/services/weapps/create_course_service.rb @@ -14,6 +14,13 @@ class Weapps::CreateCourseService < ApplicationService course.school_id = course.teacher&.school_id course.is_public = 0 course.credit = params[:credit].blank? ? nil : params[:credit] + course_list = CourseList.find_by(name: params[:course_list_name].to_s.strip) + if course_list + course.course_list_id = course_list.id + else + new_course_list = CourseList.create!(name: params[:course_list_name].to_s.strip, user_id: course.tea_id, is_admin: 0) + course.course_list_id = new_course_list.id + end course.save! course.generate_invite_code diff --git a/app/services/weapps/update_course_service.rb b/app/services/weapps/update_course_service.rb new file mode 100644 index 000000000..4f2fa4676 --- /dev/null +++ b/app/services/weapps/update_course_service.rb @@ -0,0 +1,31 @@ +class Weapps::UpdateCourseService < ApplicationService + attr_reader :course, :params + + def initialize(course, params) + @course = course + @params = params + end + + def call + Weapps::UpdateCourseForm.new(form_params).validate! + + ActiveRecord::Base.transaction do + course.name = params[:name].to_s.strip + course.credit = params[:credit].blank? ? nil : params[:credit] + course_list = CourseList.find_by(name: params[:course_list_name].to_s.strip) + if course_list + course.course_list_id = course_list.id + else + new_course_list = CourseList.create!(name: params[:course_list_name].to_s.strip, user_id: course.tea_id, is_admin: 0) + course.course_list_id = new_course_list.id + end + course.save! + end + end + + private + + def form_params + params.merge(course: course) + end +end \ No newline at end of file diff --git a/app/views/weapps/courses/edit.json.jbuilder b/app/views/weapps/courses/edit.json.jbuilder new file mode 100644 index 000000000..ef7a28a42 --- /dev/null +++ b/app/views/weapps/courses/edit.json.jbuilder @@ -0,0 +1,2 @@ +json.(@course, :id, :name, :credit) +json.course_list_name @course.course_list&.name \ No newline at end of file From abdd40108dda4522d2a6303da6ea5bdb8386976c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A0=91=E6=9E=97?= <904079904@qq.com> Date: Thu, 14 Nov 2019 19:52:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coursesPublic/ModulationModal_exercise.js | 99 +++++++++++-------- 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/public/react/src/modules/courses/coursesPublic/ModulationModal_exercise.js b/public/react/src/modules/courses/coursesPublic/ModulationModal_exercise.js index 41b985003..e87ad104b 100644 --- a/public/react/src/modules/courses/coursesPublic/ModulationModal_exercise.js +++ b/public/react/src/modules/courses/coursesPublic/ModulationModal_exercise.js @@ -58,81 +58,94 @@ class ModulationModal_exercise extends Component { Inputsvals } = this.state; let re = /^[0-9]+.?[0-9]*$/;//判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/ - + let res= /^\-[1-9][0-9]*$/; //判断字符串是负整数 if (subool === false) { + let subools=false; + let suboolss=false; //不是总分模式 if (Inputsval === undefined || Inputsval === null || Inputsval === "") { this.setState({ Inputsval: "", Inputsvaltype: true, - Inputsvaltest: "请填写分数", + Inputsvaltest: "主观题成绩不能为空", }) - return + subools=true; } //判断主观题 + if(subools===false){ + var nubmer = Inputsval; + if (!re.test(nubmer)&&!res.test(nubmer)) { + this.setState({ + Inputsval: Inputsval, + Inputsvaltype: true, + Inputsvaltest: "请输入0-100的分数", + }) + subools=true; + } - var nubmer = Inputsval; - if (!re.test(nubmer)) { - this.setState({ - Inputsval: Inputsval, - Inputsvaltype: true, - Inputsvaltest: "请输入0-100的分数", - }) - return; } - + if(subools===false){ if (0 > parseFloat(Inputsval)) { this.setState({ Inputsval: Inputsval, Inputsvaltype: true, - Inputsvaltest: "成绩不能小于零", + Inputsvaltest: "主观题成绩不能小于零", }) - return; + subools=true; } else if (parseFloat(Inputsval) > this.props.subjective_score) { this.setState({ Inputsval: Inputsval, Inputsvaltype: true, - Inputsvaltest: `成绩不能大于${this.props.subjective_score}`, + Inputsvaltest: `主观题成绩不能大于主观题总分值${this.props.subjective_score}`, }) - return; + subools=true; + } } //判断客观题 + if (Inputsvals === undefined || Inputsvals === null || Inputsvals === "") { this.setState({ Inputsvals: "", Inputsvaltypes: true, - Inputsvaltests: "请填写分数", + Inputsvaltests: "客观题成绩不能为空", }) - return + suboolss=true; } - var nubmers = Inputsvals; - if (!re.test(nubmers)) { - this.setState({ - Inputsvals: "", - Inputsvaltypes: true, - Inputsvaltests: "请输入0-100的分数", - }) - return; + + if(suboolss===false){ + var nubmers = Inputsvals; + if (!re.test(nubmers)&&!res.test(nubmers)) { + this.setState({ + Inputsvals: "", + Inputsvaltypes: true, + Inputsvaltests: "请输入0-100的分数", + }) + suboolss=true; + } } + if(suboolss===false){ if (0 > parseFloat(Inputsvals)) { this.setState({ Inputsvals: Inputsvals, Inputsvaltypes: true, - Inputsvaltests: "请输入0-100的分数", + Inputsvaltests: "客观题成绩不能小于零", }) - return; + suboolss=true; } else if (parseFloat(Inputsvals) > this.props.objective_score) { this.setState({ Inputsvals: Inputsvals, Inputsvaltypes: true, - Inputsvaltests: `成绩不能大于${this.props.objective_score}`, + Inputsvaltests: `客观题成绩不能大于客观题总分值${this.props.objective_score}`, }) - return; + suboolss=true; + } + } + if(subools===true ||suboolss===true){ + return; } - } else { debugger @@ -143,14 +156,14 @@ class ModulationModal_exercise extends Component { this.setState({ Inputsval: "", Inputsvaltype: true, - Inputsvaltest: "请填写分数", + Inputsvaltest: "总成绩不能为空", }) return } //判断主观题 var nubmer = Inputsval; - if (!re.test(nubmer)) { + if (!re.test(nubmer)&&!res.test(nubmer)) { this.setState({ Inputsval: Inputsval, Inputsvaltype: true, @@ -164,14 +177,14 @@ class ModulationModal_exercise extends Component { this.setState({ Inputsval: Inputsval, Inputsvaltype: true, - Inputsvaltest: "成绩不能小于零", + Inputsvaltest: "总成绩不能小于零", }) return; } else if (parseFloat(Inputsval) > this.props.subjective_score) { this.setState({ Inputsval: Inputsval, Inputsvaltype: true, - Inputsvaltest: `成绩不能大于${this.props.subjective_score}`, + Inputsvaltest: `总成绩不能大于总分值${this.props.subjective_score}`, }) return; } @@ -181,12 +194,12 @@ class ModulationModal_exercise extends Component { this.setState({ Inputsvals: "", Inputsvaltypes: true, - Inputsvaltests: "请填写分数", + Inputsvaltests: "总成绩不能为空", }) return } var nubmers = Inputsvals; - if (!re.test(nubmers)) { + if (!re.test(nubmers)&&!res.test(nubmers)) { this.setState({ Inputsvals: "", Inputsvaltypes: true, @@ -199,14 +212,14 @@ class ModulationModal_exercise extends Component { this.setState({ Inputsvals: Inputsvals, Inputsvaltypes: true, - Inputsvaltests: "请输入0-100的分数", + Inputsvaltests: "总成绩不能小于零", }) return; } else if (parseFloat(Inputsvals) > this.props.objective_score) { this.setState({ Inputsvals: Inputsvals, Inputsvaltypes: true, - Inputsvaltests: `成绩不能大于${this.props.objective_score}`, + Inputsvaltests: `总成绩不能大于总分值${this.props.objective_score}`, }) return; } @@ -316,7 +329,7 @@ class ModulationModal_exercise extends Component {
-

主观题成绩:

+

主观题成绩:

{Inputsvaltest}

: "" @@ -362,7 +375,7 @@ class ModulationModal_exercise extends Component { marginTop: "20px", }}> -

客观题成绩:

+

客观题成绩:

{Inputsvaltests}

: ""