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. if (!$show_all) {
  532. $defaultStyle = empty($style) ? SCORE_DIV_PERCENT : (int) $style;
  533. $displayScore = $scoreDisplay->display_score($total_score, $defaultStyle);
  534. if (!empty($model)) {
  535. $displayScore = ExerciseLib::show_score($total_score[0], $total_score[1]);
  536. }
  537. if ($export_to_pdf) {
  538. $row['total'] = $displayScore;
  539. } else {
  540. $row[] = $displayScore;
  541. }
  542. } else {
  543. $defaultStyle = empty($style) ? SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS : (int) $style;
  544. $displayScore = $scoreDisplay->display_score($total_score, $defaultStyle);
  545. if (!empty($model)) {
  546. $displayScore = ExerciseLib::show_score($total_score[0], $total_score[1]);
  547. }
  548. if ($export_to_pdf) {
  549. $row['total'] = $displayScore;
  550. } else {
  551. $row[] = $displayScore;
  552. }
  553. }
  554. unset($score);
  555. $data[] = $row;
  556. }
  557. return $data;
  558. }
  559. /**
  560. * Parse evaluations.
  561. *
  562. * @param int $user_id
  563. * @param array $sum_categories_weight_array
  564. * @param int $items_count
  565. * @param int $items_start
  566. * @param int $show_all
  567. * @param int $parentCategoryIdFilter filter by category id if set
  568. *
  569. * @return array
  570. */
  571. public function parseEvaluations(
  572. $user_id,
  573. $sum_categories_weight_array,
  574. $items_count,
  575. $items_start,
  576. $show_all,
  577. &$row,
  578. $parentCategoryIdFilter = null,
  579. $evaluationsAlreadyAdded = []
  580. ) {
  581. // Generate actual data array
  582. $scoreDisplay = ScoreDisplay::instance();
  583. $item_total = 0;
  584. $item_value_total = 0;
  585. $evaluationsAdded = [];
  586. $model = ExerciseLib::getCourseScoreModel();
  587. for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) {
  588. /** @var AbstractLink $item */
  589. $item = $this->evals_links[$count + $items_start];
  590. if (!empty($evaluationsAlreadyAdded)) {
  591. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  592. continue;
  593. }
  594. }
  595. if (!empty($parentCategoryIdFilter)) {
  596. if ($item->get_category_id() != $parentCategoryIdFilter) {
  597. continue;
  598. }
  599. }
  600. $evaluationsAdded[] = $item->get_id();
  601. $score = $item->calc_score($user_id);
  602. $real_score = $score;
  603. $divide = isset($score[1]) && !empty($score[1]) && $score[1] > 0 ? $score[1] : 1;
  604. // Sub cat weight
  605. $item_value = isset($score[0]) ? $score[0] / $divide : 0;
  606. // Fixing total when using one or multiple gradebooks.
  607. if (empty($parentCategoryIdFilter)) {
  608. if ($this->category->get_parent_id() == 0) {
  609. if (isset($score[0])) {
  610. $item_value = $score[0] / $divide * $item->get_weight();
  611. } else {
  612. $item_value = 0;
  613. }
  614. } else {
  615. $item_value = $item_value * $item->get_weight();
  616. }
  617. } else {
  618. $item_value = $score[0] / $divide * $item->get_weight();
  619. }
  620. $item_total += $item->get_weight();
  621. $style = api_get_configuration_value('gradebook_report_score_style');
  622. $defaultStyle = SCORE_DIV_SIMPLE_WITH_CUSTOM;
  623. if (!empty($style)) {
  624. $defaultStyle = (int) $style;
  625. }
  626. $complete_score = $scoreDisplay->display_score(
  627. $score,
  628. SCORE_DIV_PERCENT,
  629. SCORE_ONLY_SCORE
  630. );
  631. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  632. $defaultShowPercentageValue = SCORE_SIMPLE;
  633. if (!empty($style)) {
  634. $defaultShowPercentageValue = $style;
  635. }
  636. $real_score = $scoreDisplay->display_score(
  637. $real_score,
  638. $defaultShowPercentageValue
  639. );
  640. $temp_score = $scoreDisplay->display_score(
  641. [$item_value, null],
  642. SCORE_DIV_SIMPLE_WITH_CUSTOM
  643. );
  644. $temp_score = Display::tip($real_score, $temp_score);
  645. } else {
  646. $temp_score = $scoreDisplay->display_score(
  647. $real_score,
  648. $defaultStyle
  649. );
  650. $temp_score = Display::tip($temp_score, $complete_score);
  651. }
  652. if (!empty($model)) {
  653. $scoreToShow = '';
  654. if (isset($score[0]) && isset($score[1])) {
  655. $scoreToShow = ExerciseLib::show_score($score[0], $score[1]);
  656. }
  657. $temp_score = $scoreToShow;
  658. }
  659. if (!isset($this->params['only_total_category']) ||
  660. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  661. ) {
  662. if (!$show_all) {
  663. if (in_array(
  664. $item->get_type(),
  665. [
  666. LINK_EXERCISE,
  667. LINK_DROPBOX,
  668. LINK_STUDENTPUBLICATION,
  669. LINK_LEARNPATH,
  670. LINK_FORUM_THREAD,
  671. LINK_ATTENDANCE,
  672. LINK_SURVEY,
  673. LINK_HOTPOTATOES,
  674. ]
  675. )
  676. ) {
  677. if (!empty($score[0])) {
  678. $row[] = $temp_score.' ';
  679. } else {
  680. $row[] = '';
  681. }
  682. } else {
  683. $row[] = $temp_score.' ';
  684. }
  685. } else {
  686. $row[] = $temp_score;
  687. }
  688. }
  689. $item_value_total += $item_value;
  690. }
  691. return [
  692. 'item_total' => $item_total,
  693. 'item_value_total' => $item_value_total,
  694. 'evaluations_added' => $evaluationsAdded,
  695. ];
  696. }
  697. /**
  698. * Get actual array data evaluation/link scores.
  699. *
  700. * @param int $session_id
  701. *
  702. * @return array
  703. */
  704. public function getEvaluationSummaryResults($session_id = null)
  705. {
  706. $usertable = [];
  707. foreach ($this->users as $user) {
  708. $usertable[] = $user;
  709. }
  710. $selected_users = $usertable;
  711. // generate actual data array for all selected users
  712. $data = [];
  713. foreach ($selected_users as $user) {
  714. $row = [];
  715. for ($count = 0; $count < count($this->evals_links); $count++) {
  716. $item = $this->evals_links[$count];
  717. $score = $item->calc_score($user[0]);
  718. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0] * 100) / $score[1] : 0;
  719. $row[$item->get_name()] = $porcent_score;
  720. }
  721. $data[$user[0]] = $row;
  722. }
  723. // get evaluations for every user by item
  724. $data_by_item = [];
  725. foreach ($data as $uid => $items) {
  726. $tmp = [];
  727. foreach ($items as $item => $value) {
  728. $tmp[] = $item;
  729. if (in_array($item, $tmp)) {
  730. $data_by_item[$item][$uid] = $value;
  731. }
  732. }
  733. }
  734. /* Get evaluation summary results
  735. (maximum, minimum and average of evaluations for all students)
  736. */
  737. $result = [];
  738. foreach ($data_by_item as $k => $v) {
  739. $average = round(array_sum($v) / count($v));
  740. arsort($v);
  741. $maximum = array_shift($v);
  742. $minimum = array_pop($v);
  743. if (is_null($minimum)) {
  744. $minimum = 0;
  745. }
  746. $summary = [
  747. 'max' => $maximum,
  748. 'min' => $minimum,
  749. 'avg' => $average,
  750. ];
  751. $result[$k] = $summary;
  752. }
  753. return $result;
  754. }
  755. /**
  756. * @return array
  757. */
  758. public function get_data_to_graph()
  759. {
  760. // do some checks on users/items counts, redefine if invalid values
  761. $usertable = [];
  762. foreach ($this->users as $user) {
  763. $usertable[] = $user;
  764. }
  765. // sort users array
  766. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  767. $data = [];
  768. $selected_users = $usertable;
  769. foreach ($selected_users as $user) {
  770. $row = [];
  771. $row[] = $user[0]; // user id
  772. $item_value = 0;
  773. $item_total = 0;
  774. for ($count = 0; $count < count($this->evals_links); $count++) {
  775. $item = $this->evals_links[$count];
  776. $score = $item->calc_score($user[0]);
  777. $divide = (($score[1]) == 0) ? 1 : $score[1];
  778. $item_value += $score[0] / $divide * $item->get_weight();
  779. $item_total += $item->get_weight();
  780. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  781. $score_final = ($score[0] / $score_denom) * 100;
  782. $row[] = $score_final;
  783. }
  784. $score_final = ($item_value / $item_total) * 100;
  785. $row[] = $score_final;
  786. $data[] = $row;
  787. }
  788. return $data;
  789. }
  790. /**
  791. * This is a function to show the generated data.
  792. *
  793. * @param bool $displayWarning
  794. *
  795. * @return array
  796. */
  797. public function get_data_to_graph2($displayWarning = true)
  798. {
  799. $course_code = api_get_course_id();
  800. $session_id = api_get_session_id();
  801. // do some checks on users/items counts, redefine if invalid values
  802. $usertable = [];
  803. foreach ($this->users as $user) {
  804. $usertable[] = $user;
  805. }
  806. // sort users array
  807. usort($usertable, ['FlatViewDataGenerator', 'sort_by_first_name']);
  808. // generate actual data array
  809. $scoreDisplay = ScoreDisplay::instance();
  810. $data = [];
  811. $selected_users = $usertable;
  812. foreach ($selected_users as $user) {
  813. $row = [];
  814. $row[] = $user[0]; // user id
  815. $item_value = 0;
  816. $item_total = 0;
  817. $final_score = 0;
  818. $item_value_total = 0;
  819. $allcat = $this->category->get_subcategories(
  820. null,
  821. $course_code,
  822. $session_id,
  823. 'ORDER BY id'
  824. );
  825. $parent_id = $this->category->get_parent_id();
  826. if ($parent_id == 0 && !empty($allcat)) {
  827. foreach ($allcat as $sub_cat) {
  828. $score = $sub_cat->calc_score($user[0]);
  829. $real_score = $score;
  830. $main_weight = $this->category->get_weight();
  831. $divide = $score[1] == 0 ? 1 : $score[1];
  832. //$sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  833. $item_value = $score[0] / $divide * $main_weight;
  834. $item_total += $sub_cat->get_weight();
  835. $row[] = [
  836. $item_value,
  837. trim(
  838. $scoreDisplay->display_score(
  839. $real_score,
  840. SCORE_CUSTOM,
  841. null,
  842. true
  843. )
  844. ),
  845. ];
  846. $item_value_total += $item_value;
  847. $final_score += $score[0];
  848. //$final_score = ($final_score / $item_total) * 100;
  849. }
  850. $total_score = [$final_score, $item_total];
  851. $row[] = [
  852. $final_score,
  853. trim(
  854. $scoreDisplay->display_score(
  855. $total_score,
  856. SCORE_CUSTOM,
  857. null,
  858. true
  859. )
  860. ),
  861. ];
  862. } else {
  863. for ($count = 0; $count < count($this->evals_links); $count++) {
  864. $item = $this->evals_links[$count];
  865. $score = $item->calc_score($user[0]);
  866. $divide = $score[1] == 0 ? 1 : $score[1];
  867. $item_value += $score[0] / $divide * $item->get_weight();
  868. $item_total += $item->get_weight();
  869. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  870. $score_final = ($score[0] / $score_denom) * 100;
  871. $row[] = [
  872. $score_final,
  873. trim(
  874. $scoreDisplay->display_score(
  875. $score,
  876. SCORE_CUSTOM,
  877. null,
  878. true
  879. )
  880. ),
  881. ];
  882. }
  883. $total_score = [$item_value, $item_total];
  884. $score_final = ($item_value / $item_total) * 100;
  885. if ($displayWarning) {
  886. echo Display::return_message($total_score[1], 'warning');
  887. }
  888. $row[] = [
  889. $score_final,
  890. trim(
  891. $scoreDisplay->display_score(
  892. $total_score,
  893. SCORE_CUSTOM,
  894. null,
  895. true
  896. )
  897. ),
  898. ];
  899. }
  900. $data[] = $row;
  901. }
  902. return $data;
  903. }
  904. /**
  905. * @param $item1
  906. * @param $item2
  907. *
  908. * @return int
  909. */
  910. public function sort_by_last_name($item1, $item2)
  911. {
  912. return api_strcmp($item1[2], $item2[2]);
  913. }
  914. /**
  915. * @param $item1
  916. * @param $item2
  917. *
  918. * @return int
  919. */
  920. public function sort_by_first_name($item1, $item2)
  921. {
  922. return api_strcmp($item1[3], $item2[3]);
  923. }
  924. /**
  925. * Add columns heders according to gradebook_flatview_extrafields_columns conf setting.
  926. *
  927. * @param array $headers
  928. */
  929. private function addExtraFieldColumnsHeaders(array &$headers)
  930. {
  931. $extraFieldColumns = api_get_configuration_value('gradebook_flatview_extrafields_columns');
  932. if (!$extraFieldColumns || !is_array($extraFieldColumns)) {
  933. return;
  934. }
  935. foreach ($extraFieldColumns['variables'] as $extraFieldVariable) {
  936. $extraField = new ExtraField('user');
  937. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldVariable);
  938. $headers[] = $extraFieldInfo['display_text'];
  939. }
  940. }
  941. /**
  942. * Add columns data according to gradebook_flatview_extrafields_columns conf setting.
  943. *
  944. * @param array $row
  945. * @param int $userId
  946. */
  947. private function addExtraFieldColumnsData(array &$row, $userId)
  948. {
  949. $extraFieldColumns = api_get_configuration_value('gradebook_flatview_extrafields_columns');
  950. if (!$extraFieldColumns || !is_array($extraFieldColumns)) {
  951. return;
  952. }
  953. foreach ($extraFieldColumns['variables'] as $extraFieldVariable) {
  954. $extraFieldValue = new ExtraFieldValue('user');
  955. $extraFieldValueInfo = $extraFieldValue->get_values_by_handler_and_field_variable(
  956. $userId,
  957. $extraFieldVariable
  958. );
  959. $row[] = $extraFieldValueInfo ? $extraFieldValueInfo['value'] : null;
  960. }
  961. }
  962. }