flatview_data_generator.class.php 35 KB

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