File:  [LON-CAPA] / loncom / automation / batchcreatecourse.pm
Revision 1.45: download - view: text, annotated - select for diffs
Wed Jul 1 20:09:03 2020 UTC (3 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bug 6400
  Options for transfer or creation of tiny URLs when cloning.

    1: #
    2: # $Id: batchcreatecourse.pm,v 1.45 2020/07/01 20:09:03 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, Community or Placement
  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:         if ($details->{'crstype'} eq 'Placement') {
  472:             $crstype = $details->{'crstype'};
  473:         } else {
  474:             $crstype = 'Course';
  475:         }
  476:         $ccrole = 'cc';
  477:         if (ref($longroles) eq 'HASH') {
  478:             $rolenames = $longroles->{'Course'};
  479:         }
  480:     }
  481:     my $linefeed;
  482:     if ($context eq 'auto') {
  483:         $linefeed = "\n";
  484:     } else {
  485:         $linefeed = "<br />\n";
  486:     }
  487:     if ($details->{'accessend'} eq '') {
  488:         $noenddate = 1;
  489:     }
  490:     my $reshome = $details->{'reshome'};
  491:     if ($reshome eq '') {
  492:         $reshome = '/res/'.$cdom;
  493:     }
  494:     my $firstres =  $details->{'firstres'};
  495:     if ($firstres eq '') {
  496:         if ($crstype eq 'Community') {
  497:             $firstres = 'nav';
  498:         } elsif ($crstype eq 'Placement') {
  499:             $firstres = 'blank'; 
  500:         } else {
  501:             $firstres = 'syl';
  502:         }
  503:     }
  504:     foreach my $secid (sort(keys(%{$details->{'sections'}}))) {
  505:         $sectionstr .= $details->{'sections'}{$secid}{'inst'}.':'.$details->{'sections'}{$secid}{'loncapa'}.',';
  506:     }
  507:     $sectionstr =~ s/,$//;
  508: 
  509:     foreach my $xlist (sort(keys(%{$details->{'crosslists'}}))) {
  510:         $xliststr .= $details->{'crosslists'}{$xlist}{'inst'}.':'.$details->{'crosslists'}{$xlist}{'loncapa'}.',';
  511:     }
  512:     $xliststr =~ s/,$//;
  513: 
  514:     my %courseinfo = (
  515:                       inst_code => $details->{'coursecode'},
  516:                       description => $details->{'title'}
  517:                      ); 
  518:     if (&Apache::lonnet::homeserver($details->{'owner'},$details->{'domain'}) eq 'no_host') { # Add user if no account
  519:         my $ownerargs = {'auth' => $details->{'ownerauthtype'},
  520:                     'authparam' => $details->{'ownerauthparam'},
  521:                     'emailenc' => $details->{'emailenc'},
  522:                     'udom' => $details->{'domain'},
  523:                     'uname' => $details->{'owner'},
  524:                     'pid' => $details->{'users'}{$owner}{'studentID'},
  525:                     'first' => $details->{'users'}{$owner}{'firstname'},
  526:                     'middle' => $details->{'users'}{$owner}{'middlename'},
  527:                     'last' => $details->{'users'}{$owner}{'lastname'},
  528:                     'gene' => $details->{'users'}{$owner}{'generation'},
  529:                     'usec' => '',
  530:                     'end' => '',
  531:                     'start' => '',
  532:                     'emailaddr' => $details->{'users'}{$owner}{'email'},
  533:                     'cid' => '',
  534:                     'context' => 'createowner',
  535:                     'linefeed' => $linefeed,
  536:                     'role' => $ccrole,
  537:                    };
  538:         $outcome = &LONCAPA::Enrollment::create_newuser($ownerargs,$logmsg,$newusermsg,$enrollcount,$addresult,$rolenames,\%courseinfo,$context);
  539:     } else {
  540:         $outcome = 'ok';
  541:     }
  542: 
  543:     if ($outcome eq 'ok') {
  544:         if ($details->{'datemode'} !~ /^(preserve|shift|delete)$/) {
  545:             $details->{'datemode'} = 'shift';
  546:             $details->{'dateshift'} = 365;
  547:         }
  548:         if ($details->{'tinyurls'} !~ /^(delete|transfer|create)$/) {
  549:             $details->{'tinyurls'} = 'create';
  550:         }
  551:         my $courseargs = {
  552:                ccuname => $details->{'owner'},
  553:                ccdomain => $details->{'domain'},
  554:                cdescr => $details->{'title'},
  555:                crstype => $details->{'crstype'},
  556:                curl => $details->{'topmap'},
  557:                course_domain => $cdom,
  558:                course_home =>  $details->{'coursehome'},
  559:                nonstandard => $details->{'nonstandard'},
  560:                crscode => $details->{'coursecode'},
  561:                defaultcredits => $details->{'defaultcredits'},
  562:                crsquota => $details->{'crsquota'},
  563:                uniquecode => $details->{'uniquecode'},
  564:                clonecourse => $details->{'clonecrs'},
  565:                clonedomain => $details->{'clonedom'},
  566:                datemode => $details->{'datemode'},
  567:                dateshift => $details->{'dateshift'},
  568:                tinyurls => $details->{'tinyurls'},
  569:                crsid => $details->{'optional_id'},
  570:                curruser => $details->{'owner'},
  571:                crssections => $sectionstr,
  572:                crsxlist => $xliststr,
  573:                autoadds => $details->{'adds'},
  574:                autodrops => $details->{'drops'},
  575:                notify => $details->{'notify_owner'},
  576:                notify_dc => $details->{'notify_dc'},
  577:                no_end_date => $noenddate,
  578:                showphotos => $details->{'showphotos'},
  579:                authtype => $details->{'authtype'},
  580:                autharg => $details->{'authparam'},
  581:                enrollstart => $details->{'enrollstart'},
  582:                enrollend => $details->{'enrollend'},
  583:                startaccess => $details->{'accessstart'},
  584:                endaccess => $details->{'accessend'},
  585:                setpolicy => $details->{'setpolicy'},
  586:                setcontent => $details->{'setcontent'},
  587:                setcomment => $details->{'setcomment'},
  588:                reshome => $reshome,
  589:                setkeys => $details->{'setkeys'},
  590:                keyauth => $details->{'keyauth'},
  591:                disresdis => $details->{'disresdis'},
  592:                disablechat => $details->{'disablechat'},
  593:                openall => $details->{'openall'},
  594:                openallfrom => $details->{'openallfrom'},
  595:                firstres => $firstres
  596:                };
  597:         if ($details->{'textbook'}) {
  598:             $courseargs->{'textbook'} = $details->{'textbook'};
  599:         }
  600:         my %host_servers = &Apache::lonnet::get_servers($cdom,'library');
  601:         if (! exists($host_servers{$details->{'coursehome'}})) {
  602:             $$logmsg .= &mt('Invalid home server for course').': '.$details->{'coursehome'};
  603:             return;
  604:         }
  605:         my ($success,$msg,$cloneinfo) = 
  606:             &Apache::loncommon::construct_course($courseargs,$logmsg,\$courseid,\$crsudom,\$crsunum,
  607:                                                  $udom,$uname,$context,$cnum,$category,$coderef,
  608:                                                  $callercontext,$user_lh);
  609: 	$$logmsg .= $msg;
  610:         if ((ref($clonemsg) eq 'ARRAY') && (ref($cloneinfo) eq 'ARRAY')) {
  611:              push(@{$clonemsg},@{$cloneinfo});
  612:         }
  613:         if (!$success) {
  614:             return;
  615:         }
  616:     } else {
  617:         return;
  618:     }
  619: 
  620: #
  621: # Make owner a coordinator
  622: #
  623:     if (($owner_domain) && ($owner_uname)) {
  624:         &Apache::lonnet::assignrole($owner_domain,$owner_uname,$courseid,$ccrole,'','','','',$context);
  625:     }
  626: 
  627: #
  628: # Process other reqested users
  629: #
  630: 
  631:     my @courseroles = qw(st ep ta in);
  632:     push(@courseroles,$ccrole);
  633:     if (&owner_is_dc($owner_uname,$owner_domain,$crsudom)) {
  634:         push(@courseroles,'ad');
  635:     }
  636:     my $stulogmsg = '';
  637:     foreach my $userkey (sort(keys(%{$details->{'users'}}))) {
  638:         my $url = '/'.$crsudom.'/'.$crsunum;
  639:         next if (ref($details->{'users'}{$userkey}{'roles'}) ne 'ARRAY');   
  640:         if (@{$details->{'users'}{$userkey}{'roles'}} > 0) {
  641:             my ($username,$userdom) = split/:/,$userkey;
  642:             if (&Apache::lonnet::homeserver($username,$userdom) eq 'no_host') { # Add user if no account
  643:                 my @reqroles = @{$details->{'users'}{$userkey}{'roles'}};
  644:                 my @badroles;
  645:                 my $firstrole = shift(@reqroles);
  646:                 while (@reqroles > 0) { 
  647:                     if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  648:                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  649:                             last;
  650:                         } else {
  651:                             push(@badroles,$firstrole);
  652:                             $firstrole = shift(@reqroles);
  653:                         }
  654:                     } elsif (grep(/^\Q$firstrole\E$/,@courseroles)) {
  655:                         last;
  656:                     } else {
  657:                         push(@badroles,$firstrole);
  658:                         $firstrole = shift(@reqroles);
  659:                     }
  660:                 }
  661:                 if (@badroles > 0) {
  662:                     if (@badroles > 1) {
  663:                         $$output .= &mt('The following requested roles are unavailable:').' '.join(', ',@badroles);
  664:                     } else {
  665:                         $$output .= &mt('The following requested role: [_1] is unavailable.',$badroles[0]); 
  666:                     }
  667:                 }
  668:                 my $firstsec;
  669:                 unless (($firstrole eq $ccrole) || ($firstrole eq ''))  {
  670:                     $firstsec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[0];
  671:                 }
  672:                 my $userargs = {
  673:                     'auth' => $details->{'users'}{$userkey}{'authtype'},
  674:                     'authparam' => $details->{'users'}{$userkey}{'autharg'},
  675:                     'emailenc' => $details->{'users'}{$userkey}{'emailenc'},
  676:                     'udom' => $userdom,
  677:                     'uname' => $username,
  678:                     'pid' => $details->{'users'}{$userkey}{'studentID'},
  679:                     'first' => $details->{'users'}{$userkey}{'firstname'},
  680:                     'middle' => $details->{'users'}{$userkey}{'middlename'},
  681:                     'last' => $details->{'users'}{$userkey}{'lastname'},
  682:                     'gene' => $details->{'users'}{$userkey}{'generation'},
  683:                     'usec' => $firstsec,
  684:                     'end' => $details->{'users'}{$userkey}{'end'},
  685:                     'start' => $details->{'users'}{$userkey}{'start'},
  686:                     'emailaddr' => $details->{'users'}{$userkey}{'emailaddr'},
  687:                     'cid' => $courseid,
  688:                     'crs' => $crsunum,
  689:                     'cdom' => $crsudom,
  690:                     'context' => 'createcourse',
  691:                     'linefeed' => $linefeed,
  692:                     'role' => $details->{'users'}{$userkey}{'roles'}[0],
  693:                    };
  694:                 if ($userargs->{'role'} eq 'st') {
  695:                     if (exists($details->{'users'}{$userkey}{'credits'})) {  
  696:                         $userargs->{'credits'} = $details->{'users'}{$userkey}{'credits'};
  697:                         $userargs->{'credits'} =~ s/[^\d\.]//g;
  698:                     }
  699:                 }
  700:                 $outcome = &LONCAPA::Enrollment::create_newuser($userargs,$logmsg,$newusermsg,$enrollcount,$addresult,$longroles,\%courseinfo,$context);
  701: # now add other roles and other sections.
  702:                 if ($outcome eq 'ok') {
  703:                     if ((($firstrole ne 'st') && ($firstrole ne $ccrole) && ($firstrole ne '')) && (@{$details->{'users'}{$userkey}{$firstrole}{'usec'}} > 1)) {
  704:                         for (my $i=1; $i<@{$details->{'users'}{$userkey}{$firstrole}{'usec'}}; $i++) {
  705:                             my $curr_role = $firstrole;
  706:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  707:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  708:                             my $usec = $details->{'users'}{$userkey}{$firstrole}{'usec'}[$i];
  709:                             $url = '/'.$crsudom.'/'.$crsunum;
  710:                             if ($usec ne '') {
  711:                                 $url .= '/'.$usec;
  712:                             }
  713:                             if ($firstrole =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  714:                                 $$output .= &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  715:                             } else {
  716:                                 $$output .= &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  717:                             }
  718:                         }
  719:                     }
  720:                     if (@reqroles > 0) {
  721:                         foreach my $curr_role (@reqroles) {
  722:                             my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  723:                             my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  724:                             if ($curr_role eq 'st') {
  725:                                 my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  726:                                 $url = '/'.$crsudom.'/'.$crsunum;
  727:                                 if ($usec ne '') {
  728:                                     $url .= '/'.$usec;
  729:                                 }
  730:                                 my $credits;
  731:                                 if (exists($details->{'users'}{$userkey}{'credits'})) {
  732:                                     $credits = $details->{'users'}{$userkey}{'credits'};
  733:                                     $credits =~ s/[^\d\.]//g;
  734:                                 }
  735:                                 $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
  736:                             } elsif ($curr_role eq $ccrole) {
  737:                                 $url = '/'.$crsudom.'/'.$crsunum;
  738:                                 my $usec = '';
  739:                                 $$output .=
  740:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  741:                             } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) || 
  742:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  743:                                 foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  744:                                     $url = '/'.$crsudom.'/'.$crsunum;
  745:                                     if ($usec ne '') {
  746:                                         $url .= '/'.$usec;
  747:                                     }
  748:                                     if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  749:                                         if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  750:                                             $$output .= 
  751:                                                 &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  752:                                         } else {
  753:                                             $$output = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  754:                                         }
  755:                                     } else {
  756:                                         $$output .= 
  757:                                             &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  758:                                     }
  759:                                 }
  760:                             } else {
  761:                                 $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  762:                             }
  763:                         }
  764:                     }
  765:                 }
  766:             } else {
  767:                 foreach my $curr_role (@{$details->{'users'}{$userkey}{'roles'}}) {
  768:                     my $start = $details->{'users'}{$userkey}{$curr_role}{'start'};
  769:                     my $end = $details->{'users'}{$userkey}{$curr_role}{'end'};
  770:                     if ($curr_role eq 'st') {
  771:                         my $usec = $details->{'users'}{$userkey}{$curr_role}{'usec'}[0];
  772:                         $url = '/'.$crsudom.'/'.$crsunum;
  773:                         if ($usec ne '') {
  774:                             $url .= '/'.$usec;
  775:                         }
  776:                         my $credits;
  777:                         if (exists($details->{'users'}{$userkey}{'credits'})) {
  778:                             $credits = $details->{'users'}{$userkey}{'credits'};
  779:                             $credits =~ s/[^\d\.]//g;
  780:                         }
  781:                         $$output .= &Apache::loncommon::commit_studentrole(\$stulogmsg,$userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context,$credits);
  782:                     } elsif ((grep(/^\Q$curr_role\E$/,@courseroles)) ||
  783:                                      ($curr_role =~ m{^cr/$match_domain/$match_username/[^/]+$})) {
  784:                         if (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}} > 0) {
  785:                             foreach my $usec (@{$details->{'users'}{$userkey}{$curr_role}{'usec'}}) {
  786:                                 $url = '/'.$crsudom.'/'.$crsunum;
  787:                                 if ($usec ne '') {
  788:                                     $url .= '/'.$usec;
  789:                                 }
  790:                                 my $stdresult;
  791:                                 if ($curr_role =~ m{/^cr/($match_domain)/($match_username)/([^/]+)$}) {
  792:                                     if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  793:                                         $stdresult = 
  794:                                             &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  795:                                     } else {
  796:                                         $stdresult = &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  797:                                     }
  798:                                 } else {
  799:                                     $stdresult = 
  800:                                         &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,$usec,$context);
  801:                                 }
  802:                                 $$output .= $stdresult;
  803:                             }
  804:                         } else {
  805:                             $url = '/'.$crsudom.'/'.$crsunum;
  806:                             if ($curr_role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
  807:                                 if (($1 eq $owner_domain) && ($2 eq $owner_uname)) {
  808:                                     $$output .= 
  809:                                         &Apache::loncommon::commit_customrole($userdom,$username,$url,$1,$2,$3,$start,$end,$context);
  810:                                 } else {
  811:                                     $$output .= &mt('Requested custom role: [_1] unavailable, as it was not defined by the course owner.',$curr_role);
  812:                                 }
  813:                             } else {
  814:                                 $$output .= 
  815:                                     &Apache::loncommon::commit_standardrole($userdom,$username,$url,$curr_role,$start,$end,$crsudom,$crsunum,'',$context);
  816:                             }
  817:                         }
  818:                     } else {
  819:                         $$output .= &mt('Requested role: [_1] is unavailable.',$curr_role);
  820:                     }
  821:                 }
  822:             }
  823:         }
  824:     }
  825: 
  826: # Information about keys.
  827:     if ($details->{'setkeys'}) {
  828:         $$keysmsg .=
  829:  '<a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">'.&mt('Manage Access Keys').'</a> for '.$details->{'title'}.$linefeed;
  830:     }
  831: # Flush the course logs so reverse user roles immediately updated
  832:     &Apache::lonnet::flushcourselogs();
  833:     return $courseid;
  834: }
  835: 
  836: sub owner_is_dc {
  837:     my ($owner_uname,$owner_dom,$cdom) = @_;
  838:     my $is_dc = 0;
  839:     my %roles = &Apache::lonnet::get_my_roles($owner_uname,$owner_dom,'userroles',
  840:                     ['active'],['dc'],[$cdom]);
  841:     if ($roles{$owner_uname.':'.$owner_dom.':dc'}) {
  842:         $is_dc = 1;
  843:     }
  844:     return $is_dc;
  845: }
  846: 
  847: #########################################################
  848: #
  849: # process_date()
  850: # 
  851: # input - date/time string in format YYYY:MM:DD:HH:MM:SS (:separators required).
  852: # output - corresponding UNIX time (seconds since epoch). 
  853: #
  854: #########################################################
  855: 
  856: sub process_date {
  857:     my $timestr = shift;
  858:     my $timestamp = '';
  859:     if ($timestr !~ /:/) {
  860:         $timestamp = '';
  861:     } else {
  862:         my @entries = split(/:/,$timestr);
  863:         for (my $j=0; $j<@entries; $j++) {
  864:             if ( length($entries[$j]) > 1 ) {
  865:                 $entries[$j] =~ s/^0//;
  866:             }
  867:             $entries[$j] =~ s/\D//g;
  868:             if ($entries[$j] < 0) {
  869:                 $entries[$j] = 0;
  870:             }
  871:         }
  872:         if ($entries[1] > 0) {  
  873:             $entries[1] = $entries[1] - 1;
  874:         }
  875:         if ($entries[5] > 60) {
  876:             $entries[5] = 60;
  877:         }
  878:         if ($entries[4] > 59) {
  879:             $entries[4] = 59;
  880:         }
  881:         if ($entries[3] > 23) {
  882:             $entries[3] = 23;
  883:         }
  884:         if ($entries[2] > 31) {
  885:             $entries[2] = 31;
  886:         }
  887:         if ($entries[1] > 11) {
  888:             $entries[1] = 11;
  889:         }
  890:         if ($entries[2] == 31) {
  891:             if (($entries[1] == 3) || ($entries[1] == 5) ||
  892:                 ($entries[1] == 8) || ($entries[1] == 10)) {
  893:                 $entries[2] = 30;
  894:             }
  895:         }
  896:         if ($entries[1] == 1) {
  897:             if ($entries[2] > 29) {
  898:                 $entries[2] = 29;
  899:             }
  900:             if ($entries[2] == 29) {
  901:                 if ($entries[0]%4 != 0) {
  902:                     $entries[2] = 28;
  903:                 } elsif ( $entries[0]%100 == 0
  904: 			  && $entries[0]%400 != 0) {
  905: 		    $entries[2] = 28;
  906: 		}
  907:             }
  908:         }     
  909:         $timestamp =  timelocal($entries[5],$entries[4],$entries[3],$entries[2],$entries[1],$entries[0]);
  910:     }
  911:     return $timestamp;
  912: }
  913: 
  914: 1;

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