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

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

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