gradebooktable.class.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. if ($this->teacherView) {
  412. $weight_total_links += $data[3];
  413. }
  414. // Edit (for admins).
  415. if ($this->teacherView) {
  416. $cat = new Category();
  417. $show_message = $cat->show_message_resource_delete($item->get_course_code());
  418. if ($show_message === false) {
  419. $row[] = $this->build_edit_column($item);
  420. }
  421. } else {
  422. $score = $item->calc_score($this->userId);
  423. if (!empty($score[1])) {
  424. $completeScore = $scoredisplay->display_score($score, SCORE_DIV_PERCENT);
  425. $score = $score[0] / $score[1] * $item->get_weight();
  426. $score = $scoredisplay->display_score([$score, null], SCORE_SIMPLE);
  427. } else {
  428. $categoryScore = null;
  429. }
  430. // Students get the results and certificates columns
  431. $value_data = isset($data[4]) ? $data[4] : null;
  432. $best = isset($data['best']) ? $data['best'] : null;
  433. $average = isset($data['average']) ? $data['average'] : null;
  434. $ranking = isset($data['ranking']) ? $data['ranking'] : null;
  435. $totalResult = [
  436. $data['result_score'][0],
  437. $data['result_score'][1],
  438. ];
  439. if (empty($model)) {
  440. $totalBest = [
  441. $scoredisplay->format_score($totalBest[0] + $data['best_score'][0]),
  442. $scoredisplay->format_score($totalBest[1] + $data['best_score'][1]),
  443. ];
  444. $totalAverage = [
  445. $data['average_score'][0],
  446. $data['average_score'][1],
  447. ];
  448. }
  449. // Student result
  450. if (empty($model)) {
  451. $row[] = $value_data;
  452. } else {
  453. $row[] = ExerciseLib::show_score(
  454. $data['result_score'][0],
  455. $data['result_score'][1]
  456. );
  457. }
  458. $mode = SCORE_AVERAGE;
  459. if ($userExerciseScoreInCategory) {
  460. $mode = SCORE_SIMPLE;
  461. $result = ExerciseLib::convertScoreToPlatformSetting($totalAverage[0], $totalAverage[1]);
  462. $totalAverage[0] = $result['score'];
  463. $totalAverage[1] = $result['weight'];
  464. $result = ExerciseLib::convertScoreToPlatformSetting($totalResult[0], $totalResult[1]);
  465. $totalResult[0] = $result['score'];
  466. $totalResult[1] = $result['weight'];
  467. $result = ExerciseLib::convertScoreToPlatformSetting(
  468. $data['result_score'][0],
  469. $data['result_score'][1]
  470. );
  471. $data['my_result_no_float'][0] = $result['score'];
  472. }
  473. $totalResultAverageValue = strip_tags($scoredisplay->display_score($totalResult, $mode));
  474. $totalAverageValue = strip_tags($scoredisplay->display_score($totalAverage, $mode));
  475. $this->dataForGraph['my_result'][] = floatval($totalResultAverageValue);
  476. $this->dataForGraph['average'][] = floatval($totalAverageValue);
  477. $this->dataForGraph['my_result_no_float'][] = $data['result_score'][0];
  478. if (empty($model)) {
  479. // Ranking
  480. if (in_array(1, $this->loadStats)) {
  481. $row[] = $ranking;
  482. }
  483. // Best
  484. if (in_array(2, $this->loadStats)) {
  485. $row[] = $best;
  486. }
  487. // Average
  488. if (in_array(3, $this->loadStats)) {
  489. $row[] = $average;
  490. }
  491. }
  492. if (get_class($item) === 'Category') {
  493. if ($this->exportToPdf == false) {
  494. $row[] = $this->build_edit_column($item);
  495. }
  496. }
  497. }
  498. // Category added.
  499. $sortable_data[] = $row;
  500. // Loading children
  501. if (get_class($item) === 'Category') {
  502. $parent_id = $item->get_id();
  503. $cats = Category::load(
  504. $parent_id,
  505. null,
  506. null,
  507. null,
  508. null,
  509. null
  510. );
  511. if (isset($cats[0])) {
  512. /** @var Category $subCategory */
  513. $subCategory = $cats[0];
  514. $allcat = $subCategory->get_subcategories($this->userId, $course_code, $session_id);
  515. $alleval = $subCategory->get_evaluations($this->userId);
  516. $alllink = $subCategory->get_links($this->userId);
  517. $sub_cat_info = new GradebookDataGenerator($allcat, $alleval, $alllink);
  518. $sub_cat_info->preLoadDataKey = $this->getPreloadDataKey();
  519. $sub_cat_info->userId = $user_id;
  520. $data_array2 = $sub_cat_info->get_data(
  521. $sorting,
  522. $from,
  523. $this->per_page,
  524. false,
  525. $this->studentList
  526. );
  527. $total_weight = 0;
  528. // Links.
  529. foreach ($data_array2 as $data) {
  530. $row = [];
  531. $item = $data[0];
  532. //if the item is invisible, wrap it in a span with class invisible
  533. $invisibility_span_open = $isAllowedToEdit && $item->is_visible() == '0' ? '<span class="text-muted">' : '';
  534. $invisibility_span_close = $isAllowedToEdit && $item->is_visible() == '0' ? '</span>' : '';
  535. if (isset($item)) {
  536. $main_categories[$parent_id]['children'][$item->get_id()]['name'] = $item->get_name();
  537. $main_categories[$parent_id]['children'][$item->get_id()]['weight'] = $item->get_weight();
  538. }
  539. if ($this->teacherView) {
  540. if ($this->exportToPdf == false) {
  541. $row[] = $this->build_id_column($item);
  542. }
  543. }
  544. // Type
  545. $row[] = $this->build_type_column($item, ['style' => 'padding-left:5px']);
  546. // Name.
  547. $row[] = $invisibility_span_open.'&nbsp;&nbsp;&nbsp; '.
  548. $this->build_name_link($item, $type).$invisibility_span_close;
  549. // Description.
  550. if ($this->exportToPdf == false) {
  551. $row[] = $invisibility_span_open.$data[2].$invisibility_span_close;
  552. }
  553. $weight = $data[3];
  554. $total_weight += $weight;
  555. // Weight
  556. $row[] = $invisibility_span_open.$weight.$invisibility_span_close;
  557. // Admins get an edit column.
  558. if (api_is_allowed_to_edit(null, true) &&
  559. isset($_GET['user_id']) == false &&
  560. (isset($_GET['action']) && $_GET['action'] != 'export_all' || !isset($_GET['action']))
  561. ) {
  562. $cat = new Category();
  563. $show_message = $cat->show_message_resource_delete($item->get_course_code());
  564. if ($show_message === false) {
  565. if ($this->exportToPdf == false) {
  566. $row[] = $this->build_edit_column($item);
  567. }
  568. }
  569. } else {
  570. // Students get the results and certificates columns
  571. $eval_n_links = array_merge($alleval, $alllink);
  572. if (count($eval_n_links) > 0) {
  573. $value_data = isset($data[4]) ? $data[4] : null;
  574. if (!is_null($value_data)) {
  575. // Result
  576. $row[] = $value_data;
  577. $best = isset($data['best']) ? $data['best'] : null;
  578. $average = isset($data['average']) ? $data['average'] : null;
  579. $ranking = isset($data['ranking']) ? $data['ranking'] : null;
  580. if (empty($model)) {
  581. if (in_array(1, $this->loadStats)) {
  582. // Ranking
  583. $row[] = $ranking;
  584. }
  585. if (in_array(2, $this->loadStats)) {
  586. // Best
  587. $row[] = $best;
  588. }
  589. // Average
  590. if (in_array(3, $this->loadStats)) {
  591. $row[] = $average;
  592. }
  593. }
  594. }
  595. }
  596. if (!empty($cats)) {
  597. if ($this->exportToPdf == false) {
  598. $row[] = null;
  599. }
  600. }
  601. }
  602. if ($this->exportToPdf == false) {
  603. $row['child_of'] = $parent_id;
  604. }
  605. $sortable_data[] = $row;
  606. }
  607. // "Warning row"
  608. if (!empty($data_array)) {
  609. if ($this->teacherView) {
  610. // Compare the category weight to the sum of all weights inside the category
  611. if (intval($total_weight) == $category_weight) {
  612. $label = null;
  613. $total = GradebookUtils::score_badges(
  614. [
  615. $total_weight.' / '.$category_weight,
  616. '100',
  617. ]
  618. );
  619. } else {
  620. $label = Display::return_icon(
  621. 'warning.png',
  622. sprintf(get_lang('TotalWeightMustBeX'), $category_weight)
  623. );
  624. $total = Display::badge($total_weight.' / '.$category_weight, 'warning');
  625. }
  626. $row = [
  627. null,
  628. null,
  629. "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<h5>".get_lang('SubTotal').'</h5>',
  630. null,
  631. $total.' '.$label,
  632. 'child_of' => $parent_id,
  633. ];
  634. $sortable_data[] = $row;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. } //end looping categories
  641. $main_weight = 0;
  642. if (count($main_cat) > 1) {
  643. /** @var Category $myCat */
  644. foreach ($main_cat as $myCat) {
  645. $myParentId = $myCat->get_parent_id();
  646. if ($myParentId == 0) {
  647. $main_weight = (int) $myCat->get_weight();
  648. }
  649. }
  650. }
  651. if ($this->teacherView) {
  652. // Total for teacher.
  653. if (count($main_cat) > 1) {
  654. if (intval($total_categories_weight) == $main_weight) {
  655. $total = GradebookUtils::score_badges(
  656. [
  657. $total_categories_weight.' / '.$main_weight,
  658. '100',
  659. ]
  660. );
  661. } else {
  662. $total = Display::badge($total_categories_weight.' / '.$main_weight, 'warning');
  663. }
  664. $row = [
  665. null,
  666. null,
  667. '<strong>'.get_lang('Total').'</strong>',
  668. null,
  669. $total,
  670. ];
  671. $sortable_data[] = $row;
  672. }
  673. } else {
  674. // Total for student.
  675. if (count($main_cat) > 1) {
  676. $main_weight = (int) $main_cat[0]->get_weight();
  677. $global = null;
  678. $average = null;
  679. $myTotal = 0;
  680. foreach ($this->dataForGraph['my_result_no_float'] as $result) {
  681. $myTotal += $result;
  682. }
  683. $totalResult[0] = $myTotal;
  684. // Overwrite main weight
  685. $totalResult[1] = $main_weight;
  686. if (!empty($model)) {
  687. $totalResult = ExerciseLib::show_score($totalResult[0], $totalResult[1], false);
  688. } else {
  689. $totalResult = $scoredisplay->display_score(
  690. $totalResult,
  691. SCORE_DIV
  692. );
  693. }
  694. $row = [
  695. null,
  696. '<strong>'.get_lang('Total').'</strong>',
  697. ];
  698. if (!$this->exportToPdf) {
  699. $row[] = null;
  700. }
  701. $row[] = $main_weight;
  702. $row[] = $totalResult;
  703. $categoryId = $main_cat[0]->get_id();
  704. if (empty($model)) {
  705. if (in_array(1, $this->loadStats)) {
  706. if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['ranking'])) {
  707. $totalRanking = $defaultData[$categoryId]['ranking'];
  708. $invalidateRanking = $defaultData[$categoryId]['ranking_invalidate'];
  709. $average = 0;
  710. foreach ($totalRanking as $ranking) {
  711. $average += $ranking;
  712. }
  713. } else {
  714. $totalRanking = [];
  715. $invalidateRanking = true;
  716. $average = 0;
  717. $main_cat[0]->setStudentList($this->studentList);
  718. foreach ($this->studentList as $student) {
  719. $score = $main_cat[0]->calc_score(
  720. $student['user_id'],
  721. null,
  722. $course_code,
  723. $session_id
  724. );
  725. if (!empty($score[0])) {
  726. $invalidateRanking = false;
  727. }
  728. $totalRanking[$student['user_id']] = $score[0];
  729. $average += $score[0];
  730. }
  731. $defaultData[$categoryId]['ranking'] = $totalRanking;
  732. $defaultData[$categoryId]['ranking_invalidate'] = $invalidateRanking;
  733. Session::write($this->getPreloadDataKey(), $defaultData);
  734. }
  735. $totalRanking = AbstractLink::getCurrentUserRanking($user_id, $totalRanking);
  736. $totalRanking = $scoredisplay->display_score(
  737. $totalRanking,
  738. SCORE_DIV,
  739. SCORE_BOTH,
  740. true,
  741. true
  742. );
  743. if ($invalidateRanking) {
  744. $totalRanking = null;
  745. }
  746. $row[] = $totalRanking;
  747. }
  748. if (in_array(2, $this->loadStats)) {
  749. if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['best'])) {
  750. $totalBest = $defaultData[$categoryId]['best'];
  751. } else {
  752. // Overwrite main weight
  753. $totalBest[1] = $main_weight;
  754. $defaultData[$categoryId]['best'] = $totalBest;
  755. }
  756. $totalBest = $scoredisplay->display_score(
  757. $totalBest,
  758. SCORE_DIV,
  759. SCORE_BOTH,
  760. true
  761. );
  762. $row[] = $totalBest;
  763. }
  764. if (in_array(3, $this->loadStats)) {
  765. if (isset($defaultData[$categoryId]) && isset($defaultData[$categoryId]['average'])) {
  766. $totalAverage = $defaultData[$categoryId]['average'];
  767. } else {
  768. // Overwrite main weight
  769. $totalAverage[0] = $average / count($this->studentList);
  770. $totalAverage[1] = $main_weight;
  771. $defaultData[$categoryId]['average'] = $totalBest;
  772. }
  773. $totalAverage = $scoredisplay->display_score(
  774. $totalAverage,
  775. SCORE_DIV,
  776. SCORE_BOTH,
  777. true
  778. );
  779. $row[] = $totalAverage;
  780. }
  781. }
  782. if (!empty($row)) {
  783. $sortable_data[] = $row;
  784. }
  785. }
  786. }
  787. Session::write('default_data', $defaultData);
  788. // Warning messages
  789. $view = isset($_GET['view']) ? $_GET['view'] : null;
  790. if ($this->teacherView) {
  791. if (isset($_GET['selectcat']) &&
  792. $_GET['selectcat'] > 0 &&
  793. $view !== 'presence'
  794. ) {
  795. $id_cat = (int) $_GET['selectcat'];
  796. $category = Category::load($id_cat);
  797. $weight_category = (int) $this->build_weight($category[0]);
  798. $course_code = $this->build_course_code($category[0]);
  799. $weight_total_links = round($weight_total_links);
  800. if ($weight_total_links > $weight_category ||
  801. $weight_total_links < $weight_category ||
  802. $weight_total_links > $weight_category
  803. ) {
  804. $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $weight_category);
  805. $modify_icons =
  806. '<a href="gradebook_edit_cat.php?editcat='.$id_cat.'&cidReq='.$course_code.'&id_session='.api_get_session_id().'">'.
  807. Display::return_icon('edit.png', $warning_message, [], ICON_SIZE_SMALL).'</a>';
  808. $warning_message .= $modify_icons;
  809. echo Display::return_message($warning_message, 'warning', false);
  810. }
  811. $content_html = DocumentManager::replace_user_info_into_html(
  812. api_get_user_id(),
  813. $course_code,
  814. api_get_session_id()
  815. );
  816. if (!empty($content_html)) {
  817. $new_content = explode('</head>', $content_html['content']);
  818. }
  819. if (empty($new_content[0])) {
  820. // Set default certificate
  821. $courseData = api_get_course_info($course_code);
  822. DocumentManager::generateDefaultCertificate($courseData);
  823. }
  824. }
  825. if (empty($_GET['selectcat'])) {
  826. $categories = Category::load();
  827. $weight_categories = $certificate_min_scores = $course_codes = [];
  828. foreach ($categories as $category) {
  829. $course_code_category = $this->build_course_code($category);
  830. if (!empty($course_code)) {
  831. if ($course_code_category == $course_code) {
  832. $weight_categories[] = intval($this->build_weight($category));
  833. $certificate_min_scores[] = intval($this->build_certificate_min_score($category));
  834. $course_codes[] = $course_code;
  835. break;
  836. }
  837. } else {
  838. $weight_categories[] = intval($this->build_weight($category));
  839. $certificate_min_scores[] = intval($this->build_certificate_min_score($category));
  840. $course_codes[] = $course_code_category;
  841. }
  842. }
  843. if (is_array($weight_categories) &&
  844. is_array($certificate_min_scores) &&
  845. is_array($course_codes)
  846. ) {
  847. $warning_message = '';
  848. for ($x = 0; $x < count($weight_categories); $x++) {
  849. $weight_category = intval($weight_categories[$x]);
  850. $certificate_min_score = intval($certificate_min_scores[$x]);
  851. $course_code = $course_codes[$x];
  852. if (empty($certificate_min_score) ||
  853. ($certificate_min_score > $weight_category)
  854. ) {
  855. $warning_message .= $course_code.
  856. '&nbsp;-&nbsp;'.get_lang('CertificateMinimunScoreIsRequiredAndMustNotBeMoreThan').
  857. '&nbsp;'.$weight_category.'<br />';
  858. }
  859. }
  860. if (!empty($warning_message)) {
  861. echo Display::return_message($warning_message, 'warning', false);
  862. }
  863. }
  864. }
  865. }
  866. return $sortable_data;
  867. }
  868. /**
  869. * @return string
  870. */
  871. public function getGraph()
  872. {
  873. $data = $this->getDataForGraph();
  874. if (!empty($data) &&
  875. isset($data['categories']) &&
  876. isset($data['my_result']) &&
  877. isset($data['average'])
  878. ) {
  879. $dataSet = new pData();
  880. $dataSet->addPoints($data['my_result'], get_lang('Me'));
  881. // In order to generate random values
  882. // $data['average'] = array(rand(0,50), rand(0,50));
  883. $dataSet->addPoints($data['average'], get_lang('Average'));
  884. $dataSet->addPoints($data['categories'], 'categories');
  885. $dataSet->setAbscissa('categories');
  886. $xSize = 600;
  887. $ySize = 400;
  888. $pChart = new pImage($xSize, $ySize, $dataSet);
  889. /* Turn of Antialiasing */
  890. $pChart->Antialias = false;
  891. /* Add a border to the picture */
  892. $pChart->drawRectangle(
  893. 0,
  894. 0,
  895. $xSize - 1,
  896. $ySize - 1,
  897. ["R" => 0, "G" => 0, "B" => 0]
  898. );
  899. $pChart->drawText(
  900. 80,
  901. 16,
  902. get_lang('Results'),
  903. ["FontSize" => 11, "Align" => TEXT_ALIGN_BOTTOMMIDDLE]
  904. );
  905. $pChart->setGraphArea(50, 30, $xSize - 50, $ySize - 70);
  906. $pChart->setFontProperties(
  907. [
  908. 'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  909. 'FontSize' => 10,
  910. ]
  911. );
  912. /* Draw the scale */
  913. $scaleSettings = [
  914. "XMargin" => AUTO,
  915. "YMargin" => 10,
  916. "Floating" => true,
  917. "GridR" => 200,
  918. "GridG" => 200,
  919. "GridB" => 200,
  920. "DrawSubTicks" => true,
  921. "CycleBackground" => true,
  922. 'LabelRotation' => 10,
  923. ];
  924. $pChart->drawScale($scaleSettings);
  925. /* Draw the line chart */
  926. $pChart->drawLineChart();
  927. $pChart->drawPlotChart(
  928. [
  929. "DisplayValues" => true,
  930. "PlotBorder" => true,
  931. "BorderSize" => 2,
  932. "Surrounding" => -60,
  933. "BorderAlpha" => 80,
  934. ]
  935. );
  936. /* Write the chart legend */
  937. $pChart->drawLegend(
  938. $xSize - 180,
  939. 9,
  940. [
  941. "Style" => LEGEND_NOBORDER,
  942. "Mode" => LEGEND_HORIZONTAL,
  943. "FontR" => 0,
  944. "FontG" => 0,
  945. "FontB" => 0,
  946. ]
  947. );
  948. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  949. $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
  950. $chartHash = $myCache->getHash($dataSet);
  951. $myCache->writeToCache($chartHash, $pChart);
  952. $imgSysPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  953. $myCache->saveFromCache($chartHash, $imgSysPath);
  954. $imgWebPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  955. if (file_exists($imgSysPath)) {
  956. $result = '<br /><div id="contentArea" style="text-align: center;" >';
  957. $result .= '<img src="'.$imgWebPath.'" >';
  958. $result .= '</div>';
  959. return $result;
  960. }
  961. }
  962. return '';
  963. }
  964. /**
  965. * @return array
  966. */
  967. private function getDataForGraph()
  968. {
  969. return $this->dataForGraph;
  970. }
  971. /**
  972. * @param $item
  973. *
  974. * @return mixed
  975. */
  976. private function build_certificate_min_score($item)
  977. {
  978. return $item->getCertificateMinScore();
  979. }
  980. /**
  981. * @param $item
  982. *
  983. * @return mixed
  984. */
  985. private function build_weight($item)
  986. {
  987. return $item->get_weight();
  988. }
  989. /**
  990. * @param $item
  991. *
  992. * @return mixed
  993. */
  994. private function build_course_code($item)
  995. {
  996. return $item->get_course_code();
  997. }
  998. /**
  999. * @param $item
  1000. *
  1001. * @return string
  1002. */
  1003. private function build_id_column($item)
  1004. {
  1005. switch ($item->get_item_type()) {
  1006. // category
  1007. case 'C':
  1008. return 'CATE'.$item->get_id();
  1009. // evaluation
  1010. case 'E':
  1011. return 'EVAL'.$item->get_id();
  1012. // link
  1013. case 'L':
  1014. return 'LINK'.$item->get_id();
  1015. }
  1016. }
  1017. /**
  1018. * @param $item
  1019. * @param array $attributes
  1020. *
  1021. * @return string
  1022. */
  1023. private function build_type_column($item, $attributes = [])
  1024. {
  1025. return GradebookUtils::build_type_icon_tag($item->get_icon_name(), $attributes);
  1026. }
  1027. /**
  1028. * Generate name column.
  1029. *
  1030. * @param GradebookItem $item
  1031. * @param string $type simple|detail
  1032. *
  1033. * @return string
  1034. */
  1035. private function build_name_link($item, $type = 'detail')
  1036. {
  1037. $view = isset($_GET['view']) ? Security::remove_XSS($_GET['view']) : null;
  1038. $categoryId = $item->getCategory()->get_id();
  1039. switch ($item->get_item_type()) {
  1040. // category
  1041. case 'C':
  1042. $prms_uri = '?selectcat='.$item->get_id().'&view='.$view;
  1043. $isStudentView = api_is_student_view_active();
  1044. if (isset($is_student) || $isStudentView) {
  1045. $prms_uri = $prms_uri.'&amp;isStudentView=studentview';
  1046. }
  1047. $cat = new Category();
  1048. $show_message = $cat->show_message_resource_delete($item->get_course_code());
  1049. return '&nbsp;<a href="'.Category::getUrl().$prms_uri.'">'
  1050. .$item->get_name()
  1051. .'</a>'
  1052. .($item->is_course() ? ' &nbsp;['.$item->get_course_code().']'.$show_message : '');
  1053. // evaluation
  1054. case 'E':
  1055. $cat = new Category();
  1056. $course_id = CourseManager::get_course_by_category($categoryId);
  1057. $show_message = $cat->show_message_resource_delete($course_id);
  1058. // course/platform admin can go to the view_results page
  1059. if (api_is_allowed_to_edit() && $show_message === false) {
  1060. if ($item->get_type() == 'presence') {
  1061. return '&nbsp;'
  1062. .'<a href="gradebook_view_result.php?cidReq='.$course_id.'&amp;selecteval='.$item->get_id().'">'
  1063. .$item->get_name()
  1064. .'</a>';
  1065. } else {
  1066. $extra = Display::label(get_lang('Evaluation'));
  1067. if ($type == 'simple') {
  1068. $extra = '';
  1069. }
  1070. return '&nbsp;'
  1071. .'<a href="gradebook_view_result.php?'.api_get_cidreq().'&selecteval='.$item->get_id().'">'
  1072. .$item->get_name()
  1073. .'</a>&nbsp;'.$extra;
  1074. }
  1075. } elseif (ScoreDisplay::instance()->is_custom() && $show_message === false) {
  1076. // students can go to the statistics page (if custom display enabled)
  1077. return '&nbsp;'
  1078. .'<a href="gradebook_statistics.php?'.api_get_cidreq().'&selecteval='.$item->get_id().'">'
  1079. .$item->get_name()
  1080. .'</a>';
  1081. } elseif ($show_message === false && !api_is_allowed_to_edit() && !ScoreDisplay::instance()->is_custom()) {
  1082. return '&nbsp;'
  1083. .'<a href="gradebook_statistics.php?'.api_get_cidreq().'&selecteval='.$item->get_id().'">'
  1084. .$item->get_name()
  1085. .'</a>';
  1086. } else {
  1087. return '['.get_lang('Evaluation').']&nbsp;&nbsp;'.$item->get_name().$show_message;
  1088. }
  1089. // no break because of return
  1090. case 'L':
  1091. // link
  1092. $cat = new Category();
  1093. $course_id = CourseManager::get_course_by_category($categoryId);
  1094. $show_message = $cat->show_message_resource_delete($course_id);
  1095. $url = $item->get_link();
  1096. $text = $item->get_name();
  1097. if (isset($url) && $show_message === false) {
  1098. $text = '&nbsp;<a href="'.$item->get_link().'">'
  1099. .$item->get_name()
  1100. .'</a>';
  1101. }
  1102. $extra = Display::label($item->get_type_name(), 'info');
  1103. if ($type == 'simple') {
  1104. $extra = '';
  1105. }
  1106. $extra .= $item->getSkillsFromItem();
  1107. $text .= "&nbsp;".$extra.$show_message;
  1108. $cc = $this->currentcat->get_course_code();
  1109. if (empty($cc)) {
  1110. $text .= '&nbsp;[<a href="'.api_get_path(REL_COURSE_PATH).$item->get_course_code().'/">'.$item->get_course_code().'</a>]';
  1111. }
  1112. return $text;
  1113. }
  1114. }
  1115. /**
  1116. * @param AbstractLink $item
  1117. *
  1118. * @return string|null
  1119. */
  1120. private function build_edit_column($item)
  1121. {
  1122. switch ($item->get_item_type()) {
  1123. // category
  1124. case 'C':
  1125. return GradebookUtils::build_edit_icons_cat($item, $this->currentcat);
  1126. // evaluation
  1127. case 'E':
  1128. return GradebookUtils::build_edit_icons_eval($item, $this->currentcat->get_id());
  1129. // link
  1130. case 'L':
  1131. return GradebookUtils::build_edit_icons_link($item, $this->currentcat->get_id());
  1132. }
  1133. }
  1134. }