Location: PHPKode > scripts > A Class Libraries & Classes > a-class-rating/class.aclassrating.php
<?php
// class.aclassrating.php
// version 0.0.1, 28 October, 2005
//
// 28 October 2005: Added function getAuName() which extracts the author's name
// from the author's page. The name of the author is no longer needed to be
// passed to function startit(). However, it can be, to maintain compatibility
// with the original example script.
//  
// License
//
// Copyright (C) 2005 George A. Clarke, hide@address.com, http://gaclarke.com/
//
// This program 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.
//
// This program 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
// this program; if not, write to the Free Software Foundation, Inc., 59 Temple
// Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Description
//
// This class is intended to be used by those who have published classes
//  on phpclasses.org website.
// AClassRating is used to retrieve and display an author's rank and the
//  ratings of each of his/her published classes on PHPClasses.org. Given
//  the author's number, it will extract the following information from the
//  PHPClasses website.
// For the author it will display:
//     All time rank
//     Week Rank
//     Number of classes
// For each published class it will display: 
//    All time users
//    Week users
//    All time rank
//    Week rank              

class aclassrating
{
 var $cnum;
 var $pg;
 var $rate = array();
 var $cnums = array();
 var $authname;

// Prints to the browser the beginning of the web page and the table headings.
// getClassNums($auth) should be called before this function because the
// author ranks are needed.
// $authname = the author's name to be displayed in the heading.
function startit()
{
print "<html><head></head><body bgcolor='#a3c5cc'>\n";
print "<table border='1' ><tr><td colspan='5' align='center'>\n";
print "<h2>Ratings for ".$this->authname.", ".date('l j F Y')."</h2>\n";
print "<div align='left'>Published Packages: ".$this->cnums[0];
print "<br>All time rank: ".$this->cnums[1];
print "<br>Week rank: ".$this->cnums[2];
print "</div>";
print "</td></tr>";
print "<tr bgcolor='#abced6'>\n";
print "<td align='center'>Class Name</td>\n";
print "<td align='center'>All Time Users</td>\n";
print "<td align='center'>All Time Rank</td>\n";
print "<td align='center'>Week Users</td>\n";
print "<td align='center'>Week Rank</td>\n";
print "</tr>\n";
return;
}

// Displays the table ending and the webpage ending.
function endit()
{
print "</table>\n";
print "</body></html>\n";
return;
}

// Reads the class page of class number $classnum and puts it in the
// variable $this->pg.
// $classnum = the class number 
function getClasspg($classnum)
{
 $this->cnum=$classnum;
$lnk="http://www.phpclasses.org/browse/package/".$this->cnum.".html";
if (PHP_VERSION >= "4.3.0"){
 // use file_get_contents
 $this->pg=file_get_contents($lnk);
}else{
 // use fread
 $handle = fopen($lnk, "rb");
 $this->pg = '';
 while (!feof($handle)) {
  $this->pg .= fread($handle, 8192);
 }
 fclose($handle);
}
}

// Extracts the class numbers and the author's ranks from the page
// that displays the author's ranks and published classes on phpclasses.org.
// The information is returned in the array $this->cnums.
// $this->cnums[0]= Number of published classes for the author
// $this->cnums[1]= All time rank of the author
// $this->cnums[2]= Week rank of the author
// $this->cnums[3]= Class number
//    |
//    |
//     (The number of elements in this array depends on the number of
//      published classes for the author.)
// $auth = the author number
function getClassNums($auth)
{
$lnk="http://www.phpclasses.org/browse/author/".$auth.".html";
if (PHP_VERSION >= "4.3.0"){
 // use file_get_contents
 $this->pg=file_get_contents($lnk);
}else{
 // use fread
 $handle = fopen($lnk, "rb");
 $this->pg = '';
 while (!feof($handle)) {
  $this->pg .= fread($handle, 8192);
 }
 fclose($handle);
}
unset($this->cnums);
$this->getAuName();
$this->cnums[] = $this->getRank("packages");
$this->cnums[] = $this->getRank("time rank");
$this->cnums[] = $this->getRank("Week rank");

$ii=0;
 while(strpos($this->pg,"/package/",$ii)!==false)
 {
  $ij=strpos($this->pg,"/package/",$ii)+9;
  $ik=strpos($this->pg,'.',$ij);
  $this->cnums[]=substr($this->pg,$ij,$ik-$ij);
 $ii=$ik;
 }
return $this->cnums;
}

// Extracts the following information from the class page that was
// read into $this->pg and returns it in an array $this->rate.
// $this->rate[0] = class name
// $this->rate[1] = all time users
// $this->rate[2] = all time rank
// $this->rate[3] = week users
// $this->rate[4] = week rank
function getRating()
{
unset($this->rate);
$this->rate[0] = $this->getName("Name:");
$this->rate[1] = $this->getRate("All time users:");
$this->rate[2] = $this->getRate("All time rank:");
$this->rate[3] = $this->getRate("Week users:");
$this->rate[4] = $this->getRate("Week rank:");
return $this->rate;
}

// Displays one row of the table which contains the class ratings.
// $j = a row count (used to alternate colors of rows in the table)
function showRating($j)
{
$bg=($j==0)?"#9abbc1":"#abced6";
print "<tr bgcolor='$bg'>\n";
for ($i=0;$i<count($this->rate);$i++){
 print "<td align='center'>".$this->rate[$i]."</td>\n";
}
print "</tr>";
}

// Used by function getClassNums().
// Will extract the author's name from the author's page
//  and put it in $this->authname
// $a is not used but is there so the original example script
//  can still be used without change.
function getAuName($a)
{
 if(strpos($this->pg,"Name<")!==false)
 {
  $ij=strpos($this->pg,"of ")+3;
  $ik=strpos($this->pg,'<',$ij);
  $this->authname=substr($this->pg,$ij,$ik-$ij);
 }else{
  $rt="Not found";
 }
return $this->authname;
}


// Extracts one value from the authors page. It is used by the function getClassNums.
// $s = the item to search for to extract
//   (number of classes,all time rank,week rank)
function getRank($s)
{
 if(strpos($this->pg,$s)!==false)
 {
  $ii=strpos($this->pg,$s)+1;
  $ij=(strpos($this->pg,'nowrap"',$ii)===false)?strpos($this->pg,'NOWRAP',$ii)+7:strpos($this->pg,'nowrap"',$ii)+8;
//  $ij=strpos($this->pg,'nowrap"',$ii)+8;
  $ik=strpos($this->pg,'<',$ij);
  $rt=substr($this->pg,$ij,$ik-$ij);
 }else{
  $rt="Not found";
 }
return $rt;
}

// Used by function getRating() to extract one rating from the class page.
// $s = the item to search for to extract.
//      (All time users, All time rank, Week users, Week rank)
function getRate($s)
{
 if(strpos($this->pg,$s)!==false)
 {
  $ii=strpos($this->pg,$s)+1;
  $ij=strpos($this->pg,'NOWRAP',$ii)+7;
  $ik=strpos($this->pg,'<',$ij);
  $rt=substr($this->pg,$ij,$ik-$ij);
  preg_match("/[0-9]*/",$rt,$r);
  $rt=$r[0];
 }else{
  $rt="Not found";
 }
return $rt;
}

// Used by function getRating() to extract the class name from the class page.
// $s = "Name:"
function getName($s="Name:")
{
 if(strpos($this->pg,$s)!==false)
 {
  $ii=strpos($this->pg,$s)+1;
  $ij=strpos($this->pg,'<B>',$ii)+3;
  $ik=strpos($this->pg,'</',$ij);
  $name=substr($this->pg,$ij,$ik-$ij);
 }else{
  $name="Not found";
 }
return $name;
}
}
?>
Return current item: A Class Libraries & Classes