Annotation of loncom/interface/lonuserutils.pm, revision 1.109.2.9

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Utility functions for managing LON-CAPA user accounts
                      3: #
1.109.2.9! raeburn     4: # $Id: lonuserutils.pm,v 1.109.2.8 2010/11/09 14:14:55 raeburn Exp $
1.1       raeburn     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: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: #
                     28: ###############################################################
                     29: ###############################################################
                     30: 
                     31: package Apache::lonuserutils;
                     32: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon;
                     37: use Apache::lonlocal;
1.8       raeburn    38: use Apache::longroup;
1.109.2.3  raeburn    39: use Apache::lonnavmaps;
1.8       raeburn    40: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn    41: 
                     42: ###############################################################
                     43: ###############################################################
                     44: # Drop student from all sections of a course, except optional $csec
                     45: sub modifystudent {
1.52      raeburn    46:     my ($udom,$unam,$courseid,$csec,$desiredhost,$context)=@_;
1.1       raeburn    47:     # if $csec is undefined, drop the student from all the courses matching
                     48:     # this one.  If $csec is defined, drop them from all other sections of
                     49:     # this course and add them to section $csec
1.17      raeburn    50:     my ($cnum,$cdom) = &get_course_identity($courseid);
1.1       raeburn    51:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
                     52:     my ($tmp) = keys(%roles);
                     53:     # Bail out if we were unable to get the students roles
                     54:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                     55:     # Go through the roles looking for enrollment in this course
                     56:     my $result = '';
                     57:     foreach my $course (keys(%roles)) {
                     58:         if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
                     59:             # We are in this course
                     60:             my $section=$1;
                     61:             $section='' if ($course eq "/$cdom/$cnum".'_st');
                     62:             if (defined($csec) && $section eq $csec) {
                     63:                 $result .= 'ok:';
                     64:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
                     65:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
                     66:                 my $now=time;
                     67:                 # if this is an active role
                     68:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
                     69:                     my $reply=&Apache::lonnet::modifystudent
                     70:                         # dom  name  id mode pass     f     m     l     g
                     71:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.22      raeburn    72:                          $section,time,undef,undef,$desiredhost,'','manual',
1.52      raeburn    73:                          '',$courseid,'',$context);
1.1       raeburn    74:                     $result .= $reply.':';
                     75:                 }
                     76:             }
                     77:         }
                     78:     }
                     79:     if ($result eq '') {
1.37      raeburn    80:         $result = &mt('Unable to find section for this student');
1.1       raeburn    81:     } else {
                     82:         $result =~ s/(ok:)+/ok/g;
                     83:     }
                     84:     return $result;
                     85: }
                     86: 
                     87: sub modifyuserrole {
                     88:     my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
                     89:         $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
1.84      raeburn    90:         $end,$start,$checkid,$inststatus) = @_;
1.5       raeburn    91:     my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1       raeburn    92:     if ($setting eq 'course' || $context eq 'course') {
                     93:         $scope = '/'.$cid;
                     94:         $scope =~ s/\_/\//g;
1.103     raeburn    95:         if (($role ne 'cc') && ($role ne 'co') && ($sec ne '')) {
1.1       raeburn    96:             $scope .='/'.$sec;
                     97:         }
1.5       raeburn    98:     } elsif ($context eq 'domain') {
1.1       raeburn    99:         $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn   100:     } elsif ($context eq 'author') {
1.1       raeburn   101:         $scope =  '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                    102:     }
                    103:     if ($context eq 'domain') {
                    104:         my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    105:         if ($uhome ne 'no_host') {
1.5       raeburn   106:             if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1       raeburn   107:                 if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
                    108:                     ($umode eq 'localauth')) {
                    109:                     $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
                    110:                 }
                    111:             }
1.5       raeburn   112:             if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
                    113:                 ($env{'form.recurseid'}) && ($checkid)) {
                    114:                 my %userupdate = (
                    115:                                   lastname   => $last,
                    116:                                   middlename => $middle,
                    117:                                   firstname  => $first,
                    118:                                   generation => $gene,
                    119:                                   id         => $uid,
                    120:                                  );
                    121:                 $idresult = &propagate_id_change($uname,$udom,\%userupdate);
                    122:             }
1.1       raeburn   123:         }
                    124:     }
                    125:     $userresult =
                    126:         &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
                    127:                                     $middle,$last,$gene,$forceid,$desiredhome,
1.84      raeburn   128:                                     $email,$inststatus);
1.1       raeburn   129:     if ($userresult eq 'ok') {
1.5       raeburn   130:         if ($role ne '') {
1.22      raeburn   131:             $role =~ s/_/\//g;
1.1       raeburn   132:             $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
1.52      raeburn   133:                                                       $role,$end,$start,'',
                    134:                                                       '',$context);
1.1       raeburn   135:         }
                    136:     }
1.5       raeburn   137:     return ($userresult,$authresult,$roleresult,$idresult);
1.1       raeburn   138: }
                    139: 
1.5       raeburn   140: sub propagate_id_change {
                    141:     my ($uname,$udom,$user) = @_;
1.12      raeburn   142:     my (@types,@roles);
1.5       raeburn   143:     @types = ('active','future');
                    144:     @roles = ('st');
                    145:     my $idresult;
                    146:     my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12      raeburn   147:                         $udom,'userroles',\@types,\@roles);
                    148:     my %args = (
                    149:                 one_time => 1,
                    150:                );
1.5       raeburn   151:     foreach my $item (keys(%roleshash)) {
1.22      raeburn   152:         my ($cnum,$cdom,$role) = split(/:/,$item,-1);
1.5       raeburn   153:         my ($start,$end) = split(/:/,$roleshash{$item});
                    154:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   155:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
                    156:             my %coursehash = 
                    157:                 &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
                    158:             my $cdesc = $coursehash{'description'};
                    159:             if ($cdesc eq '') { 
                    160:                 $cdesc = $cdom.'_'.$cnum;
                    161:             }
1.5       raeburn   162:             if ($result eq 'ok') {
1.12      raeburn   163:                 $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   164:             } else {
1.12      raeburn   165:                 $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   166:             }
                    167:         }
                    168:     }
                    169:     return $idresult;
                    170: }
                    171: 
                    172: sub update_classlist {
1.63      raeburn   173:     my ($cdom,$cnum,$udom,$uname,$user,$newend) = @_;
1.6       albertel  174:     my ($uid,$classlistentry);
1.5       raeburn   175:     my $fullname =
                    176:         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
                    177:                                      $user->{'lastname'},$user->{'generation'},
                    178:                                      'lastname');
                    179:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
                    180:                                          $cdom,$cnum);
                    181:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
                    182:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
                    183:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
1.63      raeburn   184:     my $endidx = &Apache::loncoursedata::CL_END() - 2;
                    185:     my $startidx = &Apache::loncoursedata::CL_START() - 2;
1.5       raeburn   186:     for (my $i=0; $i<@classinfo; $i++) {
1.63      raeburn   187:         if ($i == $endidx) {
                    188:             if ($newend ne '') {
                    189:                 $classlistentry .= $newend.':';
                    190:             } else {
                    191:                 $classlistentry .= $classinfo[$i].':';
                    192:             }
                    193:         } elsif ($i == $startidx) {
                    194:             if ($newend ne '') {
                    195:                 if ($classinfo[$i] > $newend) {
                    196:                     $classlistentry .= $newend.':';
                    197:                 } else {
                    198:                     $classlistentry .= $classinfo[$i].':';
                    199:                 }
                    200:             } else {
                    201:                 $classlistentry .= $classinfo[$i].':';
                    202:             }
                    203:         } elsif ($i == $ididx) {
1.5       raeburn   204:             if (defined($user->{'id'})) {
                    205:                 $classlistentry .= $user->{'id'}.':';
                    206:             } else {
                    207:                 $classlistentry .= $classinfo[$i].':';
                    208:             }
                    209:         } elsif ($i == $nameidx) {
1.63      raeburn   210:             if (defined($user->{'lastname'})) {
                    211:                 $classlistentry .= $fullname.':';
                    212:             } else {
                    213:                 $classlistentry .= $classinfo[$i].':';
                    214:             }
1.5       raeburn   215:         } else {
                    216:             $classlistentry .= $classinfo[$i].':';
                    217:         }
                    218:     }
                    219:     $classlistentry =~ s/:$//;
                    220:     my $reply=&Apache::lonnet::cput('classlist',
                    221:                                     {"$uname:$udom" => $classlistentry},
                    222:                                     $cdom,$cnum);
                    223:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                    224:         return 'ok';
                    225:     } else {
                    226:         return 'error: '.$reply;
                    227:     }
                    228: }
                    229: 
                    230: 
1.1       raeburn   231: ###############################################################
                    232: ###############################################################
1.2       raeburn   233: # build a role type and role selection form
                    234: sub domain_roles_select {
                    235:     # Set up the role type and role selection boxes when in 
                    236:     # domain context   
                    237:     #
                    238:     # Role types
1.101     raeburn   239:     my @roletypes = ('domain','author','course','community');
1.2       raeburn   240:     my %lt = &role_type_names();
1.1       raeburn   241:     #
                    242:     # build up the menu information to be passed to
                    243:     # &Apache::loncommon::linked_select_forms
                    244:     my %select_menus;
1.2       raeburn   245:     if ($env{'form.roletype'} eq '') {
                    246:         $env{'form.roletype'} = 'domain';
                    247:     }
                    248:     foreach my $roletype (@roletypes) {
1.1       raeburn   249:         # set up the text for this domain
1.2       raeburn   250:         $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.102     raeburn   251:         my $crstype;
                    252:         if ($roletype eq 'community') {
                    253:             $crstype = 'Community';
                    254:         }
1.1       raeburn   255:         # we want a choice of 'default' as the default in the second menu
1.2       raeburn   256:         if ($env{'form.roletype'} ne '') {
                    257:             $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
                    258:         } else { 
                    259:             $select_menus{$roletype}->{'default'} = 'Any';
                    260:         }
1.1       raeburn   261:         # Now build up the other items in the second menu
1.2       raeburn   262:         my @roles;
                    263:         if ($roletype eq 'domain') {
                    264:             @roles = &domain_roles();
1.13      raeburn   265:         } elsif ($roletype eq 'author') {
1.2       raeburn   266:             @roles = &construction_space_roles();
                    267:         } else {
1.17      raeburn   268:             my $custom = 1;
1.101     raeburn   269:             @roles = &course_roles('domain',undef,$custom,$roletype);
1.1       raeburn   270:         }
1.2       raeburn   271:         my $order = ['Any',@roles];
                    272:         $select_menus{$roletype}->{'order'} = $order; 
                    273:         foreach my $role (@roles) {
1.5       raeburn   274:             if ($role eq 'cr') {
                    275:                 $select_menus{$roletype}->{'select2'}->{$role} =
                    276:                               &mt('Custom role');
                    277:             } else {
                    278:                 $select_menus{$roletype}->{'select2'}->{$role} = 
1.102     raeburn   279:                               &Apache::lonnet::plaintext($role,$crstype);
1.5       raeburn   280:             }
1.2       raeburn   281:         }
                    282:         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1       raeburn   283:     }
1.2       raeburn   284:     my $result = &Apache::loncommon::linked_select_forms
                    285:         ('studentform',('&nbsp;'x3).&mt('Role: '),$env{'form.roletype'},
1.101     raeburn   286:          'roletype','showrole',\%select_menus,
                    287:          ['domain','author','course','community']);
1.1       raeburn   288:     return $result;
                    289: }
                    290: 
                    291: ###############################################################
                    292: ###############################################################
                    293: sub hidden_input {
                    294:     my ($name,$value) = @_;
                    295:     return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
                    296: }
                    297: 
                    298: sub print_upload_manager_header {
1.109.2.5  raeburn   299:     my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype,$formname,
                    300:         $can_assign)=@_;
1.1       raeburn   301:     my $javascript;
                    302:     #
                    303:     if (! exists($env{'form.upfile_associate'})) {
                    304:         $env{'form.upfile_associate'} = 'forward';
                    305:     }
                    306:     if ($env{'form.associate'} eq 'Reverse Association') {
                    307:         if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                    308:             $env{'form.upfile_associate'} = 'reverse';
                    309:         } else {
                    310:             $env{'form.upfile_associate'} = 'forward';
                    311:         }
                    312:     }
                    313:     if ($env{'form.upfile_associate'} eq 'reverse') {
1.109.2.5  raeburn   314:         $javascript=&upload_manager_javascript_reverse_associate($can_assign);
1.1       raeburn   315:     } else {
1.109.2.5  raeburn   316:         $javascript=&upload_manager_javascript_forward_associate($can_assign);
1.1       raeburn   317:     }
                    318:     #
                    319:     # Deal with restored settings
                    320:     my $password_choice = '';
                    321:     if (exists($env{'form.ipwd_choice'}) &&
                    322:         $env{'form.ipwd_choice'} ne '') {
                    323:         # If a column was specified for password, assume it is for an
                    324:         # internal password.  This is a bug waiting to be filed (could be
                    325:         # local or krb auth instead of internal) but I do not have the
                    326:         # time to mess around with this now.
                    327:         $password_choice = 'int';
                    328:     }
                    329:     #
1.109.2.5  raeburn   330:     my ($sectionjs,$groupslist);
1.22      raeburn   331:     if ($context eq 'course') {
                    332:         $groupslist = &get_groupslist();
1.109.2.7  raeburn   333:         if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   334:             $sectionjs = <<"ENDJS";
                    335: 
                    336: function toggleSectionsDefault() {
                    337:     var usingsecs;
                    338:     if (document.$formname.usesection.length > 1) {
                    339:         for (var i=0; i<document.$formname.usesection.length; i++) {
                    340:             if (document.$formname.usesection[i].checked) {
                    341:                 usingsecs = document.$formname.usesection[i].value;
                    342:             }
                    343:         }
                    344:     }
                    345:     if (usingsecs == 1) {
                    346:         document.getElementById('defaultsec').style.display="block";
                    347:     } else {
                    348:         document.getElementById('defaultsec').style.display="none";
                    349:         document.$formname.newsec.value = "";
                    350:         document.$formname.sections.value = "";
                    351:     }
                    352:     return;
                    353: }
                    354: ENDJS
                    355:         }
1.22      raeburn   356:     }
1.1       raeburn   357:     my $javascript_validations =
1.22      raeburn   358:         &javascript_validations('upload',$krbdefdom,$password_choice,undef,
                    359:                                 $env{'request.role.domain'},$context,
1.103     raeburn   360:                                 $groupslist,$crstype);
1.91      bisitz    361:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1.95      bisitz    362:     $r->print('<p>'
                    363:              .&mt('Total number of records found in file: [_1]'
                    364:                  ,'<b>'.$distotal.'</b>')
                    365:              ."</p>\n");
1.1       raeburn   366:     $r->print('<div class="LC_left_float"><h3>'.
                    367:               &mt('Identify fields in uploaded list')."</h3>\n");
1.109.2.5  raeburn   368:     $r->print(&mt('Enter as many fields as you can.').'<br />'.
                    369:               &mt('The system will inform you and bring you back to this page,').
                    370:               '<br />'.&mt('if the data selected are insufficient to add users.')."<br />\n");
1.1       raeburn   371:     $r->print(&hidden_input('action','upload').
                    372:               &hidden_input('state','got_file').
                    373:               &hidden_input('associate','').
                    374:               &hidden_input('datatoken',$datatoken).
                    375:               &hidden_input('fileupload',$env{'form.fileupload'}).
                    376:               &hidden_input('upfiletype',$env{'form.upfiletype'}).
1.109.2.5  raeburn   377:               &hidden_input('upfile_associate',$env{'form.upfile_associate'}).
1.109.2.7  raeburn   378:               &hidden_input('context',$env{'form.context'}));
1.86      bisitz    379:     $r->print('<br /><label><input type="checkbox" name="noFirstLine"'.$checked.' />'.
1.73      bisitz    380:               &mt('Ignore First Line').'</label><br />');
1.59      bisitz    381:     $r->print('<br /><input type="button" value="'.&mt('Reverse Association').'" '.
                    382:               'name="Reverse Association" '.
1.96      bisitz    383:               'onclick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
1.1       raeburn   384:     $r->print("<br /><br />\n".
                    385:               '<script type="text/javascript" language="Javascript">'."\n".
1.96      bisitz    386:               '// <![CDATA['."\n".
1.109.2.5  raeburn   387:               $javascript."\n".$javascript_validations."\n".$sectionjs."\n".
1.96      bisitz    388:               '// ]]>'."\n".
                    389:               '</script>');
1.1       raeburn   390: }
                    391: 
                    392: ###############################################################
                    393: ###############################################################
                    394: sub javascript_validations {
1.22      raeburn   395:     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
1.103     raeburn   396:         $context,$groupslist,$crstype)=@_;
1.22      raeburn   397:     my %param = (
                    398:                   kerb_def_dom => $krbdefdom,
                    399:                   curr_authtype => $curr_authtype,
                    400:                 );
1.37      raeburn   401:     if ($mode eq 'upload') {
1.22      raeburn   402:         $param{'formname'} = 'studentform';
1.1       raeburn   403:     } elsif ($mode eq 'createcourse') {
1.22      raeburn   404:         $param{'formname'} = 'ccrs';
1.1       raeburn   405:     } elsif ($mode eq 'modifycourse') {
1.22      raeburn   406:         $param{'formname'} = 'cmod';
                    407:         $param{'mode'} = 'modifycourse',
                    408:         $param{'curr_autharg'} = $curr_authfield;
                    409:     }
                    410: 
                    411:     my ($setsection_call,$setsections_js);
                    412:     my $finish = "  vf.submit();\n";
                    413:     if ($mode eq 'upload') {
                    414:         if (($context eq 'course') || ($context eq 'domain')) {
                    415:             if ($context eq 'course') {
                    416:                 if ($env{'request.course.sec'} eq '') {
1.109     raeburn   417:                     $setsection_call = 'setSections(document.'.$param{'formname'}.",'$crstype'".');';
1.109.2.7  raeburn   418:                     if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   419:                         $setsections_js =  &newsections_javascript($param{'formname'});
                    420:                     } else {
                    421:                         $setsections_js =
                    422:                             &setsections_javascript($param{'formname'},$groupslist,
                    423:                                                     $mode,'',$crstype);
                    424:                     }
1.22      raeburn   425:                 } else {
                    426:                     $setsection_call = "'ok'";
                    427:                 }
                    428:             } elsif ($context eq 'domain') {
                    429:                 $setsection_call = 'setCourse()';
1.37      raeburn   430:                 $setsections_js = &dc_setcourse_js($param{'formname'},$mode,$context);
1.22      raeburn   431:             }
                    432:             $finish = "  var checkSec = $setsection_call\n".
                    433:                       "  if (checkSec == 'ok') {\n".
                    434:                       "      vf.submit();\n".
                    435:                       "   }\n";
                    436:         }
1.1       raeburn   437:     }
1.22      raeburn   438:     my $authheader = &Apache::loncommon::authform_header(%param);
1.1       raeburn   439: 
                    440:     my %alert = &Apache::lonlocal::texthash
                    441:         (username => 'You need to specify the username field.',
                    442:          authen   => 'You must choose an authentication type.',
                    443:          krb      => 'You need to specify the Kerberos domain.',
                    444:          ipass    => 'You need to specify the initial password.',
                    445:          name     => 'The optional name field was not specified.',
1.93      bisitz    446:          snum     => 'The optional student/employee ID field was not specified.',
1.1       raeburn   447:          section  => 'The optional section field was not specified.',
1.75      schafran  448:          email    => 'The optional e-mail address field was not specified.',
1.1       raeburn   449:          role     => 'The optional role field was not specified.',
1.57      raeburn   450:          domain   => 'The optional domain field was not specified.',
1.1       raeburn   451:          continue => 'Continue adding users?',
                    452:          );
1.84      raeburn   453:     if (($mode eq 'upload') && ($context eq 'domain')) {
                    454:         $alert{'inststatus'} = &mt('The optional affiliation field was not specified'); 
                    455:     }
1.37      raeburn   456:     my $function_name = <<"END";
1.22      raeburn   457: $setsections_js
                    458: 
1.84      raeburn   459: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus) {
1.1       raeburn   460: END
                    461:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($domain);
                    462:     my $auth_checks;
                    463:     if ($mode eq 'createcourse') {
                    464:         $auth_checks .= (<<END);
                    465:     if (vf.autoadds[0].checked == true) {
                    466:         if (current.radiovalue == null || current.radiovalue == 'nochange') {
                    467:             alert('$alert{'authen'}');
                    468:             return;
                    469:         }
                    470:     }
                    471: END
                    472:     } else {
                    473:         $auth_checks .= (<<END);
                    474:     var foundatype=0;
                    475:     if (founduname==0) {
                    476:         alert('$alert{'username'}');
                    477:         return;
                    478:     }
                    479: 
                    480: END
                    481:         if ($authnum > 1) {
                    482:             $auth_checks .= (<<END);
                    483:     if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
                    484:         // They did not check any of the login radiobuttons.
                    485:         alert('$alert{'authen'}');
                    486:         return;
                    487:     }
                    488: END
                    489:         }
                    490:     }
                    491:     if ($mode eq 'createcourse') {
                    492:         $auth_checks .= "
                    493:     if ( (vf.autoadds[0].checked == true) &&
                    494:          (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
                    495: ";
                    496:     } elsif ($mode eq 'modifycourse') {
                    497:         $auth_checks .= "
                    498:     if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
                    499: ";
                    500:     }
                    501:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    502:         $auth_checks .= (<<END);
                    503:         var alertmsg = '';
                    504:         switch (current.radiovalue) {
                    505:             case 'krb':
                    506:                 alertmsg = '$alert{'krb'}';
                    507:                 break;
                    508:             default:
                    509:                 alertmsg = '';
                    510:         }
                    511:         if (alertmsg != '') {
                    512:             alert(alertmsg);
                    513:             return;
                    514:         }
                    515:     }
                    516: END
                    517:     } else {
                    518:         $auth_checks .= (<<END);
                    519:     foundatype=1;
                    520:     if (current.argfield == null || current.argfield == '') {
                    521:         var alertmsg = '';
1.38      raeburn   522:         switch (current.radiovalue) {
1.1       raeburn   523:             case 'krb':
                    524:                 alertmsg = '$alert{'krb'}';
                    525:                 break;
                    526:             case 'loc':
                    527:             case 'fsys':
                    528:                 alertmsg = '$alert{'ipass'}';
                    529:                 break;
                    530:             case 'fsys':
                    531:                 alertmsg = '';
                    532:                 break;
                    533:             default:
                    534:                 alertmsg = '';
                    535:         }
                    536:         if (alertmsg != '') {
                    537:             alert(alertmsg);
                    538:             return;
                    539:         }
                    540:     }
                    541: END
                    542:     }
                    543:     my $section_checks;
                    544:     my $optional_checks = '';
                    545:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    546:         $optional_checks = (<<END);
                    547:     vf.submit();
                    548: }
                    549: END
                    550:     } else {
                    551:         $section_checks = &section_check_js();
1.109.2.5  raeburn   552: 
1.109.2.7  raeburn   553:         unless ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   554:             $optional_checks = (<<END);
1.1       raeburn   555:     var message='';
                    556:     if (foundname==0) {
                    557:         message='$alert{'name'}';
                    558:     }
                    559:     if (foundid==0) {
                    560:         if (message!='') {
                    561:             message+='\\n';
                    562:         }
                    563:         message+='$alert{'snum'}';
                    564:     }
                    565:     if (foundsec==0) {
                    566:         if (message!='') {
                    567:             message+='\\n';
                    568:         }
1.109.2.8  raeburn   569:         message+='$alert{'section'}';
1.1       raeburn   570:     }
                    571:     if (foundemail==0) {
                    572:         if (message!='') {
                    573:             message+='\\n';
                    574:         }
                    575:         message+='$alert{'email'}';
                    576:     }
1.57      raeburn   577:     if (foundrole==0) {
                    578:         if (message!='') {
                    579:             message+='\\n';
                    580:         }
                    581:         message+='$alert{'role'}';
                    582:     }
                    583:     if (founddomain==0) {
                    584:         if (message!='') {
                    585:             message+='\\n';
                    586:         }
                    587:         message+='$alert{'domain'}';
                    588:     }
1.84      raeburn   589: END
1.109.2.5  raeburn   590:         }
1.84      raeburn   591:         if (($mode eq 'upload') && ($context eq 'domain')) {
                    592:             $optional_checks .= (<<END);
                    593: 
                    594:     if (foundinststatus==0) {
                    595:         if (message!='') {
                    596:             message+='\\n';
                    597:         }
                    598:         message+='$alert{'inststatus'}';
                    599:     }
                    600: END
                    601:         }
1.109.2.7  raeburn   602:         if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   603:             $optional_checks .= (<<END);
                    604:     vf.state.value='enrolling';
                    605:     $finish
                    606: }
                    607: END
                    608:         } else {
                    609:             $optional_checks .= (<<END);
1.84      raeburn   610: 
1.1       raeburn   611:     if (message!='') {
                    612:         message+= '\\n$alert{'continue'}';
                    613:         if (confirm(message)) {
                    614:             vf.state.value='enrolling';
1.22      raeburn   615:             $finish
1.1       raeburn   616:         }
                    617:     } else {
                    618:         vf.state.value='enrolling';
1.22      raeburn   619:         $finish
1.1       raeburn   620:     }
                    621: }
                    622: END
1.109.2.5  raeburn   623:        }
1.1       raeburn   624:     }
1.37      raeburn   625:     my $result = $function_name.$auth_checks.$optional_checks."\n".
                    626:                  $section_checks.$authheader;
1.1       raeburn   627:     return $result;
                    628: }
                    629: ###############################################################
                    630: ###############################################################
                    631: sub upload_manager_javascript_forward_associate {
1.109.2.5  raeburn   632:     my ($can_assign) = @_;
1.109.2.9! raeburn   633:     my ($auth_update,$numbuttons,$argreset);
1.109.2.5  raeburn   634:     if (ref($can_assign) eq 'HASH') {
1.109.2.9! raeburn   635:         if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
        !           636:             $argreset .= "      vf.krbarg.value='';\n";
        !           637:             $numbuttons ++ ;
        !           638:         }
        !           639:         if ($can_assign->{'int'}) {
        !           640:             $argreset .= "      vf.intarg.value='';\n";
        !           641:             $numbuttons ++;
        !           642:         }
        !           643:         if ($can_assign->{'loc'}) {
        !           644:             $argreset .= "      vf.locarg.value='';\n";
        !           645:             $numbuttons ++;
        !           646:         }
        !           647:         if (!$can_assign->{'int'}) {
        !           648:             my $warning = &mt('You may not specify an initial password for each user, as this is only available when new users use LON-CAPA internal authentication.\n').
        !           649:                           &mt('Your current role does not have rights to create users with that authentication type.');
        !           650:             $auth_update = <<"END";
        !           651:    // Currently the initial password field is only supported for internal auth
        !           652:    // (see bug 6368).
        !           653:    if (nw==2) {
        !           654:        eval('vf.f'+tf+'.selectedIndex=0;')
        !           655:        alert('$warning');
        !           656:    }
        !           657: END
        !           658:         } elsif ($numbuttons > 1) {
1.109.2.5  raeburn   659:             $auth_update = <<"END";
                    660:    // If we set the password, make the password form below correspond to
                    661:    // the new value.
1.109.2.9! raeburn   662:    if (nw==2) {
1.109.2.5  raeburn   663:       changed_radio('int',document.studentform);
                    664:       set_auth_radio_buttons('int',document.studentform);
1.109.2.9! raeburn   665: $argreset
        !           666:    }
        !           667: 
1.109.2.5  raeburn   668: END
                    669:         }
                    670:     }
                    671: 
1.1       raeburn   672:     return(<<ENDPICK);
                    673: function verify(vf,sec_caller) {
                    674:     var founduname=0;
                    675:     var foundpwd=0;
                    676:     var foundname=0;
                    677:     var foundid=0;
                    678:     var foundsec=0;
                    679:     var foundemail=0;
                    680:     var foundrole=0;
1.57      raeburn   681:     var founddomain=0;
1.84      raeburn   682:     var foundinststatus=0;
1.1       raeburn   683:     var tw;
                    684:     for (i=0;i<=vf.nfields.value;i++) {
                    685:         tw=eval('vf.f'+i+'.selectedIndex');
                    686:         if (tw==1) { founduname=1; }
1.109.2.8  raeburn   687:         if (tw==2) { foundpwd=1; }
                    688:         if ((tw>=3) && (tw<=7)) { foundname=1; }
                    689:         if (tw==8) { foundid=1; }
                    690:         if (tw==9) { foundsec=1; }
1.1       raeburn   691:         if (tw==10) { foundemail=1; }
                    692:         if (tw==11) { foundrole=1; }
1.57      raeburn   693:         if (tw==12) { founddomain=1; }
1.84      raeburn   694:         if (tw==13) { foundinststatus=1; }
1.1       raeburn   695:     }
1.84      raeburn   696:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
1.1       raeburn   697: }
                    698: 
                    699: //
                    700: // vf = this.form
                    701: // tf = column number
                    702: //
                    703: // values of nw
                    704: //
                    705: // 0 = none
                    706: // 1 = username
1.109.2.7  raeburn   707: // 2 = ipwd  (password)
                    708: // 3 = names (lastname, firstnames)
                    709: // 4 = fname (firstname)
                    710: // 5 = mname (middlename)
                    711: // 6 = lname (lastname)
                    712: // 7 = gen   (generation)
                    713: // 8 = id
                    714: // 9 = section
1.1       raeburn   715: // 10 = email address
                    716: // 11 = role
1.57      raeburn   717: // 12 = domain
1.84      raeburn   718: // 13 = inststatus
1.1       raeburn   719: 
                    720: function flip(vf,tf) {
                    721:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    722:    var i;
                    723:    // make sure no other columns are labeled the same as this one
                    724:    for (i=0;i<=vf.nfields.value;i++) {
                    725:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    726:           eval('vf.f'+i+'.selectedIndex=0;')
                    727:       }
                    728:    }
                    729:    // If we set this to 'lastname, firstnames', clear out all the ones
1.109.2.7  raeburn   730:    // set to 'fname','mname','lname','gen' (4,5,6,7) currently.
                    731:    if (nw==3) {
1.1       raeburn   732:       for (i=0;i<=vf.nfields.value;i++) {
1.109.2.7  raeburn   733:          if ((eval('vf.f'+i+'.selectedIndex')>=4) &&
                    734:              (eval('vf.f'+i+'.selectedIndex')<=7)) {
1.1       raeburn   735:              eval('vf.f'+i+'.selectedIndex=0;')
                    736:          }
                    737:       }
                    738:    }
1.109.2.7  raeburn   739:    // If we set this to one of 'fname','mname','lname','gen' (4,5,6,7),
                    740:    // clear out any that are set to 'lastname, firstnames' (3)
                    741:    if ((nw>=4) && (nw<=7)) {
1.1       raeburn   742:       for (i=0;i<=vf.nfields.value;i++) {
1.109.2.7  raeburn   743:          if (eval('vf.f'+i+'.selectedIndex')==3) {
1.1       raeburn   744:              eval('vf.f'+i+'.selectedIndex=0;')
                    745:          }
                    746:       }
                    747:    }
