/* Stats on writing                */
/* Author : Philippe Van Der Gucht */
/* Date   : 8/8/1993               */
/* Version: 1.0                    */
/* FIDO   : 2:291/708.15           */

Address Spot
Options results

'requeststring TITLE "Select a user in current message-base"'
naam=RESULT
if naam == 'RESULT' then exit
'messagelist'
'include FROM "'naam'" NOREQ NOGUI'
'getnumselected'
totaal=RESULT
if totaal == 0 then do 
   'requestnotify PROMPT "Name not found."'
   exit
end
'messages'
'firstmessage'
'getfrom'
eerst_nm=RESULT
if naam ~= eerst_nm then 'nextmarked'
CR='0d'x
LF='0a'x
tel=0
einde=0
say totaal
'progressopen TITLE "Reading dates..."'
prg=RESULT
do while einde == 0
   'progressupdate' prg tel totaal
   res2=RC
   if res2 == 5 then do
      'progressclose' prg
      'excludeflag ALL'
      exit
   end
   'getdatewritten'
   datum.tel=RESULT
   tel=tel+1
   'nextmarked'
   einde=RC
end
'progressclose' prg
'excludeflag ALL'
address command 'Date >t:date.temp'
Datum=Get_current_date()
address command 'Delete >NIL: t:date.temp'
call Get_dates_right()
'requestresponse TITLE "Choose time-basis:" PROMPT " Day   - Stats of the last 7 days 'cr' Week  - Stats of the last 4 weeks" GADGETS "_Day|_Week|_Cancel"'
res2=RC 
select 
   when res2 == 1 then call Make_day_stats()
   when res2 == 2 then call Make_week_stats()
   otherwise exit
end
if (res2 == 1) | (res2 == 2) then do
   'requestresponse TITLE "Include result:" PROMPT " File    - Results will be saved in file 'cr' Message - Write result in message base" GADGETS "File|_Message|_Cancel"'
   res2=RC
   select 
      when res2 == 1 then call Save_to_file()
      when res2 == 2 then call Save_to_message()
      otherwise NOP
   end
end
exit

Save_to_file:
   'requestfile TITLE "Save file:" PATH "SYS:"'
   file_nm=RESULT
   address command 'rename t:graf.temp "'file_nm'"'
   address command 'Delete >NIL: t:graf.temp'
return

Save_to_message:
   'write FILE "t:graf.temp"'
   address command 'Delete >NIL: t:graf.temp'
return   

Make_week_stats:
   Datum=right(Datum,4) || substr(Datum,4,2) || left(Datum,2)
   teller=tel-1
   tel2=0
   Nw_datum=Datum
   do while (tel2 <= 30) & (teller >= 0) 
      Nw_datum=Check_date(Nw_datum)
      Nw_datum.tel2=Nw_datum
      C_datum.tel2=0
      do while Nw_datum == datum.teller
         teller=teller-1
         if teller < 0 then leave
         C_datum.tel2=C_datum.tel2+1
      end
      say C_datum.tel2 Nw_datum 
      tel2=tel2+1
      Nw_datum=Nw_datum-1
   end
   tel=0
   groot=0   
   do while (tel <= 30) & (tel <= tel2-1) 
      if C_datum.tel >= groot then groot=C_datum.tel
      tel=tel+1
   end
   say groot
   /* Verhoudingsgetal (R van 3!) naar 15 voor graph te verkleinen */
   index=15/groot
   tel=0
   do while (tel <= 30) & (tel <= tel2-1)
      graf.tel=round(C_datum.tel*index)
      tel=tel+1
   end
   call open(graph,'t:graf.temp','W')
   call Print_info()
   call writeln(graph,groot)
   tel3=15
   do while tel3 >= 0 
      call writech(graph,' | ')
      tel=0 
      spat=1
      do while (tel <= 30) & (tel <= tel2-1) 
         if graf.tel >= tel3 then do
            call writech(graph,copies(' ',2*spat-1) || '')
            spat=1
         end
         else spat=spat+1   
         tel=tel+1
      end
      tel3=tel3-1
      call writech(graph,LF)
   end
   call writeln(graph,' +' || copies('-',74))
   tel=0
   call writech(graph,'    ')
   do while (tel <= 30) & (tel <= tel2-1)
      call writech(graph,right(Nw_datum.tel,2))
      tel=tel+1
   end 
   call writeln(graph,' ')
   call writeln(graph,' ')
   call writeln(graph,naam 'wrote' totaal 'messages.')
   call writeln(graph,'Average messages/day:' round(totaal/(tel2-1)))
   call close(graph)  
