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