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

Invalidação dos documentos assinados com QR Code, correção da tela de expirados, override do tipo de assinatura #512

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
8 changes: 5 additions & 3 deletions app/controllers/concerns/shared_pdf_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def render_course_classes_summary_pdf(course_class)
)
end

def render_enrollments_academic_transcript_pdf(enrollment, filename = "transcript.pdf")
def render_enrollments_academic_transcript_pdf(enrollment, filename = "transcript.pdf", signature_override = nil)
class_enrollments = enrollment.class_enrollments
.where(situation: ClassEnrollment::APPROVED)
.joins(:course_class)
Expand All @@ -51,11 +51,12 @@ def render_enrollments_academic_transcript_pdf(enrollment, filename = "transcrip
enrollment: enrollment,
class_enrollments: class_enrollments,
accomplished_phases: accomplished_phases,
signature_override: signature_override
}
)
end

def render_enrollments_grades_report_pdf(enrollment, filename = "grades_report.pdf")
def render_enrollments_grades_report_pdf(enrollment, filename = "grades_report.pdf", signature_override = nil)
class_enrollments = enrollment.class_enrollments
.where(situation: ClassEnrollment::APPROVED)
.joins(:course_class)
Expand All @@ -71,7 +72,8 @@ def render_enrollments_grades_report_pdf(enrollment, filename = "grades_report.p
enrollment: enrollment,
class_enrollments: class_enrollments,
accomplished_phases: accomplished_phases,
deferrals: deferrals
deferrals: deferrals,
signature_override: signature_override
}
)
end
Expand Down
24 changes: 19 additions & 5 deletions app/controllers/enrollments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ class EnrollmentsController < ApplicationController
:accomplishments, :deferrals, :phase_due_dates
]
config.update.columns = columns - [:phase_due_dates]
config.show.columns = columns - [:accomplishments]
config.show.columns = columns - [:accomplishments]
config.show.columns.add :academic_transcript, :grades_report

config.actions.exclude :deleted_records
end
Expand Down Expand Up @@ -213,28 +214,41 @@ def to_pdf
end
end

def academic_transcript_pdf
def override_signature_transcript_pdf
academic_transcript_pdf(params[:signature_type])
end

def override_signature_grades_report_pdf
grades_report_pdf(params[:signature_type])
end

def academic_transcript_pdf(signature_type = nil)
enrollment = Enrollment.find(params[:id])

if cannot? :academic_transcript_pdf, enrollment
raise CanCan::AccessDenied.new
end

respond_to do |format|
format.pdf do
title = I18n.t("pdf_content.enrollment.academic_transcript.title")
student = enrollment.student.name
filename = "#{title} - #{student}.pdf"
send_data render_enrollments_academic_transcript_pdf(enrollment, filename),
send_data render_enrollments_academic_transcript_pdf(enrollment, filename, signature_type),
filename: filename,
type: "application/pdf"
end
end
end

def grades_report_pdf
def grades_report_pdf(signature_type = nil)
enrollment = Enrollment.find(params[:id])
respond_to do |format|
format.pdf do
title = I18n.t("pdf_content.enrollment.grades_report.title")
student = enrollment.student.name
filename = "#{title} - #{student}.pdf"
send_data render_enrollments_grades_report_pdf(enrollment, filename),
send_data render_enrollments_grades_report_pdf(enrollment, filename, signature_type),
filename: filename,
type: "application/pdf"
end
Expand Down
39 changes: 28 additions & 11 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class ReportsController < ApplicationController
before_action :set_report, only: [:download, :download_by_identifier]
before_action :set_report, only: [:download, :download_by_identifier, :invalidate]
before_action :check_downloadable, only: [:download, :download_by_identifier]
authorize_resource

Expand All @@ -10,22 +10,33 @@ class ReportsController < ApplicationController
skip_before_action :authenticate_user!, only: :download_by_identifier

