# 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['sex_value'] = params['data']['sex'].to_i - 1 result['Age_value'] = params['data']['age'].to_i if params['data']['calcification_score'].to_i < 1400 result['cal_value'] = 0 else result['cal_value'] = 1 end result['lpv'] = -0.51427548* (result['sex_value']- 0.508312) + 0.05764604* (result['Age_value'] - 61.894501) + 0.49138819*(result['cal_value'] - 0.334399) rescue 'error' @years = @form_to_show.years result['table'] = '

'+@form_to_show.table_above_texts[locale].to_s+'

' result['table'] += (''+(locale == 'zh_tw' ? '第' : '')+'') result['years'] = @years @years.each{|year| result['table'] += ('')} result['table'] += (''+(locale == 'zh_tw' ? '年' : '')+'') @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 @lpv_calc = [0.9736358,0.9548993,0.9229336] @servive_ratio = ((1-(@lpv_calc[0]**(Math.exp(result['lpv']))))*100).round(2) @texts = @form_to_show.text_above_texts[locale].to_s.gsub('
','
').gsub('{{Surgery_only}}',''+@servive_ratio.round(2).to_s+'') @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 if !@form_to_show.surgery_only_texts[locale].blank? @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 += '' else @surgery_only_texts = '' end 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 || [] @table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt') { "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) : '-') + '