[LON-CAPA-cvs] cvs: loncom /interface lonrequestcourse.pm

raeburn raeburn@source.lon-capa.org
Mon, 27 Jul 2009 22:58:24 -0000


This is a MIME encoded message

--raeburn1248735504
Content-Type: text/plain

raeburn		Mon Jul 27 22:58:24 2009 EDT

  Added files:                 
    /loncom/interface	lonrequestcourse.pm 
  Log:
  - Ability to make course requests.
  - Saving my work.
  
  
--raeburn1248735504
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20090727225824.txt"


Index: loncom/interface/lonrequestcourse.pm
+++ loncom/interface/lonrequestcourse.pm
# The LearningOnline Network
# Request a course
#
# $Id: lonrequestcourse.pm,v 1.1 2009/07/27 22:58:24 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
###

=head1 NAME

Apache::lonrequestcourse.pm

=head1 SYNOPSIS

Allows users to request creation of new courses.

This is part of the LearningOnline Network with CAPA project
described at http://www.lon-capa.org.

=head1 SUBROUTINES

=over

=item handler()

=back

=cut

package Apache::lonrequestcourse;

use strict;
use Apache::Constants qw(:common :http);
use Apache::lonnet;
use Apache::loncommon;
use Apache::lonlocal;
use LONCAPA;

sub handler {
    my ($r) = @_;
    if ($r->header_only) {
        &Apache::loncommon::content_type($r,'text/html');
        $r->send_http_header;
        return OK;
    }
    &Apache::loncommon::content_type($r,'text/html');
    $r->send_http_header;

    my $action = $env{'form.action'};
    my $state = $env{'form.state'};
    my $dom = &get_course_dom();
    my %can_request;
    my $canreq = &check_can_request($dom,\%can_request);
    if ($action eq 'new') {
        if ($canreq) {
            if ($state eq 'crstype') {
                &print_main_menu($r,\%can_request,$dom);
            } else {
                &request_administration($r,$action,$state,$dom);
            }
        } else {
            $r->print(&Apache::loncommon::start_page('Course Requests').
                      '<div class="LC_warning">'.
                      &mt('You do not have privileges to request creation of courses.').
                      '</div>'.
                      &Apache::loncommon::end_page());
        }
    } elsif ($action eq 'view') {
        &print_request_status();
    } elsif ($action eq 'log') {
        &print_request_logs();
    } else {
        &print_main_menu($r,\%can_request,$dom);
    }
    return OK;
}

sub check_can_request {
    my ($dom,$can_request) = @_;
    my $canreq = 0;
    if (ref($can_request) eq 'HASH') {
        foreach my $type ('official','unofficial','community') {
            if (&Apache::lonnet::usertools_access($env{'user.name'},
                                                  $env{'user.domain'},
                                                  $type,undef,'requestcourses')) {
                $canreq ++;
                if ($dom eq $env{'user.domain'}) {
                    $can_request->{$type} = 1;
                }
            }
            if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                if (@curr > 0) {
                    $canreq ++;
                    unless ($dom eq $env{'user.domain'}) {
                        if (grep(/^\Q$dom\E$/,@curr)) {
                            $can_request->{$type} = 1;
                        }
                    }
                }
            }
        }
    }
    return $canreq;
}

