Annotation of loncom/interface/loncreateuser.pm, revision 1.268.2.4

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.268.2.4! raeburn     4: # $Id: loncreateuser.pm,v 1.268.2.3 2008/12/18 17:28:24 raeburn Exp $
1.22      albertel    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: #
1.20      harris41   28: ###
                     29: 
1.1       www        30: package Apache::loncreateuser;
1.66      bowersj2   31: 
                     32: =pod
                     33: 
                     34: =head1 NAME
                     35: 
1.263     jms        36: Apache::loncreateuser.pm
1.66      bowersj2   37: 
                     38: =head1 SYNOPSIS
                     39: 
1.263     jms        40:     Handler to create users and custom roles
                     41: 
                     42:     Provides an Apache handler for creating users,
1.66      bowersj2   43:     editing their login parameters, roles, and removing roles, and
                     44:     also creating and assigning custom roles.
                     45: 
                     46: =head1 OVERVIEW
                     47: 
                     48: =head2 Custom Roles
                     49: 
                     50: In LON-CAPA, roles are actually collections of privileges. "Teaching
                     51: Assistant", "Course Coordinator", and other such roles are really just
                     52: collection of privileges that are useful in many circumstances.
                     53: 
                     54: Creating custom roles can be done by the Domain Coordinator through
                     55: the Create User functionality. That screen will show all privileges
                     56: that can be assigned to users. For a complete list of privileges,
                     57: please see C</home/httpd/lonTabs/rolesplain.tab>.
                     58: 
                     59: Custom role definitions are stored in the C<roles.db> file of the role
                     60: author.
                     61: 
                     62: =cut
1.1       www        63: 
                     64: use strict;
                     65: use Apache::Constants qw(:common :http);
                     66: use Apache::lonnet;
1.54      bowersj2   67: use Apache::loncommon;
1.68      www        68: use Apache::lonlocal;
1.117     raeburn    69: use Apache::longroup;
1.190     raeburn    70: use Apache::lonuserutils;
1.139     albertel   71: use LONCAPA qw(:DEFAULT :match);
1.1       www        72: 
1.20      harris41   73: my $loginscript; # piece of javascript used in two separate instances
                     74: my $authformnop;
                     75: my $authformkrb;
                     76: my $authformint;
                     77: my $authformfsys;
                     78: my $authformloc;
                     79: 
1.94      matthew    80: sub initialize_authen_forms {
1.227     raeburn    81:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     82:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     83:     my %param = ( formname => $formname,
1.187     raeburn    84:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    85:                   kerb_def_auth => $krbdef,
1.187     raeburn    86:                   domain => $dom,
                     87:                 );
1.188     raeburn    88:     my %abv_auth = &auth_abbrev();
1.227     raeburn    89:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    90:         my $long_auth = $1;
1.227     raeburn    91:         my $curr_autharg = $2;
1.188     raeburn    92:         my %abv_auth = &auth_abbrev();
                     93:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     94:         if ($long_auth =~ /^krb(4|5)$/) {
                     95:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    96:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn    97:         }
1.205     raeburn    98:         if ($mode eq 'modifyuser') {
                     99:             $param{'mode'} = $mode;
                    100:         }
1.187     raeburn   101:     }
1.227     raeburn   102:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    103:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   104:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    105:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    106:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    107:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  108: }
                    109: 
1.188     raeburn   110: sub auth_abbrev {
                    111:     my %abv_auth = (
                    112:                      krb4     => 'krb',
                    113:                      internal => 'int',
                    114:                      localuth => 'loc',
                    115:                      unix     => 'fsys',
                    116:                    );
                    117:     return %abv_auth;
                    118: }
1.43      www       119: 
1.134     raeburn   120: # ====================================================
                    121: 
                    122: sub portfolio_quota {
                    123:     my ($ccuname,$ccdomain) = @_;
                    124:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   125:                    'usrt'      => "User Tools",
                    126:                    'blog'      => "Personal User Blog",
                    127:                    'aboutme'   => "Personal Information Page",
                    128:                    'portfolio' => "Personal User Portfolio",
                    129:                    'avai'      => "Available",
                    130:                    'cusa'      => "availability",
                    131:                    'chse'      => "Change setting",
                    132:                    'disk'      => "Disk space allocated to user's portfolio files",
                    133:                    'cuqu'      => "Current quota",
                    134:                    'cust'      => "Custom quota",
                    135:                    'defa'      => "Default",
                    136:                    'usde'      => "Use default",
                    137:                    'uscu'      => "Use custom",
                    138:                    'chqu'      => "Change quota",
1.134     raeburn   139:     );
1.149     raeburn   140:     my ($currquota,$quotatype,$inststatus,$defquota) = 
                    141:         &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
                    142:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
                    143:     my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
                    144:     if ($inststatus ne '') {
                    145:         if ($usertypes->{$inststatus} ne '') {
                    146:             $longinsttype = $usertypes->{$inststatus};
                    147:         }
                    148:     }
                    149:     $custom_on = ' ';
                    150:     $custom_off = ' checked="checked" ';
                    151:     my $quota_javascript = <<"END_SCRIPT";
                    152: <script type="text/javascript">
                    153: function quota_changes(caller) {
                    154:     if (caller == "custom") {
                    155:         if (document.cu.customquota[0].checked) {
                    156:             document.cu.portfolioquota.value = "";
                    157:         }
                    158:     }
                    159:     if (caller == "quota") {
                    160:         document.cu.customquota[1].checked = true;
                    161:     }
                    162: }
                    163: </script>
                    164: END_SCRIPT
                    165:     if ($quotatype eq 'custom') {
                    166:         $custom_on = $custom_off;
                    167:         $custom_off = ' ';
                    168:         $showquota = $currquota;
                    169:         if ($longinsttype eq '') {
1.230     bisitz    170:             $defaultinfo = &mt('For this user, the default quota would be [_1]'
                    171:                             .' Mb.',$defquota);
1.149     raeburn   172:         } else {
1.231     raeburn   173:             $defaultinfo = &mt("For this user, the default quota would be [_1]".
                    174:                                " Mb, as determined by the user's institutional".
                    175:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   176:         }
                    177:     } else {
                    178:         if ($longinsttype eq '') {
1.230     bisitz    179:             $defaultinfo = &mt('For this user, the default quota is [_1]'
                    180:                             .' Mb.',$defquota);
1.149     raeburn   181:         } else {
1.231     raeburn   182:             $defaultinfo = &mt("For this user, the default quota of [_1]".
                    183:                                " Mb, is determined by the user's institutional".
                    184:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   185:         }
                    186:     }
1.267     raeburn   187: 
                    188:     my $output = $quota_javascript."\n".
                    189:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    190:                  &Apache::loncommon::start_data_table();
                    191: 
                    192:     if (&Apache::lonnet::allowed('mut',$ccdomain)) {
                    193:         my %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    194:                           'tools.aboutme','tools.portfolio','tools.blog');
                    195:         my @usertools = ('aboutme','blog','portfolio');
                    196:         foreach my $item (@usertools) {
                    197:             my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off);
                    198:             $cust_off = 'checked="checked" ';
                    199:             $tool_on = 'checked="checked" ';
                    200:             $curr_access = &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item);
                    201:             if ($userenv{'tools.'.$item} eq '') {
                    202:                 $custom_access = 'default';
                    203:                 if (!$curr_access) {
                    204:                     $tool_off = 'checked="checked" ';
                    205:                     $tool_on = '';
                    206:                 }
                    207:             } else {
                    208:                 $custom_access = 'custom';
                    209:                 $cust_on = ' checked="checked" ';
                    210:                 $cust_off = '';
                    211:                 if ($userenv{'tools.'.$item} == 0) {
                    212:                     $tool_off = 'checked="checked" ';
                    213:                     $tool_on = '';
                    214:                 }
                    215:             }
                    216:             $output .= '  <tr class="LC_info_row">'."\n".
                    217:                        '   <td>'.$lt{$item}.'</td>'."\n".
                    218:                        '  </tr>'."\n".
                    219:                        &Apache::loncommon::start_data_table_row()."\n".
                    220:                        '  <td>'.&mt('Availability determined currently from [_1] setting.',$custom_access).
                    221:                        '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$lt{'avai'}.': '.
                    222:                        ($curr_access?&mt('Yes'):&mt('No')).'</td>'."\n".
                    223:                        &Apache::loncommon::end_data_table_row()."\n".
                    224:                        &Apache::loncommon::start_data_table_row()."\n".
                    225:                        '  <td><span class="LC_nobreak">'.$lt{'chse'}.': <label>'.
                    226:                        '<input type="radio" name="custom'.$item.'" value="0" '.
                    227:                        $cust_off.'/>'.$lt{'usde'}.'</label>&nbsp;&nbsp;&nbsp;'.
                    228:                        '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    229:                        $cust_on.'/>'.$lt{'uscu'}.'</label>&nbsp;&nbsp;--&nbsp;&nbsp;'.
                    230:                        $lt{'cusa'}.':&nbsp;<label>'.
                    231:                        '<input type="radio" name="tools_'.$item.'" value="1" '.
                    232:                        $tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
                    233:                        '<input type="radio" name="tools_'.$item.'" value="0" '.
                    234:                        $tool_off.'/>'.&mt('Off').'</label></span></td>'."\n".
                    235:                        &Apache::loncommon::end_data_table_row()."\n";
                    236:         }
                    237:     }
                    238:     if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    239:         $output .= '<tr class="LC_info_row">'."\n".
                    240:                    '    <td>'.$lt{'disk'}.'</td>'."\n".
                    241:                    '  </tr>'."\n".
                    242:                    &Apache::loncommon::start_data_table_row()."\n".
                    243:                    '  <td>'.$lt{'cuqu'}.': '.
                    244:                    $currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
                    245:                    $defaultinfo.'</td>'."\n".
                    246:                    &Apache::loncommon::end_data_table_row()."\n".
                    247:                    &Apache::loncommon::start_data_table_row()."\n".
                    248:                    '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    249:                    ': <label>'.
                    250:                    '<input type="radio" name="customquota" value="0" '.
                    251:                    $custom_off.' onchange="javascript:quota_changes('."'custom'".')"'.
                    252:                    ' />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
                    253:                    '&nbsp;<label><input type="radio" name="customquota" value="1" '. 
                    254:                    $custom_on.'  onchange="javascript:quota_changes('."'custom'".')" />'.
                    255:                    $lt{'cust'}.':</label>&nbsp;'.
                    256:                    '<input type="text" name="portfolioquota" size ="5" value="'.
                    257:                    $showquota.'" onfocus="javascript:quota_changes('."'quota'".')" '.
                    258:                    '/>&nbsp;Mb</span></td>'."\n".
                    259:                    &Apache::loncommon::end_data_table_row()."\n";
                    260:     }  
                    261:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   262:     return $output;
                    263: }
                    264: 
1.2       www       265: # =================================================================== Phase one
1.1       www       266: 
1.42      matthew   267: sub print_username_entry_form {
1.207     raeburn   268:     my ($r,$context,$response,$srch,$forcenewuser) = @_;
1.101     albertel  269:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   270:     my $formtoset = 'crtuser';
                    271:     if (exists($env{'form.startrolename'})) {
                    272:         $formtoset = 'docustom';
                    273:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   274:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    275:         $formtoset =  $env{'form.origform'};
1.160     raeburn   276:     }
                    277: 
                    278:     my ($jsback,$elements) = &crumb_utilities();
                    279: 
                    280:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  281:         '<script type="text/javascript">'."\n".
1.160     raeburn   282:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset}).
1.162     raeburn   283:         '</script>'."\n";
1.160     raeburn   284: 
                    285:     my %loaditems = (
                    286:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    287:                     );
1.229     raeburn   288:     my %breadcrumb_text = &singleuser_breadcrumb();
1.110     albertel  289:     my $start_page =
1.190     raeburn   290: 	&Apache::loncommon::start_page('User Management',
1.160     raeburn   291: 				       $jscript,{'add_entries' => \%loaditems,});
1.214     raeburn   292:     if ($env{'form.action'} eq 'custom') {
                    293:         &Apache::lonhtmlcommon::add_breadcrumb
                    294:           ({href=>"javascript:backPage(document.crtuser)",
                    295:             text=>"Pick custom role",});
                    296:     } else {
1.190     raeburn   297:         &Apache::lonhtmlcommon::add_breadcrumb
                    298:           ({href=>"javascript:backPage(document.crtuser)",
1.229     raeburn   299:             text=>$breadcrumb_text{'search'},
1.190     raeburn   300:             faq=>282,bug=>'Instructor Interface',});
                    301:     }
1.224     raeburn   302:     my $helpitem = 'Course_Change_Privileges';
                    303:     if ($env{'form.action'} eq 'custom') {
                    304:         $helpitem = 'Course_Editing_Custom_Roles';
                    305:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    306:         $helpitem = 'Course_Add_Student';
                    307:     }
                    308:     my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management',
                    309:                                                      $helpitem);
1.190     raeburn   310:     my %existingroles=&Apache::lonuserutils::my_custom_roles();
1.59      www       311:     my $choice=&Apache::loncommon::select_form('make new role','rolename',
                    312: 		('make new role' => 'Generate new role ...',%existingroles));
1.71      sakharuk  313:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   314:                     'srst' => 'Search for a user and enroll as a student',
                    315:                     'srad' => 'Search for a user and modify/add user information or roles',
1.71      sakharuk  316: 		    'usr'  => "Username",
                    317:                     'dom'  => "Domain",
                    318:                     'ecrp' => "Edit Custom Role Privileges",
1.72      sakharuk  319:                     'nr'   => "Name of Role",
1.160     raeburn   320:                     'cre'  => "Custom Role Editor",
1.71      sakharuk  321: 				       );
1.190     raeburn   322:     $r->print($start_page."\n".$crumbs);
1.214     raeburn   323:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   324:         if (&Apache::lonnet::allowed('mcr','/')) {
                    325:             $r->print(<<ENDCUSTOM);
1.58      www       326: <form action="/adm/createuser" method="post" name="docustom">
1.190     raeburn   327: <input type="hidden" name="action" value="$env{'form.action'}" />
1.157     albertel  328: <input type="hidden" name="phase" value="selected_custom_edit" />
1.224     raeburn   329: <h3>$lt{'ecrp'}</h3>
1.72      sakharuk  330: $lt{'nr'}: $choice <input type="text" size="15" name="newrolename" /><br />
1.71      sakharuk  331: <input name="customeditor" type="submit" value="$lt{'cre'}" />
1.107     www       332: </form>
1.106     www       333: ENDCUSTOM
1.190     raeburn   334:         }
1.213     raeburn   335:     } else {
1.229     raeburn   336:         my $actiontext = $lt{'srad'};
1.213     raeburn   337:         if ($env{'form.action'} eq 'singlestudent') {
1.229     raeburn   338:             $actiontext = $lt{'srst'};
1.213     raeburn   339:         }
                    340:         $r->print("
1.229     raeburn   341: <h3>$actiontext</h3>");
1.213     raeburn   342:         if ($env{'form.origform'} ne 'crtusername') {
                    343:             $r->print("\n".$response);
                    344:         }
                    345:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response));
1.107     www       346:     }
1.110     albertel  347:     $r->print(&Apache::loncommon::end_page());
                    348: }
                    349: 
1.160     raeburn   350: sub entry_form {
1.214     raeburn   351:     my ($dom,$srch,$forcenewuser,$context,$responsemsg) = @_;
1.207     raeburn   352:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
1.229     raeburn   353:     my ($usertype,$inexact);
1.214     raeburn   354:     if (ref($srch) eq 'HASH') {
                    355:         if (($srch->{'srchin'} eq 'dom') &&
                    356:             ($srch->{'srchby'} eq 'uname') &&
                    357:             ($srch->{'srchtype'} eq 'exact') &&
                    358:             ($srch->{'srchdomain'} ne '') &&
                    359:             ($srch->{'srchterm'} ne '')) {
                    360:             my ($rules,$ruleorder) =
                    361:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
                    362:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules);
1.229     raeburn   363:         } else {
                    364:             $inexact = 1;
1.214     raeburn   365:         }
1.207     raeburn   366:     }
1.214     raeburn   367:     my $cancreate =
                    368:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.160     raeburn   369:     my $userpicker = 
1.179     raeburn   370:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.214     raeburn   371:                                        'document.crtuser',$cancreate,$usertype);
1.160     raeburn   372:     my $srchbutton = &mt('Search');
1.229     raeburn   373:     if ($env{'form.action'} eq 'singlestudent') {
                    374:         $srchbutton = &mt('Search and Enroll');
                    375:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    376:         $srchbutton = &mt('Search or Add New User');
                    377:     }
1.207     raeburn   378:     my $output = <<"ENDBLOCK";
1.160     raeburn   379: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   380: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   381: <input type="hidden" name="phase" value="get_user_info" />
                    382: $userpicker
1.179     raeburn   383: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   384: </form>
1.207     raeburn   385: ENDBLOCK
1.229     raeburn   386:     if ($env{'form.phase'} eq '') {
1.207     raeburn   387:         my $defdom=$env{'request.role.domain'};
                    388:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain');
                    389:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   390:                   'enro' => 'Enroll one student',
                    391:                   'admo' => 'Add/modify a single user',
                    392:                   'crea' => 'create new user if required',
                    393:                   'uskn' => "username is known",
1.207     raeburn   394:                   'crnu' => 'Create a new user',
                    395:                   'usr'  => 'Username',
                    396:                   'dom'  => 'in domain',
1.229     raeburn   397:                   'enrl' => 'Enroll',
                    398:                   'cram'  => 'Create/Modify user',
1.207     raeburn   399:         );
1.229     raeburn   400:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    401:         my ($title,$buttontext,$showresponse);
                    402:         if ($env{'form.action'} eq 'singlestudent') {   
                    403:             $title = $lt{'enro'};
                    404:             $buttontext = $lt{'enrl'};
                    405:         } else {
                    406:             $title = $lt{'admo'};
                    407:             $buttontext = $lt{'cram'};
                    408:         }
                    409:         if ($cancreate) {
                    410:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    411:         } else {
                    412:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    413:         }
                    414:         if ($env{'form.origform'} eq 'crtusername') {
                    415:             $showresponse = $responsemsg;
                    416:         }
1.207     raeburn   417:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   418: <br />
1.207     raeburn   419: <form action="/adm/createuser" method="post" name="crtusername">
                    420: <input type="hidden" name="action" value="$env{'form.action'}" />
                    421: <input type="hidden" name="phase" value="createnewuser" />
                    422: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   423: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   424: <input type="hidden" name="srchin" value="dom" />
                    425: <input type="hidden" name="forcenewuser" value="1" />
                    426: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   427: <h3>$title</h3>
                    428: $showresponse
1.207     raeburn   429: <table>
                    430:  <tr>
                    431:   <td>$lt{'usr'}:</td>
                    432:   <td><input type="text" size="15" name="srchterm" /></td>
                    433:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   434:   <td>&nbsp;$sellink&nbsp;</td>
                    435:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   436:  </tr>
                    437: </table>
                    438: </form>
1.160     raeburn   439: ENDDOCUMENT
1.207     raeburn   440:     }
1.160     raeburn   441:     return $output;
                    442: }
1.110     albertel  443: 
                    444: sub user_modification_js {
1.113     raeburn   445:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    446:     
1.110     albertel  447:     return <<END;
                    448: <script type="text/javascript" language="Javascript">
                    449: 
                    450:     function pclose() {
                    451:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
                    452:                  "height=350,width=350,scrollbars=no,menubar=no");
                    453:         parmwin.close();
                    454:     }
                    455: 
                    456:     $pjump_def
                    457:     $dc_setcourse_code
                    458: 
                    459:     function dateset() {
                    460:         eval("document.cu."+document.cu.pres_marker.value+
                    461:             ".value=document.cu.pres_value.value");
                    462:         pclose();
                    463:     }
                    464: 
1.113     raeburn   465:     $nondc_setsection_code
                    466: 
1.110     albertel  467: </script>
                    468: END
1.2       www       469: }
                    470: 
                    471: # =================================================================== Phase two
1.160     raeburn   472: sub print_user_selection_page {
1.229     raeburn   473:     my ($r,$response,$srch,$srch_results,$srcharray,$context) = @_;
1.160     raeburn   474:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    475:     my $sortby = $env{'form.sortby'};
                    476: 
                    477:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    478:         $sortby = 'lastname';
                    479:     }
                    480: 
                    481:     my ($jsback,$elements) = &crumb_utilities();
                    482: 
                    483:     my $jscript = (<<ENDSCRIPT);
                    484: <script type="text/javascript">
                    485: function pickuser(uname,udom) {
                    486:     document.usersrchform.seluname.value=uname;
                    487:     document.usersrchform.seludom.value=udom;
                    488:     document.usersrchform.phase.value="userpicked";
                    489:     document.usersrchform.submit();
                    490: }
                    491: 
                    492: $jsback
                    493: </script>
                    494: ENDSCRIPT
                    495: 
                    496:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   497:                                        'usrch'          => "User Search to add/modify roles",
                    498:                                        'stusrch'        => "User Search to enroll student",
                    499:                                        'usel'           => "Select a user to add/modify roles",
                    500:                                        'stusel'         => "Select a user to enroll as a student", 
1.160     raeburn   501:                                        'username'       => "username",
                    502:                                        'domain'         => "domain",
                    503:                                        'lastname'       => "last name",
                    504:                                        'firstname'      => "first name",
                    505:                                        'permanentemail' => "permanent e-mail",
                    506:                                       );
1.214     raeburn   507:     $r->print(&Apache::loncommon::start_page('User Management',$jscript));
1.229     raeburn   508: 
                    509:     my %breadcrumb_text = &singleuser_breadcrumb();
                    510:     &Apache::lonhtmlcommon::add_breadcrumb
                    511:         ({href=>"javascript:backPage(document.usersrchform,'','')",
                    512:           text=>$breadcrumb_text{'search'},
                    513:           faq=>282,bug=>'Instructor Interface',},
                    514:          {href=>"javascript:backPage(document.usersrchform,'get_user_info','select')",
                    515:           text=>$breadcrumb_text{'userpicked'},
                    516:           faq=>282,bug=>'Instructor Interface',});
                    517:     if ($env{'form.action'} eq 'singleuser') {
1.224     raeburn   518:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management',
                    519:                                                       'Course_Change_Privileges'));
1.179     raeburn   520:         $r->print("<b>$lt{'usrch'}</b><br />");
1.207     raeburn   521:         $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context));
1.179     raeburn   522:         $r->print('<h3>'.$lt{'usel'}.'</h3>');
1.229     raeburn   523:     } elsif ($env{'form.action'} eq 'singlestudent') {
1.224     raeburn   524:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management',
                    525:                                                       'Course_Add_Student'));
1.179     raeburn   526:         $r->print($jscript."<b>$lt{'stusrch'}</b><br />");
1.214     raeburn   527:         $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context));
1.179     raeburn   528:         $r->print('</form><h3>'.$lt{'stusel'}.'</h3>');
                    529:     }
1.160     raeburn   530:     $r->print('<form name="usersrchform" method="post">'.
                    531:               &Apache::loncommon::start_data_table()."\n".
                    532:               &Apache::loncommon::start_data_table_header_row()."\n".
                    533:               ' <th> </th>'."\n");
                    534:     foreach my $field (@fields) {
                    535:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                    536:                   "'".$field."'".';document.usersrchform.submit();">'.
                    537:                   $lt{$field}.'</a></th>'."\n");
                    538:     }
                    539:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    540: 
                    541:     my @sorted_users = sort {
1.167     albertel  542:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn   543:             ||
1.167     albertel  544:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn   545:             ||
                    546:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel  547: 	    ||
                    548: 	lc($a) cmp lc($b)
1.160     raeburn   549:         } (keys(%$srch_results));
                    550: 
                    551:     foreach my $user (@sorted_users) {
                    552:         my ($uname,$udom) = split(/:/,$user);
                    553:         $r->print(&Apache::loncommon::start_data_table_row().
                    554:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".')" /></td>'.
                    555:                   '<td><tt>'.$uname.'</tt></td>'.
                    556:                   '<td><tt>'.$udom.'</tt></td>');
                    557:         foreach my $field ('lastname','firstname','permanentemail') {
                    558:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                    559:         }
                    560:         $r->print(&Apache::loncommon::end_data_table_row());
                    561:     }
                    562:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn   563:     if (ref($srcharray) eq 'ARRAY') {
                    564:         foreach my $item (@{$srcharray}) {
                    565:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                    566:         }
                    567:     }
1.160     raeburn   568:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                    569:               ' <input type="hidden" name="seluname" value="" />'."\n".
                    570:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn   571:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn   572:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn   573:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
                    574:     $r->print($response.'</form>'.&Apache::loncommon::end_page());
1.160     raeburn   575: }
                    576: 
                    577: sub print_user_query_page {
1.179     raeburn   578:     my ($r,$caller) = @_;
1.160     raeburn   579: # FIXME - this is for a network-wide name search (similar to catalog search)
                    580: # To use frames with similar behavior to catalog/portfolio search.
                    581: # To be implemented. 
                    582:     return;
                    583: }
                    584: 
1.42      matthew   585: sub print_user_modification_page {
1.215     raeburn   586:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission) = @_;
1.185     raeburn   587:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn   588:         my $usermsg = &mt('No username and/or domain provided.');
                    589:         $env{'form.phase'} = '';
1.207     raeburn   590: 	&print_username_entry_form($r,$context,$usermsg);
1.58      www       591:         return;
                    592:     }
1.213     raeburn   593:     my ($form,$formname);
                    594:     if ($env{'form.action'} eq 'singlestudent') {
                    595:         $form = 'document.enrollstudent';
                    596:         $formname = 'enrollstudent';
                    597:     } else {
                    598:         $form = 'document.cu';
                    599:         $formname = 'cu';
                    600:     }
1.188     raeburn   601:     my %abv_auth = &auth_abbrev();
1.227     raeburn   602:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn   603:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                    604:     if ($uhome eq 'no_host') {
1.215     raeburn   605:         my $usertype;
                    606:         my ($rules,$ruleorder) =
                    607:             &Apache::lonnet::inst_userrules($ccdomain,'username');
                    608:             $usertype =
                    609:                 &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules);
                    610:         my $cancreate =
                    611:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                    612:                                                    $usertype);
                    613:         if (!$cancreate) {
                    614:             my $helplink = ' href="javascript:helpMenu('."'display'".')"';
                    615:             my %usertypetext = (
                    616:                 official   => 'institutional',
                    617:                 unofficial => 'non-institutional',
                    618:             );
                    619:             my $response;
                    620:             if ($env{'form.origform'} eq 'crtusername') {
                    621:                 $response =  '<span class="LC_warning">'.&mt('No match was found for the username ([_1]) in LON-CAPA domain: [_2]',$ccuname,$ccdomain).
                    622:                             '</span><br />';
                    623:             }
                    624:             $response .= '<span class="LC_warning">'.&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.").' '.&mt('Contact the <a[_1]>helpdesk</a> for assistance.',$helplink).'</span><br /><br />';
                    625:             $env{'form.phase'} = '';
                    626:             &print_username_entry_form($r,$context,$response);
                    627:             return;
                    628:         }
1.188     raeburn   629:         $newuser = 1;
1.193     raeburn   630:         my $checkhash;
                    631:         my $checks = { 'username' => 1 };
1.196     raeburn   632:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn   633:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn   634:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                    635:         if (ref($alerts{'username'}) eq 'HASH') {
                    636:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                    637:                 my $domdesc =
1.193     raeburn   638:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn   639:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                    640:                     my $userchkmsg;
                    641:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                    642:                         $userchkmsg = 
                    643:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn   644:                                                                  $domdesc,1).
                    645:                         &Apache::loncommon::user_rule_formats($ccdomain,
                    646:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                    647:                             'username');
