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

1.4       albertel    1: #
1.33    ! raeburn     2: # $Id: batchcreatecourse.pm,v 1.32 2009/10/31 17:54:51 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 = ();
                    208:     my $newcoursedir = $$perlvarref{'lonDaemons'}.'/tmp/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;
                    323:                     $$details{$num}{'users'}{$userkey}{'emailenc'} = &Apache::lonnet::escape($text); 
                    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 '') {
                    437:         $firstres = 'syl';
                    438:     }
1.29      raeburn   439:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
                    440:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
1.1       raeburn   441:     }
1.5       raeburn   442:     $sectionstr =~ s/,$//;
1.1       raeburn   443: 
1.29      raeburn   444:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
                    445:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
1.1       raeburn   446:     }
1.5       raeburn   447:     $xliststr =~ s/,$//;
1.1       raeburn   448: 
                    449:     my %courseinfo = (
1.29      raeburn   450:                       inst_code => $details->{'coursecode'},
                    451:                       description => $details->{'title'}
1.1       raeburn   452:                      ); 
1.29      raeburn   453:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
                    454:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
                    455:                     'authparam' => $details->{'ownerauthparam'},
                    456:                     'emailenc' => $details->{'emailenc'},
                    457:                     'udom' => $details->{'domain'},
                    458:                     'uname' => $details->{'owner'},
                    459:                     'pid' => $details->{'users'}{$owner}{'studentID'},
                    460:                     'first' => $details->{'users'}{$owner}{'firstname'},
                    461:                     'middle' => $details->{'users'}{$owner}{'middlename'},
                    462:                     'last' => $details->{'users'}{$owner}{'lastname'},
                    463:                     'gene' => $details->{'users'}{$owner}{'generation'},
1.1       raeburn   464:                     'usec' => '',
                    465:                     'end' => '',
                    466:                     'start' => '',
1.29      raeburn   467:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
1.1       raeburn   468:                     'cid' => '',
                    469:                     'context' => 'createowner',
                    470:                     'linefeed' => $linefeed,
1.32      raeburn   471:                     'role' => $ccrole,
1.5       raeburn   472:                    };
1.32      raeburn   473:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
1.1       raeburn   474:     } else {
                    475:         $outcome = 'ok';
                    476:     }
                    477: 
1.6       raeburn   478:     if ($outcome eq 'ok') {
1.29      raeburn   479:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
                    480:             $details->{'datemode'} = 'shift';
                    481:             $details->{'dateshift'} = 365;
1.27      raeburn   482:         }
1.6       raeburn   483:         my $courseargs = {
1.29      raeburn   484:                ccuname => $details->{'owner'},
                    485:                ccdomain => $details->{'domain'},
                    486:                cdescr => $details->{'title'},
                    487:                crstype => $details->{'crstype'},
                    488:                curl => $details->{'topmap'},
1.1       raeburn   489:                course_domain => $cdom,
1.29      raeburn   490:                course_home =>  $details->{'coursehome'},
                    491:                nonstandard => $details->{'nonstandard'},
                    492:                crscode => $details->{'coursecode'},
                    493:                crsquota => $details->{'crsquota'},
                    494:                clonecourse => $details->{'clonecrs'},
                    495:                clonedomain => $details->{'clonedom'},
                    496:                datemode => $details->{'datemode'},
                    497:                dateshift => $details->{'dateshift'},
                    498:                crsid => $details->{'optional_id'},
                    499:                curruser => $details->{'owner'},
1.1       raeburn   500:                crssections => $sectionstr,
                    501:                crsxlist => $xliststr,
1.29      raeburn   502:                autoadds => $details->{'adds'},
                    503:                autodrops => $details->{'drops'},
                    504:                notify => $details->{'notify_owner'},
                    505:                notify_dc => $details->{'notify_dc'},
1.1       raeburn   506:                no_end_date => $noenddate,
1.29      raeburn   507:                showphotos => $details->{'showphotos'},
                    508:                authtype => $details->{'authtype'},
                    509:                autharg => $details->{'authparam'},
                    510:                enrollstart => $details->{'enrollstart'},
                    511:                enrollend => $details->{'enrollend'},
                    512:                startaccess => $details->{'accessstart'},
                    513:                endaccess => $details->{'accessend'},
                    514:                setpolicy => $details->{'setpolicy'},
                    515:                setcontent => $details->{'setcontent'},
1.1       raeburn   516:                reshome => $reshome,
1.29      raeburn   517:                setkeys => $details->{'setkeys'},
                    518:                keyauth => $details->{'keyauth'},
                    519:                disresdis => $details->{'disresdis'},
                    520:                disablechat => $details->{'disablechat'},
                    521:                openall => $details->{'openall'},
1.1       raeburn   522:                firstres => $firstres
                    523:                };
