flatview_data_generator.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. * @author Bert Steppé
  8. *
  9. * @package chamilo.gradebook
  10. */
  11. class FlatViewDataGenerator
  12. {
  13. // Sorting types constants
  14. const FVDG_SORT_LASTNAME = 1;
  15. const FVDG_SORT_FIRSTNAME = 2;
  16. const FVDG_SORT_ASC = 4;
  17. const FVDG_SORT_DESC = 8;
  18. private $users;
  19. private $evals;
  20. private $links;
  21. private $evals_links;
  22. public $params;
  23. public $category;
  24. private $mainCourseCategory;
  25. /**
  26. * @param array $users
  27. * @param array $evals
  28. * @param array $links
  29. * @param array $params
  30. * @param Category|null $mainCourseCategory
  31. */
  32. public function __construct(
  33. $users = array(),
  34. $evals = array(),
  35. $links = array(),
  36. $params = array(),
  37. $mainCourseCategory = null
  38. ) {
  39. $this->users = (isset($users) ? $users : array());
  40. $this->evals = (isset($evals) ? $evals : array());
  41. $this->links = (isset($links) ? $links : array());
  42. $this->evals_links = array_merge($this->evals, $this->links);
  43. $this->params = $params;
  44. $this->mainCourseCategory = $mainCourseCategory;
  45. }
  46. /**
  47. * @return Category
  48. */
  49. public function getMainCourseCategory()
  50. {
  51. return $this->mainCourseCategory;
  52. }
  53. /**
  54. * Get total number of users (rows)
  55. * @return int
  56. */
  57. public function get_total_users_count()
  58. {
  59. return count($this->users);
  60. }
  61. /**
  62. * Get total number of evaluations/links (columns) (the 2 users columns not included)
  63. * @return int
  64. */
  65. public function get_total_items_count()
  66. {
  67. return count($this->evals_links);
  68. }
  69. /**
  70. * Get array containing column header names (incl user columns)
  71. * @param int $items_start Start item offset
  72. * @param int $items_count Number of items to get
  73. * @param bool $show_detail whether to show the details or not
  74. * @return array List of headers
  75. */
  76. public function get_header_names($items_start = 0, $items_count = null, $show_detail = false)
  77. {
  78. $headers = array();
  79. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  80. $headers[] = get_lang('OfficialCode');
  81. }
  82. if (isset($this->params['join_firstname_lastname']) && $this->params['join_firstname_lastname']) {
  83. if (api_is_western_name_order()) {
  84. $headers[] = get_lang('FirstnameAndLastname');
  85. } else {
  86. $headers[] = get_lang('LastnameAndFirstname');
  87. }
  88. } else {
  89. if (api_is_western_name_order()) {
  90. $headers[] = get_lang('FirstName');
  91. $headers[] = get_lang('LastName');
  92. } else {
  93. $headers[] = get_lang('LastName');
  94. $headers[] = get_lang('FirstName');
  95. }
  96. }
  97. $headers[] = get_lang('Username');
  98. if (!isset($items_count)) {
  99. $items_count = count($this->evals_links) - $items_start;
  100. }
  101. $parent_id = $this->category->get_parent_id();
  102. if ($parent_id == 0 ||
  103. isset($this->params['only_subcat']) &&
  104. $this->params['only_subcat'] == $this->category->get_id()
  105. ) {
  106. $main_weight = $this->category->get_weight();
  107. $grade_model_id = $this->category->get_grade_model_id();
  108. } else {
  109. $main_cat = Category::load($parent_id, null, null);
  110. $main_weight = $main_cat[0]->get_weight();
  111. $grade_model_id = $main_cat[0]->get_grade_model_id();
  112. }
  113. $use_grade_model = true;
  114. if (empty($grade_model_id) || $grade_model_id == -1) {
  115. $use_grade_model = false;
  116. }
  117. //@todo move these in a function
  118. $sum_categories_weight_array = array();
  119. $mainCategoryId = null;
  120. $mainCourseCategory = $this->getMainCourseCategory();
  121. if (!empty($mainCourseCategory)) {
  122. $mainCategoryId = $mainCourseCategory->get_id();
  123. }
  124. if (isset($this->category) && !empty($this->category)) {
  125. $categories = Category::load(null, null, null, $this->category->get_id());
  126. if (!empty($categories)) {
  127. foreach ($categories as $category) {
  128. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  129. }
  130. } else {
  131. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  132. }
  133. }
  134. // No category was added
  135. $course_code = api_get_course_id();
  136. $session_id = api_get_session_id();
  137. $allcat = $this->category->get_subcategories(
  138. null,
  139. $course_code,
  140. $session_id,
  141. 'ORDER BY id'
  142. );
  143. $evaluationsAdded = array();
  144. if ($parent_id == 0 && !empty($allcat)) {
  145. // Means there are any subcategory
  146. foreach ($allcat as $sub_cat) {
  147. $sub_cat_weight = round(100 * $sub_cat->get_weight() / $main_weight, 1);
  148. $add_weight = " $sub_cat_weight %";
  149. $mainHeader = Display::url(
  150. $sub_cat->get_name(),
  151. api_get_self().'?selectcat='.$sub_cat->get_id().'&'.api_get_cidreq()
  152. ).$add_weight;
  153. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  154. $links = $sub_cat->get_links();
  155. $evaluations = $sub_cat->get_evaluations();
  156. /** @var ExerciseLink $link */
  157. $linkNameList = [];
  158. foreach ($links as $link) {
  159. $linkNameList[] = $link->get_name();
  160. }
  161. $evalNameList = [];
  162. foreach ($evaluations as $evaluation) {
  163. $evalNameList[] = $evaluation->get_name();
  164. }
  165. $finalList = array_merge($linkNameList, $evalNameList);
  166. if (!empty($finalList)) {
  167. $finalList[] = get_lang('Average');
  168. }
  169. $list = [];
  170. $list['items'] = $finalList;
  171. $list['header'] = '<center>'.$mainHeader.'</center>';
  172. $headers[] = $list;
  173. } else {
  174. $headers[] = '<center>'.$mainHeader.'</center>';
  175. }
  176. }
  177. } else {
  178. if (!isset($this->params['only_total_category']) ||
  179. (isset($this->params['only_total_category']) &&
  180. $this->params['only_total_category'] == false)
  181. ) {
  182. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  183. /** @var AbstractLink $item */
  184. $item = $this->evals_links[$count + $items_start];
  185. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  186. $headers[] = $item->get_name().' '.$weight.' % ';
  187. $evaluationsAdded[] = $item->get_id();
  188. }
  189. }
  190. }
  191. if (!empty($mainCategoryId)) {
  192. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  193. /** @var AbstractLink $item */
  194. $item = $this->evals_links[$count + $items_start];
  195. if ($mainCategoryId == $item->get_category_id() &&
  196. !in_array($item->get_id(), $evaluationsAdded)
  197. ) {
  198. $weight = round(100 * $item->get_weight() / $main_weight, 1);
  199. $headers[] = $item->get_name().' '.$weight.' % ';
  200. }
  201. }
  202. }
  203. $headers[] = '<center>'.api_strtoupper(get_lang('GradebookQualificationTotal')).'</center>';
  204. return $headers;
  205. }
  206. /**
  207. * @param int $id
  208. *
  209. * @return int
  210. */
  211. public function get_max_result_by_link($id)
  212. {
  213. $max = 0;
  214. foreach ($this->users as $user) {
  215. $item = $this->evals_links [$id];
  216. $score = $item->calc_score($user[0]);
  217. if ($score[0] > $max) {
  218. $max = $score[0];
  219. }
  220. }
  221. return $max;
  222. }
  223. /**
  224. * Get array containing evaluation items
  225. * @return array
  226. */
  227. public function get_evaluation_items($items_start = 0, $items_count = null)
  228. {
  229. $headers = array();
  230. if (!isset($items_count)) {
  231. $items_count = count($this->evals_links) - $items_start;
  232. }
  233. for ($count = 0; ($count < $items_count) && ($items_start + $count < count($this->evals_links)); $count++) {
  234. $item = $this->evals_links [$count + $items_start];
  235. $headers[] = $item->get_name();
  236. }
  237. return $headers;
  238. }
  239. /**
  240. * Get actual array data
  241. * @param integer $users_count
  242. * @param integer $items_count
  243. * @return array 2-dimensional array - each array contains the elements:
  244. * 0: user id
  245. * 1: user lastname
  246. * 2: user firstname
  247. * 3+: evaluation/link scores
  248. */
  249. public function get_data(
  250. $users_sorting = 0,
  251. $users_start = 0,
  252. $users_count = null,
  253. $items_start = 0,
  254. $items_count = null,
  255. $ignore_score_color = false,
  256. $show_all = false
  257. ) {
  258. // Do some checks on users/items counts, redefine if invalid values
  259. if (!isset($users_count)) {
  260. $users_count = count($this->users) - $users_start;
  261. }
  262. if ($users_count < 0) {
  263. $users_count = 0;
  264. }
  265. if (!isset($items_count)) {
  266. $items_count = count($this->evals) + count($this->links) - $items_start;
  267. }
  268. if ($items_count < 0) {
  269. $items_count = 0;
  270. }
  271. $userTable = array();
  272. foreach ($this->users as $user) {
  273. $userTable[] = $user;
  274. }
  275. // sort users array
  276. if ($users_sorting & self::FVDG_SORT_LASTNAME) {
  277. usort($userTable, array('FlatViewDataGenerator', 'sort_by_last_name'));
  278. } elseif ($users_sorting & self::FVDG_SORT_FIRSTNAME) {
  279. usort($userTable, array('FlatViewDataGenerator', 'sort_by_first_name'));
  280. }
  281. if ($users_sorting & self::FVDG_SORT_DESC) {
  282. $userTable = array_reverse($userTable);
  283. }
  284. // Select the requested users
  285. $selected_users = array_slice($userTable, $users_start, $users_count);
  286. // Generate actual data array
  287. $scoredisplay = ScoreDisplay::instance();
  288. $data = array();
  289. $displaytype = SCORE_DIV;
  290. if ($ignore_score_color) {
  291. $displaytype |= SCORE_IGNORE_SPLIT;
  292. }
  293. //@todo move these in a function
  294. $sum_categories_weight_array = array();
  295. $mainCategoryId = null;
  296. $mainCourseCategory = $this->getMainCourseCategory();
  297. if (!empty($mainCourseCategory)) {
  298. $mainCategoryId = $mainCourseCategory->get_id();
  299. }
  300. if (isset($this->category) && !empty($this->category)) {
  301. $categories = Category::load(null, null, null, $this->category->get_id());
  302. if (!empty($categories)) {
  303. foreach ($categories as $category) {
  304. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  305. }
  306. } else {
  307. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  308. }
  309. }
  310. $parent_id = $this->category->get_parent_id();
  311. if (
  312. $parent_id == 0 ||
  313. (isset($this->params['only_subcat']) && $this->params['only_subcat'] == $this->category->get_id())
  314. ) {
  315. $main_weight = $this->category->get_weight();
  316. $grade_model_id = $this->category->get_grade_model_id();
  317. } else {
  318. $main_cat = Category::load($parent_id, null, null);
  319. $main_weight = $main_cat[0]->get_weight();
  320. $grade_model_id = $main_cat[0]->get_grade_model_id();
  321. }
  322. $use_grade_model = true;
  323. if (empty($grade_model_id) || $grade_model_id == -1) {
  324. $use_grade_model = false;
  325. }
  326. $export_to_pdf = false;
  327. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  328. $export_to_pdf = true;
  329. }
  330. foreach ($selected_users as $user) {
  331. $row = array();
  332. // User id
  333. if ($export_to_pdf) {
  334. $row['user_id'] = $user_id = $user[0];
  335. } else {
  336. $row[] = $user_id = $user[0];
  337. }
  338. // Official code
  339. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  340. if ($export_to_pdf) {
  341. $row['official_code'] = $user[4];
  342. } else {
  343. $row[] = $user[4];
  344. }
  345. }
  346. // Last name
  347. if (isset($this->params['join_firstname_lastname']) &&
  348. $this->params['join_firstname_lastname']
  349. ) {
  350. if ($export_to_pdf) {
  351. $row['name'] = api_get_person_name($user[3], $user[2]);
  352. } else {
  353. $row[] = api_get_person_name($user[3], $user[2]);
  354. }
  355. } else {
  356. if ($export_to_pdf) {
  357. if (api_is_western_name_order()) {
  358. $row['firstname'] = $user[3];
  359. $row['lastname'] = $user[2];
  360. } else {
  361. $row['lastname'] = $user[2];
  362. $row['firstname'] = $user[3];
  363. }
  364. } else {
  365. if (api_is_western_name_order()) {
  366. $row[] = $user[3]; //first name
  367. $row[] = $user[2]; //last name
  368. } else {
  369. $row[] = $user[2]; //last name
  370. $row[] = $user[3]; //first name
  371. }
  372. }
  373. }
  374. $row[] = $user[1];
  375. $item_value = 0;
  376. $item_value_total = 0;
  377. $item_total = 0;
  378. $convert_using_the_global_weight = true;
  379. $course_code = api_get_course_id();
  380. $session_id = api_get_session_id();
  381. $allcat = $this->category->get_subcategories(
  382. null,
  383. $course_code,
  384. $session_id,
  385. 'ORDER BY id'
  386. );
  387. $evaluationsAdded = array();
  388. if ($parent_id == 0 && !empty($allcat)) {
  389. /** @var Category $sub_cat */
  390. foreach ($allcat as $sub_cat) {
  391. $score = $sub_cat->calc_score($user_id);
  392. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  393. $links = $sub_cat->get_links();
  394. $evaluations = $sub_cat->get_evaluations();
  395. /** @var ExerciseLink $link */
  396. $linkScoreList = [];
  397. foreach ($links as $link) {
  398. $linkScore = $link->calc_score($user_id);
  399. $linkScoreList[] = $scoredisplay->display_score(
  400. $linkScore,
  401. SCORE_SIMPLE
  402. );
  403. }
  404. $evalScoreList = [];
  405. foreach ($evaluations as $evaluation) {
  406. $evalScore = $evaluation->calc_score($user_id);
  407. $evalScoreList[] = $scoredisplay->display_score(
  408. $evalScore,
  409. SCORE_SIMPLE
  410. );
  411. }
  412. }
  413. $real_score = $score;
  414. $divide = $score[1] == 0 ? 1 : $score[1];
  415. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  416. $item_value = $score[0] / $divide * $main_weight;
  417. // Fixing total when using one or multiple gradebooks
  418. $percentage = $sub_cat->get_weight() / ($sub_cat_percentage) * $sub_cat_percentage / $this->category->get_weight();
  419. $item_value = $percentage * $item_value;
  420. $item_total += $sub_cat->get_weight();
  421. /*
  422. if ($convert_using_the_global_weight) {
  423. $score[0] = $score[0]/$main_weight*$sub_cat->get_weight();
  424. $score[1] = $main_weight ;
  425. }
  426. */
  427. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  428. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  429. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  430. $temp_score = Display::tip($real_score, $temp_score);
  431. } else {
  432. $real_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  433. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  434. $temp_score = Display::tip($temp_score, $real_score);
  435. }
  436. if (!isset($this->params['only_total_category']) ||
  437. (isset($this->params['only_total_category']) &&
  438. $this->params['only_total_category'] == false)
  439. ) {
  440. if (!$show_all) {
  441. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  442. $finalList = array_merge($linkScoreList, $evalScoreList);
  443. if (empty($finalList)) {
  444. $average = 0;
  445. } else {
  446. $average = array_sum($finalList) / count($finalList);
  447. }
  448. $finalList[] = round($average, 2);
  449. foreach ($finalList as $finalValue) {
  450. $row[] = '<center>'.$finalValue.'</center>';
  451. }
  452. } else {
  453. $row[] = $temp_score.' ';
  454. }
  455. } else {
  456. $row[] = $temp_score;
  457. }
  458. }
  459. $item_value_total += $item_value;
  460. }
  461. if ($convert_using_the_global_weight) {
  462. //$item_total = $main_weight;
  463. }
  464. } else {
  465. // All evaluations
  466. $result = $this->parseEvaluations(
  467. $user_id,
  468. $sum_categories_weight_array,
  469. $items_count,
  470. $items_start,
  471. $show_all,
  472. $row
  473. );
  474. $item_total += $result['item_total'];
  475. $item_value_total += $result['item_value_total'];
  476. $evaluationsAdded = $result['evaluations_added'];
  477. $item_total = $main_weight;
  478. }
  479. // All evaluations
  480. $result = $this->parseEvaluations(
  481. $user_id,
  482. $sum_categories_weight_array,
  483. $items_count,
  484. $items_start,
  485. $show_all,
  486. $row,
  487. $mainCategoryId,
  488. $evaluationsAdded
  489. );
  490. $item_total += $result['item_total'];
  491. $item_value_total += $result['item_value_total'];
  492. $total_score = array($item_value_total, $item_total);
  493. if (!$show_all) {
  494. if ($export_to_pdf) {
  495. $row['total'] = $scoredisplay->display_score($total_score);
  496. } else {
  497. $row[] = $scoredisplay->display_score($total_score);
  498. }
  499. } else {
  500. if ($export_to_pdf) {
  501. $row['total'] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  502. } else {
  503. $row[] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  504. }
  505. }
  506. unset($score);
  507. $data[] = $row;
  508. }
  509. return $data;
  510. }
  511. /**
  512. * Parse evaluations
  513. *
  514. * @param int $user_id
  515. * @param array $sum_categories_weight_array
  516. * @param int $items_count
  517. * @param int $items_start
  518. * @param int $show_all
  519. * @param int $parentCategoryIdFilter filter by category id if set
  520. * @return array
  521. */
  522. public function parseEvaluations(
  523. $user_id,
  524. $sum_categories_weight_array,
  525. $items_count,
  526. $items_start,
  527. $show_all,
  528. & $row,
  529. $parentCategoryIdFilter = null,
  530. $evaluationsAlreadyAdded = array()
  531. ) {
  532. // Generate actual data array
  533. $scoredisplay = ScoreDisplay::instance();
  534. $item_total = 0;
  535. $item_value_total = 0;
  536. $evaluationsAdded = array();
  537. for ($count = 0; $count < $items_count && ($items_start + $count < count($this->evals_links)); $count++) {
  538. /** @var AbstractLink $item */
  539. $item = $this->evals_links[$count + $items_start];
  540. if (!empty($evaluationsAlreadyAdded)) {
  541. if (in_array($item->get_id(), $evaluationsAlreadyAdded)) {
  542. continue;
  543. }
  544. }
  545. if (!empty($parentCategoryIdFilter)) {
  546. if ($item->get_category_id() != $parentCategoryIdFilter) {
  547. continue;
  548. }
  549. }
  550. $evaluationsAdded[] = $item->get_id();
  551. $score = $item->calc_score($user_id);
  552. $real_score = $score;
  553. $divide = isset($score[1]) && !empty($score[1]) && $score[1] > 0 ? $score[1] : 1;
  554. // Sub cat weight
  555. //$sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
  556. $item_value = isset($score[0]) ? $score[0] / $divide : 0;
  557. // Fixing total when using one or multiple gradebooks.
  558. if (empty($parentCategoryIdFilter)) {
  559. if ($this->category->get_parent_id() == 0) {
  560. if (isset($score[0])) {
  561. $item_value = $score[0] / $divide * $item->get_weight();
  562. } else {
  563. $item_value = 0;
  564. }
  565. } else {
  566. $item_value = $item_value * $item->get_weight();
  567. }
  568. } else {
  569. $item_value = $score[0] / $divide * $item->get_weight();
  570. }
  571. $item_total += $item->get_weight();
  572. $complete_score = $scoredisplay->display_score($score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  573. //$temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  574. //$temp_score = Display::tip($temp_score, $complete_score);
  575. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  576. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  577. $temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  578. $temp_score = Display::tip($real_score, $temp_score);
  579. } else {
  580. $temp_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  581. $temp_score = Display::tip($temp_score, $complete_score);
  582. }
  583. if (!isset($this->params['only_total_category']) ||
  584. (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)
  585. ) {
  586. if (!$show_all) {
  587. if (in_array(
  588. $item->get_type(),
  589. array(
  590. LINK_EXERCISE,
  591. LINK_DROPBOX,
  592. LINK_STUDENTPUBLICATION,
  593. LINK_LEARNPATH,
  594. LINK_FORUM_THREAD,
  595. LINK_ATTENDANCE,
  596. LINK_SURVEY,
  597. LINK_HOTPOTATOES,
  598. )
  599. )
  600. ) {
  601. if (!empty($score[0])) {
  602. $row[] = $temp_score.' ';
  603. } else {
  604. $row[] = '';
  605. }
  606. } else {
  607. $row[] = $temp_score.' ';
  608. }
  609. } else {
  610. $row[] = $temp_score;
  611. }
  612. }
  613. $item_value_total += $item_value;
  614. }
  615. return array(
  616. 'item_total' => $item_total,
  617. 'item_value_total' => $item_value_total,
  618. 'evaluations_added' => $evaluationsAdded
  619. );
  620. }
  621. /**
  622. * Get actual array data evaluation/link scores
  623. * @param int $session_id
  624. * @return array
  625. */
  626. public function get_evaluation_sumary_results($session_id = null)
  627. {
  628. $usertable = array();
  629. foreach ($this->users as $user) {
  630. $usertable[] = $user;
  631. }
  632. $selected_users = $usertable;
  633. // generate actual data array for all selected users
  634. $data = array();
  635. foreach ($selected_users as $user) {
  636. $row = array();
  637. for ($count = 0; $count < count($this->evals_links); $count++) {
  638. $item = $this->evals_links [$count];
  639. $score = $item->calc_score($user[0]);
  640. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0] * 100) / $score[1] : 0;
  641. $row[$item->get_name()] = $porcent_score;
  642. }
  643. $data[$user[0]] = $row;
  644. }
  645. // get evaluations for every user by item
  646. $data_by_item = array();
  647. foreach ($data as $uid => $items) {
  648. $tmp = array();
  649. foreach ($items as $item => $value) {
  650. $tmp[] = $item;
  651. if (in_array($item, $tmp)) {
  652. $data_by_item[$item][$uid] = $value;
  653. }
  654. }
  655. }
  656. /* Get evaluation summary results
  657. (maximum, minimum and average of evaluations for all students)
  658. */
  659. $result = array();
  660. foreach ($data_by_item as $k => $v) {
  661. $average = round(array_sum($v) / count($v));
  662. arsort($v);
  663. $maximum = array_shift($v);
  664. $minimum = array_pop($v);
  665. if (is_null($minimum)) {
  666. $minimum = 0;
  667. }
  668. $summary = array(
  669. 'max' => $maximum,
  670. 'min' => $minimum,
  671. 'avg' => $average
  672. );
  673. $result[$k] = $summary;
  674. }
  675. return $result;
  676. }
  677. /**
  678. * @return array
  679. */
  680. public function get_data_to_graph()
  681. {
  682. // do some checks on users/items counts, redefine if invalid values
  683. $usertable = array();
  684. foreach ($this->users as $user) {
  685. $usertable[] = $user;
  686. }
  687. // sort users array
  688. usort($usertable, array('FlatViewDataGenerator', 'sort_by_first_name'));
  689. $data = array();
  690. $selected_users = $usertable;
  691. foreach ($selected_users as $user) {
  692. $row = array();
  693. $row[] = $user[0]; // user id
  694. $item_value = 0;
  695. $item_total = 0;
  696. for ($count = 0; $count < count($this->evals_links); $count++) {
  697. $item = $this->evals_links[$count];
  698. $score = $item->calc_score($user[0]);
  699. $divide = (($score[1]) == 0) ? 1 : $score[1];
  700. $item_value += $score[0] / $divide * $item->get_weight();
  701. $item_total += $item->get_weight();
  702. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  703. $score_final = ($score[0] / $score_denom) * 100;
  704. $row[] = $score_final;
  705. }
  706. //$total_score = array($item_value, $item_total);
  707. $score_final = ($item_value / $item_total) * 100;
  708. $row[] = $score_final;
  709. $data[] = $row;
  710. }
  711. return $data;
  712. }
  713. /**
  714. * This is a function to show the generated data
  715. * @param bool $displayWarning
  716. * @return array
  717. */
  718. public function get_data_to_graph2($displayWarning = true)
  719. {
  720. // do some checks on users/items counts, redefine if invalid values
  721. $usertable = array();
  722. foreach ($this->users as $user) {
  723. $usertable[] = $user;
  724. }
  725. // sort users array
  726. usort($usertable, array('FlatViewDataGenerator', 'sort_by_first_name'));
  727. // generate actual data array
  728. $scoredisplay = ScoreDisplay::instance();
  729. $data = array();
  730. $selected_users = $usertable;
  731. foreach ($selected_users as $user) {
  732. $row = array();
  733. $row[] = $user[0]; // user id
  734. $item_value = 0;
  735. $item_total = 0;
  736. $final_score = 0;
  737. $item_value_total = 0;
  738. $convert_using_the_global_weight = true;
  739. $course_code = api_get_course_id();
  740. $session_id = api_get_session_id();
  741. $allcat = $this->category->get_subcategories(null, $course_code, $session_id, 'ORDER BY id');
  742. $parent_id = $this->category->get_parent_id();
  743. if ($parent_id == 0 && !empty($allcat)) {
  744. foreach ($allcat as $sub_cat) {
  745. $score = $sub_cat->calc_score($user[0]);
  746. $real_score = $score;
  747. $main_weight = $this->category->get_weight();
  748. $divide = $score[1] == 0 ? 1 : $score[1];
  749. //$sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  750. $item_value = $score[0] / $divide * $main_weight;
  751. $item_total += $sub_cat->get_weight();
  752. $row[] = array(
  753. $item_value,
  754. trim($scoredisplay->display_score($real_score, SCORE_CUSTOM, null, true))
  755. );
  756. $item_value_total += $item_value;
  757. $final_score += $score[0];
  758. //$final_score = ($final_score / $item_total) * 100;
  759. }
  760. $total_score = array($final_score, $item_total);
  761. $row[] = array(
  762. $final_score,
  763. trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true))
  764. );
  765. } else {
  766. for ($count = 0; $count < count($this->evals_links); $count++) {
  767. $item = $this->evals_links [$count];
  768. $score = $item->calc_score($user[0]);
  769. $divide = ($score[1]) == 0 ? 1 : $score[1];
  770. $item_value += $score[0] / $divide * $item->get_weight();
  771. $item_total += $item->get_weight();
  772. $score_denom = ($score[1] == 0) ? 1 : $score[1];
  773. $score_final = ($score[0] / $score_denom) * 100;
  774. $row[] = array($score_final, trim($scoredisplay->display_score($score, SCORE_CUSTOM, null, true)));
  775. }
  776. $total_score = array($item_value, $item_total);
  777. $score_final = ($item_value / $item_total) * 100;
  778. if ($displayWarning) {
  779. echo Display::return_message($total_score[1], 'warning');
  780. }
  781. $row[] = array($score_final, trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true)));
  782. }
  783. $data[] = $row;
  784. }
  785. return $data;
  786. }
  787. /**
  788. * @param $item1
  789. * @param $item2
  790. * @return int
  791. */
  792. public function sort_by_last_name($item1, $item2)
  793. {
  794. return api_strcmp($item1[2], $item2[2]);
  795. }
  796. /**
  797. * @param $item1
  798. * @param $item2
  799. * @return int
  800. */
  801. public function sort_by_first_name($item1, $item2)
  802. {
  803. return api_strcmp($item1[3], $item2[3]);
  804. }
  805. }