scoredisplay.class.php 20 KB

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