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. /** @var ExerciseLink $link */
  413. $linkScoreList = [];
  414. foreach ($links as $link) {
  415. $linkScore = $link->calc_score($user_id);
  416. $linkScoreList[] = $scoreDisplay->display_score(
  417. $linkScore,
  418. SCORE_SIMPLE
  419. );
  420. }
  421. $evaluations = $sub_cat->get_evaluations();
  422. $evalScoreList = [];
  423. /** @var Evaluation $evaluation */
  424. foreach ($evaluations as $evaluation) {
  425. $evalScore = $evaluation->calc_score($user_id);
  426. $evalScoreList[] = $scoreDisplay->display_score(
  427. $evalScore,
  428. SCORE_SIMPLE
  429. );
  430. }
  431. }
  432. $real_score = $score;
  433. $divide = $score[1] == 0 ? 1 : $score[1];
  434. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  435. $item_value = $score[0] / $divide * $main_weight;
  436. // Fixing total when using one or multiple gradebooks
  437. $percentage = $sub_cat->get_weight() / ($sub_cat_percentage) * $sub_cat_percentage / $this->category->get_weight();
  438. $item_value = $percentage * $item_value;
  439. $item_total += $sub_cat->get_weight();
  440. $style = api_get_configuration_value('gradebook_report_score_style');
  441. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  442. if (!empty($style)) {
  443. $defaultStyle = (int) $style;
  444. }
  445. if (api_get_setting('gradebook_show_percentage_in_reports') === 'false') {
  446. $defaultShowPercentageValue = SCORE_SIMPLE;
  447. if (!empty($style)) {
  448. $defaultShowPercentageValue = $style;
  449. }
  450. $real_score = $scoreDisplay->display_score(
  451. $real_score,
  452. $defaultShowPercentageValue,
  453. true
  454. );
  455. $temp_score = $scoreDisplay->display_score(
  456. $score,
  457. SCORE_DIV_SIMPLE_WITH_CUSTOM,
  458. null
  459. );
  460. $temp_score = Display::tip($real_score, $temp_score);
  461. } else {
  462. $real_score = $scoreDisplay->display_score(
  463. $real_score,
  464. SCORE_DIV_PERCENT,
  465. SCORE_ONLY_SCORE
  466. );
  467. $temp_score = $scoreDisplay->display_score(
  468. $score,
  469. $defaultStyle,
  470. null
  471. );
  472. $temp_score = Display::tip($temp_score, $real_score);
  473. }
  474. if (!isset($this->params['only_total_category']) ||
  475. (isset($this->params['only_total_category']) &&
  476. $this->params['only_total_category'] == false)
  477. ) {
  478. if (!$show_all) {
  479. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  480. $finalList = array_merge($linkScoreList, $evalScoreList);
  481. if (empty($finalList)) {
  482. $average = 0;
  483. } else {
  484. $average = array_sum($finalList) / count($finalList);
  485. }
  486. $finalList[] = round($average, 2);
  487. foreach ($finalList as $finalValue) {
  488. $row[] = '<center>'.$finalValue.'</center>';
  489. }
  490. } else {
  491. $row[] = $temp_score.' ';
  492. }
  493. } else {
  494. $row[] = $temp_score;
  495. }
  496. }
  497. $item_value_total += $item_value;
  498. }
  499. } else {
  500. // All evaluations
  501. $result = $this->parseEvaluations(
  502. $user_id,
  503. $sum_categories_weight_array,
  504. $items_count,
  505. $items_start,
  506. $show_all,
  507. $row
  508. );
  509. $item_total += $result['item_total'];
  510. $item_value_total += $result['item_value_total'];
  511. $evaluationsAdded = $result['evaluations_added'];
  512. $item_total = $main_weight;
  513. }
  514. // All evaluations
  515. $result = $this->parseEvaluations(
  516. $user_id,
  517. $sum_categories_weight_array,
  518. $items_count,
  519. $items_start,
  520. $show_all,
  521. $row,
  522. $mainCategoryId,
  523. $evaluationsAdded
  524. );
  525. $item_total += $result['item_total'];
  526. $item_value_total += $result['item_value_total'];
  527. $total_score = [$item_value_total, $item_total];
  528. $style = api_get_configuration_value('gradebook_report_score_style');
  529. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS;
  530. if (!empty($style)) {
  531. $defaultStyle = (int) $style;
  532. }
  533. if (!$show_all) {
  534. $displayScore = $scoreDisplay->display_score($total_score);
  535. if (!empty($model)) {
  536. $displayScore = ExerciseLib::show_score($total_score[0], $total_score[1]);
  537. }
  538. if ($export_to_pdf) {
  539. $row['total'] = $displayScore;
  540. } else {
  541. $row[] = $displayScore;
  542. }
  543. } else {
  544. $displayScore = $scoreDisplay->display_score($total_score, $defaultStyle);
  545. if (!empty($model)) {
  546. $displayScore = ExerciseLib::show_score($total_score[0], $total_score[1]);
  547. }
  548. if ($export_to_pdf) {
  549. $row['total'] = $displayScore;
  550. } else {
  551. $row[] = $displayScore;
  552. }
  553. }
  554. unset($score);
  555. $data[] = $row;
  556. }
  557. return $data;
  558. }
  559. /**
  560. * Parse evaluations.
  561. *
  562. * @param int $user_id
  563. * @param array $sum_categories_weight_array
  564. * @param int $items_count
  565. * @param int $items_start
  566. * @param int $show_all
  567. * @param int $parentCategoryIdFilter filter by category id if set
  568. *
  569. * @return array
  570. */
  571. public function parseEvaluations(
  572. $user_id,
  573. $sum_categories_weight_array,
  574. $items_count,
  575. $items_start,
  576. $show_all,
  577. &$row,
  578. $parentCategoryIdFilter = null,
  579. $evaluationsAlreadyAdded = []
  580. ) {
  581. // Generate actual data array
  582. $scoreDisplay = ScoreDisplay::instance();
  583. $item_total = 0;
  584. $item_value_total = 0;
  585. $evaluationsAdded = [];
  586. $model = ExerciseLib::getCourseScoreModel();
  587. for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) {
  588. /** @var AbstractLink $item */
  589. $item = $this->evals_links[$count + $items_start];
  590. if (!empty($evaluationsAlreadyAdded)) {
  591. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  592. continue;
  593. }
  594. }
  595. if (!empty($parentCategoryIdFilter)) {
  596. if ($item->get_category_id() != $parentCategoryIdFilter) {
  597. continue;
  598. }
  599. }
  600. $evaluationsAdded[] = $item->get_id();
  601. $score = $item->calc_score($user_id);
  602. $real_score = $score;
  603. $divide = isset($score[1]) && !empty($score[1]) && $score[1] > 0 ? $score[1] : 1;
  604. // Sub cat weight
  605. $item_value = isset($score[0]) ? $score[0] / $divide : 0;
  606. // Fixing total when using one or multiple gradebooks.
  607. if (empty($parentCategoryIdFilter)) {
  608. if ($this->category->get_parent_id() == 0) {
  609. if (isset($score[0])) {
  610. $item_value = $score[0] / $divide * $item->get_weight();
  611. } else {
  612. $item_value = 0;
  613. }
  614. } else {
  615. $item_value = $item_value * $item->get_weight();
  616. }
  617. } else {
  618. $item_value = $score[0] / $divide * $item->get_weight();
  619. }
  620. $item_total += $item->get_weight();
  621. $style = api_get_configuration_value('gradebook_report_score_style');
  622. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  623. if (!empty($style)) {
  624. $defaultStyle = (int) $style;
  625. }
  626. $complete_score = $scoreDisplay->display_score(
  627. $score,
  628. SCORE_DIV_PERCENT,
  629. SCORE_ONLY_SCORE
  630. );
  631. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  632. $defaultShowPercentageValue = SCORE_SIMPLE;
  633. if (!empty($style)) {
  634. $defaultShowPercentageValue = $style;
  635. }
  636. $real_score = $scoreDisplay->display_score(
  637. $real_score,
  638. $defaultShowPercentageValue
  639. );
  640. $temp_score = $scoreDisplay->display_score(
  641. [$item_value, null],
  642. SCORE_DIV_SIMPLE_WITH_CUSTOM
  643. );
  644. $temp_score = Display::tip($real_score, $temp_score);
  645. } else {
  646. $temp_score = $scoreDisplay->display_score(
  647. $real_score,
  648. $defaultStyle
  649. );
  650. $temp_score = Display::tip($temp_score, $complete_score);
  651. }
  652. if (!empty($model)) {
  653. $scoreToShow = '';
  654. if (isset($score[0]) && isset($score[1])) {
  655. $scoreToShow = ExerciseLib::show_score($score[0], $score[1]);
  656. }
  657. $temp_score = $scoreToShow;
  658. }
  659. if (!isset($this->params['only_total_category']) ||
  660. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  661. ) {
  662. if (!$show_all) {
  663. if (in_array(
  664. $item->get_type(),
  665. [
  666. LINK_EXERCISE,
  667. LINK_DROPBOX,
  668. LINK_STUDENTPUBLICATION,
  669. LINK_LEARNPATH,
  670. LINK_FORUM_THREAD,
  671. LINK_ATTENDANCE,
  672. LINK_SURVEY,
  673. LINK_HOTPOTATOES,
  674. ]
  675. )
  676. ) {
  677. if (!empty($score[0])) {
  678. $row[] = $temp_score.' ';
  679. } else {
  680. $row[] = '';
  681. }
  682. } else {
  683. $row[] = $temp_score.' ';
  684. }
  685. } else {
  686. $row[] = $temp_score;
  687. }
  688. }
  689. $item_value_total += $item_value;
  690. }
  691. return [
  692. 'item_total' => $item_total,
  693. 'item_value_total' => $item_value_total,
  694. 'evaluations_added' => $evaluationsAdded,
  695. ];
  696. }
  697. /**
  698. * Get actual array data evaluation/link scores.
  699. *
  700. * @param int $session_id
  701. *
  702. * @return array
  703. */
  704. public function getEvaluationSummaryResults($session_id = null)
  705. {
  706. $usertable = [];
  707. foreach ($this->users as $user) {
  708. $usertable[] = $user;
  709. }
  710. $selected_users = $usertable;
  711. // generate actual data array for all selected users
  712. $data = [];
  713. foreach ($selected_users as $user) {
  714. $row = [];
  715. for ($count = 0; $count < count($this->evals_links); $count++) {
  716. $item = $this->evals_links[$count];
  717. $score = $item->calc_score($user[0]);
  718. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0] * 100) / $score[1] : 0;
  719. $row[$item->get_name()] = $porcent_score;
  720. }
  721. $data[$user[0]] = $row;
  722. }
  723. // get evaluations for every user by item
  724. $data_by_item = [];
  725. foreach ($data as $uid => $items) {
  726. $tmp = [];
  727. foreach ($items as $item => $value) {
  728. $tmp[] = $item;
  729. if (in_array($item, $tmp)) {
  730. $data_by_item[$item][$uid] = $value;
  731. }
  732. }
  733. }
  734. /* Get evaluation summary results
  735. (maximum, minimum and average of evaluations for all students)
  736. */
  737. $result = [];
  738. foreach ($data_by_item as $k => $v) {
  739. $average = round(array_sum($v) / count($v));
  740. arsort($v);
  741. $maximum = array_shift($v);
  742. $minimum = array_pop($v);
  743. if (is_null($minimum)) {
  744. $minimum = 0;
  745. }
  746. $summary = [
  747. 'max' => $maximum,
  748. 'min' => $minimum,
  749. 'avg' => $average,
  750. ];
  751. $result[$k] = $summary;
  752. }
  753. return $result;
  754. }
  755. /**
  756. * @return array
  757. */
  758. public function get_data_to_graph()
  759. {
  760. // do some checks on users/items counts, redefine if invalid values
  761. $usertable = [];
  762. foreach ($this->users as $user) {
  763. $usertable[] = $user;
  764. }
  765. // sort users array
  766. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  767. $data = [];
  768. $selected_users = $usertable;
  769. foreach ($selected_users as $user) {
  770. $row = [];
  771. $row[] = $user[0]; // user id
  772. $item_value = 0;
  773. $item_total = 0;
  774. for ($count = 0; $count < count($this->evals_links); $count++) {
  775. $item = $this->evals_links[$count];
  776. $score = $item->calc_score($user[0]);
  777. $divide = (($score[1]) == 0) ? 1 : $score[1];
  778. $item_value += $score[0] / $divide * $item->get_weight();
  779. $item_total += $item->get_weight();
  780. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  781. $score_final = ($score[0] / $score_denom) * 100;
  782. $row[] = $score_final;
  783. }
  784. $score_final = ($item_value / $item_total) * 100;
  785. $row[] = $score_final;
  786. $data[] = $row;
  787. }
  788. return $data;
  789. }
  790. /**
  791. * This is a function to show the generated data.
  792. *
  793. * @param bool $displayWarning
  794. *
  795. * @return array
  796. */
  797. public function get_data_to_graph2($displayWarning = true)
  798. {
  799. $course_code = api_get_course_id();
  800. $session_id = api_get_session_id();
  801. // do some checks on users/items counts, redefine if invalid values
  802. $usertable = [];
  803. foreach ($this->users as $user) {
  804. $usertable[] = $user;
  805. }
  806. // sort users array
  807. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  808. // generate actual data array
  809. $scoreDisplay = ScoreDisplay::instance();
  810. $data = [];
  811. $selected_users = $usertable;
  812. foreach ($selected_users as $user) {
  813. $row = [];
  814. $row[] = $user[0]; // user id
  815. $item_value = 0;
  816. $item_total = 0;
  817. $final_score = 0;
  818. $item_value_total = 0;
  819. $allcat = $this->category->get_subcategories(
  820. null,
  821. $course_code,
  822. $session_id,
  823. 'ORDER BY id'
  824. );
  825. $parent_id = $this->category->get_parent_id();
  826. if ($parent_id == 0 && !empty($allcat)) {
  827. foreach ($allcat as $sub_cat) {
  828. $score = $sub_cat->calc_score($user[0]);
  829. $real_score = $score;
  830. $main_weight = $this->category->get_weight();
  831. $divide = $score[1] == 0 ? 1 : $score[1];
  832. //$sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  833. $item_value = $score[0] / $divide * $main_weight;
  834. $item_total += $sub_cat->get_weight();
  835. $row[] = [
  836. $item_value,
  837. trim(
  838. $scoreDisplay->display_score(
  839. $real_score,
  840. SCORE_CUSTOM,
  841. null,
  842. true
  843. )
  844. ),
  845. ];
  846. $item_value_total += $item_value;
  847. $final_score += $score[0];
  848. //$final_score = ($final_score / $item_total) * 100;
  849. }
  850. $total_score = [$final_score, $item_total];
  851. $row[] = [
  852. $final_score,
  853. trim(
  854. $scoreDisplay->display_score(
  855. $total_score,
  856. SCORE_CUSTOM,
  857. null,
  858. true
  859. )
  860. ),
  861. ];
  862. } else {
  863. for ($count = 0; $count < count($this->evals_links); $count++) {
  864. $item = $this->evals_links[$count];
  865. $score = $item->calc_score($user[0]);
  866. $divide = $score[1] == 0 ? 1 : $score[1];
  867. $item_value += $score[0] / $divide * $item->get_weight();
  868. $item_total += $item->get_weight();
  869. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  870. $score_final = ($score[0] / $score_denom) * 100;
  871. $row[] = [
  872. $score_final,
  873. trim(
  874. $scoreDisplay->display_score(
  875. $score,
  876. SCORE_CUSTOM,
  877. null,
  878. true
  879. )
  880. ),
  881. ];
  882. }
  883. $total_score = [$item_value, $item_total];
  884. $score_final = ($item_value / $item_total) * 100;
  885. if ($displayWarning) {
  886. echo Display::return_message($total_score[1], 'warning');
  887. }
  888. $row[] = [
  889. $score_final,
  890. trim(
  891. $scoreDisplay->display_score(
  892. $total_score,
  893. SCORE_CUSTOM,
  894. null,
  895. true
  896. )
  897. ),
  898. ];
  899. }
  900. $data[] = $row;
  901. }
  902. return $data;
  903. }
  904. /**
  905. * @param $item1
  906. * @param $item2
  907. *
  908. * @return int
  909. */
  910. public function sort_by_last_name($item1, $item2)
  911. {
  912. return api_strcmp($item1[2], $item2[2]);
  913. }
  914. /**
  915. * @param $item1
  916. * @param $item2
  917. *
  918. * @return int
  919. */
  920. public function sort_by_first_name($item1, $item2)
  921. {
  922. return api_strcmp($item1[3], $item2[3]);
  923. }
  924. }