Index: openacs-4/packages/assessment/www/asm-admin/results-export.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/assessment/www/asm-admin/results-export.tcl,v diff -u -r1.6.2.1 -r1.6.2.2 --- openacs-4/packages/assessment/www/asm-admin/results-export.tcl 16 Aug 2005 10:20:57 -0000 1.6.2.1 +++ openacs-4/packages/assessment/www/asm-admin/results-export.tcl 21 Feb 2006 18:09:34 -0000 1.6.2.2 @@ -48,25 +48,11 @@ } if {$assessment_data(anonymous_p) == "t"} { - set csv_first_row [list score submission_date] + set csv_first_row_list [list score submission_date] } else { - set csv_first_row [list score submission_date user_id email first_names last_name] + set csv_first_row_list [list score submission_date user_id email name] } - set column_list "" - db_foreach all_items {} { - lappend item_list [list $as_item_item_id $section_item_id [string range $object_type end-1 end] $data_type] - if {$column_format == "name"} { - lappend csv_first_row [as::assessment::quote_export -text $field_name] - } else { - if {[empty_string_p $description]} { - lappend csv_first_row [as::assessment::quote_export -text $title] - } else { - lappend csv_first_row [as::assessment::quote_export -text "$title / $description"] - } - } - } - set start_date_sql "" set end_date_sql "" if {![empty_string_p $start_time]} { @@ -81,36 +67,94 @@ lappend session_list $session_id if {$assessment_data(anonymous_p) == "t"} { - set csv($session_id) [list $percent_score $submission_date] + set csv_result_list($session_id) [list $percent_score $submission_date] } else { - set csv($session_id) [list $percent_score $submission_date $subject_id [as::assessment::quote_export -text $email] [as::assessment::quote_export -text $first_names] [as::assessment::quote_export -text $last_name]] + set subject_mail [db_string get-email "select email from parties where party_id = :subject_id"] + set csv_result_list($session_id) [list $percent_score $submission_date $subject_id [as::assessment::quote_export -text $subject_mail] [as::assessment::quote_export -text [person::name -person_id $subject_id]]] } } - + + set item_list [list] if {![empty_string_p $session_list]} { - foreach one_item $item_list { - util_unlist $one_item as_item_item_id section_item_id item_type data_type - array set results [as::item_type_$item_type\::results -as_item_item_id $as_item_item_id -section_item_id $section_item_id -data_type $data_type -sessions $session_list] - - foreach session_id $session_list { - if {[info exists results($session_id)]} { - lappend csv($session_id) [as::assessment::quote_export -text $results($session_id)] + + set section_list [db_list_of_lists all_sections {}] + + foreach one_section $section_list { + util_unlist $one_section section_id section_item_id + set mc_item_list [list] + db_foreach all_section_items {} { + lappend item_list $as_item_item_id + if {$column_format == "name"} { + set csv_first_row($as_item_item_id) [as::assessment::quote_export -text $field_name] } else { - lappend csv($session_id) "" + if {[empty_string_p $description]} { + set csv_first_row($as_item_item_id) [as::assessment::quote_export -text $title] + } else { + set csv_first_row($as_item_item_id) [as::assessment::quote_export -text "$title / $description"] + } } + set item_type [string range $object_type end-1 end] + if {$item_type == "mc"} { + lappend mc_item_list $as_item_item_id + } else { + array set results [as::item_type_$item_type\::results -as_item_item_id $as_item_item_id -section_item_id $section_item_id -data_type $data_type -sessions $session_list] + foreach session_id $session_list { + if {[info exists results($session_id)]} { + set csv_${as_item_item_id}($session_id) [as::assessment::quote_export -text $results($session_id)] + } else { + set csv_${as_item_item_id}($session_id) "" + } + } + array unset results + } } - - array unset results + + # Now get all MC items in one go + if {![empty_string_p $mc_item_list]} { + db_foreach mc_items {} { + if {[empty_string_p $text_value]} { + if {[exists_and_not_null csv_${mc_item_id}($session_id)]} { + # append list of choices seperated with comma + append csv_${mc_item_id}($session_id) ",[as::assessment::quote_export -text $title]" + } else { + # just set the choice value + set csv_${mc_item_id}($session_id) [as::assessment::quote_export -text $title] + } + } else { + set csv_${mc_item_id}($session_id) [as::assessment::quote_export -text $text_value] + } + } + array unset results + } } } - - set csv_text "[join $csv_first_row ","]\r\n" + + foreach item_id $item_list { + lappend csv_first_row_list $csv_first_row($item_id) + } + set csv_text "[join $csv_first_row_list ";"]\r\n" + foreach session_id $session_list { - append csv_text "[join $csv($session_id) ","]\r\n" + foreach item_id $item_list { + if {[exists_and_not_null csv_${item_id}($session_id)]} { + lappend csv_result_list($session_id) "[set csv_${item_id}($session_id)]" + } else { + lappend csv_result_list($session_id) "" + } + } + append csv_text "[join $csv_result_list($session_id) ";"]\r\n" } + set csv_text [string map {\xe4 � \xfc � \xf6 � \xdf � \xc4 � \xdc � \xd6 �} $csv_text] } -after_submit { - ns_set put [ad_conn outputheaders] Content-Disposition "attachment;filename=results.csv" - ns_return 200 "text/plain" "$csv_text" + set tmp_filename [ns_tmpnam] + set tmp_csv_filename "$tmp_filename.csv" + set fp [open $tmp_csv_filename w] + puts $fp "$csv_text" + close $fp + ns_set put [ad_conn outputheaders] Content-Disposition "attachment;filename=results.csv" + ns_return 200 "text/plain" [encoding convertfrom iso8859-1 "$csv_text"] + ns_returnfile 200 text/csv $tmp_csv_filename + # iso-8859-1 } ad_return_template