Annotation of loncom/interface/londropadd.pm, revision 1.70

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to drop and add students in courses 
                      3: #
1.70    ! matthew     4: # $Id: londropadd.pm,v 1.69 2003/06/23 14:38:53 matthew Exp $
1.17      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       www        28: # (Handler to set parameters for assessments
                     29: #
                     30: # (Handler to resolve ambiguous file locations
                     31: #
                     32: # (TeX Content Handler
                     33: #
1.50      matthew    34: ###############################################################
                     35: ###############################################################
1.1       www        36: 
                     37: package Apache::londropadd;
                     38: 
                     39: use strict;
1.24      albertel   40: use Apache::lonnet();
                     41: use Apache::loncommon();
1.50      matthew    42: use Apache::lonhtmlcommon();
1.1       www        43: use Apache::Constants qw(:common :http REDIRECT);
1.60      matthew    44: use Spreadsheet::WriteExcel;
1.1       www        45: 
1.50      matthew    46: ###############################################################
                     47: ###############################################################
1.10      www        48: sub header {
1.46      www        49:     my $bodytag=&Apache::loncommon::bodytag('Enrollment Manager');
1.27      matthew    50:     return(<<ENDHEAD);
1.1       www        51: <html>
                     52: <head>
1.40      matthew    53: <title>LON-CAPA Enrollment Manager</title>
1.1       www        54: </head>
1.46      www        55: $bodytag
1.40      matthew    56: <form method="post" enctype="multipart/form-data"  
                     57:       action="/adm/dropadd" name="studentform">
1.1       www        58: ENDHEAD
1.10      www        59: }
                     60: 
1.50      matthew    61: ###############################################################
                     62: ###############################################################
                     63: # Drop student from all sections of a course, except optional $csec
1.26      matthew    64: sub modifystudent {
1.33      matthew    65:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
1.26      matthew    66:     # if $csec is undefined, drop the student from all the courses matching
                     67:     # this one.  If $csec is defined, drop them from all other sections of 
                     68:     # this course and add them to section $csec
1.25      matthew    69:     $courseid=~s/\_/\//g;
                     70:     $courseid=~s/^(\w)/\/$1/;
1.26      matthew    71:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
                     72:     my ($tmp) = keys(%roles);
                     73:     # Bail out if we were unable to get the students roles
1.35      matthew    74:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
1.26      matthew    75:     # Go through the roles looking for enrollment in this course
1.35      matthew    76:     my $result = '';
1.26      matthew    77:     foreach my $course (keys(%roles)) {
1.35      matthew    78:         if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
1.26      matthew    79:             # We are in this course
1.25      matthew    80:             my $section=$1;
1.26      matthew    81:             $section='' if ($course eq $courseid.'_st');
1.35      matthew    82:             if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
1.27      matthew    83:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
1.25      matthew    84:                 my $now=time;
1.50      matthew    85:                 # if this is an active role 
1.27      matthew    86:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
1.25      matthew    87:                     my $reply=&Apache::lonnet::modifystudent
1.70    ! matthew    88:                         # dom  name  id mode pass     f     m     l     g
        !            89:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.33      matthew    90:                          $section,time,undef,undef,$desiredhost);
1.35      matthew    91:                     $result .= $reply.':';
1.25      matthew    92:                 }
1.10      www        93:             }
                     94:         }
1.20      harris41   95:     }
1.35      matthew    96:     if ($result eq '') {
1.62      matthew    97:         $result = 'Unable to find section for this student';
1.37      matthew    98:     } else {
                     99:         $result =~ s/(ok:)+/ok/g;
1.35      matthew   100:     }
                    101:     return $result;
1.10      www       102: }
                    103: 
1.50      matthew   104: ###############################################################
                    105: ###############################################################
                    106: # build a domain and server selection form
1.31      matthew   107: sub domain_form {
                    108:     my ($defdom) = @_;
                    109:     # Set up domain and server selection forms
                    110:     #
                    111:     # Get the domains
                    112:     my @domains = &Apache::loncommon::get_domains();
                    113:     # build up the menu information to be passed to 
                    114:     # &Apache::loncommon::linked_select_forms
                    115:     my %select_menus;
                    116:     foreach my $dom (@domains) {
                    117:         # set up the text for this domain
                    118:         $select_menus{$dom}->{'text'}= $dom;
                    119:         # we want a choice of 'default' as the default in the second menu
                    120:         $select_menus{$dom}->{'default'}= 'default';
                    121:         $select_menus{$dom}->{'select2'}->{'default'} = 'default';
                    122:         # Now build up the other items in the second menu
1.45      matthew   123:         my %servers = &Apache::loncommon::get_library_servers($dom);
1.31      matthew   124:         foreach my $server (keys(%servers)) {
                    125:             $select_menus{$dom}->{'select2'}->{$server} 
                    126:                                             = "$server $servers{$server}";
                    127:         }
                    128:     }
                    129:     my $result  = &Apache::loncommon::linked_select_forms
                    130:         ('studentform',' with home server ',$defdom,
                    131:          'lcdomain','lcserver',\%select_menus);
                    132:     return $result;
                    133: }
                    134: 
1.50      matthew   135: ###############################################################
                    136: ###############################################################
                    137: #  Menu Phase One
                    138: sub print_main_menu {
1.10      www       139:     my $r=shift;
1.48      matthew   140:     $r->print(<<END);
                    141: <p>
1.50      matthew   142: <font size="+1">
                    143:     <a href="/adm/dropadd?action=upload">Upload a course list</a>
                    144: </font>
                    145: </p><p>
                    146: <font size="+1">
                    147:     <a href="/adm/dropadd?action=enrollstudent">Enroll a single student</a>
                    148: </font>
1.48      matthew   149: </p><p>
1.50      matthew   150: <font size="+1">
                    151:     <a href="/adm/dropadd?action=modifystudent">Modify student data</a>
                    152: </font>
1.48      matthew   153: </p><p>
1.50      matthew   154: <font size="+1">
                    155:     <a href="/adm/dropadd?action=classlist">View Classlist</a>
                    156: </font>
1.48      matthew   157: </p><p>
1.50      matthew   158: <font size="+1">
                    159:     <a href="/adm/dropadd?action=drop">Drop Students</a>
                    160: </font>
1.48      matthew   161: </p>
                    162: END
1.10      www       163: }
                    164: 
1.50      matthew   165: ###############################################################
                    166: ###############################################################
                    167: sub print_upload_manager_header {
1.23      albertel  168:     my ($r,$datatoken,$distotal,$krbdefdom)=@_;
1.24      albertel  169:     my $javascript;
1.50      matthew   170:     if (! exists($ENV{'form.upfile_associate'})) {
                    171:         $ENV{'form.upfile_associate'} = 'forward';
                    172:     }
                    173:     if ($ENV{'form.associate'} eq 'Reverse Association') {
                    174:         if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                    175:             $ENV{'form.upfile_associate'} = 'reverse';
                    176:         } else {
                    177:             $ENV{'form.upfile_associate'} = 'forward';
                    178:         }
                    179:     }
1.24      albertel  180:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
1.50      matthew   181: 	$javascript=&upload_manager_javascript_reverse_associate();
1.24      albertel  182:     } else {
1.50      matthew   183: 	$javascript=&upload_manager_javascript_forward_associate();
1.24      albertel  184:     }
                    185:     my $javascript_validations=&javascript_validations($krbdefdom);
1.10      www       186:     $r->print(<<ENDPICK);
1.40      matthew   187: <h3>Uploading Class List</h3>
1.2       www       188: <hr>
                    189: <h3>Identify fields</h3>
1.22      albertel  190: Total number of records found in file: $distotal <hr />
                    191: Enter as many fields as you can. The system will inform you and bring you back
                    192: to this page if the data selected is insufficient to run your class.<hr />
1.36      albertel  193: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1.50      matthew   194: <input type="hidden" name="action"     value="upload" />
                    195: <input type="hidden" name="state"      value="got_file" />
1.36      albertel  196: <input type="hidden" name="associate"  value="" />
1.26      matthew   197: <input type="hidden" name="datatoken"  value="$datatoken" />
1.24      albertel  198: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                    199: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
1.26      matthew   200: <input type="hidden" name="upfile_associate" 
                    201:                                        value="$ENV{'form.upfile_associate'}" />
1.24      albertel  202: <hr />
1.28      matthew   203: <script type="text/javascript" language="Javascript">
1.24      albertel  204: $javascript
                    205: $javascript_validations
                    206: </script>
                    207: ENDPICK
                    208: }
                    209: 
1.50      matthew   210: ###############################################################
                    211: ###############################################################
