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