Source for file StatusStars.class.php

Documentation is available at StatusStars.class.php

  1. <?php
  2. /**
  3. * @since 2/22/06
  4. * @package harmoni.utilities
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: StatusStars.class.php,v 1.3 2007/03/21 15:51:21 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * This lovely little class will print a semi-accurate series of asterisks as a
  14. * status bar for anything you want(written for the importer system). The idea
  15. * is that you give it a total number of objects and a level of detail, then
  16. * make sure that you update statistics when one of your total is done, and it
  17. * will print directly to the end user the status of the process. Don't forget
  18. * to use a javascript re-direct at the end of your process, otherwise you'll
  19. * have header problems.
  20. *
  21. * @since 2/22/06
  22. * @package harmoni.utilities
  23. *
  24. * @copyright Copyright &copy; 2005, Middlebury College
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  26. *
  27. * @version $Id: StatusStars.class.php,v 1.3 2007/03/21 15:51:21 adamfranco Exp $
  28. */
  29. class StatusStars {
  30. /*******************************************************
  31. * STATISTICS HANDLING
  32. *********************************************************/
  33.  
  34.  
  35. var $_total; // assets being an appropriate granule
  36. var $_completed; // right now only using assets
  37. var $_currentPercent; // the percentage of assets that are completed
  38. var $_ob_data = array();
  39. var $_detail; // an integer number of stars for the 100% complete bar
  40. /**
  41. * Constructor
  42. *
  43. * @access public
  44. * @since 2/22/06
  45. */
  46. function StatusStars ($label = '') {
  47. // nothing to do here
  48. $this->_label = $label;
  49. }
  50.  
  51. /**
  52. * Gathers the total number of granules for importer status
  53. *
  54. * @param int $total the total number of items being statused
  55. * @param int $detail the number
  56. * @access public
  57. * @since 2/17/06
  58. */
  59. function initializeStatistics ($total, $detail = 100) {
  60. $this->_totalAssets = $total;
  61. $this->_completedAssets = 0;
  62. $this->_currentPercent = 0;
  63. $this->_detail = $detail;
  64. $this->_createStatusBar();
  65. }
  66.  
  67. /**
  68. * Updates the number of granules imported for importer status
  69. *
  70. * @access public
  71. * @since 2/17/06
  72. */
  73. function updateStatistics () {
  74. $this->_completedAssets++;
  75. $pct = floor(
  76. $this->_detail * $this->_completedAssets
  77. / $this->_totalAssets);
  78. $stars = $pct - $this->_currentPercent;
  79. $this->_currentPercent = $pct;
  80. $end = false;
  81. if ($pct == $this->_detail)
  82. $end = true;
  83. if ($stars > 0)
  84. $this->_updateStatusBar($stars, $end);
  85. }
  86. /**
  87. * Prints the status bar to the user
  88. *
  89. * @access private
  90. * @since 2/20/06
  91. */
  92. function _createStatusBar () {
  93. $this->_jump_obs();
  94. if ($this->_label) {
  95. $this->startBlock();
  96. print $this->_label;
  97. $this->endBlock();
  98. }
  99. $this->startBlock();
  100. print "0";
  101. $this->_addSpaces();
  102. print "25";
  103. $this->_addSpaces(1);
  104. print "50";
  105. $this->_addSpaces(1);
  106. print "75";
  107. $this->_addSpaces(2);
  108. print "100%\n";
  109. print "|";
  110. $this->_addDashes();
  111. print "|";
  112. $this->_addDashes();
  113. print "|";
  114. $this->_addDashes();
  115. print "|";
  116. $this->_addDashes();
  117. print "|\n";
  118. // print "*";
  119. $this->_land_obs();
  120. }
  121. /**
  122. * Prints the right number of Spaces
  123. *
  124. * @param int $mod the number of spaces to be taken away due to numbers
  125. * @access private
  126. * @since 2/22/06
  127. */
  128. function _addSpaces ($mod = 0) {
  129. for ($count = floor((($this->_detail - 4) / 4) - $mod); $count > 0; $count--) {
  130. print " ";
  131. }
  132. }
  133.  
  134. /**
  135. * Prints the right number of dashes
  136. *
  137. * @access private
  138. * @since 2/22/06
  139. */
  140. function _addDashes () {
  141. for ($count = floor((($this->_detail - 4) / 4)); $count > 0; $count--) {
  142. print "-";
  143. }
  144. }
  145. /**
  146. * Updates the statys bar for the user
  147. *
  148. * @param int $stars the number of asterisks that need to be printed
  149. * @param bool $end whether or not we're done and should continue
  150. * @access private
  151. * @since 2/20/06
  152. */
  153. function _updateStatusBar ($stars, $end) {
  154. $this->_jump_obs();
  155. for (; $stars > 0; $stars--)
  156. print "*";
  157. flush();
  158. if ($end)
  159. $this->endBlock();
  160. $this->_land_obs();
  161. }
  162. /**
  163. * Climbs down the ob ladder and saves the data to put back later
  164. *
  165. * @access private
  166. * @since 2/20/06
  167. */
  168. function _jump_obs () {
  169. $level = ob_get_level();
  170. while ($level > 0) {
  171. $this->_ob_data[$level] = ob_get_clean();
  172. $level = ob_get_level();
  173. }
  174. }
  175.  
  176. /**
  177. * Climps back up the ob ladder adding back the data that was there
  178. *
  179. * @access private
  180. * @since 2/20/06
  181. */
  182. function _land_obs() {
  183. foreach ($this->_ob_data as $level => $data) {
  184. ob_start();
  185. print $data;
  186. unset($this->_ob_data[$level]);
  187. }
  188. }
  189. /**
  190. * Start a new line
  191. *
  192. * @return void
  193. * @access public
  194. * @since 3/12/07
  195. */
  196. function startBlock () {
  197. print "\n<pre>";
  198. }
  199. /**
  200. * Start a new line
  201. *
  202. * @return void
  203. * @access public
  204. * @since 3/12/07
  205. */
  206. function endBlock () {
  207. print "</pre>";
  208. }
  209. }
  210.  
  211. /**
  212. * A command-line-environment of status stars. This class will not output HTML.
  213. *
  214. * @since 3/12/07
  215. * @package harmoni.utilities
  216. *
  217. * @copyright Copyright &copy; 2005, Middlebury College
  218. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  219. *
  220. * @version $Id: StatusStars.class.php,v 1.3 2007/03/21 15:51:21 adamfranco Exp $
  221. */
  222. class CLIStatusStars
  223. extends StatusStars
  224. {
  225. /**
  226. * Start a new line
  227. *
  228. * @return void
  229. * @access public
  230. * @since 3/12/07
  231. */
  232. function startBlock () {
  233. print "\n";
  234. }
  235. /**
  236. * Start a new line
  237. *
  238. * @return void
  239. * @access public
  240. * @since 3/12/07
  241. */
  242. function endBlock () {
  243. }
  244. }
  245.  
  246. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:42 -0400 by phpDocumentor 1.3.0RC3