/mod/questionnaire/resultslib.php

https://github.com/jarednipper/HSU-common-code · PHP · 251 lines · 212 code · 17 blank · 22 comment · 35 complexity · 4685f2ef17eb17deeadeeba3b4ea72f7 MD5 · raw file

  1. <?php // $Id: resultslib.php,v 1.5 2007/06/12 17:04:48 mchurch Exp $
  2. // Written by James Flemer
  3. // <jflemer@alum.rpi.edu>
  4. // void mkrespercent(int[char*] counts, int total, bool showTotals);
  5. // void mkresrank(int[char*] counts, int total, bool showTotals);
  6. // void mkrescount(int[char*] counts, int total, bool showTotals);
  7. // void mkreslist(int[char*] counts, int total, bool showTotals);
  8. // void mkresavg(int[char*] counts, int total, bool showTotals);
  9. /* {{{ proto void mkrespercent(array weights, int total, int precision, bool show_totals)
  10. Builds HTML showing PERCENTAGE results. */
  11. function mkrespercent($counts,$total,$precision,$showTotals) {
  12. global $CFG;
  13. $i=0;
  14. $bg='';
  15. $image_url = $CFG->wwwroot.'/mod/questionnaire/images/';
  16. ?>
  17. <table width="90%" border="0">
  18. <?php
  19. while(list($content,$num) = each($counts)) {
  20. if($num>0) { $percent = $num/$total*100.0; }
  21. else { $percent = 0; }
  22. if($percent > 100) { $percent = 100; }
  23. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  24. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  25. else
  26. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  27. ?>
  28. <tr bgcolor="<?php echo($bg); ?>">
  29. <td><?php echo($content); ?></td>
  30. <td align="left">
  31. <?php
  32. if($num) {
  33. echo("&nbsp;<img src=\"" .$image_url ."hbar_l.gif\" height=9 width=4>");
  34. printf("<img src=\"" .$image_url ."hbar.gif\" height=9 width=%d>",$percent*2);
  35. echo("<img src=\"" .$image_url ."hbar_r.gif\" height=9 width=4>");
  36. printf("&nbsp;%.${precision}f%%",$percent);
  37. }
  38. ?></td>
  39. <td align="right">(<?php echo($num); ?>)</td>
  40. </tr>
  41. <?php
  42. $i += $num;
  43. } // end while
  44. if($showTotals) {
  45. if($i>0) { $percent = $i/$total*100.0; }
  46. else { $percent = 0; }
  47. if($percent > 100) { $percent = 100; }
  48. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  49. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  50. else
  51. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  52. ?>
  53. <tr bgcolor="<?php echo($bg); ?>">
  54. <td><b><?php print_string('total', 'questionnaire'); ?></b></td>
  55. <td width="40%"><b>&nbsp;<?php
  56. echo("<img src=\"" .$image_url ."hbar_l.gif\" height=9 width=4>");
  57. printf("<img src=\"" .$image_url ."hbar.gif\" height=9 width=%d>",$percent*2);
  58. echo("<img src=\"" .$image_url ."hbar_r.gif\" height=9 width=4>");
  59. printf("&nbsp;%.${precision}f%%",$percent); ?></b></td>
  60. <td width="10%" align="right"><b><?php echo($total); ?></b></td>
  61. </tr>
  62. <?php } ?>
  63. </table>
  64. <?php
  65. }
  66. /* }}} */
  67. /* {{{ proto void mkresrank(array weights, int total, int precision, bool show_totals)
  68. Builds HTML showing RANK results. */
  69. function mkresrank($counts,$total,$precision,$showTotals) {
  70. global $CFG;
  71. $bg='';
  72. $image_url = $CFG->wwwroot.'/mod/questionnaire/images/';
  73. ?>
  74. <table border="0">
  75. <tr>
  76. <td align="right"><b><?php print_string('rank', 'questionnaire'); ?></b></td>
  77. <td>&nbsp;</td>
  78. <td>&nbsp;</td>
  79. <td>&nbsp;</td>
  80. </tr>
  81. <?php
  82. arsort($counts);
  83. $i=0; $pt=0;
  84. while(list($content,$num) = each($counts)) {
  85. if($num)
  86. $p = $num/$total*100.0;
  87. else
  88. $p = 0;
  89. $pt += $p;
  90. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  91. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  92. else
  93. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  94. ?>
  95. <tr bgcolor="<?php echo($bg); ?>">
  96. <td align="right"><b><?php echo(++$i); ?></b></td>
  97. <td><?php echo($content); ?></td>
  98. <td align="right" width="60"><?php if($p) printf("%.${precision}f%%",$p); ?></td>
  99. <td align="right" width="60">(<?php echo($num); ?>)</td>
  100. </tr>
  101. <?php
  102. } // end while
  103. if($showTotals) {
  104. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  105. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  106. else
  107. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  108. ?>
  109. <tr bgcolor="<?php echo($bg); ?>">
  110. <td colspan=2 align="left"><b><?php print_string('total', 'questionnaire'); ?></b></td>
  111. <td align="right"><b><?php printf("%.${precision}f%%",$pt); ?></b></td>
  112. <td align="right"><b><?php echo($total); ?></b></td>
  113. </tr>
  114. <?php } ?>
  115. </table>
  116. <?php
  117. }
  118. /* }}} */
  119. /* {{{ proto void mkrescount(array weights, int total, int precision, bool show_totals)
  120. Builds HTML showing COUNT results. */
  121. function mkrescount($counts,$total,$precision,$showTotals) {
  122. global $CFG;
  123. $i=0;
  124. $image_url = $CFG->wwwroot.'/mod/questionnaire/images/';
  125. ?>
  126. <table width="90%" border="0">
  127. <?php
  128. $bg = '';
  129. while(list($content,$num) = each($counts)) {
  130. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  131. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  132. else
  133. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  134. ?>
  135. <tr bgcolor="<?php echo($bg); ?>">
  136. <td><?php echo($content); ?></td>
  137. <td align="right" width="60"><?php echo($num); ?></td>
  138. <td align="right" width="60">(<?php if($num) printf("%.${precision}f",$num/$total*100.0); ?>%)</td>
  139. </tr>
  140. <?php
  141. $i += $num;
  142. } // end while
  143. if($showTotals) {
  144. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  145. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  146. else
  147. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  148. ?>
  149. <tr bgcolor="<?php echo($bg); ?>">
  150. <td><b><?php print_string('total', 'questionnaire'); ?></b></td>
  151. <td align="right"><b><?php echo($total); ?></b></td>
  152. <td align="right"><b>(<?php if($i) printf("%.${precision}f",$i/$total*100.0); ?>%)</b></td>
  153. </tr>
  154. <?php } ?>
  155. </table>
  156. <?php
  157. }
  158. /* }}} */
  159. /* {{{ proto void mkreslist(array weights, int total, int precision, bool show_totals)
  160. Builds HTML showing LIST results. */
  161. function mkreslist($counts,$total,$precision,$showTotals) {
  162. global $CFG;
  163. if($total==0) return;
  164. $bg='';
  165. $image_url = $CFG->wwwroot.'/mod/questionnaire/images/';
  166. ?>
  167. <table width="90%" border="0" cellpadding="1">
  168. <tr><th align="left"><?php print_string('num', 'questionnaire'); ?></th><th><?php print_string('response', 'questionnaire'); ?></th></tr>
  169. <?php
  170. while(list($text,$num) = each($counts)) {
  171. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  172. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  173. else
  174. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  175. echo("<tr bgcolor=\"$bg\"><th align=\"left\" valign=\"top\">$num</th><td>$text</td></tr>\n");
  176. }
  177. ?>
  178. </table>
  179. <?php
  180. }
  181. /* }}} */
  182. /* {{{ proto void mkresavg(array weights, int total, int precision, bool show_totals)
  183. Builds HTML showing AVG results. */
  184. function mkresavg($counts,$total,$precision,$showTotals,$length) {
  185. global $CFG;
  186. $image_url = $CFG->wwwroot.'/mod/questionnaire/images/';
  187. if (!$length)
  188. $length = 5;
  189. $width = 200 / $length;
  190. ?>
  191. <table border="0" cellspacing="0" cellpadding="0">
  192. <tr>
  193. <td></td>
  194. <td align="center" colspan="<?php echo($length+2); ?>"><?php print_string('averagerank', 'questionnaire'); ?></td>
  195. </tr>
  196. <tr>
  197. <td></td>
  198. <?php
  199. for ($i = 0; $i < $length; )
  200. echo( "<td align=\"right\" width=\"$width\">". ++$i ."</td>\n");
  201. ?>
  202. <td width="20"></td>
  203. <td></td>
  204. </tr>
  205. <?php
  206. $bg = '';
  207. while(list($content,$avg) = each($counts)) {
  208. if($bg != $GLOBALS['ESPCONFIG']['bgalt_color1'])
  209. $bg = $GLOBALS['ESPCONFIG']['bgalt_color1'];
  210. else
  211. $bg = $GLOBALS['ESPCONFIG']['bgalt_color2'];
  212. ?>
  213. <tr bgcolor="<?php echo($bg); ?>">
  214. <td align="right"><?php echo($content); ?>&nbsp;</td>
  215. <td align="left" width="220" colspan="<?php echo($length+1); ?>">
  216. <?php
  217. if($avg) {
  218. echo('<img src="'. $image_url .'hbar_l.gif" height="9" width="4">');
  219. if (($j = $avg * $width - 11) > 0)
  220. printf('<img src="'. $image_url .'hbar.gif" height="9" width="%d">', $j);
  221. echo('<img src="'. $image_url .'hbar_r.gif" height="9" width="4">');
  222. }
  223. ?>
  224. </td>
  225. <td align="right" width="60">(<?php printf("%.${precision}f",$avg); ?>)</td>
  226. </tr>
  227. <?php
  228. } // end while
  229. ?>
  230. </table>
  231. <?php
  232. }
  233. /* }}} */
  234. ?>