scoredisplay.class.php 19 KB

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