File:  [LON-CAPA] / loncom / automation / batchcreatecourse.pm
Revision 1.40: download - view: text, annotated - select for diffs
Fri Jan 3 18:42:16 2014 UTC (10 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_X, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0, HEAD
- Store title of textbook cloned for 'textbook' courses (as internal.textbook)
  in course's environment.db.

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

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