Annotation of loncom/automation/batchcreatecourse.pm, revision 1.44

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>