index.php 37 KB

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