|
|
class CnmoocsService
|
|
|
include ApplicationHelper
|
|
|
include GamesHelper
|
|
|
|
|
|
def get_resources_data params
|
|
|
page = params[:pageNo].to_i
|
|
|
limit = params[:pageSize] || 16
|
|
|
offset = page * limit.to_i
|
|
|
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.id, 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: count, totalCount: totalCount, pageNo: page, pageSize: limit, pageCount: pageCount},
|
|
|
data: {resouces: resouces} }
|
|
|
end
|
|
|
|
|
|
def search_resources params
|
|
|
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).where("name like ?", "%#{params[:name]}%")
|
|
|
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
|
|
|
{error: 0, messages: "请求成功",
|
|
|
page: {count: shixuns.count, totalCount: shixun_count, pageNo: page, pageSize: limit, pageCount: pageCount},
|
|
|
data: shixun_list }
|
|
|
end
|
|
|
|
|
|
def find_user params
|
|
|
c_user = CnmoocUser.find_by_uuid(params[:userName])
|
|
|
if c_user
|
|
|
{error: 0, messages: "找到用户", data: { userId: c_user.user_id } }
|
|
|
else
|
|
|
{error: -1, messages: "找不到用户"}
|
|
|
end
|
|
|
end
|
|
|
|
|
|
def create_user(params)
|
|
|
c_user = CnmoocUser.find_by_uuid(params[:userName])
|
|
|
|
|
|
if c_user.present?
|
|
|
return { error: -1, messages: '用户已存在' }
|
|
|
end
|
|
|
c_user = CnmoocUser.new(uuid: params[:userName], name: params[:name])
|
|
|
|
|
|
mail = params[:email] || c_user.generate_email
|
|
|
name = params[:name] || "好大学_#{params[:userName]}"
|
|
|
login = generate_login('m')
|
|
|
Rails.logger.info("#######mail: #{mail}, #{name}, #{login}")
|
|
|
create_params = {
|
|
|
lastname: name,
|
|
|
mail: mail,
|
|
|
mail_notification: mail,
|
|
|
password: OauthController::DEFAULT_PASSWORD,
|
|
|
certification: 1
|
|
|
}
|
|
|
user = User.new(create_params)
|
|
|
# login 有问题,只能这样赋值
|
|
|
user.login = login
|
|
|
ActiveRecord::Base.transaction do
|
|
|
user.save!
|
|
|
|
|
|
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0)
|
|
|
|
|
|
c_user.user_id = user.id
|
|
|
c_user.save!
|
|
|
end
|
|
|
|
|
|
{ error: 0, messages: "创建成功", data: { userId: user.id } }
|
|
|
end
|
|
|
|
|
|
def login_educoder params
|
|
|
user, last_login_on = User.try_to_login(params[:mail], params[:password])
|
|
|
if user
|
|
|
self.logged_user = user
|
|
|
{error: 0, messages: "登录成功"}
|
|
|
else
|
|
|
{error: -1, messages: "登录失败,请检查邮箱和密码是否正确"}
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
def source_url(params)
|
|
|
user = User.find_by_id(params[:userId])
|
|
|
return {error: -1, messages: "用户不存在,请先创建用户"} unless user
|
|
|
token = Token.get_or_create_permanent_login_token(user)
|
|
|
shixun = Shixun.find_by_id(params[:resouceId])
|
|
|
if shixun.blank?
|
|
|
return { error: -1, messages: '资源不存在' }
|
|
|
end
|
|
|
{ error: 0, messages: '成功', accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.identifier}/challenges?authToken=#{token.value}" }
|
|
|
end
|
|
|
|
|
|
def get_students_data params
|
|
|
shixun = Shixun.find_by_id params[:resouceId]
|
|
|
return {error: -1, messages: "资源id不对,请使用资源的id查找"} if shixun.blank?
|
|
|
myshixun = shixun.myshixuns.where(:user_id => params[:userId]).includes(:games).first
|
|
|
if myshixun.present?
|
|
|
score = myshixun.total_score
|
|
|
time = 0
|
|
|
myshixun.games.each do |game|
|
|
|
time += game.consumes_time_int
|
|
|
end
|
|
|
{error: 0, messages: '成功', data: {time: time, score: score * 10}}
|
|
|
else
|
|
|
{error: -1, messages: '用户还未开始学习此资源'}
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
private
|
|
|
def shixun_data shixuns
|
|
|
shixun_list = []
|
|
|
shixuns.includes(:tag_repertoires).each do |shixun|
|
|
|
tag_name = shixun.tag_repertoires.first.try(:name)
|
|
|
level = %W(初级 中级 高级 顶级)[shixun.trainee - 1]
|
|
|
shixun_list << {identifier: shixun.identifier, name: shixun.name, students_count: shixun.myshixuns_count,
|
|
|
challenges_count: shixun.challenges_count, score_info: shixun.averge_star, level: level}
|
|
|
|
|
|
end
|
|
|
{resouces: shixun_list}
|
|
|
end
|
|
|
|
|
|
# 为新创建的用户随机生成以m为前缀的用户名,m表示该用户是用邮箱注册
|
|
|
def generate_login(login_pre)
|
|
|
us = UsersService.new
|
|
|
us.generate_user_login(login_pre)
|
|
|
end
|
|
|
end |