scoredisplay.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 = ScoreDisplay :: 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. protected function ScoreDisplay($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. return $this->custom_enabled;
  121. }
  122. /**
  123. * Is upperlimit included ?
  124. */
  125. public function is_upperlimit_included ()
  126. {
  127. return $this->upperlimit_included;
  128. }
  129. /**
  130. * If custom score display is enabled, this will return the current settings.
  131. * See also update_custom_score_display_settings
  132. * @return array current settings (or null if feature not enabled)
  133. */
  134. public function get_custom_score_display_settings()
  135. {
  136. return $this->custom_display;
  137. }
  138. /**
  139. * If coloring is enabled, scores below this value will be displayed in red.
  140. * @return int color split value, in percent (or null if feature not enabled)
  141. */
  142. public function get_color_split_value()
  143. {
  144. return $this->color_split_value;
  145. }
  146. /**
  147. * Get current gradebook category id
  148. * @return int Category id
  149. */
  150. private function get_current_gradebook_category_id()
  151. {
  152. $tbl_gradebook_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  153. $curr_course_code = api_get_course_id();
  154. $curr_session_id = api_get_session_id();
  155. if (empty($curr_session_id)) {
  156. $session_condition = ' AND session_id is null ';
  157. } else {
  158. $session_condition = ' AND session_id = '.$curr_session_id;
  159. }
  160. $sql = 'SELECT id FROM '.$tbl_gradebook_category.' WHERE course_code = "'.$curr_course_code.'" '. $session_condition;
  161. $rs = Database::query($sql);
  162. $category_id = 0;
  163. if (Database::num_rows($rs) > 0) {
  164. $row = Database::fetch_row($rs);
  165. $category_id = $row[0];
  166. }
  167. return $category_id;
  168. }
  169. /**
  170. * Update custom score display settings
  171. * @param array $displays 2-dimensional array - every sub array must have keys (score, display)
  172. * @param int score color percent (optional)
  173. * @param int gradebook category id (optional)
  174. */
  175. public function update_custom_score_display_settings($displays, $scorecolpercent = 0, $category_id = null)
  176. {
  177. $this->custom_display = $displays;
  178. $this->custom_display_conv = $this->convert_displays($this->custom_display);
  179. if (isset($category_id)) {
  180. $category_id = intval($category_id);
  181. } else {
  182. $category_id = $this->get_current_gradebook_category_id();
  183. }
  184. // remove previous settings
  185. $table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  186. $sql = 'DELETE FROM '.$table.' WHERE category_id = '.$category_id;
  187. Database::query($sql);
  188. // add new settings
  189. $count = 0;
  190. foreach ($displays as $display) {
  191. $params = [
  192. 'score' => $display['score'],
  193. 'display' => $display['display'],
  194. 'category_id' => $category_id,
  195. 'score_color_percent' => $scorecolpercent,
  196. ];
  197. Database::insert($table, $params);
  198. $count++;
  199. }
  200. }
  201. /**
  202. * @param int $category_id
  203. * @return bool
  204. */
  205. public function insert_defaults($category_id)
  206. {
  207. if (empty($category_id)) {
  208. return false;
  209. }
  210. //Get this from DB settings
  211. $display = array(
  212. 50 => get_lang('GradebookFailed'),
  213. 60 => get_lang('GradebookPoor'),
  214. 70 => get_lang('GradebookFair'),
  215. 80 => get_lang('GradebookGood'),
  216. 90 => get_lang('GradebookOutstanding'),
  217. 100 => get_lang('GradebookExcellent')
  218. );
  219. $tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  220. foreach($display as $value => $text) {
  221. $params = array(
  222. 'score' => $value,
  223. 'display' => $text,
  224. 'category_id' => $category_id,
  225. 'score_color_percent' => 0,
  226. );
  227. Database::insert($tbl_display, $params);
  228. }
  229. }
  230. /**
  231. * @return int|null|string
  232. */
  233. public function get_number_decimals()
  234. {
  235. $number_decimals = api_get_setting('gradebook_number_decimals');
  236. if (!isset($number_decimals)) {
  237. $number_decimals = 0;
  238. }
  239. return $number_decimals;
  240. }
  241. /**
  242. * Formats a number depending of the number of decimals
  243. *
  244. * @param float $score
  245. * @return float the score formatted
  246. */
  247. public function format_score($score)
  248. {
  249. return floatval(number_format($score, $this->get_number_decimals()));
  250. }
  251. /**
  252. * Display a score according to the current settings
  253. * @param array $score data structure, as returned by the calc_score functions
  254. * @param int $type one of the following constants:
  255. * SCORE_DIV, SCORE_PERCENT, SCORE_DIV_PERCENT, SCORE_AVERAGE
  256. * (ignored for student's view if custom score display is enabled)
  257. * @param int $what one of the following constants:
  258. * SCORE_BOTH, SCORE_ONLY_DEFAULT, SCORE_ONLY_CUSTOM (default: SCORE_BOTH)
  259. * (only taken into account if custom score display is enabled and for course/platform admin)
  260. *
  261. * @return string
  262. */
  263. public function display_score(
  264. $score,
  265. $type = SCORE_DIV_PERCENT,
  266. $what = SCORE_BOTH,
  267. $no_color = false
  268. ) {
  269. $my_score = $score == 0 ? 1 : $score;
  270. if ($type == SCORE_BAR) {
  271. $percentage = $my_score[0]/$my_score[1]*100;
  272. return Display::bar_progress($percentage);
  273. }
  274. if ($type == SCORE_SIMPLE) {
  275. $simple_score = $this->format_score($my_score[0]);
  276. return $simple_score;
  277. }
  278. if ($this->custom_enabled && isset($this->custom_display_conv)) {
  279. $display = $this->display_default($my_score, $type);
  280. } else {
  281. // if no custom display set, use default display
  282. $display = $this->display_default($my_score, $type);
  283. }
  284. if ($this->coloring_enabled && $no_color == false) {
  285. $my_score_denom = isset($score[1]) && !empty($score[1]) ? $score[1] : 1;
  286. $scoreCleaned = isset($score[0]) ? $score[0] : 0;
  287. if (($scoreCleaned / $my_score_denom) < ($this->color_split_value / 100)) {
  288. $display = Display::tag('font', $display, array('color'=>'red'));
  289. }
  290. }
  291. return $display;
  292. }
  293. /**
  294. * @param $score
  295. * @param $type
  296. * @return float|string
  297. */
  298. private function display_default($score, $type)
  299. {
  300. switch ($type) {
  301. case SCORE_DIV : // X / Y
  302. return $this->display_as_div($score);
  303. case SCORE_PERCENT : // XX %
  304. return $this->display_as_percent($score);
  305. case SCORE_DIV_PERCENT : // X / Y (XX %)
  306. return $this->display_as_div($score).' (' . $this->display_as_percent($score) . ')';
  307. case SCORE_AVERAGE : // XX %
  308. return $this->display_as_percent($score);
  309. case SCORE_DECIMAL : // 0.50 (X/Y)
  310. return $this->display_as_decimal($score);
  311. case SCORE_DIV_PERCENT_WITH_CUSTOM : // X / Y (XX %) - Good!
  312. $custom = $this->display_custom($score);
  313. if (!empty($custom)) {
  314. $custom = ' - '.$custom;
  315. }
  316. return $this->display_as_div($score).' (' . $this->display_as_percent($score) . ')'.$custom;
  317. case SCORE_DIV_SIMPLE_WITH_CUSTOM : // X - Good!
  318. $custom = $this->display_custom($score);
  319. if (!empty($custom)) {
  320. $custom = ' - '.$custom;
  321. }
  322. return $this->display_simple_score($score).$custom;
  323. break;
  324. case SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS:
  325. $custom = $this->display_custom($score);
  326. if (!empty($custom)) {
  327. $custom = ' - '.$custom;
  328. }
  329. $score = $this->display_simple_score($score);
  330. //needs sudo apt-get install php5-intl
  331. if (class_exists('NumberFormatter')) {
  332. $iso = api_get_language_isocode();
  333. $f = new NumberFormatter($iso, NumberFormatter::SPELLOUT);
  334. $letters = $f->format($score);
  335. $letters = api_strtoupper($letters);
  336. $letters = " ($letters) ";
  337. }
  338. return $score.$letters.$custom;
  339. break;
  340. case SCORE_CUSTOM: // Good!
  341. return $this->display_custom($score);
  342. }
  343. }
  344. /**
  345. * @param array $score
  346. * @return float|string
  347. */
  348. private function display_simple_score($score)
  349. {
  350. if (isset($score[0])) {
  351. return $this->format_score($score[0]);
  352. }
  353. return '';
  354. }
  355. /**
  356. * Returns "1" for array("100", "100");
  357. * @param array $score
  358. * @return float
  359. */
  360. private function display_as_decimal($score)
  361. {
  362. $score_denom = ($score[1]==0) ? 1 : $score[1];
  363. return $this->format_score($score[0]/$score_denom);
  364. }
  365. /**
  366. * Returns "100 %" for array("100", "100");
  367. */
  368. private function display_as_percent($score)
  369. {
  370. $score_denom = ($score[1]==0) ? 1 : $score[1];
  371. return $this->format_score($score[0]/$score_denom*100) . ' %';
  372. }
  373. /**
  374. *
  375. * Returns 10.00 / 10.00 for array("100", "100");
  376. * @param array $score
  377. */
  378. private function display_as_div($score)
  379. {
  380. if ($score == 1) {
  381. return '0 / 0';
  382. } else {
  383. $score[0] = isset($score[0]) ? $this->format_score($score[0]) : 0;
  384. $score[1] = isset($score[1]) ? $this->format_score($score[1]) : 0;
  385. return $score[0] . ' / ' . $score[1];
  386. }
  387. }
  388. /**
  389. * Depends on the teacher's configuration of thresholds. i.e. [0 50] "Bad", [50:100] "Good"
  390. * @param array $score
  391. */
  392. private function display_custom($score)
  393. {
  394. $my_score_denom= ($score[1]==0) ? 1 : $score[1];
  395. $scaledscore = $score[0] / $my_score_denom;
  396. if ($this->upperlimit_included) {
  397. foreach ($this->custom_display_conv as $displayitem) {
  398. if ($scaledscore <= $displayitem['score']) {
  399. return $displayitem['display'];
  400. }
  401. }
  402. } else {
  403. if (!empty($this->custom_display_conv)) {
  404. foreach ($this->custom_display_conv as $displayitem) {
  405. if ($scaledscore < $displayitem['score'] || $displayitem['score'] == 1) {
  406. return $displayitem['display'];
  407. }
  408. }
  409. }
  410. }
  411. }
  412. /**
  413. * Get score color percent by category
  414. * @param int Gradebook category id
  415. * @return int Score
  416. */
  417. private function get_score_color_percent($category_id = null)
  418. {
  419. $tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  420. if (isset($category_id)) {
  421. $category_id = intval($category_id);
  422. } else {
  423. $category_id = $this->get_current_gradebook_category_id();
  424. }
  425. $sql = 'SELECT score_color_percent FROM '.$tbl_display.' WHERE category_id = '.$category_id.' LIMIT 1';
  426. $result = Database::query($sql);
  427. $score = 0;
  428. if (Database::num_rows($result) > 0) {
  429. $row = Database::fetch_row($result);
  430. $score = $row[0];
  431. }
  432. return $score;
  433. }
  434. /**
  435. * Get current custom score display settings
  436. * @param int Gradebook category id
  437. * @return array 2-dimensional array - every element contains 3 subelements (id, score, display)
  438. */
  439. private function get_custom_displays($category_id = null)
  440. {
  441. $tbl_display = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY);
  442. if (isset($category_id)) {
  443. $category_id = intval($category_id);
  444. } else {
  445. $category_id = $this->get_current_gradebook_category_id();
  446. }
  447. $sql = 'SELECT * FROM '.$tbl_display.'
  448. WHERE category_id = '.$category_id.'
  449. ORDER BY score';
  450. $result = Database::query($sql);
  451. return Database::store_result($result,'ASSOC');
  452. }
  453. /**
  454. * Convert display settings to internally used values
  455. */
  456. private function convert_displays($custom_display)
  457. {
  458. if (isset($custom_display)) {
  459. // get highest score entry, and copy each element to a new array
  460. $converted = array();
  461. $highest = 0;
  462. foreach ($custom_display as $element) {
  463. if ($element['score'] > $highest) {
  464. $highest = $element['score'];
  465. }
  466. $converted[] = $element;
  467. }
  468. // sort the new array (ascending)
  469. usort($converted, array('ScoreDisplay', 'sort_display'));
  470. // adjust each score in such a way that
  471. // each score is scaled between 0 and 1
  472. // the highest score in this array will be equal to 1
  473. $converted2 = array();
  474. foreach ($converted as $element) {
  475. $newelement = array();
  476. if (isset($highest) && !empty($highest) && $highest > 0) {
  477. $newelement['score'] = $element['score'] / $highest;
  478. } else {
  479. $newelement['score'] = 0;
  480. }
  481. $newelement['display'] = $element['display'];
  482. $converted2[] = $newelement;
  483. }
  484. return $converted2;
  485. } else {
  486. return null;
  487. }
  488. }
  489. /**
  490. * @param array $item1
  491. * @param array $item2
  492. * @return int
  493. */
  494. private function sort_display($item1, $item2)
  495. {
  496. if ($item1['score'] == $item2['score']) {
  497. return 0;
  498. } else {
  499. return ($item1['score'] < $item2['score'] ? -1 : 1);
  500. }
  501. }
  502. }