flatview_data_generator.class.php 32 KB

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