flatview_data_generator.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. $mainHeader = Display::url(
  148. $sub_cat->get_name(),
  149. api_get_self().'?selectcat='.$sub_cat->get_id().'&'.api_get_cidreq()
  150. ).$add_weight;
  151. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  152. $links = $sub_cat->get_links();
  153. $evaluations = $sub_cat->get_evaluations();
  154. /** @var ExerciseLink $link */
  155. $linkNameList = [];
  156. foreach ($links as $link) {
  157. $linkNameList[] = $link->get_name();
  158. }
  159. $evalNameList = [];
  160. foreach ($evaluations as $evaluation) {
  161. $evalNameList[] = $evaluation->get_name();
  162. }
  163. $finalList = array_merge($linkNameList, $evalNameList);
  164. if (!empty($finalList)) {
  165. $finalList[] = get_lang('Average');
  166. }
  167. $list = [];
  168. $list['items'] = $finalList;
  169. $list['header'] = '<center>'.$mainHeader.'</center>';
  170. $headers[]= $list;
  171. } else {
  172. $headers[] = '<center>'.$mainHeader.'</center>';
  173. }
  174. }
  175. } else {
  176. if (!isset($this->params['only_total_category']) ||
  177. (isset($this->params['only_total_category']) &&
  178. $this->params['only_total_category'] == false)
  179. ) {
  180. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  181. /** @var AbstractLink $item */
  182. $item = $this->evals_links[$count + $items_start];
  183. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  184. $headers[] = $item->get_name().' '.$weight.' % ';
  185. $evaluationsAdded[] = $item->get_id();
  186. }
  187. }
  188. }
  189. if (!empty($mainCategoryId)) {
  190. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  191. /** @var AbstractLink $item */
  192. $item = $this->evals_links[$count + $items_start];
  193. if ($mainCategoryId == $item->get_category_id() &&
  194. !in_array($item->get_id(), $evaluationsAdded)
  195. ) {
  196. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  197. $headers[] = $item->get_name() . ' ' . $weight . ' % ';
  198. }
  199. }
  200. }
  201. $headers[] = '<center>'.api_strtoupper(get_lang('GradebookQualificationTotal')).'</center>';
  202. return $headers;
  203. }
  204. /**
  205. * @param int $id
  206. *
  207. * @return int
  208. */
  209. public function get_max_result_by_link($id)
  210. {
  211. $max = 0;
  212. foreach ($this->users as $user) {
  213. $item = $this->evals_links [$id];
  214. $score = $item->calc_score($user[0]);
  215. if ($score[0] > $max) {
  216. $max = $score[0];
  217. }
  218. }
  219. return $max;
  220. }
  221. /**
  222. * Get array containing evaluation items
  223. * @return array
  224. */
  225. public function get_evaluation_items($items_start = 0, $items_count = null)
  226. {
  227. $headers = array();
  228. if (!isset($items_count)) {
  229. $items_count = count($this->evals_links) - $items_start;
  230. }
  231. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  232. $item = $this->evals_links [$count + $items_start];
  233. $headers[] = $item->get_name();
  234. }
  235. return $headers;
  236. }
  237. /**
  238. * Get actual array data
  239. * @return array 2-dimensional array - each array contains the elements:
  240. * 0: user id
  241. * 1: user lastname
  242. * 2: user firstname
  243. * 3+: evaluation/link scores
  244. */
  245. public function get_data(
  246. $users_sorting = 0,
  247. $users_start = 0,
  248. $users_count = null,
  249. $items_start = 0,
  250. $items_count = null,
  251. $ignore_score_color = false,
  252. $show_all = false
  253. ) {
  254. // Do some checks on users/items counts, redefine if invalid values
  255. if (!isset($users_count)) {
  256. $users_count = count ($this->users) - $users_start;
  257. }
  258. if ($users_count < 0) {
  259. $users_count = 0;
  260. }
  261. if (!isset($items_count)) {
  262. $items_count = count ($this->evals) + count ($this->links) - $items_start;
  263. }
  264. if ($items_count < 0) {
  265. $items_count = 0;
  266. }
  267. $userTable = array();
  268. foreach ($this->users as $user) {
  269. $userTable[] = $user;
  270. }
  271. // sort users array
  272. if ($users_sorting & self :: FVDG_SORT_LASTNAME) {
  273. usort($userTable, array('FlatViewDataGenerator','sort_by_last_name'));
  274. } elseif ($users_sorting & self :: FVDG_SORT_FIRSTNAME) {
  275. usort($userTable, array('FlatViewDataGenerator','sort_by_first_name'));
  276. }
  277. if ($users_sorting & self :: FVDG_SORT_DESC) {
  278. $userTable = array_reverse($userTable);
  279. }
  280. // Select the requested users
  281. $selected_users = array_slice($userTable, $users_start, $users_count);
  282. // Generate actual data array
  283. $scoredisplay = ScoreDisplay::instance();
  284. $data = array();
  285. $displaytype = SCORE_DIV;
  286. if ($ignore_score_color) {
  287. $displaytype |= SCORE_IGNORE_SPLIT;
  288. }
  289. //@todo move these in a function
  290. $sum_categories_weight_array = array();
  291. $mainCategoryId = null;
  292. $mainCourseCategory = $this->getMainCourseCategory();
  293. if (!empty($mainCourseCategory)) {
  294. $mainCategoryId = $mainCourseCategory->get_id();
  295. }
  296. if (isset($this->category) && !empty($this->category)) {
  297. $categories = Category::load(null, null, null, $this->category->get_id());
  298. if (!empty($categories)) {
  299. foreach ($categories as $category) {
  300. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  301. }
  302. } else {
  303. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  304. }
  305. }
  306. $parent_id = $this->category->get_parent_id();
  307. if ($parent_id == 0 or $this->params['only_subcat'] == $this->category->get_id()) {
  308. $main_weight = $this->category->get_weight();
  309. $grade_model_id = $this->category->get_grade_model_id();
  310. } else {
  311. $main_cat = Category::load($parent_id, null, null);
  312. $main_weight = $main_cat[0]->get_weight();
  313. $grade_model_id = $main_cat[0]->get_grade_model_id();
  314. }
  315. $use_grade_model = true;
  316. if (empty($grade_model_id) || $grade_model_id == -1) {
  317. $use_grade_model = false;
  318. }
  319. $export_to_pdf = false;
  320. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  321. $export_to_pdf = true;
  322. }
  323. foreach ($selected_users as $user) {
  324. $row = array();
  325. // User id
  326. if ($export_to_pdf) {
  327. $row['user_id'] = $user_id = $user[0];
  328. } else {
  329. $row[] = $user_id = $user[0];
  330. }
  331. // Official code
  332. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  333. if ($export_to_pdf) {
  334. $row['official_code'] = $user[4];
  335. } else {
  336. $row[] = $user[4];
  337. }
  338. }
  339. // Last name
  340. if (isset($this->params['join_firstname_lastname']) &&
  341. $this->params['join_firstname_lastname']
  342. ) {
  343. if ($export_to_pdf) {
  344. $row['name'] = api_get_person_name($user[3], $user[2]);
  345. } else {
  346. $row[] = api_get_person_name($user[3], $user[2]);
  347. }
  348. } else {
  349. if ($export_to_pdf) {
  350. if (api_is_western_name_order()) {
  351. $row['firstname'] = $user[3];
  352. $row['lastname'] = $user[2];
  353. } else {
  354. $row['lastname'] = $user[2];
  355. $row['firstname'] = $user[3];
  356. }
  357. } else {
  358. if (api_is_western_name_order()) {
  359. $row[] = $user[3]; //first name
  360. $row[] = $user[2]; //last name
  361. } else {
  362. $row[] = $user[2]; //last name
  363. $row[] = $user[3]; //first name
  364. }
  365. }
  366. }
  367. $item_value = 0;
  368. $item_value_total = 0;
  369. $item_total = 0;
  370. $convert_using_the_global_weight = true;
  371. $course_code = api_get_course_id();
  372. $session_id = api_get_session_id();
  373. $allcat = $this->category->get_subcategories(
  374. null,
  375. $course_code,
  376. $session_id,
  377. 'ORDER BY id'
  378. );
  379. $evaluationsAdded = array();
  380. if ($parent_id == 0 && !empty($allcat)) {
  381. /** @var Category $sub_cat */
  382. foreach ($allcat as $sub_cat) {
  383. $score = $sub_cat->calc_score($user_id);
  384. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  385. $links = $sub_cat->get_links();
  386. $evaluations = $sub_cat->get_evaluations();
  387. /** @var ExerciseLink $link */
  388. $linkScoreList = [];
  389. foreach ($links as $link) {
  390. $linkScore = $link->calc_score($user_id);
  391. $linkScoreList[] = $scoredisplay->display_score(
  392. $linkScore,
  393. SCORE_SIMPLE
  394. );
  395. }
  396. $evalScoreList = [];
  397. foreach ($evaluations as $evaluation) {
  398. $evalScore = $evaluation->calc_score($user_id);
  399. $evalScoreList[] = $scoredisplay->display_score(
  400. $evalScore,
  401. SCORE_SIMPLE
  402. );
  403. }
  404. }
  405. $real_score = $score;
  406. $divide = $score[1] == 0 ? 1 : $score[1];
  407. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  408. $item_value = $score[0]/$divide*$main_weight;
  409. // Fixing total when using one or multiple gradebooks
  410. $percentage = $sub_cat->get_weight()/($sub_cat_percentage) * $sub_cat_percentage/$this->category->get_weight();
  411. $item_value = $percentage*$item_value;
  412. $item_total += $sub_cat->get_weight();
  413. /*
  414. if ($convert_using_the_global_weight) {
  415. $score[0] = $score[0]/$main_weight*$sub_cat->get_weight();
  416. $score[1] = $main_weight ;
  417. }
  418. */
  419. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  420. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  421. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  422. $temp_score = Display::tip($real_score, $temp_score);
  423. } else {
  424. $real_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  425. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  426. $temp_score = Display::tip($temp_score, $real_score);
  427. }
  428. if (!isset($this->params['only_total_category']) ||
  429. (isset($this->params['only_total_category']) &&
  430. $this->params['only_total_category'] == false)
  431. ) {
  432. if (!$show_all) {
  433. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  434. $finalList = array_merge($linkScoreList, $evalScoreList);
  435. if (empty($finalList)) {
  436. $average = 0;
  437. } else {
  438. $average = array_sum($finalList) / count($finalList);
  439. }
  440. $finalList[] = round($average, 2);
  441. foreach ($finalList as $finalValue) {
  442. $row[] = '<center>'.$finalValue.'</center>';
  443. }
  444. } else {
  445. $row[] = $temp_score.' ';
  446. }
  447. } else {
  448. $row[] = $temp_score;
  449. }
  450. }
  451. $item_value_total += $item_value;
  452. }
  453. if ($convert_using_the_global_weight) {
  454. //$item_total = $main_weight;
  455. }
  456. } else {
  457. // All evaluations
  458. $result = $this->parseEvaluations(
  459. $user_id,
  460. $sum_categories_weight_array,
  461. $items_count,
  462. $items_start,
  463. $show_all,
  464. $row
  465. );
  466. $item_total += $result['item_total'];
  467. $item_value_total += $result['item_value_total'];
  468. $evaluationsAdded = $result['evaluations_added'];
  469. $item_total = $main_weight;
  470. }
  471. // All evaluations
  472. $result = $this->parseEvaluations(
  473. $user_id,
  474. $sum_categories_weight_array,
  475. $items_count,
  476. $items_start,
  477. $show_all,
  478. $row,
  479. $mainCategoryId,
  480. $evaluationsAdded
  481. );
  482. $item_total += $result['item_total'];
  483. $item_value_total += $result['item_value_total'];
  484. $total_score = array($item_value_total, $item_total);
  485. if (!$show_all) {
  486. if ($export_to_pdf) {
  487. $row['total'] = $scoredisplay->display_score($total_score);
  488. } else {
  489. $row[] = $scoredisplay->display_score($total_score);
  490. }
  491. } else {
  492. if ($export_to_pdf) {
  493. $row['total'] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  494. } else {
  495. $row[] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  496. }
  497. }
  498. unset($score);
  499. $data[] = $row;
  500. }
  501. return $data;
  502. }
  503. /**
  504. * Parse evaluations
  505. *
  506. * @param int $user_id
  507. * @param array $sum_categories_weight_array
  508. * @param int $items_count
  509. * @param int $items_start
  510. * @param int $show_all
  511. * @param int $parentCategoryIdFilter filter by category id if set
  512. * @return array
  513. */
  514. public function parseEvaluations(
  515. $user_id,
  516. $sum_categories_weight_array,
  517. $items_count,
  518. $items_start,
  519. $show_all,
  520. & $row,
  521. $parentCategoryIdFilter = null,
  522. $evaluationsAlreadyAdded = array()
  523. ) {
  524. // Generate actual data array
  525. $scoredisplay = ScoreDisplay :: instance();
  526. $item_total = 0;
  527. $item_value_total = 0;
  528. $evaluationsAdded = array();
  529. for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) {
  530. /** @var AbstractLink $item */
  531. $item = $this->evals_links[$count + $items_start];
  532. if (!empty($evaluationsAlreadyAdded)) {
  533. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  534. continue;
  535. }
  536. }
  537. if (!empty($parentCategoryIdFilter)) {
  538. if ($item->get_category_id() != $parentCategoryIdFilter) {
  539. continue;
  540. }
  541. }
  542. $evaluationsAdded[] = $item->get_id();
  543. $score = $item->calc_score($user_id);
  544. $real_score = $score;
  545. $divide = isset($score[1]) && !empty($score[1]) ? $score[1] : 1;
  546. // Sub cat weight
  547. //$sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
  548. $item_value = isset($score[0]) ? $score[0]/$divide : 0;
  549. // Fixing total when using one or multiple gradebooks.
  550. if (empty($parentCategoryIdFilter)) {
  551. if ($this->category->get_parent_id() == 0) {
  552. $item_value = $score[0] / $divide * $item->get_weight();
  553. } else {
  554. $item_value = $item_value * $item->get_weight();
  555. }
  556. } else {
  557. $item_value = $score[0] / $divide * $item->get_weight();
  558. }
  559. $item_total += $item->get_weight();
  560. $complete_score = $scoredisplay->display_score($score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  561. //$temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  562. //$temp_score = Display::tip($temp_score, $complete_score);
  563. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  564. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  565. $temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  566. $temp_score = Display::tip($real_score, $temp_score);
  567. } else {
  568. $temp_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  569. $temp_score = Display::tip($temp_score, $complete_score);
  570. }
  571. if (!isset($this->params['only_total_category']) ||
  572. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  573. ) {
  574. if (!$show_all) {
  575. if (in_array($item->get_type(), array(
  576. LINK_EXERCISE,
  577. LINK_DROPBOX,
  578. LINK_STUDENTPUBLICATION,
  579. LINK_LEARNPATH,
  580. LINK_FORUM_THREAD,
  581. LINK_ATTENDANCE,
  582. LINK_SURVEY,
  583. LINK_HOTPOTATOES)
  584. )
  585. ) {
  586. if (!empty($score[0])) {
  587. $row[] = $temp_score.' ';
  588. } else {
  589. $row[] = '';
  590. }
  591. } else {
  592. $row[] = $temp_score.' ';
  593. }
  594. } else {
  595. $row[] = $temp_score;
  596. }
  597. }
  598. $item_value_total += $item_value;
  599. }
  600. return array(
  601. 'item_total' => $item_total,
  602. 'item_value_total' => $item_value_total,
  603. 'evaluations_added' => $evaluationsAdded
  604. );
  605. }
  606. /**
  607. * Get actual array data evaluation/link scores
  608. * @param int $session_id
  609. * @return array
  610. */
  611. public function get_evaluation_sumary_results($session_id = null)
  612. {
  613. $usertable = array();
  614. foreach ($this->users as $user) {
  615. $usertable[] = $user;
  616. }
  617. $selected_users = $usertable;
  618. // generate actual data array for all selected users
  619. $data = array();
  620. foreach ($selected_users as $user) {
  621. $row = array ();
  622. for ($count=0;$count < count($this->evals_links); $count++) {
  623. $item = $this->evals_links [$count];
  624. $score = $item->calc_score($user[0]);
  625. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0]*100)/$score[1] :0;
  626. $row[$item->get_name()] = $porcent_score;
  627. }
  628. $data[$user[0]] = $row;
  629. }
  630. // get evaluations for every user by item
  631. $data_by_item = array();
  632. foreach ($data as $uid => $items) {
  633. $tmp = array();
  634. foreach ($items as $item => $value) {
  635. $tmp[] = $item;
  636. if (in_array($item,$tmp)) {
  637. $data_by_item[$item][$uid] = $value;
  638. }
  639. }
  640. }
  641. /* Get evaluation summary results
  642. (maximum, minimum and average of evaluations for all students)
  643. */
  644. $result = array();
  645. foreach ($data_by_item as $k => $v) {
  646. $average = round(array_sum($v)/count($v));
  647. arsort($v);
  648. $maximum = array_shift($v);
  649. $minimum = array_pop($v);
  650. if (is_null($minimum)) {
  651. $minimum = 0;
  652. }
  653. $summary= array(
  654. 'max' => $maximum,
  655. 'min' => $minimum,
  656. 'avg' => $average
  657. );
  658. $result[$k] = $summary;
  659. }
  660. return $result;
  661. }
  662. /**
  663. * @return array
  664. */
  665. public function get_data_to_graph()
  666. {
  667. // do some checks on users/items counts, redefine if invalid values
  668. $usertable = array ();
  669. foreach ($this->users as $user) {
  670. $usertable[] = $user;
  671. }
  672. // sort users array
  673. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  674. $data = array ();
  675. $selected_users = $usertable;
  676. foreach ($selected_users as $user) {
  677. $row = array ();
  678. $row[] = $user[0]; // user id
  679. $item_value = 0;
  680. $item_total = 0;
  681. for ($count=0;$count < count($this->evals_links); $count++) {
  682. $item = $this->evals_links[$count];
  683. $score = $item->calc_score($user[0]);
  684. $divide =( ($score[1])==0 ) ? 1 : $score[1];
  685. $item_value += $score[0]/$divide*$item->get_weight();
  686. $item_total += $item->get_weight();
  687. $score_denom = ($score[1]==0) ? 1 : $score[1];
  688. $score_final = ($score[0] / $score_denom) * 100;
  689. $row[] = $score_final;
  690. }
  691. //$total_score = array($item_value, $item_total);
  692. $score_final = ($item_value / $item_total) * 100;
  693. $row[] = $score_final;
  694. $data[] = $row;
  695. }
  696. return $data;
  697. }
  698. /**
  699. * This is a function to show the generated data
  700. * @param bool $displayWarning
  701. * @return array
  702. */
  703. public function get_data_to_graph2($displayWarning = true)
  704. {
  705. // do some checks on users/items counts, redefine if invalid values
  706. $usertable = array();
  707. foreach ($this->users as $user) {
  708. $usertable[] = $user;
  709. }
  710. // sort users array
  711. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  712. // generate actual data array
  713. $scoredisplay = ScoreDisplay :: instance();
  714. $data = array();
  715. $selected_users = $usertable;
  716. foreach ($selected_users as $user) {
  717. $row = array ();
  718. $row[] = $user[0]; // user id
  719. $item_value = 0;
  720. $item_total = 0;
  721. $final_score = 0;
  722. $item_value_total = 0;
  723. $convert_using_the_global_weight = true;
  724. $course_code = api_get_course_id();
  725. $session_id = api_get_session_id();
  726. $allcat = $this->category->get_subcategories(null, $course_code, $session_id, 'ORDER BY id');
  727. $parent_id = $this->category->get_parent_id();
  728. if ($parent_id == 0 && !empty($allcat)) {
  729. foreach ($allcat as $sub_cat) {
  730. $score = $sub_cat->calc_score($user[0]);
  731. $real_score = $score;
  732. $main_weight = $this->category->get_weight();
  733. $divide = $score[1] == 0 ? 1 : $score[1];
  734. //$sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  735. $item_value = $score[0]/$divide*$main_weight;
  736. $item_total += $sub_cat->get_weight();
  737. $row[] = array(
  738. $item_value,
  739. trim($scoredisplay->display_score($real_score, SCORE_CUSTOM, null, true))
  740. );
  741. $item_value_total += $item_value;
  742. $final_score += $score[0];
  743. //$final_score = ($final_score / $item_total) * 100;
  744. }
  745. $total_score = array($final_score, $item_total);
  746. $row[] = array(
  747. $final_score,
  748. trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true))
  749. );
  750. } else {
  751. for ($count=0;$count < count($this->evals_links); $count++) {
  752. $item = $this->evals_links [$count];
  753. $score = $item->calc_score($user[0]);
  754. $divide = ($score[1]) == 0 ? 1 : $score[1];
  755. $item_value += $score[0] / $divide * $item->get_weight();
  756. $item_total+=$item->get_weight();
  757. $score_denom=($score[1]==0) ? 1 : $score[1];
  758. $score_final = ($score[0] / $score_denom) * 100;
  759. $row[] = array ($score_final, trim($scoredisplay->display_score($score, SCORE_CUSTOM,null, true)));
  760. }
  761. $total_score=array($item_value,$item_total);
  762. $score_final = ($item_value / $item_total) * 100;
  763. if ($displayWarning) {
  764. Display::display_warning_message( Display::display_warning_message($total_score[1]));
  765. }
  766. $row[] =array ($score_final, trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true)));
  767. }
  768. $data[] = $row;
  769. }
  770. return $data;
  771. }
  772. /**
  773. * @param $item1
  774. * @param $item2
  775. * @return int
  776. */
  777. public function sort_by_last_name($item1, $item2)
  778. {
  779. return api_strcmp($item1[2], $item2[2]);
  780. }
  781. /**
  782. * @param $item1
  783. * @param $item2
  784. * @return int
  785. */
  786. public function sort_by_first_name($item1, $item2)
  787. {
  788. return api_strcmp($item1[3], $item2[3]);
  789. }
  790. }