1.20      albertel  524:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
1.29      raeburn   525:         if (! exists($host_servers{$details->{'coursehome'}})) {
                    526:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
1.1       raeburn   527:             return;
                    528:         }
1.30      raeburn   529:         my ($success, $msg) = &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,$udom,$uname,$context,$cnum,$category);
1.23      albertel  530: 	$$logmsg .= $msg;
1.24      raeburn   531:         if (!$success) {
                    532:             return;
                    533:         }
1.1       raeburn   534:     } else {
                    535:         return;
                    536:     }
                    537:     
                    538: #
1.32      raeburn   539: # Make owner a coordinator
1.1       raeburn   540: #
                    541:     if (($owner_domain) && ($owner_uname)) {
1.32      raeburn   542:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
1.1       raeburn   543:     }
                    544: 
                    545: #
                    546: # Process other reqested users
                    547: #
1.32      raeburn   548: 
                    549:     my @courseroles = qw(st ep ta in);
                    550:     push(@courseroles,$ccrole);
1.31      raeburn   551:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
                    552:         push(@courseroles,'ad');
                    553:     }
1.1       raeburn   554:     my $stulogmsg = '';
1.29      raeburn   555:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
1.1       raeburn   556:         my $url = '/'.$crsudom.'/'.$crsunum;
1.29      raeburn   557:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
                    558:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
1.1       raeburn   559:             my ($username,$userdom) = split/:/,$userkey;
                    560:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
1.31      raeburn   561:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
                    562:                 my @badroles;
                    563:                 my $firstrole = shift(@reqroles);
                    564:                 while (@reqroles > 0) { 
                    565:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    566:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    567:                             last;
                    568:                         } else {
                    569:                             push(@badroles,$firstrole);
                    570:                             $firstrole = shift(@reqroles);
                    571:                         }
                    572:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
                    573:                         last;
                    574:                     } else {
                    575:                         push(@badroles,$firstrole);
                    576:                         $firstrole = shift(@reqroles);
                    577:                     }
                    578:                 }
                    579:                 if (@badroles > 0) {
                    580:                     if (@badroles > 1) {
                    581:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
                    582:                     } else {
                    583:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
                    584:                     }
                    585:                 }
                    586:                 my $firstsec;
1.32      raeburn   587:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
1.31      raeburn   588:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
                    589:                 }
