Annotation of loncom/enrollment/Enrollment.pm, revision 1.22
1.7 albertel 1: # Automated Enrollment manager
1.22 ! albertel 2: # $Id: Enrollment.pm,v 1.21 2005/01/30 01:07:35 raeburn Exp $
1.7 albertel 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: #
1.1 raeburn 26: package LONCAPA::Enrollment;
27:
28: use Apache::loncoursedata;
29: use Apache::lonnet;
1.8 raeburn 30: use Apache::lonmsg;
1.1 raeburn 31: use HTML::Entities;
32: use LONCAPA::Configuration;
1.8 raeburn 33: use Time::Local;
34: use lib '/home/httpd/lib/perl';
1.1 raeburn 35:
36: use strict;
37:
38: sub update_LC {
1.8 raeburn 39: my ($dom,$crs,$adds,$drops,$startdate,$enddate,$authtype,$autharg,$classesref,$groupref,$logmsg,$newusermsg,$context) = @_;
1.19 raeburn 40: # Get institutional code and title of this class
41: my %courseinfo = ();
42: &get_courseinfo($dom,$crs,\%courseinfo);
1.1 raeburn 43: # Get current LON-CAPA student enrollment for this class
44: my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
45: my $cid = $dom."_".$crs;
46: my $roster = &Apache::loncoursedata::get_classlist($cid,$dom,$crs);
47: my $cend = &Apache::loncoursedata::CL_END;
48: my $cstart = &Apache::loncoursedata::CL_START;
49: my $stuid=&Apache::loncoursedata::CL_ID;
50: my $sec=&Apache::loncoursedata::CL_SECTION;
51: my $status=&Apache::loncoursedata::CL_STATUS;
52: my $type=&Apache::loncoursedata::CL_TYPE;
1.16 raeburn 53: my $lockedtype=&Apache::loncoursedata::CL_LOCKEDTYPE;
1.1 raeburn 54: my @localstudents = ();
1.15 raeburn 55: my @futurestudents = ();
56: my @activestudents = ();
1.18 raeburn 57: my @excludedstudents = ();
1.1 raeburn 58: my $currlist;
59: foreach my $uname (keys %{$roster} ) {
60: if ($uname =~ m/^(.+):$dom$/) {
61: if ($$roster{$uname}[$status] eq "Active") {
1.15 raeburn 62: push @activestudents, $1;
63: @{$$currlist{$1}} = @{$$roster{$uname}};
1.1 raeburn 64: push @localstudents, $1;
1.15 raeburn 65: } elsif ( ($$roster{$uname}[$cstart] > time) && ($$roster{$uname}[$cend] > time || $$roster{$uname}[$cend] == 0 || $$roster{$uname}[$cend] eq '') ) {
66: push @futurestudents, $1;
1.1 raeburn 67: @{$$currlist{$1}} = @{$$roster{$uname}};
1.15 raeburn 68: push @localstudents, $1;
1.18 raeburn 69: } elsif ($$roster{$uname}[$lockedtype] == 1) {
70: push @excludedstudents, $1;
1.1 raeburn 71: }
72: }
73: }
74: my $linefeed = '';
75: my $addresult = '';
76: my $dropresult = '';
1.21 raeburn 77: my $switchresult = '';
1.1 raeburn 78: if ($context eq "updatenow") {
79: $linefeed = "</li>\n<li>";
80: } elsif ($context eq "automated") {
81: $linefeed = "\n";
82: }
83: my $enrollcount = 0;
84: my $dropcount = 0;
1.21 raeburn 85: my $switchcount = 0;
1.1 raeburn 86:
1.19 raeburn 87: # Get role names
88: my %longroles = ();
89: open(FILE,"<$$configvars{'lonTabDir'}.'/rolesplain.tab");
90: my @rolesplain = <FILE>;
91: close(FILE);
92: foreach (@rolesplain) {
93: if ($_ =~ /^(st|ta|ex|ad|in|cc):([\w\s]+)$/) {
94: $longroles{$1} = $2;
95: }
96: }
97:
1.8 raeburn 98: srand( time() ^ ($$ + ($$ << 15)) ); # Seed rand in case initial passwords have to be generated for new users.
99:
1.1 raeburn 100: # Get mapping of IDs to usernames for current LON-CAPA student enrollment for this class
101: my @LCids = ();
102: my %unameFromLCid = ();
103: foreach my $uname (sort keys %{$currlist}) {
104: my $stuID = $$currlist{$uname}[$stuid];
105: if (!grep/^$stuID$/,@LCids) {
106: push @LCids, $stuID;
107: @{$unameFromLCid{$stuID}} = ();
108: }
109: push @{$unameFromLCid{$stuID}},$uname;
110: }
111:
112: # Get latest institutional enrollment for this class.
113: my %allenrolled = ();
114: my @reg_students = ();
115: my %place = ();
116: $place{'autharg'} = &CL_autharg();
117: $place{'authtype'} = &CL_authtype();
118: $place{'email'} = &CL_email();
119: $place{'enddate'} = &CL_enddate();
120: $place{'firstname'} = &CL_firstname();
121: $place{'generation'} = &CL_generation();
122: $place{'groupID'} = &CL_groupID();
123: $place{'lastname'} = &CL_lastname();
124: $place{'middlename'} = &CL_middlename();
125: $place{'startdate'} = &CL_startdate();
126: $place{'studentID'} = &CL_studentID();
127: my %ucount = ();
128: my %enrollinfo = ();
129: foreach my $class (@{$classesref}) {
130: my %enrolled = ();
131: &parse_classlist($$configvars{'lonDaemons'},$dom,$crs,$class,\%place,$$groupref{$class},\%enrolled);
132: foreach my $uname (sort keys %enrolled ) {
133: if (!grep/^$uname$/,@reg_students) {
134: push @reg_students,$uname;
135: $ucount{$uname} = 0;
136: @{$allenrolled{$uname}} = ();
137: }
138: @{$allenrolled{$uname}[$ucount{$uname}]} = @{$enrolled{$uname}};
139: $ucount{$uname} ++;
140: }
141: }
142:
143: # Check for multiple sections for a single student
144: my @okusers = ();
145: foreach my $uname (@reg_students) {
1.18 raeburn 146: if (grep/^$uname$/,@excludedstudents) {
147: $$logmsg .= "No re-enrollment for $uname - user was previously manually unenrolled and locked.".$linefeed;
148: } elsif (@{$allenrolled{$uname}} > 1) {
1.1 raeburn 149: my @sections = ();
150: my $saved;
151: for (my $i=0; $i<@{$allenrolled{$uname}}; $i++) {
152: my @stuinfo = @{$allenrolled{$uname}[$i]};
153: my $secnum = $stuinfo[ $place{'groupID'} ];
154: unless ($secnum eq '') {
155: unless (grep/^$secnum$/,@sections) {
156: $saved = $i;
157: push @sections,$secnum;
158: }
159: }
160: }
161: if (@sections == 0) {
162: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
163: push @okusers, $uname;
164: }
165: elsif (@sections == 1) {
166: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[$saved]};
167: push @okusers, $uname;
168: }
169: elsif (@sections > 1) {
1.5 raeburn 170: $$logmsg = "$uname appears in classlists for the more than one section of this course, i.e. in sections: ";
1.1 raeburn 171: foreach (@sections) {
1.5 raeburn 172: $$logmsg .= " $_,";
1.1 raeburn 173: }
1.5 raeburn 174: chop($$logmsg);
1.6 raeburn 175: $$logmsg .= ". Because of this ambiguity, no enrollment action was taken for this student.".$linefeed;
1.1 raeburn 176: }
177: } else {
178: @{$enrollinfo{$uname}} = @{$allenrolled{$uname}[0]};
179: push @okusers, $uname;
180: }
181: }
182: # Get mapping of student IDs to usernames for users in institutional data for this class
183: my @allINids = ();
1.3 raeburn 184: my %unameFromINid = ();
1.1 raeburn 185: foreach my $uname (@okusers) {
186: $enrollinfo{$uname}[ $place{'studentID'} ] =~ tr/A-Z/a-z/;
187: my $stuID = $enrollinfo{$uname}[ $place{'studentID'} ];
188: if (grep/^$stuID$/,@allINids) {
189: push @{$unameFromINid{$stuID}},$uname;
190: } else {
191: push @allINids, $stuID;
192: @{$unameFromINid{$stuID}} = $uname;
193: }
194: }
1.5 raeburn 195: # Explicitly allow access to creation/modification of students if called as an automated process.
196: if ($context eq 'automated') {
1.22 ! albertel 197: $env{'allowed.cst'}='F';
1.5 raeburn 198: }
199:
1.1 raeburn 200: # Compare IDs with existing LON-CAPA enrollment for this class
201: foreach my $uname (@okusers) {
1.5 raeburn 202: unless ($uname eq '') {
203: my %uidhash=&Apache::lonnet::idrget($dom,$uname);
204: my @stuinfo = @{$enrollinfo{$uname}};
1.15 raeburn 205: my $access = '';
1.5 raeburn 206: if (grep/^$uname$/,@localstudents) {
1.1 raeburn 207: # Check for studentID changes
1.5 raeburn 208: if ( ($uidhash{$uname}) && ($uidhash{$uname} !~ /error\:/) ) {
209: unless ( ($uidhash{$uname}) eq ($stuinfo[ $place{studentID} ]) ) {
1.6 raeburn 210: $$logmsg .= "Change in ID for $uname. StudentID in LON-CAPA system is $uidhash{$uname}; StudentID in institutional data is $stuinfo[ $place{studentID} ]".$linefeed;
1.5 raeburn 211: }
1.1 raeburn 212: }
1.16 raeburn 213: # Check for switch from manual to auto
214: unless (($$currlist{$uname}[$type] eq "auto") || ($$currlist{$uname}[$lockedtype] eq "1") || (!$adds) ) {
215: # drop manually added student
216: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid);
217: # re-enroll as auto student
218: if ($drop_reply !~ /^ok/) {
219: $$logmsg .= "An error occured during the attempt to convert $uname from a manual type to an auto type student - $drop_reply.".$linefeed;
220: } else {
221: # re-enroll as auto student
222: my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc);
223: &prepare_add($authtype,$autharg,$enddate,$startdate,\@stuinfo,\%place,\$dom,\$uname,\$auth,\$authparam,\$first,\$middle,\$last,\$gene,\$usec,\$end,\$start,\$emailaddr,\$pid,\$emailenc);
224: if ($$currlist{$uname}[$sec] ne $usec) {
1.21 raeburn 225: $switchresult .= "Section for $uname switched from $$currlist{$uname}[$sec] to ".$usec.$linefeed;
226: if ($context eq 'automated') {
227: $$logmsg .= "Section switch for $uname from $$currlist{$uname}[$sec] to ".$usec.$linefeed; ;
228: }
229: $switchcount ++;
1.16 raeburn 230: }
231: &execute_add($context,'switchtype',$uname,$dom,$auth,$authparam,$first,$middle,$last,$gene,$pid,$usec,$end,$start,$emailenc,$cid,\$addresult,\$enrollcount,$linefeed,$logmsg);
232: }
233: }
1.1 raeburn 234: # Check for section changes
1.15 raeburn 235: if ($$currlist{$uname}[$sec] eq $stuinfo[ $place{groupID} ]) {
236: # Check for access date changes for students with access starting in the future.
237: if ( (grep/^$uname$/,@futurestudents) && ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.16 raeburn 238: my $datechange = &datechange_check($$currlist{$uname}[$cstart],$$currlist{$uname}[$cend],$startdate,$enddate);
1.15 raeburn 239: if ($datechange) {
1.16 raeburn 240: my $modify_access_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid);
1.15 raeburn 241: $access = &showaccess($enddate,$startdate);
242: if ($modify_access_result =~ /^ok/) {
243: $$logmsg .= "Change in access dates for $uname.".$access.$linefeed;
244: } else {
245: $$logmsg .= "Error when attempting to change start and/or end access dates for $uname in section: ".$stuinfo[ $place{groupID} ]." -error $modify_access_result".$linefeed;
246: }
247: }
248: }
249: } else {
1.5 raeburn 250: if ( ($$currlist{$uname}[$type] eq "auto") && ($adds == 1) ) {
1.10 raeburn 251: # Delete from roles.db for current section
252: my $expiretime = time;
253: my $uurl='/'.$cid;
254: $uurl=~s/\_/\//g;
255: if ($$currlist{$uname}[$sec]) {
256: $uurl.='/'.$$currlist{$uname}[$sec];
257: }
258: my $expire_role_result = &Apache::lonnet::assignrole($dom,$uname,$uurl,'st',$expiretime);
259: if ($expire_role_result eq 'ok') {
1.15 raeburn 260: my $modify_section_result;
261: if (grep/^$uname$/,@activestudents) {
1.16 raeburn 262: $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);
1.15 raeburn 263: } else {
1.16 raeburn 264: $modify_section_result = &Apache::lonnet::modify_student_enrollment($dom,$uname,undef,undef,undef,undef,undef,$stuinfo[ $place{groupID} ],$enddate,$startdate,'auto','',$cid);
1.15 raeburn 265: $access = &showaccess($enddate,$startdate);
266: }
1.10 raeburn 267: if ($modify_section_result =~ /^ok/) {
1.21 raeburn 268: $switchresult .= "Section for $uname switched from old section: ".$$currlist{$uname}[$sec] ." to new section: ".$stuinfo[ $place{groupID} ].".".$access.$linefeed;
269: if ($context eq 'automated') {
270: $$logmsg .= "Section switch for $uname from $$currlist{$uname}[$sec] to $stuinfo[ $place{groupID} ]".$linefeed;
271: }
272: $switchcount ++;
1.10 raeburn 273: } else {
274: $$logmsg .= "Error when attempting section change for $uname from old section ".$$currlist{$uname}[$sec]." to new section: ".$stuinfo[ $place{groupID} ]." -error: $modify_section_result".$linefeed;
275: }
1.5 raeburn 276: } else {
1.10 raeburn 277: $$logmsg .= "Error when attempting to expire role for $uname in old section" .$$currlist{$uname}[$sec]." -error: $expire_role_result".$linefeed;
1.5 raeburn 278: }
1.1 raeburn 279: }
280: }
1.5 raeburn 281: } else {
1.1 raeburn 282: # Check for changed usernames by checking studentIDs
1.5 raeburn 283: if ( ($stuinfo[ $place{studentID} ] ne '') && (grep/^$stuinfo[ $place{studentID} ]$/,@LCids) ) {
284: if (grep/^$$currlist{$uname}[ $place{'studentID'} ]$/,@allINids) {
285: foreach my $match ( @{ $unameFromLCid{ $stuinfo[ $place{studentID} ] } } ) {
286: if (grep/^$match$/,@okusers) {
1.6 raeburn 287: $$logmsg .= "A possible change in username has been detected for a student enrolled in this course. The existing LON-CAPA classlist contains user: $uname and student ID: ".$$currlist{$uname}[ $place{studentID} ].". This username has been dropped from the institutional classlist, but the same student ID is used for user: $match who still appears in the institutional classlist. You may need to contact your Domain Coordinator to request a move of the student data files for user: $uname to $match".$linefeed;
1.5 raeburn 288: }
1.1 raeburn 289: }
290: }
1.5 raeburn 291: } elsif ($adds == 1) {
1.16 raeburn 292: my ($auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc);
293: &prepare_add($authtype,$autharg,$enddate,$startdate,\@stuinfo,\%place,\$dom,\$uname,\$auth,\$authparam,\$first,\$middle,\$last,\$gene,\$usec,\$end,\$start,\$emailaddr,\$pid,\$emailenc);
1.1 raeburn 294: # Check for existing account in this LON-CAPA domain for this username
1.5 raeburn 295: my $uhome=&Apache::lonnet::homeserver($uname,$dom);
296: if ($uhome eq 'no_host') { # User does not exist
1.19 raeburn 297: my $args = {'auth' => $auth,
298: 'authparam' => $authparam,
299: 'emailenc' => $emailenc,
300: 'udom' => $dom,
301: 'uname' => $uname,
302: 'pid' => $pid,
303: 'first' => $first,
304: 'middle' => $middle,
305: 'last' => $last,
306: 'gene' => $gene,
307: 'usec' => $usec,
308: 'end' => $end,
309: 'start' => $start,
310: 'emailaddr' => $emailaddr,
311: 'cid' => $cid,
312: 'crs' => $crs,
313: 'cdom' => $dom,
314: 'context' => $context,
315: 'linefeed' => $linefeed,
316: 'role' => 'st'
317: };
1.20 raeburn 318: my $outcome = &create_newuser($args,$logmsg,$newusermsg,\$enrollcount,\$addresult,\%longroles,\%courseinfo);
1.5 raeburn 319: } else {
1.16 raeburn 320: &execute_add($context,'newstudent',$uname,$dom,$auth,$authparam,$first,$middle,$last,$gene,$pid,$usec,$end,$start,$emailenc,$cid,\$addresult,\$enrollcount,$linefeed,$logmsg);
1.3 raeburn 321: }
1.1 raeburn 322: }
323: }
324: }
325: }
326: # Do drops
327: if ( ($drops == 1) && (@reg_students > 0) ) {
328: foreach my $uname (@localstudents) {
329: if ($$currlist{$uname}[$type] eq "auto") {
330: my @saved = ();
331: if (!grep/^$uname$/,@reg_students) {
332: # Check for changed usernames by checking studentIDs
333: if (grep/^$$currlist{$uname}[ $stuid ]$/,@allINids) {
334: foreach my $match (@{$unameFromINid{$$currlist{$uname}[ $stuid ]}} ) {
1.6 raeburn 335: $$logmsg .= "A possible change in username has been detected for a student enrolled in this course. The existing LON-CAPA classlist contains user: $uname and student ID: $$currlist{$uname}[ $place{studentID} ]. This username has been dropped from the institutional classlist, but the same student ID is used for user: $match who still appears in the institutional classlist. You may need to move the student data files for user: $uname to $match.".$linefeed;
1.1 raeburn 336: push @saved,$uname;
337: }
338: } elsif (@saved == 0) {
1.16 raeburn 339: my $drop_reply = &Apache::lonnet::modifystudent($dom,$uname,'','','',undef,undef,undef,undef,$$currlist{$uname}[$sec],time,undef,undef,undef,undef,'auto','',$cid);
1.1 raeburn 340: if ($drop_reply !~ /^ok/) {
1.5 raeburn 341: $$logmsg .= "An error occured during the attempt to expire the $uname from the old section $$currlist{$uname}[$sec] - $drop_reply.".$linefeed;
1.1 raeburn 342: } else {
343: $dropcount ++;
344: my %userenv = &Apache::lonnet::get('environment',['firstname','lastname','id'],$dom,$uname);
345: $dropresult .= $userenv{'firstname'}." ".$userenv{'lastname'}." (".$userenv{'id'}.") - ".$uname." dropped from section/group ".$$currlist{$uname}[$sec].$linefeed;
1.8 raeburn 346: if ($context eq 'automated') {
347: $$logmsg .= "User $uname student role expired from course.".$linefeed;
348: }
1.1 raeburn 349: }
350: }
351: }
352: }
353: }
354: }
1.5 raeburn 355:
356: # Terminated explictly allowed access to student creation/modification
357: if ($context eq 'automated') {
1.22 ! albertel 358: delete($env{'allowed.cst'});
1.5 raeburn 359: }
1.1 raeburn 360: if ($enrollcount > 0) {
361: if ($context eq "updatenow") {
1.6 raeburn 362: $addresult = substr($addresult,0,rindex($addresult,"<li>"));
1.21 raeburn 363: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:<br/><ul><li>".$addresult."</ul><br/><br/>";
1.1 raeburn 364: } else {
1.21 raeburn 365: $addresult = "The following $enrollcount student(s) was/were added to this LON-CAPA course:\n\n".$addresult."\n\n";
366: }
1.1 raeburn 367: }
368: if ($dropcount > 0) {
369: if ($context eq "updatenow") {
1.6 raeburn 370: $dropresult = substr($dropresult,0,rindex($dropresult,"<li>"));
1.21 raeburn 371: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:<br/><ul><li>".$dropresult."</ul><br/><br/>";
1.1 raeburn 372: } else {
373: $dropresult = "The following $dropcount student(s) was/were expired from this LON-CAPA course:\n\n".$dropresult."\n\n";
374: }
375: }
1.21 raeburn 376: if ($switchcount > 0) {
377: if ($context eq "updatenow") {
378: $switchresult = substr($switchresult,0,rindex($switchresult,"<li>"));
379: $switchresult = "The following $switchcount student(s) switched sections in this LON-CAPA course:<br/><ul><li>".$switchresult."</ul><br/><br/>";
380: } else {
381: $switchresult = "The following $switchcount student(s) switched sections in this LON-CAPA course:\n\n".$switchresult."\n\n";
382: }
383: }
1.1 raeburn 384: if ( ($adds) && ($enrollcount == 0) ) {
385: $addresult = "There were no new students to add to the course.";
386: if ($context eq "updatenow") {
387: $addresult .="<br/><br/>";
388: } else {
389: $addresult .="\n";
390: }
391: }
392: if ( ($drops) && ($dropcount == 0) ) {
393: $dropresult = "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.";
394: if ($context eq "updatenow") {
395: $dropresult .="<br/>";
396: } else {
397: $dropresult .="\n";
398: }
399: }
1.21 raeburn 400: my $changecount = $enrollcount + $dropcount + $switchcount;
401: return ($changecount,$addresult.$dropresult.$switchresult);
1.6 raeburn 402: }
1.1 raeburn 403:
1.19 raeburn 404: sub create_newuser {
405: my ($args,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,$courseinfo) = @_;
406: my $auth = $args->{'auth'};
407: my $authparam = $args->{'authparam'};
408: my $emailenc = $args->{'emailenc'};
409: my $udom = $args->{'udom'};
410: my $uname = $args->{'uname'};
411: my $pid = $args->{'pid'};
412: my $first = $args->{'first'};
413: my $middle = $args->{'middle'};
414: my $last = $args->{'last'} ;
415: my $gene = $args->{'gene'};
416: my $usec = $args->{'usec'};
417: my $end = $args->{'end'};
418: my $start = $args->{'start'};
419: my $emailaddr = $args->{'emailaddr'};
420: my $cid = $args->{'cid'};
421: my $crs = $args->{'crs'};
422: my $cdom = $args->{'cdom'};
423: my $context = $args->{'context'};
424: my $linefeed = $args->{'linefeed'};
425: my $role = $args->{'role'};
426: my $create_passwd = 0;
427: my $authchk = '';
428: my $outcome;
429: unless ($authparam eq '') { $authchk = 'ok'; };
430: # If no account exists and passwords should be generated
431: if ($auth eq "internal") {
432: if ($authparam eq '') {
433: $authparam = &create_password();
434: if ($authparam eq '') {
435: $authchk = '';
436: } else {
437: $create_passwd = 1;
438: $authchk = 'ok';
439: }
440: }
441: } elsif ($auth eq "localauth") {
442: ($authparam,$create_passwd,$authchk) = &Apache::lonnet::auto_create_password($crs,$cdom,$authparam);
443: } elsif ($auth =~ m/^krb/) {
444: if ($authparam eq '') {
445: $$logmsg .= "No Kerberos domain was provided for the new user - $uname, so the new user was not enrolled in the course.".$linefeed;
446: $authchk = 'invalid';
447: }
448: } else {
449: $authchk = 'invalid';
450: $$logmsg .= "An invalid authentication type was provided for the new user - $uname, so the user was not enrolled in the course.".$linefeed;
451: }
452: if ($authchk eq 'ok') {
453: # Now create user.
454: my $type = 'auto';
455: my $userurl = '/'.$cdom.'/'.$crs;
456: if ($usec ne '') {
457: $userurl .= '/'.$usec;
458: }
459: if ($context eq 'createowner' || $context eq 'createcourse') {
460: my $result = &Apache::lonnet::modifyuser($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,'1',undef,$emailaddr);
461: if ($result eq 'ok' && $context eq 'createcourse') {
462: $outcome = &Apache::loncreateuser::commit_standardrole($userurl,$role,$cdom,$crs,$start,$end);
463: unless ($outcome =~ /^Error:/) {
464: $outcome = 'ok';
465: }
466: } else {
467: $outcome = $result;
468: }
469: } else {
470: $outcome=&Apache::lonnet::modifystudent($udom,$uname,$pid,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,'',undef,$emailaddr,'auto','',$cid);
471: }
472: if ($outcome eq 'ok') {
473: my $access = &showaccess($end,$start);
474: $$addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$access.$linefeed;
475: unless ($context eq 'createowner' || $context eq 'createcourse') {
476: $$enrollcount ++;
477: }
478: if ($context eq 'automated') {
479: $$logmsg .= "New $udom user $uname added successfully.";
480: }
481: unless ($emailenc eq '' || $context eq 'createowner' || $context eq 'createcourse') {
482: my %emailHash;
483: $emailHash{'critnotification'} = $emailenc;
484: $emailHash{'notification'} = $emailenc;
485: my $putresult = &Apache::lonnet::put('environment',\%emailHash,$udom,$uname);
486: }
487: if ($create_passwd) {
488: # Send e-mail with initial password to new user at $emailaddr.
489: # If e-mail address is invalid, send password via message to courseowner i
490: # (if automated call) or to user if roster update.
491: if ($emailaddr eq '') {
492: $$newusermsg .= " username: $uname, password: ".$authparam.$linefeed."\n";
493: } else {
494: my $subject = "New LON-CAPA account";
495: my $body;
496: if ($context eq 'createowner') {
497: $body = "A user account has been created for you while creating your new course in the LON-CAPA course management and online homework system.\n\nYou should log-in to the system using the following credentials:\nusername: $uname\npassword: $authparam\n\nThe URL you should use to access the LON-CAPA system at your school is: http://".$ENV{'SERVER_NAME'}."\n\n";
498: } elsif ($context eq 'createcourse') {
499: $body = "You have been assigned the role of $$longroles{$role} in a new course: $$courseinfo{'description'} - $$courseinfo{'inst_code'} in the LON-CAPA course management and online homework system. As you did not have an existing user account in the system, one has been created for you.\n\nYou should log-in to the system using the following credentials:\nusername: $uname\npassword: $authparam\n\nThe URL you should use to access the LON-CAPA system at your school is: http://".$ENV{'SERVER_NAME'}."\n\n";
500: } else {
501: my $access_start = 'immediately';
502: if ($start > 0) {
503: $access_start = localtime($start)
504: }
505: $body = "You have been enrolled in the LON-CAPA system at your school, because you are a registered student in a class that is using the LON-CAPA couse management and online homework system.\n\nYou should log-in to the system using the following credentials:\nusername: $uname\npassword: $authparam\n\nThe URL you should use to access the LON-CAPA system at your school is: http://".$ENV{'SERVER_NAME'}."\n\n.When you log-in you will be able to access the LON-CAPA course for $$courseinfo{'description'} - $$courseinfo{'inst_code'} starting $access_start.\n";
506: }
507: &Apache::lonmsg::sendemail($emailaddr,$subject,$body);
508: }
509: if ($context eq 'automated') {
510: $$logmsg .= " Initial password - - sent to ".$emailaddr.$linefeed;
511: }
512: } else {
513: if ($context eq 'automated') {
514: $$logmsg .= $linefeed;
515: }
516: }
517: } else {
518: $$logmsg .= "An error occurred adding new user $uname - ".$outcome.$linefeed;
519: }
520: }
521: return $outcome;
522: }
523:
1.16 raeburn 524: sub prepare_add {
525: my ($authtype,$autharg,$enddate,$startdate,$stuinfo,$place,$dom,$uname,$auth,$authparam,$first,$middle,$last,$gene,$usec,$end,$start,$emailaddr,$pid,$emailenc) = @_;
526: $$auth = $$stuinfo[ $$place{'authtype'} ];
527: $$authparam = $$stuinfo[ $$place{'autharg'} ];
528: $$first = $$stuinfo[ $$place{'firstname'} ];
529: $$middle = $$stuinfo[ $$place{'middlename'} ];
530: $$last = $$stuinfo[ $$place{'lastname'} ];
531: $$gene = $$stuinfo[ $$place{'generation'} ];
532: $$usec = $$stuinfo[ $$place{'groupID'} ];
533: $$end = $$stuinfo[ $$place{'enddate'} ];
534: $$start = $$stuinfo[ $$place{'startdate'} ];
535: $$emailaddr = $$stuinfo[ $$place{'email'} ];
536: $$pid = $$stuinfo[ $$place{'studentID'} ];
537:
538: # remove non alphanumeric values from section
539: $$usec =~ s/\W//g;
540:
541: unless ($$emailaddr =~/^[^\@]+\@[^\@]+$/) { $$emailaddr =''; }
542: $$emailenc = &HTML::Entities::encode($$emailaddr,'<>&"');
543:
544: # Use course defaults where entry is absent
545: if ( ($$auth eq '') || (!defined($$auth)) ) {
546: $$auth = $authtype;
547: }
548: if ( ($$authparam eq '') || (!defined($$authparam)) ) {
549: $$authparam = $autharg;
550: }
551: if ( ($$end eq '') || (!defined($$end)) ) {
552: $$end = $enddate;
553: }
554: if ( ($$start eq '') || (!defined($$start)) ) {
555: $$start = $startdate;
556: }
557: # Clean up whitespace
558: foreach ($dom,$uname,$pid,$first,$middle,$last,$gene,$usec) {
559: $$_ =~ s/(\s+$|^\s+)//g;
560: }
561: return;
562: }
563:
564: sub execute_add {
565: my ($context,$caller,$uname,$dom,$auth,$authparam,$first,$middle,$last,$gene,$pid,$usec,$end,$start,$emailenc,$cid,$addresult,$enrollcount,$linefeed,$logmsg) = @_;
566: # Get the user's information and authentication
567: my %userenv = &Apache::lonnet::get('environment',['firstname','middlename','lastname','generation','id','critnotification','notification'],$dom,$uname);
568: my ($tmp) = keys(%userenv);
569: if ($tmp =~ /^(con_lost|error)/i) {
570: %userenv = ();
571: }
572: # Get the user's e-mail address
573: if ($userenv{critnotification} =~ m/%40/) {
574: unless ($emailenc eq $userenv{critnotification}) {
575: $$logmsg .= "Current critical notification e-mail
576: - ".$userenv{critnotification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
577: }
578: }
579: if ($userenv{notification} =~ m/%40/) {
580: unless ($emailenc eq $userenv{critnotification}) {
581: $$logmsg .= "Current standard notification e-mail
582: - ".$userenv{notification}." for $uname is different to e-mail address in institutional classlist - ".$emailenc.$linefeed;
583: }
584: }
585: my $krbdefdom = '';
586: my $currentauth=&Apache::lonnet::queryauthenticate($uname,$dom);
587: if ($currentauth=~/^(krb[45]):(.*)/) {
588: $currentauth = $1;
589: $krbdefdom = $2;
590: } elsif ($currentauth=~ /^(unix|internal|localauth):/) {
591: $currentauth = $1;
592: } else {
593: $$logmsg .= "Invalid authentication method $currentauth for $uname.".$linefeed;
594: }
595: # Report if authentication methods are different.
596: if ($currentauth ne $auth) {
597: $$logmsg .= "Authentication type mismatch for $uname - '$currentauth' in system, '$auth' based on information in classlist or default for this course.".$linefeed;
598: } elsif ($auth =~ m/^krb/) {
599: if ($krbdefdom ne $authparam) {
600: $$logmsg .= "Kerberos domain mismatch for $uname - '$krbdefdom' in system, '$authparam' based on information in classlist or default for this course.".$linefeed;
601: }
602: }
603:
604: # Check user data
605: if ($first ne $userenv{'firstname'} ||
606: $middle ne $userenv{'middlename'} ||
607: $last ne $userenv{'lastname'} ||
608: $gene ne $userenv{'generation'} ||
609: $pid ne $userenv{'id'} ) {
610: # Make the change(s)
611: my %changeHash;
612: $changeHash{'firstname'} = $first;
613: $changeHash{'middlename'} = $middle;
614: $changeHash{'lastname'} = $last;
615: $changeHash{'generation'} = $gene;
616: $changeHash{'id'} = $pid;
617: my $putresult = &Apache::lonnet::put('environment',\%changeHash,$dom,$uname);
618: if ($putresult eq 'ok') {
619: $$logmsg .= "User information updated for user: $uname prior to enrollment.".$linefeed;
620: } else {
621: $$logmsg .= "There was a problem modifying user data for existing user - $uname -error: $putresult, enrollment will still be attempted.".$linefeed;
622: }
623: }
624:
625: # Assign the role of student in the course.
626: my $classlist_reply = &Apache::lonnet::modify_student_enrollment($dom,$uname,$pid,$first,$middle,$last,$gene,$usec,$end,$start,'auto','',$cid);
627: if ($classlist_reply eq 'ok') {
628: my $access = &showaccess($end,$start);
629: if ($caller eq 'switchtype') {
630: $$logmsg .= "Existing user $uname detected in institutional classlist - switched from 'manual' to 'auto' enrollment in section/group $usec.".$access.$linefeed;
631: } elsif ($caller eq 'newstudent') {
632: $$enrollcount ++;
633: $$addresult .= "$first $last ($pid) - $uname enrolled in section/group $usec.".$access.$linefeed;
634: }
635: if ($context eq 'automated') {
636: $$logmsg .= "Existing $dom user $uname enrolled successfully.".$linefeed;
637: }
638: } else {
639: $$logmsg .= "There was a problem updating the classlist db file for user $uname to show the new enrollment -error: $classlist_reply, so no enrollment occurred for this user.".$linefeed;
640: }
641: return;
642: }
643:
644: sub datechange_check {
645: my ($oldstart,$oldend,$startdate,$enddate) = @_;
646: my $datechange = 0;
647: unless ($oldstart eq $startdate) {
648: $datechange = 1;
649: }
650: if (!$datechange) {
651: if (!$oldend) {
652: if ($enddate) {
653: $datechange = 1;
654: }
655: } elsif ($oldend ne $enddate) {
656: $datechange = 1;
657: }
658: }
659: return $datechange;
660: }
661:
1.15 raeburn 662: sub showaccess {
663: my ($end,$start) = @_;
664: my $showstart;
665: my $showend;
666: if ( (!$start) || ($start <= time) ) {
667: $showstart = 'immediately';
668: } else {
669: $showstart = &Apache::lonlocal::locallocaltime($start);
670: }
671: if (!$end) {
672: $showend = 'no end date';
673: } else {
674: $showend = &Apache::lonlocal::locallocaltime($end);
675: }
676: my $access_msg = " Access starts: ".$showstart.", ends: ".$showend.".";
677: return $access_msg;
678: }
679:
1.1 raeburn 680: sub parse_classlist {
1.6 raeburn 681: my ($tmpdir,$dom,$crs,$class,$placeref,$groupID,$studentsref) = @_;
1.5 raeburn 682: my $xmlfile = $tmpdir."/tmp/".$dom."_".$crs."_".$class."_classlist.xml";
1.6 raeburn 683: my $uname = '';
684: my @state;
1.8 raeburn 685: my @items = ('autharg','authtype','email','firstname','generation','lastname','middlename','studentID');
1.6 raeburn 686: my $p = HTML::Parser->new
687: (
688: xml_mode => 1,
689: start_h =>
690: [sub {
691: my ($tagname, $attr) = @_;
692: push @state, $tagname;
693: if ("@state" eq "students student") {
694: $uname = $attr->{username};
695: }
696: }, "tagname, attr"],
697: text_h =>
698: [sub {
699: my ($text) = @_;
700: if ("@state" eq "students student groupID") {
701: $$studentsref{$uname}[ $$placeref{'groupID'} ] = $groupID;
1.8 raeburn 702: } elsif ("@state" eq "students student startdate") {
703: my $start = $text;
704: unless ($text eq '') {
705: $start = &process_date($text);
706: }
707: $$studentsref{$uname}[ $$placeref{'startdate'} ] = $start;
708: } elsif ("@state" eq "students student enddate") {
709: my $end = $text;
710: unless ($text eq '') {
711: $end = &process_date($text);
712: }
713: $$studentsref{$uname}[ $$placeref{'enddate'} ] = $end;
1.6 raeburn 714: } else {
715: foreach my $item (@items) {
716: if ("@state" eq "students student $item") {
717: $$studentsref{$uname}[ $$placeref{$item} ] = $text;
718: }
719: }
720: }
721: }, "dtext"],
722: end_h =>
723: [sub {
724: my ($tagname) = @_;
725: pop @state;
726: }, "tagname"],
727: );
728:
729: $p->parse_file($xmlfile);
730: $p->eof;
1.8 raeburn 731: if (-e "$xmlfile") {
732: unlink $xmlfile;
733: }
1.3 raeburn 734: return;
1.1 raeburn 735: }
736:
1.8 raeburn 737: sub process_date {
738: my $timestr = shift;
739: my $timestamp = '';
740: if ($timestr =~ m/^\d{4}:\d{2}:\d{2}/) {
741: my @entries = split/:/,$timestr;
742: for (my $j=0; $j<@entries; $j++) {
743: if ( length($entries[$j]) > 1 ) {
744: $entries[$j] =~ s/^0//;
745: }
746: }
747: $entries[1] = $entries[1] - 1;
748: $timestamp = timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
749: }
750: return $timestamp;
751: }
752:
1.1 raeburn 753: sub create_password {
1.8 raeburn 754: my $passwd = '';
1.11 raeburn 755: 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");
1.8 raeburn 756: for (my $i=0; $i<8; $i++) {
757: my $lettnum = int (rand 2);
758: my $item = '';
759: if ($lettnum) {
760: $item = $letts[int( rand(26) )];
761: my $uppercase = int(rand 2);
762: if ($uppercase) {
763: $item =~ tr/a-z/A-Z/;
764: }
765: } else {
766: $item = int( rand(10) );
767: }
768: $passwd .= $item;
769: }
770: return ($passwd);
1.9 raeburn 771: }
772:
773: sub check_user_status {
774: my ($udom,$uname,$cdom,$crs,$role,$secgrp) = @_;
775: my %userinfo = &Apache::lonnet::dump('roles',$udom,$uname);
776: my @uroles = keys %userinfo;
777: my $srchstr;
778: my $active_chk = 'none';
779: if (@uroles > 0) {
780: if ( ($role eq 'cc') || ($secgrp eq '') || ( !defined($secgrp) ) ) {
781: $srchstr = '/'.$cdom.'/'.$crs.'_'.$role;
782: } else {
783: $srchstr = '/'.$cdom.'/'.$crs.'/'.$secgrp.'_'.$role;
784: }
785: if (grep/^$srchstr$/,@uroles) {
786: my $role_end = 0;
787: my $role_start = 0;
788: $active_chk = 'ok';
789: if ( $userinfo{$srchstr} =~ m/^($role)_(\d+)/ ) {
790: $role_end = $2;
791: if ( $userinfo{$srchstr} =~ m/^($role)_($role_end)_(\d+)$/ )
792: {
793: $role_start = $3;
794: }
795: }
796: if ($role_start > 0) {
797: if (time < $role_start) {
798: $active_chk = 'expired';
799: }
800: }
801: if ($role_end > 0) {
802: if (time > $role_end) {
803: $active_chk = 'expired';
804: }
805: }
806: }
807: }
808: return $active_chk;
1.1 raeburn 809: }
810:
1.19 raeburn 811: sub get_courseinfo {
812: my ($dom,$crs,$courseinfo) = @_;
813: my $owner;
814: if (defined($dom) && defined($crs)) {
815: my %settings = &Apache::lonnet::get('environment',['internal.coursecode','description'],$dom,$crs);
816: if ( defined($settings{'internal.coursecode'}) ) {
817: $$courseinfo{'inst_code'} = $settings{'internal.coursecode'};
818:
819: }
820: if ( defined($settings{'description'}) ) {
821: $$courseinfo{'description'} = $settings{'description'};
822: }
823: }
824: return;
825: }
826:
1.1 raeburn 827: sub CL_autharg { return 0; }
828: sub CL_authtype { return 1;}
829: sub CL_email { return 2;}
830: sub CL_enddate { return 3;}
831: sub CL_firstname { return 4;}
832: sub CL_generation { return 5;}
833: sub CL_groupID { return 6;}
834: sub CL_lastname { return 7;}
835: sub CL_middlename { return 8;}
836: sub CL_startdate { return 9; }
837: sub CL_studentID { return 10; }
838:
839: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>