Annotation of loncom/interface/lonpickuser.pm, revision 1.3

1.1       raeburn     1: # The LearningOnline Network
                      2: # Search for a user
                      3: #
1.3     ! raeburn     4: # $Id: lonpickuser.pm,v 1.2 2009/08/06 20:46:42 raeburn Exp $
1.1       raeburn     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ###
                     29: 
                     30: =head1 NAME
                     31: 
                     32: Apache::lonpickuser.pm
                     33: 
                     34: =head1 SYNOPSIS
                     35: 
                     36: Allows users to search by username, lastname, or lastname,firstname in LON-CAPA,
                     37: and also in an institutional directory (if enabled).
                     38: 
                     39: This is part of the LearningOnline Network with CAPA project
                     40: described at http://www.lon-capa.org.
                     41: 
                     42: =head1 SUBROUTINES
                     43: 
                     44: =over
                     45: 
                     46: =item handler()
                     47: 
                     48: =item gochoose_javascript()
                     49:  
                     50: =back
                     51: 
                     52: =cut
                     53: 
                     54: package Apache::lonpickuser;
                     55: 
                     56: use strict;
                     57: use Apache::Constants qw(:common :http);
                     58: use Apache::lonnet;
                     59: use Apache::loncommon;
                     60: use Apache::lonlocal;
                     61: use Apache::lonuserutils;
                     62: use Apache::loncreateuser;
                     63: use LONCAPA;
                     64: 
                     65: sub handler {
                     66:     my ($r) = @_;
                     67:     &Apache::loncommon::content_type($r,'text/html');
                     68:     $r->send_http_header;
                     69:     return OK if $r->header_only;
                     70: 
                     71:     # Get parameters from query string
                     72:     &Apache::loncommon::get_unprocessed_cgi
                     73:         ($ENV{'QUERY_STRING'},['srchdom','form','udomelement','unameelement','ulastelement',
1.2       raeburn    74:                                'ufirstelement','uemailelement','hideudomelement','coursedom']);
1.1       raeburn    75:     my $js;
                     76: 
1.2       raeburn    77:     if (($env{'form.phase'} eq 'get_user_info') || 
                     78:         ($env{'form.phase'} eq 'store_newuser')) {
1.1       raeburn    79:         $js = &gochoose_javascript();
1.2       raeburn    80:     } elsif ($env{'form.phase'} eq 'define_newuser') {
                     81:         $js = <<"ENDSP";
                     82: <script type="text/javascript">
                     83: // <![CDATA[
                     84: function setphase(caller) {
                     85:     document.newuserinfo.phase.value = caller;
                     86:     document.newuserinfo.submit();
                     87:     return;
                     88: }
                     89: // ]]>
                     90: </script>
                     91: ENDSP
                     92:     }
                     93:     my $startargs = {'no_nav_bar'  => 1, };
                     94:     if ($env{'form.phase'} eq 'store_newuser') {
                     95:         my $uname = $env{'form.srchterm'};
                     96:         my $udom = $env{'form.srchdomain'};
                     97:         my $newfirst = $env{'form.newfirst'};
                     98:         my $newlast = $env{'form.newlast'};
                     99:         my $newemail = $env{'form.newemail'};
                    100:         $startargs->{'add_entries'} = { onload => 'javascript:gochoose('."'$uname','$udom','$newfirst','$newlast','$newemail'".');' };
1.1       raeburn   101:     }
1.2       raeburn   102:     $r->print(&Apache::loncommon::start_page('Selecting course personnel',
                    103:                                              $js,$startargs));
1.1       raeburn   104:     my %toset = (
                    105:                   uname   => 'unameelement',
                    106:                   udom    => 'udomelement',
                    107:                   first   => 'ufirstelement',
                    108:                   lastn   => 'ulastelement',
                    109:                   email   => 'uemailelement',
                    110:                   hidedom => 'hideudomelement',
                    111:                 );
                    112: 
                    113:     my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
                    114:     my %srch;
                    115:     foreach my $item (@search) {
                    116:         $srch{$item} = $env{'form.'.$item};
                    117:     }
                    118:     my $forcenewuser;
                    119:     my $context = 'requestcrs';
                    120:     my $usertype;
                    121:     if (($srch{'srchin'} eq 'dom') &&
                    122:         ($srch{'srchby'} eq 'uname') &&
                    123:         ($srch{'srchtype'} eq 'exact') &&
                    124:         ($srch{'srchdomain'} ne '') &&
                    125:         ($srch{'srchterm'} ne '')) {
                    126:         my ($rules,$ruleorder) =
                    127:             &Apache::lonnet::inst_userrules($srch{'srchdomain'},'username');
                    128:         $usertype = &Apache::lonuserutils::check_usertype($srch{'srchdomain'},
                    129:                                                           $srch{'srchterm'},$rules);
                    130:     }
                    131: 
1.2       raeburn   132:     my $dom = $env{'form.coursedom'};
1.1       raeburn   133: 
                    134:     my $cancreate =
                    135:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
                    136:     my $userpicker =
                    137:        &Apache::loncommon::user_picker($dom,\%srch,$forcenewuser,
                    138:                                        'document.userpicker',$cancreate,$usertype);
                    139:     my $srchbutton = &mt('Search');
                    140: 
                    141:     my $headertext = &mt('Search for a user to add to course personnel');  
1.2       raeburn   142:     my $opener_elems = '<input type="hidden" name="form" value="'.
                    143:                        $env{'form.form'}.'" />'."\n".
                    144:                        '<input type="hidden" name="coursedom" value="'.
                    145:                        $env{'form.coursedom'}.'" />'."\n";
1.1       raeburn   146:     foreach my $item (keys(%toset)) {
                    147:         if (exists($env{'form.'.$toset{$item}})) {
                    148:             $opener_elems .= '<input type="hidden" name="'.$toset{$item}.'" value="'.$env{'form.'.$toset{$item}}.'" />'."\n";
                    149:         }
1.2       raeburn   150:     }
                    151:     unless ($env{'form.phase'} eq 'define_newuser') { 
                    152:         $r->print(<<"ENDBLOCK");
1.1       raeburn   153: <h3>$headertext</h3>
                    154: <form action="/adm/pickuser" method="post" name="userpicker">
                    155: $opener_elems
                    156: <input type="hidden" name="phase" value="get_user_info" />
                    157: $userpicker
                    158: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.userpicker)" />
                    159: </form>
                    160: ENDBLOCK
1.2       raeburn   161:     }
1.1       raeburn   162:     if ($env{'form.phase'} eq 'get_user_info') {
                    163:         $r->print('<hr /><div>'.&mt('Searching ...').'</div>');
                    164:         $r->rflush();
                    165:         my ($currstate,$response,$forcenewuser,$results) = 
                    166:             &Apache::loncreateuser::user_search_result($context,\%srch);
                    167:         if (($currstate eq 'select') || ($currstate eq 'modify')) {
                    168:             &Apache::loncreateuser::print_user_selection_page($r,$response,\%srch,$results,
                    169:                                                               \@search,$context,$opener_elems);
                    170:         } else {
                    171:             $r->print('<div>'.$response.'</div>');
1.2       raeburn   172:             if ($forcenewuser) {
                    173:                 if ($srch{'srchby'} eq 'uname' && 
                    174:                     $srch{'srchtype'} eq 'exact' && 
                    175:                     $srch{'srchin'} eq 'dom' && 
                    176:                     $srch{'srchdomain'} eq $env{'form.coursedom'}) {
                    177:                     if ($cancreate) {
                    178:                         $r->print("\n".'<div><form name="newuser" '.
                    179:                                   'method="post" action="/adm/pickuser">');
                    180:                         foreach my $item (keys(%srch)) {
                    181:                             $r->print('<input type="hidden" name="'.$item.'"'.
                    182:                                       ' value="'.$srch{$item}.'" />'."\n");
                    183:                         }
                    184:                         $r->print('
                    185: <input type="hidden" name="phase" value="define_newuser">
                    186: <input type="submit" name="forcenew" value="'.
                    187: &HTML::Entities::encode(&mt('Make new user "[_1]"',$srch{'srchterm'}),'<>&"').'" />'."\n".
                    188: $opener_elems.'
                    189: </form></div>');
                    190:                     } else {
                    191:                         my $helplink = 'javascript:helpMenu('."'display'".')';
1.3     ! raeburn   192:                         my %usertypetext = 
        !           193:                             &Apache::lonlocal::texthash(
        !           194:                               official   => 'You are not authorized to create new institutional users in this domain.',
        !           195:                               unofficial => 'You are not authorized to create new non-institutional users in this domain.',
1.2       raeburn   196:                         );
                    197:                         $r->print('<div class="LC_warning">'.
1.3     ! raeburn   198:                                   $usertypetext{$usertype}.' '.
1.2       raeburn   199:                                   &mt('Please contact the [_1]helpdesk[_2] for assistance.','<a href="'.$helplink.'">','</a>').'</div>');
                    200: 
                    201:                     }
                    202:                 }
                    203:             }
                    204:         }
                    205:     }
                    206:     if ($env{'form.phase'} eq 'define_newuser') {
                    207:         $r->print('<h3>'.&mt('Define new user').' '.$srch{'srchterm'}.':'.
                    208:                   $srch{'srchdomain'}.'</h3>'."\n".'<div>'.
                    209:                   '<form name="newuserinfo" method="post" action="/adm/pickuser">'."\n".
                    210:                   '<input type="hidden" name="phase" value="" />'."\n".
                    211:                   $opener_elems.
                    212:                   &Apache::lonhtmlcommon::start_pick_box()."\n".
                    213:                   &Apache::lonhtmlcommon::row_title(&mt('First Name')).
                    214:                   '<input type="text" name="newfirst" value="" size="15" />'.
                    215:                   &Apache::lonhtmlcommon::row_closure()."\n".
                    216:                   &Apache::lonhtmlcommon::row_title(&mt('Last Name')).
                    217:                   '<input type="text" name="newlast" value="" size="25" />'
                    218: .
                    219:                   &Apache::lonhtmlcommon::row_closure()."\n".
                    220:                   &Apache::lonhtmlcommon::row_title(&mt('E-mail Address')).
                    221:                   '<input type="text" name="newemail" value="" size="25" />'
                    222: .
                    223:                   &Apache::lonhtmlcommon::row_closure(1)."\n".
                    224:                   &Apache::lonhtmlcommon::end_pick_box());
                    225:         foreach my $item (keys(%srch)) {
                    226:             $r->print('<input type="hidden" name="'.$item.'"'.
                    227:                       ' value="'.$srch{$item}.'" />'."\n");
1.1       raeburn   228:         }
1.2       raeburn   229:         $r->print('<br /><input type="button" name="cancelnew" value="'.
                    230:                   &mt('Cancel').'" onclick="javascript:setphase('.
                    231:                   "'get_user_info'".');" />'."\n".
                    232:                   '<input type="button" name="definenew" value="'.
                    233:                   &mt('Save').'" onclick="javascript:setphase('.
                    234:                   "'store_newuser'".');" /></form></div>');
1.1       raeburn   235:     }
                    236:     $r->print(&Apache::loncommon::end_page());
                    237:     return OK;
                    238: }
                    239: 
                    240: sub gochoose_javascript {
                    241:     return <<"ENDGC"; 
                    242: <script type="text/javascript">
1.2       raeburn   243: // <![CDATA[
1.1       raeburn   244: function gochoose(uname,udom,ufirst,ulast,uemail) {
                    245:     opener.document.$env{'form.form'}.$env{'form.unameelement'}.value=uname;
                    246:     opener.document.$env{'form.form'}.$env{'form.ufirstelement'}.value=ufirst;
                    247:     opener.document.$env{'form.form'}.$env{'form.ulastelement'}.value=ulast;
                    248:     opener.document.$env{'form.form'}.$env{'form.uemailelement'}.value=uemail;
                    249: 
                    250:     var slct=opener.document.$env{'form.form'}.$env{'form.udomelement'};
                    251:     if (slct.options == undefined) {
                    252:         opener.document.$env{'form.form'}.$env{'form.udomelement'}.value=udom;
                    253:     }
                    254:     else {
                    255:         var i;
                    256:         for (i=0;i<slct.length;i++) {
                    257:             if (slct.options[i].value==udom) { slct.selectedIndex=i; }
                    258:         }
                    259:     }
                    260:     opener.document.$env{'form.form'}.$env{'form.hideudomelement'}.value=udom;
                    261:     self.close();
                    262: }
1.2       raeburn   263: // ]]>
1.1       raeburn   264: </script>
                    265: 
                    266: ENDGC
                    267: }
                    268: 
                    269: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>