[LON-CAPA-cvs] cvs: modules /msu coursecreator.pl

raeburn lon-capa-cvs@mail.lon-capa.org
Wed, 27 Apr 2005 21:36:57 -0000


This is a MIME encoded message

--raeburn1114637817
Content-Type: text/plain

raeburn		Wed Apr 27 17:36:57 2005 EDT

  Added files:                 
    /modules/msu	coursecreator.pl 
  Log:
  Script to run as cron on a library server to process MSU course requests.
  
  
--raeburn1114637817
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20050427173657.txt"


Index: modules/msu/coursecreator.pl
+++ modules/msu/coursecreator.pl
#!/usr/bin/perl
#############################
#
# coursecreator.pl
#
# Intended to run on a library server at MSU.
# Calls autocreation.pl script on zaphod.lite.msu.edu
# to fetch information about pending
# course requests for authorized course owners.
#
# Calls /home/httpd/perl/Autocreate.pl on the
# library server which will process pending
# course requests in
# /home/httpd/perl/tmp/addcourse/msu/auto/pending   
#
# To be run as a cron process owned by root or www. 
#
# Supply username and domain of a Domain Coordinator
# in the msu domain. 
#
# Sends confirmation e-mail to course owner(s) for all
# courses created.
#
# Logs to /home/httpd/perl/logs/creationrequests.log
#   
#############################

    use strict;
    use lib '/home/httpd/lib/perl';
    use LWP::UserAgent;
    use HTTP::Request::Common;
    use LONCAPA::Configuration;
    use Apache::lonnet;
                                                                                
    my $perlvarref = &LONCAPA::Configuration::read_conf('loncapa.conf');
    my $defdom = $$perlvarref{'DefDomain'}; 
    my $url = "http://zaphod.lite.msu.edu/cgi-bin/autocreation.pl";
    my $ua = LWP::UserAgent->new();
    my $req = HTTP::Request->new(GET => $url);
    open(FILE,"<$$perlvarref{'lonDaemons'}/scriptkey.dat");
    my $buffer = <FILE>;
    close(FILE);
    my ($admuser,$admpass) = split/:/,$buffer;
    $req->authorization_basic($admuser,$admpass);
    my $res = $ua->request($req);
    my %newcourses = ();
    my $newowners = 0;
    my %coursedata = ();
    my $dc_uname = 'raeburn';
    my $dc_dom = 'msu';
    my $logfile = $$perlvarref{'lonDaemons'}.'/logs/creationrequests.log';
    open (my $fh,">>$logfile");

    if ($res->is_success) {
        my $timestamp = time;
        my $path = "addcourse/$defdom/auto/pending";
        my $dirpath = "$$perlvarref{'lonDaemons'}/tmp";
        my @dirs = split/\//,$path;
        while (@dirs > 0) {
            $dirpath .= '/'.$dirs[0];
            if (!-e $dirpath) {
                mkdir($dirpath,0755);
            }
            shift @dirs;
        }
        open(FILE,">$dirpath/$timestamp");
        print FILE $res->content;
        unless ($res->content eq '') {
            open(PIPE, "$$perlvarref{'lonDaemons'}/Autocreate.pl $defdom $dc_uname:$dc_dom  |");
            my $courseid_str = <PIPE>;
            close(PIPE);
            
            $courseid_str =~ s/:$//;
            my @courses = split/:/,$courseid_str;
            foreach my $courseid (@courses) {
                my($cdom,$cnum) = ($courseid =~ /^([^_]+)_(\w+)$/);
                if ($cdom && $cnum) {
                    my %settings = &Apache::lonnet::dump('environment',$cdom,$cnum);
                    my $owner = $settings{'internal.owner'};
                    $coursedata{$courseid}{title} = $settings{'description'};
                    $coursedata{$courseid}{sections} = $settings{'internal.sections'};
                    $coursedata{$courseid}{coursecode} = $settings{'internal.coursecode'};
                    if (exists($newcourses{$owner})) {
                        push(@{$newcourses{$owner}},$courseid);
                    } else {
                        @{$newcourses{$owner}} = ($courseid);
                        $newowners ++;
                    }
                }
            }
        }
        if ($newowners > 0) {
            foreach my $owner (sort keys (%newcourses)) {
                &send_msg($defdom,$owner,\%coursedata,\%newcourses,$fh);
            }
        } 
    }
    close($fh);


