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

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

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