Annotation of loncom/interface/lonuserutils.pm, revision 1.14
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Utility functions for managing LON-CAPA user accounts
3: #
1.14 ! raeburn 4: # $Id: lonuserutils.pm,v 1.13 2007/12/11 01:17:16 raeburn Exp $
1.1 raeburn 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: #
28: ###############################################################
29: ###############################################################
30:
31: package Apache::lonuserutils;
32:
33: use strict;
34: use Apache::lonnet;
35: use Apache::loncommon();
36: use Apache::lonhtmlcommon;
37: use Apache::lonlocal;
1.8 raeburn 38: use Apache::longroup;
39: use LONCAPA qw(:DEFAULT :match);
1.1 raeburn 40:
41: ###############################################################
42: ###############################################################
43: # Drop student from all sections of a course, except optional $csec
44: sub modifystudent {
45: my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
46: # if $csec is undefined, drop the student from all the courses matching
47: # this one. If $csec is defined, drop them from all other sections of
48: # this course and add them to section $csec
49: my $cdom = $env{'course.'.$courseid.'.domain'};
50: my $cnum = $env{'course.'.$courseid.'.num'};
51: my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
52: my ($tmp) = keys(%roles);
53: # Bail out if we were unable to get the students roles
54: return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
55: # Go through the roles looking for enrollment in this course
56: my $result = '';
57: foreach my $course (keys(%roles)) {
58: if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
59: # We are in this course
60: my $section=$1;
61: $section='' if ($course eq "/$cdom/$cnum".'_st');
62: if (defined($csec) && $section eq $csec) {
63: $result .= 'ok:';
64: } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
65: my (undef,$end,$start)=split(/\_/,$roles{$course});
66: my $now=time;
67: # if this is an active role
68: if (!($start && ($now<$start)) || !($end && ($now>$end))) {
69: my $reply=&Apache::lonnet::modifystudent
70: # dom name id mode pass f m l g
71: ($udom,$unam,'', '', '',undef,undef,undef,undef,
72: $section,time,undef,undef,$desiredhost);
73: $result .= $reply.':';
74: }
75: }
76: }
77: }
78: if ($result eq '') {
79: $result = 'Unable to find section for this student';
80: } else {
81: $result =~ s/(ok:)+/ok/g;
82: }
83: return $result;
84: }
85:
86: sub modifyuserrole {
87: my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
88: $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
1.5 raeburn 89: $end,$start,$checkid) = @_;
90: my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1 raeburn 91: if ($setting eq 'course' || $context eq 'course') {
92: $scope = '/'.$cid;
93: $scope =~ s/\_/\//g;
94: if ($role ne 'cc' && $sec ne '') {
95: $scope .='/'.$sec;
96: }
1.5 raeburn 97: } elsif ($context eq 'domain') {
1.1 raeburn 98: $scope = '/'.$env{'request.role.domain'}.'/';
1.13 raeburn 99: } elsif ($context eq 'author') {
1.1 raeburn 100: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
101: }
102: if ($context eq 'domain') {
103: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
104: if ($uhome ne 'no_host') {
1.5 raeburn 105: if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1 raeburn 106: if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
107: ($umode eq 'localauth')) {
108: $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
109: }
110: }
1.5 raeburn 111: if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
112: ($env{'form.recurseid'}) && ($checkid)) {
113: my %userupdate = (
114: lastname => $last,
115: middlename => $middle,
116: firstname => $first,
117: generation => $gene,
118: id => $uid,
119: );
120: $idresult = &propagate_id_change($uname,$udom,\%userupdate);
121: }
1.1 raeburn 122: }
123: }
124: $userresult =
125: &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
126: $middle,$last,$gene,$forceid,$desiredhome,
127: $email,$role,$start,$end);
128: if ($userresult eq 'ok') {
1.5 raeburn 129: if ($role ne '') {
1.1 raeburn 130: $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
131: $role,$end,$start);
132: }
133: }
1.5 raeburn 134: return ($userresult,$authresult,$roleresult,$idresult);
1.1 raeburn 135: }
136:
1.5 raeburn 137: sub propagate_id_change {
138: my ($uname,$udom,$user) = @_;
1.12 raeburn 139: my (@types,@roles);
1.5 raeburn 140: @types = ('active','future');
141: @roles = ('st');
142: my $idresult;
143: my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12 raeburn 144: $udom,'userroles',\@types,\@roles);
145: my %args = (
146: one_time => 1,
147: );
1.5 raeburn 148: foreach my $item (keys(%roleshash)) {
149: my ($cnum,$cdom,$role) = split(/:/,$item);
150: my ($start,$end) = split(/:/,$roleshash{$item});
151: if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12 raeburn 152: my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
153: my %coursehash =
154: &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
155: my $cdesc = $coursehash{'description'};
156: if ($cdesc eq '') {
157: $cdesc = $cdom.'_'.$cnum;
158: }
1.5 raeburn 159: if ($result eq 'ok') {
1.12 raeburn 160: $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5 raeburn 161: } else {
1.12 raeburn 162: $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5 raeburn 163: }
164: }
165: }
166: return $idresult;
167: }
168:
169: sub update_classlist {
170: my ($cdom,$cnum,$udom,$uname,$user) = @_;
1.6 albertel 171: my ($uid,$classlistentry);
1.5 raeburn 172: my $fullname =
173: &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
174: $user->{'lastname'},$user->{'generation'},
175: 'lastname');
176: my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
177: $cdom,$cnum);
178: my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
179: my $ididx=&Apache::loncoursedata::CL_ID() - 2;
180: my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
181: for (my $i=0; $i<@classinfo; $i++) {
182: if ($i == $ididx) {
183: if (defined($user->{'id'})) {
184: $classlistentry .= $user->{'id'}.':';
185: } else {
186: $classlistentry .= $classinfo[$i].':';
187: }
188: } elsif ($i == $nameidx) {
189: $classlistentry .= $fullname.':';
190: } else {
191: $classlistentry .= $classinfo[$i].':';
192: }
193: }
194: $classlistentry =~ s/:$//;
195: my $reply=&Apache::lonnet::cput('classlist',
196: {"$uname:$udom" => $classlistentry},
197: $cdom,$cnum);
198: if (($reply eq 'ok') || ($reply eq 'delayed')) {
199: return 'ok';
200: } else {
201: return 'error: '.$reply;
202: }
203: }
204:
205:
1.1 raeburn 206: ###############################################################
207: ###############################################################
1.2 raeburn 208: # build a role type and role selection form
209: sub domain_roles_select {
210: # Set up the role type and role selection boxes when in
211: # domain context
212: #
213: # Role types
1.13 raeburn 214: my @roletypes = ('domain','author','course');
1.2 raeburn 215: my %lt = &role_type_names();
1.1 raeburn 216: #
217: # build up the menu information to be passed to
218: # &Apache::loncommon::linked_select_forms
219: my %select_menus;
1.2 raeburn 220: if ($env{'form.roletype'} eq '') {
221: $env{'form.roletype'} = 'domain';
222: }
223: foreach my $roletype (@roletypes) {
1.1 raeburn 224: # set up the text for this domain
1.2 raeburn 225: $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.1 raeburn 226: # we want a choice of 'default' as the default in the second menu
1.2 raeburn 227: if ($env{'form.roletype'} ne '') {
228: $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
229: } else {
230: $select_menus{$roletype}->{'default'} = 'Any';
231: }
1.1 raeburn 232: # Now build up the other items in the second menu
1.2 raeburn 233: my @roles;
234: if ($roletype eq 'domain') {
235: @roles = &domain_roles();
1.13 raeburn 236: } elsif ($roletype eq 'author') {
1.2 raeburn 237: @roles = &construction_space_roles();
238: } else {
239: @roles = &course_roles('domain');
1.5 raeburn 240: unshift(@roles,'cr');
1.1 raeburn 241: }
1.2 raeburn 242: my $order = ['Any',@roles];
243: $select_menus{$roletype}->{'order'} = $order;
244: foreach my $role (@roles) {
1.5 raeburn 245: if ($role eq 'cr') {
246: $select_menus{$roletype}->{'select2'}->{$role} =
247: &mt('Custom role');
248: } else {
249: $select_menus{$roletype}->{'select2'}->{$role} =
250: &Apache::lonnet::plaintext($role);
251: }
1.2 raeburn 252: }
253: $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1 raeburn 254: }
1.2 raeburn 255: my $result = &Apache::loncommon::linked_select_forms
256: ('studentform',(' 'x3).&mt('Role: '),$env{'form.roletype'},
1.13 raeburn 257: 'roletype','showrole',\%select_menus,['domain','author','course']);
1.1 raeburn 258: return $result;
259: }
260:
261: ###############################################################
262: ###############################################################
263: sub hidden_input {
264: my ($name,$value) = @_;
265: return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
266: }
267:
268: sub print_upload_manager_header {
269: my ($r,$datatoken,$distotal,$krbdefdom,$context)=@_;
270: my $javascript;
271: #
272: if (! exists($env{'form.upfile_associate'})) {
273: $env{'form.upfile_associate'} = 'forward';
274: }
275: if ($env{'form.associate'} eq 'Reverse Association') {
276: if ( $env{'form.upfile_associate'} ne 'reverse' ) {
277: $env{'form.upfile_associate'} = 'reverse';
278: } else {
279: $env{'form.upfile_associate'} = 'forward';
280: }
281: }
282: if ($env{'form.upfile_associate'} eq 'reverse') {
283: $javascript=&upload_manager_javascript_reverse_associate();
284: } else {
285: $javascript=&upload_manager_javascript_forward_associate();
286: }
287: #
288: # Deal with restored settings
289: my $password_choice = '';
290: if (exists($env{'form.ipwd_choice'}) &&
291: $env{'form.ipwd_choice'} ne '') {
292: # If a column was specified for password, assume it is for an
293: # internal password. This is a bug waiting to be filed (could be
294: # local or krb auth instead of internal) but I do not have the
295: # time to mess around with this now.
296: $password_choice = 'int';
297: }
298: #
299: my $javascript_validations =
300: &javascript_validations('auth',$krbdefdom,$password_choice,undef,
301: $env{'request.role.domain'});
302: my $checked=(($env{'form.noFirstLine'})?' checked="checked" ':'');
303: $r->print(&mt('Total number of records found in file: <b>[_1]</b>.',$distotal).
304: "<br />\n");
305: $r->print('<div class="LC_left_float"><h3>'.
306: &mt('Identify fields in uploaded list')."</h3>\n");
307: $r->print(&mt('Enter as many fields as you can.<br /> The system will inform you and bring you back to this page, <br /> if the data selected are insufficient to add users.')."<br />\n");
308: $r->print(&hidden_input('action','upload').
309: &hidden_input('state','got_file').
310: &hidden_input('associate','').
311: &hidden_input('datatoken',$datatoken).
312: &hidden_input('fileupload',$env{'form.fileupload'}).
313: &hidden_input('upfiletype',$env{'form.upfiletype'}).
314: &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
315: $r->print('<br /><input type="button" value="Reverse Association" '.
316: 'name="'.&mt('Reverse Association').'" '.
317: 'onClick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
318: $r->print('<label><input type="checkbox" name="noFirstLine"'.$checked.'/>'.
319: &mt('Ignore First Line').'</label>');
320: $r->print("<br /><br />\n".
321: '<script type="text/javascript" language="Javascript">'."\n".
322: $javascript."\n".$javascript_validations.'</script>');
323: }
324:
325: ###############################################################
326: ###############################################################
327: sub javascript_validations {
328: my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain)=@_;
329: my $authheader;
330: if ($mode eq 'auth') {
331: my %param = ( formname => 'studentform',
332: kerb_def_dom => $krbdefdom,
333: curr_authtype => $curr_authtype);
334: $authheader = &Apache::loncommon::authform_header(%param);
335: } elsif ($mode eq 'createcourse') {
336: my %param = ( formname => 'ccrs',
337: kerb_def_dom => $krbdefdom,
338: curr_authtype => $curr_authtype );
339: $authheader = &Apache::loncommon::authform_header(%param);
340: } elsif ($mode eq 'modifycourse') {
341: my %param = ( formname => 'cmod',
342: kerb_def_dom => $krbdefdom,
343: mode => 'modifycourse',
344: curr_authtype => $curr_authtype,
345: curr_autharg => $curr_authfield );
346: $authheader = &Apache::loncommon::authform_header(%param);
347: }
348:
349: my %alert = &Apache::lonlocal::texthash
350: (username => 'You need to specify the username field.',
351: authen => 'You must choose an authentication type.',
352: krb => 'You need to specify the Kerberos domain.',
353: ipass => 'You need to specify the initial password.',
354: name => 'The optional name field was not specified.',
355: snum => 'The optional ID number field was not specified.',
356: section => 'The optional section field was not specified.',
357: email => 'The optional email address field was not specified.',
358: role => 'The optional role field was not specified.',
359: continue => 'Continue adding users?',
360: );
361:
362: # my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
363: my $function_name =(<<END);
364: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
365: END
366: my ($authnum,%can_assign) = &Apache::loncommon::get_assignable_auth($domain);
367: my $auth_checks;
368: if ($mode eq 'createcourse') {
369: $auth_checks .= (<<END);
370: if (vf.autoadds[0].checked == true) {
371: if (current.radiovalue == null || current.radiovalue == 'nochange') {
372: alert('$alert{'authen'}');
373: return;
374: }
375: }
376: END
377: } else {
378: $auth_checks .= (<<END);
379: var foundatype=0;
380: if (founduname==0) {
381: alert('$alert{'username'}');
382: return;
383: }
384:
385: END
386: if ($authnum > 1) {
387: $auth_checks .= (<<END);
388: if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
389: // They did not check any of the login radiobuttons.
390: alert('$alert{'authen'}');
391: return;
392: }
393: END
394: }
395: }
396: if ($mode eq 'createcourse') {
397: $auth_checks .= "
398: if ( (vf.autoadds[0].checked == true) &&
399: (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
400: ";
401: } elsif ($mode eq 'modifycourse') {
402: $auth_checks .= "
403: if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
404: ";
405: }
406: if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
407: $auth_checks .= (<<END);
408: var alertmsg = '';
409: switch (current.radiovalue) {
410: case 'krb':
411: alertmsg = '$alert{'krb'}';
412: break;
413: default:
414: alertmsg = '';
415: }
416: if (alertmsg != '') {
417: alert(alertmsg);
418: return;
419: }
420: }
421: END
422: } else {
423: $auth_checks .= (<<END);
424: foundatype=1;
425: if (current.argfield == null || current.argfield == '') {
426: var alertmsg = '';
427: switch (current.value) {
428: case 'krb':
429: alertmsg = '$alert{'krb'}';
430: break;
431: case 'loc':
432: case 'fsys':
433: alertmsg = '$alert{'ipass'}';
434: break;
435: case 'fsys':
436: alertmsg = '';
437: break;
438: default:
439: alertmsg = '';
440: }
441: if (alertmsg != '') {
442: alert(alertmsg);
443: return;
444: }
445: }
446: END
447: }
448: my $section_checks;
449: my $optional_checks = '';
450: if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
451: $optional_checks = (<<END);
452: vf.submit();
453: }
454: END
455: } else {
456: $section_checks = §ion_check_js();
457: $optional_checks = (<<END);
458: var message='';
459: if (foundname==0) {
460: message='$alert{'name'}';
461: }
462: if (foundid==0) {
463: if (message!='') {
464: message+='\\n';
465: }
466: message+='$alert{'snum'}';
467: }
468: if (foundsec==0) {
469: if (message!='') {
470: message+='\\n';
471: }
472: }
473: if (foundemail==0) {
474: if (message!='') {
475: message+='\\n';
476: }
477: message+='$alert{'email'}';
478: }
479: if (message!='') {
480: message+= '\\n$alert{'continue'}';
481: if (confirm(message)) {
482: vf.state.value='enrolling';
483: vf.submit();
484: }
485: } else {
486: vf.state.value='enrolling';
487: vf.submit();
488: }
489: }
490: END
491: }
492: my $result = $function_name;
493: if ( ($mode eq 'auth') || ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
494: $result .= $auth_checks;
495: }
496: $result .= $optional_checks."\n".$section_checks;
497: if ( ($mode eq 'auth') || ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
498: $result .= $authheader;
499: }
500: return $result;
501: }
502: ###############################################################
503: ###############################################################
504: sub upload_manager_javascript_forward_associate {
505: return(<<ENDPICK);
506: function verify(vf,sec_caller) {
507: var founduname=0;
508: var foundpwd=0;
509: var foundname=0;
510: var foundid=0;
511: var foundsec=0;
512: var foundemail=0;
513: var foundrole=0;
514: var tw;
515: for (i=0;i<=vf.nfields.value;i++) {
516: tw=eval('vf.f'+i+'.selectedIndex');
517: if (tw==1) { founduname=1; }
518: if ((tw>=2) && (tw<=6)) { foundname=1; }
519: if (tw==7) { foundid=1; }
520: if (tw==8) { foundsec=1; }
521: if (tw==9) { foundpwd=1; }
522: if (tw==10) { foundemail=1; }
523: if (tw==11) { foundrole=1; }
524: }
525: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole);
526: }
527:
528: //
529: // vf = this.form
530: // tf = column number
531: //
532: // values of nw
533: //
534: // 0 = none
535: // 1 = username
536: // 2 = names (lastname, firstnames)
537: // 3 = fname (firstname)
538: // 4 = mname (middlename)
539: // 5 = lname (lastname)
540: // 6 = gen (generation)
541: // 7 = id
542: // 8 = section
543: // 9 = ipwd (password)
544: // 10 = email address
545: // 11 = role
546:
547: function flip(vf,tf) {
548: var nw=eval('vf.f'+tf+'.selectedIndex');
549: var i;
550: // make sure no other columns are labeled the same as this one
551: for (i=0;i<=vf.nfields.value;i++) {
552: if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
553: eval('vf.f'+i+'.selectedIndex=0;')
554: }
555: }
556: // If we set this to 'lastname, firstnames', clear out all the ones
557: // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
558: if (nw==2) {
559: for (i=0;i<=vf.nfields.value;i++) {
560: if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
561: (eval('vf.f'+i+'.selectedIndex')<=6)) {
562: eval('vf.f'+i+'.selectedIndex=0;')
563: }
564: }
565: }
566: // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
567: // clear out any that are set to 'lastname, firstnames' (2)
568: if ((nw>=3) && (nw<=6)) {
569: for (i=0;i<=vf.nfields.value;i++) {
570: if (eval('vf.f'+i+'.selectedIndex')==2) {
571: eval('vf.f'+i+'.selectedIndex=0;')
572: }
573: }
574: }
575: // If we set the password, make the password form below correspond to
576: // the new value.
577: if (nw==9) {
578: changed_radio('int',document.studentform);
579: set_auth_radio_buttons('int',document.studentform);
580: vf.intarg.value='';
581: vf.krbarg.value='';
582: vf.locarg.value='';
583: }
584: }
585:
586: function clearpwd(vf) {
587: var i;
588: for (i=0;i<=vf.nfields.value;i++) {
589: if (eval('vf.f'+i+'.selectedIndex')==9) {
590: eval('vf.f'+i+'.selectedIndex=0;')
591: }
592: }
593: }
594:
595: ENDPICK
596: }
597:
598: ###############################################################
599: ###############################################################
600: sub upload_manager_javascript_reverse_associate {
601: return(<<ENDPICK);
602: function verify(vf,sec_caller) {
603: var founduname=0;
604: var foundpwd=0;
605: var foundname=0;
606: var foundid=0;
607: var foundsec=0;
608: var foundrole=0;
609: var tw;
610: for (i=0;i<=vf.nfields.value;i++) {
611: tw=eval('vf.f'+i+'.selectedIndex');
612: if (i==0 && tw!=0) { founduname=1; }
613: if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
614: if (i==6 && tw!=0) { foundid=1; }
615: if (i==7 && tw!=0) { foundsec=1; }
616: if (i==8 && tw!=0) { foundpwd=1; }
617: if (i==9 && tw!=0) { foundrole=1; }
618: }
619: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundrole);
620: }
621:
622: function flip(vf,tf) {
623: var nw=eval('vf.f'+tf+'.selectedIndex');
624: var i;
625: // picked the all one name field, reset the other name ones to blank
626: if (tf==1 && nw!=0) {
627: for (i=2;i<=5;i++) {
628: eval('vf.f'+i+'.selectedIndex=0;')
629: }
630: }
631: //picked one of the piecewise name fields, reset the all in
632: //one field to blank
633: if ((tf>=2) && (tf<=5) && (nw!=0)) {
634: eval('vf.f1.selectedIndex=0;')
635: }
636: // intial password specified, pick internal authentication
637: if (tf==8 && nw!=0) {
638: changed_radio('int',document.studentform);
639: set_auth_radio_buttons('int',document.studentform);
640: vf.krbarg.value='';
641: vf.intarg.value='';
642: vf.locarg.value='';
643: }
644: }
645:
646: function clearpwd(vf) {
647: var i;
648: if (eval('vf.f8.selectedIndex')!=0) {
649: eval('vf.f8.selectedIndex=0;')
650: }
651: }
652: ENDPICK
653: }
654:
655: ###############################################################
656: ###############################################################
657: sub print_upload_manager_footer {
658: my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context)=@_;
659: my $formname;
660: if ($context eq 'course') {
661: $formname = 'document.studentform';
1.13 raeburn 662: } elsif ($context eq 'author') {
1.1 raeburn 663: $formname = 'document.studentform';
664: } elsif ($context eq 'domain') {
665: $formname = 'document.studentform';
666: }
667: my ($krbdef,$krbdefdom) =
668: &Apache::loncommon::get_kerberos_defaults($defdom);
669: my %param = ( formname => $formname,
670: kerb_def_dom => $krbdefdom,
671: kerb_def_auth => $krbdef
672: );
673: if (exists($env{'form.ipwd_choice'}) &&
674: defined($env{'form.ipwd_choice'}) &&
675: $env{'form.ipwd_choice'} ne '') {
676: $param{'curr_authtype'} = 'int';
677: }
678: my $krbform = &Apache::loncommon::authform_kerberos(%param);
679: my $intform = &Apache::loncommon::authform_internal(%param);
680: my $locform = &Apache::loncommon::authform_local(%param);
681: my $date_table = &date_setting_table(undef,undef,$context);
682:
683: my $Str = "\n".'<div class="LC_left_float">';
684: $Str .= &hidden_input('nfields',$i);
685: $Str .= &hidden_input('keyfields',$keyfields);
686: $Str .= "<h3>".&mt('Login Type')."</h3>\n";
687: if ($context eq 'domain') {
688: $Str .= '<p>'.&mt('Change authentication for existing users to these settings?').' <span class="LC_nobreak"><label><input type="radio" name="changeauth" value="No" checked="checked" />'.&mt('No').'</label> <label><input type="radio" name="changeauth" value="Yes" />'.&mt('Yes').'</label></span></p>';
689: } else {
690: $Str .= "<p>\n".
691: &mt('Note: this will not take effect if the user already exists').
692: &Apache::loncommon::help_open_topic('Auth_Options').
693: "</p>\n";
694: }
695: $Str .= &set_login($defdom,$krbform,$intform,$locform);
696: my ($home_server_pick,$numlib) =
697: &Apache::loncommon::home_server_form_item($defdom,'lcserver',
698: 'default','hide');
699: if ($numlib > 1) {
700: $Str .= '<h3>'.&mt('LON-CAPA Home Server for New Users')."</h3>\n".
701: &mt('LON-CAPA domain: [_1] with home server: [_2]',$defdom,
702: $home_server_pick).'<br />';
703: } else {
704: $Str .= $home_server_pick;
705: }
706: $Str .= '<h3>'.&mt('Starting and Ending Dates').
707: "</h3>\n";
708: $Str .= "<p>\n".$date_table."</p>\n";
709: if ($context eq 'domain') {
710: $Str .= '<h3>'.&mt('Settings for assigning roles:').'</h3>'."\n".
711: &mt('Pick the action to take on roles for these users:').'<br /><span class="LC_nobreak"><label><input type="radio" name="roleaction" value="norole" checked="checked" /> '.&mt('No role changes').'</label> <label><input type="radio" name="roleaction" value="domain" /> '.&mt('Add a domain role').'</label> <label><input type="radio" name="roleaction" value="course" /> '.&mt('Add a course role').'</label></span>';
712: }
1.13 raeburn 713: if ($context eq 'author') {
1.1 raeburn 714: $Str .= '<h3>'.&mt('Default role')."</h3>\n".
715: &mt('Choose the role to assign to users without one specified in the uploaded file');
716: } elsif ($context eq 'course') {
717: $Str .= '<h3>'.&mt('Default role and section')."</h3>\n".
718: &mt('Choose the role and/or section to assign to users without one specified in the uploaded file');
719: } else {
720: $Str .= '<br /><br /><b>'.&mt('Default role and/or section')."</b><br />\n".
721: &mt('Role and/or section for users without one in the uploaded file.');
722: }
723: $Str .= '<br /><br />';
1.2 raeburn 724: my ($options,$cb_script,$coursepick) = &default_role_selector($context,'defaultrole',1);
1.1 raeburn 725: if ($context eq 'domain') {
726: $Str .= '<span class="LC_role_level">'.&mt('Domain Level').'</span><br />'.$options.'<br /><br /><span class="LC_role_level">'.&mt('Course Level').'</span><br />'.$cb_script.$coursepick;
1.13 raeburn 727: } elsif ($context eq 'author') {
1.5 raeburn 728: $Str .= $options;
1.1 raeburn 729: } else {
1.5 raeburn 730: $Str .= '<table><tr><td><span class="LC_nobreak"<b>'.&mt('role').': </b>'.
731: $options.'</span></td><td> </td><td><span class="LC_nobreak">'.
732: '<b>'.&mt('section').': </b><input type="text" name="section" value="" size="12" /></span></td></tr></table>';
1.1 raeburn 733: }
734: if ($context eq 'course') {
735: $Str .= "<h3>".&mt('Full Update')."</h3>\n".
736: '<label><input type="checkbox" name="fullup" value="yes">'.
737: ' '.&mt('Full update (also print list of users not enrolled anymore)').
738: "</label></p>\n";
739: }
1.5 raeburn 740: if ($context eq 'course' || $context eq 'domain') {
741: $Str .= &forceid_change($context);
742: }
1.1 raeburn 743: $Str .= '</div><div class="LC_clear_float_footer"><br /><input type="button"'.
744: 'onClick="javascript:verify(this.form,this.form.csec)" '.
745: 'value="Update Users" />'."<br />\n";
746: if ($context eq 'course') {
747: $Str .= &mt('Note: for large courses, this operation may be time '.
748: 'consuming');
749: }
750: $Str .= '</div>';
751: $r->print($Str);
752: return;
753: }
754:
1.5 raeburn 755: sub forceid_change {
756: my ($context) = @_;
757: my $output =
758: "<h3>".&mt('ID/Student Number')."</h3>\n".
759: "<p>\n".'<label><input type="checkbox" name="forceid" value="yes">'.
760: &mt('Disable ID/Student Number Safeguard and Force Change '.
761: 'of Conflicting IDs').'</label><br />'."\n".
762: &mt('(only do if you know what you are doing.)')."</br><br />\n";
763: if ($context eq 'domain') {
764: $output .= '<label><input type="checkbox" name="recurseid"'.
765: ' value="yes">'.
1.13 raeburn 766: &mt('Update ID/Student Number in courses in which user is Active/Future student,<br />(if forcing change).').
1.5 raeburn 767: '</label></p>'."\n";
768: }
769: return $output;
770: }
771:
1.1 raeburn 772: ###############################################################
773: ###############################################################
774: sub print_upload_manager_form {
775: my ($r,$context) = @_;
776: my $firstLine;
777: my $datatoken;
778: if (!$env{'form.datatoken'}) {
779: $datatoken=&Apache::loncommon::upfile_store($r);
780: } else {
781: $datatoken=$env{'form.datatoken'};
782: &Apache::loncommon::load_tmp_file($r);
783: }
784: my @records=&Apache::loncommon::upfile_record_sep();
785: if($env{'form.noFirstLine'}){
786: $firstLine=shift(@records);
787: }
788: my $total=$#records;
789: my $distotal=$total+1;
790: my $today=time;
791: my $halfyear=$today+15552000;
792: #
793: # Restore memorized settings
794: my $col_setting_names = { 'username_choice' => 'scalar', # column settings
795: 'names_choice' => 'scalar',
796: 'fname_choice' => 'scalar',
797: 'mname_choice' => 'scalar',
798: 'lname_choice' => 'scalar',
799: 'gen_choice' => 'scalar',
800: 'id_choice' => 'scalar',
801: 'sec_choice' => 'scalar',
802: 'ipwd_choice' => 'scalar',
803: 'email_choice' => 'scalar',
804: 'role_choice' => 'scalar',
805: };
806: my $defdom = $env{'request.role.domain'};
807: if ($context eq 'course') {
808: &Apache::loncommon::restore_course_settings('enrollment_upload',
809: $col_setting_names);
810: } else {
811: &Apache::loncommon::restore_settings($context,'user_upload',
812: $col_setting_names);
813: }
814: #
815: # Determine kerberos parameters as appropriate
816: my ($krbdef,$krbdefdom) =
817: &Apache::loncommon::get_kerberos_defaults($defdom);
818: #
819: &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context);
820: my $i;
821: my $keyfields;
822: if ($total>=0) {
823: my @field=
824: (['username',&mt('Username'), $env{'form.username_choice'}],
825: ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
826: ['fname',&mt('First Name'), $env{'form.fname_choice'}],
827: ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
828: ['lname',&mt('Last Name'), $env{'form.lname_choice'}],
829: ['gen', &mt('Generation'), $env{'form.gen_choice'}],
830: ['id', &mt('ID/Student Number'),$env{'form.id_choice'}],
831: ['sec', &mt('Section'), $env{'form.sec_choice'}],
832: ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
833: ['email',&mt('E-mail Address'), $env{'form.email_choice'}],
834: ['role',&mt('Role'), $env{'form.role_choice'}]);
835: if ($env{'form.upfile_associate'} eq 'reverse') {
836: &Apache::loncommon::csv_print_samples($r,\@records);
837: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
838: \@field);
839: foreach (@field) {
840: $keyfields.=$_->[0].',';
841: }
842: chop($keyfields);
843: } else {
844: unshift(@field,['none','']);
845: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
846: \@field);
847: my %sone=&Apache::loncommon::record_sep($records[0]);
848: $keyfields=join(',',sort(keys(%sone)));
849: }
850: }
851: $r->print('</div>');
852: &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
853: $context);
854: }
855:
856: sub setup_date_selectors {
1.11 raeburn 857: my ($starttime,$endtime,$mode,$nolink) = @_;
1.1 raeburn 858: if (! defined($starttime)) {
859: $starttime = time;
860: unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
861: if (exists($env{'course.'.$env{'request.course.id'}.
862: '.default_enrollment_start_date'})) {
863: $starttime = $env{'course.'.$env{'request.course.id'}.
864: '.default_enrollment_start_date'};
865: }
866: }
867: }
868: if (! defined($endtime)) {
869: $endtime = time+(6*30*24*60*60); # 6 months from now, approx
870: unless ($mode eq 'createcourse') {
871: if (exists($env{'course.'.$env{'request.course.id'}.
872: '.default_enrollment_end_date'})) {
873: $endtime = $env{'course.'.$env{'request.course.id'}.
874: '.default_enrollment_end_date'};
875: }
876: }
877: }
1.11 raeburn 878:
879: my $startdateform =
880: &Apache::lonhtmlcommon::date_setter('studentform','startdate',$starttime,
881: undef,undef,undef,undef,undef,undef,undef,$nolink);
882:
883: my $enddateform =
884: &Apache::lonhtmlcommon::date_setter('studentform','enddate',$endtime,
885: undef,undef,undef,undef,undef,undef,undef,$nolink);
886:
1.1 raeburn 887: if ($mode eq 'create_enrolldates') {
888: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
889: 'startenroll',
890: $starttime);
891: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
892: 'endenroll',
893: $endtime);
894: }
895: if ($mode eq 'create_defaultdates') {
896: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
897: 'startaccess',
898: $starttime);
899: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
900: 'endaccess',
901: $endtime);
902: }
903: return ($startdateform,$enddateform);
904: }
905:
906:
907: sub get_dates_from_form {
908: my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
909: my $enddate = &Apache::lonhtmlcommon::get_date_from_form('enddate');
910: if ($env{'form.no_end_date'}) {
911: $enddate = 0;
912: }
913: return ($startdate,$enddate);
914: }
915:
916: sub date_setting_table {
1.11 raeburn 917: my ($starttime,$endtime,$mode,$bulkaction) = @_;
918: my $nolink;
919: if ($bulkaction) {
920: $nolink = 1;
921: }
922: my ($startform,$endform) =
923: &setup_date_selectors($starttime,$endtime,$mode,$nolink);
1.1 raeburn 924: my $dateDefault;
925: if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
926: $dateDefault = ' ';
1.13 raeburn 927: } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11 raeburn 928: if (($bulkaction eq 'reenable') ||
929: ($bulkaction eq 'activate') ||
930: ($bulkaction eq 'chgdates')) {
931: $dateDefault = '<span class="LC_nobreak">'.
932: '<label><input type="checkbox" name="makedatesdefault" /> '.
933: &mt('make these dates the default for future enrollment').
934: '</label></span>';
935: }
1.1 raeburn 936: }
1.11 raeburn 937: my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1 raeburn 938: if (defined($endtime) && $endtime == 0) {
939: $perpetual .= ' checked';
940: }
1.11 raeburn 941: $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1 raeburn 942: if ($mode eq 'create_enrolldates') {
943: $perpetual = ' ';
944: }
1.11 raeburn 945: my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
946: $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
947: 'LC_oddrow_value')."\n".
948: $startform."\n".
949: &Apache::lonhtmlcommon::row_closure(1).
950: &Apache::lonhtmlcommon::row_title(&mt('Ending Date'),
951: 'LC_oddrow_value')."\n".
952: $endform.' '.$perpetual.
953: &Apache::lonhtmlcommon::row_closure(1).
954: &Apache::lonhtmlcommon::end_pick_box().'<br />';
1.1 raeburn 955: if ($dateDefault) {
956: $result .= $dateDefault.'<br />'."\n";
957: }
958: return $result;
959: }
960:
961: sub make_dates_default {
962: my ($startdate,$enddate,$context) = @_;
963: my $result = '';
964: if ($context eq 'course') {
965: my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
966: my $crs = $env{'course.'.$env{'request.course.id'}.'.num'};
967: my $put_result = &Apache::lonnet::put('environment',
968: {'default_enrollment_start_date'=>$startdate,
969: 'default_enrollment_end_date' =>$enddate},$dom,$crs);
970: if ($put_result eq 'ok') {
1.11 raeburn 971: $result .= &mt('Set default start and end dates for course').
972: '<br />'."\n";
1.1 raeburn 973: #
974: # Refresh the course environment
975: &Apache::lonnet::coursedescription($env{'request.course.id'},
976: {'freshen_cache' => 1});
977: } else {
978: $result .= &mt('Unable to set default dates for course').":".$put_result.
979: '<br />';
980: }
981: }
982: return $result;
983: }
984:
985: sub default_role_selector {
1.2 raeburn 986: my ($context,$checkpriv) = @_;
1.1 raeburn 987: my %customroles;
988: my ($options,$coursepick,$cb_jscript);
1.13 raeburn 989: if ($context ne 'author') {
1.1 raeburn 990: %customroles = &my_custom_roles();
991: }
992:
993: my %lt=&Apache::lonlocal::texthash(
994: 'rol' => "Role",
995: 'grs' => "Section",
996: 'exs' => "Existing sections",
997: 'new' => "New section",
998: );
999: $options = '<select name="defaultrole">'."\n".
1000: ' <option value="">'.&mt('Please select').'</option>'."\n";
1001: if ($context eq 'course') {
1.2 raeburn 1002: $options .= &default_course_roles($context,$checkpriv,%customroles);
1.13 raeburn 1003: } elsif ($context eq 'author') {
1.2 raeburn 1004: my @roles = &construction_space_roles($checkpriv);
1.1 raeburn 1005: foreach my $role (@roles) {
1006: my $plrole=&Apache::lonnet::plaintext($role);
1007: $options .= ' <option value="'.$role.'">'.$plrole.'</option>'."\n";
1008: }
1009: } elsif ($context eq 'domain') {
1.2 raeburn 1010: my @roles = &domain_roles($checkpriv);
1.1 raeburn 1011: foreach my $role (@roles) {
1012: my $plrole=&Apache::lonnet::plaintext($role);
1013: $options .= ' <option value="'.$role.'">'.$plrole.'</option>';
1014: }
1015: my $courseform = &Apache::loncommon::selectcourse_link
1016: ('studentform','defaultcourse','defaultdomain','defaultdesc',"$env{'request.role.domain'}",undef,'Course');
1017: $cb_jscript =
1018: &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'defaultsec','studentform');
1019: $coursepick = &Apache::loncommon::start_data_table().
1020: &Apache::loncommon::start_data_table_header_row().
1021: '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
1022: '<th>'.$lt{'grs'}.'</th>'.
1023: &Apache::loncommon::end_data_table_header_row().
1024: &Apache::loncommon::start_data_table_row()."\n".
1025: '<td><input type="text" name="defaultdesc" value="" onFocus="this.blur();opencrsbrowser('."'studentform','defcourse','defdomain','coursedesc',''".')" /></td>'."\n".
1026: '<td><select name="courserole">'."\n".
1.2 raeburn 1027: &default_course_roles($context,$checkpriv,%customroles)."\n".
1.1 raeburn 1028: '</select></td><td>'.
1029: '<table class="LC_createuser">'.
1030: '<tr class="LC_section_row"><td valign"top">'.
1031: $lt{'exs'}.'<br /><select name="defaultsec">'.
1032: ' <option value=""><--'.&mt('Pick course first').
1033: '</select></td>'.
1034: '<td> </td>'.
1035: '<td valign="top">'.$lt{'new'}.'<br />'.
1036: '<input type="text" name="newsec" value="" size="5" />'.
1037: '<input type="hidden" name="groups" value="" /></td>'.
1038: '</tr></table></td>'.
1039: &Apache::loncommon::end_data_table_row().
1040: &Apache::loncommon::end_data_table()."\n".
1041: '<input type="hidden" name="defaultcourse" value="" />'.
1042: '<input type="hidden" name="defaultdomain" value="" />';
1043: }
1044: $options .= '</select>';
1045: return ($options,$cb_jscript,$coursepick);
1046: }
1047:
1048: sub default_course_roles {
1.2 raeburn 1049: my ($context,$checkpriv,%customroles) = @_;
1.1 raeburn 1050: my $output;
1.2 raeburn 1051: my @roles = &course_roles($context,$checkpriv);
1.1 raeburn 1052: foreach my $role (@roles) {
1053: my $plrole=&Apache::lonnet::plaintext($role);
1054: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1055: }
1056: if (keys(%customroles) > 0) {
1.2 raeburn 1057: my %customroles = &my_custom_roles();
1.1 raeburn 1058: foreach my $cust (sort(keys(%customroles))) {
1059: my $custrole='cr_cr_'.$env{'user.domain'}.
1060: '_'.$env{'user.name'}.'_'.$cust;
1061: $output .= ' <option value="'.$custrole.'">'.$cust.'</option>';
1062: }
1063: }
1064: return $output;
1065: }
1066:
1067: sub construction_space_roles {
1.2 raeburn 1068: my ($checkpriv) = @_;
1.1 raeburn 1069: my @allroles = ('ca','aa');
1070: my @roles;
1.2 raeburn 1071: if ($checkpriv) {
1072: foreach my $role (@allroles) {
1073: if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) {
1074: push(@roles,$role);
1075: }
1.1 raeburn 1076: }
1.2 raeburn 1077: return @roles;
1078: } else {
1079: return @allroles;
1.1 raeburn 1080: }
1081: }
1082:
1083: sub domain_roles {
1.2 raeburn 1084: my ($checkpriv) = @_;
1.1 raeburn 1085: my @allroles = ('dc','li','dg','au','sc');
1086: my @roles;
1.2 raeburn 1087: if ($checkpriv) {
1088: foreach my $role (@allroles) {
1089: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
1090: push(@roles,$role);
1091: }
1.1 raeburn 1092: }
1.2 raeburn 1093: return @roles;
1094: } else {
1095: return @allroles;
1.1 raeburn 1096: }
1097: }
1098:
1099: sub course_roles {
1.2 raeburn 1100: my ($context,$checkpriv) = @_;
1.1 raeburn 1101: my @allroles = ('st','ta','ep','in','cc');
1102: my @roles;
1103: if ($context eq 'domain') {
1104: @roles = @allroles;
1105: } elsif ($context eq 'course') {
1106: if ($env{'request.course.id'}) {
1.2 raeburn 1107: if ($checkpriv) {
1108: foreach my $role (@allroles) {
1109: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
1110: push(@roles,$role);
1111: } else {
1112: if ($role ne 'cc' && $env{'request.course.section'} ne '') {
1113: if (!&Apache::lonnet::allowed('c'.$role,
1114: $env{'request.course.id'}.'/'.
1115: $env{'request.course.section'})) {
1116: push(@roles,$role);
1117: }
1.1 raeburn 1118: }
1119: }
1120: }
1.2 raeburn 1121: } else {
1122: @roles = @allroles;
1.1 raeburn 1123: }
1124: }
1125: }
1126: return @roles;
1127: }
1128:
1129: sub curr_role_permissions {
1.2 raeburn 1130: my ($context,$setting,$checkpriv) = @_;
1.1 raeburn 1131: my @roles;
1.13 raeburn 1132: if ($context eq 'author') {
1.2 raeburn 1133: @roles = &construction_space_roles($checkpriv);
1.1 raeburn 1134: } elsif ($context eq 'domain') {
1135: if ($setting eq 'course') {
1.2 raeburn 1136: @roles = &course_roles($context,$checkpriv);
1.1 raeburn 1137: } else {
1.2 raeburn 1138: @roles = &domain_roles($checkpriv);
1.1 raeburn 1139: }
1140: } elsif ($context eq 'course') {
1.2 raeburn 1141: @roles = &course_roles($context,$checkpriv);
1.1 raeburn 1142: }
1143: return @roles;
1144: }
1145:
1146: # ======================================================= Existing Custom Roles
1147:
1148: sub my_custom_roles {
1149: my %returnhash=();
1150: my %rolehash=&Apache::lonnet::dump('roles');
1151: foreach my $key (keys %rolehash) {
1152: if ($key=~/^rolesdef\_(\w+)$/) {
1153: $returnhash{$1}=$1;
1154: }
1155: }
1156: return %returnhash;
1157: }
1158:
1.2 raeburn 1159: sub print_userlist {
1160: my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
1161: $idlist,$idlist_titles) = @_;
1162: my $format = $env{'form.output'};
1.1 raeburn 1163: if (! exists($env{'form.sortby'})) {
1164: $env{'form.sortby'} = 'username';
1165: }
1.2 raeburn 1166: if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
1167: $env{'form.Status'} = 'Active';
1.1 raeburn 1168: }
1169: my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2 raeburn 1170: ($env{'form.Status'});
1.1 raeburn 1171:
1.2 raeburn 1172: if ($env{'form.showrole'} eq '') {
1.13 raeburn 1173: if ($context eq 'course') {
1174: $env{'form.showrole'} = 'st';
1175: } else {
1176: $env{'form.showrole'} = 'Any';
1177: }
1.2 raeburn 1178: }
1.1 raeburn 1179: if (! defined($env{'form.output'}) ||
1180: $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
1181: $env{'form.output'} = 'html';
1182: }
1183:
1.2 raeburn 1184: my @statuses;
1185: if ($env{'form.Status'} eq 'Any') {
1186: @statuses = ('previous','active','future');
1187: } elsif ($env{'form.Status'} eq 'Expired') {
1188: @statuses = ('previous');
1189: } elsif ($env{'form.Status'} eq 'Active') {
1190: @statuses = ('active');
1191: } elsif ($env{'form.Status'} eq 'Future') {
1192: @statuses = ('future');
1193: }
1.1 raeburn 1194:
1.2 raeburn 1195: # if ($context eq 'course') {
1196: # $r->print(&display_adv_courseroles());
1197: # }
1.1 raeburn 1198: #
1199: # Interface output
1.2 raeburn 1200: $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
1201: '<input type="hidden" name="action" value="'.
1.1 raeburn 1202: $env{'form.action'}.'" />');
1203: $r->print("<p>\n");
1204: if ($env{'form.action'} ne 'modifystudent') {
1205: my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
1206: 'excel' => "Excel",
1207: 'html' => 'HTML');
1208: my $output_selector = '<select size="1" name="output" >';
1209: foreach my $outputformat ('html','csv','excel') {
1210: my $option = '<option value="'.$outputformat.'" ';
1211: if ($outputformat eq $env{'form.output'}) {
1212: $option .= 'selected ';
1213: }
1214: $option .='>'.$lt{$outputformat}.'</option>';
1215: $output_selector .= "\n".$option;
1216: }
1217: $output_selector .= '</select>';
1218: $r->print('<label>'.&mt('Output Format: [_1]',$output_selector).'</label>'.(' 'x3));
1219: }
1.2 raeburn 1220: $r->print('<label>'.&mt('User Status: [_1]',$status_select).'</label>'.(' 'x3)."\n");
1221: my $roleselected = '';
1222: if ($env{'form.showrole'} eq 'Any') {
1223: $roleselected = ' selected="selected" ';
1224: }
1225: my $role_select;
1226: if ($context eq 'domain') {
1227: $role_select = &domain_roles_select();
1228: $r->print('<label>'.&mt('Role Type: [_1]',$role_select).'</label>');
1229: } else {
1230: $role_select = '<select name="showrole">'."\n".
1231: '<option value="Any" '.$roleselected.'>'.
1232: &mt('Any role').'</option>';
1233: my @poss_roles = &curr_role_permissions($context);
1234: foreach my $role (@poss_roles) {
1235: $roleselected = '';
1236: if ($role eq $env{'form.showrole'}) {
1237: $roleselected = ' selected="selected" ';
1238: }
1239: my $plrole=&Apache::lonnet::plaintext($role);
1240: $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
1241: }
1242: $roleselected = '';
1243: if ($env{'form.showrole'} eq 'cr') {
1244: $roleselected = ' selected="selected" ';
1245: }
1246: $role_select .= '<option value="cr"'.$roleselected.'>'.&mt('Custom role').'</option>'.
1247: '</select>';
1248: $r->print('<label>'.&mt('Role: [_1]',$role_select).'</label>');
1249: }
1250: if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
1251: $r->print(&list_submit_button(&mt('Update Display'))."\n</p>\n");
1252: }
1253: my ($indexhash,$keylist) = &make_keylist_array();
1254: my (%userlist,%userinfo);
1.3 raeburn 1255: if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
1256: my $courseform =
1257: &Apache::lonhtmlcommon::course_selection($formname,$totcodes,
1258: $codetitles,$idlist,$idlist_titles);
1259: $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
1260: &Apache::lonhtmlcommon::start_pick_box()."\n".
1261: &Apache::lonhtmlcommon::row_title(&mt('Select Course(s)'),
1262: 'LC_oddrow_value')."\n".
1263: $courseform."\n".
1264: &Apache::lonhtmlcommon::row_closure(1).
1265: &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1266: '<p>'.&list_submit_button(&mt('Update Display')).
1267: "\n</p>\n");
1.11 raeburn 1268: if ($env{'form.coursepick'}) {
1269: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1270: }
1271: } else {
1272: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1.3 raeburn 1273: }
1274: $r->rflush();
1.1 raeburn 1275: if ($context eq 'course') {
1.3 raeburn 1276: my $classlist = &Apache::loncoursedata::get_classlist();
1277: my $secidx = &Apache::loncoursedata::CL_SECTION();
1278: foreach my $student (keys(%{$classlist})) {
1279: if (exists($permission->{'view_section'})) {
1280: if ($classlist->{$student}[$secidx] ne $permission->{'view_section'}) {
1281: next;
1282: } else {
1283: $userlist{$student} = $classlist->{$student};
1.1 raeburn 1284: }
1.3 raeburn 1285: } else {
1286: $userlist{$student} = $classlist->{$student};
1.1 raeburn 1287: }
1288: }
1.2 raeburn 1289: my $cid =$env{'request.course.id'};
1290: my $cdom=$env{'course.'.$cid.'.domain'};
1291: my $cnum=$env{'course.'.$cid.'.num'};
1292: my $showroles;
1293: if ($env{'form.showrole'} ne 'Any') {
1294: $showroles = [$env{'form.showrole'}];
1.1 raeburn 1295: } else {
1.2 raeburn 1296: $showroles = undef;
1297: }
1298: my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
1299: \@statuses,$showroles);
1300: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11 raeburn 1301: \%advrolehash,$permission);
1.2 raeburn 1302: } else {
1303: my (%cstr_roles,%dom_roles);
1.13 raeburn 1304: if ($context eq 'author') {
1.2 raeburn 1305: # List co-authors and assistant co-authors
1306: my @possroles = ('ca','aa');
1307: %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
1308: \@statuses,\@possroles);
1309: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11 raeburn 1310: \%cstr_roles,$permission);
1.2 raeburn 1311: } elsif ($context eq 'domain') {
1312: if ($env{'form.roletype'} eq 'domain') {
1313: %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
1314: foreach my $key (keys(%dom_roles)) {
1315: if (ref($dom_roles{$key}) eq 'HASH') {
1316: &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11 raeburn 1317: \%userinfo,$dom_roles{$key},$permission);
1.2 raeburn 1318: }
1319: }
1.13 raeburn 1320: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 1321: my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
1322: my %coauthors;
1323: foreach my $key (keys(%dom_roles)) {
1324: if (ref($dom_roles{$key}) eq 'HASH') {
1325: if ($env{'form.showrole'} eq 'au') {
1326: &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11 raeburn 1327: \%userinfo,$dom_roles{$key},$permission);
1.2 raeburn 1328: } else {
1329: my @possroles;
1330: if ($env{'form.showrole'} eq 'Any') {
1331: @possroles = ('ca','aa');
1332: } else {
1333: @possroles = ($env{'form.showrole'});
1334: }
1335: foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1336: my ($role,$authorname,$authordom) = split(/:/,$author);
1337: my $extent = '/'.$authordom.'/'.$authorname;
1338: %{$coauthors{$extent}} =
1339: &Apache::lonnet::get_my_roles($authorname,
1340: $authordom,undef,\@statuses,\@possroles);
1341: }
1342: &gather_userinfo($context,$format,\%userlist,
1.11 raeburn 1343: $indexhash,\%userinfo,\%coauthors,$permission);
1.2 raeburn 1344: }
1345: }
1346: }
1347: } elsif ($env{'form.roletype'} eq 'course') {
1348: if ($env{'form.coursepick'}) {
1349: my %courses = &process_coursepick();
1350: my %allusers;
1351: foreach my $cid (keys(%courses)) {
1352: my %coursehash =
1353: &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
1354: my $cdom = $coursehash{'domain'};
1355: my $cnum = $coursehash{'num'};
1.11 raeburn 1356: next if ($cnum eq '' || $cdom eq '');
1.2 raeburn 1357: my $cdesc = $coursehash{'description'};
1358: my (@roles,@sections,%access,%users,%userdata,
1.6 albertel 1359: %statushash);
1.2 raeburn 1360: if ($env{'form.showrole'} eq 'Any') {
1361: @roles = &course_roles($context);
1.5 raeburn 1362: unshift(@roles,'cr');
1.2 raeburn 1363: } else {
1364: @roles = ($env{'form.showrole'});
1365: }
1366: foreach my $role (@roles) {
1367: %{$users{$role}} = ();
1368: }
1369: foreach my $type (@statuses) {
1370: $access{$type} = $type;
1371: }
1372: &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash);
1373: foreach my $user (keys(%userdata)) {
1374: next if (ref($userinfo{$user}) eq 'HASH');
1375: foreach my $item ('fullname','id') {
1376: $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
1377: }
1378: }
1379: foreach my $role (keys(%users)) {
1380: foreach my $user (keys(%{$users{$role}})) {
1381: my $uniqid = $user.':'.$role;
1382: $allusers{$uniqid}{$cid} = { desc => $cdesc,
1383: secs => $statushash{$user}{$role},
1384: };
1385: }
1386: }
1387: }
1388: &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11 raeburn 1389: \%userinfo,\%allusers,$permission);
1.2 raeburn 1390: } else {
1.10 raeburn 1391: $r->print('<input type="hidden" name="phase" value="'.
1392: $env{'form.phase'}.'" /></form>');
1.2 raeburn 1393: return;
1394: }
1.1 raeburn 1395: }
1396: }
1.3 raeburn 1397: }
1398: if (keys(%userlist) == 0) {
1.13 raeburn 1399: if ($context eq 'author') {
1.3 raeburn 1400: $r->print(&mt('There are no co-authors to display.')."\n");
1401: } elsif ($context eq 'domain') {
1402: if ($env{'form.roletype'} eq 'domain') {
1403: $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13 raeburn 1404: } elsif ($env{'form.roletype'} eq 'author') {
1.3 raeburn 1405: $r->print(&mt('There are no authors or co-authors to display.')."\n");
1406: } elsif ($env{'form.roletype'} eq 'course') {
1407: $r->print(&mt('There are no course users to display')."\n");
1.2 raeburn 1408: }
1.3 raeburn 1409: } elsif ($context eq 'course') {
1410: $r->print(&mt('There are no course users to display.')."\n");
1411: }
1412: } else {
1413: # Print out the available choices
1.4 raeburn 1414: my $usercount;
1.3 raeburn 1415: if ($env{'form.action'} eq 'modifystudent') {
1.10 raeburn 1416: ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4 raeburn 1417: $env{'form.Status'},\%userlist,$keylist);
1.1 raeburn 1418: } else {
1.4 raeburn 1419: ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10 raeburn 1420: $permission,$env{'form.Status'},\%userlist,$keylist);
1.4 raeburn 1421: }
1422: if (!$usercount) {
1423: $r->print('<br />'.&mt('There are no users matching the search criteria.'));
1.2 raeburn 1424: }
1425: }
1.10 raeburn 1426: $r->print('<input type="hidden" name="phase" value="'.
1427: $env{'form.phase'}.'" /></form>');
1.2 raeburn 1428: }
1429:
1430: sub list_submit_button {
1431: my ($text) = @_;
1.11 raeburn 1432: return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2 raeburn 1433: }
1434:
1435: sub gather_userinfo {
1.11 raeburn 1436: my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.2 raeburn 1437: foreach my $item (keys(%{$rolehash})) {
1438: @{$userlist->{$item}} = ();
1439: my %userdata;
1.13 raeburn 1440: if ($context eq 'author' || $context eq 'course') {
1.2 raeburn 1441: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1442: split(/:/,$item);
1443: ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1444: &build_user_record(\%userdata,$userinfo,$indexhash,$item,$userlist);
1445: } elsif ($context eq 'domain') {
1446: if ($env{'form.roletype'} eq 'domain') {
1447: ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
1448: split(/:/,$item);
1449: ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1450: &build_user_record(\%userdata,$userinfo,$indexhash,$item,$userlist);
1.13 raeburn 1451: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 1452: if (ref($rolehash->{$item}) eq 'HASH') {
1453: $userdata{'extent'} = $item;
1454: foreach my $key (keys(%{$rolehash->{$item}})) {
1455: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) = split(/:/,$key);
1456: ($userdata{'start'},$userdata{'end'}) =
1457: split(/:/,$rolehash->{$item}{$key});
1458: my $uniqid = $key.':'.$item;
1459: &build_user_record(\%userdata,$userinfo,$indexhash,$uniqid,$userlist);
1460: }
1461: }
1462: } elsif ($env{'form.roletype'} eq 'course') {
1463: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1464: split(/:/,$item);
1465: if (ref($rolehash->{$item}) eq 'HASH') {
1.11 raeburn 1466: my $numcids = keys(%{$rolehash->{$item}});
1.2 raeburn 1467: foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
1468: if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
1469: my $spanstart = '';
1470: my $spanend = '; ';
1471: my $space = ', ';
1472: if ($format eq 'html' || $format eq 'view') {
1473: $spanstart = '<span class="LC_nobreak">';
1.11 raeburn 1474: if ($permission->{'cusr'}) {
1475: if ($numcids > 1) {
1476: $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />';
1477: } else {
1478: $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />';
1479: }
1480: }
1.2 raeburn 1481: $spanend = '</span><br />';
1482: $space = ', ';
1483: }
1484: $userdata{'extent'} .= $spanstart.
1485: $rolehash->{$item}{$cid}{'desc'}.$space;
1486: if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') {
1487: foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1488: $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
1489: }
1490: }
1491: }
1492: }
1493: }
1494: &build_user_record(\%userdata,$userinfo,$indexhash,$item,$userlist);
1495: }
1496: }
1497: }
1498: return;
1499: }
1500:
1501: sub build_user_record {
1502: my ($userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11 raeburn 1503: next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.2 raeburn 1504: &process_date_info($userdata);
1505: my $username = $userdata->{'username'};
1506: my $domain = $userdata->{'domain'};
1507: if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1508: $userdata->{'fullname'} =
1509: $userinfo->{$username.':'.$domain}{'fullname'};
1510: $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
1511: } else {
1512: &aggregate_user_info($domain,$username,$userinfo);
1513: $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1514: $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
1515: }
1516: foreach my $key (keys(%{$indexhash})) {
1517: if (defined($userdata->{$key})) {
1518: $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
1519: }
1520: }
1521: return;
1522: }
1523:
1524: sub courses_selector {
1525: my ($cdom,$formname) = @_;
1526: my %coursecodes = ();
1527: my %codes = ();
1528: my @codetitles = ();
1529: my %cat_titles = ();
1530: my %cat_order = ();
1531: my %idlist = ();
1532: my %idnums = ();
1533: my %idlist_titles = ();
1534: my $caller = 'global';
1535: my $format_reply;
1536: my $jscript = '';
1537:
1.7 albertel 1538: my $totcodes = 0;
1539: $totcodes =
1.2 raeburn 1540: &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
1541: $cdom,$totcodes);
1542: if ($totcodes > 0) {
1543: $format_reply =
1544: &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
1545: \%codes,\@codetitles,\%cat_titles,\%cat_order);
1546: if ($format_reply eq 'ok') {
1547: my $numtypes = @codetitles;
1548: &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
1549: my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
1550: my $longtitles_str = join('","',@{$longtitles});
1551: my $allidlist = $idlist{$codetitles[0]};
1552: $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
1553: $jscript .= $scripttext;
1554: $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
1555: }
1556: }
1557: my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
1558:
1559: my %elements = (
1560: Year => 'selectbox',
1561: coursepick => 'radio',
1562: coursetotal => 'text',
1563: courselist => 'text',
1564: );
1565: $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
1566: if ($env{'form.coursepick'} eq 'category') {
1567: $jscript .= qq|
1568: function setCourseCat(formname) {
1569: if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
1570: return;
1571: }
1572: courseSet('Year');
1573: for (var j=0; j<formname.Semester.length; j++) {
1574: if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
1575: formname.Semester.options[j].selected = true;
1576: }
1577: }
1578: if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
1579: return;
1580: }
1581: courseSet('Semester');
1582: for (var j=0; j<formname.Department.length; j++) {
1583: if (formname.Department.options[j].value == "$env{'form.Department'}") { formname.Department.options[j].selected = true;
1584: }
1585: }
1586: if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
1587: return;
1588: }
1589: courseSet('Department');
1590: for (var j=0; j<formname.Number.length; j++) {
1591: if (formname.Number.options[j].value == "$env{'form.Number'}") {
1592: formname.Number.options[j].selected = true;
1593: }
1594: }
1595: }
1596: |;
1597: }
1598: return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
1599: \%idlist_titles);
1600: }
1601:
1602: sub course_selector_loadcode {
1603: my ($formname) = @_;
1604: my $loadcode;
1605: if ($env{'form.coursepick'} ne '') {
1606: $loadcode = 'javascript:setFormElements(document.'.$formname.')';
1607: if ($env{'form.coursepick'} eq 'category') {
1608: $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
1609: }
1610: }
1611: return $loadcode;
1612: }
1613:
1614: sub process_coursepick {
1615: my $coursefilter = $env{'form.coursepick'};
1616: my $cdom = $env{'request.role.domain'};
1617: my %courses;
1618: if ($coursefilter eq 'all') {
1619: %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1620: undef,undef,'Course');
1621: } elsif ($coursefilter eq 'category') {
1622: my $instcode = &instcode_from_coursefilter();
1623: %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1624: undef,undef,'Course');
1625: } elsif ($coursefilter eq 'specific') {
1626: if ($env{'form.coursetotal'} > 1) {
1627: my @course_ids = split(/&&/,$env{'form.courselist'});
1628: foreach my $cid (@course_ids) {
1629: $courses{$cid} = '';
1.1 raeburn 1630: }
1.2 raeburn 1631: } else {
1632: $courses{$env{'form.courselist'}} = '';
1.1 raeburn 1633: }
1.2 raeburn 1634: }
1635: return %courses;
1636: }
1637:
1638: sub instcode_from_coursefilter {
1639: my $instcode = '';
1640: my @cats = ('Semester','Year','Department','Number');
1641: foreach my $category (@cats) {
1642: if (defined($env{'form.'.$category})) {
1643: unless ($env{'form.'.$category} eq '-1') {
1644: $instcode .= $env{'form.'.$category};
1645: }
1646: }
1647: }
1648: if ($instcode eq '') {
1649: $instcode = '.';
1650: }
1651: return $instcode;
1652: }
1653:
1654: sub display_adv_courseroles {
1655: my $output;
1656: #
1657: # List course personnel
1658: my %coursepersonnel =
1659: &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
1660: #
1661: $output = '<br />'.&Apache::loncommon::start_data_table();
1662: foreach my $role (sort(keys(%coursepersonnel))) {
1663: next if ($role =~ /^\s*$/);
1664: $output .= &Apache::loncommon::start_data_table_row().
1665: '<td>'.$role.'</td><td>';
1666: foreach my $user (split(',',$coursepersonnel{$role})) {
1667: my ($puname,$pudom)=split(':',$user);
1668: $output .= ' '.&Apache::loncommon::aboutmewrapper(
1669: &Apache::loncommon::plainname($puname,$pudom),
1670: $puname,$pudom);
1671: }
1672: $output .= '</td>'.&Apache::loncommon::end_data_table_row();
1673: }
1674: $output .= &Apache::loncommon::end_data_table();
1675: }
1676:
1677: sub make_keylist_array {
1678: my ($index,$keylist);
1679: $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
1680: $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
1681: $index->{'end'} = &Apache::loncoursedata::CL_END();
1682: $index->{'start'} = &Apache::loncoursedata::CL_START();
1683: $index->{'id'} = &Apache::loncoursedata::CL_ID();
1684: $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
1685: $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
1686: $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
1687: $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
1688: $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
1689: $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
1690: $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
1691: $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
1692: $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1693: foreach my $key (keys(%{$index})) {
1694: $keylist->[$index->{$key}] = $key;
1695: }
1696: return ($index,$keylist);
1697: }
1698:
1699: sub aggregate_user_info {
1700: my ($udom,$uname,$userinfo) = @_;
1701: my %info=&Apache::lonnet::get('environment',
1702: ['firstname','middlename',
1703: 'lastname','generation','id'],
1704: $udom,$uname);
1705: my ($tmp) = keys(%info);
1706: my ($fullname,$id);
1707: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
1708: $fullname = 'not available';
1709: $id = 'not available';
1710: &Apache::lonnet::logthis('unable to retrieve environment '.
1711: 'for '.$uname.':'.$udom);
1.1 raeburn 1712: } else {
1.2 raeburn 1713: $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
1714: $id = $info{'id'};
1715: }
1716: $userinfo->{$uname.':'.$udom} = {
1717: fullname => $fullname,
1718: id => $id,
1719: };
1720: return;
1721: }
1.1 raeburn 1722:
1.2 raeburn 1723: sub process_date_info {
1724: my ($userdata) = @_;
1725: my $now = time;
1726: $userdata->{'status'} = 'Active';
1727: if ($userdata->{'start'} > 0) {
1728: if ($now < $userdata->{'start'}) {
1729: $userdata->{'status'} = 'Future';
1730: }
1.1 raeburn 1731: }
1.2 raeburn 1732: if ($userdata->{'end'} > 0) {
1733: if ($now > $userdata->{'end'}) {
1734: $userdata->{'status'} = 'Expired';
1735: }
1736: }
1737: return;
1.1 raeburn 1738: }
1739:
1740: sub show_users_list {
1.10 raeburn 1741: my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist)=@_;
1.1 raeburn 1742: #
1743: # Variables for excel output
1744: my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
1745: #
1746: # Variables for csv output
1747: my ($CSVfile,$CSVfilename);
1748: #
1749: my $sortby = $env{'form.sortby'};
1.3 raeburn 1750: my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2 raeburn 1751: if ($context eq 'course') {
1.3 raeburn 1752: push(@sortable,('section','groups','type'));
1.2 raeburn 1753: } else {
1.3 raeburn 1754: push(@sortable,'extent');
1755: }
1756: if (!grep(/^\Q$sortby\E$/,@sortable)) {
1757: $sortby = 'username';
1.1 raeburn 1758: }
1.11 raeburn 1759: my $setting = $env{'form.roleaction'};
1.2 raeburn 1760: my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers);
1.1 raeburn 1761: if ($context eq 'course') {
1762: $cid=$env{'request.course.id'};
1763: $cdom = $env{'course.'.$cid.'.domain'};
1764: $cnum = $env{'course.'.$cid.'.num'};
1.2 raeburn 1765: ($classgroups) = &Apache::loncoursedata::get_group_memberships(
1766: $userlist,$keylist,$cdom,$cnum);
1.1 raeburn 1767: if (! exists($env{'form.displayphotos'})) {
1768: $env{'form.displayphotos'} = 'off';
1769: }
1770: $displayphotos = $env{'form.displayphotos'};
1771: if (! exists($env{'form.displayclickers'})) {
1772: $env{'form.displayclickers'} = 'off';
1773: }
1774: $displayclickers = $env{'form.displayclickers'};
1775: if ($env{'course.'.$cid.'.internal.showphoto'}) {
1776: $r->print('
1777: <script type="text/javascript">
1778: function photowindow(photolink) {
1779: var title = "Photo_Viewer";
1780: var options = "scrollbars=1,resizable=1,menubar=0";
1781: options += ",width=240,height=240";
1782: stdeditbrowser = open(photolink,title,options,"1");
1783: stdeditbrowser.focus();
1784: }
1785: </script>
1786: ');
1787: }
1788: $r->print(<<END);
1789: <input type="hidden" name="displayphotos" value="$displayphotos" />
1790: <input type="hidden" name="displayclickers" value="$displayclickers" />
1791: END
1792: }
1793: unless ($mode eq 'autoenroll') {
1.11 raeburn 1794: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1795: my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
1796: my $singconfirm = &mt(' for a single user');
1797: my $multconfirm = &mt(' for multiple users');
1798: my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.1 raeburn 1799: $r->print(<<END);
1.10 raeburn 1800:
1801: <script type="text/javascript" language="Javascript">
1.11 raeburn 1802: $check_uncheck_js
1803:
1804: function verify_action (field) {
1805: var numchecked = 0;
1806: var singconf = '$singconfirm';
1807: var multconf = '$multconfirm';
1808: if (field.length > 0) {
1809: for (i = 0; i < field.length; i++) {
1810: if (field[i].checked == true) {
1811: numchecked ++;
1812: }
1813: }
1814: } else {
1815: if (field.checked == true) {
1816: numchecked ++;
1817: }
1818: }
1819: if (numchecked == 0) {
1820: alert("$alert");
1821: }
1822: else {
1823: var message = document.studentform.bulkaction[document.studentform.bulkaction.selectedIndex].text;
1824: if (numchecked == 1) {
1825: message += singconf;
1826: }
1827: else {
1828: message += multconf;
1829: }
1830: if (confirm(message)) {
1831: document.studentform.phase.value = 'bulkchange';
1832: document.studentform.submit();
1833: }
1834: }
1835: }
1.10 raeburn 1836:
1837: function username_display_launch(username,domain) {
1838: var target;
1839: for (var i=0; i<document.studentform.usernamelink.length; i++) {
1840: if (document.studentform.usernamelink[i].checked) {
1841: target = document.studentform.usernamelink[i].value;
1842: }
1843: }
1844: if (target == 'modify') {
1845: document.studentform.srchterm.value=username;
1846: document.studentform.srchdomain.value=domain;
1847: document.studentform.phase.value='get_user_info';
1848: document.studentform.action.value = 'singleuser';
1849: document.studentform.submit();
1850: }
1851: else {
1852: document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
1853: }
1854: }
1855: </script>
1.11 raeburn 1856: $date_sec_selector
1.1 raeburn 1857: <input type="hidden" name="state" value="$env{'form.state'}" />
1858: END
1859: }
1860: $r->print(<<END);
1861: <input type="hidden" name="sortby" value="$sortby" />
1862: END
1863:
1864: my %lt=&Apache::lonlocal::texthash(
1865: 'username' => "username",
1866: 'domain' => "domain",
1867: 'id' => 'ID',
1868: 'fullname' => "name",
1869: 'section' => "section",
1870: 'groups' => "active groups",
1871: 'start' => "start date",
1872: 'end' => "end date",
1873: 'status' => "status",
1.2 raeburn 1874: 'role' => "role",
1.1 raeburn 1875: 'type' => "enroll type/action",
1876: 'email' => "email address",
1877: 'clicker' => "clicker id",
1878: 'photo' => "photo",
1.2 raeburn 1879: 'extent' => "extent",
1.11 raeburn 1880: 'pr' => "Proceed",
1881: 'ca' => "check all",
1882: 'ua' => "uncheck all",
1883: 'ac' => "Action to take for selected users",
1.10 raeburn 1884: 'link' => "Behavior of username links",
1885: 'aboutme' => "Display a user's personal page",
1886: 'modify' => "Modify a user's information",
1.1 raeburn 1887: );
1.2 raeburn 1888: if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
1889: $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.13 raeburn 1890: } elsif ($context eq 'author') {
1.2 raeburn 1891: $lt{'extent'} = &mt('Author');
1892: }
1.1 raeburn 1893: my @cols = ('username','domain','id','fullname');
1894: if ($context eq 'course') {
1895: push(@cols,'section');
1896: }
1.2 raeburn 1897: if (!($context eq 'domain' && $env{'form.roletype'} eq 'course')) {
1898: push(@cols,('start','end'));
1899: }
1.3 raeburn 1900: if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
1.2 raeburn 1901: push(@cols,'role');
1902: }
1.13 raeburn 1903: if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.2 raeburn 1904: $env{'form.roletype'} eq 'course')) {
1905: push (@cols,'extent');
1906: }
1907: if (($statusmode eq 'Any') &&
1908: (!($context eq 'domain' && $env{'form.roletype'} eq 'course'))) {
1.1 raeburn 1909: push(@cols,'status');
1910: }
1911: if ($context eq 'course') {
1912: push(@cols,'groups');
1913: }
1914: push(@cols,'email');
1915:
1.4 raeburn 1916: my $rolefilter = $env{'form.showrole'};
1.5 raeburn 1917: if ($env{'form.showrole'} eq 'cr') {
1918: $rolefilter = &mt('custom');
1919: } elsif ($env{'form.showrole'} ne 'Any') {
1.2 raeburn 1920: $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
1921: }
1922: my $results_description = &results_header_row($rolefilter,$statusmode,
1923: $context);
1.3 raeburn 1924: $r->print('<b>'.$results_description.'</b><br />');
1.11 raeburn 1925: my ($output,$actionselect);
1.1 raeburn 1926: if ($mode eq 'html' || $mode eq 'view') {
1.11 raeburn 1927: if ($permission->{'cusr'}) {
1928: $actionselect = &select_actions($context,$setting,$statusmode);
1929: }
1.1 raeburn 1930: $r->print(<<END);
1.10 raeburn 1931: <input type="hidden" name="srchby" value="uname" />
1932: <input type="hidden" name="srchin" value="dom" />
1933: <input type="hidden" name="srchtype" value="exact" />
1934: <input type="hidden" name="srchterm" value="" />
1.11 raeburn 1935: <input type="hidden" name="srchdomain" value="" />
1.1 raeburn 1936: END
1.10 raeburn 1937: if ($mode ne 'autoenroll') {
1938: $output = '<p>';
1939: my @linkdests = ('aboutme');
1940: if ($permission->{'cusr'}) {
1941: push (@linkdests,'modify');
1942: $output .= '<span class="LC_nobreak">'.$lt{'link'}.': ';
1943: my $usernamelink = $env{'form.usernamelink'};
1944: if ($usernamelink eq '') {
1945: $usernamelink = 'aboutme';
1946: }
1947: foreach my $item (@linkdests) {
1948: my $checkedstr = '';
1949: if ($item eq $usernamelink) {
1950: $checkedstr = ' checked="checked" ';
1951: }
1952: $output .= '<label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.'> '.$lt{$item}.'</label> ';
1953: }
1954: $output .= '</span><br />';
1955: } else {
1956: $output .= &mt("Click on a username to view the user's personal page.").'<br />';
1957: }
1.11 raeburn 1958: if ($actionselect) {
1959: $output .= <<"END";
1960: $lt{'ac'}: $actionselect <input type="button" value="$lt{'pr'}" onclick="javascript:verify_action(document.studentform.actionlist)" /></p>
1961: <p><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.actionlist)" />
1962: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.actionlist)" />
1963: END
1964: }
1.4 raeburn 1965: }
1966: $output .= "\n<p>\n".
1.1 raeburn 1967: &Apache::loncommon::start_data_table().
1.4 raeburn 1968: &Apache::loncommon::start_data_table_header_row();
1.1 raeburn 1969: if ($mode eq 'autoenroll') {
1.4 raeburn 1970: $output .= "
1.1 raeburn 1971: <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th>
1.4 raeburn 1972: ";
1.1 raeburn 1973: } else {
1.11 raeburn 1974: $output .= "\n".'<th>'.&mt('Count').'</th>'."\n";
1975: if ($actionselect) {
1976: $output .= '<th>'.&mt('Select').'</th>'."\n";
1977: }
1.1 raeburn 1978: }
1979: foreach my $item (@cols) {
1.4 raeburn 1980: $output .= "<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n";
1.1 raeburn 1981: }
1.2 raeburn 1982: my %role_types = &role_type_names();
1.1 raeburn 1983: if ($context eq 'course') {
1.4 raeburn 1984: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
1985: # Clicker display on or off?
1986: my %clicker_options = &Apache::lonlocal::texthash(
1987: 'on' => 'Show',
1988: 'off' => 'Hide',
1989: );
1990: my $clickerchg = 'on';
1991: if ($displayclickers eq 'on') {
1992: $clickerchg = 'off';
1993: }
1994: $output .= ' <th>'."\n".' '.
1995: '<a href="javascript:document.studentform.displayclickers.value='.
1.1 raeburn 1996: "'".$clickerchg."'".';document.studentform.submit();">'.
1997: $clicker_options{$clickerchg}.'</a> '.$lt{'clicker'}."\n".
1.4 raeburn 1998: ' </th>'."\n";
1.1 raeburn 1999:
1.4 raeburn 2000: # Photo display on or off?
2001: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2002: my %photo_options = &Apache::lonlocal::texthash(
2003: 'on' => 'Show',
2004: 'off' => 'Hide',
2005: );
2006: my $photochg = 'on';
2007: if ($displayphotos eq 'on') {
2008: $photochg = 'off';
2009: }
2010: $output .= ' <th>'."\n".' '.
2011: '<a href="javascript:document.studentform.displayphotos.value='.
1.1 raeburn 2012: "'".$photochg."'".';document.studentform.submit();">'.
2013: $photo_options{$photochg}.'</a> '.$lt{'photo'}."\n".
1.4 raeburn 2014: ' </th>'."\n";
2015: }
1.1 raeburn 2016: }
1.4 raeburn 2017: $output .= &Apache::loncommon::end_data_table_header_row();
2018: }
1.1 raeburn 2019: # Done with the HTML header line
2020: } elsif ($mode eq 'csv') {
2021: #
2022: # Open a file
2023: $CSVfilename = '/prtspool/'.
1.2 raeburn 2024: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
2025: time.'_'.rand(1000000000).'.csv';
1.1 raeburn 2026: unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
2027: $r->log_error("Couldn't open $CSVfilename for output $!");
2028: $r->print("Problems occured in writing the csv file. ".
2029: "This error has been logged. ".
2030: "Please alert your LON-CAPA administrator.");
2031: $CSVfile = undef;
2032: }
2033: #
2034: # Write headers and data to file
1.2 raeburn 2035: print $CSVfile '"'.$results_description.'"'."\n";
1.1 raeburn 2036: print $CSVfile '"'.join('","',map {
2037: &Apache::loncommon::csv_translate($lt{$_})
2038: } (@cols)).'"'."\n";
2039: } elsif ($mode eq 'excel') {
2040: # Create the excel spreadsheet
2041: ($excel_workbook,$excel_filename,$format) =
2042: &Apache::loncommon::create_workbook($r);
2043: return if (! defined($excel_workbook));
2044: $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2 raeburn 2045: $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1 raeburn 2046: #
2047: my @colnames = map {$lt{$_}} (@cols);
2048: $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
2049: }
2050:
2051: # Done with header lines in all formats
2052:
2053: my %index;
2054: my $i;
1.2 raeburn 2055: foreach my $idx (@$keylist) {
2056: $index{$idx} = $i++;
2057: }
1.4 raeburn 2058: my $usercount = 0;
1.2 raeburn 2059: # Get groups, role, permanent e-mail so we can sort on them if
2060: # necessary.
2061: foreach my $user (keys(%{$userlist})) {
1.11 raeburn 2062: if ($context eq 'domain' && $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
2063: delete($userlist->{$user});
2064: next;
2065: }
1.2 raeburn 2066: my ($uname,$udom,$role,$groups,$email);
1.5 raeburn 2067: if (($statusmode ne 'Any') &&
2068: ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
2069: delete($userlist->{$user});
2070: next;
2071: }
1.2 raeburn 2072: if ($context eq 'domain') {
2073: if ($env{'form.roletype'} eq 'domain') {
2074: ($role,$uname,$udom) = split(/:/,$user);
1.11 raeburn 2075: if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
2076: ($udom eq $env{'request.role.domain'})) {
2077: delete($userlist->{$user});
2078: next;
2079: }
1.13 raeburn 2080: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 2081: ($uname,$udom,$role) = split(/:/,$user,-1);
2082: } elsif ($env{'form.roletype'} eq 'course') {
2083: ($uname,$udom,$role) = split(/:/,$user);
2084: }
2085: } else {
2086: ($uname,$udom,$role) = split(/:/,$user,-1);
2087: if (($context eq 'course') && $role eq '') {
2088: $role = 'st';
2089: }
2090: }
2091: $userlist->{$user}->[$index{'role'}] = $role;
2092: if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'} eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
2093: delete($userlist->{$user});
2094: next;
2095: }
2096: if (ref($classgroups) eq 'HASH') {
2097: $groups = $classgroups->{$user};
2098: }
2099: if (ref($groups->{active}) eq 'HASH') {
2100: $userlist->{$user}->[$index{'groups'}] = join(', ',keys(%{$groups->{'active'}}));
2101: }
2102: my %emails = &Apache::loncommon::getemails($uname,$udom);
2103: if ($emails{'permanentemail'} =~ /\S/) {
2104: $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
2105: }
1.4 raeburn 2106: $usercount ++;
2107: }
2108: my $autocount = 0;
2109: my $manualcount = 0;
2110: my $lockcount = 0;
2111: my $unlockcount = 0;
2112: if ($usercount) {
2113: $r->print($output);
2114: } else {
2115: if ($mode eq 'autoenroll') {
2116: return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
2117: } else {
2118: return;
2119: }
1.1 raeburn 2120: }
1.2 raeburn 2121: #
2122: # Sort the users
1.1 raeburn 2123: my $index = $index{$sortby};
2124: my $second = $index{'username'};
2125: my $third = $index{'domain'};
1.2 raeburn 2126: my @sorted_users = sort {
2127: lc($userlist->{$a}->[$index]) cmp lc($userlist->{$b}->[$index])
1.1 raeburn 2128: ||
1.2 raeburn 2129: lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
2130: lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
2131: } (keys(%$userlist));
1.4 raeburn 2132: my $rowcount = 0;
1.2 raeburn 2133: foreach my $user (@sorted_users) {
1.4 raeburn 2134: my %in;
1.2 raeburn 2135: my $sdata = $userlist->{$user};
1.4 raeburn 2136: $rowcount ++;
1.2 raeburn 2137: foreach my $item (@{$keylist}) {
2138: $in{$item} = $sdata->[$index{$item}];
2139: }
1.11 raeburn 2140: my $role = $in{'role'};
1.2 raeburn 2141: $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}]);
2142: if (! defined($in{'start'}) || $in{'start'} == 0) {
2143: $in{'start'} = &mt('none');
2144: } else {
2145: $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
1.1 raeburn 2146: }
1.2 raeburn 2147: if (! defined($in{'end'}) || $in{'end'} == 0) {
2148: $in{'end'} = &mt('none');
1.1 raeburn 2149: } else {
1.2 raeburn 2150: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1 raeburn 2151: }
1.2 raeburn 2152: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
2153: $r->print(&Apache::loncommon::start_data_table_row());
1.4 raeburn 2154: $r->print("<td>$rowcount</td>\n");
1.11 raeburn 2155: my $checkval;
2156: if ($mode ne 'autoenroll' && $actionselect) {
2157: $checkval = $user;
2158: if ($context eq 'course') {
2159: if ($role eq 'st') {
2160: $checkval .= ':st';
2161: }
2162: $checkval .= ':'.$in{'section'};
2163: if ($role eq 'st') {
2164: $checkval .= ':'.$in{'type'}.':'.$in{'lockedtype'};
2165: }
2166: }
2167: $r->print('<td><input type="checkbox" name="actionlist" value="'.
2168: $checkval.'"></td>');
2169: }
1.2 raeburn 2170: foreach my $item (@cols) {
1.10 raeburn 2171: if ($item eq 'username') {
2172: $r->print('<td>'.&print_username_link($permission,\%in).'</td>');
1.11 raeburn 2173: } elsif (($item eq 'start' || $item eq 'end') && ($mode ne 'autoeroll') && ($actionselect)) {
2174: $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.10 raeburn 2175: } else {
2176: $r->print('<td>'.$in{$item}.'</td>'."\n");
2177: }
1.2 raeburn 2178: }
2179: if ($context eq 'course') {
1.4 raeburn 2180: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
2181: if ($displayclickers eq 'on') {
2182: my $clickers =
1.2 raeburn 2183: (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4 raeburn 2184: if ($clickers!~/\w/) { $clickers='-'; }
2185: $r->print('<td>'.$clickers.'</td>');
1.2 raeburn 2186: } else {
2187: $r->print(' <td> </td> ');
2188: }
1.4 raeburn 2189: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2190: if ($displayphotos eq 'on' && $sdata->[$index{'role'}] eq 'st') {
2191: my $imgurl =
2192: &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'},
2193: 'gif','thumbnail');
2194: $r->print(' <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>');
2195: } else {
2196: $r->print(' <td> </td> ');
2197: }
2198: }
1.2 raeburn 2199: }
2200: }
2201: $r->print(&Apache::loncommon::end_data_table_row());
2202: } elsif ($mode eq 'csv') {
2203: next if (! defined($CSVfile));
2204: # no need to bother with $linkto
2205: if (! defined($in{'start'}) || $in{'start'} == 0) {
2206: $in{'start'} = &mt('none');
2207: } else {
2208: $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
2209: }
2210: if (! defined($in{'end'}) || $in{'end'} == 0) {
2211: $in{'end'} = &mt('none');
2212: } else {
2213: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
2214: }
2215: my @line = ();
2216: foreach my $item (@cols) {
2217: push @line,&Apache::loncommon::csv_translate($in{$item});
2218: }
2219: print $CSVfile '"'.join('","',@line).'"'."\n";
2220: } elsif ($mode eq 'excel') {
2221: my $col = 0;
2222: foreach my $item (@cols) {
2223: if ($item eq 'start' || $item eq 'end') {
2224: if (defined($item) && $item != 0) {
2225: $excel_sheet->write($row,$col++,
2226: &Apache::lonstathelpers::calc_serial($in{item}),
2227: $format->{'date'});
2228: } else {
2229: $excel_sheet->write($row,$col++,'none');
2230: }
2231: } else {
2232: $excel_sheet->write($row,$col++,$in{$item});
2233: }
2234: }
2235: $row++;
1.1 raeburn 2236: }
2237: }
1.2 raeburn 2238: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
2239: $r->print(&Apache::loncommon::end_data_table().'<br />');
2240: } elsif ($mode eq 'excel') {
2241: $excel_workbook->close();
2242: $r->print('<p><a href="'.$excel_filename.'">'.
2243: &mt('Your Excel spreadsheet').'</a> '.&mt('is ready for download').'.</p>'."\n");
2244: } elsif ($mode eq 'csv') {
2245: close($CSVfile);
2246: $r->print('<a href="'.$CSVfilename.'">'.
2247: &mt('Your CSV file').'</a> is ready for download.'.
2248: "\n");
2249: $r->rflush();
2250: }
2251: if ($mode eq 'autoenroll') {
2252: return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4 raeburn 2253: } else {
2254: return ($usercount);
1.2 raeburn 2255: }
1.1 raeburn 2256: }
2257:
1.10 raeburn 2258: sub print_username_link {
2259: my ($permission,$in) = @_;
2260: my $output;
2261: if (!$permission->{'cusr'}) {
2262: $output = &Apache::loncommon::aboutmewrapper($in->{'username'},
2263: $in->{'username'},
2264: $in->{'domain'});
2265: } else {
2266: $output = '<a href="javascript:username_display_launch('.
2267: "'$in->{'username'}','$in->{'domain'}'".')" />'.
2268: $in->{'username'}.'</a>';
2269: }
2270: return $output;
2271: }
2272:
1.2 raeburn 2273: sub role_type_names {
2274: my %lt = &Apache::lonlocal::texthash (
1.13 raeburn 2275: 'domain' => 'Domain Roles',
2276: 'author' => 'Co-Author Roles',
2277: 'course' => 'Course Roles',
1.2 raeburn 2278: );
2279: return %lt;
2280: }
2281:
1.11 raeburn 2282: sub select_actions {
2283: my ($context,$setting,$statusmode) = @_;
2284: my %lt = &Apache::lonlocal::texthash(
2285: revoke => "Revoke user roles",
2286: delete => "Delete user roles",
2287: reenable => "Re-enable expired user roles",
2288: activate => "Make future user roles active now",
2289: chgdates => "Change starting/ending dates",
2290: chgsec => "Change section associated with user roles",
2291: );
2292: my ($output,$options,%choices);
2293: if ($statusmode eq 'Any') {
2294: $options .= '
2295: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
2296: $choices{'dates'} = 1;
2297: } else {
2298: if ($statusmode eq 'Future') {
2299: $options .= '
2300: <option value="activate">'.$lt{'activate'}.'</option>';
2301: $choices{'dates'} = 1;
2302: } elsif ($statusmode eq 'Expired') {
2303: $options .= '
2304: <option value="reenable">'.$lt{'reenable'}.'</option>';
2305: $choices{'dates'} = 1;
2306: }
1.13 raeburn 2307: if ($statusmode eq 'Active' || $statusmode eq 'Future') {
2308: $options .= '
2309: <option value="chgdates">'.$lt{'chgdates'}.'</option>
2310: <option value="revoke">'.$lt{'revoke'}.'</option>';
2311: $choices{'dates'} = 1;
2312: }
1.11 raeburn 2313: }
2314: if ($context eq 'domain') {
2315: $options .= '
2316: <option value="delete">'.$lt{'delete'}.'</option>';
2317: }
2318: if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
2319: if ($statusmode ne 'Expired') {
2320: $options .= '
2321: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
2322: $choices{'sections'} = 1;
2323: }
2324: }
2325: if ($options) {
2326: $output = '<select name="bulkaction" onchange="javascript:opendatebrowser(this.form,'."'studentform'".')" />'."\n".
2327: '<option value="" selected="selected">'.
2328: &mt('Please select').'</option>'."\n".$options."\n".'</select>';
2329: if ($choices{'dates'}) {
2330: $output .=
2331: '<input type="hidden" name="startdate_month" value="" />'."\n".
2332: '<input type="hidden" name="startdate_day" value="" />'."\n".
2333: '<input type="hidden" name="startdate_year" value="" />'."\n".
2334: '<input type="hidden" name="startdate_hour" value="" />'."\n".
2335: '<input type="hidden" name="startdate_minute" value="" />'."\n".
2336: '<input type="hidden" name="startdate_second" value="" />'."\n".
2337: '<input type="hidden" name="enddate_month" value="" />'."\n".
2338: '<input type="hidden" name="enddate_day" value="" />'."\n".
2339: '<input type="hidden" name="enddate_year" value="" />'."\n".
2340: '<input type="hidden" name="enddate_hour" value="" />'."\n".
2341: '<input type="hidden" name="enddate_minute" value="" />'."\n".
2342: '<input type="hidden" name="enddate_second" value="" />'."\n";
2343: if ($context eq 'course') {
2344: $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
2345: }
2346: }
2347: if ($choices{'sections'}) {
2348: $output .= '<input type="hidden" name="retainsec" value= "" />'."\n".
2349: '<input type="hidden" name="newsecs" value= "" />'."\n";
2350: }
2351: }
2352: return $output;
2353: }
2354:
2355: sub date_section_javascript {
2356: my ($context,$setting) = @_;
2357: my $title;
2358: if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
2359: $title = &mt('Date and Section selector');
2360: } else {
2361: $title = &mt('Date selector');
2362: }
2363: my $output = '
2364: <script type="text/javascript">
2365: var stdeditbrowser;'."\n";
2366: $output .= <<"ENDONE";
2367: function opendatebrowser(callingform,formname) {
2368: var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
2369: if (bulkaction == 'revoke' || bulkaction == 'delete' || bulkaction == '') {
2370: return;
2371: }
2372: var url = '/adm/createuser?';
2373: var type = '';
2374: var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
2375: ENDONE
2376: if ($context eq 'domain') {
2377: $output .= '
2378: type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
2379: ';
2380: }
2381: my $width= '700';
2382: my $height = '400';
2383: $output .= <<"ENDTWO";
2384: url += 'action=dateselect&callingform=' + formname +
2385: '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
2386: var title = '$title';
2387: var options = 'scrollbars=1,resizable=1,menubar=0';
2388: options += ',width=$width,height=$height';
2389: stdeditbrowser = open(url,title,options,'1');
2390: stdeditbrowser.focus();
2391: }
2392: </script>
2393: ENDTWO
2394: return $output;
2395: }
2396:
2397: sub date_section_selector {
2398: my ($context) = @_;
2399: my $callingform = $env{'form.callingform'};
2400: my $formname = 'dateselect';
2401: my $groupslist = &get_groupslist();
2402: my $sec_js = &setsections_javascript($formname,$groupslist);
2403: my $output = <<"END";
2404: <script type="text/javascript">
2405:
2406: $sec_js
2407:
2408: function saveselections(formname) {
2409:
2410: END
2411: if ($env{'form.bulkaction'} eq 'chgsec') {
2412: $output .= <<"END";
2413: opener.document.$callingform.retainsec.value = formname.retainsec.value;
2414: setSections(formname);
2415: if (seccheck == 'ok') {
2416: opener.document.$callingform.newsecs.value = formname.sections.value;
2417: window.close();
2418: }
2419: return;
2420: END
2421: } else {
2422: if ($context eq 'course') {
2423: if (($env{'form.bulkaction'} eq 'reenable') ||
2424: ($env{'form.bulkaction'} eq 'activate') ||
2425: ($env{'form.bulkaction'} eq 'chgdates')) {
2426: $output .= <<"END";
2427:
2428: if (formname.makedatesdefault.checked == true) {
2429: opener.document.$callingform.makedatesdefault.value = 1;
2430: }
2431: else {
2432: opener.document.$callingform.makedatesdefault.value = 0;
2433: }
2434:
2435: END
2436: }
2437: }
2438: $output .= <<"END";
2439: opener.document.$callingform.startdate_month.value = formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
2440: opener.document.$callingform.startdate_day.value = formname.startdate_day.value;
2441: opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
2442: opener.document.$callingform.startdate_hour.value = formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
2443: opener.document.$callingform.startdate_minute.value = formname.startdate_minute.value;
2444: opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
2445: opener.document.$callingform.enddate_month.value = formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
2446: opener.document.$callingform.enddate_day.value = formname.enddate_day.value;
2447: opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
2448: opener.document.$callingform.enddate_hour.value = formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
2449: opener.document.$callingform.enddate_minute.value = formname.enddate_minute.value;
2450: opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
2451: window.close();
2452: END
2453: }
2454: $output .= '
2455: }
2456: </script>
2457: ';
2458: my %lt = &Apache::lonlocal::texthash (
2459: chac => 'Access dates to apply for selected users',
2460: chse => 'Changes in section affiliation to apply to selected users',
2461: fors => 'For student roles changing the section, will result in a section switch as students may only be in one section of a course at a time.',
2462: forn => 'For a role in a course that is not a student role, a user may have roles in more than one section of a course at a time.',
2463: reta => "Retain each user's current section affiliations?",
2464: dnap => '(Does not apply to student roles).',
2465: );
2466: my ($date_items,$headertext);
2467: if ($env{'form.bulkaction'} eq 'chgsec') {
2468: $headertext = $lt{'chse'};
2469: } else {
2470: $headertext = $lt{'chac'};
2471: my $starttime;
2472: if (($env{'form.bulkaction'} eq 'activate') ||
2473: ($env{'form.bulkaction'} eq 'reenable')) {
2474: $starttime = time;
2475: }
2476: $date_items = &date_setting_table($starttime,undef,$context,
2477: $env{'form.bulkaction'});
2478: }
2479: $output .= '<h3>'.$headertext.'</h3>'.
2480: '<form name="'.$formname.'" method="post">'."\n".
2481: $date_items;
2482: if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
2483: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
2484: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
2485: my %sections_count =
2486: &Apache::loncommon::get_sections($cdom,$cnum);
2487: my $info;
2488: if ($env{'form.showrole'} eq 'st') {
2489: $output .= '<p>'.$lt{'fors'}.'</p>';
2490: } elsif ($env{'form.shorole'} eq 'Any') {
2491: $output .= '<p>'.$lt{'fors'}.'</p>'.
2492: '<p>'.$lt{'forn'}.' ';
2493: $info = $lt{'reta'};
2494: } else {
2495: $output .= '<p>'.$lt{'forn'}.' ';
2496: $info = $lt{'reta'};
2497: }
2498: if ($info) {
2499: $info .= '<span class="LC_nobreak">'.
2500: '<label><input type="radio" name="retainsec" value="1" '.
2501: 'checked="checked" />'.&mt('Yes').'</label> '.
2502: '<label><input type="radio" name="retainsec" value="0" />'.
2503: &mt('No').'</label></span>';
2504: if ($env{'form.showrole'} eq 'Any') {
2505: $info .= '<br />'.$lt{'dnap'};
2506: }
2507: $info .= '</p>';
2508: } else {
2509: $info = '<input type="hidden" name="retainsec" value="0" />';
2510: }
2511: my $sections_select .= &course_sections(\%sections_count,$env{'form.showrole'});
2512: my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
2513: &Apache::lonhtmlcommon::row_title(&mt('New section to assign'),'LC_oddrow_value')."\n".
2514: '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
2515: '<td align="center">'.&mt('Existing sections')."\n".
2516: '<br />'.$sections_select.'</td><td align="center">'.
2517: &mt('New section').'<br />'."\n".
2518: '<input type="text" name="newsec" size="15" />'."\n".
2519: '<input type="hidden" name="sections" value="" />'."\n".
2520: '</td></tr></table>'."\n".
2521: &Apache::lonhtmlcommon::row_closure(1)."\n".
2522: &Apache::lonhtmlcommon::end_pick_box().'</p>';
2523: $output .= $info.$secbox;
2524: }
2525: $output .= '<p>'.
2526: &mt('Use "Save" to update the main window with your selections.').'<br /><br />'.
2527: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
2528: '</form>';
2529: return $output;
2530: }
2531:
1.2 raeburn 2532: sub results_header_row {
2533: my ($rolefilter,$statusmode,$context) = @_;
1.5 raeburn 2534: my ($description,$showfilter);
2535: if ($rolefilter ne 'Any') {
2536: $showfilter = $rolefilter;
2537: }
1.2 raeburn 2538: if ($context eq 'course') {
1.3 raeburn 2539: $description = &mt('Course - ').$env{'course.'.$env{'request.course.id'}.'.description'}.': ';
1.2 raeburn 2540: if ($statusmode eq 'Expired') {
1.5 raeburn 2541: $description .= &mt('Users in course with expired [_1] roles',$showfilter);
1.11 raeburn 2542: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2543: $description .= &mt('Users in course with future [_1] roles',$showfilter);
1.2 raeburn 2544: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2545: $description .= &mt('Users in course with active [_1] roles',$showfilter);
1.2 raeburn 2546: } else {
2547: if ($rolefilter eq 'Any') {
2548: $description .= &mt('All users in course');
2549: } else {
2550: $description .= &mt('All users in course with [_1] roles',$rolefilter);
2551: }
2552: }
1.13 raeburn 2553: } elsif ($context eq 'author') {
1.14 ! raeburn 2554: $description =
! 2555: &mt('Author space for <span class="LC_cusr_emph">[_1]</span>',
! 2556: &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})).': ';
1.2 raeburn 2557: if ($statusmode eq 'Expired') {
1.5 raeburn 2558: $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2 raeburn 2559: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2560: $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2 raeburn 2561: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2562: $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2 raeburn 2563: } else {
2564: if ($rolefilter eq 'Any') {
1.5 raeburn 2565: $description .= &mt('All co-authors');
1.2 raeburn 2566: } else {
2567: $description .= &mt('All co-authors with [_1] roles',$rolefilter);
2568: }
2569: }
2570: } elsif ($context eq 'domain') {
2571: my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
2572: $description = &mt('Domain - ').$domdesc.': ';
2573: if ($env{'form.roletype'} eq 'domain') {
2574: if ($statusmode eq 'Expired') {
1.5 raeburn 2575: $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2 raeburn 2576: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2577: $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2 raeburn 2578: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2579: $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2 raeburn 2580: } else {
2581: if ($rolefilter eq 'Any') {
1.5 raeburn 2582: $description .= &mt('All users in domain');
1.2 raeburn 2583: } else {
2584: $description .= &mt('All users in domain with [_1] roles',$rolefilter);
2585: }
2586: }
1.13 raeburn 2587: } elsif ($env{'form.roletype'} eq 'author') {
1.2 raeburn 2588: if ($statusmode eq 'Expired') {
1.5 raeburn 2589: $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2 raeburn 2590: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2591: $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2 raeburn 2592: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2593: $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2 raeburn 2594: } else {
2595: if ($rolefilter eq 'Any') {
1.5 raeburn 2596: $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2 raeburn 2597: } else {
2598: $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
2599: }
2600: }
2601: } elsif ($env{'form.roletype'} eq 'course') {
2602: my $coursefilter = $env{'form.coursepick'};
2603: if ($coursefilter eq 'category') {
2604: my $instcode = &instcode_from_coursefilter();
2605: if ($instcode eq '.') {
2606: $description .= &mt('All courses in domain').' - ';
2607: } else {
2608: $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
2609: }
2610: } elsif ($coursefilter eq 'selected') {
2611: $description .= &mt('Selected courses in domain').' - ';
2612: } elsif ($coursefilter eq 'all') {
2613: $description .= &mt('All courses in domain').' - ';
2614: }
2615: if ($statusmode eq 'Expired') {
1.5 raeburn 2616: $description .= &mt('users with expired [_1] roles',$showfilter);
1.2 raeburn 2617: } elsif ($statusmode eq 'Future') {
1.5 raeburn 2618: $description .= &mt('users with future [_1] roles',$showfilter);
1.2 raeburn 2619: } elsif ($statusmode eq 'Active') {
1.5 raeburn 2620: $description .= &mt('users with active [_1] roles',$showfilter);
1.2 raeburn 2621: } else {
2622: if ($rolefilter eq 'Any') {
2623: $description .= &mt('all users');
2624: } else {
2625: $description .= &mt('users with [_1] roles',$rolefilter);
2626: }
2627: }
2628: }
2629: }
2630: return $description;
2631: }
2632:
1.1 raeburn 2633: #################################################
2634: #################################################
2635: sub show_drop_list {
2636: my ($r,$classlist,$keylist,$nosort)=@_;
2637: my $cid=$env{'request.course.id'};
2638: if (! exists($env{'form.sortby'})) {
2639: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2640: ['sortby']);
2641: }
2642: my $sortby = $env{'form.sortby'};
2643: if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
2644: $sortby = 'username';
2645: }
2646: my $cdom = $env{'course.'.$cid.'.domain'};
2647: my $cnum = $env{'course.'.$cid,'.num'};
2648: my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
2649: $classlist,$keylist,$cdom,$cnum);
2650: #
2651: my $action = "drop";
2652: $r->print(<<END);
2653: <input type="hidden" name="sortby" value="$sortby" />
2654: <input type="hidden" name="action" value="$action" />
2655: <input type="hidden" name="state" value="done" />
2656: <script>
2657: function checkAll(field) {
2658: for (i = 0; i < field.length; i++)
2659: field[i].checked = true ;
2660: }
2661:
2662: function uncheckAll(field) {
2663: for (i = 0; i < field.length; i++)
2664: field[i].checked = false ;
2665: }
2666: </script>
2667: <p>
2668: <input type="hidden" name="phase" value="four">
2669: END
2670:
2671: my %lt=&Apache::lonlocal::texthash('usrn' => "username",
2672: 'dom' => "domain",
2673: 'sn' => "student name",
2674: 'sec' => "section",
2675: 'start' => "start date",
2676: 'end' => "end date",
2677: 'groups' => "active groups",
2678: );
2679: if ($nosort) {
2680: $r->print(&Apache::loncommon::start_data_table());
2681: $r->print(<<END);
2682: <tr>
2683: <th> </th>
2684: <th>$lt{'usrn'}</th>
2685: <th>$lt{'dom'}</th>
2686: <th>ID</th>
2687: <th>$lt{'sn'}</th>
2688: <th>$lt{'sec'}</th>
2689: <th>$lt{'start'}</th>
2690: <th>$lt{'end'}</th>
2691: <th>$lt{'groups'}</th>
2692: </tr>
2693: END
2694:
2695: } else {
2696: $r->print(&Apache::loncommon::start_data_table());
2697: $r->print(<<END);
2698: <tr><th> </th>
2699: <th>
2700: <a href="/adm/dropadd?action=$action&sortby=username">$lt{'usrn'}</a>
2701: </th><th>
2702: <a href="/adm/dropadd?action=$action&sortby=domain">$lt{'dom'}</a>
2703: </th><th>
2704: <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
2705: </th><th>
2706: <a href="/adm/dropadd?action=$action&sortby=fullname">$lt{'sn'}</a>
2707: </th><th>
2708: <a href="/adm/dropadd?action=$action&sortby=section">$lt{'sec'}</a>
2709: </th><th>
2710: <a href="/adm/dropadd?action=$action&sortby=start">$lt{'start'}</a>
2711: </th><th>
2712: <a href="/adm/dropadd?action=$action&sortby=end">$lt{'end'}</a>
2713: </th><th>
2714: <a href="/adm/dropadd?action=$action&sortby=groups">$lt{'groups'}</a>
2715: </th>
2716: </tr>
2717: END
2718: }
2719: #
2720: # Sort the students
2721: my %index;
2722: my $i;
2723: foreach (@$keylist) {
2724: $index{$_} = $i++;
2725: }
2726: $index{'groups'} = scalar(@$keylist);
2727: my $index = $index{$sortby};
2728: my $second = $index{'username'};
2729: my $third = $index{'domain'};
2730: my @Sorted_Students = sort {
2731: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
2732: ||
2733: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
2734: ||
2735: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
2736: } (keys(%$classlist));
2737: foreach my $student (@Sorted_Students) {
2738: my $error;
2739: my $sdata = $classlist->{$student};
2740: my $username = $sdata->[$index{'username'}];
2741: my $domain = $sdata->[$index{'domain'}];
2742: my $section = $sdata->[$index{'section'}];
2743: my $name = $sdata->[$index{'fullname'}];
2744: my $id = $sdata->[$index{'id'}];
2745: my $start = $sdata->[$index{'start'}];
2746: my $end = $sdata->[$index{'end'}];
2747: my $groups = $classgroups->{$student};
2748: my $active_groups;
2749: if (ref($groups->{active}) eq 'HASH') {
2750: $active_groups = join(', ',keys(%{$groups->{'active'}}));
2751: }
2752: if (! defined($start) || $start == 0) {
2753: $start = &mt('none');
2754: } else {
2755: $start = &Apache::lonlocal::locallocaltime($start);
2756: }
2757: if (! defined($end) || $end == 0) {
2758: $end = &mt('none');
2759: } else {
2760: $end = &Apache::lonlocal::locallocaltime($end);
2761: }
2762: my $status = $sdata->[$index{'status'}];
2763: next if ($status ne 'Active');
2764: #
2765: $r->print(&Apache::loncommon::start_data_table_row());
2766: $r->print(<<"END");
2767: <td><input type="checkbox" name="droplist" value="$student"></td>
2768: <td>$username</td>
2769: <td>$domain</td>
2770: <td>$id</td>
2771: <td>$name</td>
2772: <td>$section</td>
2773: <td>$start</td>
2774: <td>$end</td>
2775: <td>$active_groups</td>
2776: END
2777: $r->print(&Apache::loncommon::end_data_table_row());
2778: }
2779: $r->print(&Apache::loncommon::end_data_table().'<br />');
2780: %lt=&Apache::lonlocal::texthash(
2781: 'dp' => "Expire Users' Roles",
2782: 'ca' => "check all",
2783: 'ua' => "uncheck all",
2784: );
2785: $r->print(<<"END");
2786: </p><p>
2787: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)">
2788: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)">
2789: <p><input type=submit value="$lt{'dp'}"></p>
2790: END
2791: return;
2792: }
2793:
2794: #
2795: # Print out the initial form to get the file containing a list of users
2796: #
2797: sub print_first_users_upload_form {
2798: my ($r,$context) = @_;
2799: my $str;
2800: $str = '<input type="hidden" name="phase" value="two">';
2801: $str .= '<input type="hidden" name="action" value="upload" />';
2802: $str .= '<input type="hidden" name="state" value="got_file" />';
1.2 raeburn 2803: $str .= "<h3>".&mt('Upload a file containing information about users')."</h3>\n";
1.1 raeburn 2804: $str .= &Apache::loncommon::upfile_select_html();
2805: $str .= "<p>\n";
2806: $str .= '<input type="submit" name="fileupload" value="'.
1.2 raeburn 2807: &mt('Upload file of users').'">'."\n";
1.1 raeburn 2808: $str .= '<label><input type="checkbox" name="noFirstLine" /> '.
2809: &mt('Ignore First Line')."</label></p>\n";
2810: $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
2811: &mt("How do I create a users list from a spreadsheet")).
2812: "<br />\n";
2813: $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
2814: &mt("How do I create a CSV file from a spreadsheet")).
2815: "<br />\n";
2816: $str .= &Apache::loncommon::end_page();
2817: $r->print($str);
2818: return;
2819: }
2820:
2821: # ================================================= Drop/Add from uploaded file
2822: sub upfile_drop_add {
2823: my ($r,$context) = @_;
2824: &Apache::loncommon::load_tmp_file($r);
2825: my @userdata=&Apache::loncommon::upfile_record_sep();
2826: if($env{'form.noFirstLine'}){shift(@userdata);}
2827: my @keyfields = split(/\,/,$env{'form.keyfields'});
2828: my %fields=();
2829: for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
2830: if ($env{'form.upfile_associate'} eq 'reverse') {
2831: if ($env{'form.f'.$i} ne 'none') {
2832: $fields{$keyfields[$i]}=$env{'form.f'.$i};
2833: }
2834: } else {
2835: $fields{$env{'form.f'.$i}}=$keyfields[$i];
2836: }
2837: }
2838: #
2839: # Store the field choices away
2840: foreach my $field (qw/username names
2841: fname mname lname gen id sec ipwd email role/) {
2842: $env{'form.'.$field.'_choice'}=$fields{$field};
2843: }
2844: &Apache::loncommon::store_course_settings('enrollment_upload',
2845: { 'username_choice' => 'scalar',
2846: 'names_choice' => 'scalar',
2847: 'fname_choice' => 'scalar',
2848: 'mname_choice' => 'scalar',
2849: 'lname_choice' => 'scalar',
2850: 'gen_choice' => 'scalar',
2851: 'id_choice' => 'scalar',
2852: 'sec_choice' => 'scalar',
2853: 'ipwd_choice' => 'scalar',
2854: 'email_choice' => 'scalar',
2855: 'role_choice' => 'scalar' });
2856: #
2857: my ($startdate,$enddate) = &get_dates_from_form();
2858: if ($env{'form.makedatesdefault'}) {
2859: $r->print(&make_dates_default($startdate,$enddate));
2860: }
2861: # Determine domain and desired host (home server)
2862: my $domain=$env{'request.role.domain'};
2863: my $desiredhost = $env{'form.lcserver'};
2864: if (lc($desiredhost) eq 'default') {
2865: $desiredhost = undef;
2866: } else {
2867: my %home_servers = &Apache::lonnet::get_servers($domain,'library');
2868: if (! exists($home_servers{$desiredhost})) {
2869: $r->print('<span class="LC_error">'.&mt('Error').
2870: &mt('Invalid home server specified').'</span>');
2871: $r->print(&Apache::loncommon::end_page());
2872: return;
2873: }
2874: }
2875: # Determine authentication mechanism
2876: my $changeauth;
2877: if ($context eq 'domain') {
2878: $changeauth = $env{'form.changeauth'};
2879: }
2880: my $amode = '';
2881: my $genpwd = '';
2882: if ($env{'form.login'} eq 'krb') {
2883: $amode='krb';
2884: $amode.=$env{'form.krbver'};
2885: $genpwd=$env{'form.krbarg'};
2886: } elsif ($env{'form.login'} eq 'int') {
2887: $amode='internal';
2888: if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
2889: $genpwd=$env{'form.intarg'};
2890: }
2891: } elsif ($env{'form.login'} eq 'loc') {
2892: $amode='localauth';
2893: if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
2894: $genpwd=$env{'form.locarg'};
2895: }
2896: }
2897: if ($amode =~ /^krb/) {
2898: if (! defined($genpwd) || $genpwd eq '') {
2899: $r->print('<span class="Error">'.
2900: &mt('Unable to enroll users').' '.
2901: &mt('No Kerberos domain was specified.').'</span></p>');
2902: $amode = ''; # This causes the loop below to be skipped
2903: }
2904: }
2905: my ($cid,$defaultsec,$defaultrole,$setting);
2906: if ($context eq 'domain') {
2907: $setting = $env{'form.roleaction'};
2908: if ($setting eq 'domain') {
2909: $defaultrole = $env{'form.defaultrole'};
2910: } elsif ($setting eq 'course') {
2911: $defaultrole = $env{'form.courserole'};
2912: }
1.13 raeburn 2913: } elsif ($context eq 'author') {
1.1 raeburn 2914: $defaultrole = $env{'form.defaultrole'};
2915: }
2916: if ($context eq 'domain' && $setting eq 'course') {
2917: if ($env{'form.newsec'} ne '') {
2918: $defaultsec = $env{'form.newsec'};
2919: } elsif ($env{'form.defaultsec'} ne '') {
2920: $defaultsec = $env{'form.defaultsec'}
2921: }
2922: }
2923: if ($env{'request.course.id'} ne '') {
2924: $cid = $env{'request.course.id'};
2925: } elsif ($env{'form.defaultdomain'} ne '' && $env{'form.defaultcourse'} ne '') {
2926: $cid = $env{'form.defaultdomain'}.'_'.
2927: $env{'form.defaultcourse'};
2928: }
2929: if ( $domain eq &LONCAPA::clean_domain($domain)
2930: && ($amode ne '')) {
2931: #######################################
2932: ## Add/Modify Users ##
2933: #######################################
2934: if ($context eq 'course') {
2935: $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13 raeburn 2936: } elsif ($context eq 'author') {
1.1 raeburn 2937: $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
2938: } else {
2939: $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
2940: }
2941: my %counts = (
2942: user => 0,
2943: auth => 0,
2944: role => 0,
2945: );
2946: my $flushc=0;
2947: my %student=();
2948: my %curr_groups;
2949: my %userchg;
2950: if ($context eq 'course') {
2951: # Get information about course groups
2952: %curr_groups = &Apache::longroup::coursegroups();
2953: }
1.5 raeburn 2954: my (%curr_rules,%got_rules,%alerts);
1.1 raeburn 2955: # Get new users list
2956: foreach (@userdata) {
2957: my %entries=&Apache::loncommon::record_sep($_);
2958: # Determine user name
2959: unless (($entries{$fields{'username'}} eq '') ||
2960: (!defined($entries{$fields{'username'}}))) {
2961: my ($fname, $mname, $lname,$gen) = ('','','','');
2962: if (defined($fields{'names'})) {
2963: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
2964: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
2965: } else {
2966: if (defined($fields{'fname'})) {
2967: $fname=$entries{$fields{'fname'}};
2968: }
2969: if (defined($fields{'mname'})) {
2970: $mname=$entries{$fields{'mname'}};
2971: }
2972: if (defined($fields{'lname'})) {
2973: $lname=$entries{$fields{'lname'}};
2974: }
2975: if (defined($fields{'gen'})) {
2976: $gen=$entries{$fields{'gen'}};
2977: }
2978: }
2979: if ($entries{$fields{'username'}}
2980: ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
2981: $r->print('<br />'.
2982: &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
2983: $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
2984: '</b>');
2985: } else {
1.5 raeburn 2986: my $username = $entries{$fields{'username'}};
1.1 raeburn 2987: my $sec;
2988: if ($context eq 'course' || $setting eq 'course') {
2989: # determine section number
2990: if (defined($fields{'sec'})) {
2991: if (defined($entries{$fields{'sec'}})) {
2992: $sec=$entries{$fields{'sec'}};
2993: }
2994: } else {
2995: $sec = $defaultsec;
2996: }
2997: # remove non alphanumeric values from section
2998: $sec =~ s/\W//g;
2999: if ($sec eq "none" || $sec eq 'all') {
3000: $r->print('<br />'.
3001: &mt('<b>[_1]</b>: Unable to enroll: section name "[_2]" for user [_3] [_4] [_5] [_6] is a reserved word.',
3002: $username,$sec,$fname,$mname,$lname,$gen));
3003: next;
3004: } elsif (($sec ne '') && (exists($curr_groups{$sec}))) {
3005: $r->print('<br />'.
3006: &mt('<b>[_1]</b>: Unable to enroll: section name "[_2]" for user [_3] [_4] [_5] [_6] is a course group. Section names and group names must be distinct.',
3007: $username,$sec,$fname,$mname,$lname,$gen));
3008: next;
3009: }
3010: }
3011: # determine id number
3012: my $id='';
3013: if (defined($fields{'id'})) {
3014: if (defined($entries{$fields{'id'}})) {
3015: $id=$entries{$fields{'id'}};
3016: }
3017: $id=~tr/A-Z/a-z/;
3018: }
3019: # determine email address
3020: my $email='';
3021: if (defined($fields{'email'})) {
3022: if (defined($entries{$fields{'email'}})) {
3023: $email=$entries{$fields{'email'}};
3024: unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; } }
3025: }
3026: # determine user password
3027: my $password = $genpwd;
3028: if (defined($fields{'ipwd'})) {
3029: if ($entries{$fields{'ipwd'}}) {
3030: $password=$entries{$fields{'ipwd'}};
3031: }
3032: }
3033: # determine user role
3034: my $role = '';
3035: if (defined($fields{'role'})) {
3036: if ($entries{$fields{'role'}}) {
1.2 raeburn 3037: my @poss_roles =
1.1 raeburn 3038: &curr_role_permissions($context,$setting);
1.2 raeburn 3039: if (grep(/^\Q$entries{$fields{'role'}}\E/,@poss_roles)) {
1.1 raeburn 3040: $role=$entries{$fields{'role'}};
3041: } else {
3042: my $rolestr = join(', ',@poss_roles);
3043: $r->print('<br />'.
3044: &mt('<b>[_1]</b>: You do not have permission to add the requested role [_2] for the user.',$entries{$fields{'username'}},$entries{$fields{'role'}}).'<br />'.&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n");
3045: next;
3046: }
3047: }
3048: }
3049: if ($role eq '') {
3050: $role = $defaultrole;
3051: }
3052: # Clean up whitespace
3053: foreach (\$domain,\$username,\$id,\$fname,\$mname,
3054: \$lname,\$gen,\$sec,\$role) {
3055: $$_ =~ s/(\s+$|^\s+)//g;
3056: }
1.5 raeburn 3057: # check against rules
3058: my $checkid = 0;
3059: my $newuser = 0;
3060: my (%rulematch,%inst_results,%idinst_results);
3061: my $uhome=&Apache::lonnet::homeserver($username,$domain);
3062: if ($uhome eq 'no_host') {
3063: $checkid = 1;
3064: $newuser = 1;
3065: my $checkhash;
3066: my $checks = { 'username' => 1 };
3067: $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
3068: &Apache::loncommon::user_rule_check($checkhash,$checks,
3069: \%alerts,\%rulematch,\%inst_results,\%curr_rules,
3070: \%got_rules);
3071: if (ref($alerts{'username'}) eq 'HASH') {
3072: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
3073: next if ($alerts{'username'}{$domain}{$username});
3074: }
3075: }
1.13 raeburn 3076: } else {
3077: # FIXME check if user info can be updated.
1.5 raeburn 3078: }
3079: if ($id ne '') {
3080: if (!$newuser) {
3081: my %idhash = &Apache::lonnet::idrget($domain,($username));
3082: if ($idhash{$username} ne $id) {
3083: $checkid = 1;
3084: }
3085: }
3086: if ($checkid) {
3087: my $checkhash;
3088: my $checks = { 'id' => 1 };
3089: $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser,
3090: 'id' => $id };
3091: &Apache::loncommon::user_rule_check($checkhash,$checks,
3092: \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
3093: \%got_rules);
3094: if (ref($alerts{'id'}) eq 'HASH') {
3095: if (ref($alerts{'id'}{$domain}) eq 'HASH') {
3096: next if ($alerts{'id'}{$domain}{$id});
3097: }
3098: }
3099: }
3100: }
1.1 raeburn 3101: if ($password || $env{'form.login'} eq 'loc') {
3102: my ($userresult,$authresult,$roleresult);
3103: if ($role eq 'st') {
3104: &modifystudent($domain,$username,$cid,$sec,
3105: $desiredhost);
3106: $roleresult =
3107: &Apache::lonnet::modifystudent
3108: ($domain,$username,$id,$amode,$password,
3109: $fname,$mname,$lname,$gen,$sec,$enddate,
3110: $startdate,$env{'form.forceid'},
3111: $desiredhost,$email);
3112: } else {
3113: ($userresult,$authresult,$roleresult) =
3114: &modifyuserrole($context,$setting,
3115: $changeauth,$cid,$domain,$username,
3116: $id,$amode,$password,$fname,
3117: $mname,$lname,$gen,$sec,
3118: $env{'form.forceid'},$desiredhost,
1.5 raeburn 3119: $email,$role,$enddate,$startdate,$checkid);
1.1 raeburn 3120: }
3121: $flushc =
3122: &user_change_result($r,$userresult,$authresult,
3123: $roleresult,\%counts,$flushc,
3124: $username,%userchg);
3125: } else {
3126: if ($context eq 'course') {
3127: $r->print('<br />'.
3128: &mt('<b>[_1]</b>: Unable to enroll. No password specified.',$username)
3129: );
1.13 raeburn 3130: } elsif ($context eq 'author') {
1.1 raeburn 3131: $r->print('<br />'.
3132: &mt('<b>[_1]</b>: Unable to add co-author. No password specified.',$username)
3133: );
3134: } else {
3135: $r->print('<br />'.
3136: &mt('<b>[_1]</b>: Unable to add user. No password specified.',$username)
3137: );
3138: }
3139: }
3140: }
3141: }
3142: } # end of foreach (@userdata)
3143: # Flush the course logs so reverse user roles immediately updated
1.5 raeburn 3144: &Apache::lonnet::flushcourselogs();
1.1 raeburn 3145: $r->print("</p>\n<p>\n".&mt('Processed [_1] user(s).',$counts{'user'}).
3146: "</p>\n");
3147: if ($counts{'role'} > 0) {
3148: $r->print("<p>\n".
3149: &mt('Roles added for [_1] users. If user is active, the new role will be available when the user next logs in to LON-CAPA.',$counts{'role'})."</p>\n");
3150: }
3151: if ($counts{'auth'} > 0) {
3152: $r->print("<p>\n".
3153: &mt('Authentication changed for [_1] existing users.',
3154: $counts{'auth'})."</p>\n");
3155: }
1.13 raeburn 3156: $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1 raeburn 3157: $r->print('<form name="uploadresult" action="/adm/createuser">');
3158: $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','prevphase','currstate']));
3159: $r->print('</form>');
3160: #####################################
3161: # Drop students #
3162: #####################################
3163: if ($env{'form.fullup'} eq 'yes') {
3164: $r->print('<h3>'.&mt('Dropping Students')."</h3>\n");
3165: # Get current classlist
3166: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
3167: if (! defined($classlist)) {
3168: $r->print(&mt('There are no students currently enrolled.').
3169: "\n");
3170: } else {
3171: # Remove the students we just added from the list of students.
3172: foreach (@userdata) {
3173: my %entries=&Apache::loncommon::record_sep($_);
3174: unless (($entries{$fields{'username'}} eq '') ||
3175: (!defined($entries{$fields{'username'}}))) {
3176: delete($classlist->{$entries{$fields{'username'}}.
3177: ':'.$domain});
3178: }
3179: }
3180: # Print out list of dropped students.
3181: &show_drop_list($r,$classlist,$keylist,'nosort');
3182: }
3183: }
3184: } # end of unless
3185: }
3186:
1.13 raeburn 3187: sub print_namespacing_alerts {
3188: my ($domain,$alerts,$curr_rules) = @_;
3189: my $output;
3190: if (ref($alerts) eq 'HASH') {
3191: if (keys(%{$alerts}) > 0) {
3192: if (ref($alerts->{'username'}) eq 'HASH') {
3193: foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
3194: my $count;
3195: if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
3196: $count = keys(%{$alerts->{'username'}{$dom}});
3197: }
3198: my $domdesc = &Apache::lonnet::domain($domain,'description');
3199: if (ref($curr_rules->{$dom}) eq 'HASH') {
3200: $output .= &Apache::loncommon::instrule_disallow_msg(
3201: 'username',$domdesc,$count,'upload');
3202: }
3203: $output .= &Apache::loncommon::user_rule_formats($dom,
3204: $domdesc,$curr_rules->{$dom}{'username'},
3205: 'username');
3206: }
3207: }
3208: if (ref($alerts->{'id'}) eq 'HASH') {
3209: foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
3210: my $count;
3211: if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
3212: $count = keys(%{$alerts->{'id'}{$dom}});
3213: }
3214: my $domdesc = &Apache::lonnet::domain($domain,'description');
3215: if (ref($curr_rules->{$dom}) eq 'HASH') {
3216: $output .= &Apache::loncommon::instrule_disallow_msg(
3217: 'id',$domdesc,$count,'upload');
3218: }
3219: $output .= &Apache::loncommon::user_rule_formats($dom,
3220: $domdesc,$curr_rules->{$dom}{'id'},'id');
3221: }
3222: }
3223: }
3224: }
3225: }
3226:
1.1 raeburn 3227: sub user_change_result {
3228: my ($r,$userresult,$authresult,$roleresult,$counts,$flushc,$username,
3229: $userchg) = @_;
3230: my $okresult = 0;
3231: if ($userresult ne 'ok') {
3232: if ($userresult =~ /^error:(.+)$/) {
3233: my $error = $1;
3234: $r->print('<br />'.
3235: &mt('<b>[_1]</b>: Unable to add/modify: [_2]',$username,$error));
3236: }
3237: } else {
3238: $counts->{'user'} ++;
3239: $okresult = 1;
3240: }
3241: if ($authresult ne 'ok') {
3242: if ($authresult =~ /^error:(.+)$/) {
3243: my $error = $1;
3244: $r->print('<br />'.
3245: &mt('<b>[_1]</b>: Unable to modify authentication: [_2]',$username,$error));
3246: }
3247: } else {
3248: $counts->{'auth'} ++;
3249: $okresult = 1;
3250: }
3251: if ($roleresult ne 'ok') {
3252: if ($roleresult =~ /^error:(.+)$/) {
3253: my $error = $1;
3254: $r->print('<br />'.
3255: &mt('<b>[_1]</b>: Unable to add role: [_2]',$username,$error));
3256: }
3257: } else {
3258: $counts->{'role'} ++;
3259: $okresult = 1;
3260: }
3261: if ($okresult) {
3262: $flushc++;
3263: $userchg->{$username}=1;
3264: $r->print('. ');
3265: if ($flushc>15) {
3266: $r->rflush;
3267: $flushc=0;
3268: }
3269: }
3270: return $flushc;
3271: }
3272:
3273: # ========================================================= Menu Phase Two Drop
3274: sub print_expire_menu {
3275: my ($r,$context) = @_;
3276: $r->print("<h3>".&mt("Expire Users' Roles")."</h3>");
3277: my $cid=$env{'request.course.id'};
3278: my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
3279: if (! defined($classlist)) {
3280: $r->print(&mt('There are no students currently enrolled.')."\n");
3281: return;
3282: }
3283: # Print out the available choices
3284: &show_drop_list($r,$classlist,$keylist);
3285: return;
3286: }
3287:
3288:
3289: # ================================================================== Phase four
3290:
1.11 raeburn 3291: sub update_user_list {
3292: my ($r,$context,$setting,$choice) = @_;
3293: my $now = time;
1.1 raeburn 3294: my $count=0;
1.11 raeburn 3295: my @changelist;
3296: if ($choice ne '') {
3297: @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
3298: } else {
3299: @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
3300: }
3301: my %result_text = ( ok => { 'revoke' => 'Revoked',
3302: 'delete' => 'Deleted',
3303: 'reenable' => 'Re-enabled',
3304: 'activate' => 'Activated',
3305: },
3306: error => {'revoke' => 'revoking',
3307: 'delete' => 'deleting',
3308: 'reenable' => 're-enabling',
3309: 'activate' => 'activating',
3310: },
3311: );
3312: my ($startdate,$enddate);
3313: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
3314: ($startdate,$enddate) = &get_dates_from_form();
3315: }
3316: foreach my $item (@changelist) {
3317: my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
3318: $scopestem);
3319: if ($context eq 'course') {
3320: ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
3321: $cid = $env{'request.course.id'};
3322: $scopestem = '/'.$cid;
3323: $scopestem =~s/\_/\//g;
3324: if ($sec eq '') {
3325: $scope = $scopestem;
3326: } else {
3327: $scope = $scopestem.'/'.$sec;
3328: }
1.13 raeburn 3329: } elsif ($context eq 'author') {
1.11 raeburn 3330: ($uname,$udom,$role) = split(/\:/,$item,-1);
3331: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
3332: } elsif ($context eq 'domain') {
3333: if ($setting eq 'domain') {
3334: ($role,$uname,$udom) = split(/\:/,$item,-1);
3335: $scope = '/'.$env{'request.role.domain'}.'/';
1.13 raeburn 3336: } elsif ($setting eq 'author') {
1.11 raeburn 3337: ($uname,$udom,$role,$scope) = split(/\:/,$item);
3338: } elsif ($setting eq 'course') {
3339: ($uname,$udom,$role,$cid,$sec,$type,$locktype) =
3340: split(/\:/,$item);
3341: $scope = '/'.$cid;
3342: $scope =~s/\_/\//g;
3343: if ($sec ne '') {
3344: $scope .= '/'.$sec;
3345: }
3346: }
3347: }
3348: my $plrole = &Apache::lonnet::plaintext($role);
3349: my ($uid,$first,$middle,$last,$gene,$sec);
3350: my $start = $env{'form.'.$item.'_start'};
3351: my $end = $env{'form.'.$item.'_end'};
3352: # revoke or delete user role
3353: if ($choice eq 'revoke') {
3354: $end = $now;
3355: if ($role eq 'st') {
3356: $result =
3357: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3358: } else {
3359: $result =
3360: &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
3361: }
3362: } elsif ($choice eq 'delete') {
3363: $start = -1;
3364: $end = -1;
3365: if ($role eq 'st') {
3366: # FIXME - how does role deletion affect classlist?
3367: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3368: } else {
3369: $result =
3370: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
3371: 0,1);
3372: }
3373: } else {
3374: #reenable, activate, change access dates or change section
3375: if ($choice ne 'chgsec') {
3376: $start = $startdate;
3377: $end = $enddate;
3378: }
3379: if ($choice eq 'reenable') {
3380: if ($role eq 'st') {
3381: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3382: } else {
3383: $result =
3384: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3385: $now);
3386: }
3387: } elsif ($choice eq 'activate') {
3388: if ($role eq 'st') {
3389: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3390: } else {
3391: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3392: $now);
3393: }
3394: } elsif ($choice eq 'chgdates') {
3395: if ($role eq 'st') {
3396: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
3397: } else {
3398: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
3399: $start);
3400: }
3401: } elsif ($choice eq 'chgsec') {
3402: my (@newsecs,$revresult,$nochg,@retained);
3403: if ($role ne 'cc') {
3404: @newsecs = split(/,/,$env{'form.newsecs'});
3405: }
3406: # remove existing section if not to be retained.
3407: if (!$env{'form.retainsec'}) {
3408: if ($sec eq '') {
3409: if (@newsecs == 0) {
3410: $result = &mt('No change in section assignment (none)');
3411: $nochg = 1;
3412: }
3413: } else {
3414: if (!grep(/^\Q$sec\E$/,@newsecs)) {
3415: $revresult =
3416: &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
3417: } else {
3418: push(@retained,$sec);
3419: }
3420: }
3421: } else {
3422: push(@retained,$sec);
3423: }
3424: # add new sections
3425: if (@newsecs == 0) {
3426: if (!$nochg) {
3427: if ($sec ne '') {
3428: if ($role eq 'st') {
3429: $result =
3430: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid);
3431: } else {
3432: my $newscope = $scopestem;
3433: $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start);
3434: }
3435: }
3436: }
3437: } else {
3438: foreach my $newsec (@newsecs) {
3439: if (!grep(/^\Q$newsec\E$/,@retained)) {
3440: if ($role eq 'st') {
3441: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid);
3442: } else {
3443: my $newscope = $scopestem;
3444: if ($newsec ne '') {
3445: $newscope .= '/'.$newsec;
3446: }
3447: $result = &Apache::lonnet::assignrole($udom,$uname,
3448: $newscope,$role,$end,$start);
3449: }
3450: }
3451: }
3452: }
3453: }
3454: }
1.1 raeburn 3455: if ($result eq 'ok' || $result eq 'ok:') {
1.11 raeburn 3456: $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
3457: $plrole,$scope,$uname.':'.$udom).'<br />');
1.1 raeburn 3458: $count++;
3459: } else {
3460: $r->print(
1.11 raeburn 3461: &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]:[_4]",
3462: $plrole,$scope,$uname.':'.$udom,$result).'<br />');
3463: }
3464: }
3465: $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,users].",$count).'</b></p>');
3466: if ($count > 0) {
3467: if ($choice eq 'revoke') {
3468: $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.</p>'));
3469: }
3470: # Flush the course logs so reverse user roles immediately updated
3471: &Apache::lonnet::flushcourselogs();
3472: }
3473: if ($env{'form.makedatesdefault'}) {
3474: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
3475: $r->print(&make_dates_default($startdate,$enddate));
1.1 raeburn 3476: }
3477: }
3478: }
3479:
1.8 raeburn 3480: sub classlist_drop {
3481: my ($scope,$uname,$udom,$now,$action) = @_;
3482: my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
3483: my $cid=$cdom.'_'.$cnum;
3484: my $user = $uname.':'.$udom;
3485: if ($action eq 'drop') {
3486: if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
3487: my $result =
3488: &Apache::lonnet::cput('classlist',
3489: { $user => $now },
3490: $env{'course.'.$cid.'.domain'},
3491: $env{'course.'.$cid.'.num'});
3492: return &mt('Drop from classlist: [_1]',
3493: '<b>'.$result.'</b>').'<br />';
3494: }
3495: }
3496: }
3497:
3498: sub active_student_roles {
3499: my ($cnum,$cdom,$uname,$udom) = @_;
3500: my %roles =
3501: &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
3502: ['future','active'],['st']);
3503: return exists($roles{"$cnum:$cdom:st"});
3504: }
3505:
1.1 raeburn 3506: sub section_check_js {
1.8 raeburn 3507: my $groupslist= &get_groupslist();
1.1 raeburn 3508: return <<"END";
3509: function validate(caller) {
1.9 raeburn 3510: var groups = new Array($groupslist);
1.1 raeburn 3511: var secname = caller.value;
3512: if ((secname == 'all') || (secname == 'none')) {
3513: alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
3514: return 'error';
3515: }
3516: if (secname != '') {
3517: for (var k=0; k<groups.length; k++) {
3518: if (secname == groups[k]) {
3519: alert("'"+secname+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
3520: return 'error';
3521: }
3522: }
3523: }
3524: return 'ok';
3525: }
3526: END
3527: }
3528:
3529: sub set_login {
3530: my ($dom,$authformkrb,$authformint,$authformloc) = @_;
3531: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
3532: my $response;
3533: my ($authnum,%can_assign) =
3534: &Apache::loncommon::get_assignable_auth($dom);
3535: if ($authnum) {
3536: $response = &Apache::loncommon::start_data_table();
3537: if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
3538: $response .= &Apache::loncommon::start_data_table_row().
3539: '<td>'.$authformkrb.'</td>'.
3540: &Apache::loncommon::end_data_table_row()."\n";
3541: }
3542: if ($can_assign{'int'}) {
3543: $response .= &Apache::loncommon::start_data_table_row().
3544: '<td>'.$authformint.'</td>'.
3545: &Apache::loncommon::end_data_table_row()."\n"
3546: }
3547: if ($can_assign{'loc'}) {
3548: $response .= &Apache::loncommon::start_data_table_row().
3549: '<td>'.$authformloc.'</td>'.
3550: &Apache::loncommon::end_data_table_row()."\n";
3551: }
3552: $response .= &Apache::loncommon::end_data_table();
3553: }
3554: return $response;
3555: }
3556:
1.8 raeburn 3557: sub course_sections {
3558: my ($sections_count,$role) = @_;
3559: my $output = '';
3560: my @sections = (sort {$a <=> $b} keys %{$sections_count});
3561: if (scalar(@sections) == 1) {
3562: $output = '<select name="currsec_'.$role.'" >'."\n".
3563: ' <option value="">Select</option>'."\n".
3564: ' <option value="">No section</option>'."\n".
3565: ' <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
3566: } else {
3567: $output = '<select name="currsec_'.$role.'" ';
3568: my $multiple = 4;
3569: if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
3570: $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
3571: foreach my $sec (@sections) {
3572: $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
3573: }
3574: }
3575: $output .= '</select>';
3576: return $output;
3577: }
3578:
3579: sub get_groupslist {
3580: my $groupslist;
3581: my %curr_groups = &Apache::longroup::coursegroups();
3582: if (%curr_groups) {
3583: $groupslist = join('","',sort(keys(%curr_groups)));
3584: $groupslist = '"'.$groupslist.'"';
3585: }
1.11 raeburn 3586: return $groupslist;
1.8 raeburn 3587: }
3588:
3589: sub setsections_javascript {
3590: my ($form,$groupslist) = @_;
3591: my ($checkincluded,$finish,$roleplace,$setsection_js);
3592: if ($form eq 'cu') {
3593: $checkincluded = 'formname.elements[i-1].checked == true';
3594: $finish = 'formname.submit()';
3595: $roleplace = 3;
3596: } else {
1.11 raeburn 3597: $checkincluded = 'formname.name == "'.$form.'"';
1.8 raeburn 3598: $finish = "seccheck = 'ok';";
3599: $roleplace = 1;
1.11 raeburn 3600: $setsection_js = "var seccheck = 'alert';";
1.8 raeburn 3601: }
3602: my %alerts = &Apache::lonlocal::texthash(
3603: secd => 'Section designations do not apply to Course Coordinator roles.',
3604: accr => 'A course coordinator role will be added with access to all sections.',
3605: inea => 'In each course, each user may only have one student role at a time.',
3606: youh => 'You had selected ',
3607: secs => 'sections.',
3608: plmo => 'Please modify your selections so they include no more than one section.',
3609: mayn => 'may not be used as the name for a section, as it is a reserved word.',
3610: plch => 'Please choose a different section name.',
3611: mnot => 'may not be used as a section name, as it is the name of a course group.',
3612: secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.11 raeburn 3613: );
1.8 raeburn 3614: $setsection_js .= <<"ENDSECCODE";
3615:
3616: function setSections(formname) {
3617: var re1 = /^currsec_/;
3618: var groups = new Array($groupslist);
3619: for (var i=0;i<formname.elements.length;i++) {
3620: var str = formname.elements[i].name;
3621: var checkcurr = str.match(re1);
3622: if (checkcurr != null) {
3623: if ($checkincluded) {
3624: var match = str.split('_');
3625: var role = match[$roleplace];
3626: if (role == 'cc') {
3627: alert("$alerts{'secd'}\\n$alerts{'accr'}");
3628: }
3629: else {
3630: var sections = '';
3631: var numsec = 0;
3632: var sections;
3633: for (var j=0; j<formname.elements[i].length; j++) {
3634: if (formname.elements[i].options[j].selected == true ) {
3635: if (formname.elements[i].options[j].value != "") {
3636: if (numsec == 0) {
3637: if (formname.elements[i].options[j].value != "") {
3638: sections = formname.elements[i].options[j].value;
3639: numsec ++;
3640: }
3641: }
3642: else {
3643: sections = sections + "," + formname.elements[i].options[j].value
3644: numsec ++;
3645: }
3646: }
3647: }
3648: }
3649: if (numsec > 0) {
3650: if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
3651: sections = sections + "," + formname.elements[i+1].value;
3652: }
3653: }
3654: else {
3655: sections = formname.elements[i+1].value;
3656: }
3657: var newsecs = formname.elements[i+1].value;
3658: var numsplit;
3659: if (newsecs != null && newsecs != "") {
3660: numsplit = newsecs.split(/,/g);
3661: numsec = numsec + numsplit.length;
3662: }
3663:
3664: if ((role == 'st') && (numsec > 1)) {
3665: alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
3666: return;
3667: }
3668: else {
3669: if (numsplit != null) {
3670: for (var j=0; j<numsplit.length; j++) {
3671: if ((numsplit[j] == 'all') ||
3672: (numsplit[j] == 'none')) {
3673: alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
3674: return;
3675: }
3676: for (var k=0; k<groups.length; k++) {
3677: if (numsplit[j] == groups[k]) {
3678: alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
3679: return;
3680: }
3681: }
3682: }
3683: }
3684: formname.elements[i+2].value = sections;
3685: }
3686: }
3687: }
3688: }
3689: }
3690: $finish
3691: }
3692: ENDSECCODE
1.11 raeburn 3693: return $setsection_js;
1.8 raeburn 3694: }
3695:
1.1 raeburn 3696: 1;
3697:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>