1.24      albertel  212: sub javascript_validations {
                    213:     my ($krbdefdom)=@_;
1.28      matthew   214:     my %param = ( formname => 'studentform',
                    215:                   kerb_def_dom => $krbdefdom );
                    216:     my $authheader = &Apache::loncommon::authform_header(%param);
1.65      matthew   217:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.24      albertel  218:     return (<<ENDPICK);
                    219: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
1.3       www       220:     var foundatype=0;
                    221:     var message='';
                    222:     if (founduname==0) {
1.28      matthew   223: 	alert('You need to specify the username field');
1.3       www       224:         return;
                    225:     }
1.61      matthew   226:     // alert('current.radiovalue = '+current.radiovalue);
1.29      matthew   227:     if (current.radiovalue == null || current.radiovalue == 'nochange') {
1.28      matthew   228:         // They did not check any of the login radiobuttons.
                    229:         alert('You must choose an authentication type');
                    230:         return;
                    231:     }
                    232:     foundatype=1;
1.29      matthew   233:     if (current.argfield == null || current.argfield == '') {
1.28      matthew   234:         var alertmsg = '';
1.29      matthew   235:         switch (current.value) {
1.28      matthew   236:             case 'krb': 
                    237:                 alertmsg = 'You need to specify the Kerberos domain';
                    238:                 break;
                    239:             case 'loc':
                    240:             case 'fsys':
                    241:                 alertmsg = 'You need to specify the initial password';
                    242:                 break;
                    243:             case 'fsys':
                    244:                 alertmsg = '';
                    245:                 break;
                    246:             default: 
                    247:                 alertmsg = '';
1.3       www       248:         }
1.28      matthew   249:         if (alertmsg != '') {
                    250:             alert(alertmsg);
1.3       www       251:             return;
                    252:         }
                    253:     }
1.28      matthew   254: 
1.3       www       255:     if (foundname==0) { message='No name fields specified. '; }
                    256:     if (foundid==0) { message+='No ID or student number field specified. '; }
                    257:     if (foundsec==0) { message+='No section or group field specified. '; }
1.4       www       258:     if (message!='') {
                    259:        message+='Continue enrollment?';
                    260:        if (confirm(message)) {
1.50      matthew   261:           vf.state.value='enrolling';
1.4       www       262: 	  vf.submit();
                    263:        }
                    264:     } else {
1.50      matthew   265:       vf.state.value='enrolling';
1.4       www       266:       vf.submit();
1.24      albertel  267:     }
1.3       www       268: }
                    269: 
1.28      matthew   270: $authheader
1.24      albertel  271: ENDPICK
1.28      matthew   272: 
1.24      albertel  273: }
                    274: 
1.50      matthew   275: ###############################################################
                    276: ###############################################################
                    277: sub upload_manager_javascript_forward_associate {
1.24      albertel  278:     return(<<ENDPICK);
                    279: function verify(vf) {
                    280:     var founduname=0;
                    281:     var foundpwd=0;
                    282:     var foundname=0;
                    283:     var foundid=0;
                    284:     var foundsec=0;
                    285:     var tw;
                    286:     for (i=0;i<=vf.nfields.value;i++) {
                    287:         tw=eval('vf.f'+i+'.selectedIndex');
                    288:         if (tw==1) { founduname=1; }
                    289:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    290:         if (tw==7) { foundid=1; }
                    291:         if (tw==8) { foundsec=1; }
                    292:         if (tw==9) { foundpwd=1; }
                    293:     }
                    294:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
                    295: }
                    296: 
1.49      matthew   297: //
                    298: // vf = this.form
                    299: // tf = column number
                    300: //
                    301: // values of nw
                    302: //
                    303: // 0 = none
                    304: // 1 = username
                    305: // 2 = names (lastname, firstnames)
                    306: // 3 = fname (firstname)
                    307: // 4 = mname (middlename)
                    308: // 5 = lname (lastname)
                    309: // 6 = gen   (generation)
                    310: // 7 = id
                    311: // 8 = section
                    312: // 9 = ipwd  (password)
                    313: //
1.24      albertel  314: function flip(vf,tf) {
                    315:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    316:    var i;
1.49      matthew   317:    // make sure no other columns are labeled the same as this one
1.24      albertel  318:    for (i=0;i<=vf.nfields.value;i++) {
                    319:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    320:           eval('vf.f'+i+'.selectedIndex=0;')
                    321:       }
                    322:    }
1.49      matthew   323:    // If we set this to 'lastname, firstnames', clear out all the ones
                    324:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
1.24      albertel  325:    if (nw==2) {
                    326:       for (i=0;i<=vf.nfields.value;i++) {
                    327:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    328:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    329:              eval('vf.f'+i+'.selectedIndex=0;')
                    330:          }
                    331:       }
                    332:    }
1.49      matthew   333:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    334:    // clear out any that are set to 'lastname, firstnames' (2)
1.24      albertel  335:    if ((nw>=3) && (nw<=6)) {
                    336:       for (i=0;i<=vf.nfields.value;i++) {
                    337:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    338:              eval('vf.f'+i+'.selectedIndex=0;')
                    339:          }
                    340:       }
                    341:    }
1.49      matthew   342:    // If we set the password, make the password form below correspond to 
                    343:    // the new value.
1.24      albertel  344:    if (nw==9) {
1.28      matthew   345:        changed_radio('int',document.studentform);
                    346:        set_auth_radio_buttons('int',document.studentform);
                    347:        vf.intarg.value='';
                    348:        vf.krbarg.value='';
1.24      albertel  349:        vf.locarg.value='';
                    350:    }
                    351: }
                    352: 
                    353: function clearpwd(vf) {
                    354:     var i;
                    355:     for (i=0;i<=vf.nfields.value;i++) {
                    356:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    357:             eval('vf.f'+i+'.selectedIndex=0;')
                    358:         }
                    359:     }
                    360: }
                    361: 
                    362: ENDPICK
                    363: }
                    364: 
1.50      matthew   365: ###############################################################
                    366: ###############################################################
                    367: sub upload_manager_javascript_reverse_associate {
1.24      albertel  368:     return(<<ENDPICK);
                    369: function verify(vf) {
                    370:     var founduname=0;
                    371:     var foundpwd=0;
                    372:     var foundname=0;
                    373:     var foundid=0;
                    374:     var foundsec=0;
                    375:     var tw;
                    376:     for (i=0;i<=vf.nfields.value;i++) {
                    377:         tw=eval('vf.f'+i+'.selectedIndex');
                    378:         if (i==0 && tw!=0) { founduname=1; }
                    379:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    380:         if (i==6 && tw!=0) { foundid=1; }
                    381:         if (i==7 && tw!=0) { foundsec=1; }
                    382:         if (i==8 && tw!=0) { foundpwd=1; }
                    383:     }
                    384:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
                    385: }
                    386: 
                    387: function flip(vf,tf) {
                    388:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    389:    var i;
                    390:    // picked the all one one name field, reset the other name ones to blank
                    391:    if (tf==1 && nw!=0) {
                    392:       for (i=2;i<=5;i++) {
                    393:          eval('vf.f'+i+'.selectedIndex=0;')
                    394:       }
                    395:    }
                    396:    //picked one of the piecewise name fields, reset the all in
                    397:    //one field to blank
                    398:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    399:       eval('vf.f1.selectedIndex=0;')
                    400:    }
                    401:    // intial password specified, pick internal authentication
                    402:    if (tf==8 && nw!=0) {
1.28      matthew   403:        changed_radio('int',document.studentform);
                    404:        set_auth_radio_buttons('int',document.studentform);
                    405:        vf.krbarg.value='';
                    406:        vf.intarg.value='';
1.24      albertel  407:        vf.locarg.value='';
                    408:    }
                    409: }
                    410: 
                    411: function clearpwd(vf) {
                    412:     var i;
                    413:     if (eval('vf.f8.selectedIndex')!=0) {
                    414:         eval('vf.f8.selectedIndex=0;')
                    415:     }
                    416: }
1.2       www       417: ENDPICK
1.23      albertel  418: }
1.10      www       419: 
1.50      matthew   420: ###############################################################
                    421: ###############################################################
                    422: sub print_upload_manager_footer {
1.23      albertel  423:     my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
1.64      albertel  424: 
                    425:     my ($krbdef,$krbdefdom) =
                    426:         &Apache::loncommon::get_kerberos_defaults($defdom);
                    427:     my %param = ( formname => 'document.studentform',
                    428:                   kerb_def_dom => $krbdefdom,
                    429:                   kerb_def_auth => $krbdef
                    430:                   );
1.28      matthew   431:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    432:     my $intform = &Apache::loncommon::authform_internal(%param);
                    433:     my $locform = &Apache::loncommon::authform_local(%param);
1.31      matthew   434:     my $domform = &domain_form($defdom);
1.68      matthew   435:     my $date_table = &date_setting_table();
1.23      albertel  436:     $r->print(<<ENDPICK);
1.3       www       437: </table>
1.10      www       438: <input type=hidden name=nfields value=$i>
                    439: <input type=hidden name=keyfields value="$keyfields">
1.3       www       440: <h3>Login Type</h3>
1.15      albertel  441: <p>Note: this will not take effect if the user already exists</p>
                    442: <p>
1.28      matthew   443: $krbform
1.15      albertel  444: </p>
                    445: <p>
1.28      matthew   446: $intform
1.15      albertel  447: </p>
                    448: <p>
1.28      matthew   449: $locform
1.15      albertel  450: </p>
1.5       www       451: <h3>LON-CAPA Domain for Students</h3>
1.29      matthew   452: LON-CAPA domain: $domform <p>
1.5       www       453: <h3>Starting and Ending Dates</h3>
1.68      matthew   454: <p>
                    455: $date_table
                    456: </p>
1.5       www       457: <h3>Full Update</h3>
                    458: <input type=checkbox name=fullup value=yes> Full update 
1.11      www       459: (also print list of users not enrolled anymore)<p>
1.18      www       460: <h3>ID/Student Number</h3>
                    461: <input type=checkbox name=forceid value=yes> 
                    462: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.19      www       463: (only do if you know what you are doing)<p>
1.36      albertel  464: <input type="button" onClick="javascript:verify(this.form)" value="Update Courselist" /><br />
1.50      matthew   465: Note: for large courses, this operation may be time consuming.
1.3       www       466: ENDPICK
1.23      albertel  467: }
1.24      albertel  468: 
1.23      albertel  469: # ======================================================= Menu Phase Two Upload
1.50      matthew   470: sub print_upload_manager_form {
1.23      albertel  471:     my $r=shift;
1.26      matthew   472: 
1.24      albertel  473:     my $datatoken;
                    474:     if (!$ENV{'form.datatoken'}) {
1.26      matthew   475:       $datatoken=&Apache::loncommon::upfile_store($r);
1.24      albertel  476:     } else {
1.26      matthew   477:       $datatoken=$ENV{'form.datatoken'};
                    478:       &Apache::loncommon::load_tmp_file($r);
1.24      albertel  479:     }
                    480:     my @records=&Apache::loncommon::upfile_record_sep();
1.23      albertel  481:     my $total=$#records;
                    482:     my $distotal=$total+1;
                    483:     my $today=time;
                    484:     my $halfyear=$today+15552000;
                    485:     my $defdom=$r->dir_config('lonDefDomain');
1.64      albertel  486:     my ($krbdef,$krbdefdom) =
                    487:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.50      matthew   488:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
1.24      albertel  489:     my $i;
                    490:     my $keyfields;
1.23      albertel  491:     if ($total>=0) {
1.50      matthew   492: 	my @d=(['username','Username'],
                    493:                ['names','Last Name, First Names'],
                    494: 	       ['fname','First Name'],
                    495:                ['mname','Middle Names/Initials'],
                    496: 	       ['lname','Last Name'],
                    497:                ['gen','Generation'],
                    498: 	       ['id','ID/Student Number'],
                    499:                ['sec','Group/Section'],
1.24      albertel  500: 	       ['ipwd','Initial Password']);
                    501: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                    502: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                    503: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
                    504: 	    foreach (@d) { $keyfields.=$_->[0].','; }
                    505: 	    chop($keyfields);
                    506: 	} else {
                    507: 	    unshift(@d,['none','']);
                    508: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
                    509: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                    510: 	    $keyfields=join(',',sort(keys(%sone)));
1.23      albertel  511: 	}
                    512:     }
