From a294023f84b00fcd8fdc03fb7f807d4fe4b1c301 Mon Sep 17 00:00:00 2001
From: Matheus Agio Nerone <man13@inf.ufpr.br>
Date: Tue, 20 Oct 2015 11:22:39 -0200
Subject: [PATCH] add basic year filter

Signed-off-by: Matheus Agio Nerone <man13@inf.ufpr.br>
---
 app/assets/javascripts/application/search.js  | 20 +++++++++++++++++++
 app/controllers/search_controller.rb          |  3 ++-
 .../orient_db/learning_object_repository.rb   | 13 +++++++++++-
 app/views/search/index.html.erb               |  5 +++--
 4 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/app/assets/javascripts/application/search.js b/app/assets/javascripts/application/search.js
index b32799b0..1c171451 100644
--- a/app/assets/javascripts/application/search.js
+++ b/app/assets/javascripts/application/search.js
@@ -183,9 +183,29 @@ var search = function() {
       watchFilterSubject();
       watchCheckbox('type', 'filter-type');
       watchCheckbox('school_year', 'filter-school-year');
+      watchFilterYear();
       watchFilterRegex();
     }
 
+    function watchFilterYear(){
+      $('#filter-year button').on('click', function(event){
+        event.preventDefault();
+        value = $('#filter-year input[name=year]:checked').val();
+        switch (value) {
+          case "single":
+            params.add('year', $('#filter-year input[name=singleYear]').val());
+            break;
+          case "range":
+            start_year = $('#filter-year input[name=startYear]').val();
+            end_year = $('#filter-year input[name=endYear]').val();
+            params.add('year', start_year + '-' + end_year);
+            break;
+        }
+        console.log(params.get());
+        filtersFetchInterval();
+      });
+    }
+
     function watchFilterRegex() {
       $('.regex-submit').on('click', function (event) {
         event.preventDefault();
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index 7ba64007..2651714a 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -10,6 +10,7 @@ class SearchController < ApplicationController
     params[:subject]= params[:subject].split(', ')
     params[:type] = params[:type].split(', ')
     params[:school_year] = params[:school_year].split(', ')
+    params[:year] = params[:year].split('-').take(2)
     page=1
     if (params[:page])
       page = params[:page].to_i
@@ -40,7 +41,7 @@ class SearchController < ApplicationController
 
     # build objects from one page only
     @objects = build_page_result(objectsFound, page, @pagination_limit)
-
+    
     @result = Kaminari.paginate_array(objectsFound).page(page).per(@pagination_limit)
 
     render partial: 'search/fetch'
diff --git a/app/repositories/orient_db/learning_object_repository.rb b/app/repositories/orient_db/learning_object_repository.rb
index d126c700..7cd36c95 100644
--- a/app/repositories/orient_db/learning_object_repository.rb
+++ b/app/repositories/orient_db/learning_object_repository.rb
@@ -150,10 +150,10 @@ module OrientDb
 
       query = "SELECT EXPAND(rid) FROM index:learningobject_search WHERE key LUCENE '#{qry}'"
       query = "SELECT * FROM (" + query + ") WHERE " + fetch_types(params[:type]) unless params[:type].blank?
+      query = "SELECT * FROM (" + query + ") WHERE " + fetch_year(params[:year]) unless params[:year].blank?
       query = "SELECT * FROM (" + query + ") ORDER BY #{order}" unless order.nil?
       query = "SELECT @rid.asString(), last_modified FROM (" + query + ")"
       learning_objects_hash = connection.query query, limit: 10000
-
       # return only rids with their modification time
       learning_objects_hash.map do |e|
         {'@rid': e['rid'], 'last_modified': e['last_modified']}
@@ -174,6 +174,17 @@ module OrientDb
       hash
     end
 
+    def fetch_year(years)
+      if years.size() == 1
+        year = years[0]
+        qry = "created_at.format('yyyy') = '#{year}'"
+      elsif years.size() == 2
+        begin_year = years[0]
+        end_year = years[1]
+        qry = "created_at.format('yyyy') >= '#{begin_year}' AND created_at.format('yyyy') <= '#{end_year}'"
+      end
+    end
+
     def fetch_types(types)
       qry = ''
       types.each do |type|
diff --git a/app/views/search/index.html.erb b/app/views/search/index.html.erb
index 81f29aea..8a30785e 100644
--- a/app/views/search/index.html.erb
+++ b/app/views/search/index.html.erb
@@ -210,13 +210,14 @@
       <h3 class="dropdown">Ano de Publicação<span class="caret caret-align-right"/></h3>
 
       <div class="dropdown-element">
-        <form>
+        <form id="filter-year">
           <input type="radio" name="year" value="single" checked>Ano:<br/>
           <input type="text" name="singleYear" size="8">
           <br/>
           <input type="radio" name="year" value="range">Intervalo:<br/>
           <input type="text" name="startYear" size="8"> - <input type="text" name="endYear" size="8">
-          <a onclick="getYearFilter()">Ano</a>
+          <br/><br/>
+          <button type="submit" class="btn btn-default">Pesquisar</button>
         </form>
       </div>
       <h3 class="dropdown">Pesquisa Avançada<span class="caret caret-align-right"/></h3>
-- 
GitLab