1.109.2.5  raeburn   748:    $auth_update
1.1       raeburn   749: }
                    750: 
                    751: function clearpwd(vf) {
                    752:     var i;
                    753:     for (i=0;i<=vf.nfields.value;i++) {
1.109.2.7  raeburn   754:         if (eval('vf.f'+i+'.selectedIndex')==2) {
1.1       raeburn   755:             eval('vf.f'+i+'.selectedIndex=0;')
                    756:         }
                    757:     }
                    758: }
                    759: 
                    760: ENDPICK
                    761: }
                    762: 
                    763: ###############################################################
                    764: ###############################################################
                    765: sub upload_manager_javascript_reverse_associate {
1.109.2.5  raeburn   766:     my ($can_assign) = @_;
1.109.2.9! raeburn   767:     my ($auth_update,$numbuttons,$argreset);
1.109.2.5  raeburn   768:     if (ref($can_assign) eq 'HASH') {
1.109.2.9! raeburn   769:         if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
        !           770:             $argreset .= "      vf.krbarg.value='';\n";
        !           771:             $numbuttons ++ ;
1.109.2.5  raeburn   772:         }
                    773:         if ($can_assign->{'int'}) {
1.109.2.9! raeburn   774:             $argreset .= "      vf.intarg.value='';\n";
        !           775:             $numbuttons ++;
1.109.2.5  raeburn   776:         }
                    777:         if ($can_assign->{'loc'}) {
1.109.2.9! raeburn   778:             $argreset .= "      vf.locarg.value='';\n";
        !           779:             $numbuttons ++;
        !           780:         }
        !           781:         if (!$can_assign->{'int'}) {
        !           782:             my $warning = &mt('You may not specify an initial password, as this is only available when new users use LON-CAPA internal authentication.\n').
        !           783:                           &mt('Your current role does not have rights to create users with that authentication type.');
        !           784:             $auth_update = <<"END";
        !           785:    // Currently the initial password field is only supported for internal auth
        !           786:    // (see bug 6368).
        !           787:    if (tf==1 && nw!=0) {
        !           788:        eval('vf.f'+tf+'.selectedIndex=0;')
        !           789:        alert('$warning');
        !           790:    }
        !           791: END
        !           792:         } elsif ($numbuttons > 1) {
        !           793:             $auth_update = <<"END";
        !           794:    // initial password specified, pick internal authentication
        !           795:    if (tf==1 && nw!=0) {
        !           796:       changed_radio('int',document.studentform);
        !           797:       set_auth_radio_buttons('int',document.studentform);
        !           798: $argreset
        !           799:    }
        !           800: 
        !           801: END
1.109.2.5  raeburn   802:         }
                    803:     }
1.109.2.9! raeburn   804: 
1.1       raeburn   805:     return(<<ENDPICK);
                    806: function verify(vf,sec_caller) {
                    807:     var founduname=0;
                    808:     var foundpwd=0;
                    809:     var foundname=0;
                    810:     var foundid=0;
                    811:     var foundsec=0;
1.109.2.8  raeburn   812:     var foundemail=0;
1.1       raeburn   813:     var foundrole=0;
1.57      raeburn   814:     var founddomain=0;
1.84      raeburn   815:     var foundinststatus=0;
1.1       raeburn   816:     var tw;
                    817:     for (i=0;i<=vf.nfields.value;i++) {
                    818:         tw=eval('vf.f'+i+'.selectedIndex');
                    819:         if (i==0 && tw!=0) { founduname=1; }
1.109.2.8  raeburn   820:         if (i==1 && tw!=0) { foundpwd=1; }
                    821:         if (((i>=2) && (i<=6)) && tw!=0 ) { foundname=1; }
                    822:         if (i==7 && tw!=0) { foundid=1; }
                    823:         if (i==8 && tw!=0) { foundsec=1; }
                    824:         if (i==9 && tw!=0) { foundemail=1; }
                    825:         if (i==10 && tw!=0) { foundrole=1; }
                    826:         if (i==11 && tw!=0) { founddomain=1; }
                    827:         if (i==12 && tw!=0) { foundinstatus=1; }
1.1       raeburn   828:     }
1.109.2.8  raeburn   829:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
1.1       raeburn   830: }
                    831: 
                    832: function flip(vf,tf) {
                    833:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    834:    var i;
                    835:    // picked the all one name field, reset the other name ones to blank
1.109.2.8  raeburn   836:    if (tf==2 && nw!=0) {
                    837:       for (i=3;i<=6;i++) {
1.1       raeburn   838:          eval('vf.f'+i+'.selectedIndex=0;')
                    839:       }
                    840:    }
                    841:    //picked one of the piecewise name fields, reset the all in
                    842:    //one field to blank
1.109.2.8  raeburn   843:    if ((tf>=3) && (tf<=6) && (nw!=0)) {
                    844:       eval('vf.f2.selectedIndex=0;')
1.1       raeburn   845:    }
1.109.2.5  raeburn   846:    $auth_update
1.1       raeburn   847: }
                    848: 
                    849: function clearpwd(vf) {
                    850:     var i;
1.109.2.8  raeburn   851:     if (eval('vf.f1.selectedIndex')!=0) {
                    852:         eval('vf.f1.selectedIndex=0;')
1.1       raeburn   853:     }
                    854: }
                    855: ENDPICK
                    856: }
                    857: 
                    858: ###############################################################
                    859: ###############################################################
                    860: sub print_upload_manager_footer {
1.101     raeburn   861:     my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission,$crstype) = @_;
1.22      raeburn   862:     my $form = 'document.studentform';
                    863:     my $formname = 'studentform';
1.1       raeburn   864:     my ($krbdef,$krbdefdom) =
                    865:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.22      raeburn   866:     my %param = ( formname => $form,
1.1       raeburn   867:                   kerb_def_dom => $krbdefdom,
                    868:                   kerb_def_auth => $krbdef
                    869:                   );
                    870:     if (exists($env{'form.ipwd_choice'}) &&
                    871:         defined($env{'form.ipwd_choice'}) &&
                    872:         $env{'form.ipwd_choice'} ne '') {
                    873:         $param{'curr_authtype'} = 'int';
                    874:     }
1.109.2.7  raeburn   875:     if ($env{'form.context'} eq 'requestcrs') {
                    876:         $param{'context'} = $env{'form.context'};
1.109.2.5  raeburn   877:     }
1.1       raeburn   878:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    879:     my $intform = &Apache::loncommon::authform_internal(%param);
                    880:     my $locform = &Apache::loncommon::authform_local(%param);
1.22      raeburn   881:     my $date_table = &date_setting_table(undef,undef,$context,undef,
1.101     raeburn   882:                                          $formname,$permission,$crstype);
1.95      bisitz    883: 
1.1       raeburn   884:     my $Str = "\n".'<div class="LC_left_float">';
                    885:     $Str .= &hidden_input('nfields',$i);
                    886:     $Str .= &hidden_input('keyfields',$keyfields);
1.95      bisitz    887: 
                    888:     $Str .= '<h3>'.&mt('Options').'</h3>'
                    889:            .&Apache::lonhtmlcommon::start_pick_box();
1.109.2.7  raeburn   890:     if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   891:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default password'));
                    892:     } else {
                    893:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Login Type'));
                    894:     }
1.1       raeburn   895:     if ($context eq 'domain') {
1.95      bisitz    896:         $Str .= '<p>'
                    897:                .&mt('Change authentication for existing users in domain "[_1]" to these settings?'
                    898:                    ,$defdom)
                    899:                .'&nbsp;<span class="LC_nobreak"><label>'
                    900:                .'<input type="radio" name="changeauth" value="No" checked="checked" />'
                    901:                .&mt('No').'</label>'
                    902:                .'&nbsp;&nbsp;<label>'
                    903:                .'<input type="radio" name="changeauth" value="Yes" />'
                    904:                .&mt('Yes').'</label>'
                    905:                .'</span></p>'; 
1.1       raeburn   906:     } else {
1.95      bisitz    907:         $Str .= '<p class="LC_info">'."\n".
1.109.2.5  raeburn   908:             &mt('This will not take effect if the user already exists.');
                    909:             my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($defdom);
                    910:             if ($authnum > 1) {
                    911:                 $Str .= &Apache::loncommon::help_open_topic('Auth_Options');
                    912:             }
                    913:             $Str .= "</p>\n";
1.1       raeburn   914:     }
1.97      raeburn   915:     $Str .= &set_login($defdom,$krbform,$intform,$locform);
1.109.2.7  raeburn   916:     if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   917:         $Str .= '<input type="hidden" name="defaultdomain" value="gcitest" />';
                    918:     }
1.1       raeburn   919:     my ($home_server_pick,$numlib) =
                    920:         &Apache::loncommon::home_server_form_item($defdom,'lcserver',
                    921:                                                   'default','hide');
                    922:     if ($numlib > 1) {
1.97      raeburn   923:         $Str .= &Apache::lonhtmlcommon::row_closure()
                    924:                .&Apache::lonhtmlcommon::row_title(
1.95      bisitz    925:                     &mt('LON-CAPA Home Server for New Users'))
                    926:                .&mt('LON-CAPA domain: [_1] with home server:','"'.$defdom.'"')
                    927:                .$home_server_pick
                    928:                .&Apache::lonhtmlcommon::row_closure();
                    929:     } else {
1.97      raeburn   930:         $Str .= $home_server_pick.
                    931:                 &Apache::lonhtmlcommon::row_closure();
1.95      bisitz    932:     }
                    933: 
1.109.2.7  raeburn   934:     unless ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   935:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default domain'))
                    936:                .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1)
                    937:                .&Apache::lonhtmlcommon::row_closure()
                    938:                .&Apache::lonhtmlcommon::row_title(&mt('Starting and Ending Dates'))
                    939:                ."<p>\n".$date_table."</p>\n"
                    940:                .&Apache::lonhtmlcommon::row_closure();
                    941:     }
1.1       raeburn   942:     if ($context eq 'domain') {
1.95      bisitz    943:         $Str .= &Apache::lonhtmlcommon::row_title(
                    944:                     &mt('Settings for assigning roles'))
                    945:                .&mt('Pick the action to take on roles for these users:').'<br />'
                    946:                .'<span class="LC_nobreak"><label>'
                    947:                .'<input type="radio" name="roleaction" value="norole" checked="checked" />'
                    948:                .'&nbsp;'.&mt('No role changes').'</label>'
                    949:                .'&nbsp;&nbsp;&nbsp;<label>'
                    950:                .'<input type="radio" name="roleaction" value="domain" />'
                    951:                .'&nbsp;'.&mt('Add a domain role').'</label>'
                    952:                .'&nbsp;&nbsp;&nbsp;<label>'
                    953:                .'<input type="radio" name="roleaction" value="course" />'
1.103     raeburn   954:                .'&nbsp;'.&mt('Add a course/community role').'</label>'
1.95      bisitz    955:                .'</span>';
                    956:     } elsif ($context eq 'author') {
                    957:         $Str .= &Apache::lonhtmlcommon::row_title(
                    958:                     &mt('Default role'))
                    959:                .&mt('Choose the role to assign to users without a value specified in the uploaded file.')
1.1       raeburn   960:     } elsif ($context eq 'course') {
1.109.2.7  raeburn   961:         if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn   962:             $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default section')).
                    963:                     &mt('Students can be assigned to different sections.').'<br />'.
                    964:                     &mt('Will you be using sections?').'&nbsp;'.
                    965:                     '<input type="radio" name="usesection" value="1" '.
                    966:                     'onclick="javascript:toggleSectionsDefault()" />'.
                    967:                     '&nbsp;'.&mt('Yes').'</label>'.
                    968:                     '&nbsp;&nbsp;&nbsp;<label>'.
                    969:                     '<input type="radio" name="usesection" value="0" checked="checked" '.
                    970:                     'onclick="javascript:toggleSectionsDefault()" />'.
                    971:                     '&nbsp;'.&mt('No').'</label>';
                    972:         } else {
                    973:             $Str .= &Apache::lonhtmlcommon::row_title(
1.95      bisitz    974:                     &mt('Default role and section'))
1.109.2.5  raeburn   975:                    .&mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file.');
                    976:         }    
1.95      bisitz    977:     } else {
                    978:         $Str .= &Apache::lonhtmlcommon::row_title(
                    979:                     &mt('Default role and/or section(s)'))
                    980:                .&mt('Role and/or section(s) for users without values specified in the uploaded file.');
1.1       raeburn   981:     }
1.22      raeburn   982:     if (($context eq 'domain') || ($context eq 'author')) {
1.95      bisitz    983:         $Str .= '<br />';
1.22      raeburn   984:         my ($options,$cb_script,$coursepick) = &default_role_selector($context,1);
                    985:         if ($context eq 'domain') {
1.95      bisitz    986:             $Str .= '<p>'
                    987:                    .'<b>'.&mt('Domain Level').'</b><br />'
                    988:                    .$options
                    989:                    .'</p><p>'
                    990:                    .'<b>'.&mt('Course Level').'</b>'
                    991:                    .'</p>'
                    992:                    .$cb_script.$coursepick
                    993:                    .&Apache::lonhtmlcommon::row_closure();
1.22      raeburn   994:         } elsif ($context eq 'author') {
1.95      bisitz    995:             $Str .= $options
                    996:                    .&Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.22      raeburn   997:         }
1.1       raeburn   998:     } else {
1.22      raeburn   999:         my ($cnum,$cdom) = &get_course_identity();
1.109.2.5  raeburn  1000:         my ($rowtitle,$closure);
1.109.2.7  raeburn  1001:         if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn  1002:             $closure = 1;
                   1003:             $rowtitle = &mt('default section name (letters/numbers only)');
                   1004:         } else {
                   1005:             $rowtitle = &mt('section');
                   1006:         }
1.22      raeburn  1007:         my $secbox = &section_picker($cdom,$cnum,'Any',$rowtitle,
1.109.2.5  raeburn  1008:                                     $permission,$context,'upload',$crstype,
1.109.2.7  raeburn  1009:                                     $env{'form.context'}).
1.109.2.5  raeburn  1010:                      &Apache::lonhtmlcommon::row_closure($closure);
1.109.2.7  raeburn  1011:         if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn  1012:             $Str .= '<div id ="defaultsec" style="display: none" />'.$secbox.'</div>';
                   1013:         } else {
                   1014:             $Str .= $secbox; 
                   1015:         }
1.101     raeburn  1016:         my %lt;
                   1017:         if ($crstype eq 'Community') {
                   1018:             %lt = &Apache::lonlocal::texthash (
                   1019:                     disp => 'Display members with current/future access who are not in the uploaded file',
                   1020:                     stus => 'Members selected from this list can be dropped.'
                   1021:             );
                   1022:         } else {
                   1023:             %lt = &Apache::lonlocal::texthash (
                   1024:                     disp => 'Display students with current/future access who are not in the uploaded file',
                   1025:                     stus => 'Students selected from this list can be dropped.'
                   1026:             );
                   1027:         }
1.109.2.7  raeburn  1028:         unless ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn  1029:             $Str .= &Apache::lonhtmlcommon::row_title(&mt('Full Update'))
                   1030:                    .'<label><input type="checkbox" name="fullup" value="yes" />'
                   1031:                    .' '.$lt{'disp'}
                   1032:                    .'</label><br />'
                   1033:                    .$lt{'stus'}
                   1034:                    .&Apache::lonhtmlcommon::row_closure();
                   1035:         }
1.1       raeburn  1036:     }
1.5       raeburn  1037:     if ($context eq 'course' || $context eq 'domain') {
1.109.2.7  raeburn  1038:         unless ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn  1039:             $Str .= &forceid_change($context);
                   1040:         }
1.5       raeburn  1041:     }
1.95      bisitz   1042: 
                   1043:     $Str .= &Apache::lonhtmlcommon::end_pick_box();
1.73      bisitz   1044:     $Str .= '</div>';
1.95      bisitz   1045: 
                   1046:     # Footer
                   1047:     $Str .= '<div class="LC_clear_float_footer">'
                   1048:            .'<hr />';
1.1       raeburn  1049:     if ($context eq 'course') {
1.95      bisitz   1050:         $Str .= '<p class="LC_info">'
1.103     raeburn  1051:                .&mt('Note: This operation may be time consuming when adding several users.')
1.95      bisitz   1052:                .'</p>';
1.73      bisitz   1053:     }
1.95      bisitz   1054:     $Str .= '<p><input type="button"'
1.96      bisitz   1055:            .' onclick="javascript:verify(this.form,this.form.csec)"'
                   1056:            .' value="'.&mt('Update Users').'" />'
1.95      bisitz   1057:            .'</p>'."\n"
1.73      bisitz   1058:            .'</div>';
1.1       raeburn  1059:     $r->print($Str);
                   1060:     return;
                   1061: }
                   1062: 
1.5       raeburn  1063: sub forceid_change {
                   1064:     my ($context) = @_;
                   1065:     my $output = 
1.95      bisitz   1066:         &Apache::lonhtmlcommon::row_title(&mt('Student/Employee ID'))
                   1067:        .'<label><input type="checkbox" name="forceid" value="yes" />'
                   1068:        .&mt('Disable Student/Employee ID Safeguard and force change of conflicting IDs')
                   1069:        .'</label><br />'."\n"
                   1070:        .&mt('(only do if you know what you are doing.)')."\n";
1.5       raeburn  1071:     if ($context eq 'domain') {
1.25      raeburn  1072:         $output .= '<br /><label><input type="checkbox" name="recurseid"'.
1.86      bisitz   1073:                    ' value="yes" />'. 
1.93      bisitz   1074:   &mt('Update student/employee ID in courses in which user is active/future student,[_1](if forcing change).','<br />').
1.25      raeburn  1075:                    '</label>'."\n";
1.5       raeburn  1076:     }
1.95      bisitz   1077:     $output .= &Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.5       raeburn  1078:     return $output;
                   1079: }
                   1080: 
1.1       raeburn  1081: ###############################################################
                   1082: ###############################################################
                   1083: sub print_upload_manager_form {
1.109.2.5  raeburn  1084:     my ($r,$context,$permission,$crstype,$formname) = @_;
1.1       raeburn  1085:     my $firstLine;
                   1086:     my $datatoken;
                   1087:     if (!$env{'form.datatoken'}) {
                   1088:         $datatoken=&Apache::loncommon::upfile_store($r);
                   1089:     } else {
                   1090:         $datatoken=$env{'form.datatoken'};
                   1091:         &Apache::loncommon::load_tmp_file($r);
                   1092:     }
                   1093:     my @records=&Apache::loncommon::upfile_record_sep();
                   1094:     if($env{'form.noFirstLine'}){
                   1095:         $firstLine=shift(@records);
                   1096:     }
                   1097:     my $total=$#records;
                   1098:     my $distotal=$total+1;
                   1099:     my $today=time;
                   1100:     my $halfyear=$today+15552000;
                   1101:     #
                   1102:     # Restore memorized settings
                   1103:     my $col_setting_names =  { 'username_choice' => 'scalar', # column settings
                   1104:                                'names_choice' => 'scalar',
                   1105:                                'fname_choice' => 'scalar',
                   1106:                                'mname_choice' => 'scalar',
                   1107:                                'lname_choice' => 'scalar',
                   1108:                                'gen_choice' => 'scalar',
                   1109:                                'id_choice' => 'scalar',
                   1110:                                'sec_choice' => 'scalar',
                   1111:                                'ipwd_choice' => 'scalar',
                   1112:                                'email_choice' => 'scalar',
                   1113:                                'role_choice' => 'scalar',
1.57      raeburn  1114:                                'domain_choice' => 'scalar',
1.84      raeburn  1115:                                'inststatus_choice' => 'scalar',
1.1       raeburn  1116:                              };
                   1117:     my $defdom = $env{'request.role.domain'};
                   1118:     if ($context eq 'course') {
                   1119:         &Apache::loncommon::restore_course_settings('enrollment_upload',
                   1120:                                                     $col_setting_names);
                   1121:     } else {
                   1122:         &Apache::loncommon::restore_settings($context,'user_upload',
                   1123:                                              $col_setting_names);
                   1124:     }
                   1125:     #
                   1126:     # Determine kerberos parameters as appropriate
                   1127:     my ($krbdef,$krbdefdom) =
                   1128:         &Apache::loncommon::get_kerberos_defaults($defdom);
                   1129:     #
1.109.2.5  raeburn  1130:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($defdom);
1.22      raeburn  1131:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
1.109.2.5  raeburn  1132:                                  $permission,$crstype,$formname,\%can_assign);
1.1       raeburn  1133:     my $i;
                   1134:     my $keyfields;
                   1135:     if ($total>=0) {
                   1136:         my @field=
                   1137:             (['username',&mt('Username'),     $env{'form.username_choice'}],
1.109.2.5  raeburn  1138:              ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
1.1       raeburn  1139:              ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
                   1140:              ['fname',&mt('First Name'),      $env{'form.fname_choice'}],
                   1141:              ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
                   1142:              ['lname',&mt('Last Name'),       $env{'form.lname_choice'}],
                   1143:              ['gen',  &mt('Generation'),      $env{'form.gen_choice'}],
1.61      bisitz   1144:              ['id',   &mt('Student/Employee ID'),$env{'form.id_choice'}],
1.1       raeburn  1145:              ['sec',  &mt('Section'),          $env{'form.sec_choice'}],
                   1146:              ['email',&mt('E-mail Address'),   $env{'form.email_choice'}],
1.57      raeburn  1147:              ['role',&mt('Role'),             $env{'form.role_choice'}],
1.84      raeburn  1148:              ['domain',&mt('Domain'),         $env{'form.domain_choice'}],
                   1149:              ['inststatus',&mt('Affiliation'), $env{'form.inststatus_choice'}]);
1.1       raeburn  1150:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   1151:             &Apache::loncommon::csv_print_samples($r,\@records);
                   1152:             $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1153:                                                           \@field);
                   1154:             foreach (@field) {
                   1155:                 $keyfields.=$_->[0].',';
                   1156:             }
                   1157:             chop($keyfields);
                   1158:         } else {
                   1159:             unshift(@field,['none','']);
                   1160:             $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1161:                                                             \@field);
                   1162:             my %sone=&Apache::loncommon::record_sep($records[0]);
                   1163:             $keyfields=join(',',sort(keys(%sone)));
                   1164:         }
                   1165:     }
                   1166:     $r->print('</div>');
                   1167:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1.101     raeburn  1168:                                  $context,$permission,$crstype);
1.1       raeburn  1169: }
                   1170: 
                   1171: sub setup_date_selectors {
1.22      raeburn  1172:     my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
                   1173:     if ($formname eq '') {
                   1174:         $formname = 'studentform';
                   1175:     }
1.1       raeburn  1176:     if (! defined($starttime)) {
                   1177:         $starttime = time;
                   1178:         unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1179:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1180:                             '.default_enrollment_start_date'})) {
                   1181:                 $starttime = $env{'course.'.$env{'request.course.id'}.
                   1182:                                   '.default_enrollment_start_date'};
                   1183:             }
                   1184:         }
                   1185:     }
                   1186:     if (! defined($endtime)) {
                   1187:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                   1188:         unless ($mode eq 'createcourse') {
                   1189:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1190:                             '.default_enrollment_end_date'})) {
                   1191:                 $endtime = $env{'course.'.$env{'request.course.id'}.
                   1192:                                 '.default_enrollment_end_date'};
                   1193:             }
                   1194:         }
                   1195:     }
1.11      raeburn  1196: 
                   1197:     my $startdateform = 
1.22      raeburn  1198:         &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11      raeburn  1199:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1200: 
                   1201:     my $enddateform = 
1.22      raeburn  1202:         &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11      raeburn  1203:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1204: 
1.1       raeburn  1205:     if ($mode eq 'create_enrolldates') {
                   1206:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1207:                                                             'startenroll',
                   1208:                                                             $starttime);
                   1209:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1210:                                                           'endenroll',
                   1211:                                                           $endtime);
                   1212:     }
                   1213:     if ($mode eq 'create_defaultdates') {
                   1214:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1215:                                                             'startaccess',
                   1216:                                                             $starttime);
                   1217:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1218:                                                           'endaccess',
                   1219:                                                           $endtime);
                   1220:     }
                   1221:     return ($startdateform,$enddateform);
                   1222: }
                   1223: 
                   1224: 
                   1225: sub get_dates_from_form {
1.54      raeburn  1226:     my ($startname,$endname) = @_;
                   1227:     if ($startname eq '') {
                   1228:         $startname = 'startdate';
                   1229:     }
                   1230:     if ($endname eq '') {
                   1231:         $endname = 'enddate';
                   1232:     }
                   1233:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   1234:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.1       raeburn  1235:     if ($env{'form.no_end_date'}) {
                   1236:         $enddate = 0;
                   1237:     }
                   1238:     return ($startdate,$enddate);
                   1239: }
                   1240: 
                   1241: sub date_setting_table {
1.101     raeburn  1242:     my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission,$crstype) = @_;
1.11      raeburn  1243:     my $nolink;
                   1244:     if ($bulkaction) {
                   1245:         $nolink = 1;
                   1246:     }
                   1247:     my ($startform,$endform) = 
1.22      raeburn  1248:         &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1       raeburn  1249:     my $dateDefault;
                   1250:     if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1251:         $dateDefault = '&nbsp;';
1.13      raeburn  1252:     } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11      raeburn  1253:         if (($bulkaction eq 'reenable') || 
                   1254:             ($bulkaction eq 'activate') || 
1.22      raeburn  1255:             ($bulkaction eq 'chgdates') ||
                   1256:             ($env{'form.action'} eq 'upload')) {
                   1257:             if ($env{'request.course.sec'} eq '') {
                   1258:                 $dateDefault = '<span class="LC_nobreak">'.
1.101     raeburn  1259:                     '<label><input type="checkbox" name="makedatesdefault" value="1" /> ';
                   1260:                 if ($crstype eq 'Community') {
                   1261:                     $dateDefault .= &mt("make these dates the default access dates for future community enrollment");
                   1262:                 } else {
                   1263:                     $dateDefault .= &mt("make these dates the default access dates for future course enrollment");
                   1264:                 }
                   1265:                 $dateDefault .= '</label></span>';
1.22      raeburn  1266:             }
1.11      raeburn  1267:         }
1.1       raeburn  1268:     }
1.11      raeburn  1269:     my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1       raeburn  1270:     if (defined($endtime) && $endtime == 0) {
1.70      bisitz   1271:         $perpetual .= ' checked="checked"';
1.1       raeburn  1272:     }
1.11      raeburn  1273:     $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1       raeburn  1274:     if ($mode eq 'create_enrolldates') {
                   1275:         $perpetual = '&nbsp;';
                   1276:     }
1.11      raeburn  1277:     my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
                   1278:     $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
                   1279:                                                      'LC_oddrow_value')."\n".
                   1280:                $startform."\n".
                   1281:                &Apache::lonhtmlcommon::row_closure(1).
                   1282:                &Apache::lonhtmlcommon::row_title(&mt('Ending Date'), 
                   1283:                                                      'LC_oddrow_value')."\n".
                   1284:                $endform.'&nbsp;'.$perpetual.
                   1285:                &Apache::lonhtmlcommon::row_closure(1).
