scoredisplay.class.php 19 KB

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