diff --git a/app/api/mobile/apis/cnmooc.rb b/app/api/mobile/apis/cnmooc.rb index 42a6cfe3..026581cd 100644 --- a/app/api/mobile/apis/cnmooc.rb +++ b/app/api/mobile/apis/cnmooc.rb @@ -56,6 +56,14 @@ module Mobile CnmoocsService.new.login_educoder params end + desc "资源学习情况查询" + params do + requires :userId, type: Integer, desc: "用户ID" + requires :resouceId, type: String, desc: "资源唯一标示" + end + get 'get_students_data' do + CnmoocsService.new.get_students_data params + end end end diff --git a/app/services/cnmoocs_service.rb b/app/services/cnmoocs_service.rb index da119452..ba4271e4 100644 --- a/app/services/cnmoocs_service.rb +++ b/app/services/cnmoocs_service.rb @@ -6,15 +6,43 @@ class CnmoocsService page = params[:pageNo].to_i limit = params[:pageSize] || 16 offset = page * limit.to_i - shixuns = Shixun.select([:id, :identifier, :name, :myshixuns_count, :averge_star, :challenges_count, :trainee]). - where(status: 2, hidden: 0) - shixun_count = shixuns.count - pageCount = ((shixun_count / limit.to_f) == (shixun_count / limit)) ? (shixun_count / limit) : ((shixun_count / limit) + 1) - shixuns = shixuns.order("myshixuns_count desc").offset(offset).limit(limit) - shixun_list = shixun_data shixuns + resouces = [] + if params[:level].to_s == "1" + subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.status, COUNT(myshixuns.id) AS myshixun_member_count + FROM myshixuns, stage_shixuns, subjects WHERE myshixuns.shixun_id = stage_shixuns.shixun_id + AND stage_shixuns.subject_id = subjects.id AND `subjects`.`hidden` = 0 AND `subjects`.`status` = 2 + GROUP BY subjects.id ORDER BY myshixun_member_count DESC limit #{offset},#{limit}") + + subjects.each do |subject| + resouces << {resouceId: subject.id, parentId: nil, resouceName: subject.name, accessType: 0, nodeType: 0, + resouceType: 2} + end + totalCount = Subject.where(:status => 2, :hidden => 0).count + count = subjects.count + elsif params[:level].to_s == "2" + return {error: -1, messages: "请求二级及其更高目录时,parentId不能为空"} if params[:parentId].blank? + stages = Stage.where(:subject_id => params[:parentId]).offset(offset).limit(limit) + stages.each do |stage| + resouces << {resouceId: stage.id, parentId: params[:parentId], resouceName: stage.name, accessType: 0, nodeType: 0, + resouceType: 2} + end + totalCount = Stage.where(:subject_id => params[:parentId]).count + count = stages.count + elsif params[:level].to_s == "3" + return {error: -1, messages: "请求二级及其更高目录时,parentId不能为空"} if params[:parentId].blank? + shixun_ids = StageShixun.where(:stage_id => params[:parentId]).pluck(:shixun_id) + shixuns = Shixun.where(:id => shixun_ids).offset(offset).limit(limit) + shixuns.each do |shixun| + resouces << {resouceId: shixun.identifier, parentId: params[:parentId], resouceName: shixun.name, accessType: 2, + nodeType: 1, resouceType: 1} + end + totalCount = Shixun.where(:id => shixun_ids).count + count = shixuns.count + end + pageCount = ((totalCount / limit.to_f) == (totalCount / limit)) ? (totalCount / limit) : ((totalCount / limit) + 1) {error: 0, messages: "请求成功", - page: {count: shixuns.count, totalCount: shixun_count, pageNo: page, pageSize: limit, pageCount: pageCount}, - data: shixun_list } + page: {count: count, totalCount: totalCount, pageNo: page, pageSize: limit, pageCount: pageCount}, + data: {resouces: resouces} } end def search_resources params @@ -33,9 +61,9 @@ class CnmoocsService end def find_user params - user = User.find_by_mail params[:mail] - if user - {error: 0, messages: "找到用户"} + c_user = CnmoocUser.find_by_uuid(params[:userName]) + if c_user + {error: 0, messages: "找到用户", data: { userId: user.id } } else {error: -1, messages: "找不到用户"} end @@ -48,7 +76,7 @@ class CnmoocsService return { error: -1, messages: '用户已存在' } end - mail = c_user.generate_email + mail = params[:email] || c_user.generate_email create_params = { lastname: params[:name], mail: mail, @@ -94,6 +122,22 @@ class CnmoocsService { error: 0, messages: '成功', accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.id}" } end + def get_students_data params + subject = Subject.find_by_id params[:resouceId] + return {error: -1, messages: "资源id不对,请使用一级目录资源查找"} if subject.blank? + shixun_ids = StageShixun.where(:stage_id => subject.stages).pluck(:shixun_id) + myshixuns = Myshixun.where(:user_id => params[:userId], shixun_id: shixun_ids).includes(:games) + score = 0 + time = 0 + myshixuns.each do |myshixun| + score += myshixun.total_score + myshixun.games.each do |game| + time += game.consumes_time_int + end + end + {error: 0, messages: '成功', data: {totalTime: time, score: score * 10}} + end + private def shixun_data shixuns