ref: master
plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
require 'csv' class CustomFormsPluginMyprofileController < MyProfileController protect 'post_content', :profile def index @forms = CustomFormsPlugin::Form.from_profile(profile) end def new @form = CustomFormsPlugin::Form.new respond_to do |format| format.html end end def create params[:form][:profile_id] = profile.id @form = CustomFormsPlugin::Form.new(params[:form]) normalize_positions(@form) respond_to do |format| if @form.save flash[:notice] = _("Custom form %s was successfully created.") % @form.name format.html { redirect_to(:action=>'index') } else format.html { render :action => 'new' } end end end def edit @form = CustomFormsPlugin::Form.find(params[:id]) end def update @form = CustomFormsPlugin::Form.find(params[:id]) @form.attributes = params[:form] normalize_positions(@form) respond_to do |format| if @form.save flash[:notice] = _("Custom form %s was successfully updated.") % @form.name format.html { redirect_to(:action=>'index') } else session['notice'] = _('Form could not be updated') format.html { render :action => 'edit' } end end end def remove @form = CustomFormsPlugin::Form.find(params[:id]) begin @form.destroy session[:notice] = _('Form removed') rescue session[:notice] = _('Form could not be removed') end redirect_to :action => 'index' end def submissions @form = CustomFormsPlugin::Form.find(params[:id]) @sort_by = params[:sort_by] == 'author_name' ? 'author_name' : 'created_at' @submissions = @form.submissions.order(@sort_by) respond_to do |format| format.html format.csv do # CSV contains form fields, timestamp, user name and user email columns = @form.fields.count + 3 csv_content = CSV.generate_line(['Timestamp', 'Name', 'Email'] + @form.fields.map(&:name)) @submissions.each do |s| fields = [s.updated_at.strftime('%Y/%m/%d %T %Z'), s.profile.present? ? s.profile.name : s.author_name, s.profile.present? ? s.profile.email : s.author_email] @form.fields.each do |f| fields << s.answers.select{|a| a.field == f}.map{|answer| answer.to_s} end fields = fields.flatten csv_content << CSV.generate_line(fields + (columns - fields.size).times.map{""}) end send_data csv_content, :type => 'text/csv', :filename => "#{@form.name}.csv" end end end def show_submission @submission = CustomFormsPlugin::Submission.find(params[:id]) @form = @submission.form end def pending @form = CustomFormsPlugin::Form.find(params[:id]) @pendings = CustomFormsPlugin::AdmissionSurvey.from_profile(@form.profile).pending.select {|task| task.form_id == @form.id}.map {|a| {:profile => a.target, :time => a.created_at} } @sort_by = params[:sort_by] @pendings = @pendings.sort_by { |s| s[:profile].name } if @sort_by == 'user' end private def normalize_positions(form) counter = 0 form.fields.sort_by{ |f| f.position.to_i }.each do |field| field.position = counter counter += 1 end form.fields.each do |field| counter = 0 field.alternatives.sort_by{ |alt| alt.position.to_i }.each do |alt| alt.position = counter counter += 1 end end end end |