sub send_msg {
    my ($defdom,$owner,$coursedata,$newcourses,$fh);
    my ($udom,$uname,$email);
    if ($owner =~ /:/) {
        ($uname,$udom) = ($owner =~ /^([^:]):([^:])$/);
    } else {
        $uname = $owner;
        $udom = $defdom;
    } 
    my %userinfo =  &Apache::lonnet::dump('environment',$udom,$uname);
    my $ownerlast = $userinfo{'lastname'};
    if (exists($userinfo{'permanentemail'})) {
        if ($userinfo{'permanentemail'} =~ /%40/) {
            $email = $userinfo{'permanentemail'};
        }
    }
    if ($email eq '') {
        if ($udom eq 'msu') {
            $email = $uname.'@'.$udom.'.edu';
        }
    }
    my $numcourses = @{$$newcourses{$owner}};
    my $output;
    my $courselist;
    if ($ownerlast) {
        $output = "Professor $ownerlast,\n\n";
     }
    if ($numcourses > 1) {
        $output .= "The following $numcourses new LON-CAPA courses have been created for you: \n";
    } else {
        $output .= "A new LON-CAPA course has been created for you: \n";
    } 
    foreach my $course (sort @{$$newcourses{$owner}}) {
       $courselist .= " $course";
       $output .= qq|
       Title: $coursedata{$course}{coursecode}
       Course Code: $coursedata{$course}{coursecode}
       Sections: $coursedata{$course}{coursecode}
|;
    }
    $output .= qq|
    Please log-in into LON-CAPA at http://s10.lite.msu.edu/ using your LON-CAPA
username ($uname) and password (usually your MSUNet password).  Your LON-CAPA 
domain is: $udom.  If you are already logged into the system, you will need 
to log-out and then log-in again for your new course role(s) to be displayed.

    A web page that explains how to navigate within a course is
available in the "Help for Students" area, accessible via a link from
the main MSU LON-CAPA gateway page - http://loncapa.msu.edu/

    This gateway page also contains a "Help for Faculty" link that provides
access to additional information about LON-CAPA. The gateway page 
includes a link to the main log-in page which students will use to
log-in to LON-CAPA. You can use this link, or if you prefer you can
log-in directly to http://s10.lite.msu.edu/ (a server that is used
exclusively by faculty). Please direct students to the gateway page:
http://loncapa.msu.edu/, or to the log-in page: http://msu.loncapa.org/,
and NOT to the s10 server).

    If you have only a single Course Coordinator role in LON-CAPA, when
you log-in you will be taken immediately to your course, otherwise a roles 
screen will be displayed where you will select the Course Coordinator 
role for the new course.
      
    When you first access your new course, you will be taken to an 
initialization helper where you can make changes or accept the default
settings for your course. Note there may a slight delay after you first
log-in, while LON-CAPA tries to open a separate browser window for
display of the "Remote Control" which is a control pad that contains a
number of buttons that can be used to access different types of
functionality in your course. If you have pop-up windows blocked in
your browser settings, launch of the remote control will fail after
several seconds, and you will proceed with an "inline" remote in the
existing browser window (see the Getting Started page for more
information - http://loncapa.msu.edu/student/getting_started.html).

    Once the first page of the course intialization helper has loaded you
will use the "Next" buttons on each page to move forward until you have
reached the last page of the Helper. Once you have clicked the "Finish"
button you will be taken to the "DOCS" page, which is your content
editing page for the course.

    Unless you cloned your new course from an existing course, the DOCS page 
will show a single resource (a syllabus template). You can remove this
item, if you do not want to use it, or you can add items to the template
by clicking on the Syllabus link and typing within the various text boxes.
You can also construct a folder hierarchy in your course by using the 
"New Folder" button to add new folders.  Documents can be imported from the LON-CAPA repository using buttons in the "Import a document" panel.

    Contextual help can be displayed by clicking a blue question mark icon, found in numerous places in LON-CAPA.  In addition, an orange question mark icon found on many pages provides access to a help/support area including access to an online support request form.    

    To display the course in student view (rather than in the "DOCS" content
editing view) click the NAV button (on the remote) or the "Navigate
Contents" link in the inline display to display the contents of the course.

    If you have any questions about LON-CAPA, please
submit an online help request, either from within the system, by clicking
the orange question mark, and clicking the "Ask helpdesk" link, or from
outside the system via the URL: http://loncapa.msu.edu/help

    Thank you for your interest in LON-CAPA.

MSU LON-CAPA support team
    |;
    if ($email =~ /^[^\@]+\@[^\@]+$/) {
        my $confirm_mail = "To: $email\n".
        "From: helpdesk\@loncapa.org\n".
        "Subject: New LON-CAPA course(s)\n".
        $output;
        if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
            print MAIL $confirm_mail;
            close(MAIL);
            print $fh "E-mail sent to $owner ($email) for $courselist\n\n";
        } else {
            print $fh "Could not send notification e-mail to $owner (email: $email) for $courselist\n\n";
        }
    } else {
            print $fh "Invalid email: $email.   Could not send notification e-mail to $owner for $courselist\n\n";
    }
}


--raeburn1114637817--