1.196     raeburn   648:                     }
1.215     raeburn   649:                     $env{'form.phase'} = '';
1.207     raeburn   650:                     &print_username_entry_form($r,$context,$userchkmsg);
1.196     raeburn   651:                     return;
1.215     raeburn   652:                 }
1.193     raeburn   653:             }
1.185     raeburn   654:         }
1.187     raeburn   655:     } else {
1.188     raeburn   656:         $newuser = 0;
1.185     raeburn   657:     }
1.160     raeburn   658:     if ($response) {
1.215     raeburn   659:         $response = '<br />'.$response;
1.160     raeburn   660:     }
1.149     raeburn   661: 
1.52      matthew   662:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn   663:     my $dc_setcourse_code = '';
1.119     raeburn   664:     my $nondc_setsection_code = '';                                        
1.112     albertel  665:     my %loaditem;
1.114     albertel  666: 
1.216     raeburn   667:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn   668: 
1.216     raeburn   669:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,
                    670:                                $groupslist,$newuser,$formname,\%loaditem);
1.233     raeburn   671:     my $args = {'add_entries' => \%loaditem};  
                    672:     if ($env{'form.popup'}) {
                    673:        $args->{'no_nav_bar'} = 1; 
                    674:     }
1.110     albertel  675:     my $start_page = 
1.233     raeburn   676: 	&Apache::loncommon::start_page('User Management',$js,$args);
1.216     raeburn   677:     my %breadcrumb_text = &singleuser_breadcrumb();
1.160     raeburn   678:     &Apache::lonhtmlcommon::add_breadcrumb
1.216     raeburn   679:      ({href=>"javascript:backPage($form)",
                    680:        text=>$breadcrumb_text{'search'},
1.160     raeburn   681:        faq=>282,bug=>'Instructor Interface',});
                    682: 
                    683:     if ($env{'form.phase'} eq 'userpicked') {
                    684:         &Apache::lonhtmlcommon::add_breadcrumb
1.216     raeburn   685:      ({href=>"javascript:backPage($form,'get_user_info','select')",
                    686:        text=>$breadcrumb_text{'userpicked'},
1.160     raeburn   687:        faq=>282,bug=>'Instructor Interface',});
                    688:     }
                    689:     &Apache::lonhtmlcommon::add_breadcrumb
1.216     raeburn   690:       ({href=>"javascript:backPage($form,'$env{'form.phase'}','modify')",
                    691:         text=>$breadcrumb_text{'modify'},
1.160     raeburn   692:         faq=>282,bug=>'Instructor Interface',});
1.224     raeburn   693:     my $helpitem = 'Course_Change_Privileges';
                    694:     if ($env{'form.action'} eq 'singlestudent') {
                    695:         $helpitem = 'Course_Add_Student';
                    696:     }
                    697:     my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management',
                    698:                                                      $helpitem);
1.3       www       699: 
1.25      matthew   700:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn   701: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn   702: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn   703: <input type="hidden" name="ccuname" value="$ccuname" />
                    704: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel  705: <input type="hidden" name="pres_value"  value="" />
                    706: <input type="hidden" name="pres_type"   value="" />
                    707: <input type="hidden" name="pres_marker" value="" />
1.25      matthew   708: ENDFORMINFO
1.2       www       709:     my %inccourses;
1.135     raeburn   710:     foreach my $key (keys(%env)) {
1.139     albertel  711: 	if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
1.2       www       712: 	    $inccourses{$1.'_'.$2}=1;
                    713:         }
1.24      matthew   714:     }
1.216     raeburn   715:     if ($newuser) {
1.134     raeburn   716:         my $portfolioform;
1.267     raeburn   717:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                    718:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                    719:             # Current user has quota or user tools modification privileges
1.188     raeburn   720:             $portfolioform = '<br />'.&portfolio_quota($ccuname,$ccdomain);
1.134     raeburn   721:         }
1.227     raeburn   722:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn   723:         my %lt=&Apache::lonlocal::texthash(
                    724:                 'cnu'            => 'Create New User',
1.213     raeburn   725:                 'ast'            => 'as a student',
1.188     raeburn   726:                 'ind'            => 'in domain',
                    727:                 'lg'             => 'Login Data',
1.190     raeburn   728:                 'hs'             => "Home Server",
1.188     raeburn   729:         );
1.185     raeburn   730: 	$r->print(<<ENDTITLE);
1.110     albertel  731: $start_page
1.160     raeburn   732: $crumbs
                    733: $response
1.25      matthew   734: $forminfo
1.31      matthew   735: <script type="text/javascript" language="Javascript">
1.20      harris41  736: $loginscript
1.31      matthew   737: </script>
1.20      harris41  738: <input type='hidden' name='makeuser' value='1' />
1.216     raeburn   739: <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain
1.185     raeburn   740: ENDTITLE
1.213     raeburn   741:         if ($env{'form.action'} eq 'singlestudent') {
                    742:             $r->print(' ('.$lt{'ast'}.')');
                    743:         }
                    744:         $r->print('</h2>'."\n".'<div class="LC_left_float">');
1.206     raeburn   745:         my $personal_table = 
1.210     raeburn   746:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                    747:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn   748:         $r->print($personal_table);
1.187     raeburn   749:         my ($home_server_pick,$numlib) = 
                    750:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                    751:                                                       'default','hide');
                    752:         if ($numlib > 1) {
                    753:             $r->print("
1.185     raeburn   754: <br />
1.187     raeburn   755: $lt{'hs'}: $home_server_pick
                    756: <br />");
                    757:         } else {
                    758:             $r->print($home_server_pick);
                    759:         }
1.188     raeburn   760:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                    761:                   $lt{'lg'}.'</h3>');
1.185     raeburn   762:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn   763:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                    764:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                    765:             my ($rules,$ruleorder) = 
                    766:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn   767:             if (ref($rules) eq 'HASH') {
1.193     raeburn   768:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                    769:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn   770:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn   771:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn   772:                     } else { 
1.193     raeburn   773:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.185     raeburn   774:                         if ($authtype =~ /^krb(4|5)$/) {
                    775:                             my $ver = $1;
                    776:                             if ($authparm ne '') {
                    777:                                 $fixedauth = <<"KERB"; 
                    778: <input type="hidden" name="login" value="krb" />
                    779: <input type="hidden" name="krbver" value="$ver" />
                    780: <input type="hidden" name="krbarg" value="$authparm" />
                    781: KERB
1.193     raeburn   782:                                 $authmsg = $rules->{$matchedrule}{'authmsg'};    
1.185     raeburn   783:                             }
                    784:                         } else {
                    785:                             $fixedauth = 
                    786: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn   787:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn   788:                                 $fixedauth .=    
                    789: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                    790:                             } else {
                    791:                                 $varauth =  
                    792: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
                    793:                             }
                    794:                         }
                    795:                     }
                    796:                 } else {
1.190     raeburn   797:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn   798:                 }
                    799:             }
                    800:             if ($authmsg) {
                    801:                 $r->print(<<ENDAUTH);
                    802: $fixedauth
                    803: $authmsg
                    804: $varauth
                    805: ENDAUTH
                    806:             }
                    807:         } else {
1.190     raeburn   808:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn   809:         }
1.215     raeburn   810:         $r->print($portfolioform);
                    811:         if ($env{'form.action'} eq 'singlestudent') {
                    812:             $r->print(&date_sections_select($context,$newuser,$formname,
                    813:                                             $permission));
                    814:         }
                    815:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn   816:     } else { # user already exists
1.79      albertel  817: 	my %lt=&Apache::lonlocal::texthash(
1.191     raeburn   818:                     'cup'  => "Modify existing user: ",
1.213     raeburn   819:                     'ens'  => "Enroll one student: ",
1.72      sakharuk  820:                     'id'   => "in domain",
                    821: 				       );
1.26      matthew   822: 	$r->print(<<ENDCHANGEUSER);
1.110     albertel  823: $start_page
1.160     raeburn   824: $crumbs
1.25      matthew   825: $forminfo
1.213     raeburn   826: <h2>
1.26      matthew   827: ENDCHANGEUSER
1.213     raeburn   828:         if ($env{'form.action'} eq 'singlestudent') {
                    829:             $r->print($lt{'ens'});
                    830:         } else {
                    831:             $r->print($lt{'cup'});
                    832:         }
                    833:         $r->print(' "'.$ccuname.'" '.$lt{'id'}.' "'.$ccdomain.'"</h2>'.
                    834:                   "\n".'<div class="LC_left_float">');
1.206     raeburn   835:         my ($personal_table,$showforceid) = 
1.210     raeburn   836:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                    837:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn   838:         $r->print($personal_table);
                    839:         if ($showforceid) {
1.203     raeburn   840:             $r->print(&Apache::lonuserutils::forceid_change($context));
1.199     raeburn   841:         }
                    842:         $r->print('</div>');
1.227     raeburn   843:         my $user_auth_text =  &user_authentication($ccuname,$ccdomain,$formname);
1.267     raeburn   844:         my ($user_quota_text,$user_tools_text);
                    845:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                    846:             (&Apache::lonnet::allowed('mut',$ccdomain))) {
1.188     raeburn   847:             # Current user has quota modification privileges
                    848:             $user_quota_text = &portfolio_quota($ccuname,$ccdomain);
1.267     raeburn   849:         }
                    850:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    851:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                    852:                 # Get the user's portfolio information
                    853:                 my %portq = &Apache::lonnet::get('environment',['portfolioquota'],
                    854:                                                  $ccdomain,$ccuname);
                    855:                 my %lt=&Apache::lonlocal::texthash(
                    856:                     'dska'  => "Disk space allocated to user's portfolio files",
                    857:                     'youd'  => "You do not have privileges to modify the portfolio quota for this user.",
                    858:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                    859:                 );
                    860:                 $user_quota_text = <<ENDNOPORTPRIV;
1.188     raeburn   861: <h3>$lt{'dska'}</h3>
                    862: $lt{'youd'} $lt{'ichr'}: $ccdomain
                    863: ENDNOPORTPRIV
1.267     raeburn   864:             }
                    865:         }
                    866:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                    867:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                    868:                 my %lt=&Apache::lonlocal::texthash(
                    869:                     'utav'  => "User Tools Availability",
                    870:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog or Home Page settings for this user.",
                    871:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                    872:                 );
                    873:                 $user_tools_text = <<ENDNOTOOLSPRIV;
                    874: <h3>$lt{'utav'}</h3>
                    875: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                    876: ENDNOTOOLSPRIV
                    877:             }
1.188     raeburn   878:         }
                    879:         if ($user_auth_text ne '') {
                    880:             $r->print('<div class="LC_left_float">'.$user_auth_text);
                    881:             if ($user_quota_text ne '') {
                    882:                 $r->print($user_quota_text);
                    883:             }
1.267     raeburn   884:             if ($user_tools_text ne '') {
                    885:                 $r->print($user_tools_text);
                    886:             }
1.213     raeburn   887:             if ($env{'form.action'} eq 'singlestudent') {
                    888:                 $r->print(&date_sections_select($context,$newuser,$formname));
                    889:             }
1.188     raeburn   890:         } elsif ($user_quota_text ne '') {
1.213     raeburn   891:             $r->print('<div class="LC_left_float">'.$user_quota_text);
1.267     raeburn   892:             if ($user_tools_text ne '') {
                    893:                 $r->print($user_tools_text);
                    894:             }
                    895:             if ($env{'form.action'} eq 'singlestudent') {
                    896:                 $r->print(&date_sections_select($context,$newuser,$formname));
                    897:             }
                    898:         } elsif ($user_tools_text ne '') {
                    899:             $r->print('<div class="LC_left_float">'.$user_tools_text);
1.213     raeburn   900:             if ($env{'form.action'} eq 'singlestudent') {
                    901:                 $r->print(&date_sections_select($context,$newuser,$formname));
                    902:             }
                    903:         } else {
                    904:             if ($env{'form.action'} eq 'singlestudent') {
                    905:                 $r->print('<div class="LC_left_float">'.
                    906:                           &date_sections_select($context,$newuser,$formname));
                    907:             }
1.188     raeburn   908:         }
1.213     raeburn   909:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.217     raeburn   910:         if ($env{'form.action'} ne 'singlestudent') {
                    911:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses);
                    912:         }
1.25      matthew   913:     } ## End of new user/old user logic
1.218     raeburn   914: 
                    915:     if ($env{'form.action'} eq 'singlestudent') {
                    916:         $r->print('<br /><input type="button" value="'.&mt('Enroll Student').'" onClick="setSections(this.form)" />'."\n");
                    917:     } else {
                    918:         $r->print('<h3>'.&mt('Add Roles').'</h3>');
                    919:         my $addrolesdisplay = 0;
                    920:         if ($context eq 'domain' || $context eq 'author') {
                    921:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                    922:         }
                    923:         if ($context eq 'domain') {
                    924:             my $add_domainroles = &new_domain_roles($r);
                    925:             if (!$addrolesdisplay) {
                    926:                 $addrolesdisplay = $add_domainroles;
1.2       www       927:             }
1.218     raeburn   928:             $r->print(&course_level_dc($env{'request.role.domain'},'Course'));
1.262     schafran  929:             $r->print('<br /><input type="button" value="'.&mt('Save').'" onClick="setCourse()" />'."\n");
1.218     raeburn   930:         } elsif ($context eq 'author') {
                    931:             if ($addrolesdisplay) {
1.262     schafran  932:                 $r->print('<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn   933:                 if ($newuser) {
1.227     raeburn   934:                     $r->print(' onClick="auth_check()" \>'."\n");
1.218     raeburn   935:                 } else {
                    936:                     $r->print('onClick="this.form.submit()" \>'."\n");
                    937:                 }
1.188     raeburn   938:             } else {
1.218     raeburn   939:                 $r->print('<br /><a href="javascript:backPage(document.cu)">'.
                    940:                           &mt('Back to previous page').'</a>');
1.188     raeburn   941:             }
                    942:         } else {
1.218     raeburn   943:             $r->print(&course_level_table(%inccourses));
1.262     schafran  944:             $r->print('<br /><input type="button" value="'.&mt('Save').'" onClick="setSections(this.form)" />'."\n");
1.188     raeburn   945:         }
1.88      raeburn   946:     }
1.188     raeburn   947:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn   948:     $r->print('<input type="hidden" name="currstate" value="" />');
1.160     raeburn   949:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" />');
1.110     albertel  950:     $r->print("</form>".&Apache::loncommon::end_page());
1.218     raeburn   951:     return;
1.2       www       952: }
1.1       www       953: 
1.213     raeburn   954: sub singleuser_breadcrumb {
                    955:     my %breadcrumb_text;
                    956:     if ($env{'form.action'} eq 'singlestudent') {
                    957:         $breadcrumb_text{'search'} = 'Enroll a student';
                    958:         $breadcrumb_text{'userpicked'} = 'Select a user',
                    959:         $breadcrumb_text{'modify'} = 'Set section/dates',
                    960:     } else {
1.229     raeburn   961:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn   962:         $breadcrumb_text{'userpicked'} = 'Select a user',
                    963:         $breadcrumb_text{'modify'} = 'Set user role',
                    964:     }
                    965:     return %breadcrumb_text;
                    966: }
                    967: 
                    968: sub date_sections_select {
                    969:     my ($context,$newuser,$formname,$permission) = @_;
                    970:     my $cid = $env{'request.course.id'};
                    971:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                    972:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                    973:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                    974:                                                   undef,$formname,$permission);
                    975:     my $rowtitle = 'Section';
                    976:     my $secbox = '<h3>'.&mt('Section').'</h3>'."\n".
                    977:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
                    978:                                               $permission);
                    979:     my $output = $date_table.$secbox;
                    980:     return $output;
                    981: }
                    982: 
1.216     raeburn   983: sub validation_javascript {
                    984:     my ($context,$ccdomain,$pjump_def,$groupslist,$newuser,$formname,
                    985:         $loaditem) = @_;
                    986:     my $dc_setcourse_code = '';
                    987:     my $nondc_setsection_code = '';
                    988:     if ($context eq 'domain') {
                    989:         my $dcdom = $env{'request.role.domain'};
                    990:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn   991:         $dc_setcourse_code = 
                    992:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn   993:     } else {
1.227     raeburn   994:         my $checkauth; 
                    995:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                    996:             $checkauth = 1;
                    997:         }
                    998:         if ($context eq 'course') {
                    999:             $nondc_setsection_code =
                   1000:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
                   1001:                                                               undef,$checkauth);
                   1002:         }
                   1003:         if ($checkauth) {
                   1004:             $nondc_setsection_code .= 
                   1005:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1006:         }
1.216     raeburn  1007:     }
                   1008:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1009:                                    $nondc_setsection_code,$groupslist);
                   1010:     my ($jsback,$elements) = &crumb_utilities();
                   1011:     $js .= "\n".
1.227     raeburn  1012:            '<script type="text/javascript">'."\n".$jsback."\n".'</script>';
1.216     raeburn  1013:     return $js;
                   1014: }
                   1015: 
1.217     raeburn  1016: sub display_existing_roles {
                   1017:     my ($r,$ccuname,$ccdomain,$inccourses) = @_;
                   1018:     my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
                   1019:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1020:     my ($tmp) = keys(%rolesdump);
                   1021:     if ($tmp !~ /^(con_lost|error)/i) {
                   1022:         my $now=time;
                   1023:         my %lt=&Apache::lonlocal::texthash(
                   1024:                     'rer'  => "Existing Roles",
                   1025:                     'rev'  => "Revoke",
                   1026:                     'del'  => "Delete",
                   1027:                     'ren'  => "Re-Enable",
                   1028:                     'rol'  => "Role",
                   1029:                     'ext'  => "Extent",
                   1030:                     'sta'  => "Start",
                   1031:                     'end'  => "End",
                   1032:                                        );
                   1033:         my (%roletext,%sortrole,%roleclass,%rolepriv);
                   1034:         foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1035:                                     my $b1=join('_',(split('_',$b))[1,0]);
                   1036:                                     return $a1 cmp $b1;
                   1037:                                 } keys(%rolesdump)) {
                   1038:             next if ($area =~ /^rolesdef/);
                   1039:             my $envkey=$area;
                   1040:             my $role = $rolesdump{$area};
                   1041:             my $thisrole=$area;
                   1042:             $area =~ s/\_\w\w$//;
                   1043:             my ($role_code,$role_end_time,$role_start_time) =
                   1044:                 split(/_/,$role);
                   1045: # Is this a custom role? Get role owner and title.
                   1046:             my ($croleudom,$croleuname,$croletitle)=
                   1047:                 ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1048:             my $allowed=0;
                   1049:             my $delallowed=0;
                   1050:             my $sortkey=$role_code;
                   1051:             my $class='Unknown';
                   1052:             if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1053:                 $class='Course';
                   1054:                 my ($coursedom,$coursedir) = ($1,$2);
                   1055:                 $sortkey.="\0$coursedom";
                   1056:                 # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1057:                 my %coursedata=
                   1058:                     &Apache::lonnet::coursedescription($1.'_'.$2);
                   1059:                 my $carea;
                   1060:                 if (defined($coursedata{'description'})) {
                   1061:                     $carea=$coursedata{'description'}.
                   1062:                         '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
1.259     bisitz   1063:      &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
1.217     raeburn  1064:                     $sortkey.="\0".$coursedata{'description'};
                   1065:                     $class=$coursedata{'type'};
                   1066:                 } else {
                   1067:                     $carea=&mt('Unavailable course').': '.$area;
                   1068:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1069:                 }
                   1070:                 $sortkey.="\0$coursedir";
                   1071:                 $inccourses->{$1.'_'.$2}=1;
                   1072:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
                   1073:                     (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1074:                     $allowed=1;
                   1075:                 }
                   1076:                 if ((&Apache::lonnet::allowed('dro',$1)) ||
                   1077:                     (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1078:                     $delallowed=1;
                   1079:                 }
                   1080: # - custom role. Needs more info, too
                   1081:                 if ($croletitle) {
                   1082:                     if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
                   1083:                         $allowed=1;
                   1084:                         $thisrole.='.'.$role_code;
                   1085:                     }
                   1086:                 }
                   1087:                 # Compute the background color based on $area
                   1088:                 if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
                   1089:                     $carea.='<br />Section: '.$3;
                   1090:                     $sortkey.="\0$3";
                   1091:                     if (!$allowed) {
                   1092:                         if ($env{'request.course.sec'} eq $3) {
                   1093:                             if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2.'/'.$3)) {
                   1094:                                 $allowed = 1;
                   1095:                             }
                   1096:                         }
                   1097:                     }
                   1098:                 }
                   1099:                 $area=$carea;
                   1100:             } else {
                   1101:                 $sortkey.="\0".$area;
                   1102:                 # Determine if current user is able to revoke privileges
                   1103:                 if ($area=~m{^/($match_domain)/}) {
                   1104:                     if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1105:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1106:                         $allowed=1;
                   1107:                     }
                   1108:                     if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1109:                          (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1110:                         ($role_code ne 'dc')) {
                   1111:                         $delallowed=1;
                   1112:                     }
                   1113:                 } else {
                   1114:                     if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
                   1115:                         $allowed=1;
                   1116:                     }
                   1117:                 }
                   1118:                 if ($role_code eq 'ca' || $role_code eq 'au') {
                   1119:                     $class='Construction Space';
                   1120:                 } elsif ($role_code eq 'su') {
                   1121:                     $class='System';
                   1122:                 } else {
                   1123:                     $class='Domain';
                   1124:                 }
                   1125:             }
                   1126:             if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1127:                 $area=~m{/($match_domain)/($match_username)};
                   1128:                 if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1129:                     $allowed=1;
                   1130:                 } else {
                   1131:                     $allowed=0;
                   1132:                 }
                   1133:             }
                   1134:             my $row = '';
                   1135:             $row.= '<td>';
                   1136:             my $active=1;
                   1137:             $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1138:             if (($active) && ($allowed)) {
                   1139:                 $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1140:             } else {
                   1141:                 if ($active) {
                   1142:                    $row.='&nbsp;';
                   1143:                 } else {
                   1144:                    $row.=&mt('expired or revoked');
                   1145:                 }
                   1146:             }
                   1147:             $row.='</td><td>';
                   1148:             if ($allowed && !$active) {
                   1149:                 $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1150:             } else {
                   1151:                 $row.='&nbsp;';
                   1152:             }
                   1153:             $row.='</td><td>';
                   1154:             if ($delallowed) {
                   1155:                 $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1156:             } else {
                   1157:                 $row.='&nbsp;';
                   1158:             }
                   1159:             my $plaintext='';
                   1160:             if (!$croletitle) {
                   1161:                 $plaintext=&Apache::lonnet::plaintext($role_code,$class)
                   1162:             } else {
                   1163:                 $plaintext=
                   1164:         "Customrole '$croletitle'<br />defined by $croleuname\@$croleudom";
                   1165:             }
                   1166:             $row.= '</td><td>'.$plaintext.
                   1167:                    '</td><td>'.$area.
                   1168:                    '</td><td>'.($role_start_time?localtime($role_start_time)
                   1169:                                                 : '&nbsp;' ).
                   1170:                    '</td><td>'.($role_end_time  ?localtime($role_end_time)
                   1171:                                                 : '&nbsp;' )
                   1172:                    ."</td>";
                   1173:             $sortrole{$sortkey}=$envkey;
                   1174:             $roletext{$envkey}=$row;
                   1175:             $roleclass{$envkey}=$class;
                   1176:             $rolepriv{$envkey}=$allowed;
                   1177:             #$r->print($row);
                   1178:         } # end of foreach        (table building loop)
                   1179:         my $rolesdisplay = 0;
                   1180:         my %output = ();
                   1181:         foreach my $type ('Construction Space','Course','Group','Domain','System','Unknown') {
                   1182:             $output{$type} = '';
                   1183:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1184:                 if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1185:                     $output{$type}.=
                   1186:                           &Apache::loncommon::start_data_table_row().
                   1187:                           $roletext{$sortrole{$which}}.
                   1188:                           &Apache::loncommon::end_data_table_row();
                   1189:                 }
                   1190:             }
                   1191:             unless($output{$type} eq '') {
                   1192:                 $output{$type} = '<tr class="LC_info_row">'.
                   1193:                           "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1194:                            $output{$type};
                   1195:                 $rolesdisplay = 1;
                   1196:             }
                   1197:         }
                   1198:         if ($rolesdisplay == 1) {
                   1199:             $r->print('
                   1200: <h3>'.$lt{'rer'}.'</h3>'.
                   1201: &Apache::loncommon::start_data_table("LC_createuser").
                   1202: &Apache::loncommon::start_data_table_header_row().
                   1203: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
                   1204: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   1205: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   1206: &Apache::loncommon::end_data_table_header_row());
                   1207:            foreach my $type ('Construction Space','Course','Group','Domain','System','Unknown') {
                   1208:                 if ($output{$type}) {
                   1209:                     $r->print($output{$type}."\n");
                   1210:                 }
                   1211:             }
                   1212:             $r->print(&Apache::loncommon::end_data_table());
                   1213:         }
                   1214:     }  # End of check for keys in rolesdump
                   1215:     return;
                   1216: }
                   1217: 
1.218     raeburn  1218: sub new_coauthor_roles {
                   1219:     my ($r,$ccuname,$ccdomain) = @_;
                   1220:     my $addrolesdisplay = 0;
                   1221:     #
                   1222:     # Co-Author
                   1223:     #
                   1224:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1225:                                           $env{'request.role.domain'}) &&
                   1226:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   1227:         # No sense in assigning co-author role to yourself
                   1228:         $addrolesdisplay = 1;
                   1229:         my $cuname=$env{'user.name'};
                   1230:         my $cudom=$env{'request.role.domain'};
                   1231:         my %lt=&Apache::lonlocal::texthash(
                   1232:                     'cs'   => "Construction Space",
                   1233:                     'act'  => "Activate",
                   1234:                     'rol'  => "Role",
                   1235:                     'ext'  => "Extent",
                   1236:                     'sta'  => "Start",
                   1237:                     'end'  => "End",
                   1238:                     'cau'  => "Co-Author",
                   1239:                     'caa'  => "Assistant Co-Author",
                   1240:                     'ssd'  => "Set Start Date",
                   1241:                     'sed'  => "Set End Date"
                   1242:                                        );
                   1243:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   1244:                   &Apache::loncommon::start_data_table()."\n".
                   1245:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   1246:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   1247:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   1248:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   1249:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   1250:                   &Apache::loncommon::start_data_table_row().'
                   1251:            <td>
                   1252:             <input type=checkbox name="act_'.$cudom.'_'.$cuname.'_ca" />
                   1253:            </td>
                   1254:            <td>'.$lt{'cau'}.'</td>
                   1255:            <td>'.$cudom.'_'.$cuname.'</td>
                   1256:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1257:              <a href=
                   1258: "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>
                   1259: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1260: <a href=
                   1261: "javascript:pjump('."'date_end','End Date Co-Author',document.cu.end_$cudom\_$cuname\_ca.value,'end_$cudom\_$cuname\_ca','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'."\n".
                   1262:               &Apache::loncommon::end_data_table_row()."\n".
                   1263:               &Apache::loncommon::start_data_table_row()."\n".
                   1264: '<td><input type=checkbox name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
                   1265: <td>'.$lt{'caa'}.'</td>
                   1266: <td>'.$cudom.'_'.$cuname.'</td>
                   1267: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1268: <a href=
                   1269: "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>
                   1270: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1271: <a href=
                   1272: "javascript:pjump('."'date_end','End Date Assistant Co-Author',document.cu.end_$cudom\_$cuname\_aa.value,'end_$cudom\_$cuname\_aa','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'."\n".
                   1273:              &Apache::loncommon::end_data_table_row()."\n".
                   1274:              &Apache::loncommon::end_data_table());
                   1275:     } elsif ($env{'request.role'} =~ /^au\./) {
                   1276:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1277:                                                 $env{'request.role.domain'}))) {
                   1278:             $r->print('<span class="LC_error">'.
                   1279:                       &mt('You do not have privileges to assign co-author roles.').
                   1280:                       '</span>');
                   1281:         } elsif (($env{'user.name'} eq $ccuname) &&
                   1282:              ($env{'user.domain'} eq $ccdomain)) {
                   1283:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in your own author area in Construction Space is not permitted'));
                   1284:         }
                   1285:     }
                   1286:     return $addrolesdisplay;;
                   1287: }
                   1288: 
                   1289: sub new_domain_roles {
                   1290:     my ($r) = @_;
                   1291:     my $addrolesdisplay = 0;
                   1292:     #
                   1293:     # Domain level
                   1294:     #
                   1295:     my $num_domain_level = 0;
                   1296:     my $domaintext =
                   1297:     '<h4>'.&mt('Domain Level').'</h4>'.
                   1298:     &Apache::loncommon::start_data_table().
                   1299:     &Apache::loncommon::start_data_table_header_row().
                   1300:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   1301:     &mt('Extent').'</th>'.
                   1302:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   1303:     &Apache::loncommon::end_data_table_header_row();
                   1304:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
                   1305:         foreach my $role ('dc','li','dg','au','sc') {
                   1306:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   1307:                my $plrole=&Apache::lonnet::plaintext($role);
                   1308:                my %lt=&Apache::lonlocal::texthash(
                   1309:                     'ssd'  => "Set Start Date",
                   1310:                     'sed'  => "Set End Date"
                   1311:                                        );
                   1312:                $num_domain_level ++;
                   1313:                $domaintext .=
                   1314: &Apache::loncommon::start_data_table_row().
                   1315: '<td><input type=checkbox name="act_'.$thisdomain.'_'.$role.'" /></td>
                   1316: <td>'.$plrole.'</td>
                   1317: <td>'.$thisdomain.'</td>
                   1318: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   1319: <a href=
                   1320: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   1321: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   1322: <a href=
                   1323: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   1324: &Apache::loncommon::end_data_table_row();
                   1325:             }
                   1326:         }
                   1327:     }
                   1328:     $domaintext.= &Apache::loncommon::end_data_table();
                   1329:     if ($num_domain_level > 0) {
                   1330:         $r->print($domaintext);
                   1331:         $addrolesdisplay = 1;
                   1332:     }
                   1333:     return $addrolesdisplay;
                   1334: }
                   1335: 
