--- loncom/interface/lonpickcourse.pm 2005/10/03 23:53:25 1.30
+++ loncom/interface/lonpickcourse.pm 2009/12/02 18:33:27 1.91
@@ -1,7 +1,7 @@
# The LearningOnline Network
# Pick a course
#
-# $Id: lonpickcourse.pm,v 1.30 2005/10/03 23:53:25 raeburn Exp $
+# $Id: lonpickcourse.pm,v 1.91 2009/12/02 18:33:27 bisitz 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,298 +45,1187 @@ sub handler {
return OK if $r->header_only;
# ------------------------------------------------------------ Print the screen
- my $html=&Apache::lonxml::xmlbegin();
- $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','instcodefilter',
- 'ownerfilter','coursefilter']);
-# 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,'<>&"');
-# institutional code filter
- my $instcodefilter=$env{'form.instcodefilter'};
- unless ($instcodefilter) { $instcodefilter=''; }
- my $listinstcode=&HTML::Entities::encode($instcodefilter,'<>&"');
-# course owner filter
- my $ownerfilter=$env{'form.ownerfilter'};
- unless ($ownerfilter) { $ownerfilter=''; }
- my $listowner=&HTML::Entities::encode($ownerfilter,'<>&"');
-# course ID filter for DCs only
- my ($courseidtag,$coursefilter);
- if ($env{'user.adv'} && $domainfilter && exists($env{'user.role.dc./'.
- $domainfilter.'/'})) {
- $coursefilter=$env{'form.coursefilter'};
- unless ($coursefilter) { $coursefilter=''; }
- my $listcourseid=&HTML::Entities::encode($coursefilter,'<>&"');
- $courseidtag = &mt('LON-CAPA course ID');
- $courseidtag .= qq|
-
- |;
- }
-# 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,
- 'sincefilter',('-1'=>'',
- '86400' => 'today',
- '604800' => 'last week',
- '2592000' => 'last month',
- '7776000' => 'last three months',
- '15552000' => 'last six months',
- '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;
- 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);
- }
- }
-# if called from rolefilter, setup for multiple course selections.
- my $multflag = 0;
- my $jscript;
- my $title = 'Selecting a course';
- if ($env{'form.form'} eq 'rolefilter') {
- $multflag = 1;
- $title = 'Selecting course(s)';
- }
- if ($multflag) {
- $jscript = &Apache::loncommon::check_uncheck_jscript();
- }
- $r->print(&Apache::loncommon::bodytag($title,undef,$loaditem,undef,undef,undef,undef,1));
- my %lt=&Apache::lonlocal::texthash(
- 'cac' => 'Course Activity',
- 'cde' => 'Course Description',
- 'cdo' => 'Course Domain',
- 'cin' => 'Course Institutional Code',
- 'cow' => "Course Owner's usename");
- 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();';
+ 'multiple','type','setroles','fixeddom','cloner']);
+ my ($type,$title,$jscript,$multelement,$multiple,$roleelement,$typeelement,
+ $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 = &Apache::lonroles::processpick($roledom);
+ $submitopener = &processpick();
$autosubmit = 'process_pick("'.$roledom.'")';
}
- $r->print(<
-function gochoose(cname,cdom,cdesc,multflag,caller) {
- var openerForm = "$env{'form.form'}";
- courseCount = 0;
- var courses = '';
- if (multflag) {
- for (var j=0; j';
+ }
+
+ # 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); ';
}
- opener.document.$env{'form.form'}.coursetotal.value = courseCount;
- for (var j=0; jprint(&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 (courseCount > 0) {
- courses = courses.substr(0,courses.length-2);
- opener.document.$env{'form.form'}.courselist.value = courses
+ if ($cloneruname ne '' && $clonerudom ne '') {
+ push(@{$filterlist},'cloneableonly');
}
+
+ $r->print(&build_filters($filterlist,$type,$roleelement,$multelement,
+ $filter,$action,\$numtitles,undef,$cloneruname,
+ $clonerudom,$typeelement));
}
- else {
- $name_code
- opener.document.$env{'form.form'}.$env{'form.cnumelement'}.value=cname;
- if (openerForm == 'cu') {
- opener.document.$env{'form.form'}.$env{'form.cdomelement'}.value=cdom;
+
+# ---------------------------------------------------------------- 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);
}
- else {
- var slct=opener.document.$env{'form.form'}.$env{'form.cdomelement'};
- var i;
- for (i=0;iprint(&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 %coursedescription =
+ &Apache::lonnet::coursedescription($env{'form.pickedcourse'},
+ {'one_time' => '1'});
+ my $cdom = $coursedescription{'domain'};
+ my $cnum = $coursedescription{'num'};
+ my $crstype = $coursedescription{'type'};
+ 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|
+
+|;
+;
}
- else {
- if (numSections == 1) {
- opener.document.$env{"form.form"}.currsec.multiple=false;
- opener.document.$env{"form.form"}.currsec.size=1;
- opener.document.$env{"form.form"}.currsec.options[0] = new Option('Select','',true,true);
- opener.document.$env{"form.form"}.currsec.options[1] = new Option('No section','',false,false)
- opener.document.$env{"form.form"}.currsec.options[2] = new Option(sectionsArray[0],sectionsArray[0],false,false);
+ 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;
}
-$jscript
-
-$submitopener
-
-
-ENDSCRIPT
-# ---------------------------------------------------------------- Get the data
- if ($env{'form.gosearch'}) {
- $r->print(&mt('Searching').' ...
');
+$multelement
+$typeelement
+|;
+ if ($formname eq 'portform') {
+ $output .= ''."\n";
+ }
+ }
+ if ($env{'form.fixeddom'}) {
+ $output .= ''."\n";
+ }
+ $output .= "
\n".&Apache::lonhtmlcommon::start_pick_box();
+ if ($sincefilterform) {
+ $output .= &Apache::lonhtmlcommon::row_title($lt{'cac'})
+ .$sincefilterform
+ .&Apache::lonhtmlcommon::row_closure();
+ }
+ if ($domainselectform) {
+ $output .= &Apache::lonhtmlcommon::row_title($lt{'cdo'})
+ .$domainselectform
+ .&Apache::lonhtmlcommon::row_closure();
+ }
+ if ($typeselectform) {
+ if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
+ $output .= $typeselectform;
+ } else {
+ $output .= &Apache::lonhtmlcommon::row_title($lt{'cog'})
+ .$typeselectform
+ .&Apache::lonhtmlcommon::row_closure();
+ }
+ }
+ if ($instcodeform) {
+ $output .= &Apache::lonhtmlcommon::row_title($instcodetitle)
+ .$instcodeform
+ .&Apache::lonhtmlcommon::row_closure();
+ }
+ if (exists($filter->{'ownerfilter'})) {
+ $output .= &Apache::lonhtmlcommon::row_title($lt{'cow'}).
+ ''.
+ &Apache::lonhtmlcommon::row_closure();
+ }
+ if (exists($filter->{'personfilter'})) {
+ $output .= &Apache::lonhtmlcommon::row_title($lt{'cop'}).
+ ''.
+ &Apache::lonhtmlcommon::row_closure();
+ }
+ if (exists($filter->{'coursefilter'})) {
+ $output .= &Apache::lonhtmlcommon::row_title(&mt('LON-CAPA course ID'))
+ .''
+ .&Apache::lonhtmlcommon::row_closure();
+ }
+ if ($cloneableonlyform) {
+ $output .= &Apache::lonhtmlcommon::row_title($cloneabletitle).
+ $cloneableonlyform.&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().'';
+ my $warning;
+ if (($env{'form.form'} eq 'ccrs') || ($env{'form.form'} eq 'requestcrs')) {
+ my $cloneruhome=&Apache::lonnet::homeserver($cloneruname,$clonerudom);
+ my $cc_clone;
+ if ($cloneruhome eq 'no_host') {
+ $warning = '
'.&mt('Intended course owner does not exist').
+ '
';
+ } else {
+ my $ccrole;
+ if ($type eq 'Community') {
+ $ccrole = 'co';
+ } elsif ($type eq 'Course') {
+ $ccrole = 'cc';
+ }
+ if ($env{'form.form'} eq 'ccrs') {
+ $output .= ''."\n";
+ }
+ my %ccroles = &Apache::lonnet::get_my_roles($cloneruname,$clonerudom,
+ 'userroles',['active'], [$ccrole]);
+ foreach my $key (sort(keys(%ccroles))) {
+ my ($cnum,$cdom,$role) = split(':',$key);
+ $cc_clone .= $cdom.':'.$cnum.'&';
+ }
+ $cc_clone =~ s/\&$//;
+ }
+ if ($cc_clone ne '') {
+ $output .= '';
+ }
+ }
+ $output .= ''."\n".
+ '
'."\n".''."\n".'
'."\n";
+ return $jscript.$warning.$output;
+}
+
+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,$ccrole);
+ if ($type eq 'Community') {
+ $ccrole = 'co';
+ } else {
+ $ccrole = 'cc';
+ }
+ if (!$onlyown) {
+ $r->print(&mt('Searching ...').'
');
$r->rflush();
- unless ($descriptfilter) { $descriptfilter='.'; }
- unless ($instcodefilter) { $instcodefilter='.'; }
- unless ($ownerfilter) { $ownerfilter='.'; }
- unless ($coursefilter) { $coursefilter='.'; }
- my $timefilter=($sincefilter==-1?1:time-$sincefilter);
- my %courses=
- &Apache::lonnet::courseiddump
- ($domainfilter,$descriptfilter,$timefilter,$instcodefilter,$ownerfilter,$coursefilter);
- $r->print('');
+ %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('