#!/home/rebelsky/perl/bin/perl
#This module contains subroutines that will allow a Ravel plugin (and 
#maybe Ravel itself) to keep track of the last time a user has viewed a 
#particular page on the Web.
#(POD documentation at end)

##################
# Package Begins #
##################
#package ID
package History;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(history lessThan replyTest);

#Import modules
use Network;
use AFile;
use Searchslash;

###########################################################################
# history #
###########################################################################
# This subroutine is called when a web page is accessed.  It determines
# the last time the user viewed that web page and makes all necessary
# changes to the history to show that it was just accessed.
# ARGUMENTS: The name of the user in string form and the url of the page
# being accessed in string form.
# RETURNS: It returns the time that the page was last accessed in the
# following form "month/day/year|hour:minute:second".  If the page was
# never accessed by the user, all values are 0.
###########################################################################
sub history
  {
    #read in arguments
    my $name = shift;           #the user's name
    my $url = shift;            #the url of the page being checked
    #other variables
    my $info;                   #data from the history file in string form
    my @info;                   #data from the history file in array form
    my $lasttime;               #time at which the page was last accessed
    my $currenttime;            #the current time in string form
    my @currenttime;            #current time as returned by localtime
    my $newline;                #the newline at the end of the history line
    my $search_url;             #the url of the page in search form
    
    #get the current time
    @currenttime = localtime();
    
    #the month is 0 based so lets make it 1 based
    $currenttime[4] = $currenttime[4] + 1;
    
    #put the local time in the correct form
    $currenttime = $currenttime[4] . "/" . $currenttime[3] . "/" . $currenttime[5] . "\|" . $currenttime[2] . ":" . $currenttime[1] . ":" . $currenttime[0];
    
    #see if this user has a history
    if (fileExists("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/History/$name.txt"))
      {
        #get the data from the history file
        $info = readInFile("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/History/$name.txt");
        
        #turn the data into an array
        @info = split(/^/m,$info);
        
        #look at each line and see if that is the line for this url
        foreach $line (@info)
          {
            #turn the url into a search string
            $search_url = backslash($url);
            if ($line =~ /$search_url\|(.*)/i)
              {
                #save the last time that the page was looked at and then 
                #replace that line in the history with the new current 
                #time 
                $newline = chomp($lasttime = $1);
                if ($newline == 0)
                  {
                    $newline = "";
                  }             #if ($newline == 0)
                else
                  {
                    $newline = "\n";
                  }             #else
                $line =~ s/$search_url\|(.*)/$url\|$currenttime$newline/i;
                last;
              }                 #if ($line =~ /$url\|(.*)(\n)?/i)
            else
              {
                next;
              }                 #else
          }                     #foreach $line (@info)
        
        #if the url was not found...add it
        if ($lasttime eq "")
          {
            #make sure to set the lasttime the page was viewed to all 
            #zeros since it was never viewed
            $lasttime = "0/0/0\|0:0:0";
            $info = $info . "\n$url\|$currenttime";
            
            #get rid of extra white spaces
            while ($info =~ /\n\s/i)
              {
                $info =~ s/\n(\s)+/\n/gi;
              }                 #while ($info =~ /\n\s/i)
            
            writeFile("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/History/$name.txt",$info);
          }                     #if ($lasttime eq "")
        else
          {
            #put the history file back into string form
            $info = "@info";
            
            #remove extra white spaces
            while ($info =~ /\n\s/i)
              {
                $info =~ s/\n(\s)+/\n/gi;
              }                 #while ($info =~ /\n\s/i)
            
            #rewrite the history file with the changed line
            writeFile("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/History/$name.txt",$info);
          }                     #else
      }                         #if (fileExists(".../$name.txt"))
    #create the history file with this url in it
    else
      {
        #make sure to set the lasttime the page was viewed to all zeros 
        #since it was never viewed
        $lasttime = "0/0/0\|0:0:0";
        writeFile("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/History/$name.txt","$url\|$currenttime");
      }                         #else
    
    #return the lasttime the page was viewed by this user
    return $lasttime;
  }                             #sub history

