Changeset 739

Show
Ignore:
Timestamp:
01/17/08 15:57:43 (1 year ago)
Author:
markku
Message:

Closes #523.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/customer_interface_controller.rb

    r725 r739  
    275275  end 
    276276 
     277  # Method: view_form 
     278  # ================= 
     279  # View a filled form. 
     280  # 
     281  def view_form 
     282    form = Form.find(params[:id]) 
     283 
     284    unless form.authorized_to_read? 
     285      info('Unauthorized to view form ID=%d' % form.id) 
     286      redirect_to(:action => 'index') 
     287      return 
     288    end 
     289 
     290    @title = _('Filled-out form %s (%s)', form.to_s, form.form_type.name) 
     291    @show_login_name_link = session[:user].instance_of?(Customer) 
     292 
     293    # Place the field values to their field groups. 
     294    @field_values_in_groups = [] 
     295    @groupless_field_values = [] 
     296    form.form_field_values.each do |field_value| 
     297      # Check that customer has permission to view the field value. 
     298      next unless field_value.form_field.show_to_customers 
     299 
     300      group = field_value.form_field.form_field_group 
     301      if group 
     302        elem = nil 
     303        @field_values_in_groups.each { |g| elem = g if g[:group].id == group.id} 
     304        if elem 
     305          elem[:field_values].push(field_value) 
     306        else 
     307          @field_values_in_groups.push({ :group => group, :field_values => [field_value] }) 
     308        end 
     309      else 
     310        @groupless_field_values.push(field_value) 
     311      end 
     312    end 
     313    # Sort field groups and field values 
     314    @field_values_in_groups.sort! { |a,b| a[:group].place <=> b[:group].place } 
     315    @field_values_in_groups.each { |g| g[:field_values].sort! { |a,b| a.form_field.place <=> b.form_field.place } } 
     316    @groupless_field_values.sort! { |a,b| a.form_field.place <=> b.form_field.place } 
     317  end 
     318 
     319  # Method: view_form_image_field 
     320  # ============================= 
     321  # Displays a form image field. 
     322  # 
     323  require_key_for :view_form_image_field, 'customer_interface/view_form' 
     324  def view_form_image_field 
     325    @value = FormFieldImageValue.find(params[:id]) 
     326    render :layout => false 
     327  end 
     328 
    277329  protected 
    278330 
  • trunk/app/controllers/form_controller.rb

    r705 r739  
    406406  end 
    407407 
     408  # Method: get_image_field 
     409  # ======================= 
     410  # Displays an image field from a form. 
     411  # 
     412  # Can be requested by both User and Customer. 
     413  # 
     414  require_key_for :get_image_field, 'customer_interface/view_form' 
     415  def get_image_field 
     416    field = FormFieldImageValue.find(params[:id]) 
     417    authorized = field.form.authorized_to_read? 
     418    authorized = field.form_field.show_to_customers if UserAccount.current_user.instance_of?(Customer) and authorized 
     419    if authorized 
     420      send_data(field.get_filled_image.data) 
     421    else 
     422      error('Unauthorized to display image field from the form ID=%d' % form.id) 
     423      render :text => '' 
     424    end 
     425  end 
     426 
    408427  # Function: download 
    409428  # ================== 
  • trunk/app/helpers/customer_interface_helper.rb

    r712 r739  
    7070    content = "#{form.to_s} (#{form.form_type.name})" 
    7171    content = _('Form') + ' ' + content unless no_text 
     72 
     73    return link_to(content, { :controller => 'customer_interface', :action => 'view_form', :id => form.id }, { :class => 'form', :title => _('View form %s', form.to_s) }) if form.authorized_to_read? 
    7274    return span_tag('form', content) 
    7375  end 
  • trunk/app/models/customer.rb

    r713 r739  
    3636                 'customer_interface/edit_my_information', 
    3737                 'customer_interface/change_password', 
    38                  'customer_interface/comment_service_request' 
     38                 'customer_interface/comment_service_request', 
     39                 'customer_interface/view_form' 
    3940                ] 
    4041 
  • trunk/app/models/form.rb

    r592 r739  
    183183  end 
    184184 
     185  # See superclasses for description 
     186  def authorized_to_read? 
     187    return super unless UserAccount.current_user.instance_of?(Customer) 
     188 
     189    # Customer can read a form in her asset if the form has fields that 
     190    # are configured to be shown to customers. 
     191    return false unless self.asset.authorized_to_read? 
     192    self.form_field_values.each { |field| 
     193      return true if field.form_field.show_to_customers 
     194    } 
     195    return false 
     196  end 
     197 
    185198protected 
    186199 
  • trunk/app/views/form_type/create_step4.rhtml

    r644 r739  
    110110          <%= _("Can't be empty") %> 
    111111        </th> 
     112        <th> 
     113          <%= _('Show to customers') %> 
     114        </th> 
    112115      </tr> 
    113116      <% for @field in @fields -%> 
     
    142145          <td><%= check_box("field[]", "validate_presence") %></td> 
    143146        <% end -%> 
     147        <% if @field.type_code == FormField::AUTOMATIC            -%> 
     148          <td></td> 
     149        <% else-%> 
     150          <td><%= check_box("field[]", "show_to_customers") %></td> 
     151        <% end -%> 
    144152      </tr> 
    145153    <% end -%> 
  • trunk/app/views/form_type/edit.rhtml

    r511 r739  
    2929        <%= _("Can't be empty") %> 
    3030      </th> 
     31      <th> 
     32        <%= _('Show to customers') %> 
     33      </th> 
    3134    </tr> 
    3235    <% for @field in @fields -%> 
     
    4548        </td> 
    4649      <% end -%> 
     50      <td> 
     51        <%= check_box("field[]", "show_to_customers") %> 
     52      </td> 
    4753    </tr> 
    4854    <% end -%> 
  • trunk/doc/manual/latex/tex/configuration_section.tex

    r725 r739  
    193193\item \textbf{Show in form list}: Checking this defines the field to shown in the form list 
    194194\item \textbf{Can't be empty}: Checking this defines the field to be a mandatory field, which can't be left empty 
     195\item \textbf{Show to customers}: This option enables you show the content of the form field to your customers. Checking this option defines the form field to be shown to your customers in the Customer interface --- See the section~\ref{sec:customer_interface} for the Customer interface. 
    195196\end{itemize} 
    196197The 'Preview' link enables you to preview the filling form. To finish the configuration select the 'Finish' button. 
  • trunk/doc/manual/latex/tex/customer_interface.tex

    r725 r739  
    55\item View their assets 
    66\item View timeline that shows activity in their assets 
     7\item View filled forms in their assets 
    78\item Submit service requests 
    89\item View and follow their open service request 
     
    1920 
    2021\subsection{Functions in the Customer interface} 
     22\subsubsection{View a filled form} 
     23Customers can view filled forms by selecting them from the timeline. 
     24 
     25Each field of each form type can be configured --- in the Form type menu of the Configuration section \ref{subsec:form_types} --- to be viewable for customers. 
    2126\subsubsection{Submit a service request} 
    2227Customers can submit service requests with this function. The service request will show up in the service request list in the Tasks menu found from the Application section. If a customer doesn't select an asset for the a service request, the service request will be saved to the customer's first asset --- You can move it to another asset if you wish. 
    2328\subsubsection{Comment service request} 
    24 An open Service request can be commented by  
     29An open Service request can be commented by selecting the service request from the list of open service requests. This opens a commenting form that can be submitted as usual. When the comment is successfully received it will show up in the timeline. 
    2530\subsubsection{My information} 
    2631Customers can view and edit their contact information and language by clicking the account name link after the 'Logged in as:' text at the top of the page. 
  • trunk/public/stylesheets/customer_interface_timeline.css

    r712 r739  
    33 */ 
    44 
    5 span.form
     5span.form, a.form
    66        padding-left: 20px; 
    77        background: transparent url(../images/16x16/timeline_form.gif) 0px 0px no-repeat; 
     
    1717        background: transparent url(../images/16x16/timeline_attachment.gif) 0px 0px no-repeat; 
    1818} 
     19 
     20/* 
     21 * Viewing filled forms through timeline 
     22 */ 
     23 
     24div.filled_form { 
     25    background: #fff; 
     26    border: 5px solid #fff; 
     27    border-bottom: 0px; 
     28} 
     29 
     30div.filled_form h3 { 
     31    padding-bottom: 0.2em; 
     32} 
     33 
     34table.form_field_values th { 
     35    background: #ddf; 
     36    padding: 0.3em; 
     37    border-bottom: 5px solid #fff; 
     38    width: 5em; 
     39    white-space: nowrap; 
     40} 
     41 
     42table.form_field_values div { 
     43    white-space: nowrap; 
     44} 
     45 
     46table.form_field_values td { 
     47    background: #eef; 
     48    padding: 0.3em; 
     49    border-bottom: 5px solid #fff; 
     50    padding-right: 0.4em; 
     51} 
     52 
     53div.vspace { 
     54    height: 1em; 
     55} 
  • trunk/test/functional/customer_interface_controller_test.rb

    r718 r739  
    503503  end 
    504504 
     505  # Method: test_view_form 
     506  # ====================== 
     507  # Checks that form viewing works. 
     508  # 
     509  def test_view_form 
     510    # Form type 
     511    form_type = FormType.create!(:name => 'Test type', :editable => false, :ready => true) 
     512    # Form field groups 
     513    g1 = form_type.form_field_groups.create(:name => 'First', :place => 1) 
     514    g2 = form_type.form_field_groups.create(:name => 'Second', :place => 2) 
     515    # Form fields visible to customers 
     516    f1 = form_type.form_fields.create(:name => 'Integer', :user_field_name => 'int', :type_code => FormField::INTEGER, :form_field_group => g2, :show_to_customers => true, :show_to_customers => true, :place => 1) 
     517    f2 = form_type.form_fields.create(:name => 'String', :user_field_name => 'string', :type_code => FormField::STRING, :form_field_group => g2, :show_to_customers => true, :place => 2) 
     518    f3 = form_type.form_fields.create(:name => 'Boolean', :user_field_name => 'bool', :type_code => FormField::BOOLEAN, :form_field_group => g1, :show_to_customers => true, :place => 2) 
     519    f4 = form_type.form_fields.create(:name => 'Float', :user_field_name => 'float', :type_code => FormField::FLOAT, :form_field_group => g1, :show_to_customers => true, :place => 1) 
     520    f5 = form_type.form_fields.create(:name => 'Long string', :user_field_name => 'lstring', :type_code => FormField::LONG_STRING, :show_to_customers => true, :place => 5) 
     521    f6 = form_type.form_fields.create(:name => 'Date', :user_field_name => 'date', :type_code => FormField::DATE, :show_to_customers => true, :place => 4) 
     522    f7 = form_type.form_fields.create(:name => 'Time', :user_field_name => 'time', :type_code => FormField::TIME, :show_to_customers => true, :place => 3) 
     523    f8 = form_type.form_fields.create(:name => 'Datetime', :user_field_name => 'datetime', :type_code => FormField::DATETIME, :show_to_customers => true, :place => 2) 
     524    f9 = form_type.form_fields.create(:name => 'Automatic 1', :user_field_name => 'cmms_form_id', :type_code => FormField::AUTOMATIC, :show_to_customers => true, :place => 1) 
     525    # Form fields hidden from customers 
     526    form_type.form_fields.create(:name => 'Integer 2', :user_field_name => 'int2', :type_code => FormField::INTEGER) 
     527    form_type.form_fields.create(:name => 'String 2', :user_field_name => 'string2', :type_code => FormField::STRING) 
     528    form_type.form_fields.create(:name => 'Boolean 2', :user_field_name => 'bool2', :type_code => FormField::BOOLEAN) 
     529    form_type.form_fields.create(:name => 'Float 2', :user_field_name => 'float2', :type_code => FormField::FLOAT) 
     530    form_type.form_fields.create(:name => 'Long string 2', :user_field_name => 'lstring2', :type_code => FormField::LONG_STRING) 
     531    form_type.form_fields.create(:name => 'Date 2', :user_field_name => 'date2', :type_code => FormField::DATE, :form_field_group => g1) 
     532    form_type.form_fields.create(:name => 'Time 2', :user_field_name => 'time2', :type_code => FormField::TIME, :form_field_group => g2) 
     533    form_type.form_fields.create(:name => 'Datetime 2', :user_field_name => 'datetime2', :type_code => FormField::DATETIME) 
     534    form_type.form_fields.create(:name => 'Automatic 2', :user_field_name => 'cmms_form_type_name', :type_code => FormField::AUTOMATIC) 
     535    form_type.save! 
     536 
     537    # Test form 
     538    @root.customer = @customer 
     539    @root.save! 
     540    form = form_type.create_empty_form(@root) 
     541    form.ready = true 
     542    form.save! 
     543    form.update_automatic_field_values 
     544 
     545    unauthorized_customer = create_customer('unauthorized') 
     546    login(unauthorized_customer.login) 
     547 
     548    # The current customer does not own the asset, so he is unauthorized to 
     549    # view the form in it. Thus he is silently redirected to index. 
     550    get 'view_form', :id => form.id 
     551    assert_redirected_to :action => 'index' 
     552    assert_nil flash[:error] 
     553    assert_nil flash[:notice] 
     554    assert_nil assigns(:field_values) 
     555 
     556    login(@customer.login) 
     557 
     558    # The current customer is authorized to view the form 
     559    assert_equal true, form.authorized_to_read? 
     560    get 'view_form', :id => form.id 
     561    assert_response :success 
     562    assert_template 'customer_interface/view_form' 
     563    assert_nil flash[:error] 
     564    assert_nil flash[:notice] 
     565    assert_equal true, assigns(:show_login_name_link) 
     566 
     567    field_values_in_groups = assigns(:field_values_in_groups) 
     568    assert_equal 2, field_values_in_groups.size 
     569 
     570    first_group = field_values_in_groups.first 
     571    assert_equal g1, first_group[:group] 
     572    assert_equal 2, first_group[:field_values].size 
     573    assert_equal f4, first_group[:field_values].first.form_field 
     574    assert_equal f3, first_group[:field_values].last.form_field 
     575 
     576    last_group = field_values_in_groups.last 
     577    assert_equal g2, last_group[:group] 
     578    assert_equal 2, last_group[:field_values].size 
     579    assert_equal f1, last_group[:field_values].first.form_field 
     580    assert_equal f2, last_group[:field_values].last.form_field 
     581 
     582    groupless_field_values = assigns(:groupless_field_values) 
     583    assert_equal 5, groupless_field_values.size 
     584    assert_equal f9, groupless_field_values[0].form_field 
     585    assert_equal f8, groupless_field_values[1].form_field 
     586    assert_equal f7, groupless_field_values[2].form_field 
     587    assert_equal f6, groupless_field_values[3].form_field 
     588    assert_equal f5, groupless_field_values[4].form_field 
     589  end 
     590 
    505591  private 
    506592 
  • trunk/test/unit/form_test.rb

    r721 r739  
    162162  # Test automatic asset fields 
    163163 
     164  # Method: test_customer_authorized_to_read 
     165  # ======================================== 
     166  # Checks that customers are authorized to read right forms: 
     167  # Forms that belong to customers asset and have a form field with the 
     168  # option show_to_customers set to true. 
     169  # 
     170  def test_customer_authorized_to_read 
     171    user = UserAccount.current_user 
     172    customer = create_customer('customer') 
     173    UserAccount.current_user = customer 
     174    assert_equal false, @form.asset.authorized_to_read? 
     175    assert_equal false, @form.authorized_to_read? 
     176 
     177    UserAccount.current_user = user 
     178    @form.asset.customer = customer 
     179    @form.asset.save! 
     180 
     181    UserAccount.current_user = customer 
     182    assert_equal true, @form.asset.authorized_to_read? 
     183    assert_equal false, @form.authorized_to_read? 
     184 
     185    # Set a form field visible to customers 
     186    assert @form.form_field_values.find(:first).form_field.update_attribute(:show_to_customers, true) 
     187    @form.reload 
     188    # Now the customer can read the form 
     189    assert_equal true, @form.asset.authorized_to_read? 
     190    assert_equal true, @form.authorized_to_read? 
     191  end 
    164192end 

© 2004-2007 Norfello Oy All Rights Reserved