1.22      raeburn  1286:                &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn  1287:     if ($dateDefault) {
                   1288:         $result .=  $dateDefault.'<br />'."\n";
                   1289:     }
                   1290:     return $result;
                   1291: }
                   1292: 
                   1293: sub make_dates_default {
1.101     raeburn  1294:     my ($startdate,$enddate,$context,$crstype) = @_;
1.1       raeburn  1295:     my $result = '';
                   1296:     if ($context eq 'course') {
1.17      raeburn  1297:         my ($cnum,$cdom) = &get_course_identity();
1.1       raeburn  1298:         my $put_result = &Apache::lonnet::put('environment',
                   1299:                 {'default_enrollment_start_date'=>$startdate,
1.17      raeburn  1300:                  'default_enrollment_end_date'  =>$enddate},$cdom,$cnum);
1.1       raeburn  1301:         if ($put_result eq 'ok') {
1.101     raeburn  1302:             if ($crstype eq 'Community') {
                   1303:                 $result .= &mt('Set default start and end access dates for community.');
                   1304:             } else {
                   1305:                 $result .= &mt('Set default start and end access dates for course.');
                   1306:             }
                   1307:             $result .= '<br />'."\n";
1.1       raeburn  1308:             #
                   1309:             # Refresh the course environment
                   1310:             &Apache::lonnet::coursedescription($env{'request.course.id'},
                   1311:                                                {'freshen_cache' => 1});
                   1312:         } else {
1.101     raeburn  1313:             if ($crstype eq 'Community') {
                   1314:                 $result .= &mt('Unable to set default access dates for community');
                   1315:             } else {
                   1316:                 $result .= &mt('Unable to set default access dates for course');
                   1317:             }
                   1318:             $result .= ':'.$put_result.'<br />';
1.1       raeburn  1319:         }
                   1320:     }
                   1321:     return $result;
                   1322: }
                   1323: 
                   1324: sub default_role_selector {
1.101     raeburn  1325:     my ($context,$checkpriv,$crstype) = @_;
1.1       raeburn  1326:     my %customroles;
                   1327:     my ($options,$coursepick,$cb_jscript);
1.13      raeburn  1328:     if ($context ne 'author') {
1.104     raeburn  1329:         %customroles = &my_custom_roles($crstype);
1.1       raeburn  1330:     }
                   1331: 
                   1332:     my %lt=&Apache::lonlocal::texthash(
                   1333:                     'rol'  => "Role",
                   1334:                     'grs'  => "Section",
                   1335:                     'exs'  => "Existing sections",
                   1336:                     'new'  => "New section",
                   1337:                   );
1.109.2.2  raeburn  1338:     $options = '<select name="defaultrole">'."\n";
                   1339:     unless (($context eq 'course') && (&Apache::loncommon::needs_gci_custom())) {
                   1340:         $options .= ' <option value="">'.&mt('Please select').'</option>'."\n";
                   1341:     }
1.1       raeburn  1342:     if ($context eq 'course') {
1.101     raeburn  1343:         $options .= &default_course_roles($context,$checkpriv,$crstype,%customroles);
1.13      raeburn  1344:     } elsif ($context eq 'author') {
1.2       raeburn  1345:         my @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1346:         foreach my $role (@roles) {
                   1347:            my $plrole=&Apache::lonnet::plaintext($role);
                   1348:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1349:         }
                   1350:     } elsif ($context eq 'domain') {
1.2       raeburn  1351:         my @roles = &domain_roles($checkpriv);
1.1       raeburn  1352:         foreach my $role (@roles) {
                   1353:            my $plrole=&Apache::lonnet::plaintext($role);
                   1354:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1355:         }
                   1356:         my $courseform = &Apache::loncommon::selectcourse_link
1.103     raeburn  1357:             ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course/Community');
1.1       raeburn  1358:         $cb_jscript = 
1.103     raeburn  1359:             &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform','courserole','Course/Community');
1.1       raeburn  1360:         $coursepick = &Apache::loncommon::start_data_table().
                   1361:                       &Apache::loncommon::start_data_table_header_row().
                   1362:                       '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
                   1363:                       '<th>'.$lt{'grs'}.'</th>'.
                   1364:                       &Apache::loncommon::end_data_table_header_row().
                   1365:                       &Apache::loncommon::start_data_table_row()."\n".
1.103     raeburn  1366:                       '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc','','','','crstype'".')" /></td>'."\n".
1.1       raeburn  1367:                       '<td><select name="courserole">'."\n".
1.101     raeburn  1368:                       &default_course_roles($context,$checkpriv,'Course',%customroles)."\n".
1.1       raeburn  1369:                       '</select></td><td>'.
                   1370:                       '<table class="LC_createuser">'.
                   1371:                       '<tr class="LC_section_row"><td valign"top">'.
1.22      raeburn  1372:                       $lt{'exs'}.'<br /><select name="currsec">'.
1.1       raeburn  1373:                       ' <option value=""><--'.&mt('Pick course first').
                   1374:                       '</select></td>'.
                   1375:                       '<td>&nbsp;&nbsp;</td>'.
                   1376:                       '<td valign="top">'.$lt{'new'}.'<br />'.
                   1377:                       '<input type="text" name="newsec" value="" size="5" />'.
1.22      raeburn  1378:                       '<input type="hidden" name="groups" value="" />'.
                   1379:                       '<input type="hidden" name="sections" value="" />'.
                   1380:                       '<input type="hidden" name="origdom" value="'.
                   1381:                       $env{'request.role.domain'}.'" />'.
                   1382:                       '<input type="hidden" name="dccourse" value="" />'.
                   1383:                       '<input type="hidden" name="dcdomain" value="" />'.
1.103     raeburn  1384:                       '<input type="hidden" name="crstype" value="" />'.
1.22      raeburn  1385:                       '</td></tr></table></td>'.
1.1       raeburn  1386:                       &Apache::loncommon::end_data_table_row().
1.22      raeburn  1387:                       &Apache::loncommon::end_data_table()."\n";
1.1       raeburn  1388:     }
                   1389:     $options .= '</select>';
                   1390:     return ($options,$cb_jscript,$coursepick);
                   1391: }
                   1392: 
                   1393: sub default_course_roles {
1.101     raeburn  1394:     my ($context,$checkpriv,$crstype,%customroles) = @_;
1.1       raeburn  1395:     my $output;
1.17      raeburn  1396:     my $custom = 1;
1.101     raeburn  1397:     my @roles = &course_roles($context,$checkpriv,$custom,lc($crstype));
1.1       raeburn  1398:     foreach my $role (@roles) {
1.22      raeburn  1399:         if ($role ne 'cr') {
1.101     raeburn  1400:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.22      raeburn  1401:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1402:         }
1.1       raeburn  1403:     }
                   1404:     if (keys(%customroles) > 0) {
1.22      raeburn  1405:         if (grep(/^cr$/,@roles)) {
                   1406:             foreach my $cust (sort(keys(%customroles))) {
                   1407:                 my $custrole='cr_'.$env{'user.domain'}.
                   1408:                              '_'.$env{'user.name'}.'_'.$cust;
                   1409:                 $output .= '  <option value="'.$custrole.'">'.$cust.'</option>';
                   1410:             }
1.1       raeburn  1411:         }
                   1412:     }
                   1413:     return $output;
                   1414: }
                   1415: 
                   1416: sub construction_space_roles {
1.2       raeburn  1417:     my ($checkpriv) = @_;
1.17      raeburn  1418:     my @allroles = &roles_by_context('author');
1.1       raeburn  1419:     my @roles;
1.2       raeburn  1420:     if ($checkpriv) {
                   1421:         foreach my $role (@allroles) {
                   1422:             if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) { 
                   1423:                 push(@roles,$role); 
                   1424:             }
1.1       raeburn  1425:         }
1.2       raeburn  1426:         return @roles;
                   1427:     } else {
                   1428:         return @allroles;
1.1       raeburn  1429:     }
                   1430: }
                   1431: 
                   1432: sub domain_roles {
1.2       raeburn  1433:     my ($checkpriv) = @_;
1.17      raeburn  1434:     my @allroles = &roles_by_context('domain');
1.1       raeburn  1435:     my @roles;
1.2       raeburn  1436:     if ($checkpriv) {
                   1437:         foreach my $role (@allroles) {
                   1438:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   1439:                 push(@roles,$role);
                   1440:             }
1.1       raeburn  1441:         }
1.2       raeburn  1442:         return @roles;
                   1443:     } else {
                   1444:         return @allroles;
1.1       raeburn  1445:     }
                   1446: }
                   1447: 
                   1448: sub course_roles {
1.101     raeburn  1449:     my ($context,$checkpriv,$custom,$roletype) = @_;
1.102     raeburn  1450:     my $crstype;
                   1451:     if ($roletype eq 'community') {
                   1452:         $crstype = 'Community' ;
                   1453:     } else {
                   1454:         $crstype = 'Course';
                   1455:     }
                   1456:     my @allroles = &roles_by_context('course',$custom,$crstype);
1.1       raeburn  1457:     my @roles;
                   1458:     if ($context eq 'domain') {
                   1459:         @roles = @allroles;
                   1460:     } elsif ($context eq 'course') {
                   1461:         if ($env{'request.course.id'}) {
1.2       raeburn  1462:             if ($checkpriv) { 
                   1463:                 foreach my $role (@allroles) {
                   1464:                     if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   1465:                         push(@roles,$role);
                   1466:                     } else {
1.101     raeburn  1467:                         if ((($role ne 'cc') && ($role ne 'co')) && ($env{'request.course.sec'} ne '')) {
1.22      raeburn  1468:                             if (&Apache::lonnet::allowed('c'.$role,
1.2       raeburn  1469:                                              $env{'request.course.id'}.'/'.
1.22      raeburn  1470:                                              $env{'request.course.sec'})) {
1.2       raeburn  1471:                                 push(@roles,$role);
                   1472:                             }
1.1       raeburn  1473:                         }
                   1474:                     }
                   1475:                 }
1.2       raeburn  1476:             } else {
                   1477:                 @roles = @allroles;
1.1       raeburn  1478:             }
                   1479:         }
                   1480:     }
                   1481:     return @roles;
                   1482: }
                   1483: 
                   1484: sub curr_role_permissions {
1.101     raeburn  1485:     my ($context,$setting,$checkpriv,$type) = @_; 
1.17      raeburn  1486:     my $custom = 1;
1.1       raeburn  1487:     my @roles;
1.13      raeburn  1488:     if ($context eq 'author') {
1.2       raeburn  1489:         @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1490:     } elsif ($context eq 'domain') {
                   1491:         if ($setting eq 'course') {
1.101     raeburn  1492:             @roles = &course_roles($context,$checkpriv,$custom,$type); 
1.1       raeburn  1493:         } else {
1.2       raeburn  1494:             @roles = &domain_roles($checkpriv);
1.1       raeburn  1495:         }
                   1496:     } elsif ($context eq 'course') {
1.101     raeburn  1497:         @roles = &course_roles($context,$checkpriv,$custom,$type);
1.1       raeburn  1498:     }
                   1499:     return @roles;
                   1500: }
                   1501: 
                   1502: # ======================================================= Existing Custom Roles
                   1503: 
                   1504: sub my_custom_roles {
1.104     raeburn  1505:     my ($crstype) = @_;
1.1       raeburn  1506:     my %returnhash=();
                   1507:     my %rolehash=&Apache::lonnet::dump('roles');
1.104     raeburn  1508:     foreach my $key (keys(%rolehash)) {
1.1       raeburn  1509:         if ($key=~/^rolesdef\_(\w+)$/) {
1.104     raeburn  1510:             if ($crstype eq 'Community') {
                   1511:                 next if ($rolehash{$key} =~ /bre\&S/); 
                   1512:             }
1.1       raeburn  1513:             $returnhash{$1}=$1;
                   1514:         }
                   1515:     }
                   1516:     return %returnhash;
                   1517: }
                   1518: 
1.2       raeburn  1519: sub print_userlist {
                   1520:     my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
                   1521:         $idlist,$idlist_titles) = @_;
                   1522:     my $format = $env{'form.output'};
1.1       raeburn  1523:     if (! exists($env{'form.sortby'})) {
                   1524:         $env{'form.sortby'} = 'username';
                   1525:     }
1.2       raeburn  1526:     if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
                   1527:         $env{'form.Status'} = 'Active';
1.1       raeburn  1528:     }
                   1529:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2       raeburn  1530:         ($env{'form.Status'});
1.1       raeburn  1531: 
1.2       raeburn  1532:     if ($env{'form.showrole'} eq '') {
1.13      raeburn  1533:         if ($context eq 'course') {
                   1534:             $env{'form.showrole'} = 'st';
                   1535:         } else {
                   1536:             $env{'form.showrole'} = 'Any';            
                   1537:         }
1.2       raeburn  1538:     }
1.1       raeburn  1539:     if (! defined($env{'form.output'}) ||
                   1540:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
                   1541:         $env{'form.output'} = 'html';
                   1542:     }
                   1543: 
1.2       raeburn  1544:     my @statuses;
                   1545:     if ($env{'form.Status'} eq 'Any') {
                   1546:         @statuses = ('previous','active','future');
                   1547:     } elsif ($env{'form.Status'} eq 'Expired') {
                   1548:         @statuses = ('previous');
                   1549:     } elsif ($env{'form.Status'} eq 'Active') {
                   1550:         @statuses = ('active');
                   1551:     } elsif ($env{'form.Status'} eq 'Future') {
                   1552:         @statuses = ('future');
                   1553:     }
1.1       raeburn  1554: 
1.2       raeburn  1555: #    if ($context eq 'course') { 
                   1556: #        $r->print(&display_adv_courseroles());
                   1557: #    }
1.1       raeburn  1558:     #
                   1559:     # Interface output
1.2       raeburn  1560:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   1561:               '<input type="hidden" name="action" value="'.
1.1       raeburn  1562:               $env{'form.action'}.'" />');
                   1563:     $r->print("<p>\n");
                   1564:     if ($env{'form.action'} ne 'modifystudent') {
                   1565:         my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
                   1566:                                            'excel' => "Excel",
                   1567:                                            'html'  => 'HTML');
                   1568:         my $output_selector = '<select size="1" name="output" >';
                   1569:         foreach my $outputformat ('html','csv','excel') {
1.96      bisitz   1570:             my $option = '<option value="'.$outputformat.'"';
1.1       raeburn  1571:             if ($outputformat eq $env{'form.output'}) {
1.96      bisitz   1572:                 $option .= ' selected="selected"';
1.1       raeburn  1573:             }
                   1574:             $option .='>'.$lt{$outputformat}.'</option>';
                   1575:             $output_selector .= "\n".$option;
                   1576:         }
                   1577:         $output_selector .= '</select>';
1.70      bisitz   1578:         $r->print('<label><span class="LC_nobreak">'
                   1579:                  .&mt('Output Format: [_1]',$output_selector)
                   1580:                  .'</span></label>'.('&nbsp;'x3));
                   1581:     }
                   1582:     $r->print('<label><span class="LC_nobreak">'
                   1583:              .&mt('User Status: [_1]',$status_select)
                   1584:              .'</span></label>'.('&nbsp;'x3)."\n");
1.2       raeburn  1585:     my $roleselected = '';
                   1586:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1587:        $roleselected = ' selected="selected"'; 
1.2       raeburn  1588:     }
1.53      raeburn  1589:     my ($cnum,$cdom);
                   1590:     $r->print(&role_filter($context));
                   1591:     if ($context eq 'course') {
                   1592:         ($cnum,$cdom) = &get_course_identity();
                   1593:         $r->print(&section_group_filter($cnum,$cdom));
1.2       raeburn  1594:     }
1.78      raeburn  1595:     if ($env{'form.phase'} eq '') {
                   1596:         $r->print('<br /><br />'.&list_submit_button(&mt('Display List of Users')).
                   1597:                   "\n</p>\n".
                   1598:                   '<input type="hidden" name="phase" value="" /></form>');
                   1599:         return;
                   1600:     }
1.106     raeburn  1601:     if (!(($context eq 'domain') && 
                   1602:           (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
1.25      raeburn  1603:         $r->print('&nbsp;'.&list_submit_button(&mt('Update Display')).
                   1604:                   "\n</p>\n");
1.2       raeburn  1605:     }
                   1606:     my ($indexhash,$keylist) = &make_keylist_array();
1.106     raeburn  1607:     my (%userlist,%userinfo,$clearcoursepick);
1.102     raeburn  1608:     if (($context eq 'domain') && 
                   1609:         ($env{'form.roletype'} eq 'course') || 
                   1610:         ($env{'form.roletype'} eq 'community')) {
                   1611:         my ($crstype,$numcodes,$title,$warning);
                   1612:         if ($env{'form.roletype'} eq 'course') {
                   1613:             $crstype = 'Course';
                   1614:             $numcodes = $totcodes;
                   1615:             $title = &mt('Select Courses');
                   1616:             $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
                   1617:         } elsif ($env{'form.roletype'} eq 'community') {
                   1618:             $crstype = 'Community';
                   1619:             $numcodes = 0;
                   1620:             $title = &mt('Select Communities');
                   1621:             $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
                   1622:         }
1.3       raeburn  1623:         my $courseform =
1.102     raeburn  1624:             &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
                   1625:                             $codetitles,$idlist,$idlist_titles,$crstype);
1.3       raeburn  1626:         $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
                   1627:                   &Apache::lonhtmlcommon::start_pick_box()."\n".
1.102     raeburn  1628:                   &Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
1.3       raeburn  1629:                   $courseform."\n".
                   1630:                   &Apache::lonhtmlcommon::row_closure(1).
                   1631:                   &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1.106     raeburn  1632:                   '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
                   1633:                   &list_submit_button(&mt('Update Display')).
1.102     raeburn  1634:                   "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
1.106     raeburn  1635:         $clearcoursepick = 0;
                   1636:         if (($env{'form.origroletype'} ne '') &&
                   1637:             ($env{'form.origroletype'} ne $env{'form.roletype'})) {
                   1638:             $clearcoursepick = 1;
                   1639:         }
                   1640:         if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.11      raeburn  1641:             $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
                   1642:         }
                   1643:     } else {
                   1644:         $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
1.3       raeburn  1645:     }
                   1646:     $r->rflush();
1.1       raeburn  1647:     if ($context eq 'course') {
1.46      raeburn  1648:         if (($env{'form.showrole'} eq 'st') || ($env{'form.showrole'} eq 'Any')) { 
1.45      raeburn  1649:             my $classlist = &Apache::loncoursedata::get_classlist();
1.66      raeburn  1650:             if (ref($classlist) eq 'HASH') {
                   1651:                 %userlist = %{$classlist};
                   1652:             }
1.45      raeburn  1653:         }
1.43      raeburn  1654:         if ($env{'form.showrole'} ne 'st') {
                   1655:             my $showroles;
                   1656:             if ($env{'form.showrole'} ne 'Any') {
                   1657:                 $showroles = [$env{'form.showrole'}];
1.3       raeburn  1658:             } else {
1.43      raeburn  1659:                 $showroles = undef;
1.1       raeburn  1660:             }
1.43      raeburn  1661:             my $withsec = 1;
                   1662:             my $hidepriv = 1;
                   1663:             my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   1664:                               \@statuses,$showroles,undef,$withsec,$hidepriv);
                   1665:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                   1666:                              \%advrolehash,$permission);
1.1       raeburn  1667:         }
1.2       raeburn  1668:     } else {
                   1669:         my (%cstr_roles,%dom_roles);
1.13      raeburn  1670:         if ($context eq 'author') {
1.2       raeburn  1671:             # List co-authors and assistant co-authors
1.17      raeburn  1672:             my @possroles = &roles_by_context($context);
1.2       raeburn  1673:             %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   1674:                                               \@statuses,\@possroles);
                   1675:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11      raeburn  1676:                              \%cstr_roles,$permission);
1.2       raeburn  1677:         } elsif ($context eq 'domain') {
                   1678:             if ($env{'form.roletype'} eq 'domain') {
                   1679:                 %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
                   1680:                 foreach my $key (keys(%dom_roles)) {
                   1681:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1682:                         &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1683:                                          \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1684:                     }
                   1685:                 }
1.13      raeburn  1686:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1687:                 my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
                   1688:                 my %coauthors;
                   1689:                 foreach my $key (keys(%dom_roles)) {
                   1690:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1691:                         if ($env{'form.showrole'} eq 'au') {
                   1692:                             &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1693:                                              \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1694:                         } else {
                   1695:                             my @possroles;
                   1696:                             if ($env{'form.showrole'} eq 'Any') {
1.22      raeburn  1697:                                 @possroles = &roles_by_context('author');
1.2       raeburn  1698:                             } else {
                   1699:                                 @possroles = ($env{'form.showrole'}); 
                   1700:                             }
                   1701:                             foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22      raeburn  1702:                                 my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2       raeburn  1703:                                 my $extent = '/'.$authordom.'/'.$authorname;
                   1704:                                 %{$coauthors{$extent}} =
                   1705:                                     &Apache::lonnet::get_my_roles($authorname,
                   1706:                                        $authordom,undef,\@statuses,\@possroles);
                   1707:                             }
                   1708:                             &gather_userinfo($context,$format,\%userlist,
1.11      raeburn  1709:                                      $indexhash,\%userinfo,\%coauthors,$permission);
1.2       raeburn  1710:                         }
                   1711:                     }
                   1712:                 }
1.101     raeburn  1713:             } elsif (($env{'form.roletype'} eq 'course') ||
                   1714:                      ($env{'form.roletype'} eq 'community')) {
1.106     raeburn  1715:                 if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.2       raeburn  1716:                     my %courses = &process_coursepick();
1.39      raeburn  1717:                     my %allusers;
                   1718:                     my $hidepriv = 1;
1.2       raeburn  1719:                     foreach my $cid (keys(%courses)) {
1.17      raeburn  1720:                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11      raeburn  1721:                         next if ($cnum eq '' || $cdom eq '');
1.17      raeburn  1722:                         my $custom = 1;
1.2       raeburn  1723:                         my (@roles,@sections,%access,%users,%userdata,
1.6       albertel 1724:                             %statushash);
1.2       raeburn  1725:                         if ($env{'form.showrole'} eq 'Any') {
1.101     raeburn  1726:                             @roles = &course_roles($context,undef,$custom,
                   1727:                                                    $env{'form.roletype'});
1.2       raeburn  1728:                         } else {
                   1729:                             @roles = ($env{'form.showrole'});
                   1730:                         }
                   1731:                         foreach my $role (@roles) {
                   1732:                             %{$users{$role}} = ();
                   1733:                         }
                   1734:                         foreach my $type (@statuses) {
                   1735:                             $access{$type} = $type;
                   1736:                         }
1.39      raeburn  1737:                         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
1.2       raeburn  1738:                         foreach my $user (keys(%userdata)) {
                   1739:                             next if (ref($userinfo{$user}) eq 'HASH');
                   1740:                             foreach my $item ('fullname','id') {
                   1741:                                 $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
                   1742:                             }
                   1743:                         }
                   1744:                         foreach my $role (keys(%users)) {
                   1745:                             foreach my $user (keys(%{$users{$role}})) {
                   1746:                                 my $uniqid = $user.':'.$role;
                   1747:                                 $allusers{$uniqid}{$cid} = { desc => $cdesc,
                   1748:                                                              secs  => $statushash{$user}{$role},
                   1749:                                                            };
                   1750:                             }
                   1751:                         }
                   1752:                     }
                   1753:                     &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1754:                                      \%userinfo,\%allusers,$permission);
1.2       raeburn  1755:                 } else {
1.10      raeburn  1756:                     $r->print('<input type="hidden" name="phase" value="'.
                   1757:                               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1758:                     return;
                   1759:                 }
1.1       raeburn  1760:             }
                   1761:         }
1.3       raeburn  1762:     }
                   1763:     if (keys(%userlist) == 0) {
1.13      raeburn  1764:         if ($context eq 'author') {
1.3       raeburn  1765:             $r->print(&mt('There are no co-authors to display.')."\n");
                   1766:         } elsif ($context eq 'domain') {
                   1767:             if ($env{'form.roletype'} eq 'domain') {
                   1768:                 $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13      raeburn  1769:             } elsif ($env{'form.roletype'} eq 'author') {
1.3       raeburn  1770:                 $r->print(&mt('There are no authors or co-authors to display.')."\n");
                   1771:             } elsif ($env{'form.roletype'} eq 'course') {
                   1772:                 $r->print(&mt('There are no course users to display')."\n"); 
1.101     raeburn  1773:             } elsif ($env{'form.roletype'} eq 'community') {
                   1774:                 $r->print(&mt('There are no community users to display')."\n");
1.2       raeburn  1775:             }
1.3       raeburn  1776:         } elsif ($context eq 'course') {
                   1777:             $r->print(&mt('There are no course users to display.')."\n");
                   1778:         }
                   1779:     } else {
                   1780:         # Print out the available choices
1.4       raeburn  1781:         my $usercount;
1.3       raeburn  1782:         if ($env{'form.action'} eq 'modifystudent') {
1.10      raeburn  1783:             ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4       raeburn  1784:                                  $env{'form.Status'},\%userlist,$keylist);
1.1       raeburn  1785:         } else {
1.4       raeburn  1786:             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10      raeburn  1787:                                $permission,$env{'form.Status'},\%userlist,$keylist);
1.4       raeburn  1788:         }
                   1789:         if (!$usercount) {
1.72      bisitz   1790:             $r->print('<br /><span class="LC_warning">'
                   1791:                      .&mt('There are no users matching the search criteria.')
                   1792:                      .'</span>'
                   1793:             ); 
1.2       raeburn  1794:         }
                   1795:     }
1.10      raeburn  1796:     $r->print('<input type="hidden" name="phase" value="'.
                   1797:               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1798: }
                   1799: 
1.53      raeburn  1800: sub role_filter {
                   1801:     my ($context) = @_;
                   1802:     my $output;
                   1803:     my $roleselected = '';
                   1804:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1805:        $roleselected = ' selected="selected"';
1.53      raeburn  1806:     }
                   1807:     my ($role_select);
                   1808:     if ($context eq 'domain') {
                   1809:         $role_select = &domain_roles_select();
1.70      bisitz   1810:         $output = '<label><span class="LC_nobreak">'
                   1811:                  .&mt('Role Type: [_1]',$role_select)
                   1812:                  .'</span></label>';
1.53      raeburn  1813:     } else {
                   1814:         $role_select = '<select name="showrole">'."\n".
                   1815:                        '<option value="Any" '.$roleselected.'>'.
                   1816:                        &mt('Any role').'</option>';
1.101     raeburn  1817:         my ($roletype,$crstype);
                   1818:         if ($context eq 'course') {
                   1819:             $crstype = &Apache::loncommon::course_type();
                   1820:             if ($crstype eq 'Community') {
                   1821:                 $roletype = 'community';
                   1822:             } else {
                   1823:                 $roletype = 'course';
                   1824:             } 
                   1825:         }
                   1826:         my @poss_roles = &curr_role_permissions($context,'','',$roletype);
1.53      raeburn  1827:         foreach my $role (@poss_roles) {
                   1828:             $roleselected = '';
                   1829:             if ($role eq $env{'form.showrole'}) {
1.91      bisitz   1830:                 $roleselected = ' selected="selected"';
1.53      raeburn  1831:             }
                   1832:             my $plrole;
                   1833:             if ($role eq 'cr') {
                   1834:                 $plrole = &mt('Custom role');
                   1835:             } else {
1.101     raeburn  1836:                 $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.53      raeburn  1837:             }
                   1838:             $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
                   1839:         }
                   1840:         $role_select .= '</select>';
1.70      bisitz   1841:         $output = '<label><span class="LC_nobreak">'
                   1842:                  .&mt('Role: [_1]',$role_select)
                   1843:                  .'</span></label>';
1.53      raeburn  1844:     }
                   1845:     return $output;
                   1846: }
                   1847: 
1.33      raeburn  1848: sub section_group_filter {
                   1849:     my ($cnum,$cdom) = @_;
                   1850:     my @filters;
                   1851:     if ($env{'request.course.sec'} eq '') {
                   1852:         @filters = ('sec');
                   1853:     }
                   1854:     push(@filters,'grp');
                   1855:     my %name = (
                   1856:                  sec => 'secfilter',
                   1857:                  grp => 'grpfilter',
                   1858:                );
                   1859:     my %title = &Apache::lonlocal::texthash (
                   1860:                                               sec  => 'Section(s)',
                   1861:                                               grp  => 'Group(s)',
                   1862:                                               all  => 'all',
                   1863:                                               none => 'none',
                   1864:                                             );
1.47      raeburn  1865:     my $output;
1.33      raeburn  1866:     foreach my $item (@filters) {
1.47      raeburn  1867:         my ($markup,@options); 
1.33      raeburn  1868:         if ($env{'form.'.$name{$item}} eq '') {
                   1869:             $env{'form.'.$name{$item}} = 'all';
                   1870:         }
                   1871:         if ($item eq 'sec') {
1.103     raeburn  1872:             if (($env{'form.showrole'} eq 'cc') || ($env{'form.showrole'} eq 'co')) {
1.33      raeburn  1873:                 $env{'form.'.$name{$item}} = 'none';
                   1874:             }
                   1875:             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   1876:             @options = sort(keys(%sections_count));
                   1877:         } elsif ($item eq 'grp') {
                   1878:             my %curr_groups = &Apache::longroup::coursegroups();
                   1879:             @options = sort(keys(%curr_groups));
                   1880:         }
                   1881:         if (@options > 0) {
                   1882:             my $currsel;
                   1883:             $markup = '<select name="'.$name{$item}.'" />'."\n";
                   1884:             foreach my $option ('all','none',@options) { 
                   1885:                 $currsel = '';
                   1886:                 if ($env{'form.'.$name{$item}} eq $option) {
1.96      bisitz   1887:                     $currsel = ' selected="selected"';
1.33      raeburn  1888:                 }
                   1889:                 $markup .= ' <option value="'.$option.'"'.$currsel.'>';
                   1890:                 if (($option eq 'all') || ($option eq 'none')) {
                   1891:                     $markup .= $title{$option};
                   1892:                 } else {
                   1893:                     $markup .= $option;
                   1894:                 }   
                   1895:                 $markup .= '</option>'."\n";
                   1896:             }
                   1897:             $markup .= '</select>'."\n";
                   1898:             $output .= ('&nbsp;'x3).'<label>'.$title{$item}.': '.$markup.'</label>';
                   1899:         }
                   1900:     }
                   1901:     return $output;
                   1902: }
                   1903: 
1.2       raeburn  1904: sub list_submit_button {
                   1905:     my ($text) = @_;
1.11      raeburn  1906:     return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2       raeburn  1907: }
                   1908: 
                   1909: sub gather_userinfo {
1.11      raeburn  1910:     my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.52      raeburn  1911:     my $viewablesec;
                   1912:     if ($context eq 'course') {
                   1913:         $viewablesec = &viewable_section($permission);
                   1914:     }
1.2       raeburn  1915:     foreach my $item (keys(%{$rolehash})) {
                   1916:         my %userdata;
1.22      raeburn  1917:         if ($context eq 'author') { 
1.2       raeburn  1918:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1919:                 split(/:/,$item);
                   1920:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1921:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1922:                                $item,$userlist);
1.22      raeburn  1923:         } elsif ($context eq 'course') {
                   1924:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   1925:              $userdata{'section'}) = split(/:/,$item,-1);
                   1926:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   1927:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   1928:                 next if ($viewablesec ne $userdata{'section'});
                   1929:             }
1.24      raeburn  1930:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1931:                                $item,$userlist);
1.2       raeburn  1932:         } elsif ($context eq 'domain') {
                   1933:             if ($env{'form.roletype'} eq 'domain') {
                   1934:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   1935:                     split(/:/,$item);
                   1936:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1937:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1938:                                    $item,$userlist);
1.13      raeburn  1939:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1940:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   1941:                     $userdata{'extent'} = $item;
                   1942:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   1943:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   1944:                         ($userdata{'start'},$userdata{'end'}) = 
                   1945:                             split(/:/,$rolehash->{$item}{$key});
                   1946:                         my $uniqid = $key.':'.$item;
1.25      raeburn  1947:                         &build_user_record($context,\%userdata,$userinfo,
                   1948:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  1949:                     }
                   1950:                 }
1.102     raeburn  1951:             } elsif (($env{'form.roletype'} eq 'course') || 
                   1952:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  1953:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1954:                     split(/:/,$item);
                   1955:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  1956:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  1957:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   1958:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   1959:                             my $spanstart = '';
                   1960:                             my $spanend = '; ';
                   1961:                             my $space = ', ';
                   1962:                             if ($format eq 'html' || $format eq 'view') {
                   1963:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  1964:                                 # FIXME: actions on courses disabled for now
                   1965: #                                if ($permission->{'cusr'}) {
                   1966: #                                    if ($numcids > 1) {
1.25      raeburn  1967: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1968: #                                    } else {
1.25      raeburn  1969: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1970: #                                    }
                   1971: #                                }
1.2       raeburn  1972:                                 $spanend = '</span><br />';
                   1973:                                 $space = ',&nbsp;';
                   1974:                             }
                   1975:                             $userdata{'extent'} .= $spanstart.
                   1976:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   1977:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   1978:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  1979:                                     if (($env{'form.Status'} eq 'Any') ||
                   1980:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   1981:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   1982:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   1983:                                     }
1.2       raeburn  1984:                                 }
                   1985:                             }
                   1986:                         }
                   1987:                     }
                   1988:                 }
1.25      raeburn  1989:                 if ($userdata{'status'} ne '') {
                   1990:                     &build_user_record($context,\%userdata,$userinfo,
                   1991:                                        $indexhash,$item,$userlist);
                   1992:                 }
1.2       raeburn  1993:             }
                   1994:         }
                   1995:     }
                   1996:     return;
                   1997: }
                   1998: 
                   1999: sub build_user_record {
1.24      raeburn  2000:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  2001:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.102     raeburn  2002:     if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
                   2003:                              && ($env{'form.roletype'} eq 'community')))) {
1.24      raeburn  2004:         &process_date_info($userdata);
                   2005:     }
1.2       raeburn  2006:     my $username = $userdata->{'username'};
                   2007:     my $domain = $userdata->{'domain'};
                   2008:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  2009:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  2010:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   2011:     } else {
                   2012:         &aggregate_user_info($domain,$username,$userinfo);
                   2013:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   2014:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   2015:     }
                   2016:     foreach my $key (keys(%{$indexhash})) {
                   2017:         if (defined($userdata->{$key})) {
                   2018:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   2019:         }
                   2020:     }
                   2021:     return;
                   2022: }
                   2023: 
                   2024: sub courses_selector {
                   2025:     my ($cdom,$formname) = @_;
                   2026:     my %coursecodes = ();
                   2027:     my %codes = ();
                   2028:     my @codetitles = ();
                   2029:     my %cat_titles = ();
                   2030:     my %cat_order = ();
                   2031:     my %idlist = ();
                   2032:     my %idnums = ();
                   2033:     my %idlist_titles = ();
                   2034:     my $caller = 'global';
                   2035:     my $format_reply;
                   2036:     my $jscript = '';
                   2037: 
1.7       albertel 2038:     my $totcodes = 0;
                   2039:     $totcodes =
1.2       raeburn  2040:         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                   2041:                                                       $cdom,$totcodes);
                   2042:     if ($totcodes > 0) {
                   2043:         $format_reply =
                   2044:              &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
                   2045:                                 \%codes,\@codetitles,\%cat_titles,\%cat_order);
                   2046:         if ($format_reply eq 'ok') {
                   2047:             my $numtypes = @codetitles;
                   2048:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   2049:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   2050:             my $longtitles_str = join('","',@{$longtitles});
                   2051:             my $allidlist = $idlist{$codetitles[0]};
                   2052:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   2053:             $jscript .= $scripttext;
                   2054:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
                   2055:         }
                   2056:     }
                   2057:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   2058: 
                   2059:     my %elements = (
                   2060:                      Year => 'selectbox',
                   2061:                      coursepick => 'radio',
                   2062:                      coursetotal => 'text',
                   2063:                      courselist => 'text',
                   2064:                    );
                   2065:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   2066:     if ($env{'form.coursepick'} eq 'category') {
                   2067:         $jscript .= qq|
                   2068: function setCourseCat(formname) {
                   2069:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   2070:         return;
                   2071:     }
                   2072:     courseSet('Year');
                   2073:     for (var j=0; j<formname.Semester.length; j++) {
                   2074:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   2075:             formname.Semester.options[j].selected = true;
                   2076:         }
                   2077:     }
                   2078:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   2079:         return;
                   2080:     }
                   2081:     courseSet('Semester');
                   2082:     for (var j=0; j<formname.Department.length; j++) {
                   2083:         if (formname.Department.options[j].value == "$env{'form.Department'}") {            formname.Department.options[j].selected = true;
                   2084:         }
                   2085:     }
                   2086:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   2087:         return;
                   2088:     }
                   2089:     courseSet('Department');
                   2090:     for (var j=0; j<formname.Number.length; j++) {
                   2091:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   2092:             formname.Number.options[j].selected = true;
                   2093:         }
                   2094:     }
                   2095: }
                   2096: |;
                   2097:     }
                   2098:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   2099:             \%idlist_titles);
                   2100: }
                   2101: 
                   2102: sub course_selector_loadcode {
                   2103:     my ($formname) = @_;
                   2104:     my $loadcode;
                   2105:     if ($env{'form.coursepick'} ne '') {
                   2106:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   2107:         if ($env{'form.coursepick'} eq 'category') {
                   2108:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   2109:         }
                   2110:     }
                   2111:     return $loadcode;
                   2112: }
                   2113: 
                   2114: sub process_coursepick {
                   2115:     my $coursefilter = $env{'form.coursepick'};
                   2116:     my $cdom = $env{'request.role.domain'};
                   2117:     my %courses;
1.105     raeburn  2118:     my $crssrch = 'Course';
                   2119:     if ($env{'form.roletype'} eq 'community') {
                   2120:         $crssrch = 'Community';
                   2121:     }
1.2       raeburn  2122:     if ($coursefilter eq 'all') {
                   2123:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1.105     raeburn  2124:                                                  undef,undef,$crssrch);
1.2       raeburn  2125:     } elsif ($coursefilter eq 'category') {
                   2126:         my $instcode = &instcode_from_coursefilter();
                   2127:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1.105     raeburn  2128:                                                  undef,undef,$crssrch);