1.50      matthew   513:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
1.10      www       514: }
                    515: 
1.12      www       516: # ======================================================= Enroll single student
                    517: sub enroll_single_student {
                    518:     my $r=shift;
1.68      matthew   519:     #
                    520:     # We do the dates first because the action of making them the defaul
                    521:     # in the course is entirely seperate from the action of enrolling the
                    522:     # student.  Also, a failure in setting the dates as default is not fatal
                    523:     # to the process of enrolling / modifying a student.
                    524:     my ($startdate,$enddate) = &get_dates_from_form();
                    525:     if ($ENV{'form.makedatesdefault'}) {
                    526:         $r->print(&make_dates_default($startdate,$enddate));
                    527:     }
                    528: 
1.12      www       529:     $r->print('<h3>Enrolling Student</h3>');
1.54      matthew   530:     $r->print('<p>Enrolling '.$ENV{'form.cuname'}." \@ ".
1.34      matthew   531:               $ENV{'form.lcdomain'}.'</p>');
1.12      www       532:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
1.31      matthew   533:         ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
                    534:         # Deal with home server selection
                    535:         my $domain=$ENV{'form.lcdomain'};
                    536:         my $desiredhost = $ENV{'form.lcserver'};
                    537:         if (lc($desiredhost) eq 'default') {
                    538:             $desiredhost = undef;
                    539:         } else {
1.45      matthew   540:             my %home_servers =&Apache::loncommon::get_library_servers($domain);
1.31      matthew   541:             if (! exists($home_servers{$desiredhost})) {
                    542:                 $r->print('<font color="#ff0000">Error:</font>'.
                    543:                           'Invalid home server specified');
                    544:                 return;
                    545:             }
                    546:         }
1.34      matthew   547:         $r->print(" with server $desiredhost :") if (defined($desiredhost));
1.31      matthew   548:         # End of home server selection logic
1.12      www       549: 	my $amode='';
                    550:         my $genpwd='';
                    551:         if ($ENV{'form.login'} eq 'krb') {
1.47      albertel  552:            $amode='krb';
                    553: 	   $amode.=$ENV{'form.krbver'};
1.28      matthew   554:            $genpwd=$ENV{'form.krbarg'};
1.12      www       555:         } elsif ($ENV{'form.login'} eq 'int') {
1.26      matthew   556:            $amode='internal';
1.28      matthew   557:            $genpwd=$ENV{'form.intarg'};
1.15      albertel  558:         }  elsif ($ENV{'form.login'} eq 'loc') {
                    559: 	    $amode='localauth';
                    560: 	    $genpwd=$ENV{'form.locarg'};
                    561: 	    if (!$genpwd) { $genpwd=" "; }
                    562: 	}
1.34      matthew   563:         my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
                    564:                                                    $ENV{'form.lcdomain'});
                    565:         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
1.55      matthew   566:             # Clean out any old roles the student has in this class.
1.33      matthew   567:             &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                    568:                            $ENV{'request.course.id'},$ENV{'form.csec'},
                    569:                             $desiredhost);
1.55      matthew   570:             my $login_result = &Apache::lonnet::modifystudent
                    571:                 ($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                    572:                  $ENV{'form.cstid'},$amode,$genpwd,
                    573:                  $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
                    574:                  $ENV{'form.clast'},$ENV{'form.cgen'},
1.67      matthew   575:                  $ENV{'form.csec'},$enddate,
                    576:                  $startdate,$ENV{'form.forceid'},
1.55      matthew   577:                  $desiredhost);
                    578:             if ($login_result =~ /^ok/) {
                    579:                 $r->print($login_result);
                    580:                 $r->print("<p> If active, the new role will be available ".
                    581:                           "when the student next logs in to LON-CAPA.</p>");
                    582:             } else {
                    583:                 $r->print("unable to enroll: ".$login_result);
                    584:             }
1.12      www       585: 	} else {
1.34      matthew   586:             $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;'.
                    587:                       'Invalid login mode or password.  '.
                    588:                       'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
1.12      www       589:         }          
                    590:     } else {
                    591:         $r->print('Invalid username or domain');
1.26      matthew   592:     }    
1.12      www       593: }
                    594: 
1.68      matthew   595: sub setup_date_selectors {
                    596:     my ($starttime,$endtime) = @_;
                    597:     if (! defined($starttime)) {
                    598:         $starttime = time;
                    599:         if (exists($ENV{'course.'.$ENV{'request.course.id'}.
                    600:                             '.default_enrollment_start_date'})) {
                    601:             $starttime = $ENV{'course.'.$ENV{'request.course.id'}.
                    602:                                   '.default_enrollment_start_date'};
                    603:         }
                    604:     }
                    605:     if (! defined($endtime)) {
                    606:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                    607:         if (exists($ENV{'course.'.$ENV{'request.course.id'}.
                    608:                             '.default_enrollment_end_date'})) {
                    609:             $endtime = $ENV{'course.'.$ENV{'request.course.id'}.
                    610:                                 '.default_enrollment_end_date'};
                    611:         }
                    612:     }
                    613:     my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
                    614:                                                             'startdate',
                    615:                                                             $starttime);
                    616:     my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
                    617:                                                           'enddate',
                    618:                                                           $endtime);
                    619:     return ($startdateform,$enddateform);
                    620: }
                    621: 
                    622: sub get_dates_from_form {
                    623:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
                    624:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate');
                    625:     if ($ENV{'form.no_end_date'}) {
                    626:         $enddate = 0;
                    627:     }
                    628:     return ($startdate,$enddate);
                    629: }
                    630: 
                    631: sub date_setting_table {
                    632:     my ($starttime,$endtime) = @_;
                    633:     my ($startform,$endform)=&setup_date_selectors($starttime,$endtime);
                    634:     my $dateDefault = '<nobr>'.
                    635:         '<input type="checkbox" name="makedatesdefault" />'.
                    636:         ' make these dates the default for future enrollment';
                    637:     my $perpetual = '<nobr><input type="checkbox" name="no_end_date"';
                    638:     if (defined($endtime) && $endtime == 0) {
                    639:         $perpetual .= ' checked';
                    640:     }
                    641:     $perpetual.= ' />'.' no ending date</nobr>';
                    642:     my $result = '';
                    643:     $result .= "<table>\n";
                    644:     $result .= '<tr><td align="right">Starting Date</td>'.
                    645:         '<td>'.$startform.'</td>'.
                    646:         '<td>'.$dateDefault.'</td>'."</tr>\n";
                    647:     $result .= '<tr><td align="right">Ending Date</td>'.
                    648:         '<td>'.$endform.'</td>'.
                    649:         '<td>'.$perpetual.'</td>'."</tr>\n";
                    650:     $result .= "</table>\n";
                    651:     return $result;
                    652: }
                    653: 
                    654: sub make_dates_default {
                    655:     my ($startdate,$enddate) = @_;
                    656:     my $result = '';
                    657:     my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    658:     my $crs = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    659:     my $put_result = &Apache::lonnet::put('environment',
                    660:             {'default_enrollment_start_date'=>$startdate,
                    661:              'default_enrollment_end_date'  =>$enddate},$dom,$crs);
                    662:     if ($put_result eq 'ok') {
                    663:         $result .= "Set default start and end dates for course<br />";
1.69      matthew   664:         #
                    665:         # Refresh the course environment
                    666:         &Apache::lonnet::coursedescription($ENV{'request.course.id'});
1.68      matthew   667:     } else {
                    668:         $result .= "Unable to set default dates for course:".$put_result.
                    669:             '<br />';
                    670:     }
                    671:     return $result;
                    672: }
                    673: 
