[LON-CAPA-cvs] cvs: modules /gerd/harvesting lonrecommender.pm
www
www at source.lon-capa.org
Wed Jul 18 19:42:21 EDT 2012
www Wed Jul 18 23:42:21 2012 EDT
Modified files:
/modules/gerd/harvesting lonrecommender.pm
Log:
Cleanup
Index: modules/gerd/harvesting/lonrecommender.pm
diff -u modules/gerd/harvesting/lonrecommender.pm:1.25 modules/gerd/harvesting/lonrecommender.pm:1.26
--- modules/gerd/harvesting/lonrecommender.pm:1.25 Wed Jul 18 20:37:44 2012
+++ modules/gerd/harvesting/lonrecommender.pm Wed Jul 18 23:42:21 2012
@@ -5,7 +5,7 @@
#
# MODIFY $datapath VARIABLE FOR LOCATION OF DATA FILES
#
-# $Id: lonrecommender.pm,v 1.25 2012/07/18 20:37:44 www Exp $
+# $Id: lonrecommender.pm,v 1.26 2012/07/18 23:42:21 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -107,7 +107,7 @@
}
untie(%hash);
foreach my $key (@with) { delete($output{$key}); }
- return ($max,%output);
+ return (\%output,$max);
}
# =================================================================================
@@ -115,14 +115,14 @@
# Return a list of recommendation-weighted resource IDs
sub sorted_list {
- my ($list, at baseids)=@_;
- my ($maxassoc,%associations)=&associated(@baseids);
- my ($maxcount,%counts)=&counts(@$list);
+ my ($baseids, at list)=@_;
+ my ($associations,$maxassoc)=&associated(values(%$baseids));
+ my ($maxcount,%counts)=&counts(@list);
my @newlist=sort {
- $associations{$b}/$maxassoc+$counts{$b}/$maxcount
- <=> $associations{$a}/$maxassoc+$counts{$a}/$maxcount
- } (@$list);
+ $$associations{$b}/$maxassoc+$counts{$b}/$maxcount
+ <=> $$associations{$a}/$maxassoc+$counts{$a}/$maxcount
+ } (@list);
return @newlist;
}
@@ -272,7 +272,7 @@
if ($minlevel<2) {
untie(%taxo1);
}
- return %output;
+ return \%output;
}
# =================================================================================
@@ -315,46 +315,33 @@
}
}
untie(%hash);
- my @fittingids=keys(%output);
- return &sorted_list(\@fittingids,@$baseids);
-}
-
-
-# =================================================================================
-# Return sorted list of associated IDs
-#
-
-sub associated_ids {
- my ($filter,$baseids, at urlids)=@_;
- my ($max,%output)=&associated(@urlids);
- my @fittingids=keys(%output);
- return &sorted_list(\@fittingids,@$baseids);
+ return \%output;
}
# =================================================================================
#
-# Filter by resource type
+# Filter by resource type and if already existing
# Take search string and hash of resids
-# Return filtered hash
#
-sub type_filter {
- my ($skey,%urls)=@_;
- my %output=();
+sub filter {
+ my ($skey,$selectedids,$urls)=@_;
$skey=~s/^\W+//gs;
$skey=~s/\W+$//gs;
$skey=~s/\W+/\|/gs;
$skey=lc($skey);
my $pattern=qr/$skey/;
my %hash=();
- tie(%hash, 'GDBM_File', $datapath.'dbfiles/res_types.db',&GDBM_READER(),0640);
- foreach my $key (keys(%urls)) {
- if ($urls{$key}=~$pattern) {
- $output{$key}=$urls{$key};
+ if ($skey) { tie(%hash, 'GDBM_File', $datapath.'dbfiles/res_types.db',&GDBM_READER(),0640); }
+ foreach my $key (keys(%$urls)) {
+ if ($$selectedids{$key}) { delete($$urls{$key}); }
+ if ($skey) {
+ if ($hash{$key}!~$pattern) {
+ delete($$urls{$key});
+ }
}
}
- untie(%hash);
- return(%output);
+ if ($skey) { untie(%hash); }
}
# =================================================================================
@@ -374,6 +361,23 @@
}
# =================================================================================
+# Return the resource keys for URLs
+# keeping them in order. The dbfile is double-hashed
+
+sub urlres_array {
+ my @urls=@_;
+ my @output=();
+ my %hash=();
+ tie(%hash, 'GDBM_File', $datapath.'dbfiles/url.db',&GDBM_READER(),0640);
+ foreach my $url (@urls) {
+ push(@output,$hash{$url});
+ }
+ untie(%hash);
+ return(@output);
+}
+
+
+# =================================================================================
# Return the access counts for resource IDs
sub counts {
@@ -453,6 +457,15 @@
}
# =================================================================================
+# Display a filtered, sorted, list
+
+sub display {
+ my ($r,$taxonomy_categories,$selectedids,$filter,$lower,$interval,$idhash)=@_;
+ &filter($filter,$selectedids,$idhash);
+ &display_list($r,0,$taxonomy_categories,&urlres_array((&sorted_list($selectedids,keys(%$idhash)))[$lower..$lower+$interval-1]));
+}
+
+# =================================================================================
# Display a list of URLs
#
@@ -549,8 +562,7 @@
#
sub list_nextlevel_cats {
- my ($cmd,$taxocats)=@_;
- my ($current)=($cmd=~/^taxo\:(.+)$/);
+ my ($current,$taxocats)=@_;
my $output='<b>'.$current.' ...</b><br />';
foreach my $key (sort(keys(%$taxocats))) {
if ($key=~/^count\_(.+)$/) {
@@ -568,18 +580,6 @@
}
# =================================================================================
-# Show prioritized list of taxonomy members
-#
-
-sub taxonomy_list {
- my ($cmd,$taxocats,$filter, at baseids)=@_;
- my ($current)=($cmd=~/^taxo\:(.+)$/);
- my %fittingids=&taxonomy_members($1);
- my @fittingids=keys(%fittingids);
- return &sorted_list(\@fittingids, at baseids);
-}
-
-# =================================================================================
# A submitting link
#
@@ -764,18 +764,16 @@
$r->print('<td>'.&list_toplevel_cats(\%taxonomy_categories).'</td>');
$r->print("\n</tr></table>");
# ------ End header row
- if (($cmd eq 'showbasket') || ($cmd=~/^rem\:/)) {
+ if (($cmd eq 'showbasket') || ($cmd=~/^(add|rem)\:/)) {
$r->print("\n<h1>".&mt("Current Cart")."</h1>\n");
&display_list($r,1,\%taxonomy_categories, at selectedurls);
- my %list=&urlres((&associated_ids($filter,\@baseids,values(%selectedids)))[$lower..$lower+19]);
- &display_list($r,0,\%taxonomy_categories,values(%list));
+ &display($r,\%taxonomy_categories,\%selectedids,$filter,$lower,20,&associated(values(%selectedids)));
} elsif ($cmd eq 'search') {
- my %list=&urlres((&keyword_search($filter,\@baseids,$env{'form.searchterm'}))[$lower..$lower+19]);
- &display_list($r,0,\%taxonomy_categories,values(%list));
- } elsif ($cmd=~/^taxo\:/) {
- $r->print(&list_nextlevel_cats($cmd,\%taxonomy_categories));
- my %list=&urlres((&taxonomy_list($cmd,\%taxonomy_categories,$filter, at baseids))[$lower..$lower+19]);
- &display_list($r,0,\%taxonomy_categories,values(%list));
+ &display($r,\%taxonomy_categories,\%selectedids,$filter,$lower,20,&keyword_search($env{'form.searchterm'}));
+ } elsif ($cmd=~/^taxo\:(.*)$/) {
+ my $taxocat=$1;
+ $r->print(&list_nextlevel_cats($taxocat,\%taxonomy_categories));
+ &display($r,\%taxonomy_categories,\%selectedids,$filter,$lower,20,&taxonomy_members($taxocat));
} elsif ($cmd=~/^dir\:(.*)$/) {
&show_dir_list($r,\%taxonomy_categories,$filter,$1);
} elsif ($cmd=~/^indiv\:/) {
@@ -785,8 +783,7 @@
$r->print('<iframe src="'.$url.'?inhibitmenu=yes" width="100%" height="50%"></iframe>'."\n");
my %urlid=&urlres($url);
if ($urlid{$url}) {
- my %list=&urlres((&associated_ids($filter,\@baseids,$urlid{$url}))[$lower..$lower+9]);
- &display_list($r,0,\%taxonomy_categories,values(%list));
+ &display($r,\%taxonomy_categories,\%selectedids,$filter,$lower,10,&associated($urlid{$url}));
}
}
}
More information about the LON-CAPA-cvs
mailing list