--- loncom/interface/lonpickcourse.pm 2004/11/23 18:03:04 1.19 +++ loncom/interface/lonpickcourse.pm 2008/11/24 14:49:42 1.75 @@ -1,7 +1,7 @@ # The LearningOnline Network # Pick a course # -# $Id: lonpickcourse.pm,v 1.19 2004/11/23 18:03:04 raeburn Exp $ +# $Id: lonpickcourse.pm,v 1.75 2008/11/24 14:49:42 bisitz Exp $ # # Copyright Michigan State University Board of Trustees # @@ -34,6 +34,8 @@ use Apache::loncommon; use Apache::loncoursedata; use Apache::lonnet; use Apache::lonlocal; +use Apache::longroup; +use LONCAPA; sub handler { my $r = shift; @@ -42,33 +44,382 @@ sub handler { return OK if $r->header_only; # ------------------------------------------------------------ Print the screen - $r->print(< - -The LearningOnline Network with CAPA - -ENDDOCUMENT -# Get parameters from query string + + # Get parameters from query string &Apache::loncommon::get_unprocessed_cgi - ($ENV{'QUERY_STRING'},['domainfilter','descriptfilter', - 'sincefilter','form','cnumelement', + ($ENV{'QUERY_STRING'},['domainfilter','form','cnumelement', 'cdomelement','cnameelement','roleelement', - 'pickedcourse']); -# domain filter and selection - my $domainfilter=$ENV{'form.domainfilter'}; - $domainfilter=~s/\W//g; - unless ($domainfilter) { $domainfilter=''; } - my $domainselectform=&Apache::loncommon::select_dom_form($domainfilter, - 'domainfilter',1); -# description filter - my $descriptfilter=$ENV{'form.descriptfilter'}; - unless ($descriptfilter) { $descriptfilter=''; } - my $listdescript=&HTML::Entities::encode($descriptfilter,'<>&"'); -# last course activity filter and selection - my $sincefilter=$ENV{'form.sincefilter'}; - $sincefilter=~s/[^\d-]//g; - unless ($sincefilter) { $sincefilter=-1; } - my $sincefilterform=&Apache::loncommon::select_form($sincefilter, + 'multiple','type','setroles']); + + my ($type,$title,$jscript,$multelement,$multiple,$roleelement, + $lastaction,$autosubmit,$submitopener); + + # Get course type - Course or Non-standard Course. + $type = $env{'form.type'}; + if (!defined($env{'form.type'})) { + $type = 'Course'; + } + $title = &mt('Selecting a [_1]',$type); + + # Setup for multiple course selections, if flag for multiples set. + $multiple = $env{'form.multiple'}; + if ($multiple) { + ($jscript,$multelement) = &multiples_tag(); + $title = &mt('Selecting [_1](s)',lc($type)); + } + + # if called when a DC is selecting a course + my $roledom = $env{'form.roleelement'}; + if ($roledom) { + $roleelement = ''; + $submitopener = &processpick(); + $autosubmit = 'process_pick("'.$roledom.'")'; + } + + my $onlyown = 0; + # if called to assign course-based portfolio access control + if ((($env{'form.form'} eq 'portform') && (!$env{'user.adv'}))) { + $onlyown = 1; + } + + my %loaditem; + if ((($env{'form.form'} eq 'cu') || ($env{'form.form'} eq 'studentform')) && + ($env{'form.pickedcourse'})) { + $loaditem{'onload'} ="setSections()"; + } + $r->print(&Apache::loncommon::start_page($title,undef, + {'add_entries' => \%loaditem, + 'no_nav_bar' => 1, })); + + if ($env{'form.form'} eq 'portform') { + $lastaction = 'document.courselist.submit()'; + } elsif ($env{'form.form'} eq 'cu' || ($env{'form.form'} eq 'studentform' && + !$multiple)) { + $lastaction = + 'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n". + 'document.courselist.submit();'; + } else { + $lastaction = 'self.close()'; + } + + # if called to assign a role in a course to a user via CUSR + if ($env{'form.form'} eq 'cu' || $env{'form.form'} eq 'studentform') { + $r->print(&create_user_javascript($type)); + } + + # print javascript functions for choosing a course + if ($env{'form.gosearch'} || $onlyown) { + $r->print(&gochoose_javascript($type,$multiple,$autosubmit,$lastaction)); + } + $r->print(''); + $r->print($submitopener); + +# ------------------------------------------ Display of filters to limit search + my $filter = {}; + my $action = '/adm/pickcourse'; + if (!$onlyown) { + my $filterlist = ['domainfilter','descriptfilter', + 'instcodefilter','ownerfilter', + 'ownerdomfilter']; + # course ID filter for DCs only + if ($env{'user.adv'} && $env{'form.domainfilter'} && + exists($env{'user.role.dc./'.$env{'form.domainfilter'}.'/'})) { + push(@{$filterlist},'coursefilter'); + } + $r->print(&build_filters($filterlist,$type,$roleelement,$multelement, + $filter,$action)); + } + +# ---------------------------------------------------------------- Get the data + if ($env{'form.gosearch'} || $onlyown) { + my %courses = &search_courses($r,$type,$onlyown,$filter); + &display_matched_courses($r,$type,$multiple,$action,%courses); + } + $r->print(&Apache::loncommon::end_page()); + return OK; +} + +sub processpick { + my $openerform = 'rolechoice'; + if ($env{'form.form'} eq 'studentform') { + $openerform = $env{'form.form'}; + } + my $process_pick = <<"ENDONE"; + +"; + return $process_pick; +} + +sub create_user_javascript { + my ($type) = @_; + my $output; + #javascript for reporting sections and groups then closing + if ($env{'form.pickedcourse'}) { + my ($cdom,$cnum) = split/_/,$env{'form.pickedcourse'}; + my $sec_element = 'currsec'; + my $grplist_element = 'groups'; + my ($sections,$groups) = + &Apache::loncommon::get_secgrprole_info($cdom,$cnum,'',$type); + my $num_sections = scalar(@{$sections}); + my $seclist = join(',',@{$sections}); + my $num_groups = scalar(@{$groups}); + my $groupslist = join(',',@{$groups}); + $output = qq| + +|; +; + } + return $output; +} + +sub display_matched_courses { + my ($r,$type,$multiple,$action,%courses) = @_; + if ($env{'form.form'} eq 'portform') { + $action = '/adm/portfolio'; + } + my $numcourses = keys(%courses); + $r->print('
'); + if ($env{'form.form'} eq 'modifycourse') { + if ($numcourses > 0) { + my $cctitle = &Apache::lonnet::plaintext('cc',$type); + my $dctitle = &Apache::lonnet::plaintext('dc'); + my $ccrolechk = ' checked="checked" '; + my $menuchk = ' '; + if ($env{'form.prevphase'} ne '') { + $ccrolechk = ' '; + $menuchk = ' checked="checked" '; + } + $r->print( + '
' + .''.&mt('Action').'' + .'
' + .'
' + .'
' + .'
' + ); + } + } + my %by_descrip; + foreach my $course (keys(%courses)) { + my $descr; + if (ref($courses{$course}) eq 'HASH') { + $descr = $courses{$course}{'description'}; + } elsif ($courses{$course} =~ m/^([^:]*):/i) { + $descr = &unescape($1); + } else { + $descr = &unescape($courses{$course}); + } + my $description = $descr; + push (@{$by_descrip{$description}}, $course); + } + + if ($numcourses > 1 && $multiple) { + $r->print(' +    +

