gradebook_add_link.php 6.5 KB

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