ultimix
rating_algorithms.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
38  var $Database = false;
39  var $DatabaseAlgorithms = false;
40  var $LinkUtilities = false;
41  var $RatingAccess = false;
42 
53  function __construct()
54  {
55  try
56  {
57  $this->Database = get_package( 'database' , 'last' , __FILE__ );
58  $this->DatabaseAlgorithms = get_package( 'database::database_algorithms' , 'last' , __FILE__ );
59  $this->LinkUtilities = get_package( 'link::link_utilities' , 'last' , __FILE__ );
60  $this->RatingAccess = get_package( 'rating::rating_access' , 'last' , __FILE__ );
61  }
62  catch( Exception $e )
63  {
64  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
65  }
66  }
67 
90  function increase_rating( $MasterId , $MasterType , $Value )
91  {
92  try
93  {
94  $Rating = $this->LinkUtilities->get_dependent_objects(
95  $MasterId , $MasterType , $this->RatingAccess
96  );
97  }
98  catch( Exception $e )
99  {
100  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
101  }
102  }
103 
130  function get_rating_object( $MasterId = false , $MasterType = 'user' )
131  {
132  try
133  {
134  list( $MasterId , $MasterType ) = $this->LinkUtilities->trasform_master_parameters(
135  $MasterId , $MasterType
136  );
137 
138  if( $this->LinkUtilities->dependent_objects_exist( $MasterId , $MasterType , 'rating' ) )
139  {
140  $Records = $this->LinkUtilities->get_dependent_objects(
141  $MasterId , $MasterType , 'rating' , $this->RatingAccess
142  );
143  }
144  else
145  {
146  $id = $this->RatingAccess->create(
147  array( 'master_id' => $MasterId , 'master_type' => $MasterType )
148  );
149  $Records = $this->RatingAccess->select_list( $id );
150  }
151 
152  return( $Records[ 0 ] );
153  }
154  catch( Exception $e )
155  {
156  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
157  }
158  }
159 
186  function get_rating( $MasterId = false , $MasterType = 'user' )
187  {
188  try
189  {
190  $Record = $this->get_rating_object( $MasterId , $MasterType );
191 
192  return( get_field( $Record , 'value' ) );
193  }
194  catch( Exception $e )
195  {
196  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
197  }
198  }
199 
226  function set_rating( $Value , $MasterId = false , $MasterType = 'user' )
227  {
228  try
229  {
230  $Value = $this->Security->get( $Value , 'float' );
231 
232  /* just create rating record in the DB in case is does not exist */
233  $Record = $this->get_rating_object( $MasterId , $MasterType );
234 
235  $this->RatingAcces->update( get_field( $Rating , 'id' ) , array( 'value' => $Value ) );
236  }
237  catch( Exception $e )
238  {
239  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
240  }
241  }
242 
269  function change_rating( $Delta , $MasterId = false , $MasterType = 'user' )
270  {
271  try
272  {
273  $this->Database->lock( array( 'umx_rating' ) , array( 'WRITE' ) );
274 
275  $Delta = $this->Security->get( $Delta , 'float' );
276 
277  /* just create rating record in the DB in case is does not exist */
278  $Record = $this->get_rating_object( $MasterId , $MasterType );
279 
280  $Value = get_field( $Record , 'value' );
281 
282  $this->RatingAcces->update( get_field( $Rating , 'id' ) , array( 'value' => $Value + $Delta ) );
283 
284  $this->Database->unlock();
285  }
286  catch( Exception $e )
287  {
288  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
289  }
290  }
291  }
292 
293 ?>