File:
[LON-CAPA] /
loncom /
enrollment /
Enrollment.pm
Revision
1.44:
download - view:
text,
annotated -
select for diffs
Fri Mar 1 05:20:01 2013 UTC (11 years, 5 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Set institutional status when auto-enrollment adds a new LON-CAPA user.
- Store credits earned for a course in classlist.db for students for whom
credits are different from course default.
- Credits can be part of XML record for each student in course roster
retrieved from institutional data course via customized localenroll.pm
1: # Automated Enrollment manager
2: # $Id: Enrollment.pm,v 1.44 2013/03/01 05:20:01 raeburn Exp $
3: #
4: # Copyright Michigan State University Board of Trustees
5: #
6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
7: #
8: # LON-CAPA is free software; you can redistribute it and/or modify
9: # it under the terms of the GNU General Public License as published by
10: # the Free Software Foundation; either version 2 of the License, or
11: # (at your option) any later version.
12: #
13: # LON-CAPA is distributed in the hope that it will be useful,
14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: # GNU General Public License for more details.
17: #
18: # You should have received a copy of the GNU General Public License
19: # along with LON-CAPA; if not, write to the Free Software
20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: #
22: # /home/httpd/html/adm/gpl.txt
23: #
24: # http://www.lon-capa.org/
25: #
26: package LONCAPA::Enrollment;
27:
28: use Apache::loncoursedata;
29: use Apache::lonnet;
30: use Apache::loncommon();
31: use Apache::lonmsg;
32: use Apache::lonlocal;
33: use HTML::Entities;
34: use LONCAPA::Configuration;
35: use Time::Local;
36: use lib '/home/httpd/lib/perl';
37:
38: use strict;
39:
40: sub update_LC {
41: my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,
42: $showcredits,$defaultcredits,$classesref,$groupref,$logmsg,$newusermsg,
43: $context,$phototypes) = @_;
44: # Get institutional code and title of this class
45: my %courseinfo = ();
46: &get_courseinfo($dom,$crs,\%courseinfo);
47: # Get current LON-CAPA student enrollment for this class
48: my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
49: my $cid = $dom."_".$crs;
50: my $roster = &Apache::loncoursedata::get_classlist($dom,$crs);
51: my $cend = &Apache::loncoursedata::CL_END;
52: my $cstart = &Apache::loncoursedata::CL_START;
53: my $stuid=&Apache::loncoursedata::CL_ID;
54: my $sec=&Apache::loncoursedata::CL_SECTION;
55: my $status=&Apache::loncoursedata::CL_STATUS;
56: my $type=&Apache::loncoursedata::CL_TYPE;
57: my $lockedtype=&Apache::loncoursedata::CL_LOCKEDTYPE;
58: my $credidx=&Apache::loncoursedata::CL_CREDITS;
59: my @localstudents = ();
60: my @futurestudents = ();
61: my @activestudents = ();
62: my @excludedstudents = ();
63: my $currlist;
64: foreach my $uname (keys %{$roster} ) {
65: if ($uname =~ m/^(.+):$dom$/) {
66: if ($$roster{$uname}[$status] eq "Active") {
67: push @activestudents, $1;
68: @{$$currlist{$1}} = @{$$roster{$uname}};
69: push @localstudents, $1;
70: } elsif ( ($$roster{$uname}[$cstart] > time) && ($$roster{$uname}[$cend] > time || $$roster{$uname}[$cend] == 0 || $$roster{$uname}[$cend] eq '') ) {
71: push @futurestudents, $1;
72: @{$$currlist{$1}} = @{$$roster{$uname}};
73: push @localstudents, $1;
74: } elsif ($$roster{$uname}[$lockedtype] == 1) {
75: push @excludedstudents, $1;
76: }
77: }
78: }
79: my $linefeed = '';
80: my $addresult = '';
81: my $dropresult = '';
82: my $switchresult = '';
83: my $photoresult = '';
84: if ($context eq "updatenow") {
85: $linefeed = "</li>\n<li>";
86: } elsif ($context eq "automated") {
87: $linefeed = "\n";
88: }
89: my $enrollcount = 0;
90: my $dropcount = 0;
91: my $switchcount = 0;
92:
93: # Get role names
94: my %longroles = ();
95: open(FILE,"<$$configvars{'lonTabDir'}.'/rolesplain.tab");
96: my @rolesplain = <FILE>;
97: close(FILE);
98: foreach my $item (@rolesplain) {
99: if ($_ =~ /^(st|ta|ex|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
100: if ($courseinfo{'type'} eq 'Community') {
101: unless($1 eq 'cc') {
102: $longroles{$1} = $3;
103: }
104: } else {
105: unless($1 eq 'co') {
106: $longroles{$1} = $2;
107: }
108: }
109: }
110: }
111:
112: srand( time() ^ ($$ + ($$ << 15)) ); # Seed rand in case initial passwords have to be generated for new users.
113:
114: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class
115: my @LCids = ();
116: my %unameFromLCid = ();
117: foreach my $uname (sort keys %{$currlist}) {
118: my $stuID = $$currlist{$uname}[$stuid];
119: if (!grep/^$stuID$/,@LCids) {
120: push @LCids, $stuID;
121: @{$unameFromLCid{$stuID}} = ();
122: }
123: push @{$unameFromLCid{$stuID}},$uname;
124: }
125:
126: # Get latest institutional enrollment for this class.
127: my %allenrolled = ();
128: my @reg_students = ();
129: my %place = &place_hash();
130: my %ucount = ();
131: my %enrollinfo = ();
132: foreach my $class (@{$classesref}) {
133: my %enrolled = ();
134: &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
135: foreach my $uname (sort keys %enrolled ) {
136: if (!grep/^$uname$/,@reg_students) {
137: push @reg_students,$uname;
138: $ucount{$uname} = 0;
139: @{$allenrolled{$uname}} = ();
140: }
141: @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
142: $ucount{$uname} ++;
143: }
144: }
145:
146: # Check for multiple sections for a single student
147: my @okusers = ();
148: foreach my $uname (@reg_students) {
149: if (grep/^$uname$/,@excludedstudents) {
150: $$logmsg .= &mt('No re-enrollment for [_1] - user was previously manually unenrolled and locked.',$uname).$linefeed;
151: } elsif (@{$allenrolled{$uname}} > 1) {
152: my @sections = ();
153: my $saved;
154: for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
155: my @stuinfo = @{$allenrolled{$uname}[$i]};
156: my $secnum = $stuinfo[ $place{'groupID'} ];
157: unless ($secnum eq '') {
158: unless (grep/^$secnum$/,@sections) {
159: $saved = $i;
160: push @sections,$secnum;
161: }
162: }
163: }
164: if (@sections == 0) {
165: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
166: push @okusers, $uname;
167: }
168: elsif (@sections == 1) {
169: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
170: push @okusers, $uname;
171: }
172: elsif (@sections > 1) {
173: $$logmsg .= &mt('[_1] appears in classlists for more than one section of this course, i.e. in sections: ',$uname);
174: foreach (@sections) {
175: $$logmsg .= " $_,";
176: }
177: chop($$logmsg);
178: $$logmsg .= '. '.&mt('Because of this ambiguity, no enrollment action was taken for this student.').$linefeed;
179: }
180: } else {
181: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
182: push @okusers, $uname;
183: }
184: }
185: # Get mapping of student/employee IDs to usernames for users in institutional data for this class
186: my @allINids = ();
187: my %unameFromINid = ();
188: foreach my $uname (@okusers) {
189: $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
190: my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
191: if (grep/^$stuID$/,@allINids) {
192: push @{$unameFromINid{$stuID}},$uname;
193: } else {
194: push @allINids, $stuID;
195: @{$unameFromINid{$stuID}} = $uname;
196: }
197: }
198:
199: # Explicitly allow access to creation/modification of students if called as an automated process.
200: if ($context eq 'automated') {
201: $env{'allowed.cst'}='F';
202: }
203:
204: # Compare IDs with existing LON-CAPA enrollment for this class
205: foreach my $uname (@okusers) {
206: unless ($uname eq '') {
207: my %uidhash=&Apache::lonnet::idrget($dom,$uname);
208: my @stuinfo = @{$enrollinfo{$uname}};
209: my ($access,$added,$inststatus);
210: my $credits;
211: if ($showcredits) {
212: $credits = $stuinfo[$place{'credits'}];
213: $credits =~ s/[^\d\.]//g;
214: if ($credits eq $defaultcredits) {
215: undef($credits);
216: }
217: }
218: $inststatus = $stuinfo[$place{inststatus}];
219: if (grep/^$uname$/,@localstudents) {
220: # Check for studentID changes
221: if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) ) {
222: unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
223: $$logmsg .= &mt('Change in ID for [_1]. StudentID in LON-CAPA system is [_2]; StudentID in institutional data is [_3].',$uname,$uidhash{$uname},$stuinfo[ $place{studentID} ]).$linefeed;
224: }
225: }
226: # Check for switch from manual to auto
227: unless (($$currlist{$uname}[$type] eq "auto") || ($$currlist{$uname}[$lockedtype] eq "1") || (!$adds) ) {
228: # drop manually added student
229: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid,'',$context);
230: # re-enroll as auto student
231: if ($drop_reply !~ /^ok/) {
232: $$logmsg .= &mt('An error occured during the attempt to convert [_1] from a manual type to an auto type student - [_2].',$uname,$drop_reply).$linefeed;
233: } else {
234: # re-enroll as auto student
235: my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc);
236: &prepare_add($authtype,$autharg,$enddate,$startdate,\@stuinfo,\%place,\$dom,\$uname,\$auth,\$authparam,\$first,\$middle,\$last,\$gene,\$usec,\$end,\$start,\$emailaddr,\$pid,\$emailenc);
237: if ($$currlist{$uname}[$sec] ne $usec) {
238: my $showoldsec = $$currlist{$uname}[$sec];
239: if ($$currlist{$uname}[$sec] eq '') {
240: $showoldsec = &mt('none');
241: }
242: my $showsec = $usec;
243: if ($usec eq '') {
244: $showsec = &mt('none');
245: }
246: $switchresult .= &mt('Section for [_1] switched from [_2] to [_3].',$uname,$showoldsec,$showsec).$linefeed;
247: if ($context eq 'automated') {
248: $$logmsg .= &mt('Section switch for [_1] from [_2] to [_3].',$uname,$showoldsec,$usec).$linefeed;
249: }
250: $switchcount ++;
251: }
252: &execute_add($context,'switchtype',$uname,$dom,$auth,
253: $authparam,$first,$middle,$last,$gene,
254: $pid,$usec,$end,$start,$emailenc,
255: $credits,$cid,\$addresult,\$enrollcount,
256: $linefeed,$logmsg);
257: $added = 1;
258: }
259: }
260: # Check for section changes
261: if ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
262: # Check for access date changes for students with access starting in the future.
263: if ( (grep/^$uname$/,@futurestudents) && ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
264: my $datechange = &datechange_check($$currlist{$uname}[$cstart],$$currlist{$uname}[$cend],$startdate,$enddate);
265: if ($datechange) {
266: my $modify_access_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits);
267: $access = &showaccess($enddate,$startdate);
268: if ($modify_access_result =~ /^ok/) {
269: $$logmsg .= &mt('Change in access dates for [_1].',$uname).$access.$linefeed;
270: $added = 1;
271: } else {
272: $$logmsg .= &mt('Error when attempting to change start and/or end access dates for [_1] in section: [_2] -error [_3].',$uname,$stuinfo[$place{groupID}],$modify_access_result).$linefeed;
273: }
274: }
275: }
276: } else {
277: if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
278: # Delete from roles.db for current section
279: my $expiretime = time;
280: my $uurl='/'.$cid;
281: $uurl=~s/\_/\//g;
282: if ($$currlist{$uname}[$sec]) {
283: $uurl.='/'.$$currlist{$uname}[$sec];
284: }
285: my $expire_role_result = &Apache::lonnet::assignrole($dom,$uname,$uurl,'st',$expiretime,'','','',$context);
286: if ($expire_role_result eq 'ok') {
287: my $modify_section_result;
288: if (grep/^$uname$/,@activestudents) {
289: $modify_section_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$$currlist{$uname}[$cend],$$currlist{$uname}[$cstart],'auto','',$cid,'',$context,$credits);
290: } else {
291: $modify_section_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits);
292: $access = &showaccess($enddate,$startdate);
293: }
294: if ($modify_section_result =~ /^ok/) {
295: $switchresult .= &mt('Section for [_1] switched from old section: [_2] to new section: [_3].',$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ]).$access.$linefeed;
296: $added = 1;
297: if ($context eq 'automated') {
298: $$logmsg .= &mt('Section switch for [_1] from [_2] to [_3].',$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ]).$linefeed;
299: }
300: $switchcount ++;
301: } else {
302: $$logmsg .= &mt("Error when attempting section change for [_1], from old section: '[_2]' to new section: '[_3]' -error: [_4]",$uname,$$currlist{$uname}[$sec],$stuinfo[ $place{groupID} ],$modify_section_result).$linefeed;
303: }
304: } else {
305: $$logmsg .= &mt("Error when attempting to expire role for [_1] in old section: '[_2]' -error: '[_3]'.",$uname,$$currlist{$uname}[$sec],$expire_role_result).$linefeed;
306: }
307: }
308: }
309: # Check for credits changes
310: if (($showcredits) &&
311: ($$currlist{$uname}[$credidx] ne $credits) && (!$added)) {
312: my $modify_credits_result =
313: &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid,'',$context,$credits);
314: if ($modify_credits_result =~ /^ok/) {
315: if ($credits ne '') {
316: $$logmsg .= &mt('Credits change for [_1] from [_2] to [_3].',$uname,$$currlist{$uname}[$credidx],$credits).$linefeed;
317: } else {
318: $$logmsg .= &mt('Credits change for [_1] from [_2] to course default [_3].',$uname,$$currlist{$uname}[$credidx],$defaultcredits).$linefeed;
319: }
320: } else {
321: $$logmsg .= &mt('Error when attempting to change credits for [_1] in section: [_2] -error [_3].',$uname,$stuinfo[$place{groupID}],$modify_credits_result).$linefeed;
322: }
323: }
324: } else {
325: # Check for changed usernames by checking studentIDs
326: if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
327: foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } } ) {
328: $$logmsg .= &mt('A possible change in username has been detected for a student enrolled in this course.').' '.&mt('The existing LON-CAPA classlist contains user: [_1] and student/employee ID: [_2].',$match,$stuinfo[ $place{studentID} ]);
329: if (grep/^$match$/,@okusers) {
330: $$logmsg .= &mt('The username [_1] remains in the institutional classlist, but the same student/employee ID is used for new user: [_2] now found in the institutional classlist.',$match,$uname).' '.&mt('You may need to contact your Domain Coordinator to determine how to resolve this issue and whether to move student data files for user: [_1] to [_2].',$match,$uname).' ';
331: } else {
332: unless ($drops == 1) {
333: $$logmsg .= &mt('This username - [_1] - has been dropped from the institutional classlist, but the student/employee ID of this user is also used by [_2] who now appears in the institutional classlist.',$match,$uname).' '.&mt('You may need to contact your Domain Coordinator to request a move of the student data files for user: [_1] to [_2].',$match,$uname).' ';
334: }
335: }
336: $$logmsg .= &mt('Because of this student/employee ID conflict, the new username - [_1] - has not been added to the LON-CAPA classlist',$uname).$linefeed;
337: }
338: } elsif ($adds == 1) {
339: my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc,$credithours);
340: &prepare_add($authtype,$autharg,$enddate,$startdate,\@stuinfo,\%place,\$dom,\$uname,\$auth,\$authparam,\$first,\$middle,\$last,\$gene,\$usec,\$end,\$start,\$emailaddr,\$pid,\$emailenc);
341: # Check for existing account in this LON-CAPA domain for this username
342: my $uhome=&Apache::lonnet::homeserver($uname,$dom);
343: if ($uhome eq 'no_host') { # User does not exist
344: my $args = {'auth' => $auth,
345: 'authparam' => $authparam,
346: 'emailenc' => $emailenc,
347: 'udom' => $dom,
348: 'uname' => $uname,
349: 'pid' => $pid,
350: 'first' => $first,
351: 'middle' => $middle,
352: 'last' => $last,
353: 'gene' => $gene,
354: 'usec' => $usec,
355: 'end' => $end,
356: 'start' => $start,
357: 'emailaddr' => $emailaddr,
358: 'cid' => $cid,
359: 'crs' => $crs,
360: 'cdom' => $dom,
361: 'context' => $context,
362: 'linefeed' => $linefeed,
363: 'inststatus' => $inststatus,
364: 'role' => 'st',
365: };
366: if ($credits) {
367: $args->{'credits'} = $credits;
368: }
369: my $outcome = &create_newuser($args,$logmsg,$newusermsg,\$enrollcount,\$addresult,\%longroles,\%courseinfo,$context);
370: } else {
371: &execute_add($context,'newstudent',$uname,$dom,$auth,
372: $authparam,$first,$middle,$last,$gene,$pid,
373: $usec,$end,$start,$emailenc,$credits,
374: $cid,\$addresult,\$enrollcount,$linefeed,
375: $logmsg);
376: }
377: if ($courseinfo{'showphoto'}) {
378: my ($result,$resulttype) =
379: &Apache::lonnet::auto_checkphotos($uname,$dom,$pid);
380: if ($resulttype) {
381: push(@{$$phototypes{$resulttype}},$uname);
382: }
383: }
384: }
385: }
386: }
387: }
388: if ($courseinfo{'showphoto'}) {
389: if (keys(%{$phototypes})>0) {
390: my %lt = &photo_response_types();
391: foreach my $type (sort(keys(%{$phototypes}))) {
392: my $numphoto = @{$$phototypes{$type}};
393: if ($numphoto > 0) {
394: if ($context eq 'updatenow') {
395: $photoresult .= '<br /><b>'.
396: &mt('For [_1] students, photos ',$numphoto).
397: $lt{$type}.'</b><ul><li>';
398: } else {
399: $photoresult .= "\n".&mt("For [quant,_1,student], photos ",$numphoto).
400: $lt{$type}."\n";
401: }
402: foreach my $user (@{$$phototypes{$type}}) {
403: $photoresult .= $user.$linefeed;
404: }
405: if ($context eq 'updatenow') {
406: $photoresult = substr($photoresult,0,
407: rindex($photoresult,"<li>"));
408: $photoresult .= '</ul><br />';
409: } else {
410: $photoresult .= "\n";
411: }
412: }
413: }
414: }
415: }
416:
417: # Do drops
418: if ( ($drops == 1) && (@reg_students > 0) ) {
419: foreach my $uname (@localstudents) {
420: if ($$currlist{$uname}[$type] eq "auto") {
421: my @saved = ();
422: if (!grep/^$uname$/,@reg_students) {
423: # Check for changed usernames by checking studentIDs
424: if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
425: foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
426: $$logmsg .= &mt('A possible change in username has been detected for a student enrolled in this course.').' '.&mt('The existing LON-CAPA classlist contains user: [_1] and student/employee ID: [_2].',$uname,$$currlist{$uname}[ $place{studentID} ]).' '.&mt('This username has been dropped from the institutional classlist, but the same student/employee ID is used for user: [_1] who still appears in the institutional classlist.',$match).' '.&mt('You may need to move the student data files for user: [_1] to [_2]',$uname,$match).' '.&mt('Because of this, user [_1] has not been dropped from the course.',$uname).$linefeed;
427: push @saved,$uname;
428: }
429: } elsif (@saved == 0) {
430: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid,'',$context);
431: if ($drop_reply !~ /^ok/) {
432: $$logmsg .= &mt('An error occured during the attempt to expire the [_1] from the old section [_2] - [_3].',$uname,$$currlist{$uname}[$sec],$drop_reply).$linefeed;
433: } else {
434: $dropcount ++;
435: my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
436: $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname.' '.&mt("dropped from section: '[_1]'.",$$currlist{$uname}[$sec]).$linefeed;
437: if ($context eq 'automated') {
438: $$logmsg .= &mt('User [_1] student role expired from course.',$uname).$linefeed;
439: }
440: }
441: }
442: }
443: }
444: }
445: }
446:
447: # Terminated explictly allowed access to student creation/modification
448: if ($context eq 'automated') {
449: delete($env{'allowed.cst'});
450: }
451: if ($enrollcount > 0) {
452: if ($context eq "updatenow") {
453: $addresult = substr($addresult,0,rindex($addresult,"<li>"));
454: $addresult = &mt("The following [quant,_1,student was,students were] added to this LON-CAPA course:",$enrollcount).'<br/><ul><li>'.$addresult.'</ul><br/><br/>';
455: } else {
456: $addresult = &mt("The following [quant,_1,student was,students were] added to this LON-CAPA course:",$enrollcount)."\n\n".$addresult."\n\n";
457: }
458: }
459: if ($dropcount > 0) {
460: if ($context eq "updatenow") {
461: $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
462: $dropresult = &mt("The following [quant,_1,student was,students were] expired from this LON-CAPA course:",$dropcount).'<br/><ul><li>'.$dropresult.'</ul><br/><br/>';
463: } else {
464: $dropresult = &mt("The following [quant,_1,student was,students were] expired from this LON-CAPA course:",$dropcount)."\n\n".$dropresult."\n\n";
465: }
466: }
467: if ($switchcount > 0) {
468: if ($context eq "updatenow") {
469: $switchresult = substr($switchresult,0,rindex($switchresult,"<li>"));
470: $switchresult = &mt("The following [quant,_1,student] switched sections in this LON-CAPA course:",$switchcount).'<br/><ul><li>'.$switchresult.'</ul><br/><br/>';
471: } else {
472: $switchresult = &mt("The following [quant,_1,student] switched sections in this LON-CAPA course:",$switchcount)."\n\n".$switchresult."\n\n";
473: }
474: }
475: if ( ($adds) && ($enrollcount == 0) ) {
476: $addresult = &mt('There were no new students to add to the course.');
477: if ($context eq "updatenow") {
478: $addresult .="<br/><br/>";
479: } else {
480: $addresult .="\n";
481: }
482: }
483: if ( ($drops) && ($dropcount == 0) ) {
484: $dropresult = &mt('There were no students with roles to expire because all active students previously added to the course from institutional classlist(s) are still officially registered.');
485: if ($context eq "updatenow") {
486: $dropresult .="<br/>";
487: } else {
488: $dropresult .="\n";
489: }
490: }
491: my $changecount = $enrollcount + $dropcount + $switchcount;
492: return ($changecount,$addresult.$photoresult.$dropresult.$switchresult);
493: }
494:
495: sub create_newuser {
496: my ($args,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,
497: $courseinfo,$called_context) = @_;
498: my $auth = $args->{'auth'};
499: my $authparam = $args->{'authparam'};
500: my $emailenc = $args->{'emailenc'};
501: my $udom = $args->{'udom'};
502: my $uname = $args->{'uname'};
503: my $pid = $args->{'pid'};
504: my $first = $args->{'first'};
505: my $middle = $args->{'middle'};
506: my $last = $args->{'last'} ;
507: my $gene = $args->{'gene'};
508: my $usec = $args->{'usec'};
509: my $end = $args->{'end'};
510: my $start = $args->{'start'};
511: my $emailaddr = $args->{'emailaddr'};
512: my $cid = $args->{'cid'};
513: my $crs = $args->{'crs'};
514: my $cdom = $args->{'cdom'};
515: my $context = $args->{'context'};
516: my $linefeed = $args->{'linefeed'};
517: my $role = $args->{'role'};
518: my $inststatus = $args->{'inststatus'};
519: my $credits = $args->{'credits'};
520: my $create_passwd = 0;
521: my $authchk = '';
522: my $outcome;
523: unless ($authparam eq '') { $authchk = 'ok'; };
524: # If no account exists and passwords should be generated
525: if ($auth eq "internal") {
526: if ($authparam eq '') {
527: $authparam = &create_password();
528: if ($authparam eq '') {
529: $authchk = '';
530: } else {
531: $create_passwd = 1;
532: $authchk = 'ok';
533: }
534: }
535: } elsif ($auth eq "localauth") {
536: ($authparam,$create_passwd,$authchk) = &Apache::lonnet::auto_create_password($crs,$cdom,$authparam,$udom);
537: } elsif ($auth =~ m/^krb/) {
538: if ($authparam eq '') {
539: $$logmsg .= &mt('No Kerberos domain was provided for the new user - [_1], so the new user was not enrolled in the course',$uname).$linefeed;
540: $authchk = 'invalid';
541: }
542: } else {
543: $authchk = 'invalid';
544: $$logmsg .= &mt('An invalid authentication type was provided for the new user - [_1], so the user was not enrolled in the course.',$uname).$linefeed;
545: }
546: if ($authchk eq 'ok') {
547: # Now create user.
548: my $type = 'auto';
549: my $userurl = '/'.$cdom.'/'.$crs;
550: if ($usec ne '') {
551: $userurl .= '/'.$usec;
552: }
553: if ($context eq 'createowner' || $context eq 'createcourse') {
554: my $result = &Apache::lonnet::modifyuser($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,'1',undef,$emailaddr);
555: if ($result eq 'ok' && $context eq 'createcourse') {
556: $outcome = &Apache::loncommon::commit_standardrole($udom,$uname,$userurl,$role,$start,$end,$cdom,$crs,$usec,$called_context);
557: unless ($outcome =~ /^Error:/) {
558: $outcome = 'ok';
559: }
560: } else {
561: $outcome = $result;
562: }
563: } else {
564: $outcome=&Apache::lonnet::modifystudent($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto','',$cid,'',$called_context,$inststatus,$credits);
565: }
566: if ($outcome eq 'ok') {
567: my $access = &showaccess($end,$start);
568: my $showsec = $usec;
569: if ($usec eq '') {
570: $showsec = &mt('none');
571: }
572: $$addresult .= "$first $last ($pid) - $uname ".&mt("enrolled in section: '[_1]'.",$showsec).$access.$linefeed;
573: unless ($context eq 'createowner' || $context eq 'createcourse') {
574: $$enrollcount ++;
575: }
576: if ($called_context eq 'automated') {
577: $$logmsg .= &mt('New [_1] user [_2] added successfully.',$udom,$uname);
578: }
579: unless ($emailenc eq '' || $context eq 'createowner' || $context eq 'createcourse') {
580: my %emailHash;
581: $emailHash{'critnotification'} = $emailenc;
582: $emailHash{'notification'} = $emailenc;
583: $emailHash{'permanentemail'} = $emailenc;
584: my $putresult = &Apache::lonnet::put('environment',\%emailHash,$udom,$uname);
585: }
586: if ($create_passwd) {
587: # Send e-mail with initial password to new user at $emailaddr.
588: # If e-mail address is invalid, send password via message to courseowner i
589: # (if automated call) or to user if roster update.
590: if ($emailaddr eq '') {
591: $$newusermsg .= &mt(' username: [_1], password: [_2]',$uname,$authparam).$linefeed."\n";
592: } else {
593: my $subject = &mt('New LON-CAPA account');
594: my $body;
595: my $portalurl = 'http://'.$ENV{'SERVER_NAME'};
596: my $protocol = 'http';
597: my $lonhost=&Apache::lonnet::domain($udom,'primary');
598: if ($lonhost ne '') {
599: my $ip = &Apache::lonnet::get_host_ip($lonhost);
600: if ($Apache::lonnet::protocol{$lonhost} eq 'https') {
601: $protocol = 'https';
602: }
603: if ($ip ne '') {
604: $portalurl = $protocol.'://'.$ip
605: }
606: }
607: if ($context eq 'createowner') {
608: $body = &mt('A user account has been created for you while creating your new course in the LON-CAPA course management and online homework system.')."\n\n".&mt('You should log-in to the system using the following credentials:')."\n".&mt('username: ').$uname."\n".&mt('password: ').$authparam."\n\n".&mt('The URL you should use to access the LON-CAPA system at your institution is: ').$portalurl."\n\n";
609: } elsif ($context eq 'createcourse') {
610: $body = &mt('You have been assigned the role of [_1] in a new course: [_2] - [_3] in the LON-CAPA course management and online homework system.',$$longroles{$role},$$courseinfo{'description'},$$courseinfo{'inst_code'}).' '.&mt('As you did not have an existing user account in the system, one has been created for you.')."\n\n".&mt("You should log-in to the system using the following credentials:\nusername: [_1]\npassword: [_2]",$uname,$authparam)."\n\n".&mt('The URL you should use to access the LON-CAPA system at your institution is: ').$portalurl."\n\n";
611: } else {
612: my $access_start = 'immediately';
613: if ($start > 0) {
614: $access_start = localtime($start)
615: }
616: $body = &mt('You have been enrolled in the LON-CAPA system at your institution, because you are a registered student in a class which is using the LON-CAPA couse management and online homework system.')."\n\n".&mt("You should log-in to the system using the following credentials:\nusername: [_1]\npassword: [_2]",$uname,$authparam)."\n\n".&mt('The URL you should use to access the LON-CAPA system at your institution is: ').$portalurl."\n\n".&mt('When you log-in you will be able to access the LON-CAPA course for [_1] - [_2] starting [_3].',$$courseinfo{'description'},$$courseinfo{'inst_code'},$access_start)."\n";
617: }
618: &Apache::lonmsg::sendemail($emailaddr,$subject,$body);
619: }
620: if ($called_context eq 'automated') {
621: $$logmsg .= &mt(' Initial password - sent to ').$emailaddr.$linefeed;
622: }
623: } else {
624: if ($called_context eq 'automated') {
625: $$logmsg .= $linefeed;
626: }
627: }
628: } else {
629: $$logmsg .= &mt('An error occurred adding new user [_1] - [_2].',$uname,$outcome).$linefeed;
630: }
631: } else {
632: $$logmsg .= &mt('An error occurred adding the new user [_1] because the authcheck failed for authtype [_2] and parameter [_3].',$uname,$auth,$authparam).' '.&mt('The authcheck response was [_1].',$authchk).$linefeed;
633: }
634: return $outcome;
635: }
636:
637: sub prepare_add {
638: my ($authtype,$autharg,$enddate,$startdate,$stuinfo,$place,$dom,$uname,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc) = @_;
639: $$auth = $$stuinfo[ $$place{'authtype'} ];
640: $$authparam = $$stuinfo[ $$place{'autharg'} ];
641: $$first = $$stuinfo[ $$place{'firstname'} ];
642: $$middle = $$stuinfo[ $$place{'middlename'} ];
643: $$last = $$stuinfo[ $$place{'lastname'} ];
644: $$gene = $$stuinfo[ $$place{'generation'} ];
645: $$usec = $$stuinfo[ $$place{'groupID'} ];
646: $$end = $$stuinfo[ $$place{'enddate'} ];
647: $$start = $$stuinfo[ $$place{'startdate'} ];
648: $$emailaddr = $$stuinfo[ $$place{'email'} ];
649: $$pid = $$stuinfo[ $$place{'studentID'} ];
650:
651: # remove non alphanumeric values from section
652: $$usec =~ s/\W//g;
653:
654: unless ($$emailaddr =~/^[^\@]+\@[^\@]+$/) { $$emailaddr =''; }
655: $$emailenc = &HTML::Entities::encode($$emailaddr,'<>&"');
656:
657: # Use course defaults where entry is absent
658: if ( ($$auth eq '') || (!defined($$auth)) ) {
659: $$auth = $authtype;
660: }
661: if ( ($$authparam eq '') || (!defined($$authparam)) ) {
662: $$authparam = $autharg;
663: }
664: if ( ($$end eq '') || (!defined($$end)) ) {
665: $$end = $enddate;
666: }
667: if ( ($$start eq '') || (!defined($$start)) ) {
668: $$start = $startdate;
669: }
670: # Clean up whitespace
671: foreach ($dom,$uname,$pid,$first,$middle,$last,$gene,$usec) {
672: $$_ =~ s/(\s+$|^\s+)//g;
673: }
674: return;
675: }
676:
677: sub execute_add {
678: my ($context,$caller,$uname,$dom,$auth,$authparam,$first,$middle,$last,
679: $gene,$pid,$usec,$end,$start,$emailenc,$credits,$cid,$addresult,
680: $enrollcount,$linefeed,$logmsg) = @_;
681: # Get the user's information and authentication
682: my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification','permanentemail','inststatus'],$dom,$uname);
683: my ($tmp) = keys(%userenv);
684: if ($tmp =~ /^(con_lost|error)/i) {
685: %userenv = ();
686: }
687: # Get the user's e-mail address
688: if ($userenv{critnotification} =~ m/%40/) {
689: unless ($emailenc eq $userenv{critnotification}) {
690: $$logmsg .= &mt('Current critical notification e-mail - [_1] for [_2] is different to e-mail address in institutional classlist - [_3].',
691: $userenv{critnotification},$uname,$emailenc).
692: $linefeed;
693: }
694: }
695: if ($userenv{notification} =~ m/%40/) {
696: unless ($emailenc eq $userenv{notification}) {
697: $$logmsg .= &mt('Current standard notification e-mail - [_1] for [_2] is different to e-mail address in institutional classlist - [_3].',
698: $userenv{notification},$uname,$emailenc).
699: $linefeed;
700: }
701: }
702: if ($userenv{permanentemail} =~ m/%40/) {
703: unless ($emailenc eq $userenv{permanentemail}) {
704: $$logmsg .= &mt('Current permanent e-mail
705: - [_1] for [_2] is different to e-mail address in institutional classlist - [_3]',$userenv{permanentemail},$uname,$emailenc).$linefeed;
706: }
707: }
708: my $krbdefdom = '';
709: my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
710: if ($currentauth=~/^(krb[45]):(.*)/) {
711: $currentauth = $1;
712: $krbdefdom = $2;
713: } elsif ($currentauth=~ /^(unix|internal|localauth):/) {
714: $currentauth = $1;
715: } else {
716: $$logmsg .= &mt('Invalid authentication method [_1] for [_2].',$currentauth,$uname).$linefeed;
717: }
718: # Report if authentication methods are different.
719: if ($currentauth ne $auth) {
720: $$logmsg .= &mt("Authentication type mismatch for [_1] - '[_2]' in system, '[_3]' based on information in classlist or default for this course.",$uname,$currentauth,$auth).$linefeed;
721: } elsif ($auth =~ m/^krb/) {
722: if ($krbdefdom ne $authparam) {
723: $$logmsg .= &mt("Kerberos domain mismatch for [_1] - '[_2]' in system, '[_3]' based on information in classlist or default for this course.",$uname,$krbdefdom,$authparam).$linefeed;
724: }
725: }
726:
727: # Check user data
728: if ($first ne $userenv{'firstname'} ||
729: $middle ne $userenv{'middlename'} ||
730: $last ne $userenv{'lastname'} ||
731: $gene ne $userenv{'generation'} ||
732: $pid ne $userenv{'id'} ||
733: $emailenc ne $userenv{'permanentemail'} ) {
734: # Make the change(s)
735: my %changeHash;
736: $changeHash{'firstname'} = $first;
737: $changeHash{'middlename'} = $middle;
738: $changeHash{'lastname'} = $last;
739: $changeHash{'generation'} = $gene;
740: $changeHash{'id'} = $pid;
741: $changeHash{'permanentemail'} = $emailenc;
742: my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
743: if ($putresult eq 'ok') {
744: $$logmsg .= &mt('User information updated for user: [_1] prior to enrollment.',$uname).$linefeed;
745: } else {
746: $$logmsg .= &mt('There was a problem modifying user data for existing user - [_1] -error: [_2], enrollment will still be attempted.',$uname,$putresult).$linefeed;
747: }
748: }
749:
750: # Assign the role of student in the course.
751: my $classlist_reply =
752: &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,
753: $last,$gene,$usec,$end,$start,
754: 'auto','',$cid,'',$context,
755: $credits);
756: if ($classlist_reply eq 'ok') {
757: my $access = &showaccess($end,$start);
758: my $showsec = $usec;
759: if ($usec eq '') {
760: $showsec = &mt('none');
761: }
762: if ($caller eq 'switchtype') {
763: $$logmsg .= &mt("Existing user [_1] detected in institutional classlist - switched from 'manual' to 'auto' enrollment in section [_2].",$uname,$showsec).$access.$linefeed;
764: } elsif ($caller eq 'newstudent') {
765: $$enrollcount ++;
766: $$addresult .= "$first $last ($pid) - $uname ".&mt("enrolled in section '[_1]'.",$showsec).$access.$linefeed;
767: }
768: if ($context eq 'automated') {
769: $$logmsg .= &mt('Existing [_1] user [_2] enrolled successfully.',$dom,$uname).$linefeed;
770: }
771: } else {
772: $$logmsg .= &mt('There was a problem updating the classlist db file for user [_1] to show the new enrollment -error: [_2], so no enrollment occurred for this user.',$uname,$classlist_reply).$linefeed;
773: }
774: return;
775: }
776:
777: sub datechange_check {
778: my ($oldstart,$oldend,$startdate,$enddate) = @_;
779: my $datechange = 0;
780: unless ($oldstart eq $startdate) {
781: $datechange = 1;
782: }
783: if (!$datechange) {
784: if (!$oldend) {
785: if ($enddate) {
786: $datechange = 1;
787: }
788: } elsif ($oldend ne $enddate) {
789: $datechange = 1;
790: }
791: }
792: return $datechange;
793: }
794:
795: sub showaccess {
796: my ($end,$start) = @_;
797: my $showstart;
798: my $showend;
799: if ( (!$start) || ($start <= time) ) {
800: $showstart = 'immediately';
801: } else {
802: $showstart = &Apache::lonlocal::locallocaltime($start);
803: }
804: if (!$end) {
805: $showend = 'no end date';
806: } else {
807: $showend = &Apache::lonlocal::locallocaltime($end);
808: }
809: my $access_msg = ' '.&mt('Access starts: [_1], ends: [_2].',$showstart,$showend);
810: return $access_msg;
811: }
812:
813: sub parse_classlist {
814: my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
815: my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
816: my $uname = '';
817: my @state;
818: my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID','credits','inststatus');
819: my $p = HTML::Parser->new
820: (
821: xml_mode => 1,
822: start_h =>
823: [sub {
824: my ($tagname, $attr) = @_;
825: push @state, $tagname;
826: if ("@state" eq "students student") {
827: $uname = $attr->{username};
828: $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
829: }
830: }, "tagname, attr"],
831: text_h =>
832: [sub {
833: my ($text) = @_;
834: if ("@state" eq "students student startdate") {
835: my $start = $text;
836: unless ($text eq '') {
837: $start = &process_date($text);
838: }
839: $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start;
840: } elsif ("@state" eq "students student enddate") {
841: my $end = $text;
842: unless ($text eq '') {
843: $end = &process_date($text);
844: }
845: $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
846: } else {
847: foreach my $item (@items) {
848: if ("@state" eq "students student $item") {
849: $$studentsref{$uname}[ $$placeref{$item} ] = $text;
850: }
851: }
852: }
853: }, "dtext"],
854: end_h =>
855: [sub {
856: my ($tagname) = @_;
857: pop @state;
858: }, "tagname"],
859: );
860:
861: $p->parse_file($xmlfile);
862: $p->eof;
863: if (-e "$xmlfile") {
864: unlink $xmlfile;
865: }
866: return;
867: }
868:
869: sub process_date {
870: my $timestr = shift;
871: my $timestamp = '';
872: if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
873: my @entries = split/:/,$timestr;
874: for (my $j=0; $j<@entries; $j++) {
875: if ( length($entries[$j]) > 1 ) {
876: $entries[$j] =~ s/^0//;
877: }
878: }
879: $entries[1] = $entries[1] - 1;
880: $timestamp = timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
881: }
882: return $timestamp;
883: }
884:
885: sub create_password {
886: my $passwd = '';
887: my @letts = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
888: for (my $i=0; $i<8; $i++) {
889: my $lettnum = int (rand 2);
890: my $item = '';
891: if ($lettnum) {
892: $item = $letts[int( rand(26) )];
893: my $uppercase = int(rand 2);
894: if ($uppercase) {
895: $item =~ tr/a-z/A-Z/;
896: }
897: } else {
898: $item = int( rand(10) );
899: }
900: $passwd .= $item;
901: }
902: return ($passwd);
903: }
904:
905: sub get_courseinfo {
906: my ($dom,$crs,$courseinfo) = @_;
907: my $owner;
908: if (defined($dom) && defined($crs)) {
909: my %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.showphoto','description','internal.defaultcredits'],$dom,$crs);
910: if ( defined($settings{'internal.coursecode'}) ) {
911: $$courseinfo{'inst_code'} = $settings{'internal.coursecode'};
912: }
913: if ( defined($settings{'description'}) ) {
914: $$courseinfo{'description'} = $settings{'description'};
915: }
916: if ( defined($settings{'internal.showphoto'}) ) {
917: $$courseinfo{'showphoto'} = $settings{'internal.showphoto'};
918: }
919: if ( defined($settings{'internal.credithours'}) ) {
920: $$courseinfo{'defaultcredits'} = $settings{'internal.defaultcredits'};
921: }
922: }
923: return;
924: }
925:
926: sub place_hash {
927: my %place = (
928: autharg => 0,
929: authtype => 1,
930: email => 2,
931: enddate => 3,
932: firstname => 4,
933: generation => 5,
934: groupID => 6,
935: lastname => 7,
936: middlename => 8,
937: startdate => 9,
938: studentID => 10,
939: credits => 11,
940: inststatus => 12,
941: );
942: return %place;
943: }
944:
945: sub photo_response_types {
946: my %lt = &Apache::lonlocal::texthash(
947: 'same' => 'remained unchanged',
948: 'update' => 'were updated',
949: 'new' => 'were added',
950: 'missing' => 'were missing',
951: 'error' => 'were not imported because an error occurred',
952: 'nouser' => 'were for users without accounts',
953: 'noid' => 'were for users without student/employee IDs',
954: );
955: return %lt;
956: }
957:
958:
959: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>