ad_library { Assessment Checks procs @author Anny Flores (annyflores@viaro.net) Viaro Networks (www.viaro.net) @creation-date 2005-01-13 } namespace eval as::assessment::check {} ad_proc -public as::assessment::check::get_types { } { Return the checks types } { set types [list [list "\#assessment.action\#" "t"] [list "\#assessment.branch\#" "f"]] return $types } ad_proc -public as::assessment::check::get_assessments { } { } { set package_id [ad_conn package_id] set user_id [ad_conn user_id] set permission "" if {![acs_user::site_wide_admin_p -user_id $user_id]} { set permission "and ci.item_id in (select object_id from acs_permissions where grantee_id=:user_id and privilege='admin')"} set assessment_list [list [list "[_ assessment.all]" "all"]] set assessments [db_list_of_lists assessment {}] foreach assessment $assessments { lappend assessment_list [list [lindex $assessment 0] [lindex $assessment 1]] } return $assessment_list } ad_proc -public as::assessment::check::state_options { } { } { set approved_options [list [list "[_ assessment.approved]" "t"] [list "[_ assessment.approved_with]" "ae"] [list "[_ assessment.not_approved]" "f"] ] return $approved_options } ad_proc -public as::assessment::check::intervals { } { Return the time intervals } { set today [clock format [clock seconds] -format %Y-%m-%d] set yesterday [clock format [clock scan yesterday] -format %Y-%m-%d] set two_days [clock format [clock scan "2 days ago"] -format %Y-%m-%d] set last_week [clock format [clock scan "1 week ago"] -format %Y-%m-%d] set last_month [clock format [clock scan "1 month ago"] -format %Y-%m-%d] return [list [list [_ assessment.all] "all"] \ [list [_ assessment.today] $today] \ [list [_ assessment.yesterday] $yesterday] \ [list [_ assessment.two_days] $two_days] \ [list [_ assessment.last_week] $last_week] \ [list [_ assessment.last_month] $last_month]] } ad_proc -public as::assessment::check::add_check_return_url { {action_p} } { Return the url } { if { $action_p == "t"} { return "action-select" } else { return "section-select" } } ad_proc -public as::assessment::check::get_max_order { {-action_perform} {-section_id} } { Return the next value for order_by } { set order [db_string get_max_order {} -default 1] if { $order eq ""} { set order 1 } else { incr order } return $order } ad_proc -public as::assessment::check::get_parameter_value { {-parameter_id} {-type} {-check_id} } { Return the parameter_value } { if { $type eq "n"} { return [db_string get_param_n {} -default " "] } else { return [db_string get_param_q {} -default " "] } } ad_proc -public as::assessment::check::set_parameter_value { {-parameter_id} {-value} {-check_id} {-type} } { } { set exists_p [db_string get_check_id {} -default 0] if { $type eq "n"} { if {$exists_p != 0} { db_dml param_value_update_n {} } else { db_dml param_value_insert_n {} } } else { if { $exists_p != 0} { db_dml param_value_update_q {} } else { db_dml param_value_insert_q {} } } } ad_proc -public as::assessment::check::re_order_actions { {-check_id} {-action_perform} {-section_id} } { } { set order_by [db_string get_order_by {}] set count 0 db_foreach next_order {} { set order [expr {$order_by+$count}] db_dml update_order {} incr count } } ad_proc -public as::assessment::check::get_sql { {-item_id} {-condition} } { } { set as_item_id [db_string get_item_id {select item_id from cr_revisions where revision_id=:item_id}] set check_sql "select (case when idc.choice_id in (select revision_id from cr_revisions where item_id=$condition) then \'1\' else \'0\' end) as perform_p from as_item_data id, as_item_data_choices idc where id.as_item_id in (select revision_id from cr_revisions where item_id=$as_item_id) and id.item_data_id=idc.item_data_id and id.session_id=:session_id" return $check_sql } ad_proc -public as::assessment::check::swap_actions { {-check_id} {-action_perform} {-section_id} {-direction} {-order_by} } { } { if { $direction eq "d"} { set order_p [expr {$order_by + 1}] set swap_check_id [db_string get_swap_check {}] db_dml update_1 {} db_dml update_2 {} } else { set order_p [expr {$order_by - 1}] set swap_check_id [db_string get_swap_check_e {}] db_dml update_1_e {} db_dml update_2_e {} } } ad_proc -public as::assessment::check::action_log { {-session_id} {-check_id} {-failed } } { } { set user_id [ad_conn user_id] set log_id [db_string get_next_val {}] set action_id [db_string action_id {}] set message " " if { $failed == "f" } { set message "This action failed." } db_transaction { db_dml insert_action {} } } ad_proc -public as::assessment::check::manual_action_log { {-session_id} {-check_id} } { } { set user_id [ad_conn user_id] set log_id [db_string get_next_val {}] set action_id [db_string action_id {}] set message " " db_transaction { db_dml insert_action {} } } ad_proc -public as::assessment::check::action_exec { {-inter_item_check_id} {-session_id} } { } { set error_txt "" db_foreach get_check_params {} { set parameter_name [db_1row select_name {}] set $varname "" if {$value eq ""} { set choice [db_list_of_lists get_item_choice {}] set answer [db_0or1row get_answer {}] if {([info exists choice_id] && $choice_id ne "")} { set $varname "$choice_id" } else { if { [info exists boolean_answer] } { append $varname $boolean_answer } if { [info exists numeric_answer] } { append $varname $numeric_answer } if { [info exists integer_answer] } { append $varname $integer_answer } if { [info exists text_answer] } { append $varname $text_answer } if { [info exists clob_answer] } { append $varname $clob_answer } if { [info exists content_answer] } { append $varname $content_answer } } } else { set $varname $value } } set tcl_code_p [db_1row select_tcl {}] set failed_p "f" if {[catch $tcl_code errorMsg]} { set failed_p "t" ns_log error "Error running assessment action $action_name '${errorMsg}'" } set admin [db_list_of_lists get_assessment_admin {}] set to [list] foreach notify_user $admin { lappend to $notify_user } if {$failed_p} { notification::new -type_id [notification::type::get_type_id -short_name inter_item_check_notif] -object_id $inter_item_check_id -notif_subject "$action_name has been executed" -notif_text "The action $action_name has encountered an error: $errorMsg" -subset $to -force -action_id $inter_item_check_id } as::assessment::check::action_log -session_id $session_id -check_id $inter_item_check_id -failed $failed_p } ad_proc -public as::assessment::check::manual_action_exec { {-inter_item_check_id} {-session_id} {-action_log_id} } { } { db_0or1row subject_id {select subject_id from as_sessions where session_id=:session_id} db_foreach get_check_params {} { set parameter_name [db_1row select_name {}] set $varname "" if {$value eq ""} { set choice [db_list_of_lists get_item_choice {}] set answer [db_0or1row get_answer {}] if {([info exists choice_id] && $choice_id ne "")} { set $varname "$choice_id" } else { if { [info exists boolean_answer] } { append $varname $boolean_answer } if { [info exists numeric_answer] } { append $varname $numeric_answer } if { [info exists integer_answer] } { append $varname $integer_answer } if { [info exists text_answer] } { append $varname $text_answer } if { [info exists clob_answer] } { append $varname $clob_answer } if { [info exists content_answer] } { append $varname $content_answer } } } else { set $varname $value } } set tcl_code_p [db_1row select_tcl {}] set failed_p "t" set failed [catch $tcl_code] if { $failed > 0 } { set failed_p "f" } set user_id [ad_conn user_id] db_dml update_actions_log {} set admin [db_list_of_lists get_assessment_admin {}] set to [list] foreach notify_user $admin { lappend to $notify_user } if { [parameter::get -package_id [ad_conn package_id] -parameter NotifyAdminOfActions -default 1] } { notification::new -type_id [notification::type::get_type_id -short_name inter_item_check_notif] -object_id $inter_item_check_id -notif_subject "$action_name has been executed" -notif_text "The action $action_name has been executed. This message has been showed to the user: $user_message" -subset $to -force -action_id $inter_item_check_id } } ad_proc -public as::assessment::check::eval_i_checks { {-session_id} {-section_id} } { } { set section_checks [db_list_of_lists section_checks {}] foreach check $section_checks { set check_sql [lindex $check 1] set perform [db_string check_sql $check_sql -default 0] if {[lindex $check 2] == "t"} { if {$perform == 1} { as::assessment::check::action_exec -inter_item_check_id [lindex $check 0] -session_id $session_id } } } } ad_proc -public as::assessment::check::branch_checks { {-session_id} {-section_id} {-assessment_id} } { } { set order "f" set perform 0 set checks [db_list_of_lists section_checks {}] foreach check $checks { as::assessment::data -assessment_id $assessment_id set new_assessment_revision $assessment_data(assessment_rev_id) set section_id_to [lindex $check 2] set perform [db_string check_sql "[lindex $check 0]" -default 0] if {$perform == 1} { set order [db_string get_order {}] } } if {$order == "f"} { return $order } { return [expr {$order -1}] } } ad_proc -public as::assessment::check::eval_aa_checks { {-session_id} {-assessment_id} } { } { set assessment_rev_id [db_string get_assessment_id {}] set checks [db_list_of_lists section_checks {}] foreach check_id $checks { set info [db_0or1row check_info {}] set perform [db_string check_sql $check_sql -default 0] if {$action_p == "t"} { if {$perform == 1} { as::assessment::check::action_exec -inter_item_check_id $inter_item_check_id -session_id $session_id } } } } ad_proc -public as::assessment::check::eval_m_checks { {-session_id} {-assessment_id} } { } { db_foreach assessment_checks {} { if {$action_p == "t"} { set perform [db_string check_sql $check_sql -default 0] if {$perform == 1} { set failed "" as::assessment::check::manual_action_log -check_id $inter_item_check_id -session_id $session_id } } } } ad_proc -public as::assessment::check::eval_or_checks { {-session_id} {-section_id} } { } { set section_checks [db_list_of_lists section_checks {}] foreach check $section_checks { set check_sql [lindex $check 1] set perform [db_string check_sql $check_sql -default 0] if {[lindex $check 2] == "t"} { if {$perform == 1} { as::assessment::check::action_exec -inter_item_check_id [lindex $check 0] -session_id $session_id } } } } ad_proc -public as::assessment::check::eval_sa_checks { {-session_id} {-assessment_id} } { } { set assessment_rev_id [db_string get_assessment_id {}] set checks [db_list_of_lists section_checks {}] foreach check_id $checks { set info [db_0or1row check_info {}] set perform [db_string check_sql $check_sql -default 0] if {$action_p == "t"} { if {$perform == 1} { as::assessment::check::action_exec -inter_item_check_id $inter_item_check_id -session_id $session_id } } } } ad_proc -public as::assessment::check::confirm_display { {-check_id} {-index} } { } { set show_check_info "" set mod "" set info [db_0or1row get_check_info {}] set mod [expr {$index%2}] if {$mod==0} { set class "odd" } else { set class "even" } set action "" set parameter_list "" if { $action_p=="t"} { set info [db_0or1row get_check_info_a {}] db_foreach parameters {} { append parameter_list "