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