index.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook controller.
  5. *
  6. * @package chamilo.gradebook
  7. */
  8. // $cidReset : This is the main difference with gradebook.php, here we say,
  9. // basically, that we are inside a course, and many things depend from that
  10. //$cidReset = false;
  11. $_in_course = true;
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $current_course_tool = TOOL_GRADEBOOK;
  14. api_block_anonymous_users();
  15. api_protect_course_script(true);
  16. $course_code = api_get_course_id();
  17. $stud_id = api_get_user_id();
  18. $session_id = api_get_session_id();
  19. $course_id = api_get_course_int_id();
  20. $courseInfo = api_get_course_info();
  21. $action = isset($_GET['action']) ? $_GET['action'] : null;
  22. $itemId = isset($_GET['itemId']) ? $_GET['itemId'] : 0;
  23. switch ($action) {
  24. case 'generate_eval_stats':
  25. if (!empty($itemId)) {
  26. Evaluation::generateStats($itemId);
  27. Display::addFlash(Display::return_message(get_lang('Updated')));
  28. }
  29. header('Location: '.api_get_self().'?'.api_get_cidreq());
  30. exit;
  31. break;
  32. case 'generate_link_stats':
  33. if (!empty($itemId)) {
  34. $link = LinkFactory::create(LINK_EXERCISE);
  35. $links = $link::load($itemId);
  36. /** @var ExerciseLink $link */
  37. foreach ($links as $link) {
  38. $exercise = new Exercise(api_get_course_int_id());
  39. $exercise->read($link->get_ref_id());
  40. $exercise->generateStats($link->get_ref_id(), api_get_course_info(), api_get_session_id());
  41. }
  42. Display::addFlash(Display::return_message(get_lang('Updated')));
  43. }
  44. header('Location: '.api_get_self().'?'.api_get_cidreq());
  45. exit;
  46. break;
  47. case 'lock':
  48. $category_to_lock = Category::load($_GET['category_id']);
  49. $category_to_lock[0]->lockAllItems(1);
  50. $confirmation_message = get_lang('GradebookLockedAlert');
  51. break;
  52. case 'unlock':
  53. if (api_is_platform_admin()) {
  54. $category_to_lock = Category::load($_GET['category_id']);
  55. $category_to_lock[0]->lockAllItems(0);
  56. $confirmation_message = get_lang('EvaluationHasBeenUnLocked');
  57. }
  58. break;
  59. case 'export_table':
  60. $hidePdfReport = api_get_configuration_value('gradebook_hide_pdf_report_button');
  61. if ($hidePdfReport) {
  62. api_not_allowed(true);
  63. }
  64. if (isset($_GET['category_id'])) {
  65. $cats = Category::load($_GET['category_id'], null, null, null, null, null, false);
  66. GradebookUtils::generateTable($courseInfo, api_get_user_id(), $cats);
  67. exit;
  68. }
  69. break;
  70. }
  71. ob_start();
  72. // Make sure the destination for scripts is index.php instead of gradebook.php
  73. Category::setUrl('index.php');
  74. $this_section = SECTION_COURSES;
  75. $htmlHeadXtra[] = '<script>
  76. var show_icon = "'.Display::returnIconPath('view_more_stats.gif').'";
  77. var hide_icon = "'.Display::returnIconPath('view_less_stats.gif').'";
  78. function confirmation() {
  79. if (confirm("'.get_lang('DeleteAll').'?")) {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. $(function() {
  86. $("body").on("click", ".view_children", function() {
  87. var id = $(this).attr("data-cat-id");
  88. $(".hidden_"+id).removeClass("hidden");
  89. $(this).removeClass("view_children");
  90. $(this).find("img").attr("src", hide_icon);
  91. $(this).attr("class", "hide_children");
  92. });
  93. $("body").on("click", ".hide_children", function(event) {
  94. var id = $(this).attr("data-cat-id");
  95. $(".hidden_"+id).addClass("hidden");
  96. $(this).removeClass("hide_children");
  97. $(this).addClass("view_children");
  98. $(this).find("img").attr("src", show_icon);
  99. });
  100. for (i=0;i<$(".actions").length;i++) {
  101. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null || $(".actions:eq("+i+")").html().split("<TBODY></TBODY>").length==2) {
  102. $(".actions:eq("+i+")").hide();
  103. }
  104. }
  105. });
  106. </script>';
  107. $list_actions = [];
  108. $list_values = [];
  109. if (isset($_GET['movecat'])) {
  110. $list_actions[] = 'movecat';
  111. $list_values[] = $_GET['movecat'];
  112. }
  113. if (isset($_GET['moveeval'])) {
  114. $list_actions[] = 'moveeval';
  115. $list_values[] = $_GET['moveeval'];
  116. }
  117. if (isset($_GET['movelink'])) {
  118. $list_actions[] = 'movelink';
  119. $list_values[] = $_GET['movelink'];
  120. }
  121. if (isset($_GET['visiblecat'])) {
  122. $list_actions[] = 'visiblecat';
  123. $list_values[] = $_GET['visiblecat'];
  124. }
  125. if (isset($_GET['deletecat'])) {
  126. $list_actions[] = 'deletecat';
  127. $list_values[] = $_GET['deletecat'];
  128. }
  129. if (isset($_GET['visibleeval'])) {
  130. $list_actions[] = 'visibleeval';
  131. $list_values[] = $_GET['visibleeval'];
  132. }
  133. if (isset($_GET['lockedeval'])) {
  134. $list_actions[] = 'lockedeval';
  135. $list_values[] = $_GET['lockedeval'];
  136. }
  137. if (isset($_GET['deleteeval'])) {
  138. $list_actions[] = 'deleteeval';
  139. $list_values[] = $_GET['deleteeval'];
  140. }
  141. if (isset($_GET['visiblelink'])) {
  142. $list_actions[] = 'visiblelink';
  143. $list_values[] = $_GET['visiblelink'];
  144. }
  145. if (isset($_GET['deletelink'])) {
  146. $list_actions[] = 'deletelink';
  147. $list_values[] = $_GET['deletelink'];
  148. }
  149. if (isset($_GET['action'])) {
  150. $list_actions[] = $_GET['action'];
  151. }
  152. $my_actions = implode(';', $list_actions);
  153. $my_actions_values = implode(';', $list_values);
  154. $logInfo = [
  155. 'tool' => TOOL_GRADEBOOK,
  156. 'tool_id' => 0,
  157. 'tool_id_detail' => 0,
  158. 'action' => $my_actions,
  159. 'action_details' => $my_actions_values,
  160. ];
  161. Event::registerLog($logInfo);
  162. $tbl_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  163. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  164. $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  165. $filter_confirm_msg = true;
  166. $filter_warning_msg = true;
  167. $courseInfo = api_get_course_info();
  168. $cats = Category::load(
  169. null,
  170. null,
  171. $course_code,
  172. null,
  173. null,
  174. $session_id,
  175. 'ORDER By id'
  176. );
  177. $first_time = null;
  178. if (empty($cats)) {
  179. // first time
  180. $cats = Category::load(
  181. 0,
  182. null,
  183. $course_code,
  184. null,
  185. null,
  186. $session_id,
  187. 'ORDER By id'
  188. );
  189. $first_time = 1;
  190. }
  191. $selectCat = (int) $cats[0]->get_id();
  192. $_GET['selectcat'] = $selectCat;
  193. $isStudentView = api_is_student_view_active();
  194. if ($selectCat > 0 && $isStudentView) {
  195. $interbreadcrumb[] = [
  196. 'url' => 'index.php?selectcat=0&isStudentView=true',
  197. 'name' => get_lang('ToolGradebook'),
  198. ];
  199. }
  200. // ACTIONS
  201. //this is called when there is no data for the course admin
  202. if (isset($_GET['createallcategories'])) {
  203. GradebookUtils::block_students();
  204. $coursecat = Category::get_not_created_course_categories($stud_id);
  205. if (!count($coursecat) == 0) {
  206. foreach ($coursecat as $row) {
  207. $cat = new Category();
  208. $cat->set_name($row[1]);
  209. $cat->set_course_code($row[0]);
  210. $cat->set_description(null);
  211. $cat->set_user_id($stud_id);
  212. $cat->set_parent_id(0);
  213. $cat->set_weight(0);
  214. $cat->set_visible(0);
  215. $cat->add();
  216. unset($cat);
  217. }
  218. }
  219. header('Location: '.Category::getUrl().'addallcat=&selectcat=0');
  220. exit;
  221. }
  222. //show logs evaluations
  223. if (isset($_GET['visiblelog'])) {
  224. header('Location: '.api_get_self().'/gradebook_showlog_eval.php');
  225. exit;
  226. }
  227. //move a category
  228. if (isset($_GET['movecat'])) {
  229. GradebookUtils::block_students();
  230. $cats = Category::load($_GET['movecat']);
  231. if (!isset($_GET['targetcat'])) {
  232. $move_form = new CatForm(
  233. CatForm::TYPE_MOVE,
  234. $cats[0],
  235. 'move_cat_form',
  236. null,
  237. api_get_self().'?movecat='.intval($_GET['movecat']).'&selectcat='.$selectCat
  238. );
  239. if ($move_form->validate()) {
  240. header('Location: '.api_get_self().'?selectcat='.$selectCat
  241. .'&movecat='.intval($_GET['movecat'])
  242. .'&targetcat='.$move_form->exportValue('move_cat'));
  243. exit;
  244. }
  245. } else {
  246. $targetcat = Category::load($_GET['targetcat']);
  247. $course_to_crsind = ($cats[0]->get_course_code() != null && $targetcat[0]->get_course_code() == null);
  248. if (!($course_to_crsind && !isset($_GET['confirm']))) {
  249. $cats[0]->move_to_cat($targetcat[0]);
  250. header('Location: '.api_get_self().'?categorymoved=&selectcat='.$selectCat);
  251. exit;
  252. }
  253. unset($targetcat);
  254. }
  255. unset($cats);
  256. }
  257. //move an evaluation
  258. if (isset($_GET['moveeval'])) {
  259. GradebookUtils::block_students();
  260. $evals = Evaluation::load($_GET['moveeval']);
  261. if (!isset($_GET['targetcat'])) {
  262. $move_form = new EvalForm(
  263. EvalForm::TYPE_MOVE,
  264. $evals[0],
  265. null,
  266. 'move_eval_form',
  267. null,
  268. api_get_self().'?moveeval='.Security::remove_XSS($_GET['moveeval']).'&selectcat='.$selectCat
  269. );
  270. if ($move_form->validate()) {
  271. header('Location: '.api_get_self().'?selectcat='.$selectCat
  272. .'&moveeval='.Security::remove_XSS($_GET['moveeval'])
  273. .'&targetcat='.$move_form->exportValue('move_cat'));
  274. exit;
  275. }
  276. } else {
  277. $targetcat = Category::load($_GET['targetcat']);
  278. $course_to_crsind = $evals[0]->get_course_code() != null && $targetcat[0]->get_course_code() == null;
  279. if (!($course_to_crsind && !isset($_GET['confirm']))) {
  280. $evals[0]->move_to_cat($targetcat[0]);
  281. header('Location: '.api_get_self().'?evaluationmoved=&selectcat='.$selectCat);
  282. exit;
  283. }
  284. unset($targetcat);
  285. }
  286. unset($evals);
  287. }
  288. //move a link
  289. if (isset($_GET['movelink'])) {
  290. $moveLink = (int) $_GET['movelink'];
  291. GradebookUtils::block_students();
  292. $link = LinkFactory::load($moveLink);
  293. $move_form = new LinkForm(
  294. LinkForm::TYPE_MOVE,
  295. null,
  296. $link[0],
  297. 'move_link_form',
  298. null,
  299. api_get_self().'?movelink='.$moveLink.'&selectcat='.$selectCat.'&'.api_get_cidreq()
  300. );
  301. if ($move_form->validate()) {
  302. $targetcat = Category::load($move_form->exportValue('move_cat'));
  303. $link[0]->move_to_cat($targetcat[0]);
  304. header('Location: '.api_get_self().'?linkmoved=&selectcat='.$selectCat.'&'.api_get_cidreq());
  305. exit;
  306. }
  307. }
  308. // Parameters for categories.
  309. if (isset($_GET['visiblecat'])) {
  310. GradebookUtils::block_students();
  311. $visibility_command = 0;
  312. if (isset($_GET['set_visible'])) {
  313. $visibility_command = 1;
  314. }
  315. $cats = Category::load($_GET['visiblecat']);
  316. $cats[0]->set_visible($visibility_command);
  317. $cats[0]->save();
  318. $cats[0]->apply_visibility_to_children();
  319. unset($cats);
  320. if ($visibility_command) {
  321. $confirmation_message = get_lang('ViMod');
  322. $filter_confirm_msg = false;
  323. } else {
  324. $confirmation_message = get_lang('InViMod');
  325. $filter_confirm_msg = false;
  326. }
  327. }
  328. if (isset($_GET['deletecat'])) {
  329. GradebookUtils::block_students();
  330. $cats = Category::load($_GET['deletecat']);
  331. if (isset($cats[0])) {
  332. //delete all categories,subcategories and results
  333. if ($cats[0] != null) {
  334. if ($cats[0]->get_id() != 0) {
  335. // better don't try to delete the root...
  336. $cats[0]->delete_all();
  337. }
  338. }
  339. }
  340. $confirmation_message = get_lang('CategoryDeleted');
  341. $filter_confirm_msg = false;
  342. }
  343. // Parameters for evaluations.
  344. if (isset($_GET['visibleeval'])) {
  345. GradebookUtils::block_students();
  346. $visibility_command = 0;
  347. if (isset($_GET['set_visible'])) {
  348. $visibility_command = 1;
  349. }
  350. $eval = Evaluation::load($_GET['visibleeval']);
  351. $eval[0]->set_visible($visibility_command);
  352. $eval[0]->save();
  353. unset($eval);
  354. if ($visibility_command) {
  355. $confirmation_message = get_lang('ViMod');
  356. $filter_confirm_msg = false;
  357. } else {
  358. $confirmation_message = get_lang('InViMod');
  359. $filter_confirm_msg = false;
  360. }
  361. }
  362. // Parameters for evaluations.
  363. if (isset($_GET['lockedeval'])) {
  364. GradebookUtils::block_students();
  365. $locked = (int) $_GET['lockedeval'];
  366. if (isset($_GET['typelocked']) && api_is_platform_admin()) {
  367. $type_locked = 0;
  368. $confirmation_message = get_lang('EvaluationHasBeenUnLocked');
  369. } else {
  370. $type_locked = 1;
  371. $confirmation_message = get_lang('EvaluationHasBeenLocked');
  372. }
  373. $eval = Evaluation::load($locked);
  374. if ($eval[0] != null) {
  375. $eval[0]->lock($type_locked);
  376. }
  377. $filter_confirm_msg = false;
  378. }
  379. if (isset($_GET['deleteeval'])) {
  380. GradebookUtils::block_students();
  381. $eval = Evaluation::load($_GET['deleteeval']);
  382. if ($eval[0] != null) {
  383. $eval[0]->delete_with_results();
  384. }
  385. $confirmation_message = get_lang('GradebookEvaluationDeleted');
  386. $filter_confirm_msg = false;
  387. }
  388. // Parameters for links.
  389. if (isset($_GET['visiblelink'])) {
  390. GradebookUtils::block_students();
  391. $visibility_command = 0;
  392. if (isset($_GET['set_visible'])) {
  393. $visibility_command = 1;
  394. }
  395. $link = LinkFactory::load($_GET['visiblelink']);
  396. if (isset($link) && isset($link[0])) {
  397. $link[0]->set_visible($visibility_command);
  398. $link[0]->save();
  399. }
  400. unset($link);
  401. if ($visibility_command) {
  402. $confirmation_message = get_lang('ViMod');
  403. $filter_confirm_msg = false;
  404. } else {
  405. $confirmation_message = get_lang('InViMod');
  406. $filter_confirm_msg = false;
  407. }
  408. }
  409. if (isset($_GET['deletelink'])) {
  410. GradebookUtils::block_students();
  411. $get_delete_link = (int) $_GET['deletelink'];
  412. //fixing #5229
  413. if (!empty($get_delete_link)) {
  414. $link = LinkFactory::load($get_delete_link);
  415. if ($link[0] != null) {
  416. // Clean forum qualify
  417. $sql = 'UPDATE '.$tbl_forum_thread.' SET
  418. thread_qualify_max = 0,
  419. thread_weight = 0,
  420. thread_title_qualify = ""
  421. WHERE c_id = '.$course_id.' AND thread_id = (
  422. SELECT ref_id FROM '.$tbl_grade_links.'
  423. WHERE id='.$get_delete_link.' AND type = '.LINK_FORUM_THREAD.'
  424. )';
  425. Database::query($sql);
  426. // clean attendance
  427. $sql = 'UPDATE '.$tbl_attendance.' SET
  428. attendance_weight = 0,
  429. attendance_qualify_title = ""
  430. WHERE c_id = '.$course_id.' AND id = (
  431. SELECT ref_id FROM '.$tbl_grade_links.'
  432. WHERE id='.$get_delete_link.' AND type = '.LINK_ATTENDANCE.'
  433. )';
  434. Database::query($sql);
  435. $link[0]->delete();
  436. }
  437. unset($link);
  438. $confirmation_message = get_lang('LinkDeleted');
  439. $filter_confirm_msg = false;
  440. }
  441. }
  442. if (!empty($course_to_crsind) && !isset($_GET['confirm'])) {
  443. GradebookUtils::block_students();
  444. if (!isset($_GET['movecat']) && !isset($_GET['moveeval'])) {
  445. die('Error: movecat or moveeval not defined');
  446. }
  447. $button = '<form name="confirm" method="post" action="'.api_get_self().'?confirm='
  448. .(isset($_GET['movecat']) ? '&movecat='.intval($_GET['movecat'])
  449. : '&moveeval='.intval($_GET['moveeval'])).'&selectcat='.$selectCat.'&targetcat='.intval($_GET['targetcat']).'">
  450. <input type="submit" value="'.get_lang('Ok').'">
  451. </form>';
  452. $warning_message = get_lang('MoveWarning').'<br><br>'.$button;
  453. $filter_warning_msg = false;
  454. }
  455. // Actions on the sortabletable.
  456. if (isset($_POST['action'])) {
  457. GradebookUtils::block_students();
  458. $number_of_selected_items = count($_POST['id']);
  459. if ($number_of_selected_items == 0) {
  460. $warning_message = get_lang('NoItemsSelected');
  461. $filter_warning_msg = false;
  462. } else {
  463. switch ($_POST['action']) {
  464. case 'deleted':
  465. $number_of_deleted_categories = 0;
  466. $number_of_deleted_evaluations = 0;
  467. $number_of_deleted_links = 0;
  468. foreach ($_POST['id'] as $indexstr) {
  469. if (substr($indexstr, 0, 4) == 'CATE') {
  470. $cats = Category::load(substr($indexstr, 4));
  471. if ($cats[0] != null) {
  472. $cats[0]->delete_all();
  473. }
  474. $number_of_deleted_categories++;
  475. }
  476. if (substr($indexstr, 0, 4) == 'EVAL') {
  477. $eval = Evaluation::load(substr($indexstr, 4));
  478. if ($eval[0] != null) {
  479. $eval[0]->delete_with_results();
  480. }
  481. $number_of_deleted_evaluations++;
  482. }
  483. if (substr($indexstr, 0, 4) == 'LINK') {
  484. //fixing #5229
  485. $id = substr($indexstr, 4);
  486. if (!empty($id)) {
  487. $link = LinkFactory::load($id);
  488. if ($link[0] != null) {
  489. $link[0]->delete();
  490. }
  491. $number_of_deleted_links++;
  492. }
  493. }
  494. }
  495. $confirmation_message =
  496. get_lang('DeletedCategories').' : <b>'.$number_of_deleted_categories.'</b><br />'.
  497. get_lang('DeletedEvaluations').' : <b>'.$number_of_deleted_evaluations.'</b><br />'.
  498. get_lang('DeletedLinks').' : <b>'.$number_of_deleted_links.'</b><br /><br />'.
  499. get_lang('TotalItems').' : <b>'.$number_of_selected_items.'</b>';
  500. $filter_confirm_msg = false;
  501. break;
  502. case 'setvisible':
  503. foreach ($_POST['id'] as $indexstr) {
  504. if (substr($indexstr, 0, 4) == 'CATE') {
  505. $cats = Category::load(substr($indexstr, 4));
  506. $cats[0]->set_visible(1);
  507. $cats[0]->save();
  508. $cats[0]->apply_visibility_to_children();
  509. }
  510. if (substr($indexstr, 0, 4) == 'EVAL') {
  511. $eval = Evaluation::load(substr($indexstr, 4));
  512. $eval[0]->set_visible(1);
  513. $eval[0]->save();
  514. }
  515. if (substr($indexstr, 0, 4) == 'LINK') {
  516. $link = LinkFactory::load(substr($indexstr, 4));
  517. $link[0]->set_visible(1);
  518. $link[0]->save();
  519. }
  520. }
  521. $confirmation_message = get_lang('ItemsVisible');
  522. $filter_confirm_msg = false;
  523. break;
  524. case 'setinvisible':
  525. foreach ($_POST['id'] as $indexstr) {
  526. if (substr($indexstr, 0, 4) == 'CATE') {
  527. $cats = Category::load(substr($indexstr, 4));
  528. $cats[0]->set_visible(0);
  529. $cats[0]->save();
  530. $cats[0]->apply_visibility_to_children();
  531. }
  532. if (substr($indexstr, 0, 4) == 'EVAL') {
  533. $eval = Evaluation::load(substr($indexstr, 4));
  534. $eval[0]->set_visible(0);
  535. $eval[0]->save();
  536. }
  537. if (substr($indexstr, 0, 4) == 'LINK') {
  538. $link = LinkFactory::load(substr($indexstr, 4));
  539. $link[0]->set_visible(0);
  540. $link[0]->save();
  541. }
  542. }
  543. $confirmation_message = get_lang('ItemsInVisible');
  544. $filter_confirm_msg = false;
  545. break;
  546. }
  547. }
  548. }
  549. if (isset($_POST['submit']) && isset($_POST['keyword'])) {
  550. header('Location: '.api_get_self().'?selectcat='.$selectCat.'&search='.Security::remove_XSS($_POST['keyword']));
  551. exit;
  552. }
  553. if (isset($_GET['categorymoved'])) {
  554. Display::addFlash(Display::return_message(get_lang('CategoryMoved'), 'confirmation', false));
  555. }
  556. if (isset($_GET['evaluationmoved'])) {
  557. Display::addFlash(Display::return_message(get_lang('EvaluationMoved'), 'confirmation', false));
  558. }
  559. if (isset($_GET['linkmoved'])) {
  560. Display::addFlash(Display::return_message(get_lang('LinkMoved'), 'confirmation', false));
  561. }
  562. if (isset($_GET['addcat'])) {
  563. Display::addFlash(Display::return_message(get_lang('CategoryAdded'), 'confirmation', false));
  564. }
  565. if (isset($_GET['linkadded'])) {
  566. Display::addFlash(Display::return_message(get_lang('LinkAdded'), 'confirmation', false));
  567. }
  568. if (isset($_GET['addresult'])) {
  569. Display::addFlash(Display::return_message(get_lang('ResultAdded'), 'confirmation', false));
  570. }
  571. if (isset($_GET['editcat'])) {
  572. Display::addFlash(Display::return_message(get_lang('CategoryEdited'), 'confirmation', false));
  573. }
  574. if (isset($_GET['editeval'])) {
  575. Display::addFlash(Display::return_message(get_lang('EvaluationEdited'), 'confirmation', false));
  576. }
  577. if (isset($_GET['linkedited'])) {
  578. Display::addFlash(Display::return_message(get_lang('LinkEdited'), 'confirmation', false));
  579. }
  580. if (isset($_GET['nolinkitems'])) {
  581. Display::addFlash(Display::return_message(get_lang('NoLinkItems'), 'warning', false));
  582. }
  583. if (isset($_GET['addallcat'])) {
  584. Display::addFlash(Display::return_message(get_lang('AddAllCat'), 'normal', false));
  585. }
  586. if (isset($confirmation_message)) {
  587. Display::addFlash(Display::return_message($confirmation_message, 'confirmation', $filter_confirm_msg));
  588. }
  589. if (isset($warning_message)) {
  590. Display::addFlash(Display::return_message($warning_message, 'warning', $filter_warning_msg));
  591. }
  592. if (isset($move_form)) {
  593. Display::addFlash(Display::return_message($move_form->toHtml(), 'normal', false));
  594. }
  595. $viewTitle = '';
  596. // DISPLAY HEADERS AND MESSAGES
  597. if (!isset($_GET['exportpdf'])) {
  598. if (isset($_GET['studentoverview'])) {
  599. $interbreadcrumb[] = [
  600. 'url' => Category::getUrl().'selectcat='.$selectCat,
  601. 'name' => get_lang('ToolGradebook'),
  602. ];
  603. $viewTitle = get_lang('FlatView');
  604. } elseif (isset($_GET['search'])) {
  605. $interbreadcrumb[] = [
  606. 'url' => Category::getUrl().'selectcat='.$selectCat,
  607. 'name' => get_lang('ToolGradebook'),
  608. ];
  609. $viewTitle = get_lang('SearchResults');
  610. } elseif (!empty($selectCat)) {
  611. $interbreadcrumb[] = [
  612. 'url' => '#',
  613. 'name' => get_lang('ToolGradebook'),
  614. ];
  615. } else {
  616. $viewTitle = get_lang('ToolGradebook');
  617. }
  618. }
  619. // LOAD DATA & DISPLAY TABLE
  620. $is_platform_admin = api_is_platform_admin();
  621. $is_course_admin = api_is_allowed_to_edit(null, true);
  622. $simple_search_form = '';
  623. if (isset($_GET['studentoverview'])) {
  624. //@todo this code also seems to be deprecated ...
  625. $cats = Category::load($selectCat);
  626. $stud_id = api_is_allowed_to_edit() ? null : $stud_id;
  627. $allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
  628. $alleval = $cats[0]->get_evaluations($stud_id, true);
  629. $alllink = $cats[0]->get_links($stud_id, true);
  630. if (isset($_GET['exportpdf'])) {
  631. $datagen = new GradebookDataGenerator($allcat, $alleval, $alllink);
  632. $header_names = [
  633. get_lang('Name'),
  634. get_lang('Description'),
  635. get_lang('Weight'),
  636. get_lang('Date'),
  637. get_lang('Results'),
  638. ];
  639. $data_array = $datagen->get_data(
  640. GradebookDataGenerator::GDG_SORT_NAME,
  641. 0,
  642. null,
  643. true
  644. );
  645. $newarray = [];
  646. foreach ($data_array as $data) {
  647. $newarray[] = array_slice($data, 1);
  648. }
  649. $pdf = new Cezpdf();
  650. $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
  651. $pdf->ezSetMargins(30, 30, 50, 30);
  652. $pdf->ezSetY(810);
  653. $pdf->ezText(
  654. get_lang('FlatView').' ('.api_convert_and_format_date(
  655. null,
  656. DATE_FORMAT_SHORT
  657. ).' '.api_convert_and_format_date(null, TIME_NO_SEC_FORMAT).')',
  658. 12,
  659. ['justification' => 'center']
  660. );
  661. $pdf->line(50, 790, 550, 790);
  662. $pdf->line(50, 40, 550, 40);
  663. $pdf->ezSetY(750);
  664. $pdf->ezTable(
  665. $newarray,
  666. $header_names,
  667. '',
  668. [
  669. 'showHeadings' => 1,
  670. 'shaded' => 1,
  671. 'showLines' => 1,
  672. 'rowGap' => 3,
  673. 'width' => 500,
  674. ]
  675. );
  676. $pdf->ezStream();
  677. exit;
  678. }
  679. } else {
  680. // Student view
  681. // In any other case (no search, no pdf), print the available gradebooks
  682. // Important note: loading a category will actually load the *contents* of
  683. // this category. This means that, to show the categories of a course,
  684. // we have to show the root category and show its subcategories that
  685. // are inside this course. This is done at the time of calling
  686. // $cats[0]->get_subcategories(), not at the time of doing Category::load()
  687. // $category comes from GET['selectcat']
  688. // if $category = 0 (which happens when GET['selectcat'] is undefined)
  689. // then Category::load() will create a new 'root' category with empty
  690. // course and session fields in memory (Category::create_root_category())
  691. $cats = Category:: load(
  692. null,
  693. null,
  694. $course_code,
  695. null,
  696. null,
  697. $session_id,
  698. false
  699. );
  700. if (empty($cats)) {
  701. // There is no category for this course+session, so create one
  702. $cat = new Category();
  703. if (!empty($session_id)) {
  704. $sessionName = api_get_session_name($session_id);
  705. $cat->set_name($course_code.' - '.get_lang('Session').' '.$sessionName);
  706. $cat->set_session_id($session_id);
  707. } else {
  708. $cat->set_name($course_code);
  709. }
  710. $cat->set_course_code($course_code);
  711. $cat->set_description(null);
  712. $cat->set_user_id($stud_id);
  713. $cat->set_parent_id(0);
  714. $cat->set_weight(100);
  715. $cat->set_visible(0);
  716. $cat->set_certificate_min_score(75);
  717. $can_edit = api_is_allowed_to_edit(true, true);
  718. if ($can_edit) {
  719. $cat->add();
  720. }
  721. unset($cat);
  722. }
  723. $cats = Category::load($selectCat, null, null, null, null, null, false);
  724. // With this fix the teacher only can view 1 gradebook
  725. if (api_is_platform_admin()) {
  726. $stud_id = api_is_allowed_to_edit() ? null : api_get_user_id();
  727. }
  728. $allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
  729. $alleval = $cats[0]->get_evaluations($stud_id);
  730. $alllink = $cats[0]->get_links($stud_id);
  731. }
  732. // add params to the future links (in the table shown)
  733. $addparams = ['selectcat' => $selectCat];
  734. if (isset($_GET['studentoverview'])) {
  735. $addparams['studentoverview'] = '';
  736. }
  737. if (isset($_GET['cidReq']) && $_GET['cidReq'] != '') {
  738. $addparams['cidReq'] = Security::remove_XSS($_GET['cidReq']);
  739. } else {
  740. $addparams['cidReq'] = '';
  741. }
  742. $no_qualification = false;
  743. // Show certificate link.
  744. $certificate = [];
  745. $actionsLeft = '';
  746. $hideCertificateExport = api_get_setting('hide_certificate_export_link');
  747. if (!empty($selectCat)) {
  748. $cat = new Category();
  749. $course_id = CourseManager::get_course_by_category($selectCat);
  750. $show_message = $cat->show_message_resource_delete($course_id);
  751. if ($show_message == '') {
  752. // Student
  753. if (!api_is_allowed_to_edit() && !api_is_excluded_user_type()) {
  754. $certificate = Category::generateUserCertificate(
  755. $selectCat,
  756. $stud_id
  757. );
  758. if ($hideCertificateExport !== 'true' && isset($certificate['pdf_url'])) {
  759. $actionsLeft .= Display::url(
  760. Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadCertificatePdf'),
  761. $certificate['pdf_url'],
  762. ['class' => 'btn btn-default']
  763. );
  764. }
  765. $currentScore = Category::getCurrentScore(
  766. $stud_id,
  767. $cats[0],
  768. true
  769. );
  770. Category::registerCurrentScore($currentScore, $stud_id, $selectCat);
  771. }
  772. }
  773. }
  774. if (!api_is_allowed_to_edit(null, true)) {
  775. $allowButton = api_get_configuration_value('gradebook_hide_pdf_report_button') === false;
  776. if ($allowButton) {
  777. $actionsLeft .= Display::url(
  778. Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadReportPdf'),
  779. api_get_self().'?action=export_table&'.api_get_cidreq().'&category_id='.$selectCat,
  780. ['class' => 'btn btn-default']
  781. );
  782. }
  783. }
  784. if (isset($first_time) && $first_time == 1 && api_is_allowed_to_edit(null, true)) {
  785. echo '<meta http-equiv="refresh" content="0;url='.api_get_self().'?'.api_get_cidreq().'" />';
  786. } else {
  787. // Tool introduction
  788. Display::display_introduction_section(
  789. TOOL_GRADEBOOK,
  790. ['ToolbarSet' => 'AssessmentsIntroduction']
  791. );
  792. if (!empty($actionsLeft)) {
  793. echo $toolbar = Display::toolbarAction(
  794. 'gradebook-student-actions',
  795. [$actionsLeft]
  796. );
  797. }
  798. $cats = Category::load(
  799. null,
  800. null,
  801. $course_code,
  802. null,
  803. null,
  804. $session_id,
  805. false
  806. );
  807. if (!empty($cats)) {
  808. if ((api_get_setting('gradebook_enable_grade_model') === 'true') &&
  809. (
  810. api_is_platform_admin() || (
  811. api_is_allowed_to_edit(null, true) &&
  812. api_get_setting('teachers_can_change_grade_model_settings') === 'true'
  813. )
  814. )
  815. ) {
  816. // Getting grade models.
  817. $obj = new GradeModel();
  818. $grade_models = $obj->get_all();
  819. $grade_model_id = $cats[0]->get_grade_model_id();
  820. // No children.
  821. if ((count($cats) == 1 && empty($grade_model_id)) ||
  822. (count($cats) == 1 && $grade_model_id != -1)
  823. ) {
  824. if (!empty($grade_models)) {
  825. $form_grade = new FormValidator('grade_model_settings');
  826. $obj->fill_grade_model_select_in_form($form_grade, 'grade_model_id', $grade_model_id);
  827. $form_grade->addButtonSave(get_lang('Save'));
  828. if ($form_grade->validate()) {
  829. $value = $form_grade->exportValue('grade_model_id');
  830. $gradebook = new Gradebook();
  831. $gradebook->update(['id' => $cats[0]->get_id(), 'grade_model_id' => $value], true);
  832. //do something
  833. $obj = new GradeModel();
  834. $components = $obj->get_components($value);
  835. foreach ($components as $component) {
  836. $gradebook = new Gradebook();
  837. $params = [];
  838. $params['name'] = $component['acronym'];
  839. $params['description'] = $component['title'];
  840. $params['user_id'] = api_get_user_id();
  841. $params['parent_id'] = $cats[0]->get_id();
  842. $params['weight'] = $component['percentage'];
  843. $params['session_id'] = api_get_session_id();
  844. $params['course_code'] = api_get_course_id();
  845. $params['grade_model_id'] = api_get_session_id();
  846. $gradebook->save($params);
  847. }
  848. // Reloading cats
  849. $cats = Category:: load(
  850. null,
  851. null,
  852. $course_code,
  853. null,
  854. null,
  855. $session_id,
  856. false
  857. );
  858. } else {
  859. $form_grade->display();
  860. }
  861. }
  862. }
  863. }
  864. $i = 0;
  865. $allcat = [];
  866. $model = ExerciseLib::getCourseScoreModel();
  867. $allowGraph = api_get_configuration_value('gradebook_hide_graph') === false;
  868. $isAllow = api_is_allowed_to_edit(null, true);
  869. /** @var Category $cat */
  870. foreach ($cats as $cat) {
  871. $allcat = $cat->get_subcategories($stud_id, $course_code, $session_id);
  872. $alleval = $cat->get_evaluations($stud_id, false, $course_code, $session_id);
  873. $alllink = $cat->get_links($stud_id, true, $course_code, $session_id);
  874. if ($cat->get_parent_id() != 0) {
  875. $i++;
  876. } else {
  877. // This is the father
  878. // Create gradebook/add gradebook links.
  879. DisplayGradebook::header(
  880. $cat,
  881. 0,
  882. $cat->get_id(),
  883. $is_course_admin,
  884. $is_platform_admin,
  885. $simple_search_form,
  886. false,
  887. true,
  888. $certificate
  889. );
  890. if ($isAllow && api_get_setting('gradebook_enable_grade_model') === 'true') {
  891. // Showing the grading system
  892. if (!empty($grade_models[$grade_model_id])) {
  893. echo Display::return_message(
  894. get_lang('GradeModel').': '.$grade_models[$grade_model_id]['name']
  895. );
  896. }
  897. }
  898. $exportToPdf = false;
  899. if ($action === 'export_table') {
  900. $exportToPdf = true;
  901. }
  902. $loadStats = [];
  903. if (!$isAllow) {
  904. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  905. $loadStats = [1, 2, 3];
  906. } else {
  907. if (api_get_configuration_value('gradebook_enable_best_score') !== false) {
  908. $loadStats = [2];
  909. }
  910. }
  911. }
  912. $gradebookTable = new GradebookTable(
  913. $cat,
  914. $allcat,
  915. $alleval,
  916. $alllink,
  917. $addparams,
  918. $exportToPdf,
  919. null,
  920. null,
  921. [],
  922. $loadStats
  923. );
  924. if ($isAllow) {
  925. $gradebookTable->td_attributes = [
  926. 4 => 'class="text-center"',
  927. ];
  928. }
  929. $table = $gradebookTable->return_table();
  930. $graph = '';
  931. if ($allowGraph && empty($model)) {
  932. $graph = $gradebookTable->getGraph();
  933. }
  934. if ($action === 'export_table') {
  935. ob_clean();
  936. $params = [
  937. 'pdf_title' => sprintf(get_lang('GradeFromX'), $courseInfo['name']),
  938. 'course_code' => api_get_course_id(),
  939. 'session_info' => '',
  940. 'course_info' => '',
  941. 'pdf_date' => '',
  942. 'student_info' => api_get_user_info(),
  943. 'show_grade_generated_date' => true,
  944. 'show_real_course_teachers' => false,
  945. 'show_teacher_as_myself' => false,
  946. 'orientation' => 'P',
  947. ];
  948. $pdf = new PDF('A4', $params['orientation'], $params);
  949. $pdf->html_to_pdf_with_template(
  950. $table.
  951. $graph.
  952. '<br />'.get_lang('Feedback').'<br />
  953. <textarea rows="5" cols="100" >&nbsp;</textarea>'
  954. );
  955. } else {
  956. echo $table;
  957. echo $graph;
  958. }
  959. }
  960. }
  961. }
  962. }
  963. api_set_in_gradebook();
  964. $contents = ob_get_contents();
  965. ob_end_clean();
  966. $view = new Template($viewTitle);
  967. $view->assign('content', $contents);
  968. $view->display_one_col_template();