index.php 35 KB

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