Annotation of loncom/automation/batchcreatecourse.pm, revision 1.40.2.1.4.1
1.4 albertel 1: #
1.40.2.1.4.1! raeburn 2: # $Id: batchcreatecourse.pm,v 1.40.2.1 2016/09/01 01:05:05 raeburn Exp $
1.4 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: #
26:
1.1 raeburn 27: package LONCAPA::batchcreatecourse;
28: use LONCAPA::Configuration;
29: use LONCAPA::Enrollment;
30: use HTML::Parser;
31: use Time::Local;
32: use Apache::lonnet;
1.2 raeburn 33: use Apache::loncommon;
1.1 raeburn 34: use Apache::lonlocal;
1.21 raeburn 35: use LONCAPA qw(:match);
1.1 raeburn 36:
1.9 raeburn 37: use strict;
38:
1.1 raeburn 39: # Collection of routines used for batch creation of courses and users.
40: # &create_courses() should be called by an Autocreate.pl
41: # script via a cron entry, or alternatively from a web page, after upload
42: # of a file containing an XML description of a course request (lonbatchccrs.pm).
43: #
44: # XML file(s) describing courses that are to be created in domain $dom are stored in
1.3 raeburn 45: # /home/httpd/perl/tmp/addcourse/$dom
1.1 raeburn 46: #
47: # &create_courses() will create an account for the course owner
48: # (if one does not currently exist), will create the course (cloning if necessary),
49: # and will add additional instructional staff (creating accounts if necessary).
50: #
51: # Example of XML file (which could contain more than one class to be created):
52: #
53: #<?xml version="1.0" encoding="UTF-8"?>
54: #<!DOCTYPE text>
55: #<class id="ss05ubw101">
56: # <title>Underwater Basket Weaving</title>
1.16 raeburn 57: # <crstype>Course</crstype>
1.1 raeburn 58: # <coursecode>ss05ubw101</coursecode>
1.38 raeburn 59: # <defaultcredits>3</defaultcredits>
1.1 raeburn 60: # <coursehome>msul1</coursehome>
61: # <coursedomain>msu</coursedomain>
62: # <reshome>/res/msu/</reshome>
63: # <optional_id></optional_id>
64: # <adds>1</adds>
65: # <drops>1</drops>
66: # <enrollstart>2005:01:04:10:30</enrollstart>
67: # <enrollend>2005:07:04:20:30</enrollend>
68: # <accessstart>2005:01:10:10:30</accessstart>
69: # <accessend>2005:05:31:10:30</accessend>
70: # <authentication>
71: # <method>krb4</method>
72: # <param>MSU.EDU</param>
73: # </authentication>
74: # <nonstandard></nonstandard>
75: # <topmap></topmap>
76: # <firstres>nav</firstres>
1.15 raeburn 77: # <crsquota>20</crsquota>
1.39 raeburn 78: # <uniquecode>1</uniquecode>
1.1 raeburn 79: # <clonecrs>466011437c34194msul1</clonecrs>
80: # <clonedom>msu</clonedom>
1.27 raeburn 81: # <datemode>shift</datemode>
82: # <dateshift>365</dateshift>
1.40.2.1.4.1! raeburn 83: # <tinyurls>create</tinyurls>
1.1 raeburn 84: # <showphotos></showphotos>
85: # <setpolicy>1</setpolicy>
86: # <setcontent>1</setcontent>
1.40.2.1 raeburn 87: # <setcomment>1</setcomment>
1.1 raeburn 88: # <setkeys>0</setkeys>
1.19 albertel 89: # <keyauth>keyadmin:msu</keyauth>
1.1 raeburn 90: # <disresdis>1</disresdis>
91: # <disablechat>1</disablechat>
92: # <openall></openall>
93: # <notify_dc>1</notify_dc>
94: # <notify_owner>1</notify_owner>
95: # <owner>
96: # <username>sparty</username>
97: # <domain>msu</domain>
98: # <authtype>krb4</authtype>
99: # <autharg>MSU.EDU</autharg>
100: # </owner>
101: # <sections>
102: # <section>
103: # <inst>001</inst>
104: # <loncapa>1</loncapa>
105: # </section>
106: # <section>
107: # <inst>002</inst>
108: # <loncapa>2</loncapa>
109: # </section>
110: # </sections>
111: # <crosslists>
112: # <xlist>
113: # <inst>ss05zzz101001</inst>
114: # <loncapa>1</loncapa>
115: # </xlist>
116: # </crosslists>
117: # <users>
118: # <user>
119: # <username>sparty</username>
120: # <domain>msu</domain>
121: # <email>sparty@msu.edu</email>
122: # <authtype>krb4</authtype>
123: # <autharg></autharg>
124: # <firstname>MSU</firstname>
125: # <generation></generation>
126: # <lastname>Spartan</lastname>x
127: # <middlename></middlename>
128: # <studentID></studentID>
1.38 raeburn 129: # <credits></credits>
1.1 raeburn 130: # <roles></roles>
131: # </user>
132: # <user>
133: # <username>itds0001</username>
134: # <domain>northwood5</domain>
135: # <email>itds0001@msu.edu</email>
136: # <authtype>int</authtype>
137: # <autharg></autharg>
138: # <firstname>Info</firstname>
139: # <generation></generation>
140: # <lastname>Techc</lastname>x
141: # <middlename></middlename>
142: # <studentID></studentID>
143: # <roles>
144: # <role id='in'>
145: # <start>2005:01:01:12:10</start>
146: # <end>2005:12:01:12:10</end>
147: # <usec>1</usec>
148: # <usec>2</usec>
149: # </role>
150: # </roles>
151: # </user>
152: # </users>
153: #</class>
154: #
155: # Many of these are binary options (corresponding to either checkboxes or
156: # radio buttons in the interactive CCRS page). Examples include:
1.40.2.1 raeburn 157: # setpolicy, setcontent, setcomment, setkeys, disableresdis, disablechat, openall,
1.39 raeburn 158: # uniquecode
1.1 raeburn 159: #
160: # A value of 1 between opening and closing tags is equivalent to a
161: # checked checkbox or 'Yes' response in the original CCRS web page.
162: # A value of 0 or blank is equivalent to an unchecked box or 'No'
163: # response. Dates are in format YYYY:MM:DD:HH:MM:SS (:separators required)
164: #
1.15 raeburn 165: # firstres can be nav, syl, or blank for "Navigate Contents", Syllabus, or
1.1 raeburn 166: # no entry respectively.
1.15 raeburn 167: #
1.33 raeburn 168: # crstype can be Course or Community
1.15 raeburn 169: #
170: # crsquota is the total disk space permitted for course group portfolio files
171: # in all course groups.
172: #
1.1 raeburn 173: # For format of other parameters, refer to the interactive CCRS page
174: # and view how the equivalent parameter is displayed in the web form.
175: #
176: ##############################################################
177: # create_courses() - creates courses described in @$requests,
178: # files listed in @$requests are deleted
179: # after the files have been parsed.
180: #
1.3 raeburn 181: # Directory for retrieval of files listed in @$requests is:
182: # /home/httpd/perl/tmp/addcourse/$dom/auto/pending if $context = auto
1.13 raeburn 183: # /home/httpd/perl/tmp/addcourse/$dom/web/$uname_$udom/pending if $context = web
1.1 raeburn 184: #
1.39 raeburn 185: # inputs (six) - requests - ref to array of filename(s) containing course requests
1.1 raeburn 186: # courseids - ref to hash to store LON-CAPA course ids of new courses
187: # context - auto if called from command line, web if called from browser
188: # dom - domain for which the course is being created
1.3 raeburn 189: # uname - username of DC who is requesting course creation
190: # udom - domain of DC who is requesting course creation
1.1 raeburn 191: #
1.40.2.1.4.1! raeburn 192: # outputs (six) - output - text recording user roles added etc.
! 193: # logmsg - text to be logged
! 194: # cloneinfo - text output from cloning
! 195: # keysmsg - text containing link(s) to manage keys page(s)
! 196: # codehash - reference to hash containing courseID => unique code
! 197: # where unique code is a 6 character code, to distribute
! 198: # to students as a shortcut to the course.
! 199: # instcodes - references to hash of an array, where keys are
! 200: # institutional codes and values are an array of
! 201: # courseIDs of courses with that code.
1.1 raeburn 202: #############################################################
203:
204: sub create_courses {
1.3 raeburn 205: my ($requests,$courseids,$context,$dom,$uname,$udom) = @_;
1.1 raeburn 206: my $output;
207: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
208: # Get role names
209: my %longroles = ();
1.9 raeburn 210: open(FILE,"<$$perlvarref{'lonTabDir'}.'/rolesplain.tab");
1.1 raeburn 211: my @rolesplain = <FILE>;
212: close(FILE);
1.32 raeburn 213: foreach my $item (@rolesplain) {
214: if ($item =~ /^(st|ta|ep|ad|in|cc|co):([\w\s]+):?([\w\s]*)/) {
215: $longroles{'Course'}{$1} = $2;
216: $longroles{'Community'}{$1} = $3;
1.1 raeburn 217: }
218: }
1.40.2.1.4.1! raeburn 219: my ($logmsg,$cloneinfo,$keysmsg,$newusermsg,$addresult,$user_lh,
! 220: %codehash,%instcodes);
1.1 raeburn 221: my %enrollcount = ();
1.37 foxr 222: my $newcoursedir = LONCAPA::tempdir().'/addcourse/'.$dom.'/'.$context;
1.3 raeburn 223: if ($context eq 'auto') {
1.40.2.1.4.1! raeburn 224: $user_lh = &Apache::loncommon::user_lang($uname,$udom);
1.3 raeburn 225: $newcoursedir .= '/pending';
226: } else {
227: if ($uname && $udom) {
1.13 raeburn 228: $newcoursedir .= '/'.$uname.'_'.$udom.'/pending';
1.3 raeburn 229: } else {
230: $logmsg = "batchcreatecourse::create_courses() called without username and/or domain of requesting Domain Coordinator";
1.1 raeburn 231: }
232: }
233: if (@{$requests} > 0) {
234: foreach my $request (@{$requests}) {
235: my %details = ();
1.40.2.1.4.1! raeburn 236: my $clonemsg = [];
1.1 raeburn 237: if (-e $newcoursedir.'/'.$request) {
238: &parse_coursereqs($newcoursedir.'/'.$request, \%details);
1.29 raeburn 239: foreach my $num (sort(keys(%details))) {
240: my $reqdetails = $details{$num};
1.39 raeburn 241: my $code;
1.40.2.1.4.1! raeburn 242: my $courseid =
! 243: &build_course($dom,$num,$context,$reqdetails,\%longroles,\$logmsg,
! 244: $clonemsg,\$newusermsg,\$addresult,\%enrollcount,
! 245: \$output,\$keysmsg,undef,undef,undef,undef,\$code);
1.25 raeburn 246: if ($courseid =~m{^/$match_domain/$match_courseid}) {
1.21 raeburn 247: $$courseids{$courseid} = $details{$num}{'class'};
1.39 raeburn 248: if ($code) {
249: $codehash{$courseid} = $code;
250: }
1.40.2.1.4.1! raeburn 251: if ($details{$num}{'coursecode'} ne '') {
! 252: push(@{$instcodes{$details{$num}{'coursecode'}}},$courseid);
! 253: }
! 254: }
! 255: if (@{$clonemsg}) {
! 256: if ($context eq 'web') {
! 257: $cloneinfo .= '<p class="LC_info">';
! 258: }
! 259: foreach my $item (@{$clonemsg}) {
! 260: if (ref($item) eq 'HASH') {
! 261: if ($context eq 'auto') {
! 262: $cloneinfo .= &mt_user($user_lh,$item->{mt},
! 263: @{$item->{args}});
! 264: } else {
! 265: $cloneinfo .= &mt($item->{mt},@{$item->{args}});
! 266: }
! 267: }
! 268: if ($context eq 'web') {
! 269: $cloneinfo .= '<br />';
! 270: }
! 271: $cloneinfo .= "\n";
! 272: }
! 273: if ($context eq 'web') {
! 274: $cloneinfo .= '</p>';
! 275: }
1.21 raeburn 276: }
1.1 raeburn 277: }
278: }
279: }
280: }
1.40.2.1.4.1! raeburn 281: return ($output,$logmsg,$cloneinfo,$keysmsg,\%codehash,\%instcodes);
1.1 raeburn 282: }
283:
284: #############################################################
285: #
286: # parse_coursereqs()
287: # inputs (two) - coursefile - path to XML file containing course(s) to be created.
288: # - details - reference to hash containing extracted information.
289: # outputs (none)
290: #
291: ############################################################
292:
293: sub parse_coursereqs {
294: my ($coursefile,$details) = @_;
295: # Note all start and end dates should be in this format:
296: # YYYY:MM:DD:HH:MM:SS (:separators required).
297: my $uname = '';
298: my @state = ();
299: my $num = 0;
300: my $secid = 0;
301: my $xlist = 0;
302: my $userkey = '';
303: my $role = '';
1.40.2.1.4.1! raeburn 304: my @items = ('title','optional_id','coursecode','defaultcredits','coursehome','reshome','nonstandard','adds','drops','topmap','firstres','clonecrs','clonedom','datemode','dateshift','tinyurls','showphotos','setpolicy','setcontent','setcomment','setkeys','keyauth','disresdis','disablechat','openall','notify_owner','notify_dc','crstype','crsquota','uniquecode');
1.33 raeburn 305: my @possroles = qw(st ad ep ta in cc co);
1.1 raeburn 306: my @dateitems = ('enrollstart','enrollend','accessstart','accessend');
307: my @useritems = ('autharg','authtype','firstname','generation','lastname','middlename','studentID');
308: my $p = HTML::Parser->new
309: (
310: xml_mode => 1,
311: start_h =>
312: [sub {
313: my ($tagname, $attr) = @_;
314: push(@state, $tagname);
315: if ("@state" eq "class") {
316: %{$$details{$num}} = ();
317: $$details{$num}{'class'} = $attr->{id};
318: %{$$details{$num}{'users'}} = ();
319: %{$$details{$num}{'sections'}} = ();
320: $secid = 0;
321: $xlist = 0;
322: }
323: if ("@state" eq "class users user roles role") {
324: $role = $attr->{id};
1.33 raeburn 325: if (grep(/^\Q$role\E$/,@possroles)) {
1.1 raeburn 326: push(@{$$details{$num}{'users'}{$userkey}{'roles'}}, $role);
327: %{$$details{$num}{'users'}{$userkey}{$role}} = ();
328: @{$$details{$num}{'users'}{$userkey}{$role}{'usec'}} = ();
329: }
330: }
331: if ("@state" eq "class sections section") {
332: $secid ++;
333: %{$$details{$num}{'sections'}{$secid}} = ();
334: }
335: if ("@state" eq "class crosslists xlist") {
336: $xlist ++;
337: %{$$details{$num}{'crosslists'}{$xlist}} = ();
338: }
339: }, "tagname, attr"],
340: text_h =>
341: [sub {
342: my ($text) = @_;
343: if ("@state" eq "class owner username") {
344: $$details{$num}{'owner'} = $text;
345: } elsif ("@state" eq "class owner domain") {
346: $$details{$num}{'domain'} = $text;
347: } elsif ("@state" eq "class sections section inst") {
348: $$details{$num}{'sections'}{$secid}{'inst'} = $text;
349: } elsif ("@state" eq "class sections section loncapa") {
350: $$details{$num}{'sections'}{$secid}{'loncapa'} = $text;
351: } elsif ("@state" eq "class crosslists xlist inst") {
352: $$details{$num}{'crosslists'}{$xlist}{'inst'} = $text;
353: } elsif ("@state" eq "class crosslists xlist loncapa") {
354: $$details{$num}{'crosslists'}{$xlist}{'loncapa'} = $text;
355: } elsif ("@state" eq "class owner authtype") {
356: $$details{$num}{'ownerauthtype'} = $text;
357: } elsif ("@state" eq "class owner autharg") {
1.5 raeburn 358: $$details{$num}{'ownerauthparam'} = $text;
1.1 raeburn 359: } elsif ("@state" eq "class authentication method") {
360: $$details{$num}{'authtype'} = $text;
361: } elsif ("@state" eq "class authentication param") {
362: $$details{$num}{'authparam'} = $text;
363: } elsif ("@state" eq "class users user username") {
364: $userkey = $text;
365: } elsif ("@state" eq "class users user domain") {
366: $userkey .= ':'.$text;
367: %{$$details{$num}{'users'}{$userkey}} = ();
368: @{$$details{$num}{'users'}{$userkey}{'roles'}} = ();
369: } elsif ("@state" eq "class users user email") {
370: $$details{$num}{'users'}{$userkey}{'emailaddr'} = $text;
1.35 bisitz 371: $$details{$num}{'users'}{$userkey}{'emailenc'} = &LONCAPA::escape($text);
1.1 raeburn 372: } elsif ("@state" eq "class users user roles role start") {
1.33 raeburn 373: if (grep(/^\Q$role\E$/,@possroles)) {
1.1 raeburn 374: $$details{$num}{'users'}{$userkey}{$role}{'start'} = &process_date($text);
375: }
376: } elsif ("@state" eq "class users user roles role end") {
1.33 raeburn 377: if (grep(/^\Q$role\E$/,@possroles)) {
1.1 raeburn 378: $$details{$num}{'users'}{$userkey}{$role}{'end'} = &process_date($text);
379: }
380: } elsif ("@state" eq "class users user roles role usec") {
1.33 raeburn 381: if (grep(/^\Q$role\E$/,@possroles)) {
1.1 raeburn 382: unless ($text eq '') {
383: push(@{$$details{$num}{'users'}{$userkey}{$role}{'usec'}},$text);
384: }
385: }
386: } else {
387: foreach my $item (@items) {
388: if ("@state" eq "class $item") {
389: $$details{$num}{$item} = $text;
390: }
391: }
392: foreach my $item (@dateitems) {
393: if ("@state" eq "class $item") {
394: $$details{$num}{$item} = &process_date($text);
395: }
396: }
397: foreach my $item (@useritems) {
398: if ("@state" eq "class users user $item") {
399: $$details{$num}{'users'}{$userkey}{$item} = $text;
400: }
401: }
402: }
403: }, "dtext"],
404: end_h =>
405: [sub {
406: my ($tagname) = @_;
407: if ("@state" eq "class") {
408: $num ++;
409: }
410: pop @state;
411: }, "tagname"],
412: );
413:
414: $p->parse_file($coursefile);
415: $p->eof;
416: return;
417: }
418:
419: #########################################################
420: #
421: # build_course()
422: #
423: # inputs
1.28 raeburn 424: # course domain
1.1 raeburn 425: # course request number
426: # context - auto if called from command line, web if called from DC web interface
427: # ref to hash of course creation information
428: # ref to hash of role descriptions
429: # ref to scalar used to accumulate log messages
1.40.2.1.4.1! raeburn 430: # ref to array used to accumulate messages about cloning
1.1 raeburn 431: # ref to scalar used to accumulate messages sent to new users
432: # ref to scalar used to accumulate results of new user additions
433: # ref to hash of enrollment counts for different roles
1.28 raeburn 434: # ref to scalar used to accumulate information about added roles
435: # ref to scalar used to accumulate information about access keys
436: # domain of DC creating course
437: # username of DC creating course
438: # optional course number, if unique course number already obtained (e.g., for
1.39 raeburn 439: # course requests submitted via course request form.
440: # optional category
441: # optional ref to scalar for six character unique identifier
1.40.2.1.4.1! raeburn 442: # caller context (e.g., auto)
! 443: # user language handle, if caller context is 'auto'
1.1 raeburn 444: #
445: # outputs
446: # LON-CAPA courseID for new (created) course
447: #
448: #########################################################
449:
450: sub build_course {
1.40.2.1.4.1! raeburn 451: my ($cdom,$num,$context,$details,$longroles,$logmsg,$clonemsg,$newusermsg,
! 452: $addresult,$enrollcount,$output,$keysmsg,$udom,$uname,$cnum,$category,
! 453: $coderef,$callercontext,$user_lh) = @_;
1.29 raeburn 454: return unless (ref($details) eq 'HASH');
455: my $owner_uname = $details->{'owner'};
456: my $owner_domain = $details->{'domain'};
1.1 raeburn 457: my $owner = $owner_uname.':'.$owner_domain;
458: my $sectionstr = '';
459: my $xliststr = '';
460: my $noenddate = '';
461: my $outcome;
1.32 raeburn 462: my ($courseid,$crsudom,$crsunum,$crstype,$ccrole,$rolenames);
463: if ($details->{'crstype'} eq 'Community') {
464: $crstype = $details->{'crstype'};
465: $ccrole ='co';
466: if (ref($longroles) eq 'HASH') {
467: $rolenames = $longroles->{'Community'};
468: }
469: } else {
470: $crstype = 'Course';
471: $ccrole = 'cc';
472: if (ref($longroles) eq 'HASH') {
473: $rolenames = $longroles->{'Course'};
474: }
475: }
1.1 raeburn 476: my $linefeed;
477: if ($context eq 'auto') {
478: $linefeed = "\n";
479: } else {
480: $linefeed = "<br />\n";
481: }
1.29 raeburn 482: if ($details->{'accessend'} eq '') {
1.1 raeburn 483: $noenddate = 1;
484: }
1.29 raeburn 485: my $reshome = $details->{'reshome'};
1.1 raeburn 486: if ($reshome eq '') {
487: $reshome = '/res/'.$cdom;
488: }
1.29 raeburn 489: my $firstres = $details->{'firstres'};
1.1 raeburn 490: if ($firstres eq '') {
1.36 raeburn 491: if ($crstype eq 'Community') {
492: $firstres = 'nav';
493: } else {
494: $firstres = 'syl';
495: }
1.1 raeburn 496: }
1.29 raeburn 497: foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
498: $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
1.1 raeburn 499: }
1.5 raeburn 500: $sectionstr =~ s/,$//;
1.1 raeburn 501:
1.29 raeburn 502: foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
503: $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
1.1 raeburn 504: }
1.5 raeburn 505: $xliststr =~ s/,$//;
1.1 raeburn 506:
507: my %courseinfo = (
1.29 raeburn 508: inst_code => $details->{'coursecode'},
509: description => $details->{'title'}
1.1 raeburn 510: );
1.29 raeburn 511: if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
512: my $ownerargs = {'auth' => $details->{'ownerauthtype'},
513: 'authparam' => $details->{'ownerauthparam'},
514: 'emailenc' => $details->{'emailenc'},
515: 'udom' => $details->{'domain'},
516: 'uname' => $details->{'owner'},
517: 'pid' => $details->{'users'}{$owner}{'studentID'},
518: 'first' => $details->{'users'}{$owner}{'firstname'},
519: 'middle' => $details->{'users'}{$owner}{'middlename'},
520: 'last' => $details->{'users'}{$owner}{'lastname'},
521: 'gene' => $details->{'users'}{$owner}{'generation'},
1.1 raeburn 522: 'usec' => '',
523: 'end' => '',
524: 'start' => '',
1.29 raeburn 525: 'emailaddr' => $details->{'users'}{$owner}{'email'},
1.1 raeburn 526: 'cid' => '',
527: 'context' => 'createowner',
528: 'linefeed' => $linefeed,
1.32 raeburn 529: 'role' => $ccrole,
1.5 raeburn 530: };
1.32 raeburn 531: $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
1.1 raeburn 532: } else {
533: $outcome = 'ok';
534: }
535:
1.6 raeburn 536: if ($outcome eq 'ok') {
1.29 raeburn 537: if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
538: $details->{'datemode'} = 'shift';
539: $details->{'dateshift'} = 365;
1.27 raeburn 540: }
1.40.2.1.4.1! raeburn 541: if ($details->{'tinyurls'} !~ /^(delete|transfer|create)$/) {
! 542: $details->{'tinyurls'} = 'create';
! 543: }
1.6 raeburn 544: my $courseargs = {
1.29 raeburn 545: ccuname => $details->{'owner'},
546: ccdomain => $details->{'domain'},
547: cdescr => $details->{'title'},
548: crstype => $details->{'crstype'},
549: curl => $details->{'topmap'},
1.1 raeburn 550: course_domain => $cdom,
1.29 raeburn 551: course_home => $details->{'coursehome'},
552: nonstandard => $details->{'nonstandard'},
553: crscode => $details->{'coursecode'},
1.38 raeburn 554: defaultcredits => $details->{'defaultcredits'},
1.29 raeburn 555: crsquota => $details->{'crsquota'},
1.39 raeburn 556: uniquecode => $details->{'uniquecode'},
1.29 raeburn 557: clonecourse => $details->{'clonecrs'},
558: clonedomain => $details->{'clonedom'},
559: datemode => $details->{'datemode'},
560: dateshift => $details->{'dateshift'},
1.40.2.1.4.1! raeburn 561: tinyurls => $details->{'tinyurls'},
1.29 raeburn 562: crsid => $details->{'optional_id'},
563: curruser => $details->{'owner'},
1.1 raeburn 564: crssections => $sectionstr,
565: crsxlist => $xliststr,
1.29 raeburn 566: autoadds => $details->{'adds'},
567: autodrops => $details->{'drops'},
568: notify => $details->{'notify_owner'},
569: notify_dc => $details->{'notify_dc'},
1.1 raeburn 570: no_end_date => $noenddate,
1.29 raeburn 571: showphotos => $details->{'showphotos'},
572: authtype => $details->{'authtype'},
573: autharg => $details->{'authparam'},
574: enrollstart => $details->{'enrollstart'},
575: enrollend => $details->{'enrollend'},
576: startaccess => $details->{'accessstart'},
577: endaccess => $details->{'accessend'},
578: setpolicy => $details->{'setpolicy'},
579: setcontent => $details->{'setcontent'},
1.40.2.1 raeburn 580: setcomment => $details->{'setcomment'},
1.1 raeburn 581: reshome => $reshome,
1.29 raeburn 582: setkeys => $details->{'setkeys'},
583: keyauth => $details->{'keyauth'},
584: disresdis => $details->{'disresdis'},
585: disablechat => $details->{'disablechat'},
586: openall => $details->{'openall'},
1.1 raeburn 587: firstres => $firstres
588: };
1.40 raeburn 589: if ($details->{'textbook'}) {
590: $courseargs->{'textbook'} = $details->{'textbook'};
591: }
1.20 albertel 592: my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
1.29 raeburn 593: if (! exists($host_servers{$details->{'coursehome'}})) {
594: $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
1.1 raeburn 595: return;
596: }
1.40.2.1.4.1! raeburn 597: my ($success,$msg,$cloneinfo) =
1.39 raeburn 598: &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
1.40.2.1.4.1! raeburn 599: $udom,$uname,$context,$cnum,$category,$coderef,
! 600: $callercontext,$user_lh);
1.23 albertel 601: $$logmsg .= $msg;
1.40.2.1.4.1! raeburn 602: if ((ref($clonemsg) eq 'ARRAY') && (ref($cloneinfo) eq 'ARRAY')) {
! 603: push(@{$clonemsg},@{$cloneinfo});
! 604: }
1.24 raeburn 605: if (!$success) {
606: return;
607: }
1.1 raeburn 608: } else {
609: return;
610: }
1.40.2.1.4.1! raeburn 611:
1.1 raeburn 612: #
1.32 raeburn 613: # Make owner a coordinator
1.1 raeburn 614: #
615: if (($owner_domain) && ($owner_uname)) {
1.32 raeburn 616: &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
1.1 raeburn 617: }
618:
619: #
620: # Process other reqested users
621: #
1.32 raeburn 622:
623: my @courseroles = qw(st ep ta in);
624: push(@courseroles,$ccrole);
1.31 raeburn 625: if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
626: push(@courseroles,'ad');
627: }
1.1 raeburn 628: my $stulogmsg = '';
1.29 raeburn 629: foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
1.1 raeburn 630: my $url = '/'.$crsudom.'/'.$crsunum;
1.29 raeburn 631: next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');
632: if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
1.1 raeburn 633: my ($username,$userdom) = split/:/,$userkey;
634: if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
1.31 raeburn 635: my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
636: my @badroles;
637: my $firstrole = shift(@reqroles);
638: while (@reqroles > 0) {
639: if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
640: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
641: last;
642: } else {
643: push(@badroles,$firstrole);
644: $firstrole = shift(@reqroles);
645: }
646: } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
647: last;
648: } else {
649: push(@badroles,$firstrole);
650: $firstrole = shift(@reqroles);
651: }
652: }
653: if (@badroles > 0) {
654: if (@badroles > 1) {
655: $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
656: } else {
657: $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]);
658: }
659: }
660: my $firstsec;
1.32 raeburn 661: unless (($firstrole eq $ccrole) || ($firstrole eq '')) {
1.31 raeburn 662: $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
663: }
1.5 raeburn 664: my $userargs = {
1.29 raeburn 665: 'auth' => $details->{'users'}{$userkey}{'authtype'},
666: 'authparam' => $details->{'users'}{$userkey}{'autharg'},
667: 'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
1.7 raeburn 668: 'udom' => $userdom,
1.1 raeburn 669: 'uname' => $username,
1.29 raeburn 670: 'pid' => $details->{'users'}{$userkey}{'studentID'},
671: 'first' => $details->{'users'}{$userkey}{'firstname'},
672: 'middle' => $details->{'users'}{$userkey}{'middlename'},
673: 'last' => $details->{'users'}{$userkey}{'lastname'},
674: 'gene' => $details->{'users'}{$userkey}{'generation'},
1.1 raeburn 675: 'usec' => $firstsec,
1.29 raeburn 676: 'end' => $details->{'users'}{$userkey}{'end'},
677: 'start' => $details->{'users'}{$userkey}{'start'},
678: 'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
1.1 raeburn 679: 'cid' => $courseid,
1.8 raeburn 680: 'crs' => $crsunum,
681: 'cdom' => $crsudom,
1.1 raeburn 682: 'context' => 'createcourse',
683: 'linefeed' => $linefeed,
1.38 raeburn 684: 'role' => $details->{'users'}{$userkey}{'roles'}[0],
1.5 raeburn 685: };
1.38 raeburn 686: if ($userargs->{'role'} eq 'st') {
687: if (exists($details->{'users'}{$userkey}{'credits'})) {
688: $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
689: $userargs->{'credits'} =~ s/[^\d\.]//g;
690: }
691: }
1.22 raeburn 692: $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
1.1 raeburn 693: # now add other roles and other sections.
694: if ($outcome eq 'ok') {
1.32 raeburn 695: if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
1.29 raeburn 696: for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
1.1 raeburn 697: my $curr_role = $firstrole;
1.29 raeburn 698: my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
699: my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
700: my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
1.1 raeburn 701: $url = '/'.$crsudom.'/'.$crsunum;
702: if ($usec ne '') {
703: $url .= '/'.$usec;
704: }
1.31 raeburn 705: if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
706: $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
707: } else {
708: $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
709: }
1.1 raeburn 710: }
711: }
1.31 raeburn 712: if (@reqroles > 0) {
713: foreach my $curr_role (@reqroles) {
1.29 raeburn 714: my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
715: my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1 raeburn 716: if ($curr_role eq 'st') {
1.29 raeburn 717: my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1 raeburn 718: $url = '/'.$crsudom.'/'.$crsunum;
719: if ($usec ne '') {
720: $url .= '/'.$usec;
721: }
1.38 raeburn 722: my $credits;
723: if (exists($details->{'users'}{$userkey}{'credits'})) {
724: $credits = $details->{'users'}{$userkey}{'credits'};
725: $credits =~ s/[^\d\.]//g;
726: }
727: $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.32 raeburn 728: } elsif ($curr_role eq $ccrole) {
1.31 raeburn 729: $url = '/'.$crsudom.'/'.$crsunum;
730: my $usec = '';
731: $$output .=
732: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
733: } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
734: ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29 raeburn 735: foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1 raeburn 736: $url = '/'.$crsudom.'/'.$crsunum;
737: if ($usec ne '') {
738: $url .= '/'.$usec;
739: }
1.31 raeburn 740: if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
741: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
742: $$output .=
743: &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
744: } else {
745: $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
746: }
747: } else {
748: $$output .=
749: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
750: }
1.1 raeburn 751: }
1.31 raeburn 752: } else {
753: $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1 raeburn 754: }
755: }
756: }
757: }
758: } else {
1.29 raeburn 759: foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
760: my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
761: my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1 raeburn 762: if ($curr_role eq 'st') {
1.29 raeburn 763: my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1 raeburn 764: $url = '/'.$crsudom.'/'.$crsunum;
765: if ($usec ne '') {
766: $url .= '/'.$usec;
767: }
1.38 raeburn 768: my $credits;
769: if (exists($details->{'users'}{$userkey}{'credits'})) {
770: $credits = $details->{'users'}{$userkey}{'credits'};
771: $credits =~ s/[^\d\.]//g;
772: }
773: $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
1.31 raeburn 774: } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
775: ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29 raeburn 776: if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
777: foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1 raeburn 778: $url = '/'.$crsudom.'/'.$crsunum;
779: if ($usec ne '') {
780: $url .= '/'.$usec;
781: }
1.31 raeburn 782: my $stdresult;
783: if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
784: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
785: $stdresult =
786: &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
787: } else {
788: $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
789: }
790: } else {
791: $stdresult =
792: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
793: }
1.5 raeburn 794: $$output .= $stdresult;
1.1 raeburn 795: }
796: } else {
1.5 raeburn 797: $url = '/'.$crsudom.'/'.$crsunum;
1.31 raeburn 798: if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
799: if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
800: $$output .=
801: &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
802: } else {
803: $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
804: }
805: } else {
806: $$output .=
807: &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
808: }
1.1 raeburn 809: }
1.31 raeburn 810: } else {
811: $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1 raeburn 812: }
813: }
814: }
815: }
816: }
817:
818: # Information about keys.
1.29 raeburn 819: if ($details->{'setkeys'}) {
1.1 raeburn 820: $$keysmsg .=
1.29 raeburn 821: '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
1.1 raeburn 822: }
823: # Flush the course logs so reverse user roles immediately updated
824: &Apache::lonnet::flushcourselogs();
825: return $courseid;
826: }
827:
1.31 raeburn 828: sub owner_is_dc {
829: my ($owner_uname,$owner_dom,$cdom) = @_;
830: my $is_dc = 0;
831: my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
832: ['active'],['dc'],[$cdom]);
833: if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
834: $is_dc = 1;
835: }
836: return $is_dc;
837: }
838:
1.1 raeburn 839: #########################################################
840: #
841: # process_date()
842: #
843: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
844: # output - corresponding UNIX time (seconds since epoch).
845: #
846: #########################################################
847:
848: sub process_date {
849: my $timestr = shift;
850: my $timestamp = '';
1.5 raeburn 851: if ($timestr !~ /:/) {
1.1 raeburn 852: $timestamp = '';
853: } else {
1.12 albertel 854: my @entries = split(/:/,$timestr);
1.1 raeburn 855: for (my $j=0; $j<@entries; $j++) {
856: if ( length($entries[$j]) > 1 ) {
857: $entries[$j] =~ s/^0//;
858: }
1.10 raeburn 859: $entries[$j] =~ s/\D//g;
860: if ($entries[$j] < 0) {
861: $entries[$j] = 0;
862: }
1.1 raeburn 863: }
1.5 raeburn 864: if ($entries[1] > 0) {
865: $entries[1] = $entries[1] - 1;
866: }
1.10 raeburn 867: if ($entries[5] > 60) {
868: $entries[5] = 60;
869: }
870: if ($entries[4] > 59) {
871: $entries[4] = 59;
872: }
873: if ($entries[3] > 23) {
874: $entries[3] = 23;
875: }
876: if ($entries[2] > 31) {
877: $entries[2] = 31;
878: }
879: if ($entries[1] > 11) {
880: $entries[1] = 11;
881: }
882: if ($entries[2] == 31) {
883: if (($entries[1] == 3) || ($entries[1] == 5) ||
884: ($entries[1] == 8) || ($entries[1] == 10)) {
885: $entries[2] = 30;
886: }
887: }
888: if ($entries[1] == 1) {
889: if ($entries[2] > 29) {
890: $entries[2] = 29;
891: }
892: if ($entries[2] == 29) {
1.12 albertel 893: if ($entries[0]%4 != 0) {
1.14 raeburn 894: $entries[2] = 28;
1.12 albertel 895: } elsif ( $entries[0]%100 == 0
896: && $entries[0]%400 != 0) {
1.14 raeburn 897: $entries[2] = 28;
1.12 albertel 898: }
1.10 raeburn 899: }
900: }
1.1 raeburn 901: $timestamp = timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
902: }
903: return $timestamp;
904: }
905:
906: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>