return
     
Print_info:
   call writeln(graph,'       +-Info---------------------------------------------+')
   call writeln(graph,'       | Writing stats script by Philippe Van Der Gucht   |')
   call writeln(graph,'       +-----------------FIDO: 2:291/708.15---------------+')
   call writeln(graph,'       | The chart displays the amount of messages the    |')
   call writeln(graph,'       | writer wrote a particular day. Have fun! :)      |')
   call writeln(graph,'       +--------------------------------------------------+')
   call writeln(graph,' ')
return
 
Make_day_stats:
   Datum=right(Datum,4) || substr(Datum,4,2) || left(Datum,2)
   teller=tel-1
   tel2=0
   Nw_datum=Datum
   do while tel2 <= 6 
      Nw_datum=Check_date(Nw_datum)
      Nw_datum.tel2=Nw_datum
      C_datum.tel2=0
      do while Nw_datum == datum.teller
         teller=teller-1
         C_datum.tel2=C_datum.tel2+1
      end
      say C_datum.tel2 Nw_datum
      tel2=tel2+1
      Nw_datum=Nw_datum-1
   end
   tel2=0
   groot=0   
   do while tel2 <= 6
      if C_datum.tel2 >= groot then groot=C_datum.tel2
      tel2=tel2+1
   end
   say groot
   index=15/groot
   tel2=0
   do while tel2 <= 6
      graf.tel2=round(C_datum.tel2*index)
      tel2=tel2+1
   end
   call open(graph,'t:graf.temp','W')
   call Print_info()
   call writeln(graph,groot)
   tel3=15
   do while tel3 >= 0
      call writech(graph,' | ')
      tel2=0 
      spat=1
      do while tel2 <= 6
         if graf.tel2 >= tel3 then do
            call writech(graph,copies(' ',7*spat-1) || '')
            spat=1
         end
         else spat=spat+1   
         tel2=tel2+1
      end
      tel3=tel3-1
      call writech(graph,LF)
   end
   call writeln(graph,' +' || copies('-',60))
   tel2=0
   call writech(graph,'         ')
   do while tel2 <= 6
      call writech(graph,right(Nw_datum.tel2,4) || '   ')
      tel2=tel2+1
   end
   call writeln(graph,' ')
   call writeln(graph,' ')
   call writeln(graph,naam 'wrote' totaal 'messages')
   call writeln(graph,'Average message/day:' round(totaal/(tel2-1)))
   call close(graph)  
return

round:
   arg reel
   posi=pos('.',reel)
   if posi ~= 0 then do
      get1=left(reel,posi-1)
      get2=substr(reel,posi+1,1)
      if get2 >= 5 then return get1+1
      else return get1
   end
return reel
   
