# encoding: utf-8 require 'rubyXL' class CancerpredictsController < ApplicationController def initialize super @app_title = "cancerpredict" end def calculate uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] create_first_field #begin if params['header'].to_i == 1 locale = params['locale'].to_s rescue 'zh_tw' locale = 'zh_tw' if locale == 'zh_cn' result = {} @head_images = {} @form_to_show.head_images_id.each do |image_id| next if image_id.to_s == "" @image = Headimages.find_by(:id=>image_id.to_s) @url = @image.temp_file.to_s @head_images[@image.sort_number.to_i] = ('') end result['head_images'] = Hash[@head_images.sort].values.join('') @head_images = {} @form_to_show.title_images_id.each do |image_id| next if image_id.to_s == "" @image = Headimages.find_by(:id=>image_id.to_s) @url = @image.temp_file.to_s @head_images[@image.sort_number.to_i] = ('') end result['danger_texts'] = (@form_to_show.danger_texts[locale] rescue '') result['title'] = Hash[@head_images.sort].values.join('') result['page_title'] = @form_to_show.title_texts[params[:locale]] else @record = Cancerpredictrecord.new @record.title = @app_title @choice_keys = [] @choice_values = [] @choice_names = [] @form_to_show.form_show.values.each{|choice| @choice_keys.push choice[:variable]} @form_to_show.form_show.values.each{|choice| @choice_values.push choice[:choice_fields]} @form_to_show.form_show.values.each{|choice| @choice_names.push choice[:name]} @choice_keys.each_with_index{|key,i| @record.names[key] = @choice_names[i]} @choice_keys.each_with_index{|key,i| @record.values[key] = @choice_values[i]} params['data'].each do |rec_key,rec_value| @record.result[rec_key] = rec_value end @record.submit_time = Time.now.to_s @record.save locale = params['data']['locale'].to_s rescue 'zh_tw' locale = 'zh_tw' if locale == 'zh_cn' result = {} result['age1'] = (params['data']['age'].to_f/100)**(0.5) result['age2'] = ((params['data']['age'].to_f/100)**(0.5))*Math.log(params['data']['age'].to_f/100,Math.exp(1)) result['size1'] = Math.log(params['data']['size'].to_f/10,Math.exp(1)) if params['data']['lymph_nodes_examined'].to_i != 0 result['ratio'] = params['data']['lymph_nodes_positive'].to_f/params['data']['lymph_nodes_examined'].to_f rescue 0 else result['ratio'] = 0 end result['ratio'] = 1 if result['ratio'] > 1 result['nposit'] = ((result['ratio'].to_f+0.1)/0.1)**0.5 @T = '' if params['data']['tumor_direct_extension'].to_i != 1 if params['data']['size'].to_i <= 20 @T = 'T1' elsif params['data']['size'].to_i > 20 && params['data']['size'].to_i <= 50 @T = 'T2' elsif params['data']['size'].to_i > 50 @T = 'T3' end else @T = 'T4' end result['T'] = @T @N = '' if params['data']['lymph_nodes_positive'].to_i == 0 @N = 'N0' elsif params['data']['lymph_nodes_positive'].to_i >= 1 && params['data']['lymph_nodes_positive'].to_i <= 3 && params['data']['micrometastasis'].to_i != 1 @N = 'N1' elsif params['data']['lymph_nodes_positive'].to_i >= 1 && params['data']['lymph_nodes_positive'].to_i <= 3 && params['data']['micrometastasis'].to_i == 1 @N = 'N1mi' elsif params['data']['lymph_nodes_positive'].to_i >= 4 && params['data']['lymph_nodes_positive'].to_i <= 9 @N = 'N2' elsif params['data']['lymph_nodes_positive'].to_i >= 10 @N = 'N3' end result['N'] = @N @M = '' if params['data']['Distant_Metastasis'].to_i != 1 @M = 'M0' else @M = 'M1' end result['M'] = @M if @M == 'M0' if @T == 'T1' && (@N == 'N0' || @N == 'N1mi') result['pstage'] = 1 elsif ((@T == 'T2' || @T == 'T3') && (@N == 'N0')) || ((@T == 'T1' || @T == 'T2') && (@N == 'N1')) result['pstage'] = 2 elsif (@T == 'T4' && @N == 'N0') || ((@T == 'T3' || @T == 'T4') && @N == 'N1') || (@N == 'N2' || @N == 'N3') result['pstage'] = 3 end else result['pstage'] = 4 end if (params['data']['ER_status'].to_i != 2 || params['data']['PR_status'].to_i != 2) && (params['data']['HER2_status'].to_i != 1) result['subtype'] = 1 elsif params['data']['ER_status'].to_i == 2 && params['data']['PR_status'].to_i == 2 && (params['data']['HER2_status'].to_i == 1) result['subtype'] = 2 elsif params['data']['ER_status'].to_i == 2 && params['data']['PR_status'].to_i == 2 && (params['data']['HER2_status'].to_i != 1) result['subtype'] = 3 end if params['data']['grade'].to_i == 2 || params['data']['grade'].to_i == 4 result['grade_2'] = 1 else result['grade_2'] = 0 end if params['data']['grade'].to_i == 3 result['grade_3'] = 1 else result['grade_3'] = 0 end if result['subtype'].to_i == 2 result['subtype_HER2'] = 1 else result['subtype_HER2'] = 0 end if result['subtype'].to_i == 3 result['subtype_triple'] = 1 else result['subtype_triple'] = 0 end if result['pstage'].to_i == 2 result['pstage_2'] = 1 else result['pstage_2'] = 0 end if result['pstage'].to_i == 3 result['pstage_3'] = 1 else result['pstage_3'] = 0 end if result['pstage'].to_i == 4 result['pstage_4'] = 1 else result['pstage_4'] = 0 end if params['data']['lvi'].to_i == 1 result['lvi_yes'] = 1 else result['lvi_yes'] = 0 end result['lpv'] = ((result['age1']-0.7276655)*(-10.87)+(result['age2']+0.4540707)*8.968+(result['size1']-0.643632)*0.7678+(result['nposit']-1.346932)*0.5339+ result['grade_2']*0.4795+result['grade_3']*0.818+result['subtype_HER2']*0.1806+result['subtype_triple']*0.6457+result['pstage_2']*0.5311+ result['pstage_3']*1.134+result['pstage_4']*2.172+result['lvi_yes']*0.3321-0.04 rescue 'error') @years = @form_to_show.years result['table'] = '