1.2       raeburn  2129:     } elsif ($coursefilter eq 'specific') {
                   2130:         if ($env{'form.coursetotal'} > 1) {
                   2131:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   2132:             foreach my $cid (@course_ids) {
                   2133:                 $courses{$cid} = '';
1.1       raeburn  2134:             }
1.2       raeburn  2135:         } else {
                   2136:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  2137:         }
1.2       raeburn  2138:     }
                   2139:     return %courses;
                   2140: }
                   2141: 
                   2142: sub instcode_from_coursefilter {
                   2143:     my $instcode = '';
                   2144:     my @cats = ('Semester','Year','Department','Number');
                   2145:     foreach my $category (@cats) {
                   2146:         if (defined($env{'form.'.$category})) {
                   2147:             unless ($env{'form.'.$category} eq '-1') {
                   2148:                 $instcode .= $env{'form.'.$category};
                   2149:            }
                   2150:         }
                   2151:     }
                   2152:     if ($instcode eq '') {
                   2153:         $instcode = '.';
                   2154:     }
                   2155:     return $instcode;
                   2156: }
                   2157: 
                   2158: sub display_adv_courseroles {
                   2159:     my $output;
                   2160:     #
                   2161:     # List course personnel
                   2162:     my %coursepersonnel = 
                   2163:        &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
                   2164:     #
                   2165:     $output = '<br />'.&Apache::loncommon::start_data_table();
                   2166:     foreach my $role (sort(keys(%coursepersonnel))) {
                   2167:         next if ($role =~ /^\s*$/);
                   2168:         $output .= &Apache::loncommon::start_data_table_row().
                   2169:                   '<td>'.$role.'</td><td>';
                   2170:         foreach my $user (split(',',$coursepersonnel{$role})) {
                   2171:             my ($puname,$pudom)=split(':',$user);
                   2172:             $output .= ' '.&Apache::loncommon::aboutmewrapper(
                   2173:                        &Apache::loncommon::plainname($puname,$pudom),
                   2174:                        $puname,$pudom);
                   2175:         }
                   2176:         $output .= '</td>'.&Apache::loncommon::end_data_table_row();
                   2177:     }
                   2178:     $output .= &Apache::loncommon::end_data_table();
                   2179: }
                   2180: 
                   2181: sub make_keylist_array {
                   2182:     my ($index,$keylist);
                   2183:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   2184:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   2185:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   2186:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   2187:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   2188:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   2189:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   2190:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2191:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2192:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   2193:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   2194:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   2195:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   2196:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1.44      raeburn  2197:     $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
1.47      raeburn  2198:     $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
1.2       raeburn  2199:     foreach my $key (keys(%{$index})) {
                   2200:         $keylist->[$index->{$key}] = $key;
                   2201:     }
                   2202:     return ($index,$keylist);
                   2203: }
                   2204: 
                   2205: sub aggregate_user_info {
                   2206:     my ($udom,$uname,$userinfo) = @_;
                   2207:     my %info=&Apache::lonnet::get('environment',
                   2208:                                   ['firstname','middlename',
                   2209:                                    'lastname','generation','id'],
                   2210:                                    $udom,$uname);
                   2211:     my ($tmp) = keys(%info);
                   2212:     my ($fullname,$id);
                   2213:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   2214:         $fullname = 'not available';
                   2215:         $id = 'not available';
                   2216:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   2217:                                  'for '.$uname.':'.$udom);
1.1       raeburn  2218:     } else {
1.2       raeburn  2219:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   2220:         $id = $info{'id'};
                   2221:     }
                   2222:     $userinfo->{$uname.':'.$udom} = { 
                   2223:                                       fullname => $fullname,
                   2224:                                       id       => $id,
                   2225:                                     };
                   2226:     return;
                   2227: }
1.1       raeburn  2228: 
1.2       raeburn  2229: sub process_date_info {
                   2230:     my ($userdata) = @_;
                   2231:     my $now = time;
1.83      raeburn  2232:     $userdata->{'status'} = 'Active';
1.2       raeburn  2233:     if ($userdata->{'start'} > 0) {
                   2234:         if ($now < $userdata->{'start'}) {
1.83      raeburn  2235:             $userdata->{'status'} = 'Future';
1.2       raeburn  2236:         }
1.1       raeburn  2237:     }
1.2       raeburn  2238:     if ($userdata->{'end'} > 0) {
                   2239:         if ($now > $userdata->{'end'}) {
1.83      raeburn  2240:             $userdata->{'status'} = 'Expired';
1.2       raeburn  2241:         }
                   2242:     }
                   2243:     return;
1.1       raeburn  2244: }
                   2245: 
                   2246: sub show_users_list {
1.55      raeburn  2247:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname)=@_;
1.109.2.3  raeburn  2248:     my $custommenu; 
1.55      raeburn  2249:     if ($formname eq '') {
                   2250:         $formname = 'studentform';
                   2251:     }
1.1       raeburn  2252:     #
                   2253:     # Variables for excel output
                   2254:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   2255:     #
                   2256:     # Variables for csv output
                   2257:     my ($CSVfile,$CSVfilename);
                   2258:     #
                   2259:     my $sortby = $env{'form.sortby'};
1.3       raeburn  2260:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  2261:     if ($context eq 'course') {
1.3       raeburn  2262:         push(@sortable,('section','groups','type'));
1.2       raeburn  2263:     } else {
1.3       raeburn  2264:         push(@sortable,'extent');
                   2265:     }
1.55      raeburn  2266:     if ($mode eq 'pickauthor') {
                   2267:         @sortable = ('username','fullname','email','status');
                   2268:     }
1.3       raeburn  2269:     if (!grep(/^\Q$sortby\E$/,@sortable)) {
                   2270:         $sortby = 'username';
1.1       raeburn  2271:     }
1.22      raeburn  2272:     my $setting = $env{'form.roletype'};
1.109.2.3  raeburn  2273:     my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers,$crstype,
                   2274:         $clickersupport,$displaygroups);
1.1       raeburn  2275:     if ($context eq 'course') {
1.22      raeburn  2276:         $cid = $env{'request.course.id'};
1.101     raeburn  2277:         $crstype = &Apache::loncommon::course_type();
1.17      raeburn  2278:         ($cnum,$cdom) = &get_course_identity($cid);
1.109.2.3  raeburn  2279:         $custommenu = &Apache::loncommon::needs_gci_custom();
                   2280:         unless ($custommenu) {
                   2281:             $clickersupport = 1;
                   2282:             $displaygroups = 1;
                   2283:         }
1.2       raeburn  2284:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   2285:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  2286:         if ($mode eq 'autoenroll') {
                   2287:             $env{'form.showrole'} = 'st';
                   2288:         } else {
                   2289:             if (! exists($env{'form.displayphotos'})) {
                   2290:                 $env{'form.displayphotos'} = 'off';
                   2291:             }
                   2292:             $displayphotos = $env{'form.displayphotos'};
                   2293:             if (! exists($env{'form.displayclickers'})) {
                   2294:                 $env{'form.displayclickers'} = 'off';
                   2295:             }
                   2296:             $displayclickers = $env{'form.displayclickers'};
                   2297:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   2298:                 $r->print('
1.1       raeburn  2299: <script type="text/javascript">
1.96      bisitz   2300: // <![CDATA[
1.1       raeburn  2301: function photowindow(photolink) {
                   2302:     var title = "Photo_Viewer";
                   2303:     var options = "scrollbars=1,resizable=1,menubar=0";
                   2304:     options += ",width=240,height=240";
                   2305:     stdeditbrowser = open(photolink,title,options,"1");
                   2306:     stdeditbrowser.focus();
                   2307: }
1.96      bisitz   2308: // ]]>
1.1       raeburn  2309: </script>
1.16      raeburn  2310:                ');
                   2311:             }
                   2312:             $r->print(<<END);
1.1       raeburn  2313: <input type="hidden" name="displayphotos" value="$displayphotos" />
                   2314: <input type="hidden" name="displayclickers" value="$displayclickers" />
                   2315: END
1.16      raeburn  2316:         }
1.102     raeburn  2317:     } elsif ($context eq 'domain') {
                   2318:         if ($setting eq 'community') {
                   2319:             $crstype = 'Community';
1.105     raeburn  2320:         } elsif ($setting eq 'course') {
1.102     raeburn  2321:             $crstype = 'Course';
                   2322:         }
1.1       raeburn  2323:     }
1.55      raeburn  2324:     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.11      raeburn  2325:         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.40      raeburn  2326:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.56      raeburn  2327:         my $verify_action_js = &bulkaction_javascript($formname);
1.1       raeburn  2328:         $r->print(<<END);
1.10      raeburn  2329: 
                   2330: <script type="text/javascript" language="Javascript">
1.96      bisitz   2331: // <![CDATA[
1.11      raeburn  2332: $check_uncheck_js
                   2333: 
1.56      raeburn  2334: $verify_action_js
1.10      raeburn  2335: 
                   2336: function username_display_launch(username,domain) {
                   2337:     var target;
1.55      raeburn  2338:     for (var i=0; i<document.$formname.usernamelink.length; i++) {
                   2339:         if (document.$formname.usernamelink[i].checked) {
                   2340:             target = document.$formname.usernamelink[i].value;
1.10      raeburn  2341:         }
                   2342:     }
                   2343:     if (target == 'modify') {
1.55      raeburn  2344:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2345:             var url = '/adm/createuser?srchterm='+username+'&srchdomain='+domain+'&phase=get_user_info&action=singleuser&srchin=dom&srchby=uname&srchtype=exact&popup=1';
                   2346:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2347:             modifywin = window.open(url,'',options,1);
                   2348:             modifywin.focus();
                   2349:             return;
                   2350:         } else {
1.55      raeburn  2351:             document.$formname.srchterm.value=username;
                   2352:             document.$formname.srchdomain.value=domain;
                   2353:             document.$formname.phase.value='get_user_info';
                   2354:             document.$formname.action.value = 'singleuser';
                   2355:             document.$formname.submit();
1.50      raeburn  2356:         }
1.10      raeburn  2357:     }
1.48      raeburn  2358:     if (target == 'aboutme') {
1.55      raeburn  2359:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2360:             var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
                   2361:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2362:             aboutmewin = window.open(url,'',options,1);
                   2363:             aboutmewin.focus();
                   2364:             return;
                   2365:         } else {
                   2366:             document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   2367:         }
1.48      raeburn  2368:     }
1.98      raeburn  2369:     if (target == 'track') {
                   2370:         if (document.$formname.userwin.checked == true) {
                   2371:             var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
                   2372:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2373:             var trackwin = window.open(url,'',options,1);
                   2374:             trackwin.focus();
                   2375:             return;
                   2376:         } else {
                   2377:             document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
                   2378:         }
                   2379:     }
1.10      raeburn  2380: }
1.96      bisitz   2381: // ]]>
1.10      raeburn  2382: </script>
1.11      raeburn  2383: $date_sec_selector
1.1       raeburn  2384: <input type="hidden" name="state" value="$env{'form.state'}" />
                   2385: END
                   2386:     }
                   2387:     $r->print(<<END);
                   2388: <input type="hidden" name="sortby" value="$sortby" />
                   2389: END
                   2390: 
                   2391:     my %lt=&Apache::lonlocal::texthash(
                   2392:                        'username'   => "username",
                   2393:                        'domain'     => "domain",
                   2394:                        'id'         => 'ID',
                   2395:                        'fullname'   => "name",
                   2396:                        'section'    => "section",
                   2397:                        'groups'     => "active groups",
                   2398:                        'start'      => "start date",
                   2399:                        'end'        => "end date",
                   2400:                        'status'     => "status",
1.2       raeburn  2401:                        'role'       => "role",
1.1       raeburn  2402:                        'type'       => "enroll type/action",
1.79      schafran 2403:                        'email'      => "e-mail address",
1.109.2.3  raeburn  2404:                        'lastlogin'  => "last login",
                   2405:                        'submissions' => "test status",
1.1       raeburn  2406:                        'photo'      => "photo",
1.2       raeburn  2407:                        'extent'     => "extent",
1.11      raeburn  2408:                        'pr'         => "Proceed",
                   2409:                        'ca'         => "check all",
                   2410:                        'ua'         => "uncheck all",
                   2411:                        'ac'         => "Action to take for selected users",
1.56      raeburn  2412:                        'link'       => "Behavior of clickable username link for each user",
1.82      weissno  2413:                        'aboutme'    => "Display a user's personal information page",
1.50      raeburn  2414:                        'owin'       => "Open in a new window",
1.10      raeburn  2415:                        'modify'     => "Modify a user's information",
1.98      raeburn  2416:                        'track'      => "View a user's recent activity",
1.67      droeschl 2417:                        'clicker'    => "Clicker-ID",
1.1       raeburn  2418:                       );
1.2       raeburn  2419:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   2420:         $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.102     raeburn  2421:     } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
                   2422:         $lt{'extent'} = &mt('Communities: description, section(s), status');
1.13      raeburn  2423:     } elsif ($context eq 'author') {
1.2       raeburn  2424:         $lt{'extent'} = &mt('Author'); 
                   2425:     }
1.55      raeburn  2426:     my @cols;
                   2427:     if ($mode eq 'pickauthor') {
                   2428:         @cols = ('username','fullname','status','email');
                   2429:     } else {
                   2430:         @cols = ('username','domain','id','fullname');
                   2431:         if ($context eq 'course') {
                   2432:             push(@cols,'section');
                   2433:         }
1.102     raeburn  2434:         if (!($context eq 'domain' && ($env{'form.roletype'} eq 'course')
                   2435:                               && ($env{'form.roletype'} eq 'community'))) { 
1.55      raeburn  2436:             push(@cols,('start','end'));
                   2437:         }
                   2438:         if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
                   2439:             push(@cols,'role');
                   2440:         }
                   2441:         if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.102     raeburn  2442:                                     $env{'form.roletype'} eq 'course' ||
                   2443:                                     $env{'form.roletype'} eq 'community')) {
1.55      raeburn  2444:             push (@cols,'extent');
                   2445:         }
                   2446:         if (($statusmode eq 'Any') && 
1.102     raeburn  2447:             (!($context eq 'domain' && (($env{'form.roletype'} eq 'course')
                   2448:              || ($env{'form.roletype'} eq 'community'))))) {
1.55      raeburn  2449:             push(@cols,'status');
                   2450:         }
                   2451:         if ($context eq 'course') {
1.109.2.3  raeburn  2452:             if ($displaygroups) {
                   2453:                 push(@cols,'groups');
                   2454:             }
1.55      raeburn  2455:         }
                   2456:         push(@cols,'email');
1.109.2.3  raeburn  2457:         if ($context eq 'course') {
                   2458:             if ($custommenu) {
                   2459:                 push(@cols,'lastlogin');
                   2460:                 if (($env{'form.showrole'} eq 'Any') || ($env{'form.showrole'} eq 'st')) {
                   2461:                     push(@cols,'submissions');
                   2462:                 }
                   2463:             }
                   2464:         }
1.2       raeburn  2465:     }
1.1       raeburn  2466: 
1.4       raeburn  2467:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2468:     if ($env{'form.showrole'} eq 'cr') {
                   2469:         $rolefilter = &mt('custom');  
                   2470:     } elsif ($env{'form.showrole'} ne 'Any') {
1.101     raeburn  2471:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
1.2       raeburn  2472:     }
1.16      raeburn  2473:     my $results_description;
                   2474:     if ($mode ne 'autoenroll') {
                   2475:         $results_description = &results_header_row($rolefilter,$statusmode,
1.102     raeburn  2476:                                                    $context,$permission,$mode,$crstype);
1.56      raeburn  2477:         $r->print('<b>'.$results_description.'</b><br /><br />');
1.16      raeburn  2478:     }
1.26      raeburn  2479:     my ($output,$actionselect,%canchange,%canchangesec);
1.55      raeburn  2480:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
                   2481:         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.16      raeburn  2482:             if ($permission->{'cusr'}) {
1.105     raeburn  2483:                 unless (($context eq 'domain') && 
                   2484:                         (($setting eq 'course') || ($setting eq 'community'))) {
                   2485:                     $actionselect = 
                   2486:                         &select_actions($context,$setting,$statusmode,$formname);
                   2487:                 }
1.16      raeburn  2488:             }
                   2489:             $r->print(<<END);
1.10      raeburn  2490: <input type="hidden" name="srchby"  value="uname" />
                   2491: <input type="hidden" name="srchin"   value="dom" />
                   2492: <input type="hidden" name="srchtype" value="exact" />
                   2493: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2494: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2495: END
1.16      raeburn  2496:             if ($actionselect) {
1.41      raeburn  2497:                 $output .= <<"END";
1.94      bisitz   2498: <div class="LC_left_float"><fieldset><legend>$lt{'ac'}</legend>
1.56      raeburn  2499: $actionselect
                   2500: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" /> &nbsp;
                   2501: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.$formname.actionlist)" /><br /><input type="button" value="$lt{'pr'}" onclick="javascript:verify_action('actionlist')" /></fieldset></div>
1.11      raeburn  2502: END
1.26      raeburn  2503:                 my @allroles;
                   2504:                 if ($env{'form.showrole'} eq 'Any') {
                   2505:                     my $custom = 1;
                   2506:                     if ($context eq 'domain') {
1.101     raeburn  2507:                         @allroles = &roles_by_context($setting,$custom,$crstype);
1.26      raeburn  2508:                     } else {
1.101     raeburn  2509:                         @allroles = &roles_by_context($context,$custom,$crstype);
1.26      raeburn  2510:                     }
                   2511:                 } else {
                   2512:                     @allroles = ($env{'form.showrole'});
                   2513:                 }
                   2514:                 foreach my $role (@allroles) {
                   2515:                     if ($context eq 'domain') {
                   2516:                         if ($setting eq 'domain') {
                   2517:                             if (&Apache::lonnet::allowed('c'.$role,
                   2518:                                     $env{'request.role.domain'})) {
                   2519:                                 $canchange{$role} = 1;
                   2520:                             }
1.31      raeburn  2521:                         } elsif ($setting eq 'author') {
                   2522:                             if (&Apache::lonnet::allowed('c'.$role,
                   2523:                                     $env{'request.role.domain'})) {
                   2524:                                 $canchange{$role} = 1;
                   2525:                             }
1.26      raeburn  2526:                         }
                   2527:                     } elsif ($context eq 'author') {
                   2528:                         if (&Apache::lonnet::allowed('c'.$role,
                   2529:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2530:                             $canchange{$role} = 1;
                   2531:                         }
                   2532:                     } elsif ($context eq 'course') {
                   2533:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2534:                             $canchange{$role} = 1;
                   2535:                         } elsif ($env{'request.course.sec'} ne '') {
                   2536:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2537:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2538:                             }
                   2539:                         }
                   2540:                     }
                   2541:                 }
1.16      raeburn  2542:             }
1.94      bisitz   2543:             $output .= '<div class="LC_left_float"><fieldset><legend>'.$lt{'link'}.'</legend>'.
1.56      raeburn  2544:                        '<table><tr>';
                   2545:             my @linkdests = ('aboutme');
                   2546:             if ($permission->{'cusr'}) {
                   2547:                 unshift (@linkdests,'modify');
                   2548:             }
1.98      raeburn  2549:             if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
                   2550:                 &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
                   2551:                                          $env{'request.course.sec'})) {
                   2552:                 push(@linkdests,'track');
                   2553:             }
                   2554: 
1.56      raeburn  2555:             $output .= '<td>';
                   2556:             my $usernamelink = $env{'form.usernamelink'};
                   2557:             if ($usernamelink eq '') {
                   2558:                 $usernamelink = 'aboutme';
                   2559:             }
                   2560:             foreach my $item (@linkdests) {
                   2561:                 my $checkedstr = '';
                   2562:                 if ($item eq $usernamelink) {
1.86      bisitz   2563:                     $checkedstr = ' checked="checked"';
1.56      raeburn  2564:                 }
1.86      bisitz   2565:                 $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' />&nbsp;'.$lt{$item}.'</label></span><br />';
1.56      raeburn  2566:             }
                   2567:             my $checkwin;
                   2568:             if ($env{'form.userwin'}) {
1.86      bisitz   2569:                 $checkwin = ' checked="checked"';
1.56      raeburn  2570:             }
1.86      bisitz   2571:             $output .= '</td><td valign="top"><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
1.4       raeburn  2572:         }
1.56      raeburn  2573:         $output .= "\n".'<div class="LC_clear_float_footer">&nbsp;</div>'."\n".
1.1       raeburn  2574:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2575:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2576:         if ($mode eq 'autoenroll') {
1.4       raeburn  2577:             $output .= "
1.55      raeburn  2578:  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2579:             ";
1.1       raeburn  2580:         } else {
1.105     raeburn  2581:             $output .= "\n".'<th>&nbsp;</th>'."\n";
1.11      raeburn  2582:             if ($actionselect) {
                   2583:                 $output .= '<th>'.&mt('Select').'</th>'."\n";
                   2584:             }
1.1       raeburn  2585:         }
                   2586:         foreach my $item (@cols) {
1.109.2.3  raeburn  2587:             if (grep(/^\Q$item\E$/,@sortable)) {
                   2588:                 $output .= "<th><a href=\"javascript:document.$formname.sortby.value='$item';document.$formname.submit();\">$lt{$item}</a></th>\n";
                   2589:             } else {
                   2590:                 $output .= "<th>$lt{$item}</th>\n";
                   2591:             }
1.1       raeburn  2592:         }
1.2       raeburn  2593:         my %role_types = &role_type_names();
1.16      raeburn  2594:         if ($context eq 'course' && $mode ne 'autoenroll') {
1.4       raeburn  2595:             if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2596:                 # Clicker display on or off?
1.109.2.3  raeburn  2597:                 if ($clickersupport) {
                   2598:                     my %clicker_options = (
                   2599:                                             'on' => 'Show',
                   2600:                                             'off' => 'Hide',
                   2601:                                           );
                   2602:                     my $clickerchg = 'on';
                   2603:                     if ($displayclickers eq 'on') {
                   2604:                         $clickerchg = 'off';
                   2605:                     }
                   2606:                     $output .= '    <th>'."\n".'     '
                   2607:                        .&mt('[_1]'.$clicker_options{$clickerchg}.'[_2] clicker id'
1.58      bisitz   2608:                             ,'<a href="javascript:document.'.$formname.'.displayclickers.value='
                   2609:                              ."'".$clickerchg."'".';document.'.$formname.'.submit();">'
                   2610:                             ,'</a>')
                   2611:                         ."\n".'    </th>'."\n";
1.109.2.3  raeburn  2612:                 }
1.4       raeburn  2613:                 # Photo display on or off?
                   2614:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2615:                     my %photo_options = &Apache::lonlocal::texthash(
                   2616:                                                             'on' => 'Show',
                   2617:                                                             'off' => 'Hide',
                   2618:                                                                 );
                   2619:                     my $photochg = 'on';
                   2620:                     if ($displayphotos eq 'on') {
                   2621:                         $photochg = 'off';
                   2622:                     }
                   2623:                     $output .= '    <th>'."\n".'     '.
1.55      raeburn  2624:                 '<a href="javascript:document.'.$formname.'.displayphotos.value='.
                   2625:                       "'".$photochg."'".';document.'.$formname.'.submit();">'.
1.1       raeburn  2626:                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
1.4       raeburn  2627:                       '    </th>'."\n";
                   2628:                 }
1.1       raeburn  2629:             }
1.4       raeburn  2630:         }
1.16      raeburn  2631:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2632: # Done with the HTML header line
                   2633:     } elsif ($mode eq 'csv') {
                   2634:         #
                   2635:         # Open a file
                   2636:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2637:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2638:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2639:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2640:             $r->log_error("Couldn't open $CSVfilename for output $!");
1.108     bisitz   2641:             $r->print(
                   2642:                 '<p class="LC_error">'
                   2643:                .&mt('Problems occurred in writing the CSV file.')
                   2644:                .' '.&mt('This error has been logged.')
                   2645:                .' '.&mt('Please alert your LON-CAPA administrator.')
                   2646:                .'</p>'
                   2647:             );
1.1       raeburn  2648:             $CSVfile = undef;
                   2649:         }
                   2650:         #
1.109.2.3  raeburn  2651:         if ($clickersupport) {
                   2652:             push @cols,'clicker';
                   2653:         }
1.1       raeburn  2654:         # Write headers and data to file
1.2       raeburn  2655:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2656:         print $CSVfile '"'.join('","',map {
                   2657:             &Apache::loncommon::csv_translate($lt{$_})
1.67      droeschl 2658:             } (@cols))."\"\n";
1.1       raeburn  2659:     } elsif ($mode eq 'excel') {
1.109.2.3  raeburn  2660:         if ($clickersupport) {
                   2661:             push @cols,'clicker';
                   2662:         }
1.1       raeburn  2663:         # Create the excel spreadsheet
                   2664:         ($excel_workbook,$excel_filename,$format) =
                   2665:             &Apache::loncommon::create_workbook($r);
                   2666:         return if (! defined($excel_workbook));
                   2667:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2668:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2669:         #
                   2670:         my @colnames = map {$lt{$_}} (@cols);
1.67      droeschl 2671: 
1.1       raeburn  2672:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2673:     }
                   2674: 
                   2675: # Done with header lines in all formats
                   2676:     my %index;
                   2677:     my $i;
1.2       raeburn  2678:     foreach my $idx (@$keylist) {
                   2679:         $index{$idx} = $i++;
                   2680:     }
1.4       raeburn  2681:     my $usercount = 0;
1.33      raeburn  2682:     my ($secfilter,$grpfilter);
                   2683:     if ($context eq 'course') {
                   2684:         $secfilter = $env{'form.secfilter'};
                   2685:         $grpfilter = $env{'form.grpfilter'};
                   2686:         if ($secfilter eq '') {
                   2687:             $secfilter = 'all';
                   2688:         }
                   2689:         if ($grpfilter eq '') {
                   2690:             $grpfilter = 'all';
                   2691:         }
                   2692:     }
1.83      raeburn  2693:     my %ltstatus = &Apache::lonlocal::texthash(
                   2694:                                                 Active  => 'Active',
                   2695:                                                 Future  => 'Future',
                   2696:                                                 Expired => 'Expired',
                   2697:                                                );
1.109.2.3  raeburn  2698:     # If this is for a single course get last course "log-in" and submissions.
                   2699:     my (%crslogins,%stusubmissions,%elapsed,$numparts,%nummultipart,$multipart);
                   2700:     my $now = time;
                   2701:     if ($context eq 'course') {
                   2702:         if ($custommenu) {
                   2703:             %crslogins=&Apache::lonnet::dump('nohist_crslastlogin',$cdom,$cnum);
                   2704:             %stusubmissions=&Apache::lonnet::dump('nohist_submissiontracker',$cdom,$cnum);
                   2705:             %elapsed = &Apache::lonlocal::texthash(    
                   2706:                                          -1 => 'more than a month ago',
                   2707:                                     2592000 => 'within last 30 days',
                   2708:                                      604800 => 'within last 7 days',
                   2709:                                       86400 => 'within last 24 hours',
                   2710:                                    );
                   2711:             my $navmap = Apache::lonnavmaps::navmap->new();
                   2712:             if (defined($navmap)) {
                   2713:                 my @allres=$navmap->retrieveResources('/uploaded/'.$cdom.'/'.$cnum.'/default_1261144274.sequence',sub { $_[0]->is_problem() },0);
                   2714:                 foreach my $resource (@allres) {
                   2715:                     my @parts = $resource->parts();
                   2716:                     my $count = scalar(@parts);
                   2717:                     if ($count > 1) {
                   2718:                         $nummultipart{$count} ++;
                   2719:                     }
                   2720:                     $numparts += $count;
                   2721:                 }
                   2722:                 if (keys(%nummultipart) > 0) {
                   2723:                    $multipart = '<br />'.'contains';
                   2724:                    foreach my $key (sort {$a <=> $b} keys(%nummultipart)) {
                   2725:                        $multipart .= " nummultipart{$key} multipart questions (with $key parts)";
                   2726:                    }
                   2727:                 }
                   2728:             }
                   2729:         }
                   2730:     }
                   2731: 
1.2       raeburn  2732:     # Get groups, role, permanent e-mail so we can sort on them if
                   2733:     # necessary.
                   2734:     foreach my $user (keys(%{$userlist})) {
1.43      raeburn  2735:         if ($user eq '' ) {
                   2736:             delete($userlist->{$user});
                   2737:             next;
                   2738:         }
1.11      raeburn  2739:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2740:             delete($userlist->{$user});
                   2741:             next;
                   2742:         }
1.2       raeburn  2743:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2744:         if (($statusmode ne 'Any') && 
                   2745:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2746:             delete($userlist->{$user});
                   2747:             next;
                   2748:         }
1.2       raeburn  2749:         if ($context eq 'domain') {
                   2750:             if ($env{'form.roletype'} eq 'domain') {
                   2751:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2752:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2753:                     ($udom eq $env{'request.role.domain'})) {
                   2754:                     delete($userlist->{$user});
                   2755:                     next;
                   2756:                 }
1.13      raeburn  2757:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2758:                 ($uname,$udom,$role) = split(/:/,$user,-1);
1.102     raeburn  2759:             } elsif (($env{'form.roletype'} eq 'course') || 
                   2760:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2761:                 ($uname,$udom,$role) = split(/:/,$user);
                   2762:             }
                   2763:         } else {
                   2764:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   2765:             if (($context eq 'course') && $role eq '') {
                   2766:                 $role = 'st';
                   2767:             }
                   2768:         }
                   2769:         $userlist->{$user}->[$index{'role'}] = $role;
                   2770:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   2771:             delete($userlist->{$user});
                   2772:             next;
                   2773:         }
1.33      raeburn  2774:         if ($context eq 'course') {
                   2775:             my @ac_groups;
                   2776:             if (ref($classgroups) eq 'HASH') {
                   2777:                 $groups = $classgroups->{$user};
                   2778:             }
                   2779:             if (ref($groups->{'active'}) eq 'HASH') {
                   2780:                 @ac_groups = keys(%{$groups->{'active'}});
                   2781:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   2782:             }
                   2783:             if ($mode ne 'autoenroll') {
                   2784:                 my $section = $userlist->{$user}->[$index{'section'}];
1.43      raeburn  2785:                 if (($env{'request.course.sec'} ne '') && 
                   2786:                     ($section ne $env{'request.course.sec'})) {
                   2787:                     if ($role eq 'st') {
                   2788:                         delete($userlist->{$user});
                   2789:                         next;
                   2790:                     }
                   2791:                 }
1.33      raeburn  2792:                 if ($secfilter eq 'none') {
                   2793:                     if ($section ne '') {
                   2794:                         delete($userlist->{$user});
                   2795:                         next;
                   2796:                     }
                   2797:                 } elsif ($secfilter ne 'all') {
                   2798:                     if ($section ne $secfilter) {
                   2799:                         delete($userlist->{$user});
                   2800:                         next;
                   2801:                     }
                   2802:                 }
                   2803:                 if ($grpfilter eq 'none') {
                   2804:                     if (@ac_groups > 0) {
                   2805:                         delete($userlist->{$user});
                   2806:                         next;
                   2807:                     }
                   2808:                 } elsif ($grpfilter ne 'all') {
                   2809:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   2810:                         delete($userlist->{$user});
                   2811:                         next;
                   2812:                     }
                   2813:                 }
1.44      raeburn  2814:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2815:                     if (($displayphotos eq 'on') && ($role eq 'st')) {
                   2816:                         $userlist->{$user}->[$index{'photo'}] =
1.47      raeburn  2817:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
                   2818:                         $userlist->{$user}->[$index{'thumbnail'}] =
1.44      raeburn  2819:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,
                   2820:                                                                 'gif','thumbnail');
                   2821:                     }
                   2822:                 }