Check_date:
   arg dat
   if right(dat,2) == '00' then do
      select
         when substr(dat,5,2) == '01' then do
            dat=(left(dat,4)-1) || '12' || '31'
            return dat
         end
         when substr(dat,5,2) == '02' then do
            dat=left(dat,4) || '01' || '31'
            return dat
         end
         when substr(dat,5,2) == '03' then do
            dat=left(dat,4) || '02' || '28'
            return dat
         end 
         when substr(dat,5,2) == '04' then do
            dat=left(dat,4) || '03' || '31'
            return dat
         end         
         when substr(dat,5,2) == '05' then do
            dat=left(dat,4) || '04' || '30'
            return dat
         end
         when substr(dat,5,2) == '06' then do
            dat=left(dat,4) || '05' || '31'
            return dat
         end
         when substr(dat,5,2) == '07' then do
            dat=left(dat,4) || '06' || '30'
            return dat
         end
         when substr(dat,5,2) == '08' then do
            dat=left(dat,4) || '07' || '31'
            return dat
         end
         when substr(dat,5,2) == '09' then do
            dat=left(dat,4) || '08' || '31'
            return dat
         end
         when substr(dat,5,2) == '10' then do
            dat=left(dat,4) || '09' || '30'
            return dat
         end
         when substr(dat,5,2) == '11' then do
            dat=left(dat,4) || '10' || '31'
            return dat
         end
         when substr(dat,5,2) == '12' then do
            dat=left(dat,4) || '11' || '30'
            return dat
         end
      end
   end
   else return dat
return

Conv_date:
   arg dat
   dag=right(dat,2)
   maand=substr(dat,5,2)
   jaar=left(dat,4)
return      

Get_dates_right:
   teller=0
   do while teller <= (tel-1)
      O_aan_maand=0
      O_aan_dag=0
      posi=pos('.',datum.teller)
      if posi == 2 then O_aan_dag=1
      posi=pos('.',datum.teller,4)
      if (O_aan_dag == 1) & (posi == 4) then O_aan_maand=1
      if (O_aan_dag ~= 1) & (posi == 5) then O_aan_maand=1
      if O_aan_dag == 1 then datum.teller='0' || datum.teller
      if O_aan_maand == 1 then datum.teller=insert('0',datum.teller,3)
      datum.teller=right(datum.teller,4) || substr(datum.teller,4,2) || left(datum.teller,2)
      teller=teller+1
   end
return      

Get_current_date:
   call open(date_file,'t:date.temp','R')
   date_str=readln(date_file)
   call close(date_file)
   select
      when pos('Jan',date_str) ~= 0 then do
         posit=pos('Jan',date_str)
         return substr(date_str,posit-3,2) || '.' || '01.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Feb',date_str) ~= 0 then do
         posit=pos('Feb',date_str)
         return substr(date_str,posit-3,2) || '.' || '02.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Mar',date_str) ~= 0 then do
         posit=pos('Mar',date_str)
         return substr(date_str,posit-3,2) || '.' || '03.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Apr',date_str) ~= 0 then do
         posit=pos('Apr',date_str)
         return substr(date_str,posit-3,2) || '.' || '04.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('May',date_str) ~= 0 then do
         posit=pos('May',date_str)
         return substr(date_str,posit-3,2) || '.' || '05.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Jun',date_str) ~= 0 then do
         posit=pos('Jun',date_str)
         return substr(date_str,posit-3,2) || '.' || '06.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Jul',date_str) ~= 0 then do
         posit=pos('Jul',date_str)
         return substr(date_str,posit-3,2) || '.' || '07.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Aug',date_str) ~= 0 then do
         posit=pos('Aug',date_str)
         return substr(date_str,posit-3,2) || '.' || '08.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Sep',date_str) ~= 0 then do
         posit=pos('Sep',date_str)
         return substr(date_str,posit-3,2) || '.' || '09.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Oct',date_str) ~= 0 then do
         posit=pos('Oct',date_str)
         return substr(date_str,posit-3,2) || '.' || '10.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Nov',date_str) ~= 0 then do
         posit=pos('Nov',date_str)
         return substr(date_str,posit-3,2) || '.' || '11.' || '19' || substr(date_str,posit+4,2)
      end
      when pos('Dec',date_str) ~= 0 then do
         posit=pos('Dec',date_str)
         return substr(date_str,posit-3,2) || '.' || '12.' || '19' || substr(date_str,posit+4,2)
      end
   end
return
