You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

poll_controller.rb 30 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
12 years ago
11 years ago
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
11 years ago
​
11 years ago
​
​
​
​
​
​
​
11 years ago
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
11 years ago
​
​
​
​
​
​
​
​
​
11 years ago
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. #encoding utf-8
  2. class PollController < ApplicationController
  3. before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll,:commit_answer,:publish_poll,:republish_poll,:poll_result,:close_poll,:export_poll,:save_poll,:update_question_num]
  4. before_filter :find_container, :only => [:new,:create, :index]
  5. before_filter :is_logged, :only => [:index, :show, :poll_result,:new,:create,:edit,:update,:destroy,:publish_poll,:republish_poll,:close_poll,:export_poll,:commit_answer,:commit_poll,:statistics_result,:save_poll]
  6. before_filter :is_member_of_course, :only => [:index,:show,:poll_result]
  7. before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy,:publish_poll,:republish_poll,:close_poll,:export_poll,:save_poll]
  8. include PollHelper
  9. def index
  10. if @course
  11. @is_teacher = User.current.allowed_to?(:as_teacher,@course)
  12. if @is_teacher
  13. remove_invalid_poll(@course)
  14. polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}")
  15. else
  16. polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2")
  17. end
  18. @polls = paginateHelper polls,20 #分页
  19. @left_nav_type = 7
  20. respond_to do |format|
  21. format.html{render :layout => 'base_courses'}
  22. end
  23. elsif @project
  24. #项目的问卷调查相关代码
  25. end
  26. end
  27. def show
  28. @poll = Poll.find params[:id]
  29. if @poll.polls_status != 2 && !(User.current.allowed_to?(:as_teacher,@course) || User.current.admin?)
  30. render_403
  31. return
  32. end
  33. # 问卷消息状态更新
  34. query_course_poll = @poll.course_messages
  35. query_course_poll.each do |query|
  36. if User.current.id == query.user_id
  37. query.update_attributes(:viewed => true)
  38. end
  39. end
  40. #已提交问卷的用户不能再访问该界面
  41. if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?)
  42. redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id)
  43. else
  44. @can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin?
  45. @percent = get_percent(@poll,User.current)
  46. @poll_questions = @poll.poll_questions
  47. @left_nav_type = 7
  48. respond_to do |format|
  49. format.html {render :layout => 'base_courses'}
  50. end
  51. end
  52. end
  53. def new
  54. if @course
  55. option = {
  56. :polls_name => "",
  57. :polls_type => @course.class.to_s,
  58. :polls_group_id => @course.id,
  59. :polls_status => 1,
  60. :user_id => User.current.id,
  61. :published_at => Time.now,
  62. :closed_at => Time.now,
  63. :show_result => 1,
  64. :polls_description => ""
  65. }
  66. @poll = Poll.create option
  67. if @poll
  68. redirect_to edit_poll_url @poll.id
  69. end
  70. elsif @project
  71. #项目的问卷调查相关代码
  72. end
  73. end
  74. def create
  75. end
  76. def edit
  77. respond_to do |format|
  78. @left_nav_type = 7
  79. format.html{render :layout => 'base_courses'}
  80. end
  81. end
  82. def update
  83. @poll.polls_name = params[:polls_name]
  84. @poll.polls_description = params[:polls_description]
  85. if @poll.save
  86. respond_to do |format|
  87. format.js
  88. end
  89. else
  90. render_404
  91. end
  92. end
  93. def destroy
  94. if @poll && @poll.destroy
  95. if @is_teacher
  96. polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}")
  97. else
  98. polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2")
  99. end
  100. @polls = paginateHelper polls,20 #分页
  101. if params[:is_redirect]
  102. redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id)
  103. else
  104. respond_to do |format|
  105. format.js
  106. end
  107. end
  108. end
  109. end
  110. def statistics_result
  111. @poll = Poll.find(params[:id])
  112. poll_questions = @poll.poll_questions
  113. @poll_questions = paginateHelper poll_questions, 5
  114. @left_nav_type = 7
  115. respond_to do |format|
  116. format.html{render :layout => 'base_courses'}
  117. end
  118. end
  119. def get_poll_totalcount poll_question
  120. @total_questions_count = poll_question.poll_votes.count
  121. end
  122. def get_poll_everycount poll_answer
  123. @every_answer_count = poll_answer.poll_votes.count
  124. end
  125. def update_question_num
  126. @poll_question = PollQuestion.find params[:ques_id]
  127. poll_questions = @poll.poll_questions
  128. if @poll_question
  129. if params[:opr] == 'up' && @poll_question.question_number > 1
  130. @before_que = poll_questions.where("question_number = #{@poll_question.question_number - 1}").first
  131. if @before_que && @poll_question.update_attribute('question_number', @poll_question.question_number - 1)
  132. @before_que.update_attribute('question_number', @before_que.question_number + 1)
  133. end
  134. elsif params[:opr] == 'down' && @poll_question.question_number < poll_questions.count
  135. @after_que = poll_questions.where("question_number = #{@poll_question.question_number + 1}").first
  136. if @after_que && @poll_question.update_attribute('question_number', @poll_question.question_number + 1)
  137. @after_que.update_attribute('question_number', @after_que.question_number - 1)
  138. end
  139. end
  140. respond_to do |format|
  141. format.js
  142. end
  143. end
  144. end
  145. #添加题目
  146. def create_poll_question
  147. @last_question = @poll.poll_questions.last
  148. question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
  149. option = {
  150. :is_necessary => (params[:is_necessary]=="true" ? 1 : 0),
  151. :question_title => question_title,
  152. :question_type => params[:question_type] || 1,
  153. :question_number => @poll.poll_questions.count + 1,
  154. :max_choices => params[:max_choices].to_i || 0,
  155. :min_choices => params[:min_choices].to_i || 0,
  156. }
  157. @poll_questions = @poll.poll_questions.new option
  158. if params[:question_answer]
  159. for i in 1..params[:question_answer].count
  160. answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
  161. question_option = {
  162. :answer_position => i,
  163. :answer_text => answer
  164. }
  165. @poll_questions.poll_answers.new question_option
  166. end
  167. end
  168. if params[:question_other_answer]
  169. question_option = {
  170. :answer_position => params[:question_answer].count + 1,
  171. :answer_text => ''
  172. }
  173. @poll_questions.poll_answers.new question_option
  174. end
  175. # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1
  176. if params[:quest_id]
  177. insert_poll = PollQuestion.find params[:quest_id]
  178. if insert_poll
  179. @is_insert = true
  180. ques_num = insert_poll.question_number
  181. @poll.poll_questions.where("question_number > #{ques_num}").update_all(" question_number = question_number + 1")
  182. @poll_question_num = ques_num
  183. @poll_questions.question_number = ques_num + 1
  184. end
  185. end
  186. if @poll_questions.save
  187. respond_to do |format|
  188. format.js
  189. end
  190. end
  191. end
  192. #修改题目
  193. def update_poll_question
  194. @poll_question = PollQuestion.find params[:poll_question]
  195. #@poll = @poll_question.poll
  196. @poll_question.is_necessary = params[:is_necessary]=="true" ? 1 : 0
  197. @poll_question.question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
  198. @poll_question.max_choices = params[:max_choices].to_i || 0
  199. @poll_question.min_choices = params[:min_choices].to_i || 0
  200. ################处理选项
  201. if params[:question_answer]
  202. @poll_question.poll_answers.each do |answer|
  203. answer.destroy unless params[:question_answer].keys.include? answer.id.to_s
  204. end
  205. for i in 1..params[:question_answer].count
  206. question = @poll_question.poll_answers.find_by_id params[:question_answer].keys[i-1]
  207. answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
  208. if question
  209. question.answer_position = i
  210. question.answer_text = answer
  211. question.save
  212. else
  213. question_option = {
  214. :answer_position => i,
  215. :answer_text => answer
  216. }
  217. @poll_question.poll_answers.new question_option
  218. end
  219. end
  220. if params[:question_other_answer]
  221. question = @poll_question.poll_answers.where("answer_text = ''").first
  222. unless question
  223. question_option = {
  224. :answer_position => params[:question_answer].count + 1,
  225. :answer_text => ''
  226. }
  227. @poll_question.poll_answers.new question_option
  228. end
  229. else
  230. question = @poll_question.poll_answers.where("answer_text = ''").first
  231. if question
  232. question.destroy
  233. end
  234. end
  235. end
  236. @poll_question.save
  237. respond_to do |format|
  238. format.js
  239. end
  240. end
  241. #删除题目
  242. def delete_poll_question
  243. @poll_question = PollQuestion.find params[:poll_question]
  244. @poll = @poll_question.poll
  245. poll_questions = @poll.poll_questions.where("question_number > #{@poll_question.question_number}")
  246. poll_questions.each do |question|
  247. question.question_number -= 1
  248. question.save
  249. end
  250. if @poll_question && @poll_question.destroy
  251. respond_to do |format|
  252. format.js
  253. end
  254. end
  255. end
  256. #发布问卷
  257. def publish_poll
  258. @index = params[:index]
  259. @poll.polls_status = 2
  260. @poll.published_at = Time.now
  261. @poll.show_result = params[:show_result].to_i if params[:show_result]
  262. if @poll.save
  263. if params[:is_remote]
  264. redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id)
  265. else
  266. respond_to do |format|
  267. format.js
  268. end
  269. end
  270. end
  271. end
  272. #保存问卷
  273. def save_poll
  274. @poll.show_result = params[:show_result].to_i
  275. if @poll.save
  276. respond_to do |format|
  277. format.js
  278. end
  279. end
  280. end
  281. #提交答案
  282. def commit_answer
  283. pq = PollQuestion.find(params[:poll_question_id])
  284. if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?)
  285. render :json => {:text => "failure"}
  286. return
  287. end
  288. if pq.question_type == 1
  289. #单选题
  290. pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
  291. if pv.nil?
  292. #尚未答该题,添加答案
  293. pv = PollVote.new
  294. pv.user_id = User.current.id
  295. pv.poll_question_id = params[:poll_question_id]
  296. end
  297. #修改该题对应答案
  298. pv.poll_answer_id = params[:poll_answer_id]
  299. pv.vote_text = params[:vote_text] if params[:vote_text]
  300. if pv.save
  301. #保存成功返回成功信息及当前以答题百分比
  302. @percent = get_percent(@poll,User.current)
  303. render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
  304. else
  305. #返回失败信息
  306. render :json => {:text => "failure"}
  307. end
  308. elsif pq.question_type == 2
  309. #多选题
  310. pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id)
  311. if pv.nil?
  312. #尚未答该题,添加答案
  313. count = PollVote.where("poll_question_id = #{params[:poll_question_id].to_i} and user_id = #{User.current.id}").count
  314. if pq.max_choices != 0 && count >= pq.max_choices
  315. render :json => {:text => "over"}
  316. else
  317. pv = PollVote.new
  318. pv.user_id = User.current.id
  319. pv.poll_question_id = params[:poll_question_id]
  320. pv.poll_answer_id = params[:poll_answer_id]
  321. pv.vote_text = params[:vote_text] if params[:vote_text]
  322. if pv.save
  323. @percent = get_percent(@poll,User.current)
  324. render :json => {:text => "ok",:percent => format("%.2f" ,@percent)}
  325. else
  326. render :json => {:text => "failure"}
  327. end
  328. end
  329. else
  330. #pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案
  331. if params[:vote_text]
  332. pv.vote_text = params[:vote_text]
  333. if pv.save
  334. @percent = get_percent(@poll,User.current)
  335. render :json => {:text => "ok",:percent => format("%.2f" ,@percent)}
  336. else
  337. render :json => {:text => "failure"}
  338. end
  339. else
  340. if pv.delete
  341. @percent = get_percent(@poll,User.current)
  342. render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)}
  343. else
  344. render :json => {:text => "failure"}
  345. end
  346. end
  347. end
  348. elsif pq.question_type == 3
  349. #单行文本
  350. pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
  351. if pv.nil?
  352. #pv为空之前尚未答题,添加答案
  353. if params[:vote_text].nil? || params[:vote_text].blank?
  354. #用户提交空答案,视作不作答
  355. @percent = get_percent(@poll,User.current)
  356. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  357. else
  358. #添加答案
  359. pv = PollVote.new
  360. pv.user_id = User.current.id
  361. pv.poll_question_id = params[:poll_question_id]
  362. pv.vote_text = params[:vote_text]
  363. if pv.save
  364. @percent = get_percent(@poll,User.current)
  365. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  366. else
  367. render :json => {:text => "failure"}
  368. end
  369. end
  370. else
  371. #pv不为空说明用户之前已作答
  372. if params[:vote_text].nil? || params[:vote_text].blank?
  373. #用户提交空答案,视为删除答案
  374. if pv.delete
  375. @percent = get_percent(@poll,User.current)
  376. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  377. else
  378. render :json => {:text => "failure"}
  379. end
  380. else
  381. #用户修改答案
  382. pv.vote_text = params[:vote_text]
  383. if pv.save
  384. @percent = get_percent(@poll,User.current)
  385. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  386. else
  387. render :json => {:text => "failure"}
  388. end
  389. end
  390. end
  391. elsif pq.question_type == 4
  392. #多行文本题
  393. pv = PollVote.find_by_poll_question_id_and_poll_answer_id_and_user_id(params[:poll_question_id],params[:poll_answer_id],User.current.id)
  394. if pv.nil?
  395. #pv为空之前尚未答题,添加答案
  396. if params[:vote_text].nil? || params[:vote_text].blank?
  397. #用户提交空答案,视作不作答
  398. @percent = get_percent(@poll,User.current)
  399. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  400. else
  401. #添加答案
  402. pv = PollVote.new
  403. pv.user_id = User.current.id
  404. pv.poll_question_id = params[:poll_question_id]
  405. pv.poll_answer_id = params[:poll_answer_id]
  406. pv.vote_text = params[:vote_text]
  407. if pv.save
  408. @percent = get_percent(@poll,User.current)
  409. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  410. else
  411. render :json => {:text => "failure"}
  412. end
  413. end
  414. else
  415. #pv不为空说明用户之前已作答
  416. if params[:vote_text].nil? || params[:vote_text].blank?
  417. #用户提交空答案,视为删除答案
  418. if pv.delete
  419. @percent = get_percent(@poll,User.current)
  420. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  421. else
  422. render :json => {:text => "failure"}
  423. end
  424. else
  425. #用户修改答案
  426. pv.vote_text = params[:vote_text]
  427. if pv.save
  428. @percent = get_percent(@poll,User.current)
  429. render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
  430. else
  431. render :json => {:text => "failure"}
  432. end
  433. end
  434. end
  435. else
  436. render :json => {:text => "failure"}
  437. end
  438. end
  439. #提交问卷
  440. def commit_poll
  441. @uncomplete_question = get_uncomplete_question(@poll,User.current)
  442. if @uncomplete_question.count < 1
  443. pu = get_poll_user(@poll.id,User.current.id)
  444. pu.user_id = User.current.id
  445. pu.poll_id = @poll.id
  446. if pu.save
  447. #redirect_to poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course')
  448. @status = 0 #提交成功
  449. else
  450. @status = 2 #未知错误
  451. end
  452. else
  453. @status = 1 #有未做得必答题
  454. end
  455. respond_to do |format|
  456. format.js
  457. end
  458. end
  459. def import_poll
  460. @poll = Poll.find(params[:to_id])
  461. question_num = @poll.poll_questions.select("max(question_number) question_number").first.question_number
  462. import_poll = Poll.find(params[:import_id])
  463. import_poll.poll_questions.each_with_index do |question,index|
  464. option = {
  465. :is_necessary => question.is_necessary,
  466. :question_title => question.question_title,
  467. :question_type => question.question_type,
  468. :question_number => question_num + index+1
  469. }
  470. poll_questions = @poll.poll_questions.new option
  471. for i in 1..question.poll_answers.count
  472. answer = question.poll_answers[i-1][:answer_text]
  473. question_option = {
  474. :answer_position => i,
  475. :answer_text => answer
  476. }
  477. poll_questions.poll_answers.new question_option
  478. end
  479. @poll.poll_questions << poll_questions
  480. end
  481. if @poll.save
  482. @poll = Poll.find(params[:to_id])
  483. respond_to do |format|
  484. format.js
  485. end
  486. end
  487. end
  488. #重新发布问卷
  489. def republish_poll
  490. @index = params[:index]
  491. @poll.poll_questions.each do |poll_question|
  492. poll_question.poll_votes.destroy_all
  493. end
  494. @poll.poll_users.destroy_all
  495. @poll.polls_status = 1
  496. @poll.save
  497. respond_to do |format|
  498. format.js
  499. end
  500. end
  501. #显示某个学生某份问卷的填写结果
  502. def poll_result
  503. @poll_questions = @poll.poll_questions
  504. @left_nav_type = 7
  505. respond_to do |format|
  506. format.html{render :layout => 'base_courses'}
  507. end
  508. end
  509. #关闭问卷
  510. def close_poll
  511. @index = params[:index]
  512. @poll.polls_status = 3
  513. @poll.closed_at = Time.now
  514. if @poll.save
  515. respond_to do |format|
  516. format.js
  517. end
  518. end
  519. end
  520. #导出问卷
  521. def export_poll
  522. poll_questions = @poll.poll_questions
  523. respond_to do |format|
  524. format.xls {
  525. send_data(poll_to_xls(poll_questions), :type => "text/excel;charset=utf-8; header=present",
  526. :filename => filename_for_content_disposition("#{@poll.polls_name}.xls") )
  527. }
  528. end
  529. end
  530. # 将其他地方的问卷导出来
  531. def other_poll
  532. # 查作者是我,或者作者是当前课程的老师,且不在当前课程内的问卷 进行导入
  533. courses = User.current.courses.select { |course| User.current.allowed_to?(:as_teacher,course)}
  534. course_ids = courses.empty? ? "(-1)" : "(" + courses.map { |course| course.id}.join(',') + ")"
  535. none_courses = User.current.courses.where("is_delete = 1 or #{Course.table_name}.id = #{params[:polls_group_id].to_i}")
  536. none_course_ids = none_courses.empty? ? "(-1)" : "(" + none_courses.map { |course| course.id}.join(',') + ")"
  537. #tea_ids = '('
  538. #tea_ids << Course.find(params[:polls_group_id]).tea_id.to_s << ','<< User.current.id.to_s << ')'
  539. @polls = Poll.where("(user_id = #{User.current.id} or polls_group_id in #{course_ids}) and polls_type = 'course' and polls_group_id not in #{none_course_ids}")
  540. @polls_group_id = params[:polls_group_id]
  541. respond_to do |format|
  542. format.js
  543. end
  544. end
  545. # 将问卷导入本课程
  546. def import_other_poll
  547. course_id = params[:course_id]
  548. @course = Course.find(course_id)
  549. params[:polls].each_with_index do |p,i|
  550. poll = Poll.find(p)
  551. option = {
  552. :polls_name => poll.polls_name || l(:label_poll_new),
  553. :polls_type => 'Course',
  554. :polls_group_id => course_id,
  555. :polls_status => 1,
  556. :user_id => User.current.id,
  557. :published_at => Time.now,
  558. :closed_at => Time.now,
  559. :show_result => 1,
  560. :polls_description => poll.polls_description
  561. }
  562. @poll = Poll.create option
  563. poll.poll_questions.each do | q|
  564. #question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
  565. option = {
  566. :is_necessary => q[:is_necessary],
  567. :question_title => q[:question_title],
  568. :question_type => q[:question_type] || 1,
  569. :question_number => q[:question_number]
  570. }
  571. @poll_questions = @poll.poll_questions.new option
  572. for i in 1..q.poll_answers.count
  573. answer = q.poll_answers[i-1].nil? ? l(:label_new_answer) : q.poll_answers[i-1][:answer_text]
  574. question_option = {
  575. :answer_position => i,
  576. :answer_text => answer
  577. }
  578. @poll_questions.poll_answers.new question_option
  579. end
  580. end
  581. @poll.save
  582. end
  583. redirect_to poll_index_path(:polls_type => "Course", :polls_group_id => @course.id)
  584. # @is_teacher = User.current.allowed_to?(:as_teacher,@course)
  585. # if @is_teacher
  586. # polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}")
  587. # else
  588. # polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2")
  589. # end
  590. # @polls = paginateHelper polls,20 #分页
  591. # respond_to do |format|
  592. # format.js
  593. # end
  594. end
  595. private
  596. def remove_invalid_poll(course)
  597. polls = Poll.where("polls_type = 'Course' and polls_group_id = #{course.id} and polls_name = ''")
  598. unless polls.empty?
  599. polls.each do |poll|
  600. if poll.poll_questions.empty?
  601. poll.destroy
  602. end
  603. end
  604. end
  605. end
  606. def find_poll_and_course
  607. @poll = Poll.find params[:id]
  608. @course = Course.find @poll.polls_group_id
  609. rescue Exception => e
  610. render_404
  611. end
  612. def find_container
  613. if params[:polls_type] && params[:polls_group_id]
  614. case params[:polls_type]
  615. when "Course"
  616. @course = Course.find_by_id params[:polls_group_id]
  617. when "Project"
  618. @project = Project.find_by_id params[:polls_group_id]
  619. end
  620. else
  621. render_404
  622. end
  623. end
  624. def is_logged
  625. redirect_to signin_path unless User.current.logged?
  626. end
  627. def is_member_of_course
  628. render_403 unless(@course && (User.current.member_of_course?(@course) || User.current.admin?))
  629. end
  630. def is_course_teacher
  631. @is_teacher = User.current.allowed_to?(:as_teacher,@course)
  632. render_403 unless(@course && @is_teacher)
  633. end
  634. #获取未完成的题目
  635. def get_uncomplete_question poll,user
  636. necessary_questions = poll.poll_questions.where("#{PollQuestion.table_name}.is_necessary = 1")
  637. uncomplete_question = []
  638. necessary_questions.each do |question|
  639. answers = get_user_answer(question,user)
  640. if question.question_type != 4
  641. if answers.nil? || answers.count < 1
  642. uncomplete_question << question
  643. end
  644. else
  645. if answers.nil? || answers.count < question.poll_answers.count
  646. uncomplete_question << question
  647. end
  648. end
  649. # if answers.nil? || answers.count < 1
  650. # uncomplete_question << question
  651. # end
  652. end
  653. uncomplete_question
  654. end
  655. #获取用户对某个问题的答案
  656. def get_user_answer(question,user)
  657. user_answer = question.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id}")
  658. user_answer
  659. end
  660. def get_complete_question(poll,user)
  661. questions = poll.poll_questions
  662. complete_question = []
  663. questions.each do |question|
  664. answers = get_user_answer(question,user)
  665. if question.question_type != 4
  666. if !(answers.nil? || answers.count < 1)
  667. complete_question << question
  668. end
  669. else
  670. if !(answers.nil? || answers.count < 1)
  671. answers.each do |ans|
  672. complete_question << ans
  673. end
  674. end
  675. end
  676. end
  677. complete_question
  678. end
  679. def get_percent poll,user
  680. if poll.poll_questions.count == 0
  681. return 0
  682. else
  683. complete_count = get_complete_question(poll,user).count
  684. all_count = poll.poll_questions.where("question_type != 4").count
  685. poll.poll_questions.where("question_type = 4").each do |pq|
  686. all_count += pq.poll_answers.count
  687. end
  688. return (complete_count.to_f / all_count.to_f)*100
  689. end
  690. end
  691. #PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
  692. def get_poll_user poll_id,user_id
  693. pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id)
  694. if pu.nil?
  695. pu = PollUser.new
  696. end
  697. pu
  698. end
  699. #将poll中题目转换为Excel
  700. def poll_to_xls poll_questions
  701. xls_report = StringIO.new
  702. book = Spreadsheet::Workbook.new
  703. sheet1 = book.create_worksheet :name => "poll"
  704. blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10
  705. count_row = 0
  706. poll_questions.each do |poll_question|
  707. if poll_question.question_type == 1 || poll_question.question_type == 2
  708. sheet1.row(count_row).default_format = blue
  709. sheet1[count_row,0]= l(:label_poll_question_num,:num => poll_question.question_number)
  710. sheet1[count_row + 1,0] = l(:label_poll_subtotal)
  711. sheet1[count_row + 2,0] = l(:label_poll_proportion)
  712. poll_question.poll_answers.each_with_index do |poll_answer,i|
  713. sheet1[count_row, i + 1] = poll_answer.answer_text == "" ? l(:label_poll_other) : poll_answer.answer_text.gsub(/<\/?.*?>/,"").gsub(/&nbsp;/," ")
  714. sheet1[count_row + 1, i + 1] = poll_answer.poll_votes.count
  715. sheet1[count_row + 2, i + 1] = statistics_result_percentage(poll_answer.poll_votes.count, total_answer(poll_question.id)).to_s + "%"
  716. end
  717. sheet1[count_row + 3,0] = l(:label_poll_valid_commit)
  718. sheet1[count_row + 3,1] = total_answer(poll_question.id)
  719. count_row += 5
  720. else
  721. sheet1.row(count_row).default_format = blue
  722. sheet1[count_row,0] = l(:label_poll_question_num,:num => poll_question.question_number)
  723. sheet1[count_row,1] = poll_question.question_title
  724. count_row += 1
  725. if poll_question.question_type == 3
  726. poll_question.poll_votes.each do |poll_vote|
  727. sheet1[count_row,0] = poll_vote.vote_text.gsub(/<\/?.*?>/,"").gsub(/&nbsp;/," ")
  728. count_row += 1
  729. end
  730. count_row += 1
  731. else
  732. count = 0
  733. poll_question.poll_answers.reorder("answer_position asc").each_with_index do |poll_answer,i|
  734. sheet1.row(count_row).default_format = blue
  735. sheet1[count_row, i] = poll_answer.answer_text.gsub(/<\/?.*?>/,"").gsub(/&nbsp;/," ")
  736. count = poll_question.poll_votes.where("poll_answer_id = #{poll_answer.id}").count > count ? poll_question.poll_votes.where("poll_answer_id = #{poll_answer.id}").count : count
  737. poll_question.poll_votes.where("poll_answer_id = #{poll_answer.id}").each_with_index do |poll_vote, j|
  738. sheet1[count_row + j + 1,i] = poll_vote.vote_text.gsub(/<\/?.*?>/,"").gsub(/&nbsp;/," ")
  739. end
  740. end
  741. count_row = count_row + count + 2
  742. end
  743. end
  744. end
  745. sheet1.row(count_row).default_format = blue
  746. sheet1[count_row ,0] = l(:label_bidding_user_studentname)
  747. current_index = 1
  748. poll_questions.each do |poll_question|
  749. if poll_question.question_type == 4
  750. poll_question.poll_answers.reorder("answer_position asc").each do |ans|
  751. sheet1[count_row ,current_index] = poll_question.question_title + "(#{ans.answer_text})"
  752. current_index += 1
  753. end
  754. else
  755. sheet1[count_row ,current_index] = poll_question.question_title
  756. current_index += 1
  757. end
  758. end
  759. count_row += 1
  760. @poll.users.each do |user|
  761. sheet1[count_row ,0] = user.show_name
  762. current_index = 1
  763. poll_questions.each_with_index do |poll_question, i|
  764. if poll_question.question_type == 1 || poll_question.question_type == 2
  765. sheet1[count_row ,current_index] = user.poll_votes.where(:poll_question_id => poll_question.id).map{|poll_vote| poll_vote.poll_answer.answer_text.gsub(/<\/?.*?>/,"").gsub(/&nbsp;/," ") if poll_vote.poll_answer}.join(";")
  766. current_index += 1
  767. elsif poll_question.question_type == 3
  768. sheet1[count_row ,current_index] = user.poll_votes.where(:poll_question_id => poll_question.id).map{|poll_vote| poll_vote.vote_text.gsub(/<\/?.*?>/,"").gsub(/&nbsp;/," ")}.join(";")
  769. current_index += 1
  770. else
  771. poll_question.poll_answers.reorder("answer_position asc").each do |ans|
  772. poll_vote = user.poll_votes.where(:poll_question_id => poll_question.id, :poll_answer_id => ans.id).first
  773. sheet1[count_row ,current_index] = poll_vote.nil? ? "" : poll_vote.vote_text
  774. current_index += 1
  775. end
  776. end
  777. end
  778. count_row += 1
  779. end
  780. book.write xls_report
  781. xls_report.string
  782. end
  783. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...