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

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

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