1: # The LearningOnline Network with CAPA
2: # Handler to drop and add students in courses
3: #
4: # $Id: londropadd.pm,v 1.112 2004/05/19 17:53:43 matthew Exp $
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: #
28: #
29: ###############################################################
30: ##############################################################
31:
32: package Apache::londropadd;
33:
34: use strict;
35: use Apache::lonnet();
36: use Apache::loncommon();
37: use Apache::lonhtmlcommon();
38: use Apache::Constants qw(:common :http REDIRECT);
39: use Spreadsheet::WriteExcel;
40: use Apache::lonstathelpers();
41: use Apache::lonlocal;
42:
43: ###############################################################
44: ###############################################################
45: sub header {
46: my $bodytag=&Apache::loncommon::bodytag('Enrollment Manager');
47: my $title = &mt('LON-CAPA Enrollment Manager');
48: return(<<ENDHEAD);
49: <html>
50: <head>
51: <title>$title</title>
52: </head>
53: $bodytag
54: <form method="post" enctype="multipart/form-data"
55: action="/adm/dropadd" name="studentform">
56: ENDHEAD
57: }
58:
59: ###############################################################
60: ###############################################################
61: # Drop student from all sections of a course, except optional $csec
62: sub modifystudent {
63: my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
64: # if $csec is undefined, drop the student from all the courses matching
65: # this one. If $csec is defined, drop them from all other sections of
66: # this course and add them to section $csec
67: $courseid=~s/\_/\//g;
68: $courseid=~s/^(\w)/\/$1/;
69: my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
70: my ($tmp) = keys(%roles);
71: # Bail out if we were unable to get the students roles
72: return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
73: # Go through the roles looking for enrollment in this course
74: my $result = '';
75: foreach my $course (keys(%roles)) {
76: if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
77: # We are in this course
78: my $section=$1;
79: $section='' if ($course eq $courseid.'_st');
80: if (defined($csec) && $section eq $csec) {
81: $result .= 'ok:';
82: } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
83: my (undef,$end,$start)=split(/\_/,$roles{$course});
84: my $now=time;
85: # if this is an active role
86: if (!($start && ($now<$start)) || !($end && ($now>$end))) {
87: my $reply=&Apache::lonnet::modifystudent
88: # dom name id mode pass f m l g
89: ($udom,$unam,'', '', '',undef,undef,undef,undef,
90: $section,time,undef,undef,$desiredhost);
91: $result .= $reply.':';
92: }
93: }
94: }
95: }
96: if ($result eq '') {
97: $result = 'Unable to find section for this student';
98: } else {
99: $result =~ s/(ok:)+/ok/g;
100: }
101: return $result;
102: }
103:
104: ###############################################################
105: ###############################################################
106: # build a domain and server selection form
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
123: my %servers = &Apache::loncommon::get_library_servers($dom);
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:
135: ###############################################################
136: ###############################################################
137: # Menu Phase One
138: sub print_main_menu {
139: my $r=shift;
140: my %Text = &Apache::lonlocal::texthash
141: ('upload' => 'Upload a class list',
142: 'enrollone' => 'Enroll a single student',
143: 'modify' => 'Modify student data',
144: 'view' => 'View Class List',
145: 'drop' => 'Drop Students',
146: 'populate' => 'Automated Enrollment Manager');
147: my %help=();
148: foreach ('Course_Drop_Student','Course_Add_Student',
149: 'Course_Modify_Student_Data','Course_View_Class_List',
150: 'Course_Create_Class_List') {
151: $help{$_}=&Apache::loncommon::help_open_topic($_);
152: }
153:
154: $r->print(<<END);
155: <p>
156: <font size="+1">
157: <a href="/adm/dropadd?action=upload">$Text{'upload'}</a>
158: </font>$help{'Course_Create_Class_List'}
159: </p><p>
160: <font size="+1">
161: <a href="/adm/dropadd?action=enrollstudent">$Text{'enrollone'}</a>
162: </font>$help{'Course_Add_Student'}
163: </p><p>
164: <font size="+1">
165: <a href="/adm/dropadd?action=modifystudent">$Text{'modify'}</a>
166: </font>$help{'Course_Modify_Student_Data'}
167: </p><p>
168: <font size="+1">
169: <a href="/adm/dropadd?action=classlist">$Text{'view'}</a>
170: </font>$help{'Course_View_Class_List'}
171: </p><p>
172: <font size="+1">
173: <a href="/adm/dropadd?action=drop">$Text{'drop'}</a>
174: </font>$help{'Course_Drop_Student'}
175: </p><p>
176: <font size="+1">
177: <a href="/adm/populate">$Text{'populate'}</a>
178: </font>
179: END
180: }
181:
182: ###############################################################
183: ###############################################################
184: sub hidden_input {
185: my ($name,$value) = @_;
186: return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
187: }
188:
189: sub print_upload_manager_header {
190: my ($r,$datatoken,$distotal,$krbdefdom)=@_;
191: my $javascript;
192: #
193: if (! exists($ENV{'form.upfile_associate'})) {
194: $ENV{'form.upfile_associate'} = 'forward';
195: }
196: if ($ENV{'form.associate'} eq 'Reverse Association') {
197: if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
198: $ENV{'form.upfile_associate'} = 'reverse';
199: } else {
200: $ENV{'form.upfile_associate'} = 'forward';
201: }
202: }
203: if ($ENV{'form.upfile_associate'} eq 'reverse') {
204: $javascript=&upload_manager_javascript_reverse_associate();
205: } else {
206: $javascript=&upload_manager_javascript_forward_associate();
207: }
208: #
209: # Deal with restored settings
210: my $password_choice = '';
211: if (exists($ENV{'form.ipwd_choice'}) &&
212: $ENV{'form.ipwd_choice'} ne '') {
213: # If a column was specified for password, assume it is for an
214: # internal password. This is a bug waiting to be filed (could be
215: # local or krb auth instead of internal) but I do not have the
216: # time to mess around with this now.
217: $password_choice = 'int';
218: }
219: #
220: my $javascript_validations=&javascript_validations('auth',$krbdefdom,
221: $password_choice);
222: my $checked=(($ENV{'form.noFirstLine'})?' checked="1"':'');
223: $r->print('<h3>'.&mt('Uploading Class List')."</h3>\n".
224: "<hr>\n".
225: '<h3>'.&mt('Identify fields')."</h3>\n");
226: $r->print("<p>\n".
227: &mt('Total number of records found in file: [_1].',$distotal).
228: "\n".
229: "</p><hr>\n");
230: $r->print(&mt('Enter as many fields as you can. The system will inform you and bring you back to this page if the data selected is insufficient to enroll students in your class.')."<hr>\n");
231: $r->print(&hidden_input('action','upload').
232: &hidden_input('state','got_file').
233: &hidden_input('associate','').
234: &hidden_input('datatoken',$datatoken).
235: &hidden_input('fileupload',$ENV{'form.fileupload'}).
236: &hidden_input('upfiletype',$ENV{'form.upfiletype'}).
237: &hidden_input('upfile_associate',$ENV{'form.upfile_associate'}));
238: $r->print('<input type="button" value="Reverse Association" '.
239: 'name="'.&mt('Reverse Association').'" '.
240: 'onClick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
241: $r->print('<input type="checkbox" name="noFirstLine" $checked />'.
242: &mt('Ignore First Line'));
243: $r->print("<hr />\n".
244: '<script type="text/javascript" language="Javascript">'."\n".
245: $javascript."\n".$javascript_validations.'</script>');
246: }
247:
248: ###############################################################
249: ###############################################################
250: sub javascript_validations {
251: my ($mode,$krbdefdom,$curr_authtype,$curr_authfield)=@_;
252: my $authheader;
253: if ($mode eq 'auth') {
254: my %param = ( formname => 'studentform',
255: kerb_def_dom => $krbdefdom,
256: curr_authtype => $curr_authtype);
257: $authheader = &Apache::loncommon::authform_header(%param);
258: } elsif ($mode eq 'createcourse') {
259: my %param = ( formname => 'ccrs',
260: kerb_def_dom => $krbdefdom,
261: curr_authtype => $curr_authtype );
262: $authheader = &Apache::loncommon::authform_header(%param);
263: } elsif ($mode eq 'modifycourse') {
264: my %param = ( formname => 'cmod',
265: kerb_def_dom => $krbdefdom,
266: mode => 'modifycourse',
267: curr_authtype => $curr_authtype,
268: curr_autharg => $curr_authfield );
269: $authheader = &Apache::loncommon::authform_header(%param);
270: }
271:
272:
273: my %alert = &Apache::lonlocal::texthash
274: (username => 'You need to specify the username field.',
275: authen => 'You must choose an authentication type.',
276: krb => 'You need to specify the Kerberos domain.',
277: ipass => 'You need to specify the initial password.',
278: name => 'The optional name field was not specified.',
279: snum => 'The optional student number field was not specified.',
280: section => 'The optional section or group field was not specified.',
281: email => 'The optional email address field was not specified.',
282: continue => 'Continue enrollment?',
283: );
284:
285: # my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
286: my $function_name =(<<END);
287: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
288: END
289: my $auth_checks;
290: if ($mode eq 'createcourse') {
291: $auth_checks .= (<<END);
292: if (vf.autoadds[0].checked == true) {
293: if (current.radiovalue == null || current.radiovalue == 'nochange') {
294: alert('$alert{'authen'}');
295: return;
296: }
297: }
298: END
299: } else {
300: $auth_checks .= (<<END);
301: var foundatype=0;
302: if (founduname==0) {
303: alert('$alert{'username'}');
304: return;
305: }
306: // alert('current.radiovalue = '+current.radiovalue);
307: if (current.radiovalue == null || current.radiovalue == 'nochange') {
308: // They did not check any of the login radiobuttons.
309: alert('$alert{'authen'}');
310: return;
311: }
312: END
313: }
314: if ($mode eq 'createcourse') {
315: $auth_checks .= "
316: if ( (vf.autoadds[0].checked == true) &&
317: (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
318: ";
319: } elsif ($mode eq 'modifycourse') {
320: $auth_checks .= "
321: if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
322: ";
323: }
324: if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
325: $auth_checks .= (<<END);
326: var alertmsg = '';
327: switch (current.radiovalue) {
328: case 'krb':
329: alertmsg = '$alert{'krb'}';
330: break;
331: default:
332: alertmsg = '';
333: }
334: if (alertmsg != '') {
335: alert(alertmsg);
336: return;
337: }
338: }
339: END
340: } else {
341: $auth_checks .= (<<END);
342: foundatype=1;
343: if (current.argfield == null || current.argfield == '') {
344: var alertmsg = '';
345: switch (current.value) {
346: case 'krb':
347: alertmsg = '$alert{'krb'}';
348: break;
349: case 'loc':
350: case 'fsys':
351: alertmsg = '$alert{'ipass'}';
352: break;
353: case 'fsys':
354: alertmsg = '';
355: break;
356: default:
357: alertmsg = '';
358: }
359: if (alertmsg != '') {
360: alert(alertmsg);
361: return;
362: }
363: }
364: END
365: }
366: my $optional_checks = '';
367: if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
368: $optional_checks = (<<END);
369: vf.submit();
370: }
371: END
372: } else {
373: $optional_checks = (<<END);
374: var message='';
375: if (foundname==0) {
376: message='$alert{'name'}';
377: }
378: if (foundid==0) {
379: if (message!='') {
380: message+='\\n';
381: }
382: message+='$alert{'snum'}';
383: }
384: if (foundsec==0) {
385: if (message!='') {
386: message+='\\n';
387: }
388: message+='$alert{'section'}';
389: }
390: if (foundemail==0) {
391: if (message!='') {
392: message+='\\n';
393: }
394: message+='$alert{'email'}';
395: }
396: if (message!='') {
397: message+= '\\n$alert{'continue'}';
398: if (confirm(message)) {
399: vf.state.value='enrolling';
400: vf.submit();
401: }
402: } else {
403: vf.state.value='enrolling';
404: vf.submit();
405: }
406: }
407: END
408: }
409: my $result = $function_name;
410: if ( ($mode eq 'auth') || ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
411: $result .= $auth_checks;
412: }
413: $result .= $optional_checks;
414: if ( ($mode eq 'auth') || ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
415: $result .= $authheader;
416: }
417: return $result;
418: }
419:
420: ###############################################################
421: ###############################################################
422: sub upload_manager_javascript_forward_associate {
423: return(<<ENDPICK);
424: function verify(vf) {
425: var founduname=0;
426: var foundpwd=0;
427: var foundname=0;
428: var foundid=0;
429: var foundsec=0;
430: var foundemail=0;
431: var tw;
432: for (i=0;i<=vf.nfields.value;i++) {
433: tw=eval('vf.f'+i+'.selectedIndex');
434: if (tw==1) { founduname=1; }
435: if ((tw>=2) && (tw<=6)) { foundname=1; }
436: if (tw==7) { foundid=1; }
437: if (tw==8) { foundsec=1; }
438: if (tw==9) { foundpwd=1; }
439: if (tw==10) { foundemail=1; }
440: }
441: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail);
442: }
443:
444: //
445: // vf = this.form
446: // tf = column number
447: //
448: // values of nw
449: //
450: // 0 = none
451: // 1 = username
452: // 2 = names (lastname, firstnames)
453: // 3 = fname (firstname)
454: // 4 = mname (middlename)
455: // 5 = lname (lastname)
456: // 6 = gen (generation)
457: // 7 = id
458: // 8 = section
459: // 9 = ipwd (password)
460: // 10 = email address
461:
462: function flip(vf,tf) {
463: var nw=eval('vf.f'+tf+'.selectedIndex');
464: var i;
465: // make sure no other columns are labeled the same as this one
466: for (i=0;i<=vf.nfields.value;i++) {
467: if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
468: eval('vf.f'+i+'.selectedIndex=0;')
469: }
470: }
471: // If we set this to 'lastname, firstnames', clear out all the ones
472: // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
473: if (nw==2) {
474: for (i=0;i<=vf.nfields.value;i++) {
475: if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
476: (eval('vf.f'+i+'.selectedIndex')<=6)) {
477: eval('vf.f'+i+'.selectedIndex=0;')
478: }
479: }
480: }
481: // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
482: // clear out any that are set to 'lastname, firstnames' (2)
483: if ((nw>=3) && (nw<=6)) {
484: for (i=0;i<=vf.nfields.value;i++) {
485: if (eval('vf.f'+i+'.selectedIndex')==2) {
486: eval('vf.f'+i+'.selectedIndex=0;')
487: }
488: }
489: }
490: // If we set the password, make the password form below correspond to
491: // the new value.
492: if (nw==9) {
493: changed_radio('int',document.studentform);
494: set_auth_radio_buttons('int',document.studentform);
495: vf.intarg.value='';
496: vf.krbarg.value='';
497: vf.locarg.value='';
498: }
499: }
500:
501: function clearpwd(vf) {
502: var i;
503: for (i=0;i<=vf.nfields.value;i++) {
504: if (eval('vf.f'+i+'.selectedIndex')==9) {
505: eval('vf.f'+i+'.selectedIndex=0;')
506: }
507: }
508: }
509:
510: ENDPICK
511: }
512:
513: ###############################################################
514: ###############################################################
515: sub upload_manager_javascript_reverse_associate {
516: return(<<ENDPICK);
517: function verify(vf) {
518: var founduname=0;
519: var foundpwd=0;
520: var foundname=0;
521: var foundid=0;
522: var foundsec=0;
523: var tw;
524: for (i=0;i<=vf.nfields.value;i++) {
525: tw=eval('vf.f'+i+'.selectedIndex');
526: if (i==0 && tw!=0) { founduname=1; }
527: if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
528: if (i==6 && tw!=0) { foundid=1; }
529: if (i==7 && tw!=0) { foundsec=1; }
530: if (i==8 && tw!=0) { foundpwd=1; }
531: }
532: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
533: }
534:
535: function flip(vf,tf) {
536: var nw=eval('vf.f'+tf+'.selectedIndex');
537: var i;
538: // picked the all one one name field, reset the other name ones to blank
539: if (tf==1 && nw!=0) {
540: for (i=2;i<=5;i++) {
541: eval('vf.f'+i+'.selectedIndex=0;')
542: }
543: }
544: //picked one of the piecewise name fields, reset the all in
545: //one field to blank
546: if ((tf>=2) && (tf<=5) && (nw!=0)) {
547: eval('vf.f1.selectedIndex=0;')
548: }
549: // intial password specified, pick internal authentication
550: if (tf==8 && nw!=0) {
551: changed_radio('int',document.studentform);
552: set_auth_radio_buttons('int',document.studentform);
553: vf.krbarg.value='';
554: vf.intarg.value='';
555: vf.locarg.value='';
556: }
557: }
558:
559: function clearpwd(vf) {
560: var i;
561: if (eval('vf.f8.selectedIndex')!=0) {
562: eval('vf.f8.selectedIndex=0;')
563: }
564: }
565: ENDPICK
566: }
567:
568: ###############################################################
569: ###############################################################
570: sub print_upload_manager_footer {
571: my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
572:
573: my ($krbdef,$krbdefdom) =
574: &Apache::loncommon::get_kerberos_defaults($defdom);
575: my %param = ( formname => 'document.studentform',
576: kerb_def_dom => $krbdefdom,
577: kerb_def_auth => $krbdef
578: );
579: if (exists($ENV{'form.ipwd_choice'}) &&
580: defined($ENV{'form.ipwd_choice'}) &&
581: $ENV{'form.ipwd_choice'} ne '') {
582: $param{'curr_authtype'} = 'int';
583: }
584: my $krbform = &Apache::loncommon::authform_kerberos(%param);
585: my $intform = &Apache::loncommon::authform_internal(%param);
586: my $locform = &Apache::loncommon::authform_local(%param);
587: my $domform = &domain_form($defdom);
588: my $date_table = &date_setting_table();
589: my $Str = "</table>\n";
590: $Str .= &hidden_input('nfields',$i);
591: $Str .= &hidden_input('keyfields',$keyfields);
592: $Str .= '<h3>'.&mt('Login Type')."</h3>\n";
593: $Str .= "<p>\n".
594: &mt('Note: this will not take effect if the user already exists').
595: "</p><p>\n";
596: $Str .= $krbform."\n</p><p>\n".
597: $intform."\n</p><p>\n".
598: $locform."\n</p>\n";
599: $Str .= '<h3>'.&mt('LON-CAPA Domain for Students')."</h3>\n";
600: $Str .= "<p>\n".&mt('LON-CAPA domain: [_1]',$domform)."\n</p>\n";
601: $Str .= "<h3>".&mt('Starting and Ending Dates')."</h3>\n";
602: $Str .= "<p>\n".$date_table."</p>\n";
603: $Str .= "<h3>".&mt('Full Update')."</h3>\n";
604: $Str .= '<input type="checkbox" name="fullup" value="yes">'.
605: ' '.&mt('Full update (also print list of users not enrolled anymore)').
606: "</p>\n";
607: $Str .= "<h3>".&mt('Student Number')."</h3>\n";
608: $Str .= "<p>\n".'<input type="checkbox" name="forceid" value="yes">';
609: $Str .= &mt('Disable ID/Student Number Safeguard and Force Change '.
610: 'of Conflicting IDs (only do if you know what you are doing)').
611: "\n</p><p>\n";
612: $Str .= '<input type="button" onClick="javascript:verify(this.form)" '.
613: 'value="Update Class List" />'."<br />\n";
614: $Str .= &mt('Note: for large courses, this operation may be time '.
615: 'consuming');
616: $r->print($Str);
617: return;
618: }
619:
620: ###############################################################
621: ###############################################################
622: sub print_upload_manager_form {
623: my $r=shift;
624:
625: my $firstLine;
626: my $datatoken;
627: if (!$ENV{'form.datatoken'}) {
628: $datatoken=&Apache::loncommon::upfile_store($r);
629: } else {
630: $datatoken=$ENV{'form.datatoken'};
631: &Apache::loncommon::load_tmp_file($r);
632: }
633: my @records=&Apache::loncommon::upfile_record_sep();
634: if($ENV{'form.noFirstLine'}){
635: $firstLine=shift(@records);
636: }
637: my $total=$#records;
638: my $distotal=$total+1;
639: my $today=time;
640: my $halfyear=$today+15552000;
641: #
642: # Restore memorized settings
643: &Apache::loncommon::restore_course_settings
644: ('enrollment_upload',{ 'username_choice' => 'scalar', # column settings
645: 'names_choice' => 'scalar',
646: 'fname_choice' => 'scalar',
647: 'mname_choice' => 'scalar',
648: 'lname_choice' => 'scalar',
649: 'gen_choice' => 'scalar',
650: 'id_choice' => 'scalar',
651: 'sec_choice' => 'scalar',
652: 'ipwd_choice' => 'scalar',
653: 'email_choice' => 'scalar',
654: });
655: #
656: # Determine kerberos parameters as appropriate
657: my $defdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
658: my ($krbdef,$krbdefdom) =
659: &Apache::loncommon::get_kerberos_defaults($defdom);
660: #
661: &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
662: my $i;
663: my $keyfields;
664: if ($total>=0) {
665: my @field=
666: (['username',&mt('Username'), $ENV{'form.username_choice'}],
667: ['names',&mt('Last Name, First Names'),$ENV{'form.names_choice'}],
668: ['fname',&mt('First Name'), $ENV{'form.fname_choice'}],
669: ['mname',&mt('Middle Names/Initials'),$ENV{'form.mname_choice'}],
670: ['lname',&mt('Last Name'), $ENV{'form.lname_choice'}],
671: ['gen', &mt('Generation'), $ENV{'form.gen_choice'}],
672: ['id', &mt('ID/Student Number'),$ENV{'form.id_choice'}],
673: ['sec', &mt('Group/Section'), $ENV{'form.sec_choice'}],
674: ['ipwd', &mt('Initial Password'),$ENV{'form.ipwd_choice'}],
675: ['email',&mt('EMail Address'), $ENV{'form.email_choice'}]);
676: if ($ENV{'form.upfile_associate'} eq 'reverse') {
677: &Apache::loncommon::csv_print_samples($r,\@records);
678: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
679: \@field);
680: foreach (@field) {
681: $keyfields.=$_->[0].',';
682: }
683: chop($keyfields);
684: } else {
685: unshift(@field,['none','']);
686: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
687: \@field);
688: my %sone=&Apache::loncommon::record_sep($records[0]);
689: $keyfields=join(',',sort(keys(%sone)));
690: }
691: }
692: &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
693: }
694:
695: ###############################################################
696: ###############################################################
697: sub enroll_single_student {
698: my $r=shift;
699: # Remove non alphanumeric values from section
700: $ENV{'form.csec'}=~s/\W//g;
701: #
702: # We do the dates first because the action of making them the defaul
703: # in the course is entirely separate from the action of enrolling the
704: # student. Also, a failure in setting the dates as default is not fatal
705: # to the process of enrolling / modifying a student.
706: my ($startdate,$enddate) = &get_dates_from_form();
707: if ($ENV{'form.makedatesdefault'}) {
708: $r->print(&make_dates_default($startdate,$enddate));
709: }
710:
711: $r->print('<h3>'.&mt('Enrolling Student').'</h3>');
712: $r->print('<p>'.&mt('Enrolling').' '.$ENV{'form.cuname'}." \@ ".
713: $ENV{'form.lcdomain'}.'</p>');
714: if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
715: ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
716: # Deal with home server selection
717: my $domain=$ENV{'form.lcdomain'};
718: my $desiredhost = $ENV{'form.lcserver'};
719: if (lc($desiredhost) eq 'default') {
720: $desiredhost = undef;
721: } else {
722: my %home_servers =&Apache::loncommon::get_library_servers($domain);
723: if (! exists($home_servers{$desiredhost})) {
724: $r->print('<font color="#ff0000">'.&mt('Error').':</font>'.
725: &mt('Invalid home server specified'));
726: return;
727: }
728: }
729: $r->print(" ".&mt('with server')." $desiredhost :") if (defined($desiredhost));
730: # End of home server selection logic
731: my $amode='';
732: my $genpwd='';
733: if ($ENV{'form.login'} eq 'krb') {
734: $amode='krb';
735: $amode.=$ENV{'form.krbver'};
736: $genpwd=$ENV{'form.krbarg'};
737: } elsif ($ENV{'form.login'} eq 'int') {
738: $amode='internal';
739: $genpwd=$ENV{'form.intarg'};
740: } elsif ($ENV{'form.login'} eq 'loc') {
741: $amode='localauth';
742: $genpwd=$ENV{'form.locarg'};
743: if (!$genpwd) { $genpwd=" "; }
744: }
745: my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
746: $ENV{'form.lcdomain'});
747: if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
748: # Clean out any old roles the student has in this class.
749: &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
750: $ENV{'request.course.id'},$ENV{'form.csec'},
751: $desiredhost);
752: my $login_result = &Apache::lonnet::modifystudent
753: ($ENV{'form.lcdomain'},$ENV{'form.cuname'},
754: $ENV{'form.cstid'},$amode,$genpwd,
755: $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
756: $ENV{'form.clast'},$ENV{'form.cgen'},
757: $ENV{'form.csec'},$enddate,
758: $startdate,$ENV{'form.forceid'},
759: $desiredhost);
760: if ($login_result =~ /^ok/) {
761: $r->print($login_result);
762: $r->print("<p> ".&mt('If active, the new role will be available when the student next logs in to LON-CAPA.')."</p>");
763: } else {
764: $r->print(&mt('unable to enroll').": ".$login_result);
765: }
766: } else {
767: $r->print('<p><font color="#ff0000">'.&mt('ERROR').'</font> ');
768: if ($amode =~ /^krb/) {
769: $r->print(&mt('Missing Kerberos domain information.').' ');
770: } else {
771: $r->print(&mt('Invalid login mode or password.').' ');
772: }
773: $r->print('<b>'.&mt('Unable to enroll').' '.$ENV{'form.cuname'}.'.</b></p>');
774: }
775: } else {
776: $r->print(&mt('Invalid username or domain'));
777: }
778: }
779:
780: sub setup_date_selectors {
781: my ($starttime,$endtime,$mode) = @_;
782: if (! defined($starttime)) {
783: $starttime = time;
784: unless ($mode eq 'createcourse') {
785: if (exists($ENV{'course.'.$ENV{'request.course.id'}.
786: '.default_enrollment_start_date'})) {
787: $starttime = $ENV{'course.'.$ENV{'request.course.id'}.
788: '.default_enrollment_start_date'};
789: }
790: }
791: }
792: if (! defined($endtime)) {
793: $endtime = time+(6*30*24*60*60); # 6 months from now, approx
794: unless ($mode eq 'createcourse') {
795: if (exists($ENV{'course.'.$ENV{'request.course.id'}.
796: '.default_enrollment_end_date'})) {
797: $endtime = $ENV{'course.'.$ENV{'request.course.id'}.
798: '.default_enrollment_end_date'};
799: }
800: }
801: }
802: my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
803: 'startdate',
804: $starttime);
805: my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
806: 'enddate',
807: $endtime);
808: if ($mode eq 'createcourse') {
809: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
810: 'startdate',
811: $starttime);
812: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
813: 'enddate',
814: $endtime);
815: }
816: return ($startdateform,$enddateform);
817: }
818:
819: sub get_dates_from_form {
820: my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
821: my $enddate = &Apache::lonhtmlcommon::get_date_from_form('enddate');
822: if ($ENV{'form.no_end_date'}) {
823: $enddate = 0;
824: }
825: return ($startdate,$enddate);
826: }
827:
828: sub date_setting_table {
829: my ($starttime,$endtime,$mode) = @_;
830: my ($startform,$endform)=&setup_date_selectors($starttime,$endtime,$mode);
831: my $dateDefault = '<nobr>'.
832: '<input type="checkbox" name="makedatesdefault" /> '.
833: &mt('make these dates the default for future enrollment');
834: if ($mode eq 'createcourse') {
835: $dateDefault = ' ';
836: }
837: my $perpetual = '<nobr><input type="checkbox" name="no_end_date"';
838: if (defined($endtime) && $endtime == 0) {
839: $perpetual .= ' checked';
840: }
841: $perpetual.= ' /> '.&mt('no ending date').'</nobr>';
842: my $result = '';
843: $result .= "<table>\n";
844: $result .= '<tr><td align="right">'.&mt('Starting Date').'</td>'.
845: '<td>'.$startform.'</td>'.
846: '<td>'.$dateDefault.'</td>'."</tr>\n";
847: $result .= '<tr><td align="right">'.&mt('Ending Date').'</td>'.
848: '<td>'.$endform.'</td>'.
849: '<td>'.$perpetual.'</td>'."</tr>\n";
850: $result .= "</table>\n";
851: return $result;
852: }
853:
854: sub make_dates_default {
855: my ($startdate,$enddate) = @_;
856: my $result = '';
857: my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
858: my $crs = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
859: my $put_result = &Apache::lonnet::put('environment',
860: {'default_enrollment_start_date'=>$startdate,
861: 'default_enrollment_end_date' =>$enddate},$dom,$crs);
862: if ($put_result eq 'ok') {
863: $result .= "Set default start and end dates for course<br />";
864: #
865: # Refresh the course environment
866: &Apache::lonnet::coursedescription($ENV{'request.course.id'});
867: } else {
868: $result .= &mt('Unable to set default dates for course').":".$put_result.
869: '<br />';
870: }
871: return $result;
872: }
873:
874: ##
875: ## Single student enrollment routines (some of them)
876: ##
877: sub get_student_username_domain_form {
878: my $r = shift;
879: my $domform = &Apache::loncommon::select_dom_form
880: ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'},'cudomain',0);
881: my %lt=&Apache::lonlocal::texthash(
882: 'eos' => "Enroll One Student",
883: 'usr' => "Username",
884: 'dom' => "Domain",
885: 'been' => "Begin Enrollment",
886: );
887: $r->print(<<END);
888: <input type="hidden" name="action" value="enrollstudent" />
889: <input type="hidden" name="state" value="gotusername" />
890: <h3>$lt{'eos'}</h3>
891: <table>
892: <tr><th>$lt{'usr'}:</th>
893: <td><input type="text" name="cuname" size="15" /></td></tr>
894: <tr><th>$lt{'dom'}:</th>
895: <td>$domform</td></tr>
896: <tr><th> </th>
897: <td>
898: <input type="submit" name="Begin Enrollment" value="$lt{'been'}" />
899: </td></tr>
900: </table>
901: END
902: return;
903: }
904:
905: sub print_enroll_single_student_form {
906: my $r=shift;
907: $r->print("<h3>".&mt('Enroll One Student')."</h3>");
908: #
909: my $username = $ENV{'form.cuname'};
910: my $domain = $ENV{'form.cudomain'};
911: my $home = &Apache::lonnet::homeserver($username,$domain);
912: # $new_user flags whether we are creating a new user or using an old one
913: my $new_user = 1;
914: if ($home ne 'no_host') {
915: $new_user = 0;
916: }
917: #
918: my $user_data_html = '';
919: my $javascript_validations = '';
920: if ($new_user) {
921: my $defdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
922: # Set up authentication forms
923: my ($krbdef,$krbdefdom) =
924: &Apache::loncommon::get_kerberos_defaults($domain);
925: $javascript_validations=&javascript_validations('auth',$krbdefdom);
926: my %param = ( formname => 'document.studentform',
927: kerb_def_dom => $krbdefdom,
928: kerb_def_auth => $krbdef
929: );
930: my $krbform = &Apache::loncommon::authform_kerberos(%param);
931: my $intform = &Apache::loncommon::authform_internal(%param);
932: my $locform = &Apache::loncommon::authform_local(%param);
933: #
934: # Set up domain selection form
935: my $homeserver_form = '';
936: my %servers = &Apache::loncommon::get_library_servers($domain);
937: $homeserver_form = '<select name="lcserver" size="1">'."\n".
938: '<option value="default" selected>default</option>'."\n";
939: while (my ($servername,$serverdescription) = each (%servers)) {
940: $homeserver_form .= '<option value="'.$servername.'">'.
941: $serverdescription."</option>\n";
942: }
943: $homeserver_form .= "</select>\n";
944: #
945: #
946: my %lt=&Apache::lonlocal::texthash(
947: 'udf' => "User Data for",
948: 'fn' => "First Name",
949: 'mn' => "Middle Name",
950: 'ln' => "Last Name",
951: 'gen' => "Generation",
952: 'hs' => "Home Server",
953: 'pswd' => "Password",
954: 'psam' => "Please select an authentication mechanism",
955: );
956: $user_data_html = <<END;
957: <h3>$lt{'udf'} $username\@$domain</h3>
958: <table>
959: <tr><th>$lt{'fn'}:</th>
960: <td><input type="text" name="cfirst" size="15"></td></tr>
961: <tr><th>$lt{'mn'}:</th>
962: <td><input type="text" name="cmiddle" size="15"></td></tr>
963: <tr><th>$lt{'ln'}:</th>
964: <td><input type="text" name="clast" size="15"></td></tr>
965: <tr><th>$lt{'gen'}:</th>
966: <td><input type="text" name="cgen" size="5"> </td></tr>
967: <tr><th>$lt{'hs'}:</th>
968: <td>$homeserver_form</td></tr>
969: </table>
970: <h3>$lt{'pswd'}</h3>
971: $lt{'psam'}
972: <table>
973: <p>
974: $krbform
975: <br />
976: $intform
977: <br />
978: $locform
979: </p>
980: END
981: } else {
982: # User already exists. Do not worry about authentication
983: my %uenv = &Apache::lonnet::dump('environment',$domain,$username);
984: $javascript_validations = &javascript_validations('noauth');
985: my %lt=&Apache::lonlocal::texthash(
986: 'udf' => "User Data for",
987: 'fn' => "First Name",
988: 'mn' => "Middle Name",
989: 'ln' => "Last Name",
990: 'gen' => "Generation",
991: );
992: $user_data_html = <<END;
993: <h3>$lt{'udf'} $username\@$domain</h3>
994: <input type="hidden" name="lcserver" value="default" />
995: <table>
996: <tr><th>$lt{'fn'}:</th>
997: <td>
998: <input type="text" name="cfirst" value="$uenv{'firstname'}" size="15" />
999: </td></tr>
1000: <tr><th>$lt{'mn'}:</th>
1001: <td>
1002: <input type="text" name="cmiddle" value="$uenv{'middlename'}" size="15" />
1003: </td></tr>
1004: <tr><th>$lt{'ln'}:</th>
1005: <td>
1006: <input type="text" name="clast"value="$uenv{'lastname'}" size="15" />
1007: </td></tr>
1008: <tr><th>$lt{'gen'}:</th>
1009: <td>
1010: <input type="text" name="cgen" value="$uenv{'generation'}" size="5" />
1011: </td></tr>
1012: </table>
1013: END
1014: }
1015: my $date_table = &date_setting_table();
1016: # Print it all out
1017: my %lt=&Apache::lonlocal::texthash(
1018: 'cd' => "Course Data",
1019: 'gs' => "Group/Section",
1020: 'idsn' => "ID/Student Number",
1021: 'disn' => "Disable ID/Student Number Safeguard and Force Change of Conflicting IDs (only do if you know what you are doing)",
1022: 'eas' => "Enroll as student",
1023: );
1024: $r->print(<<END);
1025: <input type="hidden" name="action" value="enrollstudent" />
1026: <input type="hidden" name="state" value="done" />
1027: <input type="hidden" name="cuname" value="$username" />
1028: <input type="hidden" name="lcdomain" value="$domain" />
1029: <script type="text/javascript" language="Javascript">
1030: function verify(vf) {
1031: var founduname=0;
1032: var foundpwd=0;
1033: var foundname=0;
1034: var foundid=0;
1035: var foundsec=0;
1036: var tw;
1037: if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') &&
1038: (typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
1039: founduname=1;
1040: }
1041: if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
1042: (typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
1043: foundname=1;
1044: }
1045: if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
1046: foundsec=1;
1047: }
1048: if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
1049: foundid=1;
1050: }
1051: if (founduname==0) {
1052: alert('You need to specify at least the username and domain fields');
1053: return;
1054: }
1055: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
1056: }
1057:
1058: $javascript_validations
1059:
1060: function clearpwd(vf) {
1061: //nothing else needs clearing
1062: }
1063:
1064: </script>
1065:
1066: $user_data_html
1067:
1068: <h3>$lt{'cd'}</h3>
1069:
1070: <p>$lt{'gs'}: <input type="text" name="csec" size="5" />
1071: <p>
1072: $date_table
1073: </p>
1074: <h3>$lt{'idsn'}</h3>
1075: <p>
1076: $lt{'idsn'}: <input type="text" name="cstid" size="10">
1077: </p><p>
1078: <input type="checkbox" name="forceid" value="yes">
1079: $lt{'disn'}
1080: </p><p>
1081: <input type="button" onClick="verify(this.form)" value="$lt{'eas'}">
1082: </p>
1083: END
1084: return;
1085: }
1086:
1087: # ========================================================= Menu Phase Two Drop
1088: sub print_drop_menu {
1089: my $r=shift;
1090: $r->print("<h3>".&mt('Drop Students')."</h3>");
1091: my $cid=$ENV{'request.course.id'};
1092: my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
1093: if (! defined($classlist)) {
1094: $r->print(&mt('There are no students currently enrolled.')."\n");
1095: return;
1096: }
1097: # Print out the available choices
1098: &show_drop_list($r,$classlist,$keylist);
1099: return;
1100: }
1101:
1102: # ============================================== view classlist
1103: sub print_html_classlist {
1104: my ($r,$mode) = @_;
1105: if (! exists($ENV{'form.sortby'})) {
1106: $ENV{'form.sortby'} = 'username';
1107: }
1108: if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
1109: $ENV{'form.Status'} = 'Active';
1110: }
1111: my $status_select = &Apache::lonhtmlcommon::StatusOptions
1112: ($ENV{'form.Status'});
1113: my $cid=$ENV{'request.course.id'};
1114: my $cdom=$ENV{'course.'.$cid.'.domain'};
1115: my $cnum=$ENV{'course.'.$cid.'.num'};
1116: #
1117: # List course personnel
1118: my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
1119: #
1120: if (! defined($ENV{'form.output'}) ||
1121: $ENV{'form.output'} !~ /^(csv|excel|html)$/ ) {
1122: $ENV{'form.output'} = 'html';
1123: }
1124: #
1125: $r->print('<br /><table border="2">');
1126: foreach my $role (sort keys %coursepersonnel) {
1127: next if ($role =~ /^\s*$/);
1128: $r->print('<tr><td>'.$role.'</td><td>');
1129: foreach my $user (split(',',$coursepersonnel{$role})) {
1130: my ($puname,$pudom)=split(':',$user);
1131: $r->print(' '.&Apache::loncommon::aboutmewrapper(
1132: &Apache::loncommon::plainname($puname,
1133: $pudom),
1134: $puname,$pudom));
1135: }
1136: $r->print('</td></tr>');
1137: }
1138: $r->print('</table>');
1139: #
1140: # Interface output
1141: $r->print('<input type="hidden" name="action" value="'.
1142: $ENV{'form.action'}.'" />');
1143: $r->print("<p>\n");
1144: if ($ENV{'form.action'} ne 'modifystudent') {
1145: my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
1146: 'excel' => "Excel",
1147: 'html' => 'HTML');
1148: my $output_selector = '<select size="1" name="output" >';
1149: foreach my $outputformat ('html','csv','excel') {
1150: my $option = '<option value="'.$outputformat.'" ';
1151: if ($outputformat eq $ENV{'form.output'}) {
1152: $option .= 'selected ';
1153: }
1154: $option .='>'.$lt{$outputformat}.'</option>';
1155: $output_selector .= "\n".$option;
1156: }
1157: $output_selector .= '</select>';
1158: $r->print(&mt('Output Format: [_1]',$output_selector).(' 'x3));
1159: }
1160: $r->print(&mt('Student Status: [_1]',$status_select)."\n");
1161: $r->print('<input type="submit" value="'.&mt('Update Display').'" />'.
1162: "\n</p>\n");
1163: #
1164: # Print the classlist
1165: $r->print('<h2>'.&mt('Current Class List').'</h2>');
1166: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
1167: if (! defined($classlist)) {
1168: $r->print(&mt('There are no students currently enrolled.')."\n");
1169: } else {
1170: # Print out the available choices
1171: if ($ENV{'form.action'} eq 'modifystudent') {
1172: &show_class_list($r,'view','modify',
1173: $ENV{'form.Status'},$classlist,$keylist);
1174: } else {
1175: &show_class_list($r,$ENV{'form.output'},'aboutme',
1176: $ENV{'form.Status'},$classlist,$keylist);
1177: }
1178: }
1179: }
1180:
1181: # =================================================== Show student list to drop
1182: sub show_class_list {
1183: my ($r,$mode,$linkto,$statusmode,$classlist,$keylist)=@_;
1184: my $cid=$ENV{'request.course.id'};
1185: #
1186: # Variables for excel output
1187: my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
1188: #
1189: # Variables for csv output
1190: my ($CSVfile,$CSVfilename);
1191: #
1192: my $sortby = $ENV{'form.sortby'};
1193: if ($sortby !~ /^(username|domain|section|fullname|id|start|end)$/) {
1194: $sortby = 'username';
1195: }
1196: # Print out header
1197: $r->print(<<END);
1198:
1199: <input type="hidden" name="state" value="$ENV{'form.state'}" />
1200: <input type="hidden" name="sortby" value="$sortby" />
1201: END
1202: if ($mode eq 'html' || $mode eq 'view') {
1203: if ($linkto eq 'aboutme') {
1204: $r->print(&mt('Select a user name to view the users personal page.'));
1205: } elsif ($linkto eq 'modify') {
1206: $r->print(&mt('Select a user name to modify the students information'));
1207: }
1208: my %lt=&Apache::lonlocal::texthash(
1209: 'usrn' => "username",
1210: 'dom' => "domain",
1211: 'sn' => "student name",
1212: 'sec' => "section",
1213: 'start' => "start date",
1214: 'end' => "end date",
1215: );
1216: $r->print(<<END);
1217: <input type="hidden" name="sname" value="" />
1218: <input type="hidden" name="sdom" value="" />
1219: <p>
1220: <table border=2>
1221: <tr><th>Count
1222: </th><th>
1223: <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">$lt{'usrn'}</a>
1224: </th><th>
1225: <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">$lt{'dom'}</a>
1226: </th><th>
1227: <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
1228: </th><th>
1229: <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">$lt{'sn'}</a>
1230: </th><th>
1231: <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">$lt{'sec'}</a>
1232: </th><th>
1233: <a href="javascript:document.studentform.sortby.value='start';document.studentform.submit();">$lt{'start'}</a>
1234: </th><th>
1235: <a href="javascript:document.studentform.sortby.value='end';document.studentform.submit();">$lt{'end'}</a>
1236: </th>
1237: </tr>
1238: END
1239: } elsif ($mode eq 'csv') {
1240: #
1241: # Open a file
1242: $CSVfilename = '/prtspool/'.
1243: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1244: time.'_'.rand(1000000000).'.csv';
1245: unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
1246: $r->log_error("Couldn't open $CSVfilename for output $!");
1247: $r->print("Problems occured in writing the csv file. ".
1248: "This error has been logged. ".
1249: "Please alert your LON-CAPA administrator.");
1250: $CSVfile = undef;
1251: }
1252: #
1253: # Write headers and data to file
1254: if($statusmode eq 'Expired') {
1255: print $CSVfile '"'.&mt('Students with expired roles').'"'."\n";
1256: }
1257: if ($statusmode eq 'Any') {
1258: print $CSVfile '"'.join('","',map {
1259: &Apache::loncommon::csv_translate(&mt($_))
1260: } ("username","domain","ID","student name",
1261: "section","start date","end date","status")).'"'."\n";
1262: } else {
1263: print $CSVfile '"'.join('","',map {
1264: &Apache::loncommon::csv_translate(&mt($_))
1265: } ("username","domain","ID","student name",
1266: "section","start date","end date")).'"'."\n";
1267: }
1268: } elsif ($mode eq 'excel') {
1269: # Create the excel spreadsheet
1270: $excel_filename = '/prtspool/'.
1271: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
1272: time.'_'.rand(1000000000).'.xls';
1273: $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
1274: $excel_filename);
1275: $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
1276: #
1277: $format = &Apache::loncommon::define_excel_formats($excel_workbook);
1278: $excel_sheet = $excel_workbook->addworksheet('classlist');
1279: #
1280: my $description = 'Class List for '.
1281: $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1282: $excel_sheet->write($row++,0,$description,$format->{'h1'});
1283: #
1284: $excel_sheet->write($row++,0,["username","domain","ID",
1285: "student name","section",
1286: "start date","end date","status"],
1287: $format->{'bold'});
1288: }
1289: #
1290: # Sort the students
1291: my %index;
1292: my $i;
1293: foreach (@$keylist) {
1294: $index{$_} = $i++;
1295: }
1296: my $index = $index{$sortby};
1297: my $second = $index{'username'};
1298: my $third = $index{'domain'};
1299: my @Sorted_Students = sort {
1300: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
1301: ||
1302: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
1303: ||
1304: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1305: } (keys(%$classlist));
1306: my $studentcount = 0;
1307: foreach my $student (@Sorted_Students) {
1308: my $sdata = $classlist->{$student};
1309: my $username = $sdata->[$index{'username'}];
1310: my $domain = $sdata->[$index{'domain'}];
1311: my $section = $sdata->[$index{'section'}];
1312: my $name = $sdata->[$index{'fullname'}];
1313: my $id = $sdata->[$index{'id'}];
1314: my $status = $sdata->[$index{'status'}];
1315: my $start = $sdata->[$index{'start'}];
1316: my $end = $sdata->[$index{'end'}];
1317: next if (($statusmode ne 'Any') && ($status ne $statusmode));
1318: if ($mode eq 'view' || $mode eq 'html') {
1319: $start = &Apache::lonlocal::locallocaltime($start);
1320: $end = &Apache::lonlocal::locallocaltime($end);
1321: $r->print("<tr>\n <td>".(++$studentcount)."</td><td>\n ");
1322: if ($linkto eq 'nothing') {
1323: $r->print($username);
1324: } elsif ($linkto eq 'aboutme') {
1325: $r->print(&Apache::loncommon::aboutmewrapper($username,
1326: $username,
1327: $domain));
1328: } elsif ($linkto eq 'modify') {
1329: $r->print('<a href="'.
1330: "javascript:document.studentform.sname.value='".
1331: $username.
1332: "';document.studentform.sdom.value='".$domain.
1333: "';document.studentform.state.value='selected".
1334: "';document.studentform.submit();".'">'.
1335: $username."</a>\n");
1336: }
1337: $r->print(<<"END");
1338: </td>
1339: <td>$domain</td>
1340: <td>$id</td>
1341: <td>$name</td>
1342: <td>$section</td>
1343: <td>$start</td>
1344: <td>$end</td>
1345: </tr>
1346: END
1347: } elsif ($mode eq 'csv') {
1348: next if (! defined($CSVfile));
1349: # no need to bother with $linkto
1350: $start = &Apache::lonlocal::locallocaltime($start);
1351: $end = &Apache::lonlocal::locallocaltime($end);
1352: my @line = ();
1353: foreach ($username,$domain,$id,$name,$section,$start,$end) {
1354: push @line,&Apache::loncommon::csv_translate($_);
1355: }
1356: if ($statusmode eq 'Any') {
1357: push @line,&Apache::loncommon::csv_translate($status);
1358: }
1359: print $CSVfile '"'.join('","',@line).'"'."\n";
1360: } elsif ($mode eq 'excel') {
1361: $excel_sheet->write($row,0,[$username,$domain,$id,
1362: $name,$section]);
1363: my $col = 5;
1364: foreach my $time ($start,$end) {
1365: $excel_sheet->write($row,$col++,
1366: &Apache::lonstathelpers::calc_serial($time),
1367: $format->{'date'});
1368: }
1369: $excel_sheet->write($row,$col++,$status);
1370: $row++;
1371: }
1372: }
1373: if ($mode eq 'view' || $mode eq 'html') {
1374: $r->print('</table><br>');
1375: } elsif ($mode eq 'excel') {
1376: $excel_workbook->close();
1377: $r->print('<p><a href="'.$excel_filename.'">'.
1378: &mt('Your Excel spreadsheet').'</a> '.&mt('is ready for download').'.</p>'."\n");
1379: } elsif ($mode eq 'csv') {
1380: close($CSVfile);
1381: $r->print('<a href="'.$CSVfilename.'">'.
1382: &mt('Your CSV file').'</a> is ready for download.'.
1383: "\n");
1384: $r->rflush();
1385: }
1386: }
1387:
1388:
1389: #
1390: # print out form for modification of a single students data
1391: #
1392: sub print_modify_student_form {
1393: my $r = shift();
1394: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1395: ['sdom','sname']);
1396: my $sname = $ENV{'form.sname'};
1397: my $sdom = $ENV{'form.sdom'};
1398: my $sortby = $ENV{'form.sortby'};
1399: # determine the students name information
1400: my %info=&Apache::lonnet::get('environment',
1401: ['firstname','middlename',
1402: 'lastname','generation','id'],
1403: $sdom, $sname);
1404: my ($tmp) = keys(%info);
1405: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1406: $r->print('<font color="#ff0000" size="+2">'.&mt('Error').'</font>'.
1407: '<p>'.
1408: &mt('Unable to retrieve environment data for').' '.$sname.
1409: &mt('in domain').' '.$sdom.'</p><p>'.
1410: &mt('Please contact your LON-CAPA administrator regarding this situation.').'</p></body></html>');
1411: return;
1412: }
1413: # determine the students starting and ending times and section
1414: my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
1415: if ($starttime =~ /^error/) {
1416: $r->print('<h2>'&mt('Error').'</h2>');
1417: $r->print('<p>'.$starttime.'</p>');
1418: return;
1419: }
1420: #
1421: # Deal with date forms
1422: my $current_date_description = '';
1423: my $textdate = '';
1424:
1425: if (! defined($starttime) || $starttime == 0) {
1426: $current_date_description = &mt('Current Starting Date: not set').
1427: '<br />';
1428: } else {
1429: $current_date_description =
1430: &mt('Current Starting Date: [_1]',
1431: &Apache::lonlocal::locallocaltime($starttime)).'<br />';
1432: }
1433: if (! defined($endtime) || $endtime == 0) {
1434: $current_date_description.= &mt('Current Ending Date: not set').
1435: '<br />';
1436: } else {
1437: $current_date_description.=
1438: &mt('Current Ending Date: [_1]',
1439: &Apache::lonlocal::locallocaltime($endtime)).'<br />';
1440:
1441: }
1442: my $date_table = &date_setting_table($starttime,$endtime);
1443: #
1444: if (! exists($ENV{'form.Status'}) ||
1445: $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
1446: $ENV{'form.Status'} = 'crap';
1447: }
1448: # Make sure student is enrolled in course
1449: my %lt=&Apache::lonlocal::texthash(
1450: 'mef' => "Modify Enrollment for",
1451: 'odcc' => "Only domain coordinators can change a users password.",
1452: 'sn' => "Student Name",
1453: 'fn' => "First",
1454: 'mn' => "Middle",
1455: 'ln' => "Last",
1456: 'gen' => "Generation",
1457: 'sid' => "Student ID",
1458: 'disn' => "Disable ID/Student Number Safeguard and Force Change of Conflicting IDs (only do if you know what you are doing)",
1459: 'sec' => "Section",
1460: 'sm' => "Submit Modifications",
1461: );
1462: $r->print(<<END);
1463: <p>
1464: <font size="+1">
1465: $lt{'odcc'}
1466: </font>
1467: </p>
1468: <input type="hidden" name="slogin" value="$sname" />
1469: <input type="hidden" name="sdomain" value="$sdom" />
1470: <input type="hidden" name="action" value="modifystudent" />
1471: <input type="hidden" name="state" value="done" />
1472: <input type="hidden" name="sortby" value="$sortby" />
1473: <input type="hidden" name="Status" value="$ENV{'form.Status'}" />
1474: <h2>$lt{'mef'} $info{'firstname'} $info{'middlename'}
1475: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
1476: <p>
1477: <b>$lt{'sn'}</b>
1478: <table>
1479: <tr><th>$lt{'fn'}</th><th>$lt{'mn'}</th><th>$lt{'ln'}</th><th>$lt{'gen'}</th></tr>
1480: <tr><td>
1481: <input type="text" name="firstname" value="$info{'firstname'}" /></td><td>
1482: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
1483: <input type="text" name="lastname" value="$info{'lastname'}" /></td><td>
1484: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
1485: </table>
1486: </p><p>
1487: <b>$lt{'sid'}</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
1488: </p><p>
1489: <input type="checkbox" name="forceid" >
1490: $lt{'disn'}
1491: </p><p>
1492: <b>$lt{'sec'}</b>: <input type="text" name="section" value="$section" size="14"/>
1493: </p>
1494: <p>$current_date_description</p>
1495: <p>$date_table</p>
1496: <input type="submit" value="$lt{'sm'}" />
1497: </body></html>
1498: END
1499: return;
1500: }
1501:
1502: #
1503: # modify a single students section
1504: #
1505: sub modify_single_student {
1506: my $r = shift;
1507: #
1508: # Remove non alphanumeric values from the section
1509: $ENV{'form.section'} =~ s/\W//g;
1510: #
1511: # Do the date defaults first
1512: my ($starttime,$endtime) = &get_dates_from_form();
1513: if ($ENV{'form.makedatesdefault'}) {
1514: $r->print(&make_dates_default($starttime,$endtime));
1515: }
1516: # Get the 'sortby' and 'Status' variables so the user goes back to their
1517: # previous screen
1518: my $sortby = $ENV{'form.sortby'};
1519: my $status = $ENV{'form.Status'};
1520: #
1521: # We always need this information
1522: my $slogin = $ENV{'form.slogin'};
1523: my $sdom = $ENV{'form.sdomain'};
1524: #
1525: # Get the old data
1526: my %old=&Apache::lonnet::get('environment',
1527: ['firstname','middlename',
1528: 'lastname','generation','id'],
1529: $sdom, $slogin);
1530: $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
1531: $ENV{'request.course.id'});
1532: my ($tmp) = keys(%old);
1533: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1534: $r->print(&mt('There was an error determining the environment values for')." $slogin \@ $sdom.");
1535: return;
1536: }
1537: undef $tmp;
1538: #
1539: # Get the new data
1540: my $firstname = $ENV{'form.firstname'};
1541: my $middlename = $ENV{'form.middlename'};
1542: my $lastname = $ENV{'form.lastname'};
1543: my $generation = $ENV{'form.generation'};
1544: my $section = $ENV{'form.section'};
1545: my $courseid = $ENV{'request.course.id'};
1546: my $sid = $ENV{'form.id'};
1547: my $displayable_starttime = localtime($starttime);
1548: my $displayable_endtime = localtime($endtime);
1549: #
1550: # check for forceid override
1551: if ((defined($old{'id'})) && ($old{'id'} ne '') &&
1552: ($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
1553: $r->print("<font color=\"ff0000\">".&mt('You changed the students id but did not disable the ID change safeguard. The students id will not be changed.')."</font>");
1554: $sid = $old{'id'};
1555: }
1556: #
1557: # talk to the user about what we are going to do
1558: my %lt=&Apache::lonlocal::texthash(
1559: 'mdu' => "Modifying data for user",
1560: 'si' => "Student Information",
1561: 'fd' => "Field",
1562: 'ov' => "Old Value",
1563: 'nv' => "New Value",
1564: 'fn' => "First name",
1565: 'mn' => "Middle name",
1566: 'ln' => "Last name",
1567: 'gen' => "Generation",
1568: 'sec' => "Section",
1569: 'ri' => "Role Information",
1570: 'st' => "Start Time",
1571: 'et' => "End Time",
1572: );
1573: $r->print(<<END);
1574: <h2>$lt{'mdu'} $slogin \@ $sdom </h2>
1575: <h3>$lt{'si'}</h3>
1576: <table rules="rows" border="1" cellpadding="3" >
1577: <tr>
1578: <th> $lt{'fd'} </th>
1579: <th> $lt{'ov'} </th>
1580: <th> $lt{'nv'} </th>
1581: </tr>
1582: <tr>
1583: <td> <b>$lt{'fn'}</b> </td>
1584: <td> $old{'firstname'} </td>
1585: <td> $firstname </td>
1586: </tr><tr>
1587: <td> <b>$lt{'mn'}</b> </td>
1588: <td> $old{'middlename'} </td>
1589: <td> $middlename </td>
1590: </tr><tr>
1591: <td> <b>$lt{'ln'}</b> </td>
1592: <td> $old{'lastname'} </td>
1593: <td> $lastname </td>
1594: </tr><tr>
1595: <td> <b>$lt{'gen'}</b> </td>
1596: <td> $old{'generation'} </td>
1597: <td> $generation </td>
1598: </tr><tr>
1599: <td> <b>ID</b> </td>
1600: <td> $old{'id'} </td>
1601: <td> $sid </td>
1602: </tr><tr>
1603: <td> <b>$lt{'sec'}</b> </td>
1604: <td> $old{'section'} </td>
1605: <td> $section</td>
1606: </tr>
1607: </table>
1608: <h3>$lt{'ri'}</h3>
1609: <table>
1610: <tr><td align="right"><b>$lt{'st'}:</b></td><td> $displayable_starttime </td></tr>
1611: <tr><td align="right"><b>$lt{'et'}:</b></td><td> $displayable_endtime </td></tr>
1612: </table>
1613: <p>
1614: END
1615: #
1616: # Send request(s) to modify data (final undef is for 'desiredhost',
1617: # which is a moot point because the student already has an account.
1618: my $modify_section_results = &modifystudent($sdom,$slogin,
1619: $ENV{'request.course.id'},
1620: $section,undef);
1621: if ($modify_section_results !~ /^ok/) {
1622: $r->print(&mt('An error occured during the attempt to change the section for this student.')."<br />");
1623: }
1624: my $roleresults = &Apache::lonnet::modifystudent
1625: ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
1626: $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
1627: if ($roleresults eq 'refused' ) {
1628: $r->print(&mt('Your request to change the role information for this student was refused. You do not appear to have sufficient authority to change student information.'));
1629: } elsif ($roleresults !~ /ok/) {
1630: $r->print(&mt('An error occurred during the attempt to change the role information for this student.')." <br />".
1631: &mt('The error reported was')." ".
1632: $roleresults);
1633: &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
1634: " data for ".$slogin." \@ ".$sdom." by ".
1635: $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
1636: ":".$roleresults);
1637: } else { # everything is okay!
1638: $r->print(&mt('Student information updated successfully.')." <br />".
1639: &mt('The student must log out and log in again to see these changes.'));
1640: }
1641: my $Masd=&mt('Modify another students data');
1642: $r->print(<<END);
1643: </p><p>
1644: <input type="hidden" name="action" value="modifystudent" />
1645: <input type="hidden" name="sortby" value="$sortby" />
1646: <input type="hidden" name="Status" value="$status" />
1647: <a href="javascript:document.studentform.submit();">$Masd</a>
1648: </body></html>
1649: END
1650: return;
1651: }
1652:
1653: sub get_enrollment_data {
1654: my ($sname,$sdomain) = @_;
1655: my $courseid = $ENV{'request.course.id'};
1656: $courseid =~ s:_:/:g;
1657: my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
1658: my ($tmp) = keys(%roles);
1659: # Bail out if we were unable to get the students roles
1660: return ('error'.$tmp) if ($tmp =~ /^(con_lost|error|no_such_host)/i);
1661: # Go through the roles looking for enrollment in this course
1662: my ($end,$start) = (undef,undef);
1663: my $section = '';
1664: my $count = scalar(keys(%roles));
1665: while (my ($course,$role) = each(%roles)) {
1666: if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
1667: #
1668: # Get active role
1669: $section=$1;
1670: (undef,$end,$start)=split(/\_/,$role);
1671: my $now=time;
1672: my $notactive=0;
1673: if ($start) {
1674: if ($now<$start) { $notactive=1; }
1675: }
1676: if ($end) {
1677: if ($now>$end) { $notactive=1; }
1678: }
1679: unless ($notactive) { return ($start,$end,$section); }
1680: }
1681: }
1682: return ($start,$end,$section);
1683: }
1684:
1685: #################################################
1686: #################################################
1687:
1688: =pod
1689:
1690: =item show_drop_list
1691:
1692: Display a list of students to drop
1693: Inputs:
1694:
1695: =over 4
1696:
1697: =item $r, Apache request
1698:
1699: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
1700:
1701: =item $keylist, array pointer returned from loncoursedata::get_classlist()
1702: which describes the order elements are stored in the %$classlist values.
1703:
1704: =item $nosort, if true, sorting links are omitted.
1705:
1706: =back
1707:
1708: =cut
1709:
1710: #################################################
1711: #################################################
1712: sub show_drop_list {
1713: my ($r,$classlist,$keylist,$nosort)=@_;
1714: my $cid=$ENV{'request.course.id'};
1715: if (! exists($ENV{'form.sortby'})) {
1716: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1717: ['sortby']);
1718: }
1719: my $sortby = $ENV{'form.sortby'};
1720: if ($sortby !~ /^(username|domain|section|fullname|id|start|end)$/) {
1721: $sortby = 'username';
1722: }
1723: #
1724: my $action = "drop";
1725: $r->print(<<END);
1726: <input type="hidden" name="sortby" value="$sortby" />
1727: <input type="hidden" name="action" value="$action" />
1728: <input type="hidden" name="state" value="done" />
1729: <script>
1730: function checkAll(field) {
1731: for (i = 0; i < field.length; i++)
1732: field[i].checked = true ;
1733: }
1734:
1735: function uncheckAll(field) {
1736: for (i = 0; i < field.length; i++)
1737: field[i].checked = false ;
1738: }
1739: </script>
1740: <p>
1741: <input type="hidden" name="phase" value="four">
1742: END
1743:
1744: my %lt=&Apache::lonlocal::texthash('usrn' => "username",
1745: 'dom' => "domain",
1746: 'sn' => "student name",
1747: 'sec' => "section",
1748: 'start' => "start date",
1749: 'end' => "end date",
1750: );
1751: if ($nosort) {
1752: $r->print(<<END);
1753: <table border=2>
1754: <tr>
1755: <th> </th>
1756: <th>$lt{'usrn'}</th>
1757: <th>$lt{'dom'}</th>
1758: <th>ID</th>
1759: <th>$lt{'sn'}</th>
1760: <th>$lt{'sec'}</th>
1761: <th>$lt{'start'}</th>
1762: <th>$lt{'end'}</th>
1763: </tr>
1764: END
1765:
1766: } else {
1767: $r->print(<<END);
1768: <table border=2>
1769: <tr><th> </th>
1770: <th>
1771: <a href="/adm/dropadd?action=$action&sortby=username">$lt{'usrn'}</a>
1772: </th><th>
1773: <a href="/adm/dropadd?action=$action&sortby=domain">$lt{'dom'}</a>
1774: </th><th>
1775: <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
1776: </th><th>
1777: <a href="/adm/dropadd?action=$action&sortby=fullname">$lt{'sn'}</a>
1778: </th><th>
1779: <a href="/adm/dropadd?action=$action&sortby=section">$lt{'sec'}</a>
1780: </th><th>
1781: <a href="/adm/dropadd?action=$action&sortby=start">$lt{'start'}</a>
1782: </th><th>
1783: <a href="/adm/dropadd?action=$action&sortby=end">$lt{'end'}</a>
1784: </th>
1785: </tr>
1786: END
1787: }
1788: #
1789: # Sort the students
1790: my %index;
1791: my $i;
1792: foreach (@$keylist) {
1793: $index{$_} = $i++;
1794: }
1795: my $index = $index{$sortby};
1796: my $second = $index{'username'};
1797: my $third = $index{'domain'};
1798: my @Sorted_Students = sort {
1799: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
1800: ||
1801: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
1802: ||
1803: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1804: } (keys(%$classlist));
1805: foreach my $student (@Sorted_Students) {
1806: my $error;
1807: my $sdata = $classlist->{$student};
1808: my $username = $sdata->[$index{'username'}];
1809: my $domain = $sdata->[$index{'domain'}];
1810: my $section = $sdata->[$index{'section'}];
1811: my $name = $sdata->[$index{'fullname'}];
1812: my $id = $sdata->[$index{'id'}];
1813: my $start = $sdata->[$index{'start'}];
1814: my $end = $sdata->[$index{'end'}];
1815: if (! defined($start) || $start == 0) {
1816: $start = &mt('none');
1817: } else {
1818: $start = &Apache::lonlocal::locallocaltime($start);
1819: }
1820: if (! defined($end) || $end == 0) {
1821: $end = &mt('none');
1822: } else {
1823: $end = &Apache::lonlocal::locallocaltime($end);
1824: }
1825: my $status = $sdata->[$index{'status'}];
1826: next if ($status ne 'Active');
1827: #
1828: $r->print(<<"END");
1829: <tr>
1830: <td><input type="checkbox" name="droplist" value="$student"></td>
1831: <td>$username</td>
1832: <td>$domain</td>
1833: <td>$id</td>
1834: <td>$name</td>
1835: <td>$section</td>
1836: <td>$start</td>
1837: <td>$end</td>
1838: </tr>
1839: END
1840: }
1841: $r->print('</table><br>');
1842: %lt=&Apache::lonlocal::texthash(
1843: 'dp' => "Drop Students",
1844: 'ca' => "check all",
1845: 'ua' => "uncheck all",
1846: );
1847: $r->print(<<"END");
1848: </p><p>
1849: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)">
1850: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)">
1851: <p><input type=submit value="$lt{'dp'}"></p>
1852: END
1853: return;
1854: }
1855:
1856: #
1857: # Print out the initial form to get the courselist file
1858: #
1859: sub print_first_courselist_upload_form {
1860: my $r=shift;
1861: my $str;
1862: $str = '<input type="hidden" name="phase" value="two">';
1863: $str .= '<input type="hidden" name="action" value="upload" />';
1864: $str .= '<input type="hidden" name="state" value="got_file" />';
1865: $str .= "<h3>".&mt('Upload a class list')."</h3>\n";
1866: $str .= &Apache::loncommon::upfile_select_html();
1867: $str .= "<p>\n";
1868: $str .= '<input type="submit" name="fileupload" value="'.
1869: &mt('Upload class list').'">'."\n";
1870: $str .= '<input type="checkbox" name="noFirstLine" /> '.
1871: &mt('Ignore First Line')."</p>\n";
1872: $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
1873: &mt("How do I create a class list from a spreadsheet")).
1874: "<br />\n";
1875: $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
1876: &mt("How do I create a CSV file from a spreadsheet")).
1877: "<br />\n";
1878: $str .= "</body>\n</html>\n";
1879: $r->print($str);
1880: return;
1881: }
1882:
1883: # ================================================= Drop/Add from uploaded file
1884: sub upfile_drop_add {
1885: my $r=shift;
1886: &Apache::loncommon::load_tmp_file($r);
1887: my @studentdata=&Apache::loncommon::upfile_record_sep();
1888: if($ENV{'form.noFirstLine'}){shift(@studentdata);}
1889: my @keyfields = split(/\,/,$ENV{'form.keyfields'});
1890: my $cid = $ENV{'request.course.id'};
1891: my %fields=();
1892: for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
1893: if ($ENV{'form.upfile_associate'} eq 'reverse') {
1894: if ($ENV{'form.f'.$i} ne 'none') {
1895: $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
1896: }
1897: } else {
1898: $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
1899: }
1900: }
1901: #
1902: # Store the field choices away
1903: foreach my $field (qw/username names
1904: fname mname lname gen id sec ipwd email/) {
1905: $ENV{'form.'.$field.'_choice'}=$fields{$field};
1906: }
1907: &Apache::loncommon::store_course_settings('enrollment_upload',
1908: { 'username_choice' => 'scalar',
1909: 'names_choice' => 'scalar',
1910: 'fname_choice' => 'scalar',
1911: 'mname_choice' => 'scalar',
1912: 'lname_choice' => 'scalar',
1913: 'gen_choice' => 'scalar',
1914: 'id_choice' => 'scalar',
1915: 'sec_choice' => 'scalar',
1916: 'ipwd_choice' => 'scalar',
1917: 'email_choice' => 'scalar' });
1918:
1919: #
1920: my ($startdate,$enddate) = &get_dates_from_form();
1921: if ($ENV{'form.makedatesdefault'}) {
1922: $r->print(&make_dates_default($startdate,$enddate));
1923: }
1924: # Determine domain and desired host (home server)
1925: my $domain=$ENV{'form.lcdomain'};
1926: my $desiredhost = $ENV{'form.lcserver'};
1927: if (lc($desiredhost) eq 'default') {
1928: $desiredhost = undef;
1929: } else {
1930: my %home_servers = &Apache::loncommon::get_library_servers($domain);
1931: if (! exists($home_servers{$desiredhost})) {
1932: $r->print('<font color="#ff0000">'.&mt('Error').'</font>'.
1933: &mt('Invalid home server specified'));
1934: $r->print("</body>\n</html>\n");
1935: return;
1936: }
1937: }
1938: # Determine authentication mechanism
1939: my $amode = '';
1940: my $genpwd = '';
1941: if ($ENV{'form.login'} eq 'krb') {
1942: $amode='krb';
1943: $amode.=$ENV{'form.krbver'};
1944: $genpwd=$ENV{'form.krbarg'};
1945: } elsif ($ENV{'form.login'} eq 'int') {
1946: $amode='internal';
1947: if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
1948: $genpwd=$ENV{'form.intarg'};
1949: }
1950: } elsif ($ENV{'form.login'} eq 'loc') {
1951: $amode='localauth';
1952: if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
1953: $genpwd=$ENV{'form.locarg'};
1954: }
1955: }
1956: if ($amode =~ /^krb/) {
1957: if (! defined($genpwd) || $genpwd eq '') {
1958: $r->print('<font color="red" size="+1">'.
1959: &mt('Unable to enroll students').'</font> '.
1960: &mt('No Kerberos domain was specified.').'</p>');
1961: $amode = ''; # This causes the loop below to be skipped
1962: }
1963: }
1964: unless (($domain=~/\W/) || ($amode eq '')) {
1965: #######################################
1966: ## Enroll Students ##
1967: #######################################
1968: $r->print('<h3>'.&mt('Enrolling Students')."</h3>\n<p>\n");
1969: my $count=0;
1970: my $flushc=0;
1971: my %student=();
1972: # Get new classlist
1973: foreach (@studentdata) {
1974: my %entries=&Apache::loncommon::record_sep($_);
1975: # Determine student name
1976: unless (($entries{$fields{'username'}} eq '') ||
1977: (!defined($entries{$fields{'username'}}))) {
1978: my ($fname, $mname, $lname,$gen) = ('','','','');
1979: if (defined($fields{'names'})) {
1980: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
1981: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
1982: } else {
1983: if (defined($fields{'fname'})) {
1984: $fname=$entries{$fields{'fname'}};
1985: }
1986: if (defined($fields{'mname'})) {
1987: $mname=$entries{$fields{'mname'}};
1988: }
1989: if (defined($fields{'lname'})) {
1990: $lname=$entries{$fields{'lname'}};
1991: }
1992: if (defined($fields{'gen'})) {
1993: $gen=$entries{$fields{'gen'}};
1994: }
1995: }
1996: if ($entries{$fields{'username'}}=~/\W/) {
1997: $r->print('<br />'.
1998: &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
1999: $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
2000: '</b>');
2001: } else {
2002: # determine section number
2003: my $sec='';
2004: my $username=$entries{$fields{'username'}};
2005: if (defined($fields{'sec'})) {
2006: if (defined($entries{$fields{'sec'}})) {
2007: $sec=$entries{$fields{'sec'}};
2008: }
2009: }
2010: # remove non alphanumeric values from section
2011: $sec =~ s/\W//g;
2012: # determine student id number
2013: my $id='';
2014: if (defined($fields{'id'})) {
2015: if (defined($entries{$fields{'id'}})) {
2016: $id=$entries{$fields{'id'}};
2017: }
2018: $id=~tr/A-Z/a-z/;
2019: }
2020: # determine email address
2021: my $email='';
2022: if (defined($fields{'email'})) {
2023: if (defined($entries{$fields{'email'}})) {
2024: $email=$entries{$fields{'email'}};
2025: unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
2026: }
2027: }
2028: # determine student password
2029: my $password='';
2030: if ($genpwd) {
2031: $password=$genpwd;
2032: } else {
2033: if (defined($fields{'ipwd'})) {
2034: if ($entries{$fields{'ipwd'}}) {
2035: $password=$entries{$fields{'ipwd'}};
2036: }
2037: }
2038: }
2039: # Clean up whitespace
2040: foreach (\$domain,\$username,\$id,\$fname,\$mname,
2041: \$lname,\$gen,\$sec) {
2042: $$_ =~ s/(\s+$|^\s+)//g;
2043: }
2044: if ($password || $ENV{'form.login'} eq 'loc') {
2045: &modifystudent($domain,$username,$cid,$sec,
2046: $desiredhost);
2047: my $reply=&Apache::lonnet::modifystudent
2048: ($domain,$username,$id,$amode,$password,
2049: $fname,$mname,$lname,$gen,$sec,$enddate,
2050: $startdate,$ENV{'form.forceid'},$desiredhost,
2051: $email);
2052: if ($reply ne 'ok') {
2053: $reply =~ s/^error://;
2054: $r->print('<br />'.
2055: &mt('<b>[_1]</b>: Unable to enroll: [_2]',$username,$reply));
2056: } else {
2057: $count++; $flushc++;
2058: $student{$username}=1;
2059: $r->print('. ');
2060: if ($flushc>15) {
2061: $r->rflush;
2062: $flushc=0;
2063: }
2064: }
2065: } else {
2066: $r->print('<br />'.
2067: &mt('<b>[_1]</b>: Unable to enroll. No password specified.',$username)
2068: );
2069: }
2070: }
2071: }
2072: } # end of foreach (@studentdata)
2073: $r->print("</p>\n<p>\n".&mt('Processed [_1] student(s).',$count).
2074: "</p>\n");
2075: $r->print("<p>\n".
2076: &mt('If active, the new role will be available when the '.
2077: 'students next log in to LON-CAPA.')."</p>\n");
2078: #####################################
2079: # Drop students #
2080: #####################################
2081: if ($ENV{'form.fullup'} eq 'yes') {
2082: $r->print('<h3>'.&mt('Dropping Students')."</h3>\n");
2083: # Get current classlist
2084: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
2085: if (! defined($classlist)) {
2086: $r->print(&mt('There are no students currently enrolled.').
2087: "\n");
2088: } else {
2089: # Remove the students we just added from the list of students.
2090: foreach (@studentdata) {
2091: my %entries=&Apache::loncommon::record_sep($_);
2092: unless (($entries{$fields{'username'}} eq '') ||
2093: (!defined($entries{$fields{'username'}}))) {
2094: delete($classlist->{$entries{$fields{'username'}}.
2095: ':'.$domain});
2096: }
2097: }
2098: # Print out list of dropped students.
2099: &show_drop_list($r,$classlist,$keylist,'nosort');
2100: }
2101: }
2102: } # end of unless
2103: }
2104:
2105: # ================================================================== Phase four
2106: sub drop_student_list {
2107: my $r=shift;
2108: my $count=0;
2109: my @droplist;
2110: if (ref($ENV{'form.droplist'})) {
2111: @droplist = @{$ENV{'form.droplist'}};
2112: } else {
2113: @droplist = ($ENV{'form.droplist'});
2114: }
2115: foreach (@droplist) {
2116: my ($uname,$udom)=split(/\:/,$_);
2117: # drop student
2118: my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
2119: if ($result eq 'ok' || $result eq 'ok:') {
2120: $r->print(&mt('Dropped [_1]',$uname.'@'.$udom).'<br>');
2121: $count++;
2122: } else {
2123: $r->print(
2124: &mt('Error dropping [_1]:[_2]',$uname.'@'.$udom,$result).
2125: '<br />');
2126: }
2127: }
2128: $r->print('<p><b>'.&mt('Dropped [_1] student(s).',$count).'</b></p>');
2129: $r->print('<p>'.&mt('Re-enrollment will re-activate data.')) if ($count);
2130: }
2131:
2132: ###################################################################
2133: ###################################################################
2134:
2135: =pod
2136:
2137: =item &handler
2138:
2139: The typical handler you see in all these modules. Takes $r, the
2140: http request, as an argument.
2141:
2142: The response to the request is governed by two form variables
2143:
2144: form.action form.state response
2145: ---------------------------------------------------
2146: undefined undefined print main menu
2147: upload undefined print courselist upload menu
2148: upload got_file deal with uploaded file,
2149: print the upload managing menu
2150: upload enrolling enroll students based on upload
2151: drop undefined print the classlist ready to drop
2152: drop done drop the selected students
2153: enrollstudent undefined print student username domain form
2154: enrollstudent gotusername print single student enroll menu
2155: enrollstudent enrolling enroll student
2156: classlist undefined print html classlist
2157: classlist csv print csv classlist
2158: modifystudent undefined print classlist to select student to modify
2159: modifystudent selected print modify student menu
2160: modifystudent done make modifications to student record
2161:
2162: =cut
2163:
2164: ###################################################################
2165: ###################################################################
2166: sub handler {
2167: my $r=shift;
2168: if ($r->header_only) {
2169: &Apache::loncommon::content_type($r,'text/html');
2170: $r->send_http_header;
2171: return OK;
2172: }
2173: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2174: ['action','state']);
2175:
2176: &Apache::lonhtmlcommon::clear_breadcrumbs();
2177: &Apache::lonhtmlcommon::add_breadcrumb
2178: ({href=>"/adm/dropadd",
2179: text=>"Enrollment Manager",
2180: faq=>9,bug=>'Instructor Interface',});
2181: # Needs to be in a course
2182: if (! (($ENV{'request.course.fn'}) &&
2183: (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
2184: # Not in a course, or not allowed to modify parms
2185: $ENV{'user.error.msg'}=
2186: "/adm/dropadd:cst:0:0:Cannot drop or add students";
2187: return HTTP_NOT_ACCEPTABLE;
2188: }
2189: #
2190: # Only output the header information if they did not request csv format
2191: #
2192: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2193: ['state','action']);
2194: # Start page
2195: &Apache::loncommon::content_type($r,'text/html');
2196: $r->send_http_header;
2197: $r->print(&header());
2198: #
2199: # Main switch on form.action and form.state, as appropriate
2200: if (! exists($ENV{'form.action'})) {
2201: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2202: (undef,'Enrollment Manager'));
2203: &print_main_menu($r);
2204: } elsif ($ENV{'form.action'} eq 'upload') {
2205: &Apache::lonhtmlcommon::add_breadcrumb
2206: ({href=>'/adm/dropadd?action=upload&state=',
2207: text=>"Upload Classlist"});
2208: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2209: (undef,'Upload Classlist'));
2210: if (! exists($ENV{'form.state'})) {
2211: &print_first_courselist_upload_form($r);
2212: } elsif ($ENV{'form.state'} eq 'got_file') {
2213: &print_upload_manager_form($r);
2214: } elsif ($ENV{'form.state'} eq 'enrolling') {
2215: if ($ENV{'form.datatoken'}) {
2216: &upfile_drop_add($r);
2217: } else {
2218: # Hmmm, this is an error
2219: }
2220: } else {
2221: &print_first_courselist_upload_form($r);
2222: }
2223: } elsif ($ENV{'form.action'} eq 'drop') {
2224: &Apache::lonhtmlcommon::add_breadcrumb
2225: ({href=>'/adm/dropadd?action=drop',
2226: text=>"Drop Students"});
2227: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2228: (undef,'Drop Students'));
2229: if (! exists($ENV{'form.state'})) {
2230: &print_drop_menu($r);
2231: } elsif ($ENV{'form.state'} eq 'done') {
2232: &drop_student_list($r);
2233: } else {
2234: &print_drop_menu($r);
2235: }
2236: } elsif ($ENV{'form.action'} eq 'enrollstudent') {
2237: &Apache::lonhtmlcommon::add_breadcrumb
2238: ({href=>'/adm/dropadd?action=enrollstudent',
2239: text=>"Enroll Student"});
2240: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2241: (undef,'Enroll Student'));
2242: if (! exists($ENV{'form.state'})) {
2243: &get_student_username_domain_form($r);
2244: } elsif ($ENV{'form.state'} eq 'gotusername') {
2245: &print_enroll_single_student_form($r);
2246: } elsif ($ENV{'form.state'} eq 'enrolling') {
2247: &enroll_single_student($r);
2248: } else {
2249: &get_student_username_domain_form($r);
2250: }
2251: } elsif ($ENV{'form.action'} eq 'classlist') {
2252: &Apache::lonhtmlcommon::add_breadcrumb
2253: ({href=>'/adm/dropadd?action=classlist',
2254: text=>"View Classlist"});
2255: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2256: (undef,'View Classlist'));
2257: if (! exists($ENV{'form.state'})) {
2258: &print_html_classlist($r,undef);
2259: } elsif ($ENV{'form.state'} eq 'csv') {
2260: &print_html_classlist($r,'csv');
2261: } elsif ($ENV{'form.state'} eq 'excel') {
2262: &print_html_classlist($r,'excel');
2263: } else {
2264: &print_html_classlist($r,undef);
2265: }
2266: } elsif ($ENV{'form.action'} eq 'modifystudent') {
2267: &Apache::lonhtmlcommon::add_breadcrumb
2268: ({href=>'/adm/dropadd?action=modifystudent',
2269: text=>"Modify Student Data"});
2270: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2271: (undef,'Modify Student Data'));
2272: if (! exists($ENV{'form.state'})) {
2273: &print_html_classlist($r);
2274: } elsif ($ENV{'form.state'} eq 'selected') {
2275: &print_modify_student_form($r);
2276: } elsif ($ENV{'form.state'} eq 'done') {
2277: &modify_single_student($r);
2278: } else {
2279: &print_html_classlist($r);
2280: }
2281: } else {
2282: # We should not end up here, but I guess it is possible
2283: &Apache::lonnet::logthis("Undetermined state in londropadd.pm. ".
2284: "form.action = ".$ENV{'form.action'}.
2285: "Someone should fix this.");
2286: $r->print(&Apache::lonhtmlcommon::breadcrumbs
2287: (undef,'Enrollment Manager'));
2288: &print_main_menu($r);
2289: }
2290: #
2291: # Finish up
2292: $r->print('</form></body></html>');
2293: return OK;
2294: }
2295:
2296: ###################################################################
2297: ###################################################################
2298:
2299: 1;
2300: __END__
2301:
2302:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>