--- loncom/interface/lonpickcourse.pm 2003/11/04 16:33:10 1.12
+++ loncom/interface/lonpickcourse.pm 2009/07/31 03:01:31 1.85
@@ -1,7 +1,7 @@
# The LearningOnline Network
# Pick a course
#
-# $Id: lonpickcourse.pm,v 1.12 2003/11/04 16:33:10 albertel Exp $
+# $Id: lonpickcourse.pm,v 1.85 2009/07/31 03:01:31 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -34,6 +34,9 @@ use Apache::loncommon;
use Apache::loncoursedata;
use Apache::lonnet;
use Apache::lonlocal;
+use Apache::longroup;
+use Apache::courseclassifier;
+use LONCAPA qw(:DEFAULT :match);
sub handler {
my $r = shift;
@@ -42,32 +45,518 @@ 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','cdomelement']);
-# 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'};
- $descriptfilter=~s/[^\w\s]//g;
- unless ($descriptfilter) { $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,
+ ($ENV{'QUERY_STRING'},['domainfilter','form','cnumelement',
+ 'cdomelement','cnameelement','roleelement',
+ 'multiple','type','setroles','fixeddom','cloner']);
+ my ($type,$title,$jscript,$multelement,$multiple,$roleelement,
+ $lastaction,$autosubmit,$submitopener,$cloneruname,$clonerudom);
+
+ # Get course type - Course or Community.
+ $type = $env{'form.type'};
+ if (!defined($env{'form.type'})) {
+ $type = 'Course';
+ }
+ $title = 'Selecting a '.$type;
+
+ # Setup for multiple course selections, if flag for multiples set.
+ $multiple = $env{'form.multiple'};
+ if ($multiple) {
+ ($jscript,$multelement) = &multiples_tag();
+ $title = 'Selecting '.$type.'(s)';
+ }
+
+ # if called when a DC is selecting a course
+ my $roledom = $env{'form.roleelement'};
+ if ($roledom) {
+ $roleelement = '';
+ $submitopener = &processpick();
+ $autosubmit = 'process_pick("'.$roledom.'")';
+ }
+
+ # if called when a DC is creating a course
+ if ($env{'form.form'} eq 'ccrs') {
+ ($cloneruname,$clonerudom) = ($env{'form.cloner'} =~ /^($match_username):($match_domain)$/);
+ }
+
+ # if called when requesting a course
+ if ($env{'form.form'} eq 'requestcrs') {
+ $cloneruname = $env{'user.name'};
+ $clonerudom = $env{'user.domain'};
+ }
+
+ 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.type'} eq 'Course') && ($env{'form.numtitles'})) {
+ if (($env{'form.official'} eq 'on') && ($env{'form.state'} eq 'listing')) {
+ $loaditem{'onload'} = 'setElements(document.filterpicker); ';
+ }
+ }
+
+ if ((($env{'form.form'} eq 'cu') || ($env{'form.form'} eq 'studentform')) &&
+ ($env{'form.pickedcourse'})) {
+ $loaditem{'onload'} .= 'setSections()';
+ }
+ my $js = &js_changer();
+ $r->print(&Apache::loncommon::start_page($title,$js,
+ {'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'}) && ($env{'form.updater'} eq '')) ||
+ $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';
+ my ($numtitles,$showroles,$nohost);
+ if (!$onlyown) {
+ my $filterlist = ['domainfilter','descriptfilter',
+ 'instcodefilter'];
+ if ($env{'form.form'} eq 'rules') {
+ push(@{$filterlist},'personfilter');
+ if (($env{'form.personfilter'} ne '') && ($env{'form.persondomfilter'} ne '')) {
+ if (&Apache::lonnet::homeserver($env{'form.personfilter'},
+ $env{'form.persondomfilter'}) eq 'no_host') {
+ $nohost = 1;
+ } else {
+ $showroles = 1;
+ }
+ }
+ } else {
+ push(@{$filterlist},'ownerfilter');
+ }
+ # course ID filter for DCs only
+ if ($env{'user.adv'} && $env{'form.domainfilter'} &&
+ exists($env{'user.role.dc./'.$env{'form.domainfilter'}.'/'})) {
+ push(@{$filterlist},'coursefilter');
+ }
+ if ($cloneruname ne '' && $clonerudom ne '') {
+ push(@{$filterlist},'cloneableonly');
+ }
+
+ $r->print(&build_filters($filterlist,$type,$roleelement,$multelement,
+ $filter,$action,\$numtitles,undef,$cloneruname,
+ $clonerudom));
+ }
+
+# ---------------------------------------------------------------- Get the data
+ if ((($env{'form.gosearch'}) && ($env{'form.updater'} eq '')) ||
+ $onlyown) {
+ my %courses = &search_courses($r,$type,$onlyown,$filter,$numtitles,
+ $cloneruname,$clonerudom);
+ if ($nohost) {
+ $r->print (''.
+ &mt('User does not exist - username: [_1], domain: [_2].',
+ ''.$filter->{'personfilter'}.'',
+ ''.$filter->{'persondomfilter'}.'').'');
+ } else {
+ &display_matched_courses($r,$type,$multiple,$action,$showroles,$cloneruname,
+ $clonerudom,%courses);
+ }
+ }
+ $r->print(&Apache::loncommon::end_page());
+ return OK;
+}
+
+sub js_changer {
+ return <<"ENDJS";
+
+
+ENDJS
+
+}
+
+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,$showroles,$cloneruname,$clonerudom,%courses) = @_;
+ if ($env{'form.form'} eq 'portform') {
+ $action = '/adm/portfolio';
+ }
+ my $numcourses = keys(%courses);
+ $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,
+ $numtitlesref,$caller,$cloneruname,$clonerudom) = @_;
+ my ($list,$formname,$fixeddom,$codedom,$jscript);
+ $codedom = $env{'request.role.domain'};
+ if (defined($env{'form.form'})) {
+ $formname = $env{'form.form'};
+ } else {
+ $formname = $caller;
+ }
+ my $onchange;
+ unless ($env{'form.interface'} eq 'textual') {
+ $onchange = 'javascript:updateFilters(this)';
+ }
+ my ($domainselectform,$sincefilterform,$ownerdomselectform,$persondomselectform,
+ $instcodeform,$typeselectform,$instcodetitle);
+ foreach my $item (@{$filterlist}) {
+ $filter->{$item} = $env{'form.'.$item};
+ if ($item ne 'descriptfilter' && $item ne 'instcodefilter') {
+ if ($item eq 'domainfilter') {
+ $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});
+ $filter->{'ownerdomfilter'} =
+ &LONCAPA::clean_domain($env{'form.ownerdomfilter'});
+ $ownerdomselectform =
+ &Apache::loncommon::select_dom_form($filter->{'ownerdomfilter'},
+ 'ownerdomfilter',1);
+ } elsif ($item eq 'personfilter') {
+ $filter->{$item} = &LONCAPA::clean_username($filter->{$item});
+ if ($env{'form.persondomfilter'} eq '') {
+ unless ($env{'form.gosearch'}) {
+ $filter->{'persondomfilter'} = $env{'request.role.domain'};
+ }
+ } else {
+ $filter->{'persondomfilter'} =
+ &LONCAPA::clean_domain($env{'form.persondomfilter'});
+ }
+ $persondomselectform =
+ &Apache::loncommon::select_dom_form($filter->{'persondomfilter'},
+ 'persondomfilter',1);
+ } 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;
+ }
+ if ($env{'form.fixeddom'}) {
+ $domainselectform = ''.
+ &Apache::lonnet::domain($env{'request.role.domain'},
+ 'description');
+ $codedom = $env{'request.role.domain'};
+ } else {
+ $domainselectform =
+ &Apache::loncommon::select_dom_form($filter->{$item},
+ 'domainfilter',
+ $allow_blank,'',$onchange);
+ $codedom = $filter->{'domainfilter'};
+ }
+ } 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',
@@ -77,69 +566,601 @@ ENDDOCUMENT
'31104000' => 'last year',
'select_form_order' =>
['-1','86400','604800','2592000','7776000',
- '15552000','31104000']));
-
- $r->print(&Apache::loncommon::bodytag("Selecting a Course"));
- my %lt=&Apache::lonlocal::texthash(
- 'cac' => 'Course Activity',
- 'cde' => 'Course Description',
- 'cdo' => 'Course Domain');
- $r->print(<
-function gochoose(cname,cdom) {
- opener.document.$ENV{'form.form'}.$ENV{'form.cnumelement'}.value=cname;
- var slct=opener.document.$ENV{'form.form'}.$ENV{'form.cdomelement'};
- var i;
- for (i=0;i &mt("$type Activity"),
+ 'cde' => &mt("$type Title"),
+ 'cdo' => &mt("$type Domain"),
+ 'ins' => &mt('Institutional Code'),
+ 'inc' => &mt('Institutional Categorization'),
+ 'cow' => &mt("$type Owner/Co-owner"),
+ 'cop' => &mt("$type Personnel Includes"),
+ 'cog' => &mt('Type')
+ );
+
+ $typeselectform = '";
+ my $output = qq|
+
'."\n".''."\n".'
'."\n";
+ return $jscript.$warning.$output;
}
-
-
-
-ENDSCRIPT
-# ---------------------------------------------------------------- Get the data
- if ($ENV{'form.gosearch'}) {
- $r->print(&mt('Searching').' ...
');
+
+sub instcode_selectors {
+ my ($codedom,$formname,$officialjs) = @_;
+ my ($output,@codetitles,%cat_titles,%cat_order,%cat_items);
+ my ($jscript,$totcodes,$numtitles,$lasttitle) =
+ &Apache::courseclassifier::instcode_selectors_data($codedom,$formname,
+ \%cat_items,\@codetitles,\%cat_titles,\%cat_order,$officialjs);
+ if ($numtitles > 0) {
+ my $official = ' checked="checked" ';
+ my $unofficial = '';
+ if ($env{'form.official'} eq 'off') {
+ $unofficial = $official;
+ $official = '';
+ }
+ $output .= ''.&mt('Official course:').' '.(' 'x3).'
'.
+ &Apache::courseclassifier::build_instcode_selectors($numtitles,
+ $lasttitle,\%cat_items,\@codetitles,\%cat_titles,\%cat_order)."\n".
+ ''."\n".
+ ''."\n";
+
+ }
+ return ($output,$jscript,$numtitles);
+}
+
+sub search_courses {
+ my ($r,$type,$onlyown,$filter,$numtitles,$cloneruname,$clonerudom) = @_;
+ my (%courses,%showcourses,$cloner);
+ if (!$onlyown) {
+ $r->print(&mt('Searching ...').'
');
$r->rflush();
- unless ($descriptfilter) { $descriptfilter='.'; }
- my $timefilter=($sincefilter==-1?1:time-$sincefilter);
- my %courses=
- &Apache::lonnet::courseiddump
- ($domainfilter,$descriptfilter,$timefilter);
- $r->print('');
+ if (($filter->{'ownerfilter'} ne '') ||
+ ($filter->{'ownerdomfilter'} ne '')) {
+ $filter->{'combownerfilter'} = $filter->{'ownerfilter'}.':'.
+ $filter->{'ownerdomfilter'};
+ }
+ foreach my $item ('descriptfilter','coursefilter','combownerfilter') {
+ if (!$filter->{$item}) {
+ $filter->{$item}='.';
+ }
+ }
+ my $timefilter =
+ ($filter->{'sincefilter'}==-1?1:time-$filter->{'sincefilter'});
+ my ($instcodefilter,$regexpok);
+ if ($numtitles) {
+ if ($env{'form.official'} eq 'on') {
+ $instcodefilter =
+ &Apache::courseclassifier::instcode_search_str($filter->{'domainfilter'},$numtitles);
+ $regexpok = 1;
+ } elsif ($env{'form.official'} eq 'off') {
+ $instcodefilter = &Apache::courseclassifier::instcode_search_str($filter->{'domainfilter'},$numtitles);
+ unless ($instcodefilter eq '') {
+ $regexpok = -1;
+ }
+ }
+ } else {
+ $instcodefilter = $filter->{'instcodefilter'};
+ }
+ if ($instcodefilter eq '') { $instcodefilter = '.'; }
+ if ($type eq '') { $type = '.'; }
+
+ if (($clonerudom ne '') && ($cloneruname ne '')) {
+ $cloner = $cloneruname.':'.$clonerudom;
+ }
+ %courses =
+ &Apache::lonnet::courseiddump($filter->{'domainfilter'},
+ $filter->{'descriptfilter'},
+ $timefilter,
+ $instcodefilter,
+ $filter->{'combownerfilter'},
+ $filter->{'coursefilter'},
+ undef,undef,$type,$regexpok,undef,undef,
+ undef,undef,$cloner,$env{'form.cc_clone'},
+ $filter->{'cloneableonly'});
+ if (($filter->{'personfilter'} ne '') && ($filter->{'persondomfilter'} ne '')) {
+ my %rolehash = &Apache::lonnet::get_my_roles($filter->{'personfilter'},
+ $filter->{'persondomfilter'},
+ 'userroles',undef,
+ ['cc','in','ad','ep','ta','cr'],
+ $filter->{'domainfilter'});
+ foreach my $role (keys(%rolehash)) {
+ my ($cnum,$cdom,$courserole) = split(':',$role);
+ my $cid = $cdom.'_'.$cnum;
+ if (exists($courses{$cid})) {
+ if (ref($courses{$cid}) eq 'HASH') {
+ if (ref($courses{$cid}{roles}) eq 'ARRAY') {
+ if (!grep(/^\Q$courserole\E$/,@{$courses{$cid}{roles}})) {
+ push (@{$courses{$cid}{roles}},$courserole);
+ }
+ } else {
+ $courses{$cid}{roles} = [$courserole];
+ }
+ $showcourses{$cid} = $courses{$cid};
+ }
+ }
+ }
+ %courses = %showcourses;
+ }
+ } else {
+ $r->print('
');
+ my %coursehash = &Apache::loncommon::findallcourses();
+ foreach my $cid (sort(keys(%coursehash))) {
+ $courses{$cid}{'description'} = $env{'course.'.$cid.'.description'};
+ }
}
- $r->print('