1.188     raeburn  1336: sub user_authentication {
1.227     raeburn  1337:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  1338:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  1339:     my $outcome;
1.188     raeburn  1340:     # Check for a bad authentication type
                   1341:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   1342:         # bad authentication scheme
                   1343:         my %lt=&Apache::lonlocal::texthash(
                   1344:                        'err'   => "ERROR",
                   1345:                        'uuas'  => "This user has an unrecognized authentication scheme",
                   1346:                        'adcs'  => "Please alert a domain coordinator of this situation",
                   1347:                        'sldb'  => "Please specify login data below",
                   1348:                        'ld'    => "Login Data"
                   1349:         );
                   1350:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  1351:             &initialize_authen_forms($ccdomain,$formname);
                   1352: 
1.190     raeburn  1353:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  1354:             $outcome = <<ENDBADAUTH;
                   1355: <script type="text/javascript" language="Javascript">
                   1356: $loginscript
                   1357: </script>
                   1358: <span class="LC_error">$lt{'err'}:
                   1359: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   1360: <h3>$lt{'ld'}</h3>
                   1361: $choices
                   1362: ENDBADAUTH
                   1363:         } else {
                   1364:             # This user is not allowed to modify the user's
                   1365:             # authentication scheme, so just notify them of the problem
                   1366:             $outcome = <<ENDBADAUTH;
                   1367: <span class="LC_error"> $lt{'err'}: 
                   1368: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   1369: </span>
                   1370: ENDBADAUTH
                   1371:         }
                   1372:     } else { # Authentication type is valid
1.227     raeburn  1373:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  1374:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  1375:             &modify_login_block($ccdomain,$currentauth);
                   1376:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   1377:             # Current user has login modification privileges
                   1378:             my %lt=&Apache::lonlocal::texthash (
                   1379:                            'ld'    => "Login Data",
                   1380:                            'ccld'  => "Change Current Login Data",
                   1381:                            'enld'  => "Enter New Login Data"
                   1382:                                                );
                   1383:             $outcome =
                   1384:                        '<script type="text/javascript" language="Javascript">'."\n".
                   1385:                        $loginscript."\n".
                   1386:                        '</script>'."\n".
                   1387:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   1388:                        &Apache::loncommon::start_data_table().
1.205     raeburn  1389:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  1390:                        '<td>'.$authformnop;
                   1391:             if ($can_modify) {
                   1392:                 $outcome .= '</td>'."\n".
                   1393:                             &Apache::loncommon::end_data_table_row().
                   1394:                             &Apache::loncommon::start_data_table_row().
                   1395:                             '<td>'.$authformcurrent.'</td>'.
                   1396:                             &Apache::loncommon::end_data_table_row()."\n";
                   1397:             } else {
1.200     raeburn  1398:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   1399:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  1400:             }
1.205     raeburn  1401:             foreach my $item (@authform_others) { 
                   1402:                 $outcome .= &Apache::loncommon::start_data_table_row().
                   1403:                             '<td>'.$item.'</td>'.
                   1404:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  1405:             }
1.205     raeburn  1406:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  1407:         } else {
                   1408:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   1409:                 my %lt=&Apache::lonlocal::texthash(
                   1410:                            'ccld'  => "Change Current Login Data",
                   1411:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   1412:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1413:                 );
                   1414:                 $outcome .= <<ENDNOPRIV;
                   1415: <h3>$lt{'ccld'}</h3>
                   1416: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  1417: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  1418: ENDNOPRIV
                   1419:             }
                   1420:         }
                   1421:     }  ## End of "check for bad authentication type" logic
                   1422:     return $outcome;
                   1423: }
                   1424: 
1.187     raeburn  1425: sub modify_login_block {
                   1426:     my ($dom,$currentauth) = @_;
                   1427:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   1428:     my ($authnum,%can_assign) =
                   1429:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  1430:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  1431:     if ($currentauth=~/^krb(4|5):/) {
                   1432:         $authformcurrent=$authformkrb;
                   1433:         if ($can_assign{'int'}) {
1.205     raeburn  1434:             push(@authform_others,$authformint);
1.187     raeburn  1435:         }
                   1436:         if ($can_assign{'loc'}) {
1.205     raeburn  1437:             push(@authform_others,$authformloc);
1.187     raeburn  1438:         }
                   1439:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   1440:             $show_override_msg = 1;
                   1441:         }
                   1442:     } elsif ($currentauth=~/^internal:/) {
                   1443:         $authformcurrent=$authformint;
                   1444:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1445:             push(@authform_others,$authformkrb);
1.187     raeburn  1446:         }
                   1447:         if ($can_assign{'loc'}) {
1.205     raeburn  1448:             push(@authform_others,$authformloc);
1.187     raeburn  1449:         }
                   1450:         if ($can_assign{'int'}) {
                   1451:             $show_override_msg = 1;
                   1452:         }
                   1453:     } elsif ($currentauth=~/^unix:/) {
                   1454:         $authformcurrent=$authformfsys;
                   1455:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1456:             push(@authform_others,$authformkrb);
1.187     raeburn  1457:         }
                   1458:         if ($can_assign{'int'}) {
1.205     raeburn  1459:             push(@authform_others,$authformint);
1.187     raeburn  1460:         }
                   1461:         if ($can_assign{'loc'}) {
1.205     raeburn  1462:             push(@authform_others,$authformloc);
1.187     raeburn  1463:         }
                   1464:         if ($can_assign{'fsys'}) {
                   1465:             $show_override_msg = 1;
                   1466:         }
                   1467:     } elsif ($currentauth=~/^localauth:/) {
                   1468:         $authformcurrent=$authformloc;
                   1469:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1470:             push(@authform_others,$authformkrb);
1.187     raeburn  1471:         }
                   1472:         if ($can_assign{'int'}) {
1.205     raeburn  1473:             push(@authform_others,$authformint);
1.187     raeburn  1474:         }
                   1475:         if ($can_assign{'loc'}) {
                   1476:             $show_override_msg = 1;
                   1477:         }
                   1478:     }
                   1479:     if ($show_override_msg) {
1.205     raeburn  1480:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   1481:                            '</td></tr>'."\n".
                   1482:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   1483:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   1484:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  1485:                             &mt('will override current values').
1.205     raeburn  1486:                             '</span></td></tr></table>';
1.187     raeburn  1487:     }
1.205     raeburn  1488:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  1489: }
                   1490: 
1.188     raeburn  1491: sub personal_data_display {
1.252     raeburn  1492:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray) = @_;
1.257     raeburn  1493:     my ($output,$showforceid,%userenv,%canmodify);
1.219     raeburn  1494:     my @userinfo = ('firstname','middlename','lastname','generation',
                   1495:                     'permanentemail','id');
1.252     raeburn  1496:     my $rowcount = 0;
                   1497:     my $editable = 0;
1.253     raeburn  1498:     if (!$newuser) {
1.188     raeburn  1499:         # Get the users information
                   1500:         %userenv = &Apache::lonnet::get('environment',
                   1501:                    ['firstname','middlename','lastname','generation',
                   1502:                     'permanentemail','id'],$ccdomain,$ccuname);
1.219     raeburn  1503:         %canmodify =
                   1504:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  1505:                                                        \@userinfo,$rolesarray);
1.257     raeburn  1506:     } elsif ($context eq 'selfcreate') {
                   1507:         %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   1508:                                            $inst_results,$rolesarray);
1.188     raeburn  1509:     }
                   1510:     my %lt=&Apache::lonlocal::texthash(
                   1511:                 'pd'             => "Personal Data",
                   1512:                 'firstname'      => "First Name",
                   1513:                 'middlename'     => "Middle Name",
                   1514:                 'lastname'       => "Last Name",
                   1515:                 'generation'     => "Generation",
                   1516:                 'permanentemail' => "Permanent e-mail address",
1.259     bisitz   1517:                 'id'             => "Student/Employee ID",
1.188     raeburn  1518:                 'lg'             => "Login Data"
                   1519:     );
                   1520:     my %textboxsize = (
                   1521:                        firstname      => '15',
                   1522:                        middlename     => '15',
                   1523:                        lastname       => '15',
                   1524:                        generation     => '5',
                   1525:                        permanentemail => '25',
                   1526:                        id             => '15',
                   1527:                       );
                   1528:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   1529:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   1530:               &Apache::lonhtmlcommon::start_pick_box();
                   1531:     foreach my $item (@userinfo) {
                   1532:         my $rowtitle = $lt{$item};
1.252     raeburn  1533:         my $hiderow = 0;
1.188     raeburn  1534:         if ($item eq 'generation') {
                   1535:             $rowtitle = $genhelp.$rowtitle;
                   1536:         }
1.252     raeburn  1537:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  1538:         if ($newuser) {
1.210     raeburn  1539:             if (ref($inst_results) eq 'HASH') {
                   1540:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  1541:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  1542:                 } else {
1.252     raeburn  1543:                     if ($context eq 'selfcreate') {
                   1544:                         if ($canmodify{$item}) { 
                   1545:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   1546:                             $editable ++;
                   1547:                         } else {
                   1548:                             $hiderow = 1;
                   1549:                         }
1.253     raeburn  1550:                     } else {
                   1551:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  1552:                     }
1.210     raeburn  1553:                 }
1.188     raeburn  1554:             } else {
1.252     raeburn  1555:                 if ($context eq 'selfcreate') {
                   1556:                     if ($canmodify{$item}) {
                   1557:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   1558:                         $editable ++;
                   1559:                     } else {
                   1560:                         $hiderow = 1;
                   1561:                     }
1.253     raeburn  1562:                 } else {
                   1563:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  1564:                 }
1.188     raeburn  1565:             }
                   1566:         } else {
1.219     raeburn  1567:             if ($canmodify{$item}) {
1.252     raeburn  1568:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.188     raeburn  1569:             } else {
1.252     raeburn  1570:                 $row .= $userenv{$item};
1.188     raeburn  1571:             }
1.206     raeburn  1572:             if ($item eq 'id') {
1.219     raeburn  1573:                 $showforceid = $canmodify{$item};
                   1574:             }
1.188     raeburn  1575:         }
1.252     raeburn  1576:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   1577:         if (!$hiderow) {
                   1578:             $output .= $row;
                   1579:             $rowcount ++;
                   1580:         }
1.188     raeburn  1581:     }
                   1582:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  1583:     if (wantarray) {
1.252     raeburn  1584:         if ($context eq 'selfcreate') {
                   1585:             return($output,$rowcount,$editable);
                   1586:         } else {
                   1587:             return ($output,$showforceid);
                   1588:         }
1.206     raeburn  1589:     } else {
                   1590:         return $output;
                   1591:     }
1.188     raeburn  1592: }
                   1593: 
1.257     raeburn  1594: sub selfcreate_canmodify {
                   1595:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   1596:     if (ref($inst_results) eq 'HASH') {
                   1597:         my @inststatuses = &get_inststatuses($inst_results);
                   1598:         if (@inststatuses == 0) {
                   1599:             @inststatuses = ('default');
                   1600:         }
                   1601:         $rolesarray = \@inststatuses;
                   1602:     }
                   1603:     my %canmodify =
                   1604:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   1605:                                                    $rolesarray);
                   1606:     return %canmodify;
                   1607: }
                   1608: 
1.252     raeburn  1609: sub get_inststatuses {
                   1610:     my ($insthashref) = @_;
                   1611:     my @inststatuses = ();
                   1612:     if (ref($insthashref) eq 'HASH') {
                   1613:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   1614:             @inststatuses = @{$insthashref->{'inststatus'}};
                   1615:         }
                   1616:     }
                   1617:     return @inststatuses;
                   1618: }
                   1619: 
1.4       www      1620: # ================================================================= Phase Three
1.42      matthew  1621: sub update_user_data {
1.206     raeburn  1622:     my ($r,$context) = @_; 
1.101     albertel 1623:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   1624:                                           $env{'form.ccdomain'});
1.27      matthew  1625:     # Error messages
1.188     raeburn  1626:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  1627:     my $end       = '</span><br /><br />';
                   1628:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  1629:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  1630:                     &mt('Return to previous page').'</a>'.
                   1631:                     &Apache::loncommon::end_page();
                   1632:     my $now = time;
1.40      www      1633:     my $title;
1.101     albertel 1634:     if (exists($env{'form.makeuser'})) {
1.40      www      1635: 	$title='Set Privileges for New User';
                   1636:     } else {
                   1637:         $title='Modify User Privileges';
                   1638:     }
1.213     raeburn  1639:     my $newuser = 0;
1.160     raeburn  1640:     my ($jsback,$elements) = &crumb_utilities();
                   1641:     my $jscript = '<script type="text/javascript">'."\n".
                   1642:                   $jsback."\n".'</script>'."\n";
1.213     raeburn  1643:     my %breadcrumb_text = &singleuser_breadcrumb();
1.233     raeburn  1644:     my $args;
                   1645:     if ($env{'form.popup'}) {
                   1646:         $args->{'no_nav_bar'} = 1;
                   1647:     } else {
                   1648:         $args = undef;
                   1649:     }
                   1650:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
1.160     raeburn  1651:     &Apache::lonhtmlcommon::add_breadcrumb
                   1652:        ({href=>"javascript:backPage(document.userupdate)",
1.213     raeburn  1653:          text=>$breadcrumb_text{'search'},
1.160     raeburn  1654:          faq=>282,bug=>'Instructor Interface',});
                   1655:     if ($env{'form.prevphase'} eq 'userpicked') {
                   1656:         &Apache::lonhtmlcommon::add_breadcrumb
                   1657:            ({href=>"javascript:backPage(document.userupdate,'get_user_info','select')",
1.213     raeburn  1658:              text=>$breadcrumb_text{'userpicked'},
1.160     raeburn  1659:              faq=>282,bug=>'Instructor Interface',});
                   1660:     }
                   1661:     &Apache::lonhtmlcommon::add_breadcrumb
                   1662:        ({href=>"javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
1.219     raeburn  1663:          text=>$breadcrumb_text{'modify'},
1.160     raeburn  1664:          faq=>282,bug=>'Instructor Interface',},
                   1665:         {href=>"/adm/createuser",
                   1666:          text=>"Result",
                   1667:          faq=>282,bug=>'Instructor Interface',});
1.224     raeburn  1668:     my $helpitem = 'Course_Change_Privileges';
                   1669:     if ($env{'form.action'} eq 'singlestudent') {
                   1670:         $helpitem = 'Course_Add_Student';
                   1671:     }
                   1672:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management',
                   1673:                                                  $helpitem));
1.188     raeburn  1674:     $r->print(&update_result_form($uhome));
1.27      matthew  1675:     # Check Inputs
1.101     albertel 1676:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  1677: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  1678: 	return;
                   1679:     }
1.138     albertel 1680:     if (  $env{'form.ccuname'} ne 
                   1681: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.73      sakharuk 1682: 	$r->print($error.&mt('Invalid login name').'.  '.
1.160     raeburn  1683: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid').'.'.
1.193     raeburn  1684: 		  $end.$rtnlink);
1.27      matthew  1685: 	return;
                   1686:     }
1.101     albertel 1687:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  1688: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  1689: 	return;
                   1690:     }
1.138     albertel 1691:     if (  $env{'form.ccdomain'} ne
                   1692: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.73      sakharuk 1693: 	$r->print($error.&mt ('Invalid domain name').'.  '.
1.138     albertel 1694: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid').'.'.
1.193     raeburn  1695: 		  $end.$rtnlink);
1.27      matthew  1696: 	return;
                   1697:     }
1.219     raeburn  1698:     if ($uhome eq 'no_host') {
                   1699:         $newuser = 1;
                   1700:     }
1.101     albertel 1701:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  1702:         # Modifying an existing user, so check the validity of the name
                   1703:         if ($uhome eq 'no_host') {
1.73      sakharuk 1704:             $r->print($error.&mt('Unable to determine home server for ').
1.101     albertel 1705:                       $env{'form.ccuname'}.&mt(' in domain ').
                   1706:                       $env{'form.ccdomain'}.'.');
1.29      matthew  1707:             return;
                   1708:         }
                   1709:     }
1.27      matthew  1710:     # Determine authentication method and password for the user being modified
                   1711:     my $amode='';
                   1712:     my $genpwd='';
1.101     albertel 1713:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 1714: 	$amode='krb';
1.101     albertel 1715: 	$amode.=$env{'form.krbver'};
                   1716: 	$genpwd=$env{'form.krbarg'};
                   1717:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  1718: 	$amode='internal';
1.101     albertel 1719: 	$genpwd=$env{'form.intarg'};
                   1720:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  1721: 	$amode='unix';
1.101     albertel 1722: 	$genpwd=$env{'form.fsysarg'};
                   1723:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  1724: 	$amode='localauth';
1.101     albertel 1725: 	$genpwd=$env{'form.locarg'};
1.27      matthew  1726: 	$genpwd=" " if (!$genpwd);
1.101     albertel 1727:     } elsif (($env{'form.login'} eq 'nochange') ||
                   1728:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  1729:         # There is no need to tell the user we did not change what they
                   1730:         # did not ask us to change.
1.35      matthew  1731:         # If they are creating a new user but have not specified login
                   1732:         # information this will be caught below.
1.30      matthew  1733:     } else {
1.193     raeburn  1734: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.30      matthew  1735: 	    return;
1.27      matthew  1736:     }
1.164     albertel 1737: 
1.188     raeburn  1738:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
                   1739: 			 $env{'form.ccuname'}, $env{'form.ccdomain'}).'</h3>');
1.193     raeburn  1740:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.267     raeburn  1741:     my @usertools = ('aboutme','blog','portfolio');
1.101     albertel 1742:     if ($env{'form.makeuser'}) {
1.164     albertel 1743: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  1744:         # Check for the authentication mode and password
                   1745:         if (! $amode || ! $genpwd) {
1.193     raeburn  1746: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  1747: 	    return;
1.18      albertel 1748: 	}
1.29      matthew  1749:         # Determine desired host
1.101     albertel 1750:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  1751:         if (lc($desiredhost) eq 'default') {
                   1752:             $desiredhost = undef;
                   1753:         } else {
1.147     albertel 1754:             my %home_servers = 
                   1755: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  1756:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  1757:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   1758:                 return;
                   1759:             }
                   1760:         }
                   1761:         # Check ID format
                   1762:         my %checkhash;
                   1763:         my %checks = ('id' => 1);
                   1764:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  1765:             'newuser' => $newuser, 
1.196     raeburn  1766:             'id' => $env{'form.cid'},
1.193     raeburn  1767:         );
1.196     raeburn  1768:         if ($env{'form.cid'} ne '') {
                   1769:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   1770:                                           \%rulematch,\%inst_results,\%curr_rules);
                   1771:             if (ref($alerts{'id'}) eq 'HASH') {
                   1772:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   1773:                     my $domdesc =
                   1774:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   1775:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   1776:                         my $userchkmsg;
                   1777:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   1778:                             $userchkmsg  = 
                   1779:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   1780:                                                                     $domdesc,1).
                   1781:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   1782:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   1783:                         }
                   1784:                         $r->print($error.&mt('Invalid ID format').$end.
                   1785:                                   $userchkmsg.$rtnlink);
                   1786:                         return;
                   1787:                     }
                   1788:                 }
1.29      matthew  1789:             }
                   1790:         }
1.27      matthew  1791: 	# Call modifyuser
                   1792: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  1793: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  1794:              $amode,$genpwd,$env{'form.cfirstname'},
                   1795:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   1796:              $env{'form.cgeneration'},undef,$desiredhost,
                   1797:              $env{'form.cpermanentemail'});
1.77      www      1798: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  1799:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 1800:                                                $env{'form.ccdomain'});
1.267     raeburn  1801:         my (%changeHash,%newcustom,%changed);
                   1802:         if ($uhome ne 'no_host') {
                   1803:             if ($env{'form.customquota'} == 1) {
                   1804:                 if ($env{'form.portfolioquota'} eq '') {
                   1805:                     $newcustom{'quota'} = 0;
                   1806:                 } else {
                   1807:                     $newcustom{'quota'} = $env{'form.portfolioquota'};
                   1808:                     $newcustom{'quota'} =~ s/[^\d\.]//g;
                   1809:                 }
                   1810:                 $changed{'quota'} = &quota_admin($newcustom{'quota'},\%changeHash);
                   1811:             }
                   1812:             foreach my $item (@usertools) {
                   1813:                 if ($env{'form.custom'.$item} == 1) {
                   1814:                     $newcustom{$item} = $env{'form.tools_'.$item};
                   1815:                     $changed{$item} = &tool_admin($item,$newcustom{$item},\%changeHash);
                   1816:                 }
1.232     raeburn  1817:             }
1.267     raeburn  1818:             if (keys(%changed)) {
1.232     raeburn  1819:                 $changeHash{'firstname'}  = $env{'form.cfirstname'};
                   1820:                 $changeHash{'middlename'} = $env{'form.cmiddlename'};
                   1821:                 $changeHash{'lastname'}   = $env{'form.clastname'};
                   1822:                 $changeHash{'generation'} = $env{'form.cgeneration'};
                   1823:                 $changeHash{'id'}         = $env{'form.cid'};
                   1824:                 $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
1.267     raeburn  1825:                 my $chgresult =
                   1826:                      &Apache::lonnet::put('environment',\%changeHash,
                   1827:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   1828:             } 
1.232     raeburn  1829:         }
1.219     raeburn  1830:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   1831:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 1832:     } elsif (($env{'form.login'} ne 'nochange') &&
                   1833:              ($env{'form.login'} ne ''        )) {
1.27      matthew  1834: 	# Modify user privileges
                   1835:         if (! $amode || ! $genpwd) {
1.193     raeburn  1836: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  1837: 	    return;
1.20      harris41 1838: 	}
1.27      matthew  1839: 	# Only allow authentification modification if the person has authority
1.101     albertel 1840: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 1841: 	    $r->print('Modifying authentication: '.
1.31      matthew  1842:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 1843: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 1844:                        $amode,$genpwd));
1.102     albertel 1845:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 1846: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      1847: 	} else {
1.27      matthew  1848: 	    # Okay, this is a non-fatal error.
1.193     raeburn  1849: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
1.27      matthew  1850: 	}
1.28      matthew  1851:     }
                   1852:     ##
1.213     raeburn  1853:     my (@userroles,%userupdate,$cnum,$cdom,$namechanged);
                   1854:     if ($context eq 'course') {
                   1855:         ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
                   1856:     }
1.101     albertel 1857:     if (! $env{'form.makeuser'} ) {
1.28      matthew  1858:         # Check for need to change
                   1859:         my %userenv = &Apache::lonnet::get
1.134     raeburn  1860:             ('environment',['firstname','middlename','lastname','generation',
1.267     raeburn  1861:              'id','permanentemail','portfolioquota','inststatus','tools.aboutme',
                   1862:              'tools.blog','tools.portfolio'],
1.160     raeburn  1863:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  1864:         my ($tmp) = keys(%userenv);
                   1865:         if ($tmp =~ /^(con_lost|error)/i) { 
                   1866:             %userenv = ();
                   1867:         }
1.206     raeburn  1868:         my $no_forceid_alert;
                   1869:         # Check to see if user information can be changed
                   1870:         my %domconfig =
                   1871:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   1872:                                      $env{'form.ccdomain'});
1.213     raeburn  1873:         my @statuses = ('active','future');
                   1874:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   1875:         my ($auname,$audom);
1.220     raeburn  1876:         if ($context eq 'author') {
1.206     raeburn  1877:             $auname = $env{'user.name'};
                   1878:             $audom = $env{'user.domain'};     
                   1879:         }
                   1880:         foreach my $item (keys(%roles)) {
1.220     raeburn  1881:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  1882:             if ($context eq 'course') {
                   1883:                 if ($cnum ne '' && $cdom ne '') {
                   1884:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   1885:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   1886:                             push(@userroles,$role);
                   1887:                         }
                   1888:                     }
                   1889:                 }
                   1890:             } elsif ($context eq 'author') {
                   1891:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   1892:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   1893:                         push(@userroles,$role);
                   1894:                     }
                   1895:                 }
                   1896:             }
                   1897:         }
1.220     raeburn  1898:         if ($env{'form.action'} eq 'singlestudent') {
                   1899:             if (!grep(/^st$/,@userroles)) {
                   1900:                 push(@userroles,'st');
                   1901:             }
                   1902:         } else {
                   1903:             # Check for course or co-author roles being activated or re-enabled
                   1904:             if ($context eq 'author' || $context eq 'course') {
                   1905:                 foreach my $key (keys(%env)) {
                   1906:                     if ($context eq 'author') {
                   1907:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   1908:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   1909:                                 push(@userroles,$1);
                   1910:                             }
                   1911:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   1912:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   1913:                                 push(@userroles,$1);
                   1914:                             }
1.206     raeburn  1915:                         }
1.220     raeburn  1916:                     } elsif ($context eq 'course') {
                   1917:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   1918:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   1919:                                 push(@userroles,$1);
                   1920:                             }
                   1921:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   1922:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   1923:                                 push(@userroles,$1);
                   1924:                             }