'); + } + + if (%courses) { + $r->print(&Apache::loncommon::start_data_table()); + $r->print(&Apache::loncommon::start_data_table_header_row()); + $r->print(''.&mt('Select').'' + .''.&mt('Course Description').'' + .''.&mt('Domain').'' + .''.&mt('Course Code').'' + .''.&mt('Owner/Co-owner(s)').'' + .''.&mt('Type').'' + ); + $r->print(&Apache::loncommon::end_data_table_header_row()); + } + foreach my $description (sort { lc($a) cmp lc($b) } (keys(%by_descrip))) { + foreach my $course (@{$by_descrip{$description}}) { + $r->print(&Apache::loncommon::start_data_table_row()); + my $cleandesc=&HTML::Entities::encode($description,'<>&"'); + $cleandesc=~s/'/\\'/g; + my ($cdom,$cnum)=split(/\_/,$course); + my ($descr,$instcode,$ttype,@owners); + if (ref($courses{$course}) eq 'HASH') { + $descr = $courses{$course}{'description'}; + $instcode = $courses{$course}{'inst_code'}; + $ttype = $courses{$course}{'type'}; + push(@owners,&unescape($courses{$course}{'owner'})); + if (ref($courses{$course}{'co-owners'}) eq 'ARRAY') { + foreach my $item (@{$courses{$course}{'co-owners'}}) { + push(@owners,&unescape($item)); + } + } + } else { + my $singleowner; + ($descr,$instcode,$singleowner,$ttype)=split(/:/,$courses{$course}); + push(@owners,&unescape($singleowner)); + } + my $ownerstr = join(', ',@owners); + $r->print(''.&course_chooser($multiple,$cdom,$cnum,$cleandesc).''); + $r->print(''.$description.''); + $r->print(''); + $r->print(&Apache::lonnet::domain($cdom,'description')? + $cdom.' ('.&Apache::lonnet::domain($cdom,'description').')':$cdom); + $r->print(''); + $r->print(''); + if ($instcode ne '') { + $r->print(&unescape($instcode)); + } else { + $r->print(' '); + } + $r->print(''); + $r->print(''.$ownerstr.''); + $r->print(''); + if ($ttype ne '') { + $r->print(&mt(&unescape($ttype))); + } else { + $r->print(' '); + } + $r->print(''); + if ($multiple) { $r->print("\n"); } + $r->print(&Apache::loncommon::end_data_table_row()); + # $r->print("
\n"); + } + } + if (%courses) { + $r->print(&Apache::loncommon::end_data_table()); + } + + if (!%courses) { + $r->print(&mt('None found')); + } elsif ($multiple) { + $r->print(''); + } + $r->print(''. + "\n".''."\n". + ''."\n"); + if ((exists($env{'form.roleelement'})) && ($env{'form.form'} eq 'rolechoice')) { + $r->print(''."\n"); + } + if ($env{'form.form'} eq 'portform') { + $r->print(''); + $r->print(''); + $r->print(''); + $r->print(''); + } elsif ($env{'form.form'} eq 'modifycourse') { + $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','pickedcourse','type','form'])); + } else { + $r->print(''."\n". + ''."\n"); + } + $r->print("
\n"); + return; +} + + +sub multiples_tag { + my $jscript = &Apache::loncommon::check_uncheck_jscript(); + my $multelement = ''; + return ($jscript,$multelement); +} + +sub build_filters { + my ($filterlist,$type,$roleelement,$multelement,$filter,$action, + $caller) = @_; + my $list; + my $formname; + if (defined($env{'form.form'})) { + $formname = $env{'form.form'}; + } else { + $formname = $caller; + } + my ($domainselectform,$sincefilterform,$ownerdomselectform, + $instcodeform,$typeselectform); + foreach my $item (@{$filterlist}) { + $filter->{$item} = $env{'form.'.$item}; + if ($item ne 'descriptfilter' && $item ne 'instcodefilter') { + if ($item eq 'domainfilter' || $item eq 'ownerdomfilter') { + $filter->{$item} = &LONCAPA::clean_domain($filter->{$item}); + } elsif ($item eq 'coursefilter') { + $filter->{$item} = &LONCAPA::clean_courseid($filter->{$item}); + } elsif ($item eq 'ownerfilter') { + $filter->{$item} = &LONCAPA::clean_username($filter->{$item}); + } else { + $filter->{$item} =~ s/\W//g; + } + if (!$filter->{$item}) { + $filter->{$item} = ''; + } + } + if ($item eq 'domainfilter') { + my $allow_blank = 1; + if ($formname eq 'portform') { + $filter->{$item} ||= $env{'user.domain'}; + $allow_blank=0; + } elsif ($formname eq 'studentform') { + $filter->{$item} ||= $env{'request.role.domain'}; + $allow_blank=0; + } + $domainselectform = + &Apache::loncommon::select_dom_form($filter->{$item}, + 'domainfilter', + $allow_blank); + } elsif ($item eq 'ownerdomfilter') { + $ownerdomselectform = + &Apache::loncommon::select_dom_form($filter->{$item},'ownerdomfilter',1); + } else { + $list->{$item} = &HTML::Entities::encode($filter->{$item},'<>&"'); + } + } + + # last course activity filter and selection + $filter->{'sincefilter'} = $env{'form.sincefilter'}; + $filter->{'sincefilter'} =~ s/[^\d-]//g; + if (!$filter->{'sincefilter'}) { $filter->{'sincefilter'}=-1; } + $sincefilterform=&Apache::loncommon::select_form($filter->{'sincefilter'}, 'sincefilter',('-1'=>'', '86400' => 'today', '604800' => 'last week', @@ -78,148 +429,439 @@ ENDDOCUMENT '31104000' => 'last year', 'select_form_order' => ['-1','86400','604800','2592000','7776000', - '15552000','31104000'])); -# if called from loncreateuser, report sections, then close - my ($loaditem,$seclist); - my $num_sections = 0; - my @sections = (); - if ($ENV{'form.form'} eq 'cu' && $ENV{'form.pickedcourse'}) { - $loaditem = 'onLoad="setSections()"'; - my ($cdom,$cnum) = split/_/,$ENV{'form.pickedcourse'}; - my %sections_count = (); - $num_sections = &Apache::loncommon::get_sections($cdom,$cnum,\%sections_count); - my @sections = (); - if ($num_sections > 0) { - @sections = (sort {$a <=> $b} keys %sections_count); - $seclist = join('","',@sections); - } - } - $r->print(&Apache::loncommon::bodytag("Selecting a Course",undef,$loaditem)); - my %lt=&Apache::lonlocal::texthash( - 'cac' => 'Course Activity', - 'cde' => 'Course Description', - 'cdo' => 'Course Domain'); - my ($name_code,$name_input); - if (defined($ENV{'form.cnameelement'}) && $ENV{'form.cnameelement'} ne '') { - $name_code = "opener.document.$ENV{'form.form'}.$ENV{'form.cnameelement'}.value=cdesc;"; - $name_input =''; - } - my $submitopener = ''; - my $autosubmit = ''; - my $roleelement = ''; - my $lastaction = 'self.close()'; - if ($ENV{'form.form'} eq 'cu') { - $lastaction = 'document.courselist.pickedcourse.value = cdom+"_"+cname;'."\n". - 'document.courselist.submit();'; + '15552000','31104000'])); + + my %lt = ( + 'cac' => &mt("$type Activity"), + 'cde' => &mt("$type Description"), + 'cdo' => &mt("$type Domain"), + 'cin' => &mt('Course Institutional Code'), + 'cow' => &mt("$type Owner's Username"), + 'cod' => &mt("$type Owner's Domain"), + 'cog' => &mt('Course Type') + ); + + $typeselectform = ''; } - my $roledom = $ENV{'form.roleelement'}; - if ($roledom) { - $roleelement = ''; - $submitopener = &Apache::lonroles::processpick($roledom); - $autosubmit = 'process_pick("'.$roledom.'")'; + foreach my $posstype ('Course','Non-standard Course') { + $typeselectform.='\n"; } - $r->print(< -function gochoose(cname,cdom,cdesc) { - $name_code - var openerForm = "$ENV{'form.form'}"; - opener.document.$ENV{'form.form'}.$ENV{'form.cnumelement'}.value=cname; - if (openerForm == 'cu' || openerForm == 'rolechoice') { - opener.document.$ENV{'form.form'}.$ENV{'form.cdomelement'}.value=cdom; + $typeselectform.=""; + my $output = qq| +
+ +|; + if ($formname eq 'modifycourse') { + $output .= ''."\n". + ''."\n"; + } else { + my $name_input; + if ($env{'form.cnameelement'} ne '') { + $name_input = ''; + } + $output .= qq| + + +$name_input +$roleelement +$multelement +|; + if ($formname eq 'portform') { + $output .= ''; + } } - else { - var slct=opener.document.$ENV{'form.form'}.$ENV{'form.cdomelement'}; - var i; - for (i=0;i{'ownerfilter'})) { + $output .= &Apache::lonhtmlcommon::row_title($lt{'cow'}) + .'' + .&Apache::lonhtmlcommon::row_closure() + .&Apache::lonhtmlcommon::row_title($lt{'cod'}) + .$ownerdomselectform + .&Apache::lonhtmlcommon::row_closure(); + } + if (exists($filter->{'coursefilter'})) { + $output .= &Apache::lonhtmlcommon::row_title(&mt('LON-CAPA course ID')) + .'' + .&Apache::lonhtmlcommon::row_closure(); + } + if (exists($filter->{'descriptfilter'})) { + $output .= &Apache::lonhtmlcommon::row_title($lt{'cde'}) + .'' + .&Apache::lonhtmlcommon::row_closure(1); + } + $output .= &Apache::lonhtmlcommon::end_pick_box(); + $output .= '

