File:  [LON-CAPA] / loncom / automation / batchcreatecourse.pm
Revision 1.40.2.3.2.1: download - view: text, annotated - select for diffs
Mon Jul 20 01:02:00 2020 UTC (3 years, 9 months ago) by raeburn
Branches: version_2_11_3_msu
CVS tags: version_2_11_4_msu
Diff to branchpoint 1.40.2.3: preferred, unified
- For 2.11.3 (modified)
  Include changes in 1.45

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

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