1.33      raeburn  2823:             }
1.2       raeburn  2824:         }
                   2825:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   2826:         if ($emails{'permanentemail'} =~ /\S/) {
                   2827:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   2828:         }
1.4       raeburn  2829:         $usercount ++;
                   2830:     }
                   2831:     my $autocount = 0;
                   2832:     my $manualcount = 0;
                   2833:     my $lockcount = 0;
                   2834:     my $unlockcount = 0;
                   2835:     if ($usercount) {
                   2836:         $r->print($output);
                   2837:     } else {
                   2838:         if ($mode eq 'autoenroll') {
                   2839:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   2840:         } else {
                   2841:             return;
                   2842:         }
1.1       raeburn  2843:     }
1.2       raeburn  2844:     #
                   2845:     # Sort the users
1.1       raeburn  2846:     my $index  = $index{$sortby};
                   2847:     my $second = $index{'username'};
                   2848:     my $third  = $index{'domain'};
1.2       raeburn  2849:     my @sorted_users = sort {
                   2850:         lc($userlist->{$a}->[$index])  cmp lc($userlist->{$b}->[$index])
1.1       raeburn  2851:             ||
1.2       raeburn  2852:         lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second])            ||
                   2853:         lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   2854:         } (keys(%$userlist));
1.4       raeburn  2855:     my $rowcount = 0;
1.2       raeburn  2856:     foreach my $user (@sorted_users) {
1.4       raeburn  2857:         my %in;
1.2       raeburn  2858:         my $sdata = $userlist->{$user};
1.4       raeburn  2859:         $rowcount ++; 
1.2       raeburn  2860:         foreach my $item (@{$keylist}) {
                   2861:             $in{$item} = $sdata->[$index{$item}];
                   2862:         }
1.67      droeschl 2863:         my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   2864:         if ($clickers!~/\w/) { $clickers='-'; }
                   2865:         $in{'clicker'} = $clickers; 
                   2866: 	my $role = $in{'role'};
1.102     raeburn  2867:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
1.2       raeburn  2868:         if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2869:             $in{'start'} = &mt('none');
                   2870:         } else {
                   2871:             $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
1.1       raeburn  2872:         }
1.2       raeburn  2873:         if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2874:             $in{'end'} = &mt('none');
1.1       raeburn  2875:         } else {
1.2       raeburn  2876:             $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1       raeburn  2877:         }
1.109.2.3  raeburn  2878:         if ($context eq 'course') {
                   2879:             if ($custommenu) {
                   2880:                 my $lastlogin = $crslogins{$in{'username'}.':'.$in{'domain'}.':'.$in{'section'}.':'.$role};
                   2881:                 if ($lastlogin ne '') {
                   2882:                     my $sincelogin = $now - $lastlogin;
                   2883:                     if ($sincelogin < 86400) {
                   2884:                         $in{'lastlogin'} = $elapsed{'86400'};
                   2885:                     } elsif ($sincelogin < 604800) {
                   2886:                         $in{'lastlogin'} = $elapsed{'604800'};
                   2887:                     } elsif ($sincelogin < 2592000 ) {
                   2888:                         $in{'lastlogin'} = $elapsed{'2592000'};
                   2889:                     } else {
                   2890:                         $in{'lastlogin'} = $elapsed{'-1'};
                   2891:                     }
                   2892:                 }
                   2893:             }
                   2894:             if ($role eq 'st') {
                   2895:                 my $numsub = $stusubmissions{$in{'username'}.':'.$in{'domain'}."\0attempts"} + 
1.109.2.5  raeburn  2896:                              $stusubmissions{$in{'username'}.':'.$in{'domain'}."\0surveysubs"};
1.109.2.3  raeburn  2897:                 if (!$numsub) {
1.109.2.4  raeburn  2898:                     $in{'submissions'} = 'not attempted';
1.109.2.3  raeburn  2899:                 } elsif ($numsub < $numparts) {
                   2900:                     $in{'submissions'} = 'incomplete ('.$numsub.'/'.$numparts.')';
                   2901:                 } else {
                   2902:                     $in{'submissions'} = 'completed';
                   2903:                 }
                   2904:             }
                   2905:         }
1.55      raeburn  2906:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  2907:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  2908:             my $checkval;
1.16      raeburn  2909:             if ($mode eq 'autoenroll') {
                   2910:                 my $cellentry;
                   2911:                 if ($in{'type'} eq 'auto') {
                   2912:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label>';
                   2913:                     $autocount ++;
                   2914:                 } else {
1.76      bisitz   2915:                     $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><span class="LC_nobreak"><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label></span></td></tr><tr><td><span class="LC_nobreak">';
1.16      raeburn  2916:                     $manualcount ++;
                   2917:                     if ($in{'lockedtype'}) {
                   2918:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Unlock').'</label>';
                   2919:                         $unlockcount ++;
                   2920:                     } else {
                   2921:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Lock').'</label>';
                   2922:                         $lockcount ++;
1.11      raeburn  2923:                     }
1.76      bisitz   2924:                     $cellentry .= '</span></td></tr></table>';
1.16      raeburn  2925:                 }
                   2926:                 $r->print("<td>$cellentry</td>\n");
                   2927:             } else {
1.55      raeburn  2928:                 if ($mode ne 'pickauthor') {  
                   2929:                     $r->print("<td>$rowcount</td>\n");
                   2930:                 }
1.16      raeburn  2931:                 if ($actionselect) {
1.26      raeburn  2932:                     my $showcheckbox;
                   2933:                     if ($role =~ /^cr\//) {
                   2934:                         $showcheckbox = $canchange{'cr'};
                   2935:                     } else {
                   2936:                         $showcheckbox = $canchange{$role};
                   2937:                     }
                   2938:                     if (!$showcheckbox) {
                   2939:                         if ($context eq 'course') {
                   2940:                             if ($canchangesec{$role} ne '') {
                   2941:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   2942:                                     $showcheckbox = 1;
                   2943:                                 }
                   2944:                             }
1.16      raeburn  2945:                         }
1.26      raeburn  2946:                     }
                   2947:                     if ($showcheckbox) {
                   2948:                         $checkval = $user; 
                   2949:                         if ($context eq 'course') {
                   2950:                             if ($role eq 'st') {
                   2951:                                 $checkval .= ':st';
                   2952:                             }
                   2953:                             $checkval .= ':'.$in{'section'};
                   2954:                             if ($role eq 'st') {
                   2955:                                 $checkval .= ':'.$in{'type'}.':'.
                   2956:                                              $in{'lockedtype'};
                   2957:                             }
1.16      raeburn  2958:                         }
1.26      raeburn  2959:                         $r->print('<td><input type="checkbox" name="'.
1.86      bisitz   2960:                                   'actionlist" value="'.$checkval.'" /></td>');
1.26      raeburn  2961:                     } else {
                   2962:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  2963:                     }
1.55      raeburn  2964:                 } elsif ($mode eq 'pickauthor') {
                   2965:                         $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
1.11      raeburn  2966:                 }
                   2967:             }
1.2       raeburn  2968:             foreach my $item (@cols) {
1.10      raeburn  2969:                 if ($item eq 'username') {
1.48      raeburn  2970:                     $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
1.16      raeburn  2971:                 } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11      raeburn  2972:                     $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.83      raeburn  2973:                 } elsif ($item eq 'status') {
                   2974:                     my $showitem = $in{$item};
                   2975:                     if (defined($ltstatus{$in{$item}})) {
                   2976:                         $showitem = $ltstatus{$in{$item}};
                   2977:                     }
                   2978:                     $r->print('<td>'.$showitem.'</td>'."\n");
1.109.2.3  raeburn  2979:                 } elsif ($item eq 'submissions') {
                   2980:                     if ($in{$item} =~ /^incomplete/) { 
                   2981:                         $r->print('<td>'.$in{$item}.$multipart.'</td>');
                   2982:                     } else {
                   2983:                         $r->print('<td>'.$in{$item}.'</td>'."\n");
                   2984:                     }
1.10      raeburn  2985:                 } else {
                   2986:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   2987:                 }
1.2       raeburn  2988:             }
1.16      raeburn  2989:             if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4       raeburn  2990:                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
1.109.2.3  raeburn  2991:                     if ($clickersupport) {
                   2992:                         if ($displayclickers eq 'on') {
                   2993:                             my $clickers =
1.2       raeburn  2994:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.109.2.3  raeburn  2995:                             if ($clickers!~/\w/) { $clickers='-'; }
                   2996:                             $r->print('<td>'.$clickers.'</td>');
                   2997:                         } else {
                   2998:                             $r->print('    <td>&nbsp;</td>  ');
                   2999:                         }
1.2       raeburn  3000:                     }
1.4       raeburn  3001:                     if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.44      raeburn  3002:                         if ($displayphotos eq 'on' && $role eq 'st' && $in{'photo'} ne '') {
1.90      bisitz   3003:                             $r->print('    <td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
1.4       raeburn  3004:                         } else {
                   3005:                             $r->print('    <td>&nbsp;</td>  ');
                   3006:                         }
                   3007:                     }
1.2       raeburn  3008:                 }
                   3009:             }
                   3010:             $r->print(&Apache::loncommon::end_data_table_row());
                   3011:         } elsif ($mode eq 'csv') {
                   3012:             next if (! defined($CSVfile));
                   3013:             # no need to bother with $linkto
                   3014:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   3015:                 $in{'start'} = &mt('none');
                   3016:             } else {
                   3017:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   3018:             }
                   3019:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   3020:                 $in{'end'} = &mt('none');
                   3021:             } else {
                   3022:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   3023:             }
                   3024:             my @line = ();
                   3025:             foreach my $item (@cols) {
                   3026:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   3027:             }
1.67      droeschl 3028:             print $CSVfile '"'.join('","',@line)."\"\n";
1.2       raeburn  3029:         } elsif ($mode eq 'excel') {
                   3030:             my $col = 0;
                   3031:             foreach my $item (@cols) {
                   3032:                 if ($item eq 'start' || $item eq 'end') {
                   3033:                     if (defined($item) && $item != 0) {
                   3034:                         $excel_sheet->write($row,$col++,
                   3035:                             &Apache::lonstathelpers::calc_serial($in{item}),
                   3036:                                     $format->{'date'});
                   3037:                     } else {
                   3038:                         $excel_sheet->write($row,$col++,'none');
                   3039:                     }
                   3040:                 } else {
                   3041:                     $excel_sheet->write($row,$col++,$in{$item});
                   3042:                 }
                   3043:             }
                   3044:             $row++;
1.1       raeburn  3045:         }
                   3046:     }
1.55      raeburn  3047:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  3048:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3049:     } elsif ($mode eq 'excel') {
                   3050:         $excel_workbook->close();
1.68      droeschl 3051: 	$r->print(&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<p><a href="'.$excel_filename.'">','</a>')."</p>\n");
1.2       raeburn  3052:     } elsif ($mode eq 'csv') {
                   3053:         close($CSVfile);
1.68      droeschl 3054: 	$r->print(&mt('[_1]Your CSV file[_2] is ready for download.', '<p><a href="'.$CSVfilename.'">','</a>')."</p>\n");
1.2       raeburn  3055:         $r->rflush();
                   3056:     }
                   3057:     if ($mode eq 'autoenroll') {
                   3058:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  3059:     } else {
                   3060:         return ($usercount);
1.2       raeburn  3061:     }
1.1       raeburn  3062: }
                   3063: 
1.56      raeburn  3064: sub bulkaction_javascript {
                   3065:     my ($formname,$caller) = @_;
                   3066:     my $docstart = 'document';
                   3067:     if ($caller eq 'popup') {
                   3068:         $docstart = 'opener.document';
                   3069:     }
                   3070:     my %lt = &Apache::lonlocal::texthash(
                   3071:               acwi => 'Access will be set to start immediately',
                   3072:               asyo => 'as you did not select an end date in the pop-up window',
                   3073:               accw => 'Access will be set to continue indefinitely',
                   3074:               asyd => 'as you did not select an end date in the pop-up window',
                   3075:               sewi => "Sections will be switched to 'No section'",
                   3076:               ayes => "as you either selected the 'No section' option",
                   3077:               oryo => 'or you did not select a section in the pop-up window',
                   3078:               arol => 'A role with no section will be added',
                   3079:               swbs => 'Sections will be switched to:',
                   3080:               rwba => 'Roles will be added for section(s):',
                   3081:             );
                   3082:     my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
                   3083:     my $noaction = &mt("You need to select an action to take for the user(s) you have selected"); 
                   3084:     my $singconfirm = &mt(' for a single user?');
                   3085:     my $multconfirm = &mt(' for multiple users?');
                   3086:     my $output = <<"ENDJS";
                   3087: function verify_action (field) {
                   3088:     var numchecked = 0;
                   3089:     var singconf = '$singconfirm';
                   3090:     var multconf = '$multconfirm';
                   3091:     if ($docstart.$formname.elements[field].length > 0) {
                   3092:         for (i=0; i<$docstart.$formname.elements[field].length; i++) {
                   3093:             if ($docstart.$formname.elements[field][i].checked == true) {
                   3094:                numchecked ++;
                   3095:             }
                   3096:         }
                   3097:     } else {
                   3098:         if ($docstart.$formname.elements[field].checked == true) {
                   3099:             numchecked ++;
                   3100:         }
                   3101:     }
                   3102:     if (numchecked == 0) {
                   3103:         alert("$alert");
                   3104:         return;
                   3105:     } else {
                   3106:         var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
                   3107:         var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
                   3108:         if (choice == '') {
                   3109:             alert("$noaction");
                   3110:             return;
                   3111:         } else {
                   3112:             if (numchecked == 1) {
                   3113:                 message += singconf;
                   3114:             } else {
                   3115:                 message += multconf;
                   3116:             }
                   3117: ENDJS
                   3118:     if ($caller ne 'popup') {
                   3119:         $output .= <<"NEWWIN";
                   3120:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
                   3121:                 opendatebrowser(document.$formname,'$formname','go');
                   3122:                 return;
                   3123: 
                   3124:             } else {
                   3125:                 if (confirm(message)) {
                   3126:                     document.$formname.phase.value = 'bulkchange';
                   3127:                     document.$formname.submit();
                   3128:                     return;
                   3129:                 }
                   3130:             }
                   3131: NEWWIN
                   3132:     } else {
                   3133:         $output .= <<"POPUP";
                   3134:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
                   3135:                 var datemsg = '';
                   3136:                 if (($docstart.$formname.startdate_month.value == '') &&
                   3137:                     ($docstart.$formname.startdate_day.value  == '') &&
                   3138:                     ($docstart.$formname.startdate_year.value == '')) {
                   3139:                     datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
                   3140:                 }
                   3141:                 if (($docstart.$formname.enddate_month.value == '') &&
                   3142:                     ($docstart.$formname.enddate_day.value  == '') &&
                   3143:                     ($docstart.$formname.enddate_year.value == '')) {
                   3144:                     datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
                   3145:                 }
                   3146:                 if (datemsg != '') {
                   3147:                     message += "\\n"+datemsg;
                   3148:                 }
                   3149:             }
                   3150:             if (choice == 'chgsec') {
                   3151:                 var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
                   3152:                 var retained =  $docstart.$formname.retainsec.value;
                   3153:                 var secshow = $docstart.$formname.newsecs.value;
                   3154:                 if (secshow == '') {
                   3155:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   3156:                         message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   3157:                     } else {
                   3158:                         message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   3159:                     }
                   3160:                 } else {
                   3161:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   3162:                         message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
                   3163:                     } else {
                   3164:                         message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
                   3165:                     }
                   3166:                 }
                   3167:             }
                   3168:             if (confirm(message)) {
                   3169:                 $docstart.$formname.phase.value = 'bulkchange';
                   3170:                 $docstart.$formname.submit();
                   3171:                 window.close();
                   3172:             }
                   3173: POPUP
                   3174:     }
                   3175:     $output .= '
                   3176:         }
                   3177:     }
                   3178: }
                   3179: ';
                   3180:     return $output;
                   3181: }
                   3182: 
1.10      raeburn  3183: sub print_username_link {
1.48      raeburn  3184:     my ($mode,$in) = @_;
1.10      raeburn  3185:     my $output;
1.16      raeburn  3186:     if ($mode eq 'autoenroll') {
                   3187:         $output = $in->{'username'};
1.10      raeburn  3188:     } else {
                   3189:         $output = '<a href="javascript:username_display_launch('.
                   3190:                   "'$in->{'username'}','$in->{'domain'}'".')" />'.
                   3191:                   $in->{'username'}.'</a>';
                   3192:     }
                   3193:     return $output;
                   3194: }
                   3195: 
1.2       raeburn  3196: sub role_type_names {
                   3197:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  3198:                          'domain' => 'Domain Roles',
                   3199:                          'author' => 'Co-Author Roles',
                   3200:                          'course' => 'Course Roles',
1.101     raeburn  3201:                          'community' => 'Community Roles',
1.2       raeburn  3202:              );
                   3203:     return %lt;
                   3204: }
                   3205: 
1.11      raeburn  3206: sub select_actions {
1.55      raeburn  3207:     my ($context,$setting,$statusmode,$formname) = @_;
1.11      raeburn  3208:     my %lt = &Apache::lonlocal::texthash(
                   3209:                 revoke   => "Revoke user roles",
                   3210:                 delete   => "Delete user roles",
                   3211:                 reenable => "Re-enable expired user roles",
                   3212:                 activate => "Make future user roles active now",
                   3213:                 chgdates  => "Change starting/ending dates",
                   3214:                 chgsec   => "Change section associated with user roles",
                   3215:     );
                   3216:     my ($output,$options,%choices);
1.23      raeburn  3217:     # FIXME Disable actions for now for roletype=course in domain context
                   3218:     if ($context eq 'domain' && $setting eq 'course') {
                   3219:         return;
                   3220:     }
1.26      raeburn  3221:     if ($context eq 'course') {
                   3222:         if ($env{'form.showrole'} ne 'Any') {
                   3223:              if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   3224:                                            $env{'request.course.id'})) {
                   3225:                  if ($env{'request.course.sec'} eq '') {
                   3226:                      return;
                   3227:                  } else {
                   3228:                      if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   3229:                          return;
                   3230:                      }
                   3231:                  }
                   3232:             }
                   3233:         }
                   3234:     }
1.11      raeburn  3235:     if ($statusmode eq 'Any') {
                   3236:         $options .= '
                   3237: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   3238:         $choices{'dates'} = 1;
                   3239:     } else {
                   3240:         if ($statusmode eq 'Future') {
                   3241:             $options .= '
                   3242: <option value="activate">'.$lt{'activate'}.'</option>';
                   3243:             $choices{'dates'} = 1;
                   3244:         } elsif ($statusmode eq 'Expired') {
                   3245:             $options .= '
                   3246: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   3247:             $choices{'dates'} = 1;
                   3248:         }
1.13      raeburn  3249:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   3250:             $options .= '
                   3251: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   3252: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   3253:             $choices{'dates'} = 1;
                   3254:         }
1.11      raeburn  3255:     }
                   3256:     if ($context eq 'domain') {
                   3257:         $options .= '
                   3258: <option value="delete">'.$lt{'delete'}.'</option>';
                   3259:     }
                   3260:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  3261:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  3262:             $options .= '
                   3263: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   3264:             $choices{'sections'} = 1;
                   3265:         }
                   3266:     }
                   3267:     if ($options) {
1.56      raeburn  3268:         $output = '<select name="bulkaction">'."\n".
1.11      raeburn  3269:                   '<option value="" selected="selected">'.
                   3270:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   3271:         if ($choices{'dates'}) {
                   3272:             $output .= 
                   3273:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   3274:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   3275:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   3276:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   3277:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   3278:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   3279:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   3280:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   3281:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   3282:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   3283:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
1.56      raeburn  3284:                 '<input type="hidden" name="enddate_second" value="" />'."\n".
                   3285:                 '<input type="hidden" name="no_end_date" value="" />'."\n";
1.11      raeburn  3286:             if ($context eq 'course') {
                   3287:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   3288:             }
                   3289:         }
                   3290:         if ($choices{'sections'}) {
1.91      bisitz   3291:             $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
                   3292:                        '<input type="hidden" name="newsecs" value="" />'."\n";
1.11      raeburn  3293:         }
                   3294:     }
                   3295:     return $output;
                   3296: }
                   3297: 
                   3298: sub date_section_javascript {
                   3299:     my ($context,$setting) = @_;
1.49      raeburn  3300:     my $title = 'Date_And_Section_Selector';
1.41      raeburn  3301:     my %nopopup = &Apache::lonlocal::texthash (
                   3302:         revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
                   3303:         delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
                   3304:         none   => "Choose an action to take for selected users",
                   3305:     );  
1.96      bisitz   3306:     my $output = <<"ENDONE";
                   3307: <script type="text/javascript">
                   3308: // <![CDATA[
1.41      raeburn  3309:     function opendatebrowser(callingform,formname,calledby) {
1.11      raeburn  3310:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   3311:         var url = '/adm/createuser?';
                   3312:         var type = '';
                   3313:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   3314: ENDONE
                   3315:     if ($context eq 'domain') {
                   3316:         $output .= '
                   3317:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   3318: ';
                   3319:     }
                   3320:     my $width= '700';
                   3321:     my $height = '400';
                   3322:     $output .= <<"ENDTWO";
                   3323:         url += 'action=dateselect&callingform=' + formname + 
                   3324:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   3325:         var title = '$title';
                   3326:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   3327:         options += ',width=$width,height=$height';
                   3328:         stdeditbrowser = open(url,title,options,'1');
                   3329:         stdeditbrowser.focus();
                   3330:     }
1.96      bisitz   3331: // ]]>
1.11      raeburn  3332: </script>
                   3333: ENDTWO
                   3334:     return $output;
                   3335: }
                   3336: 
                   3337: sub date_section_selector {
1.101     raeburn  3338:     my ($context,$permission,$crstype) = @_;
1.11      raeburn  3339:     my $callingform = $env{'form.callingform'};
                   3340:     my $formname = 'dateselect';  
                   3341:     my $groupslist = &get_groupslist();
                   3342:     my $sec_js = &setsections_javascript($formname,$groupslist);
                   3343:     my $output = <<"END";
                   3344: <script type="text/javascript">
1.96      bisitz   3345: // <![CDATA[
1.11      raeburn  3346: 
                   3347: $sec_js
                   3348: 
                   3349: function saveselections(formname) {
                   3350: 
                   3351: END
                   3352:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3353:         $output .= <<"END";
1.40      raeburn  3354:         if (formname.retainsec.length > 1) {  
                   3355:             for (var i=0; i<formname.retainsec.length; i++) {
                   3356:                 if (formname.retainsec[i].checked == true) {
                   3357:                     opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
                   3358:                 }
                   3359:             }
                   3360:         } else {
                   3361:             opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   3362:         }
1.103     raeburn  3363:         setSections(formname,'$crstype');
1.11      raeburn  3364:         if (seccheck == 'ok') {
                   3365:             opener.document.$callingform.newsecs.value = formname.sections.value;
                   3366:         }
                   3367: END
                   3368:     } else {
                   3369:         if ($context eq 'course') {
                   3370:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   3371:                 ($env{'form.bulkaction'} eq 'activate') || 
                   3372:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  3373:                 if ($env{'request.course.sec'} eq '') {
                   3374:                     $output .= <<"END";
1.11      raeburn  3375:  
                   3376:         if (formname.makedatesdefault.checked == true) {
                   3377:             opener.document.$callingform.makedatesdefault.value = 1;
                   3378:         }
                   3379:         else {
                   3380:             opener.document.$callingform.makedatesdefault.value = 0;
                   3381:         }
                   3382: 
                   3383: END
1.26      raeburn  3384:                 }
1.11      raeburn  3385:             }
                   3386:         }
                   3387:         $output .= <<"END";
                   3388:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   3389:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   3390:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   3391:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   3392:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   3393:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   3394:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   3395:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   3396:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   3397:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   3398:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   3399:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
1.56      raeburn  3400:     if (formname.no_end_date.checked) {
                   3401:         opener.document.$callingform.no_end_date.value = '1';
                   3402:     } else {
                   3403:         opener.document.$callingform.no_end_date.value = '0';
                   3404:     }
1.11      raeburn  3405: END
                   3406:     }
1.56      raeburn  3407:     my $verify_action_js = &bulkaction_javascript($callingform,'popup');
                   3408:     $output .= <<"ENDJS";
                   3409:     verify_action('actionlist');
1.11      raeburn  3410: }
1.56      raeburn  3411: 
                   3412: $verify_action_js
                   3413: 
1.96      bisitz   3414: // ]]>
1.11      raeburn  3415: </script>
1.56      raeburn  3416: ENDJS
1.11      raeburn  3417:     my %lt = &Apache::lonlocal::texthash (
                   3418:                  chac => 'Access dates to apply for selected users',
                   3419:                  chse => 'Changes in section affiliation to apply to selected users',
                   3420:                  fors => 'For student roles changing the section, will result in a section switch as students may only be in one section of a course at a time.',
                   3421:                  forn => 'For a role in a course that is not a student role, a user may have roles in more than one section of a course at a time.',
                   3422:                  reta => "Retain each user's current section affiliations?", 
1.103     raeburn  3423:                  dnap => '(Does not apply to student roles).',
1.11      raeburn  3424:             );
                   3425:     my ($date_items,$headertext);
                   3426:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3427:         $headertext = $lt{'chse'};
                   3428:     } else {
                   3429:         $headertext = $lt{'chac'};
                   3430:         my $starttime;
                   3431:         if (($env{'form.bulkaction'} eq 'activate') || 
                   3432:             ($env{'form.bulkaction'} eq 'reenable')) {
                   3433:             $starttime = time;
                   3434:         }
                   3435:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  3436:                                           $env{'form.bulkaction'},$formname,
1.101     raeburn  3437:                                           $permission,$crstype);
1.11      raeburn  3438:     }
                   3439:     $output .= '<h3>'.$headertext.'</h3>'.
                   3440:                '<form name="'.$formname.'" method="post">'."\n".
                   3441:                 $date_items;
                   3442:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  3443:         my ($cnum,$cdom) = &get_course_identity();
1.101     raeburn  3444:         my $crstype = &Apache::loncommon::course_type();
1.103     raeburn  3445:         if ($crstype eq 'Community') {
                   3446:             $lt{'fors'} = &mt('For member roles changing the section, will result in a section switch as members may only be in one section of a community at a time.');
                   3447:             $lt{'forn'} = &mt('For a role in a community that is not a member role, a user may have roles in more than one section of a community at a time.');
                   3448:             $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
                   3449:         }
1.11      raeburn  3450:         my $info;
                   3451:         if ($env{'form.showrole'} eq 'st') {
                   3452:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  3453:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  3454:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   3455:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   3456:             $info = $lt{'reta'};
                   3457:         } else {
                   3458:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   3459:             $info = $lt{'reta'};
                   3460:         }
                   3461:         if ($info) {
                   3462:             $info .= '<span class="LC_nobreak">'.
                   3463:                      '<label><input type="radio" name="retainsec" value="1" '.
                   3464:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   3465:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   3466:                      &mt('No').'</label></span>';
                   3467:             if ($env{'form.showrole'} eq 'Any') {
                   3468:                 $info .= '<br />'.$lt{'dnap'};
                   3469:             }
                   3470:             $info .= '</p>';
                   3471:         } else {
                   3472:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   3473:         }
1.21      raeburn  3474:         my $rowtitle = &mt('New section to assign');
1.101     raeburn  3475:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context,'',$crstype);
1.11      raeburn  3476:         $output .= $info.$secbox;
                   3477:     }
                   3478:     $output .= '<p>'.
1.81      schafran 3479: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
1.11      raeburn  3480: '</form>';
                   3481:     return $output;
                   3482: }
                   3483: 
1.17      raeburn  3484: sub section_picker {
1.109.2.5  raeburn  3485:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype,$caller) = @_;
1.17      raeburn  3486:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3487:     my $sections_select .= &course_sections(\%sections_count,$role);
                   3488:     my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
1.109.2.5  raeburn  3489:     if ($mode eq 'upload' && $caller ne 'requestcrs') {
1.17      raeburn  3490:         my ($options,$cb_script,$coursepick) =
1.101     raeburn  3491:             &default_role_selector($context,1,$crstype);
1.59      bisitz   3492:         $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
1.17      raeburn  3493:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3494:     }
                   3495:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
1.109.2.5  raeburn  3496:     if ($caller eq 'requestcrs') {
                   3497:        $secbox .=  '<input type="hidden" name="defaultrole" value="st" />'."\n".
                   3498:                    '<input type="text" name="newsec" size="15" />'."\n".
                   3499:                    '<input type="hidden" name="sections" value="" />'."\n";
                   3500:     } elsif ($env{'request.course.sec'} eq '') {
1.17      raeburn  3501:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   3502:                    '<td align="center">'.&mt('Existing sections')."\n".
                   3503:                    '<br />'.$sections_select.'</td><td align="center">'.
                   3504:                    &mt('New section').'<br />'."\n".
                   3505:                    '<input type="text" name="newsec" size="15" />'."\n".
                   3506:                    '<input type="hidden" name="sections" value="" />'."\n".
                   3507:                    '</td></tr></table>'."\n";
                   3508:     } else {
                   3509:        $secbox .= '<input type="hidden" name="sections" value="'.
                   3510:                    $env{'request.course.sec'}.'" />'.
                   3511:                    $env{'request.course.sec'};
                   3512:     }
                   3513:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                   3514:                &Apache::lonhtmlcommon::end_pick_box().'</p>';
                   3515:     return $secbox;
                   3516: }
                   3517: 