sub print_main_menu {
    my ($r,$can_request,$dom) = @_;
    my $onchange;
    unless ($env{'form.interface'} eq 'textual') {
        $onchange = 1;
    }

    &Apache::lonhtmlcommon::clear_breadcrumbs();

    my $js = <<END;
<script type="text/javascript">

function nextPage(formname,nextstate) {
    if (check_can_request() == true) {
        formname.state.value= nextstate;
        formname.submit();
    }
    return;
}

function backPage(formname,prevstate) {
    formname.state.value = prevstate;
    formname.submit();
}

function check_can_request() {
    var official = '';
    var unofficial = '';
    var community = '';    
END

    foreach my $item (keys(%{$can_request})) {
            $js .= " 
        $item = 1;
";
    }
    my %lt = &Apache::lonlocal::texthash(
        official => 'You are not permitted to request creation of an official course in this domain.',
        unofficial => 'You are not permitted to request creation of an unofficial course in this domain.',
        community => 'You are not permitted to request creation of a community this domain.',
        all => 'You must choose a specific course type when making a new course request.\\nAll types is not allowed.',
    ); 
    $js .= <<END;
    var crschoice = document.requestform.crstype.value;
    if (crschoice == 'official') {
        if (official != 1) {
            alert("$lt{'official'}");
            return false;
        }
    } else {
        if (crschoice == 'unofficial') {
            if (unofficial != 1) {
                alert("$lt{'unofficial'}");
                return false;
            }
        } else {
            if (crschoice == 'community') {
                if (community != 1) {
                    alert("$lt{'community'}");
                    return false;
                }
            } else {
                var actionchoice = document.requestform.action.value;     
                if (actionchoice == 'new') {
                    alert("$lt{'all'}");
                    return false;
                }               
            }
        }
    }
    return true;
}
</script>

END

    my $formname = 'requestform';
    $r->print(&Apache::loncommon::start_page('Course Requests',$js).
              '<p><div>'.
              '<form name="domforcourse" method="post" action="/adm/requestcourse">'.
              &Apache::lonhtmlcommon::start_pick_box().
              &Apache::lonhtmlcommon::row_title('Domain').
              &Apache::loncommon::select_dom_form($dom,'showdom','',1,$onchange));
    if (!$onchange) {
        $r->print('&nbsp;<input type="submit" name="godom" value="'.
                   &mt('Change').'" />');
    }
    $r->print(&Apache::lonhtmlcommon::row_closure(1).
              &Apache::lonhtmlcommon::end_pick_box().'</form></div>');

    my $formname = 'requestform';
    my $next = 'codepick';
    my $nexttext = &mt('Next');
    $r->print('<div><form name="'.$formname.'" method="post" action="/adm/requestcourse">'.
              &Apache::lonhtmlcommon::start_pick_box().
              &Apache::lonhtmlcommon::row_title('Action').'
<input type="hidden" name="showdom" value="'.$dom.'" />
<select size="1" name="action" >
 <option value="new">'.&mt('New course request').'</option>
 <option value="view">'.&mt('View/Modify/Cancel pending requests').'</option>
 <option value="log">'.&mt('View request history').'</option>
</select>'.
              &Apache::lonhtmlcommon::row_closure().
              &Apache::lonhtmlcommon::row_title('Course Type').'
<select size="1" name="crstype">
 <option value="any">'.&mt('All types').'</option>
 <option value="official">'.&mt('Official course').'</option>
 <option value="unofficial">'.&mt('Unofficial course').'</option>
 <option value="community">'.&mt('Community').'</option>
</select>
<input type="hidden" name="state" value="crstype" />'.
              &Apache::lonhtmlcommon::row_closure(1).
              &Apache::lonhtmlcommon::end_pick_box().'<br />
<input type="button" name="next" value="'.$nexttext.'" onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />
</form></div>');
    $r->print(&Apache::loncommon::end_page());
    return;
}

sub request_administration {
    my ($r,$action,$state,$dom) = @_;
    if ($action eq 'new') {
        my $js =  <<END;
<script type="text/javascript">

function nextPage(formname,nextstate) {
    formname.state.value= nextstate;
    formname.submit();
}
function backPage(formname,prevstate) {
    formname.state.value = prevstate;
    formname.submit();
}

</script>

END

        unless (($state eq 'review') || ($state eq 'process')) {
            $js .= "\n".&Apache::loncommon::coursebrowser_javascript($dom);
        }
        my $start_page =
            &Apache::loncommon::start_page('Request a course',$js);
        $r->print($start_page);
        if ($state eq 'review') {
            &print_review($r,$state,$dom);
        } elsif ($state eq 'process') {
            &print_request_outcome($r,$state,$dom);
        } else {
            &print_request_form($r,$state,$dom);
        }
        $r->print(&Apache::loncommon::end_page());
    } elsif ($action eq 'log') {
        $r->print(&coursereq_log());       
    }
    return;
}

sub print_request_form {
    my ($r,$state,$dom) = @_;
    my $formname = 'requestcrs';
    my ($next,$prev,$message,$output,$codepicker);
    my $prev = 'crstype';
    $r->print('<form name="'.$formname.'" method="post" action="/adm/requestcourse">');
    my $crstype = $env{'form.crstype'};
    my $xlist = 0;
    if ($crstype eq 'official') {
        my (@codetitles,%cat_titles,%cat_order,@code_order);
        &Apache::lonnet::auto_possible_instcodes($dom,\@codetitles,\%cat_titles,
                                                 \%cat_order,\@code_order);
        if ($state eq 'courseinfo') {
            my $instcode;
            if (@code_order > 0) {
                foreach my $item (@code_order) {
                    $instcode .= $env{'form.instcode_'.$item};  
                }
            }
            if ($instcode ne '') {
                my $code_chk = &Apache::lonnet::auto_validate_instcode('',$dom,$instcode);
                if ($code_chk eq 'ok') {
                    $message = '<div class="LC_info">'.
                               &mt('The chosen course category [_1] is valid.','<b>'.
                               $instcode.'</b>').'</div>';  
                    my @sections = &Apache::lonnet::auto_get_sections(undef,$dom,
                                                                      $instcode);
                    if (@sections) {
                        $output .= &inst_section_selector(\@sections);
                    }
                    $output .= &coursecode_form($dom,'crosslist',\@codetitles,
                                                \%cat_titles,\%cat_order,$xlist);
                    $prev = 'codepick';
                } else {
                    $message = '<div class="LC_warning">'.
                               &mt('No course was found matching your choice of institutional course category.');
                    if ($code_chk ne '') {
                        $message .= '<br />'.$code_chk;
                    }
                    $message .= '</div>';
                }
            }
        }
        unless ($prev eq 'codepick') {
            $codepicker = &coursecode_form($dom,'instcode',\@codetitles,
                                           \%cat_titles,\%cat_order);
        }
    }
    $r->print($message.'<div>'.&Apache::lonhtmlcommon::start_pick_box());
    if ($output ne '') {
        $r->print($output);
    }
    if ($codepicker) {
        $r->print($codepicker);
        $state = 'codepick';
        $next = 'courseinfo';
    } else {
        $r->print(&courseinfo_form());
        $state = 'courseinfo';
        $next = 'review';
    }
    $r->print(&Apache::lonhtmlcommon::end_pick_box().'</div>'."\n");
    unless ($codepicker) {
        $r->print('<div>'.&clone_form($dom,$formname,$crstype).'</div>'."\n");
    }
    $r->print('<input type="hidden" name="state" value="'.$state.'" />'.
              &Apache::lonhtmlcommon::echo_form_input(['state']).'</form>');
    &display_navbuttons($r,$formname,$prev,'Previous',$next,'Next');
    return;
}

sub inst_section_selector {
    my ($sections,$selected) = @_;
    my $output;
    if (ref($sections) eq 'ARRAY') {
        $output = &Apache::lonhtmlcommon::row_title('Sections').
                  &Apache::loncommon::start_data_table().
                  &Apache::loncommon::start_data_table_row().
                  '<th>'.&mt('Institutional Section').'</th>'.
                  '<th>'.&mt('LON-CAPA section ID').'</th>'.
                  '<th>'.&mt('Include in course?').'</th>'.
                  &Apache::loncommon::end_data_table_row();
        for (my $i=0; $i<@{$sections}; $i++) {
            my $colflag = $i%2;
            $output .= &Apache::loncommon::start_data_table_row().
                       '<td>'.$sections->[$i].
                       '<input type="hidden" name="secnum_'.$i.'" value="'.
                       $sections->[$i].'" /></td>'.
                       '<td><input type="text" size="10" name="loncapasec_'.$i.
                       '" value="'.$sections->[$i].'" /></td>'.
                       '<td><input type="checkbox" name="sec_'.$i. 
                       '" checked="checked" /></td>'.
                       &Apache::loncommon::end_data_table_row();
        }
        $output .= &Apache::loncommon::end_data_table();
    }
    return $output;
}

sub print_request_status {
    return;
}

sub print_request_logs {
    return;
}

sub print_review {
    my ($r,$state,$dom) = @_;
    return;
}

sub courseinfo_form {
    my $output = &Apache::lonhtmlcommon::row_title('Course Description').
                 '<input type="text" size="40" name="description" />';
    return $output;
}

sub clone_form {
    my ($dom,$formname,$crstype) = @_;
    my $type = 'Course';
    if ($crstype eq 'community') {
        $type = 'Community';
    }
    my $cloneform = &Apache::loncommon::select_dom_form($dom,'clonedomain').
                    &Apache::loncommon::selectcourse_link($formname,'clonecourse',
                                                         'clonedomain','','','',$type);
    my %lt = &Apache::lonlocal::texthash(
                    'cid'  => 'Course ID',
                    'dmn'  => 'Domain',
                    'dsh'  => 'Date Shift',
                    'ncd'  => 'Do not clone date parameters',
                    'prd'  => 'Clone date parameters as-is',
                    'shd'  => 'Shift date parameters by number of days',
    );
    my $output .= &Apache::lonhtmlcommon::start_pick_box(). 
                  &Apache::lonhtmlcommon::row_title($lt{'cid'}).
                  '<label><input type="text" size="25" name="clonecourse" value="" />'.
                  '</label>'.&Apache::lonhtmlcommon::row_closure(1).'<label>'.
                  &Apache::lonhtmlcommon::row_title($lt{'dmn'}).'</label>'.
                  $cloneform.'</label>'.&Apache::lonhtmlcommon::row_closure().
                  &Apache::lonhtmlcommon::row_title($lt{'dsh'}).'<label>'.
                  '<input type="radio" name="datemode" value="delete" /> '.$lt{'ncd'}.
                  '</label><br /><label>'.
                  '<input type="radio" name="datemode" value="preserve" /> '.$lt{'prd'}.
                  '</label><br /><label>'.
                  '<input type="radio" name="datemode" value="shift" checked="checked" /> '.
                  $lt{'shd'}.'</label>'.
                  '<input type="text" size="5" name="dateshift" value="365" />'.
                  &Apache::lonhtmlcommon::row_closure(1).
                  &Apache::lonhtmlcommon::end_pick_box();
    return $output;
}

sub coursecode_form {
    my ($dom,$context,$codetitles,$cat_titles,$cat_order,$count) = @_;
    my $output;
    if ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
        (ref($cat_order))) {
        if (@{$codetitles} > 0) {
            my $lastitem = pop(@{$codetitles});
            my $sel = $context;
            if ($context eq 'crosslist') {
                $sel .= '_'.$count;
            }
            my $lastinput = '<input type="text" size="5" name="'.$sel.'_'.                                    $lastitem.'" />';
            my %rowtitle = (
                             instcode  => 'Course Category',
                             crosslist => 'Cross Listed Course',
                           );
            if (@{$codetitles} > 0) {
                my $sel = $context;
                if ($context eq 'crosslist') {
                    $sel .= '_'.$count;
                }
                $output = &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                          '<table><tr>';
                foreach my $title (@{$codetitles}) {
                    if (ref($cat_order->{$title}) eq 'ARRAY') {
                        if (@{$cat_order->{$title}} > 0) {
                            $output .= '<td align="center">'.$title.'<br />'."\n".
                                       '<select name="'.$sel.'_'.$title.'">'."\n".
                                       ' <option value="" selected="selected">'.
                                       &mt('Select').'</option>'."\n";
                            foreach my $item (@{$cat_order->{$title}}) {
                                my $longitem = $item;
                                if (ref($cat_titles->{$title}) eq 'HASH') {
                                    if ($cat_titles->{$title}{$item} ne '') {
                                        $longitem = $cat_titles->{$title}{$item};
                                    }
                                }
                                $output .= '<option value="'.$item.'">'.$longitem.
                                           '</option>'."\n";
                            }
                        }
                        $output .= '</select></td>'."\n";
                    }
                }
                if ($context eq 'crosslist') {
                    $output .= '<td align="center">'.$lastitem.'<br />'."\n".
                               $lastinput.'</td></tr></table>';
                } else {
                    $output .= '</tr></table>'.
                               &Apache::lonhtmlcommon::row_closure().
                               &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                               $lastinput;
                }
            } else {
                if ($context eq 'crosslist') {
                    $output .= &Apache::lonhtmlcommon::row_title($rowtitle{$context}).
                               $lastitem.'<br />';
                } else { 
                    $output .= &Apache::lonhtmlcommon::row_title('Course '.$lastitem).
                               $lastinput;
                }
                $output .=  $lastinput.&Apache::lonhtmlcommon::row_closure(1);
            }
        }
    }
    return $output;
}

