scoredisplay.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. /**
  8. * Init
  9. */
  10. // Score display types constants
  11. define('SCORE_DIV', 1); // X / Y
  12. define('SCORE_PERCENT', 2); // XX %
  13. define('SCORE_DIV_PERCENT', 3); // X / Y (XX %)
  14. define('SCORE_AVERAGE', 4); // XX %
  15. define('SCORE_DECIMAL', 5); // 0.50 (X/Y)
  16. define('SCORE_BAR', 6); // Uses the Display::bar_progress function
  17. define('SCORE_SIMPLE', 7); // X
  18. //@todo where is number 6?
  19. define('SCORE_IGNORE_SPLIT', 8); // ??
  20. define('SCORE_DIV_PERCENT_WITH_CUSTOM', 9); // X / Y (XX %) - Good!
  21. define('SCORE_CUSTOM', 10); // Good!
  22. define('SCORE_DIV_SIMPLE_WITH_CUSTOM', 11); // X - Good!
  23. define('SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS', 12); // X - Good!
  24. define('SCORE_BOTH',1);
  25. define('SCORE_ONLY_DEFAULT',2);
  26. define('SCORE_ONLY_CUSTOM',3);
  27. /**
  28. * Class to display scores according to the settings made by the platform admin.
  29. * This class works as a singleton: call instance() to retrieve an object.
  30. * @author Bert Steppé
  31. * @package chamilo.gradebook
  32. */
  33. class ScoreDisplay
  34. {
  35. // Singleton stuff
  36. /**
  37. * Get the instance of this class
  38. */
  39. public static function instance($category_id = 0) {
  40. static $instance;
  41. if (!isset ($instance)) {
  42. $instance = new ScoreDisplay($category_id);
  43. }
  44. return $instance;
  45. }
  46. // Static methods
  47. /**
  48. * Compare the custom display of 2 scores, can be useful in sorting
  49. */
  50. public static function compare_scores_by_custom_display ($score1, $score2) {
  51. if (!isset($score1)) {
  52. return (isset($score2) ? 1 : 0);
  53. } elseif (!isset($score2)) {
  54. return -1;
  55. } else {
  56. $scoredisplay = ScoreDisplay :: instance();
  57. $custom1 = $scoredisplay->display_custom($score1);
  58. $custom2 = $scoredisplay->display_custom($score2);
  59. if ($custom1 == $custom2) {
  60. return 0;
  61. } else {
  62. return (($score1[0]/$score1[1]) < ($score2[0]/$score2[1]) ? -1 : 1);
  63. }
  64. }
  65. }
  66. private $coloring_enabled;
  67. private $color_split_value;
  68. private $custom_enabled;
  69. private $upperlimit_included;
  70. private $custom_display;
  71. private $custom_display_conv;
  72. /**
  73. * Protected constructor - call instance() to instantiate
  74. */
  75. protected function ScoreDisplay($category_id = 0) {
  76. if (!empty($category_id)) {
  77. $this->category_id = $category_id;
  78. }
  79. //Loading portal settings + using standard functions
  80. $value = api_get_setting('gradebook_score_display_coloring');
  81. $value = $value['my_display_coloring'];
  82. //Settting coloring
  83. $this->coloring_enabled = $value == 'true' ? true : false;
  84. if ($this->coloring_enabled) {
  85. $value = api_get_setting('gradebook_score_display_colorsplit');
  86. if (isset($value)) {
  87. $this->color_split_value = $value;
  88. }
  89. }
  90. //Setting custom enabled
  91. $value = api_get_setting('gradebook_score_display_custom');
  92. $value = $value['my_display_custom'];
  93. $this->custom_enabled = $value == 'true' ? true : false;
  94. if ($this->custom_enabled) {
  95. $params = array('category = ? AND subkey = ?' => array('Gradebook', 'ranking'));
  96. $displays = api_get_settings_params($params);
  97. $portal_displays = array();
  98. if (!empty($displays)) {
  99. foreach ($displays as $display) {
  100. $data = explode('::', $display['selected_value']);
  101. $portal_displays[$data[0]] = array('score' => $data[0], 'display' =>$data[1]);
  102. }
  103. sort($portal_displays);
  104. }
  105. $this->custom_display = $portal_displays;
  106. if (count($this->custom_display)>0) {
  107. $value = api_get_setting('gradebook_score_display_upperlimit');
  108. $value = $value['my_display_upperlimit'];
  109. $this->upperlimit_included = $value == 'true' ? true : false;
  110. $this->custom_display_conv = $this->convert_displays($this->custom_display);
  111. }
  112. }
  113. //If teachers can override the portal parameters
  114. if (api_get_setting('teachers_can_change_score_settings') == 'true') {
  115. //Load course settings
  116. if ($this->custom_enabled) {
  117. $this->custom_display = $this->get_custom_displays();
  118. if (count($this->custom_display)> 0) {
  119. $this->custom_display_conv = $this->convert_displays($this->custom_display);
  120. }
  121. }
  122. if ($this->coloring_enabled) {
  123. $this->color_split_value = $this->get_score_color_percent();
  124. }
  125. }
  126. }
  127. /**
  128. * Is coloring enabled ?
  129. */
  130. public function is_coloring_enabled () {
  131. return $this->coloring_enabled;
  132. }
  133. /**
  134. * Is custom score display enabled ?
  135. */
  136. public function is_custom() {
  137. return $this->custom_enabled;
  138. }
  139. /**
  140. * Is upperlimit included ?
  141. */
  142. public function is_upperlimit_included () {
  143. return $this->upperlimit_included;
  144. }
  145. /**
  146. * If custom score display is enabled, this will return the current settings.
  147. * See also update_custom_score_display_settings
  148. * @return array current settings (or null if feature not enabled)
  149. */
  150. public function get_custom_score_display_settings() {
  151. return $this->custom_display;
  152. }
  153. /**
  154. * If coloring is enabled, scores below this value will be displayed in red.
  155. * @return int color split value, in percent (or null if feature not enabled)
  156. */
  157. public function get_color_split_value() {
  158. return $this->color_split_value;
  159. }
  160. /**
  161. * Get current gradebook category id
  162. * @return int Category id
  163. */
  164. private function get_current_gradebook_category_id() {
  165. $tbl_gradebook_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  166. $curr_course_code = api_get_course_id();
  167. $curr_session_id = api_get_session_id();
  168. $session_condition = '';
  169. if (empty($curr_session_id)) {
  170. $session_condition = ' AND session_id is null ';
  171. } else {
  172. $session_condition = ' AND session_id = '.$curr_session_id;
  173. }
  174. $sql = 'SELECT id FROM '.$tbl_gradebook_category.' WHERE course_code = "'.$curr_course_code.'" '. $session_condition;
  175. $rs = Database::query($sql);
  176. $category_id = 0;
  177. if (Database::num_rows($rs) > 0) {
  178. $row = Database::fetch_row($rs);
  179. $category_id = $row[0];
  180. }
  181. return $category_id;
  182. }
  183. /**
  184. * Update custom score display settings
  185. * @param array $displays 2-dimensional array - every subarray must have keys (score, display)
  186. * @param int score color percent (optional)
  187. * @param int gradebook category id (optional)
  188. */
  189. public function update_custom_score_display_settings ($displays, $scorecolpercent = 0, $category_id = null) {
  190. $this->custom_display = $displays;
  191. $this->custom_display_conv = $this->convert_displays($this->custom_display);
  192. if (isset($category_id)) {
  193. $category_id = intval($category_id);
  194. } else {
  195. $category_id = $this->get_current_gradebook_category_id();
  196. }
  197. // remove previous settings
  198. $tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  199. $sql = 'DELETE FROM '.$tbl_display.' WHERE category_id = '.$category_id;
  200. Database::query($sql);
  201. // add new settings
  202. $sql = 'INSERT INTO '.$tbl_display.' (id, score, display, category_id, score_color_percent) VALUES ';
  203. $count = 0;
  204. foreach ($displays as $display) {
  205. if ($count > 0) {
  206. $sql .= ',';
  207. }
  208. $sql .= "(NULL, '".$display['score']."', '".Database::escape_string($display['display'])."', ".$category_id.", ".intval($scorecolpercent).")";
  209. $count++;
  210. }
  211. Database::query($sql);
  212. }
  213. public function get_number_decimals() {
  214. $number_decimals = api_get_setting('gradebook_number_decimals');
  215. if (!isset($number_decimals)) {
  216. $number_decimals = 0;
  217. }
  218. return $number_decimals;
  219. }
  220. /**
  221. * Formats a number depending of the number of decimals
  222. *
  223. * @param float a score
  224. * @return float the score formatted
  225. */
  226. public function format_score($score) {
  227. return floatval(number_format($score, $this->get_number_decimals()));
  228. }
  229. /**
  230. * Display a score according to the current settings
  231. * @param array $score score data structure, as returned by the calc_score functions
  232. * @param int $type one of the following constants: SCORE_DIV, SCORE_PERCENT, SCORE_DIV_PERCENT, SCORE_AVERAGE
  233. * (ignored for student's view if custom score display is enabled)
  234. * @param int $what one of the following constants: SCORE_BOTH, SCORE_ONLY_DEFAULT, SCORE_ONLY_CUSTOM (default: SCORE_BOTH)
  235. * (only taken into account if custom score display is enabled and for course/platform admin)
  236. */
  237. public function display_score($score, $type = SCORE_DIV_PERCENT, $what = SCORE_BOTH, $no_color = false) {
  238. $my_score = $score == 0 ? 1 : $score;
  239. if ($type == SCORE_BAR) {
  240. $percentage = $my_score[0]/$my_score[1]*100;
  241. return Display::bar_progress($percentage);
  242. }
  243. if ($type == SCORE_SIMPLE) {
  244. $simple_score = $this->format_score($my_score[0]);
  245. return $simple_score;
  246. }
  247. if ($this->custom_enabled && isset($this->custom_display_conv)) {
  248. $display = $this->display_default($my_score, $type);
  249. } else {
  250. // if no custom display set, use default display
  251. $display = $this->display_default($my_score, $type);
  252. }
  253. if ($this->coloring_enabled && $no_color == false) {
  254. $my_score_denom = ($score[1]==0)?1:$score[1];
  255. if (($score[0] / $my_score_denom) < ($this->color_split_value / 100)) {
  256. $display = Display::tag('font', $display, array('color'=>'red'));
  257. //$display = Display::label($display, 'important');
  258. }
  259. }
  260. return $display;
  261. }
  262. // Internal functions
  263. private function display_default ($score, $type) {
  264. switch ($type) {
  265. case SCORE_DIV : // X / Y
  266. return $this->display_as_div($score);
  267. case SCORE_PERCENT : // XX %
  268. return $this->display_as_percent($score);
  269. case SCORE_DIV_PERCENT : // X / Y (XX %)
  270. return $this->display_as_div($score).' (' . $this->display_as_percent($score) . ')';
  271. case SCORE_AVERAGE : // XX %
  272. return $this->display_as_percent($score);
  273. case SCORE_DECIMAL : // 0.50 (X/Y)
  274. return $this->display_as_decimal($score);
  275. case SCORE_DIV_PERCENT_WITH_CUSTOM : // X / Y (XX %) - Good!
  276. $custom = $this->display_custom($score);
  277. if (!empty($custom)) {
  278. $custom = ' - '.$custom;
  279. }
  280. return $this->display_as_div($score).' (' . $this->display_as_percent($score) . ')'.$custom;
  281. case SCORE_DIV_SIMPLE_WITH_CUSTOM : // X - Good!
  282. $custom = $this->display_custom($score);
  283. if (!empty($custom)) {
  284. $custom = ' - '.$custom;
  285. }
  286. return $this->display_simple_score($score).$custom;
  287. break;
  288. case SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS:
  289. $custom = $this->display_custom($score);
  290. if (!empty($custom)) {
  291. $custom = ' - '.$custom;
  292. }
  293. $score = $this->display_simple_score($score);
  294. //needs sudo apt-get install php5-intl
  295. if (class_exists(NumberFormatter)) {
  296. $iso = api_get_language_isocode();
  297. $f = new NumberFormatter($iso, NumberFormatter::SPELLOUT);
  298. $letters = $f->format($score);
  299. $letters = api_strtoupper($letters);
  300. $letters = " ($letters) ";
  301. }
  302. return $score.$letters.$custom;
  303. break;
  304. case SCORE_CUSTOM: // Good!
  305. return $this->display_custom($score);
  306. }
  307. }
  308. private function display_simple_score($score) {
  309. if (isset($score[0])) {
  310. return $this->format_score($score[0]);
  311. }
  312. return '';
  313. }
  314. /**
  315. * Returns "1" for array("100", "100");
  316. */
  317. private function display_as_decimal($score) {
  318. $score_denom = ($score[1]==0) ? 1 : $score[1];
  319. return $this->format_score($score[0]/$score_denom);
  320. }
  321. /**
  322. * Returns "100 %" for array("100", "100");
  323. */
  324. private function display_as_percent($score) {
  325. $score_denom = ($score[1]==0) ? 1 : $score[1];
  326. return $this->format_score($score[0]/$score_denom*100) . ' %';
  327. }
  328. /**
  329. *
  330. * Returns 10.00 / 10.00 for array("100", "100");
  331. * @param array $score
  332. */
  333. private function display_as_div($score) {
  334. if ($score == 1) {
  335. return '0/0';
  336. } else {
  337. $score[0] =$this->format_score($score[0]);
  338. $score[1] =$this->format_score($score[1]);
  339. return $score[0] . ' / ' . $score[1];
  340. }
  341. }
  342. /**
  343. *
  344. * Depends in the user selections [0 50] Bad [50:100] Good
  345. * @param array $score
  346. */
  347. private function display_custom ($score) {
  348. $my_score_denom= ($score[1]==0) ? 1 : $score[1];
  349. $scaledscore = $score[0] / $my_score_denom;
  350. if ($this->upperlimit_included) {
  351. foreach ($this->custom_display_conv as $displayitem) {
  352. if ($scaledscore <= $displayitem['score']) {
  353. return $displayitem['display'];
  354. }
  355. }
  356. } else {
  357. if (!empty($this->custom_display_conv)) {
  358. foreach ($this->custom_display_conv as $displayitem) {
  359. if ($scaledscore < $displayitem['score'] || $displayitem['score'] == 1) {
  360. return $displayitem['display'];
  361. }
  362. }
  363. }
  364. }
  365. }
  366. /**
  367. * Get score color percent by category
  368. * @param int Gradebook category id
  369. * @return int Score
  370. */
  371. private function get_score_color_percent($category_id = null) {
  372. $tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  373. if (isset($category_id)) {
  374. $category_id = intval($category_id);
  375. } else {
  376. $category_id = $this->get_current_gradebook_category_id();
  377. }
  378. $sql = 'SELECT score_color_percent FROM '.$tbl_display.' WHERE category_id = '.$category_id.' LIMIT 1';
  379. $result = Database::query($sql);
  380. $score = 0;
  381. if (Database::num_rows($result) > 0) {
  382. $row = Database::fetch_row($result);
  383. $score = $row[0];
  384. }
  385. return $score;
  386. }
  387. /**
  388. * Get current custom score display settings
  389. * @param int Gradebook category id
  390. * @return array 2-dimensional array - every element contains 3 subelements (id, score, display)
  391. */
  392. private function get_custom_displays($category_id = null) {
  393. $tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  394. if (isset($category_id)) {
  395. $category_id = intval($category_id);
  396. } else {
  397. $category_id = $this->get_current_gradebook_category_id();
  398. }
  399. $sql = 'SELECT * FROM '.$tbl_display.' WHERE category_id = '.$category_id.' ORDER BY score';
  400. $result = Database::query($sql);
  401. return Database::store_result($result,'ASSOC');
  402. }
  403. /**
  404. * Convert display settings to internally used values
  405. */
  406. private function convert_displays($custom_display) {
  407. if (isset($custom_display)) {
  408. // get highest score entry, and copy each element to a new array
  409. $converted = array();
  410. $highest = 0;
  411. foreach ($custom_display as $element) {
  412. if ($element['score'] > $highest) {
  413. $highest = $element['score'];
  414. }
  415. $converted[] = $element;
  416. }
  417. // sort the new array (ascending)
  418. usort($converted, array('ScoreDisplay', 'sort_display'));
  419. // adjust each score in such a way that
  420. // each score is scaled between 0 and 1
  421. // the highest score in this array will be equal to 1
  422. $converted2 = array();
  423. foreach ($converted as $element) {
  424. $newelement = array();
  425. if (!empty($highest)) {
  426. $newelement['score'] = $element['score'] / $highest;
  427. } else {
  428. $newelement['score'] = 0;
  429. }
  430. $newelement['display'] = $element['display'];
  431. $converted2[] = $newelement;
  432. }
  433. return $converted2;
  434. } else {
  435. return null;
  436. }
  437. }
  438. private function sort_display($item1, $item2) {
  439. if ($item1['score'] == $item2['score']) {
  440. return 0;
  441. } else {
  442. return ($item1['score'] < $item2['score'] ? -1 : 1);
  443. }
  444. }
  445. }