1.2       raeburn  3518: sub results_header_row {
1.102     raeburn  3519:     my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
1.5       raeburn  3520:     my ($description,$showfilter);
                   3521:     if ($rolefilter ne 'Any') {
                   3522:         $showfilter = $rolefilter;
                   3523:     }
1.2       raeburn  3524:     if ($context eq 'course') {
1.24      raeburn  3525:         if ($mode eq 'csv' || $mode eq 'excel') {
1.102     raeburn  3526:             if ($crstype eq 'Community') {
                   3527:                 $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3528:             } else {
                   3529:                 $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3530:             }
1.24      raeburn  3531:         }
1.2       raeburn  3532:         if ($statusmode eq 'Expired') {
1.102     raeburn  3533:             if ($crstype eq 'Community') {
                   3534:                 $description .= &mt('Users in community with expired [_1] roles',$showfilter);
                   3535:             } else {
                   3536:                 $description .= &mt('Users in course with expired [_1] roles',$showfilter);
                   3537:             }
1.11      raeburn  3538:         } elsif ($statusmode eq 'Future') {
1.102     raeburn  3539:             if ($crstype eq 'Community') {
                   3540:                 $description .= &mt('Users in community with future [_1] roles',$showfilter);
                   3541:             } else {
                   3542:                 $description .= &mt('Users in course with future [_1] roles',$showfilter);
                   3543:             }
1.2       raeburn  3544:         } elsif ($statusmode eq 'Active') {
1.102     raeburn  3545:             if ($crstype eq 'Community') {
                   3546:                 $description .= &mt('Users in community with active [_1] roles',$showfilter);
                   3547:             } else {
                   3548:                 $description .= &mt('Users in course with active [_1] roles',$showfilter);
                   3549:             }
1.2       raeburn  3550:         } else {
                   3551:             if ($rolefilter eq 'Any') {
1.102     raeburn  3552:                 if ($crstype eq 'Community') {
                   3553:                     $description .= &mt('All users in community');
                   3554:                 } else {
                   3555:                     $description .= &mt('All users in course');
                   3556:                 }
1.2       raeburn  3557:             } else {
1.102     raeburn  3558:                 if ($crstype eq 'Community') {
                   3559:                     $description .= &mt('All users in community with [_1] roles',$rolefilter);
                   3560:                 } else {
                   3561:                     $description .= &mt('All users in course with [_1] roles',$rolefilter);
                   3562:                 }
1.2       raeburn  3563:             }
                   3564:         }
1.33      raeburn  3565:         my $constraint;
1.26      raeburn  3566:         my $viewablesec = &viewable_section($permission);
                   3567:         if ($viewablesec ne '') {
1.15      raeburn  3568:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  3569:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.103     raeburn  3570:             } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
1.33      raeburn  3571:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   3572:             }
                   3573:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3574:                 if ($env{'form.grpfilter'} eq 'none') {
                   3575:                     $constraint .= &mt(' and not in any group');
                   3576:                 } else {
                   3577:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   3578:                 }
                   3579:             }
                   3580:         } else {
                   3581:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   3582:                 if ($env{'form.secfilter'} eq 'none') {
                   3583:                     $constraint = &mt('only users affiliated with no section');
                   3584:                 } else {
                   3585:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   3586:                 }
                   3587:             }
                   3588:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3589:                 if ($env{'form.grpfilter'} eq 'none') {
                   3590:                     if ($constraint eq '') {
                   3591:                         $constraint = &mt('only users not in any group');
                   3592:                     } else {
                   3593:                         $constraint .= &mt(' and also not in any group'); 
                   3594:                     }
                   3595:                 } else {
                   3596:                     if ($constraint eq '') {
                   3597:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   3598:                     } else {
                   3599:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   3600:                     }
                   3601:                 }
1.15      raeburn  3602:             }
                   3603:         }
1.33      raeburn  3604:         if ($constraint ne '') {
                   3605:             $description .= ' ('.$constraint.')';
                   3606:         } 
1.13      raeburn  3607:     } elsif ($context eq 'author') {
1.14      raeburn  3608:         $description = 
1.73      bisitz   3609:             &mt('Author space for [_1]'
                   3610:                 ,'<span class="LC_cusr_emph">'
                   3611:                 .&Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})
                   3612:                 .'</span>')
                   3613:             .':&nbsp;&nbsp;';
1.2       raeburn  3614:         if ($statusmode eq 'Expired') {
1.5       raeburn  3615:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  3616:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  3617:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  3618:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  3619:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  3620:         } else {
                   3621:             if ($rolefilter eq 'Any') {
1.5       raeburn  3622:                 $description .= &mt('All co-authors');
1.2       raeburn  3623:             } else {
                   3624:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   3625:             }
                   3626:         }
                   3627:     } elsif ($context eq 'domain') {
                   3628:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.73      bisitz   3629:         $description = &mt('Domain - [_1]:',$domdesc).' ';
1.2       raeburn  3630:         if ($env{'form.roletype'} eq 'domain') {
                   3631:             if ($statusmode eq 'Expired') {
1.5       raeburn  3632:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3633:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3634:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  3635:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3636:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  3637:             } else {
                   3638:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3639:                     $description .= &mt('All users in domain');
1.2       raeburn  3640:                 } else {
                   3641:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   3642:                 }
                   3643:             }
1.13      raeburn  3644:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  3645:             if ($statusmode eq 'Expired') {
1.5       raeburn  3646:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3647:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3648:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  3649:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3650:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  3651:             } else {
                   3652:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3653:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  3654:                 } else {
                   3655:                     $description .= &mt('All co-authors in domain  with [_1] roles',$rolefilter);
                   3656:                 }
                   3657:             }
1.102     raeburn  3658:         } elsif (($env{'form.roletype'} eq 'course') || 
                   3659:                  ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  3660:             my $coursefilter = $env{'form.coursepick'};
1.102     raeburn  3661:             if ($env{'form.roletype'} eq 'course') {
                   3662:                 if ($coursefilter eq 'category') {
                   3663:                     my $instcode = &instcode_from_coursefilter();
                   3664:                     if ($instcode eq '.') {
                   3665:                         $description .= &mt('All courses in domain').' - ';
                   3666:                     } else {
                   3667:                         $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                   3668:                     }
                   3669:                 } elsif ($coursefilter eq 'selected') {
                   3670:                     $description .= &mt('Selected courses in domain').' - ';
                   3671:                 } elsif ($coursefilter eq 'all') {
1.2       raeburn  3672:                     $description .= &mt('All courses in domain').' - ';
                   3673:                 }
1.102     raeburn  3674:             } elsif ($env{'form.roletype'} eq 'community') {
                   3675:                 if ($coursefilter eq 'selected') {
                   3676:                     $description .= &mt('Selected communities in domain').' - ';
                   3677:                 } elsif ($coursefilter eq 'all') {
                   3678:                     $description .= &mt('All communities in domain').' - ';
                   3679:                 }
1.2       raeburn  3680:             }
                   3681:             if ($statusmode eq 'Expired') {
1.5       raeburn  3682:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  3683:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3684:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  3685:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3686:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  3687:             } else {
                   3688:                 if ($rolefilter eq 'Any') {
                   3689:                     $description .= &mt('all users');
                   3690:                 } else {
                   3691:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   3692:                 }
                   3693:             }
                   3694:         }
                   3695:     }
                   3696:     return $description;
                   3697: }
1.22      raeburn  3698: 
                   3699: sub viewable_section {
                   3700:     my ($permission) = @_;
                   3701:     my $viewablesec;
                   3702:     if (ref($permission) eq 'HASH') {
                   3703:         if (exists($permission->{'view_section'})) {
                   3704:             $viewablesec = $permission->{'view_section'};
                   3705:         } elsif (exists($permission->{'cusr_section'})) {
                   3706:             $viewablesec = $permission->{'cusr_section'};
                   3707:         }
                   3708:     }
                   3709:     return $viewablesec;
                   3710: }
                   3711: 
1.2       raeburn  3712:     
1.1       raeburn  3713: #################################################
                   3714: #################################################
                   3715: sub show_drop_list {
1.101     raeburn  3716:     my ($r,$classlist,$nosort,$permission,$crstype) = @_;
1.29      raeburn  3717:     my $cid = $env{'request.course.id'};
1.17      raeburn  3718:     my ($cnum,$cdom) = &get_course_identity($cid);
1.109.2.3  raeburn  3719:     my $displaygroups;
                   3720:     unless (&Apache::loncommon::needs_gci_custom()) {
                   3721:         $displaygroups = 1;
                   3722:     }
1.1       raeburn  3723:     if (! exists($env{'form.sortby'})) {
                   3724:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   3725:                                                 ['sortby']);
                   3726:     }
                   3727:     my $sortby = $env{'form.sortby'};
                   3728:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   3729:         $sortby = 'username';
                   3730:     }
                   3731:     my $action = "drop";
1.17      raeburn  3732:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  3733:     $r->print(<<END);
                   3734: <input type="hidden" name="sortby" value="$sortby" />
                   3735: <input type="hidden" name="action" value="$action" />
                   3736: <input type="hidden" name="state"  value="done" />
1.17      raeburn  3737: <script type="text/javascript" language="Javascript">
1.96      bisitz   3738: // <![CDATA[
1.17      raeburn  3739: $check_uncheck_js
1.96      bisitz   3740: // ]]>
1.1       raeburn  3741: </script>
                   3742: <p>
1.86      bisitz   3743: <input type="hidden" name="phase" value="four" />
1.1       raeburn  3744: END
1.30      raeburn  3745:     my ($indexhash,$keylist) = &make_keylist_array();
                   3746:     my $studentcount = 0;
                   3747:     if (ref($classlist) eq 'HASH') {
                   3748:         foreach my $student (keys(%{$classlist})) {
                   3749:             my $sdata = $classlist->{$student}; 
                   3750:             my $status = $sdata->[$indexhash->{'status'}];
                   3751:             my $section = $sdata->[$indexhash->{'section'}];
                   3752:             if ($status ne 'Active') {
                   3753:                 delete($classlist->{$student});
                   3754:                 next;
                   3755:             }
                   3756:             if ($env{'request.course.sec'} ne '') {
                   3757:                 if ($section ne $env{'request.course.sec'}) {
                   3758:                     delete($classlist->{$student});
                   3759:                     next;
                   3760:                 }
                   3761:             }
                   3762:             $studentcount ++;
                   3763:         }
                   3764:     }
                   3765:     if (!$studentcount) {
1.101     raeburn  3766:         if ($crstype eq 'Community') {
                   3767:             $r->print(&mt('There are no members to drop.'));
                   3768:         } else {
                   3769:             $r->print(&mt('There are no students to drop.'));
                   3770:         }
1.30      raeburn  3771:         return;
                   3772:     }
                   3773:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   3774:                                               $classlist,$keylist,$cdom,$cnum);
                   3775:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   3776:                                        'dom'    => "domain",
                   3777:                                        'sn'     => "student name",
1.101     raeburn  3778:                                        'mn'     => "member name",
1.30      raeburn  3779:                                        'sec'    => "section",
                   3780:                                        'start'  => "start date",
                   3781:                                        'end'    => "end date",
                   3782:                                        'groups' => "active groups",
                   3783:                                       );
1.101     raeburn  3784:     my $nametitle = $lt{'sn'};
                   3785:     if ($crstype eq 'Community') {
                   3786:         $nametitle = $lt{'mn'};
                   3787:     }
1.1       raeburn  3788:     if ($nosort) {
1.17      raeburn  3789:         $r->print(&Apache::loncommon::start_data_table().
                   3790:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3791:         $r->print(<<END);
                   3792:     <th>&nbsp;</th>
                   3793:     <th>$lt{'usrn'}</th>
                   3794:     <th>$lt{'dom'}</th>
                   3795:     <th>ID</th>
1.101     raeburn  3796:     <th>$nametitle</th>
1.1       raeburn  3797:     <th>$lt{'sec'}</th>
                   3798:     <th>$lt{'start'}</th>
                   3799:     <th>$lt{'end'}</th>
                   3800: END
1.109.2.3  raeburn  3801:         if ($displaygroups) {
                   3802:             $r->print("    <th>$lt{'groups'}</th>\n");
                   3803:         }
1.17      raeburn  3804:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3805:     } else  {
1.17      raeburn  3806:         $r->print(&Apache::loncommon::start_data_table().
                   3807:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3808:         $r->print(<<END);
1.17      raeburn  3809:     <th>&nbsp;</th>
1.1       raeburn  3810:     <th>
1.17      raeburn  3811:        <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1       raeburn  3812:     </th><th>
1.17      raeburn  3813:        <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1       raeburn  3814:     </th><th>
1.17      raeburn  3815:        <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1       raeburn  3816:     </th><th>
1.101     raeburn  3817:        <a href="/adm/createuser?action=$action&sortby=fullname">$nametitle</a>
1.1       raeburn  3818:     </th><th>
1.17      raeburn  3819:        <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1       raeburn  3820:     </th><th>
1.17      raeburn  3821:        <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1       raeburn  3822:     </th><th>
1.17      raeburn  3823:        <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1       raeburn  3824:     </th>
                   3825: END
1.109.2.3  raeburn  3826:        if ($displaygroups) {
                   3827:            $r->print("<th>
                   3828:        <a href=\"/adm/createuser?action=$action&sortby=groups\">$lt{'groups'}</a>
                   3829:     </th>\n");
                   3830:         }
1.17      raeburn  3831:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3832:     }
                   3833:     #
                   3834:     # Sort the students
1.30      raeburn  3835:     my $index  = $indexhash->{$sortby};
                   3836:     my $second = $indexhash->{'username'};
                   3837:     my $third  = $indexhash->{'domain'};
1.1       raeburn  3838:     my @Sorted_Students = sort {
                   3839:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   3840:             ||
                   3841:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   3842:             ||
                   3843:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  3844:         } (keys(%{$classlist}));
1.1       raeburn  3845:     foreach my $student (@Sorted_Students) {
                   3846:         my $error;
                   3847:         my $sdata = $classlist->{$student};
1.30      raeburn  3848:         my $username = $sdata->[$indexhash->{'username'}];
                   3849:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   3850:         my $section  = $sdata->[$indexhash->{'section'}];
                   3851:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   3852:         my $id       = $sdata->[$indexhash->{'id'}];
                   3853:         my $start    = $sdata->[$indexhash->{'start'}];
                   3854:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  3855:         my $groups = $classgroups->{$student};
                   3856:         my $active_groups;
                   3857:         if (ref($groups->{active}) eq 'HASH') {
                   3858:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   3859:         }
                   3860:         if (! defined($start) || $start == 0) {
                   3861:             $start = &mt('none');
                   3862:         } else {
                   3863:             $start = &Apache::lonlocal::locallocaltime($start);
                   3864:         }
                   3865:         if (! defined($end) || $end == 0) {
                   3866:             $end = &mt('none');
                   3867:         } else {
                   3868:             $end = &Apache::lonlocal::locallocaltime($end);
                   3869:         }
1.17      raeburn  3870:         my $studentkey = $student.':'.$section;
1.30      raeburn  3871:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  3872:         #
                   3873:         $r->print(&Apache::loncommon::start_data_table_row());
                   3874:         $r->print(<<"END");
1.86      bisitz   3875:     <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
1.1       raeburn  3876:     <td>$username</td>
                   3877:     <td>$domain</td>
                   3878:     <td>$id</td>
                   3879:     <td>$name</td>
                   3880:     <td>$section</td>
1.29      raeburn  3881:     <td>$start $startitem</td>
1.1       raeburn  3882:     <td>$end</td>
                   3883: END
1.109.2.3  raeburn  3884:         if ($displaygroups) {
                   3885:             $r->print("    <td>$active_groups</td>\n");
                   3886:         }
1.1       raeburn  3887:         $r->print(&Apache::loncommon::end_data_table_row());
                   3888:     }
                   3889:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3890:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  3891:                        'dp'   => "Drop Students",
1.101     raeburn  3892:                        'dm'   => "Drop Members",
1.1       raeburn  3893:                        'ca'   => "check all",
                   3894:                        'ua'   => "uncheck all",
                   3895:                                        );
1.101     raeburn  3896:     my $btn = $lt{'dp'};
                   3897:     if ($crstype eq 'Community') {
                   3898:         $btn = $lt{'dm'}; 
                   3899:     }
1.1       raeburn  3900:     $r->print(<<"END");
1.89      bisitz   3901: </p>
                   3902: <p>
1.86      bisitz   3903: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" /> &nbsp;
                   3904: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
1.89      bisitz   3905: </p>
                   3906: <p>
1.101     raeburn  3907: <input type="submit" value="$btn" />
1.89      bisitz   3908: </p>
1.1       raeburn  3909: END
                   3910:     return;
                   3911: }
                   3912: 
                   3913: #
                   3914: # Print out the initial form to get the file containing a list of users
                   3915: #
                   3916: sub print_first_users_upload_form {
1.109.2.5  raeburn  3917:     my ($r,$context,$noheader,$caller) = @_;
1.1       raeburn  3918:     my $str;
1.86      bisitz   3919:     $str  = '<input type="hidden" name="phase" value="two" />';
1.1       raeburn  3920:     $str .= '<input type="hidden" name="action" value="upload" />';
1.101     raeburn  3921:     $str .= '<input type="hidden" name="state"  value="got_file" />';
1.95      bisitz   3922: 
1.109.2.5  raeburn  3923:     unless ($noheader) {
                   3924:         $str .= '<h3>'.&mt('Upload a file containing information about users').'</h3>'."\n";
                   3925:     }
1.95      bisitz   3926: 
                   3927:     # Excel and CSV Help
1.107     raeburn  3928:     $str .= '<div class="LC_left_float">'
1.95      bisitz   3929:            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   3930:                 &mt("How do I create a users list from a spreadsheet"))
1.107     raeburn  3931:            .'</div><div class="LC_left_float">'."\n"
1.95      bisitz   3932:            .&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   3933:                 &mt("How do I create a CSV file from a spreadsheet"))
1.107     raeburn  3934:            .'</div><br clear="all" />'."\n";
1.95      bisitz   3935:     $str .= &Apache::lonhtmlcommon::start_pick_box()
1.109.2.1  raeburn  3936:            .&Apache::lonhtmlcommon::row_title(&mt('File'))
                   3937:            .&Apache::loncommon::upfile_select_html()
1.95      bisitz   3938:            .&Apache::lonhtmlcommon::row_closure()
1.109.2.5  raeburn  3939:            .&Apache::lonhtmlcommon::row_title(&mt('Ignore First Line, e.g., contains column titles'))
                   3940:            .'<label><input type="radio" name="noFirstLine" id="noFirstLine" value="1" />&nbsp;'.&mt('Yes').'</label>'
                   3941:            .'&nbsp;&nbsp;&nbsp;'
                   3942:            .'<label><input type="radio" name="noFirstLine" id="FirstLineOK" value="0" checked="checked" />&nbsp;'.&mt('No').'</label>'
1.95      bisitz   3943:            .&Apache::lonhtmlcommon::row_closure(1)
                   3944:            .&Apache::lonhtmlcommon::end_pick_box();
                   3945: 
                   3946:     $str .= '<p>'
1.109.2.7  raeburn  3947:            .'<input type="hidden" name="context" value="'.$caller.'" />' 
1.95      bisitz   3948:            .'<input type="submit" name="fileupload" value="'.&mt('Next').'" />'
                   3949:            .'</p>';
                   3950: 
1.1       raeburn  3951:     $r->print($str);
                   3952:     return;
                   3953: }
                   3954: 
                   3955: # ================================================= Drop/Add from uploaded file
                   3956: sub upfile_drop_add {
1.21      raeburn  3957:     my ($r,$context,$permission) = @_;
1.1       raeburn  3958:     &Apache::loncommon::load_tmp_file($r);
                   3959:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   3960:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   3961:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   3962:     my %fields=();
                   3963:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   3964:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   3965:             if ($env{'form.f'.$i} ne 'none') {
                   3966:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   3967:             }
                   3968:         } else {
                   3969:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   3970:         }
                   3971:     }
1.109.2.7  raeburn  3972:     if (($env{'form.fullup'} ne 'yes') && ($env{'form.context'} ne 'requestcrs')) {
1.29      raeburn  3973:         $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   3974:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
                   3975:     }
1.1       raeburn  3976:     #
                   3977:     # Store the field choices away
                   3978:     foreach my $field (qw/username names
1.57      raeburn  3979:                        fname mname lname gen id sec ipwd email role domain/) {
1.1       raeburn  3980:         $env{'form.'.$field.'_choice'}=$fields{$field};
                   3981:     }
                   3982:     &Apache::loncommon::store_course_settings('enrollment_upload',
                   3983:                                               { 'username_choice' => 'scalar',
                   3984:                                                 'names_choice' => 'scalar',
                   3985:                                                 'fname_choice' => 'scalar',
                   3986:                                                 'mname_choice' => 'scalar',
                   3987:                                                 'lname_choice' => 'scalar',
                   3988:                                                 'gen_choice' => 'scalar',
                   3989:                                                 'id_choice' => 'scalar',
                   3990:                                                 'sec_choice' => 'scalar',
                   3991:                                                 'ipwd_choice' => 'scalar',
                   3992:                                                 'email_choice' => 'scalar',
1.57      raeburn  3993:                                                 'role_choice'  => 'scalar',
1.84      raeburn  3994:                                                 'domain_choice' => 'scalar',
                   3995:                                                 'inststatus_choice' => 'scalar'});
1.1       raeburn  3996:     #
1.101     raeburn  3997:     my ($cid,$crstype,$setting);
                   3998:     if ($context eq 'domain') {
                   3999:         $setting = $env{'form.roleaction'};
                   4000:     }
                   4001:     if ($env{'request.course.id'} ne '') {
                   4002:         $cid = $env{'request.course.id'};
                   4003:         $crstype = &Apache::loncommon::course_type();
                   4004:     } elsif ($setting eq 'course') {
                   4005:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   4006:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
                   4007:             $crstype = &Apache::loncommon::course_type($cid);
                   4008:         }
                   4009:     }
1.109.2.5  raeburn  4010:     my ($startdate,$enddate);
1.109.2.7  raeburn  4011:     if ($env{'form.context'} eq 'requestcrs') {
1.109.2.5  raeburn  4012:         $startdate = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   4013:         $enddate = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   4014:     } else {
                   4015:         ($startdate,$enddate) = &get_dates_from_form();
                   4016:         if ($env{'form.makedatesdefault'}) {
                   4017:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
                   4018:         }
1.1       raeburn  4019:     }
                   4020:     # Determine domain and desired host (home server)
1.57      raeburn  4021:     my $defdom=$env{'request.role.domain'};
                   4022:     my $domain;
                   4023:     if ($env{'form.defaultdomain'} ne '') {
                   4024:         $domain = $env{'form.defaultdomain'};
                   4025:     } else {
                   4026:         $domain = $defdom;
                   4027:     }
1.1       raeburn  4028:     my $desiredhost = $env{'form.lcserver'};
                   4029:     if (lc($desiredhost) eq 'default') {
                   4030:         $desiredhost = undef;
                   4031:     } else {
1.57      raeburn  4032:         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
1.1       raeburn  4033:         if (! exists($home_servers{$desiredhost})) {
                   4034:             $r->print('<span class="LC_error">'.&mt('Error').
                   4035:                       &mt('Invalid home server specified').'</span>');
                   4036:             $r->print(&Apache::loncommon::end_page());
                   4037:             return;
                   4038:         }
                   4039:     }
                   4040:     # Determine authentication mechanism
                   4041:     my $changeauth;
                   4042:     if ($context eq 'domain') {
                   4043:         $changeauth = $env{'form.changeauth'};
                   4044:     }
                   4045:     my $amode  = '';
                   4046:     my $genpwd = '';
                   4047:     if ($env{'form.login'} eq 'krb') {
                   4048:         $amode='krb';
                   4049:         $amode.=$env{'form.krbver'};
                   4050:         $genpwd=$env{'form.krbarg'};
                   4051:     } elsif ($env{'form.login'} eq 'int') {
                   4052:         $amode='internal';
                   4053:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   4054:             $genpwd=$env{'form.intarg'};
                   4055:         }
                   4056:     } elsif ($env{'form.login'} eq 'loc') {
                   4057:         $amode='localauth';
                   4058:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   4059:             $genpwd=$env{'form.locarg'};
                   4060:         }
                   4061:     }
                   4062:     if ($amode =~ /^krb/) {
                   4063:         if (! defined($genpwd) || $genpwd eq '') {
                   4064:             $r->print('<span class="Error">'.
                   4065:                       &mt('Unable to enroll users').' '.
                   4066:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   4067:             $amode = ''; # This causes the loop below to be skipped
                   4068:         }
                   4069:     }
1.101     raeburn  4070:     my ($defaultsec,$defaultrole);
1.1       raeburn  4071:     if ($context eq 'domain') {
                   4072:         if ($setting eq 'domain') {
                   4073:             $defaultrole = $env{'form.defaultrole'};
                   4074:         } elsif ($setting eq 'course') {
                   4075:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  4076:             $defaultsec = $env{'form.sections'};
1.1       raeburn  4077:         }  
1.13      raeburn  4078:     } elsif ($context eq 'author') {
1.1       raeburn  4079:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  4080:     } elsif ($context eq 'course') {
                   4081:         $defaultrole = $env{'form.defaultrole'};
                   4082:         $defaultsec = $env{'form.sections'};
1.1       raeburn  4083:     }
1.27      raeburn  4084:     # Check to see if user information can be changed
                   4085:     my @userinfo = ('firstname','middlename','lastname','generation',
                   4086:                     'permanentemail','id');
                   4087:     my %canmodify;
                   4088:     if (&Apache::lonnet::allowed('mau',$domain)) {
1.84      raeburn  4089:         push(@userinfo,'inststatus');
1.27      raeburn  4090:         foreach my $field (@userinfo) {
                   4091:             $canmodify{$field} = 1;
                   4092:         }
                   4093:     }
                   4094:     my (%userlist,%modifiable_fields,@poss_roles);
                   4095:     my $secidx = &Apache::loncoursedata::CL_SECTION();
1.102     raeburn  4096:     my @courseroles = &roles_by_context('course',1,$crstype);
1.27      raeburn  4097:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   4098:         if ($context eq 'course' || $context eq 'author') {
1.101     raeburn  4099:             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
1.27      raeburn  4100:             my @statuses = ('active','future');
                   4101:             my ($indexhash,$keylist) = &make_keylist_array();
                   4102:             my %info;
                   4103:             foreach my $role (@poss_roles) {
                   4104:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   4105:                                                         \@userinfo,[$role]);
                   4106:             }
                   4107:             if ($context eq 'course') {
                   4108:                 my ($cnum,$cdom) = &get_course_identity();
                   4109:                 my $roster = &Apache::loncoursedata::get_classlist();
1.66      raeburn  4110:                 if (ref($roster) eq 'HASH') {
                   4111:                     %userlist = %{$roster};
                   4112:                 }
1.27      raeburn  4113:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   4114:                                                          \@statuses,\@poss_roles);
                   4115:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   4116:                                 \%advrolehash,$permission);
                   4117:             } elsif ($context eq 'author') {
                   4118:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   4119:                                                   \@statuses,\@poss_roles);
                   4120:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   4121:                              \%cstr_roles,$permission);
                   4122: 
                   4123:             }
                   4124:         }
1.1       raeburn  4125:     }
                   4126:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   4127:         && ($amode ne '')) {
                   4128:         #######################################
                   4129:         ##         Add/Modify Users          ##
                   4130:         #######################################
                   4131:         if ($context eq 'course') {
                   4132:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  4133:         } elsif ($context eq 'author') {
1.1       raeburn  4134:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   4135:         } else {
                   4136:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   4137:         }
1.87      bisitz   4138:         $r->rflush;
                   4139: 
1.1       raeburn  4140:         my %counts = (
                   4141:                        user => 0,
                   4142:                        auth => 0,
                   4143:                        role => 0,
                   4144:                      );
                   4145:         my $flushc=0;
                   4146:         my %student=();
1.42      raeburn  4147:         my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
1.1       raeburn  4148:         my %userchg;
1.27      raeburn  4149:         if ($context eq 'course' || $setting eq 'course') {
                   4150:             if ($context eq 'course') {
                   4151:                 # Get information about course groups
                   4152:                 %curr_groups = &Apache::longroup::coursegroups();
                   4153:             } elsif ($setting eq 'course') {
                   4154:                 if ($cid) {
                   4155:                     %curr_groups =
                   4156:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   4157:                                                         $env{'form.dccourse'});
                   4158:                 }
                   4159:             }
                   4160:             # determine section number
                   4161:             if ($defaultsec =~ /,/) {
                   4162:                 push(@sections,split(/,/,$defaultsec));
                   4163:             } else {
                   4164:                 push(@sections,$defaultsec);
                   4165:             }
                   4166:             # remove non alphanumeric values from section
                   4167:             foreach my $item (@sections) {
                   4168:                 $item =~ s/\W//g;
                   4169:                 if ($item eq "none" || $item eq 'all') {
                   4170:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   4171:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   4172:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   4173:                 } elsif ($item ne '') {
                   4174:                     push(@cleansec,$item);
                   4175:                 }
                   4176:             }
                   4177:             if ($defaultwarn) {
                   4178:                 $r->print($defaultwarn.'<br />');
                   4179:             }
                   4180:             if ($groupwarn) {
                   4181:                 $r->print($groupwarn.'<br />');
                   4182:             }
1.1       raeburn  4183:         }
1.109.2.5  raeburn  4184:         my (%curr_rules,%got_rules,%alerts,%cancreate);
1.104     raeburn  4185:         my %customroles = &my_custom_roles($crstype);
1.101     raeburn  4186:         my @permitted_roles = 
1.109.2.6  raeburn  4187:             &roles_on_upload($context,$setting,$crstype,%customroles);
1.109.2.5  raeburn  4188:         my %longtypes = &Apache::lonlocal::texthash(
                   4189:                             official   => 'Institutional',
                   4190:                             unofficial => 'Non-institutional',
                   4191:                         );
                   4192:         map { $cancreate{$_} = &can_create_user($domain,$context,$_); } keys(%longtypes);
1.1       raeburn  4193:         # Get new users list
1.27      raeburn  4194:         foreach my $line (@userdata) {
1.42      raeburn  4195:             my @secs;
1.27      raeburn  4196:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4197:             # Determine user name
1.109.2.9! raeburn  4198:             $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4199:             unless (($entries{$fields{'username'}} eq '') ||
                   4200:                     (!defined($entries{$fields{'username'}}))) {
                   4201:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   4202:                 if (defined($fields{'names'})) {
                   4203:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   4204:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   4205:                 } else {
                   4206:                     if (defined($fields{'fname'})) {
                   4207:                         $fname=$entries{$fields{'fname'}};
                   4208:                     }
                   4209:                     if (defined($fields{'mname'})) {
                   4210:                         $mname=$entries{$fields{'mname'}};
                   4211:                     }
                   4212:                     if (defined($fields{'lname'})) {
                   4213:                         $lname=$entries{$fields{'lname'}};
                   4214:                     }
                   4215:                     if (defined($fields{'gen'})) {
                   4216:                         $gen=$entries{$fields{'gen'}};
                   4217:                     }
                   4218:                 }
1.109.2.9! raeburn  4219: 
1.1       raeburn  4220:                 if ($entries{$fields{'username'}}
                   4221:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
1.109.2.9! raeburn  4222:                     my $nowhitespace;
        !          4223:                     if ($entries{$fields{'username'}} =~ /\s/) {
        !          4224:                         $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
        !          4225:                     }
1.1       raeburn  4226:                     $r->print('<br />'.
1.74      bisitz   4227:       &mt('[_1]: Unacceptable username for user [_2] [_3] [_4] [_5]',
1.109.2.9! raeburn  4228:           '<b>'.$entries{$fields{'username'}}.'</b>',$fname,$mname,$lname,$gen).
        !          4229:                               $nowhitespace);
1.27      raeburn  4230:                     next;
1.1       raeburn  4231:                 } else {
1.109.2.9! raeburn  4232:                     $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
1.71      droeschl 4233:                     if ($entries{$fields{'domain'}} 
1.57      raeburn  4234:                         ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
                   4235:                         $r->print('<br />'. '<b>'.$entries{$fields{'domain'}}.
1.77      raeburn  4236:                                   '</b>: '.&mt('Unacceptable domain for user [_2] [_3] [_4] [_5]',$fname,$mname,$lname,$gen));
1.57      raeburn  4237:                         next;
                   4238:                     }
1.5       raeburn  4239:                     my $username = $entries{$fields{'username'}};
1.57      raeburn  4240:                     my $userdomain = $entries{$fields{'domain'}};
                   4241:                     if ($userdomain eq '') {
                   4242:                         $userdomain = $domain;
                   4243:                     }
1.27      raeburn  4244:                     if (defined($fields{'sec'})) {
                   4245:                         if (defined($entries{$fields{'sec'}})) {
1.42      raeburn  4246:                             $entries{$fields{'sec'}} =~ s/\W//g;
1.27      raeburn  4247:                             my $item = $entries{$fields{'sec'}};
                   4248:                             if ($item eq "none" || $item eq 'all') {
1.74      bisitz   4249:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item));
1.27      raeburn  4250:                                 next;
                   4251:                             } elsif (exists($curr_groups{$item})) {
1.74      bisitz   4252:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item).' '.&mt('Section names and group names must be distinct.'));
1.27      raeburn  4253:                                 next;
                   4254:                             } else {
                   4255:                                 push(@secs,$item);
                   4256:                             }
                   4257:                         }
                   4258:                     }
                   4259:                     if ($env{'request.course.sec'} ne '') {
                   4260:                         @secs = ($env{'request.course.sec'});
1.57      raeburn  4261:                         if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
                   4262:                             my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
1.27      raeburn  4263:                             if ($currsec ne $env{'request.course.sec'}) {
1.74      bisitz   4264:                                 $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$secs[0]).'<br />');
1.27      raeburn  4265:                                 if ($currsec eq '') {
                   4266:                                     $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.'));
                   4267: 
                   4268:                                 } else {
                   4269:                                     $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec));
                   4270:                                 }
                   4271:                                 $r->print('<br />'.&mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$secs[0]).'<br />');
                   4272:                                 next;