1.206     raeburn  1925:                         }
                   1926:                     }
                   1927:                 }
                   1928:             }
                   1929:         }
                   1930:         #Check to see if we can change personal data for the user 
                   1931:         my (@mod_disallowed,@longroles);
                   1932:         foreach my $role (@userroles) {
                   1933:             if ($role eq 'cr') {
                   1934:                 push(@longroles,'Custom');
                   1935:             } else {
                   1936:                 push(@longroles,&Apache::lonnet::plaintext($role)); 
                   1937:             }
                   1938:         }
1.219     raeburn  1939:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   1940:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   1941:         foreach my $item (@userinfo) {
1.28      matthew  1942:             # Strip leading and trailing whitespace
1.203     raeburn  1943:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  1944:             if (!$canmodify{$item}) {
1.207     raeburn  1945:                 if (defined($env{'form.c'.$item})) {
                   1946:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   1947:                         push(@mod_disallowed,$item);
                   1948:                     }
1.206     raeburn  1949:                 }
                   1950:                 $env{'form.c'.$item} = $userenv{$item};
                   1951:             }
1.28      matthew  1952:         }
1.259     bisitz   1953:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  1954:         my $forceid = $env{'form.forceid'};
                   1955:         my $recurseid = $env{'form.recurseid'};
                   1956:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  1957:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   1958:                                             $env{'form.ccuname'});
                   1959:         if (($uidhash{$env{'form.ccuname'}}) && 
                   1960:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   1961:             (!$forceid)) {
                   1962:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   1963:                 $env{'form.cid'} = $userenv{'id'};
1.259     bisitz   1964:                 $no_forceid_alert = &mt('New Student/Employee ID does not match existing ID for this user.')
                   1965:                                    .'<br />'
                   1966:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   1967:                                    .'<br />'."\n";
1.203     raeburn  1968:             }
                   1969:         }
                   1970:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  1971:             my $checkhash;
                   1972:             my $checks = { 'id' => 1 };
                   1973:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   1974:                    { 'newuser' => $newuser,
                   1975:                      'id'  => $env{'form.cid'}, 
                   1976:                    };
                   1977:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   1978:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   1979:             if (ref($alerts{'id'}) eq 'HASH') {
                   1980:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  1981:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  1982:                 }
                   1983:             }
                   1984:         }
1.213     raeburn  1985:         my ($quotachanged,$oldportfolioquota,$newportfolioquota,
1.204     raeburn  1986:             $inststatus,$oldisdefault,$newisdefault,$olddefquotatext,
1.267     raeburn  1987:             $newdefquotatext,%oldaccess,%oldaccesstext,%newaccess,%newaccesstext);
1.149     raeburn  1988:         my ($defquota,$settingstatus) = 
                   1989:             &Apache::loncommon::default_quota($env{'form.ccdomain'},$inststatus);
1.267     raeburn  1990:         my ($showquota,$showtools);
1.220     raeburn  1991:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   1992:             $showquota = 1;
                   1993:         }
1.267     raeburn  1994:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   1995:             $showtools = 1;
                   1996:         }
                   1997:         my (%changeHash,%changed);
1.204     raeburn  1998:         $changeHash{'portfolioquota'} = $userenv{'portfolioquota'};
1.149     raeburn  1999:         if ($userenv{'portfolioquota'} ne '') {
1.134     raeburn  2000:             $oldportfolioquota = $userenv{'portfolioquota'};
1.149     raeburn  2001:             if ($env{'form.customquota'} == 1) {
                   2002:                 if ($env{'form.portfolioquota'} eq '') {
                   2003:                     $newportfolioquota = 0;
                   2004:                 } else {
                   2005:                     $newportfolioquota = $env{'form.portfolioquota'};
                   2006:                     $newportfolioquota =~ s/[^\d\.]//g;
                   2007:                 }
1.204     raeburn  2008:                 if ($newportfolioquota != $oldportfolioquota) {
1.267     raeburn  2009:                     $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
1.134     raeburn  2010:                 }
1.149     raeburn  2011:             } else {
1.267     raeburn  2012:                 $changed{'quota'} = &quota_admin('',\%changeHash);
1.149     raeburn  2013:                 $newportfolioquota = $defquota;
1.204     raeburn  2014:                 $newisdefault = 1; 
1.134     raeburn  2015:             }
                   2016:         } else {
1.204     raeburn  2017:             $oldisdefault = 1;
1.149     raeburn  2018:             $oldportfolioquota = $defquota;
                   2019:             if ($env{'form.customquota'} == 1) {
                   2020:                 if ($env{'form.portfolioquota'} eq '') {
                   2021:                     $newportfolioquota = 0;
                   2022:                 } else {
                   2023:                     $newportfolioquota = $env{'form.portfolioquota'};
                   2024:                     $newportfolioquota =~ s/[^\d\.]//g;
                   2025:                 }
1.267     raeburn  2026:                 $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
1.149     raeburn  2027:             } else {
                   2028:                 $newportfolioquota = $defquota;
1.204     raeburn  2029:                 $newisdefault = 1;
1.149     raeburn  2030:             }
                   2031:         }
1.204     raeburn  2032:         if ($oldisdefault) {
                   2033:             $olddefquotatext = &get_defaultquota_text($settingstatus);
                   2034:         }
                   2035:         if ($newisdefault) {
                   2036:             $newdefquotatext = &get_defaultquota_text($settingstatus);
1.134     raeburn  2037:         }
1.267     raeburn  2038:         
                   2039:         foreach my $tool (@usertools) {
                   2040:             if ($userenv{'tools.'.$tool} ne '') {
                   2041:                 $oldaccess{$tool} = &mt('custom');
                   2042:                 if ($userenv{'tools_'.$tool}) {
                   2043:                     $oldaccesstext{$tool} = &mt("availability set to 'on'");
                   2044:                 } else {
                   2045:                     $oldaccesstext{$tool} = &mt("availability set to 'off'");
                   2046:                 }
                   2047:                 $changeHash{'tools.'.$tool} = $userenv{'tools.'.$tool};
                   2048:                 if ($env{'form.custom'.$tool} == 1) {
                   2049:                     if ($env{'form.tools_'.$tool} ne $userenv{'tools.'.$tool}) {
                   2050:                         $changed{$tool} = &tool_admin($tool,$env{'form.tools_'.$tool},
                   2051:                                                       \%changeHash);
                   2052:                         if ($changed{$tool}) {
                   2053:                             $newaccess{$tool} = &mt('custom');
                   2054:                             if ($env{'form.tools_'.$tool}) { 
                   2055:                                 $newaccesstext{$tool} = &mt("availability set to 'on'");
                   2056:                             } else {
                   2057:                                 $newaccesstext{$tool} = &mt("availability set to 'off'");
                   2058:                             }
                   2059:                         } else {
                   2060:                             $newaccess{$tool} = $oldaccess{$tool};
                   2061:                             if ($userenv{'tools.'.$tool}) {
                   2062:                                 $newaccesstext{$tool} = &mt("availability set to 'on'");
                   2063:                             } else {
                   2064:                                 $newaccesstext{$tool} = &mt("availability set to 'off'");
                   2065:                             }
                   2066:                         }
                   2067:                     } else {
                   2068:                         $newaccess{$tool} = $oldaccess{$tool};
                   2069:                         $newaccesstext{$tool} = $oldaccesstext{$tool};
                   2070:                     }
                   2071:                 } else {
                   2072:                     $changed{$tool} = &tool_admin($tool,'',\%changeHash);
                   2073:                     if ($changed{$tool}) {
                   2074:                         $newaccess{$tool} = &mt('default');
                   2075:                     } else {
                   2076:                         $newaccess{$tool} = $oldaccess{$tool};
                   2077:                         if ($userenv{'tools.'.$tool}) {
                   2078:                              $newaccesstext{$tool} = &mt("availability set to 'on'");
                   2079:                         } else {
                   2080:                              $newaccesstext{$tool} = &mt("availability set to 'off'");
                   2081:                         }
                   2082:                     }
                   2083:                 }
                   2084:             } else {
                   2085:                 $oldaccess{$tool} = &mt('default');
                   2086:                 if ($env{'form.custom'.$tool} == 1) {
                   2087:                     $changed{$tool} = &tool_admin($tool,$env{'form.tools_'.$tool},
                   2088:                                                   \%changeHash);
                   2089:                     if ($changed{$tool}) {
                   2090:                         $newaccess{$tool} = &mt('custom');
                   2091:                         if ($env{'form.tools_'.$tool}) {
                   2092:                             $newaccesstext{$tool} = &mt("availability set to 'on'");
                   2093:                         } else {
                   2094:                             $newaccesstext{$tool} = &mt("availability set to 'off'");
                   2095:                         }
                   2096:                     } else {
                   2097:                         $newaccess{$tool} = $oldaccess{$tool};
                   2098:                     }
                   2099:                 } else {
                   2100:                     $newaccess{$tool} = $oldaccess{$tool};
                   2101:                 }
                   2102:             }
                   2103:         }
                   2104: 
1.206     raeburn  2105:         if ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
                   2106:             $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
                   2107:             $env{'form.clastname'}   ne $userenv{'lastname'}   ||
                   2108:             $env{'form.cgeneration'} ne $userenv{'generation'} ||
                   2109:             $env{'form.cid'} ne $userenv{'id'}                 ||
                   2110:             $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} ) {
1.134     raeburn  2111:             $namechanged = 1;
                   2112:         }
1.267     raeburn  2113:         if (($namechanged) || (keys(%changed) > 0)) {
1.101     albertel 2114:             $changeHash{'firstname'}  = $env{'form.cfirstname'};
                   2115:             $changeHash{'middlename'} = $env{'form.cmiddlename'};
                   2116:             $changeHash{'lastname'}   = $env{'form.clastname'};
                   2117:             $changeHash{'generation'} = $env{'form.cgeneration'};
1.196     raeburn  2118:             $changeHash{'id'}         = $env{'form.cid'};
1.174     raeburn  2119:             $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
1.267     raeburn  2120:             my ($chgresult,$namechgresult);
                   2121:             if (keys(%changed) > 0) {
                   2122:                 $chgresult = 
1.204     raeburn  2123:                     &Apache::lonnet::put('environment',\%changeHash,
                   2124:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  2125:                 if ($chgresult eq 'ok') {
                   2126:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   2127:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.268.2.3  raeburn  2128:                         my %newenvhash;
                   2129:                         foreach my $key (keys(%changed)) {
                   2130:                             if ($key ne 'quota') {
                   2131:                                 $newenvhash{'environment.tools.'.$key} = 
                   2132:                                     $changeHash{'tools.'.$key};
                   2133:                                 $newenvhash{'environment.availabletools.'.$key} =
                   2134:                                     $changeHash{'tools.'.$key};
                   2135:                             }
                   2136:                         }
                   2137:                         if (keys(%newenvhash)) {
                   2138:                             &Apache::lonnet::appenv(\%newenvhash);
                   2139:                         }
1.267     raeburn  2140:                     }
                   2141:                 }
1.204     raeburn  2142:             }
                   2143:             if ($namechanged) {
                   2144:             # Make the change
                   2145:                 $namechgresult =
                   2146:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   2147:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   2148:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   2149:                         $changeHash{'lastname'},$changeHash{'generation'},
                   2150:                         $changeHash{'id'},undef,$changeHash{'permanentemail'});
1.220     raeburn  2151:                 %userupdate = (
                   2152:                                lastname   => $env{'form.clastname'},
                   2153:                                middlename => $env{'form.cmiddlename'},
                   2154:                                firstname  => $env{'form.cfirstname'},
                   2155:                                generation => $env{'form.cgeneration'},
                   2156:                                id         => $env{'form.cid'},
                   2157:                              );
1.204     raeburn  2158:             }
                   2159:             if (($namechanged && $namechgresult eq 'ok') || 
1.267     raeburn  2160:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  2161:             # Tell the user we changed the name
1.73      sakharuk 2162: 		my %lt=&Apache::lonlocal::texthash(
1.267     raeburn  2163:                              'uic'       => "User Information Changed",             
                   2164:                              'frst'      => "First",
                   2165:                              'mddl'      => "Middle",
                   2166:                              'lst'       => "Last",
                   2167: 			     'gen'       => "Generation",
                   2168:                              'id'        => "Student/Employee ID",
                   2169:                              'mail'      => "Permanent E-mail",
                   2170:                              'disk'      => "Disk space allocated to portfolio files",
                   2171:                              'blog'      => "Blog Availability",
                   2172:                              'aboutme'   => "Home Page Availability",
                   2173:                              'portfolio' => "Portfolio Availability",
                   2174:                              'prvs'      => "Previous",
                   2175:                              'chto'      => "Changed To"
1.73      sakharuk 2176: 						   );
1.201     raeburn  2177:                 $r->print('<h4>'.$lt{'uic'}.'</h4>'.
                   2178:                           &Apache::loncommon::start_data_table().
                   2179:                           &Apache::loncommon::start_data_table_header_row());
1.28      matthew  2180:                 $r->print(<<"END");
1.201     raeburn  2181:     <th>&nbsp;</th>
1.73      sakharuk 2182:     <th>$lt{'frst'}</th>
                   2183:     <th>$lt{'mddl'}</th>
                   2184:     <th>$lt{'lst'}</th>
1.134     raeburn  2185:     <th>$lt{'gen'}</th>
1.196     raeburn  2186:     <th>$lt{'id'}</th>
1.173     raeburn  2187:     <th>$lt{'mail'}</th>
1.201     raeburn  2188: END
1.220     raeburn  2189:                 if ($showquota) {
                   2190:                     $r->print("
                   2191:     <th>$lt{'disk'}</th>\n");
                   2192:                 }
1.267     raeburn  2193:                 if ($showtools) {
                   2194:                     foreach my $item (@usertools) {
                   2195:                         $r->print("
                   2196:     <th>$lt{$item}</th>\n");
                   2197:                     }
                   2198:                 }
1.201     raeburn  2199:                 $r->print(&Apache::loncommon::end_data_table_header_row().
                   2200:                           &Apache::loncommon::start_data_table_row());
                   2201:                 $r->print(<<"END");
                   2202:     <td><b>$lt{'prvs'}</b></td>
1.28      matthew  2203:     <td>$userenv{'firstname'}  </td>
                   2204:     <td>$userenv{'middlename'} </td>
                   2205:     <td>$userenv{'lastname'}   </td>
1.134     raeburn  2206:     <td>$userenv{'generation'} </td>
1.196     raeburn  2207:     <td>$userenv{'id'}</td>
1.160     raeburn  2208:     <td>$userenv{'permanentemail'} </td>
1.201     raeburn  2209: END
1.220     raeburn  2210:                 if ($showquota) {
                   2211:                     $r->print("
                   2212:     <td>$oldportfolioquota Mb $olddefquotatext </td>\n");
                   2213:                 }
1.267     raeburn  2214:                 if ($showtools) {
                   2215:                     foreach my $item (@usertools) {
                   2216:                         $r->print("
                   2217:     <td>$oldaccess{$item} $oldaccesstext{$item} </td>\n");
                   2218:                     }
                   2219:                 }
1.201     raeburn  2220:                 $r->print(&Apache::loncommon::end_data_table_row().
                   2221:                           &Apache::loncommon::start_data_table_row());
                   2222:                 $r->print(<<"END");
1.267     raeburn  2223:     <td><span class="LC_nobreak"><b>$lt{'chto'}</b></span></td>
1.101     albertel 2224:     <td>$env{'form.cfirstname'}  </td>
                   2225:     <td>$env{'form.cmiddlename'} </td>
                   2226:     <td>$env{'form.clastname'}   </td>
1.134     raeburn  2227:     <td>$env{'form.cgeneration'} </td>
1.196     raeburn  2228:     <td>$env{'form.cid'} </td>
1.160     raeburn  2229:     <td>$env{'form.cpermanentemail'} </td>
1.28      matthew  2230: END
1.220     raeburn  2231:                 if ($showquota) {
                   2232:                     $r->print("
                   2233:     <td>$newportfolioquota Mb $newdefquotatext </td>\n");
                   2234:                 }
1.267     raeburn  2235:                 if ($showtools) {
                   2236:                     foreach my $item (@usertools) {
                   2237:                         $r->print("
                   2238:     <td>$newaccess{$item} $newaccesstext{$item} </td>\n");
                   2239:                     }
                   2240:                 }
1.201     raeburn  2241:                 $r->print(&Apache::loncommon::end_data_table_row().
1.206     raeburn  2242:                           &Apache::loncommon::end_data_table().'<br />');
1.203     raeburn  2243:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   2244:                     &Apache::lonnet::idput($env{'form.ccdomain'},
                   2245:                          ($env{'form.ccuname'} => $env{'form.cid'}));
                   2246:                     if (($recurseid) &&
                   2247:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   2248:                         my $idresult = 
                   2249:                             &Apache::lonuserutils::propagate_id_change(
                   2250:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   2251:                                 \%userupdate);
                   2252:                         $r->print('<br />'.$idresult.'<br />');
                   2253:                     }
1.196     raeburn  2254:                 }
1.149     raeburn  2255:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   2256:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   2257:                     my %newenvhash;
                   2258:                     foreach my $key (keys(%changeHash)) {
                   2259:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   2260:                     }
1.238     raeburn  2261:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  2262:                 }
1.28      matthew  2263:             } else { # error occurred
1.188     raeburn  2264:                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                   2265:                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
1.206     raeburn  2266:                       $env{'form.ccdomain'}.'</span><br />');
1.28      matthew  2267:             }
1.101     albertel 2268:         }  else { # End of if ($env ... ) logic
1.267     raeburn  2269:             # They did not want to change the users name, quota or tool availability,
                   2270:             # but we can still tell them what the name and quota and availabilities are  
1.73      sakharuk 2271: 	    my %lt=&Apache::lonlocal::texthash(
1.267     raeburn  2272:                            'id'        => "Student/Employee ID",
                   2273:                            'mail'      => "Permanent e-mail",
                   2274:                            'disk'      => "Disk space allocated to user's portfolio files",
                   2275:                            'blog'      => "Blog Availability",
                   2276:                            'aboutme'   => "Home Page Availability",
                   2277:                            'portfolio' => "Portfolio Availability",
1.73      sakharuk 2278: 					       );
1.134     raeburn  2279:             $r->print(<<"END");
1.196     raeburn  2280: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}
1.28      matthew  2281: END
1.204     raeburn  2282:             if ($userenv{'permanentemail'} ne '') {
                   2283:                 $r->print('<br />['.$lt{'mail'}.': '.
                   2284:                           $userenv{'permanentemail'}.']');
1.134     raeburn  2285:             }
1.267     raeburn  2286:             if ($showtools) {
                   2287:                 foreach my $item (@usertools) {
                   2288:                     $r->print('<br />['.$lt{$item}.': '.$newaccess{$item}.' '.
                   2289:                               $newaccesstext{$item}.']'."\n");
                   2290:                 }
                   2291:             }
1.220     raeburn  2292:             if ($showquota) {
1.267     raeburn  2293:                 $r->print('<br />['.$lt{'disk'}.': '.$oldportfolioquota.' Mb '.
1.220     raeburn  2294:                           $olddefquotatext.']');
                   2295:             }
                   2296:             $r->print('</h4>');
1.28      matthew  2297:         }
1.206     raeburn  2298:         if (@mod_disallowed) {
                   2299:             my ($rolestr,$contextname);
                   2300:             if (@longroles > 0) {
                   2301:                 $rolestr = join(', ',@longroles);
                   2302:             } else {
                   2303:                 $rolestr = &mt('No roles');
                   2304:             }
                   2305:             if ($context eq 'course') {
                   2306:                 $contextname = &mt('course');
                   2307:             } elsif ($context eq 'author') {
                   2308:                 $contextname = &mt('co-author');
                   2309:             }
                   2310:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   2311:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   2312:             foreach my $field (@mod_disallowed) {
                   2313:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   2314:             }
1.207     raeburn  2315:             $r->print('</ul>');
                   2316:             if (@mod_disallowed == 1) {
                   2317:                 $r->print(&mt("You do not have the authority to change this field given the user's current set of active/future [_1] roles:",$contextname));
                   2318:             } else {
                   2319:                 $r->print(&mt("You do not have the authority to change these fields given the user's current set of active/future [_1] roles:",$contextname));
                   2320:             }
                   2321:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'.
                   2322:                       &mt('Contact your <a href="[_1]">helpdesk</a> for more information.',"javascript:helpMenu('display')").'<br />');
1.206     raeburn  2323:         }
1.259     bisitz   2324:         $r->print('<span class="LC_warning">'
                   2325:                   .$no_forceid_alert
                   2326:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   2327:                   .'</span>');
1.4       www      2328:     }
1.220     raeburn  2329:     if ($env{'form.action'} eq 'singlestudent') {
1.239     raeburn  2330:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context);
                   2331:         $r->print('<p><a href="javascript:backPage(document.userupdate)">'.
                   2332:                   &mt('Enroll Another Student').'</a></p>');
1.220     raeburn  2333:     } else {
1.239     raeburn  2334:         my @rolechanges = &update_roles($r,$context);
1.225     raeburn  2335:         if ($namechanged) {
1.220     raeburn  2336:             if ($context eq 'course') {
                   2337:                 if (@userroles > 0) {
1.225     raeburn  2338:                     if ((@rolechanges == 0) || 
                   2339:                         (!(grep(/^st$/,@rolechanges)))) {
                   2340:                         if (grep(/^st$/,@userroles)) {
                   2341:                             my $classlistupdated =
                   2342:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  2343:                                               $cnum,$env{'form.ccdomain'},
                   2344:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  2345:                         }
1.220     raeburn  2346:                     }
                   2347:                 }
                   2348:             }
                   2349:         }
1.226     raeburn  2350:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  2351:                                                      $env{'form.ccdomain'});
                   2352:         if ($env{'form.popup'}) {
                   2353:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   2354:         } else {
1.246     bisitz   2355:             $r->print('<p><a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   2356:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>'
                   2357:                      .('&nbsp;'x5).'<a href="javascript:backPage(document.userupdate)">'
                   2358:                      .&mt('Create/Modify Another User').'</a></p>');
1.233     raeburn  2359:         }
1.220     raeburn  2360:     }
                   2361:     $r->print(&Apache::loncommon::end_page());
                   2362: }
                   2363: 
                   2364: sub update_roles {
1.239     raeburn  2365:     my ($r,$context) = @_;
1.4       www      2366:     my $now=time;
1.225     raeburn  2367:     my @rolechanges;
1.220     raeburn  2368:     my %disallowed;
1.73      sakharuk 2369:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.135     raeburn  2370:     foreach my $key (keys (%env)) {
                   2371: 	next if (! $env{$key});
1.190     raeburn  2372:         next if ($key eq 'form.action');
1.27      matthew  2373: 	# Revoke roles
1.135     raeburn  2374: 	if ($key=~/^form\.rev/) {
                   2375: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      2376: # Revoke standard role
1.170     albertel 2377: 		my ($scope,$role) = ($1,$2);
                   2378: 		my $result =
                   2379: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   2380: 						$env{'form.ccuname'},
1.239     raeburn  2381: 						$scope,$role,'','',$context);
1.170     albertel 2382: 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
                   2383: 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
                   2384: 		if ($role eq 'st') {
1.202     raeburn  2385: 		    my $result = 
1.198     raeburn  2386:                         &Apache::lonuserutils::classlist_drop($scope,
                   2387:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  2388: 			    $now);
1.170     albertel 2389: 		    $r->print($result);
1.53      www      2390: 		}
1.225     raeburn  2391:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   2392:                     push(@rolechanges,$role);
                   2393:                 }
1.196     raeburn  2394: 	    }
1.195     raeburn  2395: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      2396: # Revoke custom role
1.113     raeburn  2397: 		$r->print(&mt('Revoking custom role:').
1.139     albertel 2398:                       ' '.$4.' by '.$3.':'.$2.' in '.$1.': <b>'.
1.101     albertel 2399:                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
1.239     raeburn  2400: 				  $env{'form.ccuname'},$1,$2,$3,$4,'','',$context).
1.102     albertel 2401: 		'</b><br />');
1.225     raeburn  2402:                 if (!grep(/^cr$/,@rolechanges)) {
                   2403:                     push(@rolechanges,'cr');
                   2404:                 }
1.64      www      2405: 	    }
1.135     raeburn  2406: 	} elsif ($key=~/^form\.del/) {
                   2407: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  2408: # Delete standard role
1.170     albertel 2409: 		my ($scope,$role) = ($1,$2);
                   2410: 		my $result =
                   2411: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   2412: 						$env{'form.ccuname'},
1.239     raeburn  2413: 						$scope,$role,$now,0,1,'',
                   2414:                                                 $context);
1.170     albertel 2415: 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
                   2416: 			      '<b>'.$result.'</b>').'<br />');
                   2417: 		if ($role eq 'st') {
1.202     raeburn  2418: 		    my $result = 
1.198     raeburn  2419:                         &Apache::lonuserutils::classlist_drop($scope,
                   2420:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  2421: 			    $now);
1.170     albertel 2422: 		    $r->print($result);
1.81      albertel 2423: 		}
1.225     raeburn  2424:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   2425:                     push(@rolechanges,$role);
                   2426:                 }
1.116     raeburn  2427:             }
1.139     albertel 2428: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  2429:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   2430: # Delete custom role
1.170     albertel 2431:                 $r->print(&mt('Deleting custom role [_1] by [_2]:[_3] in [_4]',
1.116     raeburn  2432:                       $rolename,$rnam,$rdom,$url).': <b>'.
                   2433:                       &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   2434:                          $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
1.240     raeburn  2435:                          0,1,$context).'</b><br />');
1.225     raeburn  2436:                 if (!grep(/^cr$/,@rolechanges)) {
                   2437:                     push(@rolechanges,'cr');
                   2438:                 }
1.116     raeburn  2439:             }
1.135     raeburn  2440: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 2441:             my $udom = $env{'form.ccdomain'};
                   2442:             my $uname = $env{'form.ccuname'};
1.116     raeburn  2443: # Re-enable standard role
1.135     raeburn  2444: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  2445:                 my $url = $1;
                   2446:                 my $role = $2;
                   2447:                 my $logmsg;
                   2448:                 my $output;
                   2449:                 if ($role eq 'st') {
1.141     albertel 2450:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.129     albertel 2451:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
1.220     raeburn  2452:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  2453:                             if ($result eq 'refused' && $logmsg) {
                   2454:                                 $output = $logmsg;
                   2455:                             } else { 
                   2456:                                 $output = "Error: $result\n";
                   2457:                             }
1.89      raeburn  2458:                         } else {
                   2459:                             $output = &mt('Assigning').' '.$role.' in '.$url.
                   2460:                                       &mt('starting').' '.localtime($now).
                   2461:                                       ': <br />'.$logmsg.'<br />'.
                   2462:                                       &mt('Add to classlist').': <b>ok</b><br />';
                   2463:                         }
                   2464:                     }
                   2465:                 } else {
1.101     albertel 2466: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  2467:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   2468:                                $context);
1.266     bisitz   2469: 		    $output = &mt('Re-enabling [_1] in [_2]: [_3]',
                   2470: 			      $role,$url,'<b>'.$result.'</b>').'<br />';
1.27      matthew  2471: 		}
1.89      raeburn  2472:                 $r->print($output);
1.225     raeburn  2473:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   2474:                     push(@rolechanges,$role);
                   2475:                 }
1.113     raeburn  2476: 	    }
1.116     raeburn  2477: # Re-enable custom role
1.139     albertel 2478: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  2479:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   2480:                 my $result = &Apache::lonnet::assigncustomrole(
                   2481:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  2482:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.268     raeburn  2483:                 $r->print(&mt('Re-enabling custom role [_1] by [_2]:[_3] in [_4] : [_5]',
1.266     bisitz   2484:                           $rolename,$rnam,$rdom,$url,'<b>'.$result.'</b>').'<br />');
1.225     raeburn  2485:                 if (!grep(/^cr$/,@rolechanges)) {
                   2486:                     push(@rolechanges,'cr');
                   2487:                 }
1.116     raeburn  2488:             }
1.135     raeburn  2489: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 2490:             my $udom = $env{'form.ccdomain'};
                   2491:             my $uname = $env{'form.ccuname'};