1.10      www       674: # ======================================================= Menu Phase Two Enroll
1.50      matthew   675: sub print_enroll_single_student_form {
1.10      www       676:     my $r=shift;
1.40      matthew   677:     $r->print("<h3>Enroll One Student</h3>");
1.26      matthew   678:     my $today    = time;
                    679:     my $halfyear = $today+15552000;
1.11      www       680:     my $defdom=$r->dir_config('lonDefDomain');
1.64      albertel  681:     # Set up authentication forms
                    682:     my ($krbdef,$krbdefdom) =
                    683:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.24      albertel  684:     my $javascript_validations=&javascript_validations($krbdefdom);
1.64      albertel  685:     my %param = ( formname => 'document.studentform',
                    686:                   kerb_def_dom => $krbdefdom,
                    687:                   kerb_def_auth => $krbdef
                    688:                   );
1.28      matthew   689:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    690:     my $intform = &Apache::loncommon::authform_internal(%param);
                    691:     my $locform = &Apache::loncommon::authform_local(%param);
1.29      matthew   692:     # Set up domain selection form
1.31      matthew   693:     my $domform = &domain_form($defdom);
1.68      matthew   694:     my $date_table = &date_setting_table();
1.28      matthew   695:     # Print it all out
1.50      matthew   696:     $r->print(<<END);
                    697: <input type="hidden" name="action" value="enrollstudent">
                    698: <input type="hidden" name="state"  value="done">
                    699: 
1.28      matthew   700: <script type="text/javascript" language="Javascript">
1.12      www       701: function verify(vf) {
                    702:     var founduname=0;
                    703:     var foundpwd=0;
                    704:     var foundname=0;
                    705:     var foundid=0;
                    706:     var foundsec=0;
                    707:     var tw;
1.26      matthew   708:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
1.31      matthew   709: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
1.12      www       710:         founduname=1;
                    711:     }
1.14      harris41  712:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
1.26      matthew   713: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
1.12      www       714:         foundname=1;
                    715:     }
1.14      harris41  716:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
1.12      www       717:         foundsec=1;
                    718:     }
1.14      harris41  719:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
1.12      www       720: 	foundid=1;
                    721:     }
                    722:     if (founduname==0) {
                    723: 	alert('You need to specify at least the username and domain fields');
                    724:         return;
                    725:     }
1.24      albertel  726:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
1.12      www       727: }
                    728: 
1.24      albertel  729: $javascript_validations
1.12      www       730: 
1.24      albertel  731: function clearpwd(vf) {
                    732:     //nothing else needs clearing
1.15      albertel  733: }
                    734: 
1.12      www       735: </script>
1.11      www       736: <h3>Personal Data</h3>
1.50      matthew   737: <table>
                    738: <tr><td>First Name:</td><td> <input type="text" name="cfirst"  size="15"></td></tr>
                    739: <tr><td>Middle Name:</td><td> <input type="text" name="cmiddle" size="15"></td></tr>
                    740: <tr><td>Last Name: </td><td><input type="text" name="clast"   size="15"></td></tr>
                    741: <tr><td>Generation: </td><td><input type="text" name="cgen"    size="5"> </td></tr>
                    742: </table>
1.11      www       743: 
1.12      www       744: <h3>Login Data</h3>
1.26      matthew   745: <p>Username: <input type="text" name="cuname"  size="15"></p>
1.29      matthew   746: <p>Domain:   $domform</p>
1.26      matthew   747: <p>Note: login settings below  will not take effect if the user already exists
                    748: </p><p>
1.28      matthew   749: $krbform
1.26      matthew   750: </p><p>
1.28      matthew   751: $intform
1.26      matthew   752: </p><p>
1.28      matthew   753: $locform
1.26      matthew   754: </p><p>
1.50      matthew   755: 
                    756: <h3>Course Data</h3>
                    757: 
                    758: <p>Group/Section: <input type="text" name="csec" size="5" />
                    759: <p>
1.68      matthew   760: $date_table
1.50      matthew   761: </p>
                    762: <h3>ID/Student Number</h3>
                    763: <p>
                    764: ID/Student Number: <input type="text" name="cstid" size="10">
1.26      matthew   765: </p><p>
                    766: <input type="checkbox" name="forceid" value="yes"> 
1.18      www       767: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.50      matthew   768: (only do if you know what you are doing)
                    769: </p><p>
                    770: <input type="button" onClick="verify(this.form)" value="Enroll as student">
1.26      matthew   771: </p>
1.50      matthew   772: END
                    773:     return;
1.10      www       774: }
                    775: 
                    776: # ========================================================= Menu Phase Two Drop
1.51      matthew   777: sub print_drop_menu {
1.10      www       778:     my $r=shift;
1.40      matthew   779:     $r->print("<h3>Drop Students</h3>");
1.11      www       780:     my $cid=$ENV{'request.course.id'};
1.56      matthew   781:     my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
                    782:     if (! defined($classlist)) {
1.27      matthew   783:         $r->print("There are no students currently enrolled.\n");
1.51      matthew   784:         return;
1.25      matthew   785:     }
1.51      matthew   786:     # Print out the available choices
1.56      matthew   787:     &show_drop_list($r,$classlist,$keylist);
1.51      matthew   788:     return;
1.11      www       789: }
                    790: 
1.40      matthew   791: # ============================================== view classlist
1.50      matthew   792: sub print_html_classlist {
1.40      matthew   793:     my $r=shift;
1.57      matthew   794:     if (! exists($ENV{'form.sortby'})) {
                    795:         $ENV{'form.sortby'} = 'username';
                    796:     }
1.59      matthew   797:     if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
1.57      matthew   798:         $ENV{'form.Status'} = 'Active';
                    799:     }
                    800:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
                    801:         ($ENV{'form.Status'},'studentform');
1.48      matthew   802:     $r->print(<<END);
1.58      matthew   803: <input type="hidden" name="action" value="$ENV{'form.action'}" />
1.57      matthew   804: <input type="hidden" name="state"  value="" />
1.50      matthew   805: <p>
1.57      matthew   806: <font size="+1">Current Classlist</font>
                    807: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.59      matthew   808: END
                    809:     if ($ENV{'form.action'} ne 'modifystudent') {
                    810:         $r->print(<<END);
1.57      matthew   811: <font size="+1">
                    812: <a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
1.61      matthew   813: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.60      matthew   814: <a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
1.57      matthew   815: </font>
                    816: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.61      matthew   817: Student Status:
1.48      matthew   818: END
1.59      matthew   819:     }
                    820:     $r->print($status_select."</p>\n");
1.40      matthew   821:     my $cid=$ENV{'request.course.id'};
1.56      matthew   822:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                    823:     if (! defined($classlist)) {
1.40      matthew   824:         $r->print("There are no students currently enrolled.\n");
                    825:     } else {
                    826:         # Print out the available choices
1.50      matthew   827:         if ($ENV{'form.action'} eq 'modifystudent') {
1.53      matthew   828:             &show_class_list($r,'view','modify','modifystudent',
1.57      matthew   829:                              $ENV{'form.Status'},$classlist,$keylist);
1.50      matthew   830:         } else {
1.53      matthew   831:             &show_class_list($r,'view','aboutme','classlist',
1.57      matthew   832:                              $ENV{'form.Status'},$classlist,$keylist);
1.50      matthew   833:         }
1.41      matthew   834:     }
                    835: }
                    836: 
                    837: # ============================================== view classlist
1.60      matthew   838: sub print_formatted_classlist {
1.41      matthew   839:     my $r=shift;
1.60      matthew   840:     my $mode = shift;
1.41      matthew   841:     my $cid=$ENV{'request.course.id'};
1.56      matthew   842:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                    843:     if (! defined($classlist)) {
1.41      matthew   844:         $r->print("There are no students currently enrolled.\n");
                    845:     } else {
1.60      matthew   846:         &show_class_list($r,$mode,'nolink','csv',
1.57      matthew   847:                          $ENV{'form.Status'},$classlist,$keylist);
1.40      matthew   848:     }
                    849: }
                    850: 
                    851: # =================================================== Show student list to drop
                    852: sub show_class_list {
1.56      matthew   853:     my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
1.40      matthew   854:     my $cid=$ENV{'request.course.id'};
1.60      matthew   855:     #
                    856:     # Variables for excel output
                    857:     my ($excel_workbook, $excel_sheet, $excel_filename,$row);
                    858:     #
1.53      matthew   859:     my $sortby = $ENV{'form.sortby'};
                    860:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
                    861:         $sortby = 'username';
                    862:     }
1.42      matthew   863:     # Print out header 
1.41      matthew   864:     if ($mode eq 'view') {
1.50      matthew   865:         if ($linkto eq 'aboutme') {
1.56      matthew   866:             $r->print('Select a user name to view the users personal page.');
1.50      matthew   867:         } elsif ($linkto eq 'modify') {
                    868:             $r->print('Select a user name to modify the students information');
                    869:         }
1.41      matthew   870:         $r->print(<<END);
1.59      matthew   871: 
1.53      matthew   872: <input type="hidden" name="sortby" value="$sortby" />
1.59      matthew   873: <input type="hidden" name="sname"  value="" />
                    874: <input type="hidden" name="sdom"   value="" />
1.40      matthew   875: <p>
                    876: <table border=2>
1.53      matthew   877: <tr><th>
1.57      matthew   878:        <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">username</a>
1.53      matthew   879:     </th><th>
1.57      matthew   880:        <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">domain</a>
1.53      matthew   881:     </th><th>
1.57      matthew   882:        <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
1.53      matthew   883:     </th><th>
1.57      matthew   884:        <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">student name</a>
1.53      matthew   885:     </th><th>
1.57      matthew   886:        <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">section</a>
1.53      matthew   887:     </th>
                    888: </tr>
1.40      matthew   889: END
1.41      matthew   890:     } elsif ($mode eq 'csv') {
1.58      matthew   891:         if($statusmode eq 'Expired') {
                    892:             $r->print('"Students with expired roles"');
                    893:         }
                    894:         if ($statusmode eq 'Any') {
                    895:             $r->print('"'.join('","',("username","domain","ID","student name",
                    896:                                       "section","status")).'"'."\n");
                    897:         } else {
                    898:             $r->print('"'.join('","',("username","domain","ID","student name",
                    899:                                       "section")).'"'."\n");
                    900:         }
1.60      matthew   901:     } elsif ($mode eq 'excel') {
                    902:         # Create the excel spreadsheet
                    903:         $excel_filename = '/prtspool/'.
                    904:             $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                    905:                 time.'_'.rand(1000000000).'.xls';
                    906:         $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
                    907:                                                        $excel_filename);
                    908:         $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
                    909:         $excel_sheet = $excel_workbook->addworksheet('classlist');
                    910:         #
                    911:         my $description = 'Classlist for '.
                    912:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    913:         $excel_sheet->write($row++,0,$description);
                    914:         #
                    915:         $excel_sheet->write($row++,0,["username","domain","ID",
                    916:                                       "student name","section","status"]);
