1: # The LearningOnline Network with CAPA
2: # Utility functions for managing LON-CAPA user accounts
3: #
4: # $Id: lonuserutils.pm,v 1.97.2.25 2010/11/15 18:50:37 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#
23: # /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;
38: use Apache::longroup;
39: use LONCAPA qw(:DEFAULT :match);
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,$context)=@_;
46: # if $csec is undefined, drop the student from all the courses matching
47: # this one. If $csec is defined, drop them from all other sections of
48: # this course and add them to section $csec
49: my ($cnum,$cdom) = &get_course_identity($courseid);
50: my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
51: my %roles = &Apache::lonnet::dump('roles',$udom,$unam,'.',undef,$extra);
52: my ($tmp) = keys(%roles);
53: # Bail out if we were unable to get the students roles
54: return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
55: # Go through the roles looking for enrollment in this course
56: my $result = '';
57: foreach my $course (keys(%roles)) {
58: if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
59: # We are in this course
60: my $section=$1;
61: $section='' if ($course eq "/$cdom/$cnum".'_st');
62: if (defined($csec) && $section eq $csec) {
63: $result .= 'ok:';
64: } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
65: my (undef,$end,$start)=split(/\_/,$roles{$course});
66: my $now=time;
67: # if this is an active role
68: if (!($start && ($now<$start)) || !($end && ($now>$end))) {
69: my $reply=&Apache::lonnet::modifystudent
70: # dom name id mode pass f m l g
71: ($udom,$unam,'', '', '',undef,undef,undef,undef,
72: $section,time,undef,undef,$desiredhost,'','manual',
73: '',$courseid,'',$context);
74: $result .= $reply.':';
75: }
76: }
77: }
78: }
79: if ($result eq '') {
80: $result = &mt('Unable to find section for this student');
81: } else {
82: $result =~ s/(ok:)+/ok/g;
83: }
84: return $result;
85: }
86:
87: sub modifyuserrole {
88: my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
89: $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
90: $end,$start,$checkid,$inststatus) = @_;
91: my ($scope,$userresult,$authresult,$roleresult,$idresult);
92: if ($setting eq 'course' || $context eq 'course') {
93: $scope = '/'.$cid;
94: $scope =~ s/\_/\//g;
95: if (($role ne 'cc') && ($role ne 'co') && ($sec ne '')) {
96: $scope .='/'.$sec;
97: }
98: } elsif ($context eq 'domain') {
99: $scope = '/'.$env{'request.role.domain'}.'/';
100: } elsif ($context eq 'author') {
101: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
102: }
103: if ($context eq 'domain') {
104: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
105: if ($uhome ne 'no_host') {
106: if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
107: if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
108: ($umode eq 'localauth')) {
109: $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
110: }
111: }
112: if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
113: ($env{'form.recurseid'}) && ($checkid)) {
114: my %userupdate = (
115: lastname => $last,
116: middlename => $middle,
117: firstname => $first,
118: generation => $gene,
119: id => $uid,
120: );
121: $idresult = &propagate_id_change($uname,$udom,\%userupdate);
122: }
123: }
124: }
125: $userresult =
126: &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
127: $middle,$last,$gene,$forceid,$desiredhome,
128: $email,$inststatus);
129: if ($userresult eq 'ok') {
130: if ($role ne '') {
131: $role =~ s/_/\//g;
132: $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
133: $role,$end,$start,'',
134: '',$context);
135: }
136: }
137: return ($userresult,$authresult,$roleresult,$idresult);
138: }
139:
140: sub propagate_id_change {
141: my ($uname,$udom,$user) = @_;
142: my (@types,@roles);
143: @types = ('active','future');
144: @roles = ('st');
145: my $idresult;
146: my %roleshash = &Apache::lonnet::get_my_roles($uname,
147: $udom,'userroles',\@types,\@roles);
148: my %args = (
149: one_time => 1,
150: );
151: foreach my $item (keys(%roleshash)) {
152: my ($cnum,$cdom,$role) = split(/:/,$item,-1);
153: my ($start,$end) = split(/:/,$roleshash{$item});
154: if (&Apache::lonnet::is_course($cdom,$cnum)) {
155: my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
156: my %coursehash =
157: &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
158: my $cdesc = $coursehash{'description'};
159: if ($cdesc eq '') {
160: $cdesc = $cdom.'_'.$cnum;
161: }
162: if ($result eq 'ok') {
163: $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
164: } else {
165: $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
166: }
167: }
168: }
169: return $idresult;
170: }
171:
172: sub update_classlist {
173: my ($cdom,$cnum,$udom,$uname,$user,$newend) = @_;
174: my ($uid,$classlistentry);
175: my $fullname =
176: &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
177: $user->{'lastname'},$user->{'generation'},
178: 'lastname');
179: my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
180: $cdom,$cnum);
181: my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
182: my $ididx=&Apache::loncoursedata::CL_ID() - 2;
183: my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
184: my $endidx = &Apache::loncoursedata::CL_END() - 2;
185: my $startidx = &Apache::loncoursedata::CL_START() - 2;
186: for (my $i=0; $i<@classinfo; $i++) {
187: if ($i == $endidx) {
188: if ($newend ne '') {
189: $classlistentry .= $newend.':';
190: } else {
191: $classlistentry .= $classinfo[$i].':';
192: }
193: } elsif ($i == $startidx) {
194: if ($newend ne '') {
195: if ($classinfo[$i] > $newend) {
196: $classlistentry .= $newend.':';
197: } else {
198: $classlistentry .= $classinfo[$i].':';
199: }
200: } else {
201: $classlistentry .= $classinfo[$i].':';
202: }
203: } elsif ($i == $ididx) {
204: if (defined($user->{'id'})) {
205: $classlistentry .= $user->{'id'}.':';
206: } else {
207: $classlistentry .= $classinfo[$i].':';
208: }
209: } elsif ($i == $nameidx) {
210: if (defined($user->{'lastname'})) {
211: $classlistentry .= $fullname.':';
212: } else {
213: $classlistentry .= $classinfo[$i].':';
214: }
215: } else {
216: $classlistentry .= $classinfo[$i].':';
217: }
218: }
219: $classlistentry =~ s/:$//;
220: my $reply=&Apache::lonnet::cput('classlist',
221: {"$uname:$udom" => $classlistentry},
222: $cdom,$cnum);
223: if (($reply eq 'ok') || ($reply eq 'delayed')) {
224: return 'ok';
225: } else {
226: return 'error: '.$reply;
227: }
228: }
229:
230:
231: ###############################################################
232: ###############################################################
233: # build a role type and role selection form
234: sub domain_roles_select {
235: # Set up the role type and role selection boxes when in
236: # domain context
237: #
238: # Role types
239: my @roletypes = ('domain','author','course','community');
240: my %lt = &role_type_names();
241: #
242: # build up the menu information to be passed to
243: # &Apache::loncommon::linked_select_forms
244: my %select_menus;
245: if ($env{'form.roletype'} eq '') {
246: $env{'form.roletype'} = 'domain';
247: }
248: foreach my $roletype (@roletypes) {
249: # set up the text for this domain
250: $select_menus{$roletype}->{'text'}= $lt{$roletype};
251: my $crstype;
252: if ($roletype eq 'community') {
253: $crstype = 'Community';
254: }
255: # we want a choice of 'default' as the default in the second menu
256: if ($env{'form.roletype'} ne '') {
257: $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
258: } else {
259: $select_menus{$roletype}->{'default'} = 'Any';
260: }
261: # Now build up the other items in the second menu
262: my @roles;
263: if ($roletype eq 'domain') {
264: @roles = &domain_roles();
265: } elsif ($roletype eq 'author') {
266: @roles = &construction_space_roles();
267: } else {
268: my $custom = 1;
269: @roles = &course_roles('domain',undef,$custom,$roletype);
270: }
271: my $order = ['Any',@roles];
272: $select_menus{$roletype}->{'order'} = $order;
273: foreach my $role (@roles) {
274: if ($role eq 'cr') {
275: $select_menus{$roletype}->{'select2'}->{$role} =
276: &mt('Custom role');
277: } else {
278: $select_menus{$roletype}->{'select2'}->{$role} =
279: &Apache::lonnet::plaintext($role,$crstype);
280: }
281: }
282: $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
283: }
284: my $result = &Apache::loncommon::linked_select_forms
285: ('studentform',(' 'x3).&mt('Role: '),$env{'form.roletype'},
286: 'roletype','showrole',\%select_menus,
287: ['domain','author','course','community']);
288: return $result;
289: }
290:
291: ###############################################################
292: ###############################################################
293: sub hidden_input {
294: my ($name,$value) = @_;
295: return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
296: }
297:
298: sub print_upload_manager_header {
299: my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype,
300: $can_assign)=@_;
301: my $javascript;
302: #
303: if (! exists($env{'form.upfile_associate'})) {
304: $env{'form.upfile_associate'} = 'forward';
305: }
306: if ($env{'form.associate'} eq 'Reverse Association') {
307: if ( $env{'form.upfile_associate'} ne 'reverse' ) {
308: $env{'form.upfile_associate'} = 'reverse';
309: } else {
310: $env{'form.upfile_associate'} = 'forward';
311: }
312: }
313: if ($env{'form.upfile_associate'} eq 'reverse') {
314: $javascript=&upload_manager_javascript_reverse_associate($can_assign);
315: } else {
316: $javascript=&upload_manager_javascript_forward_associate($can_assign);
317: }
318: #
319: # Deal with restored settings
320: my $password_choice = '';
321: if (exists($env{'form.ipwd_choice'}) &&
322: $env{'form.ipwd_choice'} ne '') {
323: # If a column was specified for password, assume it is for an
324: # internal password. This is a bug waiting to be filed (could be
325: # local or krb auth instead of internal) but I do not have the
326: # time to mess around with this now.
327: $password_choice = 'int';
328: }
329: #
330: my $groupslist;
331: if ($context eq 'course') {
332: $groupslist = &get_groupslist();
333: }
334: my $javascript_validations =
335: &javascript_validations('upload',$krbdefdom,$password_choice,undef,
336: $env{'request.role.domain'},$context,
337: $groupslist,$crstype);
338: my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
339: $r->print('<p>'
340: .&mt('Total number of records found in file: [_1]'
341: ,'<b>'.$distotal.'</b>')
342: ."</p>\n");
343: $r->print('<div class="LC_left_float"><h3>'.
344: &mt('Identify fields in uploaded list')."</h3>\n");
345: $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");
346: $r->print(&hidden_input('action','upload').
347: &hidden_input('state','got_file').
348: &hidden_input('associate','').
349: &hidden_input('datatoken',$datatoken).
350: &hidden_input('fileupload',$env{'form.fileupload'}).
351: &hidden_input('upfiletype',$env{'form.upfiletype'}).
352: &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
353: $r->print('<br /><label><input type="checkbox" name="noFirstLine"'.$checked.' />'.
354: &mt('Ignore First Line').'</label><br />');
355: $r->print('<br /><input type="button" value="'.&mt('Reverse Association').'" '.
356: 'name="Reverse Association" '.
357: 'onclick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
358: $r->print("<br /><br />\n".
359: '<script type="text/javascript" language="Javascript">'."\n".
360: '// <![CDATA['."\n".
361: $javascript."\n".$javascript_validations."\n".
362: '// ]]>'."\n".
363: '</script>');
364: }
365:
366: ###############################################################
367: ###############################################################
368: sub javascript_validations {
369: my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
370: $context,$groupslist,$crstype)=@_;
371: my %param = (
372: kerb_def_dom => $krbdefdom,
373: curr_authtype => $curr_authtype,
374: );
375: if ($mode eq 'upload') {
376: $param{'formname'} = 'studentform';
377: } elsif ($mode eq 'createcourse') {
378: $param{'formname'} = 'ccrs';
379: } elsif ($mode eq 'modifycourse') {
380: $param{'formname'} = 'cmod';
381: $param{'mode'} = 'modifycourse',
382: $param{'curr_autharg'} = $curr_authfield;
383: }
384:
385: my ($setsection_call,$setsections_js);
386: my $finish = " vf.submit();\n";
387: if ($mode eq 'upload') {
388: if (($context eq 'course') || ($context eq 'domain')) {
389: if ($context eq 'course') {
390: if ($env{'request.course.sec'} eq '') {
391: $setsection_call = 'setSections(document.'.$param{'formname'}.",'$crstype'".');';
392: $setsections_js =
393: &setsections_javascript($param{'formname'},$groupslist,
394: $mode,'',$crstype);
395: } else {
396: $setsection_call = "'ok'";
397: }
398: } elsif ($context eq 'domain') {
399: $setsection_call = 'setCourse()';
400: $setsections_js = &dc_setcourse_js($param{'formname'},$mode,$context);
401: }
402: $finish = " var checkSec = $setsection_call\n".
403: " if (checkSec == 'ok') {\n".
404: " vf.submit();\n".
405: " }\n";
406: }
407: }
408: my $authheader = &Apache::loncommon::authform_header(%param);
409:
410: my %alert = &Apache::lonlocal::texthash
411: (username => 'You need to specify the username field.',
412: authen => 'You must choose an authentication type.',
413: krb => 'You need to specify the Kerberos domain.',
414: ipass => 'You need to specify the initial password.',
415: name => 'The optional name field was not specified.',
416: snum => 'The optional student/employee ID field was not specified.',
417: section => 'The optional section field was not specified.',
418: email => 'The optional e-mail address field was not specified.',
419: role => 'The optional role field was not specified.',
420: domain => 'The optional domain field was not specified.',
421: continue => 'Continue adding users?',
422: );
423: if (($mode eq 'upload') && ($context eq 'domain')) {
424: $alert{'inststatus'} = &mt('The optional affiliation field was not specified');
425: }
426: my $function_name = <<"END";
427: $setsections_js
428:
429: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus) {
430: END
431: my ($authnum,%can_assign) = &Apache::loncommon::get_assignable_auth($domain);
432: my $auth_checks;
433: if ($mode eq 'createcourse') {
434: $auth_checks .= (<<END);
435: if (vf.autoadds[0].checked == true) {
436: if (current.radiovalue == null || current.radiovalue == 'nochange') {
437: alert('$alert{'authen'}');
438: return;
439: }
440: }
441: END
442: } else {
443: $auth_checks .= (<<END);
444: var foundatype=0;
445: if (founduname==0) {
446: alert('$alert{'username'}');
447: return;
448: }
449:
450: END
451: if ($authnum > 1) {
452: $auth_checks .= (<<END);
453: if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
454: // They did not check any of the login radiobuttons.
455: alert('$alert{'authen'}');
456: return;
457: }
458: END
459: }
460: }
461: if ($mode eq 'createcourse') {
462: $auth_checks .= "
463: if ( (vf.autoadds[0].checked == true) &&
464: (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
465: ";
466: } elsif ($mode eq 'modifycourse') {
467: $auth_checks .= "
468: if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
469: ";
470: }
471: if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
472: $auth_checks .= (<<END);
473: var alertmsg = '';
474: switch (current.radiovalue) {
475: case 'krb':
476: alertmsg = '$alert{'krb'}';
477: break;
478: default:
479: alertmsg = '';
480: }
481: if (alertmsg != '') {
482: alert(alertmsg);
483: return;
484: }
485: }
486: END
487: } else {
488: $auth_checks .= (<<END);
489: foundatype=1;
490: if (current.argfield == null || current.argfield == '') {
491: var alertmsg = '';
492: switch (current.radiovalue) {
493: case 'krb':
494: alertmsg = '$alert{'krb'}';
495: break;
496: case 'loc':
497: case 'fsys':
498: alertmsg = '$alert{'ipass'}';
499: break;
500: case 'fsys':
501: alertmsg = '';
502: break;
503: default:
504: alertmsg = '';
505: }
506: if (alertmsg != '') {
507: alert(alertmsg);
508: return;
509: }
510: }
511: END
512: }
513: my $section_checks;
514: my $optional_checks = '';
515: if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
516: $optional_checks = (<<END);
517: vf.submit();
518: }
519: END
520: } else {
521: $section_checks = §ion_check_js();
522: $optional_checks = (<<END);
523: var message='';
524: if (foundname==0) {
525: message='$alert{'name'}';
526: }
527: if (foundid==0) {
528: if (message!='') {
529: message+='\\n';
530: }
531: message+='$alert{'snum'}';
532: }
533: if (foundsec==0) {
534: if (message!='') {
535: message+='\\n';
536: }
537: message+='$alert{'section'}';
538: }
539: if (foundemail==0) {
540: if (message!='') {
541: message+='\\n';
542: }
543: message+='$alert{'email'}';
544: }
545: if (foundrole==0) {
546: if (message!='') {
547: message+='\\n';
548: }
549: message+='$alert{'role'}';
550: }
551: if (founddomain==0) {
552: if (message!='') {
553: message+='\\n';
554: }
555: message+='$alert{'domain'}';
556: }
557: END
558: if (($mode eq 'upload') && ($context eq 'domain')) {
559: $optional_checks .= (<<END);
560:
561: if (foundinststatus==0) {
562: if (message!='') {
563: message+='\\n';
564: }
565: message+='$alert{'inststatus'}';
566: }
567: END
568: }
569: $optional_checks .= (<<END);
570:
571: if (message!='') {
572: message+= '\\n$alert{'continue'}';
573: if (confirm(message)) {
574: vf.state.value='enrolling';
575: $finish
576: }
577: } else {
578: vf.state.value='enrolling';
579: $finish
580: }
581: }
582: END
583: }
584: my $result = $function_name.$auth_checks.$optional_checks."\n".
585: $section_checks.$authheader;
586: return $result;
587: }
588: ###############################################################
589: ###############################################################
590: sub upload_manager_javascript_forward_associate {
591: my ($can_assign) = @_;
592: my ($auth_update,$numbuttons,$argreset);
593: if (ref($can_assign) eq 'HASH') {
594: if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
595: $argreset .= " vf.krbarg.value='';\n";
596: $numbuttons ++ ;
597: }
598: if ($can_assign->{'int'}) {
599: $argreset .= " vf.intarg.value='';\n";
600: $numbuttons ++;
601: }
602: if ($can_assign->{'loc'}) {
603: $argreset .= " vf.locarg.value='';\n";
604: $numbuttons ++;
605: }
606: if (!$can_assign->{'int'}) {
607: my $warning = &mt('You may not specify an initial password for each user, as this is only available when new users use LON-CAPA internal authentication.\n').
608: &mt('Your current role does not have rights to create users with that authentication type.');
609: $auth_update = <<"END";
610: // Currently the initial password field is only supported for internal auth
611: // (see bug 6368).
612: if (nw==9) {
613: eval('vf.f'+tf+'.selectedIndex=0;')
614: alert('$warning');
615: }
616: END
617: } elsif ($numbuttons > 1) {
618: $auth_update = <<"END";
619: // If we set the password, make the password form below correspond to
620: // the new value.
621: if (nw==9) {
622: changed_radio('int',document.studentform);
623: set_auth_radio_buttons('int',document.studentform);
624: $argreset
625: }
626:
627: END
628: }
629: }
630:
631: return(<<ENDPICK);
632: function verify(vf,sec_caller) {
633: var founduname=0;
634: var foundpwd=0;
635: var foundname=0;
636: var foundid=0;
637: var foundsec=0;
638: var foundemail=0;
639: var foundrole=0;
640: var founddomain=0;
641: var foundinststatus=0;
642: var tw;
643: for (i=0;i<=vf.nfields.value;i++) {
644: tw=eval('vf.f'+i+'.selectedIndex');
645: if (tw==1) { founduname=1; }
646: if ((tw>=2) && (tw<=6)) { foundname=1; }
647: if (tw==7) { foundid=1; }
648: if (tw==8) { foundsec=1; }
649: if (tw==9) { foundpwd=1; }
650: if (tw==10) { foundemail=1; }
651: if (tw==11) { foundrole=1; }
652: if (tw==12) { founddomain=1; }
653: if (tw==13) { foundinststatus=1; }
654: }
655: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
656: }
657:
658: //
659: // vf = this.form
660: // tf = column number
661: //
662: // values of nw
663: //
664: // 0 = none
665: // 1 = username
666: // 2 = names (lastname, firstnames)
667: // 3 = fname (firstname)
668: // 4 = mname (middlename)
669: // 5 = lname (lastname)
670: // 6 = gen (generation)
671: // 7 = id
672: // 8 = section
673: // 9 = ipwd (password)
674: // 10 = email address
675: // 11 = role
676: // 12 = domain
677: // 13 = inststatus
678:
679: function flip(vf,tf) {
680: var nw=eval('vf.f'+tf+'.selectedIndex');
681: var i;
682: // make sure no other columns are labeled the same as this one
683: for (i=0;i<=vf.nfields.value;i++) {
684: if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
685: eval('vf.f'+i+'.selectedIndex=0;')
686: }
687: }
688: // If we set this to 'lastname, firstnames', clear out all the ones
689: // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
690: if (nw==2) {
691: for (i=0;i<=vf.nfields.value;i++) {
692: if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
693: (eval('vf.f'+i+'.selectedIndex')<=6)) {
694: eval('vf.f'+i+'.selectedIndex=0;')
695: }
696: }
697: }
698: // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
699: // clear out any that are set to 'lastname, firstnames' (2)
700: if ((nw>=3) && (nw<=6)) {
701: for (i=0;i<=vf.nfields.value;i++) {
702: if (eval('vf.f'+i+'.selectedIndex')==2) {
703: eval('vf.f'+i+'.selectedIndex=0;')
704: }
705: }
706: }
707: $auth_update
708: }
709:
710: function clearpwd(vf) {
711: var i;
712: for (i=0;i<=vf.nfields.value;i++) {
713: if (eval('vf.f'+i+'.selectedIndex')==9) {
714: eval('vf.f'+i+'.selectedIndex=0;')
715: }
716: }
717: }
718:
719: ENDPICK
720: }
721:
722: ###############################################################
723: ###############################################################
724: sub upload_manager_javascript_reverse_associate {
725: my ($can_assign) = @_;
726: my ($auth_update,$numbuttons,$argreset);
727: if (ref($can_assign) eq 'HASH') {
728: if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
729: $argreset .= " vf.krbarg.value='';\n";
730: $numbuttons ++ ;
731: }
732: if ($can_assign->{'int'}) {
733: $argreset .= " vf.intarg.value='';\n";
734: $numbuttons ++;
735: }
736: if ($can_assign->{'loc'}) {
737: $argreset .= " vf.locarg.value='';\n";
738: $numbuttons ++;
739: }
740: if (!$can_assign->{'int'}) {
741: my $warning = &mt('You may not specify an initial password, as this is only available when new users use LON-CAPA internal authentication.\n').
742: &mt('Your current role does not have rights to create users with that authentication type.');
743: $auth_update = <<"END";
744: // Currently the initial password field is only supported for internal auth
745: // (see bug 6368).
746: if (tf==8 && nw!=0) {
747: eval('vf.f'+tf+'.selectedIndex=0;')
748: alert('$warning');
749: }
750: END
751: } elsif ($numbuttons > 1) {
752: $auth_update = <<"END";
753: // initial password specified, pick internal authentication
754: if (tf==8 && nw!=0) {
755: changed_radio('int',document.studentform);
756: set_auth_radio_buttons('int',document.studentform);
757: $argreset
758: }
759:
760: END
761: }
762: }
763:
764: return(<<ENDPICK);
765: function verify(vf,sec_caller) {
766: var founduname=0;
767: var foundpwd=0;
768: var foundname=0;
769: var foundid=0;
770: var foundsec=0;
771: var foundemail=0;
772: var foundrole=0;
773: var founddomain=0;
774: var foundinststatus=0;
775: var tw;
776: for (i=0;i<=vf.nfields.value;i++) {
777: tw=eval('vf.f'+i+'.selectedIndex');
778: if (i==0 && tw!=0) { founduname=1; }
779: if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
780: if (i==6 && tw!=0) { foundid=1; }
781: if (i==7 && tw!=0) { foundsec=1; }
782: if (i==8 && tw!=0) { foundpwd=1; }
783: if (i==9 && tw!=0) { foundemail=1; }
784: if (i==10 && tw!=0) { foundrole=1; }
785: if (i==11 && tw!=0) { founddomain=1; }
786: if (i==12 && tw!=0) { foundinstatus=1; }
787: }
788: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus);
789: }
790:
791: function flip(vf,tf) {
792: var nw=eval('vf.f'+tf+'.selectedIndex');
793: var i;
794: // picked the all one name field, reset the other name ones to blank
795: if (tf==1 && nw!=0) {
796: for (i=2;i<=5;i++) {
797: eval('vf.f'+i+'.selectedIndex=0;')
798: }
799: }
800: //picked one of the piecewise name fields, reset the all in
801: //one field to blank
802: if ((tf>=2) && (tf<=5) && (nw!=0)) {
803: eval('vf.f1.selectedIndex=0;')
804: }
805: $auth_update
806: }
807:
808: function clearpwd(vf) {
809: var i;
810: if (eval('vf.f8.selectedIndex')!=0) {
811: eval('vf.f8.selectedIndex=0;')
812: }
813: }
814: ENDPICK
815: }
816:
817: ###############################################################
818: ###############################################################
819: sub print_upload_manager_footer {
820: my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission,$crstype) = @_;
821: my $form = 'document.studentform';
822: my $formname = 'studentform';
823: my ($krbdef,$krbdefdom) =
824: &Apache::loncommon::get_kerberos_defaults($defdom);
825: my %param = ( formname => $form,
826: kerb_def_dom => $krbdefdom,
827: kerb_def_auth => $krbdef
828: );
829: if (exists($env{'form.ipwd_choice'}) &&
830: defined($env{'form.ipwd_choice'}) &&
831: $env{'form.ipwd_choice'} ne '') {
832: $param{'curr_authtype'} = 'int';
833: }
834: my $krbform = &Apache::loncommon::authform_kerberos(%param);
835: my $intform = &Apache::loncommon::authform_internal(%param);
836: my $locform = &Apache::loncommon::authform_local(%param);
837: my $date_table = &date_setting_table(undef,undef,$context,undef,
838: $formname,$permission,$crstype);
839:
840: my $Str = "\n".'<div class="LC_left_float">';
841: $Str .= &hidden_input('nfields',$i);
842: $Str .= &hidden_input('keyfields',$keyfields);
843:
844: $Str .= '<h3>'.&mt('Options').'</h3>'
845: .&Apache::lonhtmlcommon::start_pick_box();
846:
847: $Str .= &Apache::lonhtmlcommon::row_title(&mt('Login Type'));
848: if ($context eq 'domain') {
849: $Str .= '<p>'
850: .&mt('Change authentication for existing users in domain "[_1]" to these settings?'
851: ,$defdom)
852: .' <span class="LC_nobreak"><label>'
853: .'<input type="radio" name="changeauth" value="No" checked="checked" />'
854: .&mt('No').'</label>'
855: .' <label>'
856: .'<input type="radio" name="changeauth" value="Yes" />'
857: .&mt('Yes').'</label>'
858: .'</span></p>';
859: } else {
860: $Str .= '<p class="LC_info">'."\n".
861: &mt('This will not take effect if the user already exists.').
862: &Apache::loncommon::help_open_topic('Auth_Options').
863: "</p>\n";
864: }
865: $Str .= &set_login($defdom,$krbform,$intform,$locform);
866:
867: my ($home_server_pick,$numlib) =
868: &Apache::loncommon::home_server_form_item($defdom,'lcserver',
869: 'default','hide');
870: if ($numlib > 1) {
871: $Str .= &Apache::lonhtmlcommon::row_closure()
872: .&Apache::lonhtmlcommon::row_title(
873: &mt('LON-CAPA Home Server for New Users'))
874: .&mt('LON-CAPA domain: [_1] with home server:','"'.$defdom.'"')
875: .$home_server_pick
876: .&Apache::lonhtmlcommon::row_closure();
877: } else {
878: $Str .= $home_server_pick.
879: &Apache::lonhtmlcommon::row_closure();
880: }
881:
882: $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default domain'))
883: .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1)
884: .&Apache::lonhtmlcommon::row_closure();
885:
886: $Str .= &Apache::lonhtmlcommon::row_title(&mt('Starting and Ending Dates'))
887: ."<p>\n".$date_table."</p>\n"
888: .&Apache::lonhtmlcommon::row_closure();
889:
890: if ($context eq 'domain') {
891: $Str .= &Apache::lonhtmlcommon::row_title(
892: &mt('Settings for assigning roles'))
893: .&mt('Pick the action to take on roles for these users:').'<br />'
894: .'<span class="LC_nobreak"><label>'
895: .'<input type="radio" name="roleaction" value="norole" checked="checked" />'
896: .' '.&mt('No role changes').'</label>'
897: .' <label>'
898: .'<input type="radio" name="roleaction" value="domain" />'
899: .' '.&mt('Add a domain role').'</label>'
900: .' <label>'
901: .'<input type="radio" name="roleaction" value="course" />'
902: .' '.&mt('Add a course/community role').'</label>'
903: .'</span>';
904: } elsif ($context eq 'author') {
905: $Str .= &Apache::lonhtmlcommon::row_title(
906: &mt('Default role'))
907: .&mt('Choose the role to assign to users without a value specified in the uploaded file.')
908: } elsif ($context eq 'course') {
909: $Str .= &Apache::lonhtmlcommon::row_title(
910: &mt('Default role and section'))
911: .&mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file.');
912: } else {
913: $Str .= &Apache::lonhtmlcommon::row_title(
914: &mt('Default role and/or section(s)'))
915: .&mt('Role and/or section(s) for users without values specified in the uploaded file.');
916: }
917: if (($context eq 'domain') || ($context eq 'author')) {
918: $Str .= '<br />';
919: my ($options,$cb_script,$coursepick) = &default_role_selector($context,1);
920: if ($context eq 'domain') {
921: $Str .= '<p>'
922: .'<b>'.&mt('Domain Level').'</b><br />'
923: .$options
924: .'</p><p>'
925: .'<b>'.&mt('Course Level').'</b>'
926: .'</p>'
927: .$cb_script.$coursepick
928: .&Apache::lonhtmlcommon::row_closure();
929: } elsif ($context eq 'author') {
930: $Str .= $options
931: .&Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
932: }
933: } else {
934: my ($cnum,$cdom) = &get_course_identity();
935: my $rowtitle = &mt('section');
936: my $secbox = §ion_picker($cdom,$cnum,'Any',$rowtitle,
937: $permission,$context,'upload',$crstype);
938: $Str .= $secbox
939: .&Apache::lonhtmlcommon::row_closure();
940: my %lt;
941: if ($crstype eq 'Community') {
942: %lt = &Apache::lonlocal::texthash (
943: disp => 'Display members with current/future access who are not in the uploaded file',
944: stus => 'Members selected from this list can be dropped.'
945: );
946: } else {
947: %lt = &Apache::lonlocal::texthash (
948: disp => 'Display students with current/future access who are not in the uploaded file',
949: stus => 'Students selected from this list can be dropped.'
950: );
951: }
952: $Str .= &Apache::lonhtmlcommon::row_title(&mt('Full Update'))
953: .'<label><input type="checkbox" name="fullup" value="yes" />'
954: .' '.$lt{'disp'}
955: .'</label><br />'
956: .$lt{'stus'}
957: .&Apache::lonhtmlcommon::row_closure();
958: }
959: if ($context eq 'course' || $context eq 'domain') {
960: $Str .= &forceid_change($context);
961: }
962:
963: $Str .= &Apache::lonhtmlcommon::end_pick_box();
964: $Str .= '</div>';
965:
966: # Footer
967: $Str .= '<div class="LC_clear_float_footer">'
968: .'<hr />';
969: if ($context eq 'course') {
970: $Str .= '<p class="LC_info">'
971: .&mt('Note: This operation may be time consuming when adding several users.')
972: .'</p>';
973: }
974: $Str .= '<p><input type="button"'
975: .' onclick="javascript:verify(this.form,this.form.csec)"'
976: .' value="'.&mt('Update Users').'" />'
977: .'</p>'."\n"
978: .'</div>';
979: $r->print($Str);
980: return;
981: }
982:
983: sub forceid_change {
984: my ($context) = @_;
985: my $output =
986: &Apache::lonhtmlcommon::row_title(&mt('Student/Employee ID'))
987: .'<label><input type="checkbox" name="forceid" value="yes" />'
988: .&mt('Disable Student/Employee ID Safeguard and force change of conflicting IDs')
989: .'</label><br />'."\n"
990: .&mt('(only do if you know what you are doing.)')."\n";
991: if ($context eq 'domain') {
992: $output .= '<br /><label><input type="checkbox" name="recurseid"'.
993: ' value="yes" />'.
994: &mt('Update student/employee ID in courses in which user is active/future student,[_1](if forcing change).','<br />').
995: '</label>'."\n";
996: }
997: $output .= &Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
998: return $output;
999: }
1000:
1001: ###############################################################
1002: ###############################################################
1003: sub print_upload_manager_form {
1004: my ($r,$context,$permission,$crstype) = @_;
1005: my $firstLine;
1006: my $datatoken;
1007: if (!$env{'form.datatoken'}) {
1008: $datatoken=&Apache::loncommon::upfile_store($r);
1009: } else {
1010: $datatoken=$env{'form.datatoken'};
1011: &Apache::loncommon::load_tmp_file($r);
1012: }
1013: my @records=&Apache::loncommon::upfile_record_sep();
1014: if($env{'form.noFirstLine'}){
1015: $firstLine=shift(@records);
1016: }
1017: my $total=$#records;
1018: my $distotal=$total+1;
1019: my $today=time;
1020: my $halfyear=$today+15552000;
1021: #
1022: # Restore memorized settings
1023: my $col_setting_names = { 'username_choice' => 'scalar', # column settings
1024: 'names_choice' => 'scalar',
1025: 'fname_choice' => 'scalar',
1026: 'mname_choice' => 'scalar',
1027: 'lname_choice' => 'scalar',
1028: 'gen_choice' => 'scalar',
1029: 'id_choice' => 'scalar',
1030: 'sec_choice' => 'scalar',
1031: 'ipwd_choice' => 'scalar',
1032: 'email_choice' => 'scalar',
1033: 'role_choice' => 'scalar',
1034: 'domain_choice' => 'scalar',
1035: 'inststatus_choice' => 'scalar',
1036: };
1037: my $defdom = $env{'request.role.domain'};
1038: if ($context eq 'course') {
1039: &Apache::loncommon::restore_course_settings('enrollment_upload',
1040: $col_setting_names);
1041: } else {
1042: &Apache::loncommon::restore_settings($context,'user_upload',
1043: $col_setting_names);
1044: }
1045: #
1046: # Determine kerberos parameters as appropriate
1047: my ($krbdef,$krbdefdom) =
1048: &Apache::loncommon::get_kerberos_defaults($defdom);
1049: #
1050: my ($authnum,%can_assign) = &Apache::loncommon::get_assignable_auth($defdom);
1051: &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
1052: $permission,$crstype,\%can_assign);
1053: my $i;
1054: my $keyfields;
1055: if ($total>=0) {
1056: my @field=
1057: (['username',&mt('Username'), $env{'form.username_choice'}],
1058: ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
1059: ['fname',&mt('First Name'), $env{'form.fname_choice'}],
1060: ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
1061: ['lname',&mt('Last Name'), $env{'form.lname_choice'}],
1062: ['gen', &mt('Generation'), $env{'form.gen_choice'}],
1063: ['id', &mt('Student/Employee ID'),$env{'form.id_choice'}],
1064: ['sec', &mt('Section'), $env{'form.sec_choice'}],
1065: ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
1066: ['email',&mt('E-mail Address'), $env{'form.email_choice'}],
1067: ['role',&mt('Role'), $env{'form.role_choice'}],
1068: ['domain',&mt('Domain'), $env{'form.domain_choice'}],
1069: ['inststatus',&mt('Affiliation'), $env{'form.inststatus_choice'}]);
1070: if ($env{'form.upfile_associate'} eq 'reverse') {
1071: &Apache::loncommon::csv_print_samples($r,\@records);
1072: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
1073: \@field);
1074: foreach (@field) {
1075: $keyfields.=$_->[0].',';
1076: }
1077: chop($keyfields);
1078: } else {
1079: unshift(@field,['none','']);
1080: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
1081: \@field);
1082: my %sone=&Apache::loncommon::record_sep($records[0]);
1083: $keyfields=join(',',sort(keys(%sone)));
1084: }
1085: }
1086: $r->print('</div>');
1087: &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1088: $context,$permission,$crstype);
1089: }
1090:
1091: sub setup_date_selectors {
1092: my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
1093: if ($formname eq '') {
1094: $formname = 'studentform';
1095: }
1096: if (! defined($starttime)) {
1097: $starttime = time;
1098: unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
1099: if (exists($env{'course.'.$env{'request.course.id'}.
1100: '.default_enrollment_start_date'})) {
1101: $starttime = $env{'course.'.$env{'request.course.id'}.
1102: '.default_enrollment_start_date'};
1103: }
1104: }
1105: }
1106: if (! defined($endtime)) {
1107: $endtime = time+(6*30*24*60*60); # 6 months from now, approx
1108: unless ($mode eq 'createcourse') {
1109: if (exists($env{'course.'.$env{'request.course.id'}.
1110: '.default_enrollment_end_date'})) {
1111: $endtime = $env{'course.'.$env{'request.course.id'}.
1112: '.default_enrollment_end_date'};
1113: }
1114: }
1115: }
1116:
1117: my $startdateform =
1118: &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1119: undef,undef,undef,undef,undef,undef,undef,$nolink);
1120:
1121: my $enddateform =
1122: &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1123: undef,undef,undef,undef,undef,undef,undef,$nolink);
1124:
1125: if ($mode eq 'create_enrolldates') {
1126: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
1127: 'startenroll',
1128: $starttime);
1129: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
1130: 'endenroll',
1131: $endtime);
1132: }
1133: if ($mode eq 'create_defaultdates') {
1134: $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
1135: 'startaccess',
1136: $starttime);
1137: $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
1138: 'endaccess',
1139: $endtime);
1140: }
1141: return ($startdateform,$enddateform);
1142: }
1143:
1144:
1145: sub get_dates_from_form {
1146: my ($startname,$endname) = @_;
1147: if ($startname eq '') {
1148: $startname = 'startdate';
1149: }
1150: if ($endname eq '') {
1151: $endname = 'enddate';
1152: }
1153: my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
1154: my $enddate = &Apache::lonhtmlcommon::get_date_from_form($endname);
1155: if ($env{'form.no_end_date'}) {
1156: $enddate = 0;
1157: }
1158: return ($startdate,$enddate);
1159: }
1160:
1161: sub date_setting_table {
1162: my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission,$crstype) = @_;
1163: my $nolink;
1164: if ($bulkaction) {
1165: $nolink = 1;
1166: }
1167: my ($startform,$endform) =
1168: &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1169: my $dateDefault;
1170: if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
1171: $dateDefault = ' ';
1172: } elsif ($mode ne 'author' && $mode ne 'domain') {
1173: if (($bulkaction eq 'reenable') ||
1174: ($bulkaction eq 'activate') ||
1175: ($bulkaction eq 'chgdates') ||
1176: ($env{'form.action'} eq 'upload')) {
1177: if ($env{'request.course.sec'} eq '') {
1178: $dateDefault = '<span class="LC_nobreak">'.
1179: '<label><input type="checkbox" name="makedatesdefault" value="1" /> ';
1180: if ($crstype eq 'Community') {
1181: $dateDefault .= &mt("make these dates the default access dates for future community enrollment");
1182: } else {
1183: $dateDefault .= &mt("make these dates the default access dates for future course enrollment");
1184: }
1185: $dateDefault .= '</label></span>';
1186: }
1187: }
1188: }
1189: my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1190: if (defined($endtime) && $endtime == 0) {
1191: $perpetual .= ' checked="checked"';
1192: }
1193: $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1194: if ($mode eq 'create_enrolldates') {
1195: $perpetual = ' ';
1196: }
1197: my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
1198: $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
1199: 'LC_oddrow_value')."\n".
1200: $startform."\n".
1201: &Apache::lonhtmlcommon::row_closure(1).
1202: &Apache::lonhtmlcommon::row_title(&mt('Ending Date'),
1203: 'LC_oddrow_value')."\n".
1204: $endform.' '.$perpetual.
1205: &Apache::lonhtmlcommon::row_closure(1).
1206: &Apache::lonhtmlcommon::end_pick_box();
1207: if ($dateDefault) {
1208: $result .= $dateDefault.'<br />'."\n";
1209: }
1210: return $result;
1211: }
1212:
1213: sub make_dates_default {
1214: my ($startdate,$enddate,$context,$crstype) = @_;
1215: my $result = '';
1216: if ($context eq 'course') {
1217: my ($cnum,$cdom) = &get_course_identity();
1218: my $put_result = &Apache::lonnet::put('environment',
1219: {'default_enrollment_start_date'=>$startdate,
1220: 'default_enrollment_end_date' =>$enddate},$cdom,$cnum);
1221: if ($put_result eq 'ok') {
1222: if ($crstype eq 'Community') {
1223: $result .= &mt('Set default start and end access dates for community.');
1224: } else {
1225: $result .= &mt('Set default start and end access dates for course.');
1226: }
1227: $result .= '<br />'."\n";
1228: #
1229: # Refresh the course environment
1230: &Apache::lonnet::coursedescription($env{'request.course.id'},
1231: {'freshen_cache' => 1});
1232: } else {
1233: if ($crstype eq 'Community') {
1234: $result .= &mt('Unable to set default access dates for community');
1235: } else {
1236: $result .= &mt('Unable to set default access dates for course');
1237: }
1238: $result .= ':'.$put_result.'<br />';
1239: }
1240: }
1241: return $result;
1242: }
1243:
1244: sub default_role_selector {
1245: my ($context,$checkpriv,$crstype) = @_;
1246: my %customroles;
1247: my ($options,$coursepick,$cb_jscript);
1248: if ($context ne 'author') {
1249: %customroles = &my_custom_roles($crstype);
1250: }
1251:
1252: my %lt=&Apache::lonlocal::texthash(
1253: 'rol' => "Role",
1254: 'grs' => "Section",
1255: 'exs' => "Existing sections",
1256: 'new' => "New section",
1257: );
1258: $options = '<select name="defaultrole">'."\n".
1259: ' <option value="">'.&mt('Please select').'</option>'."\n";
1260: if ($context eq 'course') {
1261: $options .= &default_course_roles($context,$checkpriv,$crstype,%customroles);
1262: } elsif ($context eq 'author') {
1263: my @roles = &construction_space_roles($checkpriv);
1264: foreach my $role (@roles) {
1265: my $plrole=&Apache::lonnet::plaintext($role);
1266: $options .= ' <option value="'.$role.'">'.$plrole.'</option>'."\n";
1267: }
1268: } elsif ($context eq 'domain') {
1269: my @roles = &domain_roles($checkpriv);
1270: foreach my $role (@roles) {
1271: my $plrole=&Apache::lonnet::plaintext($role);
1272: $options .= ' <option value="'.$role.'">'.$plrole.'</option>';
1273: }
1274: my $courseform = &Apache::loncommon::selectcourse_link
1275: ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course/Community');
1276: $cb_jscript =
1277: &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform','courserole','Course/Community');
1278: $coursepick = &Apache::loncommon::start_data_table().
1279: &Apache::loncommon::start_data_table_header_row().
1280: '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
1281: '<th>'.$lt{'grs'}.'</th>'.
1282: &Apache::loncommon::end_data_table_header_row().
1283: &Apache::loncommon::start_data_table_row()."\n".
1284: '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc','','','','crstype'".')" /></td>'."\n".
1285: '<td><select name="courserole">'."\n".
1286: &default_course_roles($context,$checkpriv,'Course',%customroles)."\n".
1287: '</select></td><td>'.
1288: '<table class="LC_createuser">'.
1289: '<tr class="LC_section_row"><td valign"top">'.
1290: $lt{'exs'}.'<br /><select name="currsec">'.
1291: ' <option value=""><--'.&mt('Pick course first').
1292: '</select></td>'.
1293: '<td> </td>'.
1294: '<td valign="top">'.$lt{'new'}.'<br />'.
1295: '<input type="text" name="newsec" value="" size="5" />'.
1296: '<input type="hidden" name="groups" value="" />'.
1297: '<input type="hidden" name="sections" value="" />'.
1298: '<input type="hidden" name="origdom" value="'.
1299: $env{'request.role.domain'}.'" />'.
1300: '<input type="hidden" name="dccourse" value="" />'.
1301: '<input type="hidden" name="dcdomain" value="" />'.
1302: '<input type="hidden" name="crstype" value="" />'.
1303: '</td></tr></table></td>'.
1304: &Apache::loncommon::end_data_table_row().
1305: &Apache::loncommon::end_data_table()."\n";
1306: }
1307: $options .= '</select>';
1308: return ($options,$cb_jscript,$coursepick);
1309: }
1310:
1311: sub default_course_roles {
1312: my ($context,$checkpriv,$crstype,%customroles) = @_;
1313: my $output;
1314: my $custom = 1;
1315: my @roles = &course_roles($context,$checkpriv,$custom,lc($crstype));
1316: foreach my $role (@roles) {
1317: if ($role ne 'cr') {
1318: my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1319: $output .= ' <option value="'.$role.'">'.$plrole.'</option>';
1320: }
1321: }
1322: if (keys(%customroles) > 0) {
1323: if (grep(/^cr$/,@roles)) {
1324: foreach my $cust (sort(keys(%customroles))) {
1325: my $custrole='cr_'.$env{'user.domain'}.
1326: '_'.$env{'user.name'}.'_'.$cust;
1327: $output .= ' <option value="'.$custrole.'">'.$cust.'</option>';
1328: }
1329: }
1330: }
1331: return $output;
1332: }
1333:
1334: sub construction_space_roles {
1335: my ($checkpriv) = @_;
1336: my @allroles = &roles_by_context('author');
1337: my @roles;
1338: if ($checkpriv) {
1339: foreach my $role (@allroles) {
1340: if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) {
1341: push(@roles,$role);
1342: }
1343: }
1344: return @roles;
1345: } else {
1346: return @allroles;
1347: }
1348: }
1349:
1350: sub domain_roles {
1351: my ($checkpriv) = @_;
1352: my @allroles = &roles_by_context('domain');
1353: my @roles;
1354: if ($checkpriv) {
1355: foreach my $role (@allroles) {
1356: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
1357: push(@roles,$role);
1358: }
1359: }
1360: return @roles;
1361: } else {
1362: return @allroles;
1363: }
1364: }
1365:
1366: sub course_roles {
1367: my ($context,$checkpriv,$custom,$roletype) = @_;
1368: my $crstype;
1369: if ($roletype eq 'community') {
1370: $crstype = 'Community' ;
1371: } else {
1372: $crstype = 'Course';
1373: }
1374: my @allroles = &roles_by_context('course',$custom,$crstype);
1375: my @roles;
1376: if ($context eq 'domain') {
1377: @roles = @allroles;
1378: } elsif ($context eq 'course') {
1379: if ($env{'request.course.id'}) {
1380: if ($checkpriv) {
1381: foreach my $role (@allroles) {
1382: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
1383: push(@roles,$role);
1384: } else {
1385: if ((($role ne 'cc') && ($role ne 'co')) && ($env{'request.course.sec'} ne '')) {
1386: if (&Apache::lonnet::allowed('c'.$role,
1387: $env{'request.course.id'}.'/'.
1388: $env{'request.course.sec'})) {
1389: push(@roles,$role);
1390: }
1391: }
1392: }
1393: }
1394: } else {
1395: @roles = @allroles;
1396: }
1397: }
1398: }
1399: return @roles;
1400: }
1401:
1402: sub curr_role_permissions {
1403: my ($context,$setting,$checkpriv,$type) = @_;
1404: my $custom = 1;
1405: my @roles;
1406: if ($context eq 'author') {
1407: @roles = &construction_space_roles($checkpriv);
1408: } elsif ($context eq 'domain') {
1409: if ($setting eq 'course') {
1410: @roles = &course_roles($context,$checkpriv,$custom,$type);
1411: } else {
1412: @roles = &domain_roles($checkpriv);
1413: }
1414: } elsif ($context eq 'course') {
1415: @roles = &course_roles($context,$checkpriv,$custom,$type);
1416: }
1417: return @roles;
1418: }
1419:
1420: # ======================================================= Existing Custom Roles
1421:
1422: sub my_custom_roles {
1423: my ($crstype) = @_;
1424: my %returnhash=();
1425: my %rolehash=&Apache::lonnet::dump('roles');
1426: foreach my $key (keys(%rolehash)) {
1427: if ($key=~/^rolesdef\_(\w+)$/) {
1428: if ($crstype eq 'Community') {
1429: next if ($rolehash{$key} =~ /bre\&S/);
1430: }
1431: $returnhash{$1}=$1;
1432: }
1433: }
1434: return %returnhash;
1435: }
1436:
1437: sub print_userlist {
1438: my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
1439: $idlist,$idlist_titles) = @_;
1440: my $format = $env{'form.output'};
1441: if (! exists($env{'form.sortby'})) {
1442: $env{'form.sortby'} = 'username';
1443: }
1444: if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
1445: $env{'form.Status'} = 'Active';
1446: }
1447: my $status_select = &Apache::lonhtmlcommon::StatusOptions
1448: ($env{'form.Status'});
1449:
1450: if ($env{'form.showrole'} eq '') {
1451: if ($context eq 'course') {
1452: $env{'form.showrole'} = 'st';
1453: } else {
1454: $env{'form.showrole'} = 'Any';
1455: }
1456: }
1457: if (! defined($env{'form.output'}) ||
1458: $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
1459: $env{'form.output'} = 'html';
1460: }
1461:
1462: my @statuses;
1463: if ($env{'form.Status'} eq 'Any') {
1464: @statuses = ('previous','active','future');
1465: } elsif ($env{'form.Status'} eq 'Expired') {
1466: @statuses = ('previous');
1467: } elsif ($env{'form.Status'} eq 'Active') {
1468: @statuses = ('active');
1469: } elsif ($env{'form.Status'} eq 'Future') {
1470: @statuses = ('future');
1471: }
1472:
1473: # if ($context eq 'course') {
1474: # $r->print(&display_adv_courseroles());
1475: # }
1476: #
1477: # Interface output
1478: $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
1479: '<input type="hidden" name="action" value="'.
1480: $env{'form.action'}.'" />');
1481: $r->print("<p>\n");
1482: if ($env{'form.action'} ne 'modifystudent') {
1483: my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
1484: 'excel' => "Excel",
1485: 'html' => 'HTML');
1486: my $output_selector = '<select size="1" name="output" >';
1487: foreach my $outputformat ('html','csv','excel') {
1488: my $option = '<option value="'.$outputformat.'"';
1489: if ($outputformat eq $env{'form.output'}) {
1490: $option .= ' selected="selected"';
1491: }
1492: $option .='>'.$lt{$outputformat}.'</option>';
1493: $output_selector .= "\n".$option;
1494: }
1495: $output_selector .= '</select>';
1496: $r->print('<label><span class="LC_nobreak">'
1497: .&mt('Output Format: [_1]',$output_selector)
1498: .'</span></label>'.(' 'x3));
1499: }
1500: $r->print('<label><span class="LC_nobreak">'
1501: .&mt('User Status: [_1]',$status_select)
1502: .'</span></label>'.(' 'x3)."\n");
1503: my $roleselected = '';
1504: if ($env{'form.showrole'} eq 'Any') {
1505: $roleselected = ' selected="selected"';
1506: }
1507: my ($cnum,$cdom);
1508: $r->print(&role_filter($context));
1509: if ($context eq 'course') {
1510: ($cnum,$cdom) = &get_course_identity();
1511: $r->print(§ion_group_filter($cnum,$cdom));
1512: }
1513: if ($env{'form.phase'} eq '') {
1514: $r->print('<br /><br />'.&list_submit_button(&mt('Display List of Users')).
1515: "\n</p>\n".
1516: '<input type="hidden" name="phase" value="" /></form>');
1517: return;
1518: }
1519: if (!(($context eq 'domain') &&
1520: (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
1521: $r->print(
1522: "\n</p>\n"
1523: .'<p>'
1524: .&list_submit_button(&mt('Update Display'))
1525: ."</p>\n"
1526: );
1527: }
1528: my ($indexhash,$keylist) = &make_keylist_array();
1529: my (%userlist,%userinfo,$clearcoursepick);
1530: if (($context eq 'domain') &&
1531: ($env{'form.roletype'} eq 'course') ||
1532: ($env{'form.roletype'} eq 'community')) {
1533: my ($crstype,$numcodes,$title,$warning);
1534: if ($env{'form.roletype'} eq 'course') {
1535: $crstype = 'Course';
1536: $numcodes = $totcodes;
1537: $title = &mt('Select Courses');
1538: $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
1539: } elsif ($env{'form.roletype'} eq 'community') {
1540: $crstype = 'Community';
1541: $numcodes = 0;
1542: $title = &mt('Select Communities');
1543: $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
1544: }
1545: my $courseform =
1546: &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
1547: $codetitles,$idlist,$idlist_titles,$crstype);
1548: $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
1549: &Apache::lonhtmlcommon::start_pick_box()."\n".
1550: &Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
1551: $courseform."\n".
1552: &Apache::lonhtmlcommon::row_closure(1).
1553: &Apache::lonhtmlcommon::end_pick_box().'</p>'.
1554: '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
1555: &list_submit_button(&mt('Update Display')).
1556: "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
1557: $clearcoursepick = 0;
1558: if (($env{'form.origroletype'} ne '') &&
1559: ($env{'form.origroletype'} ne $env{'form.roletype'})) {
1560: $clearcoursepick = 1;
1561: }
1562: if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1563: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1564: }
1565: } else {
1566: $r->print('<hr />'.&mt('Searching').' ...<br /> <br />');
1567: }
1568: $r->rflush();
1569: if ($context eq 'course') {
1570: if (($env{'form.showrole'} eq 'st') || ($env{'form.showrole'} eq 'Any')) {
1571: my $classlist = &Apache::loncoursedata::get_classlist();
1572: if (ref($classlist) eq 'HASH') {
1573: %userlist = %{$classlist};
1574: }
1575: }
1576: if ($env{'form.showrole'} ne 'st') {
1577: my $showroles;
1578: if ($env{'form.showrole'} ne 'Any') {
1579: $showroles = [$env{'form.showrole'}];
1580: } else {
1581: $showroles = undef;
1582: }
1583: my $withsec = 1;
1584: my $hidepriv = 1;
1585: my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
1586: \@statuses,$showroles,undef,$withsec,$hidepriv);
1587: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1588: \%advrolehash,$permission);
1589: }
1590: } else {
1591: my (%cstr_roles,%dom_roles);
1592: if ($context eq 'author') {
1593: # List co-authors and assistant co-authors
1594: my @possroles = &roles_by_context($context);
1595: %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
1596: \@statuses,\@possroles);
1597: &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1598: \%cstr_roles,$permission);
1599: } elsif ($context eq 'domain') {
1600: if ($env{'form.roletype'} eq 'domain') {
1601: %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
1602: foreach my $key (keys(%dom_roles)) {
1603: if (ref($dom_roles{$key}) eq 'HASH') {
1604: &gather_userinfo($context,$format,\%userlist,$indexhash,
1605: \%userinfo,$dom_roles{$key},$permission);
1606: }
1607: }
1608: } elsif ($env{'form.roletype'} eq 'author') {
1609: my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
1610: my %coauthors;
1611: foreach my $key (keys(%dom_roles)) {
1612: if (ref($dom_roles{$key}) eq 'HASH') {
1613: if ($env{'form.showrole'} eq 'au') {
1614: &gather_userinfo($context,$format,\%userlist,$indexhash,
1615: \%userinfo,$dom_roles{$key},$permission);
1616: } else {
1617: my @possroles;
1618: if ($env{'form.showrole'} eq 'Any') {
1619: @possroles = &roles_by_context('author');
1620: } else {
1621: @possroles = ($env{'form.showrole'});
1622: }
1623: foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1624: my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1625: my $extent = '/'.$authordom.'/'.$authorname;
1626: %{$coauthors{$extent}} =
1627: &Apache::lonnet::get_my_roles($authorname,
1628: $authordom,undef,\@statuses,\@possroles);
1629: }
1630: &gather_userinfo($context,$format,\%userlist,
1631: $indexhash,\%userinfo,\%coauthors,$permission);
1632: }
1633: }
1634: }
1635: } elsif (($env{'form.roletype'} eq 'course') ||
1636: ($env{'form.roletype'} eq 'community')) {
1637: if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1638: my %courses = &process_coursepick();
1639: my %allusers;
1640: my $hidepriv = 1;
1641: foreach my $cid (keys(%courses)) {
1642: my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1643: next if ($cnum eq '' || $cdom eq '');
1644: my $custom = 1;
1645: my (@roles,@sections,%access,%users,%userdata,
1646: %statushash);
1647: if ($env{'form.showrole'} eq 'Any') {
1648: @roles = &course_roles($context,undef,$custom,
1649: $env{'form.roletype'});
1650: } else {
1651: @roles = ($env{'form.showrole'});
1652: }
1653: foreach my $role (@roles) {
1654: %{$users{$role}} = ();
1655: }
1656: foreach my $type (@statuses) {
1657: $access{$type} = $type;
1658: }
1659: &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
1660: foreach my $user (keys(%userdata)) {
1661: next if (ref($userinfo{$user}) eq 'HASH');
1662: foreach my $item ('fullname','id') {
1663: $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
1664: }
1665: }
1666: foreach my $role (keys(%users)) {
1667: foreach my $user (keys(%{$users{$role}})) {
1668: my $uniqid = $user.':'.$role;
1669: $allusers{$uniqid}{$cid} = { desc => $cdesc,
1670: secs => $statushash{$user}{$role},
1671: };
1672: }
1673: }
1674: }
1675: &gather_userinfo($context,$format,\%userlist,$indexhash,
1676: \%userinfo,\%allusers,$permission);
1677: } else {
1678: $r->print('<input type="hidden" name="phase" value="'.
1679: $env{'form.phase'}.'" /></form>');
1680: return;
1681: }
1682: }
1683: }
1684: }
1685: if (keys(%userlist) == 0) {
1686: if ($context eq 'author') {
1687: $r->print(&mt('There are no co-authors to display.')."\n");
1688: } elsif ($context eq 'domain') {
1689: if ($env{'form.roletype'} eq 'domain') {
1690: $r->print(&mt('There are no users with domain roles to display.')."\n");
1691: } elsif ($env{'form.roletype'} eq 'author') {
1692: $r->print(&mt('There are no authors or co-authors to display.')."\n");
1693: } elsif ($env{'form.roletype'} eq 'course') {
1694: $r->print(&mt('There are no course users to display')."\n");
1695: } elsif ($env{'form.roletype'} eq 'community') {
1696: $r->print(&mt('There are no community users to display')."\n");
1697: }
1698: } elsif ($context eq 'course') {
1699: $r->print(&mt('There are no course users to display.')."\n");
1700: }
1701: } else {
1702: # Print out the available choices
1703: my $usercount;
1704: if ($env{'form.action'} eq 'modifystudent') {
1705: ($usercount) = &show_users_list($r,$context,'view',$permission,
1706: $env{'form.Status'},\%userlist,$keylist);
1707: } else {
1708: ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1709: $permission,$env{'form.Status'},\%userlist,$keylist);
1710: }
1711: if (!$usercount) {
1712: $r->print('<br /><span class="LC_warning">'
1713: .&mt('There are no users matching the search criteria.')
1714: .'</span>'
1715: );
1716: }
1717: }
1718: $r->print('<input type="hidden" name="phase" value="'.
1719: $env{'form.phase'}.'" /></form>');
1720: }
1721:
1722: sub role_filter {
1723: my ($context) = @_;
1724: my $output;
1725: my $roleselected = '';
1726: if ($env{'form.showrole'} eq 'Any') {
1727: $roleselected = ' selected="selected"';
1728: }
1729: my ($role_select);
1730: if ($context eq 'domain') {
1731: $role_select = &domain_roles_select();
1732: $output = '<label><span class="LC_nobreak">'
1733: .&mt('Role Type: [_1]',$role_select)
1734: .'</span></label>';
1735: } else {
1736: $role_select = '<select name="showrole">'."\n".
1737: '<option value="Any" '.$roleselected.'>'.
1738: &mt('Any role').'</option>';
1739: my ($roletype,$crstype);
1740: if ($context eq 'course') {
1741: $crstype = &Apache::loncommon::course_type();
1742: if ($crstype eq 'Community') {
1743: $roletype = 'community';
1744: } else {
1745: $roletype = 'course';
1746: }
1747: }
1748: my @poss_roles = &curr_role_permissions($context,'','',$roletype);
1749: foreach my $role (@poss_roles) {
1750: $roleselected = '';
1751: if ($role eq $env{'form.showrole'}) {
1752: $roleselected = ' selected="selected"';
1753: }
1754: my $plrole;
1755: if ($role eq 'cr') {
1756: $plrole = &mt('Custom role');
1757: } else {
1758: $plrole=&Apache::lonnet::plaintext($role,$crstype);
1759: }
1760: $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
1761: }
1762: $role_select .= '</select>';
1763: $output = '<label><span class="LC_nobreak">'
1764: .&mt('Role: [_1]',$role_select)
1765: .'</span></label>';
1766: }
1767: return $output;
1768: }
1769:
1770: sub section_group_filter {
1771: my ($cnum,$cdom) = @_;
1772: my @filters;
1773: if ($env{'request.course.sec'} eq '') {
1774: @filters = ('sec');
1775: }
1776: push(@filters,'grp');
1777: my %name = (
1778: sec => 'secfilter',
1779: grp => 'grpfilter',
1780: );
1781: my %title = &Apache::lonlocal::texthash (
1782: sec => 'Section(s)',
1783: grp => 'Group(s)',
1784: all => 'all',
1785: none => 'none',
1786: );
1787: my $output;
1788: foreach my $item (@filters) {
1789: my ($markup,@options);
1790: if ($env{'form.'.$name{$item}} eq '') {
1791: $env{'form.'.$name{$item}} = 'all';
1792: }
1793: if ($item eq 'sec') {
1794: if (($env{'form.showrole'} eq 'cc') || ($env{'form.showrole'} eq 'co')) {
1795: $env{'form.'.$name{$item}} = 'none';
1796: }
1797: my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
1798: @options = sort(keys(%sections_count));
1799: } elsif ($item eq 'grp') {
1800: my %curr_groups = &Apache::longroup::coursegroups();
1801: @options = sort(keys(%curr_groups));
1802: }
1803: if (@options > 0) {
1804: my $currsel;
1805: $markup = '<select name="'.$name{$item}.'">'."\n";
1806: foreach my $option ('all','none',@options) {
1807: $currsel = '';
1808: if ($env{'form.'.$name{$item}} eq $option) {
1809: $currsel = ' selected="selected"';
1810: }
1811: $markup .= ' <option value="'.$option.'"'.$currsel.'>';
1812: if (($option eq 'all') || ($option eq 'none')) {
1813: $markup .= $title{$option};
1814: } else {
1815: $markup .= $option;
1816: }
1817: $markup .= '</option>'."\n";
1818: }
1819: $markup .= '</select>'."\n";
1820: $output .= (' 'x3).'<span class="LC_nobreak">'
1821: .'<label>'.$title{$item}.': '.$markup.'</label>'
1822: .'</span> ';
1823: }
1824: }
1825: return $output;
1826: }
1827:
1828: sub list_submit_button {
1829: my ($text) = @_;
1830: return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1831: }
1832:
1833: sub gather_userinfo {
1834: my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1835: my $viewablesec;
1836: if ($context eq 'course') {
1837: $viewablesec = &viewable_section($permission);
1838: }
1839: foreach my $item (keys(%{$rolehash})) {
1840: my %userdata;
1841: if ($context eq 'author') {
1842: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1843: split(/:/,$item);
1844: ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1845: &build_user_record($context,\%userdata,$userinfo,$indexhash,
1846: $item,$userlist);
1847: } elsif ($context eq 'course') {
1848: ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
1849: $userdata{'section'}) = split(/:/,$item,-1);
1850: ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1851: if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
1852: next if ($viewablesec ne $userdata{'section'});
1853: }
1854: &build_user_record($context,\%userdata,$userinfo,$indexhash,
1855: $item,$userlist);
1856: } elsif ($context eq 'domain') {
1857: if ($env{'form.roletype'} eq 'domain') {
1858: ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
1859: split(/:/,$item);
1860: ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1861: &build_user_record($context,\%userdata,$userinfo,$indexhash,
1862: $item,$userlist);
1863: } elsif ($env{'form.roletype'} eq 'author') {
1864: if (ref($rolehash->{$item}) eq 'HASH') {
1865: $userdata{'extent'} = $item;
1866: foreach my $key (keys(%{$rolehash->{$item}})) {
1867: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) = split(/:/,$key);
1868: ($userdata{'start'},$userdata{'end'}) =
1869: split(/:/,$rolehash->{$item}{$key});
1870: my $uniqid = $key.':'.$item;
1871: &build_user_record($context,\%userdata,$userinfo,
1872: $indexhash,$uniqid,$userlist);
1873: }
1874: }
1875: } elsif (($env{'form.roletype'} eq 'course') ||
1876: ($env{'form.roletype'} eq 'community')) {
1877: ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
1878: split(/:/,$item);
1879: if (ref($rolehash->{$item}) eq 'HASH') {
1880: my $numcids = keys(%{$rolehash->{$item}});
1881: foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
1882: if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
1883: my $spanstart = '';
1884: my $spanend = '; ';
1885: my $space = ', ';
1886: if ($format eq 'html' || $format eq 'view') {
1887: $spanstart = '<span class="LC_nobreak">';
1888: # FIXME: actions on courses disabled for now
1889: # if ($permission->{'cusr'}) {
1890: # if ($numcids > 1) {
1891: # $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" /> ';
1892: # } else {
1893: # $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" /> ';
1894: # }
1895: # }
1896: $spanend = '</span><br />';
1897: $space = ', ';
1898: }
1899: $userdata{'extent'} .= $spanstart.
1900: $rolehash->{$item}{$cid}{'desc'}.$space;
1901: if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') {
1902: foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1903: if (($env{'form.Status'} eq 'Any') ||
1904: ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
1905: $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
1906: $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
1907: }
1908: }
1909: }
1910: }
1911: }
1912: }
1913: if ($userdata{'status'} ne '') {
1914: &build_user_record($context,\%userdata,$userinfo,
1915: $indexhash,$item,$userlist);
1916: }
1917: }
1918: }
1919: }
1920: return;
1921: }
1922:
1923: sub build_user_record {
1924: my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1925: next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1926: if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
1927: && ($env{'form.roletype'} eq 'community')))) {
1928: &process_date_info($userdata);
1929: }
1930: my $username = $userdata->{'username'};
1931: my $domain = $userdata->{'domain'};
1932: if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1933: $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1934: $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
1935: } else {
1936: &aggregate_user_info($domain,$username,$userinfo);
1937: $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1938: $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
1939: }
1940: foreach my $key (keys(%{$indexhash})) {
1941: if (defined($userdata->{$key})) {
1942: $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
1943: }
1944: }
1945: return;
1946: }
1947:
1948: sub courses_selector {
1949: my ($cdom,$formname) = @_;
1950: my %coursecodes = ();
1951: my %codes = ();
1952: my @codetitles = ();
1953: my %cat_titles = ();
1954: my %cat_order = ();
1955: my %idlist = ();
1956: my %idnums = ();
1957: my %idlist_titles = ();
1958: my $caller = 'global';
1959: my $format_reply;
1960: my $jscript = '';
1961:
1962: my $totcodes = 0;
1963: $totcodes =
1964: &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
1965: $cdom,$totcodes);
1966: if ($totcodes > 0) {
1967: $format_reply =
1968: &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
1969: \%codes,\@codetitles,\%cat_titles,\%cat_order);
1970: if ($format_reply eq 'ok') {
1971: my $numtypes = @codetitles;
1972: &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
1973: my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
1974: my $longtitles_str = join('","',@{$longtitles});
1975: my $allidlist = $idlist{$codetitles[0]};
1976: $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
1977: $jscript .= $scripttext;
1978: $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
1979: }
1980: }
1981: my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
1982:
1983: my %elements = (
1984: Year => 'selectbox',
1985: coursepick => 'radio',
1986: coursetotal => 'text',
1987: courselist => 'text',
1988: );
1989: $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
1990: if ($env{'form.coursepick'} eq 'category') {
1991: $jscript .= qq|
1992: function setCourseCat(formname) {
1993: if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
1994: return;
1995: }
1996: courseSet('Year');
1997: for (var j=0; j<formname.Semester.length; j++) {
1998: if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
1999: formname.Semester.options[j].selected = true;
2000: }
2001: }
2002: if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
2003: return;
2004: }
2005: courseSet('Semester');
2006: for (var j=0; j<formname.Department.length; j++) {
2007: if (formname.Department.options[j].value == "$env{'form.Department'}") { formname.Department.options[j].selected = true;
2008: }
2009: }
2010: if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
2011: return;
2012: }
2013: courseSet('Department');
2014: for (var j=0; j<formname.Number.length; j++) {
2015: if (formname.Number.options[j].value == "$env{'form.Number'}") {
2016: formname.Number.options[j].selected = true;
2017: }
2018: }
2019: }
2020: |;
2021: }
2022: return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
2023: \%idlist_titles);
2024: }
2025:
2026: sub course_selector_loadcode {
2027: my ($formname) = @_;
2028: my $loadcode;
2029: if ($env{'form.coursepick'} ne '') {
2030: $loadcode = 'javascript:setFormElements(document.'.$formname.')';
2031: if ($env{'form.coursepick'} eq 'category') {
2032: $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
2033: }
2034: }
2035: return $loadcode;
2036: }
2037:
2038: sub process_coursepick {
2039: my $coursefilter = $env{'form.coursepick'};
2040: my $cdom = $env{'request.role.domain'};
2041: my %courses;
2042: my $crssrch = 'Course';
2043: if ($env{'form.roletype'} eq 'community') {
2044: $crssrch = 'Community';
2045: }
2046: if ($coursefilter eq 'all') {
2047: %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
2048: undef,undef,$crssrch);
2049: } elsif ($coursefilter eq 'category') {
2050: my $instcode = &instcode_from_coursefilter();
2051: %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
2052: undef,undef,$crssrch);
2053: } elsif ($coursefilter eq 'specific') {
2054: if ($env{'form.coursetotal'} > 1) {
2055: my @course_ids = split(/&&/,$env{'form.courselist'});
2056: foreach my $cid (@course_ids) {
2057: $courses{$cid} = '';
2058: }
2059: } else {
2060: $courses{$env{'form.courselist'}} = '';
2061: }
2062: }
2063: return %courses;
2064: }
2065:
2066: sub instcode_from_coursefilter {
2067: my $instcode = '';
2068: my @cats = ('Semester','Year','Department','Number');
2069: foreach my $category (@cats) {
2070: if (defined($env{'form.'.$category})) {
2071: unless ($env{'form.'.$category} eq '-1') {
2072: $instcode .= $env{'form.'.$category};
2073: }
2074: }
2075: }
2076: if ($instcode eq '') {
2077: $instcode = '.';
2078: }
2079: return $instcode;
2080: }
2081:
2082: sub display_adv_courseroles {
2083: my $output;
2084: #
2085: # List course personnel
2086: my %coursepersonnel =
2087: &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
2088: #
2089: $output = '<br />'.&Apache::loncommon::start_data_table();
2090: foreach my $role (sort(keys(%coursepersonnel))) {
2091: next if ($role =~ /^\s*$/);
2092: $output .= &Apache::loncommon::start_data_table_row().
2093: '<td>'.$role.'</td><td>';
2094: foreach my $user (split(',',$coursepersonnel{$role})) {
2095: my ($puname,$pudom)=split(':',$user);
2096: $output .= ' '.&Apache::loncommon::aboutmewrapper(
2097: &Apache::loncommon::plainname($puname,$pudom),
2098: $puname,$pudom);
2099: }
2100: $output .= '</td>'.&Apache::loncommon::end_data_table_row();
2101: }
2102: $output .= &Apache::loncommon::end_data_table();
2103: }
2104:
2105: sub make_keylist_array {
2106: my ($index,$keylist);
2107: $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
2108: $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
2109: $index->{'end'} = &Apache::loncoursedata::CL_END();
2110: $index->{'start'} = &Apache::loncoursedata::CL_START();
2111: $index->{'id'} = &Apache::loncoursedata::CL_ID();
2112: $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
2113: $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
2114: $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
2115: $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
2116: $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
2117: $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
2118: $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
2119: $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
2120: $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
2121: $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
2122: $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
2123: foreach my $key (keys(%{$index})) {
2124: $keylist->[$index->{$key}] = $key;
2125: }
2126: return ($index,$keylist);
2127: }
2128:
2129: sub aggregate_user_info {
2130: my ($udom,$uname,$userinfo) = @_;
2131: my %info=&Apache::lonnet::get('environment',
2132: ['firstname','middlename',
2133: 'lastname','generation','id'],
2134: $udom,$uname);
2135: my ($tmp) = keys(%info);
2136: my ($fullname,$id);
2137: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
2138: $fullname = 'not available';
2139: $id = 'not available';
2140: &Apache::lonnet::logthis('unable to retrieve environment '.
2141: 'for '.$uname.':'.$udom);
2142: } else {
2143: $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
2144: $id = $info{'id'};
2145: }
2146: $userinfo->{$uname.':'.$udom} = {
2147: fullname => $fullname,
2148: id => $id,
2149: };
2150: return;
2151: }
2152:
2153: sub process_date_info {
2154: my ($userdata) = @_;
2155: my $now = time;
2156: $userdata->{'status'} = 'Active';
2157: if ($userdata->{'start'} > 0) {
2158: if ($now < $userdata->{'start'}) {
2159: $userdata->{'status'} = 'Future';
2160: }
2161: }
2162: if ($userdata->{'end'} > 0) {
2163: if ($now > $userdata->{'end'}) {
2164: $userdata->{'status'} = 'Expired';
2165: }
2166: }
2167: return;
2168: }
2169:
2170: sub show_users_list {
2171: my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname)=@_;
2172: if ($formname eq '') {
2173: $formname = 'studentform';
2174: }
2175: #
2176: # Variables for excel output
2177: my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
2178: #
2179: # Variables for csv output
2180: my ($CSVfile,$CSVfilename);
2181: #
2182: my $sortby = $env{'form.sortby'};
2183: my @sortable = ('username','domain','id','fullname','start','end','email','role');
2184: if ($context eq 'course') {
2185: push(@sortable,('section','groups','type'));
2186: } else {
2187: push(@sortable,'extent');
2188: }
2189: if ($mode eq 'pickauthor') {
2190: @sortable = ('username','fullname','email','status');
2191: }
2192: if (!grep(/^\Q$sortby\E$/,@sortable)) {
2193: $sortby = 'username';
2194: }
2195: my $setting = $env{'form.roletype'};
2196: my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers,$crstype);
2197: if ($context eq 'course') {
2198: $cid = $env{'request.course.id'};
2199: $crstype = &Apache::loncommon::course_type();
2200: ($cnum,$cdom) = &get_course_identity($cid);
2201: ($classgroups) = &Apache::loncoursedata::get_group_memberships(
2202: $userlist,$keylist,$cdom,$cnum);
2203: if ($mode eq 'autoenroll') {
2204: $env{'form.showrole'} = 'st';
2205: } else {
2206: if (! exists($env{'form.displayphotos'})) {
2207: $env{'form.displayphotos'} = 'off';
2208: }
2209: $displayphotos = $env{'form.displayphotos'};
2210: if (! exists($env{'form.displayclickers'})) {
2211: $env{'form.displayclickers'} = 'off';
2212: }
2213: $displayclickers = $env{'form.displayclickers'};
2214: if ($env{'course.'.$cid.'.internal.showphoto'}) {
2215: $r->print('
2216: <script type="text/javascript">
2217: // <![CDATA[
2218: function photowindow(photolink) {
2219: var title = "Photo_Viewer";
2220: var options = "scrollbars=1,resizable=1,menubar=0";
2221: options += ",width=240,height=240";
2222: stdeditbrowser = open(photolink,title,options,"1");
2223: stdeditbrowser.focus();
2224: }
2225: // ]]>
2226: </script>
2227: ');
2228: }
2229: $r->print(<<END);
2230: <input type="hidden" name="displayphotos" value="$displayphotos" />
2231: <input type="hidden" name="displayclickers" value="$displayclickers" />
2232: END
2233: }
2234: } elsif ($context eq 'domain') {
2235: if ($setting eq 'community') {
2236: $crstype = 'Community';
2237: } elsif ($setting eq 'course') {
2238: $crstype = 'Course';
2239: }
2240: }
2241: if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
2242: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
2243: my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
2244: my $verify_action_js = &bulkaction_javascript($formname);
2245: $r->print(<<END);
2246:
2247: <script type="text/javascript" language="Javascript">
2248: // <![CDATA[
2249: $check_uncheck_js
2250:
2251: $verify_action_js
2252:
2253: function username_display_launch(username,domain) {
2254: var target;
2255: for (var i=0; i<document.$formname.usernamelink.length; i++) {
2256: if (document.$formname.usernamelink[i].checked) {
2257: target = document.$formname.usernamelink[i].value;
2258: }
2259: }
2260: if (target == 'modify') {
2261: if (document.$formname.userwin.checked == true) {
2262: var url = '/adm/createuser?srchterm='+username+'&srchdomain='+domain+'&phase=get_user_info&action=singleuser&srchin=dom&srchby=uname&srchtype=exact&popup=1';
2263: var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
2264: modifywin = window.open(url,'',options,1);
2265: modifywin.focus();
2266: return;
2267: } else {
2268: document.$formname.srchterm.value=username;
2269: document.$formname.srchdomain.value=domain;
2270: document.$formname.phase.value='get_user_info';
2271: document.$formname.action.value = 'singleuser';
2272: document.$formname.submit();
2273: }
2274: }
2275: if (target == 'aboutme') {
2276: if (document.$formname.userwin.checked == true) {
2277: var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
2278: var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
2279: aboutmewin = window.open(url,'',options,1);
2280: aboutmewin.focus();
2281: return;
2282: } else {
2283: document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
2284: }
2285: }
2286: if (target == 'track') {
2287: if (document.$formname.userwin.checked == true) {
2288: var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
2289: var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
2290: var trackwin = window.open(url,'',options,1);
2291: trackwin.focus();
2292: return;
2293: } else {
2294: document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
2295: }
2296: }
2297: }
2298: // ]]>
2299: </script>
2300: $date_sec_selector
2301: <input type="hidden" name="state" value="$env{'form.state'}" />
2302: END
2303: }
2304: $r->print(<<END);
2305: <input type="hidden" name="sortby" value="$sortby" />
2306: END
2307:
2308: my %lt=&Apache::lonlocal::texthash(
2309: 'username' => "username",
2310: 'domain' => "domain",
2311: 'id' => 'ID',
2312: 'fullname' => "name",
2313: 'section' => "section",
2314: 'groups' => "active groups",
2315: 'start' => "start date",
2316: 'end' => "end date",
2317: 'status' => "status",
2318: 'role' => "role",
2319: 'type' => "enroll type/action",
2320: 'email' => "e-mail address",
2321: 'photo' => "photo",
2322: 'extent' => "extent",
2323: 'pr' => "Proceed",
2324: 'ca' => "check all",
2325: 'ua' => "uncheck all",
2326: 'ac' => "Action to take for selected users",
2327: 'link' => "Behavior of clickable username link for each user",
2328: 'aboutme' => "Display a user's personal information page",
2329: 'owin' => "Open in a new window",
2330: 'modify' => "Modify a user's information",
2331: 'track' => "View a user's recent activity",
2332: 'clicker' => "Clicker-ID",
2333: );
2334: if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
2335: $lt{'extent'} = &mt('Course(s): description, section(s), status');
2336: } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
2337: $lt{'extent'} = &mt('Communities: description, section(s), status');
2338: } elsif ($context eq 'author') {
2339: $lt{'extent'} = &mt('Author');
2340: }
2341: my @cols;
2342: if ($mode eq 'pickauthor') {
2343: @cols = ('username','fullname','status','email');
2344: } else {
2345: @cols = ('username','domain','id','fullname');
2346: if ($context eq 'course') {
2347: push(@cols,'section');
2348: }
2349: if (!($context eq 'domain' && ($env{'form.roletype'} eq 'course')
2350: && ($env{'form.roletype'} eq 'community'))) {
2351: push(@cols,('start','end'));
2352: }
2353: if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
2354: push(@cols,'role');
2355: }
2356: if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
2357: $env{'form.roletype'} eq 'course' ||
2358: $env{'form.roletype'} eq 'community')) {
2359: push (@cols,'extent');
2360: }
2361: if (($statusmode eq 'Any') &&
2362: (!($context eq 'domain' && (($env{'form.roletype'} eq 'course')
2363: || ($env{'form.roletype'} eq 'community'))))) {
2364: push(@cols,'status');
2365: }
2366: if ($context eq 'course') {
2367: push(@cols,'groups');
2368: }
2369: push(@cols,'email');
2370: }
2371:
2372: my $rolefilter = $env{'form.showrole'};
2373: if ($env{'form.showrole'} eq 'cr') {
2374: $rolefilter = &mt('custom');
2375: } elsif ($env{'form.showrole'} ne 'Any') {
2376: $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
2377: }
2378: my $results_description;
2379: if ($mode ne 'autoenroll') {
2380: $results_description = &results_header_row($rolefilter,$statusmode,
2381: $context,$permission,$mode,$crstype);
2382:
2383: $r->print('<b>'.$results_description.'</b><br /><br />');
2384: }
2385: my ($output,$actionselect,%canchange,%canchangesec);
2386: if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
2387: if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
2388: if ($permission->{'cusr'}) {
2389: unless (($context eq 'domain') &&
2390: (($setting eq 'course') || ($setting eq 'community'))) {
2391: $actionselect =
2392: &select_actions($context,$setting,$statusmode,$formname);
2393: }
2394: }
2395: $r->print(<<END);
2396: <input type="hidden" name="srchby" value="uname" />
2397: <input type="hidden" name="srchin" value="dom" />
2398: <input type="hidden" name="srchtype" value="exact" />
2399: <input type="hidden" name="srchterm" value="" />
2400: <input type="hidden" name="srchdomain" value="" />
2401: END
2402: if ($actionselect) {
2403: $output .= <<"END";
2404: <div class="LC_left_float"><fieldset><legend><b>$lt{'ac'}</b></legend>
2405: $actionselect
2406: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" />
2407: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.$formname.actionlist)" /><br /><input type="button" value="$lt{'pr'}" onclick="javascript:verify_action('actionlist')" /></fieldset></div>
2408: END
2409: my @allroles;
2410: if ($env{'form.showrole'} eq 'Any') {
2411: my $custom = 1;
2412: if ($context eq 'domain') {
2413: @allroles = &roles_by_context($setting,$custom,$crstype);
2414: } else {
2415: @allroles = &roles_by_context($context,$custom,$crstype);
2416: }
2417: } else {
2418: @allroles = ($env{'form.showrole'});
2419: }
2420: foreach my $role (@allroles) {
2421: if ($context eq 'domain') {
2422: if ($setting eq 'domain') {
2423: if (&Apache::lonnet::allowed('c'.$role,
2424: $env{'request.role.domain'})) {
2425: $canchange{$role} = 1;
2426: }
2427: } elsif ($setting eq 'author') {
2428: if (&Apache::lonnet::allowed('c'.$role,
2429: $env{'request.role.domain'})) {
2430: $canchange{$role} = 1;
2431: }
2432: }
2433: } elsif ($context eq 'author') {
2434: if (&Apache::lonnet::allowed('c'.$role,
2435: $env{'user.domain'}.'/'.$env{'user.name'})) {
2436: $canchange{$role} = 1;
2437: }
2438: } elsif ($context eq 'course') {
2439: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
2440: $canchange{$role} = 1;
2441: } elsif ($env{'request.course.sec'} ne '') {
2442: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
2443: $canchangesec{$role} = $env{'request.course.sec'};
2444: }
2445: }
2446: }
2447: }
2448: }
2449: $output .= '<div class="LC_left_float"><fieldset><legend><b>'.$lt{'link'}.'</b></legend>'.
2450: '<table><tr>';
2451: my @linkdests = ('aboutme');
2452: if ($permission->{'cusr'}) {
2453: unshift (@linkdests,'modify');
2454: }
2455: if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
2456: &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
2457: $env{'request.course.sec'})) {
2458: push(@linkdests,'track');
2459: }
2460: $output .= '<td>';
2461: my $usernamelink = $env{'form.usernamelink'};
2462: if ($usernamelink eq '') {
2463: $usernamelink = 'aboutme';
2464: }
2465: foreach my $item (@linkdests) {
2466: my $checkedstr = '';
2467: if ($item eq $usernamelink) {
2468: $checkedstr = ' checked="checked"';
2469: }
2470: $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' /> '.$lt{$item}.'</label></span><br />';
2471: }
2472: my $checkwin;
2473: if ($env{'form.userwin'}) {
2474: $checkwin = ' checked="checked"';
2475: }
2476: $output .= '</td><td valign="top" style="border-left: 1px solid;><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
2477: }
2478: $output .= "\n".'<div class="LC_clear_float_footer"> </div>'."\n".
2479: &Apache::loncommon::start_data_table().
2480: &Apache::loncommon::start_data_table_header_row();
2481: if ($mode eq 'autoenroll') {
2482: $output .= "
2483: <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
2484: ";
2485: } else {
2486: $output .= "\n".'<th> </th>'."\n";
2487: if ($actionselect) {
2488: $output .= '<th>'.&mt('Select').'</th>'."\n";
2489: }
2490: }
2491: foreach my $item (@cols) {
2492: $output .= "<th><a href=\"javascript:document.$formname.sortby.value='$item';document.$formname.submit();\">$lt{$item}</a></th>\n";
2493: }
2494: my %role_types = &role_type_names();
2495: if ($context eq 'course' && $mode ne 'autoenroll') {
2496: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
2497: # Clicker display on or off?
2498: my %clicker_options = (
2499: 'on' => 'Show',
2500: 'off' => 'Hide',
2501: );
2502: my $clickerchg = 'on';
2503: if ($displayclickers eq 'on') {
2504: $clickerchg = 'off';
2505: }
2506: $output .= ' <th>'."\n".' '
2507: .&mt('[_1]'.$clicker_options{$clickerchg}.'[_2] clicker id'
2508: ,'<a href="javascript:document.'.$formname.'.displayclickers.value='
2509: ."'".$clickerchg."'".';document.'.$formname.'.submit();">'
2510: ,'</a>')
2511: ."\n".' </th>'."\n";
2512:
2513: # Photo display on or off?
2514: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2515: my %photo_options = &Apache::lonlocal::texthash(
2516: 'on' => 'Show',
2517: 'off' => 'Hide',
2518: );
2519: my $photochg = 'on';
2520: if ($displayphotos eq 'on') {
2521: $photochg = 'off';
2522: }
2523: $output .= ' <th>'."\n".' '.
2524: '<a href="javascript:document.'.$formname.'.displayphotos.value='.
2525: "'".$photochg."'".';document.'.$formname.'.submit();">'.
2526: $photo_options{$photochg}.'</a> '.$lt{'photo'}."\n".
2527: ' </th>'."\n";
2528: }
2529: }
2530: }
2531: $output .= &Apache::loncommon::end_data_table_header_row();
2532: # Done with the HTML header line
2533: } elsif ($mode eq 'csv') {
2534: #
2535: # Open a file
2536: $CSVfilename = '/prtspool/'.
2537: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
2538: time.'_'.rand(1000000000).'.csv';
2539: unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
2540: $r->log_error("Couldn't open $CSVfilename for output $!");
2541: $r->print(
2542: '<p class="LC_error">'
2543: .&mt('Problems occurred in writing the CSV file.')
2544: .' '.&mt('This error has been logged.')
2545: .' '.&mt('Please alert your LON-CAPA administrator.')
2546: .'</p>'
2547: );
2548: $CSVfile = undef;
2549: }
2550: #
2551: push @cols,'clicker';
2552: # Write headers and data to file
2553: print $CSVfile '"'.$results_description.'"'."\n";
2554: print $CSVfile '"'.join('","',map {
2555: &Apache::loncommon::csv_translate($lt{$_})
2556: } (@cols))."\"\n";
2557: } elsif ($mode eq 'excel') {
2558: push @cols,'clicker';
2559: # Create the excel spreadsheet
2560: ($excel_workbook,$excel_filename,$format) =
2561: &Apache::loncommon::create_workbook($r);
2562: return if (! defined($excel_workbook));
2563: $excel_sheet = $excel_workbook->addworksheet('userlist');
2564: $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
2565: #
2566: my @colnames = map {$lt{$_}} (@cols);
2567:
2568: $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
2569: }
2570:
2571: # Done with header lines in all formats
2572: my %index;
2573: my $i;
2574: foreach my $idx (@$keylist) {
2575: $index{$idx} = $i++;
2576: }
2577: my $usercount = 0;
2578: my ($secfilter,$grpfilter);
2579: if ($context eq 'course') {
2580: $secfilter = $env{'form.secfilter'};
2581: $grpfilter = $env{'form.grpfilter'};
2582: if ($secfilter eq '') {
2583: $secfilter = 'all';
2584: }
2585: if ($grpfilter eq '') {
2586: $grpfilter = 'all';
2587: }
2588: }
2589: my %ltstatus = &Apache::lonlocal::texthash(
2590: Active => 'Active',
2591: Future => 'Future',
2592: Expired => 'Expired',
2593: );
2594: # Get groups, role, permanent e-mail so we can sort on them if
2595: # necessary.
2596: foreach my $user (keys(%{$userlist})) {
2597: if ($user eq '' ) {
2598: delete($userlist->{$user});
2599: next;
2600: }
2601: if ($context eq 'domain' && $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
2602: delete($userlist->{$user});
2603: next;
2604: }
2605: my ($uname,$udom,$role,$groups,$email);
2606: if (($statusmode ne 'Any') &&
2607: ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
2608: delete($userlist->{$user});
2609: next;
2610: }
2611: if ($context eq 'domain') {
2612: if ($env{'form.roletype'} eq 'domain') {
2613: ($role,$uname,$udom) = split(/:/,$user);
2614: if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
2615: ($udom eq $env{'request.role.domain'})) {
2616: delete($userlist->{$user});
2617: next;
2618: }
2619: } elsif ($env{'form.roletype'} eq 'author') {
2620: ($uname,$udom,$role) = split(/:/,$user,-1);
2621: } elsif (($env{'form.roletype'} eq 'course') ||
2622: ($env{'form.roletype'} eq 'community')) {
2623: ($uname,$udom,$role) = split(/:/,$user);
2624: }
2625: } else {
2626: ($uname,$udom,$role) = split(/:/,$user,-1);
2627: if (($context eq 'course') && $role eq '') {
2628: $role = 'st';
2629: }
2630: }
2631: $userlist->{$user}->[$index{'role'}] = $role;
2632: if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'} eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
2633: delete($userlist->{$user});
2634: next;
2635: }
2636: if ($context eq 'course') {
2637: my @ac_groups;
2638: if (ref($classgroups) eq 'HASH') {
2639: $groups = $classgroups->{$user};
2640: }
2641: if (ref($groups->{'active'}) eq 'HASH') {
2642: @ac_groups = keys(%{$groups->{'active'}});
2643: $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
2644: }
2645: if ($mode ne 'autoenroll') {
2646: my $section = $userlist->{$user}->[$index{'section'}];
2647: if (($env{'request.course.sec'} ne '') &&
2648: ($section ne $env{'request.course.sec'})) {
2649: if ($role eq 'st') {
2650: delete($userlist->{$user});
2651: next;
2652: }
2653: }
2654: if ($secfilter eq 'none') {
2655: if ($section ne '') {
2656: delete($userlist->{$user});
2657: next;
2658: }
2659: } elsif ($secfilter ne 'all') {
2660: if ($section ne $secfilter) {
2661: delete($userlist->{$user});
2662: next;
2663: }
2664: }
2665: if ($grpfilter eq 'none') {
2666: if (@ac_groups > 0) {
2667: delete($userlist->{$user});
2668: next;
2669: }
2670: } elsif ($grpfilter ne 'all') {
2671: if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
2672: delete($userlist->{$user});
2673: next;
2674: }
2675: }
2676: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2677: if (($displayphotos eq 'on') && ($role eq 'st')) {
2678: $userlist->{$user}->[$index{'photo'}] =
2679: &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
2680: $userlist->{$user}->[$index{'thumbnail'}] =
2681: &Apache::lonnet::retrievestudentphoto($udom,$uname,
2682: 'gif','thumbnail');
2683: }
2684: }
2685: }
2686: }
2687: my %emails = &Apache::loncommon::getemails($uname,$udom);
2688: if ($emails{'permanentemail'} =~ /\S/) {
2689: $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
2690: }
2691: $usercount ++;
2692: }
2693: my $autocount = 0;
2694: my $manualcount = 0;
2695: my $lockcount = 0;
2696: my $unlockcount = 0;
2697: if ($usercount) {
2698: $r->print($output);
2699: } else {
2700: if ($mode eq 'autoenroll') {
2701: return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
2702: } else {
2703: return;
2704: }
2705: }
2706: #
2707: # Sort the users
2708: my $index = $index{$sortby};
2709: my $second = $index{'username'};
2710: my $third = $index{'domain'};
2711: my @sorted_users = sort {
2712: lc($userlist->{$a}->[$index]) cmp lc($userlist->{$b}->[$index])
2713: ||
2714: lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
2715: lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
2716: } (keys(%$userlist));
2717: my $rowcount = 0;
2718: foreach my $user (@sorted_users) {
2719: my %in;
2720: my $sdata = $userlist->{$user};
2721: $rowcount ++;
2722: foreach my $item (@{$keylist}) {
2723: $in{$item} = $sdata->[$index{$item}];
2724: }
2725: my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
2726: if ($clickers!~/\w/) { $clickers='-'; }
2727: $in{'clicker'} = $clickers;
2728: my $role = $in{'role'};
2729: $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
2730: if (! defined($in{'start'}) || $in{'start'} == 0) {
2731: $in{'start'} = &mt('none');
2732: } else {
2733: $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
2734: }
2735: if (! defined($in{'end'}) || $in{'end'} == 0) {
2736: $in{'end'} = &mt('none');
2737: } else {
2738: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
2739: }
2740: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
2741: $r->print(&Apache::loncommon::start_data_table_row());
2742: my $checkval;
2743: if ($mode eq 'autoenroll') {
2744: my $cellentry;
2745: if ($in{'type'} eq 'auto') {
2746: $cellentry = '<b>'.&mt('auto').'</b> <label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" /> Change</label>';
2747: $autocount ++;
2748: } else {
2749: $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><span class="LC_nobreak"><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'" /> Change</label></span></td></tr><tr><td><span class="LC_nobreak">';
2750: $manualcount ++;
2751: if ($in{'lockedtype'}) {
2752: $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" /> '.&mt('Unlock').'</label>';
2753: $unlockcount ++;
2754: } else {
2755: $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" /> '.&mt('Lock').'</label>';
2756: $lockcount ++;
2757: }
2758: $cellentry .= '</span></td></tr></table>';
2759: }
2760: $r->print("<td>$cellentry</td>\n");
2761: } else {
2762: if ($mode ne 'pickauthor') {
2763: $r->print("<td>$rowcount</td>\n");
2764: }
2765: if ($actionselect) {
2766: my $showcheckbox;
2767: if ($role =~ /^cr\//) {
2768: $showcheckbox = $canchange{'cr'};
2769: } else {
2770: $showcheckbox = $canchange{$role};
2771: }
2772: if (!$showcheckbox) {
2773: if ($context eq 'course') {
2774: if ($canchangesec{$role} ne '') {
2775: if ($canchangesec{$role} eq $in{'section'}) {
2776: $showcheckbox = 1;
2777: }
2778: }
2779: }
2780: }
2781: if ($showcheckbox) {
2782: $checkval = $user;
2783: if ($context eq 'course') {
2784: if ($role eq 'st') {
2785: $checkval .= ':st';
2786: }
2787: $checkval .= ':'.$in{'section'};
2788: if ($role eq 'st') {
2789: $checkval .= ':'.$in{'type'}.':'.
2790: $in{'lockedtype'};
2791: }
2792: }
2793: $r->print('<td><input type="checkbox" name="'.
2794: 'actionlist" value="'.$checkval.'" /></td>');
2795: } else {
2796: $r->print('<td> </td>');
2797: }
2798: } elsif ($mode eq 'pickauthor') {
2799: $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
2800: }
2801: }
2802: foreach my $item (@cols) {
2803: if ($item eq 'username') {
2804: $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
2805: } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
2806: $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
2807: } elsif ($item eq 'status') {
2808: my $showitem = $in{$item};
2809: if (defined($ltstatus{$in{$item}})) {
2810: $showitem = $ltstatus{$in{$item}};
2811: }
2812: $r->print('<td>'.$showitem.'</td>'."\n");
2813: } else {
2814: $r->print('<td>'.$in{$item}.'</td>'."\n");
2815: }
2816: }
2817: if (($context eq 'course') && ($mode ne 'autoenroll')) {
2818: if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
2819: if ($displayclickers eq 'on') {
2820: my $clickers =
2821: (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
2822: if ($clickers!~/\w/) { $clickers='-'; }
2823: $r->print('<td>'.$clickers.'</td>');
2824: } else {
2825: $r->print(' <td> </td> ');
2826: }
2827: if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
2828: if ($displayphotos eq 'on' && $role eq 'st' && $in{'photo'} ne '') {
2829: $r->print(' <td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
2830: } else {
2831: $r->print(' <td> </td> ');
2832: }
2833: }
2834: }
2835: }
2836: $r->print(&Apache::loncommon::end_data_table_row());
2837: } elsif ($mode eq 'csv') {
2838: next if (! defined($CSVfile));
2839: # no need to bother with $linkto
2840: if (! defined($in{'start'}) || $in{'start'} == 0) {
2841: $in{'start'} = &mt('none');
2842: } else {
2843: $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
2844: }
2845: if (! defined($in{'end'}) || $in{'end'} == 0) {
2846: $in{'end'} = &mt('none');
2847: } else {
2848: $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
2849: }
2850: my @line = ();
2851: foreach my $item (@cols) {
2852: push @line,&Apache::loncommon::csv_translate($in{$item});
2853: }
2854: print $CSVfile '"'.join('","',@line)."\"\n";
2855: } elsif ($mode eq 'excel') {
2856: my $col = 0;
2857: foreach my $item (@cols) {
2858: if ($item eq 'start' || $item eq 'end') {
2859: if (defined($item) && $item != 0) {
2860: $excel_sheet->write($row,$col++,
2861: &Apache::lonstathelpers::calc_serial($in{item}),
2862: $format->{'date'});
2863: } else {
2864: $excel_sheet->write($row,$col++,'none');
2865: }
2866: } else {
2867: $excel_sheet->write($row,$col++,$in{$item});
2868: }
2869: }
2870: $row++;
2871: }
2872: }
2873: if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
2874: $r->print(&Apache::loncommon::end_data_table().'<br />');
2875: } elsif ($mode eq 'excel') {
2876: $excel_workbook->close();
2877: $r->print(&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<p><a href="'.$excel_filename.'">','</a>')."</p>\n");
2878: } elsif ($mode eq 'csv') {
2879: close($CSVfile);
2880: $r->print(&mt('[_1]Your CSV file[_2] is ready for download.', '<p><a href="'.$CSVfilename.'">','</a>')."</p>\n");
2881: $r->rflush();
2882: }
2883: if ($mode eq 'autoenroll') {
2884: return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
2885: } else {
2886: return ($usercount);
2887: }
2888: }
2889:
2890: sub bulkaction_javascript {
2891: my ($formname,$caller) = @_;
2892: my $docstart = 'document';
2893: if ($caller eq 'popup') {
2894: $docstart = 'opener.document';
2895: }
2896: my %lt = &Apache::lonlocal::texthash(
2897: acwi => 'Access will be set to start immediately',
2898: asyo => 'as you did not select an end date in the pop-up window',
2899: accw => 'Access will be set to continue indefinitely',
2900: asyd => 'as you did not select an end date in the pop-up window',
2901: sewi => "Sections will be switched to 'No section'",
2902: ayes => "as you either selected the 'No section' option",
2903: oryo => 'or you did not select a section in the pop-up window',
2904: arol => 'A role with no section will be added',
2905: swbs => 'Sections will be switched to:',
2906: rwba => 'Roles will be added for section(s):',
2907: );
2908: my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
2909: my $noaction = &mt("You need to select an action to take for the user(s) you have selected");
2910: my $singconfirm = &mt(' for a single user?');
2911: my $multconfirm = &mt(' for multiple users?');
2912: my $output = <<"ENDJS";
2913: function verify_action (field) {
2914: var numchecked = 0;
2915: var singconf = '$singconfirm';
2916: var multconf = '$multconfirm';
2917: if ($docstart.$formname.elements[field].length > 0) {
2918: for (i=0; i<$docstart.$formname.elements[field].length; i++) {
2919: if ($docstart.$formname.elements[field][i].checked == true) {
2920: numchecked ++;
2921: }
2922: }
2923: } else {
2924: if ($docstart.$formname.elements[field].checked == true) {
2925: numchecked ++;
2926: }
2927: }
2928: if (numchecked == 0) {
2929: alert("$alert");
2930: return;
2931: } else {
2932: var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
2933: var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
2934: if (choice == '') {
2935: alert("$noaction");
2936: return;
2937: } else {
2938: if (numchecked == 1) {
2939: message += singconf;
2940: } else {
2941: message += multconf;
2942: }
2943: ENDJS
2944: if ($caller ne 'popup') {
2945: $output .= <<"NEWWIN";
2946: if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
2947: opendatebrowser(document.$formname,'$formname','go');
2948: return;
2949:
2950: } else {
2951: if (confirm(message)) {
2952: document.$formname.phase.value = 'bulkchange';
2953: document.$formname.submit();
2954: return;
2955: }
2956: }
2957: NEWWIN
2958: } else {
2959: $output .= <<"POPUP";
2960: if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
2961: var datemsg = '';
2962: if (($docstart.$formname.startdate_month.value == '') &&
2963: ($docstart.$formname.startdate_day.value == '') &&
2964: ($docstart.$formname.startdate_year.value == '')) {
2965: datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
2966: }
2967: if (($docstart.$formname.enddate_month.value == '') &&
2968: ($docstart.$formname.enddate_day.value == '') &&
2969: ($docstart.$formname.enddate_year.value == '')) {
2970: datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
2971: }
2972: if (datemsg != '') {
2973: message += "\\n"+datemsg;
2974: }
2975: }
2976: if (choice == 'chgsec') {
2977: var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
2978: var retained = $docstart.$formname.retainsec.value;
2979: var secshow = $docstart.$formname.newsecs.value;
2980: if (secshow == '') {
2981: if (rolefilter == 'st' || retained == 0 || retained == "") {
2982: message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
2983: } else {
2984: message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
2985: }
2986: } else {
2987: if (rolefilter == 'st' || retained == 0 || retained == "") {
2988: message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
2989: } else {
2990: message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
2991: }
2992: }
2993: }
2994: if (confirm(message)) {
2995: $docstart.$formname.phase.value = 'bulkchange';
2996: $docstart.$formname.submit();
2997: window.close();
2998: }
2999: POPUP
3000: }
3001: $output .= '
3002: }
3003: }
3004: }
3005: ';
3006: return $output;
3007: }
3008:
3009: sub print_username_link {
3010: my ($mode,$in) = @_;
3011: my $output;
3012: if ($mode eq 'autoenroll') {
3013: $output = $in->{'username'};
3014: } else {
3015: $output = '<a href="javascript:username_display_launch('.
3016: "'$in->{'username'}','$in->{'domain'}'".')">'.
3017: $in->{'username'}.'</a>';
3018: }
3019: return $output;
3020: }
3021:
3022: sub role_type_names {
3023: my %lt = &Apache::lonlocal::texthash (
3024: 'domain' => 'Domain Roles',
3025: 'author' => 'Co-Author Roles',
3026: 'course' => 'Course Roles',
3027: 'community' => 'Community Roles',
3028: );
3029: return %lt;
3030: }
3031:
3032: sub select_actions {
3033: my ($context,$setting,$statusmode,$formname) = @_;
3034: my %lt = &Apache::lonlocal::texthash(
3035: revoke => "Revoke user roles",
3036: delete => "Delete user roles",
3037: reenable => "Re-enable expired user roles",
3038: activate => "Make future user roles active now",
3039: chgdates => "Change starting/ending dates",
3040: chgsec => "Change section associated with user roles",
3041: );
3042: my ($output,$options,%choices);
3043: # FIXME Disable actions for now for roletype=course in domain context
3044: if ($context eq 'domain' && $setting eq 'course') {
3045: return;
3046: }
3047: if ($context eq 'course') {
3048: if ($env{'form.showrole'} ne 'Any') {
3049: if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},
3050: $env{'request.course.id'})) {
3051: if ($env{'request.course.sec'} eq '') {
3052: return;
3053: } else {
3054: if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
3055: return;
3056: }
3057: }
3058: }
3059: }
3060: }
3061: if ($statusmode eq 'Any') {
3062: $options .= '
3063: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
3064: $choices{'dates'} = 1;
3065: } else {
3066: if ($statusmode eq 'Future') {
3067: $options .= '
3068: <option value="activate">'.$lt{'activate'}.'</option>';
3069: $choices{'dates'} = 1;
3070: } elsif ($statusmode eq 'Expired') {
3071: $options .= '
3072: <option value="reenable">'.$lt{'reenable'}.'</option>';
3073: $choices{'dates'} = 1;
3074: }
3075: if ($statusmode eq 'Active' || $statusmode eq 'Future') {
3076: $options .= '
3077: <option value="chgdates">'.$lt{'chgdates'}.'</option>
3078: <option value="revoke">'.$lt{'revoke'}.'</option>';
3079: $choices{'dates'} = 1;
3080: }
3081: }
3082: if ($context eq 'domain') {
3083: $options .= '
3084: <option value="delete">'.$lt{'delete'}.'</option>';
3085: }
3086: if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
3087: if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
3088: $options .= '
3089: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
3090: $choices{'sections'} = 1;
3091: }
3092: }
3093: if ($options) {
3094: $output = '<select name="bulkaction">'."\n".
3095: '<option value="" selected="selected">'.
3096: &mt('Please select').'</option>'."\n".$options."\n".'</select>';
3097: if ($choices{'dates'}) {
3098: $output .=
3099: '<input type="hidden" name="startdate_month" value="" />'."\n".
3100: '<input type="hidden" name="startdate_day" value="" />'."\n".
3101: '<input type="hidden" name="startdate_year" value="" />'."\n".
3102: '<input type="hidden" name="startdate_hour" value="" />'."\n".
3103: '<input type="hidden" name="startdate_minute" value="" />'."\n".
3104: '<input type="hidden" name="startdate_second" value="" />'."\n".
3105: '<input type="hidden" name="enddate_month" value="" />'."\n".
3106: '<input type="hidden" name="enddate_day" value="" />'."\n".
3107: '<input type="hidden" name="enddate_year" value="" />'."\n".
3108: '<input type="hidden" name="enddate_hour" value="" />'."\n".
3109: '<input type="hidden" name="enddate_minute" value="" />'."\n".
3110: '<input type="hidden" name="enddate_second" value="" />'."\n".
3111: '<input type="hidden" name="no_end_date" value="" />'."\n";
3112: if ($context eq 'course') {
3113: $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
3114: }
3115: }
3116: if ($choices{'sections'}) {
3117: $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
3118: '<input type="hidden" name="newsecs" value="" />'."\n";
3119: }
3120: }
3121: return $output;
3122: }
3123:
3124: sub date_section_javascript {
3125: my ($context,$setting) = @_;
3126: my $title = 'Date_And_Section_Selector';
3127: my %nopopup = &Apache::lonlocal::texthash (
3128: revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
3129: delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
3130: none => "Choose an action to take for selected users",
3131: );
3132: my $output = <<"ENDONE";
3133: <script type="text/javascript">
3134: // <![CDATA[
3135: function opendatebrowser(callingform,formname,calledby) {
3136: var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
3137: var url = '/adm/createuser?';
3138: var type = '';
3139: var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
3140: ENDONE
3141: if ($context eq 'domain') {
3142: $output .= '
3143: type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
3144: ';
3145: }
3146: my $width= '700';
3147: my $height = '400';
3148: $output .= <<"ENDTWO";
3149: url += 'action=dateselect&callingform=' + formname +
3150: '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
3151: var title = '$title';
3152: var options = 'scrollbars=1,resizable=1,menubar=0';
3153: options += ',width=$width,height=$height';
3154: stdeditbrowser = open(url,title,options,'1');
3155: stdeditbrowser.focus();
3156: }
3157: // ]]>
3158: </script>
3159: ENDTWO
3160: return $output;
3161: }
3162:
3163: sub date_section_selector {
3164: my ($context,$permission,$crstype) = @_;
3165: my $callingform = $env{'form.callingform'};
3166: my $formname = 'dateselect';
3167: my $groupslist = &get_groupslist();
3168: my $sec_js = &setsections_javascript($formname,$groupslist);
3169: my $output = <<"END";
3170: <script type="text/javascript">
3171: // <![CDATA[
3172:
3173: $sec_js
3174:
3175: function saveselections(formname) {
3176:
3177: END
3178: if ($env{'form.bulkaction'} eq 'chgsec') {
3179: $output .= <<"END";
3180: if (formname.retainsec.length > 1) {
3181: for (var i=0; i<formname.retainsec.length; i++) {
3182: if (formname.retainsec[i].checked == true) {
3183: opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
3184: }
3185: }
3186: } else {
3187: opener.document.$callingform.retainsec.value = formname.retainsec.value;
3188: }
3189: setSections(formname,'$crstype');
3190: if (seccheck == 'ok') {
3191: opener.document.$callingform.newsecs.value = formname.sections.value;
3192: }
3193: END
3194: } else {
3195: if ($context eq 'course') {
3196: if (($env{'form.bulkaction'} eq 'reenable') ||
3197: ($env{'form.bulkaction'} eq 'activate') ||
3198: ($env{'form.bulkaction'} eq 'chgdates')) {
3199: if ($env{'request.course.sec'} eq '') {
3200: $output .= <<"END";
3201:
3202: if (formname.makedatesdefault.checked == true) {
3203: opener.document.$callingform.makedatesdefault.value = 1;
3204: }
3205: else {
3206: opener.document.$callingform.makedatesdefault.value = 0;
3207: }
3208:
3209: END
3210: }
3211: }
3212: }
3213: $output .= <<"END";
3214: opener.document.$callingform.startdate_month.value = formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
3215: opener.document.$callingform.startdate_day.value = formname.startdate_day.value;
3216: opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
3217: opener.document.$callingform.startdate_hour.value = formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
3218: opener.document.$callingform.startdate_minute.value = formname.startdate_minute.value;
3219: opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
3220: opener.document.$callingform.enddate_month.value = formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
3221: opener.document.$callingform.enddate_day.value = formname.enddate_day.value;
3222: opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
3223: opener.document.$callingform.enddate_hour.value = formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
3224: opener.document.$callingform.enddate_minute.value = formname.enddate_minute.value;
3225: opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
3226: if (formname.no_end_date.checked) {
3227: opener.document.$callingform.no_end_date.value = '1';
3228: } else {
3229: opener.document.$callingform.no_end_date.value = '0';
3230: }
3231: END
3232: }
3233: my $verify_action_js = &bulkaction_javascript($callingform,'popup');
3234: $output .= <<"ENDJS";
3235: verify_action('actionlist');
3236: }
3237:
3238: $verify_action_js
3239:
3240: // ]]>
3241: </script>
3242: ENDJS
3243: my %lt = &Apache::lonlocal::texthash (
3244: chac => 'Access dates to apply for selected users',
3245: chse => 'Changes in section affiliation to apply to selected users',
3246: 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.',
3247: forn => 'For a course role that is not "student", users may have roles in more than one section at a time.',
3248: reta => "Retain each user's current section affiliations?",
3249: dnap => '(Does not apply to student roles).',
3250: );
3251: my ($date_items,$headertext);
3252: if ($env{'form.bulkaction'} eq 'chgsec') {
3253: $headertext = $lt{'chse'};
3254: } else {
3255: $headertext = $lt{'chac'};
3256: my $starttime;
3257: if (($env{'form.bulkaction'} eq 'activate') ||
3258: ($env{'form.bulkaction'} eq 'reenable')) {
3259: $starttime = time;
3260: }
3261: $date_items = &date_setting_table($starttime,undef,$context,
3262: $env{'form.bulkaction'},$formname,
3263: $permission,$crstype);
3264: }
3265: $output .= '<h3>'.$headertext.'</h3>'.
3266: '<form name="'.$formname.'" method="post" action="">'."\n".
3267: $date_items;
3268: if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
3269: my ($cnum,$cdom) = &get_course_identity();
3270: if ($crstype eq 'Community') {
3271: $lt{'fors'} = &mt('For member roles, changing the section will result in a section switch, as members may only be in one section of a community at a time.');
3272: $lt{'forn'} = &mt('For a community role that is not "member", users may have roles in more than one section at a time.');
3273: $lt{'dnap'} = &mt('(Does not apply to member roles).');
3274: }
3275: my $info;
3276: if ($env{'form.showrole'} eq 'st') {
3277: $output .= '<p>'.$lt{'fors'}.'</p>';
3278: } elsif ($env{'form.showrole'} eq 'Any') {
3279: $output .= '<p>'.$lt{'fors'}.'</p>'.
3280: '<p>'.$lt{'forn'}.' ';
3281: $info = $lt{'reta'};
3282: } else {
3283: $output .= '<p>'.$lt{'forn'}.' ';
3284: $info = $lt{'reta'};
3285: }
3286: if ($info) {
3287: $info .= '<span class="LC_nobreak">'.
3288: '<label><input type="radio" name="retainsec" value="1" '.
3289: 'checked="checked" />'.&mt('Yes').'</label> '.
3290: '<label><input type="radio" name="retainsec" value="0" />'.
3291: &mt('No').'</label></span>';
3292: if ($env{'form.showrole'} eq 'Any') {
3293: $info .= '<br />'.$lt{'dnap'};
3294: }
3295: $info .= '</p>';
3296: } else {
3297: $info = '<input type="hidden" name="retainsec" value="0" />';
3298: }
3299: my $rowtitle = &mt('New section to assign');
3300: my $secbox = §ion_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context,'',$crstype);
3301: $output .= $info.$secbox;
3302: }
3303: $output .= '<p>'.
3304: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
3305: '</form>';
3306: return $output;
3307: }
3308:
3309: sub section_picker {
3310: my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype) = @_;
3311: my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
3312: my $sections_select .= &course_sections(\%sections_count,$role);
3313: my $secbox = '<div>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
3314: if ($mode eq 'upload') {
3315: my ($options,$cb_script,$coursepick) =
3316: &default_role_selector($context,1,$crstype);
3317: $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
3318: $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
3319: }
3320: $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
3321: if ($env{'request.course.sec'} eq '') {
3322: $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
3323: '<td align="center">'.&mt('Existing sections')."\n".
3324: '<br />'.$sections_select.'</td><td align="center">'.
3325: &mt('New section').'<br />'."\n".
3326: '<input type="text" name="newsec" size="15" value="" />'."\n".
3327: '<input type="hidden" name="sections" value="" />'."\n".
3328: '</td></tr></table>'."\n";
3329: } else {
3330: $secbox .= '<input type="hidden" name="sections" value="'.
3331: $env{'request.course.sec'}.'" />'.
3332: $env{'request.course.sec'};
3333: }
3334: $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
3335: &Apache::lonhtmlcommon::end_pick_box().'</div>';
3336: return $secbox;
3337: }
3338:
3339: sub results_header_row {
3340: my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
3341: my ($description,$showfilter);
3342: if ($rolefilter ne 'Any') {
3343: $showfilter = $rolefilter;
3344: }
3345: if ($context eq 'course') {
3346: if ($mode eq 'csv' || $mode eq 'excel') {
3347: if ($crstype eq 'Community') {
3348: $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
3349: } else {
3350: $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
3351: }
3352: }
3353: if ($statusmode eq 'Expired') {
3354: if ($crstype eq 'Community') {
3355: $description .= &mt('Users in community with expired [_1] roles',$showfilter);
3356: } else {
3357: $description .= &mt('Users in course with expired [_1] roles',$showfilter);
3358: }
3359: } elsif ($statusmode eq 'Future') {
3360: if ($crstype eq 'Community') {
3361: $description .= &mt('Users in community with future [_1] roles',$showfilter);
3362: } else {
3363: $description .= &mt('Users in course with future [_1] roles',$showfilter);
3364: }
3365: } elsif ($statusmode eq 'Active') {
3366: if ($crstype eq 'Community') {
3367: $description .= &mt('Users in community with active [_1] roles',$showfilter);
3368: } else {
3369: $description .= &mt('Users in course with active [_1] roles',$showfilter);
3370: }
3371: } else {
3372: if ($rolefilter eq 'Any') {
3373: if ($crstype eq 'Community') {
3374: $description .= &mt('All users in community');
3375: } else {
3376: $description .= &mt('All users in course');
3377: }
3378: } else {
3379: if ($crstype eq 'Community') {
3380: $description .= &mt('All users in community with [_1] roles',$rolefilter);
3381: } else {
3382: $description .= &mt('All users in course with [_1] roles',$rolefilter);
3383: }
3384: }
3385: }
3386: my $constraint;
3387: my $viewablesec = &viewable_section($permission);
3388: if ($viewablesec ne '') {
3389: if ($env{'form.showrole'} eq 'st') {
3390: $constraint = &mt('only users in section "[_1]"',$viewablesec);
3391: } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
3392: $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
3393: }
3394: if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
3395: if ($env{'form.grpfilter'} eq 'none') {
3396: $constraint .= &mt(' and not in any group');
3397: } else {
3398: $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
3399: }
3400: }
3401: } else {
3402: if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
3403: if ($env{'form.secfilter'} eq 'none') {
3404: $constraint = &mt('only users affiliated with no section');
3405: } else {
3406: $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
3407: }
3408: }
3409: if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
3410: if ($env{'form.grpfilter'} eq 'none') {
3411: if ($constraint eq '') {
3412: $constraint = &mt('only users not in any group');
3413: } else {
3414: $constraint .= &mt(' and also not in any group');
3415: }
3416: } else {
3417: if ($constraint eq '') {
3418: $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
3419: } else {
3420: $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
3421: }
3422: }
3423: }
3424: }
3425: if ($constraint ne '') {
3426: $description .= ' ('.$constraint.')';
3427: }
3428: } elsif ($context eq 'author') {
3429: $description =
3430: &mt('Author space for [_1]'
3431: ,'<span class="LC_cusr_emph">'
3432: .&Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})
3433: .'</span>')
3434: .': ';
3435: if ($statusmode eq 'Expired') {
3436: $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
3437: } elsif ($statusmode eq 'Future') {
3438: $description .= &mt('Co-authors with future [_1] roles',$showfilter);
3439: } elsif ($statusmode eq 'Active') {
3440: $description .= &mt('Co-authors with active [_1] roles',$showfilter);
3441: } else {
3442: if ($rolefilter eq 'Any') {
3443: $description .= &mt('All co-authors');
3444: } else {
3445: $description .= &mt('All co-authors with [_1] roles',$rolefilter);
3446: }
3447: }
3448: } elsif ($context eq 'domain') {
3449: my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
3450: $description = &mt('Domain - [_1]:',$domdesc).' ';
3451: if ($env{'form.roletype'} eq 'domain') {
3452: if ($statusmode eq 'Expired') {
3453: $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
3454: } elsif ($statusmode eq 'Future') {
3455: $description .= &mt('Users in domain with future [_1] roles',$showfilter);
3456: } elsif ($statusmode eq 'Active') {
3457: $description .= &mt('Users in domain with active [_1] roles',$showfilter);
3458: } else {
3459: if ($rolefilter eq 'Any') {
3460: $description .= &mt('All users in domain');
3461: } else {
3462: $description .= &mt('All users in domain with [_1] roles',$rolefilter);
3463: }
3464: }
3465: } elsif ($env{'form.roletype'} eq 'author') {
3466: if ($statusmode eq 'Expired') {
3467: $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
3468: } elsif ($statusmode eq 'Future') {
3469: $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
3470: } elsif ($statusmode eq 'Active') {
3471: $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
3472: } else {
3473: if ($rolefilter eq 'Any') {
3474: $description .= &mt('All users with co-author roles in domain',$showfilter);
3475: } else {
3476: $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
3477: }
3478: }
3479: } elsif (($env{'form.roletype'} eq 'course') ||
3480: ($env{'form.roletype'} eq 'community')) {
3481:
3482: my $coursefilter = $env{'form.coursepick'};
3483: if ($env{'form.roletype'} eq 'course') {
3484: if ($coursefilter eq 'category') {
3485: my $instcode = &instcode_from_coursefilter();
3486: if ($instcode eq '.') {
3487: $description .= &mt('All courses in domain').' - ';
3488: } else {
3489: $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
3490: }
3491: } elsif ($coursefilter eq 'selected') {
3492: $description .= &mt('Selected courses in domain').' - ';
3493: } elsif ($coursefilter eq 'all') {
3494: $description .= &mt('All courses in domain').' - ';
3495: }
3496: } elsif ($env{'form.roletype'} eq 'community') {
3497: if ($coursefilter eq 'selected') {
3498: $description .= &mt('Selected communities in domain').' - ';
3499: } elsif ($coursefilter eq 'all') {
3500: $description .= &mt('All communities in domain').' - ';
3501: }
3502: }
3503: if ($statusmode eq 'Expired') {
3504: $description .= &mt('users with expired [_1] roles',$showfilter);
3505: } elsif ($statusmode eq 'Future') {
3506: $description .= &mt('users with future [_1] roles',$showfilter);
3507: } elsif ($statusmode eq 'Active') {
3508: $description .= &mt('users with active [_1] roles',$showfilter);
3509: } else {
3510: if ($rolefilter eq 'Any') {
3511: $description .= &mt('all users');
3512: } else {
3513: $description .= &mt('users with [_1] roles',$rolefilter);
3514: }
3515: }
3516: }
3517: }
3518: return $description;
3519: }
3520:
3521: sub viewable_section {
3522: my ($permission) = @_;
3523: my $viewablesec;
3524: if (ref($permission) eq 'HASH') {
3525: if (exists($permission->{'view_section'})) {
3526: $viewablesec = $permission->{'view_section'};
3527: } elsif (exists($permission->{'cusr_section'})) {
3528: $viewablesec = $permission->{'cusr_section'};
3529: }
3530: }
3531: return $viewablesec;
3532: }
3533:
3534:
3535: #################################################
3536: #################################################
3537: sub show_drop_list {
3538: my ($r,$classlist,$nosort,$permission,$crstype) = @_;
3539: my $cid = $env{'request.course.id'};
3540: my ($cnum,$cdom) = &get_course_identity($cid);
3541: if (! exists($env{'form.sortby'})) {
3542: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
3543: ['sortby']);
3544: }
3545: my $sortby = $env{'form.sortby'};
3546: if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
3547: $sortby = 'username';
3548: }
3549: my $action = "drop";
3550: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
3551: $r->print(<<END);
3552: <input type="hidden" name="sortby" value="$sortby" />
3553: <input type="hidden" name="action" value="$action" />
3554: <input type="hidden" name="state" value="done" />
3555: <script type="text/javascript" language="Javascript">
3556: // <![CDATA[
3557: $check_uncheck_js
3558: // ]]>
3559: </script>
3560: <p>
3561: <input type="hidden" name="phase" value="four" />
3562: END
3563: my ($indexhash,$keylist) = &make_keylist_array();
3564: my $studentcount = 0;
3565: if (ref($classlist) eq 'HASH') {
3566: foreach my $student (keys(%{$classlist})) {
3567: my $sdata = $classlist->{$student};
3568: my $status = $sdata->[$indexhash->{'status'}];
3569: my $section = $sdata->[$indexhash->{'section'}];
3570: if ($status ne 'Active') {
3571: delete($classlist->{$student});
3572: next;
3573: }
3574: if ($env{'request.course.sec'} ne '') {
3575: if ($section ne $env{'request.course.sec'}) {
3576: delete($classlist->{$student});
3577: next;
3578: }
3579: }
3580: $studentcount ++;
3581: }
3582: }
3583: if (!$studentcount) {
3584: if ($crstype eq 'Community') {
3585: $r->print(&mt('There are no members to drop.'));
3586: } else {
3587: $r->print(&mt('There are no students to drop.'));
3588: }
3589: return;
3590: }
3591: my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
3592: $classlist,$keylist,$cdom,$cnum);
3593: my %lt=&Apache::lonlocal::texthash('usrn' => "username",
3594: 'dom' => "domain",
3595: 'sn' => "student name",
3596: 'mn' => "member name",
3597: 'sec' => "section",
3598: 'start' => "start date",
3599: 'end' => "end date",
3600: 'groups' => "active groups",
3601: );
3602: my $nametitle = $lt{'sn'};
3603: if ($crstype eq 'Community') {
3604: $nametitle = $lt{'mn'};
3605: }
3606: if ($nosort) {
3607: $r->print(&Apache::loncommon::start_data_table().
3608: &Apache::loncommon::start_data_table_header_row());
3609: $r->print(<<END);
3610: <th> </th>
3611: <th>$lt{'usrn'}</th>
3612: <th>$lt{'dom'}</th>
3613: <th>ID</th>
3614: <th>$nametitle</th>
3615: <th>$lt{'sec'}</th>
3616: <th>$lt{'start'}</th>
3617: <th>$lt{'end'}</th>
3618: <th>$lt{'groups'}</th>
3619: END
3620: $r->print(&Apache::loncommon::end_data_table_header_row());
3621: } else {
3622: $r->print(&Apache::loncommon::start_data_table().
3623: &Apache::loncommon::start_data_table_header_row());
3624: $r->print(<<END);
3625: <th> </th>
3626: <th>
3627: <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
3628: </th><th>
3629: <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
3630: </th><th>
3631: <a href="/adm/createuser?action=$action&sortby=id">ID</a>
3632: </th><th>
3633: <a href="/adm/createuser?action=$action&sortby=fullname">$nametitle</a>
3634: </th><th>
3635: <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
3636: </th><th>
3637: <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
3638: </th><th>
3639: <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
3640: </th><th>
3641: <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
3642: </th>
3643: END
3644: $r->print(&Apache::loncommon::end_data_table_header_row());
3645: }
3646: #
3647: # Sort the students
3648: my $index = $indexhash->{$sortby};
3649: my $second = $indexhash->{'username'};
3650: my $third = $indexhash->{'domain'};
3651: my @Sorted_Students = sort {
3652: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
3653: ||
3654: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
3655: ||
3656: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
3657: } (keys(%{$classlist}));
3658: foreach my $student (@Sorted_Students) {
3659: my $error;
3660: my $sdata = $classlist->{$student};
3661: my $username = $sdata->[$indexhash->{'username'}];
3662: my $domain = $sdata->[$indexhash->{'domain'}];
3663: my $section = $sdata->[$indexhash->{'section'}];
3664: my $name = $sdata->[$indexhash->{'fullname'}];
3665: my $id = $sdata->[$indexhash->{'id'}];
3666: my $start = $sdata->[$indexhash->{'start'}];
3667: my $end = $sdata->[$indexhash->{'end'}];
3668: my $groups = $classgroups->{$student};
3669: my $active_groups;
3670: if (ref($groups->{active}) eq 'HASH') {
3671: $active_groups = join(', ',keys(%{$groups->{'active'}}));
3672: }
3673: if (! defined($start) || $start == 0) {
3674: $start = &mt('none');
3675: } else {
3676: $start = &Apache::lonlocal::locallocaltime($start);
3677: }
3678: if (! defined($end) || $end == 0) {
3679: $end = &mt('none');
3680: } else {
3681: $end = &Apache::lonlocal::locallocaltime($end);
3682: }
3683: my $studentkey = $student.':'.$section;
3684: my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
3685: #
3686: $r->print(&Apache::loncommon::start_data_table_row());
3687: $r->print(<<"END");
3688: <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
3689: <td>$username</td>
3690: <td>$domain</td>
3691: <td>$id</td>
3692: <td>$name</td>
3693: <td>$section</td>
3694: <td>$start $startitem</td>
3695: <td>$end</td>
3696: <td>$active_groups</td>
3697: END
3698: $r->print(&Apache::loncommon::end_data_table_row());
3699: }
3700: $r->print(&Apache::loncommon::end_data_table().'<br />');
3701: %lt=&Apache::lonlocal::texthash(
3702: 'dp' => "Drop Students",
3703: 'dm' => "Drop Members",
3704: 'ca' => "check all",
3705: 'ua' => "uncheck all",
3706: );
3707: my $btn = $lt{'dp'};
3708: if ($crstype eq 'Community') {
3709: $btn = $lt{'dm'};
3710: }
3711: $r->print(<<"END");
3712: </p>
3713: <p>
3714: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" />
3715: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
3716: </p>
3717: <p>
3718: <input type="submit" value="$btn" />
3719: </p>
3720: END
3721: return;
3722: }
3723:
3724: #
3725: # Print out the initial form to get the file containing a list of users
3726: #
3727: sub print_first_users_upload_form {
3728: my ($r,$context) = @_;
3729: my $str;
3730: $str = '<input type="hidden" name="phase" value="two" />';
3731: $str .= '<input type="hidden" name="action" value="upload" />';
3732: $str .= '<input type="hidden" name="state" value="got_file" />';
3733:
3734: $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
3735:
3736: # Excel and CSV Help
3737: $str .= '<div class="LC_left_float">'
3738: .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
3739: &mt("How do I create a users list from a spreadsheet"))
3740: .'</div><div class="LC_left_float">'."\n"
3741: .&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
3742: &mt("How do I create a CSV file from a spreadsheet"))
3743: .'</div><br clear="all" />'."\n";
3744: $str .= &Apache::lonhtmlcommon::start_pick_box()
3745: .&Apache::lonhtmlcommon::row_title(&mt('File'))
3746: .&Apache::loncommon::upfile_select_html()
3747: .&Apache::lonhtmlcommon::row_closure()
3748: .&Apache::lonhtmlcommon::row_title(
3749: '<label for="noFirstLine">'
3750: .&mt('Ignore First Line')
3751: .'</label>')
3752: .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
3753: .&Apache::lonhtmlcommon::row_closure(1)
3754: .&Apache::lonhtmlcommon::end_pick_box();
3755:
3756: $str .= '<p>'
3757: .'<input type="submit" name="fileupload" value="'.&mt('Next').'" />'
3758: .'</p>';
3759:
3760: $r->print($str);
3761: return;
3762: }
3763:
3764: # ================================================= Drop/Add from uploaded file
3765: sub upfile_drop_add {
3766: my ($r,$context,$permission) = @_;
3767: &Apache::loncommon::load_tmp_file($r);
3768: my @userdata=&Apache::loncommon::upfile_record_sep();
3769: if($env{'form.noFirstLine'}){shift(@userdata);}
3770: my @keyfields = split(/\,/,$env{'form.keyfields'});
3771: my %fields=();
3772: for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
3773: if ($env{'form.upfile_associate'} eq 'reverse') {
3774: if ($env{'form.f'.$i} ne 'none') {
3775: $fields{$keyfields[$i]}=$env{'form.f'.$i};
3776: }
3777: } else {
3778: $fields{$env{'form.f'.$i}}=$keyfields[$i];
3779: }
3780: }
3781: if ($env{'form.fullup'} ne 'yes') {
3782: $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
3783: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
3784: }
3785: #
3786: # Store the field choices away
3787: foreach my $field (qw/username names
3788: fname mname lname gen id sec ipwd email role domain/) {
3789: $env{'form.'.$field.'_choice'}=$fields{$field};
3790: }
3791: &Apache::loncommon::store_course_settings('enrollment_upload',
3792: { 'username_choice' => 'scalar',
3793: 'names_choice' => 'scalar',
3794: 'fname_choice' => 'scalar',
3795: 'mname_choice' => 'scalar',
3796: 'lname_choice' => 'scalar',
3797: 'gen_choice' => 'scalar',
3798: 'id_choice' => 'scalar',
3799: 'sec_choice' => 'scalar',
3800: 'ipwd_choice' => 'scalar',
3801: 'email_choice' => 'scalar',
3802: 'role_choice' => 'scalar',
3803: 'domain_choice' => 'scalar',
3804: 'inststatus_choice' => 'scalar'});
3805: #
3806: my ($cid,$crstype,$setting);
3807: if ($context eq 'domain') {
3808: $setting = $env{'form.roleaction'};
3809: }
3810: if ($env{'request.course.id'} ne '') {
3811: $cid = $env{'request.course.id'};
3812: $crstype = &Apache::loncommon::course_type();
3813: } elsif ($setting eq 'course') {
3814: if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
3815: $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
3816: $crstype = &Apache::loncommon::course_type($cid);
3817: }
3818: }
3819: my ($startdate,$enddate) = &get_dates_from_form();
3820: if ($env{'form.makedatesdefault'}) {
3821: $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
3822: }
3823: # Determine domain and desired host (home server)
3824: my $defdom=$env{'request.role.domain'};
3825: my $domain;
3826: if ($env{'form.defaultdomain'} ne '') {
3827: $domain = $env{'form.defaultdomain'};
3828: } else {
3829: $domain = $defdom;
3830: }
3831: my $desiredhost = $env{'form.lcserver'};
3832: if (lc($desiredhost) eq 'default') {
3833: $desiredhost = undef;
3834: } else {
3835: my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
3836: if (! exists($home_servers{$desiredhost})) {
3837: $r->print('<span class="LC_error">'.&mt('Error').
3838: &mt('Invalid home server specified').'</span>');
3839: $r->print(&Apache::loncommon::end_page());
3840: return;
3841: }
3842: }
3843: # Determine authentication mechanism
3844: my $changeauth;
3845: if ($context eq 'domain') {
3846: $changeauth = $env{'form.changeauth'};
3847: }
3848: my $amode = '';
3849: my $genpwd = '';
3850: if ($env{'form.login'} eq 'krb') {
3851: $amode='krb';
3852: $amode.=$env{'form.krbver'};
3853: $genpwd=$env{'form.krbarg'};
3854: } elsif ($env{'form.login'} eq 'int') {
3855: $amode='internal';
3856: if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
3857: $genpwd=$env{'form.intarg'};
3858: }
3859: } elsif ($env{'form.login'} eq 'loc') {
3860: $amode='localauth';
3861: if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
3862: $genpwd=$env{'form.locarg'};
3863: }
3864: }
3865: if ($amode =~ /^krb/) {
3866: if (! defined($genpwd) || $genpwd eq '') {
3867: $r->print('<span class="Error">'.
3868: &mt('Unable to enroll users').' '.
3869: &mt('No Kerberos domain was specified.').'</span></p>');
3870: $amode = ''; # This causes the loop below to be skipped
3871: }
3872: }
3873: my ($defaultsec,$defaultrole);
3874: if ($context eq 'domain') {
3875: if ($setting eq 'domain') {
3876: $defaultrole = $env{'form.defaultrole'};
3877: } elsif ($setting eq 'course') {
3878: $defaultrole = $env{'form.courserole'};
3879: $defaultsec = $env{'form.sections'};
3880: }
3881: } elsif ($context eq 'author') {
3882: $defaultrole = $env{'form.defaultrole'};
3883: } elsif ($context eq 'course') {
3884: $defaultrole = $env{'form.defaultrole'};
3885: $defaultsec = $env{'form.sections'};
3886: }
3887: # Check to see if user information can be changed
3888: my @userinfo = ('firstname','middlename','lastname','generation',
3889: 'permanentemail','id');
3890: my %canmodify;
3891: if (&Apache::lonnet::allowed('mau',$domain)) {
3892: push(@userinfo,'inststatus');
3893: foreach my $field (@userinfo) {
3894: $canmodify{$field} = 1;
3895: }
3896: }
3897: my (%userlist,%modifiable_fields,@poss_roles);
3898: my $secidx = &Apache::loncoursedata::CL_SECTION();
3899: my @courseroles = &roles_by_context('course',1,$crstype);
3900: if (!&Apache::lonnet::allowed('mau',$domain)) {
3901: if ($context eq 'course' || $context eq 'author') {
3902: @poss_roles = &curr_role_permissions($context,'','',$crstype);
3903: my @statuses = ('active','future');
3904: my ($indexhash,$keylist) = &make_keylist_array();
3905: my %info;
3906: foreach my $role (@poss_roles) {
3907: %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
3908: \@userinfo,[$role]);
3909: }
3910: if ($context eq 'course') {
3911: my ($cnum,$cdom) = &get_course_identity();
3912: my $roster = &Apache::loncoursedata::get_classlist();
3913: if (ref($roster) eq 'HASH') {
3914: %userlist = %{$roster};
3915: }
3916: my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
3917: \@statuses,\@poss_roles);
3918: &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
3919: \%advrolehash,$permission);
3920: } elsif ($context eq 'author') {
3921: my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
3922: \@statuses,\@poss_roles);
3923: &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
3924: \%cstr_roles,$permission);
3925:
3926: }
3927: }
3928: }
3929: if ( $domain eq &LONCAPA::clean_domain($domain)
3930: && ($amode ne '')) {
3931: #######################################
3932: ## Add/Modify Users ##
3933: #######################################
3934: if ($context eq 'course') {
3935: $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
3936: } elsif ($context eq 'author') {
3937: $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
3938: } else {
3939: $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
3940: }
3941: $r->rflush;
3942:
3943: my %counts = (
3944: user => 0,
3945: auth => 0,
3946: role => 0,
3947: );
3948: my $flushc=0;
3949: my %student=();
3950: my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
3951: my %userchg;
3952: if ($context eq 'course' || $setting eq 'course') {
3953: if ($context eq 'course') {
3954: # Get information about course groups
3955: %curr_groups = &Apache::longroup::coursegroups();
3956: } elsif ($setting eq 'course') {
3957: if ($cid) {
3958: %curr_groups =
3959: &Apache::longroup::coursegroups($env{'form.dcdomain'},
3960: $env{'form.dccourse'});
3961: }
3962: }
3963: # determine section number
3964: if ($defaultsec =~ /,/) {
3965: push(@sections,split(/,/,$defaultsec));
3966: } else {
3967: push(@sections,$defaultsec);
3968: }
3969: # remove non alphanumeric values from section
3970: foreach my $item (@sections) {
3971: $item =~ s/\W//g;
3972: if ($item eq "none" || $item eq 'all') {
3973: $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
3974: } elsif ($item ne '' && exists($curr_groups{$item})) {
3975: $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
3976: } elsif ($item ne '') {
3977: push(@cleansec,$item);
3978: }
3979: }
3980: if ($defaultwarn) {
3981: $r->print($defaultwarn.'<br />');
3982: }
3983: if ($groupwarn) {
3984: $r->print($groupwarn.'<br />');
3985: }
3986: }
3987: my (%curr_rules,%got_rules,%alerts,%cancreate);
3988: my %customroles = &my_custom_roles($crstype);
3989: my @permitted_roles =
3990: &roles_on_upload($context,$setting,$crstype,%customroles);
3991: my %longtypes = &Apache::lonlocal::texthash(
3992: official => 'Institutional',
3993: unofficial => 'Non-institutional',
3994: );
3995: map { $cancreate{$_} = &can_create_user($domain,$context,$_); } keys(%longtypes);
3996: # Get new users list
3997: foreach my $line (@userdata) {
3998: my @secs;
3999: my %entries=&Apache::loncommon::record_sep($line);
4000: # Determine user name
4001: $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
4002: unless (($entries{$fields{'username'}} eq '') ||
4003: (!defined($entries{$fields{'username'}}))) {
4004: my ($fname, $mname, $lname,$gen) = ('','','','');
4005: if (defined($fields{'names'})) {
4006: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
4007: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
4008: } else {
4009: if (defined($fields{'fname'})) {
4010: $fname=$entries{$fields{'fname'}};
4011: }
4012: if (defined($fields{'mname'})) {
4013: $mname=$entries{$fields{'mname'}};
4014: }
4015: if (defined($fields{'lname'})) {
4016: $lname=$entries{$fields{'lname'}};
4017: }
4018: if (defined($fields{'gen'})) {
4019: $gen=$entries{$fields{'gen'}};
4020: }
4021: }
4022: if ($entries{$fields{'username'}}
4023: ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
4024: my $nowhitespace;
4025: if ($entries{$fields{'username'}} =~ /\s/) {
4026: $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
4027: }
4028: $r->print('<br />'.
4029: &mt('[_1]: Unacceptable username for user [_2] [_3] [_4] [_5]',
4030: '<b>'.$entries{$fields{'username'}}.'</b>',$fname,$mname,$lname,$gen).
4031: $nowhitespace);
4032: next;
4033: } else {
4034: $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
4035: if ($entries{$fields{'domain'}}
4036: ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
4037: $r->print('<br />'. '<b>'.$entries{$fields{'domain'}}.
4038: '</b>: '.&mt('Unacceptable domain for user [_2] [_3] [_4] [_5]',$fname,$mname,$lname,$gen));
4039: next;
4040: }
4041: my $username = $entries{$fields{'username'}};
4042: my $userdomain = $entries{$fields{'domain'}};
4043: if ($userdomain eq '') {
4044: $userdomain = $domain;
4045: }
4046: if (defined($fields{'sec'})) {
4047: if (defined($entries{$fields{'sec'}})) {
4048: $entries{$fields{'sec'}} =~ s/\W//g;
4049: my $item = $entries{$fields{'sec'}};
4050: if ($item eq "none" || $item eq 'all') {
4051: $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item));
4052: next;
4053: } elsif (exists($curr_groups{$item})) {
4054: $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item).' '.&mt('Section names and group names must be distinct.'));
4055: next;
4056: } else {
4057: push(@secs,$item);
4058: }
4059: }
4060: }
4061: if ($env{'request.course.sec'} ne '') {
4062: @secs = ($env{'request.course.sec'});
4063: if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
4064: my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
4065: if ($currsec ne $env{'request.course.sec'}) {
4066: $r->print('<br />'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".','<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$secs[0]).'<br />');
4067: if ($currsec eq '') {
4068: $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.'));
4069:
4070: } else {
4071: $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec));
4072: }
4073: $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 />');
4074: next;
4075: }
4076: }
4077: } elsif ($context eq 'course' || $setting eq 'course') {
4078: if (@secs == 0) {
4079: @secs = @cleansec;
4080: }
4081: }
4082: # determine id number
4083: my $id='';
4084: if (defined($fields{'id'})) {
4085: if (defined($entries{$fields{'id'}})) {
4086: $id=$entries{$fields{'id'}};
4087: }
4088: $id=~tr/A-Z/a-z/;
4089: }
4090: # determine email address
4091: my $email='';
4092: if (defined($fields{'email'})) {
4093: $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
4094: if (defined($entries{$fields{'email'}})) {
4095: $email=$entries{$fields{'email'}};
4096: unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
4097: }
4098: }
4099: # determine affiliation
4100: my $inststatus='';
4101: if (defined($fields{'inststatus'})) {
4102: if (defined($entries{$fields{'inststatus'}})) {
4103: $inststatus=$entries{$fields{'inststatus'}};
4104: }
4105: }
4106: # determine user password
4107: my $password = $genpwd;
4108: if (defined($fields{'ipwd'})) {
4109: if ($entries{$fields{'ipwd'}}) {
4110: $password=$entries{$fields{'ipwd'}};
4111: }
4112: }
4113: # determine user role
4114: my $role = '';
4115: if (defined($fields{'role'})) {
4116: if ($entries{$fields{'role'}}) {
4117: $entries{$fields{'role'}} =~ s/(\s+$|^\s+)//g;
4118: if ($entries{$fields{'role'}} ne '') {
4119: if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
4120: $role = $entries{$fields{'role'}};
4121: }
4122: }
4123: if ($role eq '') {
4124: my $rolestr = join(', ',@permitted_roles);
4125: $r->print('<br />'
4126: .&mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
4127: ,'<b>'.$entries{$fields{'username'}}.'</b>'
4128: ,$entries{$fields{'role'}})
4129: .'<br />'
4130: .&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n"
4131: );
4132: next;
4133: }
4134: }
4135: }
4136: if ($role eq '') {
4137: $role = $defaultrole;
4138: }
4139: # Clean up whitespace
4140: foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
4141: $$_ =~ s/(\s+$|^\s+)//g;
4142: }
4143: # check against rules
4144: my $checkid = 0;
4145: my $newuser = 0;
4146: my (%rulematch,%inst_results,%idinst_results);
4147: my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
4148: if ($uhome eq 'no_host') {
4149: if ($userdomain ne $domain) {
4150: $r->print('<br />'.
4151: &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
4152: '<b>'.$username.'</b>',$userdomain).'<br />'.
4153: &mt('The user does not already exist, and you may not create a new user in a different domain.'));
4154: next;
4155: }
4156: $checkid = 1;
4157: $newuser = 1;
4158: my $user = $username.':'.$domain;
4159: my $checkhash;
4160: my $checks = { 'username' => 1 };
4161: $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
4162: &Apache::loncommon::user_rule_check($checkhash,$checks,
4163: \%alerts,\%rulematch,\%inst_results,\%curr_rules,
4164: \%got_rules);
4165: if (ref($alerts{'username'}) eq 'HASH') {
4166: if (ref($alerts{'username'}{$domain}) eq 'HASH') {
4167: if ($alerts{'username'}{$domain}{$username}) {
4168: $r->print('<br />'.
4169: &mt('[_1]: matches the username format at your institution, but is not known to your directory service.','<b>'.$username.'</b>').'<br />'.
4170: &mt('Consequently, the user was not created.'));
4171: next;
4172: }
4173: }
4174: }
4175: my $usertype = 'unofficial';
4176: if (ref($rulematch{$user}) eq 'HASH') {
4177: if ($rulematch{$user}{'username'}) {
4178: $usertype = 'official';
4179: }
4180: }
4181: unless ($cancreate{$usertype}) {
4182: my $showtype = $longtypes{$usertype};
4183: $r->print('<br />'.
4184: &mt('[_1]: The user does not exist, and you are not permitted to create users of type: [_2].','<b>'.$username.'</b>',$showtype));
4185: next;
4186: }
4187: } else {
4188: if ($context eq 'course' || $context eq 'author') {
4189: if ($userdomain eq $domain ) {
4190: if ($role eq '') {
4191: my @checkroles;
4192: foreach my $role (@poss_roles) {
4193: my $endkey;
4194: if ($role ne 'st') {
4195: $endkey = ':'.$role;
4196: }
4197: if (exists($userlist{$username.':'.$userdomain.$endkey})) {
4198: if (!grep(/^\Q$role\E$/,@checkroles)) {
4199: push(@checkroles,$role);
4200: }
4201: }
4202: }
4203: if (@checkroles > 0) {
4204: %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
4205: }
4206: } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
4207: %canmodify = %{$modifiable_fields{$role}};
4208: }
4209: }
4210: my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
4211: for (my $i=0; $i<@newinfo; $i++) {
4212: if (${$newinfo[$i]} ne '') {
4213: if (!$canmodify{$userinfo[$i]}) {
4214: ${$newinfo[$i]} = '';
4215: }
4216: }
4217: }
4218: }
4219: }
4220: if ($id ne '') {
4221: if (!$newuser) {
4222: my %idhash = &Apache::lonnet::idrget($userdomain,($username));
4223: if ($idhash{$username} ne $id) {
4224: $checkid = 1;
4225: }
4226: }
4227: if ($checkid) {
4228: my $checkhash;
4229: my $checks = { 'id' => 1 };
4230: $checkhash->{$username.':'.$userdomain} = { 'newuser' => $newuser,
4231: 'id' => $id };
4232: &Apache::loncommon::user_rule_check($checkhash,$checks,
4233: \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
4234: \%got_rules);
4235: if (ref($alerts{'id'}) eq 'HASH') {
4236: if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
4237: if ($alerts{'id'}{$userdomain}{$id}) {
4238: $r->print(&mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is found by your directory service.',
4239: '<b>'.$username.'</b>').'<br />'.
4240: &mt('Consequently, the user was not created.'));
4241: next;
4242: }
4243: }
4244: }
4245: }
4246: }
4247: if ($password || $env{'form.login'} eq 'loc') {
4248: my $multiple = 0;
4249: my ($userresult,$authresult,$roleresult,$idresult);
4250: my (%userres,%authres,%roleres,%idres);
4251: my $singlesec = '';
4252: if ($role eq 'st') {
4253: my $sec;
4254: if (@secs > 0) {
4255: $sec = $secs[0];
4256: }
4257: &modifystudent($userdomain,$username,$cid,$sec,
4258: $desiredhost,$context);
4259: $roleresult =
4260: &Apache::lonnet::modifystudent
4261: ($userdomain,$username,$id,$amode,$password,
4262: $fname,$mname,$lname,$gen,$sec,$enddate,
4263: $startdate,$env{'form.forceid'},
4264: $desiredhost,$email,'manual','',$cid,
4265: '',$context,$inststatus);
4266: $userresult = $roleresult;
4267: } else {
4268: if ($role ne '') {
4269: if ($context eq 'course' || $setting eq 'course') {
4270: if ($customroles{$role}) {
4271: $role = 'cr_'.$env{'user.domain'}.'_'.
4272: $env{'user.name'}.'_'.$role;
4273: }
4274: if (($role ne 'cc') && ($role ne 'co')) {
4275: if (@secs > 1) {
4276: $multiple = 1;
4277: foreach my $sec (@secs) {
4278: ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
4279: &modifyuserrole($context,$setting,
4280: $changeauth,$cid,$userdomain,$username,
4281: $id,$amode,$password,$fname,
4282: $mname,$lname,$gen,$sec,
4283: $env{'form.forceid'},$desiredhost,
4284: $email,$role,$enddate,
4285: $startdate,$checkid,$inststatus);
4286: }
4287: } elsif (@secs > 0) {
4288: $singlesec = $secs[0];
4289: }
4290: }
4291: }
4292: }
4293: if (!$multiple) {
4294: ($userresult,$authresult,$roleresult,$idresult) =
4295: &modifyuserrole($context,$setting,
4296: $changeauth,$cid,$userdomain,$username,
4297: $id,$amode,$password,$fname,
4298: $mname,$lname,$gen,$singlesec,
4299: $env{'form.forceid'},$desiredhost,
4300: $email,$role,$enddate,$startdate,
4301: $checkid,$inststatus);
4302: }
4303: }
4304: if ($multiple) {
4305: foreach my $sec (sort(keys(%userres))) {
4306: $flushc =
4307: &user_change_result($r,$userres{$sec},$authres{$sec},
4308: $roleres{$sec},$idres{$sec},\%counts,$flushc,
4309: $username,$userdomain,\%userchg);
4310:
4311: }
4312: } else {
4313: $flushc =
4314: &user_change_result($r,$userresult,$authresult,
4315: $roleresult,$idresult,\%counts,$flushc,
4316: $username,$userdomain,\%userchg);
4317: }
4318: } else {
4319: if ($context eq 'course') {
4320: $r->print('<br />'.
4321: &mt('[_1]: Unable to enroll. No password specified.','<b>'.$username.'</b>')
4322: );
4323: } elsif ($context eq 'author') {
4324: $r->print('<br />'.
4325: &mt('[_1]: Unable to add co-author. No password specified.','<b>'.$username.'</b>')
4326: );
4327: } else {
4328: $r->print('<br />'.
4329: &mt('[_1]: Unable to add user. No password specified.','<b>'.$username.'</b>')
4330: );
4331: }
4332: }
4333: }
4334: }
4335: } # end of foreach (@userdata)
4336: # Flush the course logs so reverse user roles immediately updated
4337: $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
4338: $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
4339: "</p>\n");
4340: if ($counts{'role'} > 0) {
4341: $r->print("<p>\n".
4342: &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.&mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.')."</p>\n");
4343: } else {
4344: $r->print('<p>'.&mt('No roles added').'</p>');
4345: }
4346: if ($counts{'auth'} > 0) {
4347: $r->print("<p>\n".
4348: &mt('Authentication changed for [_1] existing users.',
4349: $counts{'auth'})."</p>\n");
4350: }
4351: $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
4352: #####################################
4353: # Display list of students to drop #
4354: #####################################
4355: if ($env{'form.fullup'} eq 'yes') {
4356: $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
4357: # Get current classlist
4358: my $classlist = &Apache::loncoursedata::get_classlist();
4359: if (! defined($classlist)) {
4360: $r->print('<form name="studentform" method="post" action="/adm/createuser" />'.
4361: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
4362: &mt('There are no students with current/future access to the course.').
4363: '</form>'."\n");
4364: } elsif (ref($classlist) eq 'HASH') {
4365: # Remove the students we just added from the list of students.
4366: foreach my $line (@userdata) {
4367: my %entries=&Apache::loncommon::record_sep($line);
4368: unless (($entries{$fields{'username'}} eq '') ||
4369: (!defined($entries{$fields{'username'}}))) {
4370: delete($classlist->{$entries{$fields{'username'}}.
4371: ':'.$domain});
4372: }
4373: }
4374: # Print out list of dropped students.
4375: &show_drop_list($r,$classlist,'nosort',$permission);
4376: }
4377: }
4378: } # end of unless
4379: if ($env{'form.fullup'} ne 'yes') {
4380: $r->print('</form>');
4381: }
4382: }
4383:
4384: sub print_namespacing_alerts {
4385: my ($domain,$alerts,$curr_rules) = @_;
4386: my $output;
4387: if (ref($alerts) eq 'HASH') {
4388: if (keys(%{$alerts}) > 0) {
4389: if (ref($alerts->{'username'}) eq 'HASH') {
4390: foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
4391: my $count;
4392: if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
4393: $count = keys(%{$alerts->{'username'}{$dom}});
4394: }
4395: my $domdesc = &Apache::lonnet::domain($domain,'description');
4396: if (ref($curr_rules->{$dom}) eq 'HASH') {
4397: $output .= &Apache::loncommon::instrule_disallow_msg(
4398: 'username',$domdesc,$count,'upload');
4399: }
4400: $output .= &Apache::loncommon::user_rule_formats($dom,
4401: $domdesc,$curr_rules->{$dom}{'username'},
4402: 'username');
4403: }
4404: }
4405: if (ref($alerts->{'id'}) eq 'HASH') {
4406: foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
4407: my $count;
4408: if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
4409: $count = keys(%{$alerts->{'id'}{$dom}});
4410: }
4411: my $domdesc = &Apache::lonnet::domain($domain,'description');
4412: if (ref($curr_rules->{$dom}) eq 'HASH') {
4413: $output .= &Apache::loncommon::instrule_disallow_msg(
4414: 'id',$domdesc,$count,'upload');
4415: }
4416: $output .= &Apache::loncommon::user_rule_formats($dom,
4417: $domdesc,$curr_rules->{$dom}{'id'},'id');
4418: }
4419: }
4420: }
4421: }
4422: }
4423:
4424: sub user_change_result {
4425: my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
4426: $username,$userdomain,$userchg) = @_;
4427: my $okresult = 0;
4428: if ($userresult ne 'ok') {
4429: if ($userresult =~ /^error:(.+)$/) {
4430: my $error = $1;
4431: $r->print('<br />'.
4432: &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
4433: }
4434: } else {
4435: $counts->{'user'} ++;
4436: $okresult = 1;
4437: }
4438: if ($authresult ne 'ok') {
4439: if ($authresult =~ /^error:(.+)$/) {
4440: my $error = $1;
4441: $r->print('<br />'.
4442: &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
4443: }
4444: } else {
4445: $counts->{'auth'} ++;
4446: $okresult = 1;
4447: }
4448: if ($roleresult ne 'ok') {
4449: if ($roleresult =~ /^error:(.+)$/) {
4450: my $error = $1;
4451: $r->print('<br />'.
4452: &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
4453: }
4454: } else {
4455: $counts->{'role'} ++;
4456: $okresult = 1;
4457: }
4458: if ($okresult) {
4459: $flushc++;
4460: $userchg->{$username.':'.$userdomain}=1;
4461: $r->print('. ');
4462: if ($flushc>15) {
4463: $r->rflush;
4464: $flushc=0;
4465: }
4466: }
4467: if ($idresult) {
4468: $r->print($idresult);
4469: }
4470: return $flushc;
4471: }
4472:
4473: # ========================================================= Menu Phase Two Drop
4474: sub print_drop_menu {
4475: my ($r,$context,$permission,$crstype) = @_;
4476: my $heading;
4477: if ($crstype eq 'Community') {
4478: $heading = &mt("Drop Members");
4479: } else {
4480: $heading = &mt("Drop Students");
4481: }
4482: $r->print('<h3>'.$heading.'</h3>'."\n".
4483: '<form name="studentform" method="post">'."\n");
4484: my $classlist = &Apache::loncoursedata::get_classlist();
4485: if (! defined($classlist)) {
4486: if ($crstype eq 'Community') {
4487: $r->print(&mt('There are no members currently enrolled.')."\n");
4488: } else {
4489: $r->print(&mt('There are no students currently enrolled.')."\n");
4490: }
4491: } else {
4492: &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
4493: }
4494: $r->print('</form>'. &Apache::loncommon::end_page());
4495: return;
4496: }
4497:
4498: # ================================================================== Phase four
4499:
4500: sub update_user_list {
4501: my ($r,$context,$setting,$choice,$crstype) = @_;
4502: my $now = time;
4503: my $count=0;
4504: if ($context eq 'course') {
4505: $crstype = &Apache::loncommon::course_type();
4506: }
4507: my @changelist;
4508: if ($choice eq 'drop') {
4509: @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
4510: } else {
4511: @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
4512: }
4513: my %result_text = ( ok => { 'revoke' => 'Revoked',
4514: 'delete' => 'Deleted',
4515: 'reenable' => 'Re-enabled',
4516: 'activate' => 'Activated',
4517: 'chgdates' => 'Changed Access Dates for',
4518: 'chgsec' => 'Changed section(s) for',
4519: 'drop' => 'Dropped',
4520: },
4521: error => {'revoke' => 'revoking',
4522: 'delete' => 'deleting',
4523: 'reenable' => 're-enabling',
4524: 'activate' => 'activating',
4525: 'chgdates' => 'changing access dates for',
4526: 'chgsec' => 'changing section for',
4527: 'drop' => 'dropping',
4528: },
4529: );
4530: my ($startdate,$enddate);
4531: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
4532: ($startdate,$enddate) = &get_dates_from_form();
4533: }
4534: foreach my $item (@changelist) {
4535: my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,
4536: @sections,$scopestem,$singlesec,$showsecs,$warn_singlesec,
4537: $nothingtodo,$keepnosection);
4538: if ($choice eq 'drop') {
4539: ($uname,$udom,$sec) = split(/:/,$item,-1);
4540: $role = 'st';
4541: $cid = $env{'request.course.id'};
4542: $scopestem = '/'.$cid;
4543: $scopestem =~s/\_/\//g;
4544: if ($sec eq '') {
4545: $scope = $scopestem;
4546: } else {
4547: $scope = $scopestem.'/'.$sec;
4548: }
4549: } elsif ($context eq 'course') {
4550: ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
4551: $cid = $env{'request.course.id'};
4552: $scopestem = '/'.$cid;
4553: $scopestem =~s/\_/\//g;
4554: if ($sec eq '') {
4555: $scope = $scopestem;
4556: } else {
4557: $scope = $scopestem.'/'.$sec;
4558: }
4559: } elsif ($context eq 'author') {
4560: ($uname,$udom,$role) = split(/\:/,$item,-1);
4561: $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
4562: } elsif ($context eq 'domain') {
4563: if ($setting eq 'domain') {
4564: ($role,$uname,$udom) = split(/\:/,$item,-1);
4565: $scope = '/'.$env{'request.role.domain'}.'/';
4566: } elsif ($setting eq 'author') {
4567: ($uname,$udom,$role,$scope) = split(/\:/,$item);
4568: } elsif ($setting eq 'course') {
4569: ($uname,$udom,$role,$cid,$sec,$type,$locktype) =
4570: split(/\:/,$item);
4571: $scope = '/'.$cid;
4572: $scope =~s/\_/\//g;
4573: if ($sec ne '') {
4574: $scope .= '/'.$sec;
4575: }
4576: }
4577: }
4578: my $plrole = &Apache::lonnet::plaintext($role,$crstype);
4579: my $start = $env{'form.'.$item.'_start'};
4580: my $end = $env{'form.'.$item.'_end'};
4581: if ($choice eq 'drop') {
4582: # drop students
4583: $end = $now;
4584: $type = 'manual';
4585: $result =
4586: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
4587: } elsif ($choice eq 'revoke') {
4588: # revoke or delete user role
4589: $end = $now;
4590: if ($role eq 'st') {
4591: $result =
4592: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
4593: } else {
4594: $result =
4595: &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
4596: '','',$context);
4597: }
4598: } elsif ($choice eq 'delete') {
4599: if ($role eq 'st') {
4600: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context);
4601: }
4602: $result =
4603: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
4604: $start,1,'',$context);
4605: } else {
4606: #reenable, activate, change access dates or change section
4607: if ($choice ne 'chgsec') {
4608: $start = $startdate;
4609: $end = $enddate;
4610: }
4611: if ($choice eq 'reenable') {
4612: if ($role eq 'st') {
4613: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
4614: } else {
4615: $result =
4616: &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
4617: $now,'','',$context);
4618: }
4619: } elsif ($choice eq 'activate') {
4620: if ($role eq 'st') {
4621: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
4622: } else {
4623: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
4624: $now,'','',$context);
4625: }
4626: } elsif ($choice eq 'chgdates') {
4627: if ($role eq 'st') {
4628: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
4629: } else {
4630: $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
4631: $start,'','',$context);
4632: }
4633: } elsif ($choice eq 'chgsec') {
4634: my (@newsecs,$revresult,$nochg,@retained);
4635: if (($role ne 'cc') && ($role ne 'co')) {
4636: my @secs = sort(split(/,/,$env{'form.newsecs'}));
4637: if (@secs) {
4638: my %curr_groups = &Apache::longroup::coursegroups();
4639: foreach my $sec (@secs) {
4640: next if (($sec =~ /\W/) || ($sec eq 'none') ||
4641: (exists($curr_groups{$sec})));
4642: push(@newsecs,$sec);
4643: }
4644: }
4645: }
4646: # remove existing section if not to be retained.
4647: if (!$env{'form.retainsec'} || ($role eq 'st')) {
4648: if ($sec eq '') {
4649: if (@newsecs == 0) {
4650: $result = 'ok';
4651: $nochg = 1;
4652: $nothingtodo = 1;
4653: } else {
4654: $revresult =
4655: &Apache::lonnet::revokerole($udom,$uname,
4656: $scope,$role,
4657: '','',$context);
4658: }
4659: } else {
4660: if (@newsecs > 0) {
4661: if (grep(/^\Q$sec\E$/,@newsecs)) {
4662: push(@retained,$sec);
4663: } else {
4664: $revresult =
4665: &Apache::lonnet::revokerole($udom,$uname,
4666: $scope,$role,
4667: '','',$context);
4668: }
4669: } else {
4670: $revresult =
4671: &Apache::lonnet::revokerole($udom,$uname,
4672: $scope,$role,
4673: '','',$context);
4674: }
4675: }
4676: } else {
4677: if ($sec eq '') {
4678: $nochg = 1;
4679: $keepnosection = 1;
4680: } else {
4681: push(@retained,$sec);
4682: }
4683: }
4684: # add new sections
4685: my (@diffs,@shownew);
4686: if (@retained) {
4687: @diffs = &Apache::loncommon::compare_arrays(\@retained,\@newsecs);
4688: } else {
4689: @diffs = @newsecs;
4690: }
4691: if (@newsecs == 0) {
4692: if ($nochg) {
4693: $result = 'ok';
4694: $nothingtodo = 1;
4695: } else {
4696: if ($role eq 'st') {
4697: $result =
4698: &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context);
4699: } else {
4700: my $newscope = $scopestem;
4701: $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
4702: }
4703: }
4704: $showsecs = &mt('No section');
4705: } elsif (@diffs == 0) {
4706: $result = 'ok';
4707: $nothingtodo = 1;
4708: } else {
4709: foreach my $newsec (@newsecs) {
4710: if (!grep(/^\Q$newsec\E$/,@retained)) {
4711: if ($role eq 'st') {
4712: $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context);
4713: if (@newsecs > 1) {
4714: my $showsingle;
4715: if ($newsec eq '') {
4716: $showsingle = &mt('No section');
4717: } else {
4718: $showsingle = $newsec;
4719: }
4720: if ($crstype eq 'Community') {
4721: $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each community member may only be in one section at a time.','<i>'.$showsingle.'</i>');
4722: } else {
4723: $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each student may only be in one section of a course at a time.','<i>'.$showsingle.'</i>');
4724: }
4725: $showsecs = $showsingle;
4726: last;
4727: } else {
4728: if ($newsec eq '') {
4729: $showsecs = &mt('No section');
4730: } else {
4731: $showsecs = $newsec;
4732: }
4733: }
4734: } else {
4735: my $newscope = $scopestem;
4736: if ($newsec ne '') {
4737: $newscope .= '/'.$newsec;
4738: push(@shownew,$newsec);
4739: }
4740: $result = &Apache::lonnet::assignrole($udom,$uname,
4741: $newscope,$role,$end,$start);
4742: }
4743: }
4744: }
4745: }
4746: unless ($role eq 'st') {
4747: unless ($showsecs) {
4748: my @tolist = sort(@shownew,@retained);
4749: if ($keepnosection) {
4750: push(@tolist,&mt('No section'));
4751: }
4752: $showsecs = join(', ',@tolist);
4753: }
4754: }
4755: }
4756: }
4757: my $extent = $scope;
4758: if ($choice eq 'drop' || $context eq 'course') {
4759: my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
4760: if ($cdesc) {
4761: $extent = $cdesc;
4762: }
4763: }
4764: if ($result eq 'ok' || $result eq 'ok:') {
4765: my $dates;
4766: if (($choice eq 'chgsec') || ($choice eq 'chgdates')) {
4767: $dates = &dates_feedback($start,$end,$now);
4768: }
4769: if ($choice eq 'chgsec') {
4770: if ($nothingtodo) {
4771: $r->print(&mt("Section assignment for role of '[_1]' in [_2] for '[_3]' unchanged.",$plrole,$extent,'<i>'.
4772: &Apache::loncommon::plainname($uname,$udom).
4773: '</i>').' ');
4774: if ($sec eq '') {
4775: $r->print(&mt('[_1]No section[_2] - [_3]','<b>','</b>',$dates));
4776: } else {
4777: $r->print(&mt('Section(s): [_1] - [_2]',
4778: '<b>'.$showsecs.'</b>',$dates));
4779: }
4780: $r->print('<br />');
4781: } else {
4782: $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
4783: '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
4784: '<b>'.$showsecs.'</b>',$dates).'<br />');
4785: $count ++;
4786: }
4787: if ($warn_singlesec) {
4788: $r->print('<div class="LC_warning">'.$warn_singlesec.'</div>');
4789: }
4790: } elsif ($choice eq 'chgdates') {
4791: $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent,
4792: '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
4793: $dates).'<br />');
4794: $count ++;
4795: } else {
4796: $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
4797: '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>').
4798: '<br />');
4799: $count ++;
4800: }
4801: } else {
4802: $r->print(
4803: &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
4804: $plrole,$extent,
4805: '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
4806: $result).'<br />');
4807: }
4808: }
4809: $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
4810: if ($choice eq 'drop') {
4811: $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
4812: '<input type="hidden" name="Status" value="Active" />'."\n".
4813: '<input type="hidden" name="showrole" value="st" />'."\n");
4814: } else {
4815: foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
4816: if ($env{'form.'.$item} ne '') {
4817: $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
4818: '" />'."\n");
4819: }
4820: }
4821: }
4822: $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} for [quant,_1,user role,user roles,no user roles].",$count).'</b></p>');
4823: if ($count > 0) {
4824: if ($choice eq 'revoke' || $choice eq 'drop') {
4825: $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
4826: }
4827: # Flush the course logs so reverse user roles immediately updated
4828: $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
4829: }
4830: if ($env{'form.makedatesdefault'}) {
4831: if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
4832: $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
4833: }
4834: }
4835: my $linktext = &mt('Display User Lists');
4836: if ($choice eq 'drop') {
4837: $linktext = &mt('Display current class roster');
4838: }
4839: $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
4840: }
4841:
4842: sub dates_feedback {
4843: my ($start,$end,$now) = @_;
4844: my $dates;
4845: if ($start < $now) {
4846: if ($end == 0) {
4847: $dates .= &mt('role(s) active now; no end date');
4848: } elsif ($end > $now) {
4849: $dates = &mt('role(s) active now; ends [_1].',&Apache::lonlocal::locallocaltime($end));
4850: } else {
4851: $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
4852: }
4853: } else {
4854: if ($end == 0 || $end > $now) {
4855: $dates = &mt('future role(s); starts: [_1].',&Apache::lonlocal::locallocaltime($start));
4856: } else {
4857: $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
4858: }
4859: }
4860: return $dates;
4861: }
4862:
4863: sub classlist_drop {
4864: my ($scope,$uname,$udom,$now) = @_;
4865: my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
4866: if (&Apache::lonnet::is_course($cdom,$cnum)) {
4867: if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
4868: my %user;
4869: my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
4870: return &mt('Drop from classlist: [_1]',
4871: '<b>'.$result.'</b>').'<br />';
4872: }
4873: }
4874: }
4875:
4876: sub active_student_roles {
4877: my ($cnum,$cdom,$uname,$udom) = @_;
4878: my %roles =
4879: &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
4880: ['future','active'],['st']);
4881: return exists($roles{"$cnum:$cdom:st"});
4882: }
4883:
4884: sub section_check_js {
4885: my $groupslist= &get_groupslist();
4886: return <<"END";
4887: function validate(caller) {
4888: var groups = new Array($groupslist);
4889: var secname = caller.value;
4890: if ((secname == 'all') || (secname == 'none')) {
4891: alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
4892: return 'error';
4893: }
4894: if (secname != '') {
4895: for (var k=0; k<groups.length; k++) {
4896: if (secname == groups[k]) {
4897: 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.");
4898: return 'error';
4899: }
4900: }
4901: }
4902: return 'ok';
4903: }
4904: END
4905: }
4906:
4907: sub set_login {
4908: my ($dom,$authformkrb,$authformint,$authformloc) = @_;
4909: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
4910: my $response;
4911: my ($authnum,%can_assign) =
4912: &Apache::loncommon::get_assignable_auth($dom);
4913: if ($authnum) {
4914: $response = &Apache::loncommon::start_data_table();
4915: if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
4916: $response .= &Apache::loncommon::start_data_table_row().
4917: '<td>'.$authformkrb.'</td>'.
4918: &Apache::loncommon::end_data_table_row()."\n";
4919: }
4920: if ($can_assign{'int'}) {
4921: $response .= &Apache::loncommon::start_data_table_row().
4922: '<td>'.$authformint.'</td>'.
4923: &Apache::loncommon::end_data_table_row()."\n"
4924: }
4925: if ($can_assign{'loc'}) {
4926: $response .= &Apache::loncommon::start_data_table_row().
4927: '<td>'.$authformloc.'</td>'.
4928: &Apache::loncommon::end_data_table_row()."\n";
4929: }
4930: $response .= &Apache::loncommon::end_data_table();
4931: }
4932: return $response;
4933: }
4934:
4935: sub course_sections {
4936: my ($sections_count,$role,$current_sec) = @_;
4937: my $output = '';
4938: my @sections = (sort {$a <=> $b} keys %{$sections_count});
4939: my $numsec = scalar(@sections);
4940: my $is_selected = ' selected="selected"';
4941: if ($numsec <= 1) {
4942: $output = '<select name="currsec_'.$role.'" >'."\n".
4943: ' <option value="">'.&mt('Select').'</option>'."\n";
4944: if ($current_sec eq 'none') {
4945: $output .=
4946: ' <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
4947: } else {
4948: $output .=
4949: ' <option value="">'.&mt('No section').'</option>'."\n";
4950: }
4951: if ($numsec == 1) {
4952: if ($current_sec eq $sections[0]) {
4953: $output .=
4954: ' <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
4955: } else {
4956: $output .=
4957: ' <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
4958: }
4959: }
4960: } else {
4961: $output = '<select name="currsec_'.$role.'" ';
4962: my $multiple = 4;
4963: if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
4964: if ($role eq 'st') {
4965: $output .= '>'."\n".
4966: ' <option value="">'.&mt('Select').'</option>'."\n";
4967: if ($current_sec eq 'none') {
4968: $output .=
4969: ' <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
4970: } else {
4971: $output .=
4972: ' <option value="">'.&mt('No section')."</option>\n";
4973: }
4974: } else {
4975: $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
4976: }
4977: foreach my $sec (@sections) {
4978: if ($current_sec eq $sec) {
4979: $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
4980: } else {
4981: $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
4982: }
4983: }
4984: }
4985: $output .= '</select>';
4986: return $output;
4987: }
4988:
4989: sub get_groupslist {
4990: my $groupslist;
4991: my %curr_groups = &Apache::longroup::coursegroups();
4992: if (%curr_groups) {
4993: $groupslist = join('","',sort(keys(%curr_groups)));
4994: $groupslist = '"'.$groupslist.'"';
4995: }
4996: return $groupslist;
4997: }
4998:
4999: sub setsections_javascript {
5000: my ($formname,$groupslist,$mode,$checkauth,$crstype) = @_;
5001: my ($checkincluded,$finish,$rolecode,$setsection_js);
5002: if ($mode eq 'upload') {
5003: $checkincluded = 'formname.name == "'.$formname.'"';
5004: $finish = "return 'ok';";
5005: $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
5006: } elsif ($formname eq 'cu') {
5007: $checkincluded = 'formname.elements[i-1].checked == true';
5008: if ($checkauth) {
5009: $finish = "var authcheck = auth_check();\n".
5010: " if (authcheck == 'ok') {\n".
5011: " formname.submit();\n".
5012: " }\n";
5013: } else {
5014: $finish = 'formname.submit()';
5015: }
5016: $rolecode = "var match = str.split('_');
5017: var role = match[3];\n";
5018: } elsif ($formname eq 'enrollstudent') {
5019: $checkincluded = 'formname.name == "'.$formname.'"';
5020: if ($checkauth) {
5021: $finish = "var authcheck = auth_check();\n".
5022: " if (authcheck == 'ok') {\n".
5023: " formname.submit();\n".
5024: " }\n";
5025: } else {
5026: $finish = 'formname.submit()';
5027: }
5028: $rolecode = "var match = str.split('_');
5029: var role = match[1];\n";
5030: } else {
5031: $checkincluded = 'formname.name == "'.$formname.'"';
5032: $finish = "seccheck = 'ok';";
5033: $rolecode = "var match = str.split('_');
5034: var role = match[1];\n";
5035: $setsection_js = "var seccheck = 'alert';";
5036: }
5037: my %alerts = &Apache::lonlocal::texthash(
5038: secd => 'Section designations do not apply to Course Coordinator roles.',
5039: sedn => 'Section designations do not apply to Coordinator roles.',
5040: accr => 'A course coordinator role will be added with access to all sections.',
5041: acor => 'A coordinator role will be added with access to all sections',
5042: inea => 'In each course, each user may only have one student role at a time.',
5043: inco => 'In each community, each user may only have one member role at a time.',
5044: youh => 'You had selected ',
5045: secs => 'sections.',
5046: plmo => 'Please modify your selections so they include no more than one section.',
5047: mayn => 'may not be used as the name for a section, as it is a reserved word.',
5048: plch => 'Please choose a different section name.',
5049: mnot => 'may not be used as a section name, as it is the name of a course group.',
5050: secn => 'Section names and group names must be distinct. Please choose a different section name.',
5051: nonw => 'Section names may only contain letters or numbers.',
5052: );
5053: $setsection_js .= <<"ENDSECCODE";
5054:
5055: function setSections(formname,crstype) {
5056: var re1 = /^currsec_/;
5057: var re2 =/\\W/;
5058: var trimleading = /^\\s+/;
5059: var trimtrailing = /\\s+\$/;
5060: var groups = new Array($groupslist);
5061: for (var i=0;i<formname.elements.length;i++) {
5062: var str = formname.elements[i].name;
5063: var checkcurr = str.match(re1);
5064: if (checkcurr != null) {
5065: var num = i;
5066: if ($checkincluded) {
5067: $rolecode
5068: if (role == 'cc' || role == 'co') {
5069: if (role == 'cc') {
5070: alert("$alerts{'secd'}\\n$alerts{'accr'}");
5071: } else {
5072: alert("$alerts{'sedn'}\\n$alerts{'acor'}");
5073: }
5074: } else {
5075: var sections = '';
5076: var numsec = 0;
5077: var fromexisting = new Array();
5078: for (var j=0; j<formname.elements[num].length; j++) {
5079: if (formname.elements[num].options[j].selected == true ) {
5080: var addsec = formname.elements[num].options[j].value;
5081: if ((addsec != "") && (addsec != null)) {
5082: fromexisting.push(addsec);
5083: if (numsec == 0) {
5084: sections = addsec;
5085: } else {
5086: sections = sections + "," + addsec;
5087: }
5088: numsec ++;
5089: }
5090: }
5091: }
5092: var newsecs = formname.elements[num+1].value;
5093: var validsecs = new Array();
5094: var validsecstr = '';
5095: var badsecs = new Array();
5096: if (newsecs != null && newsecs != "") {
5097: var numsplit;
5098: if (newsecs.indexOf(',') == -1) {
5099: numsplit = new Array(newsecs);
5100: } else {
5101: numsplit = newsecs.split(/,/g);
5102: }
5103: for (var m=0; m<numsplit.length; m++) {
5104: var newsec = numsplit[m];
5105: newsec = newsec.replace(trimleading,'');
5106: newsec = newsec.replace(trimtrailing,'');
5107: if (re2.test(newsec) == true) {
5108: badsecs.push(newsec);
5109: } else {
5110: if (newsec != '') {
5111: var isnew = 1;
5112: if (fromexisting != null) {
5113: for (var n=0; n<fromexisting.length; n++) {
5114: if (newsec == fromexisting[n]) {
5115: isnew = 0;
5116: }
5117: }
5118: }
5119: if (isnew == 1) {
5120: validsecs.push(newsec);
5121: }
5122: }
5123: }
5124: }
5125: if (badsecs.length > 0) {
5126: alert("$alerts{'nonw'}\\n$alerts{'plch'}");
5127: return;
5128: }
5129: numsec = numsec + validsecs.length;
5130: }
5131: if ((role == 'st') && (numsec > 1)) {
5132: if (crstype == 'Community') {
5133: alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
5134: } else {
5135: alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
5136: }
5137: return;
5138: } else {
5139: if (validsecs != null) {
5140: for (var j=0; j<validsecs.length; j++) {
5141: if (validsecstr == '' || validsecstr == null) {
5142: validsecstr = validsecs[j];
5143: } else {
5144: validsecstr += ','+validsecs[j];
5145: }
5146: if ((validsecs[j] == 'all') ||
5147: (validsecs[j] == 'none')) {
5148: alert("'"+validsecs[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
5149: return;
5150: }
5151: for (var k=0; k<groups.length; k++) {
5152: if (validsecs[j] == groups[k]) {
5153: alert("'"+validsecs[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
5154: return;
5155: }
5156: }
5157: }
5158: }
5159: }
5160: if ((validsecstr != '') && (validsecstr != null)) {
5161: if ((sections == '') || (sections == null)) {
5162: sections = validsecstr;
5163: } else {
5164: sections = sections + "," + validsecstr;
5165: }
5166: }
5167: formname.elements[num+2].value = sections;
5168: }
5169: }
5170: }
5171: }
5172: $finish
5173: }
5174: ENDSECCODE
5175: return $setsection_js;
5176: }
5177:
5178: sub can_create_user {
5179: my ($dom,$context,$usertype) = @_;
5180: my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
5181: my $cancreate = 1;
5182: if (&Apache::lonnet::allowed('mau',$dom)) {
5183: return $cancreate;
5184: }
5185: if (ref($domconf{'usercreation'}) eq 'HASH') {
5186: if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
5187: if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
5188: my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
5189: if ($creation eq 'none') {
5190: $cancreate = 0;
5191: } elsif ($creation ne 'any') {
5192: if (defined($usertype)) {
5193: if ($creation ne $usertype) {
5194: $cancreate = 0;
5195: }
5196: }
5197: }
5198: }
5199: }
5200: }
5201: return $cancreate;
5202: }
5203:
5204: sub can_modify_userinfo {
5205: my ($context,$dom,$fields,$userroles) = @_;
5206: my %domconfig =
5207: &Apache::lonnet::get_dom('configuration',['usermodification'],
5208: $dom);
5209: my %canmodify;
5210: if (ref($fields) eq 'ARRAY') {
5211: foreach my $field (@{$fields}) {
5212: $canmodify{$field} = 0;
5213: if (&Apache::lonnet::allowed('mau',$dom)) {
5214: $canmodify{$field} = 1;
5215: } else {
5216: if (ref($domconfig{'usermodification'}) eq 'HASH') {
5217: if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
5218: if (ref($userroles) eq 'ARRAY') {
5219: foreach my $role (@{$userroles}) {
5220: my $testrole;
5221: if ($context eq 'selfcreate') {
5222: $testrole = $role;
5223: } else {
5224: if ($role =~ /^cr\//) {
5225: $testrole = 'cr';
5226: } else {
5227: $testrole = $role;
5228: }
5229: }
5230: if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
5231: if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
5232: $canmodify{$field} = 1;
5233: last;
5234: }
5235: }
5236: }
5237: } else {
5238: foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
5239: if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
5240: if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
5241: $canmodify{$field} = 1;
5242: last;
5243: }
5244: }
5245: }
5246: }
5247: }
5248: } elsif ($context eq 'course') {
5249: if (ref($userroles) eq 'ARRAY') {
5250: if (grep(/^st$/,@{$userroles})) {
5251: $canmodify{$field} = 1;
5252: }
5253: } else {
5254: $canmodify{$field} = 1;
5255: }
5256: }
5257: }
5258: }
5259: }
5260: return %canmodify;
5261: }
5262:
5263: sub check_usertype {
5264: my ($dom,$uname,$rules) = @_;
5265: my $usertype;
5266: if (ref($rules) eq 'HASH') {
5267: my @user_rules = keys(%{$rules});
5268: if (@user_rules > 0) {
5269: my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
5270: if (keys(%rule_check) > 0) {
5271: $usertype = 'unofficial';
5272: foreach my $item (keys(%rule_check)) {
5273: if ($rule_check{$item}) {
5274: $usertype = 'official';
5275: last;
5276: }
5277: }
5278: }
5279: }
5280: }
5281: return $usertype;
5282: }
5283:
5284: sub roles_by_context {
5285: my ($context,$custom,$crstype) = @_;
5286: my @allroles;
5287: if ($context eq 'course') {
5288: @allroles = ('st');
5289: if ($env{'request.role'} =~ m{^dc\./}) {
5290: push(@allroles,'ad');
5291: }
5292: push(@allroles,('ta','ep','in'));
5293: if ($crstype eq 'Community') {
5294: push(@allroles,'co');
5295: } else {
5296: push(@allroles,'cc');
5297: }
5298: if ($custom) {
5299: push(@allroles,'cr');
5300: }
5301: } elsif ($context eq 'author') {
5302: @allroles = ('ca','aa');
5303: } elsif ($context eq 'domain') {
5304: @allroles = ('li','ad','dg','sc','au','dc');
5305: }
5306: return @allroles;
5307: }
5308:
5309: sub get_permission {
5310: my ($context,$crstype) = @_;
5311: my %permission;
5312: if ($context eq 'course') {
5313: my $custom = 1;
5314: my @allroles = &roles_by_context($context,$custom,$crstype);
5315: foreach my $role (@allroles) {
5316: if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
5317: $permission{'cusr'} = 1;
5318: last;
5319: }
5320: }
5321: if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
5322: $permission{'custom'} = 1;
5323: }
5324: if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
5325: $permission{'view'} = 1;
5326: }
5327: if (!$permission{'view'}) {
5328: my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
5329: $permission{'view'} = &Apache::lonnet::allowed('vcl',$scope);
5330: if ($permission{'view'}) {
5331: $permission{'view_section'} = $env{'request.course.sec'};
5332: }
5333: }
5334: if (!$permission{'cusr'}) {
5335: if ($env{'request.course.sec'} ne '') {
5336: my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
5337: $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
5338: if ($permission{'cusr'}) {
5339: $permission{'cusr_section'} = $env{'request.course.sec'};
5340: }
5341: }
5342: }
5343: if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
5344: $permission{'grp_manage'} = 1;
5345: }
5346: } elsif ($context eq 'author') {
5347: $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
5348: $permission{'view'} = $permission{'cusr'};
5349: } else {
5350: my @allroles = &roles_by_context($context);
5351: foreach my $role (@allroles) {
5352: if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
5353: $permission{'cusr'} = 1;
5354: last;
5355: }
5356: }
5357: if (!$permission{'cusr'}) {
5358: if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
5359: $permission{'cusr'} = 1;
5360: }
5361: }
5362: if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
5363: $permission{'custom'} = 1;
5364: }
5365: $permission{'view'} = $permission{'cusr'};
5366: }
5367: my $allowed = 0;
5368: foreach my $perm (values(%permission)) {
5369: if ($perm) { $allowed=1; last; }
5370: }
5371: return (\%permission,$allowed);
5372: }
5373:
5374: # ==================================================== Figure out author access
5375:
5376: sub authorpriv {
5377: my ($auname,$audom)=@_;
5378: unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
5379: || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; } return 1;
5380: }
5381:
5382: sub roles_on_upload {
5383: my ($context,$setting,$crstype,%customroles) = @_;
5384: my (@possible_roles,@permitted_roles);
5385: @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
5386: foreach my $role (@possible_roles) {
5387: if ($role eq 'cr') {
5388: push(@permitted_roles,keys(%customroles));
5389: } else {
5390: push(@permitted_roles,$role);
5391: }
5392: }
5393: return @permitted_roles;
5394: }
5395:
5396: sub get_course_identity {
5397: my ($cid) = @_;
5398: my ($cnum,$cdom,$cdesc);
5399: if ($cid eq '') {
5400: $cid = $env{'request.course.id'}
5401: }
5402: if ($cid ne '') {
5403: $cnum = $env{'course.'.$cid.'.num'};
5404: $cdom = $env{'course.'.$cid.'.domain'};
5405: $cdesc = $env{'course.'.$cid.'.description'};
5406: if ($cnum eq '' || $cdom eq '') {
5407: my %coursehash =
5408: &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
5409: $cdom = $coursehash{'domain'};
5410: $cnum = $coursehash{'num'};
5411: $cdesc = $coursehash{'description'};
5412: }
5413: }
5414: return ($cnum,$cdom,$cdesc);
5415: }
5416:
5417: sub dc_setcourse_js {
5418: my ($formname,$mode,$context) = @_;
5419: my ($dc_setcourse_code,$authen_check);
5420: my $cctext = &Apache::lonnet::plaintext('cc');
5421: my $cotext = &Apache::lonnet::plaintext('co');
5422: my %alerts = §ioncheck_alerts();
5423: my $role = 'role';
5424: if ($mode eq 'upload') {
5425: $role = 'courserole';
5426: } else {
5427: $authen_check = &verify_authen($formname,$context);
5428: }
5429: $dc_setcourse_code = (<<"SCRIPTTOP");
5430: $authen_check
5431:
5432: function setCourse() {
5433: var course = document.$formname.dccourse.value;
5434: if (course != "") {
5435: if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
5436: alert("$alerts{'curd'}");
5437: return;
5438: }
5439: var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
5440: var section="";
5441: var numsections = 0;
5442: var newsecs = new Array();
5443: for (var i=0; i<document.$formname.currsec.length; i++) {
5444: if (document.$formname.currsec.options[i].selected == true ) {
5445: if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
5446: if (numsections == 0) {
5447: section = document.$formname.currsec.options[i].value
5448: numsections = 1;
5449: }
5450: else {
5451: section = section + "," + document.$formname.currsec.options[i].value
5452: numsections ++;
5453: }
5454: }
5455: }
5456: }
5457: if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
5458: if (numsections == 0) {
5459: section = document.$formname.newsec.value
5460: }
5461: else {
5462: section = section + "," + document.$formname.newsec.value
5463: }
5464: newsecs = document.$formname.newsec.value.split(/,/g);
5465: numsections = numsections + newsecs.length;
5466: }
5467: if ((userrole == 'st') && (numsections > 1)) {
5468: if (document.$formname.crstype.value == 'Community') {
5469: alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
5470: } else {
5471: alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
5472: }
5473: return;
5474: }
5475: for (var j=0; j<newsecs.length; j++) {
5476: if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
5477: alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
5478: return;
5479: }
5480: if (document.$formname.groups.value != '') {
5481: var groups = document.$formname.groups.value.split(/,/g);
5482: for (var k=0; k<groups.length; k++) {
5483: if (newsecs[j] == groups[k]) {
5484: if (document.$formname.crstype.value == 'Community') {
5485: alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
5486: } else {
5487: alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
5488: }
5489: return;
5490: }
5491: }
5492: }
5493: }
5494: if ((userrole == 'cc') && (numsections > 0)) {
5495: alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
5496: section = "";
5497: }
5498: if ((userrole == 'co') && (numsections > 0)) {
5499: alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
5500: section = "";
5501: }
5502: SCRIPTTOP
5503: if ($mode ne 'upload') {
5504: $dc_setcourse_code .= (<<"ENDSCRIPT");
5505: var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
5506: var numcourse = getIndex(document.$formname.dccourse);
5507: if (numcourse == "-1") {
5508: if (document.$formname.type == 'Community') {
5509: alert("$alerts{'thwc'}");
5510: } else {
5511: alert("$alerts{'thwa'}");
5512: }
5513: return;
5514: }
5515: else {
5516: document.$formname.elements[numcourse].name = "act"+coursename;
5517: var numnewsec = getIndex(document.$formname.newsec);
5518: if (numnewsec != "-1") {
5519: document.$formname.elements[numnewsec].name = "sec"+coursename;
5520: document.$formname.elements[numnewsec].value = section;
5521: }
5522: var numstart = getIndex(document.$formname.start);
5523: if (numstart != "-1") {
5524: document.$formname.elements[numstart].name = "start"+coursename;
5525: }
5526: var numend = getIndex(document.$formname.end);
5527: if (numend != "-1") {
5528: document.$formname.elements[numend].name = "end"+coursename
5529: }
5530: }
5531: }
5532: var authcheck = auth_check();
5533: if (authcheck == 'ok') {
5534: document.$formname.submit();
5535: }
5536: }
5537: ENDSCRIPT
5538: } else {
5539: $dc_setcourse_code .= "
5540: document.$formname.sections.value = section;
5541: }
5542: return 'ok';
5543: }
5544: ";
5545: }
5546: $dc_setcourse_code .= (<<"ENDSCRIPT");
5547:
5548: function getIndex(caller) {
5549: for (var i=0;i<document.$formname.elements.length;i++) {
5550: if (document.$formname.elements[i] == caller) {
5551: return i;
5552: }
5553: }
5554: return -1;
5555: }
5556: ENDSCRIPT
5557: return $dc_setcourse_code;
5558: }
5559:
5560: sub verify_authen {
5561: my ($formname,$context) = @_;
5562: my %alerts = &authcheck_alerts();
5563: my $finish = "return 'ok';";
5564: if ($context eq 'author') {
5565: $finish = "document.$formname.submit();";
5566: }
5567: my $outcome = <<"ENDSCRIPT";
5568:
5569: function auth_check() {
5570: var logintype;
5571: if (document.$formname.login.length) {
5572: if (document.$formname.login.length > 0) {
5573: var loginpicked = 0;
5574: for (var i=0; i<document.$formname.login.length; i++) {
5575: if (document.$formname.login[i].checked == true) {
5576: loginpicked = 1;
5577: logintype = document.$formname.login[i].value;
5578: }
5579: }
5580: if (loginpicked == 0) {
5581: alert("$alerts{'authen'}");
5582: return;
5583: }
5584: }
5585: } else {
5586: logintype = document.$formname.login.value;
5587: }
5588: if (logintype == 'nochange') {
5589: return 'ok';
5590: }
5591: var argpicked = document.$formname.elements[logintype+'arg'].value;
5592: if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
5593: var alertmsg = '';
5594: switch (logintype) {
5595: case 'krb':
5596: alertmsg = '$alerts{'krb'}';
5597: break;
5598: case 'int':
5599: alertmsg = '$alerts{'ipass'}';
5600: case 'fsys':
5601: alertmsg = '$alerts{'ipass'}';
5602: break;
5603: case 'loc':
5604: alertmsg = '';
5605: break;
5606: default:
5607: alertmsg = '';
5608: }
5609: if (alertmsg != '') {
5610: alert(alertmsg);
5611: return;
5612: }
5613: }
5614: $finish
5615: }
5616: ENDSCRIPT
5617: }
5618:
5619: sub sectioncheck_alerts {
5620: my %alerts = &Apache::lonlocal::texthash(
5621: curd => 'You must select a course or community in the current domain',
5622: inea => 'In each course, each user may only have one student role at a time',
5623: inco => 'In each community, each user may only have one member role at a time',
5624: youh => 'You had selected',
5625: sect => 'sections',
5626: plsm => 'Please modify your selections so they include no more than one section',
5627: mayn => 'may not be used as the name for a section, as it is a reserved word',
5628: plsc => 'Please choose a different section name',
5629: mayt => 'may not be used as the name for a section, as it is the name of a course group',
5630: mayc => 'may not be used as the name for a section, as it is the name of a community group',
5631: secn => 'Section names and group names must be distinct',
5632: secd => 'Section designations do not apply to ',
5633: role => 'roles',
5634: accr => 'role will be added with access to all sections',
5635: thwa => 'There was a problem with your course selection',
5636: thwc => 'There was a problem with your community selection',
5637: );
5638: return %alerts;
5639: }
5640:
5641: sub authcheck_alerts {
5642: my %alerts =
5643: &Apache::lonlocal::texthash(
5644: authen => 'You must choose an authentication type.',
5645: krb => 'You need to specify the Kerberos domain.',
5646: ipass => 'You need to specify the initial password.',
5647: );
5648: return %alerts;
5649: }
5650:
5651: 1;
5652:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>