# The LearningOnline Network with CAPA # Routines to control the wishlist # # $Id: lonwishlist.pm,v 1.1 2010/08/10 14:30:20 wenzelju Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). # # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # LON-CAPA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LON-CAPA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt # # http://www.lon-capa.org/ # package Apache::lonwishlist; use strict; use Apache::Constants qw(:common); use Apache::lonnet; use Apache::loncommon(); use Apache::lonhtmlcommon; use Apache::lonlocal; use Tree; # Global variables my $root; my @childrenRt; my %TreeHash; my %TreeToHash; my @allFolders; my @allNodes; my $indentConst = 20; # Read wishlist from user-data sub getWishlist { my @wishlistkeys = &Apache::lonnet::getkeys('wishlist'); my %wishlist = &Apache::lonnet::get('wishlist',\@wishlistkeys); foreach my $i (%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' => ''}); @wishlistkeys = &Apache::lonnet::getkeys('wishlist'); %wishlist = &Apache::lonnet::get('wishlist',\@wishlistkeys); } elsif ($i =~ /^(con_lost|error|no_such_host)/i) { &Apache::lonnet::logthis('ERROR while attempting to get wishlist: '.$i); return 'error'; } } #If hash is empty, put 'root' into it, so we got a node to start the tree if ((keys %wishlist) == 0) { &Apache::lonnet::logthis('ERROR while attempting to get wishlist: no keys retrieved!'); return 'error'; } return %wishlist; } # Write wishlist to user-data sub putWishlist { my $wishlist = shift; &Apache::lonnet::put('wishlist',$wishlist); } # Removes all existing entrys for wishlist in user-data sub deleteWishlist { my @wishlistkeys = &Apache::lonnet::getkeys('wishlist'); my %wishlist = &Apache::lonnet::del('wishlist',\@wishlistkeys); } # Create a new entry sub newEntry() { my ($title, $path, $note) = @_; my $date = gmtime(); # Create Entry-Object my $entry = Entry->new(title => $title, path => $path, note => $note, date => $date); # Create Tree-Object, this correspones a node in the wishlist-tree my $tree = Tree->new($entry); # Add this node to wishlist-tree my $folderIndex = $env{'form.folders'}; if ($folderIndex ne '') { @allFolders = (); &getFoldersToArray(\@childrenRt); my $folderToInsertOn = &Tree::getNodeByIndex($folderIndex,\@allFolders); $folderToInsertOn->add_child($tree); } else { $root->add_child($tree); } &saveChanges(); } # Delete entries sub deleteEntries { my $marked = shift; &getNodesToArray(\@childrenRt); foreach my $m (@$marked) { my $found = &Tree::getNodeByIndex($m, \@allNodes); &Tree::removeNode($found); } @allNodes = (); &saveChanges(); } # Sort entries sub sortEntries { my $indexNode = shift; my $at = shift; &getNodesToArray(\@childrenRt); my $foundNode = &Tree::getNodeByIndex($indexNode, \@allNodes); &Tree::moveNode($foundNode,$at,undef); @allNodes = (); } # Move entries sub moveEntries { my $indexNodesToMove = shift; my $indexParent = shift; my @nodesToMove = (); &getNodesToArray(\@childrenRt); foreach my $index (@$indexNodesToMove) { my $foundNode = &Tree::getNodeByIndex($index, \@allNodes); push(@nodesToMove, $foundNode); } foreach my $node (@nodesToMove) { my $foundParent; my $parentIsIn = 0; foreach my $n (@nodesToMove) { if ($node->parent()->value() ne "root") { if ($node->parent()->value()->nindex() == $n->value()->nindex()) { $parentIsIn = 1; } } } if (!$parentIsIn) { if ($indexParent ne "root") { $foundParent = &Tree::getNodeByIndex($indexParent, \@allNodes); &Tree::moveNode($node,undef,$foundParent); } else { &Tree::moveNode($node,undef,$root); } } } @allNodes = (); } # Set a new title for an entry sub setNewTitle { my ($nodeindex, $newTitle) = @_; &getNodesToArray(\@childrenRt); my $found = &Tree::getNodeByIndex($nodeindex, \@allNodes); $found->value()->title($newTitle); @allNodes = (); } # Set a new note for an entry sub setNewNote { my ($nodeindex, $newNote) = @_; &getNodesToArray(\@childrenRt); my $found = &Tree::getNodeByIndex($nodeindex, \@allNodes); $found->value()->note($newNote); @allNodes = (); } # Save all changes sub saveChanges { @childrenRt = $root->children(); &Tree::TreeIndex(\@childrenRt); &Tree::setCountZero(); &Tree::RootToHash(\@childrenRt); &Tree::TreeToHash(\@childrenRt); &deleteWishlist(); &putWishlist(\%TreeToHash); } # 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; foreach my $n (@$nodes) { if ($n->value()->path() eq '') { $foldersOption .= ''; my @children = $n->children(); if ($#children >=0) { $indent += 10; &getFoldersForOption(\@children); $indent -= 10; } } } } 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; foreach my $c (@$children) { if ($c->value()->path() eq '') { push(@allFolders,$c); } my @newchildren = $c->children(); if ($#newchildren >= 0) { &getFoldersToArray(\@newchildren); } } } # Put all nodes to an array sub getNodesToArray { my $children = shift; foreach my $c (@$children) { push(@allNodes,$c); my @newchildren = $c->children(); if ($#newchildren >= 0) { &getNodesToArray(\@newchildren); } } } # Return a script-tag containing Javascript-function # needed for wishlist actions like 'new link' ect. sub JSforWishlist { my $startPagePopup = &Apache::loncommon::start_page('Wishlist',undef, {'only_body' => 1, 'js_ready' => 1, 'bgcolor' => '#FFFFFF',}); my $endPagePopup = &Apache::loncommon::end_page({'js_ready' => 1}); @allFolders = (); &getFoldersToArray(\@childrenRt); &getFoldersForOption(\@childrenRt); # texthash my %lt = &Apache::lonlocal::texthash( 'nl' => 'New Link', 'nf' => 'New Folder', 'lt' => 'Link Title', 'ft' => 'Folder Title', 'pa' => 'Path', 'nt' => 'Note', 'si' => 'Save in', 'cl' => 'Cancel'); my $inPageNewLink = '