1.41      matthew   917:     }
1.56      matthew   918:     #
                    919:     # Sort the students
                    920:     my %index;
                    921:     my $i;
                    922:     foreach (@$keylist) {
                    923:         $index{$_} = $i++;
                    924:     }
                    925:     my $index  = $index{$sortby};
                    926:     my $second = $index{'username'};
                    927:     my $third  = $index{'domain'};
1.53      matthew   928:     my @Sorted_Students = sort {
1.56      matthew   929:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                    930:             ||
                    931:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                    932:             ||
                    933:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
                    934:         } (keys(%$classlist));
1.53      matthew   935:     foreach my $student (@Sorted_Students) {
1.56      matthew   936:         my $username = $classlist->{$student}->[$index{'username'}];
                    937:         my $domain   = $classlist->{$student}->[$index{'domain'}];
                    938:         my $section  = $classlist->{$student}->[$index{'section'}];
                    939:         my $name     = $classlist->{$student}->[$index{'fullname'}];
                    940:         my $id       = $classlist->{$student}->[$index{'id'}];
                    941:         my $status   = $classlist->{$student}->[$index{'status'}];
1.57      matthew   942:         next if (($statusmode ne 'Any') && ($status ne $statusmode));
1.51      matthew   943:         if ($mode eq 'view') {
                    944:             $r->print("<tr>\n    <td>\n        ");
                    945:             if ($linkto eq 'nothing') {
                    946:                 $r->print($username);
                    947:             } elsif ($linkto eq 'aboutme') {
                    948:                 $r->print(&Apache::loncommon::aboutmewrapper($username,
                    949:                                                              $username,
                    950:                                                              $domain));
                    951:             } elsif ($linkto eq 'modify') {
1.59      matthew   952:                 $r->print('<a href="'.
                    953:                           "javascript:document.studentform.sname.value='".
                    954:                           $username.
                    955:                           "';document.studentform.sdom.value='".$domain.
                    956:                           "';document.studentform.state.value='selected".
                    957:                           "';document.studentform.submit();".'">'.
1.53      matthew   958:                           $username."</a>\n");
1.50      matthew   959:             }
1.51      matthew   960:             $r->print(<<"END");
1.50      matthew   961:     </td>
1.51      matthew   962:     <td>$domain</td>
                    963:     <td>$id</td>
                    964:     <td>$name</td>
                    965:     <td>$section</td>
1.40      matthew   966: </tr>
                    967: END
1.51      matthew   968:         } elsif ($mode eq 'csv') {
                    969:             # no need to bother with $linkto
                    970:             my @line = ();
                    971:             foreach ($username,$domain,$id,$name,$section) {
                    972:                 push @line,&Apache::loncommon::csv_translate($_);
1.58      matthew   973:             }
                    974:             if ($statusmode eq 'Any') {
                    975:                 push @line,&Apache::loncommon::csv_translate($status);
1.41      matthew   976:             }
1.51      matthew   977:             my $tmp = $";
                    978:             $" = '","';
                    979:             $r->print("\"@line\"\n");
                    980:             $" = $tmp;
1.60      matthew   981:         } elsif ($mode eq 'excel') {
                    982:             $excel_sheet->write($row++,0,[$username,$domain,$id,
                    983:                                           $name,$section,$status]);
1.40      matthew   984:         }
                    985:     }
1.60      matthew   986:     if ($mode eq 'view') {
                    987:         $r->print('</table><br>');
                    988:     } elsif ($mode eq 'excel') {
                    989:         $excel_workbook->close();
                    990:         $r->print('<p><a href="'.$excel_filename.'">'.
                    991:                   'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
                    992:     }
1.40      matthew   993: }
                    994: 
1.50      matthew   995: 
                    996: #
                    997: # print out form for modification of a single students data
                    998: #
                    999: sub print_modify_student_form {
                   1000:     my $r = shift();
                   1001:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.59      matthew  1002:                                             ['sdom','sname']);    
1.53      matthew  1003:     my $sname  = $ENV{'form.sname'};
                   1004:     my $sdom   = $ENV{'form.sdom'};
                   1005:     my $sortby = $ENV{'form.sortby'};
1.50      matthew  1006:     # determine the students name information
                   1007:     my %info=&Apache::lonnet::get('environment',
                   1008:                                   ['firstname','middlename',
1.52      matthew  1009:                                    'lastname','generation','id'],
1.50      matthew  1010:                                   $sdom, $sname);
                   1011:     my ($tmp) = keys(%info);
                   1012:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1013:         $r->print('<font color="#ff0000" size="+2">Error</font>'.
                   1014:                   '<p>'.
                   1015:                   'Unable to retrieve environment data for '.$sname.
                   1016:                   'in domain '.$sdom.'</p><p>'.
                   1017:                   'Please contact your LON-CAPA administrator '.
                   1018:                   'regarding this situation.</p></body></html>');
                   1019:         return;
                   1020:     }
                   1021:     # determine the students starting and ending times and section
                   1022:     my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
                   1023:     # Deal with date forms
1.68      matthew  1024:     my $date_table = &date_setting_table($starttime,$endtime);
1.59      matthew  1025:     #
                   1026:     if (! exists($ENV{'form.Status'}) || 
                   1027:         $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
                   1028:         $ENV{'form.Status'} = 'crap';
                   1029:     }
1.50      matthew  1030:     # Make sure student is enrolled in course    
                   1031:     $r->print(<<END);
1.52      matthew  1032: <p>
                   1033: <font size="+1">
                   1034: Only domain coordinators can change a users password.
                   1035: </font>
                   1036: </p>
1.50      matthew  1037: <input type="hidden" name="slogin"  value="$sname"  />
                   1038: <input type="hidden" name="sdomain" value="$sdom" />
                   1039: <input type="hidden" name="action"  value="modifystudent" />
1.53      matthew  1040: <input type="hidden" name="state"   value="done" />
                   1041: <input type="hidden" name="sortby"  value="$sortby" />
1.59      matthew  1042: <input type="hidden" name="Status"  value="$ENV{'form.Status'}" />
                   1043: 
1.50      matthew  1044: <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'} 
                   1045: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
                   1046: <p>
                   1047: <b>Student Name</b>
                   1048: <table>
                   1049: <tr><th>First</th><th>Middle</th><th>Last</th><th>Generation</th></tr>
                   1050: <tr><td>
                   1051: <input type="text" name="firstname"  value="$info{'firstname'}"  /></td><td>
                   1052: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
                   1053: <input type="text" name="lastname"   value="$info{'lastname'}"   /></td><td>
                   1054: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
                   1055: </table>
                   1056: </p><p>
1.52      matthew  1057: <b>Student ID</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
                   1058: </p><p>
1.53      matthew  1059: <input type="checkbox" name="forceid" > 
                   1060: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
                   1061: (only do if you know what you are doing)
                   1062: </p><p>
1.50      matthew  1063: <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
                   1064: </p>
1.68      matthew  1065: <p>$date_table</p>
1.50      matthew  1066: <input type="submit" value="Submit Modifications" />
                   1067: </body></html>
                   1068: END
                   1069:     return;
                   1070: }
                   1071: 
                   1072: #
                   1073: # modify a single students section 
                   1074: #
                   1075: sub modify_single_student {
                   1076:     my $r = shift;
1.68      matthew  1077:     #
                   1078:     # Do the date defaults first
                   1079:     my ($starttime,$endtime) = &get_dates_from_form();
                   1080:     if ($ENV{'form.makedatesdefault'}) {
                   1081:         $r->print(&make_dates_default($starttime,$endtime));
                   1082:     }
1.59      matthew  1083:     # Get the 'sortby' and 'Status' variables so the user goes back to their
                   1084:     # previous screen
1.53      matthew  1085:     my $sortby = $ENV{'form.sortby'};
1.59      matthew  1086:     my $status = $ENV{'form.Status'};
1.53      matthew  1087:     #
                   1088:     # We always need this information
                   1089:     my $slogin     = $ENV{'form.slogin'};
                   1090:     my $sdom       = $ENV{'form.sdomain'};
                   1091:     #
                   1092:     # Get the old data
                   1093:     my %old=&Apache::lonnet::get('environment',
                   1094:                                  ['firstname','middlename',
                   1095:                                   'lastname','generation','id'],
                   1096:                                  $sdom, $slogin);
1.59      matthew  1097:     $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
                   1098:                                                   $ENV{'request.course.id'});
1.53      matthew  1099:     my ($tmp) = keys(%old);
                   1100:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1101:         $r->print("There was an error determining the environment values ".
                   1102:                   " for $slogin \@ $sdom.");
                   1103:         return;
                   1104:     }
                   1105:     undef $tmp;
                   1106:     #
                   1107:     # Get the new data
1.50      matthew  1108:     my $firstname  = $ENV{'form.firstname'};
                   1109:     my $middlename = $ENV{'form.middlename'};
                   1110:     my $lastname   = $ENV{'form.lastname'};
                   1111:     my $generation = $ENV{'form.generation'};
                   1112:     my $section    = $ENV{'form.section'};
                   1113:     my $courseid   = $ENV{'request.course.id'};
1.52      matthew  1114:     my $sid        = $ENV{'form.id'};
1.50      matthew  1115:     my $displayable_starttime = localtime($starttime);
                   1116:     my $displayable_endtime   = localtime($endtime);
