scoredisplay.class.php 18 KB

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