gradebook_add_link.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. require_once '../forum/forumfunction.inc.php';
  9. $current_course_tool = TOOL_GRADEBOOK;
  10. api_protect_course_script(true);
  11. api_block_anonymous_users();
  12. GradebookUtils::block_students();
  13. $courseCode = isset($_GET['course_code']) ? Security::remove_XSS($_GET['course_code']) : null;
  14. $selectCat = isset($_GET['selectcat']) ? (int) $_GET['selectcat'] : 0;
  15. $course_info = api_get_course_info($courseCode);
  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
  91. c_id = '.$course_info['real_id'].' AND
  92. thread_id = '.$addvalues['select_link'];
  93. $res1 = Database::query($sql1);
  94. $rowtit = Database::fetch_row($res1);
  95. $course_id = api_get_course_id();
  96. $sql_l = 'SELECT count(*) FROM '.$tbl_link.'
  97. WHERE
  98. ref_id='.$addvalues['select_link'].' AND
  99. course_code="'.$course_id.'" AND
  100. type = 5;';
  101. $res_l = Database::query($sql_l);
  102. $row = Database::fetch_row($res_l);
  103. if ($row[0] == 0) {
  104. $link->add();
  105. $sql = 'UPDATE '.$tbl_forum_thread.' SET
  106. thread_qualify_max= "'.api_float_val($addvalues['weight']).'",
  107. thread_weight= "'.api_float_val($addvalues['weight']).'",
  108. thread_title_qualify = "'.$rowtit[0].'"
  109. WHERE
  110. thread_id='.$addvalues['select_link'].' AND
  111. c_id = '.$course_info['real_id'].' ';
  112. Database::query($sql);
  113. }
  114. }
  115. $link->add();
  116. $addvalue_result = !empty($addvalues['addresult']) ? $addvalues['addresult'] : array();
  117. if ($addvalue_result == 1) {
  118. header('Location: gradebook_add_result.php?selecteval='.$link->get_ref_id().'&'.api_get_cidreq());
  119. exit;
  120. } else {
  121. header('Location: '.Security::remove_XSS($_SESSION['gradebook_dest']).'?linkadded=&selectcat='.$selectCat.'&'.api_get_cidreq());
  122. exit;
  123. }
  124. }
  125. }
  126. $interbreadcrumb[] = array(
  127. 'url' => $_SESSION['gradebook_dest'].'?selectcat='.$selectCat.'&'.api_get_cidreq(),
  128. 'name' => get_lang('Gradebook')
  129. );
  130. $this_section = SECTION_COURSES;
  131. $htmlHeadXtra[] = '<script>
  132. $(document).ready( function() {
  133. $("#hide_category_id").change(function() {
  134. $("#hide_category_id option:selected").each(function () {
  135. var cat_id = $(this).val();
  136. $.ajax({
  137. url: "'.api_get_path(WEB_AJAX_PATH).'gradebook.ajax.php?a=get_gradebook_weight",
  138. data: "cat_id="+cat_id,
  139. success: function(return_value) {
  140. if (return_value != 0 ) {
  141. $("#max_weight").html(return_value);
  142. }
  143. }
  144. });
  145. });
  146. });
  147. });
  148. </script>';
  149. Display::display_header(get_lang('MakeLink'));
  150. if (isset($typeform)) {
  151. echo Display::return_message(get_lang('LearningPathGradebookWarning'), 'warning');
  152. $typeform->display();
  153. }
  154. if (isset($addform)) {
  155. $addform->display();
  156. }
  157. Display::display_footer();