1.141     albertel 2492: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      2493:                 # Activate a custom role
1.83      albertel 2494: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   2495: 		my $url='/'.$one.'/'.$two;
                   2496: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      2497: 
1.101     albertel 2498:                 my $start = ( $env{'form.start_'.$full} ?
                   2499:                               $env{'form.start_'.$full} :
1.88      raeburn  2500:                               $now );
1.101     albertel 2501:                 my $end   = ( $env{'form.end_'.$full} ?
                   2502:                               $env{'form.end_'.$full} :
1.88      raeburn  2503:                               0 );
                   2504:                                                                                      
                   2505:                 # split multiple sections
                   2506:                 my %sections = ();
1.101     albertel 2507:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  2508:                 if ($num_sections == 0) {
1.240     raeburn  2509:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  2510:                 } else {
1.114     albertel 2511: 		    my %curr_groups =
1.117     raeburn  2512: 			&Apache::longroup::coursegroups($one,$two);
1.113     raeburn  2513:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   2514:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   2515:                             exists($curr_groups{$sec})) {
                   2516:                             $disallowed{$sec} = $url;
                   2517:                             next;
                   2518:                         }
                   2519:                         my $securl = $url.'/'.$sec;
1.240     raeburn  2520: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  2521:                     }
                   2522:                 }
1.225     raeburn  2523:                 if (!grep(/^cr$/,@rolechanges)) {
                   2524:                     push(@rolechanges,'cr');
                   2525:                 }
1.142     raeburn  2526: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  2527: 		# Activate roles for sections with 3 id numbers
                   2528: 		# set start, end times, and the url for the class
1.83      albertel 2529: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 2530: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   2531: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  2532: 			      $now );
1.101     albertel 2533: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   2534: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  2535: 			      0 );
1.83      albertel 2536: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  2537:                 my $type = 'three';
                   2538:                 # split multiple sections
                   2539:                 my %sections = ();
1.101     albertel 2540:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.88      raeburn  2541:                 if ($num_sections == 0) {
1.240     raeburn  2542:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context));
1.88      raeburn  2543:                 } else {
1.114     albertel 2544:                     my %curr_groups = 
1.117     raeburn  2545: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  2546:                     my $emptysec = 0;
                   2547:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   2548:                         $sec =~ s/\W//g;
1.113     raeburn  2549:                         if ($sec ne '') {
                   2550:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   2551:                                 exists($curr_groups{$sec})) {
                   2552:                                 $disallowed{$sec} = $url;
                   2553:                                 next;
                   2554:                             }
1.88      raeburn  2555:                             my $securl = $url.'/'.$sec;
1.240     raeburn  2556:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context));
1.88      raeburn  2557:                         } else {
                   2558:                             $emptysec = 1;
                   2559:                         }
                   2560:                     }
                   2561:                     if ($emptysec) {
1.240     raeburn  2562:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context));
1.88      raeburn  2563:                     }
1.225     raeburn  2564:                 }
                   2565:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   2566:                     push(@rolechanges,$three);
                   2567:                 }
1.135     raeburn  2568: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  2569: 		# Activate roles for sections with two id numbers
                   2570: 		# set start, end times, and the url for the class
1.101     albertel 2571: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   2572: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  2573: 			      $now );
1.101     albertel 2574: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   2575: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  2576: 			      0 );
1.225     raeburn  2577:                 my $one = $1;
                   2578:                 my $two = $2;
                   2579: 		my $url='/'.$one.'/';
1.88      raeburn  2580:                 # split multiple sections
                   2581:                 my %sections = ();
1.225     raeburn  2582:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  2583:                 if ($num_sections == 0) {
1.240     raeburn  2584:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  2585:                 } else {
                   2586:                     my $emptysec = 0;
                   2587:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   2588:                         if ($sec ne '') {
                   2589:                             my $securl = $url.'/'.$sec;
1.240     raeburn  2590:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  2591:                         } else {
                   2592:                             $emptysec = 1;
                   2593:                         }
                   2594:                     }
                   2595:                     if ($emptysec) {
1.240     raeburn  2596:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  2597:                     }
                   2598:                 }
1.225     raeburn  2599:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   2600:                     push(@rolechanges,$two);
                   2601:                 }
1.64      www      2602: 	    } else {
1.190     raeburn  2603: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      2604:             }
1.113     raeburn  2605:             foreach my $key (sort(keys(%disallowed))) {
                   2606:                 if (($key eq 'none') || ($key eq 'all')) {  
                   2607:                     $r->print('<p>'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key));
                   2608:                 } else {
                   2609:                     $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));
                   2610:                 }
                   2611:                 $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />');
                   2612:             }
                   2613: 	}
1.101     albertel 2614:     } # End of foreach (keys(%env))
1.75      www      2615: # Flush the course logs so reverse user roles immediately updated
                   2616:     &Apache::lonnet::flushcourselogs();
1.225     raeburn  2617:     if (@rolechanges == 0) {
1.193     raeburn  2618:         $r->print(&mt('No roles to modify'));
                   2619:     }
1.225     raeburn  2620:     return @rolechanges;
1.220     raeburn  2621: }
                   2622: 
                   2623: sub enroll_single_student {
1.239     raeburn  2624:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context) = @_;
1.220     raeburn  2625:     $r->print('<h3>'.&mt('Enrolling Student').'</h3>');
                   2626: 
                   2627:     # Remove non alphanumeric values from section
                   2628:     $env{'form.sections'}=~s/\W//g;
                   2629: 
                   2630:     # Clean out any old student roles the user has in this class.
                   2631:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   2632:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   2633:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   2634:     my $enroll_result =
                   2635:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   2636:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   2637:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   2638:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.239     raeburn  2639:             $startdate,'manual',undef,$env{'request.course.id'},'',$context);
1.220     raeburn  2640:     if ($enroll_result =~ /^ok/) {
                   2641:         $r->print(&mt('<b>[_1]</b> enrolled',$env{'form.ccuname'}.':'.$env{'form.ccdomain'}));
                   2642:         if ($env{'form.sections'} ne '') {
                   2643:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   2644:         }
                   2645:         my ($showstart,$showend);
                   2646:         if ($startdate <= $now) {
                   2647:             $showstart = &mt('Access starts immediately');
                   2648:         } else {
                   2649:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   2650:         }
                   2651:         if ($enddate == 0) {
                   2652:             $showend = &mt('ends: no ending date');
                   2653:         } else {
                   2654:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   2655:         }
                   2656:         $r->print('.<br />'.$showstart.'; '.$showend);
                   2657:         if ($startdate <= $now && !$newuser) {
                   2658:             $r->print("<p> ".&mt('If the student is currently logged-in to LON-CAPA, the new role will be available when the student next logs in.')."</p>");
                   2659:         }
                   2660:     } else {
                   2661:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   2662:     }
                   2663:     return;
1.188     raeburn  2664: }
                   2665: 
1.204     raeburn  2666: sub get_defaultquota_text {
                   2667:     my ($settingstatus) = @_;
                   2668:     my $defquotatext; 
                   2669:     if ($settingstatus eq '') {
                   2670:         $defquotatext = &mt('(default)');
                   2671:     } else {
                   2672:         my ($usertypes,$order) =
                   2673:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   2674:         if ($usertypes->{$settingstatus} eq '') {
                   2675:             $defquotatext = &mt('(default)');
                   2676:         } else {
                   2677:             $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
                   2678:         }
                   2679:     }
                   2680:     return $defquotatext;
                   2681: }
                   2682: 
1.188     raeburn  2683: sub update_result_form {
                   2684:     my ($uhome) = @_;
                   2685:     my $outcome = 
                   2686:     '<form name="userupdate" method="post" />'."\n";
1.160     raeburn  2687:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  2688:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  2689:     }
1.207     raeburn  2690:     if ($env{'form.origname'} ne '') {
                   2691:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   2692:     }
1.160     raeburn  2693:     foreach my $item ('sortby','seluname','seludom') {
                   2694:         if (exists($env{'form.'.$item})) {
1.188     raeburn  2695:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  2696:         }
                   2697:     }
1.188     raeburn  2698:     if ($uhome eq 'no_host') {
                   2699:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   2700:     }
                   2701:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
                   2702:                 '<input type ="hidden" name="currstate" value="" />'."\n".
1.220     raeburn  2703:                 '<input type ="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  2704:                 '</form>';
                   2705:     return $outcome;
1.4       www      2706: }
                   2707: 
1.149     raeburn  2708: sub quota_admin {
                   2709:     my ($setquota,$changeHash) = @_;
                   2710:     my $quotachanged;
                   2711:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   2712:         # Current user has quota modification privileges
1.267     raeburn  2713:         if (ref($changeHash) eq 'HASH') {
                   2714:             $quotachanged = 1;
                   2715:             $changeHash->{'portfolioquota'} = $setquota;
                   2716:         }
1.149     raeburn  2717:     }
                   2718:     return $quotachanged;
                   2719: }
                   2720: 
1.267     raeburn  2721: sub tool_admin {
                   2722:     my ($tool,$settool,$changeHash) = @_;
                   2723:     my $toolchanged;
                   2724:     if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   2725:         # Current user has quota modification privileges
                   2726:         if (ref($changeHash) eq 'HASH') {
                   2727:             $toolchanged = 1;
                   2728:             $changeHash->{'tools.'.$tool} = $settool;
                   2729:         }
                   2730:     }
                   2731:     return $toolchanged;
                   2732: }
                   2733: 
1.88      raeburn  2734: sub build_roles {
1.89      raeburn  2735:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  2736:     my $num_sections = 0;
                   2737:     if ($sectionstr=~ /,/) {
                   2738:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  2739:         if ($role eq 'st') {
                   2740:             $secnums[0] =~ s/\W//g;
                   2741:             $$sections{$secnums[0]} = 1;
                   2742:             $num_sections = 1;
                   2743:         } else {
                   2744:             foreach my $sec (@secnums) {
                   2745:                 $sec =~ ~s/\W//g;
1.150     banghart 2746:                 if (!($sec eq "")) {
1.89      raeburn  2747:                     if (exists($$sections{$sec})) {
                   2748:                         $$sections{$sec} ++;
                   2749:                     } else {
                   2750:                         $$sections{$sec} = 1;
                   2751:                         $num_sections ++;
                   2752:                     }
1.88      raeburn  2753:                 }
                   2754:             }
                   2755:         }
                   2756:     } else {
                   2757:         $sectionstr=~s/\W//g;
                   2758:         unless ($sectionstr eq '') {
                   2759:             $$sections{$sectionstr} = 1;
                   2760:             $num_sections ++;
                   2761:         }
                   2762:     }
1.129     albertel 2763: 
1.88      raeburn  2764:     return $num_sections;
                   2765: }
                   2766: 
1.58      www      2767: # ========================================================== Custom Role Editor
                   2768: 
                   2769: sub custom_role_editor {
1.160     raeburn  2770:     my ($r) = @_;
1.101     albertel 2771:     my $rolename=$env{'form.rolename'};
1.58      www      2772: 
1.59      www      2773:     if ($rolename eq 'make new role') {
1.101     albertel 2774: 	$rolename=$env{'form.newrolename'};
1.59      www      2775:     }
                   2776: 
1.63      www      2777:     $rolename=~s/[^A-Za-z0-9]//gs;
1.58      www      2778: 
1.190     raeburn  2779:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
1.58      www      2780: 	&print_username_entry_form($r);
                   2781:         return;
                   2782:     }
1.153     banghart 2783: # ------------------------------------------------------- What can be assigned?
                   2784:     my %full=();
                   2785:     my %courselevel=();
                   2786:     my %courselevelcurrent=();
1.61      www      2787:     my $syspriv='';
                   2788:     my $dompriv='';
                   2789:     my $coursepriv='';
1.153     banghart 2790:     my $body_top;
1.150     banghart 2791:     my ($disp_dummy,$disp_roles) = &Apache::lonnet::get('roles',["st"]);
1.59      www      2792:     my ($rdummy,$roledef)=
                   2793: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      2794: # ------------------------------------------------------- Does this role exist?
1.153     banghart 2795:     $body_top .= '<h2>';
1.59      www      2796:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.153     banghart 2797: 	$body_top .= &mt('Existing Role').' "';
1.61      www      2798: # ------------------------------------------------- Get current role privileges
                   2799: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.59      www      2800:     } else {
1.153     banghart 2801: 	$body_top .= &mt('New Role').' "';
1.59      www      2802: 	$roledef='';
                   2803:     }
1.153     banghart 2804:     $body_top .= $rolename.'"</h2>';
1.135     raeburn  2805:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   2806: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 2807:         if (!$restrict) { $restrict='F'; }
1.60      www      2808:         $courselevel{$priv}=$restrict;
1.61      www      2809:         if ($coursepriv=~/\:$priv/) {
                   2810: 	    $courselevelcurrent{$priv}=1;
                   2811: 	}
1.60      www      2812: 	$full{$priv}=1;
                   2813:     }
                   2814:     my %domainlevel=();
1.61      www      2815:     my %domainlevelcurrent=();
1.135     raeburn  2816:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   2817: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 2818:         if (!$restrict) { $restrict='F'; }
1.60      www      2819:         $domainlevel{$priv}=$restrict;
1.61      www      2820:         if ($dompriv=~/\:$priv/) {
                   2821: 	    $domainlevelcurrent{$priv}=1;
                   2822: 	}
1.60      www      2823: 	$full{$priv}=1;
                   2824:     }
1.61      www      2825:     my %systemlevel=();
                   2826:     my %systemlevelcurrent=();
1.135     raeburn  2827:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   2828: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 2829:         if (!$restrict) { $restrict='F'; }
1.61      www      2830:         $systemlevel{$priv}=$restrict;
                   2831:         if ($syspriv=~/\:$priv/) {
                   2832: 	    $systemlevelcurrent{$priv}=1;
                   2833: 	}
                   2834: 	$full{$priv}=1;
                   2835:     }
1.160     raeburn  2836:     my ($jsback,$elements) = &crumb_utilities();
1.154     banghart 2837:     my $button_code = "\n";
1.153     banghart 2838:     my $head_script = "\n";
                   2839:     $head_script .= '<script type="text/javascript">'."\n";
1.154     banghart 2840:     my @template_roles = ("cc","in","ta","ep","st");
                   2841:     foreach my $role (@template_roles) {
                   2842:         $head_script .= &make_script_template($role);
1.264     bisitz   2843:         $button_code .= &make_button_code($role).' ';
1.154     banghart 2844:     }
1.160     raeburn  2845:     $head_script .= "\n".$jsback."\n".'</script>'."\n";
1.153     banghart 2846:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',$head_script));
1.160     raeburn  2847:    &Apache::lonhtmlcommon::add_breadcrumb
1.190     raeburn  2848:      ({href=>"javascript:backPage(document.form1,'pickrole','')",
                   2849:        text=>"Pick custom role",
1.160     raeburn  2850:        faq=>282,bug=>'Instructor Interface',},
                   2851:       {href=>"javascript:backPage(document.form1,'','')",
                   2852:          text=>"Edit custom role",
                   2853:          faq=>282,bug=>'Instructor Interface',});
1.224     raeburn  2854:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management',
                   2855:                                                   'Course_Editing_Custom_Roles'));
1.160     raeburn  2856: 
1.153     banghart 2857:     $r->print($body_top);
1.73      sakharuk 2858:     my %lt=&Apache::lonlocal::texthash(
                   2859: 		    'prv'  => "Privilege",
1.131     raeburn  2860: 		    'crl'  => "Course Level",
1.73      sakharuk 2861:                     'dml'  => "Domain Level",
1.150     banghart 2862:                     'ssl'  => "System Level");
1.264     bisitz   2863: 
                   2864:     $r->print('<div>'
                   2865:              .'<form action=""><fieldset>'
                   2866:              .'<legend>'.&mt('Select a Template').'</legend>'
                   2867:              .$button_code
                   2868:              .'</fieldset></form>'
                   2869:              .'</div>'
                   2870:     );
                   2871: 
1.61      www      2872:     $r->print(<<ENDCCF);
1.160     raeburn  2873: <form name="form1" method="post">
1.61      www      2874: <input type="hidden" name="phase" value="set_custom_roles" />
                   2875: <input type="hidden" name="rolename" value="$rolename" />
                   2876: ENDCCF
1.135     raeburn  2877:     $r->print(&Apache::loncommon::start_data_table().
                   2878:               &Apache::loncommon::start_data_table_header_row(). 
                   2879: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   2880: '</th><th>'.$lt{'ssl'}.'</th>'.
                   2881:               &Apache::loncommon::end_data_table_header_row());
1.119     raeburn  2882:     foreach my $priv (sort keys %full) {
                   2883:         my $privtext = &Apache::lonnet::plaintext($priv);
1.135     raeburn  2884:         $r->print(&Apache::loncommon::start_data_table_row().
                   2885: 	          '<td>'.$privtext.'</td><td>'.
1.150     banghart 2886:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c" '.
1.119     raeburn  2887:     ($courselevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
1.61      www      2888:     '</td><td>'.
1.150     banghart 2889:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d" '.
1.119     raeburn  2890:     ($domainlevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
1.61      www      2891:     '</td><td>'.
1.150     banghart 2892:     ($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s" '.
1.119     raeburn  2893:     ($systemlevelcurrent{$priv}?'checked="1"':'').' />':'&nbsp;').
1.135     raeburn  2894:     '</td>'.
                   2895:              &Apache::loncommon::end_data_table_row());
1.60      www      2896:     }
1.135     raeburn  2897:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  2898:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  2899:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.179     raeburn  2900:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
1.160     raeburn  2901:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
                   2902:    '<input type="submit" value="'.&mt('Define Role').'" /></form>'.
1.110     albertel 2903: 	      &Apache::loncommon::end_page());
1.61      www      2904: }
1.153     banghart 2905: # --------------------------------------------------------
                   2906: sub make_script_template {
                   2907:     my ($role) = @_;
                   2908:     my %full_c=();
                   2909:     my %full_d=();
                   2910:     my %full_s=();
                   2911:     my $return_script;
                   2912:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   2913:         my ($priv,$restrict)=split(/\&/,$item);
                   2914:         $full_c{$priv}=1;
                   2915:     }
                   2916:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   2917:         my ($priv,$restrict)=split(/\&/,$item);
                   2918:         $full_d{$priv}=1;
                   2919:     }
1.154     banghart 2920:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1.153     banghart 2921:         my ($priv,$restrict)=split(/\&/,$item);
                   2922:         $full_s{$priv}=1;
                   2923:     }
                   2924:     $return_script .= 'function set_'.$role.'() {'."\n";
                   2925:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
                   2926:     my %role_c;
1.155     banghart 2927:     foreach my $priv (@temp) {
1.153     banghart 2928:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   2929:         $role_c{$priv_item} = 1;
                   2930:     }
1.268.2.2  raeburn  2931:     my %role_d;
                   2932:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
                   2933:     foreach my $priv(@temp) {
                   2934:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   2935:         $role_d{$priv_item} = 1;
                   2936:     }
                   2937:     my %role_s;
                   2938:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
                   2939:     foreach my $priv(@temp) {
                   2940:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   2941:         $role_s{$priv_item} = 1;
                   2942:     }
1.153     banghart 2943:     foreach my $priv_item (keys(%full_c)) {
                   2944:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.268.2.2  raeburn  2945:         if ((exists($role_c{$priv})) || (exists($role_d{$priv})) || 
                   2946:             (exists($role_s{$priv}))) {
1.153     banghart 2947:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
                   2948:         } else {
                   2949:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
                   2950:         }
                   2951:     }
1.154     banghart 2952:     foreach my $priv_item (keys(%full_d)) {
                   2953:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.268.2.2  raeburn  2954:         if ((exists($role_d{$priv})) || (exists($role_s{$priv}))) {
1.154     banghart 2955:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
                   2956:         } else {
                   2957:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
                   2958:         }
                   2959:     }
                   2960:     foreach my $priv_item (keys(%full_s)) {
1.153     banghart 2961:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.154     banghart 2962:         if (exists($role_s{$priv})) {
                   2963:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
                   2964:         } else {
                   2965:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
                   2966:         }
1.153     banghart 2967:     }
                   2968:     $return_script .= '}'."\n";
1.154     banghart 2969:     return ($return_script);
                   2970: }
                   2971: # ----------------------------------------------------------
                   2972: sub make_button_code {
                   2973:     my ($role) = @_;
                   2974:     my $label = &Apache::lonnet::plaintext($role);
1.264     bisitz   2975:     my $button_code = '<input type="button" onClick="set_'.$role.'()" value="'.$label.'" />';
1.154     banghart 2976:     return ($button_code);
1.153     banghart 2977: }
1.61      www      2978: # ---------------------------------------------------------- Call to definerole
                   2979: sub set_custom_role {
1.240     raeburn  2980:     my ($r,$context) = @_;
1.101     albertel 2981:     my $rolename=$env{'form.rolename'};
1.63      www      2982:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 2983:     if (!$rolename) {
1.190     raeburn  2984: 	&custom_role_editor($r);
1.61      www      2985:         return;
                   2986:     }
1.160     raeburn  2987:     my ($jsback,$elements) = &crumb_utilities();
                   2988:     my $jscript = '<script type="text/javascript">'.$jsback."\n".'</script>';
                   2989: 
                   2990:     $r->print(&Apache::loncommon::start_page('Save Custom Role'),$jscript);
                   2991:     &Apache::lonhtmlcommon::add_breadcrumb
1.190     raeburn  2992:         ({href=>"javascript:backPage(document.customresult,'pickrole','')",
                   2993:           text=>"Pick custom role",
1.160     raeburn  2994:           faq=>282,bug=>'Instructor Interface',},
                   2995:          {href=>"javascript:backPage(document.customresult,'selected_custom_edit','')",
                   2996:           text=>"Edit custom role",
                   2997:           faq=>282,bug=>'Instructor Interface',},
                   2998:          {href=>"javascript:backPage(document.customresult,'set_custom_roles','')",
                   2999:           text=>"Result",
                   3000:           faq=>282,bug=>'Instructor Interface',});
1.224     raeburn  3001:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management',
                   3002:                                                   'Course_Editing_Custom_Roles'));
1.160     raeburn  3003: 
1.61      www      3004:     my ($rdummy,$roledef)=
1.110     albertel 3005: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   3006: 
1.61      www      3007: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  3008:     $r->print('<h3>');
1.61      www      3009:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 3010: 	$r->print(&mt('Existing Role').' "');
1.61      www      3011:     } else {
1.73      sakharuk 3012: 	$r->print(&mt('New Role').' "');
1.61      www      3013: 	$roledef='';
                   3014:     }
1.188     raeburn  3015:     $r->print($rolename.'"</h3>');
1.61      www      3016: # ------------------------------------------------------- What can be assigned?
                   3017:     my $sysrole='';
                   3018:     my $domrole='';
                   3019:     my $courole='';
                   3020: 
1.135     raeburn  3021:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3022: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3023:         if (!$restrict) { $restrict=''; }
                   3024:         if ($env{'form.'.$priv.'_c'}) {
1.135     raeburn  3025: 	    $courole.=':'.$item;
1.61      www      3026: 	}
                   3027:     }
                   3028: 
1.135     raeburn  3029:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3030: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3031:         if (!$restrict) { $restrict=''; }
                   3032:         if ($env{'form.'.$priv.'_d'}) {
1.135     raeburn  3033: 	    $domrole.=':'.$item;
1.61      www      3034: 	}
                   3035:     }
                   3036: 
1.135     raeburn  3037:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   3038: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3039:         if (!$restrict) { $restrict=''; }
                   3040:         if ($env{'form.'.$priv.'_s'}) {
1.135     raeburn  3041: 	    $sysrole.=':'.$item;
1.61      www      3042: 	}
                   3043:     }
1.63      www      3044:     $r->print('<br />Defining Role: '.
1.61      www      3045: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.101     albertel 3046:     if ($env{'request.course.id'}) {
                   3047:         my $url='/'.$env{'request.course.id'};
1.63      www      3048:         $url=~s/\_/\//g;
1.73      sakharuk 3049: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.101     albertel 3050: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
                   3051: 						$env{'user.name'},
1.63      www      3052: 						$url,
1.101     albertel 3053: 						$env{'user.domain'},
                   3054: 						$env{'user.name'},
1.240     raeburn  3055: 						$rolename,undef,undef,undef,$context));
1.63      www      3056:     }
1.190     raeburn  3057:     $r->print('<p><a href="javascript:backPage(document.customresult,'."'pickrole'".')">'.&mt('Create or edit another custom role').'</a></p><form name="customresult" method="post">');
1.160     raeburn  3058:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
1.110     albertel 3059:     $r->print(&Apache::loncommon::end_page());
1.58      www      3060: }
                   3061: 
1.2       www      3062: # ================================================================ Main Handler
                   3063: sub handler {
                   3064:     my $r = shift;
                   3065:     if ($r->header_only) {
1.68      www      3066:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      3067:        $r->send_http_header;
                   3068:        return OK;
                   3069:     }
1.190     raeburn  3070:     my $context;
                   3071:     if ($env{'request.course.id'}) {
                   3072:         $context = 'course';
                   3073:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  3074:         $context = 'author';
1.190     raeburn  3075:     } else {
                   3076:         $context = 'domain';
                   3077:     }
                   3078:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  3079:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
                   3080:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype']);
1.190     raeburn  3081:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.202     raeburn  3082:     if ($env{'form.action'} ne 'dateselect') {
                   3083:         &Apache::lonhtmlcommon::add_breadcrumb
                   3084:             ({href=>"/adm/createuser",
                   3085:               text=>"User Management"});
                   3086:     }
1.209     raeburn  3087:     my ($permission,$allowed) = 
                   3088:         &Apache::lonuserutils::get_permission($context);
1.190     raeburn  3089:     if (!$allowed) {
                   3090:         $env{'user.error.msg'}=
                   3091:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
                   3092:                                  "or view user status.";
                   3093:         return HTTP_NOT_ACCEPTABLE;
                   3094:     }
                   3095: 
                   3096:     &Apache::loncommon::content_type($r,'text/html');
                   3097:     $r->send_http_header;
                   3098: 
                   3099:     # Main switch on form.action and form.state, as appropriate
                   3100:     if (! exists($env{'form.action'})) {
                   3101:         $r->print(&header());
                   3102:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
1.208     raeburn  3103:         $r->print(&print_main_menu($permission,$context));
1.190     raeburn  3104:         $r->print(&Apache::loncommon::end_page());
                   3105:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
                   3106:         $r->print(&header());
                   3107:         &Apache::lonhtmlcommon::add_breadcrumb
                   3108:             ({href=>'/adm/createuser?action=upload&state=',
                   3109:               text=>"Upload Users List"});
                   3110:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('Upload Users List',
1.224     raeburn  3111:                                                    'Course_Create_Class_List'));
1.190     raeburn  3112:         $r->print('<form name="studentform" method="post" '.
                   3113:                   'enctype="multipart/form-data" '.
                   3114:                   ' action="/adm/createuser">'."\n");
                   3115:         if (! exists($env{'form.state'})) {
                   3116:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   3117:         } elsif ($env{'form.state'} eq 'got_file') {
1.221     raeburn  3118:             &Apache::lonuserutils::print_upload_manager_form($r,$context,
                   3119:                                                              $permission);
1.190     raeburn  3120:         } elsif ($env{'form.state'} eq 'enrolling') {
                   3121:             if ($env{'form.datatoken'}) {
1.221     raeburn  3122:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission);
1.190     raeburn  3123:             }
                   3124:         } else {
                   3125:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   3126:         }
                   3127:         $r->print('</form>'.&Apache::loncommon::end_page());