'+@form_to_show.table_above_texts[locale]+'

' result['table'] += '' result['years'] = @years @years.each{|year| result['table'] += ('')} result['table'] += '' @table_head = (locale == "zh_tw") ? ['治療','額外治療效益','總生存率(%)'] :['Treatment','Additional Benefit','Overall Survival(%)'] @head_name = ['Treatment','Additional_Benefit','Overall_Survival'] @therapy_choices = (locale == "zh_tw") ? ['純手術'] :['Surgery only'] @form_to_show.form_show_in_result.values.each{|choice| @therapy_choices.push choice["name"][locale]} @therapy_names = ['Surgery_only'] @form_to_show.form_show_in_result.values.each{|choice| @therapy_names.push choice["variable"]} result['treatmeny_method'] = @therapy_names result['table'] += '' @table_head.each_with_index{|head,index| result['table'] += ('')} result['table'] += '' @therapy_choices.each_with_index do |choice,i| result['table'] += '' @table_head.each_with_index do |head,index| result['table'] += ('') end result['table'] += '' end @texts = @form_to_show.text_above_texts[locale].gsub('
','
') @texts = @texts.split('{{years}}') @texts.delete('') result['texts'] = ''+@texts[0] @years.each{|year| result['texts'] += ('')} if @texts.count > 1 result['texts'] += (@texts[1]+'') if @texts.count > 1 else result['texts'] += '' end @lpv_calc = [-0.001476145,-0.01261639,-0.02519608] @servive_ratio = ((Math.exp(@lpv_calc[-1])**(Math.exp(result['lpv'])))*100).round @surgery_only_texts = @form_to_show.surgery_only_texts[locale] @surgery_only_texts.insert(0,'

') @surgery_only_texts = @surgery_only_texts.gsub('{{Surgery_only}}',''+@servive_ratio.round.to_s+'') @surgery_only_texts = @surgery_only_texts.gsub('{{surgery_year}}',''+@years[-1].to_s+'') @surgery_only_texts += '' result['texts'] += @surgery_only_texts result['texts'] += ''+@form_to_show.extra_texts[locale]+'