1.5       raeburn   590:                 my $userargs = {
1.29      raeburn   591:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
                    592:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
                    593:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
1.7       raeburn   594:                     'udom' => $userdom,
1.1       raeburn   595:                     'uname' => $username,
1.29      raeburn   596:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
                    597:                     'first' => $details->{'users'}{$userkey}{'firstname'},
                    598:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
                    599:                     'last' => $details->{'users'}{$userkey}{'lastname'},
                    600:                     'gene' => $details->{'users'}{$userkey}{'generation'},
1.1       raeburn   601:                     'usec' => $firstsec,
1.29      raeburn   602:                     'end' => $details->{'users'}{$userkey}{'end'},
                    603:                     'start' => $details->{'users'}{$userkey}{'start'},
                    604:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
1.1       raeburn   605:                     'cid' => $courseid,
1.8       raeburn   606:                     'crs' => $crsunum,
                    607:                     'cdom' => $crsudom,
1.1       raeburn   608:                     'context' => 'createcourse',
                    609:                     'linefeed' => $linefeed,
1.29      raeburn   610:                     'role' => $details->{'users'}{$userkey}{'roles'}[0], 
1.5       raeburn   611:                    };
1.22      raeburn   612:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
1.1       raeburn   613: # now add other roles and other sections.
                    614:                 if ($outcome eq 'ok') {
1.32      raeburn   615:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
1.29      raeburn   616:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
1.1       raeburn   617:                             my $curr_role = $firstrole;
1.29      raeburn   618:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    619:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
                    620:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
1.1       raeburn   621:                             $url = '/'.$crsudom.'/'.$crsunum;
                    622:                             if ($usec ne '') {
                    623:                                 $url .= '/'.$usec;
                    624:                             }
1.31      raeburn   625:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    626:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    627:                             } else {
                    628:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    629:                             }
1.1       raeburn   630:                         }
                    631:                     }
1.31      raeburn   632:                     if (@reqroles > 0) {
                    633:                         foreach my $curr_role (@reqroles) {
1.29      raeburn   634:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    635:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   636:                             if ($curr_role eq 'st') {
1.29      raeburn   637:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   638:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    639:                                 if ($usec ne '') {
                    640:                                     $url .= '/'.$usec;
                    641:                                 }
1.22      raeburn   642:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
1.32      raeburn   643:                             } elsif ($curr_role eq $ccrole) {
1.31      raeburn   644:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    645:                                 my $usec = '';
                    646:                                 $$output .=
                    647:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    648:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
                    649:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   650:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   651:                                     $url = '/'.$crsudom.'/'.$crsunum;
                    652:                                     if ($usec ne '') {
                    653:                                         $url .= '/'.$usec;
                    654:                                     }
1.31      raeburn   655:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    656:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    657:                                             $$output .= 
                    658:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    659:                                         } else {
                    660:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    661:                                         }
                    662:                                     } else {
                    663:                                         $$output .= 
                    664:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    665:                                     }
1.1       raeburn   666:                                 }
1.31      raeburn   667:                             } else {
                    668:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   669:                             }
                    670:                         }
                    671:                     }
                    672:                 }
                    673:             } else {
1.29      raeburn   674:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
                    675:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
                    676:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
1.1       raeburn   677:                     if ($curr_role eq 'st') {
1.29      raeburn   678:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
1.1       raeburn   679:                         $url = '/'.$crsudom.'/'.$crsunum;
                    680:                         if ($usec ne '') {
                    681:                             $url .= '/'.$usec;
                    682:                         }
1.22      raeburn   683:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
1.31      raeburn   684:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
                    685:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
1.29      raeburn   686:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
                    687:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
1.1       raeburn   688:                                 $url = '/'.$crsudom.'/'.$crsunum;
                    689:                                 if ($usec ne '') {
                    690:                                     $url .= '/'.$usec;
                    691:                                 }
1.31      raeburn   692:                                 my $stdresult;
                    693:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    694:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    695:                                         $stdresult = 
                    696:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    697:                                     } else {
                    698:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    699:                                     }
                    700:                                 } else {
                    701:                                     $stdresult = 
                    702:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
                    703:                                 }
1.5       raeburn   704:                                 $$output .= $stdresult;
1.1       raeburn   705:                             }
                    706:                         } else {
1.5       raeburn   707:                             $url = '/'.$crsudom.'/'.$crsunum;
1.31      raeburn   708:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                    709:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
                    710:                                     $$output .= 
                    711:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
                    712:                                 } else {
                    713:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
                    714:                                 }
                    715:                             } else {
                    716:                                 $$output .= 
                    717:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
                    718:                             }
