flatview_data_generator.class.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. *
  8. * @author Bert Steppé
  9. *
  10. * @package chamilo.gradebook
  11. */
  12. class FlatViewDataGenerator
  13. {
  14. // Sorting types constants
  15. const FVDG_SORT_LASTNAME = 1;
  16. const FVDG_SORT_FIRSTNAME = 2;
  17. const FVDG_SORT_ASC = 4;
  18. const FVDG_SORT_DESC = 8;
  19. public $params;
  20. /** @var Category */
  21. public $category;
  22. private $users;
  23. private $evals;
  24. private $links;
  25. private $evals_links;
  26. private $mainCourseCategory;
  27. /**
  28. * @param array $users
  29. * @param array $evals
  30. * @param array $links
  31. * @param array $params
  32. * @param Category|null $mainCourseCategory
  33. */
  34. public function __construct(
  35. $users = [],
  36. $evals = [],
  37. $links = [],
  38. $params = [],
  39. $mainCourseCategory = null
  40. ) {
  41. $this->users = isset($users) ? $users : [];
  42. $this->evals = isset($evals) ? $evals : [];
  43. $this->links = isset($links) ? $links : [];
  44. $this->evals_links = array_merge($this->evals, $this->links);
  45. $this->params = $params;
  46. $this->mainCourseCategory = $mainCourseCategory;
  47. }
  48. /**
  49. * @return Category
  50. */
  51. public function getMainCourseCategory()
  52. {
  53. return $this->mainCourseCategory;
  54. }
  55. /**
  56. * Get total number of users (rows).
  57. *
  58. * @return int
  59. */
  60. public function get_total_users_count()
  61. {
  62. return count($this->users);
  63. }
  64. /**
  65. * Get total number of evaluations/links (columns) (the 2 users columns not included).
  66. *
  67. * @return int
  68. */
  69. public function get_total_items_count()
  70. {
  71. return count($this->evals_links);
  72. }
  73. /**
  74. * Get array containing column header names (incl user columns).
  75. *
  76. * @param int $items_start Start item offset
  77. * @param int $items_count Number of items to get
  78. * @param bool $show_detail whether to show the details or not
  79. *
  80. * @return array List of headers
  81. */
  82. public function get_header_names($items_start = 0, $items_count = null, $show_detail = false)
  83. {
  84. $headers = [];
  85. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  86. $headers[] = get_lang('OfficialCode');
  87. }
  88. if (isset($this->params['join_firstname_lastname']) && $this->params['join_firstname_lastname']) {
  89. if (api_is_western_name_order()) {
  90. $headers[] = get_lang('FirstnameAndLastname');
  91. } else {
  92. $headers[] = get_lang('LastnameAndFirstname');
  93. }
  94. } else {
  95. if (api_is_western_name_order()) {
  96. $headers[] = get_lang('FirstName');
  97. $headers[] = get_lang('LastName');
  98. } else {
  99. $headers[] = get_lang('LastName');
  100. $headers[] = get_lang('FirstName');
  101. }
  102. }
  103. $headers[] = get_lang('Username');
  104. if (!isset($items_count)) {
  105. $items_count = count($this->evals_links) - $items_start;
  106. }
  107. $parent_id = $this->category->get_parent_id();
  108. if ($parent_id == 0 ||
  109. isset($this->params['only_subcat']) &&
  110. $this->params['only_subcat'] == $this->category->get_id()
  111. ) {
  112. $main_weight = $this->category->get_weight();
  113. } else {
  114. $main_cat = Category::load($parent_id, null, null);
  115. $main_weight = $main_cat[0]->get_weight();
  116. }
  117. //@todo move these in a function
  118. $sum_categories_weight_array = [];
  119. $mainCategoryId = null;
  120. $mainCourseCategory = $this->getMainCourseCategory();
  121. if (!empty($mainCourseCategory)) {
  122. $mainCategoryId = $mainCourseCategory->get_id();
  123. }
  124. if (isset($this->category) && !empty($this->category)) {
  125. $categories = Category::load(
  126. null,
  127. null,
  128. null,
  129. $this->category->get_id()
  130. );
  131. if (!empty($categories)) {
  132. foreach ($categories as $category) {
  133. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  134. }
  135. } else {
  136. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  137. }
  138. }
  139. // No category was added
  140. $course_code = api_get_course_id();
  141. $session_id = api_get_session_id();
  142. $model = ExerciseLib::getCourseScoreModel();
  143. $allcat = $this->category->get_subcategories(
  144. null,
  145. $course_code,
  146. $session_id,
  147. 'ORDER BY id'
  148. );
  149. $evaluationsAdded = [];
  150. if ($parent_id == 0 && !empty($allcat)) {
  151. // Means there are any subcategory
  152. /** @var Category $sub_cat */
  153. foreach ($allcat as $sub_cat) {
  154. $sub_cat_weight = round(100 * $sub_cat->get_weight() / $main_weight, 1);
  155. $add_weight = " $sub_cat_weight %";
  156. $mainHeader = Display::url(
  157. $sub_cat->get_name(),
  158. api_get_self().'?selectcat='.$sub_cat->get_id().'&'.api_get_cidreq()
  159. ).$add_weight;
  160. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  161. $links = $sub_cat->get_links();
  162. $evaluations = $sub_cat->get_evaluations();
  163. /** @var ExerciseLink $link */
  164. $linkNameList = [];
  165. foreach ($links as $link) {
  166. $linkNameList[] = $link->get_name();
  167. }
  168. $evalNameList = [];
  169. foreach ($evaluations as $evaluation) {
  170. $evalNameList[] = $evaluation->get_name();
  171. }
  172. $finalList = array_merge($linkNameList, $evalNameList);
  173. if (!empty($finalList)) {
  174. $finalList[] = get_lang('Average');
  175. }
  176. $list = [];
  177. $list['items'] = $finalList;
  178. $list['header'] = '<center>'.$mainHeader.'</center>';
  179. $headers[] = $list;
  180. } else {
  181. $headers[] = '<center>'.$mainHeader.'</center>';
  182. }
  183. }
  184. } else {
  185. if (!isset($this->params['only_total_category']) ||
  186. (isset($this->params['only_total_category']) &&
  187. $this->params['only_total_category'] == false)
  188. ) {
  189. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  190. /** @var AbstractLink $item */
  191. $item = $this->evals_links[$count + $items_start];
  192. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  193. $label = $item->get_name().' '.$weight.' % ';
  194. if (!empty($model)) {
  195. $label = $item->get_name();
  196. }
  197. $headers[] = $label;
  198. $evaluationsAdded[] = $item->get_id();
  199. }
  200. }
  201. }
  202. if (!empty($mainCategoryId)) {
  203. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  204. /** @var AbstractLink $item */
  205. $item = $this->evals_links[$count + $items_start];
  206. if ($mainCategoryId == $item->get_category_id() &&
  207. !in_array($item->get_id(), $evaluationsAdded)
  208. ) {
  209. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  210. $label = $item->get_name().' '.$weight.' % ';
  211. if (!empty($model)) {
  212. $label = $item->get_name();
  213. }
  214. $headers[] = $label;
  215. }
  216. }
  217. }
  218. $headers[] = '<center>'.api_strtoupper(get_lang('GradebookQualificationTotal')).'</center>';
  219. return $headers;
  220. }
  221. /**
  222. * @param int $id
  223. *
  224. * @return int
  225. */
  226. public function get_max_result_by_link($id)
  227. {
  228. $max = 0;
  229. foreach ($this->users as $user) {
  230. $item = $this->evals_links[$id];
  231. $score = $item->calc_score($user[0]);
  232. if ($score[0] > $max) {
  233. $max = $score[0];
  234. }
  235. }
  236. return $max;
  237. }
  238. /**
  239. * Get array containing evaluation items.
  240. *
  241. * @return array
  242. */
  243. public function get_evaluation_items($items_start = 0, $items_count = null)
  244. {
  245. $headers = [];
  246. if (!isset($items_count)) {
  247. $items_count = count($this->evals_links) - $items_start;
  248. }
  249. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  250. $item = $this->evals_links[$count + $items_start];
  251. $headers[] = $item->get_name();
  252. }
  253. return $headers;
  254. }
  255. /**
  256. * Get actual array data.
  257. *
  258. * @param int $users_sorting
  259. * @param int $users_start
  260. * @param null $users_count
  261. * @param int $items_start
  262. * @param null $items_count
  263. * @param bool $ignore_score_color
  264. * @param bool $show_all
  265. *
  266. * @return array 2-dimensional array - each array contains the elements:
  267. * 0: user id
  268. * 1: user lastname
  269. * 2: user firstname
  270. * 3+: evaluation/link scores
  271. */
  272. public function get_data(
  273. $users_sorting = 0,
  274. $users_start = 0,
  275. $users_count = null,
  276. $items_start = 0,
  277. $items_count = null,
  278. $ignore_score_color = false,
  279. $show_all = false
  280. ) {
  281. // Do some checks on users/items counts, redefine if invalid values
  282. if (!isset($users_count)) {
  283. $users_count = count($this->users) - $users_start;
  284. }
  285. if ($users_count < 0) {
  286. $users_count = 0;
  287. }
  288. if (!isset($items_count)) {
  289. $items_count = count($this->evals) + count($this->links) - $items_start;
  290. }
  291. if ($items_count < 0) {
  292. $items_count = 0;
  293. }
  294. $userTable = [];
  295. foreach ($this->users as $user) {
  296. $userTable[] = $user;
  297. }
  298. // sort users array
  299. if ($users_sorting & self::FVDG_SORT_LASTNAME) {
  300. usort($userTable, ['FlatViewDataGenerator', 'sort_by_last_name']);
  301. } elseif ($users_sorting & self::FVDG_SORT_FIRSTNAME) {
  302. usort($userTable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  303. }
  304. if ($users_sorting & self::FVDG_SORT_DESC) {
  305. $userTable = array_reverse($userTable);
  306. }
  307. // Select the requested users
  308. $selected_users = array_slice($userTable, $users_start, $users_count);
  309. // Generate actual data array
  310. $scoreDisplay = ScoreDisplay::instance();
  311. $data = [];
  312. //@todo move these in a function
  313. $sum_categories_weight_array = [];
  314. $mainCategoryId = null;
  315. $mainCourseCategory = $this->getMainCourseCategory();
  316. if (!empty($mainCourseCategory)) {
  317. $mainCategoryId = $mainCourseCategory->get_id();
  318. }
  319. if (isset($this->category) && !empty($this->category)) {
  320. $categories = Category::load(
  321. null,
  322. null,
  323. null,
  324. $this->category->get_id()
  325. );
  326. if (!empty($categories)) {
  327. foreach ($categories as $category) {
  328. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  329. }
  330. } else {
  331. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  332. }
  333. }
  334. $parent_id = $this->category->get_parent_id();
  335. if ($parent_id == 0 ||
  336. (isset($this->params['only_subcat']) && $this->params['only_subcat'] == $this->category->get_id())
  337. ) {
  338. $main_weight = $this->category->get_weight();
  339. } else {
  340. $main_cat = Category::load($parent_id, null, null);
  341. $main_weight = $main_cat[0]->get_weight();
  342. }
  343. $export_to_pdf = false;
  344. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  345. $export_to_pdf = true;
  346. }
  347. $course_code = api_get_course_id();
  348. $session_id = api_get_session_id();
  349. $model = ExerciseLib::getCourseScoreModel();
  350. foreach ($selected_users as $user) {
  351. $row = [];
  352. // User id
  353. if ($export_to_pdf) {
  354. $row['user_id'] = $user_id = $user[0];
  355. } else {
  356. $row[] = $user_id = $user[0];
  357. }
  358. // Official code
  359. if (isset($this->params['show_official_code']) &&
  360. $this->params['show_official_code']
  361. ) {
  362. if ($export_to_pdf) {
  363. $row['official_code'] = $user[4];
  364. } else {
  365. $row[] = $user[4];
  366. }
  367. }
  368. // Last name
  369. if (isset($this->params['join_firstname_lastname']) &&
  370. $this->params['join_firstname_lastname']
  371. ) {
  372. if ($export_to_pdf) {
  373. $row['name'] = api_get_person_name($user[3], $user[2]);
  374. } else {
  375. $row[] = api_get_person_name($user[3], $user[2]);
  376. }
  377. } else {
  378. if ($export_to_pdf) {
  379. if (api_is_western_name_order()) {
  380. $row['firstname'] = $user[3];
  381. $row['lastname'] = $user[2];
  382. } else {
  383. $row['lastname'] = $user[2];
  384. $row['firstname'] = $user[3];
  385. }
  386. } else {
  387. if (api_is_western_name_order()) {
  388. $row[] = $user[3]; //first name
  389. $row[] = $user[2]; //last name
  390. } else {
  391. $row[] = $user[2]; //last name
  392. $row[] = $user[3]; //first name
  393. }
  394. }
  395. }
  396. $row[] = $user[1];
  397. $item_value_total = 0;
  398. $item_total = 0;
  399. $allcat = $this->category->get_subcategories(
  400. null,
  401. $course_code,
  402. $session_id,
  403. 'ORDER BY id'
  404. );
  405. $evaluationsAdded = [];
  406. if ($parent_id == 0 && !empty($allcat)) {
  407. /** @var Category $sub_cat */
  408. foreach ($allcat as $sub_cat) {
  409. $score = $sub_cat->calc_score($user_id);
  410. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  411. $links = $sub_cat->get_links();
  412. $evaluations = $sub_cat->get_evaluations();
  413. /** @var ExerciseLink $link */
  414. $linkScoreList = [];
  415. foreach ($links as $link) {
  416. $linkScore = $link->calc_score($user_id);
  417. $linkScoreList[] = $scoreDisplay->display_score(
  418. $linkScore,
  419. SCORE_SIMPLE
  420. );
  421. }
  422. $evalScoreList = [];
  423. foreach ($evaluations as $evaluation) {
  424. $evalScore = $evaluation->calc_score($user_id);
  425. $evalScoreList[] = $scoreDisplay->display_score(
  426. $evalScore,
  427. SCORE_SIMPLE
  428. );
  429. }
  430. }
  431. $real_score = $score;
  432. $divide = $score[1] == 0 ? 1 : $score[1];
  433. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  434. $item_value = $score[0] / $divide * $main_weight;
  435. // Fixing total when using one or multiple gradebooks
  436. $percentage = $sub_cat->get_weight() / ($sub_cat_percentage) * $sub_cat_percentage / $this->category->get_weight();
  437. $item_value = $percentage * $item_value;
  438. $item_total += $sub_cat->get_weight();
  439. $style = api_get_configuration_value('gradebook_report_score_style');
  440. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  441. if (!empty($style)) {
  442. $defaultStyle = (int) $style;
  443. }
  444. if (api_get_setting('gradebook_show_percentage_in_reports') === 'false') {
  445. $defaultShowPercentageValue = SCORE_SIMPLE;
  446. if (!empty($style)) {
  447. $defaultShowPercentageValue = $style;
  448. }
  449. $real_score = $scoreDisplay->display_score(
  450. $real_score,
  451. $defaultShowPercentageValue,
  452. true
  453. );
  454. $temp_score = $scoreDisplay->display_score(
  455. $score,
  456. SCORE_DIV_SIMPLE_WITH_CUSTOM,
  457. null
  458. );
  459. $temp_score = Display::tip($real_score, $temp_score);
  460. } else {
  461. $real_score = $scoreDisplay->display_score(
  462. $real_score,
  463. SCORE_DIV_PERCENT,
  464. SCORE_ONLY_SCORE
  465. );
  466. $temp_score = $scoreDisplay->display_score(
  467. $score,
  468. $defaultStyle,
  469. null
  470. );
  471. $temp_score = Display::tip($temp_score, $real_score);
  472. }
  473. if (!isset($this->params['only_total_category']) ||
  474. (isset($this->params['only_total_category']) &&
  475. $this->params['only_total_category'] == false)
  476. ) {
  477. if (!$show_all) {
  478. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  479. $finalList = array_merge($linkScoreList, $evalScoreList);
  480. if (empty($finalList)) {
  481. $average = 0;
  482. } else {
  483. $average = array_sum($finalList) / count($finalList);
  484. }
  485. $finalList[] = round($average, 2);
  486. foreach ($finalList as $finalValue) {
  487. $row[] = '<center>'.$finalValue.'</center>';
  488. }
  489. } else {
  490. $row[] = $temp_score.' ';
  491. }
  492. } else {
  493. $row[] = $temp_score;
  494. }
  495. }
  496. $item_value_total += $item_value;
  497. }
  498. } else {
  499. // All evaluations
  500. $result = $this->parseEvaluations(
  501. $user_id,
  502. $sum_categories_weight_array,
  503. $items_count,
  504. $items_start,
  505. $show_all,
  506. $row
  507. );
  508. $item_total += $result['item_total'];
  509. $item_value_total += $result['item_value_total'];
  510. $evaluationsAdded = $result['evaluations_added'];
  511. $item_total = $main_weight;
  512. }
  513. // All evaluations
  514. $result = $this->parseEvaluations(
  515. $user_id,
  516. $sum_categories_weight_array,
  517. $items_count,
  518. $items_start,
  519. $show_all,
  520. $row,
  521. $mainCategoryId,
  522. $evaluationsAdded
  523. );
  524. $item_total += $result['item_total'];
  525. $item_value_total += $result['item_value_total'];
  526. $total_score = [$item_value_total, $item_total];
  527. $style = api_get_configuration_value('gradebook_report_score_style');
  528. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS;
  529. if (!empty($style)) {
  530. $defaultStyle = (int) $style;
  531. }
  532. if (!$show_all) {
  533. $displayScore = $scoreDisplay->display_score($total_score);
  534. if (!empty($model)) {
  535. $displayScore = ExerciseLib::show_score($total_score[0], $total_score[1]);
  536. }
  537. if ($export_to_pdf) {
  538. $row['total'] = $displayScore;
  539. } else {
  540. $row[] = $displayScore;
  541. }
  542. } else {
  543. $displayScore = $scoreDisplay->display_score($total_score, $defaultStyle);
  544. if (!empty($model)) {
  545. $displayScore = ExerciseLib::show_score($total_score[0], $total_score[1]);
  546. }
  547. if ($export_to_pdf) {
  548. $row['total'] = $displayScore;
  549. } else {
  550. $row[] = $displayScore;
  551. }
  552. }
  553. unset($score);
  554. $data[] = $row;
  555. }
  556. return $data;
  557. }
  558. /**
  559. * Parse evaluations.
  560. *
  561. * @param int $user_id
  562. * @param array $sum_categories_weight_array
  563. * @param int $items_count
  564. * @param int $items_start
  565. * @param int $show_all
  566. * @param int $parentCategoryIdFilter filter by category id if set
  567. *
  568. * @return array
  569. */
  570. public function parseEvaluations(
  571. $user_id,
  572. $sum_categories_weight_array,
  573. $items_count,
  574. $items_start,
  575. $show_all,
  576. &$row,
  577. $parentCategoryIdFilter = null,
  578. $evaluationsAlreadyAdded = []
  579. ) {
  580. // Generate actual data array
  581. $scoreDisplay = ScoreDisplay::instance();
  582. $item_total = 0;
  583. $item_value_total = 0;
  584. $evaluationsAdded = [];
  585. $model = ExerciseLib::getCourseScoreModel();
  586. for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) {
  587. /** @var AbstractLink $item */
  588. $item = $this->evals_links[$count + $items_start];
  589. if (!empty($evaluationsAlreadyAdded)) {
  590. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  591. continue;
  592. }
  593. }
  594. if (!empty($parentCategoryIdFilter)) {
  595. if ($item->get_category_id() != $parentCategoryIdFilter) {
  596. continue;
  597. }
  598. }
  599. $evaluationsAdded[] = $item->get_id();
  600. $score = $item->calc_score($user_id);
  601. $real_score = $score;
  602. $divide = isset($score[1]) && !empty($score[1]) && $score[1] > 0 ? $score[1] : 1;
  603. // Sub cat weight
  604. $item_value = isset($score[0]) ? $score[0] / $divide : 0;
  605. // Fixing total when using one or multiple gradebooks.
  606. if (empty($parentCategoryIdFilter)) {
  607. if ($this->category->get_parent_id() == 0) {
  608. if (isset($score[0])) {
  609. $item_value = $score[0] / $divide * $item->get_weight();
  610. } else {
  611. $item_value = 0;
  612. }
  613. } else {
  614. $item_value = $item_value * $item->get_weight();
  615. }
  616. } else {
  617. $item_value = $score[0] / $divide * $item->get_weight();
  618. }
  619. $item_total += $item->get_weight();
  620. $style = api_get_configuration_value('gradebook_report_score_style');
  621. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  622. if (!empty($style)) {
  623. $defaultStyle = (int) $style;
  624. }
  625. $complete_score = $scoreDisplay->display_score(
  626. $score,
  627. SCORE_DIV_PERCENT,
  628. SCORE_ONLY_SCORE
  629. );
  630. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  631. $defaultShowPercentageValue = SCORE_SIMPLE;
  632. if (!empty($style)) {
  633. $defaultShowPercentageValue = $style;
  634. }
  635. $real_score = $scoreDisplay->display_score(
  636. $real_score,
  637. $defaultShowPercentageValue
  638. );
  639. $temp_score = $scoreDisplay->display_score(
  640. [$item_value, null],
  641. SCORE_DIV_SIMPLE_WITH_CUSTOM
  642. );
  643. $temp_score = Display::tip($real_score, $temp_score);
  644. } else {
  645. $temp_score = $scoreDisplay->display_score(
  646. $real_score,
  647. $defaultStyle
  648. );
  649. $temp_score = Display::tip($temp_score, $complete_score);
  650. }
  651. if (!empty($model)) {
  652. $scoreToShow = '';
  653. if (isset($score[0]) && isset($score[1])) {
  654. $scoreToShow = ExerciseLib::show_score($score[0], $score[1]);
  655. }
  656. $temp_score = $scoreToShow;
  657. }
  658. if (!isset($this->params['only_total_category']) ||
  659. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  660. ) {
  661. if (!$show_all) {
  662. if (in_array(
  663. $item->get_type(),
  664. [
  665. LINK_EXERCISE,
  666. LINK_DROPBOX,
  667. LINK_STUDENTPUBLICATION,
  668. LINK_LEARNPATH,
  669. LINK_FORUM_THREAD,
  670. LINK_ATTENDANCE,
  671. LINK_SURVEY,
  672. LINK_HOTPOTATOES,
  673. ]
  674. )
  675. ) {
  676. if (!empty($score[0])) {
  677. $row[] = $temp_score.' ';
  678. } else {
  679. $row[] = '';
  680. }
  681. } else {
  682. $row[] = $temp_score.' ';
  683. }
  684. } else {
  685. $row[] = $temp_score;
  686. }
  687. }
  688. $item_value_total += $item_value;
  689. }
  690. return [
  691. 'item_total' => $item_total,
  692. 'item_value_total' => $item_value_total,
  693. 'evaluations_added' => $evaluationsAdded,
  694. ];
  695. }
  696. /**
  697. * Get actual array data evaluation/link scores.
  698. *
  699. * @param int $session_id
  700. *
  701. * @return array
  702. */
  703. public function getEvaluationSummaryResults($session_id = null)
  704. {
  705. $usertable = [];
  706. foreach ($this->users as $user) {
  707. $usertable[] = $user;
  708. }
  709. $selected_users = $usertable;
  710. // generate actual data array for all selected users
  711. $data = [];
  712. foreach ($selected_users as $user) {
  713. $row = [];
  714. for ($count = 0; $count < count($this->evals_links); $count++) {
  715. $item = $this->evals_links[$count];
  716. $score = $item->calc_score($user[0]);
  717. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0] * 100) / $score[1] : 0;
  718. $row[$item->get_name()] = $porcent_score;
  719. }
  720. $data[$user[0]] = $row;
  721. }
  722. // get evaluations for every user by item
  723. $data_by_item = [];
  724. foreach ($data as $uid => $items) {
  725. $tmp = [];
  726. foreach ($items as $item => $value) {
  727. $tmp[] = $item;
  728. if (in_array($item, $tmp)) {
  729. $data_by_item[$item][$uid] = $value;
  730. }
  731. }
  732. }
  733. /* Get evaluation summary results
  734. (maximum, minimum and average of evaluations for all students)
  735. */
  736. $result = [];
  737. foreach ($data_by_item as $k => $v) {
  738. $average = round(array_sum($v) / count($v));
  739. arsort($v);
  740. $maximum = array_shift($v);
  741. $minimum = array_pop($v);
  742. if (is_null($minimum)) {
  743. $minimum = 0;
  744. }
  745. $summary = [
  746. 'max' => $maximum,
  747. 'min' => $minimum,
  748. 'avg' => $average,
  749. ];
  750. $result[$k] = $summary;
  751. }
  752. return $result;
  753. }
  754. /**
  755. * @return array
  756. */
  757. public function get_data_to_graph()
  758. {
  759. // do some checks on users/items counts, redefine if invalid values
  760. $usertable = [];
  761. foreach ($this->users as $user) {
  762. $usertable[] = $user;
  763. }
  764. // sort users array
  765. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  766. $data = [];
  767. $selected_users = $usertable;
  768. foreach ($selected_users as $user) {
  769. $row = [];
  770. $row[] = $user[0]; // user id
  771. $item_value = 0;
  772. $item_total = 0;
  773. for ($count = 0; $count < count($this->evals_links); $count++) {
  774. $item = $this->evals_links[$count];
  775. $score = $item->calc_score($user[0]);
  776. $divide = (($score[1]) == 0) ? 1 : $score[1];
  777. $item_value += $score[0] / $divide * $item->get_weight();
  778. $item_total += $item->get_weight();
  779. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  780. $score_final = ($score[0] / $score_denom) * 100;
  781. $row[] = $score_final;
  782. }
  783. $score_final = ($item_value / $item_total) * 100;
  784. $row[] = $score_final;
  785. $data[] = $row;
  786. }
  787. return $data;
  788. }
  789. /**
  790. * This is a function to show the generated data.
  791. *
  792. * @param bool $displayWarning
  793. *
  794. * @return array
  795. */
  796. public function get_data_to_graph2($displayWarning = true)
  797. {
  798. $course_code = api_get_course_id();
  799. $session_id = api_get_session_id();
  800. // do some checks on users/items counts, redefine if invalid values
  801. $usertable = [];
  802. foreach ($this->users as $user) {
  803. $usertable[] = $user;
  804. }
  805. // sort users array
  806. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  807. // generate actual data array
  808. $scoreDisplay = ScoreDisplay::instance();
  809. $data = [];
  810. $selected_users = $usertable;
  811. foreach ($selected_users as $user) {
  812. $row = [];
  813. $row[] = $user[0]; // user id
  814. $item_value = 0;
  815. $item_total = 0;
  816. $final_score = 0;
  817. $item_value_total = 0;
  818. $allcat = $this->category->get_subcategories(
  819. null,
  820. $course_code,
  821. $session_id,
  822. 'ORDER BY id'
  823. );
  824. $parent_id = $this->category->get_parent_id();
  825. if ($parent_id == 0 && !empty($allcat)) {
  826. foreach ($allcat as $sub_cat) {
  827. $score = $sub_cat->calc_score($user[0]);
  828. $real_score = $score;
  829. $main_weight = $this->category->get_weight();
  830. $divide = $score[1] == 0 ? 1 : $score[1];
  831. //$sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  832. $item_value = $score[0] / $divide * $main_weight;
  833. $item_total += $sub_cat->get_weight();
  834. $row[] = [
  835. $item_value,
  836. trim(
  837. $scoreDisplay->display_score(
  838. $real_score,
  839. SCORE_CUSTOM,
  840. null,
  841. true
  842. )
  843. ),
  844. ];
  845. $item_value_total += $item_value;
  846. $final_score += $score[0];
  847. //$final_score = ($final_score / $item_total) * 100;
  848. }
  849. $total_score = [$final_score, $item_total];
  850. $row[] = [
  851. $final_score,
  852. trim(
  853. $scoreDisplay->display_score(
  854. $total_score,
  855. SCORE_CUSTOM,
  856. null,
  857. true
  858. )
  859. ),
  860. ];
  861. } else {
  862. for ($count = 0; $count < count($this->evals_links); $count++) {
  863. $item = $this->evals_links[$count];
  864. $score = $item->calc_score($user[0]);
  865. $divide = $score[1] == 0 ? 1 : $score[1];
  866. $item_value += $score[0] / $divide * $item->get_weight();
  867. $item_total += $item->get_weight();
  868. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  869. $score_final = ($score[0] / $score_denom) * 100;
  870. $row[] = [
  871. $score_final,
  872. trim(
  873. $scoreDisplay->display_score(
  874. $score,
  875. SCORE_CUSTOM,
  876. null,
  877. true
  878. )
  879. ),
  880. ];
  881. }
  882. $total_score = [$item_value, $item_total];
  883. $score_final = ($item_value / $item_total) * 100;
  884. if ($displayWarning) {
  885. echo Display::return_message($total_score[1], 'warning');
  886. }
  887. $row[] = [
  888. $score_final,
  889. trim(
  890. $scoreDisplay->display_score(
  891. $total_score,
  892. SCORE_CUSTOM,
  893. null,
  894. true
  895. )
  896. ),
  897. ];
  898. }
  899. $data[] = $row;
  900. }
  901. return $data;
  902. }
  903. /**
  904. * @param $item1
  905. * @param $item2
  906. *
  907. * @return int
  908. */
  909. public function sort_by_last_name($item1, $item2)
  910. {
  911. return api_strcmp($item1[2], $item2[2]);
  912. }
  913. /**
  914. * @param $item1
  915. * @param $item2
  916. *
  917. * @return int
  918. */
  919. public function sort_by_first_name($item1, $item2)
  920. {
  921. return api_strcmp($item1[3], $item2[3]);
  922. }
  923. }