1.53      matthew  1117:     # 
                   1118:     # check for forceid override
1.63      matthew  1119:     if ((defined($old{'id'})) && ($old{'id'} ne '') && 
                   1120:         ($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
1.53      matthew  1121:         $r->print("<font color=\"ff0000\">You changed the students id ".
                   1122:                   " but did not disable the ID change safeguard.".
                   1123:                   "  The students id will not be changed.</font>");
                   1124:         $sid = $old{'id'};
                   1125:     }
                   1126:     #
1.50      matthew  1127:     # talk to the user about what we are going to do
                   1128:     $r->print(<<END);
1.53      matthew  1129:     <h2>Modifying data for user $slogin \@ $sdom </h2>
1.50      matthew  1130: <h3>Student Information</h3>
1.53      matthew  1131: <table rules="rows" border="1" cellpadding="3" >
                   1132: <tr>
                   1133:     <th> Field </th>
                   1134:     <th> Old Value </th>
                   1135:     <th> New Value </th>
                   1136: </tr>
                   1137: <tr>
                   1138:     <td> <b>First name</b> </td>
                   1139:     <td> $old{'firstname'} </td>
                   1140:     <td> $firstname </td>
                   1141: </tr><tr>
                   1142:     <td> <b>Middle name</b> </td>
                   1143:     <td> $old{'middlename'} </td>
                   1144:     <td> $middlename </td>
                   1145: </tr><tr>
                   1146:     <td> <b>Last name</b> </td>
                   1147:     <td> $old{'lastname'} </td>
                   1148:     <td> $lastname </td>
                   1149: </tr><tr>
                   1150:     <td> <b>Generation</b> </td>
                   1151:     <td> $old{'generation'} </td>
                   1152:     <td> $generation </td>
                   1153: </tr><tr>
                   1154:     <td> <b>ID</b> </td>
                   1155:     <td> $old{'id'} </td>
                   1156:     <td> $sid </td>
1.59      matthew  1157: </tr><tr>
                   1158:     <td> <b>Section</b> </td>
                   1159:     <td> $old{'section'} </td>
                   1160:     <td> $section</td>
1.53      matthew  1161: </tr>
1.50      matthew  1162: </table>
                   1163: <h3>Role Information</h3>
                   1164: <table>
1.68      matthew  1165: <tr><td align="right"><b>Start Time:</b></td><td> $displayable_starttime </td></tr>
                   1166: <tr><td align="right"><b>End Time:</b></td><td> $displayable_endtime   </td></tr>
1.50      matthew  1167: </table>
1.52      matthew  1168: <p>
1.50      matthew  1169: END
1.53      matthew  1170:     #
1.63      matthew  1171:     # Send request(s) to modify data (final undef is for 'desiredhost',
                   1172:     # which is a moot point because the student already has an account.
                   1173:     my $modify_section_results = &modifystudent($sdom,$slogin,
                   1174:                                                 $ENV{'request.course.id'},
                   1175:                                                 $section,undef);
                   1176:     if ($modify_section_results !~ /^ok/) {
                   1177:         $r->print("An error occured during the attempt to change the ".
                   1178:                   "section for this student.<br />");
                   1179:     }
1.52      matthew  1180:     my $roleresults = &Apache::lonnet::modifystudent
1.53      matthew  1181:         ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
                   1182:          $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
                   1183:     if ($roleresults eq 'refused' ) {
1.50      matthew  1184:         $r->print("Your request to change the role information for this ".
1.53      matthew  1185:                   "student was refused.  You do not appear to have ".
                   1186:                   "sufficient authority to change student information.");
1.50      matthew  1187:     } elsif ($roleresults !~ /ok/) {
                   1188:         $r->print("An error occurred during the attempt to change the role".
1.52      matthew  1189:                   " information for this student.  <br />".
                   1190:                   "The error reported was ".
1.50      matthew  1191:                   $roleresults);
1.53      matthew  1192:         &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
                   1193:                                  " data for ".$slogin." \@ ".$sdom." by ".
                   1194:                                  $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
                   1195:                                  ":".$roleresults);
1.50      matthew  1196:     } else { # everything is okay!
1.52      matthew  1197:         $r->print("Student information updated successfully. <br />".
                   1198:                   "The student must log out and log in again to see ".
                   1199:                   "these changes.");
1.50      matthew  1200:     }
                   1201:     $r->print(<<END);
1.52      matthew  1202: </p><p>
1.59      matthew  1203: <input type="hidden" name="action" value="modifystudent" />
                   1204: <input type="hidden" name="sortby" value="$sortby" />
                   1205: <input type="hidden" name="Status" value="$status" />
                   1206: <a href="javascript:document.studentform.submit();">Modify another students data</a>
1.50      matthew  1207: </body></html>
                   1208: END
                   1209:     return;
                   1210: }
                   1211: 
                   1212: sub get_enrollment_data {
                   1213:     my ($sname,$sdomain) = @_;
                   1214:     my $courseid = $ENV{'request.course.id'};
                   1215:     $courseid =~ s:_:/:g;
                   1216:     my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
                   1217:     my ($tmp) = keys(%roles);
                   1218:     # Bail out if we were unable to get the students roles
                   1219:     return "666" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   1220:     # Go through the roles looking for enrollment in this course
                   1221:     my ($end,$start) = (undef,undef);
                   1222:     my $section = '';
                   1223:     my $count = scalar(keys(%roles));
                   1224:     while (my ($course,$role) = each(%roles)) {
                   1225:         if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
                   1226:             #
                   1227:             # Get active role
                   1228:             $section=$1;
                   1229:             (undef,$end,$start)=split(/\_/,$role);
                   1230:             my $now=time;
                   1231:             my $notactive=0;
                   1232:             if ($start) {
                   1233:                 if ($now<$start) { $notactive=1; }
                   1234:             }
                   1235:             if ($end) {
                   1236:                 if ($now>$end) { $notactive=1; }
                   1237:             } 
                   1238:             unless ($notactive) { return ($start,$end,$section); }
                   1239:         }
                   1240:     }
                   1241:     return ($start,$end,$section);
                   1242: }
                   1243: 
1.56      matthew  1244: #################################################
                   1245: #################################################
                   1246: 
                   1247: =pod
                   1248: 
                   1249: =item show_drop_list
                   1250: 
                   1251: Display a list of students to drop
                   1252: Inputs: 
                   1253: 
                   1254: =over 4
                   1255: 
                   1256: =item $r, Apache request
                   1257: 
                   1258: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
                   1259: 
                   1260: =item $keylist, array pointer returned from loncoursedata::get_classlist() 
                   1261: which describes the order elements are stored in the %$classlist values.
                   1262: 
                   1263: =item $nosort, if true, sorting links are omitted.
                   1264: 
                   1265: =back
                   1266: 
                   1267: =cut
                   1268: 
                   1269: #################################################
                   1270: #################################################
1.11      www      1271: sub show_drop_list {
1.56      matthew  1272:     my ($r,$classlist,$keylist,$nosort)=@_;
1.11      www      1273:     my $cid=$ENV{'request.course.id'};
1.59      matthew  1274:     if (! exists($ENV{'form.sortby'})) {
                   1275:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1276:                                                 ['sortby']);
                   1277:     }
1.54      matthew  1278:     my $sortby = $ENV{'form.sortby'};
                   1279:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
                   1280:         $sortby = 'username';
                   1281:     }
1.56      matthew  1282:     #
1.54      matthew  1283:     my $action = "drop";
                   1284:     $r->print(<<END);
                   1285: <input type="hidden" name="sortby" value="$sortby" />
                   1286: <input type="hidden" name="action" value="$action" />
1.50      matthew  1287: <input type="hidden" name="state"  value="done" />
1.32      matthew  1288: <script>
1.51      matthew  1289: function checkAll(field) {
1.32      matthew  1290:     for (i = 0; i < field.length; i++)
                   1291:         field[i].checked = true ;
                   1292: }
                   1293: 
1.51      matthew  1294: function uncheckAll(field) {
1.32      matthew  1295:     for (i = 0; i < field.length; i++)
                   1296:         field[i].checked = false ;
                   1297: }
                   1298: </script>
                   1299: <p>
1.26      matthew  1300: <input type="hidden" name="phase" value="four">
1.56      matthew  1301: END
                   1302: 
                   1303:     if ($nosort) {
                   1304:         $r->print(<<END);
                   1305: <table border=2>
                   1306: <tr>
                   1307:     <th>&nbsp;</th>
                   1308:     <th>username</th>
                   1309:     <th>domain</th>
                   1310:     <th>ID</th>
                   1311:     <th>student name</th>
                   1312:     <th>section</th>
                   1313: </tr>
                   1314: END
                   1315: 
                   1316:     } else  {
                   1317:         $r->print(<<END);
1.26      matthew  1318: <table border=2>
1.54      matthew  1319: <tr><th>&nbsp;</th>
                   1320:     <th>
                   1321:        <a href="/adm/dropadd?action=$action&sortby=username">username</a>
                   1322:     </th><th>
                   1323:        <a href="/adm/dropadd?action=$action&sortby=domain">domain</a>
                   1324:     </th><th>
                   1325:        <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
                   1326:     </th><th>
                   1327:        <a href="/adm/dropadd?action=$action&sortby=fullname">student name</a>
                   1328:     </th><th>
                   1329:        <a href="/adm/dropadd?action=$action&sortby=section">section</a>
                   1330:     </th>
                   1331: </tr>
1.26      matthew  1332: END
1.56      matthew  1333:     }
                   1334:     #
                   1335:     # Sort the students
                   1336:     my %index;
                   1337:     my $i;
                   1338:     foreach (@$keylist) {
                   1339:         $index{$_} = $i++;
                   1340:     }
                   1341:     my $index  = $index{$sortby};
                   1342:     my $second = $index{'username'};
                   1343:     my $third  = $index{'domain'};
