gradebooktable.class.php 49 KB

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