flatview_data_generator.class.php 32 KB

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