Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/admin/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def destroy
private

def test_params
params.require(:test).permit(:title, :level, :category_id, :active)
params.require(:test).permit(:title, :level, :category_id, :active, :duration_time)
end

def find_test
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/test_passages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class TestPassagesController < ApplicationController
before_action :find_test_passage, only: %i[show result update gist]
before_action :check_timer, only: :update

def show; end

Expand Down Expand Up @@ -41,6 +42,10 @@ def gist

private

def check_timer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не надо этого. добавь в if @test_passage.completed? проверку на таймер. пусть вся логика работает там. ты тут дубль пишешь

redirect_to result_test_passage_path(@test_passage) if @test_passage.time_over?
end

def find_test_passage
@test_passage = TestPassage.find(params[:id])
end
Expand Down
1 change: 1 addition & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "utilities/sorting"
import "utilities/password_check"
import "utilities/form_inline"
import "utilities/progress_bar"
import "utilities/timer"

Rails.start()
Turbolinks.start()
Expand Down
19 changes: 19 additions & 0 deletions app/javascript/utilities/timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
document.addEventListener('turbolinks:load', function() {
var timerView = document.querySelector('.timer')

if (timerView && timerView.dataset.timer !== "0") {
var timeLeft = timerView.dataset.timer * 60
var countdownInterval = setInterval(function() {
if (timeLeft > 0) {
timeLeft -= 1
} else {
clearInterval(countdownInterval)
alert('Time is over!')
document.querySelector('form').submit()
}

var resultTime = parseInt(timeLeft / 60) + ':' + timeLeft % 60
timerView.textContent = resultTime
}, 1000)
}
})
16 changes: 16 additions & 0 deletions app/models/test_passage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ def finished?
def success?
percent_correct_answers >= SUCCESS_RATE
end

def timer_on?
!test.duration_time.zero?
end

def time_over?
if timer_on?
(Time.current - created_at) / 60 >= test.duration_time
else
false
end
end

def remaining_time
remaining_minutes = (test.duration_time - (Time.current - created_at) / 60).to_i if !time_over?
end

private

Expand Down
2 changes: 1 addition & 1 deletion app/services/AchievementService.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def reward(badge)
end

def first_try?(_)
@user.test_passages.where(test_id: @test.id, finished: true).count == 1
@user.test_passages.where(test_id: @test.id).count == 1
end

def category_complete?(category)
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/tests/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<%= form.label :level %>
<%= form.number_field :level, id: :test_level, class: 'form-control' %>
</p>
<p>
<%= form.label :duration_time %>
<%= form.number_field :duration_time, id: :test_duration_time, class: 'form-control' %>
</p>
<p>
<%= form.label :category_id %>
<%= form.collection_select :category_id, Category.all, :id, :title, prompt: true, class: 'dropdown-menu' %>
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/tests/_test.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= render 'form_inline', test: test == @test ? @test : test %>
</td>
<td class="align-middle"><%= test.level %></td>
<td class="align-middle"><%= test.duration_time %></td>
<td class="align-middle"><%= link_to test.questions.count, admin_test_questions_path(test.id) %></td>
<td class="align-middle"><%= test.active %></td>
<td class="align-middle">
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<th><%= t('.id') %></th>
<th><%= t('.name') %></th>
<th><%= t('.level') %></th>
<th><%= t('.duration_time') %></th>
<th><%= t('.count_question') %></th>
<th><%= t('.active') %></th>
<th colspan="3"><%= t('.action') %></th>
Expand Down
3 changes: 3 additions & 0 deletions app/views/test_passages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<%= content_tag :div, class: "progress-bar", data: { questions_count: @test_passage.test.questions.count, current_question_number: @test_passage.current_question.number } do %>
<div class="progress-bar-line"></div>
<% end %>
<p>
<%= t('.time_left') %> <%= content_tag :span, '', class: 'timer', data: { time_left: @test_passage.remaining_time } %>
</p>
<p><%= t('.total', count: @test_passage.test.questions.count) %></p>
<% unless @test_passage.completed? %>
<p class="mb-3"><%= t('.currently', number: @test_passage.current_question.number) %></p>
Expand Down
1 change: 1 addition & 0 deletions app/views/tests/_test.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<td><%= test.id %></td>
<td><%= test.title %></td>
<td><%= test.level %></td>
<td><%= test.duration_time %></td>
<td><%= test.questions.count %></td>
<td><%= button_to t('.start'), start_test_path(test), class: 'btn btn-primary' %></td>
</tr>
1 change: 1 addition & 0 deletions app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<%= octicon 'arrow-down', class: 'text-success hide' %>
</th>
<th><%= t('.level')%></th>
<th><%= t('.duration_time')%></th>
<th><%= t('.count_question')%></th>
<th><%= t('.start')%></th>
</tr>
Expand Down
1 change: 1 addition & 0 deletions config/locales/activerecord.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ en:
level: 'Level'
category: 'Category'
active: 'Active'
duration_time: 'Duration time'
test_passage:
correct_questions: 'Correct questions'
feedback:
Expand Down
1 change: 1 addition & 0 deletions config/locales/activerecord.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ru:
level: 'Сложность'
category: 'Категория'
active: 'Активный'
duration_time: 'Время продолжительности'
test_passage:
correct_questions: 'Правильные ответы'
feedback:
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ en:
level: 'Level'
category_id: 'Category'
active: 'Active'
duration_time: 'Duration time (minutes)'
feedback:
title: 'Topic'
body: 'Feedback'
Expand Down Expand Up @@ -107,6 +108,7 @@ en:
header: 'Test list:'
gists: 'Show Gists'
badges: 'Show Badges'
duration_time: 'Duration time (minutes)'
show:
id: 'Id'
question: 'Question'
Expand Down Expand Up @@ -140,6 +142,7 @@ en:
start: 'Start'
header: 'List of available tests:'
my_badges: 'Show my Badges'
duration_time: 'Duration time (minutes)'
test:
start: 'Start'

