flatview_data_generator.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class FlatViewDataGenerator
  5. * Class to select, sort and transform object data into array data,
  6. * used for the teacher's flat view
  7. * @author Bert Steppé
  8. *
  9. * @package chamilo.gradebook
  10. */
  11. class FlatViewDataGenerator
  12. {
  13. // Sorting types constants
  14. const FVDG_SORT_LASTNAME = 1;
  15. const FVDG_SORT_FIRSTNAME = 2;
  16. const FVDG_SORT_ASC = 4;
  17. const FVDG_SORT_DESC = 8;
  18. private $users;
  19. private $evals;
  20. private $links;
  21. private $evals_links;
  22. public $params;
  23. public $category = array();
  24. private $mainCourseCategory;
  25. /**
  26. * Constructor
  27. */
  28. public function FlatViewDataGenerator(
  29. $users = array(),
  30. $evals = array(),
  31. $links = array(),
  32. $params = array(),
  33. $mainCourseCategory = null
  34. ) {
  35. $this->users = (isset($users) ? $users : array());
  36. $this->evals = (isset($evals) ? $evals : array());
  37. $this->links = (isset($links) ? $links : array());
  38. $this->evals_links = array_merge($this->evals, $this->links);
  39. $this->params = $params;
  40. $this->mainCourseCategory = $mainCourseCategory;
  41. }
  42. /**
  43. * @return Category
  44. */
  45. public function getMainCourseCategory()
  46. {
  47. return $this->mainCourseCategory;
  48. }
  49. /**
  50. * Get total number of users (rows)
  51. */
  52. public function get_total_users_count()
  53. {
  54. return count($this->users);
  55. }
  56. /**
  57. * Get total number of evaluations/links (columns) (the 2 users columns not included)
  58. */
  59. public function get_total_items_count()
  60. {
  61. return count($this->evals_links);
  62. }
  63. /**
  64. * Get array containing column header names (incl user columns)
  65. * @param int $items_start Start item offset
  66. * @param int $items_count Number of items to get
  67. * @param bool $show_detail whether to show the details or not
  68. * @return array List of headers
  69. */
  70. public function get_header_names($items_start = 0, $items_count = null, $show_detail = false)
  71. {
  72. $headers = array();
  73. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  74. $headers[] = get_lang('OfficialCode');
  75. }
  76. if (isset($this->params['join_firstname_lastname']) && $this->params['join_firstname_lastname']) {
  77. if (api_is_western_name_order()) {
  78. $headers[] = get_lang('FirstnameAndLastname');
  79. } else {
  80. $headers[] = get_lang('LastnameAndFirstname');
  81. }
  82. } else {
  83. if (api_is_western_name_order()) {
  84. $headers[] = get_lang('FirstName');
  85. $headers[] = get_lang('LastName');
  86. } else {
  87. $headers[] = get_lang('LastName');
  88. $headers[] = get_lang('FirstName');
  89. }
  90. }
  91. if (!isset($items_count)) {
  92. $items_count = count($this->evals_links) - $items_start;
  93. }
  94. $parent_id = $this->category->get_parent_id();
  95. if ($parent_id == 0 or
  96. $this->params['only_subcat'] == $this->category->get_id()
  97. ) {
  98. $main_weight = $this->category->get_weight();
  99. $grade_model_id = $this->category->get_grade_model_id();
  100. } else {
  101. $main_cat = Category::load($parent_id, null, null);
  102. $main_weight = $main_cat[0]->get_weight();
  103. $grade_model_id = $main_cat[0]->get_grade_model_id();
  104. }
  105. $use_grade_model = true;
  106. if (empty($grade_model_id) || $grade_model_id == -1) {
  107. $use_grade_model = false;
  108. }
  109. //@todo move these in a function
  110. $sum_categories_weight_array = array();
  111. $mainCategoryId = null;
  112. $mainCourseCategory = $this->getMainCourseCategory();
  113. if (!empty($mainCourseCategory)) {
  114. $mainCategoryId = $mainCourseCategory->get_id();
  115. }
  116. if (isset($this->category) && !empty($this->category)) {
  117. $categories = Category::load(null, null, null, $this->category->get_id());
  118. if (!empty($categories)) {
  119. foreach ($categories as $category) {
  120. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  121. }
  122. } else {
  123. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  124. }
  125. }
  126. //No category was added
  127. $course_code = api_get_course_id();
  128. $session_id = api_get_session_id();
  129. $allcat = $this->category->get_subcategories(
  130. null,
  131. $course_code,
  132. $session_id,
  133. 'ORDER BY id'
  134. );
  135. $evaluationsAdded = array();
  136. if ($parent_id == 0 && !empty($allcat)) {
  137. //Means there are any subcategory
  138. foreach ($allcat as $sub_cat) {
  139. $sub_cat_weight = round(100*$sub_cat->get_weight()/$main_weight,1);
  140. $add_weight = " $sub_cat_weight %";
  141. /*if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  142. $add_weight = null;
  143. }*/
  144. $headers[] = Display::url(
  145. $sub_cat->get_name(),
  146. api_get_self().'?selectcat='.$sub_cat->get_id()
  147. ).$add_weight;
  148. }
  149. } else {
  150. if (!isset($this->params['only_total_category']) ||
  151. (isset($this->params['only_total_category']) &&
  152. $this->params['only_total_category'] == false)
  153. ) {
  154. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  155. $item = $this->evals_links[$count + $items_start];
  156. $weight = round(100*$item->get_weight()/$main_weight,1);
  157. $headers[] = $item->get_name().' '.$weight.' % ';
  158. $evaluationsAdded[] = $item->get_id();
  159. }
  160. }
  161. }
  162. if (!empty($mainCategoryId)) {
  163. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  164. /** @var AbstractLink $item */
  165. $item = $this->evals_links[$count + $items_start];
  166. if ($mainCategoryId == $item->get_category_id() && !in_array($item->get_id(), $evaluationsAdded)) {
  167. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  168. $headers[] = $item->get_name() . ' ' . $weight . ' % ';
  169. }
  170. }
  171. }
  172. $headers[] = api_strtoupper(get_lang('GradebookQualificationTotal'));
  173. return $headers;
  174. }
  175. /**
  176. * @param int $id
  177. *
  178. * @return int
  179. */
  180. public function get_max_result_by_link($id)
  181. {
  182. $max = 0;
  183. foreach ($this->users as $user) {
  184. $item = $this->evals_links [$id];
  185. $score = $item->calc_score($user[0]);
  186. if ($score[0] > $max) {
  187. $max = $score[0];
  188. }
  189. }
  190. return $max ;
  191. }
  192. /**
  193. * Get array containing evaluation items
  194. */
  195. public function get_evaluation_items($items_start = 0, $items_count = null)
  196. {
  197. $headers = array();
  198. if (!isset($items_count)) {
  199. $items_count = count($this->evals_links) - $items_start;
  200. }
  201. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  202. $item = $this->evals_links [$count + $items_start];
  203. $headers[] = $item->get_name();
  204. }
  205. return $headers;
  206. }
  207. /**
  208. * Get actual array data
  209. * @return array 2-dimensional array - each array contains the elements:
  210. * 0: user id
  211. * 1: user lastname
  212. * 2: user firstname
  213. * 3+: evaluation/link scores
  214. */
  215. public function get_data(
  216. $users_sorting = 0,
  217. $users_start = 0,
  218. $users_count = null,
  219. $items_start = 0,
  220. $items_count = null,
  221. $ignore_score_color = false,
  222. $show_all = false
  223. ) {
  224. // do some checks on users/items counts, redefine if invalid values
  225. if (!isset($users_count)) {
  226. $users_count = count ($this->users) - $users_start;
  227. }
  228. if ($users_count < 0) {
  229. $users_count = 0;
  230. }
  231. if (!isset($items_count)) {
  232. $items_count = count ($this->evals) + count ($this->links) - $items_start;
  233. }
  234. if ($items_count < 0) {
  235. $items_count = 0;
  236. }
  237. // copy users to a new array that we will sort
  238. // TODO - needed ?
  239. $userTable = array();
  240. foreach ($this->users as $user) {
  241. $userTable[] = $user;
  242. }
  243. // sort users array
  244. if ($users_sorting & self :: FVDG_SORT_LASTNAME) {
  245. usort($userTable, array ('FlatViewDataGenerator','sort_by_last_name'));
  246. } elseif ($users_sorting & self :: FVDG_SORT_FIRSTNAME) {
  247. usort($userTable, array ('FlatViewDataGenerator','sort_by_first_name'));
  248. }
  249. if ($users_sorting & self :: FVDG_SORT_DESC) {
  250. $userTable = array_reverse($userTable);
  251. }
  252. // select the requested users
  253. $selected_users = array_slice($userTable, $users_start, $users_count);
  254. // generate actual data array
  255. $scoredisplay = ScoreDisplay :: instance();
  256. $data = array ();
  257. $displaytype = SCORE_DIV;
  258. if ($ignore_score_color) {
  259. $displaytype |= SCORE_IGNORE_SPLIT;
  260. }
  261. //@todo move these in a function
  262. $sum_categories_weight_array = array();
  263. $mainCategoryId = null;
  264. $mainCourseCategory = $this->getMainCourseCategory();
  265. if (!empty($mainCourseCategory)) {
  266. $mainCategoryId = $mainCourseCategory->get_id();
  267. }
  268. if (isset($this->category) && !empty($this->category)) {
  269. $categories = Category::load(null, null, null, $this->category->get_id());
  270. if (!empty($categories)) {
  271. foreach($categories as $category) {
  272. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  273. }
  274. } else {
  275. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  276. }
  277. }
  278. $parent_id = $this->category->get_parent_id();
  279. if ($parent_id == 0 or $this->params['only_subcat'] == $this->category->get_id()) {
  280. $main_weight = $this->category->get_weight();
  281. $grade_model_id = $this->category->get_grade_model_id();
  282. } else {
  283. $main_cat = Category::load($parent_id, null, null);
  284. $main_weight = $main_cat[0]->get_weight();
  285. $grade_model_id = $main_cat[0]->get_grade_model_id();
  286. }
  287. $use_grade_model = true;
  288. if (empty($grade_model_id) || $grade_model_id == -1) {
  289. $use_grade_model = false;
  290. }
  291. $export_to_pdf = false;
  292. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  293. $export_to_pdf = true;
  294. }
  295. foreach ($selected_users as $user) {
  296. $row = array();
  297. if ($export_to_pdf) {
  298. $row['user_id'] = $user_id = $user[0]; //user id
  299. } else {
  300. $row[] = $user_id = $user[0]; //user id
  301. }
  302. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  303. if ($export_to_pdf) {
  304. $row['official_code'] = $user[4]; //official code
  305. } else {
  306. $row[] = $user[4]; //official code
  307. }
  308. }
  309. if (isset($this->params['join_firstname_lastname']) && $this->params['join_firstname_lastname']) {
  310. if ($export_to_pdf) {
  311. $row['name'] = api_get_person_name($user[3], $user[2]); //last name
  312. } else {
  313. $row[] = api_get_person_name($user[3], $user[2]); //last name
  314. }
  315. } else {
  316. if ($export_to_pdf) {
  317. if (api_is_western_name_order()) {
  318. $row['firstname'] = $user[3];
  319. $row['lastname'] = $user[2];
  320. } else {
  321. $row['lastname'] = $user[2];
  322. $row['firstname'] = $user[3];
  323. }
  324. } else {
  325. if (api_is_western_name_order()) {
  326. $row[] = $user[3]; //first name
  327. $row[] = $user[2]; //last name
  328. } else {
  329. $row[] = $user[2]; //last name
  330. $row[] = $user[3]; //first name
  331. }
  332. }
  333. }
  334. $item_value = 0;
  335. $item_value_total = 0;
  336. $item_total = 0;
  337. $convert_using_the_global_weight = true;
  338. $course_code = api_get_course_id();
  339. $session_id = api_get_session_id();
  340. $allcat = $this->category->get_subcategories(null, $course_code, $session_id, 'ORDER BY id');
  341. $evaluationsAdded = array();
  342. if ($parent_id == 0 && !empty($allcat)) {
  343. foreach ($allcat as $sub_cat) {
  344. $score = $sub_cat->calc_score($user_id);
  345. $real_score = $score;
  346. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  347. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  348. $item_value = $score[0]/$divide*$main_weight;
  349. //Fixing total when using one or multiple gradebooks
  350. $percentage = $sub_cat->get_weight()/($sub_cat_percentage) * $sub_cat_percentage/$this->category->get_weight();
  351. $item_value = $percentage*$item_value;
  352. $item_total += $sub_cat->get_weight();
  353. /*
  354. if ($convert_using_the_global_weight) {
  355. $score[0] = $score[0]/$main_weight*$sub_cat->get_weight();
  356. $score[1] = $main_weight ;
  357. }
  358. */
  359. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  360. //if (true) {
  361. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  362. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  363. $temp_score = Display::tip($real_score, $temp_score);
  364. } else {
  365. $real_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  366. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  367. $temp_score = Display::tip($temp_score, $real_score);
  368. }
  369. if (!isset($this->params['only_total_category']) ||
  370. (isset($this->params['only_total_category']) &&
  371. $this->params['only_total_category'] == false)
  372. ) {
  373. if (!$show_all) {
  374. $row[] = $temp_score.' ';
  375. } else {
  376. $row[] = $temp_score;
  377. }
  378. }
  379. $item_value_total +=$item_value;
  380. }
  381. if ($convert_using_the_global_weight) {
  382. //$item_total = $main_weight;
  383. }
  384. } else {
  385. $result = $this->parseEvaluations(
  386. $user_id,
  387. $sum_categories_weight_array,
  388. $items_count,
  389. $items_start,
  390. $show_all,
  391. $row
  392. );
  393. $item_total += $result['item_total'];
  394. $item_value_total += $result['item_value_total'];
  395. $evaluationsAdded = $result['evaluations_added'];
  396. $item_total = $main_weight;
  397. }
  398. // All evaluations
  399. $result = $this->parseEvaluations(
  400. $user_id,
  401. $sum_categories_weight_array,
  402. $items_count,
  403. $items_start,
  404. $show_all,
  405. $row,
  406. $mainCategoryId,
  407. $evaluationsAdded
  408. );
  409. $item_total += $result['item_total'];
  410. $item_value_total += $result['item_value_total'];
  411. $total_score = array($item_value_total, $item_total);
  412. if (!$show_all) {
  413. if ($export_to_pdf) {
  414. $row['total'] = $scoredisplay->display_score($total_score);
  415. } else {
  416. $row[] = $scoredisplay->display_score($total_score);
  417. }
  418. } else {
  419. if ($export_to_pdf) {
  420. $row['total'] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  421. } else {
  422. $row[] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  423. }
  424. }
  425. unset($score);
  426. $data[] = $row;
  427. }
  428. return $data;
  429. }
  430. /**
  431. * Parse evaluations
  432. *
  433. * @param int $user_id
  434. * @param array $sum_categories_weight_array
  435. * @param int $items_count
  436. * @param int $items_start
  437. * @param int $show_all
  438. * @param int $parentCategoryIdFilter filter by category id if set
  439. * @return array
  440. */
  441. public function parseEvaluations(
  442. $user_id,
  443. $sum_categories_weight_array,
  444. $items_count,
  445. $items_start,
  446. $show_all,
  447. & $row,
  448. $parentCategoryIdFilter = null,
  449. $evaluationsAlreadyAdded = array()
  450. ) {
  451. // Generate actual data array
  452. $scoredisplay = ScoreDisplay :: instance();
  453. $item_total = 0;
  454. $item_value_total = 0;
  455. $evaluationsAdded = array();
  456. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  457. $item = $this->evals_links[$count + $items_start];
  458. if (!empty($evaluationsAlreadyAdded)) {
  459. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  460. continue;
  461. }
  462. }
  463. if (!empty($parentCategoryIdFilter)) {
  464. if ($item->get_category_id() != $parentCategoryIdFilter) {
  465. continue;
  466. }
  467. }
  468. $evaluationsAdded[] = $item->get_id();
  469. $score = $item->calc_score($user_id);
  470. $real_score = $score;
  471. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  472. //sub cat weight
  473. $sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
  474. $item_value = $score[0]/$divide;
  475. //Fixing total when using one or multiple gradebooks
  476. if (empty($parentCategoryIdFilter)) {
  477. if ($this->category->get_parent_id() == 0 ) {
  478. $item_value = $score[0]/$divide*$item->get_weight();
  479. } else {
  480. $item_value = $item_value*$item->get_weight();
  481. }
  482. } else {
  483. $item_value = $score[0] / $divide * $item->get_weight();
  484. }
  485. $item_total += $item->get_weight();
  486. $complete_score = $scoredisplay->display_score($score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  487. //if (true) {
  488. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  489. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  490. $temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  491. $temp_score = Display::tip($real_score, $temp_score);
  492. } else {
  493. $temp_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  494. $temp_score = Display::tip($temp_score, $complete_score);
  495. }
  496. if (!isset($this->params['only_total_category']) ||
  497. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  498. ) {
  499. if (!$show_all) {
  500. if (in_array($item->get_type(), array(
  501. LINK_EXERCISE,
  502. LINK_DROPBOX,
  503. LINK_STUDENTPUBLICATION,
  504. LINK_LEARNPATH,
  505. LINK_FORUM_THREAD,
  506. LINK_ATTENDANCE,
  507. LINK_SURVEY,
  508. LINK_HOTPOTATOES)
  509. )
  510. ) {
  511. if (!empty($score[0])) {
  512. $row[] = $temp_score.' ';
  513. } else {
  514. $row[] = '';
  515. }
  516. } else {
  517. $row[] = $temp_score.' ';
  518. }
  519. } else {
  520. $row[] = $temp_score;
  521. }
  522. }
  523. $item_value_total +=$item_value;
  524. }
  525. return array(
  526. 'item_total' => $item_total,
  527. 'item_value_total' => $item_value_total,
  528. 'evaluations_added' => $evaluationsAdded
  529. );
  530. }
  531. /**
  532. * Get actual array data evaluation/link scores
  533. */
  534. public function get_evaluation_sumary_results ($session_id = null)
  535. {
  536. $usertable = array ();
  537. foreach ($this->users as $user) {
  538. $usertable[] = $user;
  539. }
  540. $selected_users = $usertable;
  541. // generate actual data array for all selected users
  542. $data = array();
  543. foreach ($selected_users as $user) {
  544. $row = array ();
  545. for ($count=0;$count < count($this->evals_links); $count++) {
  546. $item = $this->evals_links [$count];
  547. $score = $item->calc_score($user[0]);
  548. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0]*100)/$score[1] :0;
  549. $row[$item->get_name()] = $porcent_score;
  550. }
  551. $data[$user[0]] = $row;
  552. }
  553. // get evaluations for every user by item
  554. $data_by_item = array();
  555. foreach ($data as $uid => $items) {
  556. $tmp = array();
  557. foreach ($items as $item => $value) {
  558. $tmp[] = $item;
  559. if (in_array($item,$tmp)) {
  560. $data_by_item[$item][$uid] = $value;
  561. }
  562. }
  563. }
  564. // get evaluation sumary results (maximum, minimum and average of evaluations for all students)
  565. $result = array();
  566. $maximum = $minimum = $average = 0;
  567. foreach ($data_by_item as $k => $v) {
  568. $average = round(array_sum($v)/count($v));
  569. arsort($v);
  570. $maximum = array_shift($v);
  571. $minimum = array_pop($v);
  572. $sumary_item = array('max'=>$maximum, 'min'=>$minimum, 'avg'=>$average);
  573. $result[$k] = $sumary_item;
  574. }
  575. return $result;
  576. }
  577. /**
  578. * @return array
  579. */
  580. public function get_data_to_graph()
  581. {
  582. // do some checks on users/items counts, redefine if invalid values
  583. $usertable = array ();
  584. foreach ($this->users as $user) {
  585. $usertable[] = $user;
  586. }
  587. // sort users array
  588. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  589. $data = array ();
  590. $selected_users = $usertable;
  591. foreach ($selected_users as $user) {
  592. $row = array ();
  593. $row[] = $user[0]; // user id
  594. $item_value = 0;
  595. $item_total = 0;
  596. for ($count=0;$count < count($this->evals_links); $count++) {
  597. $item = $this->evals_links[$count];
  598. $score = $item->calc_score($user[0]);
  599. $divide =( ($score[1])==0 ) ? 1 : $score[1];
  600. $item_value += $score[0]/$divide*$item->get_weight();
  601. $item_total += $item->get_weight();
  602. $score_denom = ($score[1]==0) ? 1 : $score[1];
  603. $score_final = ($score[0] / $score_denom) * 100;
  604. $row[] = $score_final;
  605. }
  606. $total_score = array($item_value, $item_total);
  607. $score_final = ($item_value / $item_total) * 100;
  608. $row[] = $score_final;
  609. $data[] = $row;
  610. }
  611. return $data;
  612. }
  613. /**
  614. * This is a function to show the generated data
  615. * @param bool $displayWarning
  616. * @return array
  617. */
  618. public function get_data_to_graph2($displayWarning = true)
  619. {
  620. // do some checks on users/items counts, redefine if invalid values
  621. $usertable = array ();
  622. foreach ($this->users as $user) {
  623. $usertable[] = $user;
  624. }
  625. // sort users array
  626. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  627. // generate actual data array
  628. $scoredisplay = ScoreDisplay :: instance();
  629. $data= array ();
  630. $displaytype = SCORE_DIV;
  631. $selected_users = $usertable;
  632. foreach ($selected_users as $user) {
  633. $row = array ();
  634. $row[] = $user[0]; // user id
  635. $item_value=0;
  636. $item_total=0;
  637. $final_score = 0;
  638. $item_value_total = 0;
  639. $convert_using_the_global_weight = true;
  640. $course_code = api_get_course_id();
  641. $session_id = api_get_session_id();
  642. $allcat = $this->category->get_subcategories(null, $course_code, $session_id, 'ORDER BY id');
  643. if ($parent_id == 0 && !empty($allcat)) {
  644. foreach ($allcat as $sub_cat) {
  645. $score = $sub_cat->calc_score($user[0]);
  646. $real_score = $score;
  647. $main_weight = $this->category->get_weight();
  648. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  649. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  650. $item_value = $score[0]/$divide*$main_weight;
  651. $item_total += $sub_cat->get_weight();
  652. $row[] = array ($item_value, trim($scoredisplay->display_score($real_score, SCORE_CUSTOM,null, true)));
  653. $item_value_total += $item_value;
  654. $final_score += $score[0];
  655. //$final_score = ($final_score / $item_total) * 100;
  656. }
  657. $total_score = array($final_score, $item_total);
  658. $row[] = array ($final_score, trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true)));
  659. } else {
  660. for ($count=0;$count < count($this->evals_links); $count++) {
  661. $item = $this->evals_links [$count];
  662. $score = $item->calc_score($user[0]);
  663. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  664. $item_value+= $score[0]/$divide*$item->get_weight();
  665. $item_total+=$item->get_weight();
  666. $score_denom=($score[1]==0) ? 1 : $score[1];
  667. $score_final = ($score[0] / $score_denom) * 100;
  668. $row[] = array ($score_final, trim($scoredisplay->display_score($score, SCORE_CUSTOM,null, true)));
  669. }
  670. $total_score=array($item_value,$item_total);
  671. $score_final = ($item_value / $item_total) * 100;
  672. if ($displayWarning) {
  673. Display::display_warning_message( Display::display_warning_message($total_score[1]));
  674. }
  675. $row[] =array ($score_final, trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true)));
  676. }
  677. $data[] = $row;
  678. }
  679. return $data;
  680. }
  681. // Sort functions - used internally
  682. public function sort_by_last_name($item1, $item2)
  683. {
  684. return api_strcmp($item1[2], $item2[2]);
  685. }
  686. public function sort_by_first_name($item1, $item2)
  687. {
  688. return api_strcmp($item1[3], $item2[3]);
  689. }
  690. }