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

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

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