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