1.213     raeburn  3128:     } elsif ((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   3129:              eq 'singlestudent')) && ($permission->{'cusr'})) {
1.190     raeburn  3130:         my $phase = $env{'form.phase'};
                   3131:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 3132: 	&Apache::loncreateuser::restore_prev_selections();
                   3133: 	my $srch;
                   3134: 	foreach my $item (@search) {
                   3135: 	    $srch->{$item} = $env{'form.'.$item};
                   3136: 	}
1.207     raeburn  3137:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
                   3138:             ($phase eq 'createnewuser')) {
                   3139:             if ($env{'form.phase'} eq 'createnewuser') {
                   3140:                 my $response;
                   3141:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
                   3142:                     my $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
1.221     raeburn  3143:                     $env{'form.phase'} = '';
1.207     raeburn  3144:                     &print_username_entry_form($r,$context,$response,$srch);
                   3145:                 } else {
                   3146:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   3147:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   3148:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  3149:                                                   $srch,$response,$context,
                   3150:                                                   $permission);
1.207     raeburn  3151:                 }
                   3152:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  3153:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  3154:                     &user_search_result($context,$srch);
1.190     raeburn  3155:                 if ($env{'form.currstate'} eq 'modify') {
                   3156:                     $currstate = $env{'form.currstate'};
                   3157:                 }
                   3158:                 if ($currstate eq 'select') {
                   3159:                     &print_user_selection_page($r,$response,$srch,$results,
1.229     raeburn  3160:                                                \@search,$context);
1.190     raeburn  3161:                 } elsif ($currstate eq 'modify') {
                   3162:                     my ($ccuname,$ccdomain);
                   3163:                     if (($srch->{'srchby'} eq 'uname') && 
                   3164:                         ($srch->{'srchtype'} eq 'exact')) {
                   3165:                         $ccuname = $srch->{'srchterm'};
                   3166:                         $ccdomain= $srch->{'srchdomain'};
                   3167:                     } else {
                   3168:                         my @matchedunames = keys(%{$results});
                   3169:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   3170:                     }
                   3171:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   3172:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
                   3173:                     if ($env{'form.forcenewuser'}) {
                   3174:                         $response = '';
                   3175:                     }
                   3176:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  3177:                                                   $srch,$response,$context,
                   3178:                                                   $permission);
1.190     raeburn  3179:                 } elsif ($currstate eq 'query') {
                   3180:                     &print_user_query_page($r,'createuser');
                   3181:                 } else {
1.229     raeburn  3182:                     $env{'form.phase'} = '';
1.207     raeburn  3183:                     &print_username_entry_form($r,$context,$response,$srch,
1.190     raeburn  3184:                                                $forcenewuser);
                   3185:                 }
                   3186:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   3187:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   3188:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.196     raeburn  3189:                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
1.221     raeburn  3190:                                               $context,$permission);
1.190     raeburn  3191:             }
                   3192:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.206     raeburn  3193:             &update_user_data($r,$context);
1.190     raeburn  3194:         } else {
1.207     raeburn  3195:             &print_username_entry_form($r,$context,undef,$srch);
1.190     raeburn  3196:         }
                   3197:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
                   3198:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.240     raeburn  3199:             &set_custom_role($r,$context);
1.190     raeburn  3200:         } else {
                   3201:             &custom_role_editor($r);
                   3202:         }
1.207     raeburn  3203:     } elsif (($env{'form.action'} eq 'listusers') && 
                   3204:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  3205:         if ($env{'form.phase'} eq 'bulkchange') {
                   3206:             &Apache::lonhtmlcommon::add_breadcrumb
1.221     raeburn  3207:                 ({href=>'/adm/createuser?action=listusers',
                   3208:                   text=>"List Users"},
                   3209:                 {href=>"/adm/createuser",
                   3210:                   text=>"Result"});
1.202     raeburn  3211:             my $setting = $env{'form.roletype'};
                   3212:             my $choice = $env{'form.bulkaction'};
                   3213:             $r->print(&header());
1.221     raeburn  3214:             $r->print(&Apache::lonhtmlcommon::breadcrumbs("Update Users",
1.224     raeburn  3215:                                                           'Course_View_Class_List'));
1.202     raeburn  3216:             if ($permission->{'cusr'}) {
                   3217:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice);
1.221     raeburn  3218:                 $r->print(&Apache::loncommon::end_page());
                   3219:             } else {
                   3220:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  3221:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.221     raeburn  3222:                 $r->print(&Apache::loncommon::end_page());
1.202     raeburn  3223:             }
                   3224:         } else {
                   3225:             &Apache::lonhtmlcommon::add_breadcrumb
                   3226:                 ({href=>'/adm/createuser?action=listusers',
                   3227:                   text=>"List Users"});
                   3228:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   3229:             my $formname = 'studentform';
                   3230:             if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   3231:                 ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   3232:                     &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   3233:                                                             $formname);
                   3234:                 $jscript .= &verify_user_display();
                   3235:                 my $js = &add_script($jscript).$cb_jscript;
                   3236:                 my $loadcode = 
                   3237:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   3238:                 if ($loadcode ne '') {
                   3239:                     $r->print(&header($js,{'onload' => $loadcode,}));
                   3240:                 } else {
                   3241:                     $r->print(&header($js));
                   3242:                 }
1.191     raeburn  3243:             } else {
1.202     raeburn  3244:                 $r->print(&header(&add_script(&verify_user_display())));
1.191     raeburn  3245:             }
1.202     raeburn  3246:             $r->print(&Apache::lonhtmlcommon::breadcrumbs("List Users",
1.224     raeburn  3247:                                                           'Course_View_Class_List'));
1.202     raeburn  3248:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
                   3249:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles);
                   3250:             $r->print(&Apache::loncommon::end_page());
1.191     raeburn  3251:         }
1.213     raeburn  3252:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
                   3253:         $r->print(&header());
                   3254:         &Apache::lonhtmlcommon::add_breadcrumb
                   3255:             ({href=>'/adm/createuser?action=drop',
                   3256:               text=>"Drop Students"});
                   3257:         if (!exists($env{'form.state'})) {
                   3258:             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Drop Students',
                   3259:                                                           'Course_Drop_Student'));
                   3260: 
                   3261:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission);
                   3262:         } elsif ($env{'form.state'} eq 'done') {
                   3263:             &Apache::lonhtmlcommon::add_breadcrumb
                   3264:             ({href=>'/adm/createuser?action=drop',
                   3265:               text=>"Result"});
                   3266:             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Drop Students',
                   3267:                                                           'Course_Drop_Student'));
                   3268:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   3269:                                                     $env{'form.action'});
                   3270:         }
                   3271:         $r->print(&Apache::loncommon::end_page());
1.202     raeburn  3272:     } elsif ($env{'form.action'} eq 'dateselect') {
                   3273:         if ($permission->{'cusr'}) {
                   3274:             $r->print(&header(undef,undef,{'no_nav_bar' => 1}).
1.221     raeburn  3275:                       &Apache::lonuserutils::date_section_selector($context,
                   3276:                                                                    $permission).
1.202     raeburn  3277:                       &Apache::loncommon::end_page());
                   3278:         } else {
                   3279:             $r->print(&header().
                   3280:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>'. 
                   3281:                      &Apache::loncommon::end_page());
                   3282:         }
1.237     raeburn  3283:     } elsif ($env{'form.action'} eq 'selfenroll') {
                   3284:         $r->print(&header());
                   3285:         &Apache::lonhtmlcommon::add_breadcrumb
                   3286:             ({href=>'/adm/createuser?action=selfenroll',
                   3287:               text=>"Configure Self-enrollment"});
                   3288:         if (!exists($env{'form.state'})) {
                   3289:             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Configure Self-enrollment',
                   3290:                                                           'Course_Self_Enrollment'));
1.241     raeburn  3291:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.237     raeburn  3292:             &print_selfenroll_menu($r,$context,$permission);
                   3293:         } elsif ($env{'form.state'} eq 'done') {
                   3294:             &Apache::lonhtmlcommon::add_breadcrumb
                   3295:             ({href=>'/adm/createuser?action=selfenroll',
                   3296:               text=>"Result"});
                   3297:             $r->print(&Apache::lonhtmlcommon::breadcrumbs('Self-enrollment result',
                   3298:                                                           'Course_Self_Enrollment'));
1.241     raeburn  3299:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   3300:             &update_selfenroll_config($r,$context,$permission);
1.237     raeburn  3301:         }
                   3302:         $r->print(&Apache::loncommon::end_page());
1.239     raeburn  3303:     } elsif ($env{'form.action'} eq 'changelogs') {
                   3304:         $r->print(&header());
                   3305:         &Apache::lonhtmlcommon::add_breadcrumb
                   3306:             ({href=>'/adm/createuser?action=changelogs',
                   3307:               text=>"User Management Logs"});
                   3308:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Changes',
                   3309:                                                       'Course_User_Logs'));
                   3310:             &print_userchangelogs_display($r,$context,$permission);
                   3311:         $r->print(&Apache::loncommon::end_page());        
1.190     raeburn  3312:     } else {
                   3313:         $r->print(&header());
1.202     raeburn  3314:         $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management'));
1.207     raeburn  3315:         $r->print(&print_main_menu($permission,$context));
1.190     raeburn  3316:         $r->print(&Apache::loncommon::end_page());
                   3317:     }
                   3318:     return OK;
                   3319: }
                   3320: 
                   3321: sub header {
1.202     raeburn  3322:     my ($jscript,$loaditems,$args) = @_;
1.190     raeburn  3323:     my $start_page;
                   3324:     if (ref($loaditems) eq 'HASH') {
1.202     raeburn  3325:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,{'add_entries' => $loaditems});
1.190     raeburn  3326:     } else {
1.202     raeburn  3327:         $start_page=&Apache::loncommon::start_page('User Management',$jscript,$args);
1.190     raeburn  3328:     }
                   3329:     return $start_page;
                   3330: }
1.2       www      3331: 
1.191     raeburn  3332: sub add_script {
                   3333:     my ($js) = @_;
                   3334:     return '<script type="text/javascript">'."\n".$js."\n".'</script>';
                   3335: }
                   3336: 
1.202     raeburn  3337: sub verify_user_display {
                   3338:     my $output = <<"END";
                   3339: 
                   3340: function display_update() {
                   3341:     document.studentform.action.value = 'listusers';
                   3342:     document.studentform.phase.value = 'display';
                   3343:     document.studentform.submit();
                   3344: }
                   3345: 
                   3346: END
                   3347:     return $output;
                   3348: 
                   3349: }
                   3350: 
1.190     raeburn  3351: ###############################################################
                   3352: ###############################################################
                   3353: #  Menu Phase One
                   3354: sub print_main_menu {
1.208     raeburn  3355:     my ($permission,$context) = @_;
                   3356:     my %links = (
                   3357:                        domain => {
                   3358:                                    upload => 'Upload a File of Users',
1.221     raeburn  3359:                                    singleuser => 'Add/Modify a Single User',
1.208     raeburn  3360:                                    listusers => 'Manage Multiple Users',
                   3361:                                  },
                   3362:                        author => {
                   3363:                                    upload => 'Upload a File of Co-authors',
1.221     raeburn  3364:                                    singleuser => 'Add/Modify a Single Co-author',
1.208     raeburn  3365:                                    listusers => 'Display Co-authors and Manage Multiple Users',
                   3366:                                  },
                   3367:                        course => {
1.268.2.1  raeburn  3368:                                    upload => 'Upload a File of Course Users',
                   3369:                                    singleuser => 'Add/Modify a Single Course User',
                   3370:                                    listusers => 'Display Class Lists and Manage Multiple Users',
1.208     raeburn  3371:                                  },
                   3372:                      );
1.268.2.1  raeburn  3373:     my @menu =
                   3374:         (
                   3375:           { text => $links{$context}{'upload'},
                   3376:             help => 'Course_Create_Class_List',
                   3377:             action => 'upload',
                   3378:             permission => $permission->{'cusr'},
                   3379:             },
                   3380:           { text => $links{$context}{'singleuser'},
                   3381:             help => 'Course_Change_Privileges',
                   3382:             action => 'singleuser',
                   3383:             permission => $permission->{'cusr'},
                   3384:             },
                   3385:           { text => $links{$context}{'listusers'},
                   3386:             help => 'Course_View_Class_List',
                   3387:             action => 'listusers',
                   3388:             permission => ($permission->{'view'} || $permission->{'cusr'}),
                   3389:           },
                   3390:         );
                   3391:     if ($context eq 'domain' || $context eq 'course') {
                   3392:         my $customlink =  { text => 'Edit Custom Roles',
                   3393:                             help => 'Course_Editing_Custom_Roles',
                   3394:                             action => 'custom',
                   3395:                             permission => $permission->{'custom'},
                   3396:                           };
                   3397:         push(@menu,$customlink);
                   3398:     }
                   3399:     if ($context eq 'course') {
                   3400:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
                   3401:         my @courselinks =
                   3402:             (
                   3403:               { text => 'Enroll a Single Student',
                   3404:                  help => 'Course_Add_Student',
                   3405:                  action => 'singlestudent',
                   3406:                  permission => $permission->{'cusr'},
                   3407:                  },
                   3408:               { text => 'Drop Students',
                   3409:                 help => 'Course_Drop_Student',
                   3410:                 action => 'drop',
                   3411:                 permission => $permission->{'cusr'},
                   3412:               });
                   3413:         if (!exists($permission->{'cusr_section'})) {
                   3414:             push(@courselinks,
                   3415:                { text => 'Automated Enrollment Manager',
                   3416:                  help => 'Course_Automated_Enrollment',
                   3417:                  permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                   3418:                                 && $permission->{'cusr'}),
                   3419:                  url  => '/adm/populate',
                   3420:                  },
                   3421:                { text => 'Configure User Self-enrollment',
                   3422:                  help => 'Course_Self_Enrollment',
                   3423:                  action => 'selfenroll',
                   3424:                  permission => $permission->{'cusr'},
                   3425:                });
                   3426:         }
                   3427:         push(@courselinks,
                   3428:                { text => 'Manage Course Groups',
                   3429:                  help => 'Course_Manage_Group',
                   3430:                  permission => $permission->{'grp_manage'},
                   3431:                  url => '/adm/coursegroups?refpage=cusr',
                   3432:                },
                   3433:                { text => 'View Change Logs',
                   3434:                  help => 'Course_User_Logs',
                   3435:                  action => 'changelogs',
                   3436:                  permission => $permission->{'cusr'},
                   3437:                },);
1.250     raeburn  3438: #               { text => 'View Log-in History',
                   3439: #                 help => 'Course_User_Logins',
                   3440: #                 action => 'logins',
                   3441: #                 permission => $permission->{'cusr'},
                   3442: #               });
1.268.2.1  raeburn  3443:         push(@menu,@courselinks);
                   3444:     }
                   3445:     my $menu_html = '';
                   3446:     foreach my $menu_item (@menu) {
                   3447:         next if (! $menu_item->{'permission'});
                   3448:         $menu_html.='<p>';
                   3449:         if (exists($menu_item->{'help'})) {
                   3450:             $menu_html.=
                   3451:                 &Apache::loncommon::help_open_topic($menu_item->{'help'});
                   3452:         }
                   3453:         $menu_html.='<font size="+1">';
                   3454:         if (exists($menu_item->{'url'})) {
                   3455:             $menu_html.=qq{<a href="$menu_item->{'url'}">};
                   3456:         } else {
                   3457:             $menu_html.=
                   3458:                 qq{<a href="/adm/createuser?action=$menu_item->{'action'}">};        }
                   3459:         $menu_html.= &mt($menu_item->{'text'}).'</a></font>';
                   3460:         $menu_html.='</p>';
                   3461:     }
                   3462:     return $menu_html;
1.190     raeburn  3463: }
                   3464: 
1.189     albertel 3465: sub restore_prev_selections {
                   3466:     my %saveable_parameters = ('srchby'   => 'scalar',
                   3467: 			       'srchin'   => 'scalar',
                   3468: 			       'srchtype' => 'scalar',
                   3469: 			       );
                   3470:     &Apache::loncommon::store_settings('user','user_picker',
                   3471: 				       \%saveable_parameters);
                   3472:     &Apache::loncommon::restore_settings('user','user_picker',
                   3473: 					 \%saveable_parameters);
                   3474: }
                   3475: 
1.237     raeburn  3476: sub print_selfenroll_menu {
                   3477:     my ($r,$context,$permission) = @_;
                   3478:     my $formname = 'enrollstudent';
                   3479:     my $nolink = 1;
                   3480:     my ($row,$lt) = &get_selfenroll_titles();
                   3481:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   3482:     my $setsec_js = 
                   3483:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  3484:     my %alerts = &Apache::lonlocal::texthash(
                   3485:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   3486:         butn => 'but no user types have been checked.',
                   3487:         wilf => "Please uncheck 'activate' or check at least one type.",
                   3488:     );
                   3489:     my $selfenroll_js = <<"ENDSCRIPT";
                   3490: function update_types(caller,num) {
                   3491:     var delidx = getIndexByName('selfenroll_delete');
                   3492:     var actidx = getIndexByName('selfenroll_activate');
                   3493:     if (caller == 'selfenroll_all') {
                   3494:         var selall;
                   3495:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   3496:             if (document.$formname.selfenroll_all[i].checked) {
                   3497:                 selall = document.$formname.selfenroll_all[i].value;
                   3498:             }
                   3499:         }
                   3500:         if (selall == 1) {
                   3501:             if (delidx != -1) {
                   3502:                 if (document.$formname.selfenroll_delete.length) {
                   3503:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   3504:                         document.$formname.selfenroll_delete[j].checked = true;
                   3505:                     }
                   3506:                 } else {
                   3507:                     document.$formname.elements[delidx].checked = true;
                   3508:                 }
                   3509:             }
                   3510:             if (actidx != -1) {
                   3511:                 if (document.$formname.selfenroll_activate.length) {
                   3512:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   3513:                         document.$formname.selfenroll_activate[j].checked = false;
                   3514:                     }
                   3515:                 } else {
                   3516:                     document.$formname.elements[actidx].checked = false;
                   3517:                 }
                   3518:             }
                   3519:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   3520:         }
                   3521:     }
                   3522:     if (caller == 'selfenroll_activate') {
                   3523:         if (document.$formname.selfenroll_activate.length) {
                   3524:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   3525:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   3526:                     if (document.$formname.selfenroll_activate[j].checked) {
                   3527:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   3528:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   3529:                                 document.$formname.selfenroll_all[i].checked = false;
                   3530:                             }
                   3531:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   3532:                                 document.$formname.selfenroll_all[i].checked = true;
                   3533:                             }
                   3534:                         }
                   3535:                     }
                   3536:                 }
                   3537:             }
                   3538:         } else {
                   3539:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   3540:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   3541:                     document.$formname.selfenroll_all[i].checked = false;
                   3542:                 }
                   3543:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   3544:                     document.$formname.selfenroll_all[i].checked = true;
                   3545:                 }
                   3546:             }
                   3547:         }
                   3548:     }
                   3549:     if (caller == 'selfenroll_delete') {
                   3550:         if (document.$formname.selfenroll_delete.length) {
                   3551:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   3552:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   3553:                     if (document.$formname.selfenroll_delete[j].checked) {
                   3554:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   3555:                         if (delindex != -1) { 
                   3556:                             if (document.$formname.elements[delindex].length) {
                   3557:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   3558:                                     document.$formname.elements[delindex][k].checked = false;
                   3559:                                 }
                   3560:                             } else {
                   3561:                                 document.$formname.elements[delindex].checked = false;
                   3562:                             }
                   3563:                         }
                   3564:                     }
                   3565:                 }
                   3566:             }
                   3567:         } else {
                   3568:             if (document.$formname.selfenroll_delete.checked) {
                   3569:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   3570:                 if (delindex != -1) {
                   3571:                     if (document.$formname.elements[delindex].length) {
                   3572:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   3573:                             document.$formname.elements[delindex][k].checked = false;
                   3574:                         }
                   3575:                     } else {
                   3576:                         document.$formname.elements[delindex].checked = false;
                   3577:                     }
                   3578:                 }
                   3579:             }
                   3580:         }
                   3581:     }
                   3582:     return;
                   3583: }
                   3584: 
                   3585: function validate_types(form) {
                   3586:     var needaction = new Array();
                   3587:     var countfail = 0;
                   3588:     var actidx = getIndexByName('selfenroll_activate');
                   3589:     if (actidx != -1) {
                   3590:         if (document.$formname.selfenroll_activate.length) {
                   3591:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   3592:                 var num = document.$formname.selfenroll_activate[j].value;
                   3593:                 if (document.$formname.selfenroll_activate[j].checked) {
                   3594:                     countfail = check_types(num,countfail,needaction)
                   3595:                 }
                   3596:             }
                   3597:         } else {
                   3598:             if (document.$formname.selfenroll_activate.checked) {
                   3599:                 var num = document.enrollstudent.selfenroll_activate.value;
                   3600:                 countfail = check_types(num,countfail,needaction)
                   3601:             }
                   3602:         }
                   3603:     }
                   3604:     if (countfail > 0) {
                   3605:         var msg = "$alerts{'acto'}\\n";
                   3606:         var loopend = needaction.length -1;
                   3607:         if (loopend > 0) {
                   3608:             for (var m=0; m<loopend; m++) {
                   3609:                 msg += needaction[m]+", ";
                   3610:             }
                   3611:         }
                   3612:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   3613:         alert(msg);
                   3614:         return; 
                   3615:     }
                   3616:     setSections(form);
                   3617: }
                   3618: 
                   3619: function check_types(num,countfail,needaction) {
                   3620:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   3621:     var count = 0;
                   3622:     if (typeidx != -1) {
                   3623:         if (document.$formname.elements[typeidx].length) {
                   3624:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   3625:                 if (document.$formname.elements[typeidx][k].checked) {
                   3626:                     count ++;
                   3627:                 }
                   3628:             }
                   3629:         } else {
                   3630:             if (document.$formname.elements[typeidx].checked) {
                   3631:                 count ++;
                   3632:             }
                   3633:         }
                   3634:         if (count == 0) {
                   3635:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   3636:             if (domidx != -1) {
                   3637:                 var domname = document.$formname.elements[domidx].value;
                   3638:                 needaction[countfail] = domname;
                   3639:                 countfail ++;
                   3640:             }
                   3641:         }
                   3642:     }
                   3643:     return countfail;
                   3644: }
                   3645: 
                   3646: function getIndexByName(item) {
                   3647:     for (var i=0;i<document.$formname.elements.length;i++) {
                   3648:         if (document.$formname.elements[i].name == item) {
                   3649:             return i;
                   3650:         }
                   3651:     }
                   3652:     return -1;
                   3653: }
                   3654: ENDSCRIPT
1.256     raeburn  3655:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3656:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3657: 
1.237     raeburn  3658:     my $output = '<script type="text/javascript">'."\n".
1.249     raeburn  3659:                  $setsec_js."\n".$selfenroll_js."\n".
1.237     raeburn  3660:                  '</script>'."\n".
1.256     raeburn  3661:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
                   3662:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   3663:     if (ref($visactions) eq 'HASH') {
                   3664:         if ($visible) {
                   3665:             $output .= '<p>'.$visactions->{'vis'}.'</p>';
                   3666:         } else {
                   3667:             $output .= $visactions->{'miss'}.'<br />'.$visactions->{'yous'}.
                   3668:                        '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   3669:             if (ref($vismsgs) eq 'ARRAY') {
                   3670:                 $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   3671:                 foreach my $item (@{$vismsgs}) {
                   3672:                     $output .= '<li>'.$visactions->{$item}.'</li>';
                   3673:                 }
                   3674:                 $output .= '</ul>';
                   3675:             }
                   3676:             $output .= '</p>';
                   3677:         }
                   3678:     }
                   3679:     $output .= '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
                   3680:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  3681:     if (ref($row) eq 'ARRAY') {
                   3682:         foreach my $item (@{$row}) {
                   3683:             my $title = $item; 
                   3684:             if (ref($lt) eq 'HASH') {
                   3685:                 $title = $lt->{$item};
                   3686:             }
                   3687:             $output .= 
                   3688:                 &Apache::lonhtmlcommon::row_title($title,
                   3689:                              'LC_selfenroll_pick_box_title','LC_oddrow_value')."\n";
                   3690:             if ($item eq 'types') {
                   3691:                 my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
1.241     raeburn  3692:                 my $showdomdesc = 1;
                   3693:                 my $includeempty = 1;
                   3694:                 my $num = 0;
                   3695:                 $output .= &Apache::loncommon::start_data_table().
                   3696:                            &Apache::loncommon::start_data_table_row()
                   3697:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   3698:                            .&mt('Any user in any domain:')
                   3699:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   3700:                 if ($curr_types eq '*') {
                   3701:                     $output .= ' checked="checked" '; 
                   3702:                 }
1.249     raeburn  3703:                 $output .= 'onchange="javascript:update_types('.
                   3704:                            "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
                   3705:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  3706:                 if ($curr_types ne '*') {
                   3707:                     $output .= ' checked="checked" ';
                   3708:                 }
1.249     raeburn  3709:                 $output .= ' onchange="javascript:update_types('.
                   3710:                            "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
                   3711:                            &Apache::loncommon::end_data_table_row().
                   3712:                            &Apache::loncommon::end_data_table().
                   3713:                            &mt('Or').'<br />'.
                   3714:                            &Apache::loncommon::start_data_table();
1.241     raeburn  3715:                 my %currdoms;
1.249     raeburn  3716:                 if ($curr_types eq '') {
1.241     raeburn  3717:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   3718:                 } elsif ($curr_types ne '*') {
                   3719:                     my @entries = split(/;/,$curr_types);
                   3720:                     if (@entries > 0) {
                   3721:                         foreach my $entry (@entries) {
                   3722:                             my ($currdom,$typestr) = split(/:/,$entry);
                   3723:                             $currdoms{$currdom} = 1;
                   3724:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  3725:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  3726:                             $output .= &Apache::loncommon::start_data_table_row()
                   3727:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   3728:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   3729:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   3730:                                        .'" value="'.$currdom.'" /></span><br />'
                   3731:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.249     raeburn  3732:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
1.241     raeburn  3733:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  3734:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.241     raeburn  3735:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                   3736:                                        .&Apache::loncommon::end_data_table_row();
                   3737:                             $num ++;
                   3738:                         }
                   3739:                     }
                   3740:                 }
1.249     raeburn  3741:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  3742:                 if ($curr_types eq '*') { 
1.249     raeburn  3743:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  3744:                 } elsif ($curr_types eq '') {
1.249     raeburn  3745:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  3746:                 }
                   3747:                 $output .= &Apache::loncommon::start_data_table_row()
                   3748:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   3749:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
                   3750:                                                                 $includeempty,$showdomdesc)
                   3751:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   3752:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   3753:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  3754:             } elsif ($item eq 'registered') {
                   3755:                 my ($regon,$regoff);
                   3756:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
                   3757:                     $regon = ' checked="checked" ';
                   3758:                     $regoff = ' ';
                   3759:                 } else {
                   3760:                     $regon = ' ';
                   3761:                     $regoff = ' checked="checked" ';
                   3762:                 }
                   3763:                 $output .= '<label>'.
1.245     raeburn  3764:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.'/>'.
1.244     bisitz   3765:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.245     raeburn  3766:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.'/>'.
1.244     bisitz   3767:                            &mt('No').'</label>';
1.237     raeburn  3768:             } elsif ($item eq 'enroll_dates') {
                   3769:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
                   3770:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
                   3771:                 if ($starttime eq '') {
                   3772:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   3773:                 }
                   3774:                 if ($endtime eq '') {
                   3775:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   3776:                 }
                   3777:                 my $startform =
                   3778:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
                   3779:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   3780:                 my $endform =
                   3781:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
                   3782:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   3783:                 $output .= &selfenroll_date_forms($startform,$endform);
                   3784:             } elsif ($item eq 'access_dates') {
                   3785:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
                   3786:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
                   3787:                 if ($starttime eq '') {
                   3788:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   3789:                 }
                   3790:                 if ($endtime eq '') {
                   3791:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   3792:                 }
                   3793:                 my $startform =
                   3794:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
                   3795:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   3796:                 my $endform =
                   3797:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
                   3798:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   3799:                 $output .= &selfenroll_date_forms($startform,$endform);
                   3800:             } elsif ($item eq 'section') {
                   3801:                 my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
                   3802:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3803:                 my $newsecval;
                   3804:                 if ($currsec ne 'none' && $currsec ne '') {
                   3805:                     if (!defined($sections_count{$currsec})) {
                   3806:                         $newsecval = $currsec;
                   3807:                     }
                   3808:                 }
                   3809:                 my $sections_select = 
                   3810:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
                   3811:                 $output .= '<table class="LC_createuser">'."\n".
                   3812:                            '<tr class="LC_section_row">'."\n".
                   3813:                            '<td align="center">'.&mt('Existing sections')."\n".
                   3814:                            '<br />'.$sections_select.'</td><td align="center">'.
                   3815:                            &mt('New section').'<br />'."\n".
                   3816:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
                   3817:                            '<input type="hidden" name="sections" value="" />'."\n".
                   3818:                            '<input type="hidden" name="state" value="done" />'."\n".
                   3819:                            '</td></tr></table>'."\n";
                   3820:             }
                   3821:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   3822:         }
                   3823:     }
                   3824:     $output .= &Apache::lonhtmlcommon::end_pick_box().
