Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving exercise results #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ group :development, :test do
gem 'byebug'
gem 'pry-rails'
gem 'spring'
gem "spring-commands-rspec"
gem 'rspec-rails', '~> 3.1'
gem 'shoulda-matchers', require: false
gem 'polishgeeks-dev-tools'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ GEM
tilt (>= 1.3.3, < 2.1)
slop (3.6.0)
spring (1.4.4)
spring-commands-rspec (1.0.4)
spring (>= 0.9.1)
sprockets (3.4.1)
rack (> 1, < 3)
sprockets-rails (2.3.3)
Expand Down Expand Up @@ -363,6 +365,7 @@ DEPENDENCIES
sdoc (~> 0.4.0)
shoulda-matchers
spring
spring-commands-rspec
toastr-rails
turbolinks
twitter-bootstrap-rails
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ExerciseAnswer = function () {
var FrenchLetters = function () {

this.init = function () {
var that = this;
Expand Down
24 changes: 24 additions & 0 deletions app/assets/stylesheets/base.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
.page-wrapper {
margin: 50px;
}

.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;
background-color: rgba(162,155,155,0.5);
}

.display-modal {
width: 300px;
height: 200px;
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -150px;
background-color: white;
border-radius: 5px;
text-align: center;
z-index: 11;
}
33 changes: 32 additions & 1 deletion app/controllers/personal/exercise_sets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Personal
class ExerciseSetsController < ApplicationController
before_filter :load_context
before_filter :load_context, only: [:new, :index, :create]
before_filter :get_exercise_set, only: [:train, :check]
before_filter :load_exercise_context, only: [:train]
after_filter :load_exercise_context, only: [:check]


def new; end

Expand All @@ -15,6 +19,25 @@ def create
end
end

def train; end

def check
form_data = params.fetch(:exercise_form)
@form = AnswerEvaluationForm.new(form_data)
@check_answer ||= CheckAnswer.perform(form_data)
@template_path = @check_answer[:result] ? 'correct_answer' : 'incorrect_answer'
end

def save_progress
form_data = params.fetch(:answer_evaluation_form)
form = AnswerEvaluationForm.new(form_data)
if form.valid?
ur = UserRepetition.find(form.user_repetition_id)
ur.process_recall_result(form.quality)
end
redirect_to train_exercise_sets_path(ur.exercise_set)
end

# def edit; end

# def update; end
Expand All @@ -24,5 +47,13 @@ def create
def load_context
@context = ::Personal::ExerciseSetsContext.new(current_user, params)
end

def get_exercise_set
@exercise_set = current_user.exercise_sets.find(params[:id])
end

def load_exercise_context
@context = ::Personal::ExerciseContext.new(@exercise_set)
end
end
end
7 changes: 7 additions & 0 deletions app/forms/answer_evaluation_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AnswerEvaluationForm < BaseForm
attribute :user_repetition_id, Integer
attribute :quality, Integer

validates :user_repetition_id, presence: true
validates :quality, presence: true, numericality: {only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5}
end
2 changes: 1 addition & 1 deletion app/forms/exercise_form.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ExerciseForm < BaseForm
attribute :form_id, Integer
attribute :answer, String
attribute :user_group_id, Integer
attribute :user_repetition_id, Integer

validates :form_id, presence: true
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class User < ActiveRecord::Base
has_many :exercise_sets,
class_name: "Personal::ExerciseSet",
foreign_key: :user_id

has_many :user_repetitions,
class_name: 'UserRepetition',
foreign_key: :user_id
end
3 changes: 2 additions & 1 deletion app/services/check_answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def initialize(attributes)
def perform
{
result: answer_correct?,
correct: correct_form
correct: correct_form,
answer: attributes[:answer]
}
end

Expand Down
6 changes: 6 additions & 0 deletions app/services/exercise_context.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ExerciseContext
include Rails.application.routes.url_helpers

def initialize(params, contener)
@params = params
@contener = contener
Expand All @@ -16,6 +18,10 @@ def verb_form
@verb_form ||= Conjugation::Form.find(draw_id)
end

def submit_path
check_random_exercises_path
end

private

attr_reader :params, :contener
Expand Down
37 changes: 37 additions & 0 deletions app/services/personal/exercise_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Personal
class ExerciseContext
include Rails.application.routes.url_helpers

def initialize(exercise_set)
@exercise_set = exercise_set
end

def form
@form ||= ::ExerciseForm.new(user_repetition_id: user_repetition.id, form_id: verb_form.id)
end

def presenter
@presenter ||= ::ExercisePresenter.new(verb_form)
end

def verb_form
@verb_form ||= user_repetition.form
end

def submit_path
check_exercise_sets_path(exercise_set)
end

def remote?
true
end

private

attr_reader :exercise_set

def user_repetition
@user_repetition ||= exercise_set.user_repetitions.order(:next_repetition, number_repetitions: :asc).first
end
end
end
7 changes: 6 additions & 1 deletion app/services/personal/exercise_set_saver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ def initialize(user, settings)
end

def perform
user.exercise_sets.create!(exercise_set_attributes)
ActiveRecord::Base.transaction do
exercise_set = user.exercise_sets.create!(exercise_set_attributes)
settings_retriever.chosen_forms_ids.each do |form_id|
user.user_repetitions.create!(form_id: form_id, exercise_set: exercise_set)
end
end
end

private
Expand Down
3 changes: 3 additions & 0 deletions app/views/personal/exercise_sets/check.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('.x-overlay').removeClass( "hide" );
$('.x-answer-evaluation').removeClass( "hide" );
$('.x-answer-evaluation').html('<%= j(render partial: "personal/exercise_sets/elements/answer_evaluation", locals: { form: @form }) %>');
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= render partial: "personal/exercise_sets/elements/#{@template_path}" %>

Oceń swoją odpowiedź
<br>
<%= form_for(@form, url: save_progress_exercise_sets_path) do |f| %>
<%= f.hidden_field :user_repetition_id %>
<br>
<div>
<% (1..5).each do |i| %>
<%= f.label :quality, i %>
<%= f.radio_button :quality, i %>
<% end %>
</div>
<%= f.submit "Zapisz"%>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Brawo! Poprawna odpowiedź: <%= @check_answer[:answer] %>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Zestaw: <strong><%= exercise_set.name %></strong>
Form do powtórzenia: xxx
<!-- TODO : forms to repeat count -->
<%= button_tag "Ćwicz" %>
<%= link_to "Ćwicz", train_exercise_sets_path(exercise_set) %>
<%= link_to "Rozwiń", "#", class: "x-show-details", data: { group: exercise_set.id } %>
<%= link_to "Edytuj", "#" %>
<%= link_to "Staty", "#" %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Niestety - błędna odpowiedź.
Twoja: <%= @check_answer[:answer] %>
Poprawna: <%= @check_answer[:correct] %>
7 changes: 7 additions & 0 deletions app/views/personal/exercise_sets/train.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="hide overlay x-overlay"></div>
<%= render partial: "shared/exercise_form", locals: { context: @context } %>
<div class="display-modal hide x-answer-evaluation"></div>

<script>
$(document).on("page:change", (new FrenchLetters).init());
</script>
32 changes: 2 additions & 30 deletions app/views/random_exercise/random_exercises/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
<div>
<% if flash[:notice] %>
<p class="flash-notice"><%= flash[:notice] %></p>
<% end %>
<% if flash[:error] %>
<p class="flash-error"><%= flash[:error] %></p>
<% end %>
<%= form_for(@context.form, url: check_random_exercises_path) do |f| %>
<%= f.render_errors %>
<%= f.hidden_field :form_id %>
<%= f.hidden_field :user_group_id %>
<br>
<ul>
<li>czasownik: <b><%= @context.presenter.verb %></b></li>
<li>czas: <b><%= @context.presenter.tense %></b></li>
<li>osoba: <b><%= @context.presenter.person %></b></li>
</ul>
<div>
<%= f.label :answer %>
<%= f.text_field :answer, class: "x-answer" %>
</div>

<div class="x-french-letter">
<%= render partial: "shared/letter_button", collection: @context.presenter.french_letters, as: :letter %>
</div>
<%= f.submit "Sprawdż!" %>
<% end %>
</div>

<%= render partial: "shared/exercise_form", locals: { context: @context } %>

<script>
$(document).on("page:change", (new ExerciseAnswer).init());
$(document).on("page:change", (new FrenchLetters).init());
</script>
28 changes: 28 additions & 0 deletions app/views/shared/_exercise_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div>
<% if flash[:notice] %>
<p class="flash-notice"><%= flash[:notice] %></p>
<% end %>
<% if flash[:error] %>
<p class="flash-error"><%= flash[:error] %></p>
<% end %>
<%= form_for(context.form, url: context.submit_path, remote: context.try(:remote?)) do |f| %>
<%= f.render_errors %>
<%= f.hidden_field :form_id %>
<%= f.hidden_field :user_repetition_id %>
<br>
<ul>
<li>czasownik: <b><%= context.presenter.verb %></b></li>
<li>czas: <b><%= context.presenter.tense %></b></li>
<li>osoba: <b><%= context.presenter.person %></b></li>
</ul>
<div>
<%= f.label :answer %>
<%= f.text_field :answer, class: "x-answer" %>
</div>

<div class="x-french-letter">
<%= render partial: "shared/letter_button", collection: context.presenter.french_letters, as: :letter %>
</div>
<%= f.submit "Sprawdż!", class: "x-submit" %>
<% end %>
</div>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
scope module: :personal do
resource :exercise_sets, path: 'zestawy-cwiczeniowe' do
get '/', action: :index, as: :index
get '/cwicz/:id', action: :train, as: :train
post '/cwicz/:id', action: :check, as: :check
post '/zapisz-postepy', action: :save_progress, as: :save_progress
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddDefaultValuesToUserRepetitions < ActiveRecord::Migration
def change
change_column :user_repetitions, :easiness_factor, :decimal, default: 2.5
change_column :user_repetitions, :number_repetitions, :integer, default: 0
change_column :user_repetitions, :next_repetition, :datetime, default: Date.today
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160516193409) do
ActiveRecord::Schema.define(version: 20160522182427) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -38,10 +38,10 @@
add_index "sessions", ["updated_at"], name: "index_sessions_on_updated_at", using: :btree

create_table "user_repetitions", force: :cascade do |t|
t.decimal "easiness_factor"
t.integer "number_repetitions"
t.decimal "easiness_factor", default: 2.5
t.integer "number_repetitions", default: 0
t.integer "quality_of_last_recall"
t.datetime "next_repetition"
t.datetime "next_repetition", default: '2016-05-22 00:00:00'
t.integer "repetition_interval"
t.datetime "last_studied"
t.integer "user_id"
Expand Down
2 changes: 1 addition & 1 deletion lib/super_memo/sm2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def process_recall_result(quality_of_recall)
self.easiness_factor = calculate_easiness_factor(quality_of_recall)
self.repetition_interval = calculate_interval(quality_of_recall)
self.number_repetitions += 1
self.save
end

self.next_repetition = Date.today + repetition_interval
self.last_studied = Date.today
self.quality_of_last_recall = quality_of_recall
self.save
end

def scheduled_to_recall?
Expand Down
Loading