File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.114: download - view: text, annotated - select for diffs
Tue May 9 14:38:10 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changing loncommon::coursegroups to be easier to use

    1: # The LearningOnline Network with CAPA
    2: # Create a user
    3: #
    4: # $Id: loncreateuser.pm,v 1.114 2006/05/09 14:38:10 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: 
   68: my $loginscript; # piece of javascript used in two separate instances
   69: my $generalrule;
   70: my $authformnop;
   71: my $authformkrb;
   72: my $authformint;
   73: my $authformfsys;
   74: my $authformloc;
   75: 
   76: sub initialize_authen_forms {
   77:     my ($krbdefdom)=( $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/);
   78:     $krbdefdom= uc($krbdefdom);
   79:     my %param = ( formname => 'document.cu',
   80:                   kerb_def_dom => $krbdefdom 
   81:                   );
   82: # no longer static due to configurable kerberos defaults
   83: #    $loginscript  = &Apache::loncommon::authform_header(%param);
   84:     $generalrule  = &Apache::loncommon::authform_authorwarning(%param);
   85:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
   86: # no longer static due to configurable kerberos defaults
   87: #    $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
   88:     $authformint  = &Apache::loncommon::authform_internal(%param);
   89:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
   90:     $authformloc  = &Apache::loncommon::authform_local(%param);
   91: }
   92: 
   93: 
   94: # ======================================================= Existing Custom Roles
   95: 
   96: sub my_custom_roles {
   97:     my %returnhash=();
   98:     my %rolehash=&Apache::lonnet::dump('roles');
   99:     foreach (keys %rolehash) {
  100: 	if ($_=~/^rolesdef\_(\w+)$/) {
  101: 	    $returnhash{$1}=$1;
  102: 	}
  103:     }
  104:     return %returnhash;
  105: }
  106: 
  107: # ==================================================== Figure out author access
  108: 
  109: sub authorpriv {
  110:     my ($auname,$audom)=@_;
  111:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
  112:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }
  113:     return 1;
  114: }
  115: 
  116: # =================================================================== Phase one
  117: 
  118: sub print_username_entry_form {
  119:     my ($r) = @_;
  120:     my $defdom=$env{'request.role.domain'};
  121:     my @domains = &Apache::loncommon::get_domains();
  122:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
  123:     my $selscript=&Apache::loncommon::studentbrowser_javascript();
  124:     my $start_page =
  125: 	&Apache::loncommon::start_page('Create Users, Change User Privileges',
  126: 				       $selscript);
  127: 
  128:     my $sellink=&Apache::loncommon::selectstudent_link
  129:                                         ('crtuser','ccuname','ccdomain');
  130:     my %existingroles=&my_custom_roles();
  131:     my $choice=&Apache::loncommon::select_form('make new role','rolename',
  132: 		('make new role' => 'Generate new role ...',%existingroles));
  133:     my %lt=&Apache::lonlocal::texthash(
  134: 		    'siur'   => "Set Individual User Roles",
  135: 		    'usr'  => "Username",
  136:                     'dom'  => "Domain",
  137:                     'usrr' => "User Roles",
  138:                     'ecrp' => "Edit Custom Role Privileges",
  139:                     'nr'   => "Name of Role",
  140:                     'cre'  => "Custom Role Editor"
  141: 				       );
  142:     my $help = &Apache::loncommon::help_open_menu('',undef,undef,'',282,'Instructor Interface');
  143:     my $helpsiur=&Apache::loncommon::help_open_topic('Course_Change_Privileges');
  144:     my $helpecpr=&Apache::loncommon::help_open_topic('Course_Editing_Custom_Roles');
  145:     $r->print(<<"ENDDOCUMENT");
  146: $start_page
  147: <form action="/adm/createuser" method="post" name="crtuser">
  148: <input type="hidden" name="phase" value="get_user_info">
  149: <h2>$lt{siur}$helpsiur</h2>
  150: <table>
  151: <tr><td>$lt{usr}:</td><td><input type="text" size="15" name="ccuname">
  152: </td><td rowspan="2">$sellink</td></tr><tr><td>
  153: $lt{'dom'}:</td><td>$domform</td></tr>
  154: </table>
  155: <input name="userrole" type="submit" value="$lt{usrr}" />
  156: </form>
  157: ENDDOCUMENT
  158:    if (&Apache::lonnet::allowed('mcr','/')) {
  159:        $r->print(<<ENDCUSTOM);
  160: <form action="/adm/createuser" method="post" name="docustom">
  161: <input type="hidden" name="phase" value="selected_custom_edit">
  162: <h2>$lt{'ecrp'}$helpecpr</h2>
  163: $lt{'nr'}: $choice <input type="text" size="15" name="newrolename" /><br />
  164: <input name="customeditor" type="submit" value="$lt{'cre'}" />
  165: </form>
  166: ENDCUSTOM
  167:     }
  168:     $r->print(&Apache::loncommon::end_page());
  169: }
  170: 
  171: 
  172: sub user_modification_js {
  173:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
  174:     
  175:     return <<END;
  176: <script type="text/javascript" language="Javascript">
  177: 
  178:     function pclose() {
  179:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  180:                  "height=350,width=350,scrollbars=no,menubar=no");
  181:         parmwin.close();
  182:     }
  183: 
  184:     $pjump_def
  185:     $dc_setcourse_code
  186: 
  187:     function dateset() {
  188:         eval("document.cu."+document.cu.pres_marker.value+
  189:             ".value=document.cu.pres_value.value");
  190:         pclose();
  191:     }
  192: 
  193:     $nondc_setsection_code
  194: 
  195:     function setSections() {
  196:         var re1 = /^currsec_/;
  197:         var groups = new Array($groupslist);
  198:         for (var i=0;i<document.cu.elements.length;i++) {
  199:             var str = document.cu.elements[i].name;
  200:             var checkcurr = str.match(re1);
  201:             if (checkcurr != null) {
  202:                 var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/;
  203:                 if (document.cu.elements[i-1].checked == true) {
  204:                     var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/;
  205:                     match = re2.exec(str);
  206:                     var role = match[1];
  207:                     if (role == 'cc') {
  208:                         alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
  209:                     }
  210:                     else {
  211:                         var sections = '';
  212:                         var numsec = 0;
  213:                         var sections;
  214:                         for (var j=0; j<document.cu.elements[i].length; j++) {
  215:                             if (document.cu.elements[i].options[j].selected == true ) {
  216:                                 if (document.cu.elements[i].options[j].value != "") {
  217:                                     if (numsec == 0) {
  218:                                         if (document.cu.elements[i].options[j].value != "") {
  219:                                             sections = document.cu.elements[i].options[j].value;
  220:                                             numsec ++;
  221:                                         }
  222:                                     }
  223:                                     else {
  224:                                         sections = sections + "," +  document.cu.elements[i].options[j].value
  225:                                         numsec ++;
  226:                                     }
  227:                                 }
  228:                             }
  229:                         }
  230:                         if (numsec > 0) {
  231:                             if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) {
  232:                                 sections = sections + "," +  document.cu.elements[i+1].value;
  233:                             }
  234:                         }
  235:                         else {
  236:                             sections = document.cu.elements[i+1].value;    
  237:                         }
  238:                         var newsecs = document.cu.elements[i+1].value;
  239:                         if (newsecs != null && newsecs != "") {
  240:                             var numsplit = newsecs.split(/,/g);
  241:                             numsec = numsec + numsplit.length;
  242:                         }
  243:                         if ((role == 'st') && (numsec > 1)) {
  244:                             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.")  
  245:                             return;
  246:                         }
  247:                         else {
  248:                             for (var j=0; j<numsplit.length; j++) {
  249:                                 if ((numsplit[j] == 'all') || 
  250:                                     (numsplit[j] == 'none')) {
  251:                                     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.");
  252:                                     return;
  253:                                 }
  254:                                 for (var k=0; k<groups.length; k++) {
  255:                                     if (numsplit[j] == groups[k]) {
  256:                                         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.");
  257:                                         return;
  258:                                     }
  259:                                 }
  260:                             }
  261:                             document.cu.elements[i+2].value = sections;
  262:                         }
  263:                     }
  264:                 }
  265:             }
  266:         }
  267:         document.cu.submit();
  268:     }
  269: </script>
  270: END
  271: }
  272: 
  273: # =================================================================== Phase two
  274: sub print_user_modification_page {
  275:     my $r=shift;
  276:     my $ccuname=$env{'form.ccuname'};
  277:     my $ccdomain=$env{'form.ccdomain'};
  278: 
  279:     $ccuname=~s/\W//g;
  280:     $ccdomain=~s/\W//g;
  281: 
  282:     unless (($ccuname) && ($ccdomain)) {
  283: 	&print_username_entry_form($r);
  284:         return;
  285:     }
  286: 
  287:     my $defdom=$env{'request.role.domain'};
  288: 
  289:     my ($krbdef,$krbdefdom) =
  290:        &Apache::loncommon::get_kerberos_defaults($defdom);
  291: 
  292:     my %param = ( formname => 'document.cu',
  293:                   kerb_def_dom => $krbdefdom,
  294:                   kerb_def_auth => $krbdef
  295:                   );
  296:     $loginscript  = &Apache::loncommon::authform_header(%param);
  297:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
  298: 
  299:     $ccuname=~s/\W//g;
  300:     $ccdomain=~s/\W//g;
  301:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  302:     my $dc_setcourse_code = '';
  303:     my $nondc_setsection_code = '';
  304:     my %loaditem;
  305: 
  306:     my $groupslist;
  307:     my %curr_groups = &Apache::loncommon::coursegroups();
  308:     if (%curr_groups) {
  309:         $groupslist = join('","',sort(keys(%curr_groups)));
  310:         $groupslist = '"'.$groupslist.'"';   
  311:     }
  312: 
  313:     if ($env{'request.role'} =~ m-^dc\./(\w+)/$-) {
  314:         my $dcdom = $1;
  315:         $loaditem{'onload'} = "document.cu.coursedesc.value=''";
  316:         $dc_setcourse_code = <<"ENDSCRIPT";
  317:     function setCourse() {
  318:         var course = document.cu.dccourse.value;
  319:         if (course != "") {
  320:             if (document.cu.dcdomain.value != document.cu.origdom.value) {
  321:                 alert("You must select a course in the current domain");
  322:                 return;
  323:             } 
  324:             var userrole = document.cu.role.options[document.cu.role.selectedIndex].value
  325:             var section="";
  326:             var numsections = 0;
  327:             for (var i=0; i<document.cu.currsec.length; i++) {
  328:                 if (document.cu.currsec.options[i].selected == true ) {
  329:                     if (document.cu.currsec.options[i].value != "" && document.cu.currsec.options[i].value != null) { 
  330:                         if (numsections == 0) {
  331:                             section = document.cu.currsec.options[i].value
  332:                             numsections = 1;
  333:                         }
  334:                         else {
  335:                             section = section + "," +  document.cu.currsec.options[i].value
  336:                             numsections ++;
  337:                         }
  338:                     }
  339:                 }
  340:             }
  341:             if (document.cu.newsec.value != "" && document.cu.newsec.value != null) {
  342:                 if (numsections == 0) {
  343:                     section = document.cu.newsec.value
  344:                 }
  345:                 else {
  346:                     section = section + "," +  document.cu.newsec.value
  347:                 }
  348:                 var numsplit = document.cu.newsec.value.split(/,/g);
  349:                 numsections = numsections + numsplit.length;
  350:             }
  351:             if ((userrole == 'st') && (numsections > 1)) {
  352:                 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.")
  353:                 return;
  354:             }
  355:             for (var j=0; j<numsplit.length; j++) {
  356:                 if ((numsplit[j] == 'all') || (numsplit[j] == 'none')) {
  357:                     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.");
  358:                     return;
  359:                 }
  360:                 if (document.cu.groups.value != '') {
  361:                     var groups = document.cu.groups.value.split(/,/g);
  362:                     for (var k=0; k<groups.length; k++) {
  363:                         if (numsplit[j] == groups[k]) {
  364:                             alert("'"+numsplit[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.");
  365:                             return; 
  366:                         }
  367:                     }
  368:                 }
  369:             }
  370:             if ((userrole == 'cc') && (numsections > 0)) {
  371:                 alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
  372:                 section = "";
  373:             }
  374:             var numcourse = getIndex(document.cu.dccourse);
  375:             if (numcourse == "-1") {
  376:                 alert("There was a problem with your course selection");
  377:                 return
  378:             }
  379:             else { 
  380:                 var coursename = "_$dcdom"+"_"+course+"_"+userrole
  381:                 document.cu.elements[numcourse].name = "act"+coursename
  382:                 document.cu.elements[numcourse+4].name = "sec"+coursename
  383:                 document.cu.elements[numcourse+4].value = section
  384:                 document.cu.elements[numcourse+5].name = "start"+coursename
  385:                 document.cu.elements[numcourse+6].name = "end"+coursename
  386:             }
  387:         }
  388:         document.cu.submit();
  389:     }
  390: 
  391:     function getIndex(caller) {
  392:         for (var i=0;i<document.cu.elements.length;i++) {
  393:             if (document.cu.elements[i] == caller) {
  394:                 return i;
  395:             }
  396:         }
  397:         return -1;
  398:     }
  399: ENDSCRIPT
  400:     } else {
  401:         $nondc_setsection_code = <<"ENDSECCODE";
  402:     function setSections() {
  403:         var re1 = /^currsec_/;
  404:         var groups = new Array($groupslist);
  405:         for (var i=0;i<document.cu.elements.length;i++) {
  406:             var str = document.cu.elements[i].name;
  407:             var checkcurr = str.match(re1);
  408:             if (checkcurr != null) {
  409:                 var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/;
  410:                 if (document.cu.elements[i-1].checked == true) {
  411:                     var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/;
  412:                     match = re2.exec(str);
  413:                     var role = match[1];
  414:                     if (role == 'cc') {
  415:                         alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
  416:                     }
  417:                     else {
  418:                         var sections = '';
  419:                         var numsec = 0;
  420:                         var sections;
  421:                         for (var j=0; j<document.cu.elements[i].length; j++) {
  422:                             if (document.cu.elements[i].options[j].selected == true ) {
  423:                                 if (document.cu.elements[i].options[j].value != "") {
  424:                                     if (numsec == 0) {
  425:                                         if (document.cu.elements[i].options[j].value != "") {
  426:                                             sections = document.cu.elements[i].options[j].value;
  427:                                             numsec ++;
  428:                                         }
  429:                                     }
  430:                                     else {
  431:                                         sections = sections + "," +  document.cu.elements[i].options[j].value
  432:                                         numsec ++;
  433:                                     }
  434:                                 }
  435:                             }
  436:                         }
  437:                         if (numsec > 0) {
  438:                             if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) {
  439:                                 sections = sections + "," +  document.cu.elements[i+1].value;
  440:                             }
  441:                         }
  442:                         else {
  443:                             sections = document.cu.elements[i+1].value;
  444:                         }
  445:                         var newsecs = document.cu.elements[i+1].value;
  446:                         if (newsecs != null && newsecs != "") {
  447:                             var numsplit = newsecs.split(/,/g);
  448:                             numsec = numsec + numsplit.length;
  449:                         }
  450:                         if ((role == 'st') && (numsec > 1)) {
  451:                             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.")
  452:                             return;
  453:                         }
  454:                         else {
  455:                             for (var j=0; j<numsplit.length; j++) {
  456:                                 if ((numsplit[j] == 'all') ||
  457:                                     (numsplit[j] == 'none')) {
  458:                                     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.");
  459:                                     return;
  460:                                 }
  461:                                 for (var k=0; k<groups.length; k++) {
  462:                                     if (numsplit[j] == groups[k]) {
  463:                                         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.");
  464:                                         return;
  465:                                     }
  466:                                 }
  467:                             }
  468:                             document.cu.elements[i+2].value = sections;
  469:                         }
  470:                     }
  471:                 }
  472:             }
  473:         }
  474:         document.cu.submit();
  475:     }
  476: ENDSECCODE
  477:     }
  478:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
  479:                                    $nondc_setsection_code,$groupslist);
  480:     my $start_page = 
  481: 	&Apache::loncommon::start_page('Create Users, Change User Privileges',
  482: 				       $js,{'add_entries' => \%loaditem,});
  483: 
  484:     my $forminfo =<<"ENDFORMINFO";
  485: <form action="/adm/createuser" method="post" name="cu">
  486: <input type="hidden" name="phase"       value="update_user_data">
  487: <input type="hidden" name="ccuname"     value="$ccuname">
  488: <input type="hidden" name="ccdomain"    value="$ccdomain">
  489: <input type="hidden" name="pres_value"  value="" >
  490: <input type="hidden" name="pres_type"   value="" >
  491: <input type="hidden" name="pres_marker" value="" >
  492: ENDFORMINFO
  493:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
  494:     my %incdomains; 
  495:     my %inccourses;
  496:     foreach (values(%Apache::lonnet::hostdom)) {
  497:        $incdomains{$_}=1;
  498:     }
  499:     foreach (keys(%env)) {
  500: 	if ($_=~/^user\.priv\.cm\.\/(\w+)\/(\w+)/) {
  501: 	    $inccourses{$1.'_'.$2}=1;
  502:         }
  503:     }
  504:     if ($uhome eq 'no_host') {
  505:         my $home_server_list=
  506:             '<option value="default" selected>default</option>'."\n".
  507:                 &Apache::loncommon::home_server_option_list($ccdomain);
  508:         
  509: 	my %lt=&Apache::lonlocal::texthash(
  510:                     'cnu'  => "Create New User",
  511:                     'nu'   => "New User",
  512:                     'id'   => "in domain",
  513:                     'pd'   => "Personal Data",
  514:                     'fn'   => "First Name",
  515:                     'mn'   => "Middle Name",
  516:                     'ln'   => "Last Name",
  517:                     'gen'  => "Generation",
  518:                     'idsn' => "ID/Student Number",
  519:                     'hs'   => "Home Server",
  520:                     'lg'   => "Login Data"
  521: 				       );
  522: 	my $genhelp=&Apache::loncommon::help_open_topic('Generation');
  523:         &initialize_authen_forms();
  524: 	$r->print(<<ENDNEWUSER);
  525: $start_page
  526: <h1>$lt{'cnu'}</h1>
  527: $forminfo
  528: <h2>$lt{'nu'} "$ccuname" $lt{'id'} $ccdomain</h2>
  529: <script type="text/javascript" language="Javascript">
  530: $loginscript
  531: </script>
  532: <input type='hidden' name='makeuser' value='1' />
  533: <h3>$lt{'pd'}</h3>
  534: <p>
  535: <table>
  536: <tr><td>$lt{'fn'}  </td>
  537:     <td><input type='text' name='cfirst'  size='15' /></td></tr>
  538: <tr><td>$lt{'mn'} </td> 
  539:     <td><input type='text' name='cmiddle' size='15' /></td></tr>
  540: <tr><td>$lt{'ln'}   </td>
  541:     <td><input type='text' name='clast'   size='15' /></td></tr>
  542: <tr><td>$lt{'gen'}$genhelp</td>
  543:     <td><input type='text' name='cgen'    size='5'  /></td></tr>
  544: </table>
  545: $lt{'idsn'} <input type='text' name='cstid'   size='15' /></p>
  546: $lt{'hs'}: <select name="hserver" size="1"> $home_server_list </select>
  547: <hr />
  548: <h3>$lt{'lg'}</h3>
  549: <p>$generalrule </p>
  550: <p>$authformkrb </p>
  551: <p>$authformint </p>
  552: <p>$authformfsys</p>
  553: <p>$authformloc </p>
  554: ENDNEWUSER
  555:     } else { # user already exists
  556: 	my %lt=&Apache::lonlocal::texthash(
  557:                     'cup'  => "Change User Privileges",
  558:                     'usr'  => "User",                    
  559:                     'id'   => "in domain",
  560:                     'fn'   => "first name",
  561:                     'mn'   => "middle name",
  562:                     'ln'   => "last name",
  563:                     'gen'  => "generation"
  564: 				       );
  565: 	$r->print(<<ENDCHANGEUSER);
  566: $start_page
  567: <h1>$lt{'cup'}</h1>
  568: $forminfo
  569: <h2>$lt{'usr'} "$ccuname" $lt{'id'} "$ccdomain"</h2>
  570: ENDCHANGEUSER
  571:         # Get the users information
  572:         my %userenv = &Apache::lonnet::get('environment',
  573:                           ['firstname','middlename','lastname','generation'],
  574:                           $ccdomain,$ccuname);
  575:         my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
  576:         $r->print(<<END);
  577: <hr />
  578: <table border="2">
  579: <tr>
  580: <th>$lt{'fn'}</th><th>$lt{'mn'}</th><th>$lt{'ln'}</th><th>$lt{'gen'}</th>
  581: </tr>
  582: <tr>
  583: END
  584:         foreach ('firstname','middlename','lastname','generation') {
  585:            if (&Apache::lonnet::allowed('mau',$ccdomain)) {
  586:               $r->print(<<"END");            
  587: <td><input type="text" name="c$_" value="$userenv{$_}" size="15" /></td>
  588: END
  589:            } else {
  590:                $r->print('<td>'.$userenv{$_}.'</td>');
  591:            }
  592:         }
  593:       $r->print(<<END);
  594: </tr>
  595: </table>
  596: END
  597:         # Build up table of user roles to allow revocation of a role.
  598:         my ($tmp) = keys(%rolesdump);
  599:         unless ($tmp =~ /^(con_lost|error)/i) {
  600:            my $now=time;
  601: 	   my %lt=&Apache::lonlocal::texthash(
  602: 		    'rer'  => "Revoke Existing Roles",
  603:                     'rev'  => "Revoke",                    
  604:                     'del'  => "Delete",
  605: 		    'ren'  => "Re-Enable",
  606:                     'rol'  => "Role",
  607:                     'ext'  => "Extent",
  608:                     'sta'  => "Start",
  609:                     'end'  => "End"
  610: 				       );
  611:            my (%roletext,%sortrole,%roleclass,%rolepriv);
  612: 	   foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
  613: 				    my $b1=join('_',(split('_',$b))[1,0]);
  614: 				    return $a1 cmp $b1;
  615: 				} keys(%rolesdump)) {
  616:                next if ($area =~ /^rolesdef/);
  617: 	       my $envkey=$area;
  618:                my $role = $rolesdump{$area};
  619:                my $thisrole=$area;
  620:                $area =~ s/\_\w\w$//;
  621:                my ($role_code,$role_end_time,$role_start_time) = 
  622:                    split(/_/,$role);
  623: # Is this a custom role? Get role owner and title.
  624: 	       my ($croleudom,$croleuname,$croletitle)=
  625: 	           ($role_code=~/^cr\/(\w+)\/(\w+)\/(\w+)$/);
  626:                my $bgcol='ffffff';
  627:                my $allowed=0;
  628:                my $delallowed=0;
  629: 	       my $sortkey=$role_code;
  630: 	       my $class='Unknown';
  631:                if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
  632: 		   $class='Course';
  633:                    my ($coursedom,$coursedir) = ($1,$2);
  634: 		   $sortkey.="\0$1";
  635:                    # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
  636:                    my %coursedata=
  637:                        &Apache::lonnet::coursedescription($1.'_'.$2);
  638: 		   my $carea;
  639: 		   if (defined($coursedata{'description'})) {
  640: 		       $carea=$coursedata{'description'}.
  641:                            '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
  642:      &Apache::loncommon::syllabuswrapper('Syllabus',$coursedir,$coursedom);
  643: 		       $sortkey.="\0".$coursedata{'description'};
  644: 		   } else {
  645: 		       $carea=&mt('Unavailable course').': '.$area;
  646: 		       $sortkey.="\0".&mt('Unavailable course').': '.$area;
  647: 		   }
  648:                    $inccourses{$1.'_'.$2}=1;
  649:                    if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
  650:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  651:                        $allowed=1;
  652:                    }
  653:                    if ((&Apache::lonnet::allowed('dro',$1)) ||
  654:                        (&Apache::lonnet::allowed('dro',$ccdomain))) {
  655:                        $delallowed=1;
  656:                    }
  657: # - custom role. Needs more info, too
  658: 		   if ($croletitle) {
  659: 		       if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
  660: 			   $allowed=1;
  661: 			   $thisrole.='.'.$role_code;
  662: 		       }
  663: 		   }
  664:                    # Compute the background color based on $area
  665:                    $bgcol=$1.'_'.$2;
  666:                    $bgcol=~s/[^7-9a-e]//g;
  667:                    $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
  668:                    if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
  669:                        $carea.='<br />Section: '.$3;
  670: 		       $sortkey.="\0$3";
  671:                    }
  672:                    $area=$carea;
  673:                } else {
  674: 		   $sortkey.="\0".$area;
  675:                    # Determine if current user is able to revoke privileges
  676:                    if ($area=~ /^\/(\w+)\//) {
  677:                        if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
  678:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  679:                            $allowed=1;
  680:                        }
  681:                        if (((&Apache::lonnet::allowed('dro',$1))  ||
  682:                             (&Apache::lonnet::allowed('dro',$ccdomain))) &&
  683:                            ($role_code ne 'dc')) {
  684:                            $delallowed=1;
  685:                        }
  686:                    } else {
  687:                        if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
  688:                            $allowed=1;
  689:                        }
  690:                    }
  691: 		   if ($role_code eq 'ca' || $role_code eq 'au') {
  692: 		       $class='Construction Space';
  693: 		   } elsif ($role_code eq 'su') {
  694: 		       $class='System';
  695: 		   } else {
  696: 		       $class='Domain';
  697: 		   }
  698:                }
  699:                if (($role_code eq 'ca') || ($role_code eq 'aa')) {
  700:                    $area=~/\/(\w+)\/(\w+)/;
  701: 		   if (&authorpriv($2,$1)) {
  702: 		       $allowed=1;
  703:                    } else {
  704:                        $allowed=0;
  705:                    }
  706:                }
  707: 	       $bgcol='77FF77';
  708:                my $row = '';
  709:                $row.='<tr bgcolor="#'.$bgcol.'"><td>';
  710:                my $active=1;
  711:                $active=0 if (($role_end_time) && ($now>$role_end_time));
  712:                if (($active) && ($allowed)) {
  713:                    $row.= '<input type="checkbox" name="rev:'.$thisrole.'">';
  714:                } else {
  715:                    if ($active) {
  716:                       $row.='&nbsp;';
  717: 		   } else {
  718:                       $row.=&mt('expired or revoked');
  719: 		   }
  720:                }
  721: 	       $row.='</td><td>';
  722:                if ($allowed && !$active) {
  723:                    $row.= '<input type="checkbox" name="ren:'.$thisrole.'">';
  724:                } else {
  725:                    $row.='&nbsp;';
  726:                }
  727: 	       $row.='</td><td>';
  728:                if ($delallowed) {
  729:                    $row.= '<input type="checkbox" name="del:'.$thisrole.'">';
  730:                } else {
  731:                    $row.='&nbsp;';
  732:                }
  733: 	       my $plaintext='';
  734: 	       unless ($croletitle) {
  735: 		   $plaintext=&Apache::lonnet::plaintext($role_code);
  736: 	       } else {
  737: 	           $plaintext=
  738: 		"Customrole '$croletitle' defined by $croleuname\@$croleudom";
  739: 	       }
  740:                $row.= '</td><td>'.$plaintext.
  741:                       '</td><td>'.$area.
  742:                       '</td><td>'.($role_start_time?localtime($role_start_time)
  743:                                                    : '&nbsp;' ).
  744:                       '</td><td>'.($role_end_time  ?localtime($role_end_time)
  745:                                                    : '&nbsp;' )
  746:                       ."</td></tr>\n";
  747: 	       $sortrole{$sortkey}=$envkey;
  748: 	       $roletext{$envkey}=$row;
  749: 	       $roleclass{$envkey}=$class;
  750:                $rolepriv{$envkey}=$allowed;
  751:                #$r->print($row);
  752:            } # end of foreach        (table building loop)
  753:            my $rolesdisplay = 0;
  754:            my %output = ();
  755: 	   foreach my $type ('Construction Space','Course','Domain','System','Unknown') {
  756: 	       $output{$type} = '';
  757: 	       foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  758: 		   if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) { 
  759: 		       $output{$type}.=$roletext{$sortrole{$which}};
  760: 		   }
  761: 	       }
  762: 	       unless($output{$type} eq '') {
  763: 		   $output{$type} = "<tr bgcolor='#BBffBB'>".
  764: 			     "<td align='center' colspan='7'>".&mt($type)."</td>".
  765:                               $output{$type};
  766:                    $rolesdisplay = 1;
  767: 	       }
  768: 	   }
  769:            if ($rolesdisplay == 1) {
  770:                $r->print(<<END);
  771: <hr />
  772: <h3>$lt{'rer'}</h3>
  773: <table>
  774: <tr><th>$lt{'rev'}</th><th>$lt{'ren'}</th><th>$lt{'del'}</th><th>$lt{'rol'}</th><th>$lt{'e
  775: xt'}</th><th>$lt{'sta'}</th><th>$lt{'end'}</th>
  776: END
  777:                foreach my $type ('Construction Space','Course','Domain','System','Unknown') {
  778:                    if ($output{$type}) {
  779:                        $r->print($output{$type}."\n");
  780:                    }
  781:                }
  782: 	       $r->print('</table>');
  783:            }
  784:         }  # End of unless
  785: 	my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
  786: 	if ($currentauth=~/^krb(4|5):/) {
  787: 	    $currentauth=~/^krb(4|5):(.*)/;
  788: 	    my $krbdefdom=$2;
  789:             my %param = ( formname => 'document.cu',
  790:                           kerb_def_dom => $krbdefdom 
  791:                           );
  792:             $loginscript  = &Apache::loncommon::authform_header(%param);
  793: 	}
  794: 	# Check for a bad authentication type
  795:         unless ($currentauth=~/^krb(4|5):/ or
  796: 		$currentauth=~/^unix:/ or
  797: 		$currentauth=~/^internal:/ or
  798: 		$currentauth=~/^localauth:/
  799: 		) { # bad authentication scheme
  800: 	    if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
  801:                 &initialize_authen_forms();
  802: 		my %lt=&Apache::lonlocal::texthash(
  803:                                'err'   => "ERROR",
  804: 			       'uuas'  => "This user has an unrecognized authentication scheme",
  805:                                'sldb'  => "Please specify login data below",
  806:                                'ld'    => "Login Data"
  807: 						   );
  808: 		$r->print(<<ENDBADAUTH);
  809: <hr />
  810: <script type="text/javascript" language="Javascript">
  811: $loginscript
  812: </script>
  813: <font color='#ff0000'>$lt{'err'}:</font>
  814: $lt{'uuas'} ($currentauth). $lt{'sldb'}.
  815: <h3>$lt{'ld'}</h3>
  816: <p>$generalrule</p>
  817: <p>$authformkrb</p>
  818: <p>$authformint</p>
  819: <p>$authformfsys</p>
  820: <p>$authformloc</p>
  821: ENDBADAUTH
  822:             } else { 
  823:                 # This user is not allowed to modify the users 
  824:                 # authentication scheme, so just notify them of the problem
  825: 		my %lt=&Apache::lonlocal::texthash(
  826:                                'err'   => "ERROR",
  827: 			       'uuas'  => "This user has an unrecognized authentication scheme",
  828:                                'adcs'  => "Please alert a domain coordinator of this situation"
  829: 						   );
  830: 		$r->print(<<ENDBADAUTH);
  831: <hr />
  832: <script type="text/javascript" language="Javascript">
  833: $loginscript
  834: </script>
  835: <font color="#ff0000"> $lt{'err'}: </font>
  836: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
  837: <hr />
  838: ENDBADAUTH
  839:             }
  840:         } else { # Authentication type is valid
  841: 	    my $authformcurrent='';
  842: 	    my $authform_other='';
  843:             &initialize_authen_forms();
  844: 	    if ($currentauth=~/^krb(4|5):/) {
  845: 		$authformcurrent=$authformkrb;
  846: 		$authform_other="<p>$authformint</p>\n".
  847:                     "<p>$authformfsys</p><p>$authformloc</p>";
  848: 	    }
  849: 	    elsif ($currentauth=~/^internal:/) {
  850: 		$authformcurrent=$authformint;
  851: 		$authform_other="<p>$authformkrb</p>".
  852:                     "<p>$authformfsys</p><p>$authformloc</p>";
  853: 	    }
  854: 	    elsif ($currentauth=~/^unix:/) {
  855: 		$authformcurrent=$authformfsys;
  856: 		$authform_other="<p>$authformkrb</p>".
  857:                     "<p>$authformint</p><p>$authformloc;</p>";
  858: 	    }
  859: 	    elsif ($currentauth=~/^localauth:/) {
  860: 		$authformcurrent=$authformloc;
  861: 		$authform_other="<p>$authformkrb</p>".
  862:                     "<p>$authformint</p><p>$authformfsys</p>";
  863: 	    }
  864:             $authformcurrent.=' <i>(will override current values)</i><br />';
  865:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
  866: 		# Current user has login modification privileges
  867: 		my %lt=&Apache::lonlocal::texthash(
  868:                                'ccld'  => "Change Current Login Data",
  869: 			       'enld'  => "Enter New Login Data"
  870: 						   );
  871: 		$r->print(<<ENDOTHERAUTHS);
  872: <hr />
  873: <script type="text/javascript" language="Javascript">
  874: $loginscript
  875: </script>
  876: <h3>$lt{'ccld'}</h3>
  877: <p>$generalrule</p>
  878: <p>$authformnop</p>
  879: <p>$authformcurrent</p>
  880: <h3>$lt{'enld'}</h3>
  881: $authform_other
  882: ENDOTHERAUTHS
  883:             }
  884:         }  ## End of "check for bad authentication type" logic
  885:     } ## End of new user/old user logic
  886:     $r->print('<hr /><h3>'.&mt('Add Roles').'</h3>');
  887: #
  888: # Co-Author
  889: # 
  890:     if (&authorpriv($env{'user.name'},$env{'request.role.domain'}) &&
  891:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
  892:         # No sense in assigning co-author role to yourself
  893: 	my $cuname=$env{'user.name'};
  894:         my $cudom=$env{'request.role.domain'};
  895: 	   my %lt=&Apache::lonlocal::texthash(
  896: 		    'cs'   => "Construction Space",
  897:                     'act'  => "Activate",                    
  898:                     'rol'  => "Role",
  899:                     'ext'  => "Extent",
  900:                     'sta'  => "Start",
  901:                     'end'  => "End",
  902:                     'cau'  => "Co-Author",
  903:                     'caa'  => "Assistant Co-Author",
  904:                     'ssd'  => "Set Start Date",
  905:                     'sed'  => "Set End Date"
  906: 				       );
  907:        $r->print(<<ENDCOAUTH);
  908: <h4>$lt{'cs'}</h4>
  909: <table border=2><tr><th>$lt{'act'}</th><th>$lt{'rol'}</th><th>$lt{'ext'}</th>
  910: <th>$lt{'sta'}</th><th>$lt{'end'}</th></tr>
  911: <tr>
  912: <td><input type=checkbox name="act_$cudom\_$cuname\_ca" /></td>
  913: <td>$lt{'cau'}</td>
  914: <td>$cudom\_$cuname</td>
  915: <td><input type=hidden name="start_$cudom\_$cuname\_ca" value='' />
  916: <a href=
  917: "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>
  918: <td><input type=hidden name="end_$cudom\_$cuname\_ca" value='' />
  919: <a href=
  920: "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>
  921: </tr>
  922: <tr>
  923: <td><input type=checkbox name="act_$cudom\_$cuname\_aa" /></td>
  924: <td>$lt{'caa'}</td>
  925: <td>$cudom\_$cuname</td>
  926: <td><input type=hidden name="start_$cudom\_$cuname\_aa" value='' />
  927: <a href=
  928: "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>
  929: <td><input type=hidden name="end_$cudom\_$cuname\_aa" value='' />
  930: <a href=
  931: "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>
  932: </tr>
  933: </table>
  934: ENDCOAUTH
  935:     }
  936: #
  937: # Domain level
  938: #
  939:     my $num_domain_level = 0;
  940:     my $domaintext = 
  941:     '<h4>'.&mt('Domain Level').'</h4>'.
  942:     '<table border=2><tr><th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.&mt('Extent').'</th>'.
  943:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th></tr>';
  944:     foreach ( sort( keys(%incdomains))) {
  945: 	my $thisdomain=$_;
  946:         foreach ('dc','li','dg','au','sc') {
  947:             if (&Apache::lonnet::allowed('c'.$_,$thisdomain)) {
  948:                my $plrole=&Apache::lonnet::plaintext($_);
  949: 	       my %lt=&Apache::lonlocal::texthash(
  950:                     'ssd'  => "Set Start Date",
  951:                     'sed'  => "Set End Date"
  952: 				       );
  953:                $num_domain_level ++;
  954:                $domaintext .= <<"ENDDROW";
  955: <tr>
  956: <td><input type=checkbox name="act_$thisdomain\_$_"></td>
  957: <td>$plrole</td>
  958: <td>$thisdomain</td>
  959: <td><input type=hidden name="start_$thisdomain\_$_" value=''>
  960: <a href=
  961: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$thisdomain\_$_.value,'start_$thisdomain\_$_','cu.pres','dateset')">$lt{'ssd'}</a></td>
  962: <td><input type=hidden name="end_$thisdomain\_$_" value=''>
  963: <a href=
  964: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$thisdomain\_$_.value,'end_$thisdomain\_$_','cu.pres','dateset')">$lt{'sed'}</a></td>
  965: </tr>
  966: ENDDROW
  967:             }
  968:         } 
  969:     }
  970:     $domaintext.='</table>';
  971:     if ($num_domain_level > 0) {
  972:         $r->print($domaintext);
  973:     }
  974: #
  975: # Course level
  976: #
  977:     my $num_sections;
  978: 
  979:     if ($env{'request.role'} =~ m-^dc\./(\w+)/$-) {
  980:         $r->print(&course_level_dc($1));
  981:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setCourse()">'."\n");
  982:     } else {
  983:         $r->print(&course_level_table(%inccourses));
  984:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setSections()">'."\n");
  985:     }
  986:     $r->print("</form>".&Apache::loncommon::end_page());
  987: }
  988: 
  989: # ================================================================= Phase Three
  990: sub update_user_data {
  991:     my $r=shift;
  992:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
  993:                                           $env{'form.ccdomain'});
  994:     # Error messages
  995:     my $error     = '<font color="#ff0000">'.&mt('Error').':</font>';
  996:     my $end       = &Apache::loncommon::end_page();
  997: 
  998:     my $title;
  999:     if (exists($env{'form.makeuser'})) {
 1000: 	$title='Set Privileges for New User';
 1001:     } else {
 1002:         $title='Modify User Privileges';
 1003:     }
 1004:     $r->print(&Apache::loncommon::start_page($title));
 1005:     my %disallowed;
 1006:     # Check Inputs
 1007:     if (! $env{'form.ccuname'} ) {
 1008: 	$r->print($error.&mt('No login name specified').'.'.$end);
 1009: 	return;
 1010:     }
 1011:     if (  $env{'form.ccuname'}  =~/\W/) {
 1012: 	$r->print($error.&mt('Invalid login name').'.  '.
 1013: 		  &mt('Only letters, numbers, and underscores are valid').'.'.
 1014: 		  $end);
 1015: 	return;
 1016:     }
 1017:     if (! $env{'form.ccdomain'}       ) {
 1018: 	$r->print($error.&mt('No domain specified').'.'.$end);
 1019: 	return;
 1020:     }
 1021:     if (  $env{'form.ccdomain'} =~/\W/) {
 1022: 	$r->print($error.&mt ('Invalid domain name').'.  '.
 1023: 		  &mt('Only letters, numbers, and underscores are valid').'.'.
 1024: 		  $end);
 1025: 	return;
 1026:     }
 1027:     if (! exists($env{'form.makeuser'})) {
 1028:         # Modifying an existing user, so check the validity of the name
 1029:         if ($uhome eq 'no_host') {
 1030:             $r->print($error.&mt('Unable to determine home server for ').
 1031:                       $env{'form.ccuname'}.&mt(' in domain ').
 1032:                       $env{'form.ccdomain'}.'.');
 1033:             return;
 1034:         }
 1035:     }
 1036:     # Determine authentication method and password for the user being modified
 1037:     my $amode='';
 1038:     my $genpwd='';
 1039:     if ($env{'form.login'} eq 'krb') {
 1040: 	$amode='krb';
 1041: 	$amode.=$env{'form.krbver'};
 1042: 	$genpwd=$env{'form.krbarg'};
 1043:     } elsif ($env{'form.login'} eq 'int') {
 1044: 	$amode='internal';
 1045: 	$genpwd=$env{'form.intarg'};
 1046:     } elsif ($env{'form.login'} eq 'fsys') {
 1047: 	$amode='unix';
 1048: 	$genpwd=$env{'form.fsysarg'};
 1049:     } elsif ($env{'form.login'} eq 'loc') {
 1050: 	$amode='localauth';
 1051: 	$genpwd=$env{'form.locarg'};
 1052: 	$genpwd=" " if (!$genpwd);
 1053:     } elsif (($env{'form.login'} eq 'nochange') ||
 1054:              ($env{'form.login'} eq ''        )) { 
 1055:         # There is no need to tell the user we did not change what they
 1056:         # did not ask us to change.
 1057:         # If they are creating a new user but have not specified login
 1058:         # information this will be caught below.
 1059:     } else {
 1060: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
 1061: 	    return;
 1062:     }
 1063:     if ($env{'form.makeuser'}) {
 1064:         # Create a new user
 1065: 	my %lt=&Apache::lonlocal::texthash(
 1066:                     'cru'  => "Creating user",                    
 1067:                     'id'   => "in domain"
 1068: 					   );
 1069: 	$r->print(<<ENDNEWUSERHEAD);
 1070: <h3>$lt{'cru'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}"</h3>
 1071: ENDNEWUSERHEAD
 1072:         # Check for the authentication mode and password
 1073:         if (! $amode || ! $genpwd) {
 1074: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
 1075: 	    return;
 1076: 	}
 1077:         # Determine desired host
 1078:         my $desiredhost = $env{'form.hserver'};
 1079:         if (lc($desiredhost) eq 'default') {
 1080:             $desiredhost = undef;
 1081:         } else {
 1082:             my %home_servers = &Apache::loncommon::get_library_servers
 1083:                 ($env{'form.ccdomain'});  
 1084:             if (! exists($home_servers{$desiredhost})) {
 1085:                 $r->print($error.&mt('Invalid home server specified'));
 1086:                 return;
 1087:             }
 1088:         }
 1089: 	# Call modifyuser
 1090: 	my $result = &Apache::lonnet::modifyuser
 1091: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cstid'},
 1092:              $amode,$genpwd,$env{'form.cfirst'},
 1093:              $env{'form.cmiddle'},$env{'form.clast'},$env{'form.cgen'},
 1094:              undef,$desiredhost
 1095: 	     );
 1096: 	$r->print(&mt('Generating user').': '.$result);
 1097:         my $home = &Apache::lonnet::homeserver($env{'form.ccuname'},
 1098:                                                $env{'form.ccdomain'});
 1099:         $r->print('<br />'.&mt('Home server').': '.$home.' '.
 1100:                   $Apache::lonnet::libserv{$home});
 1101:     } elsif (($env{'form.login'} ne 'nochange') &&
 1102:              ($env{'form.login'} ne ''        )) {
 1103: 	# Modify user privileges
 1104:     my %lt=&Apache::lonlocal::texthash(
 1105:                     'usr'  => "User",                    
 1106:                     'id'   => "in domain"
 1107: 				       );
 1108: 	$r->print(<<ENDMODIFYUSERHEAD);
 1109: <h2>$lt{'usr'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}"</h2>
 1110: ENDMODIFYUSERHEAD
 1111:         if (! $amode || ! $genpwd) {
 1112: 	    $r->print($error.'Invalid login mode or password'.$end);    
 1113: 	    return;
 1114: 	}
 1115: 	# Only allow authentification modification if the person has authority
 1116: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
 1117: 	    $r->print('Modifying authentication: '.
 1118:                       &Apache::lonnet::modifyuserauth(
 1119: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
 1120:                        $amode,$genpwd));
 1121:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
 1122: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
 1123: 	} else {
 1124: 	    # Okay, this is a non-fatal error.
 1125: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.');    
 1126: 	}
 1127:     }
 1128:     ##
 1129:     if (! $env{'form.makeuser'} ) {
 1130:         # Check for need to change
 1131:         my %userenv = &Apache::lonnet::get
 1132:             ('environment',['firstname','middlename','lastname','generation'],
 1133:              $env{'form.ccdomain'},$env{'form.ccuname'});
 1134:         my ($tmp) = keys(%userenv);
 1135:         if ($tmp =~ /^(con_lost|error)/i) { 
 1136:             %userenv = ();
 1137:         }
 1138:         # Check to see if we need to change user information
 1139:         foreach ('firstname','middlename','lastname','generation') {
 1140:             # Strip leading and trailing whitespace
 1141:             $env{'form.c'.$_} =~ s/(\s+$|^\s+)//g; 
 1142:         }
 1143:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}) && 
 1144:             ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
 1145:              $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
 1146:              $env{'form.clastname'}   ne $userenv{'lastname'}   ||
 1147:              $env{'form.cgeneration'} ne $userenv{'generation'} )) {
 1148:             # Make the change
 1149:             my %changeHash;
 1150:             $changeHash{'firstname'}  = $env{'form.cfirstname'};
 1151:             $changeHash{'middlename'} = $env{'form.cmiddlename'};
 1152:             $changeHash{'lastname'}   = $env{'form.clastname'};
 1153:             $changeHash{'generation'} = $env{'form.cgeneration'};
 1154:             my $putresult = &Apache::lonnet::put
 1155:                 ('environment',\%changeHash,
 1156:                  $env{'form.ccdomain'},$env{'form.ccuname'});
 1157:             if ($putresult eq 'ok') {
 1158:             # Tell the user we changed the name
 1159: 		my %lt=&Apache::lonlocal::texthash(
 1160:                              'uic'  => "User Information Changed",             
 1161:                              'frst' => "first",
 1162:                              'mddl' => "middle",
 1163:                              'lst'  => "last",
 1164: 			     'gen'  => "generation",
 1165:                              'prvs' => "Previous",
 1166:                              'chto' => "Changed To"
 1167: 						   );
 1168:                 $r->print(<<"END");
 1169: <table border="2">
 1170: <caption>$lt{'uic'}</caption>
 1171: <tr><th>&nbsp;</th>
 1172:     <th>$lt{'frst'}</th>
 1173:     <th>$lt{'mddl'}</th>
 1174:     <th>$lt{'lst'}</th>
 1175:     <th>$lt{'gen'}</th></tr>
 1176: <tr><td>$lt{'prvs'}</td>
 1177:     <td>$userenv{'firstname'}  </td>
 1178:     <td>$userenv{'middlename'} </td>
 1179:     <td>$userenv{'lastname'}   </td>
 1180:     <td>$userenv{'generation'} </td></tr>
 1181: <tr><td>$lt{'chto'}</td>
 1182:     <td>$env{'form.cfirstname'}  </td>
 1183:     <td>$env{'form.cmiddlename'} </td>
 1184:     <td>$env{'form.clastname'}   </td>
 1185:     <td>$env{'form.cgeneration'} </td></tr>
 1186: </table>
 1187: END
 1188:             } else { # error occurred
 1189:                 $r->print("<h2>".&mt('Unable to successfully change environment for')." ".
 1190:                       $env{'form.ccuname'}." ".&mt('in domain')." ".
 1191:                       $env{'form.ccdomain'}."</h2>");
 1192:             }
 1193:         }  else { # End of if ($env ... ) logic
 1194:             # They did not want to change the users name but we can
 1195:             # still tell them what the name is
 1196: 	    my %lt=&Apache::lonlocal::texthash(
 1197:                            'usr'  => "User",                    
 1198:                            'id'   => "in domain",
 1199:                            'gen'  => "Generation"
 1200: 					       );
 1201:                 $r->print(<<"END");
 1202: <h2>$lt{'usr'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}"</h2>
 1203: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
 1204: <h4>$lt{'gen'}: $userenv{'generation'}</h4>
 1205: END
 1206:         }
 1207:     }
 1208:     ##
 1209:     my $now=time;
 1210:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
 1211:     foreach (keys (%env)) {
 1212: 	next if (! $env{$_});
 1213: 	# Revoke roles
 1214: 	if ($_=~/^form\.rev/) {
 1215: 	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
 1216: # Revoke standard role
 1217: 	        $r->print(&mt('Revoking').' '.$2.' in '.$1.': <b>'.
 1218:                      &Apache::lonnet::revokerole($env{'form.ccdomain'},
 1219:                      $env{'form.ccuname'},$1,$2).'</b><br />');
 1220: 		if ($2 eq 'st') {
 1221: 		    $1=~/^\/(\w+)\/(\w+)/;
 1222: 		    my $cid=$1.'_'.$2;
 1223: 		    $r->print(&mt('Drop from classlist').': <b>'.
 1224: 			 &Apache::lonnet::critical('put:'.
 1225:                              $env{'course.'.$cid.'.domain'}.':'.
 1226: 	                     $env{'course.'.$cid.'.num'}.':classlist:'.
 1227:                          &Apache::lonnet::escape($env{'form.ccuname'}.':'.
 1228:                              $env{'form.ccdomain'}).'='.
 1229:                          &Apache::lonnet::escape($now.':'),
 1230: 	                     $env{'course.'.$cid.'.home'}).'</b><br />');
 1231: 		}
 1232: 	    } 
 1233: 	    if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
 1234: # Revoke custom role
 1235: 		$r->print(&mt('Revoking custom role:').
 1236:                       ' '.$4.' by '.$3.'@'.$2.' in '.$1.': <b>'.
 1237:                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
 1238: 				  $env{'form.ccuname'},$1,$2,$3,$4).
 1239: 		'</b><br />');
 1240: 	    }
 1241: 	} elsif ($_=~/^form\.del/) {
 1242: 	    if ($_=~/^form\.del\:([^\_]+)\_([^\_]+)$/) {
 1243: 	        $r->print(&mt('Deleting').' '.$2.' in '.$1.': '.
 1244:                      &Apache::lonnet::assignrole($env{'form.ccdomain'},
 1245:                      $env{'form.ccuname'},$1,$2,$now,0,1).'<br />');
 1246: 		if ($2 eq 'st') {
 1247: 		    $1=~/^\/(\w+)\/(\w+)/;
 1248: 		    my $cid=$1.'_'.$2;
 1249: 		    $r->print(&mt('Drop from classlist').': <b>'.
 1250: 			 &Apache::lonnet::critical('put:'.
 1251:                              $env{'course.'.$cid.'.domain'}.':'.
 1252: 	                     $env{'course.'.$cid.'.num'}.':classlist:'.
 1253:                          &Apache::lonnet::escape($env{'form.ccuname'}.':'.
 1254:                              $env{'form.ccdomain'}).'='.
 1255:                          &Apache::lonnet::escape($now.':'),
 1256: 	                     $env{'course.'.$cid.'.home'}).'</b><br />');
 1257: 		}
 1258: 	    }
 1259: 	} elsif ($_=~/^form\.ren/) {
 1260:             my $udom = $env{'form.ccdomain'};
 1261:             my $uname = $env{'form.ccuname'};
 1262: 	    if ($_=~/^form\.ren\:([^\_]+)\_([^\_]+)$/) {
 1263:                 my $url = $1;
 1264:                 my $role = $2;
 1265:                 my $logmsg;
 1266:                 my $output;
 1267:                 if ($role eq 'st') {
 1268:                     if ($url =~ m-^/(\w+)/(\w+)/?(\w*)$-) {
 1269:                         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
 1270:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course')) {
 1271:                             $output = "Error: $result\n";
 1272:                         } else {
 1273:                             $output = &mt('Assigning').' '.$role.' in '.$url.
 1274:                                       &mt('starting').' '.localtime($now).
 1275:                                       ': <br />'.$logmsg.'<br />'.
 1276:                                       &mt('Add to classlist').': <b>ok</b><br />';
 1277:                         }
 1278:                     }
 1279:                 } else {
 1280: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
 1281:                                $env{'form.ccuname'},$url,$role,0,$now);
 1282: 		    $output = &mt('Re-Enabling [_1] in [_2]: [_3]',
 1283: 			      $role,$url,$result).'<br />';
 1284: 		}
 1285:                 $r->print($output);
 1286: 	    }
 1287: 	} elsif ($_=~/^form\.act/) {
 1288:             my $udom = $env{'form.ccdomain'};
 1289:             my $uname = $env{'form.ccuname'};
 1290: 	    if ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_cr_cr_([^\_]+)_(\w+)_([^\_]+)$/) {
 1291:                 # Activate a custom role
 1292: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
 1293: 		my $url='/'.$one.'/'.$two;
 1294: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
 1295: 
 1296:                 my $start = ( $env{'form.start_'.$full} ?
 1297:                               $env{'form.start_'.$full} :
 1298:                               $now );
 1299:                 my $end   = ( $env{'form.end_'.$full} ?
 1300:                               $env{'form.end_'.$full} :
 1301:                               0 );
 1302:                                                                                      
 1303:                 # split multiple sections
 1304:                 my %sections = ();
 1305:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
 1306:                 if ($num_sections == 0) {
 1307:                     $r->print(&commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end));
 1308:                 } else {
 1309: 		    my %curr_groups =
 1310: 			&Apache::loncommon::coursegroups($one,$two);
 1311:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1312:                         if (($sec eq 'none') || ($sec eq 'all') || 
 1313:                             exists($curr_groups{$sec})) {
 1314:                             $disallowed{$sec} = $url;
 1315:                             next;
 1316:                         }
 1317:                         my $securl = $url.'/'.$sec;
 1318: 		        $r->print(&commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end));
 1319:                     }
 1320:                 }
 1321: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_(\w+)\_([^\_]+)$/) {
 1322: 		# Activate roles for sections with 3 id numbers
 1323: 		# set start, end times, and the url for the class
 1324: 		my ($one,$two,$three)=($1,$2,$3);
 1325: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
 1326: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
 1327: 			      $now );
 1328: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
 1329: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
 1330: 			      0 );
 1331: 		my $url='/'.$one.'/'.$two;
 1332:                 my $type = 'three';
 1333:                 # split multiple sections
 1334:                 my %sections = ();
 1335:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
 1336:                 if ($num_sections == 0) {
 1337:                     $r->print(&commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
 1338:                 } else {
 1339:                     my %curr_groups = 
 1340: 			&Apache::loncommon::coursegroups($one,$two);
 1341:                     my $emptysec = 0;
 1342:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1343:                         $sec =~ s/\W//g;
 1344:                         if ($sec ne '') {
 1345:                             if (($sec eq 'none') || ($sec eq 'all') || 
 1346:                                 exists($curr_groups{$sec})) {
 1347:                                 $disallowed{$sec} = $url;
 1348:                                 next;
 1349:                             }
 1350:                             my $securl = $url.'/'.$sec;
 1351:                             $r->print(&commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec));
 1352:                         } else {
 1353:                             $emptysec = 1;
 1354:                         }
 1355:                     }
 1356:                     if ($emptysec) {
 1357:                         $r->print(&commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,''));
 1358:                     }
 1359:                 } 
 1360: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
 1361: 		# Activate roles for sections with two id numbers
 1362: 		# set start, end times, and the url for the class
 1363: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
 1364: 			      $env{'form.start_'.$1.'_'.$2} : 
 1365: 			      $now );
 1366: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
 1367: 			      $env{'form.end_'.$1.'_'.$2} :
 1368: 			      0 );
 1369: 		my $url='/'.$1.'/';
 1370:                 # split multiple sections
 1371:                 my %sections = ();
 1372:                 my $num_sections = &build_roles($env{'form.sec_'.$1.'_'.$2},\%sections,$2);
 1373:                 if ($num_sections == 0) {
 1374:                     $r->print(&commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
 1375:                 } else {
 1376:                     my $emptysec = 0;
 1377:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
 1378:                         if ($sec ne '') {
 1379:                             my $securl = $url.'/'.$sec;
 1380:                             $r->print(&commit_standardrole($udom,$uname,$securl,$2,$start,$end,$1,undef,$sec));
 1381:                         } else {
 1382:                             $emptysec = 1;
 1383:                         }
 1384:                     }
 1385:                     if ($emptysec) {
 1386:                         $r->print(&commit_standardrole($udom,$uname,$url,$2,$start,$end,$1,undef,''));
 1387:                     }
 1388:                 }
 1389: 	    } else {
 1390: 		$r->print('<p>'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$_.'</tt></p><br />');
 1391:             }
 1392:             foreach my $key (sort(keys(%disallowed))) {
 1393:                 if (($key eq 'none') || ($key eq 'all')) {  
 1394:                     $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key));
 1395:                 } else {
 1396:                     $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));
 1397:                 }
 1398:                 $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />');
 1399:             }
 1400: 	}
 1401:     } # End of foreach (keys(%env))
 1402: # Flush the course logs so reverse user roles immediately updated
 1403:     &Apache::lonnet::flushcourselogs();
 1404:     $r->print('<p><a href="/adm/createuser">Create/Modify Another User</a></p>');
 1405:     $r->print(&Apache::loncommon::end_page());
 1406: }
 1407: 
 1408: sub commit_customrole {
 1409:     my ($udom,$uname,$url,$three,$four,$five,$start,$end) = @_;
 1410:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.'@'.$three.' in '.$url.
 1411:                          ($start?', '.&mt('starting').' '.localtime($start):'').
 1412:                          ($end?', ending '.localtime($end):'').': <b>'.
 1413:               &Apache::lonnet::assigncustomrole(
 1414:                  $udom,$uname,$url,$three,$four,$five,$end,$start).
 1415:                  '</b><br />';
 1416:     return $output;
 1417: }
 1418: 
 1419: sub commit_standardrole {
 1420:     my ($udom,$uname,$url,$three,$start,$end,$one,$two,$sec) = @_;
 1421:     my $output;
 1422:     my $logmsg;
 1423:     if ($three eq 'st') {
 1424:         my $result = &commit_studentrole(\$logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec);
 1425:         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course')) {
 1426:             $output = "Error: $result\n"; 
 1427:         } else {
 1428:             $output = &mt('Assigning').' '.$three.' in '.$url.
 1429:                ($start?', '.&mt('starting').' '.localtime($start):'').
 1430:                ($end?', '.&mt('ending').' '.localtime($end):'').
 1431:                ': <b>'.$result.'</b><br />'.
 1432:                &mt('Add to classlist').': <b>ok</b><br />';
 1433:         }
 1434:     } else {
 1435:         $output = &mt('Assigning').' '.$three.' in '.$url.
 1436:                ($start?', '.&mt('starting').' '.localtime($start):'').
 1437:                ($end?', '.&mt('ending').' '.localtime($end):'').': <b>'.
 1438:                &Apache::lonnet::assignrole(
 1439:                    $udom,$uname,$url,$three,$end,$start).
 1440:                    '</b><br />';
 1441:     }
 1442:     return $output;
 1443: }
 1444: 
 1445: sub commit_studentrole {
 1446:     my ($logmsg,$udom,$uname,$url,$three,$start,$end,$one,$two,$sec) = @_;
 1447:     my $linefeed =  '<br />'."\n";
 1448:     my $result;
 1449:     if (defined($one) && defined($two)) {
 1450:         my $cid=$one.'_'.$two;
 1451:         my $oldsec=&Apache::lonnet::getsection($udom,$uname,$cid);
 1452:         my $secchange = 0;
 1453:         my $expire_role_result;
 1454:         my $modify_section_result;
 1455:         unless ($oldsec eq '-1') {
 1456:             unless ($sec eq $oldsec) {
 1457:                 $secchange = 1;
 1458:                 my $uurl='/'.$cid;
 1459:                 $uurl=~s/\_/\//g;
 1460:                 if ($oldsec) {
 1461:                     $uurl.='/'.$oldsec;
 1462:                 }
 1463:                 $expire_role_result = &Apache::lonnet::assignrole($udom,$uname,$uurl,'st',time);
 1464:                 $result = $expire_role_result;
 1465:             }
 1466:         }
 1467:         if (($expire_role_result eq 'ok') || ($secchange == 0)) {
 1468:             $modify_section_result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,'','',$cid);
 1469:             if ($modify_section_result =~ /^ok/) {
 1470:                 if ($secchange == 1) {
 1471:                     $$logmsg .= "Section for $uname switched from old section: $oldsec to new section: $sec".$linefeed;
 1472:                 } elsif ($oldsec eq '-1') {
 1473:                     $$logmsg .= "New student role for $uname in section $sec in course $cid".$linefeed;
 1474:                 } else {
 1475:                     $$logmsg .= "Student $uname assigned to unchanged section $sec in course $cid".$linefeed;
 1476:                 }
 1477:             } else {
 1478:                 $$logmsg .= "Error when attempting section change for $uname from old section $oldsec to new section: $sec in course $cid -error: $modify_section_result".$linefeed;
 1479:             }
 1480:             $result = $modify_section_result;
 1481:         } elsif ($secchange == 1) {
 1482:             $$logmsg .= "Error when attempting to expire role for $uname in old section $oldsec in course $cid -error: $expire_role_result".$linefeed;
 1483:         }
 1484:     } else {
 1485:         $$logmsg .= "Incomplete course id defined.  Addition of user $uname from domain $udom to course $one\_$two, section $sec not completed.$linefeed";
 1486:         $result = "error: incomplete course id\n";
 1487:     }
 1488:     return $result;
 1489: }
 1490: 
 1491: sub build_roles {
 1492:     my ($sectionstr,$sections,$role) = @_;
 1493:     my $num_sections = 0;
 1494:     if ($sectionstr=~ /,/) {
 1495:         my @secnums = split/,/,$sectionstr;
 1496:         if ($role eq 'st') {
 1497:             $secnums[0] =~ s/\W//g;
 1498:             $$sections{$secnums[0]} = 1;
 1499:             $num_sections = 1;
 1500:         } else {
 1501:             foreach my $sec (@secnums) {
 1502:                 $sec =~ ~s/\W//g;
 1503:                 unless ($sec eq "") {
 1504:                     if (exists($$sections{$sec})) {
 1505:                         $$sections{$sec} ++;
 1506:                     } else {
 1507:                         $$sections{$sec} = 1;
 1508:                         $num_sections ++;
 1509:                     }
 1510:                 }
 1511:             }
 1512:         }
 1513:     } else {
 1514:         $sectionstr=~s/\W//g;
 1515:         unless ($sectionstr eq '') {
 1516:             $$sections{$sectionstr} = 1;
 1517:             $num_sections ++;
 1518:         }
 1519:     }
 1520:                                                                                      
 1521:     return $num_sections;
 1522: }
 1523: 
 1524: # ========================================================== Custom Role Editor
 1525: 
 1526: sub custom_role_editor {
 1527:     my $r=shift;
 1528:     my $rolename=$env{'form.rolename'};
 1529: 
 1530:     if ($rolename eq 'make new role') {
 1531: 	$rolename=$env{'form.newrolename'};
 1532:     }
 1533: 
 1534:     $rolename=~s/[^A-Za-z0-9]//gs;
 1535: 
 1536:     unless ($rolename) {
 1537: 	&print_username_entry_form($r);
 1538:         return;
 1539:     }
 1540: 
 1541:     $r->print(&Apache::loncommon::start_page('Custom Role Editor'));
 1542:     my $syspriv='';
 1543:     my $dompriv='';
 1544:     my $coursepriv='';
 1545:     my ($rdummy,$roledef)=
 1546: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 1547: # ------------------------------------------------------- Does this role exist?
 1548:     $r->print('<h2>');
 1549:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 1550: 	$r->print(&mt('Existing Role').' "');
 1551: # ------------------------------------------------- Get current role privileges
 1552: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 1553:     } else {
 1554: 	$r->print(&mt('New Role').' "');
 1555: 	$roledef='';
 1556:     }
 1557:     $r->print($rolename.'"</h2>');
 1558: # ------------------------------------------------------- What can be assigned?
 1559:     my %full=();
 1560:     my %courselevel=();
 1561:     my %courselevelcurrent=();
 1562:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 1563: 	my ($priv,$restrict)=split(/\&/,$_);
 1564:         unless ($restrict) { $restrict='F'; }
 1565:         $courselevel{$priv}=$restrict;
 1566:         if ($coursepriv=~/\:$priv/) {
 1567: 	    $courselevelcurrent{$priv}=1;
 1568: 	}
 1569: 	$full{$priv}=1;
 1570:     }
 1571:     my %domainlevel=();
 1572:     my %domainlevelcurrent=();
 1573:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 1574: 	my ($priv,$restrict)=split(/\&/,$_);
 1575:         unless ($restrict) { $restrict='F'; }
 1576:         $domainlevel{$priv}=$restrict;
 1577:         if ($dompriv=~/\:$priv/) {
 1578: 	    $domainlevelcurrent{$priv}=1;
 1579: 	}
 1580: 	$full{$priv}=1;
 1581:     }
 1582:     my %systemlevel=();
 1583:     my %systemlevelcurrent=();
 1584:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 1585: 	my ($priv,$restrict)=split(/\&/,$_);
 1586:         unless ($restrict) { $restrict='F'; }
 1587:         $systemlevel{$priv}=$restrict;
 1588:         if ($syspriv=~/\:$priv/) {
 1589: 	    $systemlevelcurrent{$priv}=1;
 1590: 	}
 1591: 	$full{$priv}=1;
 1592:     }
 1593:     my %lt=&Apache::lonlocal::texthash(
 1594: 		    'prv'  => "Privilege",
 1595: 		    'crl'  => "Course Level",
 1596:                     'dml'  => "Domain Level",
 1597:                     'ssl'  => "System Level"
 1598: 				       );
 1599:     $r->print(<<ENDCCF);
 1600: <form method="post">
 1601: <input type="hidden" name="phase" value="set_custom_roles" />
 1602: <input type="hidden" name="rolename" value="$rolename" />
 1603: <table border="2">
 1604: <tr><th>$lt{'prv'}</th><th>$lt{'crl'}</th><th>$lt{'dml'}</th>
 1605: <th>$lt{'ssl'}</th></tr>
 1606: ENDCCF
 1607:     foreach (sort keys %full) {
 1608: 	$r->print('<tr><td>'.&Apache::lonnet::plaintext($_).'</td><td>'.
 1609:     ($courselevel{$_}?'<input type="checkbox" name="'.$_.':c" '.
 1610:     ($courselevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
 1611:     '</td><td>'.
 1612:     ($domainlevel{$_}?'<input type="checkbox" name="'.$_.':d" '.
 1613:     ($domainlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
 1614:     '</td><td>'.
 1615:     ($systemlevel{$_}?'<input type="checkbox" name="'.$_.':s" '.
 1616:     ($systemlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
 1617:     '</td></tr>');
 1618:     }
 1619:     $r->print(
 1620:    '<table><input type="submit" value="'.&mt('Define Role').'" /></form>'.
 1621: 	      &Apache::loncommon::end_page());
 1622: }
 1623: 
 1624: # ---------------------------------------------------------- Call to definerole
 1625: sub set_custom_role {
 1626:     my ($r) = @_;
 1627: 
 1628:     my $rolename=$env{'form.rolename'};
 1629: 
 1630:     $rolename=~s/[^A-Za-z0-9]//gs;
 1631: 
 1632:     unless ($rolename) {
 1633: 	&print_username_entry_form($r);
 1634:         return;
 1635:     }
 1636: 
 1637:     $r->print(&Apache::loncommon::start_page('Save Custom Role').'<h2>');
 1638:     my ($rdummy,$roledef)=
 1639: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 1640: 
 1641: # ------------------------------------------------------- Does this role exist?
 1642:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 1643: 	$r->print(&mt('Existing Role').' "');
 1644:     } else {
 1645: 	$r->print(&mt('New Role').' "');
 1646: 	$roledef='';
 1647:     }
 1648:     $r->print($rolename.'"</h2>');
 1649: # ------------------------------------------------------- What can be assigned?
 1650:     my $sysrole='';
 1651:     my $domrole='';
 1652:     my $courole='';
 1653: 
 1654:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 1655: 	my ($priv,$restrict)=split(/\&/,$_);
 1656:         unless ($restrict) { $restrict=''; }
 1657:         if ($env{'form.'.$priv.':c'}) {
 1658: 	    $courole.=':'.$_;
 1659: 	}
 1660:     }
 1661: 
 1662:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 1663: 	my ($priv,$restrict)=split(/\&/,$_);
 1664:         unless ($restrict) { $restrict=''; }
 1665:         if ($env{'form.'.$priv.':d'}) {
 1666: 	    $domrole.=':'.$_;
 1667: 	}
 1668:     }
 1669: 
 1670:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 1671: 	my ($priv,$restrict)=split(/\&/,$_);
 1672:         unless ($restrict) { $restrict=''; }
 1673:         if ($env{'form.'.$priv.':s'}) {
 1674: 	    $sysrole.=':'.$_;
 1675: 	}
 1676:     }
 1677:     $r->print('<br />Defining Role: '.
 1678: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
 1679:     if ($env{'request.course.id'}) {
 1680:         my $url='/'.$env{'request.course.id'};
 1681:         $url=~s/\_/\//g;
 1682: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
 1683: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
 1684: 						$env{'user.name'},
 1685: 						$url,
 1686: 						$env{'user.domain'},
 1687: 						$env{'user.name'},
 1688: 						$rolename));
 1689:     }
 1690:     $r->print('<p><a href="/adm/createuser">Create another role, or Create/Modify a user.</a></p>');
 1691:     $r->print(&Apache::loncommon::end_page());
 1692: }
 1693: 
 1694: # ================================================================ Main Handler
 1695: sub handler {
 1696:     my $r = shift;
 1697: 
 1698:     if ($r->header_only) {
 1699:        &Apache::loncommon::content_type($r,'text/html');
 1700:        $r->send_http_header;
 1701:        return OK;
 1702:     }
 1703: 
 1704:     if ((&Apache::lonnet::allowed('cta',$env{'request.course.id'})) ||
 1705:         (&Apache::lonnet::allowed('cin',$env{'request.course.id'})) || 
 1706:         (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) || 
 1707:         (&Apache::lonnet::allowed('cep',$env{'request.course.id'})) ||
 1708: 	(&authorpriv($env{'user.name'},$env{'request.role.domain'})) ||
 1709:         (&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))) {
 1710:        &Apache::loncommon::content_type($r,'text/html');
 1711:        $r->send_http_header;
 1712:        unless ($env{'form.phase'}) {
 1713: 	   &print_username_entry_form($r);
 1714:        }
 1715:        if ($env{'form.phase'} eq 'get_user_info') {
 1716:            &print_user_modification_page($r);
 1717:        } elsif ($env{'form.phase'} eq 'update_user_data') {
 1718:            &update_user_data($r);
 1719:        } elsif ($env{'form.phase'} eq 'selected_custom_edit') {
 1720:            &custom_role_editor($r);
 1721:        } elsif ($env{'form.phase'} eq 'set_custom_roles') {
 1722: 	   &set_custom_role($r);
 1723:        }
 1724:    } else {
 1725:       $env{'user.error.msg'}=
 1726:         "/adm/createuser:mau:0:0:Cannot modify user data";
 1727:       return HTTP_NOT_ACCEPTABLE; 
 1728:    }
 1729:    return OK;
 1730: } 
 1731: 
 1732: #-------------------------------------------------- functions for &phase_two
 1733: sub course_level_table {
 1734:     my (%inccourses) = @_;
 1735:     my $table = '';
 1736: # Custom Roles?
 1737: 
 1738:     my %customroles=&my_custom_roles();
 1739:     my %lt=&Apache::lonlocal::texthash(
 1740:             'exs'  => "Existing sections",
 1741:             'new'  => "Define new section",
 1742:             'ssd'  => "Set Start Date",
 1743:             'sed'  => "Set End Date",
 1744:             'crl'  => "Course Level",
 1745:             'act'  => "Activate",
 1746:             'rol'  => "Role",
 1747:             'ext'  => "Extent",
 1748:             'grs'  => "Section",
 1749:             'sta'  => "Start",
 1750:             'end'  => "End"
 1751:     );
 1752: 
 1753:     foreach (sort( keys(%inccourses))) {
 1754: 	my $thiscourse=$_;
 1755: 	my $protectedcourse=$_;
 1756: 	$thiscourse=~s:_:/:g;
 1757: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
 1758: 	my $area=$coursedata{'description'};
 1759: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$_; }
 1760: 	my $bgcol=$thiscourse;
 1761: 	$bgcol=~s/[^7-9a-e]//g;
 1762: 	$bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
 1763: 	my ($domain,$cnum)=split(/\//,$thiscourse);
 1764:         my %sections_count = ();
 1765:         my $num_sections = 0;
 1766:         if (defined($env{'request.course.id'})) {
 1767:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
 1768:                 $num_sections = &Apache::loncommon::get_sections($domain,$cnum,\%sections_count);
 1769:             }
 1770:         }
 1771: 	foreach  ('st','ta','ep','ad','in','cc') {
 1772: 	    if (&Apache::lonnet::allowed('c'.$_,$thiscourse)) {
 1773: 		my $plrole=&Apache::lonnet::plaintext($_);
 1774: 		$table .= <<ENDEXTENT;
 1775: <tr bgcolor="#$bgcol">
 1776: <td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
 1777: <td>$plrole</td>
 1778: <td>$area<br />Domain: $domain</td>
 1779: ENDEXTENT
 1780: 	        if ($_ ne 'cc') {
 1781:                     if ($num_sections > 0) {
 1782:                         my $currsec = &course_sections($num_sections,\%sections_count,$protectedcourse.'_'.$_);
 1783:                         $table .= 
 1784:                     '<td><table border="0" cellspacing="0" cellpadding="0">'.
 1785:                      '<tr><td valign="top">'.$lt{'exs'}.'<br />'.
 1786:                         $currsec.'</td>'.
 1787:                      '<td>&nbsp;&nbsp;</td>'.
 1788:                      '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
 1789:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$_.'" value="" /></td>'.
 1790:                      '<input type="hidden" '.
 1791:                      'name="sec_'.$protectedcourse.'_'.$_.'"></td>'.
 1792:                      '</tr></table></td>';
 1793:                     } else {
 1794:                         $table .= '<td><input type="text" size="10" '.
 1795:                      'name="sec_'.$protectedcourse.'_'.$_.'"></td>';
 1796:                     }
 1797:                 } else { 
 1798: 		    $table .= '<td>&nbsp</td>';
 1799:                 }
 1800: 		$table .= <<ENDTIMEENTRY;
 1801: <td><input type=hidden name="start_$protectedcourse\_$_" value=''>
 1802: <a href=
 1803: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">$lt{'ssd'}</a></td>
 1804: <td><input type=hidden name="end_$protectedcourse\_$_" value=''>
 1805: <a href=
 1806: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">$lt{'sed'}</a></td>
 1807: ENDTIMEENTRY
 1808:                 $table.= "</tr>\n";
 1809:             }
 1810:         }
 1811:         foreach (sort keys %customroles) {
 1812: 	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
 1813: 		my $plrole=$_;
 1814:                 my $customrole=$protectedcourse.'_cr_cr_'.$env{'user.domain'}.
 1815: 		    '_'.$env{'user.name'}.'_'.$plrole;
 1816: 		$table .= <<END;
 1817: <tr bgcolor="#$bgcol">
 1818: <td><input type="checkbox" name="act_$customrole"></td>
 1819: <td>$plrole</td>
 1820: <td>$area</td>
 1821: END
 1822:                 if ($num_sections > 0) {
 1823:                     my $currsec = &course_sections($num_sections,\%sections_count,$customrole);
 1824:                     $table.=
 1825:                    '<td><table border="0" cellspacing="0" cellpadding="0">'.
 1826:                    '<tr><td valign="top">'.$lt{'exs'}.'<br />'.
 1827:                      $currsec.'</td>'.
 1828:                    '<td>&nbsp;&nbsp;</td>'.
 1829:                    '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
 1830:                    '<input type="text" name="newsec_'.$customrole.'" value="" /></td>'.
 1831:                    '<input type="hidden" '.
 1832:                    'name="sec_'.$customrole.'"></td>'.
 1833:                    '</tr></table></td>';
 1834:                 } else {
 1835:                     $table .= '<td><input type="text" size="10" '.
 1836:                      'name="sec_'.$customrole.'"></td>';
 1837:                 }
 1838:                 $table .= <<ENDENTRY;
 1839: <td><input type=hidden name="start_$customrole" value=''>
 1840: <a href=
 1841: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">$lt{'ssd'}</a></td>
 1842: <td><input type=hidden name="end_$customrole" value=''>
 1843: <a href=
 1844: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">$lt{'sed'}</a></td></tr>
 1845: ENDENTRY
 1846:            }
 1847: 	}
 1848:     }
 1849:     return '' if ($table eq ''); # return nothing if there is nothing 
 1850:                                  # in the table
 1851:     my $result = <<ENDTABLE;
 1852: <h4>$lt{'crl'}</h4>
 1853: <table border=2><tr><th>$lt{'act'}</th><th>$lt{'rol'}</th><th>$lt{'ext'}</th>
 1854: <th>$lt{'grs'}</th><th>$lt{'sta'}</th><th>$lt{'end'}</th></tr>
 1855: $table
 1856: </table>
 1857: ENDTABLE
 1858:     return $result;
 1859: }
 1860: 
 1861: sub course_sections {
 1862:     my ($num_sections,$sections_count,$role) = @_;
 1863:     my $output = '';
 1864:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
 1865:     if ($num_sections == 1) {
 1866:         $output = '<select name="currsec_'.$role.'" >'."\n".
 1867:                   '  <option value="">Select</option>'."\n".
 1868:                   '  <option value="">No section</option>'."\n".
 1869:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
 1870:     } else {
 1871:         $output = '<select name="currsec_'.$role.'" ';
 1872:         my $multiple = 4;
 1873:         if ($num_sections <4) { $multiple = $num_sections; }
 1874:         $output .= '"multiple" size="'.$multiple.'">'."\n";
 1875:         foreach (@sections) {
 1876:             $output .= '<option value="'.$_.'">'.$_."</option>\n";
 1877:         }
 1878:     }
 1879:     $output .= '</select>'; 
 1880:     return $output;
 1881: }
 1882: 
 1883: sub course_level_dc {
 1884:     my ($dcdom) = @_;
 1885:     my %customroles=&my_custom_roles();
 1886:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
 1887:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
 1888:                       '<input type="hidden" name="dccourse" value="" />';
 1889:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
 1890:                      ('cu','dccourse','dcdomain','coursedesc').'</b>';
 1891:                                                                                       
 1892:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,$dcdom);
 1893:     my %lt=&Apache::lonlocal::texthash(
 1894:                     'crl'  => "Course Level",
 1895:                     'crt'  => "Course Title",
 1896:                     'rol'  => "Role",
 1897:                     'grs'  => "Section",
 1898:                     'exs'  => "Existing sections",
 1899:                     'new'  => "Define new section", 
 1900:                     'sta'  => "Start",
 1901:                     'end'  => "End",
 1902:                     'ssd'  => "Set Start Date",
 1903:                     'sed'  => "Set End Date"
 1904:                   );
 1905:     my $header = '<h4>'.$lt{'crl'}.'</h4>'.
 1906:                  '<table border="2"><tr><th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th></tr>';
 1907:     my $otheritems = '<tr><td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'".'cu'."'".','."'".'dccourse'."'".','."'".'dcdomain'."'".','."'".'coursedesc'."',''".')" /></td>'.
 1908:                      '<td><select name="role">'."\n";
 1909:     foreach  ('st','ta','ep','ad','in','cc') {
 1910:         my $plrole=&Apache::lonnet::plaintext($_);
 1911:         $otheritems .= '  <option value="'.$_.'">'.$plrole;
 1912:     }
 1913:     if ( keys %customroles > 0) {
 1914:         foreach (sort keys %customroles) {
 1915:             my $custrole='cr_cr_'.$env{'user.domain'}.
 1916:                     '_'.$env{'user.name'}.'_'.$_;
 1917:             $otheritems .= '  <option value="'.$custrole.'">'.$_;
 1918:         }
 1919:     }
 1920:     $otheritems .= '</select></td><td>'.
 1921:                      '<table border="0" cellspacing="0" cellpadding="0">'.
 1922:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
 1923:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
 1924:                      '<td>&nbsp;&nbsp;</td>'.
 1925:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
 1926:                      '<input type="text" name="newsec" value="" />'.
 1927:                      '<input type="hidden" name="groups" value="" /></td>'.
 1928:                      '</tr></table></td>';
 1929:     $otheritems .= <<ENDTIMEENTRY;
 1930: <td><input type=hidden name="start" value=''>
 1931: <a href=
 1932: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
 1933: <td><input type=hidden name="end" value=''>
 1934: <a href=
 1935: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
 1936: ENDTIMEENTRY
 1937:     $otheritems .= "</tr></table>\n";
 1938:     return $cb_jscript.$header.$hiddenitems.$otheritems;
 1939: }
 1940: 
 1941: #---------------------------------------------- end functions for &phase_two
 1942: 
 1943: #--------------------------------- functions for &phase_two and &phase_three
 1944: 
 1945: #--------------------------end of functions for &phase_two and &phase_three
 1946: 
 1947: 1;
 1948: __END__
 1949: 
 1950: 

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