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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.329.2.6! raeburn     4: # $Id: loncreateuser.pm,v 1.329.2.5 2010/10/11 15:38:17 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: 
1.324     raeburn    54: Custom roles can be defined by a Domain Coordinator, Course Coordinator
                     55: or Community Coordinator via the Manage User functionality.
                     56: The custom role editor screen will show all privileges which can be
                     57: assigned to users. For a complete list of privileges, please see 
                     58: C</home/httpd/lonTabs/rolesplain.tab>.
1.66      bowersj2   59: 
1.324     raeburn    60: Custom role definitions are stored in the C<roles.db> file of the creator
                     61: of the role.
1.66      bowersj2   62: 
                     63: =cut
1.1       www        64: 
                     65: use strict;
                     66: use Apache::Constants qw(:common :http);
                     67: use Apache::lonnet;
1.54      bowersj2   68: use Apache::loncommon;
1.68      www        69: use Apache::lonlocal;
1.117     raeburn    70: use Apache::longroup;
1.190     raeburn    71: use Apache::lonuserutils;
1.307     raeburn    72: use Apache::loncoursequeueadmin;
1.329.2.6! raeburn    73: use Apache::lonrequestcourse;
1.139     albertel   74: use LONCAPA qw(:DEFAULT :match);
1.1       www        75: 
1.20      harris41   76: my $loginscript; # piece of javascript used in two separate instances
                     77: my $authformnop;
                     78: my $authformkrb;
                     79: my $authformint;
                     80: my $authformfsys;
                     81: my $authformloc;
                     82: 
1.94      matthew    83: sub initialize_authen_forms {
1.227     raeburn    84:     my ($dom,$formname,$curr_authtype,$mode) = @_;
                     85:     my ($krbdef,$krbdefdom) = &Apache::loncommon::get_kerberos_defaults($dom);
                     86:     my %param = ( formname => $formname,
1.187     raeburn    87:                   kerb_def_dom => $krbdefdom,
1.227     raeburn    88:                   kerb_def_auth => $krbdef,
1.187     raeburn    89:                   domain => $dom,
                     90:                 );
1.188     raeburn    91:     my %abv_auth = &auth_abbrev();
1.227     raeburn    92:     if ($curr_authtype =~ /^(krb4|krb5|internal|localauth|unix):(.*)$/) {
1.188     raeburn    93:         my $long_auth = $1;
1.227     raeburn    94:         my $curr_autharg = $2;
1.188     raeburn    95:         my %abv_auth = &auth_abbrev();
                     96:         $param{'curr_authtype'} = $abv_auth{$long_auth};
                     97:         if ($long_auth =~ /^krb(4|5)$/) {
                     98:             $param{'curr_kerb_ver'} = $1;
1.227     raeburn    99:             $param{'curr_autharg'} = $curr_autharg;
1.188     raeburn   100:         }
1.205     raeburn   101:         if ($mode eq 'modifyuser') {
                    102:             $param{'mode'} = $mode;
                    103:         }
1.187     raeburn   104:     }
1.227     raeburn   105:     $loginscript  = &Apache::loncommon::authform_header(%param);
                    106:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew   107:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
                    108:     $authformint  = &Apache::loncommon::authform_internal(%param);
                    109:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                    110:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41  111: }
                    112: 
1.188     raeburn   113: sub auth_abbrev {
                    114:     my %abv_auth = (
1.311     raeburn   115:                      krb5     => 'krb',
1.188     raeburn   116:                      krb4     => 'krb',
                    117:                      internal => 'int',
                    118:                      localuth => 'loc',
                    119:                      unix     => 'fsys',
                    120:                    );
                    121:     return %abv_auth;
                    122: }
1.43      www       123: 
1.134     raeburn   124: # ====================================================
                    125: 
                    126: sub portfolio_quota {
                    127:     my ($ccuname,$ccdomain) = @_;
                    128:     my %lt = &Apache::lonlocal::texthash(
1.267     raeburn   129:                    'usrt'      => "User Tools",
                    130:                    'disk'      => "Disk space allocated to user's portfolio files",
                    131:                    'cuqu'      => "Current quota",
                    132:                    'cust'      => "Custom quota",
                    133:                    'defa'      => "Default",
                    134:                    'chqu'      => "Change quota",
1.134     raeburn   135:     );
1.149     raeburn   136:     my ($currquota,$quotatype,$inststatus,$defquota) = 
                    137:         &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
                    138:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
                    139:     my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
                    140:     if ($inststatus ne '') {
                    141:         if ($usertypes->{$inststatus} ne '') {
                    142:             $longinsttype = $usertypes->{$inststatus};
                    143:         }
                    144:     }
                    145:     $custom_on = ' ';
                    146:     $custom_off = ' checked="checked" ';
                    147:     my $quota_javascript = <<"END_SCRIPT";
                    148: <script type="text/javascript">
1.301     bisitz    149: // <![CDATA[
1.149     raeburn   150: function quota_changes(caller) {
                    151:     if (caller == "custom") {
                    152:         if (document.cu.customquota[0].checked) {
                    153:             document.cu.portfolioquota.value = "";
                    154:         }
                    155:     }
                    156:     if (caller == "quota") {
                    157:         document.cu.customquota[1].checked = true;
                    158:     }
                    159: }
1.301     bisitz    160: // ]]>
1.149     raeburn   161: </script>
                    162: END_SCRIPT
                    163:     if ($quotatype eq 'custom') {
                    164:         $custom_on = $custom_off;
                    165:         $custom_off = ' ';
                    166:         $showquota = $currquota;
                    167:         if ($longinsttype eq '') {
1.230     bisitz    168:             $defaultinfo = &mt('For this user, the default quota would be [_1]'
                    169:                             .' Mb.',$defquota);
1.149     raeburn   170:         } else {
1.231     raeburn   171:             $defaultinfo = &mt("For this user, the default quota would be [_1]".
                    172:                                " Mb, as determined by the user's institutional".
                    173:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   174:         }
                    175:     } else {
                    176:         if ($longinsttype eq '') {
1.230     bisitz    177:             $defaultinfo = &mt('For this user, the default quota is [_1]'
                    178:                             .' Mb.',$defquota);
1.149     raeburn   179:         } else {
1.231     raeburn   180:             $defaultinfo = &mt("For this user, the default quota of [_1]".
                    181:                                " Mb, is determined by the user's institutional".
                    182:                                " affiliation ([_2]).",$defquota,$longinsttype);
1.149     raeburn   183:         }
                    184:     }
1.267     raeburn   185: 
                    186:     my $output = $quota_javascript."\n".
                    187:                  '<h3>'.$lt{'usrt'}.'</h3>'."\n".
                    188:                  &Apache::loncommon::start_data_table();
                    189: 
                    190:     if (&Apache::lonnet::allowed('mut',$ccdomain)) {
1.275     raeburn   191:         $output .= &build_tools_display($ccuname,$ccdomain,'tools');
1.267     raeburn   192:     }
                    193:     if (&Apache::lonnet::allowed('mpq',$ccdomain)) {
                    194:         $output .= '<tr class="LC_info_row">'."\n".
                    195:                    '    <td>'.$lt{'disk'}.'</td>'."\n".
                    196:                    '  </tr>'."\n".
                    197:                    &Apache::loncommon::start_data_table_row()."\n".
                    198:                    '  <td>'.$lt{'cuqu'}.': '.
                    199:                    $currquota.'&nbsp;Mb.&nbsp;&nbsp;'.
                    200:                    $defaultinfo.'</td>'."\n".
                    201:                    &Apache::loncommon::end_data_table_row()."\n".
                    202:                    &Apache::loncommon::start_data_table_row()."\n".
                    203:                    '  <td><span class="LC_nobreak">'.$lt{'chqu'}.
                    204:                    ': <label>'.
                    205:                    '<input type="radio" name="customquota" value="0" '.
                    206:                    $custom_off.' onchange="javascript:quota_changes('."'custom'".')"'.
                    207:                    ' />'.$lt{'defa'}.'&nbsp;('.$defquota.' Mb).</label>&nbsp;'.
                    208:                    '&nbsp;<label><input type="radio" name="customquota" value="1" '. 
                    209:                    $custom_on.'  onchange="javascript:quota_changes('."'custom'".')" />'.
                    210:                    $lt{'cust'}.':</label>&nbsp;'.
                    211:                    '<input type="text" name="portfolioquota" size ="5" value="'.
                    212:                    $showquota.'" onfocus="javascript:quota_changes('."'quota'".')" '.
                    213:                    '/>&nbsp;Mb</span></td>'."\n".
                    214:                    &Apache::loncommon::end_data_table_row()."\n";
                    215:     }  
                    216:     $output .= &Apache::loncommon::end_data_table();
1.134     raeburn   217:     return $output;
                    218: }
                    219: 
1.275     raeburn   220: sub build_tools_display {
                    221:     my ($ccuname,$ccdomain,$context) = @_;
1.306     raeburn   222:     my (@usertools,%userenv,$output,@options,%validations,%reqtitles,%reqdisplay,
                    223:         $colspan);
1.275     raeburn   224:     my %lt = &Apache::lonlocal::texthash (
                    225:                    'blog'       => "Personal User Blog",
                    226:                    'aboutme'    => "Personal Information Page",
                    227:                    'portfolio'  => "Personal User Portfolio",
                    228:                    'avai'       => "Available",
                    229:                    'cusa'       => "availability",
                    230:                    'chse'       => "Change setting",
                    231:                    'usde'       => "Use default",
                    232:                    'uscu'       => "Use custom",
                    233:                    'official'   => 'Can request creation of official courses',
1.299     raeburn   234:                    'unofficial' => 'Can request creation of unofficial courses',
                    235:                    'community'  => 'Can request creation of communities',
1.275     raeburn   236:     );
1.279     raeburn   237:     if ($context eq 'requestcourses') {
1.275     raeburn   238:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
1.299     raeburn   239:                       'requestcourses.official','requestcourses.unofficial',
                    240:                       'requestcourses.community');
                    241:         @usertools = ('official','unofficial','community');
1.309     raeburn   242:         @options =('norequest','approval','autolimit','validate');
1.306     raeburn   243:         %validations = &Apache::lonnet::auto_courserequest_checks($ccdomain);
                    244:         %reqtitles = &courserequest_titles();
                    245:         %reqdisplay = &courserequest_display();
                    246:         $colspan = ' colspan="2"';
1.275     raeburn   247:     } else {
                    248:         %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    249:                           'tools.aboutme','tools.portfolio','tools.blog');
                    250:         @usertools = ('aboutme','blog','portfolio');
                    251:     }
                    252:     foreach my $item (@usertools) {
1.306     raeburn   253:         my ($custom_access,$curr_access,$cust_on,$cust_off,$tool_on,$tool_off,
                    254:             $currdisp,$custdisp,$custradio);
1.275     raeburn   255:         $cust_off = 'checked="checked" ';
                    256:         $tool_on = 'checked="checked" ';
                    257:         $curr_access =  
                    258:             &Apache::lonnet::usertools_access($ccuname,$ccdomain,$item,undef,
                    259:                                               $context);
1.306     raeburn   260:         if ($userenv{$context.'.'.$item} ne '') {
                    261:             $cust_on = ' checked="checked" ';
                    262:             $cust_off = '';
                    263:         }
                    264:         if ($context eq 'requestcourses') {
                    265:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   266:                 $custom_access = &mt('Currently from default setting.');
1.306     raeburn   267:             } else {
                    268:                 $custom_access = &mt('Currently from custom setting.');
1.275     raeburn   269:             }
                    270:         } else {
1.306     raeburn   271:             if ($userenv{$context.'.'.$item} eq '') {
1.314     raeburn   272:                 $custom_access =
1.306     raeburn   273:                     &mt('Availability determined currently from default setting.');
                    274:                 if (!$curr_access) {
                    275:                     $tool_off = 'checked="checked" ';
                    276:                     $tool_on = '';
                    277:                 }
                    278:             } else {
1.314     raeburn   279:                 $custom_access =
1.306     raeburn   280:                     &mt('Availability determined currently from custom setting.');
                    281:                 if ($userenv{$context.'.'.$item} == 0) {
                    282:                     $tool_off = 'checked="checked" ';
                    283:                     $tool_on = '';
                    284:                 }
1.275     raeburn   285:             }
                    286:         }
                    287:         $output .= '  <tr class="LC_info_row">'."\n".
1.306     raeburn   288:                    '   <td'.$colspan.'>'.$lt{$item}.'</td>'."\n".
1.275     raeburn   289:                    '  </tr>'."\n".
1.306     raeburn   290:                    &Apache::loncommon::start_data_table_row()."\n";
                    291:         if ($context eq 'requestcourses') {
                    292:             my ($curroption,$currlimit);
                    293:             $curroption = $userenv{$context.'.'.$item};
                    294:             if (!$curroption) {
                    295:                 $curroption = 'norequest';
                    296:             }
                    297:             if ($curroption =~ /^autolimit=(\d*)$/) {
                    298:                 $currlimit = $1;
1.314     raeburn   299:                 if ($currlimit eq '') {
                    300:                     $currdisp = &mt('Yes, automatic creation');
                    301:                 } else {
                    302:                     $currdisp = &mt('Yes, up to [quant,_1,request]/user',$currlimit);
                    303:                 }
1.306     raeburn   304:             } else {
                    305:                 $currdisp = $reqdisplay{$curroption};
                    306:             }
                    307:             $custdisp = '<table>';
                    308:             foreach my $option (@options) {
                    309:                 my $val = $option;
                    310:                 if ($option eq 'norequest') {
                    311:                     $val = 0;
                    312:                 }
                    313:                 if ($option eq 'validate') {
                    314:                     my $canvalidate = 0;
                    315:                     if (ref($validations{$item}) eq 'HASH') {
                    316:                         if ($validations{$item}{'_custom_'}) {
                    317:                             $canvalidate = 1;
                    318:                         }
                    319:                     }
                    320:                     next if (!$canvalidate);
                    321:                 }
                    322:                 my $checked = '';
                    323:                 if ($option eq $curroption) {
                    324:                     $checked = ' checked="checked"';
                    325:                 } elsif ($option eq 'autolimit') {
                    326:                     if ($curroption =~ /^autolimit/) {
                    327:                         $checked = ' checked="checked"';
                    328:                     }
                    329:                 }
                    330:                 $custdisp .= '<tr><td><span class="LC_nobreak"><label>'.
                    331:                              '<input type="radio" name="crsreq_'.$item.
                    332:                              '" value="'.$val.'"'.$checked.' />'.
                    333:                              $reqtitles{$option}.'</label>&nbsp;';
                    334:                 if ($option eq 'autolimit') {
                    335:                     $custdisp .= '<input type="text" name="crsreq_'.
                    336:                                  $item.'_limit" size="1" '.
1.314     raeburn   337:                                  'value="'.$currlimit.'" /></span><br />'.
                    338:                                  $reqtitles{'unlimited'};
                    339:                  } else {
                    340:                      $custdisp .= '</span>';
1.306     raeburn   341:                  }
1.314     raeburn   342:                  $custdisp .= '</td></tr>';
1.306     raeburn   343:             }
                    344:             $custdisp .= '</table>';
                    345:             $custradio = '</span></td><td>'.&mt('Custom setting').'<br />'.$custdisp;
                    346:         } else {
                    347:             $currdisp = ($curr_access?&mt('Yes'):&mt('No'));
                    348:             $custdisp = '<span class="LC_nobreak"><label>'.
1.314     raeburn   349:                         '<input type="radio" name="'.$context.'_'.$item.'"'.
1.306     raeburn   350:                         ' value="1"'. $tool_on.'/>'.&mt('On').'</label>&nbsp;<label>'.
                    351:                         '<input type="radio" name="'.$context.'_'.$item.'" value="0" '.
                    352:                         $tool_off.'/>'.&mt('Off').'</label></span>';
                    353:             $custradio = ('&nbsp;'x2).'--'.$lt{'cusa'}.':&nbsp;'.$custdisp.
                    354:                           '</span>';
                    355:         }
                    356:         $output .= '  <td'.$colspan.'>'.$custom_access.('&nbsp;'x4).
                    357:                    $lt{'avai'}.': '.$currdisp.'</td>'."\n".
1.275     raeburn   358:                    &Apache::loncommon::end_data_table_row()."\n".
                    359:                    &Apache::loncommon::start_data_table_row()."\n".
1.306     raeburn   360:                    '  <td style="vertical-align:top;"><span class="LC_nobreak">'.
                    361:                    $lt{'chse'}.': <label>'.
1.275     raeburn   362:                    '<input type="radio" name="custom'.$item.'" value="0" '.
1.306     raeburn   363:                    $cust_off.'/>'.$lt{'usde'}.'</label>'.('&nbsp;' x3).
                    364:                    '<label><input type="radio" name="custom'.$item.'" value="1" '.
                    365:                    $cust_on.'/>'.$lt{'uscu'}.'</label>'.$custradio.'</td>'.
1.275     raeburn   366:                    &Apache::loncommon::end_data_table_row()."\n";
                    367:     }
                    368:     return $output;
                    369: }
                    370: 
1.300     raeburn   371: sub coursereq_externaluser {
                    372:     my ($ccuname,$ccdomain,$cdom) = @_;
1.306     raeburn   373:     my (@usertools,@options,%validations,%userenv,$output);
1.300     raeburn   374:     my %lt = &Apache::lonlocal::texthash (
                    375:                    'official'   => 'Can request creation of official courses',
                    376:                    'unofficial' => 'Can request creation of unofficial courses',
                    377:                    'community'  => 'Can request creation of communities',
                    378:     );
                    379: 
                    380:     %userenv = &Apache::lonnet::userenvironment($ccdomain,$ccuname,
                    381:                       'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                    382:                       'reqcrsotherdom.community');
                    383:     @usertools = ('official','unofficial','community');
1.309     raeburn   384:     @options = ('approval','validate','autolimit');
1.306     raeburn   385:     %validations = &Apache::lonnet::auto_courserequest_checks($cdom);
                    386:     my $optregex = join('|',@options);
                    387:     my %reqtitles = &courserequest_titles();
1.300     raeburn   388:     foreach my $item (@usertools) {
1.306     raeburn   389:         my ($curroption,$currlimit,$tooloff);
1.300     raeburn   390:         if ($userenv{'reqcrsotherdom.'.$item} ne '') {
                    391:             my @curr = split(',',$userenv{'reqcrsotherdom.'.$item});
1.314     raeburn   392:             foreach my $req (@curr) {
                    393:                 if ($req =~ /^\Q$cdom\E\:($optregex)=?(\d*)$/) {
                    394:                     $curroption = $1;
                    395:                     $currlimit = $2;
                    396:                     last;
1.306     raeburn   397:                 }
                    398:             }
1.314     raeburn   399:             if (!$curroption) {
                    400:                 $curroption = 'norequest';
                    401:                 $tooloff = ' checked="checked"';
                    402:             }
1.306     raeburn   403:         } else {
                    404:             $curroption = 'norequest';
                    405:             $tooloff = ' checked="checked"';
                    406:         }
                    407:         $output.= &Apache::loncommon::start_data_table_row()."\n".
1.314     raeburn   408:                   '  <td><span class="LC_nobreak">'.$lt{$item}.': </span></td><td>'.
                    409:                   '<table><tr><td valign="top">'."\n".
1.306     raeburn   410:                   '<label><input type="radio" name="reqcrsotherdom_'.$item.
1.314     raeburn   411:                   '" value=""'.$tooloff.' />'.$reqtitles{'norequest'}.
                    412:                   '</label></td>';
1.306     raeburn   413:         foreach my $option (@options) {
                    414:             if ($option eq 'validate') {
                    415:                 my $canvalidate = 0;
                    416:                 if (ref($validations{$item}) eq 'HASH') {
                    417:                     if ($validations{$item}{'_external_'}) {
                    418:                         $canvalidate = 1;
                    419:                     }
                    420:                 }
                    421:                 next if (!$canvalidate);
                    422:             }
                    423:             my $checked = '';
                    424:             if ($option eq $curroption) {
                    425:                 $checked = ' checked="checked"';
                    426:             }
1.314     raeburn   427:             $output .= '<td valign="top"><span class="LC_nobreak"><label>'.
1.306     raeburn   428:                        '<input type="radio" name="reqcrsotherdom_'.$item.
                    429:                        '" value="'.$option.'"'.$checked.' />'.
1.314     raeburn   430:                        $reqtitles{$option}.'</label>';
1.306     raeburn   431:             if ($option eq 'autolimit') {
1.314     raeburn   432:                 $output .= '&nbsp;<input type="text" name="reqcrsotherdom_'.
1.306     raeburn   433:                            $item.'_limit" size="1" '.
1.314     raeburn   434:                            'value="'.$currlimit.'" /></span>'.
                    435:                            '<br />'.$reqtitles{'unlimited'};
                    436:             } else {
                    437:                 $output .= '</span>';
1.300     raeburn   438:             }
1.314     raeburn   439:             $output .= '</td>';
1.300     raeburn   440:         }
1.314     raeburn   441:         $output .= '</td></tr></table></td>'."\n".
1.300     raeburn   442:                    &Apache::loncommon::end_data_table_row()."\n";
                    443:     }
                    444:     return $output;
                    445: }
                    446: 
1.306     raeburn   447: sub courserequest_titles {
                    448:     my %titles = &Apache::lonlocal::texthash (
                    449:                                    official   => 'Official',
                    450:                                    unofficial => 'Unofficial',
                    451:                                    community  => 'Communities',
                    452:                                    norequest  => 'Not allowed',
1.309     raeburn   453:                                    approval   => 'Approval by Dom. Coord.',
1.306     raeburn   454:                                    validate   => 'With validation',
                    455:                                    autolimit  => 'Numerical limit',
1.314     raeburn   456:                                    unlimited  => '(blank for unlimited)',
1.306     raeburn   457:                  );
                    458:     return %titles;
                    459: }
                    460: 
                    461: sub courserequest_display {
                    462:     my %titles = &Apache::lonlocal::texthash (
1.309     raeburn   463:                                    approval   => 'Yes, need approval',
1.306     raeburn   464:                                    validate   => 'Yes, with validation',
                    465:                                    norequest  => 'No',
                    466:    );
                    467:    return %titles;
                    468: }
                    469: 
1.2       www       470: # =================================================================== Phase one
1.1       www       471: 
1.42      matthew   472: sub print_username_entry_form {
1.329.2.5  raeburn   473:     my ($r,$context,$response,$srch,$forcenewuser,$crstype,$brcrum) = @_;
1.101     albertel  474:     my $defdom=$env{'request.role.domain'};
1.160     raeburn   475:     my $formtoset = 'crtuser';
1.329.2.4  raeburn   476:     my $is_custom = &Apache::loncommon::needs_gci_custom();
1.160     raeburn   477:     if (exists($env{'form.startrolename'})) {
                    478:         $formtoset = 'docustom';
                    479:         $env{'form.rolename'} = $env{'form.startrolename'};
1.207     raeburn   480:     } elsif ($env{'form.origform'} eq 'crtusername') {
                    481:         $formtoset =  $env{'form.origform'};
1.160     raeburn   482:     }
                    483: 
                    484:     my ($jsback,$elements) = &crumb_utilities();
                    485: 
                    486:     my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
1.165     albertel  487:         '<script type="text/javascript">'."\n".
1.301     bisitz    488:         '// <![CDATA['."\n".
                    489:         &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset})."\n".
                    490:         '// ]]>'."\n".
1.162     raeburn   491:         '</script>'."\n";
1.160     raeburn   492: 
1.324     raeburn   493:     my %existingroles=&Apache::lonuserutils::my_custom_roles($crstype);
                    494:     if (($env{'form.action'} eq 'custom') && (keys(%existingroles) > 0)
                    495:         && (&Apache::lonnet::allowed('mcr','/'))) {
                    496:         $jscript .= &customrole_javascript();
                    497:     }
1.329.2.3  raeburn   498:     my $title = 'User Management';
                    499:     if ($context eq 'course') {
1.329.2.4  raeburn   500:         if ($is_custom) {
1.329.2.3  raeburn   501:             $title = 'Enrollment and Student Activity';
                    502:         }
                    503:     }
1.224     raeburn   504:     my $helpitem = 'Course_Change_Privileges';
                    505:     if ($env{'form.action'} eq 'custom') {
                    506:         $helpitem = 'Course_Editing_Custom_Roles';
                    507:     } elsif ($env{'form.action'} eq 'singlestudent') {
                    508:         $helpitem = 'Course_Add_Student';
                    509:     }
1.329.2.5  raeburn   510:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
                    511:     if ($env{'form.action'} eq 'custom') {
                    512:         push(@{$brcrum},
                    513:                  {href=>"javascript:backPage(document.crtuser)",       
                    514:                   text=>"Pick custom role",
                    515:                   help => $helpitem,}
                    516:                  );
                    517:     } else {
                    518:         push (@{$brcrum},
                    519:                   {href => "javascript:backPage(document.crtuser)",
                    520:                    text => $breadcrumb_text{'search'},
                    521:                    help => $helpitem,
                    522:                    faq  => 282,
                    523:                    bug  => 'Instructor Interface',}
                    524:                   );
                    525:     }
                    526:     my %loaditems = (
                    527:                 'onload' => "javascript:setFormElements(document.$formtoset)",
                    528:                     );
                    529:     my $args = {bread_crumbs           => $brcrum,
                    530:                 bread_crumbs_component => 'User Management',
                    531:                 add_entries            => \%loaditems,};
                    532:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
                    533: 
1.71      sakharuk  534:     my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   535:                     'srst' => 'Search for a user and enroll as a student',
1.318     raeburn   536:                     'srme' => 'Search for a user and enroll as a member',
1.229     raeburn   537:                     'srad' => 'Search for a user and modify/add user information or roles',
1.71      sakharuk  538: 		    'usr'  => "Username",
                    539:                     'dom'  => "Domain",
1.324     raeburn   540:                     'ecrp' => "Define or Edit Custom Role",
                    541:                     'nr'   => "role name",
1.282     schafran  542:                     'cre'  => "Next",
1.71      sakharuk  543: 				       );
1.214     raeburn   544:     if ($env{'form.action'} eq 'custom') {
1.190     raeburn   545:         if (&Apache::lonnet::allowed('mcr','/')) {
1.324     raeburn   546:             my $newroletext = &mt('Define new custom role:');
                    547:             $r->print('<form action="/adm/createuser" method="post" name="docustom">'.
                    548:                       '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                    549:                       '<input type="hidden" name="phase" value="selected_custom_edit" />'.
                    550:                       '<h3>'.$lt{'ecrp'}.'</h3>'.
                    551:                       &Apache::loncommon::start_data_table().
                    552:                       &Apache::loncommon::start_data_table_row().
                    553:                       '<td>');
                    554:             if (keys(%existingroles) > 0) {
                    555:                 $r->print('<br /><label><input type="radio" name="customroleaction" value="new" checked="checked" onclick="setCustomFields();" /><b>'.$newroletext.'</b></label>');
                    556:             } else {
                    557:                 $r->print('<br /><input type="hidden" name="customroleaction" value="new" /><b>'.$newroletext.'</b>');
                    558:             }
                    559:             $r->print('</td><td align="center">'.$lt{'nr'}.'<br /><input type="text" size="15" name="newrolename" onfocus="setCustomAction('."'new'".');" /></td>'.
                    560:                       &Apache::loncommon::end_data_table_row());
                    561:             if (keys(%existingroles) > 0) {
                    562:                 $r->print(&Apache::loncommon::start_data_table_row().'<td><br />'.
                    563:                           '<label><input type="radio" name="customroleaction" value="edit" onclick="setCustomFields();"/><b>'.
                    564:                           &mt('View/Modify existing role:').'</b></label></td>'.
                    565:                           '<td align="center"><br />'.
                    566:                           '<select name="rolename" onchange="setCustomAction('."'edit'".');">'.
1.326     raeburn   567:                           '<option value="" selected="selected">'.
1.324     raeburn   568:                           &mt('Select'));
                    569:                 foreach my $role (sort(keys(%existingroles))) {
1.326     raeburn   570:                     $r->print('<option value="'.$role.'">'.$role.'</option>');
1.324     raeburn   571:                 }
                    572:                 $r->print('</select>'.
                    573:                           '</td>'.
                    574:                           &Apache::loncommon::end_data_table_row());
                    575:             }
                    576:             $r->print(&Apache::loncommon::end_data_table().'<p>'.
                    577:                       '<input name="customeditor" type="submit" value="'.
                    578:                       $lt{'cre'}.'" /></p>'.
                    579:                       '</form>');
1.190     raeburn   580:         }
1.213     raeburn   581:     } else {
1.229     raeburn   582:         my $actiontext = $lt{'srad'};
1.213     raeburn   583:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   584:             if ($crstype eq 'Community') {
                    585:                 $actiontext = $lt{'srme'};
                    586:             } else {
                    587:                 $actiontext = $lt{'srst'};
                    588:             }
1.213     raeburn   589:         }
1.324     raeburn   590:         $r->print("<h3>$actiontext</h3>");
1.213     raeburn   591:         if ($env{'form.origform'} ne 'crtusername') {
                    592:             $r->print("\n".$response);
                    593:         }
1.318     raeburn   594:         $r->print(&entry_form($defdom,$srch,$forcenewuser,$context,$response,$crstype));
1.107     www       595:     }
1.110     albertel  596: }
                    597: 
1.324     raeburn   598: sub customrole_javascript {
                    599:     my $js = <<"END";
                    600: <script type="text/javascript">
                    601: // <![CDATA[
                    602: 
                    603: function setCustomFields() {
                    604:     if (document.docustom.customroleaction.length > 0) {
                    605:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    606:             if (document.docustom.customroleaction[i].checked) {
                    607:                 if (document.docustom.customroleaction[i].value == 'new') {
                    608:                     document.docustom.rolename.selectedIndex = 0;
                    609:                 } else {
                    610:                     document.docustom.newrolename.value = '';
                    611:                 }
                    612:             }
                    613:         }
                    614:     }
                    615:     return;
                    616: }
                    617: 
                    618: function setCustomAction(caller) {
                    619:     if (document.docustom.customroleaction.length > 0) {
                    620:         for (var i=0; i<document.docustom.customroleaction.length; i++) {
                    621:             if (document.docustom.customroleaction[i].value == caller) {
                    622:                 document.docustom.customroleaction[i].checked = true;
                    623:             }
                    624:         }
                    625:     }
                    626:     setCustomFields();
                    627:     return;
                    628: }
                    629: 
                    630: // ]]>
                    631: </script>
                    632: END
                    633:     return $js;
                    634: }
                    635: 
1.160     raeburn   636: sub entry_form {
1.318     raeburn   637:     my ($dom,$srch,$forcenewuser,$context,$responsemsg,$crstype) = @_;
1.207     raeburn   638:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
1.229     raeburn   639:     my ($usertype,$inexact);
1.214     raeburn   640:     if (ref($srch) eq 'HASH') {
                    641:         if (($srch->{'srchin'} eq 'dom') &&
                    642:             ($srch->{'srchby'} eq 'uname') &&
                    643:             ($srch->{'srchtype'} eq 'exact') &&
                    644:             ($srch->{'srchdomain'} ne '') &&
                    645:             ($srch->{'srchterm'} ne '')) {
                    646:             my ($rules,$ruleorder) =
                    647:                 &Apache::lonnet::inst_userrules($srch->{'srchdomain'},'username');
                    648:             $usertype = &Apache::lonuserutils::check_usertype($srch->{'srchdomain'},$srch->{'srchterm'},$rules);
1.229     raeburn   649:         } else {
                    650:             $inexact = 1;
1.214     raeburn   651:         }
1.207     raeburn   652:     }
1.214     raeburn   653:     my $cancreate =
                    654:         &Apache::lonuserutils::can_create_user($dom,$context,$usertype);
1.160     raeburn   655:     my $userpicker = 
1.179     raeburn   656:        &Apache::loncommon::user_picker($dom,$srch,$forcenewuser,
1.214     raeburn   657:                                        'document.crtuser',$cancreate,$usertype);
1.160     raeburn   658:     my $srchbutton = &mt('Search');
1.229     raeburn   659:     if ($env{'form.action'} eq 'singlestudent') {
                    660:         $srchbutton = &mt('Search and Enroll');
                    661:     } elsif ($cancreate && $responsemsg ne '' && $inexact) {
                    662:         $srchbutton = &mt('Search or Add New User');
                    663:     }
1.207     raeburn   664:     my $output = <<"ENDBLOCK";
1.160     raeburn   665: <form action="/adm/createuser" method="post" name="crtuser">
1.190     raeburn   666: <input type="hidden" name="action" value="$env{'form.action'}" />
1.160     raeburn   667: <input type="hidden" name="phase" value="get_user_info" />
                    668: $userpicker
1.179     raeburn   669: <input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry(document.crtuser)" />
1.160     raeburn   670: </form>
1.207     raeburn   671: ENDBLOCK
1.229     raeburn   672:     if ($env{'form.phase'} eq '') {
1.207     raeburn   673:         my $defdom=$env{'request.role.domain'};
1.329.2.4  raeburn   674:         my $domform = &Apache::loncommon::select_dom_form($defdom,'srchdomain','',1);
1.207     raeburn   675:         my %lt=&Apache::lonlocal::texthash(
1.229     raeburn   676:                   'enro' => 'Enroll one student',
1.318     raeburn   677:                   'enrm' => 'Enroll one member',
1.229     raeburn   678:                   'admo' => 'Add/modify a single user',
                    679:                   'crea' => 'create new user if required',
                    680:                   'uskn' => "username is known",
1.207     raeburn   681:                   'crnu' => 'Create a new user',
                    682:                   'usr'  => 'Username',
                    683:                   'dom'  => 'in domain',
1.229     raeburn   684:                   'enrl' => 'Enroll',
                    685:                   'cram'  => 'Create/Modify user',
1.207     raeburn   686:         );
1.229     raeburn   687:         my $sellink=&Apache::loncommon::selectstudent_link('crtusername','srchterm','srchdomain');
                    688:         my ($title,$buttontext,$showresponse);
1.318     raeburn   689:         if ($env{'form.action'} eq 'singlestudent') {
                    690:             if ($crstype eq 'Community') {
                    691:                 $title = $lt{'enrm'};
                    692:             } else {
                    693:                 $title = $lt{'enro'};
                    694:             }
1.229     raeburn   695:             $buttontext = $lt{'enrl'};
                    696:         } else {
                    697:             $title = $lt{'admo'};
                    698:             $buttontext = $lt{'cram'};
                    699:         }
                    700:         if ($cancreate) {
                    701:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'crea'}.')</span>';
                    702:         } else {
                    703:             $title .= ' <span class="LC_cusr_subheading">('.$lt{'uskn'}.')</span>';
                    704:         }
                    705:         if ($env{'form.origform'} eq 'crtusername') {
                    706:             $showresponse = $responsemsg;
                    707:         }
1.207     raeburn   708:         $output .= <<"ENDDOCUMENT";
1.229     raeburn   709: <br />
1.207     raeburn   710: <form action="/adm/createuser" method="post" name="crtusername">
                    711: <input type="hidden" name="action" value="$env{'form.action'}" />
                    712: <input type="hidden" name="phase" value="createnewuser" />
                    713: <input type="hidden" name="srchtype" value="exact" />
1.233     raeburn   714: <input type="hidden" name="srchby" value="uname" />
1.207     raeburn   715: <input type="hidden" name="srchin" value="dom" />
                    716: <input type="hidden" name="forcenewuser" value="1" />
                    717: <input type="hidden" name="origform" value="crtusername" />
1.229     raeburn   718: <h3>$title</h3>
                    719: $showresponse
1.207     raeburn   720: <table>
                    721:  <tr>
                    722:   <td>$lt{'usr'}:</td>
1.329.2.4  raeburn   723:   <td><input type="text" size="25" name="srchterm" /></td>
1.207     raeburn   724:   <td>&nbsp;$lt{'dom'}:</td><td>$domform</td>
1.229     raeburn   725:   <td>&nbsp;$sellink&nbsp;</td>
                    726:   <td>&nbsp;<input name="userrole" type="submit" value="$buttontext" /></td>
1.207     raeburn   727:  </tr>
                    728: </table>
                    729: </form>
1.160     raeburn   730: ENDDOCUMENT
1.207     raeburn   731:     }
1.160     raeburn   732:     return $output;
                    733: }
1.110     albertel  734: 
                    735: sub user_modification_js {
1.113     raeburn   736:     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
                    737:     
1.110     albertel  738:     return <<END;
                    739: <script type="text/javascript" language="Javascript">
1.301     bisitz    740: // <![CDATA[
1.314     raeburn   741: 
1.110     albertel  742:     function pclose() {
                    743:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
                    744:                  "height=350,width=350,scrollbars=no,menubar=no");
                    745:         parmwin.close();
                    746:     }
                    747: 
                    748:     $pjump_def
                    749:     $dc_setcourse_code
                    750: 
                    751:     function dateset() {
                    752:         eval("document.cu."+document.cu.pres_marker.value+
                    753:             ".value=document.cu.pres_value.value");
                    754:         pclose();
                    755:     }
                    756: 
1.113     raeburn   757:     $nondc_setsection_code
1.301     bisitz    758: // ]]>
1.110     albertel  759: </script>
                    760: END
1.2       www       761: }
                    762: 
                    763: # =================================================================== Phase two
1.160     raeburn   764: sub print_user_selection_page {
1.329.2.5  raeburn   765:     my ($r,$response,$srch,$srch_results,$srcharray,$context,$opener_elements,$crstype,$brcrum) = @_;
1.160     raeburn   766:     my @fields = ('username','domain','lastname','firstname','permanentemail');
                    767:     my $sortby = $env{'form.sortby'};
1.329.2.4  raeburn   768:     my $is_custom = &Apache::loncommon::needs_gci_custom();
1.160     raeburn   769: 
                    770:     if (!grep(/^\Q$sortby\E$/,@fields)) {
                    771:         $sortby = 'lastname';
                    772:     }
                    773: 
                    774:     my ($jsback,$elements) = &crumb_utilities();
                    775: 
                    776:     my $jscript = (<<ENDSCRIPT);
                    777: <script type="text/javascript">
1.301     bisitz    778: // <![CDATA[
1.160     raeburn   779: function pickuser(uname,udom) {
                    780:     document.usersrchform.seluname.value=uname;
                    781:     document.usersrchform.seludom.value=udom;
                    782:     document.usersrchform.phase.value="userpicked";
                    783:     document.usersrchform.submit();
                    784: }
                    785: 
                    786: $jsback
1.301     bisitz    787: // ]]>
1.160     raeburn   788: </script>
                    789: ENDSCRIPT
                    790: 
                    791:     my %lt=&Apache::lonlocal::texthash(
1.179     raeburn   792:                                        'usrch'          => "User Search to add/modify roles",
                    793:                                        'stusrch'        => "User Search to enroll student",
1.318     raeburn   794:                                        'memsrch'        => "User Search to enroll member",
1.179     raeburn   795:                                        'usel'           => "Select a user to add/modify roles",
1.318     raeburn   796:                                        'stusel'         => "Select a user to enroll as a student",
                    797:                                        'memsel'         => "Select a user to enroll as a member",
1.160     raeburn   798:                                        'username'       => "username",
                    799:                                        'domain'         => "domain",
                    800:                                        'lastname'       => "last name",
                    801:                                        'firstname'      => "first name",
                    802:                                        'permanentemail' => "permanent e-mail",
                    803:                                       );
1.302     raeburn   804:     if ($context eq 'requestcrs') {
                    805:         $r->print('<div>');
                    806:     } else {
1.329.2.3  raeburn   807:         my $title = 'User Management';
                    808:         if ($context eq 'course') {
1.329.2.4  raeburn   809:             if ($is_custom) {
1.329.2.3  raeburn   810:                 $title = 'Enrollment and Student Activity';
                    811:             }
                    812:         }
1.318     raeburn   813:         my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.329.2.5  raeburn   814:         my $helpitem;
                    815:         if ($env{'form.action'} eq 'singleuser') {
                    816:             $helpitem = 'Course_Change_Privileges';
                    817:         } elsif ($env{'form.action'} eq 'singlestudent') {
                    818:             $helpitem = 'Course_Add_Student';
                    819:         }
                    820:         push (@{$brcrum},
                    821:                   {href => "javascript:backPage(document.usersrchform,'','')",
                    822:                    text => $breadcrumb_text{'search'},
                    823:                    faq  => 282,
                    824:                    bug  => 'Instructor Interface',},
                    825:                   {href => "javascript:backPage(document.usersrchform,'get_user_info','select')",
                    826:                    text => $breadcrumb_text{'userpicked'},
                    827:                    faq  => 282,
                    828:                    bug  => 'Instructor Interface',
                    829:                    help => $helpitem}
                    830:                   );
                    831:         $r->print(&Apache::loncommon::start_page($title,$jscript,{bread_crumbs => $brcrum}));
1.302     raeburn   832:         if ($env{'form.action'} eq 'singleuser') {
                    833:             $r->print("<b>$lt{'usrch'}</b><br />");
1.318     raeburn   834:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
1.302     raeburn   835:             $r->print('<h3>'.$lt{'usel'}.'</h3>');
                    836:         } elsif ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn   837:             $r->print($jscript."<b>");
                    838:             if ($crstype eq 'Community') {
                    839:                 $r->print($lt{'memsrch'});
                    840:             } else {
                    841:                 $r->print($lt{'stusrch'});
                    842:             }
                    843:             $r->print("</b><br />");
                    844:             $r->print(&entry_form($srch->{'srchdomain'},$srch,undef,$context,undef,$crstype));
                    845:             $r->print('</form><h3>');
                    846:             if ($crstype eq 'Community') {
                    847:                 $r->print($lt{'memsel'});
                    848:             } else {
                    849:                 $r->print($lt{'stusel'});
                    850:             }
                    851:             $r->print('</h3>');
1.302     raeburn   852:         }
1.179     raeburn   853:     }
1.160     raeburn   854:     $r->print('<form name="usersrchform" method="post">'.
                    855:               &Apache::loncommon::start_data_table()."\n".
                    856:               &Apache::loncommon::start_data_table_header_row()."\n".
                    857:               ' <th> </th>'."\n");
                    858:     foreach my $field (@fields) {
                    859:         $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
                    860:                   "'".$field."'".';document.usersrchform.submit();">'.
                    861:                   $lt{$field}.'</a></th>'."\n");
                    862:     }
                    863:     $r->print(&Apache::loncommon::end_data_table_header_row());
                    864: 
                    865:     my @sorted_users = sort {
1.167     albertel  866:         lc($srch_results->{$a}->{$sortby})   cmp lc($srch_results->{$b}->{$sortby})
1.160     raeburn   867:             ||
1.167     albertel  868:         lc($srch_results->{$a}->{lastname})  cmp lc($srch_results->{$b}->{lastname})
1.160     raeburn   869:             ||
                    870:         lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname})
1.167     albertel  871: 	    ||
                    872: 	lc($a) cmp lc($b)
1.160     raeburn   873:         } (keys(%$srch_results));
                    874: 
                    875:     foreach my $user (@sorted_users) {
                    876:         my ($uname,$udom) = split(/:/,$user);
1.302     raeburn   877:         my $onclick;
                    878:         if ($context eq 'requestcrs') {
1.314     raeburn   879:             $onclick =
1.302     raeburn   880:                 'onclick="javascript:gochoose('."'$uname','$udom',".
                    881:                                                "'$srch_results->{$user}->{firstname}',".
                    882:                                                "'$srch_results->{$user}->{lastname}',".
                    883:                                                "'$srch_results->{$user}->{permanentemail}'".');"';
                    884:         } else {
1.314     raeburn   885:             $onclick =
1.302     raeburn   886:                 ' onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".');"';
                    887:         }
1.160     raeburn   888:         $r->print(&Apache::loncommon::start_data_table_row().
1.302     raeburn   889:                   '<td><input type="button" name="seluser" value="'.&mt('Select').'" '.
                    890:                   $onclick.' /></td>'.
1.160     raeburn   891:                   '<td><tt>'.$uname.'</tt></td>'.
                    892:                   '<td><tt>'.$udom.'</tt></td>');
                    893:         foreach my $field ('lastname','firstname','permanentemail') {
                    894:             $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
                    895:         }
                    896:         $r->print(&Apache::loncommon::end_data_table_row());
                    897:     }
                    898:     $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
1.179     raeburn   899:     if (ref($srcharray) eq 'ARRAY') {
                    900:         foreach my $item (@{$srcharray}) {
                    901:             $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
                    902:         }
                    903:     }
1.160     raeburn   904:     $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
                    905:               ' <input type="hidden" name="seluname" value="" />'."\n".
                    906:               ' <input type="hidden" name="seludom" value="" />'."\n".
1.179     raeburn   907:               ' <input type="hidden" name="currstate" value="select" />'."\n".
1.190     raeburn   908:               ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
1.214     raeburn   909:               ' <input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1.302     raeburn   910:     if ($context eq 'requestcrs') {
                    911:         $r->print($opener_elements.'</form></div>');
                    912:     } else {
                    913:         $r->print($response.'</form>'.&Apache::loncommon::end_page());
                    914:     }
1.160     raeburn   915: }
                    916: 
                    917: sub print_user_query_page {
1.329.2.5  raeburn   918:     my ($r,$caller,$brcrum) = @_;
1.160     raeburn   919: # FIXME - this is for a network-wide name search (similar to catalog search)
                    920: # To use frames with similar behavior to catalog/portfolio search.
                    921: # To be implemented. 
                    922:     return;
                    923: }
                    924: 
1.42      matthew   925: sub print_user_modification_page {
1.329.2.5  raeburn   926:     my ($r,$ccuname,$ccdomain,$srch,$response,$context,$permission,$crstype,$brcrum) = @_;
1.185     raeburn   927:     if (($ccuname eq '') || ($ccdomain eq '')) {
1.215     raeburn   928:         my $usermsg = &mt('No username and/or domain provided.');
                    929:         $env{'form.phase'} = '';
1.329.2.5  raeburn   930: 	&print_username_entry_form($r,$context,$usermsg,'','',$crstype,$brcrum);
1.58      www       931:         return;
                    932:     }
1.213     raeburn   933:     my ($form,$formname);
                    934:     if ($env{'form.action'} eq 'singlestudent') {
                    935:         $form = 'document.enrollstudent';
                    936:         $formname = 'enrollstudent';
                    937:     } else {
                    938:         $form = 'document.cu';
                    939:         $formname = 'cu';
                    940:     }
1.188     raeburn   941:     my %abv_auth = &auth_abbrev();
1.227     raeburn   942:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
1.185     raeburn   943:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
1.329.2.4  raeburn   944:     my $is_custom = &Apache::loncommon::needs_gci_custom();
                    945:     if ($is_custom) {
                    946:         if ($uhome eq 'no_host') {
                    947:             my $lc_ccuname = lc($ccuname);
                    948:             if ($lc_ccuname ne $ccuname) {
                    949:                 $uhome = &Apache::lonnet::homeserver($lc_ccuname,$ccdomain);
                    950:                 $ccuname = $lc_ccuname;
                    951:             }
                    952:         }
                    953:     }
1.185     raeburn   954:     if ($uhome eq 'no_host') {
1.215     raeburn   955:         my ($rules,$ruleorder) =
                    956:             &Apache::lonnet::inst_userrules($ccdomain,'username');
1.329.2.4  raeburn   957:         my $usertype =
                    958:             &Apache::lonuserutils::check_usertype($ccdomain,$ccuname,$rules);
1.215     raeburn   959:         my $cancreate =
                    960:             &Apache::lonuserutils::can_create_user($ccdomain,$context,
                    961:                                                    $usertype);
                    962:         if (!$cancreate) {
1.292     bisitz    963:             my $helplink = 'javascript:helpMenu('."'display'".')';
1.215     raeburn   964:             my %usertypetext = (
                    965:                 official   => 'institutional',
                    966:                 unofficial => 'non-institutional',
                    967:             );
1.329.2.4  raeburn   968:             if ($ccdomain eq 'gci') {
                    969:                 $usertypetext{'unofficial'} = 'institutional',
                    970:             }
1.215     raeburn   971:             my $response;
                    972:             if ($env{'form.origform'} eq 'crtusername') {
1.329.2.4  raeburn   973:                 if ($is_custom) {
                    974:                     $response = '<span class="LC_warning">'.&mt('Invalid format for username for new user: [_1]','<b>'.$ccuname.'</b>').
                    975:                                 '</span><br />';
                    976:                 } else {
                    977:                     $response =  '<span class="LC_warning">'.&mt('No match was found for the username ([_1]) in LON-CAPA domain: [_2]',$ccuname,$ccdomain).'</span><br />';
                    978:                 }
1.215     raeburn   979:             }
1.292     bisitz    980:             $response .= '<p class="LC_warning">'
                    981:                         .&mt("You are not authorized to create new $usertypetext{$usertype} users in this domain.")
1.329.2.4  raeburn   982:                         .'<br />';
                    983:             if ($ccdomain eq 'gcitest') {
                    984:                 $response .= &mt('Enter a valid e-mail address as the username for the new user.').' '.&mt('Please contact the [_1]helpdesk[_2] for assistance.'
                    985:                              ,'<a href="'.$helplink.'">','</a>')
                    986:                              .'</p><br />';
                    987:             }
1.215     raeburn   988:             $env{'form.phase'} = '';
1.329.2.5  raeburn   989:             &print_username_entry_form($r,$context,$response,undef,undef,$crstype,$brcrum);
1.215     raeburn   990:             return;
                    991:         }
1.188     raeburn   992:         $newuser = 1;
1.193     raeburn   993:         my $checkhash;
                    994:         my $checks = { 'username' => 1 };
1.196     raeburn   995:         $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
1.193     raeburn   996:         &Apache::loncommon::user_rule_check($checkhash,$checks,
1.196     raeburn   997:             \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                    998:         if (ref($alerts{'username'}) eq 'HASH') {
                    999:             if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
                   1000:                 my $domdesc =
1.193     raeburn  1001:                     &Apache::lonnet::domain($ccdomain,'description');
1.196     raeburn  1002:                 if ($alerts{'username'}{$ccdomain}{$ccuname}) {
                   1003:                     my $userchkmsg;
                   1004:                     if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
                   1005:                         $userchkmsg = 
                   1006:                             &Apache::loncommon::instrule_disallow_msg('username',
1.193     raeburn  1007:                                                                  $domdesc,1).
                   1008:                         &Apache::loncommon::user_rule_formats($ccdomain,
                   1009:                             $domdesc,$curr_rules{$ccdomain}{'username'},
                   1010:                             'username');
1.196     raeburn  1011:                     }
1.215     raeburn  1012:                     $env{'form.phase'} = '';
1.329.2.5  raeburn  1013:                     &print_username_entry_form($r,$context,$userchkmsg,undef,undef,$crstype,$brcrum);
1.196     raeburn  1014:                     return;
1.215     raeburn  1015:                 }
1.193     raeburn  1016:             }
1.185     raeburn  1017:         }
1.187     raeburn  1018:     } else {
1.188     raeburn  1019:         $newuser = 0;
1.185     raeburn  1020:     }
1.160     raeburn  1021:     if ($response) {
1.215     raeburn  1022:         $response = '<br />'.$response;
1.160     raeburn  1023:     }
1.149     raeburn  1024: 
1.52      matthew  1025:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88      raeburn  1026:     my $dc_setcourse_code = '';
1.119     raeburn  1027:     my $nondc_setsection_code = '';                                        
1.112     albertel 1028:     my %loaditem;
1.114     albertel 1029: 
1.216     raeburn  1030:     my $groupslist = &Apache::lonuserutils::get_groupslist();
1.88      raeburn  1031: 
1.216     raeburn  1032:     my $js = &validation_javascript($context,$ccdomain,$pjump_def,
                   1033:                                $groupslist,$newuser,$formname,\%loaditem);
1.329.2.3  raeburn  1034:     my $title = 'User Management';
                   1035:     if ($context eq 'course') {
1.329.2.4  raeburn  1036:         if ($is_custom) {
1.329.2.3  raeburn  1037:             $title = 'Enrollment and Student Activity';
                   1038:         }
                   1039:     }
1.318     raeburn  1040:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  1041:     my $helpitem = 'Course_Change_Privileges';
                   1042:     if ($env{'form.action'} eq 'singlestudent') {
                   1043:         $helpitem = 'Course_Add_Student';
                   1044:     }
1.329.2.5  raeburn  1045:     push (@{$brcrum},
                   1046:         {href => "javascript:backPage($form)",
                   1047:          text => $breadcrumb_text{'search'},
                   1048:          faq  => 282,
                   1049:          bug  => 'Instructor Interface',});
                   1050:     if ($env{'form.phase'} eq 'userpicked') {
                   1051:        push(@{$brcrum},
                   1052:               {href => "javascript:backPage($form,'get_user_info','select')",
                   1053:                text => $breadcrumb_text{'userpicked'},
                   1054:                faq  => 282,
                   1055:                bug  => 'Instructor Interface',});
                   1056:     }
                   1057:     push(@{$brcrum},
                   1058:             {href => "javascript:backPage($form,'$env{'form.phase'}','modify')",
                   1059:              text => $breadcrumb_text{'modify'},
                   1060:              faq  => 282,
                   1061:              bug  => 'Instructor Interface',
                   1062:              help => $helpitem});
                   1063:     my $args = {'add_entries'           => \%loaditem,
                   1064:                 'bread_crumbs'          => $brcrum,
                   1065:                 'bread_crumbs_component' => 'User Management'};
                   1066:     if ($env{'form.popup'}) {
                   1067:         $args->{'no_nav_bar'} = 1;
                   1068:     }
                   1069:     my $start_page =
                   1070:         &Apache::loncommon::start_page($title,$js,$args);
1.3       www      1071: 
1.25      matthew  1072:     my $forminfo =<<"ENDFORMINFO";
1.216     raeburn  1073: <form action="/adm/createuser" method="post" name="$formname">
1.190     raeburn  1074: <input type="hidden" name="phase" value="update_user_data" />
1.188     raeburn  1075: <input type="hidden" name="ccuname" value="$ccuname" />
                   1076: <input type="hidden" name="ccdomain" value="$ccdomain" />
1.157     albertel 1077: <input type="hidden" name="pres_value"  value="" />
                   1078: <input type="hidden" name="pres_type"   value="" />
                   1079: <input type="hidden" name="pres_marker" value="" />
1.25      matthew  1080: ENDFORMINFO
1.329     raeburn  1081:     my (%inccourses,$roledom);
                   1082:     if ($context eq 'course') {
                   1083:         $inccourses{$env{'request.course.id'}}=1;
                   1084:         $roledom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1085:     } elsif ($context eq 'author') {
                   1086:         $roledom = $env{'request.role.domain'};
                   1087:     } elsif ($context eq 'domain') {
                   1088:         foreach my $key (keys(%env)) {
                   1089:             $roledom = $env{'request.role.domain'};
                   1090:             if ($key=~/^user\.priv\.cm\.\/($roledom)\/($match_username)/) {
                   1091:                 $inccourses{$1.'_'.$2}=1;
                   1092:             }
                   1093:         }
                   1094:     } else {
                   1095:         foreach my $key (keys(%env)) {
                   1096: 	    if ($key=~/^user\.priv\.cm\.\/($match_domain)\/($match_username)/) {
                   1097: 	        $inccourses{$1.'_'.$2}=1;
                   1098:             }
1.2       www      1099:         }
1.24      matthew  1100:     }
1.216     raeburn  1101:     if ($newuser) {
1.134     raeburn  1102:         my $portfolioform;
1.267     raeburn  1103:         if ((&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) ||
                   1104:             (&Apache::lonnet::allowed('mut',$env{'request.role.domain'}))) {
                   1105:             # Current user has quota or user tools modification privileges
1.188     raeburn  1106:             $portfolioform = '<br />'.&portfolio_quota($ccuname,$ccdomain);
1.134     raeburn  1107:         }
1.227     raeburn  1108:         &initialize_authen_forms($ccdomain,$formname);
1.188     raeburn  1109:         my %lt=&Apache::lonlocal::texthash(
                   1110:                 'cnu'            => 'Create New User',
1.213     raeburn  1111:                 'ast'            => 'as a student',
1.318     raeburn  1112:                 'ame'            => 'as a member',
1.188     raeburn  1113:                 'ind'            => 'in domain',
                   1114:                 'lg'             => 'Login Data',
1.190     raeburn  1115:                 'hs'             => "Home Server",
1.188     raeburn  1116:         );
1.185     raeburn  1117: 	$r->print(<<ENDTITLE);
1.110     albertel 1118: $start_page
1.160     raeburn  1119: $response
1.25      matthew  1120: $forminfo
1.31      matthew  1121: <script type="text/javascript" language="Javascript">
1.301     bisitz   1122: // <![CDATA[
1.20      harris41 1123: $loginscript
1.301     bisitz   1124: // ]]>
1.31      matthew  1125: </script>
1.20      harris41 1126: <input type='hidden' name='makeuser' value='1' />
1.216     raeburn  1127: <h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain
1.185     raeburn  1128: ENDTITLE
1.213     raeburn  1129:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1130:             if ($crstype eq 'Community') {
                   1131:                 $r->print(' ('.$lt{'ame'}.')');
                   1132:             } else {
                   1133:                 $r->print(' ('.$lt{'ast'}.')');
                   1134:             }
1.213     raeburn  1135:         }
                   1136:         $r->print('</h2>'."\n".'<div class="LC_left_float">');
1.206     raeburn  1137:         my $personal_table = 
1.210     raeburn  1138:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1139:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1140:         $r->print($personal_table);
1.187     raeburn  1141:         my ($home_server_pick,$numlib) = 
                   1142:             &Apache::loncommon::home_server_form_item($ccdomain,'hserver',
                   1143:                                                       'default','hide');
                   1144:         if ($numlib > 1) {
                   1145:             $r->print("
1.185     raeburn  1146: <br />
1.187     raeburn  1147: $lt{'hs'}: $home_server_pick
                   1148: <br />");
                   1149:         } else {
                   1150:             $r->print($home_server_pick);
                   1151:         }
1.304     raeburn  1152:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.318     raeburn  1153:             $r->print('<br /><h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.304     raeburn  1154:                       &Apache::loncommon::start_data_table().
                   1155:                       &build_tools_display($ccuname,$ccdomain,
                   1156:                                            'requestcourses').
                   1157:                       &Apache::loncommon::end_data_table());
                   1158:         }
1.188     raeburn  1159:         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   1160:                   $lt{'lg'}.'</h3>');
1.185     raeburn  1161:         my ($fixedauth,$varauth,$authmsg); 
1.193     raeburn  1162:         if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
                   1163:             my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
                   1164:             my ($rules,$ruleorder) = 
                   1165:                 &Apache::lonnet::inst_userrules($ccdomain,'username');
1.185     raeburn  1166:             if (ref($rules) eq 'HASH') {
1.193     raeburn  1167:                 if (ref($rules->{$matchedrule}) eq 'HASH') {
                   1168:                     my $authtype = $rules->{$matchedrule}{'authtype'};
1.185     raeburn  1169:                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
1.190     raeburn  1170:                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.275     raeburn  1171:                     } else { 
1.193     raeburn  1172:                         my $authparm = $rules->{$matchedrule}{'authparm'};
1.273     raeburn  1173:                         $authmsg = $rules->{$matchedrule}{'authmsg'};
1.185     raeburn  1174:                         if ($authtype =~ /^krb(4|5)$/) {
                   1175:                             my $ver = $1;
                   1176:                             if ($authparm ne '') {
                   1177:                                 $fixedauth = <<"KERB"; 
                   1178: <input type="hidden" name="login" value="krb" />
                   1179: <input type="hidden" name="krbver" value="$ver" />
                   1180: <input type="hidden" name="krbarg" value="$authparm" />
                   1181: KERB
                   1182:                             }
                   1183:                         } else {
                   1184:                             $fixedauth = 
                   1185: '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
1.193     raeburn  1186:                             if ($rules->{$matchedrule}{'authparmfixed'}) {
1.185     raeburn  1187:                                 $fixedauth .=    
                   1188: '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                   1189:                             } else {
1.273     raeburn  1190:                                 if ($authtype eq 'int') {
                   1191:                                     $varauth = '<br />'.
1.301     bisitz   1192: &mt('[_1] Internally authenticated (with initial password [_2])','','<input type="password" size="10" name="intarg" value="" />')."<label><input type=\"checkbox\" name=\"visible\" onclick='if (this.checked) { this.form.intarg.type=\"text\" } else { this.form.intarg.type=\"password\" }' />".&mt('Visible input').'</label>';
1.273     raeburn  1193:                                 } elsif ($authtype eq 'loc') {
                   1194:                                     $varauth = '<br />'.
                   1195: &mt('[_1] Local Authentication with argument [_2]','','<input type="text" name="'.$authtype.'arg" value="" />')."\n";
                   1196:                                 } else {
                   1197:                                     $varauth =
1.185     raeburn  1198: '<input type="text" name="'.$authtype.'arg" value="" />'."\n";
1.273     raeburn  1199:                                 }
1.185     raeburn  1200:                             }
                   1201:                         }
                   1202:                     }
                   1203:                 } else {
1.190     raeburn  1204:                     $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
1.185     raeburn  1205:                 }
                   1206:             }
                   1207:             if ($authmsg) {
                   1208:                 $r->print(<<ENDAUTH);
                   1209: $fixedauth
                   1210: $authmsg
                   1211: $varauth
                   1212: ENDAUTH
                   1213:             }
                   1214:         } else {
1.190     raeburn  1215:             $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc)); 
1.187     raeburn  1216:         }
1.215     raeburn  1217:         $r->print($portfolioform);
                   1218:         if ($env{'form.action'} eq 'singlestudent') {
                   1219:             $r->print(&date_sections_select($context,$newuser,$formname,
                   1220:                                             $permission));
                   1221:         }
                   1222:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.216     raeburn  1223:     } else { # user already exists
1.79      albertel 1224: 	my %lt=&Apache::lonlocal::texthash(
1.191     raeburn  1225:                     'cup'  => "Modify existing user: ",
1.213     raeburn  1226:                     'ens'  => "Enroll one student: ",
1.318     raeburn  1227:                     'enm'  => "Enroll one member: ",
1.72      sakharuk 1228:                     'id'   => "in domain",
                   1229: 				       );
1.26      matthew  1230: 	$r->print(<<ENDCHANGEUSER);
1.110     albertel 1231: $start_page
1.25      matthew  1232: $forminfo
1.213     raeburn  1233: <h2>
1.26      matthew  1234: ENDCHANGEUSER
1.213     raeburn  1235:         if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1236:             if ($crstype eq 'Community') {
                   1237:                 $r->print($lt{'enm'});
                   1238:             } else {
                   1239:                 $r->print($lt{'ens'});
                   1240:             }
1.213     raeburn  1241:         } else {
                   1242:             $r->print($lt{'cup'});
                   1243:         }
                   1244:         $r->print(' "'.$ccuname.'" '.$lt{'id'}.' "'.$ccdomain.'"</h2>'.
                   1245:                   "\n".'<div class="LC_left_float">');
1.206     raeburn  1246:         my ($personal_table,$showforceid) = 
1.210     raeburn  1247:             &personal_data_display($ccuname,$ccdomain,$newuser,$context,
                   1248:                                    $inst_results{$ccuname.':'.$ccdomain});
1.206     raeburn  1249:         $r->print($personal_table);
                   1250:         if ($showforceid) {
1.203     raeburn  1251:             $r->print(&Apache::lonuserutils::forceid_change($context));
1.199     raeburn  1252:         }
1.275     raeburn  1253:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
1.318     raeburn  1254:             $r->print('<h3>'.&mt('User Can Request Creation of Courses/Communities in this Domain?').'</h3>'.
1.300     raeburn  1255:                       &Apache::loncommon::start_data_table());
1.314     raeburn  1256:             if ($env{'request.role.domain'} eq $ccdomain) {
1.300     raeburn  1257:                 $r->print(&build_tools_display($ccuname,$ccdomain,'requestcourses'));
                   1258:             } else {
                   1259:                 $r->print(&coursereq_externaluser($ccuname,$ccdomain,
                   1260:                                                   $env{'request.role.domain'}));
                   1261:             }
                   1262:             $r->print(&Apache::loncommon::end_data_table());
1.275     raeburn  1263:         }
1.199     raeburn  1264:         $r->print('</div>');
1.227     raeburn  1265:         my $user_auth_text =  &user_authentication($ccuname,$ccdomain,$formname);
1.275     raeburn  1266:         my ($user_quota_text,$user_tools_text,$user_reqcrs_text);
1.267     raeburn  1267:         if ((&Apache::lonnet::allowed('mpq',$ccdomain)) ||
                   1268:             (&Apache::lonnet::allowed('mut',$ccdomain))) {
1.188     raeburn  1269:             # Current user has quota modification privileges
                   1270:             $user_quota_text = &portfolio_quota($ccuname,$ccdomain);
1.267     raeburn  1271:         }
                   1272:         if (!&Apache::lonnet::allowed('mpq',$ccdomain)) {
                   1273:             if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
                   1274:                 # Get the user's portfolio information
                   1275:                 my %portq = &Apache::lonnet::get('environment',['portfolioquota'],
                   1276:                                                  $ccdomain,$ccuname);
                   1277:                 my %lt=&Apache::lonlocal::texthash(
                   1278:                     'dska'  => "Disk space allocated to user's portfolio files",
                   1279:                     'youd'  => "You do not have privileges to modify the portfolio quota for this user.",
                   1280:                     'ichr'  => "If a change is required, contact a domain coordinator for the domain",
                   1281:                 );
                   1282:                 $user_quota_text = <<ENDNOPORTPRIV;
1.188     raeburn  1283: <h3>$lt{'dska'}</h3>
                   1284: $lt{'youd'} $lt{'ichr'}: $ccdomain
                   1285: ENDNOPORTPRIV
1.267     raeburn  1286:             }
                   1287:         }
                   1288:         if (!&Apache::lonnet::allowed('mut',$ccdomain)) {
                   1289:             if (&Apache::lonnet::allowed('mut',$env{'request.role.domain'})) {
                   1290:                 my %lt=&Apache::lonlocal::texthash(
                   1291:                     'utav'  => "User Tools Availability",
1.285     weissno  1292:                     'yodo'  => "You do not have privileges to modify Portfolio, Blog or Personal Information Page settings for this user.",
1.267     raeburn  1293:                     'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1294:                 );
                   1295:                 $user_tools_text = <<ENDNOTOOLSPRIV;
                   1296: <h3>$lt{'utav'}</h3>
                   1297: $lt{'yodo'} $lt{'ifch'}: $ccdomain
                   1298: ENDNOTOOLSPRIV
                   1299:             }
1.188     raeburn  1300:         }
                   1301:         if ($user_auth_text ne '') {
                   1302:             $r->print('<div class="LC_left_float">'.$user_auth_text);
                   1303:             if ($user_quota_text ne '') {
                   1304:                 $r->print($user_quota_text);
                   1305:             }
1.267     raeburn  1306:             if ($user_tools_text ne '') {
                   1307:                 $r->print($user_tools_text);
                   1308:             }
1.213     raeburn  1309:             if ($env{'form.action'} eq 'singlestudent') {
                   1310:                 $r->print(&date_sections_select($context,$newuser,$formname));
                   1311:             }
1.188     raeburn  1312:         } elsif ($user_quota_text ne '') {
1.213     raeburn  1313:             $r->print('<div class="LC_left_float">'.$user_quota_text);
1.267     raeburn  1314:             if ($user_tools_text ne '') {
                   1315:                 $r->print($user_tools_text);
                   1316:             }
                   1317:             if ($env{'form.action'} eq 'singlestudent') {
                   1318:                 $r->print(&date_sections_select($context,$newuser,$formname));
                   1319:             }
                   1320:         } elsif ($user_tools_text ne '') {
                   1321:             $r->print('<div class="LC_left_float">'.$user_tools_text);
1.213     raeburn  1322:             if ($env{'form.action'} eq 'singlestudent') {
                   1323:                 $r->print(&date_sections_select($context,$newuser,$formname));
                   1324:             }
                   1325:         } else {
                   1326:             if ($env{'form.action'} eq 'singlestudent') {
                   1327:                 $r->print('<div class="LC_left_float">'.
                   1328:                           &date_sections_select($context,$newuser,$formname));
                   1329:             }
1.188     raeburn  1330:         }
1.213     raeburn  1331:         $r->print('</div><div class="LC_clear_float_footer"></div>');
1.217     raeburn  1332:         if ($env{'form.action'} ne 'singlestudent') {
1.329     raeburn  1333:             &display_existing_roles($r,$ccuname,$ccdomain,\%inccourses,$context,
                   1334:                                     $roledom,$crstype);
1.217     raeburn  1335:         }
1.25      matthew  1336:     } ## End of new user/old user logic
1.218     raeburn  1337:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1338:         my $btntxt;
                   1339:         if ($crstype eq 'Community') {
                   1340:             $btntxt = &mt('Enroll Member');
                   1341:         } else {
                   1342:             $btntxt = &mt('Enroll Student');
                   1343:         }
                   1344:         $r->print('<br /><input type="button" value="'.$btntxt.'" onclick="setSections(this.form)" />'."\n");
1.218     raeburn  1345:     } else {
                   1346:         $r->print('<h3>'.&mt('Add Roles').'</h3>');
                   1347:         my $addrolesdisplay = 0;
                   1348:         if ($context eq 'domain' || $context eq 'author') {
                   1349:             $addrolesdisplay = &new_coauthor_roles($r,$ccuname,$ccdomain);
                   1350:         }
                   1351:         if ($context eq 'domain') {
                   1352:             my $add_domainroles = &new_domain_roles($r);
                   1353:             if (!$addrolesdisplay) {
                   1354:                 $addrolesdisplay = $add_domainroles;
1.2       www      1355:             }
1.218     raeburn  1356:             $r->print(&course_level_dc($env{'request.role.domain'},'Course'));
1.301     bisitz   1357:             $r->print('<br /><input type="button" value="'.&mt('Save').'" onclick="setCourse()" />'."\n");
1.218     raeburn  1358:         } elsif ($context eq 'author') {
                   1359:             if ($addrolesdisplay) {
1.262     schafran 1360:                 $r->print('<br /><input type="button" value="'.&mt('Save').'"');
1.218     raeburn  1361:                 if ($newuser) {
1.301     bisitz   1362:                     $r->print(' onclick="auth_check()" \>'."\n");
1.218     raeburn  1363:                 } else {
1.301     bisitz   1364:                     $r->print('onclick="this.form.submit()" \>'."\n");
1.218     raeburn  1365:                 }
1.188     raeburn  1366:             } else {
1.218     raeburn  1367:                 $r->print('<br /><a href="javascript:backPage(document.cu)">'.
                   1368:                           &mt('Back to previous page').'</a>');
1.188     raeburn  1369:             }
                   1370:         } else {
1.218     raeburn  1371:             $r->print(&course_level_table(%inccourses));
1.301     bisitz   1372:             $r->print('<br /><input type="button" value="'.&mt('Save').'" onclick="setSections(this.form)" />'."\n");
1.188     raeburn  1373:         }
1.88      raeburn  1374:     }
1.188     raeburn  1375:     $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate','ccuname','ccdomain']));
1.179     raeburn  1376:     $r->print('<input type="hidden" name="currstate" value="" />');
1.329.2.5  raeburn  1377:     $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" /></form>');
1.218     raeburn  1378:     return;
1.2       www      1379: }
1.1       www      1380: 
1.213     raeburn  1381: sub singleuser_breadcrumb {
1.318     raeburn  1382:     my ($crstype) = @_;
1.213     raeburn  1383:     my %breadcrumb_text;
                   1384:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  1385:         if ($crstype eq 'Community') {
                   1386:             $breadcrumb_text{'search'} = 'Enroll a member';
                   1387:         } else {
                   1388:             $breadcrumb_text{'search'} = 'Enroll a student';
                   1389:         }
1.213     raeburn  1390:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1391:         $breadcrumb_text{'modify'} = 'Set section/dates',
                   1392:     } else {
1.229     raeburn  1393:         $breadcrumb_text{'search'} = 'Create/modify a user';
1.213     raeburn  1394:         $breadcrumb_text{'userpicked'} = 'Select a user',
                   1395:         $breadcrumb_text{'modify'} = 'Set user role',
                   1396:     }
                   1397:     return %breadcrumb_text;
                   1398: }
                   1399: 
                   1400: sub date_sections_select {
                   1401:     my ($context,$newuser,$formname,$permission) = @_;
                   1402:     my $cid = $env{'request.course.id'};
                   1403:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity($cid);
                   1404:     my $date_table = '<h3>'.&mt('Starting and Ending Dates').'</h3>'."\n".
                   1405:         &Apache::lonuserutils::date_setting_table(undef,undef,$context,
                   1406:                                                   undef,$formname,$permission);
                   1407:     my $rowtitle = 'Section';
                   1408:     my $secbox = '<h3>'.&mt('Section').'</h3>'."\n".
                   1409:         &Apache::lonuserutils::section_picker($cdom,$cnum,'st',$rowtitle,
                   1410:                                               $permission);
                   1411:     my $output = $date_table.$secbox;
                   1412:     return $output;
                   1413: }
                   1414: 
1.216     raeburn  1415: sub validation_javascript {
                   1416:     my ($context,$ccdomain,$pjump_def,$groupslist,$newuser,$formname,
                   1417:         $loaditem) = @_;
                   1418:     my $dc_setcourse_code = '';
                   1419:     my $nondc_setsection_code = '';
                   1420:     if ($context eq 'domain') {
                   1421:         my $dcdom = $env{'request.role.domain'};
                   1422:         $loaditem->{'onload'} = "document.cu.coursedesc.value='';";
1.227     raeburn  1423:         $dc_setcourse_code = 
                   1424:             &Apache::lonuserutils::dc_setcourse_js('cu','singleuser',$context);
1.216     raeburn  1425:     } else {
1.227     raeburn  1426:         my $checkauth; 
                   1427:         if (($newuser) || (&Apache::lonnet::allowed('mau',$ccdomain))) {
                   1428:             $checkauth = 1;
                   1429:         }
                   1430:         if ($context eq 'course') {
                   1431:             $nondc_setsection_code =
                   1432:                 &Apache::lonuserutils::setsections_javascript($formname,$groupslist,
                   1433:                                                               undef,$checkauth);
                   1434:         }
                   1435:         if ($checkauth) {
                   1436:             $nondc_setsection_code .= 
                   1437:                 &Apache::lonuserutils::verify_authen($formname,$context);
                   1438:         }
1.216     raeburn  1439:     }
                   1440:     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                   1441:                                    $nondc_setsection_code,$groupslist);
                   1442:     my ($jsback,$elements) = &crumb_utilities();
                   1443:     $js .= "\n".
1.301     bisitz   1444:            '<script type="text/javascript">'."\n".
                   1445:            '// <![CDATA['."\n".
                   1446:            $jsback."\n".
                   1447:            '// ]]>'."\n".
                   1448:            '</script>'."\n";
1.216     raeburn  1449:     return $js;
                   1450: }
                   1451: 
1.217     raeburn  1452: sub display_existing_roles {
1.329     raeburn  1453:     my ($r,$ccuname,$ccdomain,$inccourses,$context,$roledom,$crstype) = @_;
                   1454:     my $now=time;
                   1455:     my %lt=&Apache::lonlocal::texthash(
1.217     raeburn  1456:                     'rer'  => "Existing Roles",
                   1457:                     'rev'  => "Revoke",
                   1458:                     'del'  => "Delete",
                   1459:                     'ren'  => "Re-Enable",
                   1460:                     'rol'  => "Role",
                   1461:                     'ext'  => "Extent",
                   1462:                     'sta'  => "Start",
                   1463:                     'end'  => "End",
                   1464:                                        );
1.329     raeburn  1465:     my (%rolesdump,%roletext,%sortrole,%roleclass,%rolepriv);
                   1466:     if ($context eq 'course' || $context eq 'author') {
                   1467:         my @roles = &Apache::lonuserutils::roles_by_context($context,1,$crstype);
                   1468:         my %roleshash = 
                   1469:             &Apache::lonnet::get_my_roles($ccuname,$ccdomain,'userroles',
                   1470:                               ['active','previous','future'],\@roles,$roledom,1);
                   1471:         foreach my $key (keys(%roleshash)) {
                   1472:             my ($start,$end) = split(':',$roleshash{$key});
                   1473:             next if ($start eq '-1' || $end eq '-1');
                   1474:             my ($rnum,$rdom,$role,$sec) = split(':',$key);
                   1475:             if ($context eq 'course') {
                   1476:                 next unless (($rnum eq $env{'course.'.$env{'request.course.id'}.'.num'})
                   1477:                              && ($rdom eq $env{'course.'.$env{'request.course.id'}.'.domain'}));
                   1478:             } elsif ($context eq 'author') {
                   1479:                 next unless (($rnum eq $env{'user.name'}) && ($rdom eq $env{'request.role.domain'}));
                   1480:             }
                   1481:             my ($newkey,$newvalue,$newrole);
                   1482:             $newkey = '/'.$rdom.'/'.$rnum;
                   1483:             if ($sec ne '') {
                   1484:                 $newkey .= '/'.$sec;
                   1485:             }
                   1486:             $newvalue = $role;
                   1487:             if ($role =~ /^cr/) {
                   1488:                 $newrole = 'cr';
                   1489:             } else {
                   1490:                 $newrole = $role;
                   1491:             }
                   1492:             $newkey .= '_'.$newrole;
                   1493:             if ($start ne '' && $end ne '') {
                   1494:                 $newvalue .= '_'.$end.'_'.$start;
                   1495:             }
                   1496:             $rolesdump{$newkey} = $newvalue;
                   1497:         }
                   1498:     } else {
                   1499:         %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
                   1500:     }
                   1501:     # Build up table of user roles to allow revocation and re-enabling of roles.
                   1502:     my ($tmp) = keys(%rolesdump);
                   1503:     return if ($tmp =~ /^(con_lost|error)/i);
                   1504:     foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                   1505:                                 my $b1=join('_',(split('_',$b))[1,0]);
                   1506:                                 return $a1 cmp $b1;
                   1507:                             } keys(%rolesdump)) {
                   1508:         next if ($area =~ /^rolesdef/);
                   1509:         my $envkey=$area;
                   1510:         my $role = $rolesdump{$area};
                   1511:         my $thisrole=$area;
                   1512:         $area =~ s/\_\w\w$//;
                   1513:         my ($role_code,$role_end_time,$role_start_time) =
                   1514:             split(/_/,$role);
1.217     raeburn  1515: # Is this a custom role? Get role owner and title.
1.329     raeburn  1516:         my ($croleudom,$croleuname,$croletitle)=
                   1517:             ($role_code=~m{^cr/($match_domain)/($match_username)/(\w+)$});
                   1518:         my $allowed=0;
                   1519:         my $delallowed=0;
                   1520:         my $sortkey=$role_code;
                   1521:         my $class='Unknown';
                   1522:         if ($area =~ m{^/($match_domain)/($match_courseid)} ) {
                   1523:             $class='Course';
                   1524:             my ($coursedom,$coursedir) = ($1,$2);
                   1525:             my $cid = $1.'_'.$2;
                   1526:             # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
                   1527:             my %coursedata=
                   1528:                 &Apache::lonnet::coursedescription($cid);
                   1529:             if ($coursedir =~ /^$match_community$/) {
                   1530:                 $class='Community';
                   1531:             }
                   1532:             $sortkey.="\0$coursedom";
                   1533:             my $carea;
                   1534:             if (defined($coursedata{'description'})) {
                   1535:                 $carea=$coursedata{'description'}.
                   1536:                     '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
                   1537:     &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$coursedir,$coursedom);
                   1538:                 $sortkey.="\0".$coursedata{'description'};
                   1539:             } else {
                   1540:                 if ($class eq 'Community') {
                   1541:                     $carea=&mt('Unavailable community').': '.$area;
                   1542:                     $sortkey.="\0".&mt('Unavailable community').': '.$area;
1.217     raeburn  1543:                 } else {
                   1544:                     $carea=&mt('Unavailable course').': '.$area;
                   1545:                     $sortkey.="\0".&mt('Unavailable course').': '.$area;
                   1546:                 }
1.329     raeburn  1547:             }
                   1548:             $sortkey.="\0$coursedir";
                   1549:             $inccourses->{$cid}=1;
                   1550:             if ((&Apache::lonnet::allowed('c'.$role_code,$coursedom.'/'.$coursedir)) ||
                   1551:                 (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1552:                 $allowed=1;
                   1553:             }
                   1554:             unless ($allowed) {
                   1555:                 my $isowner = &is_courseowner($cid,$coursedata{'internal.courseowner'});
                   1556:                 if ($isowner) {
                   1557:                     if (($role_code eq 'co') && ($class eq 'Community')) {
                   1558:                         $allowed = 1;
                   1559:                     } elsif (($role_code eq 'cc') && ($class eq 'Course')) {
                   1560:                         $allowed = 1;
                   1561:                     }
1.217     raeburn  1562:                 }
1.329     raeburn  1563:             } 
                   1564:             if ((&Apache::lonnet::allowed('dro',$coursedom)) ||
                   1565:                 (&Apache::lonnet::allowed('dro',$ccdomain))) {
                   1566:                 $delallowed=1;
                   1567:             }
1.217     raeburn  1568: # - custom role. Needs more info, too
1.329     raeburn  1569:             if ($croletitle) {
                   1570:                 if (&Apache::lonnet::allowed('ccr',$coursedom.'/'.$coursedir)) {
                   1571:                     $allowed=1;
                   1572:                     $thisrole.='.'.$role_code;
1.217     raeburn  1573:                 }
1.329     raeburn  1574:             }
                   1575:             if ($area=~m{^/($match_domain)/($match_courseid)/(\w+)}) {
                   1576:                 $carea.='<br />Section: '.$3;
                   1577:                 $sortkey.="\0$3";
                   1578:                 if (!$allowed) {
                   1579:                     if ($env{'request.course.sec'} eq $3) {
                   1580:                         if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2.'/'.$3)) {
                   1581:                             $allowed = 1;
1.217     raeburn  1582:                         }
                   1583:                     }
                   1584:                 }
1.329     raeburn  1585:             }
                   1586:             $area=$carea;
                   1587:         } else {
                   1588:             $sortkey.="\0".$area;
                   1589:             # Determine if current user is able to revoke privileges
                   1590:             if ($area=~m{^/($match_domain)/}) {
                   1591:                 if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                   1592:                    (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
                   1593:                    $allowed=1;
1.217     raeburn  1594:                 }
1.329     raeburn  1595:                 if (((&Apache::lonnet::allowed('dro',$1))  ||
                   1596:                     (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                   1597:                     ($role_code ne 'dc')) {
                   1598:                     $delallowed=1;
1.217     raeburn  1599:                 }
1.329     raeburn  1600:             } else {
                   1601:                 if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
1.217     raeburn  1602:                     $allowed=1;
                   1603:                 }
                   1604:             }
1.329     raeburn  1605:             if ($role_code eq 'ca' || $role_code eq 'au') {
                   1606:                 $class='Construction Space';
                   1607:             } elsif ($role_code eq 'su') {
                   1608:                 $class='System';
1.217     raeburn  1609:             } else {
1.329     raeburn  1610:                 $class='Domain';
1.217     raeburn  1611:             }
1.329     raeburn  1612:         }
                   1613:         if (($role_code eq 'ca') || ($role_code eq 'aa')) {
                   1614:             $area=~m{/($match_domain)/($match_username)};
                   1615:             if (&Apache::lonuserutils::authorpriv($2,$1)) {
                   1616:                 $allowed=1;
1.217     raeburn  1617:             } else {
1.329     raeburn  1618:                 $allowed=0;
1.217     raeburn  1619:             }
1.329     raeburn  1620:         }
                   1621:         my $row = '';
                   1622:         $row.= '<td>';
                   1623:         my $active=1;
                   1624:         $active=0 if (($role_end_time) && ($now>$role_end_time));
                   1625:         if (($active) && ($allowed)) {
                   1626:             $row.= '<input type="checkbox" name="rev:'.$thisrole.'" />';
                   1627:         } else {
                   1628:             if ($active) {
                   1629:                $row.='&nbsp;';
1.217     raeburn  1630:             } else {
1.329     raeburn  1631:                $row.=&mt('expired or revoked');
1.217     raeburn  1632:             }
1.329     raeburn  1633:         }
                   1634:         $row.='</td><td>';
                   1635:         if ($allowed && !$active) {
                   1636:             $row.= '<input type="checkbox" name="ren:'.$thisrole.'" />';
                   1637:         } else {
                   1638:             $row.='&nbsp;';
                   1639:         }
                   1640:         $row.='</td><td>';
                   1641:         if ($delallowed) {
                   1642:             $row.= '<input type="checkbox" name="del:'.$thisrole.'" />';
                   1643:         } else {
                   1644:             $row.='&nbsp;';
                   1645:         }
                   1646:         my $plaintext='';
                   1647:         if (!$croletitle) {
                   1648:             $plaintext=&Apache::lonnet::plaintext($role_code,$class)
                   1649:         } else {
                   1650:             $plaintext=
1.217     raeburn  1651:         "Customrole '$croletitle'<br />defined by $croleuname\@$croleudom";
1.329     raeburn  1652:         }
                   1653:         $row.= '</td><td>'.$plaintext.
                   1654:                '</td><td>'.$area.
                   1655:                '</td><td>'.($role_start_time?&Apache::lonlocal::locallocaltime($role_start_time)
                   1656:                                             : '&nbsp;' ).
                   1657:                '</td><td>'.($role_end_time  ?&Apache::lonlocal::locallocaltime($role_end_time)
                   1658:                                             : '&nbsp;' )
                   1659:                ."</td>";
                   1660:         $sortrole{$sortkey}=$envkey;
                   1661:         $roletext{$envkey}=$row;
                   1662:         $roleclass{$envkey}=$class;
                   1663:         $rolepriv{$envkey}=$allowed;
                   1664:     } # end of foreach        (table building loop)
                   1665: 
                   1666:     my $rolesdisplay = 0;
                   1667:     my %output = ();
                   1668:     foreach my $type ('Construction Space','Course','Community','Domain','System','Unknown') {
                   1669:         $output{$type} = '';
                   1670:         foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                   1671:             if ( ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/ ) && ($rolepriv{$sortrole{$which}}) ) {
                   1672:                  $output{$type}.=
                   1673:                       &Apache::loncommon::start_data_table_row().
                   1674:                       $roletext{$sortrole{$which}}.
                   1675:                       &Apache::loncommon::end_data_table_row();
1.217     raeburn  1676:             }
1.329     raeburn  1677:         }
                   1678:         unless($output{$type} eq '') {
                   1679:             $output{$type} = '<tr class="LC_info_row">'.
                   1680:                       "<td align='center' colspan='7'>".&mt($type)."</td></tr>".
                   1681:                       $output{$type};
                   1682:             $rolesdisplay = 1;
                   1683:         }
                   1684:     }
                   1685:     if ($rolesdisplay == 1) {
                   1686:         my $contextrole='';
                   1687:         if ($env{'request.course.id'}) {
                   1688:             if (&Apache::loncommon::course_type() eq 'Community') {
                   1689:                 $contextrole = &mt('Existing Roles in this Community');
1.290     bisitz   1690:             } else {
1.329     raeburn  1691:                 $contextrole = &mt('Existing Roles in this Course');
1.290     bisitz   1692:             }
1.329     raeburn  1693:         } elsif ($env{'request.role'} =~ /^au\./) {
                   1694:             $contextrole = &mt('Existing Co-Author Roles in your Construction Space');
                   1695:         } else {
                   1696:             $contextrole = &mt('Existing Roles in this Domain');
                   1697:         }
                   1698:         $r->print('
1.217     raeburn  1699: <h3>'.$lt{'rer'}.'</h3>'.
1.329     raeburn  1700: '<div>'.$contextrole.'</div>'.
1.217     raeburn  1701: &Apache::loncommon::start_data_table("LC_createuser").
                   1702: &Apache::loncommon::start_data_table_header_row().
                   1703: '<th>'.$lt{'rev'}.'</th><th>'.$lt{'ren'}.'</th><th>'.$lt{'del'}.
                   1704: '</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.
                   1705: '</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   1706: &Apache::loncommon::end_data_table_header_row());
1.329     raeburn  1707:         foreach my $type ('Construction Space','Course','Community','Domain','System','Unknown') {
                   1708:             if ($output{$type}) {
                   1709:                 $r->print($output{$type}."\n");
1.217     raeburn  1710:             }
                   1711:         }
1.329     raeburn  1712:         $r->print(&Apache::loncommon::end_data_table());
                   1713:     }
1.217     raeburn  1714:     return;
                   1715: }
                   1716: 
1.218     raeburn  1717: sub new_coauthor_roles {
                   1718:     my ($r,$ccuname,$ccdomain) = @_;
                   1719:     my $addrolesdisplay = 0;
                   1720:     #
                   1721:     # Co-Author
                   1722:     #
                   1723:     if (&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1724:                                           $env{'request.role.domain'}) &&
                   1725:         ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
                   1726:         # No sense in assigning co-author role to yourself
                   1727:         $addrolesdisplay = 1;
                   1728:         my $cuname=$env{'user.name'};
                   1729:         my $cudom=$env{'request.role.domain'};
                   1730:         my %lt=&Apache::lonlocal::texthash(
                   1731:                     'cs'   => "Construction Space",
                   1732:                     'act'  => "Activate",
                   1733:                     'rol'  => "Role",
                   1734:                     'ext'  => "Extent",
                   1735:                     'sta'  => "Start",
                   1736:                     'end'  => "End",
                   1737:                     'cau'  => "Co-Author",
                   1738:                     'caa'  => "Assistant Co-Author",
                   1739:                     'ssd'  => "Set Start Date",
                   1740:                     'sed'  => "Set End Date"
                   1741:                                        );
                   1742:         $r->print('<h4>'.$lt{'cs'}.'</h4>'."\n".
                   1743:                   &Apache::loncommon::start_data_table()."\n".
                   1744:                   &Apache::loncommon::start_data_table_header_row()."\n".
                   1745:                   '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th>'.
                   1746:                   '<th>'.$lt{'ext'}.'</th><th>'.$lt{'sta'}.'</th>'.
                   1747:                   '<th>'.$lt{'end'}.'</th>'."\n".
                   1748:                   &Apache::loncommon::end_data_table_header_row()."\n".
                   1749:                   &Apache::loncommon::start_data_table_row().'
                   1750:            <td>
1.291     bisitz   1751:             <input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_ca" />
1.218     raeburn  1752:            </td>
                   1753:            <td>'.$lt{'cau'}.'</td>
                   1754:            <td>'.$cudom.'_'.$cuname.'</td>
                   1755:            <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1756:              <a href=
                   1757: "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>
                   1758: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_ca" value="" />
                   1759: <a href=
                   1760: "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".
                   1761:               &Apache::loncommon::end_data_table_row()."\n".
                   1762:               &Apache::loncommon::start_data_table_row()."\n".
1.291     bisitz   1763: '<td><input type="checkbox" name="act_'.$cudom.'_'.$cuname.'_aa" /></td>
1.218     raeburn  1764: <td>'.$lt{'caa'}.'</td>
                   1765: <td>'.$cudom.'_'.$cuname.'</td>
                   1766: <td><input type="hidden" name="start_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1767: <a href=
                   1768: "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>
                   1769: <td><input type="hidden" name="end_'.$cudom.'_'.$cuname.'_aa" value="" />
                   1770: <a href=
                   1771: "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".
                   1772:              &Apache::loncommon::end_data_table_row()."\n".
                   1773:              &Apache::loncommon::end_data_table());
                   1774:     } elsif ($env{'request.role'} =~ /^au\./) {
                   1775:         if (!(&Apache::lonuserutils::authorpriv($env{'user.name'},
                   1776:                                                 $env{'request.role.domain'}))) {
                   1777:             $r->print('<span class="LC_error">'.
                   1778:                       &mt('You do not have privileges to assign co-author roles.').
                   1779:                       '</span>');
                   1780:         } elsif (($env{'user.name'} eq $ccuname) &&
                   1781:              ($env{'user.domain'} eq $ccdomain)) {
                   1782:             $r->print(&mt('Assigning yourself a co-author or assistant co-author role in your own author area in Construction Space is not permitted'));
                   1783:         }
                   1784:     }
                   1785:     return $addrolesdisplay;;
                   1786: }
                   1787: 
                   1788: sub new_domain_roles {
                   1789:     my ($r) = @_;
                   1790:     my $addrolesdisplay = 0;
                   1791:     #
                   1792:     # Domain level
                   1793:     #
                   1794:     my $num_domain_level = 0;
                   1795:     my $domaintext =
                   1796:     '<h4>'.&mt('Domain Level').'</h4>'.
                   1797:     &Apache::loncommon::start_data_table().
                   1798:     &Apache::loncommon::start_data_table_header_row().
                   1799:     '<th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.
                   1800:     &mt('Extent').'</th>'.
                   1801:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th>'.
                   1802:     &Apache::loncommon::end_data_table_header_row();
1.312     raeburn  1803:     my @allroles = &Apache::lonuserutils::roles_by_context('domain');
1.218     raeburn  1804:     foreach my $thisdomain (sort(&Apache::lonnet::all_domains())) {
1.312     raeburn  1805:         foreach my $role (@allroles) {
                   1806:             next if ($role eq 'ad');
1.218     raeburn  1807:             if (&Apache::lonnet::allowed('c'.$role,$thisdomain)) {
                   1808:                my $plrole=&Apache::lonnet::plaintext($role);
                   1809:                my %lt=&Apache::lonlocal::texthash(
                   1810:                     'ssd'  => "Set Start Date",
                   1811:                     'sed'  => "Set End Date"
                   1812:                                        );
                   1813:                $num_domain_level ++;
                   1814:                $domaintext .=
                   1815: &Apache::loncommon::start_data_table_row().
1.291     bisitz   1816: '<td><input type="checkbox" name="act_'.$thisdomain.'_'.$role.'" /></td>
1.218     raeburn  1817: <td>'.$plrole.'</td>
                   1818: <td>'.$thisdomain.'</td>
                   1819: <td><input type="hidden" name="start_'.$thisdomain.'_'.$role.'" value="" />
                   1820: <a href=
                   1821: "javascript:pjump('."'date_start','Start Date $plrole',document.cu.start_$thisdomain\_$role.value,'start_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'ssd'}.'</a></td>
                   1822: <td><input type="hidden" name="end_'.$thisdomain.'_'.$role.'" value="" />
                   1823: <a href=
                   1824: "javascript:pjump('."'date_end','End Date $plrole',document.cu.end_$thisdomain\_$role.value,'end_$thisdomain\_$role','cu.pres','dateset'".')">'.$lt{'sed'}.'</a></td>'.
                   1825: &Apache::loncommon::end_data_table_row();
                   1826:             }
                   1827:         }
                   1828:     }
                   1829:     $domaintext.= &Apache::loncommon::end_data_table();
                   1830:     if ($num_domain_level > 0) {
                   1831:         $r->print($domaintext);
                   1832:         $addrolesdisplay = 1;
                   1833:     }
                   1834:     return $addrolesdisplay;
                   1835: }
                   1836: 
1.188     raeburn  1837: sub user_authentication {
1.227     raeburn  1838:     my ($ccuname,$ccdomain,$formname) = @_;
1.188     raeburn  1839:     my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.227     raeburn  1840:     my $outcome;
1.188     raeburn  1841:     # Check for a bad authentication type
                   1842:     if ($currentauth !~ /^(krb4|krb5|unix|internal|localauth):/) {
                   1843:         # bad authentication scheme
                   1844:         my %lt=&Apache::lonlocal::texthash(
                   1845:                        'err'   => "ERROR",
                   1846:                        'uuas'  => "This user has an unrecognized authentication scheme",
                   1847:                        'adcs'  => "Please alert a domain coordinator of this situation",
                   1848:                        'sldb'  => "Please specify login data below",
                   1849:                        'ld'    => "Login Data"
                   1850:         );
                   1851:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
1.227     raeburn  1852:             &initialize_authen_forms($ccdomain,$formname);
                   1853: 
1.190     raeburn  1854:             my $choices = &Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc);
1.188     raeburn  1855:             $outcome = <<ENDBADAUTH;
                   1856: <script type="text/javascript" language="Javascript">
1.301     bisitz   1857: // <![CDATA[
1.188     raeburn  1858: $loginscript
1.301     bisitz   1859: // ]]>
1.188     raeburn  1860: </script>
                   1861: <span class="LC_error">$lt{'err'}:
                   1862: $lt{'uuas'} ($currentauth). $lt{'sldb'}.</span>
                   1863: <h3>$lt{'ld'}</h3>
                   1864: $choices
                   1865: ENDBADAUTH
                   1866:         } else {
                   1867:             # This user is not allowed to modify the user's
                   1868:             # authentication scheme, so just notify them of the problem
                   1869:             $outcome = <<ENDBADAUTH;
                   1870: <span class="LC_error"> $lt{'err'}: 
                   1871: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
                   1872: </span>
                   1873: ENDBADAUTH
                   1874:         }
                   1875:     } else { # Authentication type is valid
1.227     raeburn  1876:         &initialize_authen_forms($ccdomain,$formname,$currentauth,'modifyuser');
1.205     raeburn  1877:         my ($authformcurrent,$can_modify,@authform_others) =
1.188     raeburn  1878:             &modify_login_block($ccdomain,$currentauth);
                   1879:         if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                   1880:             # Current user has login modification privileges
                   1881:             my %lt=&Apache::lonlocal::texthash (
                   1882:                            'ld'    => "Login Data",
                   1883:                            'ccld'  => "Change Current Login Data",
                   1884:                            'enld'  => "Enter New Login Data"
                   1885:                                                );
                   1886:             $outcome =
                   1887:                        '<script type="text/javascript" language="Javascript">'."\n".
1.301     bisitz   1888:                        '// <![CDATA['."\n".
1.188     raeburn  1889:                        $loginscript."\n".
1.301     bisitz   1890:                        '// ]]>'."\n".
1.188     raeburn  1891:                        '</script>'."\n".
                   1892:                        '<h3>'.$lt{'ld'}.'</h3>'.
                   1893:                        &Apache::loncommon::start_data_table().
1.205     raeburn  1894:                        &Apache::loncommon::start_data_table_row().
1.188     raeburn  1895:                        '<td>'.$authformnop;
                   1896:             if ($can_modify) {
                   1897:                 $outcome .= '</td>'."\n".
                   1898:                             &Apache::loncommon::end_data_table_row().
                   1899:                             &Apache::loncommon::start_data_table_row().
                   1900:                             '<td>'.$authformcurrent.'</td>'.
                   1901:                             &Apache::loncommon::end_data_table_row()."\n";
                   1902:             } else {
1.200     raeburn  1903:                 $outcome .= '&nbsp;('.$authformcurrent.')</td>'.
                   1904:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  1905:             }
1.205     raeburn  1906:             foreach my $item (@authform_others) { 
                   1907:                 $outcome .= &Apache::loncommon::start_data_table_row().
                   1908:                             '<td>'.$item.'</td>'.
                   1909:                             &Apache::loncommon::end_data_table_row()."\n";
1.188     raeburn  1910:             }
1.205     raeburn  1911:             $outcome .= &Apache::loncommon::end_data_table();
1.188     raeburn  1912:         } else {
                   1913:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   1914:                 my %lt=&Apache::lonlocal::texthash(
                   1915:                            'ccld'  => "Change Current Login Data",
                   1916:                            'yodo'  => "You do not have privileges to modify the authentication configuration for this user.",
                   1917:                            'ifch'  => "If a change is required, contact a domain coordinator for the domain",
                   1918:                 );
                   1919:                 $outcome .= <<ENDNOPRIV;
                   1920: <h3>$lt{'ccld'}</h3>
                   1921: $lt{'yodo'} $lt{'ifch'}: $ccdomain
1.235     raeburn  1922: <input type="hidden" name="login" value="nochange" />
1.188     raeburn  1923: ENDNOPRIV
                   1924:             }
                   1925:         }
                   1926:     }  ## End of "check for bad authentication type" logic
                   1927:     return $outcome;
                   1928: }
                   1929: 
1.187     raeburn  1930: sub modify_login_block {
                   1931:     my ($dom,$currentauth) = @_;
                   1932:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   1933:     my ($authnum,%can_assign) =
                   1934:         &Apache::loncommon::get_assignable_auth($dom);
1.205     raeburn  1935:     my ($authformcurrent,@authform_others,$show_override_msg);
1.187     raeburn  1936:     if ($currentauth=~/^krb(4|5):/) {
                   1937:         $authformcurrent=$authformkrb;
                   1938:         if ($can_assign{'int'}) {
1.205     raeburn  1939:             push(@authform_others,$authformint);
1.187     raeburn  1940:         }
                   1941:         if ($can_assign{'loc'}) {
1.205     raeburn  1942:             push(@authform_others,$authformloc);
1.187     raeburn  1943:         }
                   1944:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   1945:             $show_override_msg = 1;
                   1946:         }
                   1947:     } elsif ($currentauth=~/^internal:/) {
                   1948:         $authformcurrent=$authformint;
                   1949:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1950:             push(@authform_others,$authformkrb);
1.187     raeburn  1951:         }
                   1952:         if ($can_assign{'loc'}) {
1.205     raeburn  1953:             push(@authform_others,$authformloc);
1.187     raeburn  1954:         }
                   1955:         if ($can_assign{'int'}) {
                   1956:             $show_override_msg = 1;
                   1957:         }
                   1958:     } elsif ($currentauth=~/^unix:/) {
                   1959:         $authformcurrent=$authformfsys;
                   1960:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1961:             push(@authform_others,$authformkrb);
1.187     raeburn  1962:         }
                   1963:         if ($can_assign{'int'}) {
1.205     raeburn  1964:             push(@authform_others,$authformint);
1.187     raeburn  1965:         }
                   1966:         if ($can_assign{'loc'}) {
1.205     raeburn  1967:             push(@authform_others,$authformloc);
1.187     raeburn  1968:         }
                   1969:         if ($can_assign{'fsys'}) {
                   1970:             $show_override_msg = 1;
                   1971:         }
                   1972:     } elsif ($currentauth=~/^localauth:/) {
                   1973:         $authformcurrent=$authformloc;
                   1974:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
1.205     raeburn  1975:             push(@authform_others,$authformkrb);
1.187     raeburn  1976:         }
                   1977:         if ($can_assign{'int'}) {
1.205     raeburn  1978:             push(@authform_others,$authformint);
1.187     raeburn  1979:         }
                   1980:         if ($can_assign{'loc'}) {
                   1981:             $show_override_msg = 1;
                   1982:         }
                   1983:     }
                   1984:     if ($show_override_msg) {
1.205     raeburn  1985:         $authformcurrent = '<table><tr><td colspan="3">'.$authformcurrent.
                   1986:                            '</td></tr>'."\n".
                   1987:                            '<tr><td>&nbsp;&nbsp;&nbsp;</td>'.
                   1988:                            '<td><b>'.&mt('Currently in use').'</b></td>'.
                   1989:                            '<td align="right"><span class="LC_cusr_emph">'.
1.187     raeburn  1990:                             &mt('will override current values').
1.205     raeburn  1991:                             '</span></td></tr></table>';
1.187     raeburn  1992:     }
1.205     raeburn  1993:     return ($authformcurrent,$show_override_msg,@authform_others); 
1.187     raeburn  1994: }
                   1995: 
1.188     raeburn  1996: sub personal_data_display {
1.252     raeburn  1997:     my ($ccuname,$ccdomain,$newuser,$context,$inst_results,$rolesarray) = @_;
1.286     raeburn  1998:     my ($output,$showforceid,%userenv,%canmodify,%canmodify_status);
1.219     raeburn  1999:     my @userinfo = ('firstname','middlename','lastname','generation',
                   2000:                     'permanentemail','id');
1.252     raeburn  2001:     my $rowcount = 0;
                   2002:     my $editable = 0;
1.286     raeburn  2003:     %canmodify_status = 
                   2004:         &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
                   2005:                                                    ['inststatus'],$rolesarray);
1.253     raeburn  2006:     if (!$newuser) {
1.188     raeburn  2007:         # Get the users information
                   2008:         %userenv = &Apache::lonnet::get('environment',
                   2009:                    ['firstname','middlename','lastname','generation',
1.286     raeburn  2010:                     'permanentemail','id','inststatus'],$ccdomain,$ccuname);
1.219     raeburn  2011:         %canmodify =
                   2012:             &Apache::lonuserutils::can_modify_userinfo($context,$ccdomain,
1.252     raeburn  2013:                                                        \@userinfo,$rolesarray);
1.257     raeburn  2014:     } elsif ($context eq 'selfcreate') {
                   2015:         %canmodify = &selfcreate_canmodify($context,$ccdomain,\@userinfo,
                   2016:                                            $inst_results,$rolesarray);
1.188     raeburn  2017:     }
                   2018:     my %lt=&Apache::lonlocal::texthash(
                   2019:                 'pd'             => "Personal Data",
                   2020:                 'firstname'      => "First Name",
                   2021:                 'middlename'     => "Middle Name",
                   2022:                 'lastname'       => "Last Name",
                   2023:                 'generation'     => "Generation",
                   2024:                 'permanentemail' => "Permanent e-mail address",
1.259     bisitz   2025:                 'id'             => "Student/Employee ID",
1.286     raeburn  2026:                 'lg'             => "Login Data",
                   2027:                 'inststatus'     => "Affiliation",
1.188     raeburn  2028:     );
                   2029:     my %textboxsize = (
                   2030:                        firstname      => '15',
                   2031:                        middlename     => '15',
                   2032:                        lastname       => '15',
                   2033:                        generation     => '5',
                   2034:                        permanentemail => '25',
                   2035:                        id             => '15',
                   2036:                       );
                   2037:     my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                   2038:     $output = '<h3>'.$lt{'pd'}.'</h3>'.
                   2039:               &Apache::lonhtmlcommon::start_pick_box();
                   2040:     foreach my $item (@userinfo) {
                   2041:         my $rowtitle = $lt{$item};
1.252     raeburn  2042:         my $hiderow = 0;
1.188     raeburn  2043:         if ($item eq 'generation') {
                   2044:             $rowtitle = $genhelp.$rowtitle;
                   2045:         }
1.252     raeburn  2046:         my $row = &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
1.188     raeburn  2047:         if ($newuser) {
1.210     raeburn  2048:             if (ref($inst_results) eq 'HASH') {
                   2049:                 if ($inst_results->{$item} ne '') {
1.252     raeburn  2050:                     $row .= '<input type="hidden" name="c'.$item.'" value="'.$inst_results->{$item}.'" />'.$inst_results->{$item};
1.210     raeburn  2051:                 } else {
1.252     raeburn  2052:                     if ($context eq 'selfcreate') {
                   2053:                         if ($canmodify{$item}) { 
                   2054:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2055:                             $editable ++;
                   2056:                         } else {
                   2057:                             $hiderow = 1;
                   2058:                         }
1.253     raeburn  2059:                     } else {
                   2060:                         $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2061:                     }
1.210     raeburn  2062:                 }
1.188     raeburn  2063:             } else {
1.252     raeburn  2064:                 if ($context eq 'selfcreate') {
1.287     raeburn  2065:                     if (($item eq 'permanentemail') && ($newuser eq 'email')) {
                   2066:                         $row .= $ccuname;
1.252     raeburn  2067:                     } else {
1.287     raeburn  2068:                         if ($canmodify{$item}) {
                   2069:                             $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                   2070:                             $editable ++;
                   2071:                         } else {
                   2072:                             $hiderow = 1;
                   2073:                         }
1.252     raeburn  2074:                     }
1.253     raeburn  2075:                 } else {
                   2076:                     $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
1.252     raeburn  2077:                 }
1.188     raeburn  2078:             }
                   2079:         } else {
1.219     raeburn  2080:             if ($canmodify{$item}) {
1.252     raeburn  2081:                 $row .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="'.$userenv{$item}.'" />';
1.188     raeburn  2082:             } else {
1.252     raeburn  2083:                 $row .= $userenv{$item};
1.188     raeburn  2084:             }
1.206     raeburn  2085:             if ($item eq 'id') {
1.219     raeburn  2086:                 $showforceid = $canmodify{$item};
                   2087:             }
1.188     raeburn  2088:         }
1.252     raeburn  2089:         $row .= &Apache::lonhtmlcommon::row_closure(1);
                   2090:         if (!$hiderow) {
                   2091:             $output .= $row;
                   2092:             $rowcount ++;
                   2093:         }
1.188     raeburn  2094:     }
1.286     raeburn  2095:     if (($canmodify_status{'inststatus'}) || ($context ne 'selfcreate')) {
                   2096:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($ccdomain);
                   2097:         if (ref($types) eq 'ARRAY') {
                   2098:             if (@{$types} > 0) {
                   2099:                 my ($hiderow,$shown);
                   2100:                 if ($canmodify_status{'inststatus'}) {
                   2101:                     $shown = &pick_inst_statuses($userenv{'inststatus'},$usertypes,$types);
                   2102:                 } else {
                   2103:                     $shown .= $userenv{'inststatus'};
                   2104:                     if ($userenv{'inststatus'} eq '') {
                   2105:                         $hiderow = 1;
                   2106:                     }
                   2107:                 }
                   2108:                 if (!$hiderow) {
                   2109:                     my $row = &Apache::lonhtmlcommon::row_title(&mt('Affliations'),undef,'LC_oddrow_value')."\n".
                   2110:                               $shown.&Apache::lonhtmlcommon::row_closure(1); 
                   2111:                     if ($context eq 'selfcreate') {
                   2112:                         $rowcount ++;
                   2113:                     }
                   2114:                     $output .= $row;
                   2115:                 }
                   2116:             }
                   2117:         }
                   2118:     }
1.188     raeburn  2119:     $output .= &Apache::lonhtmlcommon::end_pick_box();
1.206     raeburn  2120:     if (wantarray) {
1.252     raeburn  2121:         if ($context eq 'selfcreate') {
                   2122:             return($output,$rowcount,$editable);
                   2123:         } else {
                   2124:             return ($output,$showforceid);
                   2125:         }
1.206     raeburn  2126:     } else {
                   2127:         return $output;
                   2128:     }
1.188     raeburn  2129: }
                   2130: 
1.286     raeburn  2131: sub pick_inst_statuses {
                   2132:     my ($curr,$usertypes,$types) = @_;
                   2133:     my ($output,$rem,@currtypes);
                   2134:     if ($curr ne '') {
                   2135:         @currtypes = map { &unescape($_); } split(/:/,$curr);
                   2136:     }
                   2137:     my $numinrow = 2;
                   2138:     if (ref($types) eq 'ARRAY') {
                   2139:         $output = '<table>';
                   2140:         my $lastcolspan; 
                   2141:         for (my $i=0; $i<@{$types}; $i++) {
                   2142:             if (defined($usertypes->{$types->[$i]})) {
                   2143:                 my $rem = $i%($numinrow);
                   2144:                 if ($rem == 0) {
                   2145:                     if ($i<@{$types}-1) {
                   2146:                         if ($i > 0) { 
                   2147:                             $output .= '</tr>';
                   2148:                         }
                   2149:                         $output .= '<tr>';
                   2150:                     }
                   2151:                 } elsif ($i==@{$types}-1) {
                   2152:                     my $colsleft = $numinrow - $rem;
                   2153:                     if ($colsleft > 1) {
                   2154:                         $lastcolspan = ' colspan="'.$colsleft.'"';
                   2155:                     }
                   2156:                 }
                   2157:                 my $check = ' ';
                   2158:                 if (grep(/^\Q$types->[$i]\E$/,@currtypes)) {
                   2159:                     $check = ' checked="checked" ';
                   2160:                 }
                   2161:                 $output .= '<td class="LC_left_item"'.$lastcolspan.'>'.
                   2162:                            '<span class="LC_nobreak"><label>'.
                   2163:                            '<input type="checkbox" name="inststatus" '.
                   2164:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   2165:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   2166:             }
                   2167:         }
                   2168:         $output .= '</tr></table>';
                   2169:     }
                   2170:     return $output;
                   2171: }
                   2172: 
1.257     raeburn  2173: sub selfcreate_canmodify {
                   2174:     my ($context,$dom,$userinfo,$inst_results,$rolesarray) = @_;
                   2175:     if (ref($inst_results) eq 'HASH') {
                   2176:         my @inststatuses = &get_inststatuses($inst_results);
                   2177:         if (@inststatuses == 0) {
                   2178:             @inststatuses = ('default');
                   2179:         }
                   2180:         $rolesarray = \@inststatuses;
                   2181:     }
                   2182:     my %canmodify =
                   2183:         &Apache::lonuserutils::can_modify_userinfo($context,$dom,$userinfo,
                   2184:                                                    $rolesarray);
                   2185:     return %canmodify;
                   2186: }
                   2187: 
1.252     raeburn  2188: sub get_inststatuses {
                   2189:     my ($insthashref) = @_;
                   2190:     my @inststatuses = ();
                   2191:     if (ref($insthashref) eq 'HASH') {
                   2192:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
                   2193:             @inststatuses = @{$insthashref->{'inststatus'}};
                   2194:         }
                   2195:     }
                   2196:     return @inststatuses;
                   2197: }
                   2198: 
1.4       www      2199: # ================================================================= Phase Three
1.42      matthew  2200: sub update_user_data {
1.329.2.5  raeburn  2201:     my ($r,$context,$crstype,$brcrum) = @_;
1.329.2.4  raeburn  2202:     my $is_custom = &Apache::loncommon::needs_gci_custom();
1.101     albertel 2203:     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                   2204:                                           $env{'form.ccdomain'});
1.27      matthew  2205:     # Error messages
1.188     raeburn  2206:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
1.193     raeburn  2207:     my $end       = '</span><br /><br />';
                   2208:     my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
1.188     raeburn  2209:                     "'$env{'form.prevphase'}','modify')".'" />'.
1.219     raeburn  2210:                     &mt('Return to previous page').'</a>'.
                   2211:                     &Apache::loncommon::end_page();
                   2212:     my $now = time;
1.40      www      2213:     my $title;
1.101     albertel 2214:     if (exists($env{'form.makeuser'})) {
1.40      www      2215: 	$title='Set Privileges for New User';
                   2216:     } else {
                   2217:         $title='Modify User Privileges';
                   2218:     }
1.213     raeburn  2219:     my $newuser = 0;
1.160     raeburn  2220:     my ($jsback,$elements) = &crumb_utilities();
                   2221:     my $jscript = '<script type="text/javascript">'."\n".
1.301     bisitz   2222:                   '// <![CDATA['."\n".
                   2223:                   $jsback."\n".
                   2224:                   '// ]]>'."\n".
                   2225:                   '</script>'."\n";
1.318     raeburn  2226:     my %breadcrumb_text = &singleuser_breadcrumb($crstype);
1.224     raeburn  2227:     my $helpitem = 'Course_Change_Privileges';
                   2228:     if ($env{'form.action'} eq 'singlestudent') {
                   2229:         $helpitem = 'Course_Add_Student';
                   2230:     }
1.329.2.5  raeburn  2231:     my $bread_crumbs_component = 'User Management';
1.329.2.3  raeburn  2232:     if ($context eq 'course') {
1.329.2.4  raeburn  2233:         if ($is_custom) {
1.329.2.5  raeburn  2234:             $bread_crumbs_component = 'Enrollment and Student Activity';
1.329.2.3  raeburn  2235:         }
                   2236:     }
1.329.2.5  raeburn  2237:     push(@{$brcrum}, 
                   2238:             {href => "javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')",
                   2239:              text => $breadcrumb_text{'modify'},
                   2240:              faq  => 282,
                   2241:              bug  => 'Instructor Interface',},
                   2242:             {href => "/adm/createuser",
                   2243:              text => "Result",
                   2244:              faq  => 282,
                   2245:              bug  => 'Instructor Interface',
                   2246:              help => $helpitem});
                   2247:     my $args = {bread_crumbs           => $brcrum,
                   2248:                 bread_crumbs_component => $bread_crumbs_component};
                   2249:     if ($env{'form.popup'}) {
                   2250:         $args->{'no_nav_bar'} = 1;
                   2251:     }
                   2252:     $r->print(&Apache::loncommon::start_page($title,$jscript,$args));
                   2253: 
1.188     raeburn  2254:     $r->print(&update_result_form($uhome));
1.27      matthew  2255:     # Check Inputs
1.101     albertel 2256:     if (! $env{'form.ccuname'} ) {
1.193     raeburn  2257: 	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
1.27      matthew  2258: 	return;
                   2259:     }
1.138     albertel 2260:     if (  $env{'form.ccuname'} ne 
                   2261: 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
1.281     bisitz   2262: 	$r->print($error.&mt('Invalid login name.').'  '.
                   2263: 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid.').
1.193     raeburn  2264: 		  $end.$rtnlink);
1.27      matthew  2265: 	return;
                   2266:     }
1.101     albertel 2267:     if (! $env{'form.ccdomain'}       ) {
1.193     raeburn  2268: 	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
1.27      matthew  2269: 	return;
                   2270:     }
1.138     albertel 2271:     if (  $env{'form.ccdomain'} ne
                   2272: 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
1.281     bisitz   2273: 	$r->print($error.&mt('Invalid domain name.').'  '.
                   2274: 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid.').
1.193     raeburn  2275: 		  $end.$rtnlink);
1.27      matthew  2276: 	return;
                   2277:     }
1.219     raeburn  2278:     if ($uhome eq 'no_host') {
                   2279:         $newuser = 1;
                   2280:     }
1.101     albertel 2281:     if (! exists($env{'form.makeuser'})) {
1.29      matthew  2282:         # Modifying an existing user, so check the validity of the name
                   2283:         if ($uhome eq 'no_host') {
1.73      sakharuk 2284:             $r->print($error.&mt('Unable to determine home server for ').
1.101     albertel 2285:                       $env{'form.ccuname'}.&mt(' in domain ').
                   2286:                       $env{'form.ccdomain'}.'.');
1.29      matthew  2287:             return;
                   2288:         }
                   2289:     }
1.27      matthew  2290:     # Determine authentication method and password for the user being modified
                   2291:     my $amode='';
                   2292:     my $genpwd='';
1.101     albertel 2293:     if ($env{'form.login'} eq 'krb') {
1.41      albertel 2294: 	$amode='krb';
1.101     albertel 2295: 	$amode.=$env{'form.krbver'};
                   2296: 	$genpwd=$env{'form.krbarg'};
                   2297:     } elsif ($env{'form.login'} eq 'int') {
1.27      matthew  2298: 	$amode='internal';
1.101     albertel 2299: 	$genpwd=$env{'form.intarg'};
                   2300:     } elsif ($env{'form.login'} eq 'fsys') {
1.27      matthew  2301: 	$amode='unix';
1.101     albertel 2302: 	$genpwd=$env{'form.fsysarg'};
                   2303:     } elsif ($env{'form.login'} eq 'loc') {
1.27      matthew  2304: 	$amode='localauth';
1.101     albertel 2305: 	$genpwd=$env{'form.locarg'};
1.27      matthew  2306: 	$genpwd=" " if (!$genpwd);
1.101     albertel 2307:     } elsif (($env{'form.login'} eq 'nochange') ||
                   2308:              ($env{'form.login'} eq ''        )) { 
1.34      matthew  2309:         # There is no need to tell the user we did not change what they
                   2310:         # did not ask us to change.
1.35      matthew  2311:         # If they are creating a new user but have not specified login
                   2312:         # information this will be caught below.
1.30      matthew  2313:     } else {
1.193     raeburn  2314: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.30      matthew  2315: 	    return;
1.27      matthew  2316:     }
1.164     albertel 2317: 
1.188     raeburn  2318:     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
                   2319: 			 $env{'form.ccuname'}, $env{'form.ccdomain'}).'</h3>');
1.193     raeburn  2320:     my (%alerts,%rulematch,%inst_results,%curr_rules);
1.267     raeburn  2321:     my @usertools = ('aboutme','blog','portfolio');
1.299     raeburn  2322:     my @requestcourses = ('official','unofficial','community');
1.286     raeburn  2323:     my ($othertitle,$usertypes,$types) = 
                   2324:         &Apache::loncommon::sorted_inst_types($env{'form.ccdomain'});
1.101     albertel 2325:     if ($env{'form.makeuser'}) {
1.164     albertel 2326: 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
1.27      matthew  2327:         # Check for the authentication mode and password
                   2328:         if (! $amode || ! $genpwd) {
1.193     raeburn  2329: 	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
1.27      matthew  2330: 	    return;
1.18      albertel 2331: 	}
1.29      matthew  2332:         # Determine desired host
1.101     albertel 2333:         my $desiredhost = $env{'form.hserver'};
1.29      matthew  2334:         if (lc($desiredhost) eq 'default') {
                   2335:             $desiredhost = undef;
                   2336:         } else {
1.147     albertel 2337:             my %home_servers = 
                   2338: 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
1.29      matthew  2339:             if (! exists($home_servers{$desiredhost})) {
1.193     raeburn  2340:                 $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
                   2341:                 return;
                   2342:             }
                   2343:         }
                   2344:         # Check ID format
                   2345:         my %checkhash;
                   2346:         my %checks = ('id' => 1);
                   2347:         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
1.219     raeburn  2348:             'newuser' => $newuser, 
1.196     raeburn  2349:             'id' => $env{'form.cid'},
1.193     raeburn  2350:         );
1.196     raeburn  2351:         if ($env{'form.cid'} ne '') {
                   2352:             &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   2353:                                           \%rulematch,\%inst_results,\%curr_rules);
                   2354:             if (ref($alerts{'id'}) eq 'HASH') {
                   2355:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
                   2356:                     my $domdesc =
                   2357:                         &Apache::lonnet::domain($env{'form.ccdomain'},'description');
                   2358:                     if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
                   2359:                         my $userchkmsg;
                   2360:                         if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
                   2361:                             $userchkmsg  = 
                   2362:                                 &Apache::loncommon::instrule_disallow_msg('id',
                   2363:                                                                     $domdesc,1).
                   2364:                                 &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
                   2365:                                     $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
                   2366:                         }
                   2367:                         $r->print($error.&mt('Invalid ID format').$end.
                   2368:                                   $userchkmsg.$rtnlink);
                   2369:                         return;
                   2370:                     }
                   2371:                 }
1.29      matthew  2372:             }
                   2373:         }
1.27      matthew  2374: 	# Call modifyuser
                   2375: 	my $result = &Apache::lonnet::modifyuser
1.193     raeburn  2376: 	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
1.188     raeburn  2377:              $amode,$genpwd,$env{'form.cfirstname'},
                   2378:              $env{'form.cmiddlename'},$env{'form.clastname'},
                   2379:              $env{'form.cgeneration'},undef,$desiredhost,
                   2380:              $env{'form.cpermanentemail'});
1.77      www      2381: 	$r->print(&mt('Generating user').': '.$result);
1.219     raeburn  2382:         $uhome = &Apache::lonnet::homeserver($env{'form.ccuname'},
1.101     albertel 2383:                                                $env{'form.ccdomain'});
1.267     raeburn  2384:         my (%changeHash,%newcustom,%changed);
                   2385:         if ($uhome ne 'no_host') {
                   2386:             if ($env{'form.customquota'} == 1) {
                   2387:                 if ($env{'form.portfolioquota'} eq '') {
                   2388:                     $newcustom{'quota'} = 0;
                   2389:                 } else {
                   2390:                     $newcustom{'quota'} = $env{'form.portfolioquota'};
                   2391:                     $newcustom{'quota'} =~ s/[^\d\.]//g;
                   2392:                 }
                   2393:                 $changed{'quota'} = &quota_admin($newcustom{'quota'},\%changeHash);
                   2394:             }
                   2395:             foreach my $item (@usertools) {
                   2396:                 if ($env{'form.custom'.$item} == 1) {
                   2397:                     $newcustom{$item} = $env{'form.tools_'.$item};
1.275     raeburn  2398:                     $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2399:                                                  \%changeHash,'tools');
                   2400:                 }
                   2401:             }
1.279     raeburn  2402:             foreach my $item (@requestcourses) {
1.306     raeburn  2403:                 $newcustom{$item} = $env{'form.crsreq_'.$item};
                   2404:                 if ($env{'form.crsreq_'.$item} eq 'autolimit') {
                   2405:                     $newcustom{$item} .= '=';
                   2406:                     unless ($env{'form.crsreq_'.$item.'_limit'} =~ /\D/) {
                   2407:                         $newcustom{$item} .= $env{'form.crsreq_'.$item.'_limit'};
                   2408:                     }
                   2409:                 }
1.279     raeburn  2410:                 $changed{$item} = &tool_admin($item,$newcustom{$item},
                   2411:                                               \%changeHash,'requestcourses');
1.232     raeburn  2412:             }
1.286     raeburn  2413:             if (exists($env{'form.inststatus'})) {
                   2414:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2415:                 if (@inststatuses > 0) {
                   2416:                     $changeHash{'inststatus'} = join(',',@inststatuses);
                   2417:                     $changed{'inststatus'} = $changeHash{'inststatus'};
                   2418:                 }
                   2419:             }
1.267     raeburn  2420:             if (keys(%changed)) {
1.232     raeburn  2421:                 $changeHash{'firstname'}  = $env{'form.cfirstname'};
                   2422:                 $changeHash{'middlename'} = $env{'form.cmiddlename'};
                   2423:                 $changeHash{'lastname'}   = $env{'form.clastname'};
                   2424:                 $changeHash{'generation'} = $env{'form.cgeneration'};
                   2425:                 $changeHash{'id'}         = $env{'form.cid'};
                   2426:                 $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
1.267     raeburn  2427:                 my $chgresult =
                   2428:                      &Apache::lonnet::put('environment',\%changeHash,
                   2429:                                           $env{'form.ccdomain'},$env{'form.ccuname'});
                   2430:             } 
1.232     raeburn  2431:         }
1.219     raeburn  2432:         $r->print('<br />'.&mt('Home server').': '.$uhome.' '.
                   2433:                   &Apache::lonnet::hostname($uhome));
1.101     albertel 2434:     } elsif (($env{'form.login'} ne 'nochange') &&
                   2435:              ($env{'form.login'} ne ''        )) {
1.27      matthew  2436: 	# Modify user privileges
                   2437:         if (! $amode || ! $genpwd) {
1.193     raeburn  2438: 	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
1.27      matthew  2439: 	    return;
1.20      harris41 2440: 	}
1.27      matthew  2441: 	# Only allow authentification modification if the person has authority
1.101     albertel 2442: 	if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
1.20      harris41 2443: 	    $r->print('Modifying authentication: '.
1.31      matthew  2444:                       &Apache::lonnet::modifyuserauth(
1.101     albertel 2445: 		       $env{'form.ccdomain'},$env{'form.ccuname'},
1.21      harris41 2446:                        $amode,$genpwd));
1.102     albertel 2447:             $r->print('<br />'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.101     albertel 2448: 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
1.4       www      2449: 	} else {
1.27      matthew  2450: 	    # Okay, this is a non-fatal error.
1.193     raeburn  2451: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
1.27      matthew  2452: 	}
1.28      matthew  2453:     }
                   2454:     ##
1.318     raeburn  2455:     my (@userroles,%userupdate,$cnum,$cdom,$crstype,$namechanged);
1.213     raeburn  2456:     if ($context eq 'course') {
                   2457:         ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  2458:         $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
1.213     raeburn  2459:     }
1.101     albertel 2460:     if (! $env{'form.makeuser'} ) {
1.28      matthew  2461:         # Check for need to change
                   2462:         my %userenv = &Apache::lonnet::get
1.134     raeburn  2463:             ('environment',['firstname','middlename','lastname','generation',
1.267     raeburn  2464:              'id','permanentemail','portfolioquota','inststatus','tools.aboutme',
1.279     raeburn  2465:              'tools.blog','tools.portfolio','requestcourses.official',
1.300     raeburn  2466:              'requestcourses.unofficial','requestcourses.community',
                   2467:              'reqcrsotherdom.official','reqcrsotherdom.unofficial',
                   2468:              'reqcrsotherdom.community'],
1.160     raeburn  2469:               $env{'form.ccdomain'},$env{'form.ccuname'});
1.28      matthew  2470:         my ($tmp) = keys(%userenv);
                   2471:         if ($tmp =~ /^(con_lost|error)/i) { 
                   2472:             %userenv = ();
                   2473:         }
1.206     raeburn  2474:         my $no_forceid_alert;
                   2475:         # Check to see if user information can be changed
                   2476:         my %domconfig =
                   2477:             &Apache::lonnet::get_dom('configuration',['usermodification'],
                   2478:                                      $env{'form.ccdomain'});
1.213     raeburn  2479:         my @statuses = ('active','future');
                   2480:         my %roles = &Apache::lonnet::get_my_roles($env{'form.ccuname'},$env{'form.ccdomain'},'userroles',\@statuses,undef,$env{'request.role.domain'});
                   2481:         my ($auname,$audom);
1.220     raeburn  2482:         if ($context eq 'author') {
1.206     raeburn  2483:             $auname = $env{'user.name'};
                   2484:             $audom = $env{'user.domain'};     
                   2485:         }
                   2486:         foreach my $item (keys(%roles)) {
1.220     raeburn  2487:             my ($rolenum,$roledom,$role) = split(/:/,$item,-1);
1.206     raeburn  2488:             if ($context eq 'course') {
                   2489:                 if ($cnum ne '' && $cdom ne '') {
                   2490:                     if ($rolenum eq $cnum && $roledom eq $cdom) {
                   2491:                         if (!grep(/^\Q$role\E$/,@userroles)) {
                   2492:                             push(@userroles,$role);
                   2493:                         }
                   2494:                     }
                   2495:                 }
                   2496:             } elsif ($context eq 'author') {
                   2497:                 if ($rolenum eq $auname && $roledom eq $audom) {
                   2498:                     if (!grep(/^\Q$role\E$/,@userroles)) { 
                   2499:                         push(@userroles,$role);
                   2500:                     }
                   2501:                 }
                   2502:             }
                   2503:         }
1.220     raeburn  2504:         if ($env{'form.action'} eq 'singlestudent') {
                   2505:             if (!grep(/^st$/,@userroles)) {
                   2506:                 push(@userroles,'st');
                   2507:             }
                   2508:         } else {
                   2509:             # Check for course or co-author roles being activated or re-enabled
                   2510:             if ($context eq 'author' || $context eq 'course') {
                   2511:                 foreach my $key (keys(%env)) {
                   2512:                     if ($context eq 'author') {
                   2513:                         if ($key=~/^form\.act_\Q$audom\E_\Q$auname\E_([^_]+)/) {
                   2514:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2515:                                 push(@userroles,$1);
                   2516:                             }
                   2517:                         } elsif ($key =~/^form\.ren\:\Q$audom\E\/\Q$auname\E_([^_]+)/) {
                   2518:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2519:                                 push(@userroles,$1);
                   2520:                             }
1.206     raeburn  2521:                         }
1.220     raeburn  2522:                     } elsif ($context eq 'course') {
                   2523:                         if ($key=~/^form\.act_\Q$cdom\E_\Q$cnum\E_([^_]+)/) {
                   2524:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2525:                                 push(@userroles,$1);
                   2526:                             }
                   2527:                         } elsif ($key =~/^form\.ren\:\Q$cdom\E\/\Q$cnum\E(\/?\w*)_([^_]+)/) {
                   2528:                             if (!grep(/^\Q$1\E$/,@userroles)) {
                   2529:                                 push(@userroles,$1);
                   2530:                             }
1.206     raeburn  2531:                         }
                   2532:                     }
                   2533:                 }
                   2534:             }
                   2535:         }
                   2536:         #Check to see if we can change personal data for the user 
                   2537:         my (@mod_disallowed,@longroles);
                   2538:         foreach my $role (@userroles) {
                   2539:             if ($role eq 'cr') {
                   2540:                 push(@longroles,'Custom');
                   2541:             } else {
1.318     raeburn  2542:                 push(@longroles,&Apache::lonnet::plaintext($role,$crstype)); 
1.206     raeburn  2543:             }
                   2544:         }
1.219     raeburn  2545:         my @userinfo = ('firstname','middlename','lastname','generation','permanentemail','id');
                   2546:         my %canmodify = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},\@userinfo,\@userroles);
                   2547:         foreach my $item (@userinfo) {
1.28      matthew  2548:             # Strip leading and trailing whitespace
1.203     raeburn  2549:             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g;
1.219     raeburn  2550:             if (!$canmodify{$item}) {
1.207     raeburn  2551:                 if (defined($env{'form.c'.$item})) {
                   2552:                     if ($env{'form.c'.$item} ne $userenv{$item}) {
                   2553:                         push(@mod_disallowed,$item);
                   2554:                     }
1.206     raeburn  2555:                 }
                   2556:                 $env{'form.c'.$item} = $userenv{$item};
                   2557:             }
1.28      matthew  2558:         }
1.259     bisitz   2559:         # Check to see if we can change the Student/Employee ID
1.196     raeburn  2560:         my $forceid = $env{'form.forceid'};
                   2561:         my $recurseid = $env{'form.recurseid'};
                   2562:         my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
1.203     raeburn  2563:         my %uidhash = &Apache::lonnet::idrget($env{'form.ccdomain'},
                   2564:                                             $env{'form.ccuname'});
                   2565:         if (($uidhash{$env{'form.ccuname'}}) && 
                   2566:             ($uidhash{$env{'form.ccuname'}}!~/error\:/) && 
                   2567:             (!$forceid)) {
                   2568:             if ($env{'form.cid'} ne $uidhash{$env{'form.ccuname'}}) {
                   2569:                 $env{'form.cid'} = $userenv{'id'};
1.293     bisitz   2570:                 $no_forceid_alert = &mt('New student/employee ID does not match existing ID for this user.')
1.259     bisitz   2571:                                    .'<br />'
                   2572:                                    .&mt("Change is not permitted without checking the 'Force ID change' checkbox on the previous page.")
                   2573:                                    .'<br />'."\n";
1.203     raeburn  2574:             }
                   2575:         }
                   2576:         if ($env{'form.cid'} ne $userenv{'id'}) {
1.196     raeburn  2577:             my $checkhash;
                   2578:             my $checks = { 'id' => 1 };
                   2579:             $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
                   2580:                    { 'newuser' => $newuser,
                   2581:                      'id'  => $env{'form.cid'}, 
                   2582:                    };
                   2583:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   2584:                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
                   2585:             if (ref($alerts{'id'}) eq 'HASH') {
                   2586:                 if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
1.203     raeburn  2587:                    $env{'form.cid'} = $userenv{'id'};
1.196     raeburn  2588:                 }
                   2589:             }
                   2590:         }
1.286     raeburn  2591:         my ($quotachanged,$oldportfolioquota,$newportfolioquota,$oldinststatus,
                   2592:             $inststatus,$newinststatus,$oldisdefault,$newisdefault,$olddefquotatext,
                   2593:             $newdefquotatext,%oldaccess,%oldaccesstext,%newaccess,%newaccesstext,
                   2594:             $oldinststatuses,$newinststatuses);
1.149     raeburn  2595:         my ($defquota,$settingstatus) = 
                   2596:             &Apache::loncommon::default_quota($env{'form.ccdomain'},$inststatus);
1.300     raeburn  2597:         my ($showquota,$showtools,$showrequestcourses,$showinststatus,$showreqotherdom);
1.220     raeburn  2598:         if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   2599:             $showquota = 1;
                   2600:         }
1.267     raeburn  2601:         if (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   2602:             $showtools = 1;
                   2603:         }
1.275     raeburn  2604:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   2605:             $showrequestcourses = 1;
1.300     raeburn  2606:         } elsif (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   2607:             $showreqotherdom = 1;
1.275     raeburn  2608:         }
1.286     raeburn  2609:         if (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'})) {
                   2610:             $showinststatus = 1;
                   2611:         }
1.267     raeburn  2612:         my (%changeHash,%changed);
1.286     raeburn  2613:         $oldinststatus = $userenv{'inststatus'};
                   2614:         if ($oldinststatus eq '') {
                   2615:             $oldinststatuses = $othertitle; 
                   2616:         } else {
                   2617:             if (ref($usertypes) eq 'HASH') {
                   2618:                 $oldinststatuses = join(', ',map{ $usertypes->{ &unescape($_) }; } (split(/:/,$userenv{'inststatus'})));
                   2619:             } else {
                   2620:                 $oldinststatuses = join(', ',map{ &unescape($_); } (split(/:/,$userenv{'inststatus'})));
                   2621:             }
                   2622:         }
                   2623:         $changeHash{'inststatus'} = $userenv{'inststatus'};
                   2624:         my %canmodify_inststatus = &Apache::lonuserutils::can_modify_userinfo($context,$env{'form.ccdomain'},['inststatus'],\@userroles);
                   2625:         if ($canmodify_inststatus{'inststatus'}) {
                   2626:             if (exists($env{'form.inststatus'})) {
                   2627:                 my @inststatuses = &Apache::loncommon::get_env_multiple('form.inststatus');
                   2628:                 if (@inststatuses > 0) {
                   2629:                     $newinststatus = join(':',map { &escape($_); } @inststatuses);
                   2630:                     $changeHash{'inststatus'} = $newinststatus;
                   2631:                     if ($newinststatus ne $oldinststatus) {
                   2632:                         $changed{'inststatus'} = $newinststatus;
                   2633:                     }
                   2634:                     if (ref($usertypes) eq 'HASH') {
                   2635:                         $newinststatuses = join(', ',map{ $usertypes->{$_}; } (@inststatuses)); 
                   2636:                     } else {
                   2637:                         $newinststatuses = join(', ',map{ $usertypes->{$_}; } (@inststatuses));
                   2638:                     }
                   2639:                 } else {
                   2640:                     $newinststatus = '';
                   2641:                     $changeHash{'inststatus'} = $newinststatus;
                   2642:                     $newinststatuses = $othertitle;
                   2643:                     if ($newinststatus ne $oldinststatus) {
                   2644:                         $changed{'inststatus'} = $changeHash{'inststatus'};
                   2645:                     }
                   2646:                 }
                   2647:             }
                   2648:         }
1.204     raeburn  2649:         $changeHash{'portfolioquota'} = $userenv{'portfolioquota'};
1.149     raeburn  2650:         if ($userenv{'portfolioquota'} ne '') {
1.134     raeburn  2651:             $oldportfolioquota = $userenv{'portfolioquota'};
1.149     raeburn  2652:             if ($env{'form.customquota'} == 1) {
                   2653:                 if ($env{'form.portfolioquota'} eq '') {
                   2654:                     $newportfolioquota = 0;
                   2655:                 } else {
                   2656:                     $newportfolioquota = $env{'form.portfolioquota'};
                   2657:                     $newportfolioquota =~ s/[^\d\.]//g;
                   2658:                 }
1.204     raeburn  2659:                 if ($newportfolioquota != $oldportfolioquota) {
1.267     raeburn  2660:                     $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
1.134     raeburn  2661:                 }
1.149     raeburn  2662:             } else {
1.267     raeburn  2663:                 $changed{'quota'} = &quota_admin('',\%changeHash);
1.149     raeburn  2664:                 $newportfolioquota = $defquota;
1.284     bisitz   2665:                 $newisdefault = 1;
1.134     raeburn  2666:             }
                   2667:         } else {
1.204     raeburn  2668:             $oldisdefault = 1;
1.149     raeburn  2669:             $oldportfolioquota = $defquota;
                   2670:             if ($env{'form.customquota'} == 1) {
                   2671:                 if ($env{'form.portfolioquota'} eq '') {
                   2672:                     $newportfolioquota = 0;
                   2673:                 } else {
                   2674:                     $newportfolioquota = $env{'form.portfolioquota'};
                   2675:                     $newportfolioquota =~ s/[^\d\.]//g;
                   2676:                 }
1.267     raeburn  2677:                 $changed{'quota'} = &quota_admin($newportfolioquota,\%changeHash);
1.149     raeburn  2678:             } else {
                   2679:                 $newportfolioquota = $defquota;
1.204     raeburn  2680:                 $newisdefault = 1;
1.149     raeburn  2681:             }
                   2682:         }
1.204     raeburn  2683:         if ($oldisdefault) {
                   2684:             $olddefquotatext = &get_defaultquota_text($settingstatus);
                   2685:         }
                   2686:         if ($newisdefault) {
                   2687:             $newdefquotatext = &get_defaultquota_text($settingstatus);
1.134     raeburn  2688:         }
1.275     raeburn  2689:         &tool_changes('tools',\@usertools,\%oldaccess,\%oldaccesstext,\%userenv,
                   2690:                       \%changeHash,\%changed,\%newaccess,\%newaccesstext);
1.300     raeburn  2691:         if ($env{'form.ccdomain'} eq $env{'request.role.domain'}) {
                   2692:             &tool_changes('requestcourses',\@requestcourses,\%oldaccess,\%oldaccesstext,
                   2693:                           \%userenv,\%changeHash,\%changed,\%newaccess,\%newaccesstext);
                   2694:         } else {
                   2695:             &tool_changes('reqcrsotherdom',\@requestcourses,\%oldaccess,\%oldaccesstext,
1.314     raeburn  2696:                           \%userenv,\%changeHash,\%changed,\%newaccess,\%newaccesstext);
1.300     raeburn  2697:         }
1.206     raeburn  2698:         if ($env{'form.cfirstname'}  ne $userenv{'firstname'}  ||
                   2699:             $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
                   2700:             $env{'form.clastname'}   ne $userenv{'lastname'}   ||
                   2701:             $env{'form.cgeneration'} ne $userenv{'generation'} ||
                   2702:             $env{'form.cid'} ne $userenv{'id'}                 ||
                   2703:             $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} ) {
1.134     raeburn  2704:             $namechanged = 1;
                   2705:         }
1.267     raeburn  2706:         if (($namechanged) || (keys(%changed) > 0)) {
1.101     albertel 2707:             $changeHash{'firstname'}  = $env{'form.cfirstname'};
                   2708:             $changeHash{'middlename'} = $env{'form.cmiddlename'};
                   2709:             $changeHash{'lastname'}   = $env{'form.clastname'};
                   2710:             $changeHash{'generation'} = $env{'form.cgeneration'};
1.196     raeburn  2711:             $changeHash{'id'}         = $env{'form.cid'};
1.174     raeburn  2712:             $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
1.267     raeburn  2713:             my ($chgresult,$namechgresult);
                   2714:             if (keys(%changed) > 0) {
                   2715:                 $chgresult = 
1.204     raeburn  2716:                     &Apache::lonnet::put('environment',\%changeHash,
                   2717:                                   $env{'form.ccdomain'},$env{'form.ccuname'});
1.267     raeburn  2718:                 if ($chgresult eq 'ok') {
                   2719:                     if (($env{'user.name'} eq $env{'form.ccuname'}) &&
                   2720:                         ($env{'user.domain'} eq $env{'form.ccdomain'})) {
1.270     raeburn  2721:                         my %newenvhash;
                   2722:                         foreach my $key (keys(%changed)) {
1.299     raeburn  2723:                             if (($key eq 'official') || ($key eq 'unofficial')
                   2724:                                 || ($key eq 'community')) {
1.279     raeburn  2725:                                 $newenvhash{'environment.requestcourses.'.$key} =
                   2726:                                     $changeHash{'requestcourses.'.$key};
                   2727:                                 if ($changeHash{'requestcourses.'.$key} ne '') {
                   2728:                                     $newenvhash{'environment.canrequest.'.$key} =
                   2729:                                         $changeHash{'requestcourses.'.$key};
                   2730:                                 } else {
                   2731:                                     $newenvhash{'environment.canrequest.'.$key} =
                   2732:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2733:                                             $key,'reload','requestcourses');
                   2734:                                 }
1.275     raeburn  2735:                             } elsif ($key ne 'quota') {
1.270     raeburn  2736:                                 $newenvhash{'environment.tools.'.$key} = 
                   2737:                                     $changeHash{'tools.'.$key};
1.279     raeburn  2738:                                 if ($changeHash{'tools.'.$key} ne '') {
                   2739:                                     $newenvhash{'environment.availabletools.'.$key} =
                   2740:                                         $changeHash{'tools.'.$key};
                   2741:                                 } else {
                   2742:                                     $newenvhash{'environment.availabletools.'.$key} =
1.329.2.4  raeburn  2743:           &Apache::lonnet::usertools_access($env{'user.name'},$env{'user.domain'},
                   2744:                                             $key,'reload','tools');
1.279     raeburn  2745:                                 }
1.270     raeburn  2746:                             }
                   2747:                         }
1.271     raeburn  2748:                         if (keys(%newenvhash)) {
                   2749:                             &Apache::lonnet::appenv(\%newenvhash);
                   2750:                         }
1.267     raeburn  2751:                     }
                   2752:                 }
1.204     raeburn  2753:             }
                   2754:             if ($namechanged) {
                   2755:             # Make the change
                   2756:                 $namechgresult =
                   2757:                     &Apache::lonnet::modifyuser($env{'form.ccdomain'},
                   2758:                         $env{'form.ccuname'},$changeHash{'id'},undef,undef,
                   2759:                         $changeHash{'firstname'},$changeHash{'middlename'},
                   2760:                         $changeHash{'lastname'},$changeHash{'generation'},
                   2761:                         $changeHash{'id'},undef,$changeHash{'permanentemail'});
1.220     raeburn  2762:                 %userupdate = (
                   2763:                                lastname   => $env{'form.clastname'},
                   2764:                                middlename => $env{'form.cmiddlename'},
                   2765:                                firstname  => $env{'form.cfirstname'},
                   2766:                                generation => $env{'form.cgeneration'},
                   2767:                                id         => $env{'form.cid'},
                   2768:                              );
1.204     raeburn  2769:             }
                   2770:             if (($namechanged && $namechgresult eq 'ok') || 
1.267     raeburn  2771:                 ((keys(%changed) > 0) && $chgresult eq 'ok')) {
1.28      matthew  2772:             # Tell the user we changed the name
1.73      sakharuk 2773: 		my %lt=&Apache::lonlocal::texthash(
1.284     bisitz   2774:                              'uic'        => 'User Information Changed',
                   2775:                              'frst'       => 'First Name',
                   2776:                              'mddl'       => 'Middle Name',
                   2777:                              'lst'        => 'Last Name',
                   2778:                              'gen'        => 'Generation',
                   2779:                              'id'         => 'Student/Employee ID',
                   2780:                              'mail'       => 'Permanent e-mail address',
                   2781:                              'disk'       => 'Disk space allocated to portfolio files',
                   2782:                              'blog'       => 'Blog Availability',
                   2783:                              'aboutme'    => 'Personal Information Page Availability',
                   2784:                              'portfolio'  => 'Portfolio Availability',
                   2785:                              'official'   => 'Can Request Official Courses',
                   2786:                              'unofficial' => 'Can Request Unofficial Courses',
1.299     raeburn  2787:                              'community'  => 'Can Request Communities',
1.286     raeburn  2788:                              'inststatus' => "Affiliation",
1.284     bisitz   2789:                              'prvs'       => 'Previous Value:',
                   2790:                              'chto'       => 'Changed To:'
1.73      sakharuk 2791: 						   );
1.201     raeburn  2792:                 $r->print('<h4>'.$lt{'uic'}.'</h4>'.
                   2793:                           &Apache::loncommon::start_data_table().
                   2794:                           &Apache::loncommon::start_data_table_header_row());
1.28      matthew  2795:                 $r->print(<<"END");
1.201     raeburn  2796:     <th>&nbsp;</th>
1.73      sakharuk 2797:     <th>$lt{'frst'}</th>
                   2798:     <th>$lt{'mddl'}</th>
                   2799:     <th>$lt{'lst'}</th>
1.134     raeburn  2800:     <th>$lt{'gen'}</th>
1.196     raeburn  2801:     <th>$lt{'id'}</th>
1.173     raeburn  2802:     <th>$lt{'mail'}</th>
1.201     raeburn  2803: END
1.286     raeburn  2804:                 if ($showinststatus) {
                   2805:                     $r->print("
                   2806:     <th>$lt{'inststatus'}</th>\n");
                   2807:                 }
1.275     raeburn  2808:                 if ($showrequestcourses) {
                   2809:                     foreach my $item (@requestcourses) {
                   2810:                         $r->print("
                   2811:     <th>$lt{$item}</th>\n");
                   2812:                     }
1.300     raeburn  2813:                 } elsif ($showreqotherdom) {
                   2814:                     foreach my $item (@requestcourses) {
                   2815:                         $r->print("
                   2816:     <th>$lt{$item}</th>\n");
                   2817:                     }
1.275     raeburn  2818:                 }
1.220     raeburn  2819:                 if ($showquota) {
                   2820:                     $r->print("
                   2821:     <th>$lt{'disk'}</th>\n");
                   2822:                 }
1.267     raeburn  2823:                 if ($showtools) {
                   2824:                     foreach my $item (@usertools) {
                   2825:                         $r->print("
                   2826:     <th>$lt{$item}</th>\n");
                   2827:                     }
                   2828:                 }
1.201     raeburn  2829:                 $r->print(&Apache::loncommon::end_data_table_header_row().
                   2830:                           &Apache::loncommon::start_data_table_row());
                   2831:                 $r->print(<<"END");
                   2832:     <td><b>$lt{'prvs'}</b></td>
1.28      matthew  2833:     <td>$userenv{'firstname'}  </td>
                   2834:     <td>$userenv{'middlename'} </td>
                   2835:     <td>$userenv{'lastname'}   </td>
1.134     raeburn  2836:     <td>$userenv{'generation'} </td>
1.196     raeburn  2837:     <td>$userenv{'id'}</td>
1.160     raeburn  2838:     <td>$userenv{'permanentemail'} </td>
1.201     raeburn  2839: END
1.286     raeburn  2840:                 if ($showinststatus) {
                   2841:                     $r->print("
                   2842:     <td>$oldinststatuses</td>\n");
                   2843:                 }  
1.275     raeburn  2844:                 if ($showrequestcourses) {
                   2845:                     foreach my $item (@requestcourses) {
                   2846:                         $r->print("
                   2847:     <td>$oldaccess{$item} $oldaccesstext{$item}</td>\n");
                   2848:                     }
1.300     raeburn  2849:                 } elsif ($showreqotherdom) {
                   2850:                     foreach my $item (@requestcourses) {
                   2851:                         $r->print("
                   2852:     <td>$oldaccess{$item} $oldaccesstext{$item}</td>\n");
                   2853:                     }
1.275     raeburn  2854:                 }
1.220     raeburn  2855:                 if ($showquota) {
                   2856:                     $r->print("
                   2857:     <td>$oldportfolioquota Mb $olddefquotatext </td>\n");
                   2858:                 }
1.267     raeburn  2859:                 if ($showtools) {
                   2860:                     foreach my $item (@usertools) {
                   2861:                         $r->print("
                   2862:     <td>$oldaccess{$item} $oldaccesstext{$item} </td>\n");
                   2863:                     }
                   2864:                 }
1.201     raeburn  2865:                 $r->print(&Apache::loncommon::end_data_table_row().
                   2866:                           &Apache::loncommon::start_data_table_row());
                   2867:                 $r->print(<<"END");
1.267     raeburn  2868:     <td><span class="LC_nobreak"><b>$lt{'chto'}</b></span></td>
1.101     albertel 2869:     <td>$env{'form.cfirstname'}  </td>
                   2870:     <td>$env{'form.cmiddlename'} </td>
                   2871:     <td>$env{'form.clastname'}   </td>
1.134     raeburn  2872:     <td>$env{'form.cgeneration'} </td>
1.196     raeburn  2873:     <td>$env{'form.cid'} </td>
1.160     raeburn  2874:     <td>$env{'form.cpermanentemail'} </td>
1.28      matthew  2875: END
1.286     raeburn  2876:                 if ($showinststatus) {
                   2877:                     $r->print("
                   2878:     <td>$newinststatuses</td>\n");
                   2879:                 }
1.275     raeburn  2880:                 if ($showrequestcourses) {
                   2881:                     foreach my $item (@requestcourses) {
                   2882:                         $r->print("
                   2883:     <td>$newaccess{$item} $newaccesstext{$item} </td>\n");
                   2884:                     }
1.300     raeburn  2885:                 } elsif ($showreqotherdom) {
                   2886:                     foreach my $item (@requestcourses) {
                   2887:                         $r->print("
                   2888:     <td>$newaccess{$item} $newaccesstext{$item} </td>\n");
                   2889:                     }
1.275     raeburn  2890:                 }
1.220     raeburn  2891:                 if ($showquota) {
                   2892:                     $r->print("
                   2893:     <td>$newportfolioquota Mb $newdefquotatext </td>\n");
                   2894:                 }
1.267     raeburn  2895:                 if ($showtools) {
                   2896:                     foreach my $item (@usertools) {
                   2897:                         $r->print("
                   2898:     <td>$newaccess{$item} $newaccesstext{$item} </td>\n");
                   2899:                     }
                   2900:                 }
1.201     raeburn  2901:                 $r->print(&Apache::loncommon::end_data_table_row().
1.206     raeburn  2902:                           &Apache::loncommon::end_data_table().'<br />');
1.203     raeburn  2903:                 if ($env{'form.cid'} ne $userenv{'id'}) {
                   2904:                     &Apache::lonnet::idput($env{'form.ccdomain'},
                   2905:                          ($env{'form.ccuname'} => $env{'form.cid'}));
                   2906:                     if (($recurseid) &&
                   2907:                         (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
                   2908:                         my $idresult = 
                   2909:                             &Apache::lonuserutils::propagate_id_change(
                   2910:                                 $env{'form.ccuname'},$env{'form.ccdomain'},
                   2911:                                 \%userupdate);
                   2912:                         $r->print('<br />'.$idresult.'<br />');
                   2913:                     }
1.196     raeburn  2914:                 }
1.149     raeburn  2915:                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                   2916:                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                   2917:                     my %newenvhash;
                   2918:                     foreach my $key (keys(%changeHash)) {
                   2919:                         $newenvhash{'environment.'.$key} = $changeHash{$key};
                   2920:                     }
1.238     raeburn  2921:                     &Apache::lonnet::appenv(\%newenvhash);
1.149     raeburn  2922:                 }
1.28      matthew  2923:             } else { # error occurred
1.188     raeburn  2924:                 $r->print('<span class="LC_error">'.&mt('Unable to successfully change environment for').' '.
                   2925:                       $env{'form.ccuname'}.' '.&mt('in domain').' '.
1.206     raeburn  2926:                       $env{'form.ccdomain'}.'</span><br />');
1.28      matthew  2927:             }
1.101     albertel 2928:         }  else { # End of if ($env ... ) logic
1.275     raeburn  2929:             # They did not want to change the users name, quota, tool availability,
                   2930:             # or ability to request creation of courses, 
1.267     raeburn  2931:             # but we can still tell them what the name and quota and availabilities are  
1.73      sakharuk 2932: 	    my %lt=&Apache::lonlocal::texthash(
1.275     raeburn  2933:                            'id'         => "Student/Employee ID",
1.284     bisitz   2934:                            'mail'       => "Permanent e-mail address",
1.275     raeburn  2935:                            'disk'       => "Disk space allocated to user's portfolio files",
                   2936:                            'blog'       => "Blog Availability",
1.285     weissno  2937:                            'aboutme'    => "Personal Information Page Availability",
1.275     raeburn  2938:                            'portfolio'  => "Portfolio Availability",
                   2939:                            'official'   => "Can Request Official Courses",
1.299     raeburn  2940:                            'unofficial' => "Can Request Unofficial Courses",
                   2941:                            'community'  => "Can Request Communities",
1.286     raeburn  2942:                            'inststatus' => "Affiliation",
1.73      sakharuk 2943: 					       );
1.134     raeburn  2944:             $r->print(<<"END");
1.196     raeburn  2945: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}
1.28      matthew  2946: END
1.204     raeburn  2947:             if ($userenv{'permanentemail'} ne '') {
                   2948:                 $r->print('<br />['.$lt{'mail'}.': '.
                   2949:                           $userenv{'permanentemail'}.']');
1.134     raeburn  2950:             }
1.286     raeburn  2951:             if ($showinststatus) {
                   2952:                 $r->print('<br />['.$lt{'inststatus'}.': '.$oldinststatuses.']');
                   2953:             }
1.275     raeburn  2954:             if ($showrequestcourses) {
                   2955:                 foreach my $item (@requestcourses) {
                   2956:                     $r->print('<br />['.$lt{$item}.': '.$newaccess{$item}.' '.
                   2957:                               $newaccesstext{$item}.']'."\n");
                   2958:                 }
1.300     raeburn  2959:             } elsif ($showreqotherdom) {
                   2960:                 foreach my $item (@requestcourses) {
                   2961:                     $r->print('<br />['.$lt{$item}.': '.$newaccess{$item}.' '.
                   2962:                               $newaccesstext{$item}.']'."\n");
                   2963:                 }
1.275     raeburn  2964:             }
1.267     raeburn  2965:             if ($showtools) {
                   2966:                 foreach my $item (@usertools) {
                   2967:                     $r->print('<br />['.$lt{$item}.': '.$newaccess{$item}.' '.
                   2968:                               $newaccesstext{$item}.']'."\n");
                   2969:                 }
                   2970:             }
1.220     raeburn  2971:             if ($showquota) {
1.267     raeburn  2972:                 $r->print('<br />['.$lt{'disk'}.': '.$oldportfolioquota.' Mb '.
1.220     raeburn  2973:                           $olddefquotatext.']');
                   2974:             }
                   2975:             $r->print('</h4>');
1.28      matthew  2976:         }
1.206     raeburn  2977:         if (@mod_disallowed) {
                   2978:             my ($rolestr,$contextname);
                   2979:             if (@longroles > 0) {
                   2980:                 $rolestr = join(', ',@longroles);
                   2981:             } else {
                   2982:                 $rolestr = &mt('No roles');
                   2983:             }
                   2984:             if ($context eq 'course') {
                   2985:                 $contextname = &mt('course');
                   2986:             } elsif ($context eq 'author') {
                   2987:                 $contextname = &mt('co-author');
                   2988:             }
                   2989:             $r->print(&mt('The following fields were not updated: ').'<ul>');
                   2990:             my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   2991:             foreach my $field (@mod_disallowed) {
                   2992:                 $r->print('<li>'.$fieldtitles{$field}.'</li>'."\n"); 
                   2993:             }
1.207     raeburn  2994:             $r->print('</ul>');
                   2995:             if (@mod_disallowed == 1) {
                   2996:                 $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));
                   2997:             } else {
                   2998:                 $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));
                   2999:             }
1.292     bisitz   3000:             my $helplink = 'javascript:helpMenu('."'display'".')';
                   3001:             $r->print('<span class="LC_cusr_emph">'.$rolestr.'</span><br />'
                   3002:                      .&mt('Please contact your [_1]helpdesk[_2] for more information.'
                   3003:                          ,'<a href="'.$helplink.'">','</a>')
                   3004:                       .'<br />');
1.206     raeburn  3005:         }
1.259     bisitz   3006:         $r->print('<span class="LC_warning">'
                   3007:                   .$no_forceid_alert
                   3008:                   .&Apache::lonuserutils::print_namespacing_alerts($env{'form.ccdomain'},\%alerts,\%curr_rules)
                   3009:                   .'</span>');
1.4       www      3010:     }
1.220     raeburn  3011:     if ($env{'form.action'} eq 'singlestudent') {
1.318     raeburn  3012:         &enroll_single_student($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype);
                   3013:         $r->print('<p><a href="javascript:backPage(document.userupdate)">');
                   3014:         if ($crstype eq 'Community') {
                   3015:             $r->print(&mt('Enroll Another Member'));
                   3016:         } else {
                   3017:             $r->print(&mt('Enroll Another Student'));
                   3018:         }
                   3019:         $r->print('</a></p>');
1.220     raeburn  3020:     } else {
1.239     raeburn  3021:         my @rolechanges = &update_roles($r,$context);
1.225     raeburn  3022:         if ($namechanged) {
1.220     raeburn  3023:             if ($context eq 'course') {
                   3024:                 if (@userroles > 0) {
1.225     raeburn  3025:                     if ((@rolechanges == 0) || 
                   3026:                         (!(grep(/^st$/,@rolechanges)))) {
                   3027:                         if (grep(/^st$/,@userroles)) {
                   3028:                             my $classlistupdated =
                   3029:                                 &Apache::lonuserutils::update_classlist($cdom,
1.220     raeburn  3030:                                               $cnum,$env{'form.ccdomain'},
                   3031:                                        $env{'form.ccuname'},\%userupdate);
1.225     raeburn  3032:                         }
1.220     raeburn  3033:                     }
                   3034:                 }
                   3035:             }
                   3036:         }
1.226     raeburn  3037:         my $userinfo = &Apache::loncommon::plainname($env{'form.ccuname'},
1.233     raeburn  3038:                                                      $env{'form.ccdomain'});
                   3039:         if ($env{'form.popup'}) {
                   3040:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
                   3041:         } else {
1.246     bisitz   3042:             $r->print('<p><a href="javascript:backPage(document.userupdate,'."'$env{'form.prevphase'}','modify'".')">'
                   3043:                      .&mt('Modify this user: [_1]','<span class="LC_cusr_emph">'.$env{'form.ccuname'}.':'.$env{'form.ccdomain'}.' ('.$userinfo.')</span>').'</a>'
                   3044:                      .('&nbsp;'x5).'<a href="javascript:backPage(document.userupdate)">'
                   3045:                      .&mt('Create/Modify Another User').'</a></p>');
1.233     raeburn  3046:         }
1.220     raeburn  3047:     }
                   3048: }
                   3049: 
1.275     raeburn  3050: sub tool_changes {
                   3051:     my ($context,$usertools,$oldaccess,$oldaccesstext,$userenv,$changeHash,
                   3052:         $changed,$newaccess,$newaccesstext) = @_;
                   3053:     if (!((ref($usertools) eq 'ARRAY') && (ref($oldaccess) eq 'HASH') &&
                   3054:           (ref($oldaccesstext) eq 'HASH') && (ref($userenv) eq 'HASH') &&
                   3055:           (ref($changeHash) eq 'HASH') && (ref($changed) eq 'HASH') &&
                   3056:           (ref($newaccess) eq 'HASH') && (ref($newaccesstext) eq 'HASH'))) {
                   3057:         return;
                   3058:     }
1.300     raeburn  3059:     if ($context eq 'reqcrsotherdom') {
1.309     raeburn  3060:         my @options = ('approval','validate','autolimit');
1.306     raeburn  3061:         my $optregex = join('|',@options);
                   3062:         my %reqdisplay = &courserequest_display();
1.300     raeburn  3063:         my $cdom = $env{'request.role.domain'};
                   3064:         foreach my $tool (@{$usertools}) {
1.314     raeburn  3065:             $oldaccesstext->{$tool} = &mt('No');
                   3066:             $newaccesstext->{$tool} = $oldaccesstext->{$tool};
1.300     raeburn  3067:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.314     raeburn  3068:             my $newop;
                   3069:             if ($env{'form.'.$context.'_'.$tool}) {
                   3070:                 $newop = $env{'form.'.$context.'_'.$tool};
                   3071:                 if ($newop eq 'autolimit') {
                   3072:                     my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3073:                     $limit =~ s/\D+//g;
                   3074:                     $newop .= '='.$limit;
                   3075:                 }
                   3076:             }
1.300     raeburn  3077:             if ($userenv->{$context.'.'.$tool} eq '') {
1.314     raeburn  3078:                 if ($newop) {
                   3079:                     $changed->{$tool}=&tool_admin($tool,$cdom.':'.$newop,
1.300     raeburn  3080:                                                   $changeHash,$context);
                   3081:                     if ($changed->{$tool}) {
1.314     raeburn  3082:                         $newaccesstext->{$tool} = &mt('Yes');
1.300     raeburn  3083:                     } else {
                   3084:                         $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3085:                     }
                   3086:                 }
                   3087:             } else {
                   3088:                 my @curr = split(',',$userenv->{$context.'.'.$tool});
                   3089:                 my @new;
                   3090:                 my $changedoms;
1.314     raeburn  3091:                 foreach my $req (@curr) {
                   3092:                     if ($req =~ /^\Q$cdom\E\:($optregex\=?\d*)$/) {
                   3093:                         $oldaccesstext->{$tool} = &mt('Yes');
                   3094:                         my $oldop = $1;
                   3095:                         if ($oldop ne $newop) {
                   3096:                             $changedoms = 1;
                   3097:                             foreach my $item (@curr) {
                   3098:                                 my ($reqdom,$option) = split(':',$item);
                   3099:                                 unless ($reqdom eq $cdom) {
                   3100:                                     push(@new,$item);
                   3101:                                 }
                   3102:                             }
                   3103:                             if ($newop) {
                   3104:                                 push(@new,$cdom.':'.$newop);
1.300     raeburn  3105:                             }
1.314     raeburn  3106:                             @new = sort(@new);
1.300     raeburn  3107:                         }
1.314     raeburn  3108:                         last;
1.300     raeburn  3109:                     }
1.314     raeburn  3110:                 }
                   3111:                 if ((!$changedoms) && ($newop)) {
1.300     raeburn  3112:                     $changedoms = 1;
1.306     raeburn  3113:                     @new = sort(@curr,$cdom.':'.$newop);
1.300     raeburn  3114:                 }
                   3115:                 if ($changedoms) {
1.314     raeburn  3116:                     my $newdomstr;
1.300     raeburn  3117:                     if (@new) {
                   3118:                         $newdomstr = join(',',@new);
                   3119:                     }
                   3120:                     $changed->{$tool}=&tool_admin($tool,$newdomstr,$changeHash,
                   3121:                                                   $context);
                   3122:                     if ($changed->{$tool}) {
                   3123:                         if ($env{'form.'.$context.'_'.$tool}) {
1.306     raeburn  3124:                             if ($env{'form.'.$context.'_'.$tool} eq 'autolimit') {
1.314     raeburn  3125:                                 my $limit = $env{'form.'.$context.'_'.$tool.'_limit'};
                   3126:                                 $limit =~ s/\D+//g;
                   3127:                                 if ($limit) {
                   3128:                                     $newaccesstext->{$tool} = &mt('Yes, up to limit of [quant,_1,request] per user.',$limit);
                   3129:                                 } else {
1.306     raeburn  3130:                                     $newaccesstext->{$tool} = &mt('Yes, processed automatically');
                   3131:                                 }
1.314     raeburn  3132:                             } else {
1.306     raeburn  3133:                                 $newaccesstext->{$tool} = $reqdisplay{$env{'form.'.$context.'_'.$tool}};
                   3134:                             }
1.300     raeburn  3135:                         } else {
1.306     raeburn  3136:                             $newaccesstext->{$tool} = &mt('No');
1.300     raeburn  3137:                         }
                   3138:                     }
                   3139:                 }
                   3140:             }
                   3141:         }
                   3142:         return;
                   3143:     }
1.275     raeburn  3144:     foreach my $tool (@{$usertools}) {
1.306     raeburn  3145:         my $newval;
                   3146:         if ($context eq 'requestcourses') {
                   3147:             $newval = $env{'form.crsreq_'.$tool};
                   3148:             if ($newval eq 'autolimit') {
                   3149:                 $newval .= '='.$env{'form.crsreq_'.$tool.'_limit'};
                   3150:             }
1.314     raeburn  3151:         } else {
1.306     raeburn  3152:             $newval = $env{'form.'.$context.'_'.$tool};
                   3153:         }
1.275     raeburn  3154:         if ($userenv->{$context.'.'.$tool} ne '') {
                   3155:             $oldaccess->{$tool} = &mt('custom');
                   3156:             if ($userenv->{$context.'.'.$tool}) {
                   3157:                 $oldaccesstext->{$tool} = &mt("availability set to 'on'");
                   3158:             } else {
                   3159:                 $oldaccesstext->{$tool} = &mt("availability set to 'off'");
                   3160:             }
1.279     raeburn  3161:             $changeHash->{$context.'.'.$tool} = $userenv->{$context.'.'.$tool};
1.275     raeburn  3162:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3163:                 if ($newval ne $userenv->{$context.'.'.$tool}) {
                   3164:                     $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3165:                                                     $context);
1.275     raeburn  3166:                     if ($changed->{$tool}) {
                   3167:                         $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3168:                         if ($newval) {
1.275     raeburn  3169:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3170:                         } else {
                   3171:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3172:                         }
                   3173:                     } else {
                   3174:                         $newaccess->{$tool} = $oldaccess->{$tool};
                   3175:                         if ($userenv->{$context.'.'.$tool}) {
                   3176:                             $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3177:                         } else {
                   3178:                             $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3179:                         }
                   3180:                     }
                   3181:                 } else {
                   3182:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3183:                     $newaccesstext->{$tool} = $oldaccesstext->{$tool};
                   3184:                 }
                   3185:             } else {
                   3186:                 $changed->{$tool} = &tool_admin($tool,'',$changeHash,$context);
                   3187:                 if ($changed->{$tool}) {
                   3188:                     $newaccess->{$tool} = &mt('default');
                   3189:                 } else {
                   3190:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3191:                     if ($userenv->{$context.'.'.$tool}) {
1.300     raeburn  3192:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
1.275     raeburn  3193:                     } else {
1.300     raeburn  3194:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
1.275     raeburn  3195:                     }
                   3196:                 }
                   3197:             }
                   3198:         } else {
                   3199:             $oldaccess->{$tool} = &mt('default');
                   3200:             if ($env{'form.custom'.$tool} == 1) {
1.306     raeburn  3201:                 $changed->{$tool} = &tool_admin($tool,$newval,$changeHash,
                   3202:                                                 $context);
1.275     raeburn  3203:                 if ($changed->{$tool}) {
                   3204:                     $newaccess->{$tool} = &mt('custom');
1.306     raeburn  3205:                     if ($newval) {
1.275     raeburn  3206:                         $newaccesstext->{$tool} = &mt("availability set to 'on'");
                   3207:                     } else {
                   3208:                         $newaccesstext->{$tool} = &mt("availability set to 'off'");
                   3209:                     }
                   3210:                 } else {
                   3211:                     $newaccess->{$tool} = $oldaccess->{$tool};
                   3212:                 }
                   3213:             } else {
                   3214:                 $newaccess->{$tool} = $oldaccess->{$tool};
                   3215:             }
                   3216:         }
                   3217:     }
                   3218:     return;
                   3219: }
                   3220: 
1.220     raeburn  3221: sub update_roles {
1.239     raeburn  3222:     my ($r,$context) = @_;
1.4       www      3223:     my $now=time;
1.225     raeburn  3224:     my @rolechanges;
1.220     raeburn  3225:     my %disallowed;
1.73      sakharuk 3226:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.135     raeburn  3227:     foreach my $key (keys (%env)) {
                   3228: 	next if (! $env{$key});
1.190     raeburn  3229:         next if ($key eq 'form.action');
1.27      matthew  3230: 	# Revoke roles
1.135     raeburn  3231: 	if ($key=~/^form\.rev/) {
                   3232: 	    if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
1.64      www      3233: # Revoke standard role
1.170     albertel 3234: 		my ($scope,$role) = ($1,$2);
                   3235: 		my $result =
                   3236: 		    &Apache::lonnet::revokerole($env{'form.ccdomain'},
                   3237: 						$env{'form.ccuname'},
1.239     raeburn  3238: 						$scope,$role,'','',$context);
1.170     albertel 3239: 	        $r->print(&mt('Revoking [_1] in [_2]: [_3]',
                   3240: 			      $role,$scope,'<b>'.$result.'</b>').'<br />');
                   3241: 		if ($role eq 'st') {
1.202     raeburn  3242: 		    my $result = 
1.198     raeburn  3243:                         &Apache::lonuserutils::classlist_drop($scope,
                   3244:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3245: 			    $now);
1.170     albertel 3246: 		    $r->print($result);
1.53      www      3247: 		}
1.225     raeburn  3248:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3249:                     push(@rolechanges,$role);
                   3250:                 }
1.196     raeburn  3251: 	    }
1.195     raeburn  3252: 	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}s) {
1.64      www      3253: # Revoke custom role
1.113     raeburn  3254: 		$r->print(&mt('Revoking custom role:').
1.139     albertel 3255:                       ' '.$4.' by '.$3.':'.$2.' in '.$1.': <b>'.
1.101     albertel 3256:                       &Apache::lonnet::revokecustomrole($env{'form.ccdomain'},
1.239     raeburn  3257: 				  $env{'form.ccuname'},$1,$2,$3,$4,'','',$context).
1.102     albertel 3258: 		'</b><br />');
1.225     raeburn  3259:                 if (!grep(/^cr$/,@rolechanges)) {
                   3260:                     push(@rolechanges,'cr');
                   3261:                 }
1.64      www      3262: 	    }
1.135     raeburn  3263: 	} elsif ($key=~/^form\.del/) {
                   3264: 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
1.116     raeburn  3265: # Delete standard role
1.170     albertel 3266: 		my ($scope,$role) = ($1,$2);
                   3267: 		my $result =
                   3268: 		    &Apache::lonnet::assignrole($env{'form.ccdomain'},
                   3269: 						$env{'form.ccuname'},
1.239     raeburn  3270: 						$scope,$role,$now,0,1,'',
                   3271:                                                 $context);
1.170     albertel 3272: 	        $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope,
                   3273: 			      '<b>'.$result.'</b>').'<br />');
                   3274: 		if ($role eq 'st') {
1.202     raeburn  3275: 		    my $result = 
1.198     raeburn  3276:                         &Apache::lonuserutils::classlist_drop($scope,
                   3277:                             $env{'form.ccuname'},$env{'form.ccdomain'},
1.202     raeburn  3278: 			    $now);
1.170     albertel 3279: 		    $r->print($result);
1.81      albertel 3280: 		}
1.225     raeburn  3281:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3282:                     push(@rolechanges,$role);
                   3283:                 }
1.116     raeburn  3284:             }
1.139     albertel 3285: 	    if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3286:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3287: # Delete custom role
1.294     bisitz   3288:                 $r->print(&mt('Deleting custom role [_1] by [_2] in [_3]',
                   3289:                       $rolename,$rnam.':'.$rdom,$url).': <b>'.
1.116     raeburn  3290:                       &Apache::lonnet::assigncustomrole($env{'form.ccdomain'},
                   3291:                          $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
1.240     raeburn  3292:                          0,1,$context).'</b><br />');
1.225     raeburn  3293:                 if (!grep(/^cr$/,@rolechanges)) {
                   3294:                     push(@rolechanges,'cr');
                   3295:                 }
1.116     raeburn  3296:             }
1.135     raeburn  3297: 	} elsif ($key=~/^form\.ren/) {
1.101     albertel 3298:             my $udom = $env{'form.ccdomain'};
                   3299:             my $uname = $env{'form.ccuname'};
1.116     raeburn  3300: # Re-enable standard role
1.135     raeburn  3301: 	    if ($key=~/^form\.ren\:([^\_]+)\_([^\_\.]+)$/) {
1.89      raeburn  3302:                 my $url = $1;
                   3303:                 my $role = $2;
                   3304:                 my $logmsg;
                   3305:                 my $output;
                   3306:                 if ($role eq 'st') {
1.141     albertel 3307:                     if ($url =~ m-^/($match_domain)/($match_courseid)/?(\w*)$-) {
1.129     albertel 3308:                         my $result = &Apache::loncommon::commit_studentrole(\$logmsg,$udom,$uname,$url,$role,$now,0,$1,$2,$3);
1.220     raeburn  3309:                         if (($result =~ /^error/) || ($result eq 'not_in_class') || ($result eq 'unknown_course') || ($result eq 'refused')) {
1.223     raeburn  3310:                             if ($result eq 'refused' && $logmsg) {
                   3311:                                 $output = $logmsg;
                   3312:                             } else { 
                   3313:                                 $output = "Error: $result\n";
                   3314:                             }
1.89      raeburn  3315:                         } else {
                   3316:                             $output = &mt('Assigning').' '.$role.' in '.$url.
                   3317:                                       &mt('starting').' '.localtime($now).
                   3318:                                       ': <br />'.$logmsg.'<br />'.
                   3319:                                       &mt('Add to classlist').': <b>ok</b><br />';
                   3320:                         }
                   3321:                     }
                   3322:                 } else {
1.101     albertel 3323: 		    my $result=&Apache::lonnet::assignrole($env{'form.ccdomain'},
1.239     raeburn  3324:                                $env{'form.ccuname'},$url,$role,0,$now,'','',
                   3325:                                $context);
1.266     bisitz   3326: 		    $output = &mt('Re-enabling [_1] in [_2]: [_3]',
                   3327: 			      $role,$url,'<b>'.$result.'</b>').'<br />';
1.27      matthew  3328: 		}
1.89      raeburn  3329:                 $r->print($output);
1.225     raeburn  3330:                 if (!grep(/^\Q$role\E$/,@rolechanges)) {
                   3331:                     push(@rolechanges,$role);
                   3332:                 }
1.113     raeburn  3333: 	    }
1.116     raeburn  3334: # Re-enable custom role
1.139     albertel 3335: 	    if ($key=~m{^form\.ren\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) {
1.116     raeburn  3336:                 my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4);
                   3337:                 my $result = &Apache::lonnet::assigncustomrole(
                   3338:                                $env{'form.ccdomain'}, $env{'form.ccuname'},
1.240     raeburn  3339:                                $url,$rdom,$rnam,$rolename,0,$now,undef,$context);
1.294     bisitz   3340:                 $r->print(&mt('Re-enabling custom role [_1] by [_2] in [_3]: [_4]',
                   3341:                           $rolename,$rnam.':'.$rdom,$url,'<b>'.$result.'</b>').'<br />');
1.225     raeburn  3342:                 if (!grep(/^cr$/,@rolechanges)) {
                   3343:                     push(@rolechanges,'cr');
                   3344:                 }
1.116     raeburn  3345:             }
1.135     raeburn  3346: 	} elsif ($key=~/^form\.act/) {
1.101     albertel 3347:             my $udom = $env{'form.ccdomain'};
                   3348:             my $uname = $env{'form.ccuname'};
1.141     albertel 3349: 	    if ($key=~/^form\.act\_($match_domain)\_($match_courseid)\_cr_cr_($match_domain)_($match_username)_([^\_]+)$/) {
1.65      www      3350:                 # Activate a custom role
1.83      albertel 3351: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   3352: 		my $url='/'.$one.'/'.$two;
                   3353: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      3354: 
1.101     albertel 3355:                 my $start = ( $env{'form.start_'.$full} ?
                   3356:                               $env{'form.start_'.$full} :
1.88      raeburn  3357:                               $now );
1.101     albertel 3358:                 my $end   = ( $env{'form.end_'.$full} ?
                   3359:                               $env{'form.end_'.$full} :
1.88      raeburn  3360:                               0 );
                   3361:                                                                                      
                   3362:                 # split multiple sections
                   3363:                 my %sections = ();
1.101     albertel 3364:                 my $num_sections = &build_roles($env{'form.sec_'.$full},\%sections,$5);
1.88      raeburn  3365:                 if ($num_sections == 0) {
1.240     raeburn  3366:                     $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$url,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3367:                 } else {
1.114     albertel 3368: 		    my %curr_groups =
1.117     raeburn  3369: 			&Apache::longroup::coursegroups($one,$two);
1.113     raeburn  3370:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3371:                         if (($sec eq 'none') || ($sec eq 'all') || 
                   3372:                             exists($curr_groups{$sec})) {
                   3373:                             $disallowed{$sec} = $url;
                   3374:                             next;
                   3375:                         }
                   3376:                         my $securl = $url.'/'.$sec;
1.240     raeburn  3377: 		        $r->print(&Apache::loncommon::commit_customrole($udom,$uname,$securl,$three,$four,$five,$start,$end,$context));
1.88      raeburn  3378:                     }
                   3379:                 }
1.225     raeburn  3380:                 if (!grep(/^cr$/,@rolechanges)) {
                   3381:                     push(@rolechanges,'cr');
                   3382:                 }
1.142     raeburn  3383: 	    } elsif ($key=~/^form\.act\_($match_domain)\_($match_name)\_([^\_]+)$/) {
1.27      matthew  3384: 		# Activate roles for sections with 3 id numbers
                   3385: 		# set start, end times, and the url for the class
1.83      albertel 3386: 		my ($one,$two,$three)=($1,$2,$3);
1.101     albertel 3387: 		my $start = ( $env{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   3388: 			      $env{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  3389: 			      $now );
1.101     albertel 3390: 		my $end   = ( $env{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   3391: 			      $env{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  3392: 			      0 );
1.83      albertel 3393: 		my $url='/'.$one.'/'.$two;
1.88      raeburn  3394:                 my $type = 'three';
                   3395:                 # split multiple sections
                   3396:                 my %sections = ();
1.101     albertel 3397:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections,$three);
1.88      raeburn  3398:                 if ($num_sections == 0) {
1.240     raeburn  3399:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context));
1.88      raeburn  3400:                 } else {
1.114     albertel 3401:                     my %curr_groups = 
1.117     raeburn  3402: 			&Apache::longroup::coursegroups($one,$two);
1.88      raeburn  3403:                     my $emptysec = 0;
                   3404:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3405:                         $sec =~ s/\W//g;
1.113     raeburn  3406:                         if ($sec ne '') {
                   3407:                             if (($sec eq 'none') || ($sec eq 'all') || 
                   3408:                                 exists($curr_groups{$sec})) {
                   3409:                                 $disallowed{$sec} = $url;
                   3410:                                 next;
                   3411:                             }
1.88      raeburn  3412:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3413:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$three,$start,$end,$one,$two,$sec,$context));
1.88      raeburn  3414:                         } else {
                   3415:                             $emptysec = 1;
                   3416:                         }
                   3417:                     }
                   3418:                     if ($emptysec) {
1.240     raeburn  3419:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$three,$start,$end,$one,$two,'',$context));
1.88      raeburn  3420:                     }
1.225     raeburn  3421:                 }
                   3422:                 if (!grep(/^\Q$three\E$/,@rolechanges)) {
                   3423:                     push(@rolechanges,$three);
                   3424:                 }
1.135     raeburn  3425: 	    } elsif ($key=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  3426: 		# Activate roles for sections with two id numbers
                   3427: 		# set start, end times, and the url for the class
1.101     albertel 3428: 		my $start = ( $env{'form.start_'.$1.'_'.$2} ? 
                   3429: 			      $env{'form.start_'.$1.'_'.$2} : 
1.27      matthew  3430: 			      $now );
1.101     albertel 3431: 		my $end   = ( $env{'form.end_'.$1.'_'.$2} ? 
                   3432: 			      $env{'form.end_'.$1.'_'.$2} :
1.27      matthew  3433: 			      0 );
1.225     raeburn  3434:                 my $one = $1;
                   3435:                 my $two = $2;
                   3436: 		my $url='/'.$one.'/';
1.88      raeburn  3437:                 # split multiple sections
                   3438:                 my %sections = ();
1.225     raeburn  3439:                 my $num_sections = &build_roles($env{'form.sec_'.$one.'_'.$two},\%sections,$two);
1.88      raeburn  3440:                 if ($num_sections == 0) {
1.240     raeburn  3441:                     $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3442:                 } else {
                   3443:                     my $emptysec = 0;
                   3444:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
                   3445:                         if ($sec ne '') {
                   3446:                             my $securl = $url.'/'.$sec;
1.240     raeburn  3447:                             $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$securl,$two,$start,$end,$one,undef,$sec,$context));
1.88      raeburn  3448:                         } else {
                   3449:                             $emptysec = 1;
                   3450:                         }
                   3451:                     }
                   3452:                     if ($emptysec) {
1.240     raeburn  3453:                         $r->print(&Apache::loncommon::commit_standardrole($udom,$uname,$url,$two,$start,$end,$one,undef,'',$context));
1.88      raeburn  3454:                     }
                   3455:                 }
1.225     raeburn  3456:                 if (!grep(/^\Q$two\E$/,@rolechanges)) {
                   3457:                     push(@rolechanges,$two);
                   3458:                 }
1.64      www      3459: 	    } else {
1.190     raeburn  3460: 		$r->print('<p><span class="LC_error">'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$key.'</tt></span></p><br />');
1.64      www      3461:             }
1.113     raeburn  3462:             foreach my $key (sort(keys(%disallowed))) {
1.274     bisitz   3463:                 $r->print('<p class="LC_warning">');
1.113     raeburn  3464:                 if (($key eq 'none') || ($key eq 'all')) {  
1.274     bisitz   3465:                     $r->print(&mt('[_1] may not be used as the name for a section, as it is a reserved word.','<tt>'.$key.'</tt>'));
1.113     raeburn  3466:                 } else {
1.274     bisitz   3467:                     $r->print(&mt('[_1] may not be used as the name for a section, as it is the name of a course group.','<tt>'.$key.'</tt>'));
1.113     raeburn  3468:                 }
1.274     bisitz   3469:                 $r->print('</p><p>'
                   3470:                          .&mt('Please [_1]go back[_2] and choose a different section name.'
                   3471:                              ,'<a href="javascript:history.go(-1)'
                   3472:                              ,'</a>')
                   3473:                          .'</p><br />'
                   3474:                 );
1.113     raeburn  3475:             }
                   3476: 	}
1.101     albertel 3477:     } # End of foreach (keys(%env))
1.75      www      3478: # Flush the course logs so reverse user roles immediately updated
                   3479:     &Apache::lonnet::flushcourselogs();
1.225     raeburn  3480:     if (@rolechanges == 0) {
1.193     raeburn  3481:         $r->print(&mt('No roles to modify'));
                   3482:     }
1.225     raeburn  3483:     return @rolechanges;
1.220     raeburn  3484: }
                   3485: 
                   3486: sub enroll_single_student {
1.318     raeburn  3487:     my ($r,$uhome,$amode,$genpwd,$now,$newuser,$context,$crstype) = @_;
                   3488:     $r->print('<h3>');
                   3489:     if ($crstype eq 'Community') {
                   3490:         $r->print(&mt('Enrolling Member'));
                   3491:     } else {
                   3492:         $r->print(&mt('Enrolling Student'));
                   3493:     }
                   3494:     $r->print('</h3>');
1.220     raeburn  3495: 
                   3496:     # Remove non alphanumeric values from section
                   3497:     $env{'form.sections'}=~s/\W//g;
                   3498: 
                   3499:     # Clean out any old student roles the user has in this class.
                   3500:     &Apache::lonuserutils::modifystudent($env{'form.ccdomain'},
                   3501:          $env{'form.ccuname'},$env{'request.course.id'},undef,$uhome);
                   3502:     my ($startdate,$enddate) = &Apache::lonuserutils::get_dates_from_form();
                   3503:     my $enroll_result =
                   3504:         &Apache::lonnet::modify_student_enrollment($env{'form.ccdomain'},
                   3505:             $env{'form.ccuname'},$env{'form.cid'},$env{'form.cfirstname'},
                   3506:             $env{'form.cmiddlename'},$env{'form.clastname'},
                   3507:             $env{'form.generation'},$env{'form.sections'},$enddate,
1.239     raeburn  3508:             $startdate,'manual',undef,$env{'request.course.id'},'',$context);
1.220     raeburn  3509:     if ($enroll_result =~ /^ok/) {
                   3510:         $r->print(&mt('<b>[_1]</b> enrolled',$env{'form.ccuname'}.':'.$env{'form.ccdomain'}));
                   3511:         if ($env{'form.sections'} ne '') {
                   3512:             $r->print(' '.&mt('in section [_1]',$env{'form.sections'}));
                   3513:         }
                   3514:         my ($showstart,$showend);
                   3515:         if ($startdate <= $now) {
                   3516:             $showstart = &mt('Access starts immediately');
                   3517:         } else {
                   3518:             $showstart = &mt('Access starts: ').&Apache::lonlocal::locallocaltime($startdate);
                   3519:         }
                   3520:         if ($enddate == 0) {
                   3521:             $showend = &mt('ends: no ending date');
                   3522:         } else {
                   3523:             $showend = &mt('ends: ').&Apache::lonlocal::locallocaltime($enddate);
                   3524:         }
                   3525:         $r->print('.<br />'.$showstart.'; '.$showend);
                   3526:         if ($startdate <= $now && !$newuser) {
1.318     raeburn  3527:             $r->print('<p> ');
                   3528:             if ($crstype eq 'Community') {
                   3529:                 $r->print(&mt('If the member is currently logged-in to LON-CAPA, the new role will be available when the member next logs in.'));
                   3530:             } else {
                   3531:                 $r->print(&mt('If the student is currently logged-in to LON-CAPA, the new role will be available when the student next logs in.'));
                   3532:            }
                   3533:            $r->print('</p>');
1.220     raeburn  3534:         }
                   3535:     } else {
                   3536:         $r->print(&mt('unable to enroll').": ".$enroll_result);
                   3537:     }
                   3538:     return;
1.188     raeburn  3539: }
                   3540: 
1.204     raeburn  3541: sub get_defaultquota_text {
                   3542:     my ($settingstatus) = @_;
                   3543:     my $defquotatext; 
                   3544:     if ($settingstatus eq '') {
                   3545:         $defquotatext = &mt('(default)');
                   3546:     } else {
                   3547:         my ($usertypes,$order) =
                   3548:             &Apache::lonnet::retrieve_inst_usertypes($env{'form.ccdomain'});
                   3549:         if ($usertypes->{$settingstatus} eq '') {
                   3550:             $defquotatext = &mt('(default)');
                   3551:         } else {
                   3552:             $defquotatext = &mt('(default for [_1])',$usertypes->{$settingstatus});
                   3553:         }
                   3554:     }
                   3555:     return $defquotatext;
                   3556: }
                   3557: 
1.188     raeburn  3558: sub update_result_form {
                   3559:     my ($uhome) = @_;
                   3560:     my $outcome = 
                   3561:     '<form name="userupdate" method="post" />'."\n";
1.160     raeburn  3562:     foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
1.188     raeburn  3563:         $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3564:     }
1.207     raeburn  3565:     if ($env{'form.origname'} ne '') {
                   3566:         $outcome .= '<input type="hidden" name="origname" value="'.$env{'form.origname'}.'" />'."\n";
                   3567:     }
1.160     raeburn  3568:     foreach my $item ('sortby','seluname','seludom') {
                   3569:         if (exists($env{'form.'.$item})) {
1.188     raeburn  3570:             $outcome .= '<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n";
1.160     raeburn  3571:         }
                   3572:     }
1.188     raeburn  3573:     if ($uhome eq 'no_host') {
                   3574:         $outcome .= '<input type="hidden" name="forcenewuser" value="1" />'."\n";
                   3575:     }
                   3576:     $outcome .= '<input type="hidden" name="phase" value="" />'."\n".
                   3577:                 '<input type ="hidden" name="currstate" value="" />'."\n".
1.220     raeburn  3578:                 '<input type ="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1.188     raeburn  3579:                 '</form>';
                   3580:     return $outcome;
1.4       www      3581: }
                   3582: 
1.149     raeburn  3583: sub quota_admin {
                   3584:     my ($setquota,$changeHash) = @_;
                   3585:     my $quotachanged;
                   3586:     if (&Apache::lonnet::allowed('mpq',$env{'form.ccdomain'})) {
                   3587:         # Current user has quota modification privileges
1.267     raeburn  3588:         if (ref($changeHash) eq 'HASH') {
                   3589:             $quotachanged = 1;
                   3590:             $changeHash->{'portfolioquota'} = $setquota;
                   3591:         }
1.149     raeburn  3592:     }
                   3593:     return $quotachanged;
                   3594: }
                   3595: 
1.267     raeburn  3596: sub tool_admin {
1.275     raeburn  3597:     my ($tool,$settool,$changeHash,$context) = @_;
                   3598:     my $canchange = 0; 
1.279     raeburn  3599:     if ($context eq 'requestcourses') {
1.275     raeburn  3600:         if (&Apache::lonnet::allowed('ccc',$env{'form.ccdomain'})) {
                   3601:             $canchange = 1;
                   3602:         }
1.300     raeburn  3603:     } elsif ($context eq 'reqcrsotherdom') {
                   3604:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   3605:             $canchange = 1;
                   3606:         }
1.275     raeburn  3607:     } elsif (&Apache::lonnet::allowed('mut',$env{'form.ccdomain'})) {
                   3608:         # Current user has quota modification privileges
                   3609:         $canchange = 1;
                   3610:     }
1.267     raeburn  3611:     my $toolchanged;
1.275     raeburn  3612:     if ($canchange) {
1.267     raeburn  3613:         if (ref($changeHash) eq 'HASH') {
                   3614:             $toolchanged = 1;
1.275     raeburn  3615:             $changeHash->{$context.'.'.$tool} = $settool;
1.267     raeburn  3616:         }
                   3617:     }
                   3618:     return $toolchanged;
                   3619: }
                   3620: 
1.88      raeburn  3621: sub build_roles {
1.89      raeburn  3622:     my ($sectionstr,$sections,$role) = @_;
1.88      raeburn  3623:     my $num_sections = 0;
                   3624:     if ($sectionstr=~ /,/) {
                   3625:         my @secnums = split/,/,$sectionstr;
1.89      raeburn  3626:         if ($role eq 'st') {
                   3627:             $secnums[0] =~ s/\W//g;
                   3628:             $$sections{$secnums[0]} = 1;
                   3629:             $num_sections = 1;
                   3630:         } else {
                   3631:             foreach my $sec (@secnums) {
                   3632:                 $sec =~ ~s/\W//g;
1.150     banghart 3633:                 if (!($sec eq "")) {
1.89      raeburn  3634:                     if (exists($$sections{$sec})) {
                   3635:                         $$sections{$sec} ++;
                   3636:                     } else {
                   3637:                         $$sections{$sec} = 1;
                   3638:                         $num_sections ++;
                   3639:                     }
1.88      raeburn  3640:                 }
                   3641:             }
                   3642:         }
                   3643:     } else {
                   3644:         $sectionstr=~s/\W//g;
                   3645:         unless ($sectionstr eq '') {
                   3646:             $$sections{$sectionstr} = 1;
                   3647:             $num_sections ++;
                   3648:         }
                   3649:     }
1.129     albertel 3650: 
1.88      raeburn  3651:     return $num_sections;
                   3652: }
                   3653: 
1.58      www      3654: # ========================================================== Custom Role Editor
                   3655: 
                   3656: sub custom_role_editor {
1.329.2.5  raeburn  3657:     my ($r,$brcrum) = @_;
1.324     raeburn  3658:     my $action = $env{'form.customroleaction'};
                   3659:     my $rolename; 
                   3660:     if ($action eq 'new') {
                   3661:         $rolename=$env{'form.newrolename'};
                   3662:     } else {
                   3663:         $rolename=$env{'form.rolename'};
1.59      www      3664:     }
                   3665: 
1.324     raeburn  3666:     my ($crstype,$context);
                   3667:     if ($env{'request.course.id'}) {
                   3668:         $crstype = &Apache::loncommon::course_type();
                   3669:         $context = 'course';
                   3670:     } else {
                   3671:         $context = 'domain';
                   3672:         $crstype = $env{'form.templatecrstype'};
                   3673:     }
1.329.2.5  raeburn  3674: 
                   3675:     $rolename=~s/[^A-Za-z0-9]//gs;
                   3676:     if (!$rolename || $env{'form.phase'} eq 'pickrole') {
                   3677: 	&print_username_entry_form($r,undef,undef,undef,undef,$crstype,$brcrum);
                   3678:         return;
                   3679:     }
1.329.2.4  raeburn  3680:     my $is_custom = &Apache::loncommon::needs_gci_custom();
1.329.2.3  raeburn  3681:     my $title = 'User Management';
                   3682:     if ($context eq 'course') {
1.329.2.4  raeburn  3683:         if ($is_custom) {
1.329.2.3  raeburn  3684:             $title = 'Enrollment and Student Activity';
                   3685:         }
                   3686:     }
1.153     banghart 3687: # ------------------------------------------------------- What can be assigned?
                   3688:     my %full=();
                   3689:     my %courselevel=();
                   3690:     my %courselevelcurrent=();
1.61      www      3691:     my $syspriv='';
                   3692:     my $dompriv='';
                   3693:     my $coursepriv='';
1.153     banghart 3694:     my $body_top;
1.59      www      3695:     my ($rdummy,$roledef)=
                   3696: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      3697: # ------------------------------------------------------- Does this role exist?
1.153     banghart 3698:     $body_top .= '<h2>';
1.59      www      3699:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.153     banghart 3700: 	$body_top .= &mt('Existing Role').' "';
1.61      www      3701: # ------------------------------------------------- Get current role privileges
                   3702: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.324     raeburn  3703:         if ($crstype eq 'Community') {
                   3704:             $syspriv =~ s/bre\&S//;   
                   3705:         }
1.59      www      3706:     } else {
1.153     banghart 3707: 	$body_top .= &mt('New Role').' "';
1.59      www      3708: 	$roledef='';
                   3709:     }
1.153     banghart 3710:     $body_top .= $rolename.'"</h2>';
1.135     raeburn  3711:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3712: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3713:         if (!$restrict) { $restrict='F'; }
1.60      www      3714:         $courselevel{$priv}=$restrict;
1.61      www      3715:         if ($coursepriv=~/\:$priv/) {
                   3716: 	    $courselevelcurrent{$priv}=1;
                   3717: 	}
1.60      www      3718: 	$full{$priv}=1;
                   3719:     }
                   3720:     my %domainlevel=();
1.61      www      3721:     my %domainlevelcurrent=();
1.135     raeburn  3722:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3723: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3724:         if (!$restrict) { $restrict='F'; }
1.60      www      3725:         $domainlevel{$priv}=$restrict;
1.61      www      3726:         if ($dompriv=~/\:$priv/) {
                   3727: 	    $domainlevelcurrent{$priv}=1;
                   3728: 	}
1.60      www      3729: 	$full{$priv}=1;
                   3730:     }
1.61      www      3731:     my %systemlevel=();
                   3732:     my %systemlevelcurrent=();
1.135     raeburn  3733:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   3734: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 3735:         if (!$restrict) { $restrict='F'; }
1.61      www      3736:         $systemlevel{$priv}=$restrict;
                   3737:         if ($syspriv=~/\:$priv/) {
                   3738: 	    $systemlevelcurrent{$priv}=1;
                   3739: 	}
                   3740: 	$full{$priv}=1;
                   3741:     }
1.160     raeburn  3742:     my ($jsback,$elements) = &crumb_utilities();
1.154     banghart 3743:     my $button_code = "\n";
1.153     banghart 3744:     my $head_script = "\n";
1.301     bisitz   3745:     $head_script .= '<script type="text/javascript">'."\n"
                   3746:                    .'// <![CDATA['."\n";
1.324     raeburn  3747:     my @template_roles = ("in","ta","ep");
                   3748:     if ($context eq 'domain') {
                   3749:         push(@template_roles,"ad");
1.318     raeburn  3750:     }
1.324     raeburn  3751:     push(@template_roles,"st");
1.318     raeburn  3752:     if ($crstype eq 'Community') {
                   3753:         unshift(@template_roles,'co');
                   3754:     } else {
                   3755:         unshift(@template_roles,'cc');
                   3756:     }
1.154     banghart 3757:     foreach my $role (@template_roles) {
1.324     raeburn  3758:         $head_script .= &make_script_template($role,$crstype);
1.318     raeburn  3759:         $button_code .= &make_button_code($role,$crstype).' ';
1.154     banghart 3760:     }
1.324     raeburn  3761:     my $context_code;
                   3762:     if ($context eq 'domain') {
                   3763:         my $checkedCommunity = '';
                   3764:         my $checkedCourse = ' checked="checked"';
                   3765:         if ($env{'form.templatecrstype'} eq 'Community') {
                   3766:             $checkedCommunity = $checkedCourse;
                   3767:             $checkedCourse = '';
                   3768:         }
                   3769:         $context_code = '<label>'.
                   3770:                         '<input type="radio" name="templatecrstype" value="Course"'.$checkedCourse.' onclick="this.form.submit();">'.
                   3771:                         &mt('Course').
                   3772:                         '</label>'.('&nbsp;' x2).
                   3773:                         '<label>'.
                   3774:                         '<input type="radio" name="templatecrstype" value="Community"'.$checkedCommunity.' onclick="this.form.submit();">'.
                   3775:                         &mt('Community').
                   3776:                         '</label>'.
                   3777:                         '</fieldset>'.
                   3778:                         '<input type="hidden" name="customroleaction" value="'.
                   3779:                         $action.'" />';
                   3780:         if ($env{'form.customroleaction'} eq 'new') {
                   3781:             $context_code .= '<input type="hidden" name="newrolename" value="'.
                   3782:                              $rolename.'" />';
                   3783:         } else {
                   3784:             $context_code .= '<input type="hidden" name="rolename" value="'.
                   3785:                              $rolename.'" />';
                   3786:         }
                   3787:         $context_code .= '<input type="hidden" name="action" value="custom" />'.
                   3788:                          '<input type="hidden" name="phase" value="selected_custom_edit" />';
                   3789:     }
                   3790: 
1.301     bisitz   3791:     $head_script .= "\n".$jsback."\n"
                   3792:                    .'// ]]>'."\n"
                   3793:                    .'</script>'."\n";
1.329.2.5  raeburn  3794:     push (@{$brcrum},
                   3795:               {href => "javascript:backPage(document.form1,'pickrole','')",
                   3796:                text => "Pick custom role",
                   3797:                faq  => 282,bug=>'Instructor Interface',},
                   3798:               {href => "javascript:backPage(document.form1,'','')",
                   3799:                text => "Edit custom role",
                   3800:                faq  => 282,
                   3801:                bug  => 'Instructor Interface',
                   3802:                help => 'Course_Editing_Custom_Roles'}
                   3803:               );
                   3804:     my $args = { bread_crumbs          => $brcrum,
                   3805:                  bread_crumbs_component => 'User Management'};
                   3806:  
                   3807:     $r->print(&Apache::loncommon::start_page('Custom Role Editor',
                   3808:                                              $head_script,$args).
                   3809:               $body_top);
1.73      sakharuk 3810:     my %lt=&Apache::lonlocal::texthash(
                   3811: 		    'prv'  => "Privilege",
1.131     raeburn  3812: 		    'crl'  => "Course Level",
1.73      sakharuk 3813:                     'dml'  => "Domain Level",
1.150     banghart 3814:                     'ssl'  => "System Level");
1.264     bisitz   3815: 
1.324     raeburn  3816:     $r->print('<div class="LC_left_float">'
1.264     bisitz   3817:              .'<form action=""><fieldset>'
                   3818:              .'<legend>'.&mt('Select a Template').'</legend>'
                   3819:              .$button_code
1.324     raeburn  3820:              .'</fieldset></form></div>');
                   3821:     if ($context_code) {
                   3822:         $r->print('<div class="LC_left_float">'
                   3823:                  .'<form action="/adm/createuser" method="post"><fieldset>'
                   3824:                  .'<legend>'.&mt('Context').'</legend>'
                   3825:                  .$context_code
                   3826:                  .'</form>'
                   3827:                  .'</div>'
                   3828:         );
                   3829:     }
                   3830:     $r->print('<br clear="all" />');
1.264     bisitz   3831: 
1.61      www      3832:     $r->print(<<ENDCCF);
1.160     raeburn  3833: <form name="form1" method="post">
1.61      www      3834: <input type="hidden" name="phase" value="set_custom_roles" />
                   3835: <input type="hidden" name="rolename" value="$rolename" />
                   3836: ENDCCF
1.135     raeburn  3837:     $r->print(&Apache::loncommon::start_data_table().
                   3838:               &Apache::loncommon::start_data_table_header_row(). 
                   3839: '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   3840: '</th><th>'.$lt{'ssl'}.'</th>'.
                   3841:               &Apache::loncommon::end_data_table_header_row());
1.324     raeburn  3842:     foreach my $priv (sort(keys(%full))) {
1.318     raeburn  3843:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
1.135     raeburn  3844:         $r->print(&Apache::loncommon::start_data_table_row().
                   3845: 	          '<td>'.$privtext.'</td><td>'.
1.288     bisitz   3846:     ($courselevel{$priv}?'<input type="checkbox" name="'.$priv.'_c"'.
                   3847:     ($courselevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.61      www      3848:     '</td><td>'.
1.288     bisitz   3849:     ($domainlevel{$priv}?'<input type="checkbox" name="'.$priv.'_d"'.
                   3850:     ($domainlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;').
1.324     raeburn  3851:     '</td><td>');
                   3852:         if ($priv eq 'bre' && $crstype eq 'Community') {
                   3853:             $r->print('&nbsp;');  
                   3854:         } else {
                   3855:             $r->print($systemlevel{$priv}?'<input type="checkbox" name="'.$priv.'_s"'.
                   3856:                       ($systemlevelcurrent{$priv}?' checked="checked"':'').' />':'&nbsp;');
                   3857:         }
                   3858:         $r->print('</td>'.
                   3859:                   &Apache::loncommon::end_data_table_row());
1.60      www      3860:     }
1.135     raeburn  3861:     $r->print(&Apache::loncommon::end_data_table().
1.190     raeburn  3862:    '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
1.160     raeburn  3863:    '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
1.179     raeburn  3864:    '" />'."\n".'<input type="hidden" name="currstate" value="" />'."\n".   
1.160     raeburn  3865:    '<input type="reset" value="'.&mt("Reset").'" />'."\n".
1.329.2.5  raeburn  3866:    '<input type="submit" value="'.&mt('Save').'" /></form>');
1.61      www      3867: }
1.153     banghart 3868: # --------------------------------------------------------
                   3869: sub make_script_template {
1.324     raeburn  3870:     my ($role,$crstype) = @_;
1.153     banghart 3871:     my %full_c=();
                   3872:     my %full_d=();
                   3873:     my %full_s=();
                   3874:     my $return_script;
                   3875:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   3876:         my ($priv,$restrict)=split(/\&/,$item);
                   3877:         $full_c{$priv}=1;
                   3878:     }
                   3879:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   3880:         my ($priv,$restrict)=split(/\&/,$item);
                   3881:         $full_d{$priv}=1;
                   3882:     }
1.154     banghart 3883:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1.324     raeburn  3884:         next if (($crstype eq 'Community') && ($item eq 'bre&S'));
1.153     banghart 3885:         my ($priv,$restrict)=split(/\&/,$item);
                   3886:         $full_s{$priv}=1;
                   3887:     }
                   3888:     $return_script .= 'function set_'.$role.'() {'."\n";
                   3889:     my @temp = split(/:/,$Apache::lonnet::pr{$role.':c'});
                   3890:     my %role_c;
1.155     banghart 3891:     foreach my $priv (@temp) {
1.153     banghart 3892:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   3893:         $role_c{$priv_item} = 1;
                   3894:     }
1.269     raeburn  3895:     my %role_d;
                   3896:     @temp = split(/:/,$Apache::lonnet::pr{$role.':d'});
                   3897:     foreach my $priv(@temp) {
                   3898:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   3899:         $role_d{$priv_item} = 1;
                   3900:     }
                   3901:     my %role_s;
                   3902:     @temp = split(/:/,$Apache::lonnet::pr{$role.':s'});
                   3903:     foreach my $priv(@temp) {
                   3904:         my ($priv_item, $dummy) = split(/\&/,$priv);
                   3905:         $role_s{$priv_item} = 1;
                   3906:     }
1.153     banghart 3907:     foreach my $priv_item (keys(%full_c)) {
                   3908:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  3909:         if ((exists($role_c{$priv})) || (exists($role_d{$priv})) || 
                   3910:             (exists($role_s{$priv}))) {
1.153     banghart 3911:             $return_script .= "document.form1.$priv"."_c.checked = true;\n";
                   3912:         } else {
                   3913:             $return_script .= "document.form1.$priv"."_c.checked = false;\n";
                   3914:         }
                   3915:     }
1.154     banghart 3916:     foreach my $priv_item (keys(%full_d)) {
                   3917:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.269     raeburn  3918:         if ((exists($role_d{$priv})) || (exists($role_s{$priv}))) {
1.154     banghart 3919:             $return_script .= "document.form1.$priv"."_d.checked = true;\n";
                   3920:         } else {
                   3921:             $return_script .= "document.form1.$priv"."_d.checked = false;\n";
                   3922:         }
                   3923:     }
                   3924:     foreach my $priv_item (keys(%full_s)) {
1.153     banghart 3925:         my ($priv, $dummy) = split(/\&/,$priv_item);
1.154     banghart 3926:         if (exists($role_s{$priv})) {
                   3927:             $return_script .= "document.form1.$priv"."_s.checked = true;\n";
                   3928:         } else {
                   3929:             $return_script .= "document.form1.$priv"."_s.checked = false;\n";
                   3930:         }
1.153     banghart 3931:     }
                   3932:     $return_script .= '}'."\n";
1.154     banghart 3933:     return ($return_script);
                   3934: }
                   3935: # ----------------------------------------------------------
                   3936: sub make_button_code {
1.318     raeburn  3937:     my ($role,$crstype) = @_;
                   3938:     my $label = &Apache::lonnet::plaintext($role,$crstype);
1.301     bisitz   3939:     my $button_code = '<input type="button" onclick="set_'.$role.'()" value="'.$label.'" />';
1.154     banghart 3940:     return ($button_code);
1.153     banghart 3941: }
1.61      www      3942: # ---------------------------------------------------------- Call to definerole
                   3943: sub set_custom_role {
1.329.2.5  raeburn  3944:     my ($r,$context,$brcrum) = @_;
1.101     albertel 3945:     my $rolename=$env{'form.rolename'};
1.63      www      3946:     $rolename=~s/[^A-Za-z0-9]//gs;
1.150     banghart 3947:     if (!$rolename) {
1.329.2.5  raeburn  3948: 	&custom_role_editor($r,$brcrum);
1.61      www      3949:         return;
                   3950:     }
1.160     raeburn  3951:     my ($jsback,$elements) = &crumb_utilities();
1.301     bisitz   3952:     my $jscript = '<script type="text/javascript">'
                   3953:                  .'// <![CDATA['."\n"
                   3954:                  .$jsback."\n"
                   3955:                  .'// ]]>'."\n"
                   3956:                  .'</script>'."\n";
1.329.2.5  raeburn  3957:     my $bread_crumbs_component = 'User Management';
1.329.2.4  raeburn  3958:     my $is_custom = &Apache::loncommon::needs_gci_custom();
1.329.2.3  raeburn  3959:     if ($context eq 'course') {
1.329.2.4  raeburn  3960:         if ($is_custom) {
1.329.2.5  raeburn  3961:             $bread_crumbs_component = 'Enrollment and Student Activity';
1.329.2.3  raeburn  3962:         }
                   3963:     }
1.329.2.5  raeburn  3964: 
                   3965:     push(@{$brcrum},
                   3966:         {href => "javascript:backPage(document.customresult,'pickrole','')",
                   3967:          text => "Pick custom role",
                   3968:          faq  => 282,
                   3969:          bug  => 'Instructor Interface',},
                   3970:         {href => "javascript:backPage(document.customresult,'selected_custom_edit','')",
                   3971:          text => "Edit custom role",
                   3972:          faq  => 282,
                   3973:          bug  => 'Instructor Interface',},
                   3974:         {href => "javascript:backPage(document.customresult,'set_custom_roles','')",
                   3975:          text => "Result",
                   3976:          faq  => 282,
                   3977:          bug  => 'Instructor Interface',
                   3978:          help => 'Course_Editing_Custom_Roles'},
                   3979:         );
                   3980:     my $args = { bread_crumbs           => $brcrum,
                   3981:                  bread_crumbs_component => $bread_crumbs_component };
                   3982:     $r->print(&Apache::loncommon::start_page('Save Custom Role',$jscript,$args));
1.160     raeburn  3983: 
1.61      www      3984:     my ($rdummy,$roledef)=
1.110     albertel 3985: 	&Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   3986: 
1.61      www      3987: # ------------------------------------------------------- Does this role exist?
1.188     raeburn  3988:     $r->print('<h3>');
1.61      www      3989:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 3990: 	$r->print(&mt('Existing Role').' "');
1.61      www      3991:     } else {
1.73      sakharuk 3992: 	$r->print(&mt('New Role').' "');
1.61      www      3993: 	$roledef='';
                   3994:     }
1.188     raeburn  3995:     $r->print($rolename.'"</h3>');
1.61      www      3996: # ------------------------------------------------------- What can be assigned?
                   3997:     my $sysrole='';
                   3998:     my $domrole='';
                   3999:     my $courole='';
                   4000: 
1.135     raeburn  4001:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   4002: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4003:         if (!$restrict) { $restrict=''; }
                   4004:         if ($env{'form.'.$priv.'_c'}) {
1.135     raeburn  4005: 	    $courole.=':'.$item;
1.61      www      4006: 	}
                   4007:     }
                   4008: 
1.135     raeburn  4009:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   4010: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4011:         if (!$restrict) { $restrict=''; }
                   4012:         if ($env{'form.'.$priv.'_d'}) {
1.135     raeburn  4013: 	    $domrole.=':'.$item;
1.61      www      4014: 	}
                   4015:     }
                   4016: 
1.135     raeburn  4017:     foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   4018: 	my ($priv,$restrict)=split(/\&/,$item);
1.150     banghart 4019:         if (!$restrict) { $restrict=''; }
                   4020:         if ($env{'form.'.$priv.'_s'}) {
1.135     raeburn  4021: 	    $sysrole.=':'.$item;
1.61      www      4022: 	}
                   4023:     }
1.63      www      4024:     $r->print('<br />Defining Role: '.
1.61      www      4025: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.101     albertel 4026:     if ($env{'request.course.id'}) {
                   4027:         my $url='/'.$env{'request.course.id'};
1.63      www      4028:         $url=~s/\_/\//g;
1.73      sakharuk 4029: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.101     albertel 4030: 	      &Apache::lonnet::assigncustomrole($env{'user.domain'},
                   4031: 						$env{'user.name'},
1.63      www      4032: 						$url,
1.101     albertel 4033: 						$env{'user.domain'},
                   4034: 						$env{'user.name'},
1.240     raeburn  4035: 						$rolename,undef,undef,undef,$context));
1.63      www      4036:     }
1.190     raeburn  4037:     $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  4038:     $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
1.58      www      4039: }
                   4040: 
1.2       www      4041: # ================================================================ Main Handler
                   4042: sub handler {
                   4043:     my $r = shift;
                   4044:     if ($r->header_only) {
1.68      www      4045:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      4046:        $r->send_http_header;
                   4047:        return OK;
                   4048:     }
1.318     raeburn  4049:     my ($context,$crstype);
1.190     raeburn  4050:     if ($env{'request.course.id'}) {
                   4051:         $context = 'course';
1.318     raeburn  4052:         $crstype = &Apache::loncommon::course_type();
1.190     raeburn  4053:     } elsif ($env{'request.role'} =~ /^au\./) {
1.206     raeburn  4054:         $context = 'author';
1.190     raeburn  4055:     } else {
                   4056:         $context = 'domain';
                   4057:     }
1.329.2.6! raeburn  4058:     my ($permission,$allowed) =
        !          4059:         &Apache::lonuserutils::get_permission($context,$crstype);
        !          4060:     if (!$allowed) {
        !          4061:         $env{'user.error.msg'}=
        !          4062:             "/adm/createuser:cst:0:0:Cannot create/modify user data ".
        !          4063:                                  "or view user status.";
        !          4064:         return HTTP_NOT_ACCEPTABLE;
        !          4065:     }
        !          4066: 
        !          4067:     &Apache::loncommon::content_type($r,'text/html');
        !          4068:     $r->send_http_header;
        !          4069: 
1.329.2.3  raeburn  4070:     my $title = 'User Management';
1.329.2.4  raeburn  4071:     my $is_custom = &Apache::loncommon::needs_gci_custom();
1.329.2.3  raeburn  4072:     if ($context eq 'course') {
1.329.2.4  raeburn  4073:         if ($is_custom) {
1.329.2.3  raeburn  4074:             $title = 'Enrollment and Student Activity';
                   4075:         }
                   4076:     }
1.190     raeburn  4077:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.233     raeburn  4078:         ['action','state','callingform','roletype','showrole','bulkaction','popup','phase',
                   4079:          'username','domain','srchterm','srchdomain','srchin','srchby','srchtype']);
1.190     raeburn  4080:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.329.2.5  raeburn  4081:     my $args;
                   4082:     my $brcrum = [];
                   4083:     my $bread_crumbs_component = $title;
1.329.2.6! raeburn  4084:     if ($env{'form.context'} eq 'requestcrs') {
        !          4085:         my $reqstate = 'uploadroster';
        !          4086:         if ($env{'form.state'} eq 'enrolling') {
        !          4087:             $reqstate = 'enrolling';
        !          4088:         }
        !          4089:         unless (&Apache::lonrequestcourse::generate_page($r,'new',$reqstate)) {
        !          4090:             return OK;
        !          4091:         }
        !          4092:     } elsif ($env{'form.action'} ne 'dateselect') {
1.329.2.5  raeburn  4093:         $brcrum = [{href=>"/adm/createuser",
                   4094:                     text=>$title,
                   4095:                     help=>'Course_Create_Class_List,Course_Change_Privileges,Course_View_Class_List,Course_Editing_Custom_Roles,Course_Add_Student,Course_Drop_Student,Course_Automated_Enrollment,Course_Self_Enrollment,Course_Manage_Group'}
                   4096:                   ];
1.202     raeburn  4097:     }
1.289     droeschl 4098:     #SD Following files not added to help, because the corresponding .tex-files seem to
                   4099:     #be missing: Course_Approve_Selfenroll,Course_User_Logs,
1.190     raeburn  4100: 
                   4101:     # Main switch on form.action and form.state, as appropriate
                   4102:     if (! exists($env{'form.action'})) {
1.329.2.5  raeburn  4103:         $args = {bread_crumbs => $brcrum,
                   4104:                  bread_crumbs_component => $bread_crumbs_component};
                   4105:         $r->print(&header(undef,$args));
1.318     raeburn  4106:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4107:     } elsif ($env{'form.action'} eq 'upload' && $permission->{'cusr'}) {
1.329.2.6! raeburn  4108:         unless ($env{'form.context'} eq 'requestcrs') {
        !          4109:             push(@{$brcrum},
        !          4110:                   { href => '/adm/createuser?action=upload&state=',
        !          4111:                     text => 'Upload Users List',
        !          4112:                     help => 'Course_Create_Class_List',
        !          4113:                   });
        !          4114:             $bread_crumbs_component = 'Upload Users List';
        !          4115:             $args = {bread_crumbs           => $brcrum,
        !          4116:                      bread_crumbs_component => $bread_crumbs_component};
        !          4117:             $r->print(&header(undef,$args));
        !          4118:         }
        !          4119:         unless (($env{'form.context'} eq 'requestcrs') && ($env{'form.state'} eq 'enrolling')) {
        !          4120:             $r->print('<form name="studentform" method="post" '.
        !          4121:                       'enctype="multipart/form-data" '.
        !          4122:                       ' action="/adm/createuser">'."\n");
        !          4123:         }
1.190     raeburn  4124:         if (! exists($env{'form.state'})) {
                   4125:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4126:         } elsif ($env{'form.state'} eq 'got_file') {
1.329.2.4  raeburn  4127:             my $formname;
1.329.2.6! raeburn  4128:             if ($env{'form.context'} eq 'requestcrs') {
1.329.2.4  raeburn  4129:                 $formname = 'studentform';
                   4130:             }
1.221     raeburn  4131:             &Apache::lonuserutils::print_upload_manager_form($r,$context,
1.329.2.4  raeburn  4132:                                                              $permission,$crstype,$formname);
1.329.2.6! raeburn  4133:             if ($env{'form.context'} eq 'requestcrs') {
        !          4134:                 $r->print('<input type="hidden" name="concepttest" value="'.$env{'form.concepttest'}.'" />');
        !          4135:             }
1.190     raeburn  4136:         } elsif ($env{'form.state'} eq 'enrolling') {
                   4137:             if ($env{'form.datatoken'}) {
1.221     raeburn  4138:                 &Apache::lonuserutils::upfile_drop_add($r,$context,$permission);
1.329.2.6! raeburn  4139:                 if ($env{'form.context'} eq 'requestcrs') {
        !          4140:                     $r->print('<br /><hr />'.
        !          4141:                               '<form name="requestcrs" method="post" action="/adm/requestcourse">'.
        !          4142:                               '<input type="hidden" name="context" value="'.$env{'form.context'}.'" />'.
        !          4143:                               '<input type="hidden" name="concepttest" value="'.$env{'form.concepttest'}.'" />'.
        !          4144:                               '<input type="hidden" name="reqaction" value="new" />'.
        !          4145:                               '<input type="hidden" name="state" value="'.$env{'form.state'}.'" />'.
        !          4146:                               &Apache::lonrequestcourse::done_display());
        !          4147:                 }
1.190     raeburn  4148:             }
                   4149:         } else {
                   4150:             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
                   4151:         }
1.329.2.6! raeburn  4152:         $r->print('</form>');
1.213     raeburn  4153:     } elsif ((($env{'form.action'} eq 'singleuser') || ($env{'form.action'}
                   4154:              eq 'singlestudent')) && ($permission->{'cusr'})) {
1.190     raeburn  4155:         my $phase = $env{'form.phase'};
                   4156:         my @search = ('srchterm','srchby','srchin','srchtype','srchdomain');
1.192     albertel 4157: 	&Apache::loncreateuser::restore_prev_selections();
                   4158: 	my $srch;
                   4159: 	foreach my $item (@search) {
                   4160: 	    $srch->{$item} = $env{'form.'.$item};
                   4161: 	}
1.207     raeburn  4162:         if (($phase eq 'get_user_info') || ($phase eq 'userpicked') ||
                   4163:             ($phase eq 'createnewuser')) {
                   4164:             if ($env{'form.phase'} eq 'createnewuser') {
                   4165:                 my $response;
                   4166:                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
1.329.2.4  raeburn  4167:                     my $response = '<p class="LC_warning">'.&mt('You must specify a valid username. Only the following are allowed: letters numbers - . @').'</p>';
1.221     raeburn  4168:                     $env{'form.phase'} = '';
1.329.2.5  raeburn  4169:                     &print_username_entry_form($r,$context,$response,$srch,undef,$crstype,$brcrum);
1.207     raeburn  4170:                 } else {
                   4171:                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                   4172:                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                   4173:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4174:                                                   $srch,$response,$context,
1.318     raeburn  4175:                                                   $permission,$crstype);
1.207     raeburn  4176:                 }
                   4177:             } elsif ($env{'form.phase'} eq 'get_user_info') {
1.190     raeburn  4178:                 my ($currstate,$response,$forcenewuser,$results) = 
1.221     raeburn  4179:                     &user_search_result($context,$srch);
1.190     raeburn  4180:                 if ($env{'form.currstate'} eq 'modify') {
                   4181:                     $currstate = $env{'form.currstate'};
                   4182:                 }
                   4183:                 if ($currstate eq 'select') {
                   4184:                     &print_user_selection_page($r,$response,$srch,$results,
1.318     raeburn  4185:                                                \@search,$context,undef,$crstype);
1.190     raeburn  4186:                 } elsif ($currstate eq 'modify') {
                   4187:                     my ($ccuname,$ccdomain);
                   4188:                     if (($srch->{'srchby'} eq 'uname') && 
                   4189:                         ($srch->{'srchtype'} eq 'exact')) {
                   4190:                         $ccuname = $srch->{'srchterm'};
                   4191:                         $ccdomain= $srch->{'srchdomain'};
                   4192:                     } else {
                   4193:                         my @matchedunames = keys(%{$results});
                   4194:                         ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]);
                   4195:                     }
                   4196:                     $ccuname =&LONCAPA::clean_username($ccuname);
                   4197:                     $ccdomain=&LONCAPA::clean_domain($ccdomain);
                   4198:                     if ($env{'form.forcenewuser'}) {
                   4199:                         $response = '';
                   4200:                     }
                   4201:                     &print_user_modification_page($r,$ccuname,$ccdomain,
1.221     raeburn  4202:                                                   $srch,$response,$context,
1.329.2.5  raeburn  4203:                                                   $permission,$crstype,$brcrum);
1.190     raeburn  4204:                 } elsif ($currstate eq 'query') {
1.329.2.5  raeburn  4205:                     &print_user_query_page($r,'createuser',$brcrum);
1.190     raeburn  4206:                 } else {
1.229     raeburn  4207:                     $env{'form.phase'} = '';
1.207     raeburn  4208:                     &print_username_entry_form($r,$context,$response,$srch,
1.329.2.5  raeburn  4209:                                                $forcenewuser,$crstype,$brcrum);
1.190     raeburn  4210:                 }
                   4211:             } elsif ($env{'form.phase'} eq 'userpicked') {
                   4212:                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                   4213:                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
1.196     raeburn  4214:                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
1.329.2.5  raeburn  4215:                                               $context,$permission,$crstype,$brcrum);
1.190     raeburn  4216:             }
                   4217:         } elsif ($env{'form.phase'} eq 'update_user_data') {
1.329.2.5  raeburn  4218:             &update_user_data($r,$context,$crstype,$brcrum);
1.190     raeburn  4219:         } else {
1.329.2.5  raeburn  4220:             &print_username_entry_form($r,$context,undef,$srch,undef,$crstype,$brcrum);
1.190     raeburn  4221:         }
                   4222:     } elsif ($env{'form.action'} eq 'custom' && $permission->{'custom'}) {
                   4223:         if ($env{'form.phase'} eq 'set_custom_roles') {
1.329.2.5  raeburn  4224:             &set_custom_role($r,$context,$brcrum);
1.190     raeburn  4225:         } else {
1.329.2.5  raeburn  4226:             &custom_role_editor($r,$brcrum);
1.190     raeburn  4227:         }
1.207     raeburn  4228:     } elsif (($env{'form.action'} eq 'listusers') && 
                   4229:              ($permission->{'view'} || $permission->{'cusr'})) {
1.202     raeburn  4230:         if ($env{'form.phase'} eq 'bulkchange') {
1.329.2.5  raeburn  4231:             push(@{$brcrum},
                   4232:                     {href => '/adm/createuser?action=listusers',
                   4233:                      text => "List Users"},
                   4234:                     {href => "/adm/createuser",
                   4235:                      text => "Result",
                   4236:                      help => 'Course_View_Class_List'});
                   4237:             $bread_crumbs_component = 'Update Users';
                   4238:             $args = {bread_crumbs           => $brcrum,
                   4239:                      bread_crumbs_component => $bread_crumbs_component};
                   4240:             $r->print(&header(undef,$args));
1.202     raeburn  4241:             my $setting = $env{'form.roletype'};
                   4242:             my $choice = $env{'form.bulkaction'};
1.221     raeburn  4243:             $r->print(&Apache::lonhtmlcommon::breadcrumbs("Update Users",
1.224     raeburn  4244:                                                           'Course_View_Class_List'));
1.202     raeburn  4245:             if ($permission->{'cusr'}) {
                   4246:                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice);
1.221     raeburn  4247:                 $r->print(&Apache::loncommon::end_page());
                   4248:             } else {
                   4249:                 $r->print(&mt('You are not authorized to make bulk changes to user roles'));
1.223     raeburn  4250:                 $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
1.202     raeburn  4251:             }
                   4252:         } else {
1.329.2.5  raeburn  4253:             push(@{$brcrum},
                   4254:                     {href => '/adm/createuser?action=listusers',
                   4255:                      text => "List Users",
                   4256:                      help => 'Course_View_Class_List'});
                   4257:             $bread_crumbs_component = 'List Users';
                   4258:             $args = {bread_crumbs           => $brcrum,
                   4259:                      bread_crumbs_component => $bread_crumbs_component};
1.202     raeburn  4260:             my ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles);
                   4261:             my $formname = 'studentform';
1.321     raeburn  4262:             if (($context eq 'domain') && (($env{'form.roletype'} eq 'course') ||
                   4263:                 ($env{'form.roletype'} eq 'community'))) {
                   4264:                 if ($env{'form.roletype'} eq 'course') {
                   4265:                     ($cb_jscript,$jscript,$totcodes,$codetitles,$idlist,$idlist_titles) = 
                   4266:                         &Apache::lonuserutils::courses_selector($env{'request.role.domain'},
                   4267:                                                                 $formname);
                   4268:                 } elsif ($env{'form.roletype'} eq 'community') {
                   4269:                     $cb_jscript = 
                   4270:                         &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
                   4271:                     my %elements = (
                   4272:                                       coursepick => 'radio',
                   4273:                                       coursetotal => 'text',
                   4274:                                       courselist => 'text',
                   4275:                                    );
                   4276:                     $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   4277:                 }
1.202     raeburn  4278:                 $jscript .= &verify_user_display();
                   4279:                 my $js = &add_script($jscript).$cb_jscript;
1.329.2.5  raeburn  4280:                 my $loadcode =
1.202     raeburn  4281:                     &Apache::lonuserutils::course_selector_loadcode($formname);
                   4282:                 if ($loadcode ne '') {
1.329.2.5  raeburn  4283:                     $args->{add_entries} = {onload => $loadcode};
1.202     raeburn  4284:                 }
1.329.2.5  raeburn  4285:                 $r->print(&header($js,$args));
1.191     raeburn  4286:             } else {
1.329.2.5  raeburn  4287:                 $r->print(&header(&add_script(&verify_user_display()),$args));
1.191     raeburn  4288:             }
1.202     raeburn  4289:             &Apache::lonuserutils::print_userlist($r,undef,$permission,$context,
                   4290:                          $formname,$totcodes,$codetitles,$idlist,$idlist_titles);
1.191     raeburn  4291:         }
1.213     raeburn  4292:     } elsif ($env{'form.action'} eq 'drop' && $permission->{'cusr'}) {
1.318     raeburn  4293:         my $brtext;
                   4294:         if ($crstype eq 'Community') {
                   4295:             $brtext = 'Drop Members';
                   4296:         } else {
                   4297:             $brtext = 'Drop Students';
                   4298:         }
1.329.2.5  raeburn  4299:         push(@{$brcrum},
                   4300:                 {href => '/adm/createuser?action=drop',
                   4301:                  text => $brtext,
                   4302:                  help => 'Course_Drop_Student'});
                   4303:         if ($env{'form.state'} eq 'done') {
                   4304:             push(@{$brcrum},
                   4305:                      {href=>'/adm/createuser?action=drop',
                   4306:                       text=>"Result"});
                   4307:         }
                   4308:         $bread_crumbs_component = $brtext;
                   4309:         $args = {bread_crumbs           => $brcrum,
                   4310:                  bread_crumbs_component => $bread_crumbs_component};
                   4311:         $r->print(&header(undef,$args));
1.213     raeburn  4312:         if (!exists($env{'form.state'})) {
1.318     raeburn  4313:             &Apache::lonuserutils::print_drop_menu($r,$context,$permission,$crstype);
1.213     raeburn  4314:         } elsif ($env{'form.state'} eq 'done') {
                   4315:             &Apache::lonuserutils::update_user_list($r,$context,undef,
                   4316:                                                     $env{'form.action'});
                   4317:         }
1.202     raeburn  4318:     } elsif ($env{'form.action'} eq 'dateselect') {
                   4319:         if ($permission->{'cusr'}) {
                   4320:             $r->print(&header(undef,undef,{'no_nav_bar' => 1}).
1.221     raeburn  4321:                       &Apache::lonuserutils::date_section_selector($context,
1.329.2.5  raeburn  4322:                                                                    $permission,$crstype));
1.202     raeburn  4323:         } else {
                   4324:             $r->print(&header().
1.329.2.5  raeburn  4325:                      '<span class="LC_error">'.&mt('You do not have permission to modify dates or sections for users').'</span>');
1.202     raeburn  4326:         }
1.237     raeburn  4327:     } elsif ($env{'form.action'} eq 'selfenroll') {
1.329.2.5  raeburn  4328:         push(@{$brcrum},
                   4329:                 {href => '/adm/createuser?action=selfenroll',
                   4330:                  text => "Configure Self-enrollment",
                   4331:                  help => 'Course_Self_Enrollment'});
1.237     raeburn  4332:         if (!exists($env{'form.state'})) {
1.329.2.5  raeburn  4333:             $args = { bread_crumbs           => $brcrum,
                   4334:                       bread_crumbs_component => 'Configure Self-enrollment'};
                   4335:             $r->print(&header(undef,$args));
1.241     raeburn  4336:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
1.237     raeburn  4337:             &print_selfenroll_menu($r,$context,$permission);
                   4338:         } elsif ($env{'form.state'} eq 'done') {
1.329.2.5  raeburn  4339:             push (@{$brcrum},
                   4340:                       {href=>'/adm/createuser?action=selfenroll',
                   4341:                        text=>"Result"});
                   4342:             $args = { bread_crumbs           => $brcrum,
                   4343:                       bread_crumbs_component => 'Self-enrollment result'};
                   4344:             $r->print(&header(undef,$args));
1.241     raeburn  4345:             $r->print('<h3>'.&mt('Self-enrollment with a student role').'</h3>'."\n");
                   4346:             &update_selfenroll_config($r,$context,$permission);
1.237     raeburn  4347:         }
1.277     raeburn  4348:     } elsif ($env{'form.action'} eq 'selfenrollqueue') {
1.329.2.5  raeburn  4349:         push(@{$brcrum},
                   4350:                  {href => '/adm/createuser?action=selfenrollqueue',
                   4351:                   text => 'Enrollment requests',
                   4352:                   help => 'Course_Self_Enrollment'});
                   4353:         $bread_crumbs_component = 'Enrollment requests';
                   4354:         if ($env{'form.state'} eq 'done') {
                   4355:             push(@{$brcrum},
                   4356:                      {href => '/adm/createuser?action=selfenrollqueue',
                   4357:                       text => 'Result',
                   4358:                       help => 'Course_Self_Enrollment'});
                   4359:             $bread_crumbs_component = 'Enrollment result';
                   4360:         }
                   4361:         $args = { bread_crumbs           => $brcrum,
                   4362:                   bread_crumbs_component => $bread_crumbs_component};
                   4363:         $r->print(&header(undef,$args));
1.277     raeburn  4364:         my $cid = $env{'request.course.id'};
                   4365:         my $cdom = $env{'course.'.$cid.'.domain'};
                   4366:         my $cnum = $env{'course.'.$cid.'.num'};
1.307     raeburn  4367:         my $coursedesc = $env{'course.'.$cid.'.description'};
1.277     raeburn  4368:         if (!exists($env{'form.state'})) {
                   4369:             $r->print('<h3>'.&mt('Pending enrollment requests').'</h3>'."\n");
1.307     raeburn  4370:             $r->print(&Apache::loncoursequeueadmin::display_queued_requests($context,
                   4371:                                                                        $cdom,$cnum));
1.277     raeburn  4372:         } elsif ($env{'form.state'} eq 'done') {
                   4373:             $r->print('<h3>'.&mt('Enrollment request processing').'</h3>'."\n");
1.307     raeburn  4374:             $r->print(&Apache::loncoursequeueadmin::update_request_queue($context,
                   4375:                           $cdom,$cnum,$coursedesc));
1.277     raeburn  4376:         }
1.239     raeburn  4377:     } elsif ($env{'form.action'} eq 'changelogs') {
1.329.2.5  raeburn  4378:         push (@{$brcrum},
                   4379:                  {href => '/adm/createuser?action=changelogs',
                   4380:                   text => 'User Management Logs',
                   4381:                   help => 'Course_User_Logs'});
                   4382:         $bread_crumbs_component = 'User Changes';
                   4383:         $args = { bread_crumbs           => $brcrum,
                   4384:                   bread_crumbs_component => $bread_crumbs_component};
                   4385:         $r->print(&header(undef,$args));
                   4386:         &print_userchangelogs_display($r,$context,$permission);
1.190     raeburn  4387:     } else {
1.329.2.5  raeburn  4388:         $args = { bread_crumbs           => $brcrum,
                   4389:                   bread_crumbs_component => $bread_crumbs_component};
                   4390:         $r->print(&header(undef,$args));
1.318     raeburn  4391:         $r->print(&print_main_menu($permission,$context,$crstype));
1.190     raeburn  4392:     }
1.329.2.5  raeburn  4393:     $r->print(&Apache::loncommon::end_page());
1.190     raeburn  4394:     return OK;
                   4395: }
                   4396: 
                   4397: sub header {
1.329.2.5  raeburn  4398:     my ($jscript,$args) = @_;
                   4399:     my $title = 'User Management';
                   4400:     my $is_custom = &Apache::loncommon::needs_gci_custom();
                   4401:     if ($env{'request.course.id'}) {
                   4402:         if ($is_custom) {
                   4403:             $title = 'Enrollment and Student Activity';
                   4404:         }
                   4405:     }
1.190     raeburn  4406:     my $start_page;
1.329.2.5  raeburn  4407:     if (ref($args) eq 'HASH') {
                   4408:         $start_page=&Apache::loncommon::start_page($title,$jscript,$args);
1.190     raeburn  4409:     } else {
1.329.2.5  raeburn  4410:         $start_page=&Apache::loncommon::start_page($title,$jscript);
1.190     raeburn  4411:     }
                   4412:     return $start_page;
                   4413: }
1.2       www      4414: 
1.191     raeburn  4415: sub add_script {
                   4416:     my ($js) = @_;
1.301     bisitz   4417:     return '<script type="text/javascript">'."\n"
                   4418:           .'// <![CDATA['."\n"
                   4419:           .$js."\n"
                   4420:           .'// ]]>'."\n"
                   4421:           .'</script>'."\n";
1.191     raeburn  4422: }
                   4423: 
1.202     raeburn  4424: sub verify_user_display {
                   4425:     my $output = <<"END";
                   4426: 
                   4427: function display_update() {
                   4428:     document.studentform.action.value = 'listusers';
                   4429:     document.studentform.phase.value = 'display';
                   4430:     document.studentform.submit();
                   4431: }
                   4432: 
                   4433: END
                   4434:     return $output;
                   4435: 
                   4436: }
                   4437: 
1.190     raeburn  4438: ###############################################################
                   4439: ###############################################################
                   4440: #  Menu Phase One
                   4441: sub print_main_menu {
1.318     raeburn  4442:     my ($permission,$context,$crstype) = @_;
1.329.2.4  raeburn  4443:     my $is_custom = &Apache::loncommon::needs_gci_custom();
                   4444:     if (($context eq 'course') && ($is_custom)) {
1.329.2.1  raeburn  4445:         return &print_gci_main_menu($permission,$context,$crstype)
                   4446:     }
1.318     raeburn  4447:     my $linkcontext = $context;
                   4448:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   4449:     if (($context eq 'course') && ($crstype eq 'Community')) {
                   4450:         $linkcontext = lc($crstype);
                   4451:         $stuterm = 'Members';
                   4452:     }
1.208     raeburn  4453:     my %links = (
1.298     droeschl 4454:                 domain => {
                   4455:                             upload     => 'Upload a File of Users',
                   4456:                             singleuser => 'Add/Modify a User',
                   4457:                             listusers  => 'Manage Users',
                   4458:                             },
                   4459:                 author => {
                   4460:                             upload     => 'Upload a File of Co-authors',
                   4461:                             singleuser => 'Add/Modify a Co-author',
                   4462:                             listusers  => 'Manage Co-authors',
                   4463:                             },
                   4464:                 course => {
                   4465:                             upload     => 'Upload a File of Course Users',
                   4466:                             singleuser => 'Add/Modify a Course User',
                   4467:                             listusers  => 'Manage Course Users',
                   4468:                             },
1.318     raeburn  4469:                 community => {
                   4470:                             upload     => 'Upload a File of Community Users',
                   4471:                             singleuser => 'Add/Modify a Community User',
                   4472:                             listusers  => 'Manage Community Users',
                   4473:                            },
                   4474:                 );
                   4475:      my %linktitles = (
                   4476:                 domain => {
                   4477:                             singleuser => 'Add a user to the domain, and/or a course or community in the domain.',
                   4478:                             listusers  => 'Show and manage users in this domain.',
                   4479:                             },
                   4480:                 author => {
                   4481:                             singleuser => 'Add a user with a co- or assistant author role.',
                   4482:                             listusers  => 'Show and manage co- or assistant authors.',
                   4483:                             },
                   4484:                 course => {
                   4485:                             singleuser => 'Add a user with a certain role to this course.',
                   4486:                             listusers  => 'Show and manage users in this course.',
                   4487:                             },
                   4488:                 community => {
                   4489:                             singleuser => 'Add a user with a certain role to this community.',
                   4490:                             listusers  => 'Show and manage users in this community.',
                   4491:                            },
1.298     droeschl 4492:                 );
                   4493:   my @menu = ( {categorytitle => 'Single Users', 
                   4494:          items =>
                   4495:          [
                   4496:             {
1.318     raeburn  4497:              linktext => $links{$linkcontext}{'singleuser'},
1.298     droeschl 4498:              icon => 'edit-redo.png',
                   4499:              #help => 'Course_Change_Privileges',
                   4500:              url => '/adm/createuser?action=singleuser',
                   4501:              permission => $permission->{'cusr'},
1.318     raeburn  4502:              linktitle => $linktitles{$linkcontext}{'singleuser'},
1.298     droeschl 4503:             },
                   4504:          ]},
                   4505: 
                   4506:          {categorytitle => 'Multiple Users',
                   4507:          items => 
                   4508:          [
                   4509:             {
1.318     raeburn  4510:              linktext => $links{$linkcontext}{'upload'},
1.298     droeschl 4511:              icon => 'sctr.png',
                   4512:              #help => 'Course_Create_Class_List',
                   4513:              url => '/adm/createuser?action=upload',
                   4514:              permission => $permission->{'cusr'},
                   4515:              linktitle => 'Upload a CSV or a text file containing users.',
                   4516:             },
                   4517:             {
1.318     raeburn  4518:              linktext => $links{$linkcontext}{'listusers'},
1.298     droeschl 4519:              icon => 'edit-find.png',
                   4520:              #help => 'Course_View_Class_List',
                   4521:              url => '/adm/createuser?action=listusers',
                   4522:              permission => ($permission->{'view'} || $permission->{'cusr'}),
1.318     raeburn  4523:              linktitle => $linktitles{$linkcontext}{'listusers'}, 
1.298     droeschl 4524:             },
                   4525: 
                   4526:          ]},
                   4527: 
                   4528:          {categorytitle => 'Administration',
                   4529:          items => [ ]},
                   4530:        );
                   4531:             
1.265     mielkec  4532:     if ($context eq 'domain'){
1.298     droeschl 4533:         
                   4534:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4535:             {
                   4536:              linktext => 'Custom Roles',
                   4537:              icon => 'emblem-photos.png',
                   4538:              #help => 'Course_Editing_Custom_Roles',
                   4539:              url => '/adm/createuser?action=custom',
                   4540:              permission => $permission->{'custom'},
                   4541:              linktitle => 'Configure a custom role.',
                   4542:             },
                   4543:         );
                   4544:         
1.265     mielkec  4545:     }elsif ($context eq 'course'){
1.298     droeschl 4546:         my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
1.318     raeburn  4547: 
                   4548:         my %linktext = (
                   4549:                          'Course'    => {
                   4550:                                           single => 'Add/Modify a Student', 
                   4551:                                           drop   => 'Drop Students',
                   4552:                                           groups => 'Course Groups',
                   4553:                                         },
                   4554:                          'Community' => {
                   4555:                                           single => 'Add/Modify a Member', 
                   4556:                                           drop   => 'Drop Members',
                   4557:                                           groups => 'Community Groups',
                   4558:                                         },
                   4559:                        );
                   4560: 
                   4561:         my %linktitle = (
                   4562:             'Course' => {
                   4563:                   single => 'Add a user with the role of student to this course',
                   4564:                   drop   => 'Remove a student from this course.',
                   4565:                   groups => 'Manage course groups',
                   4566:                         },
                   4567:             'Community' => {
                   4568:                   single => 'Add a user with the role of member to this community',
                   4569:                   drop   => 'Remove a member from this community.',
                   4570:                   groups => 'Manage community groups',
                   4571:                            },
                   4572:         );
                   4573: 
1.298     droeschl 4574:         push(@{ $menu[0]->{items} }, #Category: Single Users
                   4575:             {   
1.318     raeburn  4576:              linktext => $linktext{$crstype}{'single'},
1.298     droeschl 4577:              #help => 'Course_Add_Student',
                   4578:              icon => 'list-add.png',
                   4579:              url => '/adm/createuser?action=singlestudent',
                   4580:              permission => $permission->{'cusr'},
1.318     raeburn  4581:              linktitle => $linktitle{$crstype}{'single'},
1.298     droeschl 4582:             },
                   4583:         );
                   4584:         
                   4585:         push(@{ $menu[1]->{items} }, #Category: Multiple Users 
                   4586:             {
1.318     raeburn  4587:              linktext => $linktext{$crstype}{'drop'},
1.298     droeschl 4588:              icon => 'edit-undo.png',
                   4589:              #help => 'Course_Drop_Student',
                   4590:              url => '/adm/createuser?action=drop',
                   4591:              permission => $permission->{'cusr'},
1.318     raeburn  4592:              linktitle => $linktitle{$crstype}{'drop'},
1.298     droeschl 4593:             },
                   4594:         );
                   4595:         push(@{ $menu[2]->{items} }, #Category: Administration
                   4596:             {    
                   4597:              linktext => 'Custom Roles',
                   4598:              icon => 'emblem-photos.png',
                   4599:              #help => 'Course_Editing_Custom_Roles',
                   4600:              url => '/adm/createuser?action=custom',
                   4601:              permission => $permission->{'custom'},
                   4602:              linktitle => 'Configure a custom role.',
                   4603:             },
                   4604:             {
1.318     raeburn  4605:              linktext => $linktext{$crstype}{'groups'},
1.298     droeschl 4606:              icon => 'conf.png',
                   4607:              #help => 'Course_Manage_Group',
                   4608:              url => '/adm/coursegroups?refpage=cusr',
                   4609:              permission => $permission->{'grp_manage'},
1.318     raeburn  4610:              linktitle => $linktitle{$crstype}{'groups'},
1.298     droeschl 4611:             },
                   4612:             {
1.328     wenzelju 4613:              linktext => 'Change Log',
1.298     droeschl 4614:              icon => 'document-properties.png',
                   4615:              #help => 'Course_User_Logs',
                   4616:              url => '/adm/createuser?action=changelogs',
                   4617:              permission => $permission->{'cusr'},
                   4618:              linktitle => 'View change log.',
                   4619:             },
                   4620:         );
1.277     raeburn  4621:         if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'}) {
1.298     droeschl 4622:             push(@{ $menu[2]->{items} },
                   4623:                     {   
                   4624:                      linktext => 'Enrollment Requests',
                   4625:                      icon => 'selfenrl-queue.png',
                   4626:                      #help => 'Course_Approve_Selfenroll',
                   4627:                      url => '/adm/createuser?action=selfenrollqueue',
                   4628:                      permission => $permission->{'cusr'},
                   4629:                      linktitle =>'Approve or reject enrollment requests.',
                   4630:                     },
                   4631:             );
1.277     raeburn  4632:         }
1.298     droeschl 4633:         
1.265     mielkec  4634:         if (!exists($permission->{'cusr_section'})){
1.320     raeburn  4635:             if ($crstype ne 'Community') {
                   4636:                 push(@{ $menu[2]->{items} },
                   4637:                     {
                   4638:                      linktext => 'Automated Enrollment',
                   4639:                      icon => 'roles.png',
                   4640:                      #help => 'Course_Automated_Enrollment',
                   4641:                      permission => (&Apache::lonnet::auto_run($cnum,$cdom)
                   4642:                                          && $permission->{'cusr'}),
                   4643:                      url  => '/adm/populate',
                   4644:                      linktitle => 'Automated enrollment manager.',
                   4645:                     }
                   4646:                 );
                   4647:             }
                   4648:             push(@{ $menu[2]->{items} }, 
1.298     droeschl 4649:                 {
                   4650:                  linktext => 'User Self-Enrollment',
                   4651:                  icon => 'cstr.png',
                   4652:                  #help => 'Course_Self_Enrollment',
                   4653:                  url => '/adm/createuser?action=selfenroll',
                   4654:                  permission => $permission->{'cusr'},
1.317     bisitz   4655:                  linktitle => 'Configure user self-enrollment.',
1.298     droeschl 4656:                 },
                   4657:             );
                   4658:         }
1.265     mielkec  4659:     };
                   4660: return Apache::lonhtmlcommon::generate_menu(@menu);
1.250     raeburn  4661: #               { text => 'View Log-in History',
                   4662: #                 help => 'Course_User_Logins',
                   4663: #                 action => 'logins',
                   4664: #                 permission => $permission->{'cusr'},
                   4665: #               });
1.190     raeburn  4666: }
                   4667: 
1.329.2.1  raeburn  4668: sub print_gci_main_menu {
                   4669:     my ($permission,$context,$crstype) = @_;
                   4670:     my $stuterm = lc(&Apache::lonnet::plaintext('st',$crstype));
                   4671:     my ($cnum,$cdom) = &Apache::lonuserutils::get_course_identity();
                   4672:     my %links = (
                   4673:         course => {
                   4674:                     single     => 'Add/Modify a Student',
                   4675:                     drop       => 'Drop Students',
                   4676:                     upload     => 'Upload a File of Course Users',
                   4677:                     singleuser => 'Add/Modify a Course User',
1.329.2.3  raeburn  4678:                     listusers  => 'Concept Test Roster and Student Activity',
1.329.2.1  raeburn  4679:                   },
                   4680:      );
                   4681:      my %linktitles = (
                   4682:         course => {
                   4683:                     singleuser => 'Add a user with a certain role to this course.',
                   4684:                     listusers  => 'Show and manage users in this course.',
                   4685:                     single     => 'Add a user with the role of student to this course',
                   4686:                     drop       => 'Remove a student from this course.',
                   4687:                     upload     => 'Upload a CSV or a text file containing users.', 
                   4688:                   },
                   4689:     );
                   4690:     my @menu = ( {categorytitle => 'Manage Users',
                   4691:          items =>
                   4692:          [
                   4693:             {
                   4694:              linktext => $links{$context}{'single'},
                   4695:              #help => 'Course_Add_Student',
                   4696:              icon => 'list-add.png',
                   4697:              url => '/adm/createuser?action=singlestudent',
                   4698:              permission => $permission->{'cusr'},
                   4699:              linktitle => $linktitles{$context}{'single'},
                   4700: 
                   4701:             },
                   4702:             {
                   4703:              linktext => $links{$context}{'drop'},
                   4704:              icon => 'edit-undo.png',
                   4705:              #help => 'Course_Drop_Student',
                   4706:              url => '/adm/createuser?action=drop',
                   4707:              permission => $permission->{'cusr'},
                   4708:              linktitle => $linktitles{$context}{'drop'},
                   4709:             },
                   4710:             {
                   4711:              linktext => $links{$context}{'upload'},
                   4712:              icon => 'sctr.png',
                   4713:              #help => 'Course_Create_Class_List',
                   4714:              url => '/adm/createuser?action=upload',
                   4715:              permission => $permission->{'cusr'},
                   4716:              linktitle => $linktitles{$context}{'upload'},
                   4717:             },
                   4718:             {
                   4719:              linktext => $links{$context}{'listusers'},
                   4720:              icon => 'edit-find.png',
                   4721:              #help => 'Course_View_Class_List',
                   4722:              url => '/adm/createuser?action=listusers',
                   4723:              permission => ($permission->{'view'} || $permission->{'cusr'}),
                   4724:              linktitle => $linktitles{$context}{'listusers'},
                   4725:             },
                   4726:          ]},
                   4727:          {categorytitle => 'Administration',
                   4728:          items => [ ]},
                   4729:     );
                   4730: 
                   4731:     push(@{ $menu[1]->{items} }, #Category: Administration
                   4732:         {
                   4733:            linktext => 'Change Log',
                   4734:            icon => 'document-properties.png',
                   4735:            #help => 'Course_User_Logs',
                   4736:            url => '/adm/createuser?action=changelogs',
                   4737:            permission => $permission->{'cusr'},
                   4738:            linktitle => 'View change log.',
                   4739:          },
                   4740:     );
                   4741:     return Apache::lonhtmlcommon::generate_menu(@menu);
                   4742: }
                   4743: 
1.189     albertel 4744: sub restore_prev_selections {
                   4745:     my %saveable_parameters = ('srchby'   => 'scalar',
                   4746: 			       'srchin'   => 'scalar',
                   4747: 			       'srchtype' => 'scalar',
                   4748: 			       );
                   4749:     &Apache::loncommon::store_settings('user','user_picker',
                   4750: 				       \%saveable_parameters);
                   4751:     &Apache::loncommon::restore_settings('user','user_picker',
                   4752: 					 \%saveable_parameters);
                   4753: }
                   4754: 
1.237     raeburn  4755: sub print_selfenroll_menu {
                   4756:     my ($r,$context,$permission) = @_;
1.322     raeburn  4757:     my $crstype = &Apache::loncommon::course_type();
1.237     raeburn  4758:     my $formname = 'enrollstudent';
                   4759:     my $nolink = 1;
                   4760:     my ($row,$lt) = &get_selfenroll_titles();
                   4761:     my $groupslist = &Apache::lonuserutils::get_groupslist();
                   4762:     my $setsec_js = 
                   4763:         &Apache::lonuserutils::setsections_javascript($formname,$groupslist);
1.249     raeburn  4764:     my %alerts = &Apache::lonlocal::texthash(
                   4765:         acto => 'Activation of self-enrollment was selected for the following domain(s)',
                   4766:         butn => 'but no user types have been checked.',
                   4767:         wilf => "Please uncheck 'activate' or check at least one type.",
                   4768:     );
                   4769:     my $selfenroll_js = <<"ENDSCRIPT";
                   4770: function update_types(caller,num) {
                   4771:     var delidx = getIndexByName('selfenroll_delete');
                   4772:     var actidx = getIndexByName('selfenroll_activate');
                   4773:     if (caller == 'selfenroll_all') {
                   4774:         var selall;
                   4775:         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   4776:             if (document.$formname.selfenroll_all[i].checked) {
                   4777:                 selall = document.$formname.selfenroll_all[i].value;
                   4778:             }
                   4779:         }
                   4780:         if (selall == 1) {
                   4781:             if (delidx != -1) {
                   4782:                 if (document.$formname.selfenroll_delete.length) {
                   4783:                     for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   4784:                         document.$formname.selfenroll_delete[j].checked = true;
                   4785:                     }
                   4786:                 } else {
                   4787:                     document.$formname.elements[delidx].checked = true;
                   4788:                 }
                   4789:             }
                   4790:             if (actidx != -1) {
                   4791:                 if (document.$formname.selfenroll_activate.length) {
                   4792:                     for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   4793:                         document.$formname.selfenroll_activate[j].checked = false;
                   4794:                     }
                   4795:                 } else {
                   4796:                     document.$formname.elements[actidx].checked = false;
                   4797:                 }
                   4798:             }
                   4799:             document.$formname.selfenroll_newdom.selectedIndex = 0; 
                   4800:         }
                   4801:     }
                   4802:     if (caller == 'selfenroll_activate') {
                   4803:         if (document.$formname.selfenroll_activate.length) {
                   4804:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   4805:                 if (document.$formname.selfenroll_activate[j].value == num) {
                   4806:                     if (document.$formname.selfenroll_activate[j].checked) {
                   4807:                         for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   4808:                             if (document.$formname.selfenroll_all[i].value == '1') {
                   4809:                                 document.$formname.selfenroll_all[i].checked = false;
                   4810:                             }
                   4811:                             if (document.$formname.selfenroll_all[i].value == '0') {
                   4812:                                 document.$formname.selfenroll_all[i].checked = true;
                   4813:                             }
                   4814:                         }
                   4815:                     }
                   4816:                 }
                   4817:             }
                   4818:         } else {
                   4819:             for (var i=0; i<document.$formname.selfenroll_all.length; i++) {
                   4820:                 if (document.$formname.selfenroll_all[i].value == '1') {
                   4821:                     document.$formname.selfenroll_all[i].checked = false;
                   4822:                 }
                   4823:                 if (document.$formname.selfenroll_all[i].value == '0') {
                   4824:                     document.$formname.selfenroll_all[i].checked = true;
                   4825:                 }
                   4826:             }
                   4827:         }
                   4828:     }
                   4829:     if (caller == 'selfenroll_delete') {
                   4830:         if (document.$formname.selfenroll_delete.length) {
                   4831:             for (var j=0; j<document.$formname.selfenroll_delete.length; j++) {
                   4832:                 if (document.$formname.selfenroll_delete[j].value == num) {
                   4833:                     if (document.$formname.selfenroll_delete[j].checked) {
                   4834:                         var delindex = getIndexByName('selfenroll_types_'+num);
                   4835:                         if (delindex != -1) { 
                   4836:                             if (document.$formname.elements[delindex].length) {
                   4837:                                 for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   4838:                                     document.$formname.elements[delindex][k].checked = false;
                   4839:                                 }
                   4840:                             } else {
                   4841:                                 document.$formname.elements[delindex].checked = false;
                   4842:                             }
                   4843:                         }
                   4844:                     }
                   4845:                 }
                   4846:             }
                   4847:         } else {
                   4848:             if (document.$formname.selfenroll_delete.checked) {
                   4849:                 var delindex = getIndexByName('selfenroll_types_'+num);
                   4850:                 if (delindex != -1) {
                   4851:                     if (document.$formname.elements[delindex].length) {
                   4852:                         for (var k=0; k<document.$formname.elements[delindex].length; k++) {
                   4853:                             document.$formname.elements[delindex][k].checked = false;
                   4854:                         }
                   4855:                     } else {
                   4856:                         document.$formname.elements[delindex].checked = false;
                   4857:                     }
                   4858:                 }
                   4859:             }
                   4860:         }
                   4861:     }
                   4862:     return;
                   4863: }
                   4864: 
                   4865: function validate_types(form) {
                   4866:     var needaction = new Array();
                   4867:     var countfail = 0;
                   4868:     var actidx = getIndexByName('selfenroll_activate');
                   4869:     if (actidx != -1) {
                   4870:         if (document.$formname.selfenroll_activate.length) {
                   4871:             for (var j=0; j<document.$formname.selfenroll_activate.length; j++) {
                   4872:                 var num = document.$formname.selfenroll_activate[j].value;
                   4873:                 if (document.$formname.selfenroll_activate[j].checked) {
                   4874:                     countfail = check_types(num,countfail,needaction)
                   4875:                 }
                   4876:             }
                   4877:         } else {
                   4878:             if (document.$formname.selfenroll_activate.checked) {
                   4879:                 var num = document.enrollstudent.selfenroll_activate.value;
                   4880:                 countfail = check_types(num,countfail,needaction)
                   4881:             }
                   4882:         }
                   4883:     }
                   4884:     if (countfail > 0) {
                   4885:         var msg = "$alerts{'acto'}\\n";
                   4886:         var loopend = needaction.length -1;
                   4887:         if (loopend > 0) {
                   4888:             for (var m=0; m<loopend; m++) {
                   4889:                 msg += needaction[m]+", ";
                   4890:             }
                   4891:         }
                   4892:         msg += needaction[loopend]+"\\n$alerts{'butn'}\\n$alerts{'wilf'}";
                   4893:         alert(msg);
                   4894:         return; 
                   4895:     }
                   4896:     setSections(form);
                   4897: }
                   4898: 
                   4899: function check_types(num,countfail,needaction) {
                   4900:     var typeidx = getIndexByName('selfenroll_types_'+num);
                   4901:     var count = 0;
                   4902:     if (typeidx != -1) {
                   4903:         if (document.$formname.elements[typeidx].length) {
                   4904:             for (var k=0; k<document.$formname.elements[typeidx].length; k++) {
                   4905:                 if (document.$formname.elements[typeidx][k].checked) {
                   4906:                     count ++;
                   4907:                 }
                   4908:             }
                   4909:         } else {
                   4910:             if (document.$formname.elements[typeidx].checked) {
                   4911:                 count ++;
                   4912:             }
                   4913:         }
                   4914:         if (count == 0) {
                   4915:             var domidx = getIndexByName('selfenroll_dom_'+num);
                   4916:             if (domidx != -1) {
                   4917:                 var domname = document.$formname.elements[domidx].value;
                   4918:                 needaction[countfail] = domname;
                   4919:                 countfail ++;
                   4920:             }
                   4921:         }
                   4922:     }
                   4923:     return countfail;
                   4924: }
                   4925: 
                   4926: function getIndexByName(item) {
                   4927:     for (var i=0;i<document.$formname.elements.length;i++) {
                   4928:         if (document.$formname.elements[i].name == item) {
                   4929:             return i;
                   4930:         }
                   4931:     }
                   4932:     return -1;
                   4933: }
                   4934: ENDSCRIPT
1.256     raeburn  4935:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4936:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   4937: 
1.237     raeburn  4938:     my $output = '<script type="text/javascript">'."\n".
1.301     bisitz   4939:                  '// <![CDATA['."\n".
1.249     raeburn  4940:                  $setsec_js."\n".$selfenroll_js."\n".
1.301     bisitz   4941:                  '// ]]>'."\n".
1.237     raeburn  4942:                  '</script>'."\n".
1.256     raeburn  4943:                  '<h3>'.$lt->{'selfenroll'}.'</h3>'."\n";
                   4944:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   4945:     if (ref($visactions) eq 'HASH') {
                   4946:         if ($visible) {
1.283     bisitz   4947:             $output .= '<p class="LC_info">'.$visactions->{'vis'}.'</p>';
1.256     raeburn  4948:         } else {
1.283     bisitz   4949:             $output .= '<p class="LC_warning">'.$visactions->{'miss'}.'</p>'
                   4950:                       .$visactions->{'yous'}.
1.256     raeburn  4951:                        '<p>'.$visactions->{'gen'}.'<br />'.$visactions->{'coca'};
                   4952:             if (ref($vismsgs) eq 'ARRAY') {
                   4953:                 $output .= '<br />'.$visactions->{'make'}.'<ul>';
                   4954:                 foreach my $item (@{$vismsgs}) {
                   4955:                     $output .= '<li>'.$visactions->{$item}.'</li>';
                   4956:                 }
                   4957:                 $output .= '</ul>';
                   4958:             }
                   4959:             $output .= '</p>';
                   4960:         }
                   4961:     }
                   4962:     $output .= '<form name="'.$formname.'" method="post" action="/adm/createuser">'."\n".
                   4963:                &Apache::lonhtmlcommon::start_pick_box();
1.237     raeburn  4964:     if (ref($row) eq 'ARRAY') {
                   4965:         foreach my $item (@{$row}) {
                   4966:             my $title = $item; 
                   4967:             if (ref($lt) eq 'HASH') {
                   4968:                 $title = $lt->{$item};
                   4969:             }
1.297     bisitz   4970:             $output .= &Apache::lonhtmlcommon::row_title($title);
1.237     raeburn  4971:             if ($item eq 'types') {
                   4972:                 my $curr_types = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_types'};
1.241     raeburn  4973:                 my $showdomdesc = 1;
                   4974:                 my $includeempty = 1;
                   4975:                 my $num = 0;
                   4976:                 $output .= &Apache::loncommon::start_data_table().
                   4977:                            &Apache::loncommon::start_data_table_row()
                   4978:                            .'<td colspan="2"><span class="LC_nobreak"><label>'
                   4979:                            .&mt('Any user in any domain:')
                   4980:                            .'&nbsp;<input type="radio" name="selfenroll_all" value="1" ';
                   4981:                 if ($curr_types eq '*') {
                   4982:                     $output .= ' checked="checked" '; 
                   4983:                 }
1.249     raeburn  4984:                 $output .= 'onchange="javascript:update_types('.
                   4985:                            "'selfenroll_all'".');" />'.&mt('Yes').'</label>'.
                   4986:                            '&nbsp;&nbsp;<input type="radio" name="selfenroll_all" value="0" ';
1.241     raeburn  4987:                 if ($curr_types ne '*') {
                   4988:                     $output .= ' checked="checked" ';
                   4989:                 }
1.249     raeburn  4990:                 $output .= ' onchange="javascript:update_types('.
                   4991:                            "'selfenroll_all'".');"/>'.&mt('No').'</label></td>'.
                   4992:                            &Apache::loncommon::end_data_table_row().
                   4993:                            &Apache::loncommon::end_data_table().
                   4994:                            &mt('Or').'<br />'.
                   4995:                            &Apache::loncommon::start_data_table();
1.241     raeburn  4996:                 my %currdoms;
1.249     raeburn  4997:                 if ($curr_types eq '') {
1.241     raeburn  4998:                     $output .= &new_selfenroll_dom_row($cdom,'0');
                   4999:                 } elsif ($curr_types ne '*') {
                   5000:                     my @entries = split(/;/,$curr_types);
                   5001:                     if (@entries > 0) {
                   5002:                         foreach my $entry (@entries) {
                   5003:                             my ($currdom,$typestr) = split(/:/,$entry);
                   5004:                             $currdoms{$currdom} = 1;
                   5005:                             my $domdesc = &Apache::lonnet::domain($currdom);
1.249     raeburn  5006:                             my @currinsttypes = split(',',$typestr);
1.241     raeburn  5007:                             $output .= &Apache::loncommon::start_data_table_row()
                   5008:                                        .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'<b>'
                   5009:                                        .'&nbsp;'.$domdesc.' ('.$currdom.')'
                   5010:                                        .'</b><input type="hidden" name="selfenroll_dom_'.$num
                   5011:                                        .'" value="'.$currdom.'" /></span><br />'
                   5012:                                        .'<span class="LC_nobreak"><label><input type="checkbox" '
1.249     raeburn  5013:                                        .'name="selfenroll_delete" value="'.$num.'" onchange="javascript:update_types('."'selfenroll_delete','$num'".');" />'
1.241     raeburn  5014:                                        .&mt('Delete').'</label></span></td>';
1.249     raeburn  5015:                             $output .= '<td valign="top">&nbsp;&nbsp;'.&mt('User types:').'<br />'
1.241     raeburn  5016:                                        .&selfenroll_inst_types($num,$currdom,\@currinsttypes).'</td>'
                   5017:                                        .&Apache::loncommon::end_data_table_row();
                   5018:                             $num ++;
                   5019:                         }
                   5020:                     }
                   5021:                 }
1.249     raeburn  5022:                 my $add_domtitle = &mt('Users in additional domain:');
1.241     raeburn  5023:                 if ($curr_types eq '*') { 
1.249     raeburn  5024:                     $add_domtitle = &mt('Users in specific domain:');
1.241     raeburn  5025:                 } elsif ($curr_types eq '') {
1.249     raeburn  5026:                     $add_domtitle = &mt('Users in other domain:');
1.241     raeburn  5027:                 }
                   5028:                 $output .= &Apache::loncommon::start_data_table_row()
                   5029:                            .'<td colspan="2"><span class="LC_nobreak">'.$add_domtitle.'</span><br />'
                   5030:                            .&Apache::loncommon::select_dom_form('','selfenroll_newdom',
                   5031:                                                                 $includeempty,$showdomdesc)
                   5032:                            .'<input type="hidden" name="selfenroll_types_total" value="'.$num.'" />'
                   5033:                            .'</td>'.&Apache::loncommon::end_data_table_row()
                   5034:                            .&Apache::loncommon::end_data_table();
1.237     raeburn  5035:             } elsif ($item eq 'registered') {
                   5036:                 my ($regon,$regoff);
                   5037:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_registered'}) {
                   5038:                     $regon = ' checked="checked" ';
                   5039:                     $regoff = ' ';
                   5040:                 } else {
                   5041:                     $regon = ' ';
                   5042:                     $regoff = ' checked="checked" ';
                   5043:                 }
                   5044:                 $output .= '<label>'.
1.245     raeburn  5045:                            '<input type="radio" name="selfenroll_registered" value="1"'.$regon.'/>'.
1.244     bisitz   5046:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
1.245     raeburn  5047:                            '<input type="radio" name="selfenroll_registered" value="0"'.$regoff.'/>'.
1.244     bisitz   5048:                            &mt('No').'</label>';
1.237     raeburn  5049:             } elsif ($item eq 'enroll_dates') {
                   5050:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_date'};
                   5051:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_date'};
                   5052:                 if ($starttime eq '') {
                   5053:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5054:                 }
                   5055:                 if ($endtime eq '') {
                   5056:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5057:                 }
                   5058:                 my $startform =
                   5059:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_date',$starttime,
                   5060:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5061:                 my $endform =
                   5062:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_date',$endtime,
                   5063:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5064:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5065:             } elsif ($item eq 'access_dates') {
                   5066:                 my $starttime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_start_access'};
                   5067:                 my $endtime = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_end_access'};
                   5068:                 if ($starttime eq '') {
                   5069:                     $starttime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   5070:                 }
                   5071:                 if ($endtime eq '') {
                   5072:                     $endtime = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   5073:                 }
                   5074:                 my $startform =
                   5075:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_start_access',$starttime,
                   5076:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5077:                 my $endform =
                   5078:                     &Apache::lonhtmlcommon::date_setter($formname,'selfenroll_end_access',$endtime,
                   5079:                                       undef,undef,undef,undef,undef,undef,undef,$nolink);
                   5080:                 $output .= &selfenroll_date_forms($startform,$endform);
                   5081:             } elsif ($item eq 'section') {
                   5082:                 my $currsec = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_section'}; 
                   5083:                 my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   5084:                 my $newsecval;
                   5085:                 if ($currsec ne 'none' && $currsec ne '') {
                   5086:                     if (!defined($sections_count{$currsec})) {
                   5087:                         $newsecval = $currsec;
                   5088:                     }
                   5089:                 }
                   5090:                 my $sections_select = 
                   5091:                     &Apache::lonuserutils::course_sections(\%sections_count,'st',$currsec);
                   5092:                 $output .= '<table class="LC_createuser">'."\n".
                   5093:                            '<tr class="LC_section_row">'."\n".
                   5094:                            '<td align="center">'.&mt('Existing sections')."\n".
                   5095:                            '<br />'.$sections_select.'</td><td align="center">'.
                   5096:                            &mt('New section').'<br />'."\n".
                   5097:                            '<input type="text" name="newsec" size="15" value="'.$newsecval.'" />'."\n".
                   5098:                            '<input type="hidden" name="sections" value="" />'."\n".
                   5099:                            '<input type="hidden" name="state" value="done" />'."\n".
                   5100:                            '</td></tr></table>'."\n";
1.276     raeburn  5101:             } elsif ($item eq 'approval') {
                   5102:                 my ($appon,$appoff);
                   5103:                 my $cid = $env{'request.course.id'};
                   5104:                 my $currnotified = $env{'course.'.$cid.'.internal.selfenroll_notifylist'};
                   5105:                 if ($env{'course.'.$cid.'.internal.selfenroll_approval'}) {
                   5106:                     $appon = ' checked="checked" ';
                   5107:                     $appoff = ' ';
                   5108:                 } else {
                   5109:                     $appon = ' ';
                   5110:                     $appoff = ' checked="checked" ';
                   5111:                 }
                   5112:                 $output .= '<label>'.
                   5113:                            '<input type="radio" name="selfenroll_approval" value="1"'.$appon.'/>'.
                   5114:                            &mt('Yes').'</label>&nbsp;&nbsp;<label>'.
                   5115:                            '<input type="radio" name="selfenroll_approval" value="0"'.$appoff.'/>'.
                   5116:                            &mt('No').'</label>';
                   5117:                 my %advhash = &Apache::lonnet::get_course_adv_roles($cid,1);
                   5118:                 my (@ccs,%notified);
1.322     raeburn  5119:                 my $ccrole = 'cc';
                   5120:                 if ($crstype eq 'Community') {
                   5121:                     $ccrole = 'co';
                   5122:                 }
                   5123:                 if ($advhash{$ccrole}) {
                   5124:                     @ccs = split(/,/,$advhash{$ccrole});
1.276     raeburn  5125:                 }
                   5126:                 if ($currnotified) {
                   5127:                     foreach my $current (split(/,/,$currnotified)) {
                   5128:                         $notified{$current} = 1;
                   5129:                         if (!grep(/^\Q$current\E$/,@ccs)) {
                   5130:                             push(@ccs,$current);
                   5131:                         }
                   5132:                     }
                   5133:                 }
                   5134:                 if (@ccs) {
1.277     raeburn  5135:                     $output .= '<br />'.&mt('Personnel to be notified when an enrollment request needs approval, or has been approved:').'&nbsp;'.&Apache::loncommon::start_data_table().
1.276     raeburn  5136:                                &Apache::loncommon::start_data_table_row();
                   5137:                     my $count = 0;
                   5138:                     my $numcols = 4;
                   5139:                     foreach my $cc (sort(@ccs)) {
                   5140:                         my $notifyon;
                   5141:                         my ($ccuname,$ccudom) = split(/:/,$cc);
                   5142:                         if ($notified{$cc}) {
                   5143:                             $notifyon = ' checked="checked" ';
                   5144:                         }
                   5145:                         if ($count && !$count%$numcols) {
                   5146:                             $output .= &Apache::loncommon::end_data_table_row().
                   5147:                                        &Apache::loncommon::start_data_table_row()
                   5148:                         }
                   5149:                         $output .= '<td><span class="LC_nobreak"><label>'.
                   5150:                                    '<input type="checkbox" name="selfenroll_notify"'.$notifyon.' value="'.$cc.'" />'.
                   5151:                                    &Apache::loncommon::plainname($ccuname,$ccudom).
                   5152:                                    '</label></span></td>';
                   5153:                         $count;
                   5154:                     }
                   5155:                     my $rem = $count%$numcols;
                   5156:                     if ($rem) {
                   5157:                         my $emptycols = $numcols - $rem;
                   5158:                         for (my $i=0; $i<$emptycols; $i++) { 
                   5159:                             $output .= '<td>&nbsp;</td>';
                   5160:                         }
                   5161:                     }
                   5162:                     $output .= &Apache::loncommon::end_data_table_row().
                   5163:                                &Apache::loncommon::end_data_table();
                   5164:                 }
                   5165:             } elsif ($item eq 'limit') {
                   5166:                 my ($crslimit,$selflimit,$nolimit);
                   5167:                 my $cid = $env{'request.course.id'};
                   5168:                 my $currlim = $env{'course.'.$cid.'.internal.selfenroll_limit'};
                   5169:                 my $currcap = $env{'course.'.$cid.'.internal.selfenroll_cap'};
                   5170:                 my $nolimit = ' checked="checked" ';
                   5171:                 if ($currlim eq 'allstudents') {
                   5172:                     $crslimit = ' checked="checked" ';
                   5173:                     $selflimit = ' ';
                   5174:                     $nolimit = ' ';
                   5175:                 } elsif ($currlim eq 'selfenrolled') {
                   5176:                     $crslimit = ' ';
                   5177:                     $selflimit = ' checked="checked" ';
                   5178:                     $nolimit = ' '; 
                   5179:                 } else {
                   5180:                     $crslimit = ' ';
                   5181:                     $selflimit = ' ';
                   5182:                 }
                   5183:                 $output .= '<table><tr><td><label>'.
1.278     raeburn  5184:                            '<input type="radio" name="selfenroll_limit" value="none"'.$nolimit.'/>'.
1.276     raeburn  5185:                            &mt('No limit').'</label></td><td><label>'.
                   5186:                            '<input type="radio" name="selfenroll_limit" value="allstudents"'.$crslimit.'/>'.
                   5187:                            &mt('Limit by total students').'</label></td><td><label>'.
                   5188:                            '<input type="radio" name="selfenroll_limit" value="selfenrolled"'.$selflimit.'/>'.
                   5189:                            &mt('Limit by total self-enrolled students').
                   5190:                            '</td></tr><tr>'.
                   5191:                            '<td>&nbsp;</td><td colspan="2"><span class="LC_nobreak">'.
                   5192:                            ('&nbsp;'x3).&mt('Maximum number allowed: ').
                   5193:                            '<input type="text" name="selfenroll_cap" size = "5" value="'.$currcap.'" /></td></tr></table>';
1.237     raeburn  5194:             }
                   5195:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                   5196:         }
                   5197:     }
                   5198:     $output .= &Apache::lonhtmlcommon::end_pick_box().
1.241     raeburn  5199:                '<br /><input type="button" name="selfenrollconf" value="'
1.282     schafran 5200:                .&mt('Save').'" onclick="validate_types(this.form);" />'
1.241     raeburn  5201:                .'<input type="hidden" name="action" value="selfenroll" /></form>';
1.237     raeburn  5202:     $r->print($output);
                   5203:     return;
                   5204: }
                   5205: 
1.256     raeburn  5206: sub visible_in_cat {
                   5207:     my ($cdom,$cnum) = @_;
                   5208:     my %domconf = &Apache::lonnet::get_dom('configuration',['coursecategories'],$cdom);
                   5209:     my ($cathash,%settable,@vismsgs,$cansetvis);
                   5210:     my %visactions = &Apache::lonlocal::texthash(
1.316     bisitz   5211:                    vis => 'Your course/community currently appears in the Course/Community Catalog for this domain.',
1.256     raeburn  5212:                    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.',
1.316     bisitz   5213:                    miss => 'Your course/community does not currently appear in the Course/Community Catalog for this domain.',
1.256     raeburn  5214:                    yous => 'You should remedy this if you plan to allow self-enrollment, otherwise students will have difficulty finding your course.',
                   5215:                    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.',
1.282     schafran 5216:                    make => 'Make any changes to self-enrollment settings below, click "Save", then take action to include the course in the Catalog:',
1.256     raeburn  5217:                    take => 'Take the following action to ensure the course appears in the Catalog:',
                   5218:                    dc_unhide  => 'Ask a domain coordinator to change the "Exclude from course catalog" setting.',
                   5219:                    dc_addinst => 'Ask a domain coordinator to enable display the catalog of "Official courses (with institutional codes)".',
                   5220:                    dc_instcode => 'Ask a domain coordinator to assign an institutional code (if this is an official course).',
                   5221:                    dc_catalog  => 'Ask a domain coordinator to enable or create at least one course category in the domain.',
                   5222:                    dc_categories => 'Ask a domain coordinator to create a hierarchy of categories and sub categories for courses in the domain.',
                   5223:                    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',
                   5224:                    dc_addcat => 'Ask a domain coordinator to assign a category to the course.',
                   5225:     );
1.261     raeburn  5226:     $visactions{'unhide'} = &mt('Use [_1]Set course environment[_2] to change the "Exclude from course catalog" setting.','"<a href="/adm/parmset?action=crsenv">','</a>"');
                   5227:     $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>"');
                   5228:     $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  5229:     if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5230:         if ($domconf{'coursecategories'}{'togglecats'} eq 'crs') {
                   5231:             $settable{'togglecats'} = 1;
                   5232:         }
                   5233:         if ($domconf{'coursecategories'}{'categorize'} eq 'crs') {
                   5234:             $settable{'categorize'} = 1;
                   5235:         }
                   5236:         $cathash = $domconf{'coursecategories'}{'cats'};
                   5237:     }
1.260     raeburn  5238:     if ($settable{'togglecats'} && $settable{'categorize'}) {
1.256     raeburn  5239:         $cansetvis = &mt('You are able to both assign a course category and choose to exclude this course from the catalog.');   
                   5240:     } elsif ($settable{'togglecats'}) {
                   5241:         $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  5242:     } elsif ($settable{'categorize'}) {
1.256     raeburn  5243:         $cansetvis = &mt('You may assign a course category, but only a Domain Coordinator may choose to exclude this course from the catalog.');  
                   5244:     } else {
                   5245:         $cansetvis = &mt('Only a Domain Coordinator may assign a course category or choose to exclude this course from the catalog.'); 
                   5246:     }
                   5247:      
                   5248:     my %currsettings =
                   5249:         &Apache::lonnet::get('environment',['hidefromcat','categories','internal.coursecode'],
                   5250:                              $cdom,$cnum);
                   5251:     my $visible = 0;
                   5252:     if ($currsettings{'internal.coursecode'} ne '') {
                   5253:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5254:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5255:             if (ref($cathash) eq 'HASH') {
                   5256:                 if ($cathash->{'instcode::0'} eq '') {
                   5257:                     push(@vismsgs,'dc_addinst'); 
                   5258:                 } else {
                   5259:                     $visible = 1;
                   5260:                 }
                   5261:             } else {
                   5262:                 $visible = 1;
                   5263:             }
                   5264:         } else {
                   5265:             $visible = 1;
                   5266:         }
                   5267:     } else {
                   5268:         if (ref($cathash) eq 'HASH') {
                   5269:             if ($cathash->{'instcode::0'} ne '') {
                   5270:                 push(@vismsgs,'dc_instcode');
                   5271:             }
                   5272:         } else {
                   5273:             push(@vismsgs,'dc_instcode');
                   5274:         }
                   5275:     }
                   5276:     if ($currsettings{'categories'} ne '') {
                   5277:         my $cathash;
                   5278:         if (ref($domconf{'coursecategories'}) eq 'HASH') {
                   5279:             $cathash = $domconf{'coursecategories'}{'cats'};
                   5280:             if (ref($cathash) eq 'HASH') {
                   5281:                 if (keys(%{$cathash}) == 0) {
                   5282:                     push(@vismsgs,'dc_catalog');
                   5283:                 } elsif ((keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} ne '')) {
                   5284:                     push(@vismsgs,'dc_categories');
                   5285:                 } else {
                   5286:                     my @currcategories = split('&',$currsettings{'categories'});
                   5287:                     my $matched = 0;
                   5288:                     foreach my $cat (@currcategories) {
                   5289:                         if ($cathash->{$cat} ne '') {
                   5290:                             $visible = 1;
                   5291:                             $matched = 1;
                   5292:                             last;
                   5293:                         }
                   5294:                     }
                   5295:                     if (!$matched) {
1.260     raeburn  5296:                         if ($settable{'categorize'}) { 
1.256     raeburn  5297:                             push(@vismsgs,'chgcat');
                   5298:                         } else {
                   5299:                             push(@vismsgs,'dc_chgcat');
                   5300:                         }
                   5301:                     }
                   5302:                 }
                   5303:             }
                   5304:         }
                   5305:     } else {
                   5306:         if (ref($cathash) eq 'HASH') {
                   5307:             if ((keys(%{$cathash}) > 1) || 
                   5308:                 (keys(%{$cathash}) == 1) && ($cathash->{'instcode::0'} eq '')) {
1.260     raeburn  5309:                 if ($settable{'categorize'}) {
1.256     raeburn  5310:                     push(@vismsgs,'addcat');
                   5311:                 } else {
                   5312:                     push(@vismsgs,'dc_addcat');
                   5313:                 }
                   5314:             }
                   5315:         }
                   5316:     }
                   5317:     if ($currsettings{'hidefromcat'} eq 'yes') {
                   5318:         $visible = 0;
                   5319:         if ($settable{'togglecats'}) {
                   5320:             unshift(@vismsgs,'unhide');
                   5321:         } else {
                   5322:             unshift(@vismsgs,'dc_unhide')
                   5323:         }
                   5324:     }
                   5325:     return ($visible,$cansetvis,\@vismsgs,\%visactions);
                   5326: }
                   5327: 
1.241     raeburn  5328: sub new_selfenroll_dom_row {
                   5329:     my ($newdom,$num) = @_;
                   5330:     my $domdesc = &Apache::lonnet::domain($newdom);
                   5331:     my $output;
                   5332:     if ($domdesc ne '') {
                   5333:         $output .= &Apache::loncommon::start_data_table_row()
                   5334:                    .'<td valign="top"><span class="LC_nobreak">'.&mt('Domain:').'&nbsp;<b>'.$domdesc
                   5335:                    .' ('.$newdom.')</b><input type="hidden" name="selfenroll_dom_'.$num
1.249     raeburn  5336:                    .'" value="'.$newdom.'" /></span><br />'
                   5337:                    .'<span class="LC_nobreak"><label><input type="checkbox" '
                   5338:                    .'name="selfenroll_activate" value="'.$num.'" '
                   5339:                    .'onchange="javascript:update_types('
                   5340:                    ."'selfenroll_activate','$num'".');" />'
                   5341:                    .&mt('Activate').'</label></span></td>';
1.241     raeburn  5342:         my @currinsttypes;
                   5343:         $output .= '<td>'.&mt('User types:').'<br />'
                   5344:                    .&selfenroll_inst_types($num,$newdom,\@currinsttypes).'</td>'
                   5345:                    .&Apache::loncommon::end_data_table_row();
                   5346:     }
                   5347:     return $output;
                   5348: }
                   5349: 
                   5350: sub selfenroll_inst_types {
                   5351:     my ($num,$currdom,$currinsttypes) = @_;
                   5352:     my $output;
                   5353:     my $numinrow = 4;
                   5354:     my $count = 0;
                   5355:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($currdom);
1.247     raeburn  5356:     my $othervalue = 'any';
1.241     raeburn  5357:     if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
1.251     raeburn  5358:         if (keys(%{$usertypes}) > 0) {
1.247     raeburn  5359:             $othervalue = 'other';
                   5360:         }
1.241     raeburn  5361:         $output .= '<table><tr>';
                   5362:         foreach my $type (@{$types}) {
                   5363:             if (($count > 0) && ($count%$numinrow == 0)) {
                   5364:                 $output .= '</tr><tr>';
                   5365:             }
                   5366:             if (defined($usertypes->{$type})) {
1.257     raeburn  5367:                 my $esc_type = &escape($type);
1.241     raeburn  5368:                 $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.
1.257     raeburn  5369:                            $esc_type.'" ';
1.241     raeburn  5370:                 if (ref($currinsttypes) eq 'ARRAY') {
                   5371:                     if (@{$currinsttypes} > 0) {
1.249     raeburn  5372:                         if (grep(/^any$/,@{$currinsttypes})) {
                   5373:                             $output .= 'checked="checked"';
1.257     raeburn  5374:                         } elsif (grep(/^\Q$esc_type\E$/,@{$currinsttypes})) {
1.241     raeburn  5375:                             $output .= 'checked="checked"';
                   5376:                         }
1.249     raeburn  5377:                     } else {
                   5378:                         $output .= 'checked="checked"';
1.241     raeburn  5379:                     }
                   5380:                 }
                   5381:                 $output .= ' name="selfenroll_types_'.$num.'" />'.$usertypes->{$type}.'</label></span></td>';
                   5382:             }
                   5383:             $count ++;
                   5384:         }
                   5385:         if (($count > 0) && ($count%$numinrow == 0)) {
                   5386:             $output .= '</tr><tr>';
                   5387:         }
1.249     raeburn  5388:         $output .= '<td><span class="LC_nobreak"><label><input type = "checkbox" value="'.$othervalue.'"';
1.241     raeburn  5389:         if (ref($currinsttypes) eq 'ARRAY') {
                   5390:             if (@{$currinsttypes} > 0) {
1.249     raeburn  5391:                 if (grep(/^any$/,@{$currinsttypes})) { 
                   5392:                     $output .= ' checked="checked"';
                   5393:                 } elsif ($othervalue eq 'other') {
                   5394:                     if (grep(/^\Q$othervalue\E$/,@{$currinsttypes})) {
                   5395:                         $output .= ' checked="checked"';
                   5396:                     }
1.241     raeburn  5397:                 }
1.249     raeburn  5398:             } else {
                   5399:                 $output .= ' checked="checked"';
1.241     raeburn  5400:             }
1.249     raeburn  5401:         } else {
                   5402:             $output .= ' checked="checked"';
1.241     raeburn  5403:         }
                   5404:         $output .= ' name="selfenroll_types_'.$num.'" />'.$othertitle.'</label></span></td></tr></table>';
                   5405:     }
                   5406:     return $output;
                   5407: }
                   5408: 
1.237     raeburn  5409: sub selfenroll_date_forms {
                   5410:     my ($startform,$endform) = @_;
                   5411:     my $output .= &Apache::lonhtmlcommon::start_pick_box()."\n".
1.244     bisitz   5412:                   &Apache::lonhtmlcommon::row_title(&mt('Start date'),
1.237     raeburn  5413:                                                     'LC_oddrow_value')."\n".
                   5414:                   $startform."\n".
                   5415:                   &Apache::lonhtmlcommon::row_closure(1).
1.244     bisitz   5416:                   &Apache::lonhtmlcommon::row_title(&mt('End date'),
1.237     raeburn  5417:                                                    'LC_oddrow_value')."\n".
                   5418:                   $endform."\n".
                   5419:                   &Apache::lonhtmlcommon::row_closure(1).
                   5420:                   &Apache::lonhtmlcommon::end_pick_box();
                   5421:     return $output;
                   5422: }
                   5423: 
1.239     raeburn  5424: sub print_userchangelogs_display {
                   5425:     my ($r,$context,$permission) = @_;
                   5426:     my $formname = 'roleslog';
                   5427:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5428:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.318     raeburn  5429:     my $crstype = &Apache::loncommon::course_type();
1.239     raeburn  5430:     my %roleslog=&Apache::lonnet::dump('nohist_rolelog',$cdom,$cnum);
                   5431:     if ((keys(%roleslog))[0]=~/^error\:/) { undef(%roleslog); }
                   5432: 
                   5433:     my %saveable_parameters = ('show' => 'scalar',);
                   5434:     &Apache::loncommon::store_course_settings('roles_log',
                   5435:                                               \%saveable_parameters);
                   5436:     &Apache::loncommon::restore_course_settings('roles_log',
                   5437:                                                 \%saveable_parameters);
                   5438:     # set defaults
                   5439:     my $now = time();
                   5440:     my $defstart = $now - (7*24*3600); #7 days ago 
                   5441:     my %defaults = (
                   5442:                      page               => '1',
                   5443:                      show               => '10',
                   5444:                      role               => 'any',
                   5445:                      chgcontext         => 'any',
                   5446:                      rolelog_start_date => $defstart,
                   5447:                      rolelog_end_date   => $now,
                   5448:                    );
                   5449:     my $more_records = 0;
                   5450: 
                   5451:     # set current
                   5452:     my %curr;
                   5453:     foreach my $item ('show','page','role','chgcontext') {
                   5454:         $curr{$item} = $env{'form.'.$item};
                   5455:     }
                   5456:     my ($startdate,$enddate) = 
                   5457:         &Apache::lonuserutils::get_dates_from_form('rolelog_start_date','rolelog_end_date');
                   5458:     $curr{'rolelog_start_date'} = $startdate;
                   5459:     $curr{'rolelog_end_date'} = $enddate;
                   5460:     foreach my $key (keys(%defaults)) {
                   5461:         if ($curr{$key} eq '') {
                   5462:             $curr{$key} = $defaults{$key};
                   5463:         }
                   5464:     }
1.248     raeburn  5465:     my (%whodunit,%changed,$version);
                   5466:     ($version) = ($r->dir_config('lonVersion') =~ /^([\d\.]+)\-/);
1.239     raeburn  5467:     my ($minshown,$maxshown);
1.255     raeburn  5468:     $minshown = 1;
1.239     raeburn  5469:     my $count = 0;
                   5470:     if ($curr{'show'} ne &mt('all')) { 
                   5471:         $maxshown = $curr{'page'} * $curr{'show'};
                   5472:         if ($curr{'page'} > 1) {
                   5473:             $minshown = 1 + ($curr{'page'} - 1) * $curr{'show'};
                   5474:         }
                   5475:     }
1.301     bisitz   5476: 
1.327     raeburn  5477:     # Form Header
                   5478:     $r->print('<form action="/adm/createuser" method="post" name="'.$formname.'">'.
                   5479:               &role_display_filter($formname,$cdom,$cnum,\%curr,$version,$crstype));
                   5480: 
                   5481:     # Create navigation
                   5482:     my ($nav_script,$nav_links) = &userlogdisplay_nav($formname,\%curr,$more_records);
                   5483:     my $showntableheader = 0;
                   5484: 
                   5485:     # Table Header
                   5486:     my $tableheader = 
                   5487:         &Apache::loncommon::start_data_table_header_row()
                   5488:        .'<th>&nbsp;</th>'
                   5489:        .'<th>'.&mt('When').'</th>'
                   5490:        .'<th>'.&mt('Who made the change').'</th>'
                   5491:        .'<th>'.&mt('Changed User').'</th>'
                   5492:        .'<th>'.&mt('Role').'</th>'
                   5493:        .'<th>'.&mt('Section').'</th>'
                   5494:        .'<th>'.&mt('Context').'</th>'
                   5495:        .'<th>'.&mt('Start').'</th>'
                   5496:        .'<th>'.&mt('End').'</th>'
                   5497:        .&Apache::loncommon::end_data_table_header_row();
                   5498: 
                   5499:     # Display user change log data
1.239     raeburn  5500:     foreach my $id (sort { $roleslog{$b}{'exe_time'}<=>$roleslog{$a}{'exe_time'} } (keys(%roleslog))) {
                   5501:         next if (($roleslog{$id}{'exe_time'} < $curr{'rolelog_start_date'}) ||
                   5502:                  ($roleslog{$id}{'exe_time'} > $curr{'rolelog_end_date'}));
                   5503:         if ($curr{'show'} ne &mt('all')) {
                   5504:             if ($count >= $curr{'page'} * $curr{'show'}) {
                   5505:                 $more_records = 1;
                   5506:                 last;
                   5507:             }
                   5508:         }
                   5509:         if ($curr{'role'} ne 'any') {
                   5510:             next if ($roleslog{$id}{'logentry'}{'role'} ne $curr{'role'}); 
                   5511:         }
                   5512:         if ($curr{'chgcontext'} ne 'any') {
                   5513:             if ($curr{'chgcontext'} eq 'selfenroll') {
                   5514:                 next if (!$roleslog{$id}{'logentry'}{'selfenroll'});
                   5515:             } else {
                   5516:                 next if ($roleslog{$id}{'logentry'}{'context'} ne $curr{'chgcontext'});
                   5517:             }
                   5518:         }
                   5519:         $count ++;
                   5520:         next if ($count < $minshown);
1.327     raeburn  5521:         unless ($showntableheader) {
                   5522:             $r->print($nav_script
                   5523:                      .$nav_links
                   5524:                      .&Apache::loncommon::start_data_table()
                   5525:                      .$tableheader);
                   5526:             $r->rflush();
                   5527:             $showntableheader = 1;
                   5528:         }
1.239     raeburn  5529:         if ($whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} eq '') {
                   5530:             $whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}} =
                   5531:                 &Apache::loncommon::plainname($roleslog{$id}{'exe_uname'},$roleslog{$id}{'exe_udom'});
                   5532:         }
                   5533:         if ($changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} eq '') {
                   5534:             $changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}} =
                   5535:                 &Apache::loncommon::plainname($roleslog{$id}{'uname'},$roleslog{$id}{'udom'});
                   5536:         }
                   5537:         my $sec = $roleslog{$id}{'logentry'}{'section'};
                   5538:         if ($sec eq '') {
                   5539:             $sec = &mt('None');
                   5540:         }
                   5541:         my ($rolestart,$roleend);
                   5542:         if ($roleslog{$id}{'delflag'}) {
                   5543:             $rolestart = &mt('deleted');
                   5544:             $roleend = &mt('deleted');
                   5545:         } else {
                   5546:             $rolestart = $roleslog{$id}{'logentry'}{'start'};
                   5547:             $roleend = $roleslog{$id}{'logentry'}{'end'};
                   5548:             if ($rolestart eq '' || $rolestart == 0) {
                   5549:                 $rolestart = &mt('No start date'); 
                   5550:             } else {
                   5551:                 $rolestart = &Apache::lonlocal::locallocaltime($rolestart);
                   5552:             }
                   5553:             if ($roleend eq '' || $roleend == 0) { 
                   5554:                 $roleend = &mt('No end date');
                   5555:             } else {
                   5556:                 $roleend = &Apache::lonlocal::locallocaltime($roleend);
                   5557:             }
                   5558:         }
                   5559:         my $chgcontext = $roleslog{$id}{'logentry'}{'context'};
                   5560:         if ($roleslog{$id}{'logentry'}{'selfenroll'}) {
                   5561:             $chgcontext = 'selfenroll';
                   5562:         }
1.318     raeburn  5563:         my %lt = &rolechg_contexts($crstype);
1.239     raeburn  5564:         if ($chgcontext ne '' && $lt{$chgcontext} ne '') {
                   5565:             $chgcontext = $lt{$chgcontext};
                   5566:         }
1.327     raeburn  5567:         $r->print(
1.301     bisitz   5568:             &Apache::loncommon::start_data_table_row()
                   5569:            .'<td>'.$count.'</td>'
                   5570:            .'<td>'.&Apache::lonlocal::locallocaltime($roleslog{$id}{'exe_time'}).'</td>'
                   5571:            .'<td>'.$whodunit{$roleslog{$id}{'exe_uname'}.':'.$roleslog{$id}{'exe_udom'}}.'</td>'
                   5572:            .'<td>'.$changed{$roleslog{$id}{'uname'}.':'.$roleslog{$id}{'udom'}}.'</td>'
1.318     raeburn  5573:            .'<td>'.&Apache::lonnet::plaintext($roleslog{$id}{'logentry'}{'role'},$crstype).'</td>'
1.301     bisitz   5574:            .'<td>'.$sec.'</td>'
                   5575:            .'<td>'.$chgcontext.'</td>'
                   5576:            .'<td>'.$rolestart.'</td>'
                   5577:            .'<td>'.$roleend.'</td>'
1.327     raeburn  5578:            .&Apache::loncommon::end_data_table_row()."\n");
1.301     bisitz   5579:     }
                   5580: 
1.327     raeburn  5581:     if ($showntableheader) { # Table footer, if content displayed above
                   5582:         $r->print(&Apache::loncommon::end_data_table()
                   5583:                  .$nav_links);
                   5584:     } else { # No content displayed above
1.301     bisitz   5585:         $r->print('<p class="LC_info">'
                   5586:                  .&mt('There are no records to display.')
                   5587:                  .'</p>'
                   5588:         );
1.239     raeburn  5589:     }
1.301     bisitz   5590: 
1.327     raeburn  5591:     # Form Footer
                   5592:     $r->print( 
                   5593:         '<input type="hidden" name="page" value="'.$curr{'page'}.'" />'
                   5594:        .'<input type="hidden" name="action" value="changelogs" />'
                   5595:        .'</form>');
                   5596:     return;
                   5597: }
1.301     bisitz   5598: 
1.327     raeburn  5599: sub userlogdisplay_nav {
                   5600:     my ($formname,$curr,$more_records) = @_;
                   5601:     my ($nav_script,$nav_links);
                   5602:     if (ref($curr) eq 'HASH') {
                   5603:         # Create Navigation:
                   5604:         # Navigation Script
                   5605:         $nav_script = <<"ENDSCRIPT";
1.239     raeburn  5606: <script type="text/javascript">
1.301     bisitz   5607: // <![CDATA[
1.239     raeburn  5608: function chgPage(caller) {
                   5609:     if (caller == 'previous') {
                   5610:         document.$formname.page.value --;
                   5611:     }
                   5612:     if (caller == 'next') {
                   5613:         document.$formname.page.value ++;
                   5614:     }
1.327     raeburn  5615:     document.$formname.submit();
1.239     raeburn  5616:     return;
                   5617: }
1.301     bisitz   5618: // ]]>
1.239     raeburn  5619: </script>
                   5620: ENDSCRIPT
1.327     raeburn  5621:         # Navigation Buttons
                   5622:         $nav_links = '<p>';
                   5623:         if (($curr->{'page'} > 1) || ($more_records)) {
                   5624:             if ($curr->{'page'} > 1) {
                   5625:                 $nav_links .= '<input type="button"'
                   5626:                              .' onclick="javascript:chgPage('."'previous'".');"'
                   5627:                              .' value="'.&mt('Previous [_1] changes',$curr->{'show'})
                   5628:                              .'" /> ';
                   5629:             }
                   5630:             if ($more_records) {
                   5631:                 $nav_links .= '<input type="button"'
                   5632:                              .' onclick="javascript:chgPage('."'next'".');"'
                   5633:                              .' value="'.&mt('Next [_1] changes',$curr->{'show'})
                   5634:                              .'" />';
                   5635:             }
1.301     bisitz   5636:         }
1.327     raeburn  5637:         $nav_links .= '</p>';
1.301     bisitz   5638:     }
1.327     raeburn  5639:     return ($nav_script,$nav_links);
1.239     raeburn  5640: }
                   5641: 
                   5642: sub role_display_filter {
1.318     raeburn  5643:     my ($formname,$cdom,$cnum,$curr,$version,$crstype) = @_;
1.239     raeburn  5644:     my $context = 'course';
1.318     raeburn  5645:     my $lctype = lc($crstype);
1.239     raeburn  5646:     my $nolink = 1;
                   5647:     my $output = '<table><tr><td valign="top">'.
1.301     bisitz   5648:                  '<span class="LC_nobreak"><b>'.&mt('Changes/page:').'</b></span><br />'.
1.239     raeburn  5649:                  &Apache::lonmeta::selectbox('show',$curr->{'show'},undef,
                   5650:                                               (&mt('all'),5,10,20,50,100,1000,10000)).
                   5651:                  '</td><td>&nbsp;&nbsp;</td>';
                   5652:     my $startform =
                   5653:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_start_date',
                   5654:                                             $curr->{'rolelog_start_date'},undef,
                   5655:                                             undef,undef,undef,undef,undef,undef,$nolink);
                   5656:     my $endform =
                   5657:         &Apache::lonhtmlcommon::date_setter($formname,'rolelog_end_date',
                   5658:                                             $curr->{'rolelog_end_date'},undef,
                   5659:                                             undef,undef,undef,undef,undef,undef,$nolink);
1.318     raeburn  5660:     my %lt = &rolechg_contexts($crstype);
1.301     bisitz   5661:     $output .= '<td valign="top"><b>'.&mt('Window during which changes occurred:').'</b><br />'.
                   5662:                '<table><tr><td>'.&mt('After:').
                   5663:                '</td><td>'.$startform.'</td></tr>'.
                   5664:                '<tr><td>'.&mt('Before:').'</td>'.
                   5665:                '<td>'.$endform.'</td></tr></table>'.
                   5666:                '</td>'.
                   5667:                '<td>&nbsp;&nbsp;</td>'.
1.239     raeburn  5668:                '<td valign="top"><b>'.&mt('Role:').'</b><br />'.
                   5669:                '<select name="role"><option value="any"';
                   5670:     if ($curr->{'role'} eq 'any') {
                   5671:         $output .= ' selected="selected"';
                   5672:     }
                   5673:     $output .=  '>'.&mt('Any').'</option>'."\n";
1.318     raeburn  5674:     my @roles = &Apache::lonuserutils::course_roles($context,undef,1,$lctype);
1.239     raeburn  5675:     foreach my $role (@roles) {
                   5676:         my $plrole;
                   5677:         if ($role eq 'cr') {
                   5678:             $plrole = &mt('Custom Role');
                   5679:         } else {
1.318     raeburn  5680:             $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.239     raeburn  5681:         }
                   5682:         my $selstr = '';
                   5683:         if ($role eq $curr->{'role'}) {
                   5684:             $selstr = ' selected="selected"';
                   5685:         }
                   5686:         $output .= '  <option value="'.$role.'"'.$selstr.'>'.$plrole.'</option>';
                   5687:     }
1.301     bisitz   5688:     $output .= '</select></td>'.
                   5689:                '<td>&nbsp;&nbsp;</td>'.
                   5690:                '<td valign="top"><b>'.
1.239     raeburn  5691:                &mt('Context:').'</b><br /><select name="chgcontext">';
1.318     raeburn  5692:     foreach my $chgtype ('any','auto','updatenow','createcourse','course','domain','selfenroll','requestcourses') {
1.239     raeburn  5693:         my $selstr = '';
                   5694:         if ($curr->{'chgcontext'} eq $chgtype) {
1.301     bisitz   5695:             $selstr = ' selected="selected"';
1.239     raeburn  5696:         }
                   5697:         if (($chgtype eq 'auto') || ($chgtype eq 'updatenow')) {
                   5698:             next if (!&Apache::lonnet::auto_run($cnum,$cdom));
                   5699:         }
                   5700:         $output .= '<option value="'.$chgtype.'"'.$selstr.'>'.$lt{$chgtype}.'</option>'."\n";
1.248     raeburn  5701:     }
1.303     bisitz   5702:     $output .= '</select></td>'
                   5703:               .'</tr></table>';
                   5704: 
                   5705:     # Update Display button
                   5706:     $output .= '<p>'
                   5707:               .'<input type="submit" value="'.&mt('Update Display').'" />'
1.329.2.2  raeburn  5708:               .'</p><hr />';
1.239     raeburn  5709:     return $output;
                   5710: }
                   5711: 
                   5712: sub rolechg_contexts {
1.318     raeburn  5713:     my ($crstype) = @_;
1.239     raeburn  5714:     my %lt = &Apache::lonlocal::texthash (
                   5715:                                              any          => 'Any',
                   5716:                                              auto         => 'Automated enrollment',
                   5717:                                              updatenow    => 'Roster Update',
                   5718:                                              createcourse => 'Course Creation',
                   5719:                                              course       => 'User Management in course',
                   5720:                                              domain       => 'User Management in domain',
1.313     raeburn  5721:                                              selfenroll   => 'Self-enrolled',
1.318     raeburn  5722:                                              requestcourses => 'Course Request',
1.239     raeburn  5723:                                          );
1.318     raeburn  5724:     if ($crstype eq 'Community') {
                   5725:         $lt{'createcourse'} = &mt('Community Creation');
                   5726:         $lt{'course'} = &mt('User Management in community');
                   5727:         $lt{'requestcourses'} = &mt('Community Request');
                   5728:     }
1.239     raeburn  5729:     return %lt;
                   5730: }
                   5731: 
1.27      matthew  5732: #-------------------------------------------------- functions for &phase_two
1.160     raeburn  5733: sub user_search_result {
1.221     raeburn  5734:     my ($context,$srch) = @_;
1.160     raeburn  5735:     my %allhomes;
                   5736:     my %inst_matches;
                   5737:     my %srch_results;
1.181     raeburn  5738:     my ($response,$currstate,$forcenewuser,$dirsrchres);
1.183     raeburn  5739:     $srch->{'srchterm'} =~ s/\s+/ /g;
1.176     raeburn  5740:     if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) {
1.160     raeburn  5741:         $response = &mt('Invalid search.');
                   5742:     }
                   5743:     if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) {
                   5744:         $response = &mt('Invalid search.');
                   5745:     }
1.177     raeburn  5746:     if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) {
1.160     raeburn  5747:         $response = &mt('Invalid search.');
                   5748:     }
                   5749:     if ($srch->{'srchterm'} eq '') {
                   5750:         $response = &mt('You must enter a search term.');
                   5751:     }
1.183     raeburn  5752:     if ($srch->{'srchterm'} =~ /^\s+$/) {
                   5753:         $response = &mt('Your search term must contain more than just spaces.');
                   5754:     }
1.160     raeburn  5755:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
                   5756:         if (($srch->{'srchdomain'} eq '') || 
1.163     albertel 5757: 	    ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) {
1.329.2.4  raeburn  5758:             $response = '<p class="LC_warning">'.&mt('You must specify a valid domain when searching in a domain or institutional directory.').'</p>';
1.160     raeburn  5759:         }
                   5760:     }
                   5761:     if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') ||
                   5762:         ($srch->{'srchin'} eq 'alc')) {
1.176     raeburn  5763:         if ($srch->{'srchby'} eq 'uname') {
1.243     raeburn  5764:             my $unamecheck = $srch->{'srchterm'};
                   5765:             if ($srch->{'srchtype'} eq 'contains') {
                   5766:                 if ($unamecheck !~ /^\w/) {
                   5767:                     $unamecheck = 'a'.$unamecheck; 
                   5768:                 }
                   5769:             }
                   5770:             if ($unamecheck !~ /^$match_username$/) {
1.329.2.4  raeburn  5771:                 $response = '<p class="LC_warning">'.&mt('You must specify a valid username. Only the following are allowed: letters numbers - . @').'</p>';
1.176     raeburn  5772:             }
1.160     raeburn  5773:         }
                   5774:     }
1.180     raeburn  5775:     if ($response ne '') {
                   5776:         $response = '<span class="LC_warning">'.$response.'</span>';
                   5777:     }
1.160     raeburn  5778:     if ($srch->{'srchin'} eq 'instd') {
                   5779:         my $instd_chk = &directorysrch_check($srch);
                   5780:         if ($instd_chk ne 'ok') {
1.180     raeburn  5781:             $response = '<span class="LC_warning">'.$instd_chk.'</span>'.
                   5782:                         '<br />'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'<br /><br />';
1.160     raeburn  5783:         }
                   5784:     }
                   5785:     if ($response ne '') {
1.180     raeburn  5786:         return ($currstate,$response);
1.160     raeburn  5787:     }
                   5788:     if ($srch->{'srchby'} eq 'uname') {
                   5789:         if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) {
                   5790:             if ($env{'form.forcenew'}) {
                   5791:                 if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) {
                   5792:                     my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   5793:                     if ($uhome eq 'no_host') {
                   5794:                         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.180     raeburn  5795:                         my $showdom = &display_domain_info($env{'request.role.domain'});
                   5796:                         $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom);
1.160     raeburn  5797:                     } else {
1.179     raeburn  5798:                         $currstate = 'modify';
1.160     raeburn  5799:                     }
                   5800:                 } else {
1.179     raeburn  5801:                     $currstate = 'modify';
1.160     raeburn  5802:                 }
                   5803:             } else {
                   5804:                 if ($srch->{'srchin'} eq 'dom') {
1.162     raeburn  5805:                     if ($srch->{'srchtype'} eq 'exact') {
                   5806:                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                   5807:                         if ($uhome eq 'no_host') {
1.179     raeburn  5808:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  5809:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  5810:                         } else {
1.179     raeburn  5811:                             $currstate = 'modify';
1.310     raeburn  5812:                             my $uname = $srch->{'srchterm'};
                   5813:                             my $udom = $srch->{'srchdomain'};
                   5814:                             $srch_results{$uname.':'.$udom} =
                   5815:                                 { &Apache::lonnet::get('environment',
                   5816:                                                        ['firstname',
                   5817:                                                         'lastname',
                   5818:                                                         'permanentemail'],
                   5819:                                                          $udom,$uname)
                   5820:                                 };
1.162     raeburn  5821:                         }
                   5822:                     } else {
                   5823:                         %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  5824:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  5825:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  5826:                     }
                   5827:                 } else {
1.167     albertel 5828:                     my $courseusers = &get_courseusers();
1.162     raeburn  5829:                     if ($srch->{'srchtype'} eq 'exact') {
1.167     albertel 5830:                         if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
1.179     raeburn  5831:                             $currstate = 'modify';
1.162     raeburn  5832:                         } else {
1.179     raeburn  5833:                             ($currstate,$response,$forcenewuser) =
1.221     raeburn  5834:                                 &build_search_response($context,$srch,%srch_results);
1.162     raeburn  5835:                         }
1.160     raeburn  5836:                     } else {
1.167     albertel 5837:                         foreach my $user (keys(%$courseusers)) {
1.162     raeburn  5838:                             my ($cuname,$cudomain) = split(/:/,$user);
                   5839:                             if ($cudomain eq $srch->{'srchdomain'}) {
1.177     raeburn  5840:                                 my $matched = 0;
                   5841:                                 if ($srch->{'srchtype'} eq 'begins') {
                   5842:                                     if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) {
                   5843:                                         $matched = 1;
                   5844:                                     }
                   5845:                                 } else {
                   5846:                                     if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) {
                   5847:                                         $matched = 1;
                   5848:                                     }
                   5849:                                 }
                   5850:                                 if ($matched) {
1.167     albertel 5851:                                     $srch_results{$user} = 
                   5852: 					{&Apache::lonnet::get('environment',
                   5853: 							     ['firstname',
                   5854: 							      'lastname',
1.194     albertel 5855: 							      'permanentemail'],
                   5856: 							      $cudomain,$cuname)};
1.162     raeburn  5857:                                 }
                   5858:                             }
                   5859:                         }
1.179     raeburn  5860:                         ($currstate,$response,$forcenewuser) =
1.221     raeburn  5861:                             &build_search_response($context,$srch,%srch_results);
1.160     raeburn  5862:                     }
                   5863:                 }
                   5864:             }
                   5865:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  5866:             $currstate = 'query';
1.160     raeburn  5867:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  5868:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
                   5869:             if ($dirsrchres eq 'ok') {
                   5870:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5871:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  5872:             } else {
                   5873:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   5874:                 $response = '<span class="LC_warning">'.
                   5875:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   5876:                     '</span><br />'.
                   5877:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   5878:                     '<br /><br />'; 
                   5879:             }
1.160     raeburn  5880:         }
                   5881:     } else {
                   5882:         if ($srch->{'srchin'} eq 'dom') {
                   5883:             %srch_results = &Apache::lonnet::usersearch($srch);
1.179     raeburn  5884:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5885:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  5886:         } elsif ($srch->{'srchin'} eq 'crs') {
1.167     albertel 5887:             my $courseusers = &get_courseusers(); 
                   5888:             foreach my $user (keys(%$courseusers)) {
1.160     raeburn  5889:                 my ($uname,$udom) = split(/:/,$user);
                   5890:                 my %names = &Apache::loncommon::getnames($uname,$udom);
                   5891:                 my %emails = &Apache::loncommon::getemails($uname,$udom);
                   5892:                 if ($srch->{'srchby'} eq 'lastname') {
                   5893:                     if ((($srch->{'srchtype'} eq 'exact') && 
                   5894:                          ($names{'lastname'} eq $srch->{'srchterm'})) || 
1.177     raeburn  5895:                         (($srch->{'srchtype'} eq 'begins') &&
                   5896:                          ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) ||
1.160     raeburn  5897:                         (($srch->{'srchtype'} eq 'contains') &&
                   5898:                          ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) {
                   5899:                         $srch_results{$user} = {firstname => $names{'firstname'},
                   5900:                                             lastname => $names{'lastname'},
                   5901:                                             permanentemail => $emails{'permanentemail'},
                   5902:                                            };
                   5903:                     }
                   5904:                 } elsif ($srch->{'srchby'} eq 'lastfirst') {
                   5905:                     my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'});
1.177     raeburn  5906:                     $srchlast =~ s/\s+$//;
                   5907:                     $srchfirst =~ s/^\s+//;
1.160     raeburn  5908:                     if ($srch->{'srchtype'} eq 'exact') {
                   5909:                         if (($names{'lastname'} eq $srchlast) &&
                   5910:                             ($names{'firstname'} eq $srchfirst)) {
                   5911:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   5912:                                                 lastname => $names{'lastname'},
                   5913:                                                 permanentemail => $emails{'permanentemail'},
                   5914: 
                   5915:                                            };
                   5916:                         }
1.177     raeburn  5917:                     } elsif ($srch->{'srchtype'} eq 'begins') {
                   5918:                         if (($names{'lastname'} =~ /^\Q$srchlast\E/i) &&
                   5919:                             ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) {
                   5920:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   5921:                                                 lastname => $names{'lastname'},
                   5922:                                                 permanentemail => $emails{'permanentemail'},
                   5923:                                                };
                   5924:                         }
                   5925:                     } else {
1.160     raeburn  5926:                         if (($names{'lastname'} =~ /\Q$srchlast\E/i) && 
                   5927:                             ($names{'firstname'} =~ /\Q$srchfirst\E/i)) {
                   5928:                             $srch_results{$user} = {firstname => $names{'firstname'},
                   5929:                                                 lastname => $names{'lastname'},
                   5930:                                                 permanentemail => $emails{'permanentemail'},
                   5931:                                                };
                   5932:                         }
                   5933:                     }
                   5934:                 }
                   5935:             }
1.179     raeburn  5936:             ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5937:                 &build_search_response($context,$srch,%srch_results); 
1.160     raeburn  5938:         } elsif ($srch->{'srchin'} eq 'alc') {
1.179     raeburn  5939:             $currstate = 'query';
1.160     raeburn  5940:         } elsif ($srch->{'srchin'} eq 'instd') {
1.181     raeburn  5941:             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); 
                   5942:             if ($dirsrchres eq 'ok') {
                   5943:                 ($currstate,$response,$forcenewuser) = 
1.221     raeburn  5944:                     &build_search_response($context,$srch,%srch_results);
1.181     raeburn  5945:             } else {
1.329.2.4  raeburn  5946:                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                   5947:                 $response = '<span class="LC_warning">'.
1.181     raeburn  5948:                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
                   5949:                     '</span><br />'.
                   5950:                     &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').
                   5951:                     '<br /><br />';
                   5952:             }
1.160     raeburn  5953:         }
                   5954:     }
1.179     raeburn  5955:     return ($currstate,$response,$forcenewuser,\%srch_results);
1.160     raeburn  5956: }
                   5957: 
                   5958: sub directorysrch_check {
                   5959:     my ($srch) = @_;
                   5960:     my $can_search = 0;
                   5961:     my $response;
                   5962:     my %dom_inst_srch = &Apache::lonnet::get_dom('configuration',
                   5963:                                              ['directorysrch'],$srch->{'srchdomain'});
1.180     raeburn  5964:     my $showdom = &display_domain_info($srch->{'srchdomain'});
1.160     raeburn  5965:     if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
                   5966:         if (!$dom_inst_srch{'directorysrch'}{'available'}) {
1.180     raeburn  5967:             return &mt('Institutional directory search is not available in domain: [_1]',$showdom); 
1.160     raeburn  5968:         }
                   5969:         if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
                   5970:             if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
1.180     raeburn  5971:                 return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); 
1.160     raeburn  5972:             }
                   5973:             my @usertypes = split(/:/,$env{'environment.inststatus'});
                   5974:             if (!@usertypes) {
                   5975:                 push(@usertypes,'default');
                   5976:             }
                   5977:             if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') {
                   5978:                 foreach my $type (@usertypes) {
                   5979:                     if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) {
                   5980:                         $can_search = 1;
                   5981:                         last;
                   5982:                     }
                   5983:                 }
                   5984:             }
                   5985:             if (!$can_search) {
                   5986:                 my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'});
                   5987:                 my @longtypes; 
                   5988:                 foreach my $item (@usertypes) {
1.229     raeburn  5989:                     if (defined($insttypes->{$item})) { 
                   5990:                         push (@longtypes,$insttypes->{$item});
                   5991:                     } elsif ($item eq 'default') {
                   5992:                         push (@longtypes,&mt('other')); 
                   5993:                     }
1.160     raeburn  5994:                 }
                   5995:                 my $insttype_str = join(', ',@longtypes); 
1.180     raeburn  5996:                 return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str;
1.229     raeburn  5997:             }
1.160     raeburn  5998:         } else {
                   5999:             $can_search = 1;
                   6000:         }
                   6001:     } else {
1.180     raeburn  6002:         return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom);
1.160     raeburn  6003:     }
                   6004:     my %longtext = &Apache::lonlocal::texthash (
1.167     albertel 6005:                        uname     => 'username',
1.160     raeburn  6006:                        lastfirst => 'last name, first name',
1.167     albertel 6007:                        lastname  => 'last name',
1.172     raeburn  6008:                        contains  => 'contains',
1.178     raeburn  6009:                        exact     => 'as exact match to',
                   6010:                        begins    => 'begins with',
1.160     raeburn  6011:                    );
                   6012:     if ($can_search) {
                   6013:         if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') {
                   6014:             if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) {
1.180     raeburn  6015:                 return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}});
1.160     raeburn  6016:             }
                   6017:         } else {
1.180     raeburn  6018:             return &mt('Institutional directory search in domain: [_1] is not available.', $showdom);
1.160     raeburn  6019:         }
                   6020:     }
                   6021:     if ($can_search) {
1.178     raeburn  6022:         if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') {
                   6023:             if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) {
                   6024:                 return 'ok';
                   6025:             } else {
1.180     raeburn  6026:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6027:             }
                   6028:         } else {
                   6029:             if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') &&
                   6030:                  ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) ||
                   6031:                 ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) {
                   6032:                 return 'ok';
                   6033:             } else {
1.180     raeburn  6034:                 return &mt('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}});
1.178     raeburn  6035:             }
1.160     raeburn  6036:         }
                   6037:     }
                   6038: }
                   6039: 
                   6040: sub get_courseusers {
                   6041:     my %advhash;
1.167     albertel 6042:     my $classlist = &Apache::loncoursedata::get_classlist();
1.160     raeburn  6043:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
                   6044:     foreach my $role (sort(keys(%coursepersonnel))) {
                   6045:         foreach my $user (split(/\,/,$coursepersonnel{$role})) {
1.167     albertel 6046: 	    if (!exists($classlist->{$user})) {
                   6047: 		$classlist->{$user} = [];
                   6048: 	    }
1.160     raeburn  6049:         }
                   6050:     }
1.167     albertel 6051:     return $classlist;
1.160     raeburn  6052: }
                   6053: 
                   6054: sub build_search_response {
1.221     raeburn  6055:     my ($context,$srch,%srch_results) = @_;
1.179     raeburn  6056:     my ($currstate,$response,$forcenewuser);
1.160     raeburn  6057:     my %names = (
                   6058:           'uname' => 'username',
                   6059:           'lastname' => 'last name',
                   6060:           'lastfirst' => 'last name, first name',
                   6061:           'crs' => 'this course',
1.180     raeburn  6062:           'dom' => 'LON-CAPA domain: ',
                   6063:           'instd' => 'the institutional directory for domain: ',
1.160     raeburn  6064:     );
                   6065: 
                   6066:     my %single = (
1.180     raeburn  6067:                    begins   => 'A match',
1.160     raeburn  6068:                    contains => 'A match',
1.180     raeburn  6069:                    exact    => 'An exact match',
1.160     raeburn  6070:                  );
                   6071:     my %nomatch = (
1.180     raeburn  6072:                    begins   => 'No match',
1.160     raeburn  6073:                    contains => 'No match',
1.180     raeburn  6074:                    exact    => 'No exact match',
1.160     raeburn  6075:                   );
                   6076:     if (keys(%srch_results) > 1) {
1.179     raeburn  6077:         $currstate = 'select';
1.160     raeburn  6078:     } else {
                   6079:         if (keys(%srch_results) == 1) {
1.179     raeburn  6080:             $currstate = 'modify';
1.180     raeburn  6081:             $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'});
                   6082:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
                   6083:                 $response .= &display_domain_info($srch->{'srchdomain'});
                   6084:             }
1.160     raeburn  6085:         } else {
1.180     raeburn  6086:             $response = '<span class="LC_warning">'.&mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}",$srch->{'srchterm'});
                   6087:             if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') {
                   6088:                 $response .= &display_domain_info($srch->{'srchdomain'});
                   6089:             }
                   6090:             $response .= '</span>';
1.160     raeburn  6091:             if ($srch->{'srchin'} ne 'alc') {
                   6092:                 $forcenewuser = 1;
                   6093:                 my $cansrchinst = 0; 
                   6094:                 if ($srch->{'srchdomain'}) {
                   6095:                     my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'});
                   6096:                     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   6097:                         if ($domconfig{'directorysrch'}{'available'}) {
                   6098:                             $cansrchinst = 1;
                   6099:                         } 
                   6100:                     }
                   6101:                 }
1.180     raeburn  6102:                 if ((($srch->{'srchby'} eq 'lastfirst') || 
                   6103:                      ($srch->{'srchby'} eq 'lastname')) &&
                   6104:                     ($srch->{'srchin'} eq 'dom')) {
                   6105:                     if ($cansrchinst) {
                   6106:                         $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.');
1.160     raeburn  6107:                     }
                   6108:                 }
1.180     raeburn  6109:                 if ($srch->{'srchin'} eq 'crs') {
                   6110:                     $response .= '<br />'.&mt('You may want to broaden your search to the selected LON-CAPA domain.');
                   6111:                 }
                   6112:             }
1.305     raeburn  6113:             my $createdom = $env{'request.role.domain'};
                   6114:             if ($context eq 'requestcrs') {
                   6115:                 if ($env{'form.coursedom'} ne '') {
                   6116:                     $createdom = $env{'form.coursedom'};
                   6117:                 }
                   6118:             }
                   6119:             if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $createdom)) {
1.221     raeburn  6120:                 my $cancreate =
1.305     raeburn  6121:                     &Apache::lonuserutils::can_create_user($createdom,$context);
                   6122:                 my $targetdom = '<span class="LC_cusr_emph">'.$createdom.'</span>';
1.221     raeburn  6123:                 if ($cancreate) {
1.305     raeburn  6124:                     my $showdom = &display_domain_info($createdom); 
1.266     bisitz   6125:                     $response .= '<br /><br />'
                   6126:                                 .'<b>'.&mt('To add a new user:').'</b>'
1.305     raeburn  6127:                                 .'<br />';
                   6128:                     if ($context eq 'requestcrs') {
                   6129:                         $response .= &mt("(You can only define new users in the new course's domain - [_1])",$targetdom);
                   6130:                     } else {
                   6131:                         $response .= &mt("(You can only create new users in your current role's domain - [_1])",$targetdom);
                   6132:                     }
                   6133:                     $response .='<ul><li>'
1.266     bisitz   6134:                                 .&mt("Set 'Domain/institution to search' to: [_1]",'<span class="LC_cusr_emph">'.$showdom.'</span>')
                   6135:                                 .'</li><li>'
                   6136:                                 .&mt("Set 'Search criteria' to: [_1]username is ..... in selected LON-CAPA domain[_2]",'<span class="LC_cusr_emph">','</span>')
                   6137:                                 .'</li><li>'
                   6138:                                 .&mt('Provide the proposed username')
                   6139:                                 .'</li><li>'
                   6140:                                 .&mt("Click 'Search'")
                   6141:                                 .'</li></ul><br />';
1.221     raeburn  6142:                 } else {
                   6143:                     my $helplink = ' href="javascript:helpMenu('."'display'".')"';
1.305     raeburn  6144:                     $response .= '<br /><br />';
                   6145:                     if ($context eq 'requestcrs') {
1.314     raeburn  6146:                         $response .= &mt("You are not authorized to define new users in the new course's domain - [_1].",$targetdom);
1.305     raeburn  6147:                     } else {
                   6148:                         $response .= &mt("You are not authorized to create new users in your current role's domain - [_1].",$targetdom);
                   6149:                     }
                   6150:                     $response .= '<br />'
                   6151:                                  .&mt('Please contact the [_1]helpdesk[_2] if you need to create a new user.'
1.266     bisitz   6152:                                     ,' <a'.$helplink.'>'
                   6153:                                     ,'</a>')
1.305     raeburn  6154:                                  .'<br /><br />';
1.221     raeburn  6155:                 }
1.160     raeburn  6156:             }
                   6157:         }
                   6158:     }
1.179     raeburn  6159:     return ($currstate,$response,$forcenewuser);
1.160     raeburn  6160: }
                   6161: 
1.180     raeburn  6162: sub display_domain_info {
                   6163:     my ($dom) = @_;
                   6164:     my $output = $dom;
                   6165:     if ($dom ne '') { 
                   6166:         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   6167:         if ($domdesc ne '') {
                   6168:             $output .= ' <span class="LC_cusr_emph">('.$domdesc.')</span>';
                   6169:         }
                   6170:     }
                   6171:     return $output;
                   6172: }
                   6173: 
1.160     raeburn  6174: sub crumb_utilities {
                   6175:     my %elements = (
                   6176:        crtuser => {
                   6177:            srchterm => 'text',
1.172     raeburn  6178:            srchin => 'selectbox',
1.160     raeburn  6179:            srchby => 'selectbox',
                   6180:            srchtype => 'selectbox',
                   6181:            srchdomain => 'selectbox',
                   6182:        },
1.207     raeburn  6183:        crtusername => {
                   6184:            srchterm => 'text',
                   6185:            srchdomain => 'selectbox',
                   6186:        },
1.160     raeburn  6187:        docustom => {
                   6188:            rolename => 'selectbox',
                   6189:            newrolename => 'textbox',
                   6190:        },
1.179     raeburn  6191:        studentform => {
                   6192:            srchterm => 'text',
                   6193:            srchin => 'selectbox',
                   6194:            srchby => 'selectbox',
                   6195:            srchtype => 'selectbox',
                   6196:            srchdomain => 'selectbox',
                   6197:        },
1.160     raeburn  6198:     );
                   6199: 
                   6200:     my $jsback .= qq|
                   6201: function backPage(formname,prevphase,prevstate) {
1.211     raeburn  6202:     if (typeof prevphase == 'undefined') {
                   6203:         formname.phase.value = '';
                   6204:     }
                   6205:     else {  
                   6206:         formname.phase.value = prevphase;
                   6207:     }
                   6208:     if (typeof prevstate == 'undefined') {
                   6209:         formname.currstate.value = '';
                   6210:     }
                   6211:     else {
                   6212:         formname.currstate.value = prevstate;
                   6213:     }
1.160     raeburn  6214:     formname.submit();
                   6215: }
                   6216: |;
                   6217:     return ($jsback,\%elements);
                   6218: }
                   6219: 
1.26      matthew  6220: sub course_level_table {
1.89      raeburn  6221:     my (%inccourses) = @_;
1.26      matthew  6222:     my $table = '';
1.62      www      6223: # Custom Roles?
                   6224: 
1.190     raeburn  6225:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.89      raeburn  6226:     my %lt=&Apache::lonlocal::texthash(
                   6227:             'exs'  => "Existing sections",
                   6228:             'new'  => "Define new section",
                   6229:             'ssd'  => "Set Start Date",
                   6230:             'sed'  => "Set End Date",
1.131     raeburn  6231:             'crl'  => "Course Level",
1.89      raeburn  6232:             'act'  => "Activate",
                   6233:             'rol'  => "Role",
                   6234:             'ext'  => "Extent",
1.113     raeburn  6235:             'grs'  => "Section",
1.89      raeburn  6236:             'sta'  => "Start",
                   6237:             'end'  => "End"
                   6238:     );
1.62      www      6239: 
1.329     raeburn  6240:     foreach my $protectedcourse (sort(keys(%inccourses))) {
1.135     raeburn  6241: 	my $thiscourse=$protectedcourse;
1.26      matthew  6242: 	$thiscourse=~s:_:/:g;
                   6243: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1.329     raeburn  6244:         my $isowner = &is_courseowner($protectedcourse,$coursedata{'internal.courseowner'});
1.26      matthew  6245: 	my $area=$coursedata{'description'};
1.321     raeburn  6246:         my $crstype=$coursedata{'type'};
1.135     raeburn  6247: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$protectedcourse; }
1.89      raeburn  6248: 	my ($domain,$cnum)=split(/\//,$thiscourse);
1.115     albertel 6249:         my %sections_count;
1.101     albertel 6250:         if (defined($env{'request.course.id'})) {
                   6251:             if ($env{'request.course.id'} eq $domain.'_'.$cnum) {
1.115     albertel 6252:                 %sections_count = 
                   6253: 		    &Apache::loncommon::get_sections($domain,$cnum);
1.92      raeburn  6254:             }
                   6255:         }
1.321     raeburn  6256:         my @roles = &Apache::lonuserutils::roles_by_context('course','',$crstype);
1.213     raeburn  6257: 	foreach my $role (@roles) {
1.321     raeburn  6258:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.329     raeburn  6259: 	    if ((&Apache::lonnet::allowed('c'.$role,$thiscourse)) ||
                   6260:                 ((($role eq 'cc') || ($role eq 'co')) && ($isowner))) {
1.221     raeburn  6261:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
1.329     raeburn  6262:                                             $plrole,\%sections_count,\%lt);
1.221     raeburn  6263:             } elsif ($env{'request.course.sec'} ne '') {
                   6264:                 if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
                   6265:                                              $env{'request.course.sec'})) {
                   6266:                     $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6267:                                                 $plrole,\%sections_count,\%lt);
1.26      matthew  6268:                 }
                   6269:             }
                   6270:         }
1.221     raeburn  6271:         if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
1.324     raeburn  6272:             foreach my $cust (sort(keys(%customroles))) {
                   6273:                 next if ($crstype eq 'Community' && $customroles{$cust} =~ /bre\&S/);
1.221     raeburn  6274:                 my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
                   6275:                 $table .= &course_level_row($protectedcourse,$role,$area,$domain,
                   6276:                                             $cust,\%sections_count,\%lt);
                   6277:             }
1.62      www      6278: 	}
1.26      matthew  6279:     }
                   6280:     return '' if ($table eq ''); # return nothing if there is nothing 
                   6281:                                  # in the table
1.188     raeburn  6282:     my $result;
                   6283:     if (!$env{'request.course.id'}) {
                   6284:         $result = '<h4>'.$lt{'crl'}.'</h4>'."\n";
                   6285:     }
                   6286:     $result .= 
1.136     raeburn  6287: &Apache::loncommon::start_data_table().
                   6288: &Apache::loncommon::start_data_table_header_row().
                   6289: '<th>'.$lt{'act'}.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'ext'}.'</th>
                   6290: <th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
                   6291: &Apache::loncommon::end_data_table_header_row().
                   6292: $table.
                   6293: &Apache::loncommon::end_data_table();
1.26      matthew  6294:     return $result;
                   6295: }
1.88      raeburn  6296: 
1.221     raeburn  6297: sub course_level_row {
                   6298:     my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,$lt) = @_;
1.222     raeburn  6299:     my $row = &Apache::loncommon::start_data_table_row().
                   6300:               ' <td><input type="checkbox" name="act_'.
                   6301:               $protectedcourse.'_'.$role.'" /></td>'."\n".
                   6302:               ' <td>'.$plrole.'</td>'."\n".
                   6303:               ' <td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
1.322     raeburn  6304:     if (($role eq 'cc') || ($role eq 'co')) {
1.222     raeburn  6305:         $row .= '<td>&nbsp;</td>';
1.221     raeburn  6306:     } elsif ($env{'request.course.sec'} ne '') {
1.222     raeburn  6307:         $row .= ' <td><input type="hidden" value="'.
                   6308:                 $env{'request.course.sec'}.'" '.
                   6309:                 'name="sec_'.$protectedcourse.'_'.$role.'" />'.
                   6310:                 $env{'request.course.sec'}.'</td>';
1.221     raeburn  6311:     } else {
                   6312:         if (ref($sections_count) eq 'HASH') {
                   6313:             my $currsec = 
                   6314:                 &Apache::lonuserutils::course_sections($sections_count,
                   6315:                                                        $protectedcourse.'_'.$role);
1.222     raeburn  6316:             $row .= '<td><table class="LC_createuser">'."\n".
                   6317:                     '<tr class="LC_section_row">'."\n".
                   6318:                     ' <td valign="top">'.$lt->{'exs'}.'<br />'.
                   6319:                        $currsec.'</td>'."\n".
                   6320:                      ' <td>&nbsp;&nbsp;</td>'."\n".
                   6321:                      ' <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
1.221     raeburn  6322:                      '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
                   6323:                      '" value="" />'.
                   6324:                      '<input type="hidden" '.
                   6325:                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
1.222     raeburn  6326:                      '</tr></table></td>'."\n";
1.221     raeburn  6327:         } else {
1.222     raeburn  6328:             $row .= '<td><input type="text" size="10" '.
                   6329:                       'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n";
1.221     raeburn  6330:         }
                   6331:     }
1.222     raeburn  6332:     $row .= <<ENDTIMEENTRY;
                   6333: <td><input type="hidden" name="start_$protectedcourse\_$role" value="" />
1.221     raeburn  6334: <a href=
                   6335: "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  6336: <td><input type="hidden" name="end_$protectedcourse\_$role" value="" />
1.221     raeburn  6337: <a href=
                   6338: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
                   6339: ENDTIMEENTRY
1.222     raeburn  6340:     $row .= &Apache::loncommon::end_data_table_row();
                   6341:     return $row;
1.221     raeburn  6342: }
                   6343: 
1.88      raeburn  6344: sub course_level_dc {
                   6345:     my ($dcdom) = @_;
1.190     raeburn  6346:     my %customroles=&Apache::lonuserutils::my_custom_roles();
1.213     raeburn  6347:     my @roles = &Apache::lonuserutils::roles_by_context('course');
1.88      raeburn  6348:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
                   6349:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
1.133     raeburn  6350:                       '<input type="hidden" name="dccourse" value="" />';
1.88      raeburn  6351:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
1.323     raeburn  6352:             ('cu','dccourse','dcdomain','coursedesc',undef,undef,'Course/Community','crstype').'</b>';
                   6353:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'currsec','cu','role','Course/Community Browser');
1.88      raeburn  6354:     my %lt=&Apache::lonlocal::texthash(
                   6355:                     'rol'  => "Role",
1.113     raeburn  6356:                     'grs'  => "Section",
1.88      raeburn  6357:                     'exs'  => "Existing sections",
                   6358:                     'new'  => "Define new section", 
                   6359:                     'sta'  => "Start",
                   6360:                     'end'  => "End",
                   6361:                     'ssd'  => "Set Start Date",
                   6362:                     'sed'  => "Set End Date"
                   6363:                   );
1.323     raeburn  6364:     my $header = '<h4>'.&mt('Course/Community Level').'</h4>'.
1.136     raeburn  6365:                  &Apache::loncommon::start_data_table().
                   6366:                  &Apache::loncommon::start_data_table_header_row().
1.143     raeburn  6367:                  '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th>'.
1.136     raeburn  6368:                  &Apache::loncommon::end_data_table_header_row();
1.143     raeburn  6369:     my $otheritems = &Apache::loncommon::start_data_table_row()."\n".
1.323     raeburn  6370:                      '<td><br /><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'cu','dccourse','dcdomain','coursedesc','','','','crstype'".')" /></td>'."\n".
                   6371:                      '<td valign><br /><select name="role">'."\n";
1.213     raeburn  6372:     foreach my $role (@roles) {
1.135     raeburn  6373:         my $plrole=&Apache::lonnet::plaintext($role);
                   6374:         $otheritems .= '  <option value="'.$role.'">'.$plrole;
1.88      raeburn  6375:     }
                   6376:     if ( keys %customroles > 0) {
1.135     raeburn  6377:         foreach my $cust (sort keys %customroles) {
1.101     albertel 6378:             my $custrole='cr_cr_'.$env{'user.domain'}.
1.135     raeburn  6379:                     '_'.$env{'user.name'}.'_'.$cust;
                   6380:             $otheritems .= '  <option value="'.$custrole.'">'.$cust;
1.88      raeburn  6381:         }
                   6382:     }
                   6383:     $otheritems .= '</select></td><td>'.
                   6384:                      '<table border="0" cellspacing="0" cellpadding="0">'.
                   6385:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
                   6386:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
                   6387:                      '<td>&nbsp;&nbsp;</td>'.
                   6388:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
1.113     raeburn  6389:                      '<input type="text" name="newsec" value="" />'.
1.237     raeburn  6390:                      '<input type="hidden" name="section" value="" />'.
1.323     raeburn  6391:                      '<input type="hidden" name="groups" value="" />'.
                   6392:                      '<input type="hidden" name="crstype" value="" /></td>'.
1.88      raeburn  6393:                      '</tr></table></td>';
                   6394:     $otheritems .= <<ENDTIMEENTRY;
1.323     raeburn  6395: <td><br /><input type="hidden" name="start" value='' />
1.88      raeburn  6396: <a href=
                   6397: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.323     raeburn  6398: <td><br /><input type="hidden" name="end" value='' />
1.88      raeburn  6399: <a href=
                   6400: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
                   6401: ENDTIMEENTRY
1.136     raeburn  6402:     $otheritems .= &Apache::loncommon::end_data_table_row().
                   6403:                    &Apache::loncommon::end_data_table()."\n";
1.88      raeburn  6404:     return $cb_jscript.$header.$hiddenitems.$otheritems;
                   6405: }
                   6406: 
1.237     raeburn  6407: sub update_selfenroll_config {
1.241     raeburn  6408:     my ($r,$context,$permission) = @_;
1.237     raeburn  6409:     my ($row,$lt) = &get_selfenroll_titles();
1.241     raeburn  6410:     my %curr_groups = &Apache::longroup::coursegroups();
1.237     raeburn  6411:     my (%changes,%warning);
                   6412:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6413:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.241     raeburn  6414:     my $curr_types;
1.237     raeburn  6415:     if (ref($row) eq 'ARRAY') {
                   6416:         foreach my $item (@{$row}) {
                   6417:             if ($item eq 'enroll_dates') {
                   6418:                 my (%currenrolldate,%newenrolldate);
                   6419:                 foreach my $type ('start','end') {
                   6420:                     $currenrolldate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_date'};
                   6421:                     $newenrolldate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_date');
                   6422:                     if ($newenrolldate{$type} ne $currenrolldate{$type}) {
                   6423:                         $changes{'internal.selfenroll_'.$type.'_date'} = $newenrolldate{$type};
                   6424:                     }
                   6425:                 }
                   6426:             } elsif ($item eq 'access_dates') {
                   6427:                 my (%currdate,%newdate);
                   6428:                 foreach my $type ('start','end') {
                   6429:                     $currdate{$type} = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$type.'_access'};
                   6430:                     $newdate{$type} = &Apache::lonhtmlcommon::get_date_from_form('selfenroll_'.$type.'_access');
                   6431:                     if ($newdate{$type} ne $currdate{$type}) {
                   6432:                         $changes{'internal.selfenroll_'.$type.'_access'} = $newdate{$type};
                   6433:                     }
                   6434:                 }
1.241     raeburn  6435:             } elsif ($item eq 'types') {
                   6436:                 $curr_types =
                   6437:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6438:                 if ($env{'form.selfenroll_all'}) {
                   6439:                     if ($curr_types ne '*') {
                   6440:                         $changes{'internal.selfenroll_types'} = '*';
                   6441:                     } else {
                   6442:                         next;
                   6443:                     }
                   6444:                 } else {
1.249     raeburn  6445:                     my %currdoms;
1.241     raeburn  6446:                     my @entries = split(/;/,$curr_types);
                   6447:                     my @deletedoms = &Apache::loncommon::get_env_multiple('form.selfenroll_delete');
1.249     raeburn  6448:                     my @activations = &Apache::loncommon::get_env_multiple('form.selfenroll_activate');
1.241     raeburn  6449:                     my $newnum = 0;
1.249     raeburn  6450:                     my @latesttypes;
                   6451:                     foreach my $num (@activations) {
                   6452:                         my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$num);
                   6453:                         if (@types > 0) {
1.241     raeburn  6454:                             @types = sort(@types);
                   6455:                             my $typestr = join(',',@types);
1.249     raeburn  6456:                             my $typedom = $env{'form.selfenroll_dom_'.$num};
                   6457:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6458:                             $currdoms{$typedom} = 1;
1.241     raeburn  6459:                             $newnum ++;
                   6460:                         }
                   6461:                     }
1.249     raeburn  6462:                     for (my $j=0; $j<$env{'form.selfenroll_types_total'}; $j++) {                        if ((!grep(/^$j$/,@deletedoms)) && (!grep(/^$j$/,@activations))) {
                   6463:                             my @types = &Apache::loncommon::get_env_multiple('form.selfenroll_types_'.$j);
                   6464:                             if (@types > 0) {
                   6465:                                 @types = sort(@types);
                   6466:                                 my $typestr = join(',',@types);
                   6467:                                 my $typedom = $env{'form.selfenroll_dom_'.$j};
                   6468:                                 $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6469:                                 $currdoms{$typedom} = 1;
                   6470:                                 $newnum ++;
                   6471:                             }
                   6472:                         }
                   6473:                     }
                   6474:                     if ($env{'form.selfenroll_newdom'} ne '') {
                   6475:                         my $typedom = $env{'form.selfenroll_newdom'};
                   6476:                         if ((!defined($currdoms{$typedom})) && 
                   6477:                             (&Apache::lonnet::domain($typedom) ne '')) {
                   6478:                             my $typestr;
                   6479:                             my ($othertitle,$usertypes,$types) = 
                   6480:                                 &Apache::loncommon::sorted_inst_types($typedom);
                   6481:                             my $othervalue = 'any';
                   6482:                             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
                   6483:                                 if (@{$types} > 0) {
1.257     raeburn  6484:                                     my @esc_types = map { &escape($_); } @{$types};
1.249     raeburn  6485:                                     $othervalue = 'other';
1.258     raeburn  6486:                                     $typestr = join(',',(@esc_types,$othervalue));
1.249     raeburn  6487:                                 }
                   6488:                                 $typestr = $othervalue;
                   6489:                             } else {
                   6490:                                 $typestr = $othervalue;
                   6491:                             } 
                   6492:                             $latesttypes[$newnum] = $typedom.':'.$typestr;
                   6493:                             $newnum ++ ;
                   6494:                         }
                   6495:                     }
1.241     raeburn  6496:                     my $selfenroll_types = join(';',@latesttypes);
                   6497:                     if ($selfenroll_types ne $curr_types) {
                   6498:                         $changes{'internal.selfenroll_types'} = $selfenroll_types;
                   6499:                     }
                   6500:                 }
1.276     raeburn  6501:             } elsif ($item eq 'limit') {
                   6502:                 my $newlimit = $env{'form.selfenroll_limit'};
                   6503:                 my $newcap = $env{'form.selfenroll_cap'};
                   6504:                 $newcap =~s/\s+//g;
                   6505:                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   6506:                 $currlimit = 'none' if ($currlimit eq '');
                   6507:                 my $currcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   6508:                 if ($newlimit ne $currlimit) {
                   6509:                     if ($newlimit ne 'none') {
                   6510:                         if ($newcap =~ /^\d+$/) {
                   6511:                             if ($newcap ne $currcap) {
                   6512:                                 $changes{'internal.selfenroll_cap'} = $newcap;
                   6513:                             }
                   6514:                             $changes{'internal.selfenroll_limit'} = $newlimit;
                   6515:                         } else {
                   6516:                             $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.&mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.'); 
                   6517:                         }
                   6518:                     } elsif ($currcap ne '') {
                   6519:                         $changes{'internal.selfenroll_cap'} = '';
                   6520:                         $changes{'internal.selfenroll_limit'} = $newlimit; 
                   6521:                     }
                   6522:                 } elsif ($currlimit ne 'none') {
                   6523:                     if ($newcap =~ /^\d+$/) {
                   6524:                         if ($newcap ne $currcap) {
                   6525:                             $changes{'internal.selfenroll_cap'} = $newcap;
                   6526:                         }
                   6527:                     } else {
                   6528:                         $warning{$item} = &mt('Maximum enrollment setting unchanged.').'<br />'.&mt('The value provided was invalid - it must be a positive integer if enrollment is being limited.');
                   6529:                     }
                   6530:                 }
                   6531:             } elsif ($item eq 'approval') {
                   6532:                 my (@currnotified,@newnotified);
                   6533:                 my $currapproval = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   6534:                 my $currnotifylist = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   6535:                 if ($currnotifylist ne '') {
                   6536:                     @currnotified = split(/,/,$currnotifylist);
                   6537:                     @currnotified = sort(@currnotified);
                   6538:                 }
                   6539:                 my $newapproval = $env{'form.selfenroll_approval'};
                   6540:                 @newnotified = &Apache::loncommon::get_env_multiple('form.selfenroll_notify');
                   6541:                 @newnotified = sort(@newnotified);
                   6542:                 if ($newapproval ne $currapproval) {
                   6543:                     $changes{'internal.selfenroll_approval'} = $newapproval;
                   6544:                     if (!$newapproval) {
                   6545:                         if ($currnotifylist ne '') {
                   6546:                             $changes{'internal.selfenroll_notifylist'} = '';
                   6547:                         }
                   6548:                     } else {
                   6549:                         my @differences =  
1.295     raeburn  6550:                             &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  6551:                         if (@differences > 0) {
                   6552:                             if (@newnotified > 0) {
                   6553:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6554:                             } else {
                   6555:                                 $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6556:                             }
                   6557:                         }
                   6558:                     }
                   6559:                 } else {
1.295     raeburn  6560:                     my @differences = &Apache::loncommon::compare_arrays(\@currnotified,\@newnotified);
1.276     raeburn  6561:                     if (@differences > 0) {
                   6562:                         if (@newnotified > 0) {
                   6563:                             $changes{'internal.selfenroll_notifylist'} = join(',',@newnotified);
                   6564:                         } else {
                   6565:                             $changes{'internal.selfenroll_notifylist'} = '';
                   6566:                         }
                   6567:                     }
                   6568:                 }
1.237     raeburn  6569:             } else {
                   6570:                 my $curr_val = 
                   6571:                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_'.$item};
                   6572:                 my $newval = $env{'form.selfenroll_'.$item};
                   6573:                 if ($item eq 'section') {
                   6574:                     $newval = $env{'form.sections'};
1.241     raeburn  6575:                     if (defined($curr_groups{$newval})) {
1.237     raeburn  6576:                         $newval = $curr_val;
                   6577:                         $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');
                   6578:                     } elsif ($newval eq 'all') {
                   6579:                         $newval = $curr_val;
1.274     bisitz   6580:                         $warning{$item} = &mt('Section for self-enrolled users unchanged, as "all" is a reserved section name.');
1.237     raeburn  6581:                     }
                   6582:                     if ($newval eq '') {
                   6583:                         $newval = 'none';
                   6584:                     }
                   6585:                 }
                   6586:                 if ($newval ne $curr_val) {
                   6587:                     $changes{'internal.selfenroll_'.$item} = $newval;
                   6588:                 }
1.241     raeburn  6589:             }
1.237     raeburn  6590:         }
                   6591:         if (keys(%warning) > 0) {
                   6592:             foreach my $item (@{$row}) {
                   6593:                 if (exists($warning{$item})) {
                   6594:                     $r->print($warning{$item}.'<br />');
                   6595:                 }
                   6596:             } 
                   6597:         }
                   6598:         if (keys(%changes) > 0) {
                   6599:             my $putresult = &Apache::lonnet::put('environment',\%changes,$cdom,$cnum);
                   6600:             if ($putresult eq 'ok') {
                   6601:                 if ((exists($changes{'internal.selfenroll_types'})) ||
                   6602:                     (exists($changes{'internal.selfenroll_start_date'}))  ||
                   6603:                     (exists($changes{'internal.selfenroll_end_date'}))) {
                   6604:                     my %crsinfo = &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',
                   6605:                                                                 $cnum,undef,undef,'Course');
                   6606:                     my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                   6607:                     if (ref($crsinfo{$env{'request.course.id'}}) eq 'HASH') {
                   6608:                         foreach my $item ('selfenroll_types','selfenroll_start_date','selfenroll_end_date') {
                   6609:                             if (exists($changes{'internal.'.$item})) {
                   6610:                                 $crsinfo{$env{'request.course.id'}}{$item} = 
                   6611:                                     $changes{'internal.'.$item};
                   6612:                             }
                   6613:                         }
                   6614:                         my $crsputresult =
                   6615:                             &Apache::lonnet::courseidput($cdom,\%crsinfo,
                   6616:                                                          $chome,'notime');
                   6617:                     }
                   6618:                 }
                   6619:                 $r->print(&mt('The following changes were made to self-enrollment settings:').'<ul>');
                   6620:                 foreach my $item (@{$row}) {
                   6621:                     my $title = $item;
                   6622:                     if (ref($lt) eq 'HASH') {
                   6623:                         $title = $lt->{$item};
                   6624:                     }
                   6625:                     if ($item eq 'enroll_dates') {
                   6626:                         foreach my $type ('start','end') {
                   6627:                             if (exists($changes{'internal.selfenroll_'.$type.'_date'})) {
                   6628:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_date'});
1.244     bisitz   6629:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  6630:                                           $title,$type,$newdate).'</li>');
                   6631:                             }
                   6632:                         }
                   6633:                     } elsif ($item eq 'access_dates') {
                   6634:                         foreach my $type ('start','end') {
                   6635:                             if (exists($changes{'internal.selfenroll_'.$type.'_access'})) {
                   6636:                                 my $newdate = &Apache::lonlocal::locallocaltime($changes{'internal.selfenroll_'.$type.'_access'});
1.244     bisitz   6637:                                 $r->print('<li>'.&mt('[_1]: "[_2]" set to "[_3]".',
1.237     raeburn  6638:                                           $title,$type,$newdate).'</li>');
                   6639:                             }
                   6640:                         }
1.276     raeburn  6641:                     } elsif ($item eq 'limit') {
                   6642:                         if ((exists($changes{'internal.selfenroll_limit'})) ||
                   6643:                             (exists($changes{'internal.selfenroll_cap'}))) {
                   6644:                             my ($newval,$newcap);
                   6645:                             if ($changes{'internal.selfenroll_cap'} ne '') {
                   6646:                                 $newcap = $changes{'internal.selfenroll_cap'}
                   6647:                             } else {
                   6648:                                 $newcap = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_cap'};
                   6649:                             }
                   6650:                             if ($changes{'internal.selfenroll_limit'} eq 'none') {
                   6651:                                 $newval = &mt('No limit');
                   6652:                             } elsif ($changes{'internal.selfenroll_limit'} eq 
                   6653:                                      'allstudents') {
                   6654:                                 $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   6655:                             } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
                   6656:                                 $newval = &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
                   6657:                             } else {
                   6658:                                 my $currlimit =  $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_limit'};
                   6659:                                 if ($currlimit eq 'allstudents') {
                   6660:                                     $newval = &mt('New self-enrollment no longer allowed when total (all students) reaches [_1].',$newcap);
                   6661:                                 } elsif ($changes{'internal.selfenroll_limit'} eq 'selfenrolled') {
1.308     raeburn  6662:                                     $newval =  &mt('New self-enrollment no longer allowed when total number of self-enrolled students reaches [_1].',$newcap);
1.276     raeburn  6663:                                 }
                   6664:                             }
                   6665:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
                   6666:                         }
                   6667:                     } elsif ($item eq 'approval') {
                   6668:                         if ((exists($changes{'internal.selfenroll_approval'})) ||
                   6669:                             (exists($changes{'internal.selfenroll_notifylist'}))) {
                   6670:                             my ($newval,$newnotify);
                   6671:                             if (exists($changes{'internal.selfenroll_notifylist'})) {
                   6672:                                 $newnotify = $changes{'internal.selfenroll_notifylist'};
                   6673:                             } else {   
                   6674:                                 $newnotify = $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_notifylist'};
                   6675:                             }
                   6676:                             if ($changes{'internal.selfenroll_approval'}) {
                   6677:                                 $newval = &mt('Yes');
                   6678:                             } elsif ($changes{'internal.selfenroll_approval'} eq '0') {
                   6679:                                 $newval = &mt('No');
                   6680:                             } else {
                   6681:                                 my $currapproval = 
                   6682:                                     $env{'course.'.$env{'request.course.id'}.'.internal.selfenroll_approval'};
                   6683:                                 if ($currapproval) {
                   6684:                                     $newval = &mt('Yes');
                   6685:                                 } else {
                   6686:                                     $newval = &mt('No');
                   6687:                                 }
                   6688:                             }
                   6689:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval));
                   6690:                             if ($newnotify) {
1.277     raeburn  6691:                                 $r->print('<br />'.&mt('The following will be notified when an enrollment request needs approval, or has been approved: [_1].',$newnotify));
1.276     raeburn  6692:                             } else {
1.277     raeburn  6693:                                 $r->print('<br />'.&mt('No notifications sent when an enrollment request needs approval, or has been approved.'));
1.276     raeburn  6694:                             }
                   6695:                             $r->print('</li>'."\n");
                   6696:                         }
1.237     raeburn  6697:                     } else {
                   6698:                         if (exists($changes{'internal.selfenroll_'.$item})) {
1.241     raeburn  6699:                             my $newval = $changes{'internal.selfenroll_'.$item};
                   6700:                             if ($item eq 'types') {
                   6701:                                 if ($newval eq '') {
                   6702:                                     $newval = &mt('None');
                   6703:                                 } elsif ($newval eq '*') {
                   6704:                                     $newval = &mt('Any user in any domain');
                   6705:                                 }
1.245     raeburn  6706:                             } elsif ($item eq 'registered') {
                   6707:                                 if ($newval eq '1') {
                   6708:                                     $newval = &mt('Yes');
                   6709:                                 } elsif ($newval eq '0') {
                   6710:                                     $newval = &mt('No');
                   6711:                                 }
1.241     raeburn  6712:                             }
1.244     bisitz   6713:                             $r->print('<li>'.&mt('"[_1]" set to "[_2]".',$title,$newval).'</li>'."\n");
1.237     raeburn  6714:                         }
                   6715:                     }
                   6716:                 }
                   6717:                 $r->print('</ul>');
                   6718:                 my %newenvhash;
                   6719:                 foreach my $key (keys(%changes)) {
                   6720:                     $newenvhash{'course.'.$env{'request.course.id'}.'.'.$key} = $changes{$key};
                   6721:                 }
1.238     raeburn  6722:                 &Apache::lonnet::appenv(\%newenvhash);
1.237     raeburn  6723:             } else {
                   6724:                 $r->print(&mt('An error occurred when saving changes to self-enrollment settings in this course.').'<br />'.&mt('The error was: [_1].',$putresult));
                   6725:             }
                   6726:         } else {
1.249     raeburn  6727:             $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.237     raeburn  6728:         }
                   6729:     } else {
1.249     raeburn  6730:         $r->print(&mt('No changes were made to the existing self-enrollment settings in this course.'));
1.241     raeburn  6731:     }
1.256     raeburn  6732:     my ($visible,$cansetvis,$vismsgs,$visactions) = &visible_in_cat($cdom,$cnum);
                   6733:     if (ref($visactions) eq 'HASH') {
                   6734:         if (!$visible) {
                   6735:             $r->print('<br />'.$visactions->{'miss'}.'<br />'.$visactions->{'yous'}.
                   6736:                       '<br />');
                   6737:             if (ref($vismsgs) eq 'ARRAY') {
                   6738:                 $r->print('<br />'.$visactions->{'take'}.'<ul>');
                   6739:                 foreach my $item (@{$vismsgs}) {
                   6740:                     $r->print('<li>'.$visactions->{$item}.'</li>');
                   6741:                 }
                   6742:                 $r->print('</ul>');
                   6743:             }
                   6744:             $r->print($cansetvis);
                   6745:         }
                   6746:     } 
1.237     raeburn  6747:     return;
                   6748: }
                   6749: 
                   6750: sub get_selfenroll_titles {
1.276     raeburn  6751:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   6752:                'approval','limit');
1.237     raeburn  6753:     my %lt = &Apache::lonlocal::texthash (
                   6754:                 types        => 'Users allowed to self-enroll in this course',
1.245     raeburn  6755:                 registered   => 'Restrict self-enrollment to students officially registered for the course',
1.237     raeburn  6756:                 enroll_dates => 'Dates self-enrollment available',
1.256     raeburn  6757:                 access_dates => 'Course access dates assigned to self-enrolling users',
                   6758:                 section      => 'Section assigned to self-enrolling users',
1.276     raeburn  6759:                 approval     => 'Self-enrollment requests need approval?',
                   6760:                 limit        => 'Enrollment limit',
1.237     raeburn  6761:              );
                   6762:     return (\@row,\%lt);
                   6763: }
                   6764: 
1.329     raeburn  6765: sub is_courseowner {
                   6766:     my ($thiscourse,$courseowner) = @_;
                   6767:     if ($courseowner eq '') {
                   6768:         if ($env{'request.course.id'} eq $thiscourse) {
                   6769:             $courseowner = $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
                   6770:         }
                   6771:     }
                   6772:     if ($courseowner ne '') {
                   6773:         if ($courseowner eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   6774:                 return 1;
                   6775:         }
                   6776:     }
                   6777:     return;
                   6778: }
                   6779: 
1.27      matthew  6780: #---------------------------------------------- end functions for &phase_two
1.29      matthew  6781: 
                   6782: #--------------------------------- functions for &phase_two and &phase_three
                   6783: 
                   6784: #--------------------------end of functions for &phase_two and &phase_three
1.1       www      6785: 
                   6786: 1;
                   6787: __END__
1.2       www      6788: 
                   6789: 

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