Expand All @@ -152,6 +155,7 @@ en:
currently: "You are currently taking a test: %{number}"
next: 'Next'
create_gist: 'Create gist'
time_left: 'Time left:'
gist:
success: 'Gist was successfully created, link: %{url}'
failure: 'An error occurred while saving gist'
Expand Down
4 changes: 4 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ru:
level: 'Сложность'
category_id: 'Категория'
active: 'Активный'
duration_time: 'Время продолжительности (минут)'
feedback:
title: 'Тема отзыва'
body: 'Обратная связь'
Expand Down Expand Up @@ -75,6 +76,7 @@ ru:
header: 'Список тестов:'
gists: 'Посмотреть Гисты'
badges: 'Посмотреть Бейджи'
duration_time: 'Время продолжительности (минут)'
show:
id: 'Номер'
question: 'Вопрос'
Expand Down Expand Up @@ -108,6 +110,7 @@ ru:
start: 'Пройти тест'
header: 'Список доступных тестов:'
my_badges: 'Показать мои Бейджи'
duration_time: 'Время продолжительности (минут)'
test:
start: 'Начать'

Expand All @@ -120,6 +123,7 @@ ru:
currently: "Вы сейчас проходите тест: %{number}"
next: 'Далее'
create_gist: 'Создать gist'
time_left: 'Оставшееся время:'
gist:
success: 'Gist успешно сохранен, ссылка: %{url}'
failure: 'Во время сохранения gist произошла ошибка'
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230704222539_add_timer_to_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTimerToTests < ActiveRecord::Migration[6.1]
def change
add_column :tests, :duration_time, :integer
end
end
6 changes: 6 additions & 0 deletions db/migrate/20230706230219_add_timestamps_to_test_passages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddTimestampsToTestPassages < ActiveRecord::Migration[6.1]
def change
add_column :test_passages, :created_at, :datetime, default: -> { 'CURRENT_TIMESTAMP' }, null: false, precision: 6
add_column :test_passages, :updated_at, :datetime, default: -> { 'CURRENT_TIMESTAMP' }, null: false, precision: 6
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddDefaultDurationTimeToTests < ActiveRecord::Migration[6.1]
def up
change_column :tests, :duration_time, :integer, default: 0, null: false
end

def down
change_column :tests, :duration_time, :integer, default: nil, null: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"webpack-dev-server": "^3"
},
"packageManager": "yarn@1.22.19"
}
}
6 changes: 3 additions & 3 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.