1.1       raeburn  4273:                             }
                   4274:                         }
1.27      raeburn  4275:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   4276:                         if (@secs == 0) {
                   4277:                             @secs = @cleansec;
1.1       raeburn  4278:                         }
                   4279:                     }
                   4280:                     # determine id number
                   4281:                     my $id='';
                   4282:                     if (defined($fields{'id'})) {
                   4283:                         if (defined($entries{$fields{'id'}})) {
                   4284:                             $id=$entries{$fields{'id'}};
                   4285:                         }
                   4286:                         $id=~tr/A-Z/a-z/;
                   4287:                     }
                   4288:                     # determine email address
                   4289:                     my $email='';
                   4290:                     if (defined($fields{'email'})) {
1.109.2.9! raeburn  4291:                         $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4292:                         if (defined($entries{$fields{'email'}})) {
                   4293:                             $email=$entries{$fields{'email'}};
1.84      raeburn  4294:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   4295:                         }
                   4296:                     }
                   4297:                     # determine affiliation
                   4298:                     my $inststatus='';
                   4299:                     if (defined($fields{'inststatus'})) {
                   4300:                         if (defined($entries{$fields{'inststatus'}})) {
                   4301:                             $inststatus=$entries{$fields{'inststatus'}};
                   4302:                         }
1.1       raeburn  4303:                     }
                   4304:                     # determine user password
                   4305:                     my $password = $genpwd;
                   4306:                     if (defined($fields{'ipwd'})) {
                   4307:                         if ($entries{$fields{'ipwd'}}) {
                   4308:                             $password=$entries{$fields{'ipwd'}};
                   4309:                         }
                   4310:                     }
                   4311:                     # determine user role
                   4312:                     my $role = '';
                   4313:                     if (defined($fields{'role'})) {
                   4314:                         if ($entries{$fields{'role'}}) {
1.42      raeburn  4315:                             $entries{$fields{'role'}}  =~ s/(\s+$|^\s+)//g;
                   4316:                             if ($entries{$fields{'role'}} ne '') {
                   4317:                                 if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
                   4318:                                     $role = $entries{$fields{'role'}};
1.27      raeburn  4319:                                 }
                   4320:                             }
                   4321:                             if ($role eq '') {
                   4322:                                 my $rolestr = join(', ',@permitted_roles);
1.74      bisitz   4323:                                 $r->print('<br />'
                   4324:                                          .&mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
                   4325:                                              ,'<b>'.$entries{$fields{'username'}}.'</b>'
                   4326:                                              ,$entries{$fields{'role'}})
                   4327:                                          .'<br />'
                   4328:                                          .&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n"
                   4329:                                 );
1.1       raeburn  4330:                                 next;
                   4331:                             }
                   4332:                         }
                   4333:                     }
                   4334:                     if ($role eq '') {
                   4335:                         $role = $defaultrole;
                   4336:                     }
                   4337:                     # Clean up whitespace
1.109.2.9! raeburn  4338:                     foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
1.1       raeburn  4339:                         $$_ =~ s/(\s+$|^\s+)//g;
                   4340:                     }
1.5       raeburn  4341:                     # check against rules
                   4342:                     my $checkid = 0;
                   4343:                     my $newuser = 0;
                   4344:                     my (%rulematch,%inst_results,%idinst_results);
1.57      raeburn  4345:                     my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
1.5       raeburn  4346:                     if ($uhome eq 'no_host') {
1.109.2.5  raeburn  4347:                         if ($userdomain ne $domain) {
                   4348:                             $r->print('<br />'.
                   4349:                                       &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
                   4350:                                          '<b>'.$username.'</b>',$userdomain).'<br />'.
                   4351:                                       &mt('The user does not already exist, and you may not create a new user in a different domain.')); 
                   4352:                             next;
                   4353:                         }
1.5       raeburn  4354:                         $checkid = 1;
                   4355:                         $newuser = 1;
1.109.2.5  raeburn  4356:                         if ($username =~/^[^\@]+\@[^\@]+$/) {
                   4357:                             if ($email eq '') {
                   4358:                                 $email = $username;
                   4359:                             }
                   4360:                             if (&Apache::loncommon::needs_gci_custom()) {
                   4361:                                 my $lc_email;
                   4362:                                 if ($username eq $email) {
                   4363:                                     $lc_email = lc($email);
                   4364:                                 }
                   4365:                                 my $lc_username = lc($username);
                   4366:                                 if ($lc_username ne $username) {
                   4367:                                     if ($username eq $email) {
                   4368:                                         $email = $lc_username;
                   4369:                                     }
                   4370:                                     $username = $lc_username;
                   4371:                                     $uhome=&Apache::lonnet::homeserver($username,$userdomain);
                   4372:                                     if ($uhome ne 'no_host') {
                   4373:                                         $newuser = 0;
                   4374:                                     }
                   4375:                                 }
                   4376:                             }
                   4377:                         }
                   4378:                     }
1.109.2.8  raeburn  4379:                     my $user = $username.':'.$userdomain;
1.109.2.5  raeburn  4380:                     if ($newuser) {
1.5       raeburn  4381:                         my $checkhash;
                   4382:                         my $checks = { 'username' => 1 };
1.109.2.5  raeburn  4383:                         $checkhash->{$user} = { 'newuser' => 1, };
1.5       raeburn  4384:                         &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4385:                             \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                   4386:                             \%got_rules);
                   4387:                         if (ref($alerts{'username'}) eq 'HASH') {
                   4388:                             if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1.109.2.5  raeburn  4389:                                 if ($alerts{'username'}{$domain}{$username}) {
                   4390:                                     $r->print('<br />'.
                   4391:                                               &mt('[_1]: matches the username format at your institution, but is not known to your directory service.','<b>'.$username.'</b>').'<br />'.
                   4392:                                               &mt('Consequently, the user was not created.'));
                   4393:                                     next;
                   4394:                                 }
1.5       raeburn  4395:                             }
                   4396:                         }
1.109.2.6  raeburn  4397:                         my $usertype = 'unofficial';
                   4398:                         if (ref($rulematch{$user}) eq 'HASH') {
                   4399:                             if ($rulematch{$user}{'username'}) {
                   4400:                                 $usertype = 'official';
                   4401:                             }
                   4402:                         }
                   4403:                         if (!$cancreate{$usertype}) {
                   4404:                             $r->print('<br />'.
                   4405:                                       &mt("[_1]: The user does not exist, and you are not permitted to create users of type: $longtypes{$usertype}.",'<b>'.$username.'</b>'));
                   4406:                             next;
1.109.2.5  raeburn  4407:                         }
1.13      raeburn  4408:                     } else {
1.27      raeburn  4409:                         if ($context eq 'course' || $context eq 'author') {
1.57      raeburn  4410:                             if ($userdomain eq $domain ) {
                   4411:                                 if ($role eq '') {
                   4412:                                     my @checkroles;
                   4413:                                     foreach my $role (@poss_roles) {
                   4414:                                         my $endkey;
                   4415:                                         if ($role ne 'st') {
                   4416:                                             $endkey = ':'.$role;
                   4417:                                         }
                   4418:                                         if (exists($userlist{$username.':'.$userdomain.$endkey})) {
                   4419:                                             if (!grep(/^\Q$role\E$/,@checkroles)) {
                   4420:                                                 push(@checkroles,$role);
                   4421:                                             }
                   4422:                                         }
1.27      raeburn  4423:                                     }
1.57      raeburn  4424:                                     if (@checkroles > 0) {
                   4425:                                         %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
1.27      raeburn  4426:                                     }
1.57      raeburn  4427:                                 } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   4428:                                     %canmodify = %{$modifiable_fields{$role}};
1.27      raeburn  4429:                                 }
                   4430:                             }
1.57      raeburn  4431:                             my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
1.84      raeburn  4432:                             for (my $i=0; $i<@newinfo; $i++) {
1.57      raeburn  4433:                                 if (${$newinfo[$i]} ne '') {
                   4434:                                     if (!$canmodify{$userinfo[$i]}) {
                   4435:                                         ${$newinfo[$i]} = '';
                   4436:                                     }
1.27      raeburn  4437:                                 }
                   4438:                             }
                   4439:                         }
1.5       raeburn  4440:                     }
                   4441:                     if ($id ne '') {
                   4442:                         if (!$newuser) {
1.57      raeburn  4443:                             my %idhash = &Apache::lonnet::idrget($userdomain,($username));
1.5       raeburn  4444:                             if ($idhash{$username} ne $id) {
                   4445:                                 $checkid = 1;
                   4446:                             }
                   4447:                         }
                   4448:                         if ($checkid) {
                   4449:                             my $checkhash;
                   4450:                             my $checks = { 'id' => 1 };
1.57      raeburn  4451:                             $checkhash->{$username.':'.$userdomain} = { 'newuser' => $newuser,
1.5       raeburn  4452:                                                                     'id'  => $id };
                   4453:                             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   4454:                                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
                   4455:                                 \%got_rules);
                   4456:                             if (ref($alerts{'id'}) eq 'HASH') {
1.57      raeburn  4457:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
1.109.2.5  raeburn  4458:                                     if ($alerts{'id'}{$userdomain}{$id}) {
                   4459:                                         $r->print(&mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is found by your directory service.',
                   4460:                                                   '<b>'.$username.'</b>').'<br />'.
                   4461:                                                   &mt('Consequently, the user was not created.'));
                   4462:                                         next;
                   4463:                                     }
1.5       raeburn  4464:                                 }
                   4465:                             }
                   4466:                         }
                   4467:                     }
1.1       raeburn  4468:                     if ($password || $env{'form.login'} eq 'loc') {
1.27      raeburn  4469:                         my $multiple = 0;
                   4470:                         my ($userresult,$authresult,$roleresult,$idresult);
                   4471:                         my (%userres,%authres,%roleres,%idres);
1.42      raeburn  4472:                         my $singlesec = '';
1.1       raeburn  4473:                         if ($role eq 'st') {
1.27      raeburn  4474:                             my $sec;
1.42      raeburn  4475:                             if (@secs > 0) {
                   4476:                                 $sec = $secs[0];
1.27      raeburn  4477:                             }
1.57      raeburn  4478:                             &modifystudent($userdomain,$username,$cid,$sec,
1.52      raeburn  4479:                                            $desiredhost,$context);
1.42      raeburn  4480:                             $roleresult =
                   4481:                                 &Apache::lonnet::modifystudent
1.57      raeburn  4482:                                     ($userdomain,$username,$id,$amode,$password,
1.42      raeburn  4483:                                      $fname,$mname,$lname,$gen,$sec,$enddate,
                   4484:                                      $startdate,$env{'form.forceid'},
1.52      raeburn  4485:                                      $desiredhost,$email,'manual','',$cid,
1.84      raeburn  4486:                                      '',$context,$inststatus);
1.42      raeburn  4487:                             $userresult = $roleresult;
1.1       raeburn  4488:                         } else {
1.42      raeburn  4489:                             if ($role ne '') { 
                   4490:                                 if ($context eq 'course' || $setting eq 'course') {
                   4491:                                     if ($customroles{$role}) {
                   4492:                                         $role = 'cr_'.$env{'user.domain'}.'_'.
                   4493:                                                 $env{'user.name'}.'_'.$role;
                   4494:                                     }
1.103     raeburn  4495:                                     if (($role ne 'cc') && ($role ne 'co')) { 
1.42      raeburn  4496:                                         if (@secs > 1) {
                   4497:                                             $multiple = 1;
                   4498:                                             foreach my $sec (@secs) {
                   4499:                                                 ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   4500:                                                 &modifyuserrole($context,$setting,
1.57      raeburn  4501:                                                     $changeauth,$cid,$userdomain,$username,
1.42      raeburn  4502:                                                     $id,$amode,$password,$fname,
                   4503:                                                     $mname,$lname,$gen,$sec,
                   4504:                                                     $env{'form.forceid'},$desiredhost,
                   4505:                                                     $email,$role,$enddate,
1.84      raeburn  4506:                                                     $startdate,$checkid,$inststatus);
1.42      raeburn  4507:                                             }
                   4508:                                         } elsif (@secs > 0) {
                   4509:                                             $singlesec = $secs[0];
                   4510:                                         }
1.27      raeburn  4511:                                     }
                   4512:                                 }
                   4513:                             }
                   4514:                             if (!$multiple) {
1.28      raeburn  4515:                                 ($userresult,$authresult,$roleresult,$idresult) = 
1.27      raeburn  4516:                                     &modifyuserrole($context,$setting,
1.57      raeburn  4517:                                                     $changeauth,$cid,$userdomain,$username, 
1.42      raeburn  4518:                                                     $id,$amode,$password,$fname,
                   4519:                                                     $mname,$lname,$gen,$singlesec,
                   4520:                                                     $env{'form.forceid'},$desiredhost,
1.84      raeburn  4521:                                                     $email,$role,$enddate,$startdate,
                   4522:                                                     $checkid,$inststatus);
1.27      raeburn  4523:                             }
                   4524:                         }
                   4525:                         if ($multiple) {
                   4526:                             foreach my $sec (sort(keys(%userres))) {
1.42      raeburn  4527:                                 $flushc =
1.27      raeburn  4528:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   4529:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
1.57      raeburn  4530:                                                     $username,$userdomain,\%userchg);
1.27      raeburn  4531: 
                   4532:                             }
                   4533:                         } else {
                   4534:                             $flushc = 
                   4535:                                 &user_change_result($r,$userresult,$authresult,
1.28      raeburn  4536:                                                     $roleresult,$idresult,\%counts,$flushc,
1.57      raeburn  4537:                                                     $username,$userdomain,\%userchg);
1.1       raeburn  4538:                         }
                   4539:                     } else {
                   4540:                         if ($context eq 'course') {
                   4541:                             $r->print('<br />'. 
1.74      bisitz   4542:       &mt('[_1]: Unable to enroll. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4543:                                      );
1.13      raeburn  4544:                         } elsif ($context eq 'author') {
1.1       raeburn  4545:                             $r->print('<br />'.
1.74      bisitz   4546:       &mt('[_1]: Unable to add co-author. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4547:                                      );
                   4548:                         } else {
                   4549:                             $r->print('<br />'.
1.74      bisitz   4550:       &mt('[_1]: Unable to add user. No password specified.','<b>'.$username.'</b>')
1.1       raeburn  4551:                                      );
                   4552:                         }
                   4553:                     }
                   4554:                 }
                   4555:             }
                   4556:         } # end of foreach (@userdata)
                   4557:         # Flush the course logs so reverse user roles immediately updated
1.5       raeburn  4558:         &Apache::lonnet::flushcourselogs();
1.29      raeburn  4559:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  4560:                   "</p>\n");
                   4561:         if ($counts{'role'} > 0) {
1.109.2.5  raeburn  4562:             if (&Apache::loncommon::needs_gci_custom()) {
                   4563:                 $r->print("<p>\n".
                   4564:                           &mt('[quant,_1,student] enrolled in Concept Test.',$counts{'role'}).
                   4565:                          "</p>\n");
                   4566:             } else {
                   4567:                 $r->print("<p>\n".
                   4568:                           &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.&mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.')."</p>\n");
                   4569:             }
1.29      raeburn  4570:         } else {
1.109.2.5  raeburn  4571:             if (&Apache::loncommon::needs_gci_custom()) {
                   4572:                 $r->print('<p>'.&mt('No students enrolled').'</p>');
                   4573:             } else {
                   4574:                 $r->print('<p>'.&mt('No roles added').'</p>');
                   4575:             }
1.1       raeburn  4576:         }
                   4577:         if ($counts{'auth'} > 0) {
                   4578:             $r->print("<p>\n".
                   4579:                       &mt('Authentication changed for [_1] existing users.',
                   4580:                           $counts{'auth'})."</p>\n");
                   4581:         }
1.13      raeburn  4582:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1       raeburn  4583:         #####################################
1.29      raeburn  4584:         # Display list of students to drop  #
1.1       raeburn  4585:         #####################################
                   4586:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  4587:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  4588:             #  Get current classlist
1.30      raeburn  4589:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4590:             if (! defined($classlist)) {
1.29      raeburn  4591:                 $r->print('<form name="studentform" method="post" action="/adm/createuser" />'.
                   4592:                           '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                   4593:                           &mt('There are no students with current/future access to the course.').
                   4594:                           '</form>'."\n");
1.66      raeburn  4595:             } elsif (ref($classlist) eq 'HASH') {
1.1       raeburn  4596:                 # Remove the students we just added from the list of students.
1.30      raeburn  4597:                 foreach my $line (@userdata) {
                   4598:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4599:                     unless (($entries{$fields{'username'}} eq '') ||
                   4600:                             (!defined($entries{$fields{'username'}}))) {
                   4601:                         delete($classlist->{$entries{$fields{'username'}}.
                   4602:                                                 ':'.$domain});
                   4603:                     }
                   4604:                 }
                   4605:                 # Print out list of dropped students.
1.30      raeburn  4606:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  4607:             }
                   4608:         }
                   4609:     } # end of unless
1.29      raeburn  4610:     if ($env{'form.fullup'} ne 'yes') {
                   4611:         $r->print('</form>');
                   4612:     }
1.1       raeburn  4613: }
                   4614: 
1.13      raeburn  4615: sub print_namespacing_alerts {
                   4616:     my ($domain,$alerts,$curr_rules) = @_;
                   4617:     my $output;
                   4618:     if (ref($alerts) eq 'HASH') {
                   4619:         if (keys(%{$alerts}) > 0) {
                   4620:             if (ref($alerts->{'username'}) eq 'HASH') {
                   4621:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   4622:                     my $count;
                   4623:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   4624:                         $count = keys(%{$alerts->{'username'}{$dom}});
                   4625:                     }
                   4626:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4627:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4628:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4629:                                         'username',$domdesc,$count,'upload');
                   4630:                     }
                   4631:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4632:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   4633:                                    'username');
                   4634:                 }
                   4635:             }
                   4636:             if (ref($alerts->{'id'}) eq 'HASH') {
                   4637:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   4638:                     my $count;
                   4639:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   4640:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   4641:                     }
                   4642:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   4643:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   4644:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   4645:                                               'id',$domdesc,$count,'upload');
                   4646:                     }
                   4647:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   4648:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   4649:                 }
                   4650:             }
                   4651:         }
                   4652:     }
                   4653: }
                   4654: 
1.1       raeburn  4655: sub user_change_result {
1.29      raeburn  4656:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
1.57      raeburn  4657:         $username,$userdomain,$userchg) = @_;
1.1       raeburn  4658:     my $okresult = 0;
                   4659:     if ($userresult ne 'ok') {
                   4660:         if ($userresult =~ /^error:(.+)$/) {
                   4661:             my $error = $1;
                   4662:             $r->print('<br />'.
1.74      bisitz   4663:                   &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4664:         }
                   4665:     } else {
                   4666:         $counts->{'user'} ++;
                   4667:         $okresult = 1;
                   4668:     }
                   4669:     if ($authresult ne 'ok') {
                   4670:         if ($authresult =~ /^error:(.+)$/) {
                   4671:             my $error = $1;
                   4672:             $r->print('<br />'.
1.74      bisitz   4673:                   &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4674:         } 
                   4675:     } else {
                   4676:         $counts->{'auth'} ++;
                   4677:         $okresult = 1;
                   4678:     }
                   4679:     if ($roleresult ne 'ok') {
                   4680:         if ($roleresult =~ /^error:(.+)$/) {
                   4681:             my $error = $1;
                   4682:             $r->print('<br />'.
1.74      bisitz   4683:                   &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  4684:         }
                   4685:     } else {
                   4686:         $counts->{'role'} ++;
                   4687:         $okresult = 1;
                   4688:     }
                   4689:     if ($okresult) {
                   4690:         $flushc++;
1.57      raeburn  4691:         $userchg->{$username.':'.$userdomain}=1;
1.1       raeburn  4692:         $r->print('. ');
                   4693:         if ($flushc>15) {
                   4694:             $r->rflush;
                   4695:             $flushc=0;
                   4696:         }
                   4697:     }
1.29      raeburn  4698:     if ($idresult) {
                   4699:         $r->print($idresult);
                   4700:     }
1.1       raeburn  4701:     return $flushc;
                   4702: }
                   4703: 
                   4704: # ========================================================= Menu Phase Two Drop
1.17      raeburn  4705: sub print_drop_menu {
1.101     raeburn  4706:     my ($r,$context,$permission,$crstype) = @_;
                   4707:     my $heading;
                   4708:     if ($crstype eq 'Community') {
                   4709:         $heading = &mt("Drop Members");
                   4710:     } else {
                   4711:         $heading = &mt("Drop Students");
                   4712:     }
                   4713:     $r->print('<h3>'.$heading.'</h3>'."\n".
1.17      raeburn  4714:               '<form name="studentform" method="post">'."\n");
1.30      raeburn  4715:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  4716:     if (! defined($classlist)) {
1.101     raeburn  4717:         if ($crstype eq 'Community') {
                   4718:             $r->print(&mt('There are no members currently enrolled.')."\n");
                   4719:         } else {
                   4720:             $r->print(&mt('There are no students currently enrolled.')."\n");
                   4721:         }
1.30      raeburn  4722:     } else {
1.101     raeburn  4723:         &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
1.1       raeburn  4724:     }
1.17      raeburn  4725:     $r->print('</form>'. &Apache::loncommon::end_page());
1.1       raeburn  4726:     return;
                   4727: }
                   4728: 
                   4729: # ================================================================== Phase four
                   4730: 
1.11      raeburn  4731: sub update_user_list {
                   4732:     my ($r,$context,$setting,$choice) = @_;
                   4733:     my $now = time;
1.1       raeburn  4734:     my $count=0;
1.101     raeburn  4735:     my $crstype;
                   4736:     if ($context eq 'course') {
                   4737:         $crstype = &Apache::loncommon::course_type();
                   4738:     }
1.11      raeburn  4739:     my @changelist;
1.29      raeburn  4740:     if ($choice eq 'drop') {
                   4741:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   4742:     } else {
1.11      raeburn  4743:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   4744:     }
                   4745:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   4746:                                    'delete'   => 'Deleted',
                   4747:                                    'reenable' => 'Re-enabled',
1.17      raeburn  4748:                                    'activate' => 'Activated',
                   4749:                                    'chgdates' => 'Changed Access Dates for',
                   4750:                                    'chgsec'   => 'Changed section for',
                   4751:                                    'drop'     => 'Dropped',
1.11      raeburn  4752:                                  },
                   4753:                         error => {'revoke'    => 'revoking',
                   4754:                                   'delete'    => 'deleting',
                   4755:                                   'reenable'  => 're-enabling',
                   4756:                                   'activate'  => 'activating',
1.17      raeburn  4757:                                   'chgdates'  => 'changing access dates for',
                   4758:                                   'chgsec'    => 'changing section for',
                   4759:                                   'drop'      => 'dropping',
1.11      raeburn  4760:                                  },
                   4761:                       );
                   4762:     my ($startdate,$enddate);
                   4763:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   4764:         ($startdate,$enddate) = &get_dates_from_form();
                   4765:     }
                   4766:     foreach my $item (@changelist) {
                   4767:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
                   4768:             $scopestem);
1.17      raeburn  4769:         if ($choice eq 'drop') {
                   4770:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   4771:             $role = 'st';
                   4772:             $cid = $env{'request.course.id'};
                   4773:             $scopestem = '/'.$cid;
                   4774:             $scopestem =~s/\_/\//g;
                   4775:             if ($sec eq '') {
                   4776:                 $scope = $scopestem;
                   4777:             } else {
                   4778:                 $scope = $scopestem.'/'.$sec;
                   4779:             }
                   4780:         } elsif ($context eq 'course') {
1.11      raeburn  4781:             ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
                   4782:             $cid = $env{'request.course.id'};
                   4783:             $scopestem = '/'.$cid;
                   4784:             $scopestem =~s/\_/\//g;
                   4785:             if ($sec eq '') {
                   4786:                 $scope = $scopestem;
                   4787:             } else {
                   4788:                 $scope = $scopestem.'/'.$sec;
                   4789:             }
1.13      raeburn  4790:         } elsif ($context eq 'author') {
1.11      raeburn  4791:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   4792:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   4793:         } elsif ($context eq 'domain') {
                   4794:             if ($setting eq 'domain') {
                   4795:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   4796:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  4797:             } elsif ($setting eq 'author') { 
1.11      raeburn  4798:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   4799:             } elsif ($setting eq 'course') {
                   4800:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype) = 
                   4801:                     split(/\:/,$item);
                   4802:                 $scope = '/'.$cid;
                   4803:                 $scope =~s/\_/\//g;
                   4804:                 if ($sec ne '') {
                   4805:                     $scope .= '/'.$sec;
                   4806:                 }
                   4807:             }
                   4808:         }
1.101     raeburn  4809:         my $plrole = &Apache::lonnet::plaintext($role,$crstype);
1.11      raeburn  4810:         my $start = $env{'form.'.$item.'_start'};
                   4811:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  4812:         if ($choice eq 'drop') {
                   4813:             # drop students
                   4814:             $end = $now;
                   4815:             $type = 'manual';
                   4816:             $result =
1.52      raeburn  4817:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.17      raeburn  4818:         } elsif ($choice eq 'revoke') {
                   4819:             # revoke or delete user role
1.11      raeburn  4820:             $end = $now; 
                   4821:             if ($role eq 'st') {
                   4822:                 $result = 
1.52      raeburn  4823:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4824:             } else {
                   4825:                 $result = 
1.52      raeburn  4826:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
                   4827:                                                 '','',$context);
1.11      raeburn  4828:             }
                   4829:         } elsif ($choice eq 'delete') {
                   4830:             if ($role eq 'st') {
1.52      raeburn  4831:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context);
1.29      raeburn  4832:             }
                   4833:             $result =
                   4834:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
1.52      raeburn  4835:                                             $start,1,'',$context);
1.11      raeburn  4836:         } else {
                   4837:             #reenable, activate, change access dates or change section
                   4838:             if ($choice ne 'chgsec') {
                   4839:                 $start = $startdate; 
                   4840:                 $end = $enddate;
                   4841:             }
                   4842:             if ($choice eq 'reenable') {
                   4843:                 if ($role eq 'st') {
1.52      raeburn  4844:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4845:                 } else {
                   4846:                     $result = 
                   4847:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4848:                                                     $now,'','',$context);
1.11      raeburn  4849:                 }
                   4850:             } elsif ($choice eq 'activate') {
                   4851:                 if ($role eq 'st') {
1.52      raeburn  4852:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4853:                 } else {
                   4854:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4855:                                             $now,'','',$context);
1.11      raeburn  4856:                 }
                   4857:             } elsif ($choice eq 'chgdates') {
                   4858:                 if ($role eq 'st') {
1.52      raeburn  4859:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4860:                 } else {
                   4861:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  4862:                                                 $start,'','',$context);
1.11      raeburn  4863:                 }
                   4864:             } elsif ($choice eq 'chgsec') {
                   4865:                 my (@newsecs,$revresult,$nochg,@retained);
1.103     raeburn  4866:                 if (($role ne 'cc') && ($role ne 'co')) {
1.11      raeburn  4867:                     @newsecs = split(/,/,$env{'form.newsecs'});
                   4868:                 }
                   4869:                 # remove existing section if not to be retained.   
                   4870:                 if (!$env{'form.retainsec'}) {
                   4871:                     if ($sec eq '') {
                   4872:                         if (@newsecs == 0) {
                   4873:                             $result = &mt('No change in section assignment (none)');
                   4874:                             $nochg = 1;
1.40      raeburn  4875:                         } else {
                   4876:                             $revresult =
                   4877:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4878:                                                             $scope,$role,
                   4879:                                                             '','',$context);
1.40      raeburn  4880:                         } 
1.11      raeburn  4881:                     } else {
1.28      raeburn  4882:                         if (@newsecs > 0) {
                   4883:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   4884:                                 push(@retained,$sec);
                   4885:                             } else {
                   4886:                                 $revresult =
                   4887:                                     &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4888:                                                                 $scope,$role,
                   4889:                                                                 '','',$context);
1.28      raeburn  4890:                             }
                   4891:                         } else {
1.11      raeburn  4892:                             $revresult =
1.28      raeburn  4893:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  4894:                                                             $scope,$role,
                   4895:                                                             '','',$context);
1.11      raeburn  4896:                         }
                   4897:                     }
                   4898:                 } else {
1.28      raeburn  4899:                     if ($sec eq '') {
                   4900:                         $nochg = 1;
                   4901:                     } else { 
                   4902:                         push(@retained,$sec);
                   4903:                     }
1.11      raeburn  4904:                 }
                   4905:                 # add new sections
                   4906:                 if (@newsecs == 0) {
                   4907:                     if (!$nochg) {
1.28      raeburn  4908:                         if ($role eq 'st') {
                   4909:                             $result = 
1.52      raeburn  4910:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context);
1.28      raeburn  4911:                         } else {
                   4912:                             my $newscope = $scopestem;
1.52      raeburn  4913:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
1.11      raeburn  4914:                         }
                   4915:                     }
                   4916:                 } else {
                   4917:                     foreach my $newsec (@newsecs) { 
                   4918:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   4919:                             if ($role eq 'st') {
1.52      raeburn  4920:                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context);
1.11      raeburn  4921:                             } else {
                   4922:                                 my $newscope = $scopestem;
                   4923:                                 if ($newsec ne '') {
                   4924:                                    $newscope .= '/'.$newsec;
                   4925:                                 }
                   4926:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   4927:                                                         $newscope,$role,$end,$start);
                   4928:                             }
                   4929:                         }
                   4930:                     }
                   4931:                 }
                   4932:             }
                   4933:         }
1.17      raeburn  4934:         my $extent = $scope;
                   4935:         if ($choice eq 'drop' || $context eq 'course') {
                   4936:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   4937:             if ($cdesc) {
                   4938:                 $extent = $cdesc;
                   4939:             }
                   4940:         }
1.1       raeburn  4941:         if ($result eq 'ok' || $result eq 'ok:') {
1.11      raeburn  4942:             $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
1.17      raeburn  4943:                           $plrole,$extent,$uname.':'.$udom).'<br />');
1.1       raeburn  4944:             $count++;
                   4945:         } else {
                   4946:             $r->print(
1.29      raeburn  4947:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]: [_4].",
1.17      raeburn  4948:                     $plrole,$extent,$uname.':'.$udom,$result).'<br />');
1.11      raeburn  4949:         }
                   4950:     }
1.32      raeburn  4951:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  4952:     if ($choice eq 'drop') {
                   4953:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   4954:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   4955:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   4956:     } else {
                   4957:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   4958:             if ($env{'form.'.$item} ne '') {
                   4959:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   4960:                           '" />'."\n");
                   4961:             }
1.32      raeburn  4962:         }
                   4963:     }
1.29      raeburn  4964:     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,no users].",$count).'</b></p>');
1.11      raeburn  4965:     if ($count > 0) {
1.17      raeburn  4966:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.74      bisitz   4967:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
1.11      raeburn  4968:         }
                   4969:         # Flush the course logs so reverse user roles immediately updated
                   4970:         &Apache::lonnet::flushcourselogs();
                   4971:     }
                   4972:     if ($env{'form.makedatesdefault'}) {
                   4973:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.101     raeburn  4974:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  4975:         }
                   4976:     }
1.33      raeburn  4977:     my $linktext = &mt('Display User Lists');
                   4978:     if ($choice eq 'drop') {
                   4979:         $linktext = &mt('Display current class roster');
                   4980:     }
                   4981:     $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
1.1       raeburn  4982: }
                   4983: 
