diff --git a/.access_token b/.access_token index 610b08d2e..2e228963c 100644 --- a/.access_token +++ b/.access_token @@ -1 +1 @@ -{"access_token":"oEEf8ZKAB8Y2G0o_xnTPkPJHKKk8iHkLC-f5ptvQ2nCMj9IpC86ivLD2-p38GfOkuG-HuQp3pWZqhs3NJXUMdPLWsr5k67hPZYuqg4ozLccx0xdLswapj0mn8ovZhK1tKIKiAFAOMO","expires_in":7200,"got_token_at":1467012449} \ No newline at end of file +{"access_token":"b_Pc60Dd5eyg_ut3cHbsjQO9EJJdj2Qj5F99o9LH9ltKSme7_FZ3Of22lWLL-K2V0siWzv-bd9PO0Dn-L1PBvIy9LhXa0qPVaFl6vTtZHR2kA8qjo1ps2ancya0t7KmzURGbAFAAXM","expires_in":7200,"got_token_at":1467976842} \ No newline at end of file diff --git a/.gitignore b/.gitignore index dbc349c80..1ef7e63ce 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,7 @@ /config/database.yml /config/configuration.yml /config/additional_environment.rb -/config/menu.yml -/config/wechat.yml + /files/* /log/* @@ -35,3 +34,5 @@ vendor/cache 1234567 public/javascripts/wechat/node_modules/ .ruby-version +.access_token +tmux*.log diff --git a/Gemfile b/Gemfile index 250176066..3bea83669 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,8 @@ unless RUBY_PLATFORM =~ /w32/ gem 'iconv' if RUBY_PLATFORM =~ /darwin/ gem "rmagick", "= 2.15.4" ## osx must be this version - else - gem "rmagick", "= 2.13.1" ## centos yum install ImageMagick-devel + elsif RUBY_PLATFORM =~ /linux/ + #gem "rmagick", "= 2.13.1" ## centos yum install ImageMagick-devel end gem 'certified' gem 'net-ssh', '2.9.1' diff --git a/Gemfile~ b/Gemfile~ new file mode 100644 index 000000000..334f15853 --- /dev/null +++ b/Gemfile~ @@ -0,0 +1,114 @@ +source 'https://ruby.taobao.org/' + +unless RUBY_PLATFORM =~ /w32/ + # unix-like only + gem 'iconv' + gem "rmagick", ">= 2.0.0" + gem 'certified' +end + +gem 'net-ssh', '2.9.1' +gem 'jenkins_api_client' +gem 'nokogiri' + +gem 'wechat',path: 'lib/wechat' +gem 'grack', path:'lib/grack' +gem 'gitlab', path: 'lib/gitlab-cli' +gem 'rest-client' +gem "mysql2", "= 0.3.18" +gem 'redis-rails' +gem 'rubyzip' +gem 'delayed_job_active_record'#, :group => :production +gem 'daemons' +gem 'grape', '~> 0.9.0' +gem 'grape-entity' +gem 'rack-cors', :require => 'rack/cors' +gem 'seems_rateable', '~> 1.0.13' +gem 'rails', '~> 3.2' +gem "jquery-rails", "~> 2.0.2" +gem "i18n", "~> 0.6.0" +gem 'coderay', '~> 1.1.0' +gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby] +gem "builder", "3.0.0" +gem 'acts-as-taggable-on', '2.4.1' +gem 'spreadsheet' +gem 'ruby-ole' +gem 'rails_kindeditor',path:'lib/rails_kindeditor' +gem 'binding_of_caller' +gem 'chinese_pinyin' +# gem 'sunspot_rails', '~> 1.3.3' +# gem 'sunspot_solr' +# gem 'sunspot' +# gem 'progress_bar' +gem 'ansi' + +gem 'kaminari' +gem 'elasticsearch-model' +gem 'elasticsearch-rails' + + +### profile +#gem 'oneapm_rpm' + +group :development do + gem 'grape-swagger' + gem 'better_errors', '~> 1.1.0' + # gem "query_reviewer" + # gem 'rack-mini-profiler', '~> 0.9.3' + if RUBY_PLATFORM =~ /w32/ + gem 'win32console' + end +end + +group :development, :test do + unless RUBY_PLATFORM =~ /w32/ + gem 'pry-rails' + if RUBY_VERSION >= '2.0.0' + gem 'pry-byebug' + end + gem 'pry-stack_explorer' + if RUBY_PLATFORM =~ /darwin/ + gem 'puma' + end + end + + gem 'rspec-rails', '~> 3.0' + gem 'factory_girl_rails' +end + +# Gems used only for assets and not required +# in production environments by default. +group :assets do + gem 'sass-rails', '~> 3.2.3' + gem 'coffee-rails', '~> 3.2.1' + + # See https://github.com/sstephenson/execjs#readme for more supported runtimes + # gem 'therubyracer', :platforms => :ruby + + gem 'uglifier', '>= 1.0.3' +end + +# Optional gem for LDAP authentication +group :ldap do + gem "net-ldap", "~> 0.3.1" +end + + +# Optional gem for OpenID authentication +group :openid do + gem "ruby-openid", "~> 2.1.4", :require => "openid" + gem "rack-openid" +end + + +database_file = File.join(File.dirname(__FILE__), "config/database.yml") +if File.exist?(database_file) +else + warn("Please configure your config/database.yml first") +end + +# Load plugins' Gemfiles +Dir.glob File.expand_path("../plugins/*/Gemfile", __FILE__) do |file| + puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v` + instance_eval File.read(file) +end diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index c555eb633..9b4bdb5d6 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -19,6 +19,7 @@ module Mobile require_relative 'apis/new_comment' require_relative 'apis/praise' require_relative 'apis/resources' + require_relative 'apis/syllabuses' class API < Grape::API version 'v1', using: :path @@ -73,6 +74,7 @@ module Mobile mount Apis::NewComment mount Apis::Praise mount Apis::Resources + mount Apis::Syllabuses add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development? diff --git a/app/api/mobile/apis/activities.rb b/app/api/mobile/apis/activities.rb index 661925012..b4fb90ea7 100644 --- a/app/api/mobile/apis/activities.rb +++ b/app/api/mobile/apis/activities.rb @@ -10,30 +10,44 @@ module Mobile params do requires :page, type: Integer requires :token, type: String + requires :container_type, type: String end post do authenticate! user = current_user - shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id) - shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id) + container_type = params[:container_type] ? params[:container_type] : "All" + + if container_type == "All" || container_type == "Project" + shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id) + user_project_ids = (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).join(",") + ")" + project_types = "('Message','Issue','Project')" + end + + if container_type == "All" || container_type == "Course" + shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id) + user_course_ids = (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")" + course_types = "('Message','News','HomeworkCommon','Poll','Course')" + end + page = params[:page] ? params[:page] : 0 - user_project_ids = (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).join(",") + ")" - user_course_ids = (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")" - course_types = "('Message','News','HomeworkCommon','Poll','Course')" - project_types = "('Message','Issue','Project')" principal_types = "JournalsForMessage" watched_user_ids = User.watched_by(user.id).count == 0 ? " " : ("," + User.watched_by(user.id).map{|u| u.id.to_s }.join(',')) user_ids = "(" + user.id.to_s + watched_user_ids + ")" watched_user_blog_ids = Blog.select("id").where("author_id in #{user_ids}").count == 0 ? " " :Blog.select("id").where("author_id in #{user_ids}").map { |blog| blog.id}.join(",") blog_ids = "(" + watched_user_blog_ids + ")" - activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" + - "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ - "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{user.id}) " + - "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc') - + if container_type == "Course" + activities = UserActivity.where("(container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})").order('updated_at desc') + elsif container_type == "Project" + activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})").order('updated_at desc') + else + activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" + + "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ + "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{user.id}) " + + "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc') + end all_count = activities.count activities = activities.limit(10).offset(page * 10) count = activities.count @@ -42,6 +56,7 @@ module Mobile present :count, count present :page, page present :status, 0 + present :container_type, container_type end end end diff --git a/app/api/mobile/apis/blog_comments.rb b/app/api/mobile/apis/blog_comments.rb index 5a064245a..bb5758e71 100644 --- a/app/api/mobile/apis/blog_comments.rb +++ b/app/api/mobile/apis/blog_comments.rb @@ -7,7 +7,7 @@ module Mobile desc "get special topic" get ':id' do - user = UserWechat.find_by_openid(params[:openid]).user + user = current_user blog = BlogComment.find params[:id] present :data, blog, with: Mobile::Entities::BlogComment,user: user present :status, 0 diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 42303b63d..301417781 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -107,7 +107,7 @@ module Mobile status = cs.join_course({role: "10", openid: params[:openid], invite_code: params[:invite_code]}, current_user) { status: status[:state], - messsge:CoursesService::JoinCourseError.message(status[:state]) + message:CoursesService::JoinCourseError.message(status[:state]) } end @@ -396,6 +396,23 @@ module Mobile present :status,0 end + desc '删除课程' + params do + requires :token, type: String + end + post ':course_id/del' do + authenticate! + c = Course.find(params[:course_id]) + # if c.members.count > 1 + # {status: -1, message: '已经有成员加入,不能删除'} + # else + c.delete! + present :status,0 + # end + + + end + end end end diff --git a/app/api/mobile/apis/resources.rb b/app/api/mobile/apis/resources.rb index fca94a642..6dfe8e599 100644 --- a/app/api/mobile/apis/resources.rb +++ b/app/api/mobile/apis/resources.rb @@ -46,9 +46,25 @@ module Mobile end - end + desc '发送资源' + params do + requires :token, type: String + requires :course_ids, type: Array[Integer] + requires :send_id, type: Integer + end + post 'send' do + authenticate! + rs = ResourcesService.new + ori, flag, save_message = rs.send_resource_to_course(current_user,params) + if flag + present :status, 0 + else + {status: -1, message: save_message.first} + end + end + end end end diff --git a/app/api/mobile/apis/syllabuses.rb b/app/api/mobile/apis/syllabuses.rb new file mode 100644 index 000000000..57db4cb9e --- /dev/null +++ b/app/api/mobile/apis/syllabuses.rb @@ -0,0 +1,103 @@ +#coding=utf-8 + +module Mobile + module Apis + class Syllabuses < Grape::API + + resources :syllabuses do + desc "获取大纲列表" + params do + requires :token, type: String + end + get do + authenticate! + + cs = SyllabusesService.new + courses = cs.user_syllabus(current_user) + present :data, courses, with: Mobile::Entities::Syllabus + present :status, 0 + end + + desc "获取某个大纲" + params do + requires :token, type: String + end + get ':id' do + authenticate! + + ss = SyllabusesService.new + + sy = ::Syllabus.find(params[:id]) + sy.courses = sy.courses.not_deleted + sy = ss.judge_can_setting(sy,current_user) + + present :data, sy, with: Mobile::Entities::Syllabus + present :status, 0 + end + + desc "获取新建大纲的权限" + params do + requires :token, type: String + end + post 'auth' do + authenticate! + + auth = 0 + + if (current_user.user_extensions && current_user.user_extensions.identity == 0 && current_user.allowed_to?(:add_course, nil, :global => true)) + auth = 1 + end + + present :auth, auth + end + + desc "新建大纲" + params do + requires :token, type: String + requires :title, type: String, desc: '大纲标题' + requires :courses, type: Array[String], desc: '课程名' + end + post do + authenticate! + + ss = SyllabusesService.new + + sy = ss.create(current_user, params[:title], + params[:courses].map{|c| {name: c} }) + + if sy.new_record? + {status:-1, message: '创建大纲失败' } + else + present :data, sy, with: Mobile::Entities::Syllabus + present :status, 0 + end + + end + + desc '编辑大纲' + params do + requires :token, type: String + requires :title, type: String, desc: '大纲标题' + # requires :add_courses, type: Array[String], desc: '课程名' + # requires :modify_courses, type: Array[Integer,String], desc: '课程名' + end + post ':id/edit' do + + authenticate! + + ss = SyllabusesService.new + + #修改课程大纲 + status = ss.edit(current_user, params) + + if status == -1 + {status:status, message: '修改课程信息失败' } + else + present :status, status + end + end + end + + end + end +end \ No newline at end of file diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index b5ee14d19..a6536e73e 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -53,10 +53,20 @@ module Mobile requires :password, type: String, desc: 'password' end post do + + openid = session[:wechat_openid] + logger.debug "openid ============== #{openid}" + raise "无法获取到openid,请在微信中打开本页面" unless openid + + us = UsersService.new user = us.register params.merge(:password_confirmation => params[:password], :should_confirmation_password => true) - raise "该邮箱已经被注册过了" if user.new_record? + raise user.errors.full_messages.first if user.new_record? + UserWechat.create!( + openid: openid, + user: user + ) present :data, user, with: Mobile::Entities::User present :status, 0 diff --git a/app/api/mobile/entities/course.rb b/app/api/mobile/entities/course.rb index 487a75c4d..d44869a60 100644 --- a/app/api/mobile/entities/course.rb +++ b/app/api/mobile/entities/course.rb @@ -15,6 +15,8 @@ module Mobile #f.img_url if f.respond_to?(:img_url) elsif field == :created_at || field == :updated_at (format_time(c[field]) if (c.is_a?(Hash) && c.key?(field))) || (format_time(c.send(field)) if c.respond_to?(field)) + elsif field == :member_count + ::Course===c ? c.members.count : 0 else (c[field] if (c.is_a?(Hash) && c.key?(field))) || (c.send(field) if c.respond_to?(field)) end @@ -49,6 +51,8 @@ module Mobile course_expose :qrcode course_expose :updated_at course_expose :course_student_num + course_expose :member_count + course_expose :can_setting expose :teacher, using: Mobile::Entities::User do |c, opt| if c.is_a? ::Course c.teacher diff --git a/app/api/mobile/entities/syllabus.rb b/app/api/mobile/entities/syllabus.rb new file mode 100644 index 000000000..4f97dd868 --- /dev/null +++ b/app/api/mobile/entities/syllabus.rb @@ -0,0 +1,13 @@ +module Mobile + module Entities + class Syllabus < Grape::Entity + include ApplicationHelper + + expose :title + expose :id + expose :can_setting + + expose :courses, using: Mobile::Entities::Course + end + end +end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index e81251f63..a9d84c2d9 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1090,7 +1090,7 @@ class CoursesController < ApplicationController #删除课程 #删除课程只是将课程的is_deleted状态改为false,is_deleted为false状态的课程只有管理员可以看到 def destroy - @course.update_attributes(:is_delete => true) + @course.delete! @course = nil redirect_to user_url(User.current) end diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index 1d23c28c2..73672d11a 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -71,18 +71,21 @@ class QualityAnalysisController < ApplicationController # 判断调用sonar分析是否成功 # 等待启动时间处理, 最长时间为30分钟 - for i in 0..60 do - sleep(30) + for i in 0..360 do + sleep(5) @current_build_status = @client.job.get_current_build_status("#{job_name}") if (@current_build_status == "success" || @current_build_status == "failure") break - if i == 60 + if i == 360 @build_console_result = false break end end end + # sonar 缓冲,取数据 + sleep(5) + # 获取sonar output结果 console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')["output"] logger.info("@current_build_status is ==> #{@current_build_status}") diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5fe7efc51..3f23d94dc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1046,11 +1046,12 @@ class UsersController < ApplicationController homework_detail_group.save if homework_detail_group if homework.homework_type != 3 - unless homework.course.nil? + students = homework.course.student + if !homework.course.nil? && !students.empty? name = homework.name name_str = name + "的作品提交" str = "" - homework.course.student.each do |student| + students.each do |student| if str != "" str += "," end @@ -2021,44 +2022,8 @@ class UsersController < ApplicationController def add_exist_file_to_course @flag = true if params[:send_id].present? - send_id = params[:send_id] - @ori = Attachment.find_by_id(send_id) - course_ids = params[:course_ids] - if course_ids.nil? - @flag = false - end - unless course_ids.nil? - course_ids.each do |id| - next if @ori.blank? - @exist = false - Course.find(id).attachments.each do |att| #如果课程中包含该资源 - if att.id == @ori.id || (!att.copy_from.nil? && !@ori.copy_from.nil? && att.copy_from == @ori.copy_from) || att.copy_from == @ori.id || att.id == @ori.copy_from - att.created_on = Time.now - att.save - @exist = true - break - end - end - next if @exist - attach_copied_obj = @ori.copy - attach_copied_obj.tag_list.add(@ori.tag_list) # tag关联 - attach_copied_obj.container = Course.find(id) - attach_copied_obj.created_on = Time.now - attach_copied_obj.author_id = User.current.id - attach_copied_obj.is_public = 0 - attach_copied_obj.copy_from = @ori.copy_from.nil? ? @ori.id : @ori.copy_from #发送要添加copy_from - if attach_copied_obj.attachtype == nil - attach_copied_obj.attachtype = 4 - end - if attach_copied_obj.save - # 更新引用次数 - quotes = @ori.quotes.to_i + 1 - @ori.update_attribute(:quotes, quotes) unless @ori.nil? - @ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) - end - @save_message = attach_copied_obj.errors.full_messages - end - end + rs = ResourcesService.new + @ori, @flag, @save_message = rs.send_resource_to_course(params) elsif params[:send_ids].present? send_ids = params[:send_ids].split(",") course_ids = params[:course_ids] diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 4a79d6d93..f49398ff7 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -8,7 +8,11 @@ class WechatsController < ActionController::Base # default text responder when no other match on :text do |request, content| #邀请码 - sendBindClass(request, {invite_code: content}) + if join_request(request) + sendBindClass(request, {invite_code: content}) + else + request.reply.text '您的意见已收到,感谢您的反馈!' + end end # When receive 'help', will trigger this responder @@ -142,6 +146,11 @@ class WechatsController < ActionController::Base end end + def join_request(request) + openid = request[:FromUserName] + wl = WechatLog.where("openid = '#{openid}' and request_raw like '%\"Event\":\"click\"%'").order('id desc').first + wl && JSON(wl.request_raw)["EventKey"] == 'JOIN_CLASS' + end def sendBindClass(request, params) begin diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4ebb8eb80..23fd401fd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3282,7 +3282,7 @@ end def strip_html(text,len=0,endss="...") ss = "" - if text.length>0 + if !text.nil? && text.length>0 ss=text.gsub(/<\/?.*?>/, '').strip ss = ss.gsub(/ /, ' ') diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index f312d2275..98b993183 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -388,6 +388,21 @@ module UsersHelper return result end + + #获取指定用户的课程大纲 + def user_syllabus(user) + results = [] + courses = user_courses_list(user) + + other = Syllabus.new(title: '未命名课程') + + courses.each do |c| + other << c unless c.syllabus + end + + user.syllabuses.to_a << other + end + #获取用户参与的公开的课程列表 def user_public_course_list user membership = user.coursememberships.all#@user.coursememberships.all(:conditions => Course.visible_condition(User.current)) diff --git a/app/models/course.rb b/app/models/course.rb index e4b08697b..561a031ae 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -4,6 +4,8 @@ require 'elasticsearch/model' class Course < ActiveRecord::Base include Redmine::SafeAttributes + include CoursesHelper + STATUS_ACTIVE = 1 STATUS_CLOSED = 5 STATUS_ARCHIVED = 9 @@ -22,7 +24,7 @@ class Course < ActiveRecord::Base end end - attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student, :enterprise_name, :is_delete + attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student, :enterprise_name, :is_delete, :syllabus_id #belongs_to :project, :class_name => 'Course', :foreign_key => :extra, primary_key: :identifier belongs_to :teacher, :class_name => 'User', :foreign_key => :tea_id # 定义一个方法teacher,该方法通过tea_id来调用User表 belongs_to :school, :class_name => 'School', :foreign_key => :school_id #定义一个方法school,该方法通过school_id来调用School表 @@ -69,8 +71,10 @@ class Course < ActiveRecord::Base validates_presence_of :term,:name validates_format_of :class_period, :with =>/^[1-9]\d*$/ - validates_format_of :name,:with =>/^[^ ]+[a-zA-Z0-9_\u4e00-\u9fa5\s\S]+$/ + validates_format_of :time, :with => /^\d{4}$/ + validates_format_of :name,:with =>/^[^ ]+[a-zA-Z0-9_\u4e00-\u9fa5\s\S]*$/ validates_length_of :description, :maximum => 10000 + before_save :self_validate # 公开课程变成私有课程,所有资源都变成私有 after_update :update_files_public,:update_course_ealasticsearch_index @@ -169,6 +173,10 @@ class Course < ActiveRecord::Base ) end + def delete! + update_attribute(:is_delete, true) + end + def visible?(user=User.current) user.allowed_to?(:view_course, self) end @@ -296,6 +304,13 @@ class Course < ActiveRecord::Base end end + + def update_default_value + self.time = Time.now.year unless time + self.term = cur_course_term unless term + self.class_period = 10 unless class_period + end + # 创建课程讨论区 def create_board_sync @board = self.boards.build @@ -480,7 +495,7 @@ class Course < ActiveRecord::Base def generate_qrcode ticket = self.qrcode if !ticket || ticket.size < 10 - response = Wechat.api.qrcode_create_scene(invite_code) + response = Wechat.api.qrcode_create_scene(invite_code, 2592000) logger.debug "response = #{response}" self.qrcode = response['ticket'] save! && reload diff --git a/app/models/syllabus.rb b/app/models/syllabus.rb index 58e822ffa..29b675d25 100644 --- a/app/models/syllabus.rb +++ b/app/models/syllabus.rb @@ -9,8 +9,10 @@ class Syllabus < ActiveRecord::Base belongs_to :user has_many :courses has_many :journals_for_messages, :as => :jour, :dependent => :destroy - attr_accessible :user_id, :description, :title, :eng_name, :syllabus_type, :credit, :hours, :theory_hours, :practice_hours, :applicable_major, :pre_course - safe_attributes 'title', 'description', 'eng_name', 'syllabus_type', 'credit', 'hours', 'theory_hours', 'practice_hours', 'credit', 'applicable_major', 'pre_course' + attr_accessible :description, :user_id, :title, :eng_name, :syllabus_type, :credit, :hours, :theory_hours, :practice_hours, :applicable_major, :pre_course + safe_attributes 'title','user', 'description', 'eng_name', 'syllabus_type', 'credit', 'hours', 'theory_hours', 'practice_hours', 'credit', 'applicable_major', 'pre_course' + + validates :title, :user_id, presence: true scope :like, lambda {|arg| if arg.blank? diff --git a/app/models/wechat_log.rb b/app/models/wechat_log.rb new file mode 100644 index 000000000..118a9fade --- /dev/null +++ b/app/models/wechat_log.rb @@ -0,0 +1,4 @@ +#coding=utf-8 +# +class WechatLog < ActiveRecord::Base +end diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb index f59beb7e5..a55a81e89 100644 --- a/app/services/courses_service.rb +++ b/app/services/courses_service.rb @@ -44,6 +44,8 @@ class CoursesService } end + + #搜索课程 def search_course params,current_user courses_all = Course.all_course @@ -324,9 +326,9 @@ class CoursesService define_error [ 0, '加入成功', 1, '密码错误', - 2, '班级已过期 请联系班级管理员重启班级。', - 3, '您已经加入了班级', - 4, '您加入的班级不存在', + 2, '课程已过期 请联系课程管理员重启课程。', + 3, '您已经加入了课程', + 4, '您的邀请码不正确', 5, '您还未登录', 6, '申请成功,请等待审核完毕', 7, '您已经发送过申请了,请耐心等待', diff --git a/app/services/resources_service.rb b/app/services/resources_service.rb new file mode 100644 index 000000000..a0fec585a --- /dev/null +++ b/app/services/resources_service.rb @@ -0,0 +1,53 @@ +#coding=utf-8 + +class ResourcesService + + #发送资源到课程 + def send_resource_to_course user,params + send_id = params[:send_id] + @ori = Attachment.find_by_id(send_id) + course_ids = params[:course_ids] + @flag = false + unless course_ids.nil? + course_ids.each do |id| + next if @ori.blank? + @exist = false + Course.find(id).attachments.each do |att| #如果课程中包含该资源 + if att.id == @ori.id || (!att.copy_from.nil? && !@ori.copy_from.nil? && att.copy_from == @ori.copy_from) || att.copy_from == @ori.id || att.id == @ori.copy_from + att.created_on = Time.now + att.save + @exist = true + @flag = true + break + end + end + next if @exist + attach_copied_obj = @ori.copy + attach_copied_obj.tag_list.add(@ori.tag_list) # tag关联 + attach_copied_obj.container = Course.find(id) + attach_copied_obj.created_on = Time.now + attach_copied_obj.author_id = user.id + attach_copied_obj.is_public = 0 + attach_copied_obj.copy_from = @ori.copy_from.nil? ? @ori.id : @ori.copy_from #发送要添加copy_from + if attach_copied_obj.attachtype == nil + attach_copied_obj.attachtype = 4 + end + if attach_copied_obj.save + # 更新引用次数 + quotes = @ori.quotes.to_i + 1 + @ori.update_attribute(:quotes, quotes) unless @ori.nil? + @ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) + @flag = true + else + @flag = false + @save_message = attach_copied_obj.errors.full_messages + break + end + + end + end + + [@ori, @flag, @save_message] + end + +end \ No newline at end of file diff --git a/app/services/syllabuses_service.rb b/app/services/syllabuses_service.rb new file mode 100644 index 000000000..b3190182c --- /dev/null +++ b/app/services/syllabuses_service.rb @@ -0,0 +1,143 @@ +#coding=utf-8 + +class SyllabusesService + + include ApplicationHelper + include CoursesHelper + + def judge_can_setting(sy,user) + sy[:can_setting] = sy[:user_id] == user.id ? true : false + + sy[:can_setting] = false if sy[:id].nil? + + sy.courses.each do |c| + c[:can_setting] = false + + member = c.members.where("user_id=#{user.id} and course_id=#{c.id}")[0] + roleName = member.roles[0].name if member + + if roleName && (roleName == "TeachingAsistant" || roleName == "Teacher" ) + c[:can_setting] = true + end + + if c.tea_id == user.id + c[:can_setting] = true + end + end + + sy + end + #获取指定用户的课程大纲 + def user_syllabus(user) + courses = CoursesService.new.user_courses_list(user) + + other = Syllabus.new(title: '未命名课程',user_id: user.id) + + courses.each do |c| + other.courses << c[:course] unless c[:course].syllabus + end + + # user.syllabuses.each do |syllabus| + # syllabus.courses = syllabus.courses.not_deleted + # end + # + # user.syllabuses.to_a << other + + courses = user.courses.not_deleted + syllabus_ids = courses.empty? ? '(-1)' : "(" + courses.map{|course| !course.syllabus_id.nil? && course.syllabus_id}.join(",") + ")" + syllabuses = Syllabus.where("id in #{syllabus_ids} or user_id = #{user.id}").order("updated_at desc") + + syllabuses.each do |syllabus| + syllabus.courses = courses.where("syllabus_id = #{syllabus.id}").select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS updatetime").order("time desc") + end + + syllabuses.to_a << other + + #管理权限 can_setting + syllabuses.each do |s| + s = judge_can_setting(s,user) + end + + syllabuses + end + + def after_create_course(course, user) + #unless User.current.admin? + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => user, :roles => [r]) + m.project_id = -1 + course_info = CourseInfos.new(:user_id => user.id, :course_id => course.id) + #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) + course.members << m + course.course_infos << course_info + end + + #创建大纲 + # params {title: '大纲名称', [{course}, {course}]} + def create(user, title, courses = []) + sy = Syllabus.new(title: title, user_id: user.id) + ActiveRecord::Base.transaction() do + sy.save! + + courses.each do |course| + if ::Course === course + course.syllabus_id = sy.id + course.save! + elsif Hash === course + c = ::Course.new(course) + c.tea_id = user.id + c.syllabus_id = sy.id + c.update_default_value + c.is_public = 0 + c.save! + after_create_course(c, user) + end + end + + end + sy[:can_setting] = true + sy + end + + #修改课程大纲的名称、班级名称、新增班级 + def edit(user, option) + courses = [] + status = -1 + syllabus_id = option[:id] + + sy = Syllabus.where("id=?",option[:id]).first + + if sy && sy.user_id == user.id + syllabus_title = option[:title] + + sy.title = syllabus_title + sy.save! + #修改班级名称 + modify_courses = option[:modify_courses] + modify_courses.each do |c| + course = Course.where("id=?",c.id).first + + if course && course.tea_id == user.id + course.name = c.name + !course.save + end + end + + #新增班级 + add_courses = option[:add_courses] + add_courses.each do |c| + course = Course.new() + course.name = c + course.tea_id = user.id + course.syllabus_id = sy.id + course.update_default_value + course.is_public = 0 + course.save! + after_create_course(course, user) + end + status = 0 + end + status + end + +end diff --git a/app/services/users_service.rb b/app/services/users_service.rb index 8df42fb41..ab1bb2ec4 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -319,4 +319,8 @@ class UsersService my_jours_arr end + + + + end diff --git a/app/views/admin/_rename_course_name.html.erb b/app/views/admin/_rename_course_name.html.erb new file mode 100644 index 000000000..8cc061c76 --- /dev/null +++ b/app/views/admin/_rename_course_name.html.erb @@ -0,0 +1,3 @@ + + <%= course.name %> + \ No newline at end of file diff --git a/app/views/admin/syllabuses.html.erb b/app/views/admin/syllabuses.html.erb index 1066ac3ef..782f6b01c 100644 --- a/app/views/admin/syllabuses.html.erb +++ b/app/views/admin/syllabuses.html.erb @@ -68,10 +68,8 @@