[ { "id" : 1, "question" : " !", "answers" : [" ", "", "", ""] }, { "id" : 2, "question" : " ?", "answers" : ["", "", "", ""] }, …..( ) ]
require 'sinatra' require 'json'
get '/' do erb :index end
@@index <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1> I hh h!</h1> <h1> , !</h1> <h1> </h1> <h2> </h2> <div> <p> . </p> </div> <input value=" " name="test_begin" onclick="location.href='/test'" type="button"> </body> </html>
# @@test_hash # @@kol_testov = 13 # @@mas_test_number = Array.new(@@kol_testov) # , -1 @@mas_test_answer # test_file = File.open('mutaAlim.json',"rb") @@test_hash = JSON.parse(test_file.read) test_file.close # @@not_answer = " " # @@answers # @@ball # @@procent_tru_answers #
# get '/test' do if params[:var] == nil @@mas_test_answer = Array.new(@@kol_testov) { |i| -1} # @@mas_test_number @@mas_test_number[0] = Random.rand(@@test_hash.length) @@mas_test_number.each_index do |i| begin flag = true buf = Random.rand(@@test_hash.length) 0..i.times do |j| if @@mas_test_number[j]==buf flag = false end end if flag @@mas_test_number[i] = buf end end until flag==true end @@test_number = 0; # @@mas_random_variants @@mas_random_variants = Array.new(@@kol_testov) @@mas_random_variants.each_index do |i| @@mas_random_variants[i] = Array.new(@@test_hash[@@mas_test_number[i]]["answers"].size) end @@mas_random_variants.each_index do |i| @@mas_random_variants[i][0] = Random.rand(@@test_hash[@@mas_test_number[i]]["answers"].size) @@mas_random_variants[i].each_index do |j| begin flag = true buf = Random.rand(@@test_hash[@@mas_test_number[i]]["answers"].size) 0..j.times do |k| if @@mas_random_variants[i][k]==buf flag = false end end if flag @@mas_random_variants[i][j] = buf end end until flag==true end end else # @@mas_test_answer @@mas_test_answer[@@test_number] = params[:var].to_i # if @@test_number != @@kol_testov - 1 @@test_number += 1 end end erb :test end
params[:var] == nil
we determine that the user is on the page for the first time and “prepare” everything necessary (randomly select tests from the database, randomly “mix” response options and create an array to store the answers), otherwise we write the answer chosen by the user in array The entire functionality of the application is built on get requests, and there is a lot of confusion due to the fact that it was written in the "just to work" mode. For example, if a user clicks on the link http: // localhost: 4567 / test? Var = 0, then in the array of answers to the current question, the value will be set to 0. @@test <!DOCTYPE html> <html> <head> <title></title> </head> <body> <div> <h2> №<%= @@test_number +1 %> <h2> <h3> <%= @@test_hash[@@mas_test_number[@@test_number]]["question"] %> </h3> </div> <p> <form method="get" action="/test"> <% @@test_hash[@@mas_test_number[@@test_number]]["answers"].each_index do |i| %> <input type="radio" name="var" value="<%= @@mas_random_variants[@@test_number][i] %>"> <%= @@test_hash[@@mas_test_number[@@test_number]]["answers"][@@mas_random_variants[@@test_number][i]] %> <% end %> <input type="submit" value=""/> </form> </p> <div> <input type="button" name="next_question" value=" " onclick="location.href='/test/pred'"> <% @@mas_test_number.each_index do |i| %> <a href='/test/question/<%= i + 1 %>'> № <%= i + 1 %> </a> <% end %> <input type="button" name="pred_question" value=" " onclick="location.href='/test/next'"> </div> <input type="button" name="result" value=" " onclick="location.href='/test/result'"> </body> </html>
<%= # %>
and <% %>
. On the page itself is a question, answer options (radiobutton), links to all questions. test navigation buttons and a test completion button that directs the user to the test results page. # get '/test/question/:id' do @@test_number = params[:id].to_i - 1 erb :test end # get '/test/next' do if @@test_number != @@kol_testov - 1 @@test_number += 1 end erb :test end # get '/test/pred' do if @@test_number != 0 @@test_number -= 1 end erb :test end
# get '/test/result' do @@ball = 0 @@answers = 0 # @@mas_test_answer.each do |i| if i == 0 @@ball += 1 end if i != -1 @@answers += 1 end end @@procent_tru_answers = (100/@@kol_testov) * @@ball erb :result end
@@result <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1> </h1> <div> <table cellspacing="2" border="1" cellpadding="5"> <tr> <td> № </td> <td> </td> <td> </td> <td> </td> <tr> <% @@mas_test_answer.each_index do |i| %> <tr> <td> <%= i+1 %> </td> <td> <%= @@test_hash[@@mas_test_number[i]]["question"] %> </td> <td> <%= if (@@mas_test_answer[i] == -1) @@not_answer else @@test_hash[@@mas_test_number[i]]["answers"][@@mas_test_answer[i]] end %> </td> <td> <%= @@test_hash[@@mas_test_number[i]]["answers"][0] %> </td> <tr> <% end %> </table> </div> <div> <h3> = <%= @@kol_testov %> </h3> <h3> = <%= @@answers %> </h3> <h3> = <%= @@ball %> </h3> <h3> = <%= @@procent_tru_answers %> %</h3> <div> <div> <p> . </p> </div> <input value=" " name="test_begin" onclick="location.href='/test'" type="button"> </body> </html>
Source: https://habr.com/ru/post/226869/
All Articles