index.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. $cats = Category:: load(
  693. null,
  694. null,
  695. $course_code,
  696. null,
  697. null,
  698. $session_id,
  699. false
  700. );
  701. if (empty($cats)) {
  702. // There is no category for this course+session, so create one
  703. $cat = new Category();
  704. if (!empty($session_id)) {
  705. $sessionName = api_get_session_name($session_id);
  706. $cat->set_name($course_code.' - '.get_lang('Session').' '.$sessionName);
  707. $cat->set_session_id($session_id);
  708. } else {
  709. $cat->set_name($course_code);
  710. }
  711. $cat->set_course_code($course_code);
  712. $cat->set_description(null);
  713. $cat->set_user_id($stud_id);
  714. $cat->set_parent_id(0);
  715. $cat->set_weight(100);
  716. $cat->set_visible(0);
  717. $cat->set_certificate_min_score(75);
  718. $can_edit = api_is_allowed_to_edit(true, true);
  719. if ($can_edit) {
  720. $cat->add();
  721. }
  722. unset($cat);
  723. }
  724. $cats = Category::load($selectCat, null, null, null, null, null, false);
  725. // With this fix the teacher only can view 1 gradebook
  726. if (api_is_platform_admin()) {
  727. $stud_id = api_is_allowed_to_edit() ? null : api_get_user_id();
  728. }
  729. $allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
  730. $alleval = $cats[0]->get_evaluations($stud_id);
  731. $alllink = $cats[0]->get_links($stud_id);
  732. }
  733. // add params to the future links (in the table shown)
  734. $addparams = ['selectcat' => $selectCat];
  735. if (isset($_GET['studentoverview'])) {
  736. $addparams['studentoverview'] = '';
  737. }
  738. //$addparams['cidReq']='';
  739. if (isset($_GET['cidReq']) && $_GET['cidReq'] != '') {
  740. $addparams['cidReq'] = Security::remove_XSS($_GET['cidReq']);
  741. } else {
  742. $addparams['cidReq'] = '';
  743. }
  744. $no_qualification = false;
  745. // Show certificate link.
  746. $certificate = [];
  747. $actionsLeft = '';
  748. $hideCertificateExport = api_get_setting('hide_certificate_export_link');
  749. if (!empty($selectCat)) {
  750. $cat = new Category();
  751. $course_id = CourseManager::get_course_by_category($selectCat);
  752. $show_message = $cat->show_message_resource_delete($course_id);
  753. if ($show_message == '') {
  754. // Student
  755. if (!api_is_allowed_to_edit() && !api_is_excluded_user_type()) {
  756. $certificate = Category::generateUserCertificate(
  757. $selectCat,
  758. $stud_id
  759. );
  760. if ($hideCertificateExport !== 'true' && isset($certificate['pdf_url'])) {
  761. $actionsLeft .= Display::url(
  762. Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadCertificatePdf'),
  763. $certificate['pdf_url'],
  764. ['class' => 'btn btn-default']
  765. );
  766. }
  767. $currentScore = Category::getCurrentScore(
  768. $stud_id,
  769. $cats[0],
  770. true
  771. );
  772. Category::registerCurrentScore($currentScore, $stud_id, $selectCat);
  773. }
  774. }
  775. }
  776. if (!api_is_allowed_to_edit(null, true)) {
  777. $allowButton = api_get_configuration_value('gradebook_hide_pdf_report_button') === false;
  778. if ($allowButton) {
  779. $actionsLeft .= Display::url(
  780. Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadReportPdf'),
  781. api_get_self().'?action=export_table&'.api_get_cidreq().'&category_id='.$selectCat,
  782. ['class' => 'btn btn-default']
  783. );
  784. }
  785. }
  786. if (isset($first_time) && $first_time == 1 && api_is_allowed_to_edit(null, true)) {
  787. echo '<meta http-equiv="refresh" content="0;url='.api_get_self().'?'.api_get_cidreq().'" />';
  788. } else {
  789. // Tool introduction
  790. Display::display_introduction_section(
  791. TOOL_GRADEBOOK,
  792. ['ToolbarSet' => 'AssessmentsIntroduction']
  793. );
  794. if (!empty($actionsLeft)) {
  795. echo $toolbar = Display::toolbarAction(
  796. 'gradebook-student-actions',
  797. [$actionsLeft]
  798. );
  799. }
  800. if (api_is_allowed_to_edit(null, true)) {
  801. if (((empty($selectCat)) || (isset($_GET['cidReq']) && $_GET['cidReq'] !== '')) ||
  802. (isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'false')
  803. ) {
  804. $cats = Category:: load(
  805. null,
  806. null,
  807. $course_code,
  808. null,
  809. null,
  810. $session_id,
  811. false
  812. );
  813. }
  814. }
  815. $cats = Category::load(
  816. null,
  817. null,
  818. $course_code,
  819. null,
  820. null,
  821. $session_id,
  822. false
  823. );
  824. if (!empty($cats)) {
  825. if ((api_get_setting('gradebook_enable_grade_model') === 'true') &&
  826. (
  827. api_is_platform_admin() || (api_is_allowed_to_edit(null, true) &&
  828. api_get_setting('teachers_can_change_grade_model_settings') === 'true')
  829. )
  830. ) {
  831. // Getting grade models.
  832. $obj = new GradeModel();
  833. $grade_models = $obj->get_all();
  834. $grade_model_id = $cats[0]->get_grade_model_id();
  835. // No children.
  836. if ((count($cats) == 1 && empty($grade_model_id)) ||
  837. (count($cats) == 1 && $grade_model_id != -1)
  838. ) {
  839. if (!empty($grade_models)) {
  840. $form_grade = new FormValidator('grade_model_settings');
  841. $obj->fill_grade_model_select_in_form($form_grade, 'grade_model_id', $grade_model_id);
  842. $form_grade->addButtonSave(get_lang('Save'));
  843. if ($form_grade->validate()) {
  844. $value = $form_grade->exportValue('grade_model_id');
  845. $gradebook = new Gradebook();
  846. $gradebook->update(['id' => $cats[0]->get_id(), 'grade_model_id' => $value], true);
  847. //do something
  848. $obj = new GradeModel();
  849. $components = $obj->get_components($value);
  850. foreach ($components as $component) {
  851. $gradebook = new Gradebook();
  852. $params = [];
  853. $params['name'] = $component['acronym'];
  854. $params['description'] = $component['title'];
  855. $params['user_id'] = api_get_user_id();
  856. $params['parent_id'] = $cats[0]->get_id();
  857. $params['weight'] = $component['percentage'];
  858. $params['session_id'] = api_get_session_id();
  859. $params['course_code'] = api_get_course_id();
  860. $params['grade_model_id'] = api_get_session_id();
  861. $gradebook->save($params);
  862. }
  863. // Reloading cats
  864. $cats = Category:: load(
  865. null,
  866. null,
  867. $course_code,
  868. null,
  869. null,
  870. $session_id,
  871. false
  872. );
  873. } else {
  874. $form_grade->display();
  875. }
  876. }
  877. }
  878. }
  879. $i = 0;
  880. $allcat = [];
  881. $model = ExerciseLib::getCourseScoreModel();
  882. /** @var Category $cat */
  883. foreach ($cats as $cat) {
  884. $allcat = $cat->get_subcategories($stud_id, $course_code, $session_id);
  885. $alleval = $cat->get_evaluations($stud_id, false, $course_code, $session_id);
  886. $alllink = $cat->get_links($stud_id, true, $course_code, $session_id);
  887. if ($cat->get_parent_id() != 0) {
  888. $i++;
  889. } else {
  890. // This is the father
  891. // Create gradebook/add gradebook links.
  892. DisplayGradebook::header(
  893. $cat,
  894. 0,
  895. $cat->get_id(),
  896. $is_course_admin,
  897. $is_platform_admin,
  898. $simple_search_form,
  899. false,
  900. true,
  901. $certificate
  902. );
  903. if (api_is_allowed_to_edit(null, true) &&
  904. api_get_setting('gradebook_enable_grade_model') === 'true'
  905. ) {
  906. // Showing the grading system
  907. if (!empty($grade_models[$grade_model_id])) {
  908. echo Display::return_message(
  909. get_lang('GradeModel').': '.$grade_models[$grade_model_id]['name']
  910. );
  911. }
  912. }
  913. $exportToPdf = false;
  914. if ($action == 'export_table') {
  915. $exportToPdf = true;
  916. }
  917. $loadStats = [];
  918. $teacher = api_is_allowed_to_edit(null, true);
  919. if (!$teacher) {
  920. if (api_get_setting('gradebook_detailed_admin_view') === 'true') {
  921. $loadStats = [1, 2, 3];
  922. } else {
  923. if (api_get_configuration_value('gradebook_enable_best_score') !== false) {
  924. $loadStats = [2];
  925. }
  926. }
  927. }
  928. $gradebookTable = new GradebookTable(
  929. $cat,
  930. $allcat,
  931. $alleval,
  932. $alllink,
  933. $addparams,
  934. $exportToPdf,
  935. null,
  936. null,
  937. [],
  938. $loadStats
  939. );
  940. if (api_is_allowed_to_edit()) {
  941. $gradebookTable->td_attributes = [
  942. 4 => 'class="text-center"',
  943. ];
  944. }
  945. $table = $gradebookTable->return_table();
  946. $allowGraph = api_get_configuration_value('gradebook_hide_graph') === false;
  947. $graph = '';
  948. if ($allowGraph) {
  949. $graph = $gradebookTable->getGraph();
  950. }
  951. if ($action === 'export_table') {
  952. ob_clean();
  953. $params = [
  954. 'pdf_title' => sprintf(get_lang('GradeFromX'), $courseInfo['name']),
  955. 'course_code' => api_get_course_id(),
  956. 'session_info' => '',
  957. 'course_info' => '',
  958. 'pdf_date' => '',
  959. 'student_info' => api_get_user_info(),
  960. 'show_grade_generated_date' => true,
  961. 'show_real_course_teachers' => false,
  962. 'show_teacher_as_myself' => false,
  963. 'orientation' => 'P',
  964. ];
  965. $pdf = new PDF('A4', $params['orientation'], $params);
  966. $pdf->html_to_pdf_with_template(
  967. $table.
  968. $graph.
  969. '<br />'.get_lang('Feedback').'<br />
  970. <textarea rows="5" cols="100" >&nbsp;</textarea>'
  971. );
  972. } else {
  973. echo $table;
  974. echo $graph;
  975. }
  976. }
  977. }
  978. }
  979. }
  980. api_set_in_gradebook();
  981. $contents = ob_get_contents();
  982. ob_end_clean();
  983. $view = new Template($viewTitle);
  984. $view->assign('content', $contents);
  985. $view->display_one_col_template();