gradebooktable.class.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. <?php
  2. /* For licensing terms, see license.txt */
  3. use CpChart\Cache as pCache;
  4. use CpChart\Data as pData;
  5. use CpChart\Image as pImage;
  6. /**
  7. * GradebookTable Class
  8. * Table to display categories, evaluations and links.
  9. *
  10. * @author Stijn Konings
  11. * @author Bert Steppé (refactored, optimised)
  12. *
  13. * @package chamilo.gradebook
  14. */
  15. class GradebookTable extends SortableTable
  16. {
  17. public $cats;
  18. public $exportToPdf;
  19. public $teacherView;
  20. public $userId;
  21. public $studentList = [];
  22. private $currentcat;
  23. private $datagen;
  24. private $evals_links;
  25. private $dataForGraph;
  26. /**
  27. * @var array Indicates which columns should be shown in gradebook
  28. *
  29. * @example [1] For add Ranking column
  30. * [2] For add Best Score column
  31. * [3] For add Average column
  32. */
  33. private $loadStats = [];
  34. /**
  35. * GradebookTable constructor.
  36. *
  37. * @param Category $currentcat
  38. * @param array $cats
  39. * @param array $evals
  40. * @param array $links
  41. * @param null $addparams
  42. * @param bool $exportToPdf
  43. * @param null $showTeacherView
  44. * @param int $userId
  45. * @param array $studentList
  46. * @param array $loadStats
  47. */
  48. public function __construct(
  49. $currentcat,
  50. $cats = [],
  51. $evals = [],
  52. $links = [],
  53. $addparams = null,
  54. $exportToPdf = false,
  55. $showTeacherView = null,
  56. $userId = null,
  57. $studentList = [],
  58. array $loadStats = []
  59. ) {
  60. $this->teacherView = is_null($showTeacherView) ? api_is_allowed_to_edit(null, true) : $showTeacherView;
  61. $this->userId = is_null($userId) ? api_get_user_id() : $userId;
  62. $this->exportToPdf = $exportToPdf;
  63. parent::__construct(
  64. 'gradebooklist',
  65. null,
  66. null,
  67. api_is_allowed_to_edit() ? 1 : 0,
  68. 20,
  69. 'ASC',
  70. 'gradebook_list'
  71. );
  72. $this->evals_links = array_merge($evals, $links);
  73. $this->currentcat = $currentcat;
  74. $this->cats = $cats;
  75. $this->loadStats = $loadStats;
  76. $this->datagen = new GradebookDataGenerator($cats, $evals, $links);
  77. if (!empty($userId)) {
  78. $this->datagen->userId = $userId;
  79. }
  80. if (isset($addparams)) {
  81. $this->set_additional_parameters($addparams);
  82. }
  83. $column = 0;
  84. if ($this->teacherView) {
  85. if ($this->exportToPdf == false) {
  86. $this->set_header($column++, '', '', 'width="25px"');
  87. }
  88. }
  89. $this->set_header($column++, get_lang('Type'), '', 'width="35px"');
  90. $this->set_header($column++, get_lang('Name'), false);
  91. if ($this->exportToPdf == false) {
  92. $this->set_header($column++, get_lang('Description'), false);
  93. }
  94. $model = ExerciseLib::getCourseScoreModel();
  95. if ($this->teacherView) {
  96. $this->set_header(
  97. $column++,
  98. get_lang('Weight'),
  99. '',
  100. 'width="100px"'
  101. );
  102. } else {
  103. $this->set_header($column++, get_lang('Weight'), false);
  104. $this->set_header($column++, get_lang('Result'), false);
  105. if (empty($model)) {
  106. if (in_array(1, $this->loadStats)) {
  107. $this->set_header($column++, get_lang('Ranking'), false);
  108. }
  109. if (in_array(2, $this->loadStats)) {
  110. $this->set_header($column++, get_lang('BestScore'), false);
  111. }
  112. if (in_array(3, $this->loadStats)) {
  113. $this->set_header($column++, get_lang('Average'), false);
  114. }
  115. }
  116. if (!empty($cats)) {
  117. if ($this->exportToPdf == false) {
  118. $this->set_header($column++, get_lang('Actions'), false);
  119. }
  120. }
  121. }
  122. // Deactivates the odd/even alt rows in order that the +/- buttons work see #4047
  123. $this->odd_even_rows_enabled = false;
  124. // Admins get an edit column.
  125. if ($this->teacherView) {
  126. $this->set_header($column++, get_lang('Modify'), false, 'width="195px"');
  127. // Actions on multiple selected documents.
  128. $this->set_form_actions(
  129. [
  130. 'setvisible' => get_lang('SetVisible'),
  131. 'setinvisible' => get_lang('SetInvisible'),
  132. 'deleted' => get_lang('DeleteSelected'),
  133. ]
  134. );
  135. } else {
  136. if (empty($_GET['selectcat']) && !$this->teacherView) {
  137. if ($this->exportToPdf == false) {
  138. $this->set_header(
  139. $column++,
  140. get_lang('Certificates'),
  141. false
  142. );
  143. }
  144. }
  145. }
  146. }
  147. /**
  148. * @return GradebookDataGenerator
  149. */
  150. public function get_data()
  151. {
  152. return $this->datagen;
  153. }
  154. /**
  155. * Function used by SortableTable to get total number of items in the table.
  156. *
  157. * @return int
  158. */
  159. public function get_total_number_of_items()
  160. {
  161. return $this->datagen->get_total_items_count();
  162. }
  163. /**
  164. * Function used by SortableTable to generate the data to display.
  165. *
  166. * @param int $from
  167. * @param int $per_page
  168. * @param int $column
  169. * @param string $direction
  170. * @param int $sort
  171. *
  172. * @return array|mixed
  173. */
  174. public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = null)
  175. {
  176. //variables load in index.php
  177. global $certificate_min_score;
  178. $isAllowedToEdit = api_is_allowed_to_edit();
  179. // determine sorting type
  180. $col_adjust = $isAllowedToEdit ? 1 : 0;
  181. // By id
  182. $this->column = 5;
  183. switch ($this->column) {
  184. // Type
  185. case 0 + $col_adjust:
  186. $sorting = GradebookDataGenerator::GDG_SORT_TYPE;
  187. break;
  188. case 1 + $col_adjust:
  189. $sorting = GradebookDataGenerator::GDG_SORT_NAME;
  190. break;
  191. case 2 + $col_adjust:
  192. $sorting = GradebookDataGenerator::GDG_SORT_DESCRIPTION;
  193. break;
  194. case 3 + $col_adjust:
  195. $sorting = GradebookDataGenerator::GDG_SORT_WEIGHT;
  196. break;
  197. case 4 + $col_adjust:
  198. $sorting = GradebookDataGenerator::GDG_SORT_DATE;
  199. break;
  200. case 5 + $col_adjust:
  201. $sorting = GradebookDataGenerator::GDG_SORT_ID;
  202. break;
  203. }
  204. if ($this->direction == 'DESC') {
  205. $sorting |= GradebookDataGenerator::GDG_SORT_DESC;
  206. } else {
  207. $sorting |= GradebookDataGenerator::GDG_SORT_ASC;
  208. }
  209. // Status of user in course.
  210. $user_id = $this->userId;
  211. $course_code = api_get_course_id();
  212. $session_id = api_get_session_id();
  213. $statusToFilter = 0;
  214. if (empty($session_id)) {
  215. $statusToFilter = STUDENT;
  216. }
  217. if (empty($this->studentList) && $this->loadStats) {
  218. $studentList = CourseManager::get_user_list_from_course_code(
  219. $course_code,
  220. $session_id,
  221. null,
  222. null,
  223. $statusToFilter
  224. );
  225. $this->studentList = $studentList;
  226. }
  227. $this->datagen->userId = $this->userId;
  228. $data_array = $this->datagen->get_data(
  229. $sorting,
  230. $from,
  231. $this->per_page,
  232. false,
  233. $this->studentList,
  234. $this->loadStats
  235. );
  236. // generate the data to display
  237. $sortable_data = [];
  238. $weight_total_links = 0;
  239. $main_cat = Category::load(
  240. null,
  241. null,
  242. $course_code,
  243. null,
  244. null,
  245. $session_id,
  246. 'ORDER BY id'
  247. );
  248. $total_categories_weight = 0;
  249. $scoredisplay = ScoreDisplay::instance();
  250. $totalUserResult = [0, 0];
  251. $totalBest = [0, 0];
  252. $totalAverage = [0, 0];
  253. $type = 'detail';
  254. if ($this->exportToPdf) {
  255. $type = 'simple';
  256. }
  257. $model = ExerciseLib::getCourseScoreModel();
  258. // Categories.
  259. if (!empty($data_array)) {
  260. foreach ($data_array as $data) {
  261. // list of items inside the gradebook (exercises, lps, forums, etc)
  262. $row = [];
  263. /** @var AbstractLink $item */
  264. $item = $mainCategory = $data[0];
  265. // If the item is invisible, wrap it in a span with class invisible
  266. $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '<span class="text-muted">' : '';
  267. $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '</span>' : '';
  268. // Id
  269. if ($this->teacherView) {
  270. if ($this->exportToPdf == false) {
  271. $row[] = $this->build_id_column($item);
  272. }
  273. }
  274. // Type.
  275. $row[] = $this->build_type_column($item);
  276. // Name.
  277. if (get_class($item) == 'Category') {
  278. $row[] = $invisibility_span_open.'<strong>'.$item->get_name().'</strong>'.$invisibility_span_close;
  279. $main_categories[$item->get_id()]['name'] = $item->get_name();
  280. } else {
  281. $name = $this->build_name_link($item, $type);
  282. $row[] = $invisibility_span_open.$name.$invisibility_span_close;
  283. $main_categories[$item->get_id()]['name'] = $name;
  284. }
  285. $this->dataForGraph['categories'][] = $item->get_name();
  286. $main_categories[$item->get_id()]['weight'] = $item->get_weight();
  287. $total_categories_weight += $item->get_weight();
  288. // Description.
  289. if ($this->exportToPdf == false) {
  290. $row[] = $invisibility_span_open.$data[2].$invisibility_span_close;
  291. }
  292. // Weight.
  293. $weight = $scoredisplay->display_score(
  294. [
  295. $data['3'],
  296. $this->currentcat->get_weight(),
  297. ],
  298. SCORE_SIMPLE,
  299. SCORE_BOTH,
  300. true
  301. );
  302. if ($this->teacherView) {
  303. $row[] = $invisibility_span_open.
  304. Display::tag('p', $weight, ['class' => 'score']).
  305. $invisibility_span_close;
  306. } else {
  307. $row[] = $invisibility_span_open.$weight.$invisibility_span_close;
  308. }
  309. $category_weight = $item->get_weight();
  310. $mainCategoryWeight = $main_cat[0]->get_weight();
  311. if ($this->teacherView) {
  312. $weight_total_links += $data[3];
  313. } else {
  314. $cattotal = Category::load($_GET['selectcat']);
  315. $scoretotal = $cattotal[0]->calc_score($this->userId);
  316. }
  317. // Edit (for admins).
  318. if ($this->teacherView) {
  319. $cat = new Category();
  320. $show_message = $cat->show_message_resource_delete($item->get_course_code());
  321. if ($show_message === false) {
  322. $row[] = $this->build_edit_column($item);
  323. }
  324. } else {
  325. $score = $item->calc_score($this->userId);
  326. $scoreToDisplay = '-';
  327. if (!empty($score[1])) {
  328. $completeScore = $scoredisplay->display_score($score, SCORE_DIV_PERCENT);
  329. $score = $score[0] / $score[1] * $item->get_weight();
  330. $score = $scoredisplay->display_score([$score, null], SCORE_SIMPLE);
  331. $scoreToDisplay = Display::tip($score, $completeScore);
  332. } else {
  333. $categoryScore = null;
  334. }
  335. // Students get the results and certificates columns
  336. if (1) {
  337. $value_data = isset($data[4]) ? $data[4] : null;
  338. $best = isset($data['best']) ? $data['best'] : null;
  339. $average = isset($data['average']) ? $data['average'] : null;
  340. $ranking = isset($data['ranking']) ? $data['ranking'] : null;
  341. $totalResult = [
  342. $data['result_score'][0],
  343. $data['result_score'][1],
  344. ];
  345. $totalUserResult[0] += $totalResult[0] / ($totalResult[1] ?: 1) * $data[3];
  346. $totalUserResult[1] += $data[3];
  347. $totalBest = [
  348. $scoredisplay->format_score($totalBest[0] + $data['best_score'][0]),
  349. $scoredisplay->format_score($totalBest[1] + $data['best_score'][1]),
  350. ];
  351. $totalAverage = [
  352. $data['average_score'][0],
  353. $data['average_score'][1],
  354. ];
  355. // Student result
  356. if (empty($model)) {
  357. $row[] = $value_data;
  358. } else {
  359. $row[] = ExerciseLib::show_score(
  360. $data['result_score'][0],
  361. $data['result_score'][1]
  362. );
  363. }
  364. $totalResultAverageValue = strip_tags(
  365. $scoredisplay->display_score(
  366. $totalResult,
  367. SCORE_AVERAGE
  368. )
  369. );
  370. $this->dataForGraph['my_result'][] = floatval($totalResultAverageValue);
  371. $this->dataForGraph['my_result_no_float'][] = $data['result_score'][0];
  372. $totalAverageValue = strip_tags($scoredisplay->display_score($totalAverage, SCORE_AVERAGE));
  373. $this->dataForGraph['average'][] = floatval($totalAverageValue);
  374. if (empty($model)) {
  375. // Ranking
  376. if (in_array(1, $this->loadStats)) {
  377. $row[] = $ranking;
  378. }
  379. // Best
  380. if (in_array(2, $this->loadStats)) {
  381. $row[] = $best;
  382. }
  383. // Average
  384. if (in_array(3, $this->loadStats)) {
  385. $row[] = $average;
  386. }
  387. }
  388. if (get_class($item) == 'Category') {
  389. if ($this->exportToPdf == false) {
  390. $row[] = $this->build_edit_column($item);
  391. }
  392. }
  393. } else {
  394. $row[] = $scoreToDisplay;
  395. if (!empty($this->cats)) {
  396. if ($this->exportToPdf == false) {
  397. $row[] = $this->build_edit_column($item);
  398. }
  399. }
  400. }
  401. }
  402. // Category added.
  403. $sortable_data[] = $row;
  404. // Loading children
  405. if (get_class($item) == 'Category') {
  406. $course_code = api_get_course_id();
  407. $session_id = api_get_session_id();
  408. $parent_id = $item->get_id();
  409. $cats = Category::load(
  410. $parent_id,
  411. null,
  412. null,
  413. null,
  414. null,
  415. null
  416. );
  417. if (isset($cats[0])) {
  418. /** @var Category $subCategory */
  419. $subCategory = $cats[0];
  420. $allcat = $subCategory->get_subcategories($this->userId, $course_code, $session_id);
  421. $alleval = $subCategory->get_evaluations($this->userId);
  422. $alllink = $subCategory->get_links($this->userId);
  423. $sub_cat_info = new GradebookDataGenerator($allcat, $alleval, $alllink);
  424. $sub_cat_info->userId = $user_id;
  425. $data_array2 = $sub_cat_info->get_data(
  426. $sorting,
  427. $from,
  428. $this->per_page,
  429. false,
  430. $this->studentList
  431. );
  432. $total_weight = 0;
  433. // Links.
  434. foreach ($data_array2 as $data) {
  435. $row = [];
  436. $item = $data[0];
  437. //if the item is invisible, wrap it in a span with class invisible
  438. $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '<span class="text-muted">' : '';
  439. $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '</span>' : '';
  440. if (isset($item)) {
  441. $main_categories[$parent_id]['children'][$item->get_id()]['name'] = $item->get_name();
  442. $main_categories[$parent_id]['children'][$item->get_id()]['weight'] = $item->get_weight();
  443. }
  444. if ($this->teacherView) {
  445. if ($this->exportToPdf == false) {
  446. $row[] = $this->build_id_column($item);
  447. }
  448. }
  449. // Type
  450. $row[] = $this->build_type_column($item, ['style' => 'padding-left:5px']);
  451. // Name.
  452. $row[] = $invisibility_span_open."&nbsp;&nbsp;&nbsp; ".
  453. $this->build_name_link($item, $type).$invisibility_span_close;
  454. // Description.
  455. if ($this->exportToPdf == false) {
  456. $row[] = $invisibility_span_open.$data[2].$invisibility_span_close;
  457. }
  458. $weight = $data[3];
  459. $total_weight += $weight;
  460. // Weight
  461. $row[] = $invisibility_span_open.$weight.$invisibility_span_close;
  462. if ($this->teacherView) {
  463. //$weight_total_links += intval($data[3]);
  464. } else {
  465. $cattotal = Category::load($_GET['selectcat']);
  466. $scoretotal = $cattotal[0]->calc_score($this->userId);
  467. }
  468. // Admins get an edit column.
  469. if (api_is_allowed_to_edit(null, true) &&
  470. isset($_GET['user_id']) == false &&
  471. (isset($_GET['action']) && $_GET['action'] != 'export_all' || !isset($_GET['action']))
  472. ) {
  473. $cat = new Category();
  474. $show_message = $cat->show_message_resource_delete($item->get_course_code());
  475. if ($show_message === false) {
  476. if ($this->exportToPdf == false) {
  477. $row[] = $this->build_edit_column($item);
  478. }
  479. }
  480. } else {
  481. // Students get the results and certificates columns
  482. $eval_n_links = array_merge($alleval, $alllink);
  483. if (count($eval_n_links) > 0) {
  484. $value_data = isset($data[4]) ? $data[4] : null;
  485. if (!is_null($value_data)) {
  486. // Result
  487. $row[] = $value_data;
  488. $best = isset($data['best']) ? $data['best'] : null;
  489. $average = isset($data['average']) ? $data['average'] : null;
  490. $ranking = isset($data['ranking']) ? $data['ranking'] : null;
  491. if (in_array(1, $this->loadStats)) {
  492. // Ranking
  493. $row[] = $ranking;
  494. }
  495. if (in_array(2, $this->loadStats)) {
  496. // Best
  497. $row[] = $best;
  498. }
  499. // Average
  500. if (in_array(3, $this->loadStats)) {
  501. $row[] = $average;
  502. }
  503. }
  504. }
  505. if (!empty($cats)) {
  506. if ($this->exportToPdf == false) {
  507. $row[] = null;
  508. }
  509. }
  510. }
  511. if ($this->exportToPdf == false) {
  512. $row['child_of'] = $parent_id;
  513. }
  514. $sortable_data[] = $row;
  515. }
  516. // "Warning row"
  517. if (!empty($data_array)) {
  518. if ($this->teacherView) {
  519. // Compare the category weight to the sum of all weights inside the category
  520. if (intval($total_weight) == $category_weight) {
  521. $label = null;
  522. $total = GradebookUtils::score_badges(
  523. [
  524. $total_weight.' / '.$category_weight,
  525. '100',
  526. ]
  527. );
  528. } else {
  529. $label = Display::return_icon(
  530. 'warning.png',
  531. sprintf(get_lang('TotalWeightMustBeX'), $category_weight)
  532. );
  533. $total = Display::badge($total_weight.' / '.$category_weight, 'warning');
  534. }
  535. $row = [
  536. null,
  537. null,
  538. "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h5>".get_lang('SubTotal').'</h5>',
  539. null,
  540. $total.' '.$label,
  541. 'child_of' => $parent_id,
  542. ];
  543. $sortable_data[] = $row;
  544. }
  545. }
  546. }
  547. }
  548. }
  549. } //end looping categories
  550. $main_weight = 0;
  551. if (count($main_cat) > 1) {
  552. /** @var Category $myCat */
  553. foreach ($main_cat as $myCat) {
  554. $myParentId = $myCat->get_parent_id();
  555. if ($myParentId == 0) {
  556. $main_weight = intval($myCat->get_weight());
  557. }
  558. }
  559. }
  560. if ($this->teacherView) {
  561. // Total for teacher.
  562. if (count($main_cat) > 1) {
  563. if (intval($total_categories_weight) == $main_weight) {
  564. $total = GradebookUtils::score_badges(
  565. [
  566. $total_categories_weight.' / '.$main_weight,
  567. '100',
  568. ]
  569. );
  570. } else {
  571. $total = Display::badge($total_categories_weight.' / '.$main_weight, 'warning');
  572. }
  573. $row = [
  574. null,
  575. null,
  576. '<strong>'.get_lang('Total').'</strong>',
  577. null,
  578. $total,
  579. ];
  580. $sortable_data[] = $row;
  581. }
  582. } else {
  583. // Total for student.
  584. if (count($main_cat) > 1) {
  585. $weights = [];
  586. foreach ($main_categories as $cat) {
  587. $weights[] = $cat['weight'];
  588. }
  589. $main_weight = intval($main_cat[0]->get_weight());
  590. $global = null;
  591. $average = null;
  592. $myTotal = 0;
  593. foreach ($this->dataForGraph['my_result_no_float'] as $result) {
  594. $myTotal += $scoredisplay->format_score($result);
  595. }
  596. $totalResult[0] = $myTotal;
  597. // Overwrite main weight
  598. $totalResult[1] = $main_weight;
  599. $totalResult = $scoredisplay->display_score(
  600. $totalResult,
  601. SCORE_DIV
  602. );
  603. $row = [
  604. null,
  605. '<h3>'.get_lang('Total').'</h3>',
  606. ];
  607. if (!$this->exportToPdf) {
  608. $row[] = null;
  609. }
  610. $row[] = $main_weight;
  611. $row[] = $totalResult;
  612. if (in_array(1, $this->loadStats)) {
  613. $totalRanking = [];
  614. $invalidateRanking = true;
  615. $average = 0;
  616. foreach ($this->studentList as $student) {
  617. $score = $main_cat[0]->calc_score($student['user_id']);
  618. if (!empty($score[0])) {
  619. $invalidateRanking = false;
  620. }
  621. $totalRanking[$student['user_id']] = $score[0];
  622. $average += $score[0];
  623. }
  624. $totalRanking = AbstractLink::getCurrentUserRanking($user_id, $totalRanking);
  625. $totalRanking = $scoredisplay->display_score(
  626. $totalRanking,
  627. SCORE_DIV,
  628. SCORE_BOTH,
  629. true
  630. );
  631. if ($invalidateRanking) {
  632. $totalRanking = null;
  633. }
  634. $row[] = $totalRanking;
  635. }
  636. if (in_array(2, $this->loadStats)) {
  637. // Overwrite main weight
  638. $totalBest[1] = $main_weight;
  639. $totalBest = $scoredisplay->display_score(
  640. $totalBest,
  641. SCORE_DIV,
  642. SCORE_BOTH,
  643. true
  644. );
  645. $row[] = $totalBest;
  646. }
  647. if (in_array(3, $this->loadStats)) {
  648. // Overwrite main weight
  649. $totalAverage[0] = $average / count($this->studentList);
  650. $totalAverage[1] = $main_weight;
  651. $totalAverage = $scoredisplay->display_score(
  652. $totalAverage,
  653. SCORE_DIV,
  654. SCORE_BOTH,
  655. true
  656. );
  657. $row[] = $totalAverage;
  658. }
  659. $sortable_data[] = $row;
  660. }
  661. }
  662. // Warning messages
  663. $view = isset($_GET['view']) ? $_GET['view'] : null;
  664. if ($this->teacherView) {
  665. if (isset($_GET['selectcat']) &&
  666. $_GET['selectcat'] > 0 &&
  667. $view != 'presence'
  668. ) {
  669. $id_cat = intval($_GET['selectcat']);
  670. $category = Category::load($id_cat);
  671. $weight_category = intval($this->build_weight($category[0]));
  672. $course_code = $this->build_course_code($category[0]);
  673. $weight_total_links = round($weight_total_links);
  674. if ($weight_total_links > $weight_category ||
  675. $weight_total_links < $weight_category ||
  676. $weight_total_links > $weight_category
  677. ) {
  678. $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $weight_category);
  679. $modify_icons = '<a href="gradebook_edit_cat.php?editcat='.$id_cat.'&cidReq='.$course_code.'&id_session='.api_get_session_id().'">'.
  680. Display::return_icon('edit.png', $warning_message, [], ICON_SIZE_SMALL).'</a>';
  681. $warning_message .= $modify_icons;
  682. echo Display::return_message($warning_message, 'warning', false);
  683. }
  684. $content_html = DocumentManager::replace_user_info_into_html(
  685. api_get_user_id(),
  686. $course_code,
  687. api_get_session_id()
  688. );
  689. if (!empty($content_html)) {
  690. $new_content = explode('</head>', $content_html['content']);
  691. }
  692. if (empty($new_content[0])) {
  693. // Set default certificate
  694. $courseData = api_get_course_info($course_code);
  695. DocumentManager::generateDefaultCertificate($courseData);
  696. }
  697. }
  698. if (empty($_GET['selectcat'])) {
  699. $categories = Category::load();
  700. $weight_categories = $certificate_min_scores = $course_codes = [];
  701. foreach ($categories as $category) {
  702. $course_code_category = $this->build_course_code($category);
  703. if (!empty($course_code)) {
  704. if ($course_code_category == $course_code) {
  705. $weight_categories[] = intval($this->build_weight($category));
  706. $certificate_min_scores[] = intval($this->build_certificate_min_score($category));
  707. $course_codes[] = $course_code;
  708. break;
  709. }
  710. } else {
  711. $weight_categories[] = intval($this->build_weight($category));
  712. $certificate_min_scores[] = intval($this->build_certificate_min_score($category));
  713. $course_codes[] = $course_code_category;
  714. }
  715. }
  716. if (is_array($weight_categories) &&
  717. is_array($certificate_min_scores) &&
  718. is_array($course_codes)
  719. ) {
  720. $warning_message = '';
  721. for ($x = 0; $x < count($weight_categories); $x++) {
  722. $weight_category = intval($weight_categories[$x]);
  723. $certificate_min_score = intval($certificate_min_scores[$x]);
  724. $course_code = $course_codes[$x];
  725. if (empty($certificate_min_score) ||
  726. ($certificate_min_score > $weight_category)
  727. ) {
  728. $warning_message .= $course_code.'&nbsp;-&nbsp;'.get_lang('CertificateMinimunScoreIsRequiredAndMustNotBeMoreThan').'&nbsp;'.$weight_category.'<br />';
  729. }
  730. }
  731. if (!empty($warning_message)) {
  732. echo Display::return_message($warning_message, 'warning', false);
  733. }
  734. }
  735. }
  736. }
  737. if (!$this->teacherView) {
  738. $rowTotal = [];
  739. $rowTotal[] = ' ';
  740. $rowTotal[] = get_lang('FinalScore');
  741. if (!$this->exportToPdf) {
  742. $rowTotal[] = ' ';
  743. }
  744. $rowTotal[] = ' ';
  745. $rowTotal[] = $scoredisplay->display_score(
  746. $totalUserResult,
  747. SCORE_DIV_PERCENT_WITH_CUSTOM
  748. );
  749. foreach ($this->loadStats as $col) {
  750. $rowTotal[] = ' ';
  751. }
  752. $sortable_data[] = $rowTotal;
  753. }
  754. return $sortable_data;
  755. }
  756. /**
  757. * @return string
  758. */
  759. public function getGraph()
  760. {
  761. $data = $this->getDataForGraph();
  762. if (!empty($data) &&
  763. isset($data['categories']) &&
  764. isset($data['my_result']) &&
  765. isset($data['average'])
  766. ) {
  767. $dataSet = new pData();
  768. $dataSet->addPoints($data['my_result'], get_lang('Me'));
  769. // In order to generate random values
  770. // $data['average'] = array(rand(0,50), rand(0,50));
  771. $dataSet->addPoints($data['average'], get_lang('Average'));
  772. $dataSet->addPoints($data['categories'], 'categories');
  773. $dataSet->setAbscissa("categories");
  774. $xSize = 600;
  775. $ySize = 400;
  776. $pChart = new pImage($xSize, $ySize, $dataSet);
  777. /* Turn of Antialiasing */
  778. $pChart->Antialias = false;
  779. /* Add a border to the picture */
  780. $pChart->drawRectangle(
  781. 0,
  782. 0,
  783. $xSize - 1,
  784. $ySize - 1,
  785. ["R" => 0, "G" => 0, "B" => 0]
  786. );
  787. $pChart->drawText(
  788. 10,
  789. 16,
  790. get_lang('Results'),
  791. ["FontSize" => 11, "Align" => TEXT_ALIGN_BOTTOMMIDDLE]
  792. );
  793. $pChart->setGraphArea(50, 30, $xSize - 50, $ySize - 70);
  794. $pChart->setFontProperties(
  795. [
  796. 'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  797. 'FontSize' => 10,
  798. ]
  799. );
  800. /* Draw the scale */
  801. $scaleSettings = [
  802. "XMargin" => AUTO,
  803. "YMargin" => 10,
  804. "Floating" => true,
  805. "GridR" => 200,
  806. "GridG" => 200,
  807. "GridB" => 200,
  808. "DrawSubTicks" => true,
  809. "CycleBackground" => true,
  810. 'LabelRotation' => 10,
  811. ];
  812. $pChart->drawScale($scaleSettings);
  813. /* Draw the line chart */
  814. $pChart->drawLineChart();
  815. $pChart->drawPlotChart(
  816. [
  817. "DisplayValues" => true,
  818. "PlotBorder" => true,
  819. "BorderSize" => 2,
  820. "Surrounding" => -60,
  821. "BorderAlpha" => 80,
  822. ]
  823. );
  824. /* Write the chart legend */
  825. $pChart->drawLegend(
  826. $xSize - 180,
  827. 9,
  828. [
  829. "Style" => LEGEND_NOBORDER,
  830. "Mode" => LEGEND_HORIZONTAL,
  831. "FontR" => 0,
  832. "FontG" => 0,
  833. "FontB" => 0,
  834. ]
  835. );
  836. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  837. $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
  838. $chartHash = $myCache->getHash($dataSet);
  839. $myCache->writeToCache($chartHash, $pChart);
  840. $imgSysPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  841. $myCache->saveFromCache($chartHash, $imgSysPath);
  842. $imgWebPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  843. if (file_exists($imgSysPath)) {
  844. $result = '<div id="contentArea" style="text-align: center;" >';
  845. $result .= '<img src="'.$imgWebPath.'" >';
  846. $result .= '</div>';
  847. return $result;
  848. }
  849. }
  850. return '';
  851. }
  852. /**
  853. * @return array
  854. */
  855. private function getDataForGraph()
  856. {
  857. return $this->dataForGraph;
  858. }
  859. /**
  860. * @param $item
  861. *
  862. * @return mixed
  863. */
  864. private function build_certificate_min_score($item)
  865. {
  866. return $item->getCertificateMinScore();
  867. }
  868. /**
  869. * @param $item
  870. *
  871. * @return mixed
  872. */
  873. private function build_weight($item)
  874. {
  875. return $item->get_weight();
  876. }
  877. /**
  878. * @param $item
  879. *
  880. * @return mixed
  881. */
  882. private function build_course_code($item)
  883. {
  884. return $item->get_course_code();
  885. }
  886. /**
  887. * @param $item
  888. *
  889. * @return string
  890. */
  891. private function build_id_column($item)
  892. {
  893. switch ($item->get_item_type()) {
  894. // category
  895. case 'C':
  896. return 'CATE'.$item->get_id();
  897. // evaluation
  898. case 'E':
  899. return 'EVAL'.$item->get_id();
  900. // link
  901. case 'L':
  902. return 'LINK'.$item->get_id();
  903. }
  904. }
  905. /**
  906. * @param $item
  907. * @param array $attributes
  908. *
  909. * @return string
  910. */
  911. private function build_type_column($item, $attributes = [])
  912. {
  913. return GradebookUtils::build_type_icon_tag($item->get_icon_name(), $attributes);
  914. }
  915. /**
  916. * Generate name column.
  917. *
  918. * @param GradebookItem $item
  919. * @param string $type simple|detail
  920. *
  921. * @return string
  922. */
  923. private function build_name_link($item, $type = 'detail')
  924. {
  925. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : null;
  926. $categoryId = $item->getCategory()->get_id();
  927. switch ($item->get_item_type()) {
  928. // category
  929. case 'C':
  930. $prms_uri = '?selectcat='.$item->get_id().'&view='.$view;
  931. $isStudentView = api_is_student_view_active();
  932. if (isset($is_student) || $isStudentView) {
  933. $prms_uri = $prms_uri.'&amp;isStudentView=studentview';
  934. }
  935. $cat = new Category();
  936. $show_message = $cat->show_message_resource_delete($item->get_course_code());
  937. return '&nbsp;<a href="'.Category::getUrl().$prms_uri.'">'
  938. .$item->get_name()
  939. .'</a>'
  940. .($item->is_course() ? ' &nbsp;['.$item->get_course_code().']'.$show_message : '');
  941. // evaluation
  942. case 'E':
  943. $cat = new Category();
  944. $course_id = CourseManager::get_course_by_category($categoryId);
  945. $show_message = $cat->show_message_resource_delete($course_id);
  946. // course/platform admin can go to the view_results page
  947. if (api_is_allowed_to_edit() && $show_message === false) {
  948. if ($item->get_type() == 'presence') {
  949. return '&nbsp;'
  950. .'<a href="gradebook_view_result.php?cidReq='.$course_id.'&amp;selecteval='.$item->get_id().'">'
  951. .$item->get_name()
  952. .'</a>';
  953. } else {
  954. $extra = Display::label(get_lang('Evaluation'));
  955. if ($type == 'simple') {
  956. $extra = '';
  957. }
  958. return '&nbsp;'
  959. .'<a href="gradebook_view_result.php?'.api_get_cidreq().'&selecteval='.$item->get_id().'">'
  960. .$item->get_name()
  961. .'</a>&nbsp;'.$extra;
  962. }
  963. } elseif (ScoreDisplay::instance()->is_custom() && $show_message === false) {
  964. // students can go to the statistics page (if custom display enabled)
  965. return '&nbsp;'
  966. .'<a href="gradebook_statistics.php?'.api_get_cidreq().'&selecteval='.$item->get_id().'">'
  967. .$item->get_name()
  968. .'</a>';
  969. } elseif ($show_message === false && !api_is_allowed_to_edit() && !ScoreDisplay::instance()->is_custom()) {
  970. return '&nbsp;'
  971. .'<a href="gradebook_statistics.php?'.api_get_cidreq().'&selecteval='.$item->get_id().'">'
  972. .$item->get_name()
  973. .'</a>';
  974. } else {
  975. return '['.get_lang('Evaluation').']&nbsp;&nbsp;'.$item->get_name().$show_message;
  976. }
  977. // no break because of return
  978. case 'L':
  979. // link
  980. $cat = new Category();
  981. $course_id = CourseManager::get_course_by_category($categoryId);
  982. $show_message = $cat->show_message_resource_delete($course_id);
  983. $url = $item->get_link();
  984. $text = $item->get_name();
  985. if (isset($url) && $show_message === false) {
  986. $text = '&nbsp;<a href="'.$item->get_link().'">'
  987. .$item->get_name()
  988. .'</a>';
  989. }
  990. $extra = Display::label($item->get_type_name(), 'info');
  991. if ($type == 'simple') {
  992. $extra = '';
  993. }
  994. $extra .= $item->getSkillsFromItem();
  995. $text .= "&nbsp;".$extra.$show_message;
  996. $cc = $this->currentcat->get_course_code();
  997. if (empty($cc)) {
  998. $text .= '&nbsp;[<a href="'.api_get_path(REL_COURSE_PATH).$item->get_course_code().'/">'.$item->get_course_code().'</a>]';
  999. }
  1000. return $text;
  1001. }
  1002. }
  1003. /**
  1004. * @param AbstractLink $item
  1005. *
  1006. * @return null|string
  1007. */
  1008. private function build_edit_column($item)
  1009. {
  1010. switch ($item->get_item_type()) {
  1011. // category
  1012. case 'C':
  1013. return GradebookUtils::build_edit_icons_cat($item, $this->currentcat);
  1014. // evaluation
  1015. case 'E':
  1016. return GradebookUtils::build_edit_icons_eval($item, $this->currentcat->get_id());
  1017. // link
  1018. case 'L':
  1019. return GradebookUtils::build_edit_icons_link($item, $this->currentcat->get_id());
  1020. }
  1021. }
  1022. }