1.1       raeburn   719:                         }
1.31      raeburn   720:                     } else {
                    721:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
1.1       raeburn   722:                     }
                    723:                 }
                    724:             }
                    725:         }
                    726:     }
                    727: 
                    728: # Information about keys.
1.29      raeburn   729:     if ($details->{'setkeys'}) {
1.1       raeburn   730:         $$keysmsg .=
1.29      raeburn   731:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
1.1       raeburn   732:     }
                    733: # Flush the course logs so reverse user roles immediately updated
                    734:     &Apache::lonnet::flushcourselogs();
                    735:     return $courseid;
                    736: }
                    737: 
1.31      raeburn   738: sub owner_is_dc {
                    739:     my ($owner_uname,$owner_dom,$cdom) = @_;
                    740:     my $is_dc = 0;
                    741:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
                    742:                     ['active'],['dc'],[$cdom]);
                    743:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
                    744:         $is_dc = 1;
                    745:     }
                    746:     return $is_dc;
                    747: }
                    748: 
1.1       raeburn   749: #########################################################
                    750: #
                    751: # process_date()
                    752: # 
                    753: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
                    754: # output - corresponding UNIX time (seconds since epoch). 
                    755: #
                    756: #########################################################
                    757: 
                    758: sub process_date {
                    759:     my $timestr = shift;
                    760:     my $timestamp = '';
1.5       raeburn   761:     if ($timestr !~ /:/) {
1.1       raeburn   762:         $timestamp = '';
                    763:     } else {
1.12      albertel  764:         my @entries = split(/:/,$timestr);
1.1       raeburn   765:         for (my $j=0; $j<@entries; $j++) {
                    766:             if ( length($entries[$j]) > 1 ) {
                    767:                 $entries[$j] =~ s/^0//;
                    768:             }
1.10      raeburn   769:             $entries[$j] =~ s/\D//g;
                    770:             if ($entries[$j] < 0) {
                    771:                 $entries[$j] = 0;
                    772:             }
1.1       raeburn   773:         }
1.5       raeburn   774:         if ($entries[1] > 0) {  
                    775:             $entries[1] = $entries[1] - 1;
                    776:         }
1.10      raeburn   777:         if ($entries[5] > 60) {
                    778:             $entries[5] = 60;
                    779:         }
                    780:         if ($entries[4] > 59) {
                    781:             $entries[4] = 59;
                    782:         }
                    783:         if ($entries[3] > 23) {
                    784:             $entries[3] = 23;
                    785:         }
                    786:         if ($entries[2] > 31) {
                    787:             $entries[2] = 31;
                    788:         }
                    789:         if ($entries[1] > 11) {
                    790:             $entries[1] = 11;
                    791:         }
                    792:         if ($entries[2] == 31) {
                    793:             if (($entries[1] == 3) || ($entries[1] == 5) ||
                    794:                 ($entries[1] == 8) || ($entries[1] == 10)) {
                    795:                 $entries[2] = 30;
                    796:             }
                    797:         }
                    798:         if ($entries[1] == 1) {
                    799:             if ($entries[2] > 29) {
                    800:                 $entries[2] = 29;
                    801:             }
                    802:             if ($entries[2] == 29) {
1.12      albertel  803:                 if ($entries[0]%4 != 0) {
1.14      raeburn   804:                     $entries[2] = 28;
1.12      albertel  805:                 } elsif ( $entries[0]%100 == 0
                    806: 			  && $entries[0]%400 != 0) {
1.14      raeburn   807: 		    $entries[2] = 28;
1.12      albertel  808: 		}
1.10      raeburn   809:             }
                    810:         }     
1.1       raeburn   811:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
                    812:     }
                    813:     return $timestamp;
                    814: }
                    815: 
                    816: 1;

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