'."\n".''."\n".'
'."\n"; + return $output; +} + +sub search_courses { + my ($r,$type,$onlyown,$filter) = @_; + my %courses; + if (!$onlyown) { + $r->print(&mt('Searching ...').'
 
'); + $r->rflush(); + if (($filter->{'ownerfilter'} ne '') || + ($filter->{'ownerdomfilter'} ne '')) { + $filter->{'combownerfilter'} = $filter->{'ownerfilter'}.':'. + $filter->{'ownerdomfilter'}; + } + foreach my $item ('descriptfilter','instcodefilter','coursefilter', + 'combownerfilter') { + if (!$filter->{$item}) { + $filter->{$item}='.'; + } + } + if ($type eq '') { $type = '.'; } + my $timefilter = + ($filter->{'sincefilter'}==-1?1:time-$filter->{'sincefilter'}); + %courses = + &Apache::lonnet::courseiddump($filter->{'domainfilter'}, + $filter->{'descriptfilter'}, + $timefilter, + $filter->{'instcodefilter'}, + $filter->{'combownerfilter'}, + $filter->{'coursefilter'}, + undef,undef,$type); + } else { + $r->print('
'); + my %coursehash = &Apache::loncommon::findallcourses(); + foreach my $cid (sort(keys(%coursehash))) { + $courses{$cid}{'description'} = $env{'course.'.$cid.'.description'}; } } - $autosubmit - $lastaction + return %courses; } -function setSections() { - var numSections = $num_sections - var sectionsArray = new Array("$seclist") - opener.document.$ENV{"form.form"}.currsec.length = 0 - if (numSections == 0) { - opener.document.$ENV{"form.form"}.currsec.options[0] = new Option('No existing sections','-1',false,false) +sub course_chooser { + my ($multiple,$cdom,$cnum,$cleandesc) = @_; + my $output; + if ($multiple) { + $output = '