flatview_data_generator.class.php 34 KB

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