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