scoredisplay.class.php 18 KB

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