1.54      matthew  1344:     my @Sorted_Students = sort {
1.56      matthew  1345:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   1346:             ||
                   1347:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   1348:             ||
                   1349:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
                   1350:         } (keys(%$classlist));
1.54      matthew  1351:     foreach my $student (@Sorted_Students) {
1.52      matthew  1352:         my $error;
1.56      matthew  1353:         my $username = $classlist->{$student}->[$index{'username'}];
                   1354:         my $domain   = $classlist->{$student}->[$index{'domain'}];
                   1355:         my $section  = $classlist->{$student}->[$index{'section'}];
                   1356:         my $name     = $classlist->{$student}->[$index{'fullname'}];
                   1357:         my $id       = $classlist->{$student}->[$index{'id'}];
                   1358:         my $status   = $classlist->{$student}->[$index{'status'}];
1.51      matthew  1359:         next if ($status ne 'Active');
                   1360:         #
                   1361:         $r->print(<<"END");
1.26      matthew  1362: <tr>
1.51      matthew  1363:     <td><input type="checkbox" name="droplist" value="$student"></td>
                   1364:     <td>$username</td>
                   1365:     <td>$domain</td>
                   1366:     <td>$id</td>
                   1367:     <td>$name</td>
                   1368:     <td>$section</td>
1.26      matthew  1369: </tr>
                   1370: END
1.25      matthew  1371:     }
                   1372:     $r->print('</table><br>');
1.32      matthew  1373:     $r->print(<<"END");
                   1374: </p><p>
                   1375: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
                   1376: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
                   1377: <p><input type=submit value="Drop Students"></p>
                   1378: END
1.51      matthew  1379:     return;
1.10      www      1380: }
                   1381: 
1.48      matthew  1382: #
                   1383: # Print out the initial form to get the courselist file
                   1384: #
                   1385: sub print_first_courselist_upload_form {
                   1386:     my $r=shift;
                   1387:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   1388:     my $create_classlist_help = 
                   1389: 	&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   1390:            "How do I create a class list from a spreadsheet");
                   1391:     my $create_csv_help =
                   1392: 	&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   1393:            "How do I create a CSV file from a spreadsheet");
                   1394:     $r->print(<<ENDUPFORM);
                   1395: <input type=hidden name=phase value=two>
                   1396: <h3>Upload a courselist</h3>
                   1397: $upfile_select
1.50      matthew  1398: <p>
                   1399: <input type=submit name="fileupload" value="Upload Courselist">
                   1400: <input type="hidden" name="action" value="upload" />
                   1401: <input type="hidden" name="state"  value="got_file" />
                   1402: </p>
1.48      matthew  1403: $create_classlist_help <br />
                   1404: $create_csv_help
                   1405: </body></html>
                   1406: ENDUPFORM
                   1407:     return;
                   1408: }
                   1409: 
1.10      www      1410: # ================================================= Drop/Add from uploaded file
                   1411: sub upfile_drop_add {
                   1412:     my $r=shift;
1.24      albertel 1413:     &Apache::loncommon::load_tmp_file($r);
                   1414:     my @studentdata=&Apache::loncommon::upfile_record_sep();
1.26      matthew  1415:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   1416:     my $cid = $ENV{'request.course.id'};
1.25      matthew  1417:     my %fields=();
1.26      matthew  1418:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
1.25      matthew  1419:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1420:             if ($ENV{'form.f'.$i} ne 'none') {
                   1421:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   1422:             }
                   1423:         } else {
                   1424:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   1425:         }
                   1426:     }
1.26      matthew  1427:     #
1.68      matthew  1428:     my ($startdate,$enddate) = &get_dates_from_form();
                   1429:     if ($ENV{'form.makedatesdefault'}) {
                   1430:         $r->print(&make_dates_default($startdate,$enddate));
                   1431:     }
1.31      matthew  1432:     # Determine domain and desired host (home server)
1.25      matthew  1433:     my $domain=$ENV{'form.lcdomain'};
1.31      matthew  1434:     my $desiredhost = $ENV{'form.lcserver'};
                   1435:     if (lc($desiredhost) eq 'default') {
                   1436:         $desiredhost = undef;
                   1437:     } else {
1.45      matthew  1438:         my %home_servers = &Apache::loncommon::get_library_servers($domain);
1.31      matthew  1439:         if (! exists($home_servers{$desiredhost})) {
                   1440:             $r->print('<font color="#ff0000">Error:</font>'.
                   1441:                       'Invalid home server specified');
                   1442:             return;
                   1443:         }
                   1444:     }
1.26      matthew  1445:     # Determine authentication mechanism
                   1446:     my $amode  = '';
                   1447:     my $genpwd = '';
1.25      matthew  1448:     if ($ENV{'form.login'} eq 'krb') {
1.47      albertel 1449:         $amode='krb';
                   1450: 	$amode.=$ENV{'form.krbver'};
1.28      matthew  1451:         $genpwd=$ENV{'form.krbarg'};
1.25      matthew  1452:     } elsif ($ENV{'form.login'} eq 'int') {
                   1453:         $amode='internal';
1.28      matthew  1454:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
                   1455:             $genpwd=$ENV{'form.intarg'};
1.25      matthew  1456:         }
                   1457:     } elsif ($ENV{'form.login'} eq 'loc') {
                   1458:         $amode='localauth';
                   1459:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
                   1460:             $genpwd=$ENV{'form.locarg'};
                   1461:         }
                   1462:     }
                   1463:     unless (($domain=~/\W/) || ($amode eq '')) {
1.26      matthew  1464:         #######################################
                   1465:         ##         Enroll Students           ##
                   1466:         #######################################
1.25      matthew  1467:         $r->print('<h3>Enrolling Students</h3>');
                   1468:         my $count=0;
                   1469:         my $flushc=0;
                   1470:         my %student=();
1.26      matthew  1471:         # Get new classlist
1.25      matthew  1472:         foreach (@studentdata) {
                   1473:             my %entries=&Apache::loncommon::record_sep($_);
1.26      matthew  1474:             # Determine student name
1.25      matthew  1475:             unless (($entries{$fields{'username'}} eq '') ||
                   1476:                     (!defined($entries{$fields{'username'}}))) {
1.26      matthew  1477:                 my ($fname, $mname, $lname,$gen) = ('','','','');
1.25      matthew  1478:                 if (defined($fields{'names'})) {
1.26      matthew  1479:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   1480:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
1.25      matthew  1481:                 } else {
                   1482:                     if (defined($fields{'fname'})) {
                   1483:                         $fname=$entries{$fields{'fname'}};
                   1484:                     }
                   1485:                     if (defined($fields{'mname'})) {
                   1486:                         $mname=$entries{$fields{'mname'}};
                   1487:                     }
                   1488:                     if (defined($fields{'lname'})) {
                   1489:                         $lname=$entries{$fields{'lname'}};
                   1490:                     }
                   1491:                     if (defined($fields{'gen'})) {
                   1492:                         $gen=$entries{$fields{'gen'}};
                   1493:                     }
                   1494:                 }
                   1495:                 if ($entries{$fields{'username'}}=~/\W/) {
                   1496:                     $r->print('<p><b>Unacceptable username: '.
1.10      www      1497:                               $entries{$fields{'username'}}.' for user '.
1.4       www      1498:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
1.25      matthew  1499:                 } else {
1.26      matthew  1500:                     # determine section number
1.25      matthew  1501:                     my $sec='';
                   1502:                     my $username=$entries{$fields{'username'}};
                   1503:                     if (defined($fields{'sec'})) {
                   1504:                         if (defined($entries{$fields{'sec'}})) {
                   1505:                             $sec=$entries{$fields{'sec'}};
                   1506:                         }
                   1507:                     }
1.26      matthew  1508:                     # determine student id number
1.25      matthew  1509:                     my $id='';
                   1510:                     if (defined($fields{'id'})) {
                   1511:                         if (defined($entries{$fields{'id'}})) {
                   1512:                             $id=$entries{$fields{'id'}};
                   1513:                         }
                   1514:                         $id=~tr/A-Z/a-z/;
                   1515:                     }
1.26      matthew  1516:                     # determine student password
1.25      matthew  1517:                     my $password='';
                   1518:                     if ($genpwd) { 
                   1519:                         $password=$genpwd; 
                   1520:                     } else {
                   1521:                         if (defined($fields{'ipwd'})) {
                   1522:                             if ($entries{$fields{'ipwd'}}) {
                   1523:                                 $password=$entries{$fields{'ipwd'}};
                   1524:                             }
                   1525:                         }
                   1526:                     }
1.56      matthew  1527:                     # Clean up whitespace
                   1528:                     foreach (\$domain,\$username,\$id,\$fname,\$mname,
                   1529:                              \$lname,\$gen,\$sec) {
                   1530:                         $$_ =~ s/(\s+$|^\s+)//g;
                   1531:                     }
1.25      matthew  1532:                     if ($password) {
1.33      matthew  1533:                         &modifystudent($domain,$username,$cid,$sec,
                   1534:                                        $desiredhost);
1.25      matthew  1535:                         my $reply=&Apache::lonnet::modifystudent
                   1536:                             ($domain,$username,$id,$amode,$password,
                   1537:                              $fname,$mname,$lname,$gen,$sec,$enddate,
1.31      matthew  1538:                              $startdate,$ENV{'form.forceid'},$desiredhost);
1.26      matthew  1539:                         if ($reply ne 'ok') {
                   1540:                             $r->print('<p><b>'.
                   1541:                                       'Error enrolling '.$username.': '.
                   1542:                                       $reply.'</b></p>');
1.10      www      1543:          		} else {
1.7       www      1544:                             $count++; $flushc++;
                   1545:                             $student{$username}=1;
1.6       www      1546:                             $r->print('. ');
1.7       www      1547:                             if ($flushc>15) {
                   1548: 				$r->rflush;
                   1549:                                 $flushc=0;
                   1550:                             }
1.6       www      1551:                         }
1.25      matthew  1552:                     } else {
                   1553:                         $r->print("<p><b>No password for $username</b><p>");
                   1554:                     }
                   1555:                 }
1.26      matthew  1556:             }
                   1557:         } # end of foreach (@studentdata)
1.55      matthew  1558:         $r->print('<p>Processed Students: '.$count.'</p>');
                   1559:         $r->print("<p>If active, the new role will be available when the ".
                   1560:                   "students next log in to LON-CAPA.</p>");
1.26      matthew  1561:         #####################################
                   1562:         #           Drop students           #
                   1563:         #####################################
1.25      matthew  1564:         if ($ENV{'form.fullup'} eq 'yes') {
                   1565:             $r->print('<h3>Dropping Students</h3>');
1.26      matthew  1566:             #  Get current classlist
1.56      matthew  1567:             my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                   1568:             if (! defined($classlist)) {
                   1569:                 $r->print("There are no students currently enrolled.\n");
                   1570:             } else {
                   1571:                 # Remove the students we just added from the list of students.
1.25      matthew  1572:                 foreach (@studentdata) {
                   1573:                     my %entries=&Apache::loncommon::record_sep($_);
                   1574:                     unless (($entries{$fields{'username'}} eq '') ||
                   1575:                             (!defined($entries{$fields{'username'}}))) {
1.56      matthew  1576:                         delete($classlist->{$entries{$fields{'username'}}.
1.26      matthew  1577:                                                 ':'.$domain});
1.25      matthew  1578:                     }
                   1579:                 }
1.56      matthew  1580:                 # Print out list of dropped students.
                   1581:                 &show_drop_list($r,$classlist,$keylist,'nosort');
1.25      matthew  1582:             }
                   1583:         }
1.26      matthew  1584:     } # end of unless
1.10      www      1585: }
                   1586: 
