ad_page_contract {
    Search results -- finds employees and offices that match the search string.

    This works as-is with the ArsDigita Community System 3.x Intranet Module, but
    the queries can easily be changed to work with another system.

    You may use this software free under the GNU General Public License.

    @param letters_of_name search string
    @param passcode  The 4-digit passcode, in case someone tries to access this page w/out going through passcode-check.tcl.

    @author Eve Andersson, eveander@eveander.com
    @creation-date February 24, 2001
} {
    letters_of_name
    passcode:notnull,integer
}

if { [string compare $passcode [vxml_tellme_phone_dir_passcode]] != 0 } {
    ns_log Notice "/tellme/name-search-results.tcl exiting due to incorrect passcode"
    return
}

set sql "select u.user_id, u.first_names, u.last_name
from users u, im_employees_active e
where u.user_id = e.user_id
and vxml_keypad_string(u.last_name) like '$letters_of_name%'"

set result_counter 0
set grammar_list [list] 
set prompt_list [list]
db_foreach name_matches $sql {
    incr result_counter
    lappend grammar_list "\[$result_counter dtmf-$result_counter\] {<option \"[set user_id]\">}"
    lappend prompt_list "<audio>For [vxml_convert_illegal_characters "$first_names $last_name"] press $result_counter</audio>"
}

# also search for offices

set sql_office "select group_id, group_name from user_groups where parent_group_id=[im_office_group_id] and vxml_keypad_string(group_name) like '$letters_of_name%'"

db_foreach office_matches $sql_office {
    incr result_counter
    lappend grammar_list "\[$result_counter dtmf-$result_counter\] {<option \"office [set group_id]\">}"
    lappend prompt_list "<audio>For the [vxml_convert_illegal_characters $group_name] office press $result_counter</audio>"
}

db_release_unused_handles

lappend grammar_list "\[9 dtmf-9\] {<option \"0\">}"
lappend prompt_list "<audio>To find a different person or office press 9</audio>"

set grammars [join $grammar_list "\n      "]
set prompts [join $prompt_list "\n      "]

# if there are no results or more than 8 results, give them a message
# and send them back to name-search-input.tcl

if { $result_counter != 0 && $result_counter < 9 } {
    set content "<vxml>
<form id=\"name_search\">
   <field name=\"user_choice\">
      <prompt>
      $prompts
      </prompt>
      <grammar>
      <!\[CDATA\[
   \[
      $grammars
   \]
      \]\]>
      </grammar>
   </field>
   <filled>
      <if cond=\"user_choice == '0'\">
       <goto next=\"name-search-input.tcl?passcode={passcode}#phone_dir_name_entry\"/>
      <else/>
       <goto next=\"one.tcl?user_or_office_id={user_choice}&amp;passcode=$passcode#one_user\"/>
      </if>
   </filled>
   <nomatch>
      <audio>I'm sorry, I couldn't understand your response.</audio>
      $prompts
      <listen/>
   </nomatch>
   <noinput>
      $prompts
      <listen/>
   </noinput>
</form>
</vxml>"
} elseif { $result_counter == 0 } {
    # no results
    set content "<vxml>
<form id=\"name_search\">
   <block>
      <audio>I'm sorry, no matches were found.</audio>
      <goto next=\"name-search-input.tcl?passcode=$passcode#phone_dir_name_entry\"/>
   </block>
</form>
</vxml>"
} else {
    # more than 8 results
    set content "<vxml>
<form id=\"name_search\">
   <block>
      <audio>I'm sorry, there were too many matches.  Please try again, entering more letters this time.</audio>
      <goto next=\"name-search-input.tcl?passcode=$passcode#phone_dir_name_entry\"/>
   </block>
</form>
</vxml>"
}

vxml_return_content $content