###########################################################################
# lessThan #
###########################################################################
# This subroutine determines if the first time is less than the second 
# time.
# ARGUMENTS: It takes the first time in string form as returned by
# History->history and the second time in the same format.
# RETURNS: It returns 1 if the first time is less than the second, it
# returns 0 if the first time is greater than the second, and it returns
# "equal" if the two times are equal.
###########################################################################
sub lessThan
  {
    #read in arguments
    my $first = shift;          #the first time to check
    my $second = shift;         #the second time to check
    #other variables
    my $year1;                  #the year of the first time
    my $year2;                  #the year of the second time
    my $mon1;                   #the month of the first time
    my $mon2;                   #the month of the second time
    my $day1;                   #the day of the first time
    my $day2;                   #the day of the second time
    my $hour1;                  #the hour of the first time
    my $hour2;                  #the hour of the second time
    my $min1;                   #the minute of the first time
    my $min2;                   #the minute of the second time
    my $sec1;                   #the second of the first time
    my $sec2;                   #the second of the second time
    
    #break up the two arguments
    $first =~ /(.*?)\/(.*?)\/(.*?)\|(.*?):(.*?):(.*)/i;
    $mon1 = $1;
    $day1 = $2;
    $year1 = $3;
    $hour1 = $4;
    $min1 = $5;
    chomp($sec1 = $6);
    $second =~ /(.*?)\/(.*?)\/(.*?)\|(.*?):(.*?):(.*)/i;
    $mon2 = $1;
    $day2 = $2;
    $year2 = $3;
    $hour2 = $4;
    $min2 = $5;
    chomp($sec2 = $6);
    
    #compare years
    if ($year1 < $year2)
      {
        #return true
        return 1;
      }                         #if ($year1 < $year2)
    elsif ($year2 < $year1)
      {
        #return false
        return 0;
      }                         #elsif ($year2 < $year1)
    
    #compare months
    elsif ($mon1 < $mon2)
      {
        #return true
        return 1;
      }                         #elsif ($mon1 < $mon2)
    elsif ($mon2 < $mon1)
      {
        #return false
        return 0;
      }                         #elsif ($mon2 < $mon1)
    
    #compare days
    elsif ($day1 < $day2)
      {
        #return true
        return 1;
      }                         #elsif ($day1 < $day2)
    elsif ($day2 < $day1)
      {
        #return false
        return 0;
      }                         #elsif ($day2 < $day1)
    
    #compare hours
    elsif ($hour1 < $hour2)
      {
        #return true
        return 1;
      }                         #elsif ($hour1 < $hour2)
    elsif ($hour2 < $hour1)
      {
        #return false
        return 0;
      }                         #elsif ($hour2 < $hour1)
    
    #compare minutes
    elsif ($min1 < $min2)
      {
        #return true
        return 1;
      }                         #elsif ($min1 < $min2)
    elsif ($min2 < $min1)
      {
        #return false
        return 0;
      }                         #elsif ($min2 < $min1)
    
    #compare seconds
    elsif ($sec1 < $sec2)
      {
        #return true
        return 1;
      }                         #elsif ($sec1 < $sec2)
    elsif ($sec2 < $sec1)
      {
        #return false
        return 0;
      }                         #elsif ($sec2 < $sec1)
    
    #the times must be equal
    else
      {
        #return that they are equal
        return "equal";
      }                         #else
  }                             #sub compare