1.8       raeburn  4984: sub classlist_drop {
1.29      raeburn  4985:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  4986:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  4987:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.8       raeburn  4988:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
1.63      raeburn  4989:             my %user;
                   4990:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
1.8       raeburn  4991:             return &mt('Drop from classlist: [_1]',
                   4992:                        '<b>'.$result.'</b>').'<br />';
                   4993:         }
                   4994:     }
                   4995: }
                   4996: 
                   4997: sub active_student_roles {
                   4998:     my ($cnum,$cdom,$uname,$udom) = @_;
                   4999:     my %roles =
                   5000:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   5001:                                       ['future','active'],['st']);
                   5002:     return exists($roles{"$cnum:$cdom:st"});
                   5003: }
                   5004: 
1.1       raeburn  5005: sub section_check_js {
1.8       raeburn  5006:     my $groupslist= &get_groupslist();
1.1       raeburn  5007:     return <<"END";
                   5008: function validate(caller) {
1.9       raeburn  5009:     var groups = new Array($groupslist);
1.1       raeburn  5010:     var secname = caller.value;
                   5011:     if ((secname == 'all') || (secname == 'none')) {
                   5012:         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
                   5013:         return 'error';
                   5014:     }
                   5015:     if (secname != '') {
                   5016:         for (var k=0; k<groups.length; k++) {
                   5017:             if (secname == groups[k]) {
                   5018:                 alert("'"+secname+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
                   5019:                 return 'error';
                   5020:             }
                   5021:         }
                   5022:     }
                   5023:     return 'ok';
                   5024: }
                   5025: END
                   5026: }
                   5027: 
                   5028: sub set_login {
                   5029:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   5030:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5031:     my $response;
                   5032:     my ($authnum,%can_assign) =
                   5033:         &Apache::loncommon::get_assignable_auth($dom);
                   5034:     if ($authnum) {
                   5035:         $response = &Apache::loncommon::start_data_table();
                   5036:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   5037:             $response .= &Apache::loncommon::start_data_table_row().
                   5038:                          '<td>'.$authformkrb.'</td>'.
                   5039:                          &Apache::loncommon::end_data_table_row()."\n";
                   5040:         }
                   5041:         if ($can_assign{'int'}) {
                   5042:             $response .= &Apache::loncommon::start_data_table_row().
                   5043:                          '<td>'.$authformint.'</td>'.
                   5044:                          &Apache::loncommon::end_data_table_row()."\n"
                   5045:         }
                   5046:         if ($can_assign{'loc'}) {
                   5047:             $response .= &Apache::loncommon::start_data_table_row().
                   5048:                          '<td>'.$authformloc.'</td>'.
                   5049:                          &Apache::loncommon::end_data_table_row()."\n";
                   5050:         }
                   5051:         $response .= &Apache::loncommon::end_data_table();
                   5052:     }
                   5053:     return $response;
                   5054: }
                   5055: 
1.8       raeburn  5056: sub course_sections {
1.51      raeburn  5057:     my ($sections_count,$role,$current_sec) = @_;
1.8       raeburn  5058:     my $output = '';
                   5059:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
1.29      raeburn  5060:     my $numsec = scalar(@sections);
1.92      bisitz   5061:     my $is_selected = ' selected="selected"';
1.29      raeburn  5062:     if ($numsec <= 1) {
1.8       raeburn  5063:         $output = '<select name="currsec_'.$role.'" >'."\n".
1.51      raeburn  5064:                   '  <option value="">'.&mt('Select').'</option>'."\n";
                   5065:         if ($current_sec eq 'none') {
                   5066:             $output .=       
                   5067:                   '  <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
                   5068:         } else {
                   5069:             $output .=
1.29      raeburn  5070:                   '  <option value="">'.&mt('No section').'</option>'."\n";
1.51      raeburn  5071:         }
1.29      raeburn  5072:         if ($numsec == 1) {
1.51      raeburn  5073:             if ($current_sec eq $sections[0]) {
                   5074:                 $output .=
                   5075:                   '  <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
                   5076:             } else {
                   5077:                 $output .=  
1.8       raeburn  5078:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.51      raeburn  5079:             }
1.29      raeburn  5080:         }
1.8       raeburn  5081:     } else {
                   5082:         $output = '<select name="currsec_'.$role.'" ';
                   5083:         my $multiple = 4;
                   5084:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  5085:         if ($role eq 'st') {
                   5086:             $output .= '>'."\n".
1.51      raeburn  5087:                        '  <option value="">'.&mt('Select').'</option>'."\n";
                   5088:             if ($current_sec eq 'none') {
                   5089:                 $output .= 
                   5090:                        '  <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
                   5091:             } else {
                   5092:                 $output .=
1.29      raeburn  5093:                        '  <option value="">'.&mt('No section')."</option>\n";
1.51      raeburn  5094:             }
1.29      raeburn  5095:         } else {
                   5096:             $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
                   5097:         }
1.8       raeburn  5098:         foreach my $sec (@sections) {
1.51      raeburn  5099:             if ($current_sec eq $sec) {
                   5100:                 $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
                   5101:             } else {
                   5102:                 $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   5103:             }
1.8       raeburn  5104:         }
                   5105:     }
                   5106:     $output .= '</select>';
                   5107:     return $output;
                   5108: }
                   5109: 
                   5110: sub get_groupslist {
                   5111:     my $groupslist;
                   5112:     my %curr_groups = &Apache::longroup::coursegroups();
                   5113:     if (%curr_groups) {
                   5114:         $groupslist = join('","',sort(keys(%curr_groups)));
                   5115:         $groupslist = '"'.$groupslist.'"';
                   5116:     }
1.11      raeburn  5117:     return $groupslist; 
1.8       raeburn  5118: }
                   5119: 
                   5120: sub setsections_javascript {
1.103     raeburn  5121:     my ($formname,$groupslist,$mode,$checkauth,$crstype) = @_;
1.28      raeburn  5122:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   5123:     if ($mode eq 'upload') {
                   5124:         $checkincluded = 'formname.name == "'.$formname.'"';
                   5125:         $finish = "return 'ok';";
                   5126:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   5127:     } elsif ($formname eq 'cu') {
1.8       raeburn  5128:         $checkincluded = 'formname.elements[i-1].checked == true';
1.37      raeburn  5129:         if ($checkauth) {
                   5130:             $finish = "var authcheck = auth_check();\n".
                   5131:                       "   if (authcheck == 'ok') {\n".
                   5132:                       "       formname.submit();\n".
                   5133:                       "   }\n";
                   5134:         } else {
                   5135:             $finish = 'formname.submit()';
                   5136:         }
1.28      raeburn  5137:         $rolecode = "var match = str.split('_');
                   5138:                 var role = match[3];\n";
                   5139:     } elsif ($formname eq 'enrollstudent') {
                   5140:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  5141:         if ($checkauth) {
                   5142:             $finish = "var authcheck = auth_check();\n".
                   5143:                       "   if (authcheck == 'ok') {\n".
                   5144:                       "       formname.submit();\n".
                   5145:                       "   }\n";
                   5146:         } else {
                   5147:             $finish = 'formname.submit()';
                   5148:         }
1.28      raeburn  5149:         $rolecode = "var match = str.split('_');
                   5150:                 var role = match[1];\n";
1.8       raeburn  5151:     } else {
1.28      raeburn  5152:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  5153:         $finish = "seccheck = 'ok';";
1.28      raeburn  5154:         $rolecode = "var match = str.split('_');
                   5155:                 var role = match[1];\n";
1.11      raeburn  5156:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  5157:     }
                   5158:     my %alerts = &Apache::lonlocal::texthash(
                   5159:                     secd => 'Section designations do not apply to Course Coordinator roles.',
1.103     raeburn  5160:                     sedn => 'Section designations do not apply to Coordinator roles.',
1.8       raeburn  5161:                     accr => 'A course coordinator role will be added with access to all sections.',
1.103     raeburn  5162:                     acor => 'A coordinator role will be added with access to all sections',
1.8       raeburn  5163:                     inea => 'In each course, each user may only have one student role at a time.',
1.109.2.6  raeburn  5164:                     inec => 'In each community, each user may only have on member role at a time.',
1.8       raeburn  5165:                     youh => 'You had selected ',
                   5166:                     secs => 'sections.',
                   5167:                     plmo => 'Please modify your selections so they include no more than one section.',
                   5168:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   5169:                     plch => 'Please choose a different section name.',
                   5170:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   5171:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.11      raeburn  5172:                  );                
1.8       raeburn  5173:     $setsection_js .= <<"ENDSECCODE";
                   5174: 
1.103     raeburn  5175: function setSections(formname,crstype) {
1.8       raeburn  5176:     var re1 = /^currsec_/;
                   5177:     var groups = new Array($groupslist);
                   5178:     for (var i=0;i<formname.elements.length;i++) {
                   5179:         var str = formname.elements[i].name;
                   5180:         var checkcurr = str.match(re1);
                   5181:         if (checkcurr != null) {
                   5182:             if ($checkincluded) {
1.28      raeburn  5183:                 $rolecode
1.103     raeburn  5184:                 if (role == 'cc' || role == 'co') {
                   5185:                     if (role == 'cc') {
                   5186:                         alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   5187:                     } else {
                   5188:                         alert("$alerts{'sedn'}\\n$alerts{'acor'}");
                   5189:                     }
                   5190:                 } else {
1.8       raeburn  5191:                     var sections = '';
                   5192:                     var numsec = 0;
                   5193:                     var sections;
                   5194:                     for (var j=0; j<formname.elements[i].length; j++) {
                   5195:                         if (formname.elements[i].options[j].selected == true ) {
                   5196:                             if (formname.elements[i].options[j].value != "") {
                   5197:                                 if (numsec == 0) {
                   5198:                                     if (formname.elements[i].options[j].value != "") {
                   5199:                                         sections = formname.elements[i].options[j].value;
                   5200:                                         numsec ++;
                   5201:                                     }
                   5202:                                 }
                   5203:                                 else {
                   5204:                                     sections = sections + "," +  formname.elements[i].options[j].value
                   5205:                                     numsec ++;
                   5206:                                 }
                   5207:                             }
                   5208:                         }
                   5209:                     }
                   5210:                     if (numsec > 0) {
                   5211:                         if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
                   5212:                             sections = sections + "," +  formname.elements[i+1].value;
                   5213:                         }
                   5214:                     }
                   5215:                     else {
                   5216:                         sections = formname.elements[i+1].value;
                   5217:                     }
                   5218:                     var newsecs = formname.elements[i+1].value;
                   5219:                     var numsplit;
                   5220:                     if (newsecs != null && newsecs != "") {
                   5221:                         numsplit = newsecs.split(/,/g);
                   5222:                         numsec = numsec + numsplit.length;
                   5223:                     }
                   5224: 
                   5225:                     if ((role == 'st') && (numsec > 1)) {
1.103     raeburn  5226:                         if (crstype == 'Community') {
                   5227:                             alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5228:                         } else {
1.109.2.6  raeburn  5229:                             alert("$alerts{'inec'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
1.103     raeburn  5230:                         }
1.8       raeburn  5231:                         return;
                   5232:                     }
                   5233:                     else {
                   5234:                         if (numsplit != null) {
                   5235:                             for (var j=0; j<numsplit.length; j++) {
                   5236:                                 if ((numsplit[j] == 'all') ||
                   5237:                                     (numsplit[j] == 'none')) {
                   5238:                                     alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
                   5239:                                     return;
                   5240:                                 }
                   5241:                                 for (var k=0; k<groups.length; k++) {
                   5242:                                     if (numsplit[j] == groups[k]) {
                   5243:                                         alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
                   5244:                                         return;
                   5245:                                     }
                   5246:                                 }
                   5247:                             }
                   5248:                         }
                   5249:                         formname.elements[i+2].value = sections;
                   5250:                     }
                   5251:                 }
                   5252:             }
                   5253:         }
                   5254:     }
                   5255:     $finish
                   5256: }
                   5257: ENDSECCODE
1.11      raeburn  5258:     return $setsection_js; 
1.8       raeburn  5259: }
                   5260: 
1.109.2.5  raeburn  5261: sub newsections_javascript {
                   5262:     my %alerts = &Apache::lonlocal::texthash(
                   5263:                     inea => 'In each course, each user may only have one student role at a time.',
                   5264:                     youh => 'You had selected ',
                   5265:                     secs => 'sections.',
                   5266:                     plmo => 'Please modify your selections so they include no more than one section.',
                   5267:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   5268:                     plch => 'Please choose a different section name.',
                   5269:                  );
                   5270:     my $setsection_js = <<"ENDSECCODE";
                   5271: 
                   5272: function setSections(formname) {
                   5273:     var newsecs = formname.newsec.value;
                   5274:     var numsplit = 0;
                   5275:     var numsec = 0;
                   5276:     if (newsecs != null && newsecs != "") {
                   5277:         numsplit = newsecs.split(/,/g);
                   5278:         numsec = numsplit.length;
                   5279:     }
                   5280:     if (numsec > 1) {
                   5281:         alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5282:     } else {
                   5283:         if (numsplit > 0) {
                   5284:             for (var j=0; j<numsplit.length; j++) {
                   5285:                 if ((numsplit[j] == 'all') ||
                   5286:                     (numsplit[j] == 'none')) {
                   5287:                     alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
                   5288:                     return;
                   5289:                 }
                   5290:            }
                   5291:            formname.sections.value = newsecs;
                   5292:        }
                   5293:     }
                   5294:     return 'ok';
                   5295: }
                   5296: 
                   5297: ENDSECCODE
                   5298:     return $setsection_js;
                   5299: }
                   5300: 
1.15      raeburn  5301: sub can_create_user {
                   5302:     my ($dom,$context,$usertype) = @_;
                   5303:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5304:     my $cancreate = 1;
1.28      raeburn  5305:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   5306:         return $cancreate;
                   5307:     }
1.15      raeburn  5308:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   5309:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
1.100     raeburn  5310:             if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
1.15      raeburn  5311:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   5312:                 if ($creation eq 'none') {
                   5313:                     $cancreate = 0;
                   5314:                 } elsif ($creation ne 'any') {
                   5315:                     if (defined($usertype)) {
                   5316:                         if ($creation ne $usertype) {
                   5317:                             $cancreate = 0;
                   5318:                         }
                   5319:                     }
                   5320:                 }
                   5321:             }
                   5322:         }
                   5323:     }
                   5324:     return $cancreate;
                   5325: }
                   5326: 
1.20      raeburn  5327: sub can_modify_userinfo {
                   5328:     my ($context,$dom,$fields,$userroles) = @_;
                   5329:     my %domconfig =
                   5330:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   5331:                                 $dom);
                   5332:     my %canmodify;
                   5333:     if (ref($fields) eq 'ARRAY') {
                   5334:         foreach my $field (@{$fields}) {
                   5335:             $canmodify{$field}  = 0;
                   5336:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   5337:                 $canmodify{$field} = 1;
                   5338:             } else {
                   5339:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   5340:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   5341:                         if (ref($userroles) eq 'ARRAY') {
                   5342:                             foreach my $role (@{$userroles}) {
                   5343:                                 my $testrole;
1.60      raeburn  5344:                                 if ($context eq 'selfcreate') {
                   5345:                                     $testrole = $role;
1.20      raeburn  5346:                                 } else {
1.60      raeburn  5347:                                     if ($role =~ /^cr\//) {
                   5348:                                         $testrole = 'cr';
                   5349:                                     } else {
                   5350:                                         $testrole = $role;
                   5351:                                     }
1.20      raeburn  5352:                                 }
                   5353:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   5354:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   5355:                                         $canmodify{$field} = 1;
                   5356:                                         last;
                   5357:                                     }
                   5358:                                 }
                   5359:                             }
                   5360:                         } else {
                   5361:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   5362:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   5363:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   5364:                                         $canmodify{$field} = 1;
                   5365:                                         last;
                   5366:                                     }
                   5367:                                 }
                   5368:                             }
                   5369:                         }
                   5370:                     }
                   5371:                 } elsif ($context eq 'course') {
                   5372:                     if (ref($userroles) eq 'ARRAY') {
                   5373:                         if (grep(/^st$/,@{$userroles})) {
                   5374:                             $canmodify{$field} = 1;
                   5375:                         }
                   5376:                     } else {
                   5377:                         $canmodify{$field} = 1;
                   5378:                     }
                   5379:                 }
                   5380:             }
                   5381:         }
                   5382:     }
                   5383:     return %canmodify;
                   5384: }
                   5385: 
1.18      raeburn  5386: sub check_usertype {
                   5387:     my ($dom,$uname,$rules) = @_;
                   5388:     my $usertype;
                   5389:     if (ref($rules) eq 'HASH') {
                   5390:         my @user_rules = keys(%{$rules});
                   5391:         if (@user_rules > 0) {
                   5392:             my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   5393:             if (keys(%rule_check) > 0) {
                   5394:                 $usertype = 'unofficial';
                   5395:                 foreach my $item (keys(%rule_check)) {
                   5396:                     if ($rule_check{$item}) {
                   5397:                         $usertype = 'official';
                   5398:                         last;
                   5399:                     }
                   5400:                 }
                   5401:             }
                   5402:         }
                   5403:     }
                   5404:     return $usertype;
                   5405: }
                   5406: 
1.17      raeburn  5407: sub roles_by_context {
1.101     raeburn  5408:     my ($context,$custom,$crstype) = @_;
1.17      raeburn  5409:     my @allroles;
                   5410:     if ($context eq 'course') {
1.99      raeburn  5411:         @allroles = ('st');
                   5412:         if ($env{'request.role'} =~ m{^dc\./}) {
                   5413:             push(@allroles,'ad');
                   5414:         }
1.109.2.2  raeburn  5415:         if (&Apache::loncommon::needs_gci_custom()) {
                   5416:             if ($crstype eq 'Community') {
                   5417:                 push(@allroles,'co');
                   5418:             } else {
                   5419:                 push(@allroles,'cc');
                   5420:             }
1.101     raeburn  5421:         } else {
1.109.2.2  raeburn  5422:             push(@allroles,('ta','ep','in'));
                   5423:             if ($crstype eq 'Community') {
                   5424:                 push(@allroles,'co');
                   5425:             } else {
                   5426:                 push(@allroles,'cc');
                   5427:             }
                   5428:             if ($custom) {
                   5429:                 push(@allroles,'cr');
                   5430:             }
1.17      raeburn  5431:         }
                   5432:     } elsif ($context eq 'author') {
                   5433:         @allroles = ('ca','aa');
                   5434:     } elsif ($context eq 'domain') {
1.99      raeburn  5435:         @allroles = ('li','ad','dg','sc','au','dc');
1.17      raeburn  5436:     }
                   5437:     return @allroles;
                   5438: }
                   5439: 
1.16      raeburn  5440: sub get_permission {
1.101     raeburn  5441:     my ($context,$crstype) = @_;
1.16      raeburn  5442:     my %permission;
                   5443:     if ($context eq 'course') {
1.17      raeburn  5444:         my $custom = 1;
1.101     raeburn  5445:         my @allroles = &roles_by_context($context,$custom,$crstype);
1.17      raeburn  5446:         foreach my $role (@allroles) {
                   5447:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   5448:                 $permission{'cusr'} = 1;
                   5449:                 last;
                   5450:             }
1.16      raeburn  5451:         }
                   5452:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   5453:             $permission{'custom'} = 1;
                   5454:         }
                   5455:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   5456:             $permission{'view'} = 1;
                   5457:         }
                   5458:         if (!$permission{'view'}) {
                   5459:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5460:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   5461:             if ($permission{'view'}) {
                   5462:                 $permission{'view_section'} = $env{'request.course.sec'};
                   5463:             }
                   5464:         }
1.17      raeburn  5465:         if (!$permission{'cusr'}) {
                   5466:             if ($env{'request.course.sec'} ne '') {
                   5467:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   5468:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   5469:                 if ($permission{'cusr'}) {
                   5470:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   5471:                 }
                   5472:             }
                   5473:         }
1.16      raeburn  5474:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   5475:             $permission{'grp_manage'} = 1;
                   5476:         }
                   5477:     } elsif ($context eq 'author') {
                   5478:         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
                   5479:         $permission{'view'} = $permission{'cusr'};
                   5480:     } else {
1.17      raeburn  5481:         my @allroles = &roles_by_context($context);
                   5482:         foreach my $role (@allroles) {
1.28      raeburn  5483:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   5484:                 $permission{'cusr'} = 1;
1.17      raeburn  5485:                 last;
                   5486:             }
                   5487:         }
                   5488:         if (!$permission{'cusr'}) {
                   5489:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   5490:                 $permission{'cusr'} = 1;
                   5491:             }
1.16      raeburn  5492:         }
                   5493:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   5494:             $permission{'custom'} = 1;
                   5495:         }
                   5496:         $permission{'view'} = $permission{'cusr'};
                   5497:     }
                   5498:     my $allowed = 0;
                   5499:     foreach my $perm (values(%permission)) {
                   5500:         if ($perm) { $allowed=1; last; }
                   5501:     }
                   5502:     return (\%permission,$allowed);
                   5503: }
                   5504: 
                   5505: # ==================================================== Figure out author access
                   5506: 
                   5507: sub authorpriv {
                   5508:     my ($auname,$audom)=@_;
                   5509:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   5510:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   5511: }
                   5512: 
1.27      raeburn  5513: sub roles_on_upload {
1.101     raeburn  5514:     my ($context,$setting,$crstype,%customroles) = @_;
1.27      raeburn  5515:     my (@possible_roles,@permitted_roles);
1.101     raeburn  5516:     @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
1.27      raeburn  5517:     foreach my $role (@possible_roles) {
                   5518:         if ($role eq 'cr') {
                   5519:             push(@permitted_roles,keys(%customroles));
                   5520:         } else {
                   5521:             push(@permitted_roles,$role);
                   5522:         }
                   5523:     }
1.42      raeburn  5524:     return @permitted_roles;
1.27      raeburn  5525: }
                   5526: 
1.17      raeburn  5527: sub get_course_identity {
                   5528:     my ($cid) = @_;
                   5529:     my ($cnum,$cdom,$cdesc);
                   5530:     if ($cid eq '') {
                   5531:         $cid = $env{'request.course.id'}
                   5532:     }
                   5533:     if ($cid ne '') {
                   5534:         $cnum = $env{'course.'.$cid.'.num'};
                   5535:         $cdom = $env{'course.'.$cid.'.domain'};
                   5536:         $cdesc = $env{'course.'.$cid.'.description'};
                   5537:         if ($cnum eq '' || $cdom eq '') {
                   5538:             my %coursehash =
                   5539:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   5540:             $cdom = $coursehash{'domain'};
                   5541:             $cnum = $coursehash{'num'};
                   5542:             $cdesc = $coursehash{'description'};
                   5543:         }
                   5544:     }
                   5545:     return ($cnum,$cdom,$cdesc);
                   5546: }
                   5547: 
1.19      raeburn  5548: sub dc_setcourse_js {
1.37      raeburn  5549:     my ($formname,$mode,$context) = @_;
                   5550:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  5551:     my $cctext = &Apache::lonnet::plaintext('cc');
1.103     raeburn  5552:     my $cotext = &Apache::lonnet::plaintext('co');
1.19      raeburn  5553:     my %alerts = &sectioncheck_alerts();
                   5554:     my $role = 'role';
                   5555:     if ($mode eq 'upload') {
                   5556:         $role = 'courserole';
1.37      raeburn  5557:     } else {
                   5558:         $authen_check = &verify_authen($formname,$context);
1.19      raeburn  5559:     }
                   5560:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  5561: $authen_check
                   5562: 
1.19      raeburn  5563: function setCourse() {
                   5564:     var course = document.$formname.dccourse.value;
                   5565:     if (course != "") {
                   5566:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   5567:             alert("$alerts{'curd'}");
                   5568:             return;
                   5569:         }
                   5570:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   5571:         var section="";
                   5572:         var numsections = 0;
                   5573:         var newsecs = new Array();
                   5574:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   5575:             if (document.$formname.currsec.options[i].selected == true ) {
                   5576:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   5577:                     if (numsections == 0) {
                   5578:                         section = document.$formname.currsec.options[i].value
                   5579:                         numsections = 1;
                   5580:                     }
                   5581:                     else {
                   5582:                         section = section + "," +  document.$formname.currsec.options[i].value
                   5583:                         numsections ++;
                   5584:                     }
                   5585:                 }
                   5586:             }
                   5587:         }
                   5588:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   5589:             if (numsections == 0) {
                   5590:                 section = document.$formname.newsec.value
                   5591:             }
                   5592:             else {
                   5593:                 section = section + "," +  document.$formname.newsec.value
                   5594:             }
                   5595:             newsecs = document.$formname.newsec.value.split(/,/g);
                   5596:             numsections = numsections + newsecs.length;
                   5597:         }
                   5598:         if ((userrole == 'st') && (numsections > 1)) {
1.103     raeburn  5599:             if (document.$formname.crstype.value == 'Community') {
                   5600:                 alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5601:             } else {
                   5602:                 alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   5603:             }
1.19      raeburn  5604:             return;
                   5605:         }
                   5606:         for (var j=0; j<newsecs.length; j++) {
                   5607:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   5608:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   5609:                 return;
                   5610:             }
                   5611:             if (document.$formname.groups.value != '') {
                   5612:                 var groups = document.$formname.groups.value.split(/,/g);
                   5613:                 for (var k=0; k<groups.length; k++) {
                   5614:                     if (newsecs[j] == groups[k]) {
1.103     raeburn  5615:                         if (document.$formname.crstype.value == 'Community') {
                   5616:                             alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5617:                         } else {
                   5618:                             alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   5619:                         }
1.19      raeburn  5620:                         return;
                   5621:                     }
                   5622:                 }
                   5623:             }
                   5624:         }
                   5625:         if ((userrole == 'cc') && (numsections > 0)) {
                   5626:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5627:             section = "";
                   5628:         }
1.103     raeburn  5629:         if ((userrole == 'co') && (numsections > 0)) {
                   5630:             alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
                   5631:             section = "";
                   5632:         }
1.19      raeburn  5633: SCRIPTTOP
                   5634:     if ($mode ne 'upload') {
                   5635:         $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5636:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   5637:         var numcourse = getIndex(document.$formname.dccourse);
                   5638:         if (numcourse == "-1") {
1.103     raeburn  5639:             if (document.$formname.type == 'Community') {
                   5640:                 alert("$alerts{'thwc'}");
                   5641:             } else {
                   5642:                 alert("$alerts{'thwa'}");
                   5643:             }
1.19      raeburn  5644:             return;
                   5645:         }
                   5646:         else {
                   5647:             document.$formname.elements[numcourse].name = "act"+coursename;
                   5648:             var numnewsec = getIndex(document.$formname.newsec);
                   5649:             if (numnewsec != "-1") {
                   5650:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   5651:                 document.$formname.elements[numnewsec].value = section;
                   5652:             }
                   5653:             var numstart = getIndex(document.$formname.start);
                   5654:             if (numstart != "-1") {
                   5655:                 document.$formname.elements[numstart].name = "start"+coursename;
                   5656:             }
                   5657:             var numend = getIndex(document.$formname.end);
                   5658:             if (numend != "-1") {
                   5659:                 document.$formname.elements[numend].name = "end"+coursename
                   5660:             }
                   5661:         }
                   5662:     }
1.37      raeburn  5663:     var authcheck = auth_check();
                   5664:     if (authcheck == 'ok') {
                   5665:         document.$formname.submit();
                   5666:     }
1.19      raeburn  5667: }
                   5668: ENDSCRIPT
                   5669:     } else {
                   5670:         $dc_setcourse_code .=  "
                   5671:         document.$formname.sections.value = section;
                   5672:     }
                   5673:     return 'ok';
                   5674: }
                   5675: ";
                   5676:     }
                   5677:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   5678: 
                   5679:     function getIndex(caller) {
                   5680:         for (var i=0;i<document.$formname.elements.length;i++) {
                   5681:             if (document.$formname.elements[i] == caller) {
                   5682:                 return i;
                   5683:             }
                   5684:         }
                   5685:         return -1;
                   5686:     }
                   5687: ENDSCRIPT
1.37      raeburn  5688:     return $dc_setcourse_code;
                   5689: }
                   5690: 
                   5691: sub verify_authen {
                   5692:     my ($formname,$context) = @_;
                   5693:     my %alerts = &authcheck_alerts();
                   5694:     my $finish = "return 'ok';";
                   5695:     if ($context eq 'author') {
                   5696:         $finish = "document.$formname.submit();";
                   5697:     }
                   5698:     my $outcome = <<"ENDSCRIPT";
                   5699: 
                   5700: function auth_check() {
                   5701:     var logintype;
                   5702:     if (document.$formname.login.length) {
                   5703:         if (document.$formname.login.length > 0) {
                   5704:             var loginpicked = 0;
                   5705:             for (var i=0; i<document.$formname.login.length; i++) {
                   5706:                 if (document.$formname.login[i].checked == true) {
                   5707:                     loginpicked = 1;
                   5708:                     logintype = document.$formname.login[i].value;
                   5709:                 }
                   5710:             }
                   5711:             if (loginpicked == 0) {
                   5712:                 alert("$alerts{'authen'}");
                   5713:                 return;
                   5714:             }
                   5715:         }
                   5716:     } else {
                   5717:         logintype = document.$formname.login.value;
                   5718:     }
                   5719:     if (logintype == 'nochange') {
                   5720:         return 'ok';
                   5721:     }
                   5722:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   5723:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   5724:         var alertmsg = '';
                   5725:         switch (logintype) {
                   5726:             case 'krb':
                   5727:                 alertmsg = '$alerts{'krb'}';
                   5728:                 break;
                   5729:             case 'int':
                   5730:                 alertmsg = '$alerts{'ipass'}';
                   5731:             case 'fsys':
                   5732:                 alertmsg = '$alerts{'ipass'}';
                   5733:                 break;
                   5734:             case 'loc':
                   5735:                 alertmsg = '';
                   5736:                 break;
                   5737:             default:
                   5738:                 alertmsg = '';
                   5739:         }
                   5740:         if (alertmsg != '') {
                   5741:             alert(alertmsg);
                   5742:             return;
                   5743:         }
                   5744:     }
                   5745:     $finish
                   5746: }
                   5747: ENDSCRIPT
1.19      raeburn  5748: }
                   5749: 
                   5750: sub sectioncheck_alerts {
                   5751:     my %alerts = &Apache::lonlocal::texthash(
1.103     raeburn  5752:                     curd => 'You must select a course or community in the current domain',
1.19      raeburn  5753:                     inea => 'In each course, each user may only have one student role at a time',
1.103     raeburn  5754:                     inco => 'In each community, each user may only have one member role at a time', 
1.19      raeburn  5755:                     youh => 'You had selected',
                   5756:                     sect => 'sections',
                   5757:                     plsm => 'Please modify your selections so they include no more than one section',
                   5758:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   5759:                     plsc => 'Please choose a different section name',
                   5760:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
1.103     raeburn  5761:                     mayc => 'may not be used as the name for a section, as it is the name of a community group',
1.19      raeburn  5762:                     secn => 'Section names and group names must be distinct',
                   5763:                     secd => 'Section designations do not apply to ',
                   5764:                     role => 'roles',
                   5765:                     accr => 'role will be added with access to all sections',
1.103     raeburn  5766:                     thwa => 'There was a problem with your course selection',
                   5767:                     thwc => 'There was a problem with your community selection',
1.19      raeburn  5768:                  );
                   5769:     return %alerts;
                   5770: }
1.17      raeburn  5771: 
1.37      raeburn  5772: sub authcheck_alerts {
                   5773:     my %alerts = 
                   5774:         &Apache::lonlocal::texthash(
                   5775:                     authen => 'You must choose an authentication type.',
                   5776:                     krb    => 'You need to specify the Kerberos domain.',
                   5777:                     ipass  => 'You need to specify the initial password.',
                   5778:         );
                   5779:     return %alerts;
                   5780: }
                   5781: 
1.1       raeburn  5782: 1;
                   5783: 

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