File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.186: download - view: text, annotated - select for diffs
Tue Sep 18 23:57:15 2007 UTC (16 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- style
- portfolio priv check should be for domain of user being modified, not of requestor's role.

    1: # The LearningOnline Network with CAPA
    2: # Create a user
    3: #
    4: # $Id: loncreateuser.pm,v 1.186 2007/09/18 23:57:15 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:         if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) { 
 1074:             # bad authentication scheme
 1075: 	    if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 1076:                 &initialize_authen_forms();
 1077: 		my %lt=&Apache::lonlocal::texthash(
 1078:                                'err'   => "ERROR",
 1079: 			       'uuas'  => "This user has an unrecognized authentication scheme",
 1080:                                'sldb'  => "Please specify login data below",
 1081:                                'ld'    => "Login Data"
 1082: 						   );
 1083: 		$r->print(<<ENDBADAUTH);
 1084: <hr />
 1085: <script type="text/javascript" language="Javascript">
 1086: $loginscript
 1087: </script>
 1088: <font color='#ff0000'>$lt{'err'}:</font>
 1089: $lt{'uuas'} ($currentauth). $lt{'sldb'}.
 1090: <h3>$lt{'ld'}</h3>
 1091: <p>$generalrule</p>
 1092: <p>$authformkrb</p>
 1093: <p>$authformint</p>
 1094: <p>$authformfsys</p>
 1095: <p>$authformloc</p>
 1096: ENDBADAUTH
 1097:             } else { 
 1098:                 # This user is not allowed to modify the user's 
 1099:                 # authentication scheme, so just notify them of the problem
 1100: 		my %lt=&Apache::lonlocal::texthash(
 1101:                                'err'   => "ERROR",
 1102: 			       'uuas'  => "This user has an unrecognized authentication scheme",
 1103:                                'adcs'  => "Please alert a domain coordinator of this situation"
 1104: 						   );
 1105: 		$r->print(<<ENDBADAUTH);
 1106: <hr />
 1107: <font color="#ff0000"> $lt{'err'}: </font>
 1108: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
 1109: <hr />
 1110: ENDBADAUTH
 1111:             }
 1112:         } else { # Authentication type is valid
 1113: 	    my $authformcurrent='';
 1114: 	    my $authform_other='';
 1115:             &initialize_authen_forms();
 1116: 	    if ($currentauth=~/^krb(4|5):/) {
 1117: 		$authformcurrent=$authformkrb;
 1118: 		$authform_other="<p>$authformint</p>\n".
 1119:                     "<p>$authformfsys</p><p>$authformloc</p>";
 1120: 	    }
 1121: 	    elsif ($currentauth=~/^internal:/) {
 1122: 		$authformcurrent=$authformint;
 1123: 		$authform_other="<p>$authformkrb</p>".
 1124:                     "<p>$authformfsys</p><p>$authformloc</p>";
 1125: 	    }
 1126: 	    elsif ($currentauth=~/^unix:/) {
 1127: 		$authformcurrent=$authformfsys;
 1128: 		$authform_other="<p>$authformkrb</p>".
 1129:                     "<p>$authformint</p><p>$authformloc;</p>";
 1130: 	    }
 1131: 	    elsif ($currentauth=~/^localauth:/) {
 1132: 		$authformcurrent=$authformloc;
 1133: 		$authform_other="<p>$authformkrb</p>".
 1134:                     "<p>$authformint</p><p>$authformfsys</p>";
 1135: 	    }
 1136:             $authformcurrent.=' <i>(will override current values)</i><br />';
 1137:             if (&Apache::lonnet::allowed('mau',$ccdomain)) {
 1138: 		# Current user has login modification privileges
 1139: 		my %lt=&Apache::lonlocal::texthash(
 1140:                                'ccld'  => "Change Current Login Data",
 1141: 			       'enld'  => "Enter New Login Data"
 1142: 						   );
 1143: 		$r->print(<<ENDOTHERAUTHS);
 1144: <hr />
 1145: <script type="text/javascript" language="Javascript">
 1146: $loginscript
 1147: </script>
 1148: <h3>$lt{'ccld'}</h3>
 1149: <p>$generalrule</p>
 1150: <p>$authformnop</p>
 1151: <p>$authformcurrent</p>
 1152: <h3>$lt{'enld'}</h3>
 1153: $authform_other
 1154: ENDOTHERAUTHS
 1155:             } else {
 1156:                 if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
 1157:                     my %lt=&Apache::lonlocal::texthash(
 1158:                                'ccld'  => "Change Current Login Data",
 1159:                                'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
 1160:                                'ifch'  => "If a change is required, contact a domain coordinator for the domain",
 1161:                     );
 1162:                     $r->print(<<ENDNOPRIV);
 1163: <hr />
 1164: <h3>$lt{'ccld'}</h3>
 1165: $lt{'yodo'} $lt{'ifch'}: $ccdomain 
 1166: ENDNOPRIV
 1167:                 } 
 1168:             }
 1169:         }  ## End of "check for bad authentication type" logic
 1170:         if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
 1171:             # Current user has quota modification privileges
 1172:             $r->print(&portfolio_quota($ccuname,$ccdomain));
 1173:         } elsif (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
 1174:             my %lt=&Apache::lonlocal::texthash(
 1175:                 'dska'  => "Disk space allocated to user's portfolio files",
 1176:                 'youd'  => "You do not have privileges to modify the portfolio quota for this user.",
 1177:                 'ichr'  => "If a change is required, contact a domain coordinator for the domain",
 1178:             );
 1179:             $r->print(<<ENDNOPORTPRIV);
 1180: <hr />
 1181: <h3>$lt{'dska'}</h3>
 1182: $lt{'youd'} $lt{'ichr'}: $ccdomain
 1183: ENDNOPORTPRIV
 1184:         }
 1185:     } ## End of new user/old user logic
 1186:     $r->print('<hr /><h3>'.&mt('Add Roles').'</h3>');
 1187: #
 1188: # Co-Author
 1189: # 
 1190:     if (&authorpriv($env{'user.name'},$env{'request.role.domain'}) &&
 1191:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
 1192:         # No sense in assigning co-author role to yourself
 1193: 	my $cuname=$env{'user.name'};
 1194:         my $cudom=$env{'request.role.domain'};
 1195: 	   my %lt=&Apache::lonlocal::texthash(
 1196: 		    'cs'   => "Construction Space",
 1197:                     'act'  => "Activate",                    
 1198:                     'rol'  => "Role",
 1199:                     'ext'  => "Extent",
 1200:                     'sta'  => "Start",
 1201:                     'end'  => "End",
 1202:                     'cau'  => "Co-Author",
 1203:                     'caa'  => "Assistant Co-Author",
 1204:                     'ssd'  => "Set Start Date",
 1205:                     'sed'  => "Set End Date"
 1206: 				       );
 1207:        $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n". 
 1208:            &Apache::loncommon::start_data_table()."\n".
 1209:            &Apache::loncommon::start_data_table_header_row()."\n".
 1210:            '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
 1211:            '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
 1212:            '<th>'.$lt{'end'}.'</th>'."\n".
 1213:            &Apache::loncommon::end_data_table_header_row()."\n".
 1214:            &Apache::loncommon::start_data_table_row()."\n".
 1215:            '<td>
 1216:             <input type=checkbox name="act_'.$cudom.'_'.$cuname.'_ca" />
 1217:            </td>
 1218:            <td>'.$lt{'cau'}.'</td>
 1219:            <td>'.$cudom.'_'.$cuname.'</td>
 1220:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
 1221:              <a href=
 1222: "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>
 1223: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
 1224: <a href=
 1225: "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".
 1226:           &Apache::loncommon::end_data_table_row()."\n".
 1227:           &Apache::loncommon::start_data_table_row()."\n".
 1228: '<td><input type=checkbox name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
 1229: <td>'.$lt{'caa'}.'</td>
 1230: <td>'.$cudom.'_'.$cuname.'</td>
 1231: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
 1232: <a href=
 1233: "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>
 1234: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
 1235: <a href=
 1236: "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".
 1237:          &Apache::loncommon::end_data_table_row()."\n".
 1238:          &Apache::loncommon::end_data_table());
 1239:     }
 1240: #
 1241: # Domain level
 1242: #
 1243:     my $num_domain_level = 0;
 1244:     my $domaintext = 
 1245:     '<h4>'.&mt('Domain Level').'</h4>'.
 1246:     &Apache::loncommon::start_data_table().
 1247:     &Apache::loncommon::start_data_table_header_row().
 1248:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
 1249:     &mt('Extent').'</th>'.
 1250:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
 1251:     &Apache::loncommon::end_data_table_header_row();
 1252:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
 1253:         foreach my $role ('dc','li','dg','au','sc') {
 1254:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
 1255:                my $plrole=&Apache::lonnet::plaintext($role);
 1256: 	       my %lt=&Apache::lonlocal::texthash(
 1257:                     'ssd'  => "Set Start Date",
 1258:                     'sed'  => "Set End Date"
 1259: 				       );
 1260:                $num_domain_level ++;
 1261:                $domaintext .= 
 1262: &Apache::loncommon::start_data_table_row().
 1263: '<td><input type=checkbox name="act_'.$thisdomain.'_'.$role.'" /></td>
 1264: <td>'.$plrole.'</td>
 1265: <td>'.$thisdomain.'</td>
 1266: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
 1267: <a href=
 1268: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
 1269: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
 1270: <a href=
 1271: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
 1272: &Apache::loncommon::end_data_table_row();
 1273:             }
 1274:         } 
 1275:     }
 1276:     $domaintext.= &Apache::loncommon::end_data_table();
 1277:     if ($num_domain_level > 0) {
 1278:         $r->print($domaintext);
 1279:     }
 1280: #
 1281: # Course and group levels
 1282: #
 1283: 
 1284:     if ($env{'request.role'} =~ m{^dc\./($match_domain)/$}) {
 1285:         $r->print(&course_level_dc($1,'Course'));
 1286:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setCourse()" />'."\n");
 1287:     } else {
 1288:         $r->print(&course_level_table(%inccourses));
 1289:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setSections()" />'."\n");
 1290:     }
 1291:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate']));
 1292:     $r->print('<input type="hidden" name="currstate" value="" />');
 1293:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" />');
 1294:     $r->print("</form>".&Apache::loncommon::end_page());
 1295: }
 1296: 
 1297: sub set_login {
 1298:     my $response = (<<ENDAUTH);
 1299: <p>$generalrule </p>
 1300: <p>$authformkrb </p>
 1301: <p>$authformint </p>
 1302: <p>$authformfsys</p>
 1303: <p>$authformloc </p>
 1304: ENDAUTH
 1305:     return $response;
 1306: }
 1307: 
 1308: # ================================================================= Phase Three
 1309: sub update_user_data {
 1310:     my ($r) = @_; 
 1311:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
 1312:                                           $env{'form.ccdomain'});
 1313:     # Error messages
 1314:     my $error     = '<font color="#ff0000">'.&mt('Error').':</font>';
 1315:     my $end       = &Apache::loncommon::end_page();
 1316: 
 1317:     my $title;
 1318:     if (exists($env{'form.makeuser'})) {
 1319: 	$title='Set Privileges for New User';
 1320:     } else {
 1321:         $title='Modify User Privileges';
 1322:     }
 1323: 
 1324:     my ($jsback,$elements) = &crumb_utilities();
 1325:     my $jscript = '<script type="text/javascript">'."\n".
 1326:                   $jsback."\n".'</script>'."\n";
 1327: 
 1328:     $r->print(&Apache::loncommon::start_page($title,$jscript));
 1329:     &Apache::lonhtmlcommon::add_breadcrumb
 1330:        ({href=>"javascript:backPage(document.userupdate)",
 1331:          text=>"User modify/custom role edit",
 1332:          faq=>282,bug=>'Instructor Interface',});
 1333:     if ($env{'form.prevphase'} eq 'userpicked') {
 1334:         &Apache::lonhtmlcommon::add_breadcrumb
 1335:            ({href=>"javascript:backPage(document.userupdate,'get_user_info','select')",
 1336:              text=>"Select a user",
 1337:              faq=>282,bug=>'Instructor Interface',});
 1338:     }
 1339:     &Apache::lonhtmlcommon::add_breadcrumb
 1340:        ({href=>"javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
 1341:          text=>"Set user role",
 1342:          faq=>282,bug=>'Instructor Interface',},
 1343:         {href=>"/adm/createuser",
 1344:          text=>"Result",
 1345:          faq=>282,bug=>'Instructor Interface',});
 1346:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
 1347: 
 1348:     my %disallowed;
 1349:     # Check Inputs
 1350:     if (! $env{'form.ccuname'} ) {
 1351: 	$r->print($error.&mt('No login name specified').'.'.$end);
 1352: 	return;
 1353:     }
 1354:     if (  $env{'form.ccuname'} ne 
 1355: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
 1356: 	$r->print($error.&mt('Invalid login name').'.  '.
 1357: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid').'.'.
 1358: 		  $end);
 1359: 	return;
 1360:     }
 1361:     if (! $env{'form.ccdomain'}       ) {
 1362: 	$r->print($error.&mt('No domain specified').'.'.$end);
 1363: 	return;
 1364:     }
 1365:     if (  $env{'form.ccdomain'} ne
 1366: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
 1367: 	$r->print($error.&mt ('Invalid domain name').'.  '.
 1368: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid').'.'.
 1369: 		  $end);
 1370: 	return;
 1371:     }
 1372:     if (! exists($env{'form.makeuser'})) {
 1373:         # Modifying an existing user, so check the validity of the name
 1374:         if ($uhome eq 'no_host') {
 1375:             $r->print($error.&mt('Unable to determine home server for ').
 1376:                       $env{'form.ccuname'}.&mt(' in domain ').
 1377:                       $env{'form.ccdomain'}.'.');
 1378:             return;
 1379:         }
 1380:     }
 1381:     # Determine authentication method and password for the user being modified
 1382:     my $amode='';
 1383:     my $genpwd='';
 1384:     if ($env{'form.login'} eq 'krb') {
 1385: 	$amode='krb';
 1386: 	$amode.=$env{'form.krbver'};
 1387: 	$genpwd=$env{'form.krbarg'};
 1388:     } elsif ($env{'form.login'} eq 'int') {
 1389: 	$amode='internal';
 1390: 	$genpwd=$env{'form.intarg'};
 1391:     } elsif ($env{'form.login'} eq 'fsys') {
 1392: 	$amode='unix';
 1393: 	$genpwd=$env{'form.fsysarg'};
 1394:     } elsif ($env{'form.login'} eq 'loc') {
 1395: 	$amode='localauth';
 1396: 	$genpwd=$env{'form.locarg'};
 1397: 	$genpwd=" " if (!$genpwd);
 1398:     } elsif (($env{'form.login'} eq 'nochange') ||
 1399:              ($env{'form.login'} eq ''        )) { 
 1400:         # There is no need to tell the user we did not change what they
 1401:         # did not ask us to change.
 1402:         # If they are creating a new user but have not specified login
 1403:         # information this will be caught below.
 1404:     } else {
 1405: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
 1406: 	    return;
 1407:     }
 1408: 
 1409: 
 1410:     $r->print('<h2>'.&mt('User [_1] in domain [_2]',
 1411: 			 $env{'form.ccuname'}, $env{'form.ccdomain'}).'</h2>');
 1412: 
 1413:     if ($env{'form.makeuser'}) {
 1414: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
 1415:         # Check for the authentication mode and password
 1416:         if (! $amode || ! $genpwd) {
 1417: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
 1418: 	    return;
 1419: 	}
 1420:         # Determine desired host
 1421:         my $desiredhost = $env{'form.hserver'};
 1422:         if (lc($desiredhost) eq 'default') {
 1423:             $desiredhost = undef;
 1424:         } else {
 1425:             my %home_servers = 
 1426: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
 1427:             if (! exists($home_servers{$desiredhost})) {
 1428:                 $r->print($error.&mt('Invalid home server specified'));
 1429:                 return;
 1430:             }
 1431:         }
 1432: 	# Call modifyuser
 1433: 	my $result = &Apache::lonnet::modifyuser
 1434: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cstid'},
 1435:              $amode,$genpwd,$env{'form.cfirst'},
 1436:              $env{'form.cmiddle'},$env{'form.clast'},$env{'form.cgen'},
 1437:              undef,$desiredhost,$env{'form.cemail'}
 1438: 	     );
 1439: 	$r->print(&mt('Generating user').': '.$result);
 1440:         my $home = &Apache::lonnet::homeserver($env{'form.ccuname'},
 1441:                                                $env{'form.ccdomain'});
 1442:         $r->print('<br />'.&mt('Home server').': '.$home.' '.
 1443:                   &Apache::lonnet::hostname($home));
 1444:     } elsif (($env{'form.login'} ne 'nochange') &&
 1445:              ($env{'form.login'} ne ''        )) {
 1446: 	# Modify user privileges
 1447:         if (! $amode || ! $genpwd) {
 1448: 	    $r->print($error.'Invalid login mode or password'.$end);    
 1449: 	    return;
 1450: 	}
 1451: 	# Only allow authentification modification if the person has authority
 1452: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
 1453: 	    $r->print('Modifying authentication: '.
 1454:                       &Apache::lonnet::modifyuserauth(
 1455: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
 1456:                        $amode,$genpwd));
 1457:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
 1458: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
 1459: 	} else {
 1460: 	    # Okay, this is a non-fatal error.
 1461: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.');    
 1462: 	}
 1463:     }
 1464:     ##
 1465:     if (! $env{'form.makeuser'} ) {
 1466:         # Check for need to change
 1467:         my %userenv = &Apache::lonnet::get
 1468:             ('environment',['firstname','middlename','lastname','generation',
 1469:              'permanentemail','portfolioquota','inststatus'],
 1470:               $env{'form.ccdomain'},$env{'form.ccuname'});
 1471:         my ($tmp) = keys(%userenv);
 1472:         if ($tmp =~ /^(con_lost|error)/i) { 
 1473:             %userenv = ();
 1474:         }
 1475:         # Check to see if we need to change user information
 1476:         foreach my $item ('firstname','middlename','lastname','generation','permanentemail') {
 1477:             # Strip leading and trailing whitespace
 1478:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g; 
 1479:         }
 1480:         my ($quotachanged,$namechanged,$oldportfolioquota,$newportfolioquota,
 1481:             $inststatus,$isdefault,$defquotatext);
 1482:         my ($defquota,$settingstatus) = 
 1483:             &Apache::loncommon::default_quota($env{'form.ccdomain'},$inststatus);
 1484:         my %changeHash;
 1485:         if ($userenv{'portfolioquota'} ne '') {
 1486:             $oldportfolioquota = $userenv{'portfolioquota'};
 1487:             if ($env{'form.customquota'} == 1) {
 1488:                 if ($env{'form.portfolioquota'} eq '') {
 1489:                     $newportfolioquota = 0;
 1490:                 } else {
 1491:                     $newportfolioquota = $env{'form.portfolioquota'};
 1492:                     $newportfolioquota =~ s/[^\d\.]//g;
 1493:                 }
 1494:                 if ($newportfolioquota != $userenv{'portfolioquota'}) {
 1495:                     $quotachanged = &quota_admin($newportfolioquota,\%changeHash);
 1496:                 }
 1497:             } else {
 1498:                 $quotachanged = &quota_admin('',\%changeHash);
 1499:                 $newportfolioquota = $defquota;
 1500:                 $isdefault = 1; 
 1501:             }
 1502:         } else {
 1503:             $oldportfolioquota = $defquota;
 1504:             if ($env{'form.customquota'} == 1) {
 1505:                 if ($env{'form.portfolioquota'} eq '') {
 1506:                     $newportfolioquota = 0;
 1507:                 } else {
 1508:                     $newportfolioquota = $env{'form.portfolioquota'};
 1509:                     $newportfolioquota =~ s/[^\d\.]//g;
 1510:                 }
 1511:                 $quotachanged = &quota_admin($newportfolioquota,\%changeHash);
 1512:             } else {
 1513:                 $newportfolioquota = $defquota;
 1514:                 $isdefault = 1;
 1515:             }
 1516:         }
 1517:         if ($isdefault) {
 1518:             if ($settingstatus eq '') {
 1519:                 $defquotatext = &mt('(default)');
 1520:             } else {
 1521:                 my ($usertypes,$order) = 
 1522:                     &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
 1523:                 if ($usertypes->{$settingstatus} eq '') {
 1524:                     $defquotatext = &mt('(default)');
 1525:                 } else { 
 1526:                     $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
 1527:                 }
 1528:             }
 1529:         }
 1530:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}) && 
 1531:             ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
 1532:              $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
 1533:              $env{'form.clastname'}   ne $userenv{'lastname'}   ||
 1534:              $env{'form.cgeneration'} ne $userenv{'generation'} ||
 1535:              $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} )) {
 1536:             $namechanged = 1;
 1537:         }
 1538:         if ($namechanged) {
 1539:             # Make the change
 1540:             $changeHash{'firstname'}  = $env{'form.cfirstname'};
 1541:             $changeHash{'middlename'} = $env{'form.cmiddlename'};
 1542:             $changeHash{'lastname'}   = $env{'form.clastname'};
 1543:             $changeHash{'generation'} = $env{'form.cgeneration'};
 1544:             $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
 1545:             my $putresult = &Apache::lonnet::put
 1546:                 ('environment',\%changeHash,
 1547:                  $env{'form.ccdomain'},$env{'form.ccuname'});
 1548:             if ($putresult eq 'ok') {
 1549:             # Tell the user we changed the name
 1550: 		my %lt=&Apache::lonlocal::texthash(
 1551:                              'uic'  => "User Information Changed",             
 1552:                              'frst' => "first",
 1553:                              'mddl' => "middle",
 1554:                              'lst'  => "last",
 1555: 			     'gen'  => "generation",
 1556:                              'mail' => "permanent e-mail",
 1557:                              'disk' => "disk space allocated to portfolio files",
 1558:                              'prvs' => "Previous",
 1559:                              'chto' => "Changed To"
 1560: 						   );
 1561:                 $r->print(<<"END");
 1562: <table border="2">
 1563: <caption>$lt{'uic'}</caption>
 1564: <tr><th>&nbsp;</th>
 1565:     <th>$lt{'frst'}</th>
 1566:     <th>$lt{'mddl'}</th>
 1567:     <th>$lt{'lst'}</th>
 1568:     <th>$lt{'gen'}</th>
 1569:     <th>$lt{'mail'}</th>
 1570:     <th>$lt{'disk'}</th></tr>
 1571: <tr><td>$lt{'prvs'}</td>
 1572:     <td>$userenv{'firstname'}  </td>
 1573:     <td>$userenv{'middlename'} </td>
 1574:     <td>$userenv{'lastname'}   </td>
 1575:     <td>$userenv{'generation'} </td>
 1576:     <td>$userenv{'permanentemail'} </td>
 1577:     <td>$oldportfolioquota Mb</td>
 1578: </tr>
 1579: <tr><td>$lt{'chto'}</td>
 1580:     <td>$env{'form.cfirstname'}  </td>
 1581:     <td>$env{'form.cmiddlename'} </td>
 1582:     <td>$env{'form.clastname'}   </td>
 1583:     <td>$env{'form.cgeneration'} </td>
 1584:     <td>$env{'form.cpermanentemail'} </td>
 1585:     <td>$newportfolioquota Mb $defquotatext </td></tr>
 1586: </table>
 1587: END
 1588:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
 1589:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
 1590:                     my %newenvhash;
 1591:                     foreach my $key (keys(%changeHash)) {
 1592:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
 1593:                     }
 1594:                     &Apache::lonnet::appenv(%newenvhash);
 1595:                 }
 1596:             } else { # error occurred
 1597:                 $r->print("<h2>".&mt('Unable to successfully change environment for')." ".
 1598:                       $env{'form.ccuname'}." ".&mt('in domain')." ".
 1599:                       $env{'form.ccdomain'}."</h2>");
 1600:             }
 1601:         }  else { # End of if ($env ... ) logic
 1602:             my $putresult;
 1603:             if ($quotachanged) {
 1604:                 $putresult = &Apache::lonnet::put
 1605:                                  ('environment',\%changeHash,
 1606:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
 1607:             }
 1608:             # They did not want to change the users name but we can
 1609:             # still tell them what the name is
 1610: 	    my %lt=&Apache::lonlocal::texthash(
 1611:                            'mail' => "Permanent e-mail",
 1612:                            'disk' => "Disk space allocated to user's portfolio files",
 1613: 					       );
 1614:             $r->print(<<"END");
 1615: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}</h4>
 1616: <h4>$lt{'mail'}: $userenv{'permanentemail'}</h4>
 1617: END
 1618:             if ($putresult eq 'ok') {
 1619:                 if ($oldportfolioquota != $newportfolioquota) {
 1620:                     $r->print('<h4>'.$lt{'disk'}.': '.$newportfolioquota.' Mb '. 
 1621:                               $defquotatext.'</h4>');
 1622:                     &Apache::lonnet::appenv('environment.portfolioquota' => $changeHash{'portfolioquota'});
 1623:                 }
 1624:             }
 1625:         }
 1626:     }
 1627:     ##
 1628:     my $now=time;
 1629:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
 1630:     foreach my $key (keys (%env)) {
 1631: 	next if (! $env{$key});
 1632: 	# Revoke roles
 1633: 	if ($key=~/^form\.rev/) {
 1634: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
 1635: # Revoke standard role
 1636: 		my ($scope,$role) = ($1,$2);
 1637: 		my $result =
 1638: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
 1639: 						$env{'form.ccuname'},
 1640: 						$scope,$role);
 1641: 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
 1642: 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
 1643: 		if ($role eq 'st') {
 1644: 		    my $result = &classlist_drop($scope,$env{'form.ccuname'},
 1645: 						 $env{'form.ccdomain'},$now);
 1646: 		    $r->print($result);
 1647: 		}
 1648: 	    } 
 1649: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$ }s) {
 1650: # Revoke custom role
 1651: 		$r->print(&mt('Revoking custom role:').
 1652:                       ' '.$4.' by '.$3.':'.$2.' in '.$1.': <b>'.
 1653:                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
 1654: 				  $env{'form.ccuname'},$1,$2,$3,$4).
 1655: 		'</b><br />');
 1656: 	    }
 1657: 	} elsif ($key=~/^form\.del/) {
 1658: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
 1659: # Delete standard role
 1660: 		my ($scope,$role) = ($1,$2);
 1661: 		my $result =
 1662: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
 1663: 						$env{'form.ccuname'},
 1664: 						$scope,$role,$now,0,1);
 1665: 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
 1666: 			      '<b>'.$result.'</b>').'<br />');
 1667: 		if ($role eq 'st') {
 1668: 		    my $result = &classlist_drop($scope,$env{'form.ccuname'},
 1669: 						 $env{'form.ccdomain'},$now);
 1670: 		    $r->print($result);
 1671: 		}
 1672:             }
 1673: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
 1674:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
 1675: # Delete custom role
 1676:                 $r->print(&mt('Deleting custom role [_1] by [_2]:[_3] in [_4]',
 1677:                       $rolename,$rnam,$rdom,$url).': <b>'.
 1678:                       &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
 1679:                          $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
 1680:                          0,1).'</b><br />');
 1681:             }
 1682: 	} elsif ($key=~/^form\.ren/) {
 1683:             my $udom = $env{'form.ccdomain'};
 1684:             my $uname = $env{'form.ccuname'};
 1685: # Re-enable standard role
 1686: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
 1687:                 my $url = $1;
 1688:                 my $role = $2;
 1689:                 my $logmsg;
 1690:                 my $output;
 1691:                 if ($role eq 'st') {
 1692:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
 1693:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
 1694:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course')) {
 1695:                             $output = "Error: $result\n";
 1696:                         } else {
 1697:                             $output = &mt('Assigning').' '.$role.' in '.$url.
 1698:                                       &mt('starting').' '.localtime($now).
 1699:                                       ': <br />'.$logmsg.'<br />'.
 1700:                                       &mt('Add to classlist').': <b>ok</b><br />';
 1701:                         }
 1702:                     }
 1703:                 } else {
 1704: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
 1705:                                $env{'form.ccuname'},$url,$role,0,$now);
 1706: 		    $output = &mt('Re-enabling [_1] in [_2]: <b>[_3]</b>',
 1707: 			      $role,$url,$result).'<br />';
 1708: 		}
 1709:                 $r->print($output);
 1710: 	    }
 1711: # Re-enable custom role
 1712: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
 1713:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
 1714:                 my $result = &Apache::lonnet::assigncustomrole(
 1715:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
 1716:                                $url,$rdom,$rnam,$rolename,0,$now);
 1717:                 $r->print(&mt('Re-enabling custom role [_1] by [_2]@[_3] in [_4] : <b>[_5]</b>',
 1718:                           $rolename,$rnam,$rdom,$url,$result).'<br />');
 1719:             }
 1720: 	} elsif ($key=~/^form\.act/) {
 1721:             my $udom = $env{'form.ccdomain'};
 1722:             my $uname = $env{'form.ccuname'};
 1723: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
 1724:                 # Activate a custom role
 1725: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
 1726: 		my $url='/'.$one.'/'.$two;
 1727: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
 1728: 
 1729:                 my $start = ( $env{'form.start_'.$full} ?
 1730:                               $env{'form.start_'.$full} :
 1731:                               $now );
 1732:                 my $end   = ( $env{'form.end_'.$full} ?
 1733:                               $env{'form.end_'.$full} :
 1734:                               0 );
 1735:                                                                                      
 1736:                 # split multiple sections
 1737:                 my %sections = ();
 1738:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
 1739:                 if ($num_sections == 0) {
 1740:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end));
 1741:                 } else {
 1742: 		    my %curr_groups =
 1743: 			&Apache::longroup::coursegroups($one,$two);
 1744:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1745:                         if (($sec eq 'none') || ($sec eq 'all') || 
 1746:                             exists($curr_groups{$sec})) {
 1747:                             $disallowed{$sec} = $url;
 1748:                             next;
 1749:                         }
 1750:                         my $securl = $url.'/'.$sec;
 1751: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end));
 1752:                     }
 1753:                 }
 1754: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
 1755: 		# Activate roles for sections with 3 id numbers
 1756: 		# set start, end times, and the url for the class
 1757: 		my ($one,$two,$three)=($1,$2,$3);
 1758: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
 1759: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
 1760: 			      $now );
 1761: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
 1762: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
 1763: 			      0 );
 1764: 		my $url='/'.$one.'/'.$two;
 1765:                 my $type = 'three';
 1766:                 # split multiple sections
 1767:                 my %sections = ();
 1768:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
 1769:                 if ($num_sections == 0) {
 1770:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
 1771:                 } else {
 1772:                     my %curr_groups = 
 1773: 			&Apache::longroup::coursegroups($one,$two);
 1774:                     my $emptysec = 0;
 1775:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1776:                         $sec =~ s/\W//g;
 1777:                         if ($sec ne '') {
 1778:                             if (($sec eq 'none') || ($sec eq 'all') || 
 1779:                                 exists($curr_groups{$sec})) {
 1780:                                 $disallowed{$sec} = $url;
 1781:                                 next;
 1782:                             }
 1783:                             my $securl = $url.'/'.$sec;
 1784:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec));
 1785:                         } else {
 1786:                             $emptysec = 1;
 1787:                         }
 1788:                     }
 1789:                     if ($emptysec) {
 1790:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
 1791:                     }
 1792:                 } 
 1793: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
 1794: 		# Activate roles for sections with two id numbers
 1795: 		# set start, end times, and the url for the class
 1796: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
 1797: 			      $env{'form.start_'.$1.'_'.$2} : 
 1798: 			      $now );
 1799: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
 1800: 			      $env{'form.end_'.$1.'_'.$2} :
 1801: 			      0 );
 1802: 		my $url='/'.$1.'/';
 1803:                 # split multiple sections
 1804:                 my %sections = ();
 1805:                 my $num_sections = &build_roles($env{'form.sec_'.$1.'_'.$2},\%sections,$2);
 1806:                 if ($num_sections == 0) {
 1807:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
 1808:                 } else {
 1809:                     my $emptysec = 0;
 1810:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1811:                         if ($sec ne '') {
 1812:                             my $securl = $url.'/'.$sec;
 1813:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$2,$start,$end,$1,undef,$sec));
 1814:                         } else {
 1815:                             $emptysec = 1;
 1816:                         }
 1817:                     }
 1818:                     if ($emptysec) {
 1819:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
 1820:                     }
 1821:                 }
 1822: 	    } else {
 1823: 		$r->print('<p>'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></p><br />');
 1824:             }
 1825:             foreach my $key (sort(keys(%disallowed))) {
 1826:                 if (($key eq 'none') || ($key eq 'all')) {  
 1827:                     $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key));
 1828:                 } else {
 1829:                     $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));
 1830:                 }
 1831:                 $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />');
 1832:             }
 1833: 	}
 1834:     } # End of foreach (keys(%env))
 1835: # Flush the course logs so reverse user roles immediately updated
 1836:     &Apache::lonnet::flushcourselogs();
 1837:     $r->print('<p><a href="/adm/createuser">'.&mt('Create/Modify Another User').'</a></p>');
 1838:     $r->print('<form name="userupdate" method="post" />'."\n");
 1839:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
 1840:         $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
 1841:     }
 1842:     foreach my $item ('sortby','seluname','seludom') {
 1843:         if (exists($env{'form.'.$item})) {
 1844:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
 1845:         }
 1846:     }
 1847:     $r->print('<input type="hidden" name="phase" value="" />'."\n".
 1848:               '<input type ="hidden" name="currstate" value="" />'."\n".
 1849:               '</form>');
 1850:     $r->print(&Apache::loncommon::end_page());
 1851: }
 1852: 
 1853: sub classlist_drop {
 1854:     my ($scope,$uname,$udom,$now) = @_;
 1855:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
 1856:     my $cid=$cdom.'_'.$cnum;
 1857:     my $user = $uname.':'.$udom;
 1858:     if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
 1859: 	my $result = 
 1860: 	    &Apache::lonnet::cput('classlist',
 1861: 				  { $user => $now },
 1862: 				  $env{'course.'.$cid.'.domain'},
 1863: 				  $env{'course.'.$cid.'.num'});
 1864: 	return &mt('Drop from classlist: [_1]',
 1865: 		   '<b>'.$result.'</b>').'<br />';
 1866:     }
 1867: }
 1868: 
 1869: sub active_student_roles {
 1870:     my ($cnum,$cdom,$uname,$udom) = @_;
 1871:     my %roles = 
 1872: 	&Apache::lonnet::get_my_roles($uname,$udom,'userroles',
 1873: 				      ['future','active'],['st']);
 1874:     return exists($roles{"$cnum:$cdom:st"});
 1875: }
 1876: 
 1877: sub quota_admin {
 1878:     my ($setquota,$changeHash) = @_;
 1879:     my $quotachanged;
 1880:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
 1881:         # Current user has quota modification privileges
 1882:         $quotachanged = 1;
 1883:         $changeHash->{'portfolioquota'} = $setquota;
 1884:     }
 1885:     return $quotachanged;
 1886: }
 1887: 
 1888: sub build_roles {
 1889:     my ($sectionstr,$sections,$role) = @_;
 1890:     my $num_sections = 0;
 1891:     if ($sectionstr=~ /,/) {
 1892:         my @secnums = split/,/,$sectionstr;
 1893:         if ($role eq 'st') {
 1894:             $secnums[0] =~ s/\W//g;
 1895:             $$sections{$secnums[0]} = 1;
 1896:             $num_sections = 1;
 1897:         } else {
 1898:             foreach my $sec (@secnums) {
 1899:                 $sec =~ ~s/\W//g;
 1900:                 if (!($sec eq "")) {
 1901:                     if (exists($$sections{$sec})) {
 1902:                         $$sections{$sec} ++;
 1903:                     } else {
 1904:                         $$sections{$sec} = 1;
 1905:                         $num_sections ++;
 1906:                     }
 1907:                 }
 1908:             }
 1909:         }
 1910:     } else {
 1911:         $sectionstr=~s/\W//g;
 1912:         unless ($sectionstr eq '') {
 1913:             $$sections{$sectionstr} = 1;
 1914:             $num_sections ++;
 1915:         }
 1916:     }
 1917: 
 1918:     return $num_sections;
 1919: }
 1920: 
 1921: # ========================================================== Custom Role Editor
 1922: 
 1923: sub custom_role_editor {
 1924:     my ($r) = @_;
 1925:     my $rolename=$env{'form.rolename'};
 1926: 
 1927:     if ($rolename eq 'make new role') {
 1928: 	$rolename=$env{'form.newrolename'};
 1929:     }
 1930: 
 1931:     $rolename=~s/[^A-Za-z0-9]//gs;
 1932: 
 1933:     if (!$rolename) {
 1934: 	&print_username_entry_form($r);
 1935:         return;
 1936:     }
 1937: # ------------------------------------------------------- What can be assigned?
 1938:     my %full=();
 1939:     my %courselevel=();
 1940:     my %courselevelcurrent=();
 1941:     my $syspriv='';
 1942:     my $dompriv='';
 1943:     my $coursepriv='';
 1944:     my $body_top;
 1945:     my ($disp_dummy,$disp_roles) = &Apache::lonnet::get('roles',["st"]);
 1946:     my ($rdummy,$roledef)=
 1947: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 1948: # ------------------------------------------------------- Does this role exist?
 1949:     $body_top .= '<h2>';
 1950:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 1951: 	$body_top .= &mt('Existing Role').' "';
 1952: # ------------------------------------------------- Get current role privileges
 1953: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 1954:     } else {
 1955: 	$body_top .= &mt('New Role').' "';
 1956: 	$roledef='';
 1957:     }
 1958:     $body_top .= $rolename.'"</h2>';
 1959:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 1960: 	my ($priv,$restrict)=split(/\&/,$item);
 1961:         if (!$restrict) { $restrict='F'; }
 1962:         $courselevel{$priv}=$restrict;
 1963:         if ($coursepriv=~/\:$priv/) {
 1964: 	    $courselevelcurrent{$priv}=1;
 1965: 	}
 1966: 	$full{$priv}=1;
 1967:     }
 1968:     my %domainlevel=();
 1969:     my %domainlevelcurrent=();
 1970:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 1971: 	my ($priv,$restrict)=split(/\&/,$item);
 1972:         if (!$restrict) { $restrict='F'; }
 1973:         $domainlevel{$priv}=$restrict;
 1974:         if ($dompriv=~/\:$priv/) {
 1975: 	    $domainlevelcurrent{$priv}=1;
 1976: 	}
 1977: 	$full{$priv}=1;
 1978:     }
 1979:     my %systemlevel=();
 1980:     my %systemlevelcurrent=();
 1981:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 1982: 	my ($priv,$restrict)=split(/\&/,$item);
 1983:         if (!$restrict) { $restrict='F'; }
 1984:         $systemlevel{$priv}=$restrict;
 1985:         if ($syspriv=~/\:$priv/) {
 1986: 	    $systemlevelcurrent{$priv}=1;
 1987: 	}
 1988: 	$full{$priv}=1;
 1989:     }
 1990:     my ($jsback,$elements) = &crumb_utilities();
 1991:     my $button_code = "\n";
 1992:     my $head_script = "\n";
 1993:     $head_script .= '<script type="text/javascript">'."\n";
 1994:     my @template_roles = ("cc","in","ta","ep","st");
 1995:     foreach my $role (@template_roles) {
 1996:         $head_script .= &make_script_template($role);
 1997:         $button_code .= &make_button_code($role);
 1998:     }
 1999:     $head_script .= "\n".$jsback."\n".'</script>'."\n";
 2000:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',$head_script));
 2001:    &Apache::lonhtmlcommon::add_breadcrumb
 2002:      ({href=>"javascript:backPage(document.form1,'','')",
 2003:        text=>"User modify/custom role edit",
 2004:        faq=>282,bug=>'Instructor Interface',},
 2005:       {href=>"javascript:backPage(document.form1,'','')",
 2006:          text=>"Edit custom role",
 2007:          faq=>282,bug=>'Instructor Interface',});
 2008:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
 2009: 
 2010:     $r->print($body_top);
 2011:     my %lt=&Apache::lonlocal::texthash(
 2012: 		    'prv'  => "Privilege",
 2013: 		    'crl'  => "Course Level",
 2014:                     'dml'  => "Domain Level",
 2015:                     'ssl'  => "System Level");
 2016:     $r->print('Select a Template<br />');
 2017:     $r->print('<form action="">');
 2018:     $r->print($button_code);
 2019:     $r->print('</form>');
 2020:     $r->print(<<ENDCCF);
 2021: <form name="form1" method="post">
 2022: <input type="hidden" name="phase" value="set_custom_roles" />
 2023: <input type="hidden" name="rolename" value="$rolename" />
 2024: ENDCCF
 2025:     $r->print(&Apache::loncommon::start_data_table().
 2026:               &Apache::loncommon::start_data_table_header_row(). 
 2027: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
 2028: '</th><th>'.$lt{'ssl'}.'</th>'.
 2029:               &Apache::loncommon::end_data_table_header_row());
 2030:     foreach my $priv (sort keys %full) {
 2031:         my $privtext = &Apache::lonnet::plaintext($priv);
 2032:         $r->print(&Apache::loncommon::start_data_table_row().
 2033: 	          '<td>'.$privtext.'</td><td>'.
 2034:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c" '.
 2035:     ($courselevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
 2036:     '</td><td>'.
 2037:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d" '.
 2038:     ($domainlevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
 2039:     '</td><td>'.
 2040:     ($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s" '.
 2041:     ($systemlevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
 2042:     '</td>'.
 2043:              &Apache::loncommon::end_data_table_row());
 2044:     }
 2045:     $r->print(&Apache::loncommon::end_data_table().
 2046:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
 2047:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
 2048:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
 2049:    '<input type="submit" value="'.&mt('Define Role').'" /></form>'.
 2050: 	      &Apache::loncommon::end_page());
 2051: }
 2052: # --------------------------------------------------------
 2053: sub make_script_template {
 2054:     my ($role) = @_;
 2055:     my %full_c=();
 2056:     my %full_d=();
 2057:     my %full_s=();
 2058:     my $return_script;
 2059:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 2060:         my ($priv,$restrict)=split(/\&/,$item);
 2061:         $full_c{$priv}=1;
 2062:     }
 2063:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 2064:         my ($priv,$restrict)=split(/\&/,$item);
 2065:         $full_d{$priv}=1;
 2066:     }
 2067:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 2068:         my ($priv,$restrict)=split(/\&/,$item);
 2069:         $full_s{$priv}=1;
 2070:     }
 2071:     $return_script .= 'function set_'.$role.'() {'."\n";
 2072:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
 2073:     my %role_c;
 2074:     foreach my $priv (@temp) {
 2075:         my ($priv_item, $dummy) = split(/\&/,$priv);
 2076:         $role_c{$priv_item} = 1;
 2077:     }
 2078:     foreach my $priv_item (keys(%full_c)) {
 2079:         my ($priv, $dummy) = split(/\&/,$priv_item);
 2080:         if (exists($role_c{$priv})) {
 2081:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
 2082:         } else {
 2083:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
 2084:         }
 2085:     }
 2086:     my %role_d;
 2087:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
 2088:     foreach my $priv(@temp) {
 2089:         my ($priv_item, $dummy) = split(/\&/,$priv);
 2090:         $role_d{$priv_item} = 1;
 2091:     }
 2092:     foreach my $priv_item (keys(%full_d)) {
 2093:         my ($priv, $dummy) = split(/\&/,$priv_item);
 2094:         if (exists($role_d{$priv})) {
 2095:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
 2096:         } else {
 2097:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
 2098:         }
 2099:     }
 2100:     my %role_s;
 2101:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
 2102:     foreach my $priv(@temp) {
 2103:         my ($priv_item, $dummy) = split(/\&/,$priv);
 2104:         $role_s{$priv_item} = 1;
 2105:     }
 2106:     foreach my $priv_item (keys(%full_s)) {
 2107:         my ($priv, $dummy) = split(/\&/,$priv_item);
 2108:         if (exists($role_s{$priv})) {
 2109:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
 2110:         } else {
 2111:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
 2112:         }
 2113:     }
 2114:     $return_script .= '}'."\n";
 2115:     return ($return_script);
 2116: }
 2117: # ----------------------------------------------------------
 2118: sub make_button_code {
 2119:     my ($role) = @_;
 2120:     my $label = &Apache::lonnet::plaintext($role);
 2121:     my $button_code = '<input type="button" onClick="set_'.$role.'()" value="'.$label.'" />';    
 2122:     return ($button_code);
 2123: }
 2124: # ---------------------------------------------------------- Call to definerole
 2125: sub set_custom_role {
 2126:     my ($r) = @_;
 2127: 
 2128:     my $rolename=$env{'form.rolename'};
 2129: 
 2130:     $rolename=~s/[^A-Za-z0-9]//gs;
 2131: 
 2132:     if (!$rolename) {
 2133: 	&print_username_entry_form($r);
 2134:         return;
 2135:     }
 2136: 
 2137:     my ($jsback,$elements) = &crumb_utilities();
 2138:     my $jscript = '<script type="text/javascript">'.$jsback."\n".'</script>';
 2139: 
 2140:     $r->print(&Apache::loncommon::start_page('Save Custom Role'),$jscript);
 2141:     &Apache::lonhtmlcommon::add_breadcrumb
 2142:         ({href=>"javascript:backPage(document.customresult,'','')",
 2143:           text=>"User modify/custom role edit",
 2144:           faq=>282,bug=>'Instructor Interface',},
 2145:          {href=>"javascript:backPage(document.customresult,'selected_custom_edit','')",
 2146:           text=>"Edit custom role",
 2147:           faq=>282,bug=>'Instructor Interface',},
 2148:          {href=>"javascript:backPage(document.customresult,'set_custom_roles','')",
 2149:           text=>"Result",
 2150:           faq=>282,bug=>'Instructor Interface',});
 2151:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
 2152: 
 2153:     my ($rdummy,$roledef)=
 2154: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 2155: 
 2156: # ------------------------------------------------------- Does this role exist?
 2157:     $r->print('<h2>');
 2158:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 2159: 	$r->print(&mt('Existing Role').' "');
 2160:     } else {
 2161: 	$r->print(&mt('New Role').' "');
 2162: 	$roledef='';
 2163:     }
 2164:     $r->print($rolename.'"</h2>');
 2165: # ------------------------------------------------------- What can be assigned?
 2166:     my $sysrole='';
 2167:     my $domrole='';
 2168:     my $courole='';
 2169: 
 2170:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 2171: 	my ($priv,$restrict)=split(/\&/,$item);
 2172:         if (!$restrict) { $restrict=''; }
 2173:         if ($env{'form.'.$priv.'_c'}) {
 2174: 	    $courole.=':'.$item;
 2175: 	}
 2176:     }
 2177: 
 2178:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 2179: 	my ($priv,$restrict)=split(/\&/,$item);
 2180:         if (!$restrict) { $restrict=''; }
 2181:         if ($env{'form.'.$priv.'_d'}) {
 2182: 	    $domrole.=':'.$item;
 2183: 	}
 2184:     }
 2185: 
 2186:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 2187: 	my ($priv,$restrict)=split(/\&/,$item);
 2188:         if (!$restrict) { $restrict=''; }
 2189:         if ($env{'form.'.$priv.'_s'}) {
 2190: 	    $sysrole.=':'.$item;
 2191: 	}
 2192:     }
 2193:     $r->print('<br />Defining Role: '.
 2194: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
 2195:     if ($env{'request.course.id'}) {
 2196:         my $url='/'.$env{'request.course.id'};
 2197:         $url=~s/\_/\//g;
 2198: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
 2199: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
 2200: 						$env{'user.name'},
 2201: 						$url,
 2202: 						$env{'user.domain'},
 2203: 						$env{'user.name'},
 2204: 						$rolename));
 2205:     }
 2206:     $r->print('<p><a href="/adm/createuser">Create another role, or Create/Modify a user.</a></p><form name="customresult" method="post">');
 2207:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
 2208:     $r->print(&Apache::loncommon::end_page());
 2209: }
 2210: 
 2211: # ================================================================ Main Handler
 2212: sub handler {
 2213:     my $r = shift;
 2214: 
 2215:     if ($r->header_only) {
 2216:        &Apache::loncommon::content_type($r,'text/html');
 2217:        $r->send_http_header;
 2218:        return OK;
 2219:     }
 2220: 
 2221:     if ((&Apache::lonnet::allowed('cta',$env{'request.course.id'})) ||
 2222:         (&Apache::lonnet::allowed('cin',$env{'request.course.id'})) || 
 2223:         (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) || 
 2224:         (&Apache::lonnet::allowed('cep',$env{'request.course.id'})) ||
 2225: 	(&authorpriv($env{'user.name'},$env{'request.role.domain'})) ||
 2226:         (&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))) {
 2227:        &Apache::loncommon::content_type($r,'text/html');
 2228:        $r->send_http_header;
 2229:        &Apache::lonhtmlcommon::clear_breadcrumbs();
 2230:       
 2231:        my $phase = $env{'form.phase'};
 2232:        my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
 2233: 
 2234:        if (($phase eq 'get_user_info') || ($phase eq 'userpicked')) {
 2235:            my $srch;
 2236:            foreach my $item (@search) {
 2237:                $srch->{$item} = $env{'form.'.$item};
 2238:            }
 2239:            if ($env{'form.phase'} eq 'get_user_info') {
 2240:                my ($currstate,$response,$forcenewuser,$results) = 
 2241:                    &user_search_result($srch);
 2242:                if ($currstate eq 'select') {
 2243:                    &print_user_selection_page($r,$response,$srch,$results,'createuser',\@search);
 2244:                } elsif ($currstate eq 'modify') {
 2245:                    my ($ccuname,$ccdomain);
 2246:                    if (($srch->{'srchby'} eq 'uname') && 
 2247:                        ($srch->{'srchtype'} eq 'exact')) {
 2248:                        $ccuname = $srch->{'srchterm'};
 2249:                        $ccdomain= $srch->{'srchdomain'};
 2250:                    } else {
 2251:                        my @matchedunames = keys(%{$results});
 2252:                        ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
 2253:                    }
 2254:                    $ccuname =&LONCAPA::clean_username($ccuname);
 2255:                    $ccdomain=&LONCAPA::clean_domain($ccdomain);
 2256:                    &print_user_modification_page($r,$ccuname,$ccdomain,$srch,
 2257:                                                  $response);
 2258:                } elsif ($currstate eq 'query') {
 2259:                    &print_user_query_page($r,'createuser');
 2260:                } else {
 2261:                    &print_username_entry_form($r,$response,$srch,$forcenewuser);
 2262:                }
 2263:            } elsif ($env{'form.phase'} eq 'userpicked') {
 2264:                my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
 2265:                my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
 2266:                &print_user_modification_page($r,$ccuname,$ccdomain,$srch);
 2267:            }
 2268:        } elsif ($env{'form.phase'} eq 'update_user_data') {
 2269:            &update_user_data($r);
 2270:        } elsif ($env{'form.phase'} eq 'selected_custom_edit') {
 2271:            &custom_role_editor($r);
 2272:        } elsif ($env{'form.phase'} eq 'set_custom_roles') {
 2273: 	   &set_custom_role($r);
 2274:        } else {
 2275:            &print_username_entry_form($r);
 2276:        }
 2277:    } else {
 2278:       $env{'user.error.msg'}=
 2279:         "/adm/createuser:mau:0:0:Cannot modify user data";
 2280:       return HTTP_NOT_ACCEPTABLE; 
 2281:    }
 2282:    return OK;
 2283: }
 2284: 
 2285: #-------------------------------------------------- functions for &phase_two
 2286: sub user_search_result {
 2287:     my ($srch) = @_;
 2288:     my %allhomes;
 2289:     my %inst_matches;
 2290:     my %srch_results;
 2291:     my ($response,$currstate,$forcenewuser,$dirsrchres);
 2292:     $srch->{'srchterm'} =~ s/\s+/ /g;
 2293:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
 2294:         $response = &mt('Invalid search.');
 2295:     }
 2296:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
 2297:         $response = &mt('Invalid search.');
 2298:     }
 2299:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
 2300:         $response = &mt('Invalid search.');
 2301:     }
 2302:     if ($srch->{'srchterm'} eq '') {
 2303:         $response = &mt('You must enter a search term.');
 2304:     }
 2305:     if ($srch->{'srchterm'} =~ /^\s+$/) {
 2306:         $response = &mt('Your search term must contain more than just spaces.');
 2307:     }
 2308:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
 2309:         if (($srch->{'srchdomain'} eq '') || 
 2310: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
 2311:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
 2312:         }
 2313:     }
 2314:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
 2315:         ($srch->{'srchin'} eq 'alc')) {
 2316:         if ($srch->{'srchby'} eq 'uname') {
 2317:             if ($srch->{'srchterm'} !~ /^$match_username$/) {
 2318:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
 2319:             }
 2320:         }
 2321:     }
 2322:     if ($response ne '') {
 2323:         $response = '<span class="LC_warning">'.$response.'</span>';
 2324:     }
 2325:     if ($srch->{'srchin'} eq 'instd') {
 2326:         my $instd_chk = &directorysrch_check($srch);
 2327:         if ($instd_chk ne 'ok') {
 2328:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
 2329:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
 2330:         }
 2331:     }
 2332:     if ($response ne '') {
 2333:         return ($currstate,$response);
 2334:     }
 2335:     if ($srch->{'srchby'} eq 'uname') {
 2336:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
 2337:             if ($env{'form.forcenew'}) {
 2338:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
 2339:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
 2340:                     if ($uhome eq 'no_host') {
 2341:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
 2342:                         my $showdom = &display_domain_info($env{'request.role.domain'});
 2343:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
 2344:                     } else {
 2345:                         $currstate = 'modify';
 2346:                     }
 2347:                 } else {
 2348:                     $currstate = 'modify';
 2349:                 }
 2350:             } else {
 2351:                 if ($srch->{'srchin'} eq 'dom') {
 2352:                     if ($srch->{'srchtype'} eq 'exact') {
 2353:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
 2354:                         if ($uhome eq 'no_host') {
 2355:                             ($currstate,$response,$forcenewuser) =
 2356:                                 &build_search_response($srch,%srch_results);
 2357:                         } else {
 2358:                             $currstate = 'modify';
 2359:                         }
 2360:                     } else {
 2361:                         %srch_results = &Apache::lonnet::usersearch($srch);
 2362:                         ($currstate,$response,$forcenewuser) =
 2363:                             &build_search_response($srch,%srch_results);
 2364:                     }
 2365:                 } else {
 2366:                     my $courseusers = &get_courseusers();
 2367:                     if ($srch->{'srchtype'} eq 'exact') {
 2368:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
 2369:                             $currstate = 'modify';
 2370:                         } else {
 2371:                             ($currstate,$response,$forcenewuser) =
 2372:                                 &build_search_response($srch,%srch_results);
 2373:                         }
 2374:                     } else {
 2375:                         foreach my $user (keys(%$courseusers)) {
 2376:                             my ($cuname,$cudomain) = split(/:/,$user);
 2377:                             if ($cudomain eq $srch->{'srchdomain'}) {
 2378:                                 my $matched = 0;
 2379:                                 if ($srch->{'srchtype'} eq 'begins') {
 2380:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
 2381:                                         $matched = 1;
 2382:                                     }
 2383:                                 } else {
 2384:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
 2385:                                         $matched = 1;
 2386:                                     }
 2387:                                 }
 2388:                                 if ($matched) {
 2389:                                     $srch_results{$user} = 
 2390: 					{&Apache::lonnet::get('environment',
 2391: 							     ['firstname',
 2392: 							      'lastname',
 2393: 							      'permanentemail'])};
 2394:                                 }
 2395:                             }
 2396:                         }
 2397:                         ($currstate,$response,$forcenewuser) =
 2398:                             &build_search_response($srch,%srch_results);
 2399:                     }
 2400:                 }
 2401:             }
 2402:         } elsif ($srch->{'srchin'} eq 'alc') {
 2403:             $currstate = 'query';
 2404:         } elsif ($srch->{'srchin'} eq 'instd') {
 2405:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
 2406:             if ($dirsrchres eq 'ok') {
 2407:                 ($currstate,$response,$forcenewuser) = 
 2408:                     &build_search_response($srch,%srch_results);
 2409:             } else {
 2410:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
 2411:                 $response = '<span class="LC_warning">'.
 2412:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
 2413:                     '</span><br />'.
 2414:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
 2415:                     '<br /><br />'; 
 2416:             }
 2417:         }
 2418:     } else {
 2419:         if ($srch->{'srchin'} eq 'dom') {
 2420:             %srch_results = &Apache::lonnet::usersearch($srch);
 2421:             ($currstate,$response,$forcenewuser) = 
 2422:                 &build_search_response($srch,%srch_results); 
 2423:         } elsif ($srch->{'srchin'} eq 'crs') {
 2424:             my $courseusers = &get_courseusers(); 
 2425:             foreach my $user (keys(%$courseusers)) {
 2426:                 my ($uname,$udom) = split(/:/,$user);
 2427:                 my %names = &Apache::loncommon::getnames($uname,$udom);
 2428:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
 2429:                 if ($srch->{'srchby'} eq 'lastname') {
 2430:                     if ((($srch->{'srchtype'} eq 'exact') && 
 2431:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
 2432:                         (($srch->{'srchtype'} eq 'begins') &&
 2433:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
 2434:                         (($srch->{'srchtype'} eq 'contains') &&
 2435:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
 2436:                         $srch_results{$user} = {firstname => $names{'firstname'},
 2437:                                             lastname => $names{'lastname'},
 2438:                                             permanentemail => $emails{'permanentemail'},
 2439:                                            };
 2440:                     }
 2441:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
 2442:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
 2443:                     $srchlast =~ s/\s+$//;
 2444:                     $srchfirst =~ s/^\s+//;
 2445:                     if ($srch->{'srchtype'} eq 'exact') {
 2446:                         if (($names{'lastname'} eq $srchlast) &&
 2447:                             ($names{'firstname'} eq $srchfirst)) {
 2448:                             $srch_results{$user} = {firstname => $names{'firstname'},
 2449:                                                 lastname => $names{'lastname'},
 2450:                                                 permanentemail => $emails{'permanentemail'},
 2451: 
 2452:                                            };
 2453:                         }
 2454:                     } elsif ($srch->{'srchtype'} eq 'begins') {
 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:                     } else {
 2463:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
 2464:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
 2465:                             $srch_results{$user} = {firstname => $names{'firstname'},
 2466:                                                 lastname => $names{'lastname'},
 2467:                                                 permanentemail => $emails{'permanentemail'},
 2468:                                                };
 2469:                         }
 2470:                     }
 2471:                 }
 2472:             }
 2473:             ($currstate,$response,$forcenewuser) = 
 2474:                 &build_search_response($srch,%srch_results); 
 2475:         } elsif ($srch->{'srchin'} eq 'alc') {
 2476:             $currstate = 'query';
 2477:         } elsif ($srch->{'srchin'} eq 'instd') {
 2478:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
 2479:             if ($dirsrchres eq 'ok') {
 2480:                 ($currstate,$response,$forcenewuser) = 
 2481:                     &build_search_response($srch,%srch_results);
 2482:             } else {
 2483:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
 2484:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
 2485:                     '</span><br />'.
 2486:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
 2487:                     '<br /><br />';
 2488:             }
 2489:         }
 2490:     }
 2491:     return ($currstate,$response,$forcenewuser,\%srch_results);
 2492: }
 2493: 
 2494: sub directorysrch_check {
 2495:     my ($srch) = @_;
 2496:     my $can_search = 0;
 2497:     my $response;
 2498:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
 2499:                                              ['directorysrch'],$srch->{'srchdomain'});
 2500:     my $showdom = &display_domain_info($srch->{'srchdomain'});
 2501:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
 2502:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
 2503:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
 2504:         }
 2505:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
 2506:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
 2507:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
 2508:             }
 2509:             my @usertypes = split(/:/,$env{'environment.inststatus'});
 2510:             if (!@usertypes) {
 2511:                 push(@usertypes,'default');
 2512:             }
 2513:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
 2514:                 foreach my $type (@usertypes) {
 2515:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
 2516:                         $can_search = 1;
 2517:                         last;
 2518:                     }
 2519:                 }
 2520:             }
 2521:             if (!$can_search) {
 2522:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
 2523:                 my @longtypes; 
 2524:                 foreach my $item (@usertypes) {
 2525:                     push (@longtypes,$insttypes->{$item});
 2526:                 }
 2527:                 my $insttype_str = join(', ',@longtypes); 
 2528:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
 2529:             } 
 2530:         } else {
 2531:             $can_search = 1;
 2532:         }
 2533:     } else {
 2534:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
 2535:     }
 2536:     my %longtext = &Apache::lonlocal::texthash (
 2537:                        uname     => 'username',
 2538:                        lastfirst => 'last name, first name',
 2539:                        lastname  => 'last name',
 2540:                        contains  => 'contains',
 2541:                        exact     => 'as exact match to',
 2542:                        begins    => 'begins with',
 2543:                    );
 2544:     if ($can_search) {
 2545:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
 2546:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
 2547:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
 2548:             }
 2549:         } else {
 2550:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
 2551:         }
 2552:     }
 2553:     if ($can_search) {
 2554:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
 2555:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
 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:         } else {
 2561:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
 2562:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
 2563:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
 2564:                 return 'ok';
 2565:             } else {
 2566:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
 2567:             }
 2568:         }
 2569:     }
 2570: }
 2571: 
 2572: sub get_courseusers {
 2573:     my %advhash;
 2574:     my $classlist = &Apache::loncoursedata::get_classlist();
 2575:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
 2576:     foreach my $role (sort(keys(%coursepersonnel))) {
 2577:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
 2578: 	    if (!exists($classlist->{$user})) {
 2579: 		$classlist->{$user} = [];
 2580: 	    }
 2581:         }
 2582:     }
 2583:     return $classlist;
 2584: }
 2585: 
 2586: sub build_search_response {
 2587:     my ($srch,%srch_results) = @_;
 2588:     my ($currstate,$response,$forcenewuser);
 2589:     my %names = (
 2590:           'uname' => 'username',
 2591:           'lastname' => 'last name',
 2592:           'lastfirst' => 'last name, first name',
 2593:           'crs' => 'this course',
 2594:           'dom' => 'LON-CAPA domain: ',
 2595:           'instd' => 'the institutional directory for domain: ',
 2596:     );
 2597: 
 2598:     my %single = (
 2599:                    begins   => 'A match',
 2600:                    contains => 'A match',
 2601:                    exact    => 'An exact match',
 2602:                  );
 2603:     my %nomatch = (
 2604:                    begins   => 'No match',
 2605:                    contains => 'No match',
 2606:                    exact    => 'No exact match',
 2607:                   );
 2608:     if (keys(%srch_results) > 1) {
 2609:         $currstate = 'select';
 2610:     } else {
 2611:         if (keys(%srch_results) == 1) {
 2612:             $currstate = 'modify';
 2613:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
 2614:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
 2615:                 $response .= &display_domain_info($srch->{'srchdomain'});
 2616:             }
 2617:         } else {
 2618:             $response = '<span class="LC_warning">'.&mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}",$srch->{'srchterm'});
 2619:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
 2620:                 $response .= &display_domain_info($srch->{'srchdomain'});
 2621:             }
 2622:             $response .= '</span>';
 2623:             if ($srch->{'srchin'} ne 'alc') {
 2624:                 $forcenewuser = 1;
 2625:                 my $cansrchinst = 0; 
 2626:                 if ($srch->{'srchdomain'}) {
 2627:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
 2628:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 2629:                         if ($domconfig{'directorysrch'}{'available'}) {
 2630:                             $cansrchinst = 1;
 2631:                         } 
 2632:                     }
 2633:                 }
 2634:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
 2635:                      ($srch->{'srchby'} eq 'lastname')) &&
 2636:                     ($srch->{'srchin'} eq 'dom')) {
 2637:                     if ($cansrchinst) {
 2638:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
 2639:                     }
 2640:                 }
 2641:                 if ($srch->{'srchin'} eq 'crs') {
 2642:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
 2643:                 }
 2644:             }
 2645:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $env{'request.role.domain'})) {
 2646:                 my $showdom = &display_domain_info($env{'request.role.domain'}); 
 2647:                 $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 />';
 2648:             }
 2649:         }
 2650:     }
 2651:     return ($currstate,$response,$forcenewuser);
 2652: }
 2653: 
 2654: sub display_domain_info {
 2655:     my ($dom) = @_;
 2656:     my $output = $dom;
 2657:     if ($dom ne '') { 
 2658:         my $domdesc = &Apache::lonnet::domain($dom,'description');
 2659:         if ($domdesc ne '') {
 2660:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
 2661:         }
 2662:     }
 2663:     return $output;
 2664: }
 2665: 
 2666: sub crumb_utilities {
 2667:     my %elements = (
 2668:        crtuser => {
 2669:            srchterm => 'text',
 2670:            srchin => 'selectbox',
 2671:            srchby => 'selectbox',
 2672:            srchtype => 'selectbox',
 2673:            srchdomain => 'selectbox',
 2674:        },
 2675:        docustom => {
 2676:            rolename => 'selectbox',
 2677:            newrolename => 'textbox',
 2678:        },
 2679:        studentform => {
 2680:            srchterm => 'text',
 2681:            srchin => 'selectbox',
 2682:            srchby => 'selectbox',
 2683:            srchtype => 'selectbox',
 2684:            srchdomain => 'selectbox',
 2685:        },
 2686:     );
 2687: 
 2688:     my $jsback .= qq|
 2689: function backPage(formname,prevphase,prevstate) {
 2690:     formname.phase.value = prevphase;
 2691:     formname.currstate.value = prevstate;
 2692:     formname.submit();
 2693: }
 2694: |;
 2695:     return ($jsback,\%elements);
 2696: }
 2697: 
 2698: sub course_level_table {
 2699:     my (%inccourses) = @_;
 2700:     my $table = '';
 2701: # Custom Roles?
 2702: 
 2703:     my %customroles=&my_custom_roles();
 2704:     my %lt=&Apache::lonlocal::texthash(
 2705:             'exs'  => "Existing sections",
 2706:             'new'  => "Define new section",
 2707:             'ssd'  => "Set Start Date",
 2708:             'sed'  => "Set End Date",
 2709:             'crl'  => "Course Level",
 2710:             'act'  => "Activate",
 2711:             'rol'  => "Role",
 2712:             'ext'  => "Extent",
 2713:             'grs'  => "Section",
 2714:             'sta'  => "Start",
 2715:             'end'  => "End"
 2716:     );
 2717: 
 2718:     foreach my $protectedcourse (sort( keys(%inccourses))) {
 2719: 	my $thiscourse=$protectedcourse;
 2720: 	$thiscourse=~s:_:/:g;
 2721: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
 2722: 	my $area=$coursedata{'description'};
 2723:         my $type=$coursedata{'type'};
 2724: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
 2725: 	my ($domain,$cnum)=split(/\//,$thiscourse);
 2726:         my %sections_count;
 2727:         if (defined($env{'request.course.id'})) {
 2728:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
 2729:                 %sections_count = 
 2730: 		    &Apache::loncommon::get_sections($domain,$cnum);
 2731:             }
 2732:         }
 2733: 	foreach my $role ('st','ta','ep','in','cc') {
 2734: 	    if (&Apache::lonnet::allowed('c'.$role,$thiscourse)) {
 2735: 		my $plrole=&Apache::lonnet::plaintext($role);
 2736: 		$table .= &Apache::loncommon::start_data_table_row().
 2737: '<td><input type="checkbox" name="act_'.$protectedcourse.'_'.$role.'" /></td>
 2738: <td>'.$plrole.'</td>
 2739: <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
 2740: 	        if ($role ne 'cc') {
 2741:                     if (%sections_count) {
 2742:                         my $currsec = &course_sections(\%sections_count,$protectedcourse.'_'.$role);
 2743:                         $table .= 
 2744:                     '<td><table class="LC_createuser">'.
 2745:                      '<tr class="LC_section_row">
 2746:                         <td valign="top">'.$lt{'exs'}.'<br />'.
 2747:                         $currsec.'</td>'.
 2748:                      '<td>&nbsp;&nbsp;</td>'.
 2749:                      '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
 2750:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.'" value="" />'.
 2751:                      '<input type="hidden" '.
 2752:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'.
 2753:                      '</tr></table></td>';
 2754:                     } else {
 2755:                         $table .= '<td><input type="text" size="10" '.
 2756:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>';
 2757:                     }
 2758:                 } else { 
 2759: 		    $table .= '<td>&nbsp</td>';
 2760:                 }
 2761: 		$table .= <<ENDTIMEENTRY;
 2762: <td><input type="hidden" name="start_$protectedcourse\_$role" value='' />
 2763: <a href=
 2764: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt{'ssd'}</a></td>
 2765: <td><input type="hidden" name="end_$protectedcourse\_$role" value='' />
 2766: <a href=
 2767: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt{'sed'}</a></td>
 2768: ENDTIMEENTRY
 2769:                 $table.= &Apache::loncommon::end_data_table_row();
 2770:             }
 2771:         }
 2772:         foreach my $cust (sort keys %customroles) {
 2773: 	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
 2774: 		my $plrole=$cust;
 2775:                 my $customrole=$protectedcourse.'_cr_cr_'.$env{'user.domain'}.
 2776: 		    '_'.$env{'user.name'}.'_'.$plrole;
 2777: 		$table .= &Apache::loncommon::start_data_table_row().
 2778: '<td><input type="checkbox" name="act_'.$customrole.'" /></td>
 2779: <td>'.$plrole.'</td>
 2780: <td>'.$area.'</td>'."\n";
 2781:                 if (%sections_count) {
 2782:                     my $currsec = &course_sections(\%sections_count,$customrole);
 2783:                     $table.=
 2784:                    '<td><table border="0" cellspacing="0" cellpadding="0">'.
 2785:                    '<tr><td valign="top">'.$lt{'exs'}.'<br />'.
 2786:                      $currsec.'</td>'.
 2787:                    '<td>&nbsp;&nbsp;</td>'.
 2788:                    '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
 2789:                    '<input type="text" name="newsec_'.$customrole.'" value="" /></td>'.
 2790:                    '<input type="hidden" '.
 2791:                    'name="sec_'.$customrole.'" /></td>'.
 2792:                    '</tr></table></td>';
 2793:                 } else {
 2794:                     $table .= '<td><input type="text" size="10" '.
 2795:                      'name="sec_'.$customrole.'" /></td>';
 2796:                 }
 2797:                 $table .= <<ENDENTRY;
 2798: <td><input type="hidden" name="start_$customrole" value='' />
 2799: <a href=
 2800: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">$lt{'ssd'}</a></td>
 2801: <td><input type="hidden" name="end_$customrole" value='' />
 2802: <a href=
 2803: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">$lt{'sed'}</a></td>
 2804: ENDENTRY
 2805:                $table .= &Apache::loncommon::end_data_table_row();
 2806:            }
 2807: 	}
 2808:     }
 2809:     return '' if ($table eq ''); # return nothing if there is nothing 
 2810:                                  # in the table
 2811:     my $result = '
 2812: <h4>'.$lt{'crl'}.'</h4>'.
 2813: &Apache::loncommon::start_data_table().
 2814: &Apache::loncommon::start_data_table_header_row().
 2815: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>
 2816: <th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
 2817: &Apache::loncommon::end_data_table_header_row().
 2818: $table.
 2819: &Apache::loncommon::end_data_table();
 2820:     return $result;
 2821: }
 2822: 
 2823: sub course_sections {
 2824:     my ($sections_count,$role) = @_;
 2825:     my $output = '';
 2826:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
 2827:     if (scalar(@sections) == 1) {
 2828:         $output = '<select name="currsec_'.$role.'" >'."\n".
 2829:                   '  <option value="">Select</option>'."\n".
 2830:                   '  <option value="">No section</option>'."\n".
 2831:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
 2832:     } else {
 2833:         $output = '<select name="currsec_'.$role.'" ';
 2834:         my $multiple = 4;
 2835:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
 2836:         $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
 2837:         foreach my $sec (@sections) {
 2838:             $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
 2839:         }
 2840:     }
 2841:     $output .= '</select>'; 
 2842:     return $output;
 2843: }
 2844: 
 2845: sub course_level_dc {
 2846:     my ($dcdom) = @_;
 2847:     my %customroles=&my_custom_roles();
 2848:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
 2849:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
 2850:                       '<input type="hidden" name="dccourse" value="" />';
 2851:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
 2852:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course').'</b>';
 2853:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu');
 2854:     my %lt=&Apache::lonlocal::texthash(
 2855:                     'rol'  => "Role",
 2856:                     'grs'  => "Section",
 2857:                     'exs'  => "Existing sections",
 2858:                     'new'  => "Define new section", 
 2859:                     'sta'  => "Start",
 2860:                     'end'  => "End",
 2861:                     'ssd'  => "Set Start Date",
 2862:                     'sed'  => "Set End Date"
 2863:                   );
 2864:     my $header = '<h4>'.&mt('Course Level').'</h4>'.
 2865:                  &Apache::loncommon::start_data_table().
 2866:                  &Apache::loncommon::start_data_table_header_row().
 2867:                  '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
 2868:                  &Apache::loncommon::end_data_table_header_row();
 2869:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
 2870:                      '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
 2871:                      '<td><select name="role">'."\n";
 2872:     foreach  my $role ('st','ta','ep','in','cc') {
 2873:         my $plrole=&Apache::lonnet::plaintext($role);
 2874:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
 2875:     }
 2876:     if ( keys %customroles > 0) {
 2877:         foreach my $cust (sort keys %customroles) {
 2878:             my $custrole='cr_cr_'.$env{'user.domain'}.
 2879:                     '_'.$env{'user.name'}.'_'.$cust;
 2880:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
 2881:         }
 2882:     }
 2883:     $otheritems .= '</select></td><td>'.
 2884:                      '<table border="0" cellspacing="0" cellpadding="0">'.
 2885:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
 2886:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
 2887:                      '<td>&nbsp;&nbsp;</td>'.
 2888:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
 2889:                      '<input type="text" name="newsec" value="" />'.
 2890:                      '<input type="hidden" name="groups" value="" /></td>'.
 2891:                      '</tr></table></td>';
 2892:     $otheritems .= <<ENDTIMEENTRY;
 2893: <td><input type="hidden" name="start" value='' />
 2894: <a href=
 2895: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
 2896: <td><input type="hidden" name="end" value='' />
 2897: <a href=
 2898: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
 2899: ENDTIMEENTRY
 2900:     $otheritems .= &Apache::loncommon::end_data_table_row().
 2901:                    &Apache::loncommon::end_data_table()."\n";
 2902:     return $cb_jscript.$header.$hiddenitems.$otheritems;
 2903: }
 2904: 
 2905: #---------------------------------------------- end functions for &phase_two
 2906: 
 2907: #--------------------------------- functions for &phase_two and &phase_three
 2908: 
 2909: #--------------------------end of functions for &phase_two and &phase_three
 2910: 
 2911: 1;
 2912: __END__
 2913: 
 2914: 

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