scoredisplay.class.php 19 KB

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