'.$lt{'nl'}.'

'. '
'. &Apache::lonhtmlcommon::start_pick_box(). &Apache::lonhtmlcommon::row_title($lt{'lt'}). ''. &Apache::lonhtmlcommon::row_closure(). &Apache::lonhtmlcommon::row_title($lt{'pa'}). ''. &Apache::lonhtmlcommon::row_closure(). &Apache::lonhtmlcommon::row_title($lt{'nt'}). ''. &Apache::lonhtmlcommon::row_closure(1). &Apache::lonhtmlcommon::end_pick_box(). '

'. ''. ''. ''. '
'; my $inPageNewFolder = '

'.$lt{'nf'}.'

'. '
'. &Apache::lonhtmlcommon::start_pick_box(). &Apache::lonhtmlcommon::row_title($lt{'ft'}). '
'. &Apache::lonhtmlcommon::row_closure(). &Apache::lonhtmlcommon::row_title($lt{'nt'}). '
'. &Apache::lonhtmlcommon::row_closure(1). &Apache::lonhtmlcommon::end_pick_box(). '

'. ''. ''. ''. '
'; # Remove all \n for inserting on javascript document.write $inPageNewLink =~ s/\n//g; $inPageNewFolder =~ s/\n//g; my $warningLink = &mt('You must insert a title and a path!'); 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!'); $foldersOption = ''; my $js = &Apache::lonhtmlcommon::scripttag(<' +'function newlinksubmit(){' +'var path = document.getElementsByName("path")[0].value;' +'var title = document.getElementsByName("title")[0].value;' +'if (!path || !title) {' +'alert("$warningLink");' +'return false;}' +'else {' +'window.close();' +'return true;}}' +'<\/scr'+'ipt>' +'$inPageNewLink' +'$endPagePopup'); newlinkWin.document.close(); } function newFolder() { newfolderWin=window.open('','newfolderWin','width=580,height=270, scrollbars=yes'); newfolderWin.document.write('$startPagePopup' +'