1.241     raeburn  3825:                '<br /><input type="button" name="selfenrollconf" value="'
1.249     raeburn  3826:                .&mt('Save changes').'" onclick="validate_types(this.form);" />'
1.241     raeburn  3827:                .'<input type="hidden" name="action" value="selfenroll" /></form>';
1.237     raeburn  3828:     $r->print($output);
                   3829:     return;
                   3830: }
                   3831: 
1.256     raeburn  3832: sub visible_in_cat {
                   3833:     my ($cdom,$cnum) = @_;
                   3834:     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   3835:     my ($cathash,%settable,@vismsgs,$cansetvis);
                   3836:     my %visactions = &Apache::lonlocal::texthash(
                   3837:                    vis => 'Your course currently appears in the Course Catalog for this domain.',
                   3838:                    gen => 'Courses can be both self-cataloging, based on an institutional code (e.g., fs08phy231), or can be assigned categories from a hierarchy defined for the domain.',
                   3839:                    miss => 'Your course does not currently appear in the Course Catalog for this domain.',
                   3840:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding your course.',
                   3841:                    coca => 'Courses can be absent from the Catalog, because they do not have an institutional code, have no assigned category, or have been specifically excluded.',
                   3842:                    make => 'Make any changes to self-enrollment settings below, click "Save changes", then take action to include the course in the Catalog:',
                   3843:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   3844:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   3845:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   3846:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   3847:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   3848:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   3849:                    dc_chgcat => 'Ask a domain coordinator to change the category assigned to the course, as the one currently assigned is no longer used in the domain',
                   3850:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   3851:     );
1.261     raeburn  3852:     $visactions{'unhide'} = &mt('Use [_1]Set course environment[_2] to change the "Exclude from course catalog" setting.','"<a href="/adm/parmset?action=crsenv">','</a>"');
                   3853:     $visactions{'chgcat'} = &mt('Use [_1]Set course environment[_2] to change the category assigned to the course, as the one currently assigned is no longer used in the domain.','"<a href="/adm/parmset?action=crsenv">','</a>"');
                   3854:     $visactions{'addcat'} = &mt('Use [_1]Set course environment[_2] to assign a category to the course.','"<a href="/adm/parmset?action=crsenv">','</a>"');
1.256     raeburn  3855:     if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   3856:         if ($domconf{'coursecategories'}{'togglecats'} eq 'crs') {
                   3857:             $settable{'togglecats'} = 1;
                   3858:         }
                   3859:         if ($domconf{'coursecategories'}{'categorize'} eq 'crs') {
                   3860:             $settable{'categorize'} = 1;
                   3861:         }
                   3862:         $cathash = $domconf{'coursecategories'}{'cats'};
                   3863:     }
1.260     raeburn  3864:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  3865:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   3866:     } elsif ($settable{'togglecats'}) {
                   3867:         $cansetvis = &mt('You are able to choose to exclude this course from the catalog, but only a Domain Coordinator may assign a course category.'); 
1.260     raeburn  3868:     } elsif ($settable{'categorize'}) {
1.256     raeburn  3869:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   3870:     } else {
                   3871:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   3872:     }
                   3873:      
                   3874:     my %currsettings =
                   3875:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   3876:                              $cdom,$cnum);
                   3877:     my $visible = 0;
                   3878:     if ($currsettings{'internal.coursecode'} ne '') {
                   3879:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   3880:             $cathash = $domconf{'coursecategories'}{'cats'};
                   3881:             if (ref($cathash) eq 'HASH') {
                   3882:                 if ($cathash->{'instcode::0'} eq '') {
                   3883:                     push(@vismsgs,'dc_addinst'); 
                   3884:                 } else {
                   3885:                     $visible = 1;
                   3886:                 }
                   3887:             } else {
                   3888:                 $visible = 1;
                   3889:             }
                   3890:         } else {
                   3891:             $visible = 1;
                   3892:         }
                   3893:     } else {
                   3894:         if (ref($cathash) eq 'HASH') {
                   3895:             if ($cathash->{'instcode::0'} ne '') {
                   3896:                 push(@vismsgs,'dc_instcode');
                   3897:             }
                   3898:         } else {
                   3899:             push(@vismsgs,'dc_instcode');
                   3900:         }
                   3901:     }
                   3902:     if ($currsettings{'categories'} ne '') {
                   3903:         my $cathash;
                   3904:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   3905:             $cathash = $domconf{'coursecategories'}{'cats'};
                   3906:             if (ref($cathash) eq 'HASH') {
                   3907:                 if (keys(%{$cathash}) == 0) {
                   3908:                     push(@vismsgs,'dc_catalog');
                   3909:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   3910:                     push(@vismsgs,'dc_categories');
                   3911:                 } else {
                   3912:                     my @currcategories = split('&',$currsettings{'categories'});
                   3913:                     my $matched = 0;
                   3914:                     foreach my $cat (@currcategories) {
                   3915:                         if ($cathash->{$cat} ne '') {
                   3916:                             $visible = 1;
                   3917:                             $matched = 1;
                   3918:                             last;
                   3919:                         }
                   3920:                     }
                   3921:                     if (!$matched) {
1.260     raeburn  3922:                         if ($settable{'categorize'}) { 
1.256     raeburn  3923:                             push(@vismsgs,'chgcat');
                   3924:                         } else {
                   3925:                             push(@vismsgs,'dc_chgcat');
                   3926:                         }
                   3927:                     }
                   3928:                 }
                   3929:             }
                   3930:         }
                   3931:     } else {
                   3932:         if (ref($cathash) eq 'HASH') {
                   3933:             if ((keys(%{$cathash}) > 1) || 
                   3934:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  3935:                 if ($settable{'categorize'}) {
1.256     raeburn  3936:                     push(@vismsgs,'addcat');
                   3937:                 } else {
                   3938:                     push(@vismsgs,'dc_addcat');
                   3939:                 }
                   3940:             }
                   3941:         }
                   3942:     }
                   3943:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   3944:         $visible = 0;
                   3945:         if ($settable{'togglecats'}) {
                   3946:             unshift(@vismsgs,'unhide');
                   3947:         } else {
                   3948:             unshift(@vismsgs,'dc_unhide')
                   3949:         }
                   3950:     }
                   3951:     return ($visible,$cansetvis,\@vismsgs,\%visactions);
                   3952: }
                   3953: 
1.241     raeburn  3954: sub new_selfenroll_dom_row {
                   3955:     my ($newdom,$num) = @_;
                   3956:     my $domdesc = &Apache::lonnet::domain($newdom);
                   3957:     my $output;
                   3958:     if ($domdesc ne '') {
                   3959:         $output .= &Apache::loncommon::start_data_table_row()
                   3960:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   3961:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  3962:                    .'" value="'.$newdom.'" /></span><br />'
                   3963:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   3964:                    .'name="selfenroll_activate" value="'.$num.'" '
                   3965:                    .'onchange="javascript:update_types('
                   3966:                    ."'selfenroll_activate','$num'".');" />'
                   3967:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  3968:         my @currinsttypes;
                   3969:         $output .= '<td>'.&mt('User types:').'<br />'
                   3970:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   3971:                    .&Apache::loncommon::end_data_table_row();
                   3972:     }
                   3973:     return $output;
                   3974: }
                   3975: 
                   3976: sub selfenroll_inst_types {
                   3977:     my ($num,$currdom,$currinsttypes) = @_;
                   3978:     my $output;
                   3979:     my $numinrow = 4;
                   3980:     my $count = 0;
                   3981:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  3982:     my $othervalue = 'any';
1.241     raeburn  3983:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  3984:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  3985:             $othervalue = 'other';
                   3986:         }
1.241     raeburn  3987:         $output .= '<table><tr>';
                   3988:         foreach my $type (@{$types}) {
                   3989:             if (($count > 0) && ($count%$numinrow == 0)) {
                   3990:                 $output .= '</tr><tr>';
                   3991:             }
                   3992:             if (defined($usertypes->{$type})) {
1.257     raeburn  3993:                 my $esc_type = &escape($type);
1.241     raeburn  3994:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  3995:                            $esc_type.'" ';
1.241     raeburn  3996:                 if (ref($currinsttypes) eq 'ARRAY') {
                   3997:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  3998:                         if (grep(/^any$/,@{$currinsttypes})) {
                   3999:                             $output .= 'checked="checked"';
1.257     raeburn  4000:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  4001:                             $output .= 'checked="checked"';
                   4002:                         }
1.249     raeburn  4003:                     } else {
                   4004:                         $output .= 'checked="checked"';
1.241     raeburn  4005:                     }
                   4006:                 }
                   4007:                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
                   4008:             }
                   4009:             $count ++;
                   4010:         }
                   4011:         if (($count > 0) && ($count%$numinrow == 0)) {
                   4012:             $output .= '</tr><tr>';
                   4013:         }
1.249     raeburn  4014:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  4015:         if (ref($currinsttypes) eq 'ARRAY') {
                   4016:             if (@{$currinsttypes} > 0) {
1.249     raeburn  4017:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   4018:                     $output .= ' checked="checked"';
                   4019:                 } elsif ($othervalue eq 'other') {
                   4020:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   4021:                         $output .= ' checked="checked"';
                   4022:                     }
1.241     raeburn  4023:                 }
1.249     raeburn  4024:             } else {
                   4025:                 $output .= ' checked="checked"';
1.241     raeburn  4026:             }
1.249     raeburn  4027:         } else {
                   4028:             $output .= ' checked="checked"';
1.241     raeburn  4029:         }
                   4030:         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
                   4031:     }
                   4032:     return $output;
                   4033: }
                   4034: 
1.237     raeburn  4035: sub selfenroll_date_forms {
                   4036:     my ($startform,$endform) = @_;
                   4037:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   4038:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  4039:                                                     'LC_oddrow_value')."\n".
                   4040:                   $startform."\n".
                   4041:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   4042:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  4043:                                                    'LC_oddrow_value')."\n".
                   4044:                   $endform."\n".
                   4045:                   &Apache::lonhtmlcommon::row_closure(1).
                   4046:                   &Apache::lonhtmlcommon::end_pick_box();
                   4047:     return $output;
                   4048: }
                   4049: 
1.239     raeburn  4050: sub print_userchangelogs_display {
                   4051:     my ($r,$context,$permission) = @_;
                   4052:     my $formname = 'roleslog';
                   4053:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4054:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4055:     my %roleslog=&Apache::lonnet::dump('nohist_rolelog',$cdom,$cnum);
                   4056:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   4057: 
                   4058:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">');
                   4059:     my %saveable_parameters = ('show' => 'scalar',);
                   4060:     &Apache::loncommon::store_course_settings('roles_log',
                   4061:                                               \%saveable_parameters);
                   4062:     &Apache::loncommon::restore_course_settings('roles_log',
                   4063:                                                 \%saveable_parameters);
                   4064:     # set defaults
                   4065:     my $now = time();
                   4066:     my $defstart = $now - (7*24*3600); #7 days ago 
                   4067:     my %defaults = (
                   4068:                      page               => '1',
                   4069:                      show               => '10',
                   4070:                      role               => 'any',
                   4071:                      chgcontext         => 'any',
                   4072:                      rolelog_start_date => $defstart,
                   4073:                      rolelog_end_date   => $now,
                   4074:                    );
                   4075:     my $more_records = 0;
                   4076: 
                   4077:     # set current
                   4078:     my %curr;
                   4079:     foreach my $item ('show','page','role','chgcontext') {
                   4080:         $curr{$item} = $env{'form.'.$item};
                   4081:     }
                   4082:     my ($startdate,$enddate) = 
                   4083:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   4084:     $curr{'rolelog_start_date'} = $startdate;
                   4085:     $curr{'rolelog_end_date'} = $enddate;
                   4086:     foreach my $key (keys(%defaults)) {
                   4087:         if ($curr{$key} eq '') {
                   4088:             $curr{$key} = $defaults{$key};
                   4089:         }
                   4090:     }
1.248     raeburn  4091:     my (%whodunit,%changed,$version);
                   4092:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
                   4093:     $r->print(&role_display_filter($formname,$cdom,$cnum,\%curr,$version));
1.239     raeburn  4094:     my $showntablehdr = 0;
                   4095:     my $tablehdr = &Apache::loncommon::start_data_table().
                   4096:                    &Apache::loncommon::start_data_table_header_row().
                   4097:                    '<th>&nbsp;</th><th>'.&mt('When').'</th><th>'.&mt('Who made the change').
                   4098:                    '</th><th>'.&mt('Changed User').'</th><th>'.&mt('Role').'</th><th>'.&mt('Section').'</th><th>'.
                   4099:                    &mt('Context').'</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   4100:                    &Apache::loncommon::end_data_table_header_row();
                   4101:     my ($minshown,$maxshown);
1.255     raeburn  4102:     $minshown = 1;
1.239     raeburn  4103:     my $count = 0;
                   4104:     if ($curr{'show'} ne &mt('all')) { 
                   4105:         $maxshown = $curr{'page'} * $curr{'show'};
                   4106:         if ($curr{'page'} > 1) {
                   4107:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   4108:         }
                   4109:     }
                   4110:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   4111:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   4112:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
                   4113:         if ($curr{'show'} ne &mt('all')) {
                   4114:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   4115:                 $more_records = 1;
                   4116:                 last;
                   4117:             }
                   4118:         }
                   4119:         if ($curr{'role'} ne 'any') {
                   4120:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   4121:         }
                   4122:         if ($curr{'chgcontext'} ne 'any') {
                   4123:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   4124:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   4125:             } else {
                   4126:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   4127:             }
                   4128:         }
                   4129:         $count ++;
                   4130:         next if ($count < $minshown);
                   4131:         if (!$showntablehdr) {
                   4132:             $r->print($tablehdr);
                   4133:             $showntablehdr = 1;
                   4134:         }
                   4135:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   4136:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   4137:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   4138:         }
                   4139:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   4140:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   4141:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   4142:         }
                   4143:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   4144:         if ($sec eq '') {
                   4145:             $sec = &mt('None');
                   4146:         }
                   4147:         my ($rolestart,$roleend);
                   4148:         if ($roleslog{$id}{'delflag'}) {
                   4149:             $rolestart = &mt('deleted');
                   4150:             $roleend = &mt('deleted');
                   4151:         } else {
                   4152:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   4153:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   4154:             if ($rolestart eq '' || $rolestart == 0) {
                   4155:                 $rolestart = &mt('No start date'); 
                   4156:             } else {
                   4157:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   4158:             }
                   4159:             if ($roleend eq '' || $roleend == 0) { 
                   4160:                 $roleend = &mt('No end date');
                   4161:             } else {
                   4162:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   4163:             }
                   4164:         }
                   4165:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   4166:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   4167:             $chgcontext = 'selfenroll';
                   4168:         }
                   4169:         my %lt = &rolechg_contexts();
                   4170:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   4171:             $chgcontext = $lt{$chgcontext};
                   4172:         }
                   4173:         $r->print(&Apache::loncommon::start_data_table_row().'<td>'.$count.'</td><td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td><td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td><td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td><td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'}).'</td><td>'.$sec.'</td><td>'.$chgcontext.'</td><td>'.$rolestart.'</td><td>'.$roleend.'</td>'.&Apache::loncommon::end_data_table_row()."\n");
                   4174:     }
                   4175:     if ($showntablehdr) {
                   4176:         $r->print(&Apache::loncommon::end_data_table().'<br />');
                   4177:         if (($curr{'page'} > 1) || ($more_records)) {
                   4178:             $r->print('<table><tr>');
                   4179:             if ($curr{'page'} > 1) {
                   4180:                 $r->print('<td><a href="javascript:chgPage('."'previous'".');">'.&mt('Previous [_1] changes',$curr{'show'}).'</a></td>');
                   4181:             }
                   4182:             if ($more_records) {
                   4183:                 $r->print('<td><a href="javascript:chgPage('."'next'".');">'.&mt('Next [_1] changes',$curr{'show'}).'</a></td>');
                   4184:             }
                   4185:             $r->print('</tr></table>');
                   4186:             $r->print(<<"ENDSCRIPT");
                   4187: <script type="text/javascript">
                   4188: function chgPage(caller) {
                   4189:     if (caller == 'previous') {
                   4190:         document.$formname.page.value --;
                   4191:     }
                   4192:     if (caller == 'next') {
                   4193:         document.$formname.page.value ++;
                   4194:     }
                   4195:     document.$formname.submit(); 
                   4196:     return;
                   4197: }
                   4198: </script>
                   4199: ENDSCRIPT
                   4200:         }
                   4201:     } else {
                   4202:         $r->print(&mt('There are no records to display'));
                   4203:     }
                   4204:     $r->print('<input type="hidden" name="page" value="'.$curr{'page'}.'" />'.
                   4205:               '<input type="hidden" name="action" value="changelogs" /></form>');
                   4206:     return;
                   4207: }
                   4208: 
                   4209: sub role_display_filter {
1.248     raeburn  4210:     my ($formname,$cdom,$cnum,$curr,$version) = @_;
1.239     raeburn  4211:     my $context = 'course';
                   4212:     my $nolink = 1;
                   4213:     my $output = '<table><tr><td valign="top">'.
                   4214:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b><br />'.
                   4215:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   4216:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   4217:                  '</td><td>&nbsp;&nbsp;</td>';
                   4218:     my $startform =
                   4219:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   4220:                                             $curr->{'rolelog_start_date'},undef,
                   4221:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   4222:     my $endform =
                   4223:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   4224:                                             $curr->{'rolelog_end_date'},undef,
                   4225:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   4226:     my %lt = &rolechg_contexts();
                   4227:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br /><table><tr><td>'.&mt('After:').
                   4228:                '</td><td>'.$startform.'</td></tr><tr><td>'.&mt('Before:').'</td><td>'.
                   4229:                $endform.'</td></tr></table></td><td>&nbsp;&nbsp;</td>'.
                   4230:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   4231:                '<select name="role"><option value="any"';
                   4232:     if ($curr->{'role'} eq 'any') {
                   4233:         $output .= ' selected="selected"';
                   4234:     }
                   4235:     $output .=  '>'.&mt('Any').'</option>'."\n";
                   4236:     my @roles = &Apache::lonuserutils::course_roles($context,undef,1);
                   4237:     foreach my $role (@roles) {
                   4238:         my $plrole;
                   4239:         if ($role eq 'cr') {
                   4240:             $plrole = &mt('Custom Role');
                   4241:         } else {
                   4242:             $plrole=&Apache::lonnet::plaintext($role);
                   4243:         }
                   4244:         my $selstr = '';
                   4245:         if ($role eq $curr->{'role'}) {
                   4246:             $selstr = ' selected="selected"';
                   4247:         }
                   4248:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   4249:     }
                   4250:     $output .= '</select></td><td>&nbsp;&nbsp;</td><td valign="top"><b>'.
                   4251:                &mt('Context:').'</b><br /><select name="chgcontext">';
                   4252:     foreach my $chgtype ('any','auto','updatenow','createcourse','course','domain','selfenroll') {
                   4253:         my $selstr = '';
                   4254:         if ($curr->{'chgcontext'} eq $chgtype) {
                   4255:             $output .= $selstr = ' selected="selected"';
                   4256:         }
                   4257:         if (($chgtype eq 'auto') || ($chgtype eq 'updatenow')) {
                   4258:             next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   4259:         }
                   4260:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  4261:     }
1.239     raeburn  4262:     $output .= '</select></td><td>&nbsp;&nbsp;</td><td valign="middle"><input type="submit" value="'.
1.248     raeburn  4263:                &mt('Update Display').'" /></tr></table>'.
                   4264:                '<span class="LC_roleslog_note">'.
                   4265:                &mt('[_1]Note:[_2] Only changes made from servers running LON-CAPA 2.6.99.0 or later are displayed.');
                   4266:     if ($version) {
                   4267:         $output .= ' '.&mt('This server is version [_3].','<b>','</b>',$version);    }
                   4268:     $output .= '</span><hr noshade><br />';
1.239     raeburn  4269:     return $output;
                   4270: }
                   4271: 
                   4272: sub rolechg_contexts {
                   4273:     my %lt = &Apache::lonlocal::texthash (
                   4274:                                              any          => 'Any',
                   4275:                                              auto         => 'Automated enrollment',
                   4276:                                              updatenow    => 'Roster Update',
                   4277:                                              createcourse => 'Course Creation',
                   4278:                                              course       => 'User Management in course',
                   4279:                                              domain       => 'User Management in domain',
                   4280:                                              selfenroll   => 'Self-enrolled', 
                   4281:                                          );
                   4282:     return %lt;
                   4283: }
                   4284: 
1.27      matthew  4285: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  4286: sub user_search_result {
1.221     raeburn  4287:     my ($context,$srch) = @_;
1.160     raeburn  4288:     my %allhomes;
                   4289:     my %inst_matches;
                   4290:     my %srch_results;
1.181     raeburn  4291:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  4292:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  4293:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  4294:         $response = &mt('Invalid search.');
                   4295:     }
                   4296:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   4297:         $response = &mt('Invalid search.');
                   4298:     }
1.177     raeburn  4299:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  4300:         $response = &mt('Invalid search.');
                   4301:     }
                   4302:     if ($srch->{'srchterm'} eq '') {
                   4303:         $response = &mt('You must enter a search term.');
                   4304:     }
1.183     raeburn  4305:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   4306:         $response = &mt('Your search term must contain more than just spaces.');
                   4307:     }
1.160     raeburn  4308:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   4309:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 4310: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.160     raeburn  4311:             $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.')
                   4312:         }
                   4313:     }
                   4314:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   4315:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  4316:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  4317:             my $unamecheck = $srch->{'srchterm'};
                   4318:             if ($srch->{'srchtype'} eq 'contains') {
                   4319:                 if ($unamecheck !~ /^\w/) {
                   4320:                     $unamecheck = 'a'.$unamecheck; 
                   4321:                 }
                   4322:             }
                   4323:             if ($unamecheck !~ /^$match_username$/) {
1.176     raeburn  4324:                 $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
                   4325:             }
1.160     raeburn  4326:         }
                   4327:     }
1.180     raeburn  4328:     if ($response ne '') {
                   4329:         $response = '<span class="LC_warning">'.$response.'</span>';
                   4330:     }
1.160     raeburn  4331:     if ($srch->{'srchin'} eq 'instd') {
                   4332:         my $instd_chk = &directorysrch_check($srch);
                   4333:         if ($instd_chk ne 'ok') {
1.180     raeburn  4334:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
                   4335:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
1.160     raeburn  4336:         }
                   4337:     }
                   4338:     if ($response ne '') {
1.180     raeburn  4339:         return ($currstate,$response);
1.160     raeburn  4340:     }
                   4341:     if ($srch->{'srchby'} eq 'uname') {
                   4342:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   4343:             if ($env{'form.forcenew'}) {
                   4344:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   4345:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   4346:                     if ($uhome eq 'no_host') {
                   4347:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  4348:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   4349:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  4350:                     } else {
1.179     raeburn  4351:                         $currstate = 'modify';
1.160     raeburn  4352:                     }
                   4353:                 } else {
1.179     raeburn  4354:                     $currstate = 'modify';
1.160     raeburn  4355:                 }
                   4356:             } else {
                   4357:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  4358:                     if ($srch->{'srchtype'} eq 'exact') {
                   4359:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   4360:                         if ($uhome eq 'no_host') {
1.179     raeburn  4361:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  4362:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  4363:                         } else {
1.179     raeburn  4364:                             $currstate = 'modify';
1.162     raeburn  4365:                         }
                   4366:                     } else {
                   4367:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  4368:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  4369:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  4370:                     }
                   4371:                 } else {
1.167     albertel 4372:                     my $courseusers = &get_courseusers();
1.162     raeburn  4373:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 4374:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  4375:                             $currstate = 'modify';
1.162     raeburn  4376:                         } else {
1.179     raeburn  4377:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  4378:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  4379:                         }
1.160     raeburn  4380:                     } else {
1.167     albertel 4381:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  4382:                             my ($cuname,$cudomain) = split(/:/,$user);
                   4383:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  4384:                                 my $matched = 0;
                   4385:                                 if ($srch->{'srchtype'} eq 'begins') {
                   4386:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   4387:                                         $matched = 1;
                   4388:                                     }
                   4389:                                 } else {
                   4390:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   4391:                                         $matched = 1;
                   4392:                                     }
                   4393:                                 }
                   4394:                                 if ($matched) {
1.167     albertel 4395:                                     $srch_results{$user} = 
                   4396: 					{&Apache::lonnet::get('environment',
                   4397: 							     ['firstname',
                   4398: 							      'lastname',
1.194     albertel 4399: 							      'permanentemail'],
                   4400: 							      $cudomain,$cuname)};
1.162     raeburn  4401:                                 }
                   4402:                             }
                   4403:                         }
1.179     raeburn  4404:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  4405:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  4406:                     }
                   4407:                 }
                   4408:             }
                   4409:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  4410:             $currstate = 'query';
1.160     raeburn  4411:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  4412:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   4413:             if ($dirsrchres eq 'ok') {
                   4414:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  4415:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  4416:             } else {
                   4417:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   4418:                 $response = '<span class="LC_warning">'.
                   4419:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   4420:                     '</span><br />'.
                   4421:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   4422:                     '<br /><br />'; 
                   4423:             }
