File:  [LON-CAPA] / loncom / interface / loncreatecourse.pm
Revision 1.36: download - view: text, annotated - select for diffs
Mon Sep 8 21:55:46 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Need to rewrite absolute URLs while cloning ... aargh.

    1: # The LearningOnline Network
    2: # Create a course
    3: #
    4: # $Id: loncreatecourse.pm,v 1.36 2003/09/08 21:55:46 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (My Desk
   29: #
   30: # (Internal Server Error Handler
   31: #
   32: # (Login Screen
   33: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
   34: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
   35: #
   36: # 3/1/1 Gerd Kortemeyer)
   37: #
   38: # 3/1 Gerd Kortemeyer)
   39: #
   40: # 2/14,2/16,2/17,7/6 Gerd Kortemeyer
   41: #
   42: package Apache::loncreatecourse;
   43: 
   44: use strict;
   45: use Apache::Constants qw(:common :http);
   46: use Apache::lonnet;
   47: use Apache::loncommon;
   48: use Apache::lonratedt;
   49: use Apache::londocs;
   50: 
   51: # -------------------------------------------- Return path to profile directory
   52: 
   53: sub propath {
   54:     my ($udom,$uname)=@_;
   55:     $udom=~s/\W//g;
   56:     $uname=~s/\W//g;
   57:     my $subdir=$uname.'__';
   58:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
   59:     my $proname="$Apache::lonnet::perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
   60:     return $proname;
   61: } 
   62: 
   63: # ================================================ Get course directory listing
   64: 
   65: sub crsdirlist {
   66:     my ($courseid,$which)=@_;
   67:     unless ($which) { $which=''; }
   68:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   69:     my @listing=&Apache::lonnet::dirlist
   70: 	($which,$crsdata{'domain'},$crsdata{'num'},
   71: 	 &propath($crsdata{'domain'},$crsdata{'num'}));
   72:     my @output=();
   73:     foreach (@listing) {
   74: 	unless ($_=~/^\./) {
   75: 	    push (@output,(split(/\&/,$_))[0]);
   76: 	}
   77:     }
   78:     return @output;
   79: }
   80: 
   81: # ============================================================= Read a userfile
   82: 
   83: sub readfile {
   84:     my ($courseid,$which)=@_;
   85:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   86:     return &Apache::lonnet::getfile('/uploaded/'.$crsdata{'domain'}.'/'.
   87: 				    $crsdata{'num'}.'/'.$which);
   88: }
   89: 
   90: # ============================================================ Write a userfile
   91: 
   92: sub writefile {
   93:     (my $courseid, my $which,$ENV{'form.output'})=@_;
   94:     my %crsdata=&Apache::lonnet::coursedescription($courseid);
   95:     return &Apache::lonnet::finishuserfileupload(
   96: 					  $crsdata{'num'},$crsdata{'domain'},
   97: 					  $crsdata{'home'},
   98: 					  'output',$which);
   99: }
  100: 
  101: # ===================================================================== Rewrite
  102: 
  103: sub rewritefile {
  104:     my ($contents,%rewritehash)=@_;
  105:     foreach (keys %rewritehash) {
  106: 	my $pattern=$_;
  107: 	$pattern=~s/(\W)/\\$1/gs;
  108: 	my $new=$rewritehash{$_};
  109: 	$contents=~s/$pattern/$new/gs;
  110:     }
  111:     return $contents;
  112: }
  113: 
  114: # ============================================================= Copy a userfile
  115: 
  116: sub copyfile {
  117:     my ($origcrsid,$newcrsid,$which)=@_;
  118:     unless ($which=~/\.sequence$/) {
  119: 	return &writefile($newcrsid,$which,
  120: 		      &readfile($origcrsid,$which));
  121:     } else {
  122: 	my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  123: 	my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  124: 	return &writefile($newcrsid,$which,
  125: 		 &rewritefile(
  126:                      &readfile($origcrsid,$which),
  127: 	    (
  128:        '/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'
  129:     => '/uploaded/'. $newcrsdata{'domain'}.'/'. $newcrsdata{'num'}.'/'
  130:             )));
  131:     }
  132: }
  133: 
  134: # =============================================================== Copy a dbfile
  135: 
  136: sub copydb {
  137:     my ($origcrsid,$newcrsid,$which)=@_;
  138:     $which=~s/\.db$//;
  139:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  140:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  141:     my %data=&Apache::lonnet::dump
  142: 	($which,$origcrsdata{'domain'},$origcrsdata{'num'});
  143:     return &Apache::lonnet::put
  144: 	($which,\%data,$newcrsdata{'domain'},$newcrsdata{'num'});
  145: }
  146: 
  147: # ========================================================== Copy resourcesdata
  148: 
  149: sub copyresourcedb {
  150:     my ($origcrsid,$newcrsid)=@_;
  151:     my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid);
  152:     my %newcrsdata= &Apache::lonnet::coursedescription($newcrsid);
  153:     my %data=&Apache::lonnet::dump
  154: 	('resourcedata',$origcrsdata{'domain'},$origcrsdata{'num'});
  155:     $origcrsid=~s/^\///;
  156:     $origcrsid=~s/\//\_/;
  157:     $newcrsid=~s/^\///;
  158:     $newcrsid=~s/\//\_/;
  159:     my %newdata=();
  160:     undef %newdata;
  161:     my $startdate=$data{$origcrsid.'.0.opendate'};
  162:     my $today=time;
  163:     my $delta=0;
  164:     if ($startdate) {
  165: 	my $oneday=60*60*24;
  166: 	$delta=$today-$startdate;
  167: 	$delta=int($delta/$oneday)*$oneday;
  168:     }
  169: # ugly retro fix for broken version of types
  170:     foreach (keys %data) {
  171: 	if ($_=~/\wtype$/) {
  172: 	    my $newkey=$_;
  173: 	    $newkey=~s/type$/\.type/;
  174: 	    $data{$newkey}=$data{$_};
  175: 	    delete $data{$_};
  176: 	}
  177:     }
  178: # adjust dates
  179:     foreach (keys %data) {
  180: 	my $thiskey=$_;
  181: 	$thiskey=~s/^$origcrsid/$newcrsid/;
  182: 	$newdata{$thiskey}=$data{$_};
  183: 	if ($data{$_.'.type'}=~/^date/) {
  184: 	    $newdata{$thiskey}=$newdata{$thiskey}+$delta;
  185: 	}
  186:     }
  187:     return &Apache::lonnet::put
  188: 	('resourcedata',\%newdata,$newcrsdata{'domain'},$newcrsdata{'num'});
  189: }
  190: 
  191: # ========================================================== Copy all userfiles
  192: 
  193: sub copyuserfiles {
  194:     my ($origcrsid,$newcrsid)=@_;
  195:     foreach (&crsdirlist($origcrsid,'userfiles')) {
  196: 	&copyfile($origcrsid,$newcrsid,$_);
  197:     }
  198: }
  199: # ========================================================== Copy all userfiles
  200: 
  201: sub copydbfiles {
  202:     my ($origcrsid,$newcrsid)=@_;
  203:     foreach (&crsdirlist($origcrsid)) {
  204: 	if ($_=~/\.db$/) {
  205: 	    unless 
  206:              ($_=~/^(nohist\_|discussiontimes|classlist|versionupdate|resourcedata)/) {
  207: 		 &copydb($origcrsid,$newcrsid,$_);
  208: 	     }
  209: 	}
  210:     }
  211: }
  212: 
  213: # ======================================================= Copy all course files
  214: 
  215: sub copycoursefiles {
  216:     my ($origcrsid,$newcrsid)=@_;
  217:     &copyuserfiles($origcrsid,$newcrsid);
  218:     &copydbfiles($origcrsid,$newcrsid);
  219:     &copyresourcedb($origcrsid,$newcrsid);
  220: }
  221: 
  222: # ===================================================== Phase one: fill-in form
  223: 
  224: sub print_course_creation_page {
  225:     my $r=shift;
  226:     my $defdom=$ENV{'request.role.domain'};
  227:     my %host_servers = &Apache::loncommon::get_library_servers($defdom);
  228:     my $course_home = '<select name="course_home" size="1">'."\n";
  229:     foreach my $server (sort(keys(%host_servers))) {
  230:         $course_home .= qq{<option value="$server"};
  231:         if ($server eq $Apache::lonnet::perlvar{'lonHostID'}) {
  232:             $course_home .= " selected ";
  233:         }
  234:         $course_home .= qq{>$server $host_servers{$server}</option>};
  235:     }
  236:     $course_home .= "\n</select>\n";
  237:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
  238:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
  239:     my $helplink=&Apache::loncommon::help_open_topic('Create_Course','Help on Creating Courses');
  240:     my $cloneform=&Apache::loncommon::select_dom_form
  241: 	($ENV{'request.role.domain'},'clonedomain').
  242: 		     &Apache::loncommon::selectcourse_link
  243: 		     ('ccrs','clonecourse','clonedomain');
  244:     my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript();
  245:     $r->print(<<ENDDOCUMENT);
  246: <html>
  247: <script language="JavaScript" type="text/javascript">
  248: var editbrowser = null;
  249: function openbrowser(formname,elementname) {
  250:     var url = '/res/?';
  251:     if (editbrowser == null) {
  252:         url += 'launch=1&';
  253:     }
  254:     url += 'catalogmode=interactive&';
  255:     url += 'mode=edit&';
  256:     url += 'form=' + formname + '&';
  257:     url += 'element=' + elementname + '&';
  258:     url += 'only=sequence' + '';
  259:     var title = 'Browser';
  260:     var options = 'scrollbars=1,resizable=1,menubar=0';
  261:     options += ',width=700,height=600';
  262:     editbrowser = open(url,title,options,'1');
  263:     editbrowser.focus();
  264: }
  265: </script>
  266: $coursebrowserjs
  267: <head>
  268: <title>The LearningOnline Network with CAPA</title>
  269: </head>
  270: $bodytag
  271: $helplink
  272: <form action="/adm/createcourse" method="post" name="ccrs">
  273: <h2>Course Information</h2>
  274: <p>
  275: <b>Course Title:</b>
  276: <input type="text" size="50" name="title">
  277: </p><p>
  278: <b>Course Home Server:</b>$course_home
  279: </p><p>
  280: <b>Course ID/Number (optional)</b>
  281: <input type="text" size="30" name="crsid">
  282: </p>
  283: <h2>Course Content</h2>
  284: <table border="2">
  285: <tr><th>Completely new course</th><th>Clone an existing course</th></tr>
  286: <tr><td>
  287: <p>
  288: <b>Map:</b>
  289: <input type="text" size="50" name="topmap">
  290: <a href="javascript:openbrowser('ccrs','topmap')">Select Map</a>
  291: </p><p>
  292: <b>Do NOT generate as standard course</b><br /> 
  293: (only check if you know what you are doing):
  294: <input type="checkbox" name="nonstandard">
  295: </p>
  296: <p>
  297: <b>First Resource</b><br />(standard courses only):
  298: <input type="radio" name="firstres" value="blank">Blank
  299: &nbsp;
  300: <input type="radio" name="firstres" value="syl" checked>Syllabus
  301: &nbsp;
  302: <input type="radio" name="firstres" value="nav">Navigate
  303: </p>
  304: </td><td>
  305: Course ID: <input input type="text" size="25" name="clonecourse" value="" />
  306: <br />
  307: Domain: 
  308: $cloneform<br />&nbsp;<br />
  309: Additional settings, if specified below, will override cloned settings.
  310: </td></tr>
  311: </table>
  312: <h2>Assessment Parameters</h2>
  313: <p>
  314: <b>Open all assessments: </b>
  315: <input type="checkbox" name="openall" checked>
  316: </p>
  317: <h2>Messaging</h2>
  318: <p>
  319: <b>Set course policy feedback to Course Coordinator: </b>
  320: <input type="checkbox" name="setpolicy" checked>
  321: </p><p>
  322: <b>Set content feedback to Course Coordinator: </b>
  323: <input type="checkbox" name="setcontent" checked>
  324: </p>
  325: <h2>Communication</h2>
  326: <p>
  327: <b>Disable student resource discussion: </b>
  328: <input type="checkbox" name="disresdis" /> <br />
  329: <b>Disable student use of chatrooms: </b>
  330: <input type="checkbox" name="disablechat" />
  331: </p>
  332: <h2>Access Control</h2>
  333: <p>
  334: <b>Students need access key to enter course: </b>
  335: <input type="checkbox" name="setkeys" />
  336: </p>
  337: <h2>Course Coordinator</h2>
  338: <p>
  339: <b>Username:</b> <input type="text" size="15" name="ccuname" />
  340: </p><p>
  341: <b>Domain:</b> $domform
  342: </p><p>
  343: <b>Immediately expire own role as Course Coordinator:</b>
  344: <input type="checkbox" name="expireown" checked>
  345: </p><p>
  346: <input type="hidden" name="phase" value="two" />
  347: <input type="submit" value="Open Course">
  348: </p>
  349: </form>
  350: </body>
  351: </html>
  352: ENDDOCUMENT
  353: }
  354: 
  355: # ====================================================== Phase two: make course
  356: 
  357: sub create_course {
  358:     my $r=shift;
  359:     my $topurl='/res/'.&Apache::lonnet::declutter($ENV{'form.topmap'});
  360:     my $ccuname=$ENV{'form.ccuname'};
  361:     my $ccdomain=$ENV{'form.ccdomain'};
  362:     $ccuname=~s/\W//g;
  363:     $ccdomain=~s/\W//g;
  364:     my $cdescr=$ENV{'form.title'};
  365:     my $curl=$ENV{'form.topmap'};
  366:     my $bodytag=&Apache::loncommon::bodytag('Create a New Course');
  367:     $r->print(<<ENDENHEAD);
  368: <html>
  369: <head>
  370: <title>The LearningOnline Network with CAPA</title>
  371: </head>
  372: $bodytag
  373: ENDENHEAD
  374:     #
  375:     # Verify data
  376:     #
  377:     # Check the veracity of the course coordinator
  378:     if (&Apache::lonnet::homeserver($ccuname,$ccdomain) eq 'no_host') {
  379:         $r->print('No such user '.$ccuname.' at '.$ccdomain.'</body></html>');
  380: 	return;
  381:     }
  382:     # Check the proposed home server for the course
  383:     my %host_servers = &Apache::loncommon::get_library_servers
  384:         ($ENV{'request.role.domain'});
  385:     if (! exists($host_servers{$ENV{'form.course_home'}})) {
  386:         $r->print('Invalid home server for course: '.
  387:                   $ENV{'form.course_home'}.'</body></html>');
  388:         return;
  389:     }
  390: #
  391: # Open course
  392: #
  393:     my %cenv=();
  394:     my $courseid=&Apache::lonnet::createcourse($ENV{'request.role.domain'},
  395:                                                $cdescr,$curl,
  396:                                                $ENV{'form.course_home'},
  397:                                                $ENV{'form.nonstandard'});
  398: 
  399:     # Note: The testing routines depend on this being output; see 
  400:     # Utils::Course. This needs to at least be output as a comment
  401:     # if anyone ever decides to not show this, and Utils::Course::new
  402:     # will need to be suitably modified.
  403:     $r->print('New LON-CAPA Course ID: '.$courseid.'<br>');
  404: #
  405: # Check if created correctly
  406: #
  407:     my ($crsudom,$crsunum)=($courseid=~/^\/(\w+)\/(\w+)$/);
  408:     my $crsuhome=&Apache::lonnet::homeserver($crsunum,$crsudom);
  409:     $r->print('Created on: '.$crsuhome.'<br>');
  410: #
  411: # Are we cloning?
  412: #
  413:     my $cloneid='';
  414:     if (($ENV{'form.clonecourse'}) && ($ENV{'form.clonedomain'})) {
  415: 	$cloneid='/'.$ENV{'form.clonedomain'}.'/'.$ENV{'form.clonecourse'};
  416:         my ($clonecrsudom,$clonecrsunum)=($cloneid=~/^\/(\w+)\/(\w+)$/);
  417: 	my $clonehome=&Apache::lonnet::homeserver($clonecrsunum,$clonecrsudom);
  418: 	if ($clonehome eq 'no_host') {
  419: 	    $r->print(
  420:     '<br /><font color="red">Attempting to clone non-existing course '.$cloneid.'</font>');
  421: 	} else {
  422: 	    $r->print(
  423:     '<br /><font color="green">Cloning course from '.$clonehome.'</font>');
  424: # Copy all files
  425: 	    &copycoursefiles($cloneid,$courseid);
  426: # Restore title
  427: 	    $cenv{'description'}=$cdescr;
  428: 	    $cenv{'clonedfrom'}=$cloneid;
  429: 	}
  430:     }
  431: #
  432: # Set environment (will override cloned, if existing)
  433: #
  434:     if ($ENV{'form.crsid'}) {
  435:         $cenv{'courseid'}=$ENV{'form.crsid'};
  436:     }
  437:     if (($ccdomain) && ($ccuname)) {
  438:        if ($ENV{'form.setpolicy'}) {
  439:            $cenv{'policy.email'}=$ccuname.':'.$ccdomain;
  440:        }
  441:        if ($ENV{'form.setcontent'}) {
  442:            $cenv{'question.email'}=$ccuname.':'.$ccdomain;
  443:        }
  444:     }
  445:     if ($ENV{'form.setkeys'}) {
  446:        $cenv{'keyaccess'}='yes';
  447:     }
  448:     if ($ENV{'form.disresdis'}) {
  449:         $cenv{'pch.roles.denied'}='st';
  450:     }
  451:     if ($ENV{'form.disablechat'}) {
  452:         $cenv{'plc.roles.denied'}='st';
  453:     }
  454: 
  455:     # Record we've not yet viewed the Course Initialization Helper for this 
  456:     # course
  457:     $cenv{'course.helper.not.run'} = 1;
  458:     #
  459:     # Use new Randomseed
  460:     #
  461:     $cenv{'rndseed'}=&Apache::lonnet::latest_rnd_algorithm_id();;
  462:     #
  463:     # By default, use standard grading
  464:     $cenv{'grading'} = 'standard';
  465: 
  466:     $r->print('<br />Setting environment: '.                 
  467:           &Apache::lonnet::put('environment',\%cenv,$crsudom,$crsunum).'<br>');
  468: #
  469: # Open all assignments
  470: #
  471:     if ($ENV{'form.openall'}) {
  472:        my $storeunder=$crsudom.'_'.$crsunum.'.0.opendate';
  473:        my %storecontent = ($storeunder         => time,
  474:                            $storeunder.'.type' => 'date_start');
  475:        
  476:        $r->print('Opening all assignments: '.&Apache::lonnet::cput
  477:                  ('resourcedata',\%storecontent,$crsudom,$crsunum).'<br>');
  478:    }
  479: #
  480: # Set first page
  481: #
  482:     unless (($ENV{'form.nonstandard'}) || ($ENV{'form.firstres'} eq 'blank')) {
  483: 	$r->print('Setting first resource: ');
  484:         my ($errtext,$fatal)=
  485:            &Apache::londocs::mapread($crsunum,$crsudom,'default.sequence');
  486:         $r->print(($fatal?$errtext:'read ok').' - ');
  487:         my $title; my $url;
  488:         if ($ENV{'form.firstres'} eq 'syl') {
  489: 	    $title='Syllabus';
  490:             $url='/public/'.$crsudom.'/'.$crsunum.'/syllabus';
  491:         } else {
  492:             $title='Navigate Contents';
  493:             $url='/adm/navmaps';
  494:         }
  495:         $Apache::lonratedt::resources[1]=$title.':'.$url.':false:start:res';
  496:         ($errtext,$fatal)=
  497:            &Apache::londocs::storemap($crsunum,$crsudom,'default.sequence');
  498:         $r->print(($fatal?$errtext:'write ok').'<br>');
  499:   }
  500: #
  501: # Make current user course adminstrator
  502: #
  503:     my $end=undef;
  504:     my $addition='';
  505:     if ($ENV{'form.expireown'}) { $end=time+5; $addition='expired'; }
  506:     $r->print('Assigning '.$addition.' role of course coordinator to self: '.
  507:     &Apache::lonnet::assignrole(
  508:      $ENV{'user.domain'},$ENV{'user.name'},$courseid,'cc',$end).'<br>');
  509: #
  510: # Make additional user course administrator
  511: #
  512:    if (($ccdomain) && ($ccuname)) {
  513:     $r->print('Assigning role of course coordinator to '.
  514:                $ccuname.' at '.$ccdomain.': '.
  515:     &Apache::lonnet::assignrole($ccdomain,$ccuname,$courseid,'cc').'<p>');
  516:    }
  517:     if ($ENV{'form.setkeys'}) {
  518: 	$r->print(
  519:  '<p><a href="/adm/managekeys?cid='.$crsudom.'_'.$crsunum.'">Manage Access Keys</a></p>');
  520:     }
  521:     $r->print('<p>Roles will be active at next login.</p></body></html>');
  522: }
  523: 
  524: # ===================================================================== Handler
  525: sub handler {
  526:     my $r = shift;
  527: 
  528:     if ($r->header_only) {
  529:        $r->content_type('text/html');
  530:        $r->send_http_header;
  531:        return OK;
  532:     }
  533: 
  534:     if (&Apache::lonnet::allowed('ccc',$ENV{'request.role.domain'})) {
  535:        $r->content_type('text/html');
  536:        $r->send_http_header;
  537: 
  538:        if ($ENV{'form.phase'} eq 'two') {
  539:            &create_course($r);
  540:        } else {
  541: 	   &print_course_creation_page($r);
  542:        }
  543:    } else {
  544:       $ENV{'user.error.msg'}=
  545:         "/adm/createcourse:ccc:0:0:Cannot create courses";
  546:       return HTTP_NOT_ACCEPTABLE; 
  547:    }
  548:    return OK;
  549: } 
  550: 
  551: 1;
  552: __END__

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