# The LearningOnline Network with CAPA
# Handler for displaying the course catalog interface
#
# $Id: coursecatalog.pm,v 1.34 2008/07/04 15:40:04 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/
#
package Apache::coursecatalog;
use strict;
use lib qw(/home/httpd/lib/perl);
use Apache::Constants qw(:common);
use Apache::loncommon;
use Apache::lonhtmlcommon;
use Apache::lonnet;
use Apache::lonlocal;
use Apache::courseclassifier;
use Apache::lonacc;
use LONCAPA;
sub handler {
my ($r) = @_;
&Apache::loncommon::content_type($r,'text/html');
$r->send_http_header;
if ($r->header_only) {
return OK;
}
my $handle = &Apache::lonnet::check_for_valid_session($r);
my $lonidsdir=$r->dir_config('lonIDsDir');
if ($handle ne '') {
&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
}
&Apache::lonacc::get_posted_cgi($r);
&Apache::lonlocal::get_language_handle($r);
&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sortby']);
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'};
}
}
my $formname = 'coursecatalog';
if ($env{'form.showdom'} ne '') {
if (&Apache::lonnet::domain($env{'form.showdom'}) ne '') {
$codedom = $env{'form.showdom'};
}
}
my $domdesc = &Apache::lonnet::domain($codedom,'description');
&Apache::lonhtmlcommon::clear_breadcrumbs();
if ($env{'form.coursenum'} ne '' && &user_is_known()) {
&course_details($r,$codedom,$formname,$domdesc);
} else {
my $catlinks = &category_breadcrumbs($codedom);
my $catjs = <<"ENDSCRIPT";
function setCatDepth(depth) {
document.coursecats.catalog_maxdepth.value = depth;
document.coursecats.submit();
return;
}
ENDSCRIPT
if ($env{'form.state'} eq 'listing') {
$catjs .= qq|
function changeSort(caller) {
document.$formname.sortby.value = caller;
document.$formname.submit();
}
function setCourseId(caller) {
document.$formname.coursenum.value = caller;
document.$formname.submit();
}\n|;
}
my $numtitles;
if ($env{'form.currcat_0'} eq 'instcode::0') {
$numtitles = &instcode_course_selector($r,$codedom,$formname,$domdesc,
$catlinks,$catjs);
if ($env{'form.state'} eq 'listing') {
$r->print(&print_course_listing($codedom,$numtitles));
}
} else {
my (%add_entries);
$catjs = '';
&cat_header($r,$codedom,$catjs,\%add_entries,$catlinks);
if ($env{'form.currcat_0'} ne '') {
$r->print('
');
}
if ($env{'form.state'} eq 'listing') {
$r->print(&print_course_listing($codedom));
}
}
}
$r->print(' '.&Apache::loncommon::end_page());
return OK;
}
sub course_details {
my ($r,$codedom,$formname,$domdesc) = @_;
my $output;
my %add_entries = (topmargin => "0",
marginheight => "0",);
my $start_page =
&Apache::loncommon::start_page('Course Catalog','',
{
'add_entries' => \%add_entries,
'no_inline_link' => 1,});
$r->print($start_page);
if ($env{'form.numtitles'} > 0) {
&Apache::lonhtmlcommon::add_breadcrumb
({href=>"/adm/coursecatalog",
text=>"Select courses"});
}
&Apache::lonhtmlcommon::add_breadcrumb
({href=>"javascript:document.$formname.submit()",
text=>"Course listing"},
{text=>"Course details"});
$r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Details'));
$r->print(' '.&mt('Detailed course information:').'
'.
'');
}
sub instcode_course_selector {
my ($r,$codedom,$formname,$domdesc,$catlinks,$catjs) = @_;
my %coursecodes = ();
my %codes = ();
my @codetitles = ();
my %cat_titles = ();
my %cat_order = ();
my %idlist = ();
my %idnums = ();
my %idlist_titles = ();
my %by_year;
my %by_sem;
my %by_dept;
my %cat_items;
my $caller = 'global';
my $format_reply;
my $totcodes = 0;
my $jscript = '';
my ($numtitles,$lasttitle);
my %add_entries = (topmargin => "0",
marginheight => "0",);
my $js;
$totcodes = &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,$codedom);
if ($totcodes > 0) {
$format_reply = &Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
if ($format_reply eq 'ok') {
my $numtypes = @codetitles;
&Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
my $longtitles_str = join('","',@{$longtitles});
my $allidlist = $idlist{$codetitles[0]};
$numtitles = @codetitles;
$lasttitle = $numtitles;
if ($numtitles > 4) {
$lasttitle = 4;
}
if ($numtitles == 0) {
if (!defined($env{'form.state'})) {
$env{'form.state'} = 'listing';
}
} else {
my @data = ('top');
for (my $k=0; $k<$lasttitle; $k++) {
my $cat = $codetitles[$k];
my $level = 1;
$level = &recurse_options($codetitles[$k],$idlist{$codetitles[$k]},$level,$cat,\%cat_items,\@data,\%by_year,\%by_sem,\%by_dept);
}
$scripttext .= &build_javascript(\%by_year,\%by_sem,\%by_dept,\%cat_order,\@codetitles);
$jscript .= &javascript_select_filler($formname,$scripttext,\@codetitles,$longtitles_str,$allidlist);
if ($env{'form.state'} eq 'listing') {
$jscript .= '
function setElements() {
';
for (my $i=0; $i<@codetitles-1; $i++) {
if ($env{'form.'.$codetitles[$i]} != -1) {
$jscript .= '
for (var j=0; j'."\n$jscript\n$catjs\n".
'';
if (($env{'form.state'} eq 'listing') && ($numtitles > 0)) {
$add_entries{'onLoad'} = 'setElements()';
}
&cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
my $cat_maxdepth = $env{'form.catalog_maxdepth'};
$r->print('');
if (($numtitles > 0) && ($env{'form.state'} eq 'listing')) {
$r->print(' ');
}
} else {
&cat_header($r,$codedom,$js,\%add_entries,$catlinks,$numtitles);
my $cat_maxdepth = $env{'form.catalog_maxdepth'};
$r->print('');
}
return $numtitles;
}
sub cat_header {
my ($r,$codedom,$js,$add_entries,$catlinks,$numtitles) = @_;
my $start_page =
&Apache::loncommon::start_page('Course Catalog',$js,
{
'add_entries' => $add_entries,
'no_inline_link' => 1,});
$r->print($start_page);
if ($env{'form.state'} eq 'listing') {
if ($numtitles > 0) {
&Apache::lonhtmlcommon::add_breadcrumb
({href=>"/adm/coursecatalog",
text=>"Select courses"},
{text=>"Course listing"});
} else {
&Apache::lonhtmlcommon::add_breadcrumb
({text=>"Course listing"});
}
$r->print(&Apache::lonhtmlcommon::breadcrumbs('Course Listing'));
} else {
&Apache::lonhtmlcommon::add_breadcrumb
({href=>"/adm/coursecatalog",
text=>"Select courses"});
$r->print(&Apache::lonhtmlcommon::breadcrumbs('Select courses'));
}
$r->print(''.
'');
return;
}
sub category_breadcrumbs {
my ($dom) = @_;
my %domconfig =
&Apache::lonnet::get_dom('configuration',['coursecategories'],$dom);
my (@cats,@trails,%allitems,%idx,@jsarray,$cathash);
if (ref($domconfig{'coursecategories'}) eq 'HASH') {
$cathash = $domconfig{'coursecategories'}{'cats'};
} else {
$cathash = {};
}
&Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems,\%idx,
\@jsarray);
my ($currdepth,$deeper) = &get_depth_values();
my $currcat_str = '';
my $catlinks = '