File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.184.2.1: download - view: text, annotated - select for diffs
Wed Oct 3 13:45:58 2007 UTC (16 years, 7 months ago) by albertel
Branches: version_2_5_X
CVS tags: version_2_5_2
- backport 1.186

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

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