flatview_data_generator.class.php 37 KB

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