active_scaffold :report do |config|
config.list.columns = [:user, :file_name, :identifier, :created_at, :expires_at]
config.show.columns = [:user, :file_name, :identifier, :created_at, :expires_at]
config.list.columns = [:user, :file_name, :identifier, :created_at, :expires_at_or_invalid]
config.show.columns = [:user, :file_name, :identifier, :created_at, :expires_at, :invalidated_by, :invalidated_at]
config.update.columns = [:expires_at]
config.columns = config.list.columns
config.columns[:user].clear_link
config.columns[:expires_at_or_invalid].label = I18n.t("activerecord.attributes.report.expires_at")
config.actions.exclude :create, :delete
config.action_links.add "download",
label: "
<i title='#{I18n.t("active_scaffold.download_link")}'
class='fa fa-download'></i>
".html_safe,
page: true,
type: :member,
parameters: { format: :pdf },
method: :get,
html_options: { target: "_blank" },
ignore_method: :cant_download?
".html_safe, page: true,
type: :member,
parameters: { format: :pdf },
method: :get,
html_options: { target: "_blank" },
ignore_method: :cant_download?

config.action_links.add "invalidate",
label: "
<i title='#{I18n.t("active_scaffold.invalidate_link")}'
class='fa fa-times'></i>
".html_safe, page: true,
type: :member,
method: :put,
confirm: "Tem certeza que deseja invalidar este documento?",
ignore_method: :cant_download?
end

def download
Expand All @@ -36,14 +47,20 @@ def download_by_identifier
send_data(@report.carrierwave_file.read, filename: @report.carrierwave_file.original_filename, disposition: :inline)
end

def invalidate
@report.invalidate!(user: current_user)

redirect_to reports_path, notice: "Documento invalidado com sucesso."
end

private
def set_report
@report = params[:id] ? Report.find(params[:id]) : Report.find_by_identifier(params[:identifier])
raise ActionController::RoutingError.new("Este documento não foi encontrado.") if @report.nil?
end

def check_downloadable
raise ActionController::RoutingError.new("Este documento expirou.") if cant_download?(@report)
render("reports/expired_or_invalid_report") if cant_download?(@report)
end

def cant_download?(record)
Expand Down
16 changes: 13 additions & 3 deletions app/helpers/enrollments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def delayed_phase_search_column(record, input_name)
:record, :phases,
options_for_select([["Alguma", "all"]] +
Phase.where(active: true).map { |phase| [phase.name, phase.id] }
),
),
{ include_blank: as_(:_select_) },
select_html_options
) +
Expand All @@ -120,14 +120,14 @@ def accomplishments_search_column(record, input_name)
select_html_options = { name: "search[accomplishments][phase]" }
day_html_options = { name: "search[accomplishments][day]" }
month_html_options = { name: "search[accomplishments][month]" }
year_html_options = { name: "search[accomplishments][year]"}
year_html_options = { name: "search[accomplishments][year]" }

(
select(
:record, :phases,
options_for_select([["Todas", "all"]] +
Phase.all.map { |phase| [phase.name, phase.id] }
),
),
{ include_blank: as_(:_select_) },
select_html_options
) +
Expand Down Expand Up @@ -339,4 +339,14 @@ def options_for_association_conditions(association, record)
def permit_rs_browse_params
[:page, :update, :utf8]
end

def academic_transcript_show_column(record, column)
render(partial: "enrollments/show_academic_transcript_signature_override_links",
locals: { record: record })
end

def grades_report_show_column(record, column)
render(partial: "enrollments/show_grades_report_signature_override_links",
locals: { record: record })
end
end
27 changes: 8 additions & 19 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,6 @@ def new_document(name, title, options = {}, &block)
datetime_footer(pdf)
end
end
if options[:watermark]
pdf.create_stamp("watermark") do
pdf.rotate(60, origin: [0, 0]) do
pdf.fill_color "993333"
pdf.font("FreeMono", size: 22) do
pdf.draw_text(
I18n.t("pdf_content.professor_watermark"), at: [0, 0]
)
end
pdf.fill_color "000000"
end
end
pdf.repeat(:all, dynamic: true) do
pdf.stamp_at "watermark", [80, 0]
end
end
end