###########################################################################
# replyTest #
###########################################################################
# This test checks to see if any replies to an annotation were made since
# the user last looked at the page.
# ARGUMENTS: It takes the last time that the user viewed that page in
# string form as returned by History->history and the file address for the
# annotation that is to be checked.
# RETURNS: Returns 1 if there is a new reply and 0 if there is not.
###########################################################################
sub replyTest
  {
    #read in arguments
    my $lasttime = shift;       #the last time this user viewed this page
    my $file = shift;           #address of file to check replies of
    my $name = shift;           #the name of the user
    my $groups = shift;         #the groups that the user is in
    #other variables
    local(*fields);             #the hash to store the annotation data in
    my $new;                    #boolean: has this annotation been seen?
    my $found = 0;              #boolean: is there a new reply
    my $permission;             #boolean: does the user have permissions
    my @user_groups;            #the groups of the user
    my @annotation_groups;      #the groups of the annotation
    
    #remove .txt from the file address
    $file =~ s/(.*)\.txt/$1/i;
    
    #check each reply time to see if it is a new reply since last viewing
    foreach $reply (<$file*.r>)
      {
        #read in the annotation file using AFile->readFile
        readFile(*fields,$reply);
        
        #determine whether this is an annotation that the user has not seen
        $new = lessThan($lasttime,substr($fields{'date'},0,2) . "/" . substr($fields{'date'},2,2) . "/" . substr($fields{'date'},4,2) . "\|" . $fields{'time'});
        
        #if it was new...
        if (($new == 1) || ($new eq "equal"))
          {
            #does the user have permission to view the annotation?
            #remove any extra spaces from the permission field
            $fields{'protection'} =~ s/^(\s*)//i;
            $fields{'protection'} =~ s/(\s*)$//i;
            #are the permissions correct
            if (($fields{'author'} eq $name) || ($name eq "sysadmin"))
              {
                $permission = 1;
              }                 #if ($fields{'author'} eq $name)
            elsif ($fields{'protection'} eq "private")
              {
                $permission = 0;
              }                 #elsif ($fields{'protection'} eq "private")
            elsif ($fields{'protection'} eq "public")
              {
                $permission = 1;
              }                 #elsif ($fields{'protection'} eq "public")
            else
              {
                $permission = 0;
                #if this is not the author, and the permissions are not 
                #public or private, then we need to check group information
                @user_groups = split(/\,/,$groups);
                @annotation_groups = split(/\,/,$fields{'protection'});
                foreach $annotation_group (@annotation_groups)
                  {
                    #make sure that the group name does not end or begin 
                    #with extra spaces
                    $annotation_group =~ s/^(\s*)//i;
                    $annotation_group =~ s/(\s*)$//i;
                    foreach $user_group (@user_groups)
                      {
                        #make sure that the group name does not end or 
                        #begin with extra spaces
                        $user_group =~ s/^(\s*)//i;
                        $user_group =~ s/(\s*)$//i;
                        if ($annotation_group eq $user_group)
                          {
                            $permission = 1;
                          }     #if ($annotation_group eq $user_group)
                      }         #foreach $user_group (@user_groups)
                  }             #foreach ...(@annotation_groups)
              }                 #else
            
            #if they had permission to view it
            if ($permission == 1)
              {
                #we found a new reply
                $found = 1;
                
                #don't check any more replies
                last;
              }                 #if ($permission == 1)
            #if they didn't have permission to view the reply
            else
              {
                #check the next reply
                next;
              }                 #else
          }                     #if (($new == 1) || ($new eq "equal"))
        #if it was already seen...
        else
          {
            #go on to the next reply
            next;
          }                     #else
      }                         #foreach $reply (<$file_*.r>)
    
    #return whether a new reply was found or not
    return $found;
  }                             #sub replyTest

#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Package

History.pm

=item Author

Rachel Heck

=item Description

This module contains subroutines that will allow a Ravel plugin (and 
maybe Ravel itself) to keep track of the last time a user has viewed a 
particular page on the Web.

=back

=head1 Subroutines

=over 4

=item history

This subroutine is called when a web page is accessed.  It determines the 
last time the user viewed that web page and makes all necessary changes to
the history to show that it was just accessed.

=item lessThan

This subroutine determines if the first time is less than the second time.

=item replyTest

This test checks to see if any replies to an annotation were made since the
user last looked at the page.

=back

=head1 History

=over 4

=item [20 July 1999]

Began coding.  Subroutines history and lessThan are working and are fully 
commented.  Subroutine replyTest is working.

=item [21 July 1999]

replyTest has been fully commented.  replyTest also now checks permissions 
before it says that there is a new reply.  If the user does not have 
permission to view the reply then it does not count.

=item [22 July 1999]

The url is now turned into a search string before using it to search the 
history file. 

=item [27 July 1999]

The sysadmin user can view all annotations, including private annotations.
This should be told to all users. 

=back

=cut

