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

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

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