if pdf_config.qr_code_signature && !pdf_config.preview
Expand Down Expand Up @@ -508,9 +492,14 @@ def qrcode_signature(pdf, options = {})

def setup_pdf_config(pdf_type, options)
pdf_type_property = :"use_at_#{pdf_type}"
options[:pdf_config] ||
ReportConfiguration.where(pdf_type_property => true).order(order: :desc).first ||
ReportConfiguration.new
config = options[:pdf_config] ||
ReportConfiguration.where(pdf_type_property => true).order(order: :desc).first ||
ReportConfiguration.new
if options[:signature_override].present?
config.signature_type = options[:signature_override]
end

config
end

def generate_qr_code_key
Expand Down
5 changes: 4 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ def initialize_students(user, roles)
can :manage, Ability::STUDENT_MODELS
can :update_all_fields, Student
can :read_all_fields, Student
can :generate_report_without_watermark, Enrollment
can :generate_report_without_qrcode, Enrollment
can :invite, User
end
if roles[Role::ROLE_PROFESSOR]
can :read, Ability::STUDENT_MODELS
can :read_all_fields, Student
can :photo, Student
cannot :academic_transcript_pdf, Enrollment do |enrollment|
enrollment.dismissal&.dismissal_reason&.thesis_judgement != DismissalReason::APPROVED
end
end
if roles[Role::ROLE_SUPORTE]
can [:read, :update, :update_only_photo], (Student)
Expand Down
4 changes: 4 additions & 0 deletions app/models/course_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def semester_label
"#{year}.#{semester}"
end

def label_for_email_subject
"#{self.course.name} - #{year}/#{semester}"
end

def label_with_course
name_l = self.name.blank? ? "" : " (#{self.name})"
"#{self.course.name}#{name_l} - #{year}/#{semester}"
Expand Down
15 changes: 15 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@

class Report < ApplicationRecord
belongs_to :user, foreign_key: "generated_by_id"
belongs_to :invalidated_by, foreign_key: "invalidated_by_id", class_name: "User", optional: true
belongs_to :carrierwave_file, foreign_key: "carrierwave_file_id", class_name: "CarrierWave::Storage::ActiveRecord::ActiveRecordFile", optional: true

def to_label
"#{self.user.name} - #{I18n.l(self.created_at, format: '%d/%m/%Y %H:%M')}"
end

def invalidate!(user:)
carrierwave_file = self.carrierwave_file
self.update!(carrierwave_file_id: nil, invalidated_by: user, invalidated_at: Time.now)
carrierwave_file.delete
end

def expires_at_or_invalid
if self.invalidated_at.present?
I18n.t("activerecord.attributes.report.invalidated")
else
self.expires_at.present? ? I18n.l(self.expires_at, format: "%d/%m/%Y") : nil
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div style="display: flex; gap: 20px;">
<% if can?(:generate_report_without_qrcode, @record) %>
<%= link_to 'Sem Assinatura', override_signature_transcript_pdf_enrollment_path(id: @record.id, signature_type: :no_signature), target: "_blank" %>
<%= link_to 'Assinatura Manual', override_signature_transcript_pdf_enrollment_path(id: @record.id, signature_type: :manual), target: "_blank" %>
<% end %>
<%= link_to 'Assinatura por QR Code', override_signature_transcript_pdf_enrollment_path(id: @record.id, signature_type: :qr_code), target: "_blank" %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div style="display: flex; gap: 20px;">
<% if can?(:generate_report_without_qrcode, @record) %>
<%= link_to 'Sem Assinatura', override_signature_grades_report_pdf_enrollment_path(id: @record.id, signature_type: :no_signature, transcript: true), target: "_blank" %>
<%= link_to 'Assinatura Manual', override_signature_grades_report_pdf_enrollment_path(id: @record.id, signature_type: :manual, transcript: true), target: "_blank" %>
<% end %>
<%= link_to 'Assinatura por QR Code', override_signature_grades_report_pdf_enrollment_path(id: @record.id, signature_type: :qr_code, transcript: true), target: "_blank" %>
</div>
6 changes: 2 additions & 4 deletions app/views/enrollments/academic_transcript_pdf.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ require "prawn/measurement_extensions"
new_document(
@filename,
I18n.t("pdf_content.enrollment.header.title"),
watermark: cannot?(
:generate_report_without_watermark, @enrollment
),
pdf_type: :transcript
pdf_type: :transcript,
signature_override: can?(:generate_report_without_qrcode, @enrollment) ? @signature_override : :qr_code
) do |pdf|
enrollment_student_header(pdf, enrollment: @enrollment)

