flatview_data_generator.class.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. //@todo move these in a function
  310. $sum_categories_weight_array = [];
  311. $mainCategoryId = null;
  312. $mainCourseCategory = $this->getMainCourseCategory();
  313. if (!empty($mainCourseCategory)) {
  314. $mainCategoryId = $mainCourseCategory->get_id();
  315. }
  316. if (isset($this->category) && !empty($this->category)) {
  317. $categories = Category::load(
  318. null,
  319. null,
  320. null,
  321. $this->category->get_id()
  322. );
  323. if (!empty($categories)) {
  324. foreach ($categories as $category) {
  325. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  326. }
  327. } else {
  328. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  329. }
  330. }
  331. $parent_id = $this->category->get_parent_id();
  332. if ($parent_id == 0 ||
  333. (isset($this->params['only_subcat']) && $this->params['only_subcat'] == $this->category->get_id())
  334. ) {
  335. $main_weight = $this->category->get_weight();
  336. $grade_model_id = $this->category->get_grade_model_id();
  337. } else {
  338. $main_cat = Category::load($parent_id, null, null);
  339. $main_weight = $main_cat[0]->get_weight();
  340. $grade_model_id = $main_cat[0]->get_grade_model_id();
  341. }
  342. $use_grade_model = true;
  343. if (empty($grade_model_id) || $grade_model_id == -1) {
  344. $use_grade_model = false;
  345. }
  346. $export_to_pdf = false;
  347. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  348. $export_to_pdf = true;
  349. }
  350. $course_code = api_get_course_id();
  351. $session_id = api_get_session_id();
  352. foreach ($selected_users as $user) {
  353. $row = [];
  354. // User id
  355. if ($export_to_pdf) {
  356. $row['user_id'] = $user_id = $user[0];
  357. } else {
  358. $row[] = $user_id = $user[0];
  359. }
  360. // Official code
  361. if (isset($this->params['show_official_code']) &&
  362. $this->params['show_official_code']
  363. ) {
  364. if ($export_to_pdf) {
  365. $row['official_code'] = $user[4];
  366. } else {
  367. $row[] = $user[4];
  368. }
  369. }
  370. // Last name
  371. if (isset($this->params['join_firstname_lastname']) &&
  372. $this->params['join_firstname_lastname']
  373. ) {
  374. if ($export_to_pdf) {
  375. $row['name'] = api_get_person_name($user[3], $user[2]);
  376. } else {
  377. $row[] = api_get_person_name($user[3], $user[2]);
  378. }
  379. } else {
  380. if ($export_to_pdf) {
  381. if (api_is_western_name_order()) {
  382. $row['firstname'] = $user[3];
  383. $row['lastname'] = $user[2];
  384. } else {
  385. $row['lastname'] = $user[2];
  386. $row['firstname'] = $user[3];
  387. }
  388. } else {
  389. if (api_is_western_name_order()) {
  390. $row[] = $user[3]; //first name
  391. $row[] = $user[2]; //last name
  392. } else {
  393. $row[] = $user[2]; //last name
  394. $row[] = $user[3]; //first name
  395. }
  396. }
  397. }
  398. $row[] = $user[1];
  399. $item_value = 0;
  400. $item_value_total = 0;
  401. $item_total = 0;
  402. $convert_using_the_global_weight = true;
  403. $allcat = $this->category->get_subcategories(
  404. null,
  405. $course_code,
  406. $session_id,
  407. 'ORDER BY id'
  408. );
  409. $evaluationsAdded = [];
  410. if ($parent_id == 0 && !empty($allcat)) {
  411. /** @var Category $sub_cat */
  412. foreach ($allcat as $sub_cat) {
  413. $score = $sub_cat->calc_score($user_id);
  414. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  415. $links = $sub_cat->get_links();
  416. $evaluations = $sub_cat->get_evaluations();
  417. /** @var ExerciseLink $link */
  418. $linkScoreList = [];
  419. foreach ($links as $link) {
  420. $linkScore = $link->calc_score($user_id);
  421. $linkScoreList[] = $scoreDisplay->display_score(
  422. $linkScore,
  423. SCORE_SIMPLE
  424. );
  425. }
  426. $evalScoreList = [];
  427. foreach ($evaluations as $evaluation) {
  428. $evalScore = $evaluation->calc_score($user_id);
  429. $evalScoreList[] = $scoreDisplay->display_score(
  430. $evalScore,
  431. SCORE_SIMPLE
  432. );
  433. }
  434. }
  435. $real_score = $score;
  436. $divide = $score[1] == 0 ? 1 : $score[1];
  437. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  438. $item_value = $score[0] / $divide * $main_weight;
  439. // Fixing total when using one or multiple gradebooks
  440. $percentage = $sub_cat->get_weight() / ($sub_cat_percentage) * $sub_cat_percentage / $this->category->get_weight();
  441. $item_value = $percentage * $item_value;
  442. $item_total += $sub_cat->get_weight();
  443. $style = api_get_configuration_value('gradebook_report_score_style');
  444. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  445. if (!empty($style)) {
  446. $defaultStyle = (int) $style;
  447. }
  448. if (api_get_setting('gradebook_show_percentage_in_reports') === 'false') {
  449. $defaultShowPercentageValue = SCORE_SIMPLE;
  450. if (!empty($style)) {
  451. $defaultShowPercentageValue = $style;
  452. }
  453. $real_score = $scoreDisplay->display_score(
  454. $real_score,
  455. $defaultShowPercentageValue,
  456. true
  457. );
  458. $temp_score = $scoreDisplay->display_score(
  459. $score,
  460. SCORE_DIV_SIMPLE_WITH_CUSTOM,
  461. null
  462. );
  463. $temp_score = Display::tip($real_score, $temp_score);
  464. } else {
  465. $real_score = $scoreDisplay->display_score(
  466. $real_score,
  467. SCORE_DIV_PERCENT,
  468. SCORE_ONLY_SCORE
  469. );
  470. $temp_score = $scoreDisplay->display_score(
  471. $score,
  472. $defaultStyle,
  473. null
  474. );
  475. $temp_score = Display::tip($temp_score, $real_score);
  476. }
  477. if (!isset($this->params['only_total_category']) ||
  478. (isset($this->params['only_total_category']) &&
  479. $this->params['only_total_category'] == false)
  480. ) {
  481. if (!$show_all) {
  482. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  483. $finalList = array_merge($linkScoreList, $evalScoreList);
  484. if (empty($finalList)) {
  485. $average = 0;
  486. } else {
  487. $average = array_sum($finalList) / count($finalList);
  488. }
  489. $finalList[] = round($average, 2);
  490. foreach ($finalList as $finalValue) {
  491. $row[] = '<center>'.$finalValue.'</center>';
  492. }
  493. } else {
  494. $row[] = $temp_score.' ';
  495. }
  496. } else {
  497. $row[] = $temp_score;
  498. }
  499. }
  500. $item_value_total += $item_value;
  501. }
  502. } else {
  503. // All evaluations
  504. $result = $this->parseEvaluations(
  505. $user_id,
  506. $sum_categories_weight_array,
  507. $items_count,
  508. $items_start,
  509. $show_all,
  510. $row
  511. );
  512. $item_total += $result['item_total'];
  513. $item_value_total += $result['item_value_total'];
  514. $evaluationsAdded = $result['evaluations_added'];
  515. $item_total = $main_weight;
  516. }
  517. // All evaluations
  518. $result = $this->parseEvaluations(
  519. $user_id,
  520. $sum_categories_weight_array,
  521. $items_count,
  522. $items_start,
  523. $show_all,
  524. $row,
  525. $mainCategoryId,
  526. $evaluationsAdded
  527. );
  528. $item_total += $result['item_total'];
  529. $item_value_total += $result['item_value_total'];
  530. $total_score = [$item_value_total, $item_total];
  531. $style = api_get_configuration_value('gradebook_report_score_style');
  532. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS;
  533. if (!empty($style)) {
  534. $defaultStyle = (int) $style;
  535. }
  536. if (!$show_all) {
  537. if ($export_to_pdf) {
  538. $row['total'] = $scoreDisplay->display_score($total_score);
  539. } else {
  540. $row[] = $scoreDisplay->display_score($total_score);
  541. }
  542. } else {
  543. if ($export_to_pdf) {
  544. $row['total'] = $scoreDisplay->display_score(
  545. $total_score,
  546. $defaultStyle
  547. );
  548. } else {
  549. $row[] = $scoreDisplay->display_score(
  550. $total_score,
  551. $defaultStyle
  552. );
  553. }
  554. }
  555. unset($score);
  556. $data[] = $row;
  557. }
  558. return $data;
  559. }
  560. /**
  561. * Parse evaluations.
  562. *
  563. * @param int $user_id
  564. * @param array $sum_categories_weight_array
  565. * @param int $items_count
  566. * @param int $items_start
  567. * @param int $show_all
  568. * @param int $parentCategoryIdFilter filter by category id if set
  569. *
  570. * @return array
  571. */
  572. public function parseEvaluations(
  573. $user_id,
  574. $sum_categories_weight_array,
  575. $items_count,
  576. $items_start,
  577. $show_all,
  578. &$row,
  579. $parentCategoryIdFilter = null,
  580. $evaluationsAlreadyAdded = []
  581. ) {
  582. // Generate actual data array
  583. $scoreDisplay = ScoreDisplay::instance();
  584. $item_total = 0;
  585. $item_value_total = 0;
  586. $evaluationsAdded = [];
  587. $model = ExerciseLib::getCourseScoreModel();
  588. for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) {
  589. /** @var AbstractLink $item */
  590. $item = $this->evals_links[$count + $items_start];
  591. if (!empty($evaluationsAlreadyAdded)) {
  592. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  593. continue;
  594. }
  595. }
  596. if (!empty($parentCategoryIdFilter)) {
  597. if ($item->get_category_id() != $parentCategoryIdFilter) {
  598. continue;
  599. }
  600. }
  601. $evaluationsAdded[] = $item->get_id();
  602. $score = $item->calc_score($user_id);
  603. $real_score = $score;
  604. $divide = isset($score[1]) && !empty($score[1]) && $score[1] > 0 ? $score[1] : 1;
  605. // Sub cat weight
  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. $style = api_get_configuration_value('gradebook_report_score_style');
  623. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  624. if (!empty($style)) {
  625. $defaultStyle = (int) $style;
  626. }
  627. $complete_score = $scoreDisplay->display_score(
  628. $score,
  629. SCORE_DIV_PERCENT,
  630. SCORE_ONLY_SCORE
  631. );
  632. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  633. $defaultShowPercentageValue = SCORE_SIMPLE;
  634. if (!empty($style)) {
  635. $defaultShowPercentageValue = $style;
  636. }
  637. $real_score = $scoreDisplay->display_score(
  638. $real_score,
  639. $defaultShowPercentageValue
  640. );
  641. $temp_score = $scoreDisplay->display_score(
  642. [$item_value, null],
  643. SCORE_DIV_SIMPLE_WITH_CUSTOM
  644. );
  645. $temp_score = Display::tip($real_score, $temp_score);
  646. } else {
  647. $temp_score = $scoreDisplay->display_score(
  648. $real_score,
  649. $defaultStyle
  650. );
  651. $temp_score = Display::tip($temp_score, $complete_score);
  652. }
  653. if (!empty($model)) {
  654. $temp_score = $temp_score.'&nbsp;'.ExerciseLib::show_score($score[0], $score[1]);
  655. }
  656. if (!isset($this->params['only_total_category']) ||
  657. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  658. ) {
  659. if (!$show_all) {
  660. if (in_array(
  661. $item->get_type(),
  662. [
  663. LINK_EXERCISE,
  664. LINK_DROPBOX,
  665. LINK_STUDENTPUBLICATION,
  666. LINK_LEARNPATH,
  667. LINK_FORUM_THREAD,
  668. LINK_ATTENDANCE,
  669. LINK_SURVEY,
  670. LINK_HOTPOTATOES,
  671. ]
  672. )
  673. ) {
  674. if (!empty($score[0])) {
  675. $row[] = $temp_score.' ';
  676. } else {
  677. $row[] = '';
  678. }
  679. } else {
  680. $row[] = $temp_score.' ';
  681. }
  682. } else {
  683. $row[] = $temp_score;
  684. }
  685. }
  686. $item_value_total += $item_value;
  687. }
  688. return [
  689. 'item_total' => $item_total,
  690. 'item_value_total' => $item_value_total,
  691. 'evaluations_added' => $evaluationsAdded,
  692. ];
  693. }
  694. /**
  695. * Get actual array data evaluation/link scores.
  696. *
  697. * @param int $session_id
  698. *
  699. * @return array
  700. */
  701. public function getEvaluationSummaryResults($session_id = null)
  702. {
  703. $usertable = [];
  704. foreach ($this->users as $user) {
  705. $usertable[] = $user;
  706. }
  707. $selected_users = $usertable;
  708. // generate actual data array for all selected users
  709. $data = [];
  710. foreach ($selected_users as $user) {
  711. $row = [];
  712. for ($count = 0; $count < count($this->evals_links); $count++) {
  713. $item = $this->evals_links[$count];
  714. $score = $item->calc_score($user[0]);
  715. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0] * 100) / $score[1] : 0;
  716. $row[$item->get_name()] = $porcent_score;
  717. }
  718. $data[$user[0]] = $row;
  719. }
  720. // get evaluations for every user by item
  721. $data_by_item = [];
  722. foreach ($data as $uid => $items) {
  723. $tmp = [];
  724. foreach ($items as $item => $value) {
  725. $tmp[] = $item;
  726. if (in_array($item, $tmp)) {
  727. $data_by_item[$item][$uid] = $value;
  728. }
  729. }
  730. }
  731. /* Get evaluation summary results
  732. (maximum, minimum and average of evaluations for all students)
  733. */
  734. $result = [];
  735. foreach ($data_by_item as $k => $v) {
  736. $average = round(array_sum($v) / count($v));
  737. arsort($v);
  738. $maximum = array_shift($v);
  739. $minimum = array_pop($v);
  740. if (is_null($minimum)) {
  741. $minimum = 0;
  742. }
  743. $summary = [
  744. 'max' => $maximum,
  745. 'min' => $minimum,
  746. 'avg' => $average,
  747. ];
  748. $result[$k] = $summary;
  749. }
  750. return $result;
  751. }
  752. /**
  753. * @return array
  754. */
  755. public function get_data_to_graph()
  756. {
  757. // do some checks on users/items counts, redefine if invalid values
  758. $usertable = [];
  759. foreach ($this->users as $user) {
  760. $usertable[] = $user;
  761. }
  762. // sort users array
  763. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  764. $data = [];
  765. $selected_users = $usertable;
  766. foreach ($selected_users as $user) {
  767. $row = [];
  768. $row[] = $user[0]; // user id
  769. $item_value = 0;
  770. $item_total = 0;
  771. for ($count = 0; $count < count($this->evals_links); $count++) {
  772. $item = $this->evals_links[$count];
  773. $score = $item->calc_score($user[0]);
  774. $divide = (($score[1]) == 0) ? 1 : $score[1];
  775. $item_value += $score[0] / $divide * $item->get_weight();
  776. $item_total += $item->get_weight();
  777. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  778. $score_final = ($score[0] / $score_denom) * 100;
  779. $row[] = $score_final;
  780. }
  781. //$total_score = array($item_value, $item_total);
  782. $score_final = ($item_value / $item_total) * 100;
  783. $row[] = $score_final;
  784. $data[] = $row;
  785. }
  786. return $data;
  787. }
  788. /**
  789. * This is a function to show the generated data.
  790. *
  791. * @param bool $displayWarning
  792. *
  793. * @return array
  794. */
  795. public function get_data_to_graph2($displayWarning = true)
  796. {
  797. $course_code = api_get_course_id();
  798. $session_id = api_get_session_id();
  799. // do some checks on users/items counts, redefine if invalid values
  800. $usertable = [];
  801. foreach ($this->users as $user) {
  802. $usertable[] = $user;
  803. }
  804. // sort users array
  805. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  806. // generate actual data array
  807. $scoreDisplay = ScoreDisplay::instance();
  808. $data = [];
  809. $selected_users = $usertable;
  810. foreach ($selected_users as $user) {
  811. $row = [];
  812. $row[] = $user[0]; // user id
  813. $item_value = 0;
  814. $item_total = 0;
  815. $final_score = 0;
  816. $item_value_total = 0;
  817. $convert_using_the_global_weight = true;
  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. }