' result['extra_therapy_texts'] = @form_to_show.extra_therapy_texts[locale] rescue @form_to_show.extra_therapy_texts['zh_tw'] result['servive_ratio'] = @servive_ratio end result = result.merge(params) render :json=> result # rescue # render :json=> params #end end def index uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] create_first_field @size = {} @size_name = ['small','medium','large'] @size_name.each{|name| @size[name] = @form_to_show[name]['font_size']} @size_active_size = '' @size.each{|size_key,size_value| (@form_to_show[size_key]['active'].to_i == 1) ? ( @size_active_size = size_value ): nil } @table_str = '
' @table_str += '
'+@form_to_show.text_descibe[I18n.locale.to_s] +'
' @table_str += '
'+'' @size.each{|size_key,size_value| @table_str += ('' )} @table_str += '
' @table_str_left = '
' @table_str_right = '
' @form_to_show.form_show.each do |num,property| @field_property = {} property.each do |key,value| @value= value if @value.class == BSON::Document || @value.class == Hash @disp_value = @value[I18n.locale.to_s] rescue "" else @disp_value = @value end @field_property[key] = @disp_value end if @field_property['right'] == 0 @table_str_left += '
' break if @field_property["name"] == "" @table_str_left += '' if @field_property["comment_text"] != "" @table_str_left += '' @table_str_left += '' else @site_locales = current_site.in_use_locales @site_locales.delete(I18n.locale) @site_locales.each do |locale| next if property["comment_text"][locale.to_s] == "" @field_property["comment_text"] = property["comment_text"][locale.to_s] break end if @field_property["comment_text"] != "" @table_str_left += '' @table_str_left += '' else @table_str_left += '
' end end if @field_property["is_num"] == 1 if @field_property["is_float"] == 1 @table_str_left += '
' else @table_str_left += '
' @table_str_left += '' @table_str_left += '' @please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice" @table_str_left += '
' end else @table_str_left += '
' @field_property["choice_fields"].each do |create_choice| @table_str_left += '' @table_str_left += '' end @table_str_left += '
' end if @field_property["hint"].to_s != "" @table_str_left +='
'+@field_property["hint"]+'
' end @table_str_left += '
' else @table_str_right += '
' break if @field_property["name"] == "" @table_str_right += '' if @field_property["comment_text"] != "" @table_str_right += '' @table_str_right += '' else @site_locales = current_site.in_use_locales @site_locales.delete(I18n.locale) @site_locales.each do |locale| next if property["comment_text"][locale.to_s] == "" @field_property["comment_text"] = property["comment_text"][locale.to_s] break end if @field_property["comment_text"] != "" @table_str_right += '' @table_str_right += '' else @table_str_right += '
' end end if @field_property["is_num"] == 1 if @field_property["is_float"] == 1 @table_str_right += '
' else @table_str_right += '
' @table_str_right += '' @table_str_right += '' @please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice" @table_str_right += '
' end else @table_str_right += '
' @field_property["choice_fields"].each do |create_choice| @table_str_right += '' @table_str_right += '' end @table_str_right += '
' end if @field_property["hint"].to_s != "" @table_str_right +='
'+@field_property["hint"]+'
' end @table_str_right += '
' end end @table_str_left += '
' @table_str_right += '
' @table_str += (@table_str_left+@table_str_right) @table_str +='
' @table_result_str = '
'+t("cancerpredict.table.result").to_s+'
' @tab_name = ['table','text'] @table_result_str += '
' @tab_name.each_with_index{|name,index| @table_result_str += ('
')} @table_result_str += '
' @tab_name.each{|name| @table_result_str += ''} @table_result_str += '
' @table_button = '
' @submit_btn_str='' @reset_btn_str='' @table_button += (@submit_btn_str+@reset_btn_str+'
') @table_result_choice_fileds = '
' if @form_to_show.form_result_is_right == 1 @table_result_choice_fileds = '
' if @form_to_show.form_result_is_right == 0 @table_result_choice_fileds += ''+t('cancerpredict.table.Therapy_choice').to_s+'
' @table_result_choice_fileds += '
' @form_to_show.form_show_in_result.each do |num,property| @field_property = {} property.each do |key,value| @value= value if @value.class == BSON::Document || @value.class == Hash @disp_value = @value[I18n.locale.to_s] rescue "" else @disp_value = @value end @field_property[key] = @disp_value end @table_result_choice_fileds += '
' break if @field_property["name"] == "" @table_result_choice_fileds += '' if @field_property["comment_text"] != "" @table_result_choice_fileds += '' @table_result_choice_fileds += '' else @site_locales = current_site.in_use_locales @site_locales.delete(I18n.locale) @site_locales.each do |locale| next if property["comment_text"][locale.to_s] == "" @field_property["comment_text"] = property["comment_text"][locale.to_s] break end if @field_property["comment_text"] != "" @table_result_choice_fileds += '' @table_result_choice_fileds += '' else @table_result_choice_fileds += '
' end end if @field_property["is_num"] == 1 if @field_property["is_float"] == 1 @table_result_choice_fileds += '
' else @table_result_choice_fileds += '
' @table_result_choice_fileds += '
' @table_str_left += '' @please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice" @table_result_choice_fileds += '
' end else @table_result_choice_fileds += '
' @field_property["choice_fields"].each do |create_choice| @table_result_choice_fileds += '' @table_result_choice_fileds += '' end @table_result_choice_fileds += '
' end if @field_property["hint"].to_s != "" @table_result_choice_fileds +='
'+@field_property["hint"]+'
' end @table_result_choice_fileds += '
' end @table_result_choice_fileds += '
' @table_str += (@table_button+'
'+@table_result_str+@table_result_choice_fileds+'
'+'
') { "cancerpredict" => [], "extras"=>{"table"=> @table_str} } end def widget uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags categories = OrbitHelper.widget_categories || [] end def create_first_field if Cancerpredictfields.where("title"=>(@app_title + "_back")).take_while{true}.count == 0 @form_to_show = Cancerpredictfields.new() @form_to_show.title = @app_title +"_back" @form_to_show.save end @form_to_show if Cancerpredictfields.where("title"=>@app_title).take_while{true}.count == 0 @form_to_show = Cancerpredictfields.new() @form_to_show.title=@app_title @form_to_show.save @form_to_show = Cancerpredictfields.where("title"=>@app_title).first else @form_to_show = Cancerpredictfields.where("title"=>@app_title).first end end end
' + head + '
' + ((index == 0) ? (((i==0)? '' : '+') + choice) : '-') + '