Guest User

Untitled

a guest
Jul 11th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 65.66 KB | None | 0 0
  1.  
  2. class TeacherController < ApplicationController
  3.  
  4. #User has to login first before access
  5. before_filter :authorize, :except => [:login, :logout]
  6.  
  7. # def create_multipart_test
  8. # if(params[:commit])
  9. # @question = Question.new
  10. # @question.make("Literacy", "Multipart Master test", "Test", "Multipart", "This is some test text!", "", 10,"master")
  11. # @question.question_type = "Literacy"
  12. # @question.depreciated = false
  13. # @question.save
  14. #
  15. # @question1 = Question.new
  16. # @question1.make("Literacy", "Multipart Slave test", "Test", "True False", "This is true!", "1", "10","slave",@question.id)
  17. # @question1.add_choice("True")
  18. # @question1.add_choice("False")
  19. # @question1.question_type = "Literacy"
  20. # @question1.depreciated = false
  21. # @question1.save
  22. #
  23. # flash[:notice] = "Made it!"
  24. # redirect_to :action=>:index
  25. # end
  26. # end
  27.  
  28. #Determine the login ID, if it's a teacher/student/admin login
  29. #Check for password and set the session
  30. def login
  31. session[:student_id] = nil
  32. session[:teacher_id] = nil
  33. session[:admin_id] = nil
  34. if request.post?
  35. student = Student.authenticate(params[:name], params[:password])
  36. teacher = Teacher.authenticate(params[:name], params[:password])
  37. admin = Admin.authenticate(params[:name], params[:password])
  38. if teacher
  39. logger.info("Setting session teacher")
  40. session[:teacher_id] = teacher.id
  41. redirect_to({ :controller => :teacher, :action => "index" })
  42. elsif student
  43. logger.info("setting session student")
  44. session[:student_id] = student.id
  45. uri = session[:original_uri]
  46. session[:original_uri] = nil
  47. redirect_to(uri || { :action => "introduction" })
  48. elsif admin
  49. logger.info("Setting session admin")
  50. session[:admin_id] = admin.id
  51. redirect_to({ :controller => :admin, :action => "index" })
  52. else
  53. logger.info("Bad password")
  54. flash[:notice] = "Invalid User Name/Password"
  55. end
  56. end
  57. end
  58.  
  59. #log out by reset_session
  60. def logout
  61. @exam = false
  62. reset_session
  63.  
  64. respond_to do |format|
  65. format.html
  66. end
  67. end
  68.  
  69. #Show all exams available for teacher
  70. def index
  71. @exam = false
  72. @exams = Exam.find(:all , :conditions=>{:status=>1})
  73. #Display username when logged in
  74. #Edited at 2.40pm 01/05/2009 by Nhi
  75. #Change1
  76. @teacher = Teacher.find_by_id(session[:teacher_id])
  77. end
  78.  
  79. def old_exams
  80. @exam = false
  81. @exams = Exam.find(:all , :conditions=>{:status=>0}, :order=>'name ASC, updated_at DESC')
  82. end
  83.  
  84. #Not applicable
  85. def edit
  86. end
  87.  
  88. #Arrange the question
  89. def arrange
  90. @exam = false
  91. if params[:id]
  92. @exam = Exam.find_by_id(params[:id])
  93. eq = ExamQuestion.find_all_by_exam_id(@exam.id, :order => :position)
  94. @questions = Array.new
  95. for e in eq
  96. @questions.push(Question.find_by_id(e.question_id))
  97. end
  98. @questions.delete_at(0)
  99. else
  100. redirect_to(:action=>:arrange, :id=>params[:exam_selection])
  101. end
  102. end
  103.  
  104. def save_order
  105. @exam = Exam.find_by_id(params[:exam_id])
  106. #@exam.status = 0
  107. a = (params[:result][:answer]).split(',')
  108.  
  109. @st_exam = StudentExam.find_by_exam_id(params[:exam_id])
  110.  
  111. unless @st_exam == nil
  112. exam = @exam.clone
  113.  
  114. exam.save
  115. @exam.status = 0
  116. @exam.save
  117.  
  118. @exam = exam
  119. @exam.add_question(1, :first=>true)
  120.  
  121. end
  122.  
  123.  
  124. logger.debug(@exam.inspect)
  125.  
  126.  
  127. #sets all question in exam to position of -1
  128. #ExamQuestion.update_all("position = -1 ","exam_id = #{@exam.id}")
  129. @eq = Array.new(ExamQuestion.find_all_by_exam_id(@exam.id))
  130. @eq.each {|e| e.position = -1 }
  131.  
  132. a.insert(0, 1)
  133. logger.info(a.inspect)
  134. ExamQuestion.update_all("position = -1","exam_id = #{@exam.id}")
  135. i = 0; until i == a.length
  136. eq = ExamQuestion.find_by_exam_id_and_question_id(@exam.id, a[i].to_i)
  137. unless eq.nil?
  138. eq.update_attribute(:position, i)
  139. eq.save
  140. else
  141. if Question.find_by_id(a[i].to_i).get_type == :master
  142. logger.info("multipart")
  143. @exam.add_multipart(a[i].to_i)
  144. else
  145. logger.info("single")
  146. @exam.add_question(a[i].to_i)
  147. #updates the position in the exam questions table of new questions that have been added
  148. ExamQuestion.update_all("position = #{i}","exam_id = #{@exam.id} AND question_id=#{a[i].to_i}")
  149. end
  150. end
  151. i += 1
  152. end
  153.  
  154. #deletes any rows that haven't been updated with a new position
  155. #ie any question removed from the exam will be deleted from the database
  156. ExamQuestion.delete_all("exam_id = #{@exam.id} AND position = -1")
  157.  
  158. @questions = Array.new(@exam.questions.find(:all))
  159. #redirect_to :action => :assign, :id => @exam.id
  160. if params[:commit] == "Save Question List and Display Question Types"
  161. redirect_to :action => :add_questions, :id => @exam.id, :e_type =>params[:e_type], :q_type =>params[:q_type]
  162. else
  163. redirect_to :action => :assign, :id => @exam.id
  164. end
  165. end
  166.  
  167. #
  168. def add_questions
  169. if params[:commit] == "Mark or View Student Assessment Results" #|| "View Student Reports"
  170. redirect_to(:action=>:exam_results, :id=>params[:exam_selection])
  171. elsif params[:commit] == "View Old Exams"
  172. redirect_to(:action=>:old_exams )
  173. elsif params[:commit] == "Unassign Students from Selected Assessment"
  174. redirect_to(:action=>:unassign_exam, :id=>params[:exam_selection])
  175. else
  176. @exam = false
  177. if params[:id]
  178. if params[:commit] == "Next: Arrange Questions"
  179. redirect_to(:action=>:arrange, :id=>params[:id])
  180. else
  181. @exam = Exam.find_by_id(params[:id])
  182. if params[:e_type] == 'All'
  183. @questions = Array.new(Question.find(:all,:conditions=>["question_type <> 'New' AND question_type <> 'End' AND master_id is null"]))
  184. @exam_questions = Array.new(@exam.questions.find(:all, :conditions=>["question_type <> 'New'"]))
  185.  
  186. else
  187. if params[:q_type]
  188. @questions = Array.new(Question.find(:all,:conditions=>{:master_id=>nil}))
  189. ones_of_right_type = Array.new
  190. @questions.each { |q|
  191. if q.get_type_string(params[:q_type])==q.get_type && q.question_type!='End' && q.question_type!='New'
  192. logger.debug("q.get_type: " + q.get_type.to_s)
  193. logger.debug("q_type: " + params[:q_type])
  194. ones_of_right_type.push q
  195. end
  196. }
  197. @questions = ones_of_right_type
  198. @exam_questions = Array.new(@exam.questions.find(:all, :order => 'position asc'))
  199. else
  200. @questions = Array.new(Question.find(:all,:conditions=>{:question_type => @exam.exam_type,:master_id=>nil}))
  201. @exam_questions = Array.new(@exam.questions.find(:all, :conditions=>["question_type <> 'New'"]))
  202. end
  203. end
  204. end
  205. else
  206. if params[:exam_selection]
  207. if params[:commit] == "Assign Students to Selected Assessment"
  208. redirect_to(:action=>:assign, :id=>params[:exam_selection])
  209. else
  210. redirect_to(:action=>:add_questions, :id=>params[:exam_selection])
  211. end
  212. else
  213. flash[:notice] = "Select an exam to edit"
  214. redirect_to(:action=>:index)
  215. end
  216. end
  217. end
  218. end
  219.  
  220.  
  221. def add
  222. @exam = Exam.find_by_id(params[:exam_id])
  223. unless @exam.exam_questions.find_by_question_id_and_exam_id(params[:question_id], @exam.id)
  224. if Question.find_by_id(params[:question_id]).get_type == :master
  225. logger.info("multipart")
  226. @exam.add_multipart(params[:question_id])
  227. else
  228. logger.info("single")
  229. @exam.add_question(params[:question_id])
  230. end
  231. flash[:notice] = "Question id " + params[:question_id] + " added to exam."
  232. else
  233. flash[:notice] = "You have added this question already. You may change the order at the Arrange page"
  234. end
  235. redirect_to(:action => :add_questions, :id => params[:exam_id])
  236. end
  237.  
  238. #When assign to student button clicked/ Display success message
  239. #Edited at 12.45pm 06/05/2009 by Nhi
  240. def assign
  241. @exam = Exam.find_by_id(params[:id])
  242. @students = Student.find(:all)
  243. if params[:student]
  244. @students_assigned = Array.new #change1
  245. for student in @students
  246. if params[:student][student.id.to_s][:assign] == 1.to_s
  247. student.assign_exam(@exam.id)
  248. @students_assigned.push student # change2
  249. end
  250. end
  251. a = ""
  252. @students_assigned.each {|e| a+= e.name; a += ", " unless e == @students_assigned.last }
  253. flash[:notice] = "Students Assigned to test: " + a
  254. else
  255. end
  256. end
  257.  
  258.  
  259. def unassign_exam
  260. @exam = Exam.find(params[:id])
  261. @students = Student.find(:all)
  262. @st_exams = StudentExam.find_all_by_exam_id(@exam.id)
  263. if params[:st_exam]
  264. for st_exam in @st_exams
  265. if params[:st_exam][st_exam.id.to_s][:unassign_exam] == "1"
  266. st_exam.destroy
  267. flash[:notice] = "Test was successfully unassigned."
  268. redirect_to(:action =>:index)
  269. end
  270. end
  271. end
  272. end
  273.  
  274.  
  275. def create_new_exam
  276. @exam = Exam.new
  277. if params[:exam]
  278. @exam = Exam.new(params[:exam])
  279. if @exam.make(:e_name=>params[:exam][:name], :exam_type=>params[:exam_type], :e_status=> 1, :e_skippable => 1, :e_results_viewable=>params[:viewable]) #CHANGE WHEN INTERFACE IS FINISHED, added in default e_status
  280. if @exam.save
  281. @exam.add_question(1, :first=>true)
  282. @exam.add_question(2)
  283. redirect_to(:action=>:add_questions, :id=>@exam.id)
  284. end
  285. else
  286. flash[:notice] = "<div id = errorMessage> You can only add an exam of the same name once </div>"
  287. redirect_to :action=>:index
  288. end
  289. end
  290. end
  291.  
  292. def report
  293. @exam = Exam.find_by_id(params[:id])
  294. @st_exam = StudentExam.find_by_id(params[:subid])
  295. logger.debug("student_exam: " + @st_exam.inspect)
  296. @subcategories = @exam.get_subcategories
  297. @exam_questions = Array.new(@exam.questions.find(:all))
  298. end
  299.  
  300.  
  301. def exam_results
  302. @exam = Exam.find_by_id(params[:id]) || false
  303. @st_exams = StudentExam.find_all_by_exam_id(@exam.id)
  304. if @st_exams.nil? || @st_exams.empty?
  305. flash[:notice] = "Please select an exam before chosing view student report"
  306. redirect_to :action=>:index
  307. end
  308. end
  309.  
  310. def save_new_question
  311. @question = Question.new
  312. case params[:q_type]
  313.  
  314. when "Multipart"
  315. logger.debug("MULTIPART")
  316. @question.make(params[:question_type], params[:q_subcategory], params[:q_summary], params[:q_type], params[:q_text], "", 10, "master")
  317. redirect_to(:action=>'sub_multipart', :id=>@question.id)
  318. logger.debug("redirecting to sub_multipart")
  319. return
  320.  
  321. when "Sub_Multipart"
  322. logger.debug("sub multipart")
  323. @question1 = Question.new
  324. @question1.make(params[:question_type], params[:q_subcategory], "SLAVE QUESTION", "Long Answer", params[:q_long_text],nil,10,"slave",params[:id])
  325. @question1.question_type = params[:question_type]
  326. @question1.depreciated = false
  327. @question1.save
  328. @question2 = Question.new
  329. @question2.make(params[:question_type], params[:q_subcategory], "SLAVE QUESTION", "Short Answer", params[:q_short_text],nil,10,"slave",params[:id])
  330. @question2.question_type = params[:question_type]
  331. @question2.depreciated = false
  332. @question2.save
  333. @question3 = Question.new
  334. @question3.make(params[:question_type],params[:q_subcategory], "SLAVE QUESTION", "True False",params[:q_tf_text],params[:q_asw],params[:q_weight],"slave",params[:id])
  335. @question3.question_type = params[:question_type]
  336. @question3.depreciated = false
  337. @question3.save
  338.  
  339.  
  340.  
  341. when "Multiple Choice"
  342. error_messages = ""
  343. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Multiple Choice",params[:q_text],params[:asw],params[:q_weight])
  344. error_messages = "" if error_messages == true
  345. blank_word = false
  346. params[:question].each do |key,value|
  347. blank_word = true if value[:q_asw].blank?
  348. end
  349. error_messages += "Error: Please enter a word into all of the option boxes." if blank_word
  350.  
  351. if error_messages.blank?
  352. #if no errors, continue and add choices
  353. params[:question].each do |key,value|
  354. @question.add_choice(value[:q_asw])
  355. end
  356. else
  357. #otherwise, display the error messages
  358. flash[:error] = "<h2><font color=red>" + error_messages + "</font></h2>"
  359.  
  360. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  361. options = params[:parts]
  362. redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  363. return
  364. end
  365.  
  366.  
  367. when "Sequence"
  368. answer = ""
  369. blank_word = false
  370. blank_answer = false
  371. wrong_answer = false
  372. answer_total = 0
  373. right_answer_total = 0
  374. i = 1
  375. possible_answers = Array.new
  376. while i <= params[:parts].to_i
  377. right_answer_total += i
  378. possible_answers.push(i.to_s)
  379. i += 1
  380. end
  381. params[:question].each do |key,value|
  382. answer = answer + value[:q_answer] + ","
  383. blank_word = true if value[:q_part].blank? == true
  384. blank_answer = true if value[:q_answer].blank? == true
  385. answer_total += value[:q_answer].to_i
  386. wrong_answer = true unless possible_answers.include?(value[:q_answer])
  387. end
  388.  
  389. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Sequence",params[:q_text],answer,params[:q_weight])
  390. error_messages = "" if error_messages == true
  391. error_messages += "Error: Must write in all word boxes.<br>" if blank_word == true
  392. error_messages += "Error: Must write in all answer boxes.<br>" if blank_answer == true
  393. error_messages += "Error: One of your answers was outside the valid range.<br>" if wrong_answer == true
  394. error_messages += "Error: Your answers do not add up to the right amount.<br>" if right_answer_total != answer_total
  395.  
  396. if error_messages.blank?
  397. params[:question].each do |key,value|
  398. @question.add_choice(value[:q_part])
  399. end
  400. else
  401. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  402. redirect_to :action=>'main_form', :parts=>params[:parts]
  403. return
  404. end
  405.  
  406.  
  407.  
  408. when "True False"
  409. #make question, storing error messages in a string
  410. error_messages = @question.make(params[:q_category],params[:q_subcategory],params[:q_summary],"True False",params[:q_text],params[:q_asw],params[:q_weight])
  411. #clear error messages if question.make returned true
  412. error_messages = "" if error_messages == true
  413. #display error messages if they exist
  414. unless error_messages.blank?
  415. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  416. render :partial=>'truefalse', :layout=>true #refresh the page
  417. return #do not continue
  418. end
  419. #add the choices to the question so it displays properly to the student
  420. @question.add_choice("True")
  421. @question.add_choice("False")
  422.  
  423.  
  424.  
  425. when "Multiple Short Answer"
  426. answer = ""
  427. blank_word = false
  428. params[:question].each do |key,value|
  429. answer = answer + value[:q_answer] + "," #create answer string for question making
  430. blank_word = true if value[:q_answer].blank? #check if answer box is blank
  431. blank_word = true if value[:q_part].blank? #check if question part text is blank
  432. end
  433. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Multiple Short Answer", params[:q_text], answer, params[:weight])
  434. error_messages = "" if error_messages == true
  435. error_messages += "Error: Please enter text into all of the question and answer boxes." if blank_word == true
  436. #add choices if error messages are blank
  437. if error_messages.blank?
  438. params[:question].each do |key,value|
  439. @question.add_choice(value[:q_part])
  440. end
  441. else #display error messages if they exist
  442. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  443. redirect_to :action=>'main_form', :parts=>params[:parts]
  444. return
  445. end
  446.  
  447.  
  448.  
  449.  
  450.  
  451. when "Arrange Order"
  452. answer = ""
  453. params[:question].each do |key,value|
  454. answer = answer + value[:q_answer] + ","
  455. end
  456.  
  457. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Arrange Order", params[:q_text], answer, params[:weight])
  458. error_messages = "" if error_messages == true
  459. blank_word == false
  460. params[:question].each do |key,value|
  461. blank_word = true if value[:q_part].blank?
  462. end
  463. params[:question].each do |key,value|
  464. blank_word = true if value[:q_answer].blank?
  465. end
  466. if(blank_word)
  467. error_messages = error_messages + "Error: You must enter text in all of the boxes. Please review your question text and answers.<br>"
  468. end
  469. unless error_messages.blank?
  470. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  471. redirect_to :action=>'main_form', :choices=>params[:parts]
  472. return
  473. end
  474.  
  475. params[:question].each do |key,value|
  476. @question.add_choice(value[:q_part])
  477. end
  478.  
  479.  
  480.  
  481.  
  482. when "Select Wrong Word"
  483. #make question and store error messages in error_messages string
  484. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Select Wrong Word",params[:q_text],params[:q_asw],params[:q_weight])
  485. #clear error_messages if question.make returns true
  486. error_messages = "" if error_messages == true
  487. blank_word = false
  488. #check all option boxes are written in
  489. params[:question].each do |key,value|
  490. blank_word = true if value[:q_option].blank?
  491. end
  492. if(blank_word == true)
  493. error_messages = error_messages + "Error: You must write in all of the option boxes.<br>"
  494. end
  495. #display the error messages if they exist
  496. unless error_messages.blank?
  497. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  498. options = params[:q_options]
  499. redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  500. return
  501. else #otherwise, add all the choices to the question and continue
  502. params[:question].each do |key,value|
  503. @question.add_choice(value[:q_option])
  504. end
  505. end
  506.  
  507. when "Arrange Alphabetical"
  508. answer = ""
  509. #create answer string to use when making question
  510. params[:question].each do |key,value|
  511. answer = answer + value[:q_answer] + ","
  512. end
  513. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Arrange Alphabetical",params[:q_text],answer,params[:q_weight])
  514. error_messages = "" if error_messages == true
  515. #check all option boxes are written in
  516. blank_word = false
  517. params[:question].each do |key,value|
  518. blank_word = true if value[:q_part].blank?
  519. end
  520. error_messages += "Error: Please write in all option boxes.<br>" if blank_word == true
  521. #check all answer boxes are written in
  522. blank_answer = false
  523. params[:question].each do |key,value|
  524. blank_answer == true if value[:q_answer].blank?
  525. end
  526. error_messages += "Error: Please write in all answer boxes.<br>" if blank_answer == true
  527. #check answers add up to the right total
  528. right_total = 0
  529. i = 1
  530. while i <= params[:options].to_i
  531. right_total += i
  532. i += 1
  533. end
  534. q_ans_total = 0
  535. params[:question].each do |key,value|
  536. q_ans_total += value[:q_answer].to_i
  537. end
  538. if q_ans_total != right_total
  539. error_messages += "Error: Your answers do not add up to the right total. Please review your answers.<br>"
  540. end
  541. #check all answers are in the correct range
  542. correct_range = Array.new
  543. i = 1
  544. while i <= params[:options].to_i
  545. correct_range.push(i.to_s)
  546. i += 1
  547. end
  548. wrong_value = false
  549. params[:question].each do |key,value|
  550. wrong_value = true unless correct_range.include?(value[:q_answer])
  551. end
  552. error_messages += "Error: At least one of your answers is outside the valid range.<br>" if wrong_value == true
  553. #display error messages unless they are blank
  554. unless error_messages.blank?
  555. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  556. options = params[:options]
  557. redirect_to(:action=>'main_form', :parts=>options) # refresh the page so the error messages show
  558. return
  559. else #if error messages are blank, add all options to the question and continue (will be saved at end of method)
  560. params[:question].each do |key,value|
  561. @question.add_choice(value[:q_part])
  562. end
  563. end
  564.  
  565.  
  566. params[:question].each do |key,value|
  567. @question.add_choice(value[:q_part])
  568. end
  569.  
  570.  
  571. when "Word Match" #to create a question of type "Word Match"
  572. answer = ""
  573. #create answer string to use when making question
  574. params[:question].each do |key,value|
  575. answer = answer + value[:answer].to_s + ","
  576. end
  577. error_messages = "" #clear error messages
  578. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Word Match", params[:q_text], answer, params[:q_weight])
  579. error_messages = "" if error_messages == true
  580. #check if all word and match boxes are filled
  581. blank_word = false
  582. params[:question].each do |key,value|
  583. blank_word = true if value[:word].blank? || value[:match].blank?
  584. end
  585. if blank_word
  586. error_messages = error_messages + "Error: You have at least one blank word box. Please enter something into every word box.<br>"
  587. end
  588. # set up valid answers
  589. # what should the answers add up to
  590. answer_invalid = false
  591. right_total = 0
  592. total = 0
  593. i = 1
  594. while i <= params[:options].to_i
  595. right_total = right_total + i
  596. i = i + 1
  597. end
  598. # what are all the correct possible answers
  599. possible_answers = Array.new()
  600. i = 1
  601. while i <= params[:options].to_i
  602. possible_answers.push(i.to_s)
  603. i = i+1
  604. end
  605. #add up all the answers to check if they are all there,
  606. #and also check if they are a valid number
  607. params[:question].each do |key,value|
  608. total = total + value[:answer].to_i
  609. unless possible_answers.include?(value[:answer].to_s)
  610. answer_invalid = true
  611. end
  612. end
  613. if total != right_total
  614. answer_invalid = true
  615. end
  616. if answer_invalid
  617. error_messages = error_messages + "Error: Answers invalid. Please review your answers in the \"Matches to Number\" column."
  618. end
  619.  
  620. flash[:error] = "" #clear error messages
  621. #display error messages if they exist
  622. unless error_messages.blank? || error_messages == true #when there are error messages and question making was unsuccessful
  623. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>" #put error messages on the page
  624. #@blah = Question.new(:q_summary=>params[:q_summary]) # save the text the user has already entered
  625. #render(:action=>'create_wordmatch', :object=>@blah) # refresh the page so the error messages show
  626. options = params[:options]
  627. redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  628. return #do not continue making the question if errors are found
  629.  
  630. else #if no errors, add the choices to the question
  631. params[:question].each do |key,value|
  632. @question.add_choice(value[:word], "word")
  633. end
  634. params[:question].each do |key,value|
  635. @question.add_choice(value[:match], "match")
  636. end
  637. end
  638.  
  639. when "Calculation"
  640. answer = ""
  641. #create answer string to use when creating question
  642. params[:question].each do |key,value|
  643. answer = answer + value[:q_answer] + ","
  644. end
  645. # make question and store error messages
  646. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Calculation", params[:q_text], answer, params[:weight])
  647. # if question.make is true (successful), clear the error messages
  648. error_messages = "" if error_messages == true
  649. blank_word == false
  650. #check if the user left any word boxes blank
  651. params[:question].each do |key,value|
  652. blank_word = true if value[:q_part].blank?
  653. end
  654. params[:question].each do |key,value|
  655. blank_word = true if value[:q_answer].blank?
  656. end
  657. if(blank_word)
  658. error_messages = error_messages + "Error: You must enter text in all of the boxes. Please review your question text and answers.<br>"
  659. end
  660.  
  661. flash[:error] == ""
  662. #display the error messages unless they are blank
  663. unless error_messages.blank?
  664. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  665. options = params[:parts]
  666. redirect_to(:action => 'main_form', :parts=>options)
  667. return
  668. #if there are no error messages, add all the question choices to the question and continue
  669. #(question will be saved at the end of this method)
  670. else
  671. params[:question].each do |key,value|
  672. @question.add_choice(value[:q_part])
  673. end
  674. end
  675.  
  676. when "Fill in Blanks"
  677. answer = ""
  678. #create the answer string to be used when making the question later
  679. params[:question].each do |key,value|
  680. answer = answer + value[:q_blank] + ","
  681. end
  682.  
  683. blanks = params[:q_text].scan('%%%') #array of '%%%' is stored in blanks
  684. error_messages = ""
  685. if blanks.size != (params[:q_amount].to_i)
  686. error_messages += "Error: You have the wrong number of blank spaces in your question text. Please enter %%% instead of the word anywhere you want a blank to be.<br>"
  687. end
  688. #create question and store errors in make_errors string
  689. make_errors = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Fill in Blanks", params[:q_text], answer,params[:weight])
  690. error_messages += make_errors unless make_errors == true
  691.  
  692. #check all word boxes have text
  693. blank_word = false
  694. params[:question].each do |key,value|
  695. blank_word = true if value[:q_blank].blank?
  696. end
  697. error_messages += "Error: Please enter a word into all of the blank word boxes.<br>" if blank_word == true
  698.  
  699. #display messages if error messages exist
  700. unless error_messages.blank?
  701. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  702. options = params[:q_amount]
  703. redirect_to(:action => 'main_form', :parts=>options)
  704. return #do not continue processing (this means question will not be saved)
  705. end
  706.  
  707.  
  708. # when "Multipart"
  709. # @question.q_data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
  710. # <question category=\"Literacy\" subcategory=\"Multipart\">
  711. # <qtype>Multipart</qtype>
  712. # <qtext>" + params[:q_text] + "</qtext>
  713. # <qsummary>" + params[:q_summary] + "</qsummary>
  714. # </question>"
  715. #
  716. # @question.question_type = "Literacy"
  717. # @question.depreciated = false
  718. # @question.save
  719.  
  720. when "Spelling"
  721. error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Spelling","random",params[:q_asw],params[:q_weight])
  722. #clear error_messages if question.make returns true
  723. error_messages = "" if error_messages == true
  724. blank_answer = false
  725. blank_word = false
  726. #check all option boxes are written in
  727. params[:question].each do |key,value|
  728. blank_word = true if value[:T1].blank?
  729. blank_word = true if value[:T2].blank?
  730. blank_word = true if value[:T3].blank?
  731. blank_answer = true if value[:T4].blank?
  732. end
  733. if(blank_word == true)
  734. error_messages = error_messages + "Error: You must write in all of the option boxes.<br>"
  735. end
  736. if(blank_answer == true)
  737. error_messages = error_messages + "Error: Answer not inputted correctly.<br>"
  738. end
  739. #display the error messages if they exist
  740. unless error_messages.blank?
  741. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  742. options = params[:parts]
  743. redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  744. return
  745. else
  746. q_data = " "
  747. q_data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
  748.  
  749. <question category=\""+ params[:q_category] +"\" subcategory=\"" + params[:q_subcategory] + "\">
  750. <qtype>Spelling</qtype>"
  751. #<qtext>" + params[:q_text] + "</qtext>
  752. q_data +="<qamount> "+ (params[:parts].to_i * 3).to_s + "</qamount>"
  753. q_data+= "<qsummary>" + params[:q_summary] + "</qsummary>"
  754. i = 1; x = 0
  755. params[:question].each do |key, value|
  756.  
  757. q_data +=
  758. "<q id=\"#{i}\" subid=\"#{x+1}\">" + value[:T1] + "</q>" +
  759. "<q id=\"#{i}\" subid=\"#{x+2}\">" + value[:T2] + "</q>" +
  760. "<q id=\"#{i}\" subid=\"#{x+3}\">" + value[:T3] + "</q>"
  761. i += 1
  762. end
  763. q_data += "<asw>"
  764. params[:question].each do |key, value|
  765. q_data+= value[:T4] + ","
  766. end
  767. q_data +="</asw>
  768.  
  769.  
  770. <weight>" + params[:q_weight] + "</weight>
  771. </question>"
  772. @question.q_data = q_data
  773.  
  774. @question.question_type = params[:q_category]
  775. @question.depreciated = false
  776. @question.save
  777. end
  778.  
  779.  
  780.  
  781. when "Short Answer"
  782. #make question and store error messages in error_messages string
  783. error_messages = @question.make(params[:q_category],params[:q_subcategory],params[:q_summary],"Short Answer",params[:q_text],params[:q_asw],params[:q_weight])
  784. #if question.make() returned true, clear error messages
  785. error_messages = "" if error_messages == true
  786. #display error messages unless they are blank (which would mean there are none)
  787. unless error_messages.blank?
  788. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  789. render :partial => 'shortanswer', :layout=>true
  790. return
  791. end
  792.  
  793.  
  794. else #To create a question of any other type (at the moment only long answer)
  795. logger.debug("Reached else")
  796. error_messages = @question.make(params[:question_type], params[:q_subcategory], params[:q_summary], params[:q_type], params[:q_text],params[:q_asw]) #weight should be added when question weighting implemented
  797. error_messages = "" if error_messages == true
  798. if error_messages.blank?
  799. @question.question_type = params[:question_type]
  800. @question.depreciated = false
  801. @question.save
  802. else
  803. flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  804. render :partial=> 'longanswer', :layout=>true
  805. return
  806. end
  807. end
  808.  
  809. @question.question_type = "Literacy" if @question.question_type.nil?
  810. #this is for all question types
  811. logger.debug("question: " + @question.inspect + params[:question].to_s)
  812. logger.debug("Make question success")
  813. if @question
  814. # if @question.get_type == :multiple #|| @question.get_type == :true_false
  815. # logger.debug("Redirecting to add_choices")
  816. # redirect_to(:action=>:add_choices, :id=>@question.id, :choices=>params[:q_choices])
  817. # else
  818. flash[:notice] = "Question was successfully created."
  819. redirect_to(:action =>:index)
  820. #end
  821. else
  822. render :action => "new_question"
  823. end
  824. end
  825.  
  826. # def xml_snq
  827. # render :=>"teacher/create_wordmatch?choices=5", :layout=>false
  828. # end
  829.  
  830. #show students results
  831. def student_results
  832. @exam = false
  833. @st_exam = StudentExam.find_by_id(params[:id])
  834. #Notification of skipped question when teacher views student result
  835. #Edited at 11.45pm 15/05/2009 by Nhi
  836. #change1
  837. @skipped = Array.new(Result.find(:all, :conditions=>{:student_exam_id=>@st_exam.id, :answer=>"Skipped"}))
  838. #End of change
  839. @results = Array.new(Result.find_all_by_student_exam_id(@st_exam.id))
  840. #change2
  841. @results.delete_if {|s| s.answer == "Skipped" }
  842. #End of change
  843. if params[:r_mark]
  844. for result in @results
  845. if params[:r_mark].include?(result.id.to_s)
  846. result.score = "1"
  847. else
  848. result.score = "0"
  849. end
  850. #saves the marked result to the result table
  851. result.marked = true
  852. result.save
  853. end
  854. end
  855. end
  856.  
  857. def main_form
  858. if params[:type]
  859. case params[:type]
  860.  
  861. when "Please Select An Option Below"
  862. render :partial=>'nullpage'
  863. when "Multiple Choice"
  864. @type = 'multiplechoice'
  865. render :partial=>'choices'
  866. when "True False"
  867. render :partial=>'truefalse'
  868. when "Word Match"
  869. @type = 'wordmatch'
  870. render :partial=>'choices'
  871. when "Sequence"
  872. @type = 'sequence'
  873. render :partial=>'choices'
  874. when "Arrange Alphabetical"
  875. @type = 'arrangealpha'
  876. render :partial=>'choices'
  877. when "Arrange Order"
  878. @type = 'arrangeorder'
  879. render :partial=>'choices'
  880. when "Fill Blanks"
  881. @type = 'fillblanks'
  882. render :partial=>'choices'
  883. when "Wrong Word"
  884. @type = 'wrongword'
  885. render :partial=>'choices'
  886. when "Long Answer"
  887. render :partial=>'longanswer'
  888. when "Short Answer"
  889. render :partial=>'shortanswer'
  890. when "Spelling"
  891. @type = 'spelling'
  892. render :partial=>'choices'
  893. when "Calculation"
  894. @type = 'calculation'
  895. render :partial=>'choices'
  896. when "Multiple Short Answer"
  897. @type = 'multipleshortanswer'
  898. render :partial=>'choices'
  899. when "Multipart"
  900. render :partial=>'multipart'
  901. end
  902. end
  903. end
  904.  
  905. def choices
  906. #if
  907. @choices = params[:partamount].to_i
  908. if(@choices <= 0)
  909. flash[:error1] = "<h1><font color=red>Choices must be numeric and greater than 0</font></h1>"
  910. render :partial=>'error_page'
  911. else
  912. case params[:q_type]
  913. when 'calculation'
  914. render :partial => 'create_calculation'
  915. when 'wordmatch'
  916. render :partial => 'create_wordmatch'
  917. when 'arrangealpha'
  918. render :partial => 'create_arrangealpha'
  919. when 'wrongword'
  920. render :partial => 'create_wrongword'
  921. when 'arrangeorder'
  922. render :partial => 'create_arrangeorder'
  923. when 'sequence'
  924. render :partial => 'create_sequence'
  925. when 'spelling'
  926. render :partial => 'create_spelling'
  927. when 'multipleshortanswer'
  928. render :partial => 'create_multipleshortanswer'
  929. when 'multiplechoice'
  930. render :partial => 'create_multiplechoice'
  931. when 'fillblanks'
  932. render :partial => 'create_fillblanks'
  933. end
  934. end
  935. end
  936.  
  937. def edit_exam
  938. @exam = Exam.find(params[:id])
  939. @questions = @exam.questions.find(:all)
  940. @all_questions = Question.find(:all)
  941. end
  942.  
  943. #Update the exam, find the exam ID , then update the exam
  944. def update_exam
  945. @exam = Exam.find(params[:id])
  946.  
  947. respond_to do |format|
  948. if @exam.update_attributes(params[:exam])
  949. flash[:notice] = "Exam #{@exam.name} was successfully updated."
  950. format.html { redirect_to(:action=>:index) }
  951. else
  952. format.html { render :action => "edit_exam" }
  953. end
  954. end
  955. end
  956.  
  957. def show_help
  958. #gets ID for question data from the help index then displays the question to the teacher.
  959. #currently needs work as the question is shown in its XML format, other than that its fine!
  960. #Created by Eian Blacklock // LAST EDITED June 1st 2009
  961.  
  962. @question = Question.find(params[:id])
  963. respond_to do |format|
  964. format.html
  965. format.xml { render :xml => @question }
  966. end
  967. end
  968.  
  969.  
  970. def add_help
  971. #used to create a new help record.
  972. #@questions contains all questions and is used for the drop down list when it is implemented.
  973. #Created by Eian Blacklock // LAST EDITED June 1st 2009
  974.  
  975. @questions = Question.find(:all)
  976. @help = Help.new
  977. end
  978.  
  979.  
  980. def create_help
  981. # this will allow the teacher to create new help - Eian
  982. # takes object :help passed in from add help page and makes a new help record in table
  983. # Logger simply adds information to the development log and is not integral to the method.
  984. # Created by Eian Blacklock // LAST EDITED June 1st 2009
  985.  
  986. @help = Help.new(params[:help])
  987. @help.save
  988. logger.info(@help.inspect)
  989. redirect_to :action=>:help_index
  990. end
  991.  
  992.  
  993. def help_index
  994. #Created by Eian Blacklock // LAST EDITED June 1st 2009
  995. @helps = Help.find(:all, :order => :question_id)
  996. respond_to do |format|
  997. format.html
  998. end
  999. end
  1000.  
  1001.  
  1002. def edit_help
  1003. #this will allow the teacher to edit existing help with update_help
  1004. #Created by Eian Blacklock // LAST EDITED June 1st 2009
  1005.  
  1006. @help = Help.find(params[:id])
  1007. end
  1008.  
  1009.  
  1010. def update_help
  1011. #Created by Eian Blacklock // LAST EDITED June 1st 2009
  1012.  
  1013. @help = Help.find(params[:id])
  1014. respond_to do |format|
  1015. if @help.update_attributes(params[:help])
  1016. flash[:notice] = "Help data for question #{@help.question_id} was successfully updated."
  1017. format.html { redirect_to(:action => :help_index) }
  1018. else
  1019. format.html { render :action => "edit_help" }
  1020. end
  1021. logger.info(@help.inspect)
  1022. end
  1023. end
  1024.  
  1025.  
  1026. protected
  1027. def authorize
  1028. unless Teacher.find_by_id(session[:teacher_id])
  1029. session[:original_uri] = request.request_uri
  1030. flash[:notice] = "Please login with your user name and password."
  1031. redirect_to :controller => :student, :action => :login
  1032. end
  1033. end
  1034.  
  1035. def authorized?
  1036. unless Teacher.find_by_id(session[:teacher_id])
  1037. return false
  1038. else
  1039. return true
  1040. end
  1041. end
  1042. end
  1043. class TeacherController < ApplicationController
  1044.  
  1045. #User has to login first before access
  1046. before_filter :authorize, :except => [:login, :logout]
  1047.  
  1048. #Determine the login ID, if it's a teacher/student/admin login
  1049. #Check for password and set the session
  1050. def login
  1051. session[:student_id] = nil
  1052. session[:teacher_id] = nil
  1053. session[:admin_id] = nil
  1054. if request.post?
  1055. student = Student.authenticate(params[:name], params[:password])
  1056. teacher = Teacher.authenticate(params[:name], params[:password])
  1057. admin = Admin.authenticate(params[:name], params[:password])
  1058. if teacher
  1059. logger.info("Setting session teacher")
  1060. session[:teacher_id] = teacher.id
  1061. redirect_to({ :controller => :teacher, :action => "index" })
  1062. elsif student
  1063. logger.info("setting session student")
  1064. session[:student_id] = student.id
  1065. uri = session[:original_uri]
  1066. session[:original_uri] = nil
  1067. redirect_to(uri || { :action => "introduction" })
  1068. elsif admin
  1069. logger.info("Setting session admin")
  1070. session[:admin_id] = admin.id
  1071. redirect_to({ :controller => :admin, :action => "index" })
  1072. else
  1073. logger.info("Bad password")
  1074. flash[:notice] = "Invalid User Name/Password"
  1075. end
  1076. end
  1077. end
  1078.  
  1079. #log out by reset_session
  1080. def logout
  1081. @exam = false
  1082. reset_session
  1083.  
  1084. respond_to do |format|
  1085. format.html
  1086. end
  1087. end
  1088.  
  1089. #Show all exams available for teacher
  1090. def index
  1091. @exam = false
  1092. @exams = Exam.find(:all , :conditions=>{:status=>1})
  1093. #Display username when logged in
  1094. #Edited at 2.40pm 01/05/2009 by Nhi
  1095. #Change1
  1096. @teacher = Teacher.find_by_id(session[:teacher_id])
  1097. end
  1098.  
  1099. def old_exams
  1100. @exam = false
  1101. @exams = Exam.find(:all , :conditions=>{:status=>0}, :order=>'name ASC, updated_at DESC')
  1102. end
  1103.  
  1104. #Not applicable
  1105. def edit
  1106. end
  1107.  
  1108. #Arrange the question
  1109. def arrange
  1110. @exam = false
  1111. if params[:id]
  1112. @exam = Exam.find_by_id(params[:id])
  1113. eq = ExamQuestion.find_all_by_exam_id(@exam.id, :order => :position)
  1114. @questions = Array.new
  1115. for e in eq
  1116. @questions.push(Question.find_by_id(e.question_id))
  1117. end
  1118. @questions.delete_at(0)
  1119. else
  1120. redirect_to(:action=>:arrange, :id=>params[:exam_selection])
  1121. end
  1122. end
  1123.  
  1124. def save_order
  1125. @exam = Exam.find_by_id(params[:exam_id])
  1126. #@exam.status = 0
  1127. a = (params[:result][:answer]).split(',')
  1128.  
  1129. @st_exam = StudentExam.find_by_exam_id(params[:exam_id])
  1130.  
  1131. unless @st_exam == nil
  1132. exam = @exam.clone
  1133.  
  1134. exam.save
  1135. @exam.status = 0
  1136. @exam.save
  1137.  
  1138. @exam = exam
  1139. @exam.add_question(1, :first=>true)
  1140.  
  1141. end
  1142.  
  1143.  
  1144. logger.debug(@exam.inspect)
  1145.  
  1146.  
  1147. #sets all question in exam to position of -1
  1148. #ExamQuestion.update_all("position = -1 ","exam_id = #{@exam.id}")
  1149. @eq = Array.new(ExamQuestion.find_all_by_exam_id(@exam.id))
  1150. @eq.each {|e| e.position = -1 }
  1151.  
  1152. a.insert(0, 1)
  1153. logger.info(a.inspect)
  1154. ExamQuestion.update_all("position = -1","exam_id = #{@exam.id}")
  1155. i = 0; until i == a.length
  1156. eq = ExamQuestion.find_by_exam_id_and_question_id(@exam.id, a[i].to_i)
  1157. unless eq.nil?
  1158. eq.update_attribute(:position, i)
  1159. eq.save
  1160. else
  1161. if Question.find_by_id(a[i].to_i).get_type == :master
  1162. logger.info("multipart")
  1163. @exam.add_multipart(a[i].to_i)
  1164. else
  1165. logger.info("single")
  1166. @exam.add_question(a[i].to_i)
  1167. #updates the position in the exam questions table of new questions that have been added
  1168. ExamQuestion.update_all("position = #{i}","exam_id = #{@exam.id} AND question_id=#{a[i].to_i}")
  1169. end
  1170. end
  1171. i += 1
  1172. end
  1173.  
  1174. #deletes any rows that haven't been updated with a new position
  1175. #ie any question removed from the exam will be deleted from the database
  1176. ExamQuestion.delete_all("exam_id = #{@exam.id} AND position = -1")
  1177.  
  1178. @questions = Array.new(@exam.questions.find(:all))
  1179. #redirect_to :action => :assign, :id => @exam.id
  1180. if params[:commit] == "Save Question List and Display Question Types"
  1181. redirect_to :action => :add_questions, :id => @exam.id, :e_type =>params[:e_type], :q_type =>params[:q_type]
  1182. else
  1183. redirect_to :action => :assign, :id => @exam.id
  1184. end
  1185. end
  1186.  
  1187. #
  1188. def add_questions
  1189. if params[:commit] == "Mark or View Student Assessment Results" #|| "View Student Reports"
  1190. redirect_to(:action=>:exam_results, :id=>params[:exam_selection])
  1191. elsif params[:commit] == "View Old Exams"
  1192. redirect_to(:action=>:old_exams )
  1193. elsif params[:commit] == "Unassign Students from Selected Assessment"
  1194. redirect_to(:action=>:unassign_exam, :id=>params[:exam_selection])
  1195. else
  1196. @exam = false
  1197. if params[:id]
  1198. if params[:commit] == "Next: Arrange Questions"
  1199. redirect_to(:action=>:arrange, :id=>params[:id])
  1200. else
  1201. @exam = Exam.find_by_id(params[:id])
  1202. if params[:e_type] == 'All'
  1203. @questions = Array.new(Question.find(:all,:conditions=>["question_type <> 'New' AND question_type <> 'End' AND master_id is null"]))
  1204. @exam_questions = Array.new(@exam.questions.find(:all, :conditions=>["question_type <> 'New'"]))
  1205.  
  1206. else
  1207. if params[:q_type]
  1208. @questions = Array.new(Question.find(:all,:conditions=>{:master_id=>nil}))
  1209. ones_of_right_type = Array.new
  1210. @questions.each { |q|
  1211. if q.get_type_string(params[:q_type])==q.get_type && q.question_type!='End' && q.question_type!='New'
  1212. logger.debug("q.get_type: " + q.get_type.to_s)
  1213. logger.debug("q_type: " + params[:q_type])
  1214. ones_of_right_type.push q
  1215. end
  1216. }
  1217. @questions = ones_of_right_type
  1218. @exam_questions = Array.new(@exam.questions.find(:all, :order => 'position asc'))
  1219. else
  1220. @questions = Array.new(Question.find(:all,:conditions=>{:question_type => @exam.exam_type,:master_id=>nil}))
  1221. @exam_questions = Array.new(@exam.questions.find(:all, :conditions=>["question_type <> 'New'"]))
  1222. end
  1223. end
  1224. end
  1225. else
  1226. if params[:exam_selection]
  1227. if params[:commit] == "Assign Students to Selected Assessment"
  1228. redirect_to(:action=>:assign, :id=>params[:exam_selection])
  1229. else
  1230. redirect_to(:action=>:add_questions, :id=>params[:exam_selection])
  1231. end
  1232. else
  1233. flash[:notice] = "Select an exam to edit"
  1234. redirect_to(:action=>:index)
  1235. end
  1236. end
  1237. end
  1238. end
  1239.  
  1240.  
  1241. def add
  1242. @exam = Exam.find_by_id(params[:exam_id])
  1243. unless @exam.exam_questions.find_by_question_id_and_exam_id(params[:question_id], @exam.id)
  1244. if Question.find_by_id(params[:question_id]).get_type == :master
  1245. logger.info("multipart")
  1246. @exam.add_multipart(params[:question_id])
  1247. else
  1248. logger.info("single")
  1249. @exam.add_question(params[:question_id])
  1250. end
  1251. flash[:notice] = "Question id " + params[:question_id] + " added to exam."
  1252. else
  1253. flash[:notice] = "You have added this question already. You may change the order at the Arrange page"
  1254. end
  1255. redirect_to(:action => :add_questions, :id => params[:exam_id])
  1256. end
  1257.  
  1258. #When assign to student button clicked/ Display success message
  1259. #Edited at 12.45pm 06/05/2009 by Nhi
  1260. def assign
  1261. @exam = Exam.find_by_id(params[:id])
  1262. @students = Student.find(:all)
  1263. if params[:student]
  1264. @students_assigned = Array.new #change1
  1265. for student in @students
  1266. if params[:student][student.id.to_s][:assign] == 1.to_s
  1267. student.assign_exam(@exam.id)
  1268. @students_assigned.push student # change2
  1269. end
  1270. end
  1271. a = ""
  1272. @students_assigned.each {|e| a+= e.name; a += ", " unless e == @students_assigned.last }
  1273. flash[:notice] = "Students Assigned to test: " + a
  1274. else
  1275. end
  1276. end
  1277.  
  1278.  
  1279. def unassign_exam
  1280. @exam = Exam.find(params[:id])
  1281. @students = Student.find(:all)
  1282. @st_exams = StudentExam.find_all_by_exam_id(@exam.id)
  1283. if params[:st_exam]
  1284. for st_exam in @st_exams
  1285. if params[:st_exam][st_exam.id.to_s][:unassign_exam] == "1"
  1286. st_exam.destroy
  1287. flash[:notice] = "Test was successfully unassigned."
  1288. redirect_to(:action =>:index)
  1289. end
  1290. end
  1291. end
  1292. end
  1293.  
  1294.  
  1295. def create_new_exam
  1296. @exam = Exam.new
  1297. if params[:exam]
  1298. @exam = Exam.new(params[:exam])
  1299. if @exam.make(:e_name=>params[:exam][:name], :exam_type=>params[:exam_type], :e_status=> 1, :e_skippable => 1, :e_results_viewable=>params[:viewable]) #CHANGE WHEN INTERFACE IS FINISHED, added in default e_status
  1300. if @exam.save
  1301. @exam.add_question(1, :first=>true)
  1302. @exam.add_question(2)
  1303. redirect_to(:action=>:add_questions, :id=>@exam.id)
  1304. end
  1305. else
  1306. flash[:notice] = "<div id = errorMessage> You can only add an exam of the same name once </div>"
  1307. redirect_to :action=>:index
  1308. end
  1309. end
  1310. end
  1311.  
  1312. def report
  1313. @exam = Exam.find_by_id(params[:id])
  1314. @st_exam = StudentExam.find_by_id(params[:subid])
  1315. logger.debug("student_exam: " + @st_exam.inspect)
  1316. @subcategories = @exam.get_subcategories
  1317. @exam_questions = Array.new(@exam.questions.find(:all))
  1318. end
  1319.  
  1320.  
  1321. def exam_results
  1322. @exam = Exam.find_by_id(params[:id]) || false
  1323. @st_exams = StudentExam.find_all_by_exam_id(@exam.id)
  1324. if @st_exams.nil? || @st_exams.empty?
  1325. flash[:notice] = "Please select an exam before chosing view student report"
  1326. redirect_to :action=>:index
  1327. end
  1328. end
  1329.  
  1330. # def save_new_question
  1331. # @question = Question.new
  1332. # case params[:q_type]
  1333. #
  1334. #
  1335. # when "Multiple Choice"
  1336. # error_messages = ""
  1337. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Multiple Choice",params[:q_text],params[:asw],params[:q_weight])
  1338. # error_messages = "" if error_messages == true
  1339. # blank_word = false
  1340. # params[:question].each do |key,value|
  1341. # blank_word = true if value[:q_asw].blank?
  1342. # end
  1343. # error_messages += "Error: Please enter a word into all of the option boxes." if blank_word
  1344. #
  1345. # if error_messages.blank?
  1346. # #if no errors, continue and add choices
  1347. # params[:question].each do |key,value|
  1348. # @question.add_choice(value[:q_asw])
  1349. # end
  1350. # else
  1351. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1352. # options = params[:parts]
  1353. # redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  1354. # return
  1355. # end
  1356. #
  1357. #
  1358. # when "Sequence"
  1359. # answer = ""
  1360. # blank_word = false
  1361. # blank_answer = false
  1362. # wrong_answer = false
  1363. # answer_total = 0
  1364. # right_answer_total = 0
  1365. # i = 1
  1366. # possible_answers = Array.new
  1367. # while i <= params[:parts].to_i
  1368. # right_answer_total += i
  1369. # possible_answers.push(i.to_s)
  1370. # i += 1
  1371. # end
  1372. # params[:question].each do |key,value|
  1373. # answer = answer + value[:q_answer] + ","
  1374. # blank_word = true if value[:q_part].blank? == true
  1375. # blank_answer = true if value[:q_answer].blank? == true
  1376. # answer_total += value[:q_answer].to_i
  1377. # wrong_answer = true unless possible_answers.include?(value[:q_answer])
  1378. # end
  1379. #
  1380. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Sequence",params[:q_text],answer,params[:q_weight])
  1381. # error_messages = "" if error_messages == true
  1382. # error_messages += "Error: Must write in all word boxes.<br>" if blank_word == true
  1383. # error_messages += "Error: Must write in all answer boxes.<br>" if blank_answer == true
  1384. # error_messages += "Error: One of your answers was outside the valid range.<br>" if wrong_answer == true
  1385. # error_messages += "Error: Your answers do not add up to the right amount.<br>" if right_answer_total != answer_total
  1386. #
  1387. # if error_messages.blank?
  1388. # params[:question].each do |key,value|
  1389. # @question.add_choice(value[:q_part])
  1390. # end
  1391. # else
  1392. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1393. # redirect_to :action=>'main_form', :parts=>params[:parts]
  1394. # return
  1395. # end
  1396. #
  1397. #
  1398. #
  1399. # when "True False"
  1400. # #make question, storing error messages in a string
  1401. # error_messages = @question.make(params[:q_category],params[:q_subcategory],params[:q_summary],"True False",params[:q_text],params[:q_asw],params[:q_weight])
  1402. # #clear error messages if question.make returned true
  1403. # error_messages = "" if error_messages == true
  1404. # #display error messages if they exist
  1405. # unless error_messages.blank?
  1406. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1407. # render :partial=>'truefalse', :layout=>true #refresh the page
  1408. # return #do not continue
  1409. # end
  1410. # #add the choices to the question so it displays properly to the student
  1411. # @question.add_choice("True")
  1412. # @question.add_choice("False")
  1413. #
  1414. #
  1415. #
  1416. # when "Multiple Short Answer"
  1417. # answer = ""
  1418. # blank_word = false
  1419. # params[:question].each do |key,value|
  1420. # answer = answer + value[:q_answer] + "," #create answer string for question making
  1421. # blank_word = true if value[:q_answer].blank? #check if answer box is blank
  1422. # blank_word = true if value[:q_part].blank? #check if question part text is blank
  1423. # end
  1424. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Multiple Short Answer", params[:q_text], answer, params[:weight])
  1425. # error_messages = "" if error_messages == true
  1426. # error_messages += "Error: Please enter text into all of the question and answer boxes." if blank_word == true
  1427. # #add choices if error messages are blank
  1428. # if error_messages.blank?
  1429. # params[:question].each do |key,value|
  1430. # @question.add_choice(value[:q_part])
  1431. # end
  1432. # else #display error messages if they exist
  1433. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1434. # redirect_to :action=>'main_form', :parts=>params[:parts]
  1435. # return
  1436. # end
  1437. #
  1438. #
  1439. #
  1440. #
  1441. #
  1442. # when "Arrange Order"
  1443. # answer = ""
  1444. # params[:question].each do |key,value|
  1445. # answer = answer + value[:q_answer] + ","
  1446. # end
  1447. #
  1448. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Arrange Order", params[:q_text], answer, params[:weight])
  1449. # error_messages = "" if error_messages == true
  1450. # blank_word == false
  1451. # params[:question].each do |key,value|
  1452. # blank_word = true if value[:q_part].blank?
  1453. # end
  1454. # params[:question].each do |key,value|
  1455. # blank_word = true if value[:q_answer].blank?
  1456. # end
  1457. # if(blank_word)
  1458. # error_messages = error_messages + "Error: You must enter text in all of the boxes. Please review your question text and answers.<br>"
  1459. # end
  1460. # unless error_messages.blank?
  1461. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1462. # redirect_to :action=>'main_form', :choices=>params[:parts]
  1463. # return
  1464. # end
  1465. #
  1466. # params[:question].each do |key,value|
  1467. # @question.add_choice(value[:q_part])
  1468. # end
  1469. #
  1470. #
  1471. #
  1472. #
  1473. # when "Select Wrong Word"
  1474. # #make question and store error messages in error_messages string
  1475. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Select Wrong Word",params[:q_text],params[:q_asw],params[:q_weight])
  1476. # #clear error_messages if question.make returns true
  1477. # error_messages = "" if error_messages == true
  1478. # blank_word = false
  1479. # #check all option boxes are written in
  1480. # params[:question].each do |key,value|
  1481. # blank_word = true if value[:q_option].blank?
  1482. # end
  1483. # if(blank_word == true)
  1484. # error_messages = error_messages + "Error: You must write in all of the option boxes.<br>"
  1485. # end
  1486. # #display the error messages if they exist
  1487. # unless error_messages.blank?
  1488. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1489. # options = params[:q_options]
  1490. # redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  1491. # return
  1492. # else #otherwise, add all the choices to the question and continue
  1493. # params[:question].each do |key,value|
  1494. # @question.add_choice(value[:q_option])
  1495. # end
  1496. # end
  1497. #
  1498. # when "Arrange Alphabetical"
  1499. # answer = ""
  1500. # #create answer string to use when making question
  1501. # params[:question].each do |key,value|
  1502. # answer = answer + value[:q_answer] + ","
  1503. # end
  1504. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary],"Arrange Alphabetical",params[:q_text],answer,params[:q_weight])
  1505. # error_messages = "" if error_messages == true
  1506. # #check all option boxes are written in
  1507. # blank_word = false
  1508. # params[:question].each do |key,value|
  1509. # blank_word = true if value[:q_part].blank?
  1510. # end
  1511. # error_messages += "Error: Please write in all option boxes.<br>" if blank_word == true
  1512. # #check all answer boxes are written in
  1513. # blank_answer = false
  1514. # params[:question].each do |key,value|
  1515. # blank_answer == true if value[:q_answer].blank?
  1516. # end
  1517. # error_messages += "Error: Please write in all answer boxes.<br>" if blank_answer == true
  1518. # #check answers add up to the right total
  1519. # right_total = 0
  1520. # i = 1
  1521. # while i <= params[:options].to_i
  1522. # right_total += i
  1523. # i += 1
  1524. # end
  1525. # q_ans_total = 0
  1526. # params[:question].each do |key,value|
  1527. # q_ans_total += value[:q_answer].to_i
  1528. # end
  1529. # if q_ans_total != right_total
  1530. # error_messages += "Error: Your answers do not add up to the right total. Please review your answers.<br>"
  1531. # end
  1532. # #check all answers are in the correct range
  1533. # correct_range = Array.new
  1534. # i = 1
  1535. # while i <= params[:options].to_i
  1536. # correct_range.push(i.to_s)
  1537. # i += 1
  1538. # end
  1539. # wrong_value = false
  1540. # params[:question].each do |key,value|
  1541. # wrong_value = true unless correct_range.include?(value[:q_answer])
  1542. # end
  1543. # error_messages += "Error: At least one of your answers is outside the valid range.<br>" if wrong_value == true
  1544. # #display error messages unless they are blank
  1545. # unless error_messages.blank?
  1546. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1547. # options = params[:options]
  1548. # redirect_to(:action=>'main_form', :parts=>options) # refresh the page so the error messages show
  1549. # return
  1550. # else #if error messages are blank, add all options to the question and continue (will be saved at end of method)
  1551. # params[:question].each do |key,value|
  1552. # @question.add_choice(value[:q_part])
  1553. # end
  1554. # end
  1555. #
  1556. #
  1557. # when "Word Match" #to create a question of type "Word Match"
  1558. # answer = ""
  1559. # #create answer string to use when making question
  1560. # params[:question].each do |key,value|
  1561. # answer = answer + value[:answer].to_s + ","
  1562. # end
  1563. # error_messages = "" #clear error messages
  1564. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Word Match", params[:q_text], answer, params[:q_weight])
  1565. # error_messages = "" if error_messages == true
  1566. # #check if all word and match boxes are filled
  1567. # blank_word = false
  1568. # params[:question].each do |key,value|
  1569. # blank_word = true if value[:word].blank? || value[:match].blank?
  1570. # end
  1571. # if blank_word
  1572. # error_messages = error_messages + "Error: You have at least one blank word box. Please enter something into every word box.<br>"
  1573. # end
  1574. # # set up valid answers
  1575. # # what should the answers add up to
  1576. # answer_invalid = false
  1577. # right_total = 0
  1578. # total = 0
  1579. # i = 1
  1580. # while i <= params[:options].to_i
  1581. # right_total = right_total + i
  1582. # i = i + 1
  1583. # end
  1584. # # what are all the correct possible answers
  1585. # possible_answers = Array.new()
  1586. # i = 1
  1587. # while i <= params[:options].to_i
  1588. # possible_answers.push(i.to_s)
  1589. # i = i+1
  1590. # end
  1591. # #add up all the answers to check if they are all there,
  1592. # #and also check if they are a valid number
  1593. # params[:question].each do |key,value|
  1594. # total = total + value[:answer].to_i
  1595. # unless possible_answers.include?(value[:answer].to_s)
  1596. # answer_invalid = true
  1597. # end
  1598. # end
  1599. # if total != right_total
  1600. # answer_invalid = true
  1601. # end
  1602. # if answer_invalid
  1603. # error_messages = error_messages + "Error: Answers invalid. Please review your answers in the \"Matches to Number\" column."
  1604. # end
  1605. #
  1606. # flash[:error] = "" #clear error messages
  1607. # #display error messages if they exist
  1608. # unless error_messages.blank? || error_messages == true #when there are error messages and question making was unsuccessful
  1609. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>" #put error messages on the page
  1610. # #@blah = Question.new(:q_summary=>params[:q_summary]) # save the text the user has already entered
  1611. # #render(:action=>'create_wordmatch', :object=>@blah) # refresh the page so the error messages show
  1612. # options = params[:options]
  1613. # redirect_to(:action=>'main_form', :choices=>options) # refresh the page so the error messages show
  1614. # return #do not continue making the question if errors are found
  1615. #
  1616. # else #if no errors, add the choices to the question
  1617. # params[:question].each do |key,value|
  1618. # @question.add_choice(value[:word], "word")
  1619. # end
  1620. # params[:question].each do |key,value|
  1621. # @question.add_choice(value[:match], "match")
  1622. # end
  1623. # end
  1624. #
  1625. # when "Calculation"
  1626. # answer = ""
  1627. # #create answer string to use when creating question
  1628. # params[:question].each do |key,value|
  1629. # answer = answer + value[:q_answer] + ","
  1630. # end
  1631. # # make question and store error messages
  1632. # error_messages = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Calculation", params[:q_text], answer, params[:weight])
  1633. # # if question.make is true (successful), clear the error messages
  1634. # error_messages = "" if error_messages == true
  1635. # blank_word == false
  1636. # #check if the user left any word boxes blank
  1637. # params[:question].each do |key,value|
  1638. # blank_word = true if value[:q_part].blank?
  1639. # end
  1640. # params[:question].each do |key,value|
  1641. # blank_word = true if value[:q_answer].blank?
  1642. # end
  1643. # if(blank_word)
  1644. # error_messages = error_messages + "Error: You must enter text in all of the boxes. Please review your question text and answers.<br>"
  1645. # end
  1646. #
  1647. # flash[:error] == ""
  1648. # #display the error messages unless they are blank
  1649. # unless error_messages.blank?
  1650. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1651. # options = params[:parts]
  1652. # redirect_to(:action => 'main_form', :parts=>options)
  1653. # return
  1654. # #if there are no error messages, add all the question choices to the question and continue
  1655. # #(question will be saved at the end of this method)
  1656. # else
  1657. # params[:question].each do |key,value|
  1658. # @question.add_choice(value[:q_part])
  1659. # end
  1660. # end
  1661. #
  1662. # when "Fill in Blanks"
  1663. # answer = ""
  1664. # #create the answer string to be used when making the question later
  1665. # params[:question].each do |key,value|
  1666. # answer = answer + value[:q_blank] + ","
  1667. # end
  1668. #
  1669. # blanks = params[:q_text].scan('%%%') #array of '%%%' is stored in blanks
  1670. # error_messages = ""
  1671. # if blanks.size != (params[:q_amount].to_i)
  1672. # error_messages += "Error: You have the wrong number of blank spaces in your question text. Please enter %%% instead of the word anywhere you want a blank to be.<br>"
  1673. # end
  1674. # #create question and store errors in make_errors string
  1675. # make_errors = @question.make(params[:q_category], params[:q_subcategory], params[:q_summary], "Fill in Blanks", params[:q_text] + " ", answer,params[:weight])
  1676. # error_messages += make_errors unless make_errors == true
  1677. #
  1678. # #check all word boxes have text
  1679. # blank_word = false
  1680. # params[:question].each do |key,value|
  1681. # blank_word = true if value[:q_blank].blank?
  1682. # end
  1683. # error_messages += "Error: Please enter a word into all of the blank word boxes.<br>" if blank_word == true
  1684. #
  1685. # #display messages if error messages exist
  1686. # unless error_messages.blank?
  1687. # flash[:error] = "<h1><font color=red>" + error_messages + "</font></h1>"
  1688. # options = params[:q_amount]
  1689. # redirect_to(:action => 'main_form', :parts=>options)
  1690. # return #do not continue processing (this means question will not be saved)
  1691. # end
  1692. #
  1693. #
  1694. # when "Multipart"
  1695. # @question.q_data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
  1696. # <question category=\"Literacy\" subcategory=\"Multipart\">
  1697. # <qtype>Multipart</qtype>
  1698. # <qtext>" + params[:q_text] + "</qtext>
  1699. # <qsummary>" + params[:q_summary] + "</qsummary>
  1700. # </question>"
  1701. # @question.make(params[:q_category], params[:q_subcategory], params[:q_summarry], "Multipart")
Add Comment
Please, Sign In to add comment