scoredisplay.class.php 19 KB

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