Expand Down
8 changes: 2 additions & 6 deletions app/views/enrollments/grades_report_pdf.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
new_document(
@filename,
I18n.t("pdf_content.enrollment.grades_report.title"),
watermark: (
current_user.nil? ? false : cannot?(
:generate_report_without_watermark, @enrollment
)
),
pdf_type: :grades_report
pdf_type: :grades_report,
signature_override: can?(:generate_report_without_qrcode, @enrollment) ? @signature_override : :qr_code
) do |pdf|
enrollment_student_header(pdf, enrollment: @enrollment)

Expand Down
4 changes: 4 additions & 0 deletions app/views/reports/expired_or_invalid_report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div style="max-width: 90%; width: 310px; margin: 10% auto 0 auto; padding: 30px; text-align: center; border: 1px solid #e0e0e0; border-radius: 8px; background-color: #fff; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);">
<h1 style="font-size: 24px; font-weight: bold; color: #dc3545; margin-bottom: 20px;">Erro</h1>
<p style="font-size: 16px; color: #555;">Documento invalidado ou expirado</p>
</div>
6 changes: 3 additions & 3 deletions config/locales/class_enrollment.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pt-BR:
notifications:
class_enrollment:
email_to_student:
subject: "Sua inscrição em <%= var(:record).course_class.label_with_course %> foi atualizada"
subject: "Sua inscrição em <%= var(:record).course_class.label_for_email_subject %> foi atualizada"
email_to_advisor:
subject: "Inscrição em <%= var(:record).course_class.label_with_course %> de <%= var(:record).enrollment.to_label %> foi atualizada"
subject: "Inscrição em <%= var(:record).course_class.label_for_email_subject %> de <%= var(:record).enrollment.to_label %> foi atualizada"
email_to_professor:
subject: "Alteração da nota de <%= var(:record).enrollment.to_label %> na turma <%= var(:record).course_class.label_with_course %>"
subject: "Alteração da nota de <%= var(:record).enrollment.to_label %> em <%= var(:record).course_class.label_for_email_subject %>"
4 changes: 2 additions & 2 deletions config/locales/class_enrollment_request.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ pt-BR:
notifications:
class_enrollment_request:
email_to_student:
subject: "Seu pedido de inscrição em <%= var(:record).course_class.label_with_course %> foi efetivado"
subject: "Seu pedido de inscrição em <%= var(:record).course_class.label_for_email_subject %> foi efetivado"
removal_email_to_student:
subject: "Seu pedido de remoção da disciplina <%= var(:record).course_class.label_with_course %> foi efetivado"
subject: "Seu pedido de remoção da disciplina <%= var(:record).course_class.label_for_email_subject %> foi efetivado"
2 changes: 1 addition & 1 deletion config/locales/course_class.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pt-BR:
notifications:
course_class:
email_to_professor:
subject: "Alteração de notas da turma <%= var(:record).label_with_course %>"
subject: "Alteração de notas em <%= var(:record).label_for_email_subject %>"

pdf_content:
course_class:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/enrollment.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pt-BR:
thesis_defense_committee_professors: "Banca Avaliadora"
accomplishment_conclusion_date_not_given: "---------/----"
obs_to_academic_transcript: "Observação para o Histórico"
grades_report: "Boletim"
academic_transcript: "Histórico"

errors:
models:
Expand Down
Loading
Loading