--- loncom/interface/lonwishlist.pm 2010/08/17 12:32:58 1.5
+++ loncom/interface/lonwishlist.pm 2011/01/27 14:38:44 1.9
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
-# Routines to control the wishlist
+# Utility-routines for wishlist
#
-# $Id: lonwishlist.pm,v 1.5 2010/08/17 12:32:58 wenzelju Exp $
+# $Id: lonwishlist.pm,v 1.9 2011/01/27 14:38:44 wenzelju Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -39,12 +39,13 @@ It is only available for user with acces
The wishlist-module uses the CPAN-module "Tree" for easily handling the directory-structure of the wishlist. Each node in the tree has an index to be referenced by.
+=back
+
=cut
package Apache::lonwishlist;
use strict;
-use Apache::Constants qw(:common);
use Apache::lonnet;
use Apache::loncommon();
use Apache::lonhtmlcommon;
@@ -61,6 +62,7 @@ my %TreeToHash;
my @allFolders;
my @allNodes;
my $indentConst = 20;
+my $foldersOption;
=pod
@@ -70,7 +72,7 @@ my $indentConst = 20;
=item * &getWishlist()
- Get the wishlist-data via lonnet::dump() and returns the got data in a hash.
+ Get the wishlist-data via lonnet::getkeys() and lonnet::get() and returns the got data in a hash.
=item * &putWishlist(wishlist)
@@ -90,14 +92,18 @@ my $indentConst = 20;
# Read wishlist from user-data
sub getWishlist {
- my %wishlist = &Apache::lonnet::dump('wishlist');
+ my @keys = &Apache::lonnet::getkeys('wishlist');
+ my %wishlist = &Apache::lonnet::get('wishlist',\@keys);
foreach my $i ( keys %wishlist) {
#File not found. This appears at the first time using the wishlist
#Create file and put 'root' into it
if ($i =~m/^error:No such file/) {
&Apache::lonnet::logthis($i.'! Create file by putting in the "root" of the directory tree.');
&Apache::lonnet::put('wishlist', {'root' => ''});
- %wishlist = &Apache::lonnet::dump('wishlist');
+ my $options = '';
+ &Apache::lonnet::put('wishlist', {'folders' => $options});
+ @keys = &Apache::lonnet::getkeys('wishlist');
+ %wishlist = &Apache::lonnet::get('wishlist',\@keys);
}
elsif ($i =~ /^(con_lost|error|no_such_host)/i) {
&Apache::lonnet::logthis('ERROR while attempting to get wishlist: '.$i);
@@ -105,7 +111,7 @@ sub getWishlist {
}
}
- # if we got no keys in hash returned by dump(), return error.
+ # if we got no keys in hash returned by get(), return error.
# wishlist will not be loaded, instead the user will be asked to try again later
if ((keys %wishlist) == 0) {
&Apache::lonnet::logthis('ERROR while attempting to get wishlist: no keys retrieved!');
@@ -183,7 +189,11 @@ sub deleteWishlist {
# Create a new entry
sub newEntry() {
- my ($title, $path, $note) = @_;
+ my ($rootgiven, $title, $path, $note) = @_;
+
+ $root = $rootgiven;
+ @childrenRt = $root->children();
+
my $date = gmtime();
# Create Entry-Object
my $entry = Entry->new(title => $title, path => $path, note => $note, date => $date);
@@ -194,53 +204,69 @@ sub newEntry() {
if ($folderIndex ne '') {
@allFolders = ();
&getFoldersToArray(\@childrenRt);
- my $folderToInsertOn = &Tree::getNodeByIndex($folderIndex,\@allFolders);
+ my $folderToInsertOn = &Apache::Tree::getNodeByIndex($folderIndex,\@allFolders);
$folderToInsertOn->add_child($tree);
}
else {
$root->add_child($tree);
}
- &saveChanges();
+ return &saveChanges();
}
# Delete entries
sub deleteEntries {
+ my $rootgiven = shift;
my $marked = shift;
- &getNodesToArray(\@childrenRt);
+ $root = $rootgiven;
+ @childrenRt = $root->children();
+
+ &getNodesToArray(\@childrenRt);
foreach my $m (@$marked) {
- my $found = &Tree::getNodeByIndex($m, \@allNodes);
- &Tree::removeNode($found);
+ my $found = &Apache::Tree::getNodeByIndex($m, \@allNodes);
+ # be sure, that entry exists (may have been deleted before, e.g. in an other browsertab)
+ if (defined $found) {
+ &Apache::Tree::removeNode($found);
+ }
}
@allNodes = ();
- &saveChanges();
+ return &saveChanges();
}
# Sort entries
sub sortEntries {
+ my $rootgiven = shift;
my $indexNode = shift;
my $at = shift;
+
+ $root = $rootgiven;
+ @childrenRt = $root->children();
&getNodesToArray(\@childrenRt);
- my $foundNode = &Tree::getNodeByIndex($indexNode, \@allNodes);
+ my $foundNode = &Apache::Tree::getNodeByIndex($indexNode, \@allNodes);
- &Tree::moveNode($foundNode,$at,undef);
+ &Apache::Tree::moveNode($foundNode,$at,undef);
@allNodes = ();
+ return &saveChanges();
}
# Move entries
sub moveEntries {
+ my $rootgiven = shift;
my $indexNodesToMove = shift;
my $indexParent = shift;
my @nodesToMove = ();
+ $root = $rootgiven;
+ @childrenRt = $root->children();
+
# get all nodes that should be moved
&getNodesToArray(\@childrenRt);
foreach my $index (@$indexNodesToMove) {
- my $foundNode = &Tree::getNodeByIndex($index, \@allNodes);
+ my $foundNode = &Apache::Tree::getNodeByIndex($index, \@allNodes);
push(@nodesToMove, $foundNode);
}
@@ -256,36 +282,46 @@ sub moveEntries {
}
if (!$parentIsIn) {
if ($indexParent ne "root") {
- $foundParent = &Tree::getNodeByIndex($indexParent, \@allNodes);
- &Tree::moveNode($node,undef,$foundParent);
+ $foundParent = &Apache::Tree::getNodeByIndex($indexParent, \@allNodes);
+ &Apache::Tree::moveNode($node,undef,$foundParent);
}
else {
- &Tree::moveNode($node,undef,$root);
+ &Apache::Tree::moveNode($node,undef,$root);
}
}
}
@allNodes = ();
+ return &saveChanges();
}
# Set a new title for an entry
sub setNewTitle {
- my ($nodeindex, $newTitle) = @_;
+ my ($rootgiven, $nodeindex, $newTitle) = @_;
+
+ $root = $rootgiven;
+ @childrenRt = $root->children();
+
&getNodesToArray(\@childrenRt);
- my $found = &Tree::getNodeByIndex($nodeindex, \@allNodes);
+ my $found = &Apache::Tree::getNodeByIndex($nodeindex, \@allNodes);
$found->value()->title($newTitle);
@allNodes = ();
+ return &saveChanges();
}
# Set a new path for an entry
sub setNewPath {
- my ($nodeindex, $newPath) = @_;
+ my ($rootgiven, $nodeindex, $newPath) = @_;
+
+ $root = $rootgiven;
+ @childrenRt = $root->children();
+
&getNodesToArray(\@childrenRt);
- my $found = &Tree::getNodeByIndex($nodeindex, \@allNodes);
+ my $found = &Apache::Tree::getNodeByIndex($nodeindex, \@allNodes);
if ($found->value()->path()) {
$found->value()->path($newPath);
- return 1;
+ return &saveChanges();
}
@allNodes = ();
return 0;
@@ -294,23 +330,29 @@ sub setNewPath {
# Set a new note for an entry
sub setNewNote {
- my ($nodeindex, $newNote) = @_;
+ my ($rootgiven, $nodeindex, $newNote) = @_;
+
+ $root = $rootgiven;
+ @childrenRt = $root->children();
+
&getNodesToArray(\@childrenRt);
- my $found = &Tree::getNodeByIndex($nodeindex, \@allNodes);
+ my $found = &Apache::Tree::getNodeByIndex($nodeindex, \@allNodes);
$found->value()->note($newNote);
@allNodes = ();
+ return &saveChanges();
}
# Save all changes
sub saveChanges {
@childrenRt = $root->children();
- &Tree::TreeIndex(\@childrenRt);
- &Tree::setCountZero();
- &Tree::RootToHash(\@childrenRt);
- &Tree::TreeToHash(\@childrenRt);
+ &Apache::Tree::TreeIndex(\@childrenRt);
+ &Apache::Tree::setCountZero();
+ &Apache::Tree::RootToHash(\@childrenRt);
+ &Apache::Tree::TreeToHash(\@childrenRt);
&deleteWishlist();
&putWishlist(\%TreeToHash);
+ return $root;
}
@@ -327,11 +369,6 @@ sub saveChanges {
Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).
-=item * &getfoldersOption()
-
- Returns the option-tag build by &getFoldersForOption(nodes). Use it to transfer this to other modules (e.g. lonmenu.pm).
-
-
=item * &getFoldersToArray(children)
Puts all nodes that represent folders in the wishlist into an array.
@@ -344,7 +381,7 @@ sub saveChanges {
Recursive call starting with all children of the root of the tree (parameter nodes is reference to an array containing the nodes of the current level).
- =back
+=back
=cut
@@ -352,7 +389,6 @@ sub saveChanges {
# Return the names for all exiting folders in option-tags, so
# a new link or a new folder can be created in an existing folder
my $indent = 0;
-my $foldersOption;
sub getFoldersForOption {
my $nodes = shift;
@@ -373,22 +409,6 @@ sub getFoldersForOption {
}
-sub getfoldersOption {
- if (&getWishlist ne 'error') {
- %TreeHash = &getWishlist();
- $root = &Tree::HashToTree();
- @childrenRt = $root->children();
- &getFoldersForOption(\@childrenRt);
- my $options = ''.$foldersOption;
- $foldersOption = '';
- return $options;
- }
- else {
- return '';
- }
-}
-
-
# Put all folder-nodes to an array
sub getFoldersToArray {
my $children = shift;
@@ -565,7 +585,8 @@ sub JSforWishlist {
my $warningFolder = &mt('You must insert a title!');
my $warningDelete = &mt('Are you sure you want to delete the selected entries? Deleting a folder also deletes all entries within this folder!');
my $warningSave = &mt('You have unsaved changes. You can either save these changes now by clicking "ok" or click "cancel" if you do not want to save your changes.');
- my $warningMove = &mt('You must select a destination folder!');
+ my $warningMoveS = &mt('You must select at minimum one entry to move!');
+ my $warningMoveD = &mt('You must select a destination folder!');
$foldersOption = '';
my $js = &Apache::lonhtmlcommon::scripttag(<children();
+
# breadcrumbs and start_page
&Apache::lonhtmlcommon::clear_breadcrumbs();
&Apache::lonhtmlcommon::add_breadcrumb(
@@ -1430,7 +1508,7 @@ sub makePage {
'onload' => 'javascript:onLoadAction('."'".$mode."'".');',
'onunload' => 'javascript:window.name = '."'loncapaclient'"}});
- my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs('Wishlist '.&Apache::loncommon::help_open_topic('Wishlist'));
+ my $breadcrumbs = &Apache::lonhtmlcommon::breadcrumbs(&mt('Wishlist').&Apache::loncommon::help_open_topic('Wishlist'));
# get javascript-code for wishlist-interactions
my $js = &JSforWishlist();
@@ -1573,6 +1651,103 @@ sub makePage {
}
+# Returns the HTML-Markup for the PopUp, shown when a new link should set, when NOT
+# beeing in the wishlist-interface (method is called in lonmenu and lonsearchcat)
+sub makePopUpNewLink{
+
+ # Get all existing folders to offer posibility to set a new link
+ # into a folder
+ my %TreeHashLink = &Apache::lonwishlist::getWishlist();
+ my $rootLink = &Apache::Tree::HashToTree(\%TreeHashLink);
+ my @childrenRtLink = $rootLink->children();
+
+ $foldersOption = '';
+ @allFolders = ();
+ &getFoldersToArray(\@childrenRtLink);
+ &getFoldersForOption(\@childrenRtLink);
+
+ my $options = ''.$foldersOption;
+ $foldersOption = '';
+ @allFolders = ();
+
+ # HTML-Markup for 'Set a link for this resource to wishlist'
+ # this is written via JavaScript document.write (function set_wishlistlink)
+ # it is split into 3 parts and the inputfields for title and path are left out
+ # these fields are inserted later to set the values for title and path
+ # automatically via JavaScript (document.title and location.pathname)
+
+ my $start_page_wishlistlink =
+ &Apache::loncommon::start_page('Set link to wishlist',undef,
+ {'only_body' => 1,
+ 'js_ready' => 1,
+ 'bgcolor' => '#FFFFFF',});
+
+ my $warningLink = &mt('You must insert a title!');
+
+ my $in_page_wishlistlink1 = '
'.&mt('Set a link to wishlist').'
'.
+ '';
+ $options = '';
+
+ # remove all \n for inserting on javascript document.write
+ $in_page_wishlistlink1 =~ s/\n//g;
+ $in_page_wishlistlink2 =~ s/\n//g;
+ $in_page_wishlistlink3 =~ s/\n//g;
+
+ my $end_page_wishlistlink =
+ &Apache::loncommon::end_page({'js_ready' => 1});
+
+ # Add JavaScript-function to set link for a ressource to wishlist
+ my $js.=<