flatview_data_generator.class.php 29 KB

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