1.11      www      1587: # ================================================================== Phase four
                   1588: sub drop_student_list {
                   1589:     my $r=shift;
                   1590:     my $count=0;
1.35      matthew  1591:     my @droplist;
                   1592:     if (ref($ENV{'form.droplist'})) {
                   1593:         @droplist = @{$ENV{'form.droplist'}};
                   1594:     } else {
                   1595:         @droplist = ($ENV{'form.droplist'});
                   1596:     }
                   1597:     foreach (@droplist) {
1.26      matthew  1598:         my ($uname,$udom)=split(/\:/,$_);
1.56      matthew  1599:         # drop student
1.35      matthew  1600:         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
1.37      matthew  1601:         if ($result eq 'ok' || $result eq 'ok:') {
1.54      matthew  1602:             $r->print('Dropped '.$uname.' @ '.$udom.'<br>');
1.59      matthew  1603:             $count++;
1.35      matthew  1604:         } else {
1.54      matthew  1605:             $r->print('Error dropping '.$uname.' @ '.$udom.': '.$result.
1.35      matthew  1606:                       '<br />');
                   1607:         }
1.20      harris41 1608:     }
1.11      www      1609:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
1.59      matthew  1610:     $r->print('<p>Re-enrollment will re-activate data.') if ($count);
1.11      www      1611: }
                   1612: 
1.50      matthew  1613: ###################################################################
                   1614: ###################################################################
                   1615: 
                   1616: =pod
                   1617: 
                   1618: =item &handler
                   1619: 
                   1620: The typical handler you see in all these modules.  Takes $r, the
                   1621: http request, as an argument.  
                   1622: 
                   1623: The response to the request is governed by two form variables
                   1624: 
                   1625:  form.action      form.state     response
                   1626:  ---------------------------------------------------
                   1627:  undefined        undefined      print main menu
                   1628:  upload           undefined      print courselist upload menu
                   1629:  upload           got_file       deal with uploaded file,
                   1630:                                  print the upload managing menu
                   1631:  upload           enrolling      enroll students based on upload
                   1632:  drop             undefined      print the classlist ready to drop
                   1633:  drop             done           drop the selected students
                   1634:  enrollstudent    undefined      print single student enroll menu
                   1635:  enrollstudent    enrolling      enroll student
                   1636:  classlist        undefined      print html classlist
                   1637:  classlist        csv            print csv classlist
                   1638:  modifystudent    undefined      print classlist to select student to modify
                   1639:  modifystudent    selected       print modify student menu
                   1640:  modifystudent    done           make modifications to student record
                   1641: 
                   1642: =cut
                   1643: 
                   1644: ###################################################################
                   1645: ###################################################################
1.10      www      1646: sub handler {
1.26      matthew  1647:     my $r=shift;
                   1648:     if ($r->header_only) {
                   1649:         $r->content_type('text/html');
                   1650:         $r->send_http_header;
                   1651:         return OK;
                   1652:     }
1.48      matthew  1653:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.50      matthew  1654:                                             ['action','state']);
1.26      matthew  1655:     #  Needs to be in a course
1.50      matthew  1656:     if (! (($ENV{'request.course.fn'}) &&
                   1657:           (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
                   1658:         # Not in a course, or not allowed to modify parms
                   1659:         $ENV{'user.error.msg'}=
                   1660:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
                   1661:         return HTTP_NOT_ACCEPTABLE; 
                   1662:     }
                   1663:     #
                   1664:     # Only output the header information if they did not request csv format
                   1665:     #
                   1666:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
                   1667:         $r->content_type('text/csv');
                   1668:     } else {
1.26      matthew  1669:         # Start page
1.50      matthew  1670:         $r->content_type('text/html');
1.26      matthew  1671:         $r->send_http_header;
1.50      matthew  1672:         $r->print(&header());
                   1673:     }
                   1674:     #
                   1675:     # Main switch on form.action and form.state, as appropriate
                   1676:     if (! exists($ENV{'form.action'})) {
                   1677:         &print_main_menu($r);
                   1678:     } elsif ($ENV{'form.action'} eq 'upload') {
                   1679:         if (! exists($ENV{'form.state'})) {
                   1680:             &print_first_courselist_upload_form($r);            
                   1681:         } elsif ($ENV{'form.state'} eq 'got_file') {
                   1682:             &print_upload_manager_form($r);
                   1683:         } elsif ($ENV{'form.state'} eq 'enrolling') {
1.26      matthew  1684:             if ($ENV{'form.datatoken'}) {
                   1685:                 &upfile_drop_add($r);
1.50      matthew  1686:             } else {
                   1687:                 # Hmmm, this is an error
1.26      matthew  1688:             }
1.50      matthew  1689:         } else {
                   1690:             &print_first_courselist_upload_form($r);            
1.26      matthew  1691:         }
1.50      matthew  1692:     } elsif ($ENV{'form.action'} eq 'drop') {
                   1693:         if (! exists($ENV{'form.state'})) {
1.51      matthew  1694:             &print_drop_menu($r);
1.50      matthew  1695:         } elsif ($ENV{'form.state'} eq 'done') {
1.26      matthew  1696:             &drop_student_list($r);
1.50      matthew  1697:         } else {
1.55      matthew  1698:             &print_drop_menu($r);
1.26      matthew  1699:         }
1.50      matthew  1700:     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
                   1701:         if (! exists($ENV{'form.state'})) {
                   1702:             &print_enroll_single_student_form($r);
                   1703:         } elsif ($ENV{'form.state'} eq 'enrolling') {
1.26      matthew  1704:             &enroll_single_student($r);
1.50      matthew  1705:         } else {
                   1706:             &print_enroll_single_student_form($r);
1.26      matthew  1707:         }
1.50      matthew  1708:     } elsif ($ENV{'form.action'} eq 'classlist') {
                   1709:         if (! exists($ENV{'form.state'})) {
                   1710:             &print_html_classlist($r);
                   1711:         } elsif ($ENV{'form.state'} eq 'csv') {
1.60      matthew  1712:             &print_formatted_classlist($r,'csv');
                   1713:         } elsif ($ENV{'form.state'} eq 'excel') {
                   1714:             &print_formatted_classlist($r,'excel');
1.50      matthew  1715:         } else {
                   1716:             &print_html_classlist($r);
                   1717:         }
                   1718:     } elsif ($ENV{'form.action'} eq 'modifystudent') {
                   1719:         if (! exists($ENV{'form.state'})) {
                   1720:             &print_html_classlist($r);
                   1721:         } elsif ($ENV{'form.state'} eq 'selected') {
                   1722:             &print_modify_student_form($r);
                   1723:         } elsif ($ENV{'form.state'} eq 'done') {
                   1724:             &modify_single_student($r);
                   1725:         } else {
                   1726:             &print_html_classlist($r);
                   1727:         }        
                   1728:     } else {
                   1729:         # We should not end up here, but I guess it is possible
                   1730:         &Apache::lonnet::logthis("Undetermined state in londropadd.pm.  ".
                   1731:                                  "form.action = ".$ENV{'form.action'}.
                   1732:                                  "Someone should fix this.");
                   1733:         &print_main_menu($r);
                   1734:     }
                   1735:     #
                   1736:     # Finish up
                   1737:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
                   1738:         $r->print("\n");
1.26      matthew  1739:     } else {
1.50      matthew  1740:         $r->print('</form></body></html>');
1.26      matthew  1741:     }
                   1742:     return OK;
1.1       www      1743: }
                   1744: 
1.50      matthew  1745: ###################################################################
                   1746: ###################################################################
                   1747: 
1.1       www      1748: 1;
                   1749: __END__
1.50      matthew  1750: 
1.1       www      1751: 

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