File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.185: download - view: text, annotated - select for diffs
Wed Sep 12 23:42:37 2007 UTC (16 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- some style changes
- simplify population of instsrch hash
- homeserver check occurs earlier when modifying user

When adding a new user ...
- do username rule check, and early out if format matched but no instiutional user.
- when adding a new user for whom there is institutional data, only provide textboxes for fields without data.
- css for data entry table
- authentication method/arg set automatically for usernames matching rules

    1: # The LearningOnline Network with CAPA
    2: # Create a user
    3: #
    4: # $Id: loncreateuser.pm,v 1.185 2007/09/12 23:42:37 raeburn Exp $
    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: package Apache::loncreateuser;
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::loncreateuser - handler to create users and custom roles
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Apache::loncreateuser provides an Apache handler for creating users,
   41:     editing their login parameters, roles, and removing roles, and
   42:     also creating and assigning custom roles.
   43: 
   44: =head1 OVERVIEW
   45: 
   46: =head2 Custom Roles
   47: 
   48: In LON-CAPA, roles are actually collections of privileges. "Teaching
   49: Assistant", "Course Coordinator", and other such roles are really just
   50: collection of privileges that are useful in many circumstances.
   51: 
   52: Creating custom roles can be done by the Domain Coordinator through
   53: the Create User functionality. That screen will show all privileges
   54: that can be assigned to users. For a complete list of privileges,
   55: please see C</home/httpd/lonTabs/rolesplain.tab>.
   56: 
   57: Custom role definitions are stored in the C<roles.db> file of the role
   58: author.
   59: 
   60: =cut
   61: 
   62: use strict;
   63: use Apache::Constants qw(:common :http);
   64: use Apache::lonnet;
   65: use Apache::loncommon;
   66: use Apache::lonlocal;
   67: use Apache::longroup;
   68: use LONCAPA qw(:DEFAULT :match);
   69: 
   70: my $loginscript; # piece of javascript used in two separate instances
   71: my $generalrule;
   72: my $authformnop;
   73: my $authformkrb;
   74: my $authformint;
   75: my $authformfsys;
   76: my $authformloc;
   77: 
   78: sub initialize_authen_forms {
   79:     my ($krbdefdom)=( $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/);
   80:     $krbdefdom= uc($krbdefdom);
   81:     my %param = ( formname => 'document.cu',
   82:                   kerb_def_dom => $krbdefdom 
   83:                   );
   84: # no longer static due to configurable kerberos defaults
   85: #    $loginscript  = &Apache::loncommon::authform_header(%param);
   86:     $generalrule  = &Apache::loncommon::authform_authorwarning(%param);
   87:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
   88: # no longer static due to configurable kerberos defaults
   89: #    $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
   90:     $authformint  = &Apache::loncommon::authform_internal(%param);
   91:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
   92:     $authformloc  = &Apache::loncommon::authform_local(%param);
   93: }
   94: 
   95: 
   96: # ======================================================= Existing Custom Roles
   97: 
   98: sub my_custom_roles {
   99:     my %returnhash=();
  100:     my %rolehash=&Apache::lonnet::dump('roles');
  101:     foreach my $key (keys %rolehash) {
  102: 	if ($key=~/^rolesdef\_(\w+)$/) {
  103: 	    $returnhash{$1}=$1;
  104: 	}
  105:     }
  106:     return %returnhash;
  107: }
  108: 
  109: # ==================================================== Figure out author access
  110: 
  111: sub authorpriv {
  112:     my ($auname,$audom)=@_;
  113:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
  114:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }
  115:     return 1;
  116: }
  117: 
  118: # ====================================================
  119: 
  120: sub portfolio_quota {
  121:     my ($ccuname,$ccdomain) = @_;
  122:     my %lt = &Apache::lonlocal::texthash(
  123:                    'disk' => "Disk space allocated to user's portfolio files",
  124:                    'cuqu' => "Current quota",
  125:                    'cust' => "Custom quota",
  126:                    'defa' => "Default",
  127:                    'chqu' => "Change quota",
  128:     );
  129:     my ($currquota,$quotatype,$inststatus,$defquota) = 
  130:         &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
  131:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
  132:     my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
  133:     if ($inststatus ne '') {
  134:         if ($usertypes->{$inststatus} ne '') {
  135:             $longinsttype = $usertypes->{$inststatus};
  136:         }
  137:     }
  138:     $custom_on = ' ';
  139:     $custom_off = ' checked="checked" ';
  140:     my $quota_javascript = <<"END_SCRIPT";
  141: <script type="text/javascript">
  142: function quota_changes(caller) {
  143:     if (caller == "custom") {
  144:         if (document.cu.customquota[0].checked) {
  145:             document.cu.portfolioquota.value = "";
  146:         }
  147:     }
  148:     if (caller == "quota") {
  149:         document.cu.customquota[1].checked = true;
  150:     }
  151: }
  152: </script>
  153: END_SCRIPT
  154:     if ($quotatype eq 'custom') {
  155:         $custom_on = $custom_off;
  156:         $custom_off = ' ';
  157:         $showquota = $currquota;
  158:         if ($longinsttype eq '') {
  159:             $defaultinfo = &mt('For this user, the default quota would be [_1]
  160:                             Mb.',$defquota);
  161:         } else {
  162:             $defaultinfo = &mt("For this user, the default quota would be [_1] 
  163:                             Mb, as determined by the user's institutional
  164:                            affiliation ([_2]).",$defquota,$longinsttype);
  165:         }
  166:     } else {
  167:         if ($longinsttype eq '') {
  168:             $defaultinfo = &mt('For this user, the default quota is [_1]
  169:                             Mb.',$defquota);
  170:         } else {
  171:             $defaultinfo = &mt("For this user, the default quota of [_1]
  172:                             Mb, is determined by the user's institutional
  173:                             affiliation ([_2]).",$defquota,$longinsttype);
  174:         }
  175:     }
  176:     my $output = $quota_javascript.
  177:                  '<h3>'.$lt{'disk'}.'</h3>'.
  178:                  $lt{'cuqu'}.': '.$currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
  179:                  $defaultinfo.'<br /><span class="LC_nobreak">'.$lt{'chqu'}.
  180:                  ': <label>'.
  181:                  '<input type="radio" name="customquota" value="0" '.
  182:                  $custom_off.' onchange="javascript:quota_changes('."'custom'".')"
  183:                   />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
  184:                  '&nbsp;<label><input type="radio" name="customquota" value="1" '. 
  185:                  $custom_on.'  onchange="javascript:quota_changes('."'custom'".')" />'.
  186:                  $lt{'cust'}.':</label>&nbsp;'.
  187:                  '<input type="text" name="portfolioquota" size ="5" value="'.
  188:                  $showquota.'" onfocus="javascript:quota_changes('."'quota'".')" '.
  189:                  '/>&nbsp;Mb';
  190:     return $output;
  191: }
  192: 
  193: # =================================================================== Phase one
  194: 
  195: sub print_username_entry_form {
  196:     my ($r,$response,$srch,$forcenewuser) = @_;
  197:     my $defdom=$env{'request.role.domain'};
  198:     my $formtoset = 'crtuser';
  199:     if (exists($env{'form.startrolename'})) {
  200:         $formtoset = 'docustom';
  201:         $env{'form.rolename'} = $env{'form.startrolename'};
  202:     }
  203: 
  204:     my ($jsback,$elements) = &crumb_utilities();
  205: 
  206:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
  207:         '<script type="text/javascript">'."\n".
  208:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset}).
  209:         '</script>'."\n";
  210: 
  211:     my %loaditems = (
  212:                 'onload' => "javascript:setFormElements(document.$formtoset)",
  213:                     );
  214:     my $start_page =
  215: 	&Apache::loncommon::start_page('Create Users, Change User Privileges',
  216: 				       $jscript,{'add_entries' => \%loaditems,});
  217:    &Apache::lonhtmlcommon::add_breadcrumb
  218:      ({href=>"javascript:backPage(document.crtuser)",
  219:        text=>"User modify/custom role edit",
  220:        faq=>282,bug=>'Instructor Interface',});
  221: 
  222:     my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management');
  223:     my %existingroles=&my_custom_roles();
  224:     my $choice=&Apache::loncommon::select_form('make new role','rolename',
  225: 		('make new role' => 'Generate new role ...',%existingroles));
  226:     my %lt=&Apache::lonlocal::texthash(
  227:                     'srch' => "User Search",
  228:                      or    => "or",
  229: 		    'siur' => "Set Individual User Roles",
  230: 		    'usr'  => "Username",
  231:                     'dom'  => "Domain",
  232:                     'ecrp' => "Edit Custom Role Privileges",
  233:                     'nr'   => "Name of Role",
  234:                     'cre'  => "Custom Role Editor",
  235:                     'mod'  => "to add/modify roles",
  236: 				       );
  237:     my $help = &Apache::loncommon::help_open_menu(undef,undef,282,'Instructor Interface');
  238:     my $helpsiur=&Apache::loncommon::help_open_topic('Course_Change_Privileges');
  239:     my $helpecpr=&Apache::loncommon::help_open_topic('Course_Editing_Custom_Roles');
  240:     my $sellink=&Apache::loncommon::selectstudent_link('crtuser','srchterm','srchdomain');
  241:     if ($sellink) {
  242:         $sellink = "$lt{'or'} ".$sellink;
  243:     } 
  244:     $r->print("
  245: $start_page
  246: $crumbs
  247: <h2>$lt{siur}$helpsiur</h2>
  248: <h3>$lt{'srch'} $sellink $lt{'mod'}</h3>
  249: $response");
  250:     $r->print(&entry_form($defdom,$srch,$forcenewuser));
  251:     if (&Apache::lonnet::allowed('mcr','/')) {
  252:         $r->print(<<ENDCUSTOM);
  253: <form action="/adm/createuser" method="post" name="docustom">
  254: <input type="hidden" name="phase" value="selected_custom_edit" />
  255: <h2>$lt{'ecrp'}$helpecpr</h2>
  256: $lt{'nr'}: $choice <input type="text" size="15" name="newrolename" /><br />
  257: <input name="customeditor" type="submit" value="$lt{'cre'}" />
  258: </form>
  259: ENDCUSTOM
  260:     }
  261:     $r->print(&Apache::loncommon::end_page());
  262: }
  263: 
  264: sub entry_form {
  265:     my ($dom,$srch,$forcenewuser) = @_;
  266:     my $userpicker = 
  267:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
  268:                                        'document.crtuser');
  269:     my $srchbutton = &mt('Search');
  270:     my $output = <<"ENDDOCUMENT";
  271: <form action="/adm/createuser" method="post" name="crtuser">
  272: <input type="hidden" name="phase" value="get_user_info" />
  273: $userpicker
  274: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
  275: </form>
  276: ENDDOCUMENT
  277:     return $output;
  278: }
  279: 
  280: sub user_modification_js {
  281:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
  282:     
  283:     return <<END;
  284: <script type="text/javascript" language="Javascript">
  285: 
  286:     function pclose() {
  287:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  288:                  "height=350,width=350,scrollbars=no,menubar=no");
  289:         parmwin.close();
  290:     }
  291: 
  292:     $pjump_def
  293:     $dc_setcourse_code
  294: 
  295:     function dateset() {
  296:         eval("document.cu."+document.cu.pres_marker.value+
  297:             ".value=document.cu.pres_value.value");
  298:         pclose();
  299:     }
  300: 
  301:     $nondc_setsection_code
  302: 
  303: </script>
  304: END
  305: }
  306: 
  307: # =================================================================== Phase two
  308: sub print_user_selection_page {
  309:     my ($r,$response,$srch,$srch_results,$context,$srcharray) = @_;
  310:     my @fields = ('username','domain','lastname','firstname','permanentemail');
  311:     my $sortby = $env{'form.sortby'};
  312: 
  313:     if (!grep(/^\Q$sortby\E$/,@fields)) {
  314:         $sortby = 'lastname';
  315:     }
  316: 
  317:     my ($jsback,$elements) = &crumb_utilities();
  318: 
  319:     my $jscript = (<<ENDSCRIPT);
  320: <script type="text/javascript">
  321: function pickuser(uname,udom) {
  322:     document.usersrchform.seluname.value=uname;
  323:     document.usersrchform.seludom.value=udom;
  324:     document.usersrchform.phase.value="userpicked";
  325:     document.usersrchform.submit();
  326: }
  327: 
  328: $jsback
  329: </script>
  330: ENDSCRIPT
  331: 
  332:     my %lt=&Apache::lonlocal::texthash(
  333:                                        'usrch'          => "User Search to add/modify roles",
  334:                                        'stusrch'        => "User Search to enroll student",
  335:                                        'usel'           => "Select a user to add/modify roles",
  336:                                        'stusel'         => "Select a user to enroll as a student", 
  337:                                        'username'       => "username",
  338:                                        'domain'         => "domain",
  339:                                        'lastname'       => "last name",
  340:                                        'firstname'      => "first name",
  341:                                        'permanentemail' => "permanent e-mail",
  342:                                       );
  343:     if ($context eq 'createuser') {
  344:         $r->print(&Apache::loncommon::start_page('Create Users, Change User Privileges',$jscript));
  345:         &Apache::lonhtmlcommon::add_breadcrumb
  346:             ({href=>"javascript:backPage(document.usersrchform,'','')",
  347:               text=>"User modify/custom role edit",
  348:               faq=>282,bug=>'Instructor Interface',},
  349:              {href=>"javascript:backPage(document.usersrchform,'get_user_info','select')",
  350:               text=>"Select User",
  351:               faq=>282,bug=>'Instructor Interface',});
  352:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
  353:         $r->print("<b>$lt{'usrch'}</b><br />");
  354:         $r->print(&entry_form($srch->{'srchdomain'},$srch));
  355:         $r->print('<h3>'.$lt{'usel'}.'</h3>');
  356:     } else {
  357:         $r->print($jscript."<b>$lt{'stusrch'}</b><br />");
  358:         $r->print(&Apache::londropadd::single_user_entry_form($srch->{'srchdomain'},$srch));
  359:         $r->print('</form><h3>'.$lt{'stusel'}.'</h3>');
  360:     }
  361:     $r->print('<form name="usersrchform" method="post">'.
  362:               &Apache::loncommon::start_data_table()."\n".
  363:               &Apache::loncommon::start_data_table_header_row()."\n".
  364:               ' <th> </th>'."\n");
  365:     foreach my $field (@fields) {
  366:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
  367:                   "'".$field."'".';document.usersrchform.submit();">'.
  368:                   $lt{$field}.'</a></th>'."\n");
  369:     }
  370:     $r->print(&Apache::loncommon::end_data_table_header_row());
  371: 
  372:     my @sorted_users = sort {
  373:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
  374:             ||
  375:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
  376:             ||
  377:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
  378: 	    ||
  379: 	lc($a) cmp lc($b)
  380:         } (keys(%$srch_results));
  381: 
  382:     foreach my $user (@sorted_users) {
  383:         my ($uname,$udom) = split(/:/,$user);
  384:         $r->print(&Apache::loncommon::start_data_table_row().
  385:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".')" /></td>'.
  386:                   '<td><tt>'.$uname.'</tt></td>'.
  387:                   '<td><tt>'.$udom.'</tt></td>');
  388:         foreach my $field ('lastname','firstname','permanentemail') {
  389:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
  390:         }
  391:         $r->print(&Apache::loncommon::end_data_table_row());
  392:     }
  393:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
  394:     if (ref($srcharray) eq 'ARRAY') {
  395:         foreach my $item (@{$srcharray}) {
  396:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
  397:         }
  398:     }
  399:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
  400:               ' <input type="hidden" name="seluname" value="" />'."\n".
  401:               ' <input type="hidden" name="seludom" value="" />'."\n".
  402:               ' <input type="hidden" name="currstate" value="select" />'."\n".
  403:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n");
  404:     $r->print($response);
  405:     if ($context eq 'createuser') {
  406:         $r->print('</form>'.&Apache::loncommon::end_page());
  407:     } else {
  408:         $r->print('<input type="hidden" name="action" value="enrollstudent" />'."\n".
  409:                   '<input type="hidden" name="state" value="gotusername" />'."\n");
  410:     }
  411: }
  412: 
  413: sub print_user_query_page {
  414:     my ($r,$caller) = @_;
  415: # FIXME - this is for a network-wide name search (similar to catalog search)
  416: # To use frames with similar behavior to catalog/portfolio search.
  417: # To be implemented. 
  418:     return;
  419: }
  420: 
  421: sub print_user_modification_page {
  422:     my ($r,$ccuname,$ccdomain,$srch,$response) = @_;
  423:     if (($ccuname eq '') || ($ccdomain eq '')) {
  424:         my $usermsg = &mt('No username and/or domain provided.'); 
  425: 	&print_username_entry_form($r,$usermsg);
  426:         return;
  427:     }
  428:     my ($instsrch,$rulematch,$rules,%inst_results);
  429:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
  430:     if ($uhome eq 'no_host') {
  431:         $instsrch =
  432:                       {
  433:                          srchin => 'instd',
  434:                          srchby => 'uname',
  435:                          srchtype => 'exact',
  436:                          srchterm => $ccuname,
  437:                          srchdomain => $ccdomain,
  438:                        };
  439:         (my $usercheckmsg,$rulematch,$rules,%inst_results) = 
  440:             &Apache::loncommon::username_rule_check($instsrch,'new');
  441:         if ($usercheckmsg) {
  442:             &print_username_entry_form($r,$usercheckmsg);
  443:             return;
  444:         }
  445:     }
  446:     if ($response) {
  447:         $response = '<br />'.$response
  448:     }
  449:     my $defdom=$env{'request.role.domain'};
  450: 
  451:     my ($krbdef,$krbdefdom) =
  452:        &Apache::loncommon::get_kerberos_defaults($defdom);
  453: 
  454:     my %param = ( formname => 'document.cu',
  455:                   kerb_def_dom => $krbdefdom,
  456:                   kerb_def_auth => $krbdef
  457:                 );
  458:     $loginscript  = &Apache::loncommon::authform_header(%param);
  459:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
  460: 
  461:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  462:     my $dc_setcourse_code = '';
  463:     my $nondc_setsection_code = '';                                        
  464: 
  465:     my %loaditem;
  466: 
  467:     my $groupslist;
  468:     my %curr_groups = &Apache::longroup::coursegroups();
  469:     if (%curr_groups) {
  470:         $groupslist = join('","',sort(keys(%curr_groups)));
  471:         $groupslist = '"'.$groupslist.'"';   
  472:     }
  473: 
  474:     if ($env{'request.role'} =~ m-^dc\./($match_domain)/$-) {
  475:         my $dcdom = $1;
  476:         $loaditem{'onload'} = "document.cu.coursedesc.value='';";
  477:         my @rolevals = ('st','ta','ep','in','cc');
  478:         my (@crsroles,@grproles);
  479:         for (my $i=0; $i<@rolevals; $i++) {
  480:             $crsroles[$i]=&Apache::lonnet::plaintext($rolevals[$i],'Course');
  481:             $grproles[$i]=&Apache::lonnet::plaintext($rolevals[$i],'Group');
  482:         }
  483:         my $rolevalslist = join('","',@rolevals);
  484:         my $crsrolenameslist = join('","',@crsroles);
  485:         my $grprolenameslist = join('","',@grproles);
  486:         my $pickcrsfirst = '<--'.&mt('Pick course first');
  487:         my $pickgrpfirst = '<--'.&mt('Pick group first'); 
  488:         $dc_setcourse_code = <<"ENDSCRIPT";
  489:     function setCourse() {
  490:         var course = document.cu.dccourse.value;
  491:         if (course != "") {
  492:             if (document.cu.dcdomain.value != document.cu.origdom.value) {
  493:                 alert("You must select a course in the current domain");
  494:                 return;
  495:             } 
  496:             var userrole = document.cu.role.options[document.cu.role.selectedIndex].value
  497:             var section="";
  498:             var numsections = 0;
  499:             var newsecs = new Array();
  500:             for (var i=0; i<document.cu.currsec.length; i++) {
  501:                 if (document.cu.currsec.options[i].selected == true ) {
  502:                     if (document.cu.currsec.options[i].value != "" && document.cu.currsec.options[i].value != null) { 
  503:                         if (numsections == 0) {
  504:                             section = document.cu.currsec.options[i].value
  505:                             numsections = 1;
  506:                         }
  507:                         else {
  508:                             section = section + "," +  document.cu.currsec.options[i].value
  509:                             numsections ++;
  510:                         }
  511:                     }
  512:                 }
  513:             }
  514:             if (document.cu.newsec.value != "" && document.cu.newsec.value != null) {
  515:                 if (numsections == 0) {
  516:                     section = document.cu.newsec.value
  517:                 }
  518:                 else {
  519:                     section = section + "," +  document.cu.newsec.value
  520:                 }
  521:                 newsecs = document.cu.newsec.value.split(/,/g);
  522:                 numsections = numsections + newsecs.length;
  523:             }
  524:             if ((userrole == 'st') && (numsections > 1)) {
  525:                 alert("In each course, each user may only have one student role at a time. You had selected "+numsections+" sections.\\nPlease modify your selections so they include no more than one section.")
  526:                 return;
  527:             }
  528:             for (var j=0; j<newsecs.length; j++) {
  529:                 if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
  530:                     alert("'"+newsecs[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
  531:                     return;
  532:                 }
  533:                 if (document.cu.groups.value != '') {
  534:                     var groups = document.cu.groups.value.split(/,/g);
  535:                     for (var k=0; k<groups.length; k++) {
  536:                         if (newsecs[j] == groups[k]) {
  537:                             alert("'"+newsecs[j]+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
  538:                             return; 
  539:                         }
  540:                     }
  541:                 }
  542:             }
  543:             if ((userrole == 'cc') && (numsections > 0)) {
  544:                 alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
  545:                 section = "";
  546:             }
  547:             var coursename = "_$dcdom"+"_"+course+"_"+userrole
  548:             var numcourse = getIndex(document.cu.dccourse);
  549:             if (numcourse == "-1") {
  550:                 alert("There was a problem with your course selection");
  551:                 return
  552:             }
  553:             else {
  554:                 document.cu.elements[numcourse].name = "act"+coursename;
  555:                 var numnewsec = getIndex(document.cu.newsec);
  556:                 if (numnewsec != "-1") {
  557:                     document.cu.elements[numnewsec].name = "sec"+coursename;
  558:                     document.cu.elements[numnewsec].value = section;
  559:                 }
  560:                 var numstart = getIndex(document.cu.start);
  561:                 if (numstart != "-1") {
  562:                     document.cu.elements[numstart].name = "start"+coursename;
  563:                 }
  564:                 var numend = getIndex(document.cu.end);
  565:                 if (numend != "-1") {
  566:                     document.cu.elements[numend].name = "end"+coursename
  567:                 }
  568:             }
  569:         }
  570:         document.cu.submit();
  571:     }
  572: 
  573:     function getIndex(caller) {
  574:         for (var i=0;i<document.cu.elements.length;i++) {
  575:             if (document.cu.elements[i] == caller) {
  576:                 return i;
  577:             }
  578:         }
  579:         return -1;
  580:     }
  581: ENDSCRIPT
  582:     } else {
  583:         $nondc_setsection_code = <<"ENDSECCODE";
  584:     function setSections() {
  585:         var re1 = /^currsec_/;
  586:         var groups = new Array($groupslist);
  587:         for (var i=0;i<document.cu.elements.length;i++) {
  588:             var str = document.cu.elements[i].name;
  589:             var checkcurr = str.match(re1);
  590:             if (checkcurr != null) {
  591:                 if (document.cu.elements[i-1].checked == true) {
  592: 		    var match = str.split('_');
  593:                     var role = match[3];
  594:                     if (role == 'cc') {
  595:                         alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
  596:                     }
  597:                     else {
  598:                         var sections = '';
  599:                         var numsec = 0;
  600:                         var sections;
  601:                         for (var j=0; j<document.cu.elements[i].length; j++) {
  602:                             if (document.cu.elements[i].options[j].selected == true ) {
  603:                                 if (document.cu.elements[i].options[j].value != "") {
  604:                                     if (numsec == 0) {
  605:                                         if (document.cu.elements[i].options[j].value != "") {
  606:                                             sections = document.cu.elements[i].options[j].value;
  607:                                             numsec ++;
  608:                                         }
  609:                                     }
  610:                                     else {
  611:                                         sections = sections + "," +  document.cu.elements[i].options[j].value
  612:                                         numsec ++;
  613:                                     }
  614:                                 }
  615:                             }
  616:                         }
  617:                         if (numsec > 0) {
  618:                             if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) {
  619:                                 sections = sections + "," +  document.cu.elements[i+1].value;
  620:                             }
  621:                         }
  622:                         else {
  623:                             sections = document.cu.elements[i+1].value;
  624:                         }
  625:                         var newsecs = document.cu.elements[i+1].value;
  626: 			var numsplit;
  627:                         if (newsecs != null && newsecs != "") {
  628:                             numsplit = newsecs.split(/,/g);
  629:                             numsec = numsec + numsplit.length;
  630:                         }
  631: 
  632:                         if ((role == 'st') && (numsec > 1)) {
  633:                             alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.")
  634:                             return;
  635:                         }
  636:                         else if (numsplit != null) {
  637:                             for (var j=0; j<numsplit.length; j++) {
  638:                                 if ((numsplit[j] == 'all') ||
  639:                                     (numsplit[j] == 'none')) {
  640:                                     alert("'"+numsplit[j]+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
  641:                                     return;
  642:                                 }
  643:                                 for (var k=0; k<groups.length; k++) {
  644:                                     if (numsplit[j] == groups[k]) {
  645:                                         alert("'"+numsplit[j]+"' may not be used as a section name, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
  646:                                         return;
  647:                                     }
  648:                                 }
  649:                             }
  650:                         }
  651:                         document.cu.elements[i+2].value = sections;
  652:                     }
  653:                 }
  654:             }
  655:         }
  656:         document.cu.submit();
  657:     }
  658: ENDSECCODE
  659:     }
  660:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
  661:                                    $nondc_setsection_code,$groupslist);
  662: 
  663:     my ($jsback,$elements) = &crumb_utilities();
  664: 
  665:     $js .= "\n".
  666:            '<script type="text/javascript">'."\n".$jsback."\n".'</script>';
  667: 
  668:     my $start_page = 
  669: 	&Apache::loncommon::start_page('Create Users, Change User Privileges',
  670: 				       $js,{'add_entries' => \%loaditem,});
  671:     &Apache::lonhtmlcommon::add_breadcrumb
  672:      ({href=>"javascript:backPage(document.cu)",
  673:        text=>"User modify/custom role edit",
  674:        faq=>282,bug=>'Instructor Interface',});
  675: 
  676:     if ($env{'form.phase'} eq 'userpicked') {
  677:         &Apache::lonhtmlcommon::add_breadcrumb
  678:      ({href=>"javascript:backPage(document.cu,'get_user_info','select')",
  679:        text=>"Select a user",
  680:        faq=>282,bug=>'Instructor Interface',});
  681:     }
  682:     &Apache::lonhtmlcommon::add_breadcrumb
  683:       ({href=>"javascript:backPage(document.cu,'$env{'form.phase'}','modify')",
  684:         text=>"Set user role",
  685:         faq=>282,bug=>'Instructor Interface',});
  686:     my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management');
  687: 
  688:     my $forminfo =<<"ENDFORMINFO";
  689: <form action="/adm/createuser" method="post" name="cu">
  690: <input type="hidden" name="phase"       value="update_user_data" />
  691: <input type="hidden" name="ccuname"     value="$ccuname" />
  692: <input type="hidden" name="ccdomain"    value="$ccdomain" />
  693: <input type="hidden" name="pres_value"  value="" />
  694: <input type="hidden" name="pres_type"   value="" />
  695: <input type="hidden" name="pres_marker" value="" />
  696: ENDFORMINFO
  697:     my %inccourses;
  698:     foreach my $key (keys(%env)) {
  699: 	if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
  700: 	    $inccourses{$1.'_'.$2}=1;
  701:         }
  702:     }
  703:     if ($uhome eq 'no_host') {
  704:         my $home_server_list=
  705:             '<option value="default" selected>default</option>'."\n".
  706:                 &Apache::loncommon::home_server_option_list($ccdomain);
  707:         
  708: 	my %lt=&Apache::lonlocal::texthash(
  709:                     'cnu'            => "Create New User",
  710:                     'nu'             => "New User",
  711:                     'ind'            => "in domain",
  712:                     'pd'             => "Personal Data",
  713:                     'firstname'      => "First Name",
  714:                     'middlename'     => "Middle Name",
  715:                     'lastname'       => "Last Name",
  716:                     'generation'     => "Generation",
  717:                     'permanentemail' => "Permanent e-mail address",
  718:                     'id'             => "ID/Student Number",
  719:                     'hs'             => "Home Server",
  720:                     'lg'             => "Login Data"
  721: 				       );
  722:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
  723:         my %textboxsize = (
  724:                            firstname      => '15',
  725:                            middlename     => '15',
  726:                            lastname       => '15',
  727:                            generation     => '5',
  728:                            permanentemail => '25',
  729:                            id             => '15',
  730:                           );
  731:         my %element   = (
  732:                            firstname      => 'cfirst', 
  733:                            middlename     => 'cmiddle',
  734:                            lastname       => 'clast',
  735:                            generation     => 'cgen',
  736:                            permanentemail => 'cemail',
  737:                            id             => 'cstid',
  738:                           );
  739:         my $portfolioform;
  740:         if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
  741:             # Current user has quota modification privileges
  742:             $portfolioform = &portfolio_quota($ccuname,$ccdomain);
  743:         }
  744: 	my $genhelp=&Apache::loncommon::help_open_topic('Generation');
  745:         &initialize_authen_forms();
  746: 	$r->print(<<ENDTITLE);
  747: $start_page
  748: $crumbs
  749: <h1>$lt{'cnu'}</h1>
  750: $response
  751: $forminfo
  752: <h2>$lt{'nu'} "$ccuname" $lt{'ind'} $ccdomain</h2>
  753: <script type="text/javascript" language="Javascript">
  754: $loginscript
  755: </script>
  756: <input type='hidden' name='makeuser' value='1' />
  757: <h3>$lt{'pd'}</h3>
  758: <p>
  759: ENDTITLE
  760:         $r->print(&Apache::lonhtmlcommon::start_pick_box());
  761:         foreach my $item (@userinfo) {
  762:             my $rowtitle = $lt{$item};
  763:             if ($item eq 'generation') {
  764:                 $rowtitle = $genhelp.$rowtitle;
  765:             }
  766:             $r->print(&Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n");
  767:             if ($inst_results{$item} ne '') {
  768:                 $r->print('<input type="hidden" name="'.$element{$item}.'" value="'.$inst_results{$item}.'" />'.$inst_results{$item});
  769:             } else {
  770:                 $r->print('<input type="text" name="'.$element{$item}.'" size="'.$textboxsize{$item}.'" value="" />');
  771:             }
  772:             $r->print(&Apache::lonhtmlcommon::row_closure(1));
  773:         }
  774:         $r->print(&Apache::lonhtmlcommon::end_pick_box());
  775:         $r->print(<<ENDNEWUSER);
  776: <br />
  777: $lt{'hs'}: <select name="hserver" size="1"> $home_server_list </select>
  778: <br />
  779: <hr />
  780: <h3>$lt{'lg'}</h3>
  781: ENDNEWUSER
  782:         my ($fixedauth,$varauth,$authmsg); 
  783:         if ($rulematch) {
  784:             if (ref($rules) eq 'HASH') {
  785:                 if (ref($rules->{$rulematch}) eq 'HASH') {
  786:                     my $authtype = $rules->{$rulematch}{'authtype'};
  787:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
  788:                         $r->print(&set_login());
  789:                     } else { 
  790:                         my $authparm = $rules->{$rulematch}{'authparm'};
  791:                         if ($authtype =~ /^krb(4|5)$/) {
  792:                             my $ver = $1;
  793:                             if ($authparm ne '') {
  794:                                 $fixedauth = <<"KERB"; 
  795: <input type="hidden" name="login" value="krb" />
  796: <input type="hidden" name="krbver" value="$ver" />
  797: <input type="hidden" name="krbarg" value="$authparm" />
  798: KERB
  799:                                 $authmsg = $rules->{$rulematch}{'authmsg'};    
  800:                             }
  801:                         } else {
  802:                             $fixedauth = 
  803: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
  804:                             if ($rules->{$rulematch}{'authparmfixed'}) {
  805:                                 $fixedauth .=    
  806: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
  807:                             } else {
  808:                                 $varauth =  
  809: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
  810:                             }
  811:                         }
  812:                     }
  813:                 } else {
  814:                     $r->print(&set_login());
  815:                 }
  816:             }
  817:             if ($authmsg) {
  818:                 $r->print(<<ENDAUTH);
  819: $fixedauth
  820: $authmsg
  821: $varauth
  822: ENDAUTH
  823:             }
  824:         } else {
  825:             $r->print(&set_login()); 
  826:          }
  827:          $r->print(<<ENDPORT);
  828: <hr />
  829: $portfolioform
  830: ENDPORT
  831:     } else { # user already exists
  832: 	my %lt=&Apache::lonlocal::texthash(
  833:                     'cup'  => "Change User Privileges",
  834:                     'usr'  => "User",                    
  835:                     'id'   => "in domain",
  836:                     'fn'   => "first name",
  837:                     'mn'   => "middle name",
  838:                     'ln'   => "last name",
  839:                     'gen'  => "generation",
  840:                     'email' => "permanent e-mail",
  841: 				       );
  842: 	$r->print(<<ENDCHANGEUSER);
  843: $start_page
  844: $crumbs
  845: <h1>$lt{'cup'}</h1>
  846: $forminfo
  847: <h2>$lt{'usr'} "$ccuname" $lt{'id'} "$ccdomain"</h2>
  848: ENDCHANGEUSER
  849:         # Get the users information
  850:         my %userenv = 
  851:             &Apache::lonnet::get('environment',
  852:                 ['firstname','middlename','lastname','generation',
  853:                  'permanentemail','portfolioquota'],$ccdomain,$ccuname);
  854:         my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
  855:         $r->print('
  856: <hr />'.
  857:                   &Apache::loncommon::start_data_table().
  858:                   &Apache::loncommon::start_data_table_header_row().
  859: '<th>'.$lt{'fn'}.'</th><th>'.$lt{'mn'}.'</th><th>'.$lt{'ln'}.'</th><th>'.$lt{'gen'}.'</th><th>'.$lt{'email'}.'</th>'.
  860:                   &Apache::loncommon::end_data_table_header_row().
  861:                   &Apache::loncommon::start_data_table_row());
  862:         foreach my $item ('firstname','middlename','lastname','generation','permanentemail') {
  863:            if (&Apache::lonnet::allowed('mau',$ccdomain)) {
  864:               $r->print(<<"END");
  865: <td><input type="text" name="c$item" value="$userenv{$item}" size="15" /></td>
  866: END
  867:            } else {
  868:                $r->print('<td>'.$userenv{$item}.'</td>');
  869:            }
  870:         }
  871:         $r->print(&Apache::loncommon::end_data_table_row().
  872:                   &Apache::loncommon::end_data_table());
  873:         # Build up table of user roles to allow revocation of a role.
  874:         my ($tmp) = keys(%rolesdump);
  875:         unless ($tmp =~ /^(con_lost|error)/i) {
  876:            my $now=time;
  877: 	   my %lt=&Apache::lonlocal::texthash(
  878: 		    'rer'  => "Revoke Existing Roles",
  879:                     'rev'  => "Revoke",                    
  880:                     'del'  => "Delete",
  881: 		    'ren'  => "Re-Enable",
  882:                     'rol'  => "Role",
  883:                     'ext'  => "Extent",
  884:                     'sta'  => "Start",
  885:                     'end'  => "End"
  886: 				       );
  887:            my (%roletext,%sortrole,%roleclass,%rolepriv);
  888: 	   foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
  889: 				    my $b1=join('_',(split('_',$b))[1,0]);
  890: 				    return $a1 cmp $b1;
  891: 				} keys(%rolesdump)) {
  892:                next if ($area =~ /^rolesdef/);
  893: 	       my $envkey=$area;
  894:                my $role = $rolesdump{$area};
  895:                my $thisrole=$area;
  896:                $area =~ s/\_\w\w$//;
  897:                my ($role_code,$role_end_time,$role_start_time) = 
  898:                    split(/_/,$role);
  899: # Is this a custom role? Get role owner and title.
  900: 	       my ($croleudom,$croleuname,$croletitle)=
  901: 	           ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
  902:                my $allowed=0;
  903:                my $delallowed=0;
  904: 	       my $sortkey=$role_code;
  905: 	       my $class='Unknown';
  906:                if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
  907: 		   $class='Course';
  908:                    my ($coursedom,$coursedir) = ($1,$2);
  909: 		   $sortkey.="\0$coursedom";
  910:                    # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
  911:                    my %coursedata=
  912:                        &Apache::lonnet::coursedescription($1.'_'.$2);
  913: 		   my $carea;
  914: 		   if (defined($coursedata{'description'})) {
  915: 		       $carea=$coursedata{'description'}.
  916:                            '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
  917:      &Apache::loncommon::syllabuswrapper('Syllabus',$coursedir,$coursedom);
  918: 		       $sortkey.="\0".$coursedata{'description'};
  919:                        $class=$coursedata{'type'};
  920: 		   } else {
  921: 		       $carea=&mt('Unavailable course').': '.$area;
  922: 		       $sortkey.="\0".&mt('Unavailable course').': '.$area;
  923: 		   }
  924: 		   $sortkey.="\0$coursedir";
  925:                    $inccourses{$1.'_'.$2}=1;
  926:                    if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
  927:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  928:                        $allowed=1;
  929:                    }
  930:                    if ((&Apache::lonnet::allowed('dro',$1)) ||
  931:                        (&Apache::lonnet::allowed('dro',$ccdomain))) {
  932:                        $delallowed=1;
  933:                    }
  934: # - custom role. Needs more info, too
  935: 		   if ($croletitle) {
  936: 		       if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
  937: 			   $allowed=1;
  938: 			   $thisrole.='.'.$role_code;
  939: 		       }
  940: 		   }
  941:                    # Compute the background color based on $area
  942:                    if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
  943:                        $carea.='<br />Section: '.$3;
  944: 		       $sortkey.="\0$3";
  945:                    }
  946:                    $area=$carea;
  947:                } else {
  948: 		   $sortkey.="\0".$area;
  949:                    # Determine if current user is able to revoke privileges
  950:                    if ($area=~m{^/($match_domain)/}) {
  951:                        if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
  952:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  953:                            $allowed=1;
  954:                        }
  955:                        if (((&Apache::lonnet::allowed('dro',$1))  ||
  956:                             (&Apache::lonnet::allowed('dro',$ccdomain))) &&
  957:                            ($role_code ne 'dc')) {
  958:                            $delallowed=1;
  959:                        }
  960:                    } else {
  961:                        if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
  962:                            $allowed=1;
  963:                        }
  964:                    }
  965: 		   if ($role_code eq 'ca' || $role_code eq 'au') {
  966: 		       $class='Construction Space';
  967: 		   } elsif ($role_code eq 'su') {
  968: 		       $class='System';
  969: 		   } else {
  970: 		       $class='Domain';
  971: 		   }
  972:                }
  973:                if (($role_code eq 'ca') || ($role_code eq 'aa')) {
  974:                    $area=~m{/($match_domain)/($match_username)};
  975: 		   if (&authorpriv($2,$1)) {
  976: 		       $allowed=1;
  977:                    } else {
  978:                        $allowed=0;
  979:                    }
  980:                }
  981:                my $row = '';
  982:                $row.= '<td>';
  983:                my $active=1;
  984:                $active=0 if (($role_end_time) && ($now>$role_end_time));
  985:                if (($active) && ($allowed)) {
  986:                    $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
  987:                } else {
  988:                    if ($active) {
  989:                       $row.='&nbsp;';
  990: 		   } else {
  991:                       $row.=&mt('expired or revoked');
  992: 		   }
  993:                }
  994: 	       $row.='</td><td>';
  995:                if ($allowed && !$active) {
  996:                    $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
  997:                } else {
  998:                    $row.='&nbsp;';
  999:                }
 1000: 	       $row.='</td><td>';
 1001:                if ($delallowed) {
 1002:                    $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
 1003:                } else {
 1004:                    $row.='&nbsp;';
 1005:                }
 1006: 	       my $plaintext='';
 1007: 	       if (!$croletitle) {
 1008:                    $plaintext=&Apache::lonnet::plaintext($role_code,$class)
 1009: 	       } else {
 1010: 	           $plaintext=
 1011: 		"Customrole '$croletitle' defined by $croleuname\@$croleudom";
 1012: 	       }
 1013:                $row.= '</td><td>'.$plaintext.
 1014:                       '</td><td>'.$area.
 1015:                       '</td><td>'.($role_start_time?localtime($role_start_time)
 1016:                                                    : '&nbsp;' ).
 1017:                       '</td><td>'.($role_end_time  ?localtime($role_end_time)
 1018:                                                    : '&nbsp;' )
 1019:                       ."</td>";
 1020: 	       $sortrole{$sortkey}=$envkey;
 1021: 	       $roletext{$envkey}=$row;
 1022: 	       $roleclass{$envkey}=$class;
 1023:                $rolepriv{$envkey}=$allowed;
 1024:                #$r->print($row);
 1025:            } # end of foreach        (table building loop)
 1026:            my $rolesdisplay = 0;
 1027:            my %output = ();
 1028: 	   foreach my $type ('Construction Space','Course','Group','Domain','System','Unknown') {
 1029: 	       $output{$type} = '';
 1030: 	       foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
 1031: 		   if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) { 
 1032: 		       $output{$type}.=
 1033:                              &Apache::loncommon::start_data_table_row().
 1034:                              $roletext{$sortrole{$which}}.
 1035:                              &Apache::loncommon::end_data_table_row();
 1036: 		   }
 1037: 	       }
 1038: 	       unless($output{$type} eq '') {
 1039: 		   $output{$type} = '<tr class="LC_info_row">'.
 1040: 			     "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
 1041:                               $output{$type};
 1042:                    $rolesdisplay = 1;
 1043: 	       }
 1044: 	   }
 1045:            if ($rolesdisplay == 1) {
 1046:                $r->print('
 1047: <hr />
 1048: <h3>'.$lt{'rer'}.'</h3>'.
 1049: &Apache::loncommon::start_data_table("LC_createuser").
 1050: &Apache::loncommon::start_data_table_header_row().
 1051: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
 1052: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
 1053: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
 1054: &Apache::loncommon::end_data_table_header_row());
 1055:                foreach my $type ('Construction Space','Course','Group','Domain','System','Unknown') {
 1056:                    if ($output{$type}) {
 1057:                        $r->print($output{$type}."\n");
 1058:                    }
 1059:                }
 1060: 	       $r->print(&Apache::loncommon::end_data_table());
 1061:            }
 1062:         }  # End of unless
 1063: 	my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
 1064: 	if ($currentauth=~/^krb(4|5):/) {
 1065: 	    $currentauth=~/^krb(4|5):(.*)/;
 1066: 	    my $krbdefdom=$2;
 1067:             my %param = ( formname => 'document.cu',
 1068:                           kerb_def_dom => $krbdefdom 
 1069:                           );
 1070:             $loginscript  = &Apache::loncommon::authform_header(%param);
 1071: 	}
 1072: 	# Check for a bad authentication type
 1073:         unless ($currentauth=~/^krb(4|5):/ or
 1074: 		$currentauth=~/^unix:/ or
 1075: 		$currentauth=~/^internal:/ or
 1076: 		$currentauth=~/^localauth:/
 1077: 		) { # bad authentication scheme
 1078: 	    if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 1079:                 &initialize_authen_forms();
 1080: 		my %lt=&Apache::lonlocal::texthash(
 1081:                                'err'   => "ERROR",
 1082: 			       'uuas'  => "This user has an unrecognized authentication scheme",
 1083:                                'sldb'  => "Please specify login data below",
 1084:                                'ld'    => "Login Data"
 1085: 						   );
 1086: 		$r->print(<<ENDBADAUTH);
 1087: <hr />
 1088: <script type="text/javascript" language="Javascript">
 1089: $loginscript
 1090: </script>
 1091: <font color='#ff0000'>$lt{'err'}:</font>
 1092: $lt{'uuas'} ($currentauth). $lt{'sldb'}.
 1093: <h3>$lt{'ld'}</h3>
 1094: <p>$generalrule</p>
 1095: <p>$authformkrb</p>
 1096: <p>$authformint</p>
 1097: <p>$authformfsys</p>
 1098: <p>$authformloc</p>
 1099: ENDBADAUTH
 1100:             } else { 
 1101:                 # This user is not allowed to modify the user's 
 1102:                 # authentication scheme, so just notify them of the problem
 1103: 		my %lt=&Apache::lonlocal::texthash(
 1104:                                'err'   => "ERROR",
 1105: 			       'uuas'  => "This user has an unrecognized authentication scheme",
 1106:                                'adcs'  => "Please alert a domain coordinator of this situation"
 1107: 						   );
 1108: 		$r->print(<<ENDBADAUTH);
 1109: <hr />
 1110: <font color="#ff0000"> $lt{'err'}: </font>
 1111: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
 1112: <hr />
 1113: ENDBADAUTH
 1114:             }
 1115:         } else { # Authentication type is valid
 1116: 	    my $authformcurrent='';
 1117: 	    my $authform_other='';
 1118:             &initialize_authen_forms();
 1119: 	    if ($currentauth=~/^krb(4|5):/) {
 1120: 		$authformcurrent=$authformkrb;
 1121: 		$authform_other="<p>$authformint</p>\n".
 1122:                     "<p>$authformfsys</p><p>$authformloc</p>";
 1123: 	    }
 1124: 	    elsif ($currentauth=~/^internal:/) {
 1125: 		$authformcurrent=$authformint;
 1126: 		$authform_other="<p>$authformkrb</p>".
 1127:                     "<p>$authformfsys</p><p>$authformloc</p>";
 1128: 	    }
 1129: 	    elsif ($currentauth=~/^unix:/) {
 1130: 		$authformcurrent=$authformfsys;
 1131: 		$authform_other="<p>$authformkrb</p>".
 1132:                     "<p>$authformint</p><p>$authformloc;</p>";
 1133: 	    }
 1134: 	    elsif ($currentauth=~/^localauth:/) {
 1135: 		$authformcurrent=$authformloc;
 1136: 		$authform_other="<p>$authformkrb</p>".
 1137:                     "<p>$authformint</p><p>$authformfsys</p>";
 1138: 	    }
 1139:             $authformcurrent.=' <i>(will override current values)</i><br />';
 1140:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 1141: 		# Current user has login modification privileges
 1142: 		my %lt=&Apache::lonlocal::texthash(
 1143:                                'ccld'  => "Change Current Login Data",
 1144: 			       'enld'  => "Enter New Login Data"
 1145: 						   );
 1146: 		$r->print(<<ENDOTHERAUTHS);
 1147: <hr />
 1148: <script type="text/javascript" language="Javascript">
 1149: $loginscript
 1150: </script>
 1151: <h3>$lt{'ccld'}</h3>
 1152: <p>$generalrule</p>
 1153: <p>$authformnop</p>
 1154: <p>$authformcurrent</p>
 1155: <h3>$lt{'enld'}</h3>
 1156: $authform_other
 1157: ENDOTHERAUTHS
 1158:             } else {
 1159:                 if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
 1160:                     my %lt=&Apache::lonlocal::texthash(
 1161:                                'ccld'  => "Change Current Login Data",
 1162:                                'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
 1163:                                'ifch'  => "If a change is required, contact a domain coordinator for the domain",
 1164:                     );
 1165:                     $r->print(<<ENDNOPRIV);
 1166: <hr />
 1167: <h3>$lt{'ccld'}</h3>
 1168: $lt{'yodo'} $lt{'ifch'}: $ccdomain 
 1169: ENDNOPRIV
 1170:                 } 
 1171:             }
 1172:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
 1173:                 # Current user has quota modification privileges
 1174:                 $r->print(&portfolio_quota($ccuname,$ccdomain));
 1175:             }
 1176:         }  ## End of "check for bad authentication type" logic
 1177:     } ## End of new user/old user logic
 1178:     $r->print('<hr /><h3>'.&mt('Add Roles').'</h3>');
 1179: #
 1180: # Co-Author
 1181: # 
 1182:     if (&authorpriv($env{'user.name'},$env{'request.role.domain'}) &&
 1183:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
 1184:         # No sense in assigning co-author role to yourself
 1185: 	my $cuname=$env{'user.name'};
 1186:         my $cudom=$env{'request.role.domain'};
 1187: 	   my %lt=&Apache::lonlocal::texthash(
 1188: 		    'cs'   => "Construction Space",
 1189:                     'act'  => "Activate",                    
 1190:                     'rol'  => "Role",
 1191:                     'ext'  => "Extent",
 1192:                     'sta'  => "Start",
 1193:                     'end'  => "End",
 1194:                     'cau'  => "Co-Author",
 1195:                     'caa'  => "Assistant Co-Author",
 1196:                     'ssd'  => "Set Start Date",
 1197:                     'sed'  => "Set End Date"
 1198: 				       );
 1199:        $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n". 
 1200:            &Apache::loncommon::start_data_table()."\n".
 1201:            &Apache::loncommon::start_data_table_header_row()."\n".
 1202:            '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
 1203:            '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
 1204:            '<th>'.$lt{'end'}.'</th>'."\n".
 1205:            &Apache::loncommon::end_data_table_header_row()."\n".
 1206:            &Apache::loncommon::start_data_table_row()."\n".
 1207:            '<td>
 1208:             <input type=checkbox name="act_'.$cudom.'_'.$cuname.'_ca" />
 1209:            </td>
 1210:            <td>'.$lt{'cau'}.'</td>
 1211:            <td>'.$cudom.'_'.$cuname.'</td>
 1212:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
 1213:              <a href=
 1214: "javascript:pjump('."'date_start','Start Date Co-Author',document.cu.start_$cudom\_$cuname\_ca.value,'start_$cudom\_$cuname\_ca','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 1215: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
 1216: <a href=
 1217: "javascript:pjump('."'date_end','End Date Co-Author',document.cu.end_$cudom\_$cuname\_ca.value,'end_$cudom\_$cuname\_ca','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'."\n".
 1218:           &Apache::loncommon::end_data_table_row()."\n".
 1219:           &Apache::loncommon::start_data_table_row()."\n".
 1220: '<td><input type=checkbox name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
 1221: <td>'.$lt{'caa'}.'</td>
 1222: <td>'.$cudom.'_'.$cuname.'</td>
 1223: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
 1224: <a href=
 1225: "javascript:pjump('."'date_start','Start Date Assistant Co-Author',document.cu.start_$cudom\_$cuname\_aa.value,'start_$cudom\_$cuname\_aa','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 1226: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
 1227: <a href=
 1228: "javascript:pjump('."'date_end','End Date Assistant Co-Author',document.cu.end_$cudom\_$cuname\_aa.value,'end_$cudom\_$cuname\_aa','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'."\n".
 1229:          &Apache::loncommon::end_data_table_row()."\n".
 1230:          &Apache::loncommon::end_data_table());
 1231:     }
 1232: #
 1233: # Domain level
 1234: #
 1235:     my $num_domain_level = 0;
 1236:     my $domaintext = 
 1237:     '<h4>'.&mt('Domain Level').'</h4>'.
 1238:     &Apache::loncommon::start_data_table().
 1239:     &Apache::loncommon::start_data_table_header_row().
 1240:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
 1241:     &mt('Extent').'</th>'.
 1242:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
 1243:     &Apache::loncommon::end_data_table_header_row();
 1244:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
 1245:         foreach my $role ('dc','li','dg','au','sc') {
 1246:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
 1247:                my $plrole=&Apache::lonnet::plaintext($role);
 1248: 	       my %lt=&Apache::lonlocal::texthash(
 1249:                     'ssd'  => "Set Start Date",
 1250:                     'sed'  => "Set End Date"
 1251: 				       );
 1252:                $num_domain_level ++;
 1253:                $domaintext .= 
 1254: &Apache::loncommon::start_data_table_row().
 1255: '<td><input type=checkbox name="act_'.$thisdomain.'_'.$role.'" /></td>
 1256: <td>'.$plrole.'</td>
 1257: <td>'.$thisdomain.'</td>
 1258: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
 1259: <a href=
 1260: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 1261: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
 1262: <a href=
 1263: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
 1264: &Apache::loncommon::end_data_table_row();
 1265:             }
 1266:         } 
 1267:     }
 1268:     $domaintext.= &Apache::loncommon::end_data_table();
 1269:     if ($num_domain_level > 0) {
 1270:         $r->print($domaintext);
 1271:     }
 1272: #
 1273: # Course and group levels
 1274: #
 1275: 
 1276:     if ($env{'request.role'} =~ m{^dc\./($match_domain)/$}) {
 1277:         $r->print(&course_level_dc($1,'Course'));
 1278:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setCourse()" />'."\n");
 1279:     } else {
 1280:         $r->print(&course_level_table(%inccourses));
 1281:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setSections()" />'."\n");
 1282:     }
 1283:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate']));
 1284:     $r->print('<input type="hidden" name="currstate" value="" />');
 1285:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" />');
 1286:     $r->print("</form>".&Apache::loncommon::end_page());
 1287: }
 1288: 
 1289: sub set_login {
 1290:     my $response = (<<ENDAUTH);
 1291: <p>$generalrule </p>
 1292: <p>$authformkrb </p>
 1293: <p>$authformint </p>
 1294: <p>$authformfsys</p>
 1295: <p>$authformloc </p>
 1296: ENDAUTH
 1297:     return $response;
 1298: }
 1299: 
 1300: # ================================================================= Phase Three
 1301: sub update_user_data {
 1302:     my ($r) = @_; 
 1303:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
 1304:                                           $env{'form.ccdomain'});
 1305:     # Error messages
 1306:     my $error     = '<font color="#ff0000">'.&mt('Error').':</font>';
 1307:     my $end       = &Apache::loncommon::end_page();
 1308: 
 1309:     my $title;
 1310:     if (exists($env{'form.makeuser'})) {
 1311: 	$title='Set Privileges for New User';
 1312:     } else {
 1313:         $title='Modify User Privileges';
 1314:     }
 1315: 
 1316:     my ($jsback,$elements) = &crumb_utilities();
 1317:     my $jscript = '<script type="text/javascript">'."\n".
 1318:                   $jsback."\n".'</script>'."\n";
 1319: 
 1320:     $r->print(&Apache::loncommon::start_page($title,$jscript));
 1321:     &Apache::lonhtmlcommon::add_breadcrumb
 1322:        ({href=>"javascript:backPage(document.userupdate)",
 1323:          text=>"User modify/custom role edit",
 1324:          faq=>282,bug=>'Instructor Interface',});
 1325:     if ($env{'form.prevphase'} eq 'userpicked') {
 1326:         &Apache::lonhtmlcommon::add_breadcrumb
 1327:            ({href=>"javascript:backPage(document.userupdate,'get_user_info','select')",
 1328:              text=>"Select a user",
 1329:              faq=>282,bug=>'Instructor Interface',});
 1330:     }
 1331:     &Apache::lonhtmlcommon::add_breadcrumb
 1332:        ({href=>"javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
 1333:          text=>"Set user role",
 1334:          faq=>282,bug=>'Instructor Interface',},
 1335:         {href=>"/adm/createuser",
 1336:          text=>"Result",
 1337:          faq=>282,bug=>'Instructor Interface',});
 1338:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
 1339: 
 1340:     my %disallowed;
 1341:     # Check Inputs
 1342:     if (! $env{'form.ccuname'} ) {
 1343: 	$r->print($error.&mt('No login name specified').'.'.$end);
 1344: 	return;
 1345:     }
 1346:     if (  $env{'form.ccuname'} ne 
 1347: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
 1348: 	$r->print($error.&mt('Invalid login name').'.  '.
 1349: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid').'.'.
 1350: 		  $end);
 1351: 	return;
 1352:     }
 1353:     if (! $env{'form.ccdomain'}       ) {
 1354: 	$r->print($error.&mt('No domain specified').'.'.$end);
 1355: 	return;
 1356:     }
 1357:     if (  $env{'form.ccdomain'} ne
 1358: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
 1359: 	$r->print($error.&mt ('Invalid domain name').'.  '.
 1360: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid').'.'.
 1361: 		  $end);
 1362: 	return;
 1363:     }
 1364:     if (! exists($env{'form.makeuser'})) {
 1365:         # Modifying an existing user, so check the validity of the name
 1366:         if ($uhome eq 'no_host') {
 1367:             $r->print($error.&mt('Unable to determine home server for ').
 1368:                       $env{'form.ccuname'}.&mt(' in domain ').
 1369:                       $env{'form.ccdomain'}.'.');
 1370:             return;
 1371:         }
 1372:     }
 1373:     # Determine authentication method and password for the user being modified
 1374:     my $amode='';
 1375:     my $genpwd='';
 1376:     if ($env{'form.login'} eq 'krb') {
 1377: 	$amode='krb';
 1378: 	$amode.=$env{'form.krbver'};
 1379: 	$genpwd=$env{'form.krbarg'};
 1380:     } elsif ($env{'form.login'} eq 'int') {
 1381: 	$amode='internal';
 1382: 	$genpwd=$env{'form.intarg'};
 1383:     } elsif ($env{'form.login'} eq 'fsys') {
 1384: 	$amode='unix';
 1385: 	$genpwd=$env{'form.fsysarg'};
 1386:     } elsif ($env{'form.login'} eq 'loc') {
 1387: 	$amode='localauth';
 1388: 	$genpwd=$env{'form.locarg'};
 1389: 	$genpwd=" " if (!$genpwd);
 1390:     } elsif (($env{'form.login'} eq 'nochange') ||
 1391:              ($env{'form.login'} eq ''        )) { 
 1392:         # There is no need to tell the user we did not change what they
 1393:         # did not ask us to change.
 1394:         # If they are creating a new user but have not specified login
 1395:         # information this will be caught below.
 1396:     } else {
 1397: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
 1398: 	    return;
 1399:     }
 1400: 
 1401: 
 1402:     $r->print('<h2>'.&mt('User [_1] in domain [_2]',
 1403: 			 $env{'form.ccuname'}, $env{'form.ccdomain'}).'</h2>');
 1404: 
 1405:     if ($env{'form.makeuser'}) {
 1406: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
 1407:         # Check for the authentication mode and password
 1408:         if (! $amode || ! $genpwd) {
 1409: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
 1410: 	    return;
 1411: 	}
 1412:         # Determine desired host
 1413:         my $desiredhost = $env{'form.hserver'};
 1414:         if (lc($desiredhost) eq 'default') {
 1415:             $desiredhost = undef;
 1416:         } else {
 1417:             my %home_servers = 
 1418: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
 1419:             if (! exists($home_servers{$desiredhost})) {
 1420:                 $r->print($error.&mt('Invalid home server specified'));
 1421:                 return;
 1422:             }
 1423:         }
 1424: 	# Call modifyuser
 1425: 	my $result = &Apache::lonnet::modifyuser
 1426: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cstid'},
 1427:              $amode,$genpwd,$env{'form.cfirst'},
 1428:              $env{'form.cmiddle'},$env{'form.clast'},$env{'form.cgen'},
 1429:              undef,$desiredhost,$env{'form.cemail'}
 1430: 	     );
 1431: 	$r->print(&mt('Generating user').': '.$result);
 1432:         my $home = &Apache::lonnet::homeserver($env{'form.ccuname'},
 1433:                                                $env{'form.ccdomain'});
 1434:         $r->print('<br />'.&mt('Home server').': '.$home.' '.
 1435:                   &Apache::lonnet::hostname($home));
 1436:     } elsif (($env{'form.login'} ne 'nochange') &&
 1437:              ($env{'form.login'} ne ''        )) {
 1438: 	# Modify user privileges
 1439:         if (! $amode || ! $genpwd) {
 1440: 	    $r->print($error.'Invalid login mode or password'.$end);    
 1441: 	    return;
 1442: 	}
 1443: 	# Only allow authentification modification if the person has authority
 1444: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
 1445: 	    $r->print('Modifying authentication: '.
 1446:                       &Apache::lonnet::modifyuserauth(
 1447: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
 1448:                        $amode,$genpwd));
 1449:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
 1450: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
 1451: 	} else {
 1452: 	    # Okay, this is a non-fatal error.
 1453: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.');    
 1454: 	}
 1455:     }
 1456:     ##
 1457:     if (! $env{'form.makeuser'} ) {
 1458:         # Check for need to change
 1459:         my %userenv = &Apache::lonnet::get
 1460:             ('environment',['firstname','middlename','lastname','generation',
 1461:              'permanentemail','portfolioquota','inststatus'],
 1462:               $env{'form.ccdomain'},$env{'form.ccuname'});
 1463:         my ($tmp) = keys(%userenv);
 1464:         if ($tmp =~ /^(con_lost|error)/i) { 
 1465:             %userenv = ();
 1466:         }
 1467:         # Check to see if we need to change user information
 1468:         foreach my $item ('firstname','middlename','lastname','generation','permanentemail') {
 1469:             # Strip leading and trailing whitespace
 1470:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g; 
 1471:         }
 1472:         my ($quotachanged,$namechanged,$oldportfolioquota,$newportfolioquota,
 1473:             $inststatus,$isdefault,$defquotatext);
 1474:         my ($defquota,$settingstatus) = 
 1475:             &Apache::loncommon::default_quota($env{'form.ccdomain'},$inststatus);
 1476:         my %changeHash;
 1477:         if ($userenv{'portfolioquota'} ne '') {
 1478:             $oldportfolioquota = $userenv{'portfolioquota'};
 1479:             if ($env{'form.customquota'} == 1) {
 1480:                 if ($env{'form.portfolioquota'} eq '') {
 1481:                     $newportfolioquota = 0;
 1482:                 } else {
 1483:                     $newportfolioquota = $env{'form.portfolioquota'};
 1484:                     $newportfolioquota =~ s/[^\d\.]//g;
 1485:                 }
 1486:                 if ($newportfolioquota != $userenv{'portfolioquota'}) {
 1487:                     $quotachanged = &quota_admin($newportfolioquota,\%changeHash);
 1488:                 }
 1489:             } else {
 1490:                 $quotachanged = &quota_admin('',\%changeHash);
 1491:                 $newportfolioquota = $defquota;
 1492:                 $isdefault = 1; 
 1493:             }
 1494:         } else {
 1495:             $oldportfolioquota = $defquota;
 1496:             if ($env{'form.customquota'} == 1) {
 1497:                 if ($env{'form.portfolioquota'} eq '') {
 1498:                     $newportfolioquota = 0;
 1499:                 } else {
 1500:                     $newportfolioquota = $env{'form.portfolioquota'};
 1501:                     $newportfolioquota =~ s/[^\d\.]//g;
 1502:                 }
 1503:                 $quotachanged = &quota_admin($newportfolioquota,\%changeHash);
 1504:             } else {
 1505:                 $newportfolioquota = $defquota;
 1506:                 $isdefault = 1;
 1507:             }
 1508:         }
 1509:         if ($isdefault) {
 1510:             if ($settingstatus eq '') {
 1511:                 $defquotatext = &mt('(default)');
 1512:             } else {
 1513:                 my ($usertypes,$order) = 
 1514:                     &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
 1515:                 if ($usertypes->{$settingstatus} eq '') {
 1516:                     $defquotatext = &mt('(default)');
 1517:                 } else { 
 1518:                     $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
 1519:                 }
 1520:             }
 1521:         }
 1522:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}) && 
 1523:             ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
 1524:              $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
 1525:              $env{'form.clastname'}   ne $userenv{'lastname'}   ||
 1526:              $env{'form.cgeneration'} ne $userenv{'generation'} ||
 1527:              $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} )) {
 1528:             $namechanged = 1;
 1529:         }
 1530:         if ($namechanged) {
 1531:             # Make the change
 1532:             $changeHash{'firstname'}  = $env{'form.cfirstname'};
 1533:             $changeHash{'middlename'} = $env{'form.cmiddlename'};
 1534:             $changeHash{'lastname'}   = $env{'form.clastname'};
 1535:             $changeHash{'generation'} = $env{'form.cgeneration'};
 1536:             $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
 1537:             my $putresult = &Apache::lonnet::put
 1538:                 ('environment',\%changeHash,
 1539:                  $env{'form.ccdomain'},$env{'form.ccuname'});
 1540:             if ($putresult eq 'ok') {
 1541:             # Tell the user we changed the name
 1542: 		my %lt=&Apache::lonlocal::texthash(
 1543:                              'uic'  => "User Information Changed",             
 1544:                              'frst' => "first",
 1545:                              'mddl' => "middle",
 1546:                              'lst'  => "last",
 1547: 			     'gen'  => "generation",
 1548:                              'mail' => "permanent e-mail",
 1549:                              'disk' => "disk space allocated to portfolio files",
 1550:                              'prvs' => "Previous",
 1551:                              'chto' => "Changed To"
 1552: 						   );
 1553:                 $r->print(<<"END");
 1554: <table border="2">
 1555: <caption>$lt{'uic'}</caption>
 1556: <tr><th>&nbsp;</th>
 1557:     <th>$lt{'frst'}</th>
 1558:     <th>$lt{'mddl'}</th>
 1559:     <th>$lt{'lst'}</th>
 1560:     <th>$lt{'gen'}</th>
 1561:     <th>$lt{'mail'}</th>
 1562:     <th>$lt{'disk'}</th></tr>
 1563: <tr><td>$lt{'prvs'}</td>
 1564:     <td>$userenv{'firstname'}  </td>
 1565:     <td>$userenv{'middlename'} </td>
 1566:     <td>$userenv{'lastname'}   </td>
 1567:     <td>$userenv{'generation'} </td>
 1568:     <td>$userenv{'permanentemail'} </td>
 1569:     <td>$oldportfolioquota Mb</td>
 1570: </tr>
 1571: <tr><td>$lt{'chto'}</td>
 1572:     <td>$env{'form.cfirstname'}  </td>
 1573:     <td>$env{'form.cmiddlename'} </td>
 1574:     <td>$env{'form.clastname'}   </td>
 1575:     <td>$env{'form.cgeneration'} </td>
 1576:     <td>$env{'form.cpermanentemail'} </td>
 1577:     <td>$newportfolioquota Mb $defquotatext </td></tr>
 1578: </table>
 1579: END
 1580:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
 1581:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
 1582:                     my %newenvhash;
 1583:                     foreach my $key (keys(%changeHash)) {
 1584:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
 1585:                     }
 1586:                     &Apache::lonnet::appenv(%newenvhash);
 1587:                 }
 1588:             } else { # error occurred
 1589:                 $r->print("<h2>".&mt('Unable to successfully change environment for')." ".
 1590:                       $env{'form.ccuname'}." ".&mt('in domain')." ".
 1591:                       $env{'form.ccdomain'}."</h2>");
 1592:             }
 1593:         }  else { # End of if ($env ... ) logic
 1594:             my $putresult;
 1595:             if ($quotachanged) {
 1596:                 $putresult = &Apache::lonnet::put
 1597:                                  ('environment',\%changeHash,
 1598:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
 1599:             }
 1600:             # They did not want to change the users name but we can
 1601:             # still tell them what the name is
 1602: 	    my %lt=&Apache::lonlocal::texthash(
 1603:                            'mail' => "Permanent e-mail",
 1604:                            'disk' => "Disk space allocated to user's portfolio files",
 1605: 					       );
 1606:             $r->print(<<"END");
 1607: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}</h4>
 1608: <h4>$lt{'mail'}: $userenv{'permanentemail'}</h4>
 1609: END
 1610:             if ($putresult eq 'ok') {
 1611:                 if ($oldportfolioquota != $newportfolioquota) {
 1612:                     $r->print('<h4>'.$lt{'disk'}.': '.$newportfolioquota.' Mb '. 
 1613:                               $defquotatext.'</h4>');
 1614:                     &Apache::lonnet::appenv('environment.portfolioquota' => $changeHash{'portfolioquota'});
 1615:                 }
 1616:             }
 1617:         }
 1618:     }
 1619:     ##
 1620:     my $now=time;
 1621:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
 1622:     foreach my $key (keys (%env)) {
 1623: 	next if (! $env{$key});
 1624: 	# Revoke roles
 1625: 	if ($key=~/^form\.rev/) {
 1626: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
 1627: # Revoke standard role
 1628: 		my ($scope,$role) = ($1,$2);
 1629: 		my $result =
 1630: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
 1631: 						$env{'form.ccuname'},
 1632: 						$scope,$role);
 1633: 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
 1634: 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
 1635: 		if ($role eq 'st') {
 1636: 		    my $result = &classlist_drop($scope,$env{'form.ccuname'},
 1637: 						 $env{'form.ccdomain'},$now);
 1638: 		    $r->print($result);
 1639: 		}
 1640: 	    } 
 1641: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$ }s) {
 1642: # Revoke custom role
 1643: 		$r->print(&mt('Revoking custom role:').
 1644:                       ' '.$4.' by '.$3.':'.$2.' in '.$1.': <b>'.
 1645:                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
 1646: 				  $env{'form.ccuname'},$1,$2,$3,$4).
 1647: 		'</b><br />');
 1648: 	    }
 1649: 	} elsif ($key=~/^form\.del/) {
 1650: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
 1651: # Delete standard role
 1652: 		my ($scope,$role) = ($1,$2);
 1653: 		my $result =
 1654: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
 1655: 						$env{'form.ccuname'},
 1656: 						$scope,$role,$now,0,1);
 1657: 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
 1658: 			      '<b>'.$result.'</b>').'<br />');
 1659: 		if ($role eq 'st') {
 1660: 		    my $result = &classlist_drop($scope,$env{'form.ccuname'},
 1661: 						 $env{'form.ccdomain'},$now);
 1662: 		    $r->print($result);
 1663: 		}
 1664:             }
 1665: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
 1666:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
 1667: # Delete custom role
 1668:                 $r->print(&mt('Deleting custom role [_1] by [_2]:[_3] in [_4]',
 1669:                       $rolename,$rnam,$rdom,$url).': <b>'.
 1670:                       &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
 1671:                          $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
 1672:                          0,1).'</b><br />');
 1673:             }
 1674: 	} elsif ($key=~/^form\.ren/) {
 1675:             my $udom = $env{'form.ccdomain'};
 1676:             my $uname = $env{'form.ccuname'};
 1677: # Re-enable standard role
 1678: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
 1679:                 my $url = $1;
 1680:                 my $role = $2;
 1681:                 my $logmsg;
 1682:                 my $output;
 1683:                 if ($role eq 'st') {
 1684:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
 1685:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
 1686:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course')) {
 1687:                             $output = "Error: $result\n";
 1688:                         } else {
 1689:                             $output = &mt('Assigning').' '.$role.' in '.$url.
 1690:                                       &mt('starting').' '.localtime($now).
 1691:                                       ': <br />'.$logmsg.'<br />'.
 1692:                                       &mt('Add to classlist').': <b>ok</b><br />';
 1693:                         }
 1694:                     }
 1695:                 } else {
 1696: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
 1697:                                $env{'form.ccuname'},$url,$role,0,$now);
 1698: 		    $output = &mt('Re-enabling [_1] in [_2]: <b>[_3]</b>',
 1699: 			      $role,$url,$result).'<br />';
 1700: 		}
 1701:                 $r->print($output);
 1702: 	    }
 1703: # Re-enable custom role
 1704: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
 1705:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
 1706:                 my $result = &Apache::lonnet::assigncustomrole(
 1707:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
 1708:                                $url,$rdom,$rnam,$rolename,0,$now);
 1709:                 $r->print(&mt('Re-enabling custom role [_1] by [_2]@[_3] in [_4] : <b>[_5]</b>',
 1710:                           $rolename,$rnam,$rdom,$url,$result).'<br />');
 1711:             }
 1712: 	} elsif ($key=~/^form\.act/) {
 1713:             my $udom = $env{'form.ccdomain'};
 1714:             my $uname = $env{'form.ccuname'};
 1715: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
 1716:                 # Activate a custom role
 1717: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
 1718: 		my $url='/'.$one.'/'.$two;
 1719: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
 1720: 
 1721:                 my $start = ( $env{'form.start_'.$full} ?
 1722:                               $env{'form.start_'.$full} :
 1723:                               $now );
 1724:                 my $end   = ( $env{'form.end_'.$full} ?
 1725:                               $env{'form.end_'.$full} :
 1726:                               0 );
 1727:                                                                                      
 1728:                 # split multiple sections
 1729:                 my %sections = ();
 1730:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
 1731:                 if ($num_sections == 0) {
 1732:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end));
 1733:                 } else {
 1734: 		    my %curr_groups =
 1735: 			&Apache::longroup::coursegroups($one,$two);
 1736:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1737:                         if (($sec eq 'none') || ($sec eq 'all') || 
 1738:                             exists($curr_groups{$sec})) {
 1739:                             $disallowed{$sec} = $url;
 1740:                             next;
 1741:                         }
 1742:                         my $securl = $url.'/'.$sec;
 1743: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end));
 1744:                     }
 1745:                 }
 1746: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
 1747: 		# Activate roles for sections with 3 id numbers
 1748: 		# set start, end times, and the url for the class
 1749: 		my ($one,$two,$three)=($1,$2,$3);
 1750: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
 1751: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
 1752: 			      $now );
 1753: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
 1754: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
 1755: 			      0 );
 1756: 		my $url='/'.$one.'/'.$two;
 1757:                 my $type = 'three';
 1758:                 # split multiple sections
 1759:                 my %sections = ();
 1760:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
 1761:                 if ($num_sections == 0) {
 1762:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
 1763:                 } else {
 1764:                     my %curr_groups = 
 1765: 			&Apache::longroup::coursegroups($one,$two);
 1766:                     my $emptysec = 0;
 1767:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1768:                         $sec =~ s/\W//g;
 1769:                         if ($sec ne '') {
 1770:                             if (($sec eq 'none') || ($sec eq 'all') || 
 1771:                                 exists($curr_groups{$sec})) {
 1772:                                 $disallowed{$sec} = $url;
 1773:                                 next;
 1774:                             }
 1775:                             my $securl = $url.'/'.$sec;
 1776:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec));
 1777:                         } else {
 1778:                             $emptysec = 1;
 1779:                         }
 1780:                     }
 1781:                     if ($emptysec) {
 1782:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
 1783:                     }
 1784:                 } 
 1785: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
 1786: 		# Activate roles for sections with two id numbers
 1787: 		# set start, end times, and the url for the class
 1788: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
 1789: 			      $env{'form.start_'.$1.'_'.$2} : 
 1790: 			      $now );
 1791: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
 1792: 			      $env{'form.end_'.$1.'_'.$2} :
 1793: 			      0 );
 1794: 		my $url='/'.$1.'/';
 1795:                 # split multiple sections
 1796:                 my %sections = ();
 1797:                 my $num_sections = &build_roles($env{'form.sec_'.$1.'_'.$2},\%sections,$2);
 1798:                 if ($num_sections == 0) {
 1799:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
 1800:                 } else {
 1801:                     my $emptysec = 0;
 1802:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1803:                         if ($sec ne '') {
 1804:                             my $securl = $url.'/'.$sec;
 1805:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$2,$start,$end,$1,undef,$sec));
 1806:                         } else {
 1807:                             $emptysec = 1;
 1808:                         }
 1809:                     }
 1810:                     if ($emptysec) {
 1811:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
 1812:                     }
 1813:                 }
 1814: 	    } else {
 1815: 		$r->print('<p>'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></p><br />');
 1816:             }
 1817:             foreach my $key (sort(keys(%disallowed))) {
 1818:                 if (($key eq 'none') || ($key eq 'all')) {  
 1819:                     $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key));
 1820:                 } else {
 1821:                     $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is the name of a course group.',$key));
 1822:                 }
 1823:                 $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />');
 1824:             }
 1825: 	}
 1826:     } # End of foreach (keys(%env))
 1827: # Flush the course logs so reverse user roles immediately updated
 1828:     &Apache::lonnet::flushcourselogs();
 1829:     $r->print('<p><a href="/adm/createuser">'.&mt('Create/Modify Another User').'</a></p>');
 1830:     $r->print('<form name="userupdate" method="post" />'."\n");
 1831:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
 1832:         $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
 1833:     }
 1834:     foreach my $item ('sortby','seluname','seludom') {
 1835:         if (exists($env{'form.'.$item})) {
 1836:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
 1837:         }
 1838:     }
 1839:     $r->print('<input type="hidden" name="phase" value="" />'."\n".
 1840:               '<input type ="hidden" name="currstate" value="" />'."\n".
 1841:               '</form>');
 1842:     $r->print(&Apache::loncommon::end_page());
 1843: }
 1844: 
 1845: sub classlist_drop {
 1846:     my ($scope,$uname,$udom,$now) = @_;
 1847:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
 1848:     my $cid=$cdom.'_'.$cnum;
 1849:     my $user = $uname.':'.$udom;
 1850:     if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
 1851: 	my $result = 
 1852: 	    &Apache::lonnet::cput('classlist',
 1853: 				  { $user => $now },
 1854: 				  $env{'course.'.$cid.'.domain'},
 1855: 				  $env{'course.'.$cid.'.num'});
 1856: 	return &mt('Drop from classlist: [_1]',
 1857: 		   '<b>'.$result.'</b>').'<br />';
 1858:     }
 1859: }
 1860: 
 1861: sub active_student_roles {
 1862:     my ($cnum,$cdom,$uname,$udom) = @_;
 1863:     my %roles = 
 1864: 	&Apache::lonnet::get_my_roles($uname,$udom,'userroles',
 1865: 				      ['future','active'],['st']);
 1866:     return exists($roles{"$cnum:$cdom:st"});
 1867: }
 1868: 
 1869: sub quota_admin {
 1870:     my ($setquota,$changeHash) = @_;
 1871:     my $quotachanged;
 1872:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
 1873:         # Current user has quota modification privileges
 1874:         $quotachanged = 1;
 1875:         $changeHash->{'portfolioquota'} = $setquota;
 1876:     }
 1877:     return $quotachanged;
 1878: }
 1879: 
 1880: sub build_roles {
 1881:     my ($sectionstr,$sections,$role) = @_;
 1882:     my $num_sections = 0;
 1883:     if ($sectionstr=~ /,/) {
 1884:         my @secnums = split/,/,$sectionstr;
 1885:         if ($role eq 'st') {
 1886:             $secnums[0] =~ s/\W//g;
 1887:             $$sections{$secnums[0]} = 1;
 1888:             $num_sections = 1;
 1889:         } else {
 1890:             foreach my $sec (@secnums) {
 1891:                 $sec =~ ~s/\W//g;
 1892:                 if (!($sec eq "")) {
 1893:                     if (exists($$sections{$sec})) {
 1894:                         $$sections{$sec} ++;
 1895:                     } else {
 1896:                         $$sections{$sec} = 1;
 1897:                         $num_sections ++;
 1898:                     }
 1899:                 }
 1900:             }
 1901:         }
 1902:     } else {
 1903:         $sectionstr=~s/\W//g;
 1904:         unless ($sectionstr eq '') {
 1905:             $$sections{$sectionstr} = 1;
 1906:             $num_sections ++;
 1907:         }
 1908:     }
 1909: 
 1910:     return $num_sections;
 1911: }
 1912: 
 1913: # ========================================================== Custom Role Editor
 1914: 
 1915: sub custom_role_editor {
 1916:     my ($r) = @_;
 1917:     my $rolename=$env{'form.rolename'};
 1918: 
 1919:     if ($rolename eq 'make new role') {
 1920: 	$rolename=$env{'form.newrolename'};
 1921:     }
 1922: 
 1923:     $rolename=~s/[^A-Za-z0-9]//gs;
 1924: 
 1925:     if (!$rolename) {
 1926: 	&print_username_entry_form($r);
 1927:         return;
 1928:     }
 1929: # ------------------------------------------------------- What can be assigned?
 1930:     my %full=();
 1931:     my %courselevel=();
 1932:     my %courselevelcurrent=();
 1933:     my $syspriv='';
 1934:     my $dompriv='';
 1935:     my $coursepriv='';
 1936:     my $body_top;
 1937:     my ($disp_dummy,$disp_roles) = &Apache::lonnet::get('roles',["st"]);
 1938:     my ($rdummy,$roledef)=
 1939: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 1940: # ------------------------------------------------------- Does this role exist?
 1941:     $body_top .= '<h2>';
 1942:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 1943: 	$body_top .= &mt('Existing Role').' "';
 1944: # ------------------------------------------------- Get current role privileges
 1945: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 1946:     } else {
 1947: 	$body_top .= &mt('New Role').' "';
 1948: 	$roledef='';
 1949:     }
 1950:     $body_top .= $rolename.'"</h2>';
 1951:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 1952: 	my ($priv,$restrict)=split(/\&/,$item);
 1953:         if (!$restrict) { $restrict='F'; }
 1954:         $courselevel{$priv}=$restrict;
 1955:         if ($coursepriv=~/\:$priv/) {
 1956: 	    $courselevelcurrent{$priv}=1;
 1957: 	}
 1958: 	$full{$priv}=1;
 1959:     }
 1960:     my %domainlevel=();
 1961:     my %domainlevelcurrent=();
 1962:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 1963: 	my ($priv,$restrict)=split(/\&/,$item);
 1964:         if (!$restrict) { $restrict='F'; }
 1965:         $domainlevel{$priv}=$restrict;
 1966:         if ($dompriv=~/\:$priv/) {
 1967: 	    $domainlevelcurrent{$priv}=1;
 1968: 	}
 1969: 	$full{$priv}=1;
 1970:     }
 1971:     my %systemlevel=();
 1972:     my %systemlevelcurrent=();
 1973:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 1974: 	my ($priv,$restrict)=split(/\&/,$item);
 1975:         if (!$restrict) { $restrict='F'; }
 1976:         $systemlevel{$priv}=$restrict;
 1977:         if ($syspriv=~/\:$priv/) {
 1978: 	    $systemlevelcurrent{$priv}=1;
 1979: 	}
 1980: 	$full{$priv}=1;
 1981:     }
 1982:     my ($jsback,$elements) = &crumb_utilities();
 1983:     my $button_code = "\n";
 1984:     my $head_script = "\n";
 1985:     $head_script .= '<script type="text/javascript">'."\n";
 1986:     my @template_roles = ("cc","in","ta","ep","st");
 1987:     foreach my $role (@template_roles) {
 1988:         $head_script .= &make_script_template($role);
 1989:         $button_code .= &make_button_code($role);
 1990:     }
 1991:     $head_script .= "\n".$jsback."\n".'</script>'."\n";
 1992:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',$head_script));
 1993:    &Apache::lonhtmlcommon::add_breadcrumb
 1994:      ({href=>"javascript:backPage(document.form1,'','')",
 1995:        text=>"User modify/custom role edit",
 1996:        faq=>282,bug=>'Instructor Interface',},
 1997:       {href=>"javascript:backPage(document.form1,'','')",
 1998:          text=>"Edit custom role",
 1999:          faq=>282,bug=>'Instructor Interface',});
 2000:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
 2001: 
 2002:     $r->print($body_top);
 2003:     my %lt=&Apache::lonlocal::texthash(
 2004: 		    'prv'  => "Privilege",
 2005: 		    'crl'  => "Course Level",
 2006:                     'dml'  => "Domain Level",
 2007:                     'ssl'  => "System Level");
 2008:     $r->print('Select a Template<br />');
 2009:     $r->print('<form action="">');
 2010:     $r->print($button_code);
 2011:     $r->print('</form>');
 2012:     $r->print(<<ENDCCF);
 2013: <form name="form1" method="post">
 2014: <input type="hidden" name="phase" value="set_custom_roles" />
 2015: <input type="hidden" name="rolename" value="$rolename" />
 2016: ENDCCF
 2017:     $r->print(&Apache::loncommon::start_data_table().
 2018:               &Apache::loncommon::start_data_table_header_row(). 
 2019: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
 2020: '</th><th>'.$lt{'ssl'}.'</th>'.
 2021:               &Apache::loncommon::end_data_table_header_row());
 2022:     foreach my $priv (sort keys %full) {
 2023:         my $privtext = &Apache::lonnet::plaintext($priv);
 2024:         $r->print(&Apache::loncommon::start_data_table_row().
 2025: 	          '<td>'.$privtext.'</td><td>'.
 2026:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c" '.
 2027:     ($courselevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
 2028:     '</td><td>'.
 2029:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d" '.
 2030:     ($domainlevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
 2031:     '</td><td>'.
 2032:     ($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s" '.
 2033:     ($systemlevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
 2034:     '</td>'.
 2035:              &Apache::loncommon::end_data_table_row());
 2036:     }
 2037:     $r->print(&Apache::loncommon::end_data_table().
 2038:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
 2039:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
 2040:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
 2041:    '<input type="submit" value="'.&mt('Define Role').'" /></form>'.
 2042: 	      &Apache::loncommon::end_page());
 2043: }
 2044: # --------------------------------------------------------
 2045: sub make_script_template {
 2046:     my ($role) = @_;
 2047:     my %full_c=();
 2048:     my %full_d=();
 2049:     my %full_s=();
 2050:     my $return_script;
 2051:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 2052:         my ($priv,$restrict)=split(/\&/,$item);
 2053:         $full_c{$priv}=1;
 2054:     }
 2055:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 2056:         my ($priv,$restrict)=split(/\&/,$item);
 2057:         $full_d{$priv}=1;
 2058:     }
 2059:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 2060:         my ($priv,$restrict)=split(/\&/,$item);
 2061:         $full_s{$priv}=1;
 2062:     }
 2063:     $return_script .= 'function set_'.$role.'() {'."\n";
 2064:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
 2065:     my %role_c;
 2066:     foreach my $priv (@temp) {
 2067:         my ($priv_item, $dummy) = split(/\&/,$priv);
 2068:         $role_c{$priv_item} = 1;
 2069:     }
 2070:     foreach my $priv_item (keys(%full_c)) {
 2071:         my ($priv, $dummy) = split(/\&/,$priv_item);
 2072:         if (exists($role_c{$priv})) {
 2073:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
 2074:         } else {
 2075:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
 2076:         }
 2077:     }
 2078:     my %role_d;
 2079:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
 2080:     foreach my $priv(@temp) {
 2081:         my ($priv_item, $dummy) = split(/\&/,$priv);
 2082:         $role_d{$priv_item} = 1;
 2083:     }
 2084:     foreach my $priv_item (keys(%full_d)) {
 2085:         my ($priv, $dummy) = split(/\&/,$priv_item);
 2086:         if (exists($role_d{$priv})) {
 2087:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
 2088:         } else {
 2089:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
 2090:         }
 2091:     }
 2092:     my %role_s;
 2093:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
 2094:     foreach my $priv(@temp) {
 2095:         my ($priv_item, $dummy) = split(/\&/,$priv);
 2096:         $role_s{$priv_item} = 1;
 2097:     }
 2098:     foreach my $priv_item (keys(%full_s)) {
 2099:         my ($priv, $dummy) = split(/\&/,$priv_item);
 2100:         if (exists($role_s{$priv})) {
 2101:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
 2102:         } else {
 2103:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
 2104:         }
 2105:     }
 2106:     $return_script .= '}'."\n";
 2107:     return ($return_script);
 2108: }
 2109: # ----------------------------------------------------------
 2110: sub make_button_code {
 2111:     my ($role) = @_;
 2112:     my $label = &Apache::lonnet::plaintext($role);
 2113:     my $button_code = '<input type="button" onClick="set_'.$role.'()" value="'.$label.'" />';    
 2114:     return ($button_code);
 2115: }
 2116: # ---------------------------------------------------------- Call to definerole
 2117: sub set_custom_role {
 2118:     my ($r) = @_;
 2119: 
 2120:     my $rolename=$env{'form.rolename'};
 2121: 
 2122:     $rolename=~s/[^A-Za-z0-9]//gs;
 2123: 
 2124:     if (!$rolename) {
 2125: 	&print_username_entry_form($r);
 2126:         return;
 2127:     }
 2128: 
 2129:     my ($jsback,$elements) = &crumb_utilities();
 2130:     my $jscript = '<script type="text/javascript">'.$jsback."\n".'</script>';
 2131: 
 2132:     $r->print(&Apache::loncommon::start_page('Save Custom Role'),$jscript);
 2133:     &Apache::lonhtmlcommon::add_breadcrumb
 2134:         ({href=>"javascript:backPage(document.customresult,'','')",
 2135:           text=>"User modify/custom role edit",
 2136:           faq=>282,bug=>'Instructor Interface',},
 2137:          {href=>"javascript:backPage(document.customresult,'selected_custom_edit','')",
 2138:           text=>"Edit custom role",
 2139:           faq=>282,bug=>'Instructor Interface',},
 2140:          {href=>"javascript:backPage(document.customresult,'set_custom_roles','')",
 2141:           text=>"Result",
 2142:           faq=>282,bug=>'Instructor Interface',});
 2143:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
 2144: 
 2145:     my ($rdummy,$roledef)=
 2146: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 2147: 
 2148: # ------------------------------------------------------- Does this role exist?
 2149:     $r->print('<h2>');
 2150:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 2151: 	$r->print(&mt('Existing Role').' "');
 2152:     } else {
 2153: 	$r->print(&mt('New Role').' "');
 2154: 	$roledef='';
 2155:     }
 2156:     $r->print($rolename.'"</h2>');
 2157: # ------------------------------------------------------- What can be assigned?
 2158:     my $sysrole='';
 2159:     my $domrole='';
 2160:     my $courole='';
 2161: 
 2162:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 2163: 	my ($priv,$restrict)=split(/\&/,$item);
 2164:         if (!$restrict) { $restrict=''; }
 2165:         if ($env{'form.'.$priv.'_c'}) {
 2166: 	    $courole.=':'.$item;
 2167: 	}
 2168:     }
 2169: 
 2170:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 2171: 	my ($priv,$restrict)=split(/\&/,$item);
 2172:         if (!$restrict) { $restrict=''; }
 2173:         if ($env{'form.'.$priv.'_d'}) {
 2174: 	    $domrole.=':'.$item;
 2175: 	}
 2176:     }
 2177: 
 2178:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 2179: 	my ($priv,$restrict)=split(/\&/,$item);
 2180:         if (!$restrict) { $restrict=''; }
 2181:         if ($env{'form.'.$priv.'_s'}) {
 2182: 	    $sysrole.=':'.$item;
 2183: 	}
 2184:     }
 2185:     $r->print('<br />Defining Role: '.
 2186: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
 2187:     if ($env{'request.course.id'}) {
 2188:         my $url='/'.$env{'request.course.id'};
 2189:         $url=~s/\_/\//g;
 2190: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
 2191: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
 2192: 						$env{'user.name'},
 2193: 						$url,
 2194: 						$env{'user.domain'},
 2195: 						$env{'user.name'},
 2196: 						$rolename));
 2197:     }
 2198:     $r->print('<p><a href="/adm/createuser">Create another role, or Create/Modify a user.</a></p><form name="customresult" method="post">');
 2199:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
 2200:     $r->print(&Apache::loncommon::end_page());
 2201: }
 2202: 
 2203: # ================================================================ Main Handler
 2204: sub handler {
 2205:     my $r = shift;
 2206: 
 2207:     if ($r->header_only) {
 2208:        &Apache::loncommon::content_type($r,'text/html');
 2209:        $r->send_http_header;
 2210:        return OK;
 2211:     }
 2212: 
 2213:     if ((&Apache::lonnet::allowed('cta',$env{'request.course.id'})) ||
 2214:         (&Apache::lonnet::allowed('cin',$env{'request.course.id'})) || 
 2215:         (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) || 
 2216:         (&Apache::lonnet::allowed('cep',$env{'request.course.id'})) ||
 2217: 	(&authorpriv($env{'user.name'},$env{'request.role.domain'})) ||
 2218:         (&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))) {
 2219:        &Apache::loncommon::content_type($r,'text/html');
 2220:        $r->send_http_header;
 2221:        &Apache::lonhtmlcommon::clear_breadcrumbs();
 2222:       
 2223:        my $phase = $env{'form.phase'};
 2224:        my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
 2225: 
 2226:        if (($phase eq 'get_user_info') || ($phase eq 'userpicked')) {
 2227:            my $srch;
 2228:            foreach my $item (@search) {
 2229:                $srch->{$item} = $env{'form.'.$item};
 2230:            }
 2231:            if ($env{'form.phase'} eq 'get_user_info') {
 2232:                my ($currstate,$response,$forcenewuser,$results) = 
 2233:                    &user_search_result($srch);
 2234:                if ($currstate eq 'select') {
 2235:                    &print_user_selection_page($r,$response,$srch,$results,'createuser',\@search);
 2236:                } elsif ($currstate eq 'modify') {
 2237:                    my ($ccuname,$ccdomain);
 2238:                    if (($srch->{'srchby'} eq 'uname') && 
 2239:                        ($srch->{'srchtype'} eq 'exact')) {
 2240:                        $ccuname = $srch->{'srchterm'};
 2241:                        $ccdomain= $srch->{'srchdomain'};
 2242:                    } else {
 2243:                        my @matchedunames = keys(%{$results});
 2244:                        ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
 2245:                    }
 2246:                    $ccuname =&LONCAPA::clean_username($ccuname);
 2247:                    $ccdomain=&LONCAPA::clean_domain($ccdomain);
 2248:                    &print_user_modification_page($r,$ccuname,$ccdomain,$srch,
 2249:                                                  $response);
 2250:                } elsif ($currstate eq 'query') {
 2251:                    &print_user_query_page($r,'createuser');
 2252:                } else {
 2253:                    &print_username_entry_form($r,$response,$srch,$forcenewuser);
 2254:                }
 2255:            } elsif ($env{'form.phase'} eq 'userpicked') {
 2256:                my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
 2257:                my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
 2258:                &print_user_modification_page($r,$ccuname,$ccdomain,$srch);
 2259:            }
 2260:        } elsif ($env{'form.phase'} eq 'update_user_data') {
 2261:            &update_user_data($r);
 2262:        } elsif ($env{'form.phase'} eq 'selected_custom_edit') {
 2263:            &custom_role_editor($r);
 2264:        } elsif ($env{'form.phase'} eq 'set_custom_roles') {
 2265: 	   &set_custom_role($r);
 2266:        } else {
 2267:            &print_username_entry_form($r);
 2268:        }
 2269:    } else {
 2270:       $env{'user.error.msg'}=
 2271:         "/adm/createuser:mau:0:0:Cannot modify user data";
 2272:       return HTTP_NOT_ACCEPTABLE; 
 2273:    }
 2274:    return OK;
 2275: }
 2276: 
 2277: #-------------------------------------------------- functions for &phase_two
 2278: sub user_search_result {
 2279:     my ($srch) = @_;
 2280:     my %allhomes;
 2281:     my %inst_matches;
 2282:     my %srch_results;
 2283:     my ($response,$currstate,$forcenewuser,$dirsrchres);
 2284:     $srch->{'srchterm'} =~ s/\s+/ /g;
 2285:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
 2286:         $response = &mt('Invalid search.');
 2287:     }
 2288:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
 2289:         $response = &mt('Invalid search.');
 2290:     }
 2291:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
 2292:         $response = &mt('Invalid search.');
 2293:     }
 2294:     if ($srch->{'srchterm'} eq '') {
 2295:         $response = &mt('You must enter a search term.');
 2296:     }
 2297:     if ($srch->{'srchterm'} =~ /^\s+$/) {
 2298:         $response = &mt('Your search term must contain more than just spaces.');
 2299:     }
 2300:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
 2301:         if (($srch->{'srchdomain'} eq '') || 
 2302: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
 2303:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
 2304:         }
 2305:     }
 2306:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
 2307:         ($srch->{'srchin'} eq 'alc')) {
 2308:         if ($srch->{'srchby'} eq 'uname') {
 2309:             if ($srch->{'srchterm'} !~ /^$match_username$/) {
 2310:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
 2311:             }
 2312:         }
 2313:     }
 2314:     if ($response ne '') {
 2315:         $response = '<span class="LC_warning">'.$response.'</span>';
 2316:     }
 2317:     if ($srch->{'srchin'} eq 'instd') {
 2318:         my $instd_chk = &directorysrch_check($srch);
 2319:         if ($instd_chk ne 'ok') {
 2320:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
 2321:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
 2322:         }
 2323:     }
 2324:     if ($response ne '') {
 2325:         return ($currstate,$response);
 2326:     }
 2327:     if ($srch->{'srchby'} eq 'uname') {
 2328:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
 2329:             if ($env{'form.forcenew'}) {
 2330:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
 2331:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
 2332:                     if ($uhome eq 'no_host') {
 2333:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
 2334:                         my $showdom = &display_domain_info($env{'request.role.domain'});
 2335:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
 2336:                     } else {
 2337:                         $currstate = 'modify';
 2338:                     }
 2339:                 } else {
 2340:                     $currstate = 'modify';
 2341:                 }
 2342:             } else {
 2343:                 if ($srch->{'srchin'} eq 'dom') {
 2344:                     if ($srch->{'srchtype'} eq 'exact') {
 2345:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
 2346:                         if ($uhome eq 'no_host') {
 2347:                             ($currstate,$response,$forcenewuser) =
 2348:                                 &build_search_response($srch,%srch_results);
 2349:                         } else {
 2350:                             $currstate = 'modify';
 2351:                         }
 2352:                     } else {
 2353:                         %srch_results = &Apache::lonnet::usersearch($srch);
 2354:                         ($currstate,$response,$forcenewuser) =
 2355:                             &build_search_response($srch,%srch_results);
 2356:                     }
 2357:                 } else {
 2358:                     my $courseusers = &get_courseusers();
 2359:                     if ($srch->{'srchtype'} eq 'exact') {
 2360:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
 2361:                             $currstate = 'modify';
 2362:                         } else {
 2363:                             ($currstate,$response,$forcenewuser) =
 2364:                                 &build_search_response($srch,%srch_results);
 2365:                         }
 2366:                     } else {
 2367:                         foreach my $user (keys(%$courseusers)) {
 2368:                             my ($cuname,$cudomain) = split(/:/,$user);
 2369:                             if ($cudomain eq $srch->{'srchdomain'}) {
 2370:                                 my $matched = 0;
 2371:                                 if ($srch->{'srchtype'} eq 'begins') {
 2372:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
 2373:                                         $matched = 1;
 2374:                                     }
 2375:                                 } else {
 2376:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
 2377:                                         $matched = 1;
 2378:                                     }
 2379:                                 }
 2380:                                 if ($matched) {
 2381:                                     $srch_results{$user} = 
 2382: 					{&Apache::lonnet::get('environment',
 2383: 							     ['firstname',
 2384: 							      'lastname',
 2385: 							      'permanentemail'])};
 2386:                                 }
 2387:                             }
 2388:                         }
 2389:                         ($currstate,$response,$forcenewuser) =
 2390:                             &build_search_response($srch,%srch_results);
 2391:                     }
 2392:                 }
 2393:             }
 2394:         } elsif ($srch->{'srchin'} eq 'alc') {
 2395:             $currstate = 'query';
 2396:         } elsif ($srch->{'srchin'} eq 'instd') {
 2397:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
 2398:             if ($dirsrchres eq 'ok') {
 2399:                 ($currstate,$response,$forcenewuser) = 
 2400:                     &build_search_response($srch,%srch_results);
 2401:             } else {
 2402:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
 2403:                 $response = '<span class="LC_warning">'.
 2404:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
 2405:                     '</span><br />'.
 2406:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
 2407:                     '<br /><br />'; 
 2408:             }
 2409:         }
 2410:     } else {
 2411:         if ($srch->{'srchin'} eq 'dom') {
 2412:             %srch_results = &Apache::lonnet::usersearch($srch);
 2413:             ($currstate,$response,$forcenewuser) = 
 2414:                 &build_search_response($srch,%srch_results); 
 2415:         } elsif ($srch->{'srchin'} eq 'crs') {
 2416:             my $courseusers = &get_courseusers(); 
 2417:             foreach my $user (keys(%$courseusers)) {
 2418:                 my ($uname,$udom) = split(/:/,$user);
 2419:                 my %names = &Apache::loncommon::getnames($uname,$udom);
 2420:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
 2421:                 if ($srch->{'srchby'} eq 'lastname') {
 2422:                     if ((($srch->{'srchtype'} eq 'exact') && 
 2423:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
 2424:                         (($srch->{'srchtype'} eq 'begins') &&
 2425:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
 2426:                         (($srch->{'srchtype'} eq 'contains') &&
 2427:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
 2428:                         $srch_results{$user} = {firstname => $names{'firstname'},
 2429:                                             lastname => $names{'lastname'},
 2430:                                             permanentemail => $emails{'permanentemail'},
 2431:                                            };
 2432:                     }
 2433:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
 2434:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
 2435:                     $srchlast =~ s/\s+$//;
 2436:                     $srchfirst =~ s/^\s+//;
 2437:                     if ($srch->{'srchtype'} eq 'exact') {
 2438:                         if (($names{'lastname'} eq $srchlast) &&
 2439:                             ($names{'firstname'} eq $srchfirst)) {
 2440:                             $srch_results{$user} = {firstname => $names{'firstname'},
 2441:                                                 lastname => $names{'lastname'},
 2442:                                                 permanentemail => $emails{'permanentemail'},
 2443: 
 2444:                                            };
 2445:                         }
 2446:                     } elsif ($srch->{'srchtype'} eq 'begins') {
 2447:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
 2448:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
 2449:                             $srch_results{$user} = {firstname => $names{'firstname'},
 2450:                                                 lastname => $names{'lastname'},
 2451:                                                 permanentemail => $emails{'permanentemail'},
 2452:                                                };
 2453:                         }
 2454:                     } else {
 2455:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
 2456:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
 2457:                             $srch_results{$user} = {firstname => $names{'firstname'},
 2458:                                                 lastname => $names{'lastname'},
 2459:                                                 permanentemail => $emails{'permanentemail'},
 2460:                                                };
 2461:                         }
 2462:                     }
 2463:                 }
 2464:             }
 2465:             ($currstate,$response,$forcenewuser) = 
 2466:                 &build_search_response($srch,%srch_results); 
 2467:         } elsif ($srch->{'srchin'} eq 'alc') {
 2468:             $currstate = 'query';
 2469:         } elsif ($srch->{'srchin'} eq 'instd') {
 2470:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
 2471:             if ($dirsrchres eq 'ok') {
 2472:                 ($currstate,$response,$forcenewuser) = 
 2473:                     &build_search_response($srch,%srch_results);
 2474:             } else {
 2475:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
 2476:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
 2477:                     '</span><br />'.
 2478:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
 2479:                     '<br /><br />';
 2480:             }
 2481:         }
 2482:     }
 2483:     return ($currstate,$response,$forcenewuser,\%srch_results);
 2484: }
 2485: 
 2486: sub directorysrch_check {
 2487:     my ($srch) = @_;
 2488:     my $can_search = 0;
 2489:     my $response;
 2490:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
 2491:                                              ['directorysrch'],$srch->{'srchdomain'});
 2492:     my $showdom = &display_domain_info($srch->{'srchdomain'});
 2493:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
 2494:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
 2495:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
 2496:         }
 2497:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
 2498:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
 2499:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
 2500:             }
 2501:             my @usertypes = split(/:/,$env{'environment.inststatus'});
 2502:             if (!@usertypes) {
 2503:                 push(@usertypes,'default');
 2504:             }
 2505:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
 2506:                 foreach my $type (@usertypes) {
 2507:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
 2508:                         $can_search = 1;
 2509:                         last;
 2510:                     }
 2511:                 }
 2512:             }
 2513:             if (!$can_search) {
 2514:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
 2515:                 my @longtypes; 
 2516:                 foreach my $item (@usertypes) {
 2517:                     push (@longtypes,$insttypes->{$item});
 2518:                 }
 2519:                 my $insttype_str = join(', ',@longtypes); 
 2520:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
 2521:             } 
 2522:         } else {
 2523:             $can_search = 1;
 2524:         }
 2525:     } else {
 2526:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
 2527:     }
 2528:     my %longtext = &Apache::lonlocal::texthash (
 2529:                        uname     => 'username',
 2530:                        lastfirst => 'last name, first name',
 2531:                        lastname  => 'last name',
 2532:                        contains  => 'contains',
 2533:                        exact     => 'as exact match to',
 2534:                        begins    => 'begins with',
 2535:                    );
 2536:     if ($can_search) {
 2537:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
 2538:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
 2539:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
 2540:             }
 2541:         } else {
 2542:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
 2543:         }
 2544:     }
 2545:     if ($can_search) {
 2546:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
 2547:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
 2548:                 return 'ok';
 2549:             } else {
 2550:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
 2551:             }
 2552:         } else {
 2553:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
 2554:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
 2555:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
 2556:                 return 'ok';
 2557:             } else {
 2558:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
 2559:             }
 2560:         }
 2561:     }
 2562: }
 2563: 
 2564: sub get_courseusers {
 2565:     my %advhash;
 2566:     my $classlist = &Apache::loncoursedata::get_classlist();
 2567:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
 2568:     foreach my $role (sort(keys(%coursepersonnel))) {
 2569:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
 2570: 	    if (!exists($classlist->{$user})) {
 2571: 		$classlist->{$user} = [];
 2572: 	    }
 2573:         }
 2574:     }
 2575:     return $classlist;
 2576: }
 2577: 
 2578: sub build_search_response {
 2579:     my ($srch,%srch_results) = @_;
 2580:     my ($currstate,$response,$forcenewuser);
 2581:     my %names = (
 2582:           'uname' => 'username',
 2583:           'lastname' => 'last name',
 2584:           'lastfirst' => 'last name, first name',
 2585:           'crs' => 'this course',
 2586:           'dom' => 'LON-CAPA domain: ',
 2587:           'instd' => 'the institutional directory for domain: ',
 2588:     );
 2589: 
 2590:     my %single = (
 2591:                    begins   => 'A match',
 2592:                    contains => 'A match',
 2593:                    exact    => 'An exact match',
 2594:                  );
 2595:     my %nomatch = (
 2596:                    begins   => 'No match',
 2597:                    contains => 'No match',
 2598:                    exact    => 'No exact match',
 2599:                   );
 2600:     if (keys(%srch_results) > 1) {
 2601:         $currstate = 'select';
 2602:     } else {
 2603:         if (keys(%srch_results) == 1) {
 2604:             $currstate = 'modify';
 2605:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
 2606:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
 2607:                 $response .= &display_domain_info($srch->{'srchdomain'});
 2608:             }
 2609:         } else {
 2610:             $response = '<span class="LC_warning">'.&mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}",$srch->{'srchterm'});
 2611:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
 2612:                 $response .= &display_domain_info($srch->{'srchdomain'});
 2613:             }
 2614:             $response .= '</span>';
 2615:             if ($srch->{'srchin'} ne 'alc') {
 2616:                 $forcenewuser = 1;
 2617:                 my $cansrchinst = 0; 
 2618:                 if ($srch->{'srchdomain'}) {
 2619:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
 2620:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 2621:                         if ($domconfig{'directorysrch'}{'available'}) {
 2622:                             $cansrchinst = 1;
 2623:                         } 
 2624:                     }
 2625:                 }
 2626:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
 2627:                      ($srch->{'srchby'} eq 'lastname')) &&
 2628:                     ($srch->{'srchin'} eq 'dom')) {
 2629:                     if ($cansrchinst) {
 2630:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
 2631:                     }
 2632:                 }
 2633:                 if ($srch->{'srchin'} eq 'crs') {
 2634:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
 2635:                 }
 2636:             }
 2637:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $env{'request.role.domain'})) {
 2638:                 my $showdom = &display_domain_info($env{'request.role.domain'}); 
 2639:                 $response .= '<br /><br />'.&mt("<b>To add a new user</b> (you can only create new users in your current role's domain - <span class=\"LC_cusr_emph\">[_1]</span>):",$env{'request.role.domain'}).'<ul><li>'.&mt("Set 'Domain/institution to search' to: <span class=\"LC_cusr_emph\">[_1]</span>",$showdom).'<li>'.&mt("Set 'Search criteria' to: <span class=\"LC_cusr_emph\">'username is ...... in selected LON-CAPA domain'").'</span></li><li>'.&mt('Provide the proposed username').'</li><li>'.&mt('Search').'</li></ul><br />';
 2640:             }
 2641:         }
 2642:     }
 2643:     return ($currstate,$response,$forcenewuser);
 2644: }
 2645: 
 2646: sub display_domain_info {
 2647:     my ($dom) = @_;
 2648:     my $output = $dom;
 2649:     if ($dom ne '') { 
 2650:         my $domdesc = &Apache::lonnet::domain($dom,'description');
 2651:         if ($domdesc ne '') {
 2652:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
 2653:         }
 2654:     }
 2655:     return $output;
 2656: }
 2657: 
 2658: sub crumb_utilities {
 2659:     my %elements = (
 2660:        crtuser => {
 2661:            srchterm => 'text',
 2662:            srchin => 'selectbox',
 2663:            srchby => 'selectbox',
 2664:            srchtype => 'selectbox',
 2665:            srchdomain => 'selectbox',
 2666:        },
 2667:        docustom => {
 2668:            rolename => 'selectbox',
 2669:            newrolename => 'textbox',
 2670:        },
 2671:        studentform => {
 2672:            srchterm => 'text',
 2673:            srchin => 'selectbox',
 2674:            srchby => 'selectbox',
 2675:            srchtype => 'selectbox',
 2676:            srchdomain => 'selectbox',
 2677:        },
 2678:     );
 2679: 
 2680:     my $jsback .= qq|
 2681: function backPage(formname,prevphase,prevstate) {
 2682:     formname.phase.value = prevphase;
 2683:     formname.currstate.value = prevstate;
 2684:     formname.submit();
 2685: }
 2686: |;
 2687:     return ($jsback,\%elements);
 2688: }
 2689: 
 2690: sub course_level_table {
 2691:     my (%inccourses) = @_;
 2692:     my $table = '';
 2693: # Custom Roles?
 2694: 
 2695:     my %customroles=&my_custom_roles();
 2696:     my %lt=&Apache::lonlocal::texthash(
 2697:             'exs'  => "Existing sections",
 2698:             'new'  => "Define new section",
 2699:             'ssd'  => "Set Start Date",
 2700:             'sed'  => "Set End Date",
 2701:             'crl'  => "Course Level",
 2702:             'act'  => "Activate",
 2703:             'rol'  => "Role",
 2704:             'ext'  => "Extent",
 2705:             'grs'  => "Section",
 2706:             'sta'  => "Start",
 2707:             'end'  => "End"
 2708:     );
 2709: 
 2710:     foreach my $protectedcourse (sort( keys(%inccourses))) {
 2711: 	my $thiscourse=$protectedcourse;
 2712: 	$thiscourse=~s:_:/:g;
 2713: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
 2714: 	my $area=$coursedata{'description'};
 2715:         my $type=$coursedata{'type'};
 2716: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
 2717: 	my ($domain,$cnum)=split(/\//,$thiscourse);
 2718:         my %sections_count;
 2719:         if (defined($env{'request.course.id'})) {
 2720:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
 2721:                 %sections_count = 
 2722: 		    &Apache::loncommon::get_sections($domain,$cnum);
 2723:             }
 2724:         }
 2725: 	foreach my $role ('st','ta','ep','in','cc') {
 2726: 	    if (&Apache::lonnet::allowed('c'.$role,$thiscourse)) {
 2727: 		my $plrole=&Apache::lonnet::plaintext($role);
 2728: 		$table .= &Apache::loncommon::start_data_table_row().
 2729: '<td><input type="checkbox" name="act_'.$protectedcourse.'_'.$role.'" /></td>
 2730: <td>'.$plrole.'</td>
 2731: <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
 2732: 	        if ($role ne 'cc') {
 2733:                     if (%sections_count) {
 2734:                         my $currsec = &course_sections(\%sections_count,$protectedcourse.'_'.$role);
 2735:                         $table .= 
 2736:                     '<td><table class="LC_createuser">'.
 2737:                      '<tr class="LC_section_row">
 2738:                         <td valign="top">'.$lt{'exs'}.'<br />'.
 2739:                         $currsec.'</td>'.
 2740:                      '<td>&nbsp;&nbsp;</td>'.
 2741:                      '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
 2742:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.'" value="" />'.
 2743:                      '<input type="hidden" '.
 2744:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'.
 2745:                      '</tr></table></td>';
 2746:                     } else {
 2747:                         $table .= '<td><input type="text" size="10" '.
 2748:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>';
 2749:                     }
 2750:                 } else { 
 2751: 		    $table .= '<td>&nbsp</td>';
 2752:                 }
 2753: 		$table .= <<ENDTIMEENTRY;
 2754: <td><input type="hidden" name="start_$protectedcourse\_$role" value='' />
 2755: <a href=
 2756: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt{'ssd'}</a></td>
 2757: <td><input type="hidden" name="end_$protectedcourse\_$role" value='' />
 2758: <a href=
 2759: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt{'sed'}</a></td>
 2760: ENDTIMEENTRY
 2761:                 $table.= &Apache::loncommon::end_data_table_row();
 2762:             }
 2763:         }
 2764:         foreach my $cust (sort keys %customroles) {
 2765: 	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
 2766: 		my $plrole=$cust;
 2767:                 my $customrole=$protectedcourse.'_cr_cr_'.$env{'user.domain'}.
 2768: 		    '_'.$env{'user.name'}.'_'.$plrole;
 2769: 		$table .= &Apache::loncommon::start_data_table_row().
 2770: '<td><input type="checkbox" name="act_'.$customrole.'" /></td>
 2771: <td>'.$plrole.'</td>
 2772: <td>'.$area.'</td>'."\n";
 2773:                 if (%sections_count) {
 2774:                     my $currsec = &course_sections(\%sections_count,$customrole);
 2775:                     $table.=
 2776:                    '<td><table border="0" cellspacing="0" cellpadding="0">'.
 2777:                    '<tr><td valign="top">'.$lt{'exs'}.'<br />'.
 2778:                      $currsec.'</td>'.
 2779:                    '<td>&nbsp;&nbsp;</td>'.
 2780:                    '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
 2781:                    '<input type="text" name="newsec_'.$customrole.'" value="" /></td>'.
 2782:                    '<input type="hidden" '.
 2783:                    'name="sec_'.$customrole.'" /></td>'.
 2784:                    '</tr></table></td>';
 2785:                 } else {
 2786:                     $table .= '<td><input type="text" size="10" '.
 2787:                      'name="sec_'.$customrole.'" /></td>';
 2788:                 }
 2789:                 $table .= <<ENDENTRY;
 2790: <td><input type="hidden" name="start_$customrole" value='' />
 2791: <a href=
 2792: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">$lt{'ssd'}</a></td>
 2793: <td><input type="hidden" name="end_$customrole" value='' />
 2794: <a href=
 2795: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">$lt{'sed'}</a></td>
 2796: ENDENTRY
 2797:                $table .= &Apache::loncommon::end_data_table_row();
 2798:            }
 2799: 	}
 2800:     }
 2801:     return '' if ($table eq ''); # return nothing if there is nothing 
 2802:                                  # in the table
 2803:     my $result = '
 2804: <h4>'.$lt{'crl'}.'</h4>'.
 2805: &Apache::loncommon::start_data_table().
 2806: &Apache::loncommon::start_data_table_header_row().
 2807: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>
 2808: <th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
 2809: &Apache::loncommon::end_data_table_header_row().
 2810: $table.
 2811: &Apache::loncommon::end_data_table();
 2812:     return $result;
 2813: }
 2814: 
 2815: sub course_sections {
 2816:     my ($sections_count,$role) = @_;
 2817:     my $output = '';
 2818:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
 2819:     if (scalar(@sections) == 1) {
 2820:         $output = '<select name="currsec_'.$role.'" >'."\n".
 2821:                   '  <option value="">Select</option>'."\n".
 2822:                   '  <option value="">No section</option>'."\n".
 2823:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
 2824:     } else {
 2825:         $output = '<select name="currsec_'.$role.'" ';
 2826:         my $multiple = 4;
 2827:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
 2828:         $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
 2829:         foreach my $sec (@sections) {
 2830:             $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
 2831:         }
 2832:     }
 2833:     $output .= '</select>'; 
 2834:     return $output;
 2835: }
 2836: 
 2837: sub course_level_dc {
 2838:     my ($dcdom) = @_;
 2839:     my %customroles=&my_custom_roles();
 2840:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
 2841:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
 2842:                       '<input type="hidden" name="dccourse" value="" />';
 2843:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
 2844:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course').'</b>';
 2845:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu');
 2846:     my %lt=&Apache::lonlocal::texthash(
 2847:                     'rol'  => "Role",
 2848:                     'grs'  => "Section",
 2849:                     'exs'  => "Existing sections",
 2850:                     'new'  => "Define new section", 
 2851:                     'sta'  => "Start",
 2852:                     'end'  => "End",
 2853:                     'ssd'  => "Set Start Date",
 2854:                     'sed'  => "Set End Date"
 2855:                   );
 2856:     my $header = '<h4>'.&mt('Course Level').'</h4>'.
 2857:                  &Apache::loncommon::start_data_table().
 2858:                  &Apache::loncommon::start_data_table_header_row().
 2859:                  '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
 2860:                  &Apache::loncommon::end_data_table_header_row();
 2861:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
 2862:                      '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
 2863:                      '<td><select name="role">'."\n";
 2864:     foreach  my $role ('st','ta','ep','in','cc') {
 2865:         my $plrole=&Apache::lonnet::plaintext($role);
 2866:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
 2867:     }
 2868:     if ( keys %customroles > 0) {
 2869:         foreach my $cust (sort keys %customroles) {
 2870:             my $custrole='cr_cr_'.$env{'user.domain'}.
 2871:                     '_'.$env{'user.name'}.'_'.$cust;
 2872:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
 2873:         }
 2874:     }
 2875:     $otheritems .= '</select></td><td>'.
 2876:                      '<table border="0" cellspacing="0" cellpadding="0">'.
 2877:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
 2878:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
 2879:                      '<td>&nbsp;&nbsp;</td>'.
 2880:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
 2881:                      '<input type="text" name="newsec" value="" />'.
 2882:                      '<input type="hidden" name="groups" value="" /></td>'.
 2883:                      '</tr></table></td>';
 2884:     $otheritems .= <<ENDTIMEENTRY;
 2885: <td><input type="hidden" name="start" value='' />
 2886: <a href=
 2887: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
 2888: <td><input type="hidden" name="end" value='' />
 2889: <a href=
 2890: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
 2891: ENDTIMEENTRY
 2892:     $otheritems .= &Apache::loncommon::end_data_table_row().
 2893:                    &Apache::loncommon::end_data_table()."\n";
 2894:     return $cb_jscript.$header.$hiddenitems.$otheritems;
 2895: }
 2896: 
 2897: #---------------------------------------------- end functions for &phase_two
 2898: 
 2899: #--------------------------------- functions for &phase_two and &phase_three
 2900: 
 2901: #--------------------------end of functions for &phase_two and &phase_three
 2902: 
 2903: 1;
 2904: __END__
 2905: 
 2906: 

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