gradebook_add_link.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. require_once __DIR__.'/../forum/forumfunction.inc.php';
  8. $current_course_tool = TOOL_GRADEBOOK;
  9. api_protect_course_script(true);
  10. api_block_anonymous_users();
  11. GradebookUtils::block_students();
  12. $courseCode = isset($_GET['course_code']) ? Security::remove_XSS($_GET['course_code']) : null;
  13. $selectCat = isset($_GET['selectcat']) ? (int) $_GET['selectcat'] : 0;
  14. $course_info = api_get_course_info($courseCode);
  15. $em = Database::getManager();
  16. $tbl_forum_thread = Database :: get_course_table(TABLE_FORUM_THREAD);
  17. $tbl_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  18. $session_id = api_get_session_id();
  19. $typeSelected = isset($_GET['typeselected']) ? intval($_GET['typeselected']) : null;
  20. if ($session_id == 0) {
  21. $all_categories = Category::load(
  22. null,
  23. null,
  24. api_get_course_id(),
  25. null,
  26. null,
  27. $session_id
  28. );
  29. } else {
  30. $all_categories = Category::load_session_categories(null, $session_id);
  31. }
  32. $category = Category :: load($selectCat);
  33. $url = api_get_self().'?selectcat='.$selectCat.'&newtypeselected='.$typeSelected.'&course_code='.api_get_course_id().'&'.api_get_cidreq();
  34. $typeform = new LinkForm(
  35. LinkForm :: TYPE_CREATE,
  36. $category[0],
  37. null,
  38. 'create_link',
  39. null,
  40. $url,
  41. $typeSelected
  42. );
  43. // if user selected a link type
  44. if ($typeform->validate() && isset($_GET['newtypeselected'])) {
  45. // reload page, this time with a parameter indicating the selected type
  46. header('Location: '.api_get_self().'?selectcat='.$selectCat
  47. .'&typeselected='.$typeform->exportValue('select_link')
  48. .'&course_code='.Security::remove_XSS($_GET['course_code']).'&'.api_get_cidreq()
  49. );
  50. exit;
  51. }
  52. // link type selected, show 2nd form to retrieve the link data
  53. if (isset($typeSelected) && $typeSelected != '0') {
  54. $url = api_get_self().'?selectcat='.$selectCat.'&typeselected='.$typeSelected.'&course_code='.$courseCode.'&'.api_get_cidreq();
  55. $addform = new LinkAddEditForm(
  56. LinkAddEditForm :: TYPE_ADD,
  57. $all_categories,
  58. $typeSelected,
  59. null,
  60. 'add_link',
  61. $url
  62. );
  63. if ($addform->validate()) {
  64. $addvalues = $addform->exportValues();
  65. $link = LinkFactory::create($typeSelected);
  66. $link->set_user_id(api_get_user_id());
  67. $link->set_course_code(api_get_course_id());
  68. $link->set_category_id($addvalues['select_gradebook']);
  69. if ($link->needs_name_and_description()) {
  70. $link->set_name($addvalues['name']);
  71. } else {
  72. $link->set_ref_id($addvalues['select_link']);
  73. }
  74. $parent_cat = Category::load($addvalues['select_gradebook']);
  75. $global_weight = $category[0]->get_weight();
  76. $link->set_weight($addvalues['weight_mask']);
  77. if ($link->needs_max()) {
  78. $link->set_max($addvalues['max']);
  79. }
  80. if ($link->needs_name_and_description()) {
  81. $link->set_description($addvalues['description']);
  82. }
  83. $link->set_visible(empty($addvalues['visible']) ? 0 : 1);
  84. // Update view_properties
  85. if (isset($typeSelected) &&
  86. 5 == $typeSelected &&
  87. (isset($addvalues['select_link']) && $addvalues['select_link'] <> "")
  88. ) {
  89. $sql1 = 'SELECT thread_title from '.$tbl_forum_thread.'
  90. WHERE c_id = '.$course_info['real_id'].' AND thread_id='.$addvalues['select_link'];
  91. $res1 = Database::query($sql1);
  92. $rowtit = Database::fetch_row($res1);
  93. $course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
  94. $countLinks = $em
  95. ->getRepository('ChamiloCoreBundle:GradebookLink')
  96. ->getLinksByCourseAndReferenceAndType($course, $addvalues['select_link'], 5);
  97. if ($countLinks->count() === 0) {
  98. $link->add();
  99. $sql = 'UPDATE '.$tbl_forum_thread.' SET
  100. thread_qualify_max='.$addvalues['weight'].',
  101. thread_weight='.$addvalues['weight'].',
  102. thread_title_qualify="'.$rowtit[0].'"
  103. WHERE thread_id='.$addvalues['select_link'].' AND c_id = '.$course_info['real_id'].' ';
  104. Database::query($sql);
  105. }
  106. }
  107. $link->add();
  108. $addvalue_result = !empty($addvalues['addresult']) ? $addvalues['addresult'] : array();
  109. if ($addvalue_result == 1) {
  110. header('Location: gradebook_add_result.php?selecteval='.$link->get_ref_id().'&'.api_get_cidreq());
  111. exit;
  112. } else {
  113. header('Location: '.Security::remove_XSS($_SESSION['gradebook_dest']).'?linkadded=&selectcat='.$selectCat.'&'.api_get_cidreq());
  114. exit;
  115. }
  116. }
  117. }
  118. $interbreadcrumb[] = array(
  119. 'url' => $_SESSION['gradebook_dest'].'?selectcat='.$selectCat.'&'.api_get_cidreq(),
  120. 'name' => get_lang('Gradebook')
  121. );
  122. $this_section = SECTION_COURSES;
  123. $htmlHeadXtra[] = '<script>
  124. $(document).ready( function() {
  125. $("#hide_category_id").change(function() {
  126. $("#hide_category_id option:selected").each(function () {
  127. var cat_id = $(this).val();
  128. $.ajax({
  129. url: "'.api_get_path(WEB_AJAX_PATH).'gradebook.ajax.php?a=get_gradebook_weight",
  130. data: "cat_id="+cat_id,
  131. success: function(return_value) {
  132. if (return_value != 0 ) {
  133. $("#max_weight").html(return_value);
  134. }
  135. }
  136. });
  137. });
  138. });
  139. });
  140. </script>';
  141. Display::display_header(get_lang('MakeLink'));
  142. if (isset($typeform)) {
  143. echo Display::return_message(get_lang('LearningPathGradebookWarning'), 'warning');
  144. $typeform->display();
  145. }
  146. if (isset($addform)) {
  147. $addform->display();
  148. }
  149. Display::display_footer();