index.php 36 KB

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