index.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. if (isset($_GET['set_visible'])) {
  312. $visibility_command = 1;
  313. } else {
  314. $visibility_command = 0;
  315. }
  316. $cats = Category::load($_GET['visiblecat']);
  317. $cats[0]->set_visible($visibility_command);
  318. $cats[0]->save();
  319. $cats[0]->apply_visibility_to_children();
  320. unset($cats);
  321. if ($visibility_command) {
  322. $confirmation_message = get_lang('ViMod');
  323. $filter_confirm_msg = false;
  324. } else {
  325. $confirmation_message = get_lang('InViMod');
  326. $filter_confirm_msg = false;
  327. }
  328. }
  329. if (isset($_GET['deletecat'])) {
  330. GradebookUtils::block_students();
  331. $cats = Category::load($_GET['deletecat']);
  332. if (isset($cats[0])) {
  333. //delete all categories,subcategories and results
  334. if ($cats[0] != null) {
  335. if ($cats[0]->get_id() != 0) {
  336. // better don't try to delete the root...
  337. $cats[0]->delete_all();
  338. }
  339. }
  340. }
  341. $confirmation_message = get_lang('CategoryDeleted');
  342. $filter_confirm_msg = false;
  343. }
  344. // Parameters for evaluations.
  345. if (isset($_GET['visibleeval'])) {
  346. GradebookUtils::block_students();
  347. $visibility_command = 0;
  348. if (isset($_GET['set_visible'])) {
  349. $visibility_command = 1;
  350. }
  351. $eval = Evaluation::load($_GET['visibleeval']);
  352. $eval[0]->set_visible($visibility_command);
  353. $eval[0]->save();
  354. unset($eval);
  355. if ($visibility_command) {
  356. $confirmation_message = get_lang('ViMod');
  357. $filter_confirm_msg = false;
  358. } else {
  359. $confirmation_message = get_lang('InViMod');
  360. $filter_confirm_msg = false;
  361. }
  362. }
  363. // Parameters for evaluations.
  364. if (isset($_GET['lockedeval'])) {
  365. GradebookUtils::block_students();
  366. $locked = (int) $_GET['lockedeval'];
  367. if (isset($_GET['typelocked']) && api_is_platform_admin()) {
  368. $type_locked = 0;
  369. $confirmation_message = get_lang('EvaluationHasBeenUnLocked');
  370. } else {
  371. $type_locked = 1;
  372. $confirmation_message = get_lang('EvaluationHasBeenLocked');
  373. }
  374. $eval = Evaluation::load($locked);
  375. if ($eval[0] != null) {
  376. $eval[0]->lock($type_locked);
  377. }
  378. $filter_confirm_msg = false;
  379. }
  380. if (isset($_GET['deleteeval'])) {
  381. GradebookUtils::block_students();
  382. $eval = Evaluation::load($_GET['deleteeval']);
  383. if ($eval[0] != null) {
  384. $eval[0]->delete_with_results();
  385. }
  386. $confirmation_message = get_lang('GradebookEvaluationDeleted');
  387. $filter_confirm_msg = false;
  388. }
  389. // Parameters for links.
  390. if (isset($_GET['visiblelink'])) {
  391. GradebookUtils::block_students();
  392. $visibility_command = 0;
  393. if (isset($_GET['set_visible'])) {
  394. $visibility_command = 1;
  395. }
  396. $link = LinkFactory::load($_GET['visiblelink']);
  397. if (isset($link) && isset($link[0])) {
  398. $link[0]->set_visible($visibility_command);
  399. $link[0]->save();
  400. }
  401. unset($link);
  402. if ($visibility_command) {
  403. $confirmation_message = get_lang('ViMod');
  404. $filter_confirm_msg = false;
  405. } else {
  406. $confirmation_message = get_lang('InViMod');
  407. $filter_confirm_msg = false;
  408. }
  409. }
  410. if (isset($_GET['deletelink'])) {
  411. GradebookUtils::block_students();
  412. $get_delete_link = (int) $_GET['deletelink'];
  413. //fixing #5229
  414. if (!empty($get_delete_link)) {
  415. $link = LinkFactory::load($get_delete_link);
  416. if ($link[0] != null) {
  417. // Clean forum qualify
  418. $sql = 'UPDATE '.$tbl_forum_thread.' SET
  419. thread_qualify_max = 0,
  420. thread_weight = 0,
  421. thread_title_qualify = ""
  422. WHERE c_id = '.$course_id.' AND thread_id = (
  423. SELECT ref_id FROM '.$tbl_grade_links.'
  424. WHERE id='.$get_delete_link.' AND type = '.LINK_FORUM_THREAD.'
  425. )';
  426. Database::query($sql);
  427. // clean attendance
  428. $sql = 'UPDATE '.$tbl_attendance.' SET
  429. attendance_weight = 0,
  430. attendance_qualify_title = ""
  431. WHERE c_id = '.$course_id.' AND id = (
  432. SELECT ref_id FROM '.$tbl_grade_links.'
  433. WHERE id='.$get_delete_link.' AND type = '.LINK_ATTENDANCE.'
  434. )';
  435. Database::query($sql);
  436. $link[0]->delete();
  437. }
  438. unset($link);
  439. $confirmation_message = get_lang('LinkDeleted');
  440. $filter_confirm_msg = false;
  441. }
  442. }
  443. if (!empty($course_to_crsind) && !isset($_GET['confirm'])) {
  444. GradebookUtils::block_students();
  445. if (!isset($_GET['movecat']) && !isset($_GET['moveeval'])) {
  446. die('Error: movecat or moveeval not defined');
  447. }
  448. $button = '<form name="confirm" method="post" action="'.api_get_self().'?confirm='
  449. .(isset($_GET['movecat']) ? '&movecat='.intval($_GET['movecat'])
  450. : '&moveeval='.intval($_GET['moveeval'])).'&selectcat='.$selectCat.'&targetcat='.intval($_GET['targetcat']).'">
  451. <input type="submit" value="'.get_lang('Ok').'">
  452. </form>';
  453. $warning_message = get_lang('MoveWarning').'<br><br>'.$button;
  454. $filter_warning_msg = false;
  455. }
  456. // Actions on the sortabletable.
  457. if (isset($_POST['action'])) {
  458. GradebookUtils::block_students();
  459. $number_of_selected_items = count($_POST['id']);
  460. if ($number_of_selected_items == 0) {
  461. $warning_message = get_lang('NoItemsSelected');
  462. $filter_warning_msg = false;
  463. } else {
  464. switch ($_POST['action']) {
  465. case 'deleted':
  466. $number_of_deleted_categories = 0;
  467. $number_of_deleted_evaluations = 0;
  468. $number_of_deleted_links = 0;
  469. foreach ($_POST['id'] as $indexstr) {
  470. if (substr($indexstr, 0, 4) == 'CATE') {
  471. $cats = Category::load(substr($indexstr, 4));
  472. if ($cats[0] != null) {
  473. $cats[0]->delete_all();
  474. }
  475. $number_of_deleted_categories++;
  476. }
  477. if (substr($indexstr, 0, 4) == 'EVAL') {
  478. $eval = Evaluation::load(substr($indexstr, 4));
  479. if ($eval[0] != null) {
  480. $eval[0]->delete_with_results();
  481. }
  482. $number_of_deleted_evaluations++;
  483. }
  484. if (substr($indexstr, 0, 4) == 'LINK') {
  485. //fixing #5229
  486. $id = substr($indexstr, 4);
  487. if (!empty($id)) {
  488. $link = LinkFactory::load($id);
  489. if ($link[0] != null) {
  490. $link[0]->delete();
  491. }
  492. $number_of_deleted_links++;
  493. }
  494. }
  495. }
  496. $confirmation_message =
  497. get_lang('DeletedCategories').' : <b>'.$number_of_deleted_categories.'</b><br />'.
  498. get_lang('DeletedEvaluations').' : <b>'.$number_of_deleted_evaluations.'</b><br />'.
  499. get_lang('DeletedLinks').' : <b>'.$number_of_deleted_links.'</b><br /><br />'.
  500. get_lang('TotalItems').' : <b>'.$number_of_selected_items.'</b>';
  501. $filter_confirm_msg = false;
  502. break;
  503. case 'setvisible':
  504. foreach ($_POST['id'] as $indexstr) {
  505. if (substr($indexstr, 0, 4) == 'CATE') {
  506. $cats = Category::load(substr($indexstr, 4));
  507. $cats[0]->set_visible(1);
  508. $cats[0]->save();
  509. $cats[0]->apply_visibility_to_children();
  510. }
  511. if (substr($indexstr, 0, 4) == 'EVAL') {
  512. $eval = Evaluation::load(substr($indexstr, 4));
  513. $eval[0]->set_visible(1);
  514. $eval[0]->save();
  515. }
  516. if (substr($indexstr, 0, 4) == 'LINK') {
  517. $link = LinkFactory::load(substr($indexstr, 4));
  518. $link[0]->set_visible(1);
  519. $link[0]->save();
  520. }
  521. }
  522. $confirmation_message = get_lang('ItemsVisible');
  523. $filter_confirm_msg = false;
  524. break;
  525. case 'setinvisible':
  526. foreach ($_POST['id'] as $indexstr) {
  527. if (substr($indexstr, 0, 4) == 'CATE') {
  528. $cats = Category::load(substr($indexstr, 4));
  529. $cats[0]->set_visible(0);
  530. $cats[0]->save();
  531. $cats[0]->apply_visibility_to_children();
  532. }
  533. if (substr($indexstr, 0, 4) == 'EVAL') {
  534. $eval = Evaluation::load(substr($indexstr, 4));
  535. $eval[0]->set_visible(0);
  536. $eval[0]->save();
  537. }
  538. if (substr($indexstr, 0, 4) == 'LINK') {
  539. $link = LinkFactory::load(substr($indexstr, 4));
  540. $link[0]->set_visible(0);
  541. $link[0]->save();
  542. }
  543. }
  544. $confirmation_message = get_lang('ItemsInVisible');
  545. $filter_confirm_msg = false;
  546. break;
  547. }
  548. }
  549. }
  550. if (isset($_POST['submit']) && isset($_POST['keyword'])) {
  551. header('Location: '.api_get_self().'?selectcat='.$selectCat.'&search='.Security::remove_XSS($_POST['keyword']));
  552. exit;
  553. }
  554. if (isset($_GET['categorymoved'])) {
  555. Display::addFlash(Display::return_message(get_lang('CategoryMoved'), 'confirmation', false));
  556. }
  557. if (isset($_GET['evaluationmoved'])) {
  558. Display::addFlash(Display::return_message(get_lang('EvaluationMoved'), 'confirmation', false));
  559. }
  560. if (isset($_GET['linkmoved'])) {
  561. Display::addFlash(Display::return_message(get_lang('LinkMoved'), 'confirmation', false));
  562. }
  563. if (isset($_GET['addcat'])) {
  564. Display::addFlash(Display::return_message(get_lang('CategoryAdded'), 'confirmation', false));
  565. }
  566. if (isset($_GET['linkadded'])) {
  567. Display::addFlash(Display::return_message(get_lang('LinkAdded'), 'confirmation', false));
  568. }
  569. if (isset($_GET['addresult'])) {
  570. Display::addFlash(Display::return_message(get_lang('ResultAdded'), 'confirmation', false));
  571. }
  572. if (isset($_GET['editcat'])) {
  573. Display::addFlash(Display::return_message(get_lang('CategoryEdited'), 'confirmation', false));
  574. }
  575. if (isset($_GET['editeval'])) {
  576. Display::addFlash(Display::return_message(get_lang('EvaluationEdited'), 'confirmation', false));
  577. }
  578. if (isset($_GET['linkedited'])) {
  579. Display::addFlash(Display::return_message(get_lang('LinkEdited'), 'confirmation', false));
  580. }
  581. if (isset($_GET['nolinkitems'])) {
  582. Display::addFlash(Display::return_message(get_lang('NoLinkItems'), 'warning', false));
  583. }
  584. if (isset($_GET['addallcat'])) {
  585. Display::addFlash(Display::return_message(get_lang('AddAllCat'), 'normal', false));
  586. }
  587. if (isset($confirmation_message)) {
  588. Display::addFlash(Display::return_message($confirmation_message, 'confirmation', $filter_confirm_msg));
  589. }
  590. if (isset($warning_message)) {
  591. Display::addFlash(Display::return_message($warning_message, 'warning', $filter_warning_msg));
  592. }
  593. if (isset($move_form)) {
  594. Display::addFlash(Display::return_message($move_form->toHtml(), 'normal', false));
  595. }
  596. $viewTitle = '';
  597. // DISPLAY HEADERS AND MESSAGES
  598. if (!isset($_GET['exportpdf'])) {
  599. if (isset($_GET['studentoverview'])) {
  600. $interbreadcrumb[] = [
  601. 'url' => Category::getUrl().'selectcat='.$selectCat,
  602. 'name' => get_lang('ToolGradebook'),
  603. ];
  604. $viewTitle = get_lang('FlatView');
  605. } elseif (isset($_GET['search'])) {
  606. $interbreadcrumb[] = [
  607. 'url' => Category::getUrl().'selectcat='.$selectCat,
  608. 'name' => get_lang('ToolGradebook'),
  609. ];
  610. $viewTitle = get_lang('SearchResults');
  611. } elseif (!empty($selectCat)) {
  612. $interbreadcrumb[] = [
  613. 'url' => '#',
  614. 'name' => get_lang('ToolGradebook'),
  615. ];
  616. } else {
  617. $viewTitle = get_lang('ToolGradebook');
  618. }
  619. }
  620. // LOAD DATA & DISPLAY TABLE
  621. $is_platform_admin = api_is_platform_admin();
  622. $is_course_admin = api_is_allowed_to_edit(null, true);
  623. $simple_search_form = '';
  624. if (isset($_GET['studentoverview'])) {
  625. //@todo this code also seems to be deprecated ...
  626. $cats = Category::load($selectCat);
  627. $stud_id = (api_is_allowed_to_edit() ? null : $stud_id);
  628. $allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
  629. $alleval = $cats[0]->get_evaluations($stud_id, true);
  630. $alllink = $cats[0]->get_links($stud_id, true);
  631. if (isset($_GET['exportpdf'])) {
  632. $datagen = new GradebookDataGenerator($allcat, $alleval, $alllink);
  633. $header_names = [
  634. get_lang('Name'),
  635. get_lang('Description'),
  636. get_lang('Weight'),
  637. get_lang('Date'),
  638. get_lang('Results'),
  639. ];
  640. $data_array = $datagen->get_data(
  641. GradebookDataGenerator::GDG_SORT_NAME,
  642. 0,
  643. null,
  644. true
  645. );
  646. $newarray = [];
  647. foreach ($data_array as $data) {
  648. $newarray[] = array_slice($data, 1);
  649. }
  650. $pdf = new Cezpdf();
  651. $pdf->selectFont(api_get_path(LIBRARY_PATH).'ezpdf/fonts/Courier.afm');
  652. $pdf->ezSetMargins(30, 30, 50, 30);
  653. $pdf->ezSetY(810);
  654. $pdf->ezText(
  655. get_lang('FlatView').' ('.api_convert_and_format_date(
  656. null,
  657. DATE_FORMAT_SHORT
  658. ).' '.api_convert_and_format_date(null, TIME_NO_SEC_FORMAT).')',
  659. 12,
  660. ['justification' => 'center']
  661. );
  662. $pdf->line(50, 790, 550, 790);
  663. $pdf->line(50, 40, 550, 40);
  664. $pdf->ezSetY(750);
  665. $pdf->ezTable(
  666. $newarray,
  667. $header_names,
  668. '',
  669. [
  670. 'showHeadings' => 1,
  671. 'shaded' => 1,
  672. 'showLines' => 1,
  673. 'rowGap' => 3,
  674. 'width' => 500,
  675. ]
  676. );
  677. $pdf->ezStream();
  678. exit;
  679. }
  680. } else {
  681. // Student view
  682. //in any other case (no search, no pdf), print the available gradebooks
  683. // Important note: loading a category will actually load the *contents* of
  684. // this category. This means that, to show the categories of a course,
  685. // we have to show the root category and show its subcategories that
  686. // are inside this course. This is done at the time of calling
  687. // $cats[0]->get_subcategories(), not at the time of doing Category::load()
  688. // $category comes from GET['selectcat']
  689. //if $category = 0 (which happens when GET['selectcat'] is undefined)
  690. // then Category::load() will create a new 'root' category with empty
  691. // course and session fields in memory (Category::create_root_category())
  692. if ($_in_course === true) {
  693. $cats = Category:: load(
  694. null,
  695. null,
  696. $course_code,
  697. null,
  698. null,
  699. $session_id,
  700. false
  701. );
  702. if (empty($cats)) {
  703. // There is no category for this course+session, so create one
  704. $cat = new Category();
  705. if (!empty($session_id)) {
  706. $sessionName = api_get_session_name($session_id);
  707. $cat->set_name($course_code.' - '.get_lang('Session').' '.$sessionName);
  708. $cat->set_session_id($session_id);
  709. } else {
  710. $cat->set_name($course_code);
  711. }
  712. $cat->set_course_code($course_code);
  713. $cat->set_description(null);
  714. $cat->set_user_id($stud_id);
  715. $cat->set_parent_id(0);
  716. $cat->set_weight(100);
  717. $cat->set_visible(0);
  718. $cat->set_certificate_min_score(75);
  719. $can_edit = api_is_allowed_to_edit(true, true);
  720. if ($can_edit) {
  721. $cat->add();
  722. }
  723. unset($cat);
  724. }
  725. unset($cats);
  726. }
  727. $cats = Category::load($selectCat, null, null, null, null, null, false);
  728. // With this fix the teacher only can view 1 gradebook
  729. if (api_is_platform_admin()) {
  730. $stud_id = (api_is_allowed_to_edit() ? null : api_get_user_id());
  731. }
  732. $allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
  733. $alleval = $cats[0]->get_evaluations($stud_id);
  734. $alllink = $cats[0]->get_links($stud_id);
  735. }
  736. // add params to the future links (in the table shown)
  737. $addparams = ['selectcat' => $selectCat];
  738. if (isset($_GET['studentoverview'])) {
  739. $addparams['studentoverview'] = '';
  740. }
  741. //$addparams['cidReq']='';
  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 (isset($certificate['pdf_url']) && $hideCertificateExport !== 'true') {
  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. if (api_is_allowed_to_edit(null, true)) {
  804. if (((empty($selectCat)) || (isset($_GET['cidReq']) && $_GET['cidReq'] !== '')) ||
  805. (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'false')
  806. ) {
  807. $cats = Category:: load(
  808. null,
  809. null,
  810. $course_code,
  811. null,
  812. null,
  813. $session_id,
  814. false
  815. );
  816. }
  817. }
  818. $cats = Category::load(
  819. null,
  820. null,
  821. $course_code,
  822. null,
  823. null,
  824. $session_id,
  825. false
  826. );
  827. if (!empty($cats)) {
  828. if ((api_get_setting('gradebook_enable_grade_model') === 'true') &&
  829. (
  830. api_is_platform_admin() || (api_is_allowed_to_edit(null, true) &&
  831. api_get_setting('teachers_can_change_grade_model_settings') === 'true')
  832. )
  833. ) {
  834. // Getting grade models.
  835. $obj = new GradeModel();
  836. $grade_models = $obj->get_all();
  837. $grade_model_id = $cats[0]->get_grade_model_id();
  838. // No children.
  839. if ((count($cats) == 1 && empty($grade_model_id)) ||
  840. (count($cats) == 1 && $grade_model_id != -1)
  841. ) {
  842. if (!empty($grade_models)) {
  843. $form_grade = new FormValidator('grade_model_settings');
  844. $obj->fill_grade_model_select_in_form($form_grade, 'grade_model_id', $grade_model_id);
  845. $form_grade->addButtonSave(get_lang('Save'));
  846. if ($form_grade->validate()) {
  847. $value = $form_grade->exportValue('grade_model_id');
  848. $gradebook = new Gradebook();
  849. $gradebook->update(['id' => $cats[0]->get_id(), 'grade_model_id' => $value], true);
  850. //do something
  851. $obj = new GradeModel();
  852. $components = $obj->get_components($value);
  853. foreach ($components as $component) {
  854. $gradebook = new Gradebook();
  855. $params = [];
  856. $params['name'] = $component['acronym'];
  857. $params['description'] = $component['title'];
  858. $params['user_id'] = api_get_user_id();
  859. $params['parent_id'] = $cats[0]->get_id();
  860. $params['weight'] = $component['percentage'];
  861. $params['session_id'] = api_get_session_id();
  862. $params['course_code'] = api_get_course_id();
  863. $params['grade_model_id'] = api_get_session_id();
  864. $gradebook->save($params);
  865. }
  866. // Reloading cats
  867. $cats = Category:: load(
  868. null,
  869. null,
  870. $course_code,
  871. null,
  872. null,
  873. $session_id,
  874. false
  875. );
  876. } else {
  877. $form_grade->display();
  878. }
  879. }
  880. }
  881. }
  882. $i = 0;
  883. $allcat = [];
  884. $model = ExerciseLib::getCourseScoreModel();
  885. /** @var Category $cat */
  886. foreach ($cats as $cat) {
  887. $allcat = $cat->get_subcategories($stud_id, $course_code, $session_id);
  888. $alleval = $cat->get_evaluations($stud_id, false, $course_code, $session_id);
  889. $alllink = $cat->get_links($stud_id, true, $course_code, $session_id);
  890. if ($cat->get_parent_id() != 0) {
  891. $i++;
  892. } else {
  893. // This is the father
  894. // Create gradebook/add gradebook links.
  895. DisplayGradebook::header(
  896. $cat,
  897. 0,
  898. $cat->get_id(),
  899. $is_course_admin,
  900. $is_platform_admin,
  901. $simple_search_form,
  902. false,
  903. true,
  904. $certificate
  905. );
  906. if (api_is_allowed_to_edit(null, true) &&
  907. api_get_setting('gradebook_enable_grade_model') === 'true'
  908. ) {
  909. // Showing the grading system
  910. if (!empty($grade_models[$grade_model_id])) {
  911. echo Display::return_message(
  912. get_lang('GradeModel').': '.$grade_models[$grade_model_id]['name']
  913. );
  914. }
  915. }
  916. $exportToPdf = false;
  917. if ($action == 'export_table') {
  918. $exportToPdf = true;
  919. }
  920. $loadStats = [];
  921. $teacher = api_is_allowed_to_edit(null, true);
  922. if (!$teacher) {
  923. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  924. $loadStats = [1, 2, 3];
  925. } else {
  926. if (api_get_configuration_value('gradebook_enable_best_score') !== false) {
  927. $loadStats = [2];
  928. }
  929. }
  930. }
  931. $gradebookTable = new GradebookTable(
  932. $cat,
  933. $allcat,
  934. $alleval,
  935. $alllink,
  936. $addparams,
  937. $exportToPdf,
  938. null,
  939. null,
  940. [],
  941. $loadStats
  942. );
  943. if (api_is_allowed_to_edit()) {
  944. $gradebookTable->td_attributes = [
  945. 4 => 'class="text-center"',
  946. ];
  947. }
  948. $table = $gradebookTable->return_table();
  949. $allowGraph = api_get_configuration_value('gradebook_hide_graph') === false;
  950. $graph = '';
  951. if ($allowGraph) {
  952. $graph = $gradebookTable->getGraph();
  953. }
  954. if ($action === 'export_table') {
  955. ob_clean();
  956. $params = [
  957. 'pdf_title' => sprintf(get_lang('GradeFromX'), $courseInfo['name']),
  958. 'course_code' => api_get_course_id(),
  959. 'session_info' => '',
  960. 'course_info' => '',
  961. 'pdf_date' => '',
  962. 'student_info' => api_get_user_info(),
  963. 'show_grade_generated_date' => true,
  964. 'show_real_course_teachers' => false,
  965. 'show_teacher_as_myself' => false,
  966. 'orientation' => 'P',
  967. ];
  968. $pdf = new PDF('A4', $params['orientation'], $params);
  969. $pdf->html_to_pdf_with_template(
  970. $table.
  971. $graph.
  972. '<br />'.get_lang('Feedback').'<br />
  973. <textarea rows="5" cols="100" >&nbsp;</textarea>'
  974. );
  975. } else {
  976. echo $table;
  977. echo $graph;
  978. }
  979. }
  980. }
  981. }
  982. }
  983. api_set_in_gradebook();
  984. $contents = ob_get_contents();
  985. ob_end_clean();
  986. $view = new Template($viewTitle);
  987. $view->assign('content', $contents);
  988. $view->display_one_col_template();