You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/helpers/application_helper.rb

360 lines
12 KiB

6 years ago
# 所有的方法请按首字母的顺序依次列出
module ApplicationHelper
include Educoder::I18n
5 years ago
include GitHelper
6 years ago
ONE_MINUTE = 60 * 1000
ONE_HOUR = 60 * ONE_MINUTE
ONE_DAY = 24 * ONE_HOUR
ONE_MONTH = 30 * ONE_DAY
ONE_YEAR = 12 * ONE_MONTH
# 全局参数配置
def edu_setting name
EduSetting.get(name)
6 years ago
end
def graduation_navigation graduation
graduation.class.to_s == "GraduationTopic" ? "毕设选题" : "毕设任务"
end
def graduation_navigation_id course
course.course_modules.find_by(module_type: "graduation").try(:id)
end
# 是否关注
# from_user_id为被关注的用户
def follow?(from_user_id, user_id)
Watcher.where(watchable_type: 'Principal', watchable_id: from_user_id, user_id: user_id).exists?
end
# git用户
def git_username(email)
User.find_by_mail(email)
end
# 不同的类型扩展不同的目录
def relative_path
"avatars"
end
def storage_path
File.join(Rails.root, "public", "images", relative_path)
end
# 推荐实训
def recommend_shixun(shixun)
tag_repertoire_id = shixun.tag_repertoires.first.present? ? shixun.tag_repertoires.first.try(:id) : 0
shixun_id = ShixunTagRepertoire.where("tag_repertoire_id = #{tag_repertoire_id} and
shixun_id != #{shixun.id}").pluck(:shixun_id)
shixun_id = shixun_id.blank? ? -1 : shixun_id.join(",")
Shixun.select([:id, :name, :user_id, :challenges_count, :myshixuns_count, :trainee, :identifier]).where("id
in(#{shixun_id}) or homepage_show =1").unhidden.order("myshixuns_count desc, homepage_show asc").limit(3)
end
# 相关推荐
def relation_path(shixun)
shixun.subjects.where(hidden: 0).limit(2)
end
# shixun开启挑战对应的行为名及url
def task_operation_url current_myshixun, shixun
url = "/shixuns/#{shixun.identifier}/shixun_exec"
name =
if current_myshixun.blank?
shixun.status == 0 ? "模拟实战" : "开启挑战"
elsif current_myshixun.status == 1
"查看实战"
else
"继续挑战"
end
[name, url]
end
# 获取当前时间
def time_from_now(time)
if String === time
time = Time.parse(time)
end
lastUpdateTime = time.to_i*1000
currentTime = Time.now.to_i*1000
timePassed = currentTime - lastUpdateTime
timeIntoFormat = 0
updateAtValue = ""
if timePassed < 0
updateAtValue = "刚刚"
elsif timePassed < ONE_MINUTE
updateAtValue = "1分钟前"
elsif timePassed < ONE_HOUR
timeIntoFormat = timePassed / ONE_MINUTE
updateAtValue = timeIntoFormat.to_s + "分钟前"
elsif (timePassed < ONE_DAY)
timeIntoFormat = timePassed / ONE_HOUR
updateAtValue = timeIntoFormat.to_s + "小时前"
elsif (timePassed < ONE_MONTH)
timeIntoFormat = timePassed / ONE_DAY
updateAtValue = timeIntoFormat.to_s + "天前"
elsif (timePassed < ONE_YEAR)
timeIntoFormat = timePassed / ONE_MONTH
updateAtValue = timeIntoFormat.to_s + "个月前"
else
timeIntoFormat = timePassed / ONE_YEAR
updateAtValue = timeIntoFormat.to_s + "年前"
end
updateAtValue
end
# 计算到结束还有多长时间 **天**小时**分
def how_much_time(time)
6 years ago
if time.nil? || time < Time.now #6.21 -hs 增加小于time.now
6 years ago
''
else
result = ((time - Time.now.to_i).to_i / (24*60*60)).to_s + ""
result += (((time - Time.now.to_i).to_i % (24*60*60)) / (60*60)).to_s + " 小时 "
result + ((((time - Time.now.to_i).to_i % (24*60*60)) % (60*60)) / 60).to_s + ""
end
end
def format_time(time)
time.present? ? time.strftime("%Y-%m-%d %H:%M") : ''
end
# 用户图像url如果不存在的话source为匿名用户即默认使用匿名用户图像
def url_to_avatar(source)
if File.exist?(disk_filename(source.class, source.id))
if source.class.to_s == 'User'
File.join(relative_path, ["#{source.class}", "#{source.id}"])
else
5 years ago
File.join("images/avatars", ["#{source.class}", "#{source.id}"])
6 years ago
end
elsif source.class.to_s == 'User'
str = source.user_extension.try(:gender).to_i == 0 ? "b" : "g"
File.join(relative_path, "#{source.class}", str)
elsif source.class.to_s == 'Subject'
File.join("images","educoder", "index", "subject", "subject#{rand(17)}.jpg")
elsif source.class.to_s == 'Shixun'
File.join("images","educoder", "index", "shixun", "shixun#{rand(23)}.jpg")
end
end
def disk_filename(source_type,source_id,image_file=nil)
File.join(storage_path, "#{source_type}", "#{source_id}")
end
def shixun_url_to_avatar(shixun)
if File.exist?(disk_filename(shixun.class, shixun.id))
5 years ago
File.join("images/#{relative_path}", "#{shixun.class}", "#{shixun.id}")
6 years ago
else
File.join("educoder", "index", "shixun", "shixun#{rand(23)}.jpg")
end
end
# 选用实训的学校情况
def school_user_detail shixun
school_ids = shixun.myshixuns.joins("join user_extensions ue on myshixuns.user_id=ue.user_id").pluck(:school_id).uniq
school_names = School.where(id: school_ids[0..1]).pluck(:name)
school_size = school_ids.size
str = school_size > 0 ? "#{school_names.join("")}#{school_size}" : "0所"
end
# 普通/分组 作业作品状态数组
def student_work_status homework, user_id, course, work
status = []
homework_setting = homework.homework_group_setting user_id
work = work || StudentWork.create(homework_common_id: homework.id, user_id: user_id)
late_time = homework.late_time || course.end_date
if course.is_end && work && work.work_status > 0
status << "查看作品"
elsif !course.is_end
if homework_setting.publish_time && homework_setting.publish_time < Time.now
# 作业未截止时
if homework_setting.end_time > Time.now
if homework.homework_type == "group" && homework.homework_detail_group.base_on_project
if work.project_id.nil? || work.project_id == 0
status << "创建项目"
status << "关联项目"
elsif work.work_status == 0
status << "取消关联"
status << "提交作品"
else
status << "修改作品"
end
else
if work.work_status == 0
status << "提交作品"
else
status << "修改作品"
end
end
# 补交阶段
elsif homework.allow_late && (late_time.nil? || late_time > Time.now)
if homework.homework_type == "group" && homework.homework_detail_group.base_on_project
if work.project_id.nil? || work.project_id == 0
status << "创建项目"
status << "关联项目"
elsif work.work_status == 0
status << "取消关联"
status << "补交作品"
else
status << "补交附件"
status << "查看作品"
end
else
if work.work_status == 0
status << "补交作品"
else
status << "补交附件"
status << "查看作品"
end
end
# 匿评阶段
elsif work.work_status != 0
if homework.homework_detail_manual.comment_status == 3
work_ids = homework.student_works.has_committed.pluck(:id)
if StudentWorksEvaluationDistribution.where(student_work_id: work_ids, user_id: user_id).size > 0
status << "匿评作品"
end
end
status << "查看作品"
end
end
end
end
def commit_des_status work, homework
status = []
homework_setting = homework.homework_group_setting work.user_id
# 作业已发布且作业未截止(补交未截止)且提交了作品才能提交或修改总结
if homework_setting.publish_time && homework_setting.publish_time < Time.now && work.work_status > 0 &&
((homework_setting.end_time && homework_setting.end_time > Time.now) ||
(homework.allow_late && homework.late_time && homework.late_time > Time.now))
work.description.present? ? status << "修改总结" : status << "提交总结"
end
end
def download_url attachment
attachment_path(attachment)
end
# 耗时:天、小时、分、秒
# 小于1分钟则不显示
def game_spend_time time
day = time / 86400
hour = time % (24*60*60) / (60*60)
min = time % (24*60*60) % (60*60) / 60
sec = time % (24*60*60) % (60*60) % 60
if day < 1
if hour < 1
if min < 1
if sec < 1
time = "--"
else
time = "#{sec}"
end
else
time = "#{min}分钟 #{sec}"
end
else
time = "#{hour}小时 #{min}分钟 #{sec}"
end
else
time = "#{day}#{hour}小时 #{min}分钟 #{sec}"
end
return time
end
def absolute_path(file_path)
File.join(edu_setting('attachment_folder'), file_path)
end
def local_path(file)
File.join(file.disk_directory, file.disk_filename)
end
def update_downloads(file)
file.update_attributes(:downloads => file.downloads + 1)
end
# 项目信息,
def project_info work, current_user, course_identity
project = work.project
if project
if project.status == 9
{id: -1, name: "#{project.name}(已删除)", title: "该项目已删除", author: project.creator, member_count: project.project_members.count}
else
project_score = project.project_score
if project.is_public || current_user.manager_of_project?(project) || course_identity < Course::STUDENT
{id: project.id, name: project.name, author: project.creator, member_count: project.project_members.count,
all_score: project_score.all_score, code_score: project_score.code_score, issue_score: project_score.issue_score,
attachment_score: project_score.attachment_score, message_score: project_score.message_score}
else
{id: -1, name: "#{project.name}", title: "该项目是私有的", author: project.creator, member_count: project.project_members.count,
all_score: project_score.all_score, code_score: project_score.code_score, issue_score: project_score.issue_score,
attachment_score: project_score.attachment_score, message_score: project_score.message_score}
end
end
else
{id: -1, name: "--", title: "--"}
end
end
def message_content(content)
content = (strip_html content).strip
content = content.gsub(/\s+/, " ")
if content.gsub(" ", "") == ""
content = "[非文本消息]"
end
content
end
def strip_export_title(content)
con_ = ""
if content.length > 0
con_ = strip_tags(content)
con_ = con_.gsub(/\r\n/,'').gsub(/&nbsp;*/, '').strip
end
con_
end
def pdf_load_sources(*arg)
arr = arg.map do |path|
content_tag(:script) do
File.open(Rails.root.join('public', path)).read.to_s.html_safe
end
end
raw arr.join('')
end
5 years ago
5 years ago
# 导出pdf时转化markdown为html
5 years ago
def to_markdown(text)
5 years ago
return nil if text.blank?
5 years ago
request_url = request.base_url
5 years ago
5 years ago
options = {
:autolink => true,
:no_intra_emphasis => true,
:fenced_code_blocks => true,
:lax_html_blocks => true,
:strikethrough => true,
:superscript => true,
:tables => true
5 years ago
}
5 years ago
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,options)
5 years ago
m_t = markdown.render(text)
5 years ago
m_t&.include?("src=\"") ? m_t&.gsub("src=\"","src=\"#{request.base_url}") : m_t
5 years ago
Rails.logger.info("###########__________request_url_________###################{request_url}")
5 years ago
end
6 years ago
end