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

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

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