1.160     raeburn  4424:         }
                   4425:     } else {
                   4426:         if ($srch->{'srchin'} eq 'dom') {
                   4427:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  4428:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  4429:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  4430:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 4431:             my $courseusers = &get_courseusers(); 
                   4432:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  4433:                 my ($uname,$udom) = split(/:/,$user);
                   4434:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   4435:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   4436:                 if ($srch->{'srchby'} eq 'lastname') {
                   4437:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   4438:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  4439:                         (($srch->{'srchtype'} eq 'begins') &&
                   4440:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  4441:                         (($srch->{'srchtype'} eq 'contains') &&
                   4442:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   4443:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   4444:                                             lastname => $names{'lastname'},
                   4445:                                             permanentemail => $emails{'permanentemail'},
                   4446:                                            };
                   4447:                     }
                   4448:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   4449:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  4450:                     $srchlast =~ s/\s+$//;
                   4451:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  4452:                     if ($srch->{'srchtype'} eq 'exact') {
                   4453:                         if (($names{'lastname'} eq $srchlast) &&
                   4454:                             ($names{'firstname'} eq $srchfirst)) {
                   4455:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   4456:                                                 lastname => $names{'lastname'},
                   4457:                                                 permanentemail => $emails{'permanentemail'},
                   4458: 
                   4459:                                            };
                   4460:                         }
1.177     raeburn  4461:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   4462:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   4463:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   4464:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   4465:                                                 lastname => $names{'lastname'},
                   4466:                                                 permanentemail => $emails{'permanentemail'},
                   4467:                                                };
                   4468:                         }
                   4469:                     } else {
1.160     raeburn  4470:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   4471:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   4472:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   4473:                                                 lastname => $names{'lastname'},
                   4474:                                                 permanentemail => $emails{'permanentemail'},
                   4475:                                                };
                   4476:                         }
                   4477:                     }
                   4478:                 }
                   4479:             }
1.179     raeburn  4480:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  4481:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  4482:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  4483:             $currstate = 'query';
1.160     raeburn  4484:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  4485:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   4486:             if ($dirsrchres eq 'ok') {
                   4487:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  4488:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  4489:             } else {
                   4490:                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
                   4491:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   4492:                     '</span><br />'.
                   4493:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   4494:                     '<br /><br />';
                   4495:             }
1.160     raeburn  4496:         }
                   4497:     }
1.179     raeburn  4498:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  4499: }
                   4500: 
                   4501: sub directorysrch_check {
                   4502:     my ($srch) = @_;
                   4503:     my $can_search = 0;
                   4504:     my $response;
                   4505:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   4506:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  4507:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  4508:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   4509:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  4510:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  4511:         }
                   4512:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   4513:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  4514:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  4515:             }
                   4516:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   4517:             if (!@usertypes) {
                   4518:                 push(@usertypes,'default');
                   4519:             }
                   4520:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   4521:                 foreach my $type (@usertypes) {
                   4522:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   4523:                         $can_search = 1;
                   4524:                         last;
                   4525:                     }
                   4526:                 }
                   4527:             }
                   4528:             if (!$can_search) {
                   4529:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   4530:                 my @longtypes; 
                   4531:                 foreach my $item (@usertypes) {
1.229     raeburn  4532:                     if (defined($insttypes->{$item})) { 
                   4533:                         push (@longtypes,$insttypes->{$item});
                   4534:                     } elsif ($item eq 'default') {
                   4535:                         push (@longtypes,&mt('other')); 
                   4536:                     }
1.160     raeburn  4537:                 }
                   4538:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  4539:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  4540:             }
1.160     raeburn  4541:         } else {
                   4542:             $can_search = 1;
                   4543:         }
                   4544:     } else {
1.180     raeburn  4545:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  4546:     }
                   4547:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 4548:                        uname     => 'username',
1.160     raeburn  4549:                        lastfirst => 'last name, first name',
1.167     albertel 4550:                        lastname  => 'last name',
1.172     raeburn  4551:                        contains  => 'contains',
1.178     raeburn  4552:                        exact     => 'as exact match to',
                   4553:                        begins    => 'begins with',
1.160     raeburn  4554:                    );
                   4555:     if ($can_search) {
                   4556:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   4557:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  4558:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  4559:             }
                   4560:         } else {
1.180     raeburn  4561:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  4562:         }
                   4563:     }
                   4564:     if ($can_search) {
1.178     raeburn  4565:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   4566:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   4567:                 return 'ok';
                   4568:             } else {
1.180     raeburn  4569:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  4570:             }
                   4571:         } else {
                   4572:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   4573:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   4574:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   4575:                 return 'ok';
                   4576:             } else {
1.180     raeburn  4577:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  4578:             }
1.160     raeburn  4579:         }
                   4580:     }
                   4581: }
                   4582: 
                   4583: sub get_courseusers {
                   4584:     my %advhash;
1.167     albertel 4585:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  4586:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   4587:     foreach my $role (sort(keys(%coursepersonnel))) {
                   4588:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 4589: 	    if (!exists($classlist->{$user})) {
                   4590: 		$classlist->{$user} = [];
                   4591: 	    }
1.160     raeburn  4592:         }
                   4593:     }
1.167     albertel 4594:     return $classlist;
1.160     raeburn  4595: }
                   4596: 
                   4597: sub build_search_response {
1.221     raeburn  4598:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  4599:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  4600:     my %names = (
                   4601:           'uname' => 'username',
                   4602:           'lastname' => 'last name',
                   4603:           'lastfirst' => 'last name, first name',
                   4604:           'crs' => 'this course',
1.180     raeburn  4605:           'dom' => 'LON-CAPA domain: ',
                   4606:           'instd' => 'the institutional directory for domain: ',
1.160     raeburn  4607:     );
                   4608: 
                   4609:     my %single = (
1.180     raeburn  4610:                    begins   => 'A match',
1.160     raeburn  4611:                    contains => 'A match',
1.180     raeburn  4612:                    exact    => 'An exact match',
1.160     raeburn  4613:                  );
                   4614:     my %nomatch = (
1.180     raeburn  4615:                    begins   => 'No match',
1.160     raeburn  4616:                    contains => 'No match',
1.180     raeburn  4617:                    exact    => 'No exact match',
1.160     raeburn  4618:                   );
                   4619:     if (keys(%srch_results) > 1) {
1.179     raeburn  4620:         $currstate = 'select';
1.160     raeburn  4621:     } else {
                   4622:         if (keys(%srch_results) == 1) {
1.179     raeburn  4623:             $currstate = 'modify';
1.180     raeburn  4624:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   4625:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
                   4626:                 $response .= &display_domain_info($srch->{'srchdomain'});
                   4627:             }
1.160     raeburn  4628:         } else {
1.180     raeburn  4629:             $response = '<span class="LC_warning">'.&mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}",$srch->{'srchterm'});
                   4630:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
                   4631:                 $response .= &display_domain_info($srch->{'srchdomain'});
                   4632:             }
                   4633:             $response .= '</span>';
1.160     raeburn  4634:             if ($srch->{'srchin'} ne 'alc') {
                   4635:                 $forcenewuser = 1;
                   4636:                 my $cansrchinst = 0; 
                   4637:                 if ($srch->{'srchdomain'}) {
                   4638:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   4639:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   4640:                         if ($domconfig{'directorysrch'}{'available'}) {
                   4641:                             $cansrchinst = 1;
                   4642:                         } 
                   4643:                     }
                   4644:                 }
1.180     raeburn  4645:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   4646:                      ($srch->{'srchby'} eq 'lastname')) &&
                   4647:                     ($srch->{'srchin'} eq 'dom')) {
                   4648:                     if ($cansrchinst) {
                   4649:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  4650:                     }
                   4651:                 }
1.180     raeburn  4652:                 if ($srch->{'srchin'} eq 'crs') {
                   4653:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   4654:                 }
                   4655:             }
                   4656:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $env{'request.role.domain'})) {
1.221     raeburn  4657:                 my $cancreate =
                   4658:                     &Apache::lonuserutils::can_create_user($env{'request.role.domain'},$context);
                   4659:                 if ($cancreate) {
                   4660:                     my $showdom = &display_domain_info($env{'request.role.domain'}); 
1.266     bisitz   4661:                     $response .= '<br /><br />'
                   4662:                                 .'<b>'.&mt('To add a new user:').'</b>'
                   4663:                                 .'<br />'
                   4664:                                 .&mt("(You can only create new users in your current role's domain - [_1])"
                   4665:                                     ,'<span class="LC_cusr_emph">'.$env{'request.role.domain'}.'</span>')
                   4666:                                 .'<ul><li>'
                   4667:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   4668:                                 .'</li><li>'
                   4669:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   4670:                                 .'</li><li>'
                   4671:                                 .&mt('Provide the proposed username')
                   4672:                                 .'</li><li>'
                   4673:                                 .&mt("Click 'Search'")
                   4674:                                 .'</li></ul><br />';
1.221     raeburn  4675:                 } else {
                   4676:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.266     bisitz   4677:                     $response .= '<br /><br />'
                   4678:                                 .&mt("You are not authorized to create new users in your current role's domain - [_1]."
                   4679:                                     ,'<span class="LC_cusr_emph">'.$env{'request.role.domain'}.'</span>')
                   4680:                                 .'<br />'
                   4681:                                 .&mt('Contact the [_1]helpdesk[_2] if you need to create a new user.'
                   4682:                                     ,' <a'.$helplink.'>'
                   4683:                                     ,'</a>')
                   4684:                                 .'<br /><br />';
1.221     raeburn  4685:                 }
1.160     raeburn  4686:             }
                   4687:         }
                   4688:     }
1.179     raeburn  4689:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  4690: }
                   4691: 
1.180     raeburn  4692: sub display_domain_info {
                   4693:     my ($dom) = @_;
                   4694:     my $output = $dom;
                   4695:     if ($dom ne '') { 
                   4696:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   4697:         if ($domdesc ne '') {
                   4698:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   4699:         }
                   4700:     }
                   4701:     return $output;
                   4702: }
                   4703: 
1.160     raeburn  4704: sub crumb_utilities {
                   4705:     my %elements = (
                   4706:        crtuser => {
                   4707:            srchterm => 'text',
1.172     raeburn  4708:            srchin => 'selectbox',
1.160     raeburn  4709:            srchby => 'selectbox',
                   4710:            srchtype => 'selectbox',
                   4711:            srchdomain => 'selectbox',
                   4712:        },
1.207     raeburn  4713:        crtusername => {
                   4714:            srchterm => 'text',
                   4715:            srchdomain => 'selectbox',
                   4716:        },
1.160     raeburn  4717:        docustom => {
                   4718:            rolename => 'selectbox',
                   4719:            newrolename => 'textbox',
                   4720:        },
1.179     raeburn  4721:        studentform => {
                   4722:            srchterm => 'text',
                   4723:            srchin => 'selectbox',
                   4724:            srchby => 'selectbox',
                   4725:            srchtype => 'selectbox',
                   4726:            srchdomain => 'selectbox',
                   4727:        },
1.160     raeburn  4728:     );
                   4729: 
                   4730:     my $jsback .= qq|
                   4731: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  4732:     if (typeof prevphase == 'undefined') {
                   4733:         formname.phase.value = '';
                   4734:     }
                   4735:     else {  
                   4736:         formname.phase.value = prevphase;
                   4737:     }
                   4738:     if (typeof prevstate == 'undefined') {
                   4739:         formname.currstate.value = '';
                   4740:     }
                   4741:     else {
                   4742:         formname.currstate.value = prevstate;
                   4743:     }
1.160     raeburn  4744:     formname.submit();
                   4745: }
                   4746: |;
                   4747:     return ($jsback,\%elements);
                   4748: }
                   4749: 
1.26      matthew  4750: sub course_level_table {
1.89      raeburn  4751:     my (%inccourses) = @_;
1.26      matthew  4752:     my $table = '';
1.62      www      4753: # Custom Roles?
                   4754: 
1.190     raeburn  4755:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  4756:     my %lt=&Apache::lonlocal::texthash(
                   4757:             'exs'  => "Existing sections",
                   4758:             'new'  => "Define new section",
                   4759:             'ssd'  => "Set Start Date",
                   4760:             'sed'  => "Set End Date",
1.131     raeburn  4761:             'crl'  => "Course Level",
1.89      raeburn  4762:             'act'  => "Activate",
                   4763:             'rol'  => "Role",
                   4764:             'ext'  => "Extent",
1.113     raeburn  4765:             'grs'  => "Section",
1.89      raeburn  4766:             'sta'  => "Start",
                   4767:             'end'  => "End"
                   4768:     );
1.62      www      4769: 
1.135     raeburn  4770:     foreach my $protectedcourse (sort( keys(%inccourses))) {
                   4771: 	my $thiscourse=$protectedcourse;
1.26      matthew  4772: 	$thiscourse=~s:_:/:g;
                   4773: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
                   4774: 	my $area=$coursedata{'description'};
1.119     raeburn  4775:         my $type=$coursedata{'type'};
1.135     raeburn  4776: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  4777: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 4778:         my %sections_count;
1.101     albertel 4779:         if (defined($env{'request.course.id'})) {
                   4780:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 4781:                 %sections_count = 
                   4782: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  4783:             }
                   4784:         }
1.213     raeburn  4785:         my @roles = &Apache::lonuserutils::roles_by_context('course');
                   4786: 	foreach my $role (@roles) {
1.221     raeburn  4787:             my $plrole=&Apache::lonnet::plaintext($role);
1.135     raeburn  4788: 	    if (&Apache::lonnet::allowed('c'.$role,$thiscourse)) {
1.221     raeburn  4789:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   4790:                                             $plrole,\%sections_count,\%lt);    
                   4791:             } elsif ($env{'request.course.sec'} ne '') {
                   4792:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   4793:                                              $env{'request.course.sec'})) {
                   4794:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   4795:                                                 $plrole,\%sections_count,\%lt);
1.26      matthew  4796:                 }
                   4797:             }
                   4798:         }
1.221     raeburn  4799:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
                   4800:             foreach my $cust (sort keys %customroles) {
                   4801:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   4802:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   4803:                                             $cust,\%sections_count,\%lt);
                   4804:             }
1.62      www      4805: 	}
1.26      matthew  4806:     }
                   4807:     return '' if ($table eq ''); # return nothing if there is nothing 
                   4808:                                  # in the table
1.188     raeburn  4809:     my $result;
                   4810:     if (!$env{'request.course.id'}) {
                   4811:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   4812:     }
                   4813:     $result .= 
1.136     raeburn  4814: &Apache::loncommon::start_data_table().
                   4815: &Apache::loncommon::start_data_table_header_row().
                   4816: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>
                   4817: <th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   4818: &Apache::loncommon::end_data_table_header_row().
                   4819: $table.
                   4820: &Apache::loncommon::end_data_table();
1.26      matthew  4821:     return $result;
                   4822: }
1.88      raeburn  4823: 
1.221     raeburn  4824: sub course_level_row {
                   4825:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,$lt) = @_;
1.222     raeburn  4826:     my $row = &Apache::loncommon::start_data_table_row().
                   4827:               ' <td><input type="checkbox" name="act_'.
                   4828:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   4829:               ' <td>'.$plrole.'</td>'."\n".
                   4830:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.221     raeburn  4831:     if ($role eq 'cc') {
1.222     raeburn  4832:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  4833:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  4834:         $row .= ' <td><input type="hidden" value="'.
                   4835:                 $env{'request.course.sec'}.'" '.
                   4836:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   4837:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  4838:     } else {
                   4839:         if (ref($sections_count) eq 'HASH') {
                   4840:             my $currsec = 
                   4841:                 &Apache::lonuserutils::course_sections($sections_count,
                   4842:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  4843:             $row .= '<td><table class="LC_createuser">'."\n".
                   4844:                     '<tr class="LC_section_row">'."\n".
                   4845:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   4846:                        $currsec.'</td>'."\n".
                   4847:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   4848:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  4849:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   4850:                      '" value="" />'.
                   4851:                      '<input type="hidden" '.
                   4852:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  4853:                      '</tr></table></td>'."\n";
1.221     raeburn  4854:         } else {
1.222     raeburn  4855:             $row .= '<td><input type="text" size="10" '.
                   4856:                       'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  4857:         }
                   4858:     }
1.222     raeburn  4859:     $row .= <<ENDTIMEENTRY;
                   4860: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  4861: <a href=
                   4862: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'ssd'}</a></td>
1.222     raeburn  4863: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  4864: <a href=
                   4865: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   4866: ENDTIMEENTRY
1.222     raeburn  4867:     $row .= &Apache::loncommon::end_data_table_row();
                   4868:     return $row;
1.221     raeburn  4869: }
                   4870: 
1.88      raeburn  4871: sub course_level_dc {
                   4872:     my ($dcdom) = @_;
1.190     raeburn  4873:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  4874:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  4875:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   4876:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  4877:                       '<input type="hidden" name="dccourse" value="" />';
1.88      raeburn  4878:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1.131     raeburn  4879:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course').'</b>';
                   4880:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu');
1.88      raeburn  4881:     my %lt=&Apache::lonlocal::texthash(
                   4882:                     'rol'  => "Role",
1.113     raeburn  4883:                     'grs'  => "Section",
1.88      raeburn  4884:                     'exs'  => "Existing sections",
                   4885:                     'new'  => "Define new section", 
                   4886:                     'sta'  => "Start",
                   4887:                     'end'  => "End",
                   4888:                     'ssd'  => "Set Start Date",
                   4889:                     'sed'  => "Set End Date"
                   4890:                   );
1.131     raeburn  4891:     my $header = '<h4>'.&mt('Course Level').'</h4>'.
1.136     raeburn  4892:                  &Apache::loncommon::start_data_table().
                   4893:                  &Apache::loncommon::start_data_table_header_row().
1.143     raeburn  4894:                  '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.136     raeburn  4895:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  4896:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.131     raeburn  4897:                      '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
1.88      raeburn  4898:                      '<td><select name="role">'."\n";
1.213     raeburn  4899:     foreach my $role (@roles) {
1.135     raeburn  4900:         my $plrole=&Apache::lonnet::plaintext($role);
                   4901:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
1.88      raeburn  4902:     }
                   4903:     if ( keys %customroles > 0) {
1.135     raeburn  4904:         foreach my $cust (sort keys %customroles) {
1.101     albertel 4905:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  4906:                     '_'.$env{'user.name'}.'_'.$cust;
                   4907:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
1.88      raeburn  4908:         }
                   4909:     }
                   4910:     $otheritems .= '</select></td><td>'.
                   4911:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   4912:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
                   4913:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                   4914:                      '<td>&nbsp;&nbsp;</td>'.
                   4915:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  4916:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  4917:                      '<input type="hidden" name="section" value="" />'.
1.113     raeburn  4918:                      '<input type="hidden" name="groups" value="" /></td>'.
1.88      raeburn  4919:                      '</tr></table></td>';
                   4920:     $otheritems .= <<ENDTIMEENTRY;
1.169     albertel 4921: <td><input type="hidden" name="start" value='' />
1.88      raeburn  4922: <a href=
                   4923: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.169     albertel 4924: <td><input type="hidden" name="end" value='' />
1.88      raeburn  4925: <a href=
                   4926: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   4927: ENDTIMEENTRY
1.136     raeburn  4928:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   4929:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  4930:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   4931: }
                   4932: 
1.237     raeburn  4933: sub update_selfenroll_config {
1.241     raeburn  4934:     my ($r,$context,$permission) = @_;
1.237     raeburn  4935:     my ($row,$lt) = &get_selfenroll_titles();
1.241     raeburn  4936:     my %curr_groups = &Apache::longroup::coursegroups();
1.237     raeburn  4937:     my (%changes,%warning);
                   4938:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4939:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.241     raeburn  4940:     my $curr_types;
1.237     raeburn  4941:     if (ref($row) eq 'ARRAY') {
                   4942:         foreach my $item (@{$row}) {
                   4943:             if ($item eq 'enroll_dates') {
                   4944:                 my (%currenrolldate,%newenrolldate);
                   4945:                 foreach my $type ('start','end') {
                   4946:                     $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
                   4947:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   4948:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   4949:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   4950:                     }
                   4951:                 }
                   4952:             } elsif ($item eq 'access_dates') {
                   4953:                 my (%currdate,%newdate);
                   4954:                 foreach my $type ('start','end') {
                   4955:                     $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
                   4956:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   4957:                     if ($newdate{$type} ne $currdate{$type}) {
                   4958:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   4959:                     }
                   4960:                 }
1.241     raeburn  4961:             } elsif ($item eq 'types') {
                   4962:                 $curr_types =
                   4963:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   4964:                 if ($env{'form.selfenroll_all'}) {
                   4965:                     if ($curr_types ne '*') {
                   4966:                         $changes{'internal.selfenroll_types'} = '*';
                   4967:                     } else {
                   4968:                         next;
                   4969:                     }
                   4970:                 } else {
1.249     raeburn  4971:                     my %currdoms;
1.241     raeburn  4972:                     my @entries = split(/;/,$curr_types);
                   4973:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  4974:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  4975:                     my $newnum = 0;
1.249     raeburn  4976:                     my @latesttypes;
                   4977:                     foreach my $num (@activations) {
                   4978:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   4979:                         if (@types > 0) {
1.241     raeburn  4980:                             @types = sort(@types);
                   4981:                             my $typestr = join(',',@types);
1.249     raeburn  4982:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   4983:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   4984:                             $currdoms{$typedom} = 1;
1.241     raeburn  4985:                             $newnum ++;
                   4986:                         }
                   4987:                     }
1.249     raeburn  4988:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {                        if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
                   4989:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   4990:                             if (@types > 0) {
                   4991:                                 @types = sort(@types);
                   4992:                                 my $typestr = join(',',@types);
                   4993:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   4994:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   4995:                                 $currdoms{$typedom} = 1;
                   4996:                                 $newnum ++;
                   4997:                             }
                   4998:                         }
                   4999:                     }
                   5000:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   5001:                         my $typedom = $env{'form.selfenroll_newdom'};
                   5002:                         if ((!defined($currdoms{$typedom})) && 
                   5003:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   5004:                             my $typestr;
                   5005:                             my ($othertitle,$usertypes,$types) = 
                   5006:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   5007:                             my $othervalue = 'any';
                   5008:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   5009:                                 if (@{$types} > 0) {
1.257     raeburn  5010:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  5011:                                     $othervalue = 'other';
1.258     raeburn  5012:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  5013:                                 }
                   5014:                                 $typestr = $othervalue;
                   5015:                             } else {
                   5016:                                 $typestr = $othervalue;
                   5017:                             } 
                   5018:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   5019:                             $newnum ++ ;
                   5020:                         }
                   5021:                     }
1.241     raeburn  5022:                     my $selfenroll_types = join(';',@latesttypes);
                   5023:                     if ($selfenroll_types ne $curr_types) {
                   5024:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   5025:                     }
                   5026:                 }
1.237     raeburn  5027:             } else {
                   5028:                 my $curr_val = 
                   5029:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   5030:                 my $newval = $env{'form.selfenroll_'.$item};
                   5031:                 if ($item eq 'section') {
                   5032:                     $newval = $env{'form.sections'};
1.241     raeburn  5033:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  5034:                         $newval = $curr_val;
                   5035:                         $warning{$item} = &mt('Section for self-enrolled users unchanged as the proposed section is a group').'<br />'.&mt('Group names and section names must be distinct');
                   5036:                     } elsif ($newval eq 'all') {
                   5037:                         $newval = $curr_val;
                   5038:                         $warning{$item} = &mt("Section for self-enrolled users unchanged, as 'all' is a reserved section name.");
                   5039:                     }
                   5040:                     if ($newval eq '') {
                   5041:                         $newval = 'none';
                   5042:                     }
                   5043:                 }
                   5044:                 if ($newval ne $curr_val) {
                   5045:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   5046:                 }
1.241     raeburn  5047:             }
1.237     raeburn  5048:         }
                   5049:         if (keys(%warning) > 0) {
                   5050:             foreach my $item (@{$row}) {
                   5051:                 if (exists($warning{$item})) {
                   5052:                     $r->print($warning{$item}.'<br />');
                   5053:                 }
                   5054:             } 
                   5055:         }
                   5056:         if (keys(%changes) > 0) {
                   5057:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   5058:             if ($putresult eq 'ok') {
                   5059:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   5060:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   5061:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   5062:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   5063:                                                                 $cnum,undef,undef,'Course');
                   5064:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                   5065:                     if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
                   5066:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   5067:                             if (exists($changes{'internal.'.$item})) {
                   5068:                                 $crsinfo{$env{'request.course.id'}}{$item} = 
                   5069:                                     $changes{'internal.'.$item};
                   5070:                             }
                   5071:                         }
                   5072:                         my $crsputresult =
                   5073:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   5074:                                                          $chome,'notime');
                   5075:                     }
                   5076:                 }
                   5077:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   5078:                 foreach my $item (@{$row}) {
                   5079:                     my $title = $item;
                   5080:                     if (ref($lt) eq 'HASH') {
                   5081:                         $title = $lt->{$item};
                   5082:                     }
                   5083:                     if ($item eq 'enroll_dates') {
                   5084:                         foreach my $type ('start','end') {
                   5085:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   5086:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   5087:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  5088:                                           $title,$type,$newdate).'</li>');
                   5089:                             }
                   5090:                         }
                   5091:                     } elsif ($item eq 'access_dates') {
                   5092:                         foreach my $type ('start','end') {
                   5093:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   5094:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   5095:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  5096:                                           $title,$type,$newdate).'</li>');
                   5097:                             }
                   5098:                         }
                   5099:                     } else {
                   5100:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  5101:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   5102:                             if ($item eq 'types') {
                   5103:                                 if ($newval eq '') {
                   5104:                                     $newval = &mt('None');
                   5105:                                 } elsif ($newval eq '*') {
                   5106:                                     $newval = &mt('Any user in any domain');
                   5107:                                 }
1.245     raeburn  5108:                             } elsif ($item eq 'registered') {
                   5109:                                 if ($newval eq '1') {
                   5110:                                     $newval = &mt('Yes');
                   5111:                                 } elsif ($newval eq '0') {
                   5112:                                     $newval = &mt('No');
                   5113:                                 }
1.241     raeburn  5114:                             }
1.244     bisitz   5115:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  5116:                         }
                   5117:                     }
                   5118:                 }
                   5119:                 $r->print('</ul>');
                   5120:                 my %newenvhash;
                   5121:                 foreach my $key (keys(%changes)) {
                   5122:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
                   5123:                 }
1.238     raeburn  5124:                 &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  5125:             } else {
                   5126:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
                   5127:             }
                   5128:         } else {
1.249     raeburn  5129:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  5130:         }
                   5131:     } else {
1.249     raeburn  5132:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  5133:     }
1.256     raeburn  5134:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   5135:     if (ref($visactions) eq 'HASH') {
                   5136:         if (!$visible) {
                   5137:             $r->print('<br />'.$visactions->{'miss'}.'<br />'.$visactions->{'yous'}.
                   5138:                       '<br />');
                   5139:             if (ref($vismsgs) eq 'ARRAY') {
                   5140:                 $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   5141:                 foreach my $item (@{$vismsgs}) {
                   5142:                     $r->print('<li>'.$visactions->{$item}.'</li>');
                   5143:                 }
                   5144:                 $r->print('</ul>');
                   5145:             }
                   5146:             $r->print($cansetvis);
                   5147:         }
                   5148:     } 
1.237     raeburn  5149:     return;
                   5150: }
                   5151: 
                   5152: sub get_selfenroll_titles {
                   5153:     my @row = ('types','registered','enroll_dates','access_dates','section');
                   5154:     my %lt = &Apache::lonlocal::texthash (
                   5155:                 types        => 'Users allowed to self-enroll in this course',
1.245     raeburn  5156:                 registered   => 'Restrict self-enrollment to students officially registered for the course',
1.237     raeburn  5157:                 enroll_dates => 'Dates self-enrollment available',
1.256     raeburn  5158:                 access_dates => 'Course access dates assigned to self-enrolling users',
                   5159:                 section      => 'Section assigned to self-enrolling users',
1.237     raeburn  5160:              );
                   5161:     return (\@row,\%lt);
                   5162: }
                   5163: 
1.27      matthew  5164: #---------------------------------------------- end functions for &phase_two
1.29      matthew  5165: 
                   5166: #--------------------------------- functions for &phase_two and &phase_three
                   5167: 
                   5168: #--------------------------end of functions for &phase_two and &phase_three
1.1       www      5169: 
                   5170: 1;
                   5171: __END__
1.2       www      5172: 
                   5173: 

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