From 1f63709887031750259a961222b23e49fc63251b Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sat, 9 Feb 2013 03:32:26 -0700 Subject: [PATCH] Reporting door access logs on each card's page; also card access stats for last 7 days --- app/controllers/cards_controller.rb | 19 +++++++++++++++++-- app/views/cards/index.html.erb | 8 +++++++- app/views/cards/show.html.erb | 15 +++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/controllers/cards_controller.rb b/app/controllers/cards_controller.rb index 84b92bc..e270692 100644 --- a/app/controllers/cards_controller.rb +++ b/app/controllers/cards_controller.rb @@ -9,6 +9,19 @@ class CardsController < ApplicationController #authorize! :read, @cards @cards = @cards.sort_by{|e| e[:id]} + if can? :read, DoorLog then + most_active_count = 0 + @most_active_card = nil + @cards.each do |card| + card_num_R = card.card_number.to_i(16)%32767 + card[:accesses_this_week] = DoorLog.where('key = "R" AND data =? AND created_at > ?', card_num_R, DateTime.now - 7.days).order("created_at DESC").count + if(card[:accesses_this_week] > most_active_count) then + most_active_count = card[:accesses_this_week] + @most_active_card = card + end + end + end + respond_to do |format| format.html # index.html.erb format.json { render :json => @cards } @@ -18,8 +31,10 @@ class CardsController < ApplicationController # GET /cards/1 # GET /cards/1.json def show - #@card = Card.find(params[:id]) - + if can? :read, DoorLog then + card_num_R = @card.card_number.to_i(16)%32767 + @door_logs = DoorLog.where('key = "R" AND data =?', card_num_R).order("created_at DESC") + end respond_to do |format| format.html # show.html.erb format.json { render :json => @card } diff --git a/app/views/cards/index.html.erb b/app/views/cards/index.html.erb index 1e02060..59779f6 100644 --- a/app/views/cards/index.html.erb +++ b/app/views/cards/index.html.erb @@ -2,6 +2,10 @@ <%= link_to 'New Card', new_card_path if can? :create, Card %> <%= link_to 'Upload all cards', upload_all_path if can? :upload_all, Card %> +

+Most Active Card Last 7 Days: <%= @most_active_card.name %> (<%= @most_active_card.accesses_this_week %> times) +

+ @@ -14,6 +18,7 @@ + @@ -22,11 +27,12 @@ <% if !@cards.blank? %> <% @cards.each do |card| %> - + + diff --git a/app/views/cards/show.html.erb b/app/views/cards/show.html.erb index f77ec73..5d28dea 100644 --- a/app/views/cards/show.html.erb +++ b/app/views/cards/show.html.erb @@ -1,6 +1,6 @@

User: - <%= @card.user.name unless @card.user.blank? %> + <%= link_to @card.user.name, @card.user unless @card.user.blank? %>

@@ -20,9 +20,20 @@

Card Permissions: - <%= @card.card_permissions %> + <%= if @card.card_permissions == 1 then "Enabled" else "Disabled" end %>

+<% if can? :read, DoorLog %> +

+ Access attempts: +

+

+<% end %> + <%= link_to 'Upload to Door', upload_path(@card) if can? :upload, @card %> <% if can? :update, @card then %><%= link_to 'Edit', edit_card_path(@card) %> |<% end %> <%= link_to 'Back', cards_path %>
DB ID Card # Access?Accesses Last 7 Days
<%= card.user.name %><%= link_to card.user.name, card %> <%= card.name %> <%= card.id %> <%= card.card_number %> <%= if card.card_permissions == 1 then "Access" end %><%= card.accesses_this_week unless card.accesses_this_week < 1 %> <%= link_to 'Upload', upload_path(card) if can? :upload, card %> <%= link_to 'Edit', edit_card_path(card) if can? :update, card %> <%= link_to 'Destroy', card, :confirm => 'Are you sure? WARNING: THIS DOES NOT REMOVE THE CARD FROM THE DOOR SYSTEM! DISABLE AND UPLOAD IT FIRST.', :method => :delete if can? :destroy, card %>