sub get_course_dom {
    my $codedom = &Apache::lonnet::default_login_domain();
    if (($env{'user.domain'} ne '') && ($env{'user.domain'} ne 'public')) {
        $codedom = $env{'user.domain'};
        if ($env{'request.role.domain'} ne '') {
            $codedom = $env{'request.role.domain'};
        }
    }
    if ($env{'form.showdom'} ne '') {
        if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
            $codedom = $env{'form.showdom'};
        }
    }
    return $codedom;
}

sub display_navbuttons {
    my ($r,$formname,$prev,$prevtext,$next,$nexttext) = @_;
    $r->print('<div class="LC_navbuttons">');
    if ($prev) {
        $r->print('
      <input type="button" name="previous" value = "'.$prevtext.'"
    onclick="javascript:backPage(document.'.$formname.','."'".$prev."'".')"/>
   &nbsp;&nbsp;&nbsp;');
    } elsif ($prevtext) {
        $r->print('
      <input type="button" name="previous" value = "'.$prevtext.'"
    onclick="javascript:history.back()"/>
   &nbsp;&nbsp;&nbsp;');
    }
    if ($next) {
        $r->print('
      <input type="button" name="next" value="'.$nexttext.'"
 onclick="javascript:nextPage(document.'.$formname.','."'".$next."'".')" />');
    }
    $r->print('</div>');
}

sub print_request_outcome {
    return;
}

1;


--raeburn1248735504--