scoredisplay.class.php 18 KB

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