grade_models.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true;
  4. require_once __DIR__.'/../inc/global.inc.php';
  5. $this_section = SECTION_PLATFORM_ADMIN;
  6. api_protect_admin_script();
  7. if (api_get_setting('gradebook_enable_grade_model') != 'true') {
  8. api_not_allowed(true);
  9. }
  10. //Add the JS needed to use the jqgrid
  11. $htmlHeadXtra[] = api_get_jqgrid_js();
  12. // setting breadcrumbs
  13. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  14. $action = isset($_GET['action']) ? $_GET['action'] : null;
  15. $check = Security::check_token('request');
  16. $token = Security::get_token();
  17. if ($action === 'add') {
  18. $interbreadcrumb[] = ['url' => 'grade_models.php', 'name' => get_lang('Grading model')];
  19. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')];
  20. } elseif ($action == 'edit') {
  21. $interbreadcrumb[] = ['url' => 'grade_models.php', 'name' => get_lang('Grading model')];
  22. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
  23. } else {
  24. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Grading model')];
  25. }
  26. $htmlHeadXtra[] = '<script>
  27. function plusItem(item) {
  28. if (item != 1) {
  29. document.getElementById(item).style.display = "inline";
  30. document.getElementById("plus-"+item).style.display = "none";
  31. document.getElementById("min-"+(item-1)).style.display = "none";
  32. document.getElementById("min-"+(item)).style.display = "inline";
  33. document.getElementById("plus-"+(item+1)).style.display = "inline";
  34. //document.getElementById("txta-"+(item)).value = "100";
  35. //document.getElementById("txta-"+(item-1)).value = "";
  36. $("input").removeClass("form-control");
  37. }
  38. }
  39. function minItem(item) {
  40. if (item != 1) {
  41. document.getElementById(item).style.display = "none";
  42. //document.getElementById("txta-"+item).value = "";
  43. //document.getElementById("txtb-"+item).value = "";
  44. document.getElementById("plus-"+item).style.display = "inline";
  45. document.getElementById("min-"+(item-1)).style.display = "inline";
  46. //document.getElementById("txta-"+(item-1)).value = "100";
  47. $("input").removeClass("form-control");
  48. }
  49. if (item = 1) {
  50. document.getElementById("min-"+(item)).style.display = "none";
  51. $("input").removeClass("form-control");
  52. }
  53. }
  54. </script>';
  55. Display::display_header();
  56. //jqgrid will use this URL to do the selects
  57. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_grade_models';
  58. //The order is important you need to check the the $column variable in the model.ajax.php file
  59. $columns = [
  60. get_lang('Name'),
  61. get_lang('Description'),
  62. get_lang('Detail'),
  63. ];
  64. //Column config
  65. $column_model = [
  66. [
  67. 'name' => 'name',
  68. 'index' => 'name',
  69. 'width' => '80',
  70. 'align' => 'left',
  71. ],
  72. [
  73. 'name' => 'description',
  74. 'index' => 'description',
  75. 'width' => '500',
  76. 'align' => 'left',
  77. 'sortable' => 'false',
  78. ],
  79. [
  80. 'name' => 'actions',
  81. 'index' => 'actions',
  82. 'width' => '100',
  83. 'align' => 'left',
  84. 'formatter' => 'action_formatter',
  85. 'sortable' => 'false',
  86. ],
  87. ];
  88. //Autowidth
  89. $extra_params['autowidth'] = 'true';
  90. //height auto
  91. $extra_params['height'] = 'auto';
  92. //With this function we can add actions to the jgrid (edit, delete, etc)
  93. $action_links = 'function action_formatter(cellvalue, options, rowObject) {
  94. return \'<a href="?action=edit&id=\'+options.rowId+\'">'.Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>'.
  95. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("Please confirm your choice"), ENT_QUOTES))."\'".')) return false;" href="?sec_token='.$token.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>'.
  96. '\';
  97. }';
  98. ?>
  99. <script>
  100. $(function() {
  101. <?php
  102. // grid definition see the $obj->display() function
  103. echo Display::grid_js(
  104. 'grade_model',
  105. $url,
  106. $columns,
  107. $column_model,
  108. $extra_params,
  109. [],
  110. $action_links,
  111. true
  112. );
  113. ?>
  114. });
  115. </script>
  116. <?php
  117. $obj = new GradeModel();
  118. // Action handling: Add
  119. switch ($action) {
  120. case 'add':
  121. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  122. api_not_allowed();
  123. }
  124. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']);
  125. $form = $obj->return_form($url, 'add');
  126. // The validation or display
  127. if ($form->validate()) {
  128. if ($check) {
  129. $values = $form->exportValues();
  130. $res = $obj->save($values);
  131. if ($res) {
  132. echo Display::return_message(get_lang('Item added'), 'confirm');
  133. }
  134. }
  135. $obj->display();
  136. } else {
  137. echo '<div class="actions">';
  138. echo '<a href="'.api_get_self().'">'.
  139. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).
  140. '</a>';
  141. echo '</div>';
  142. $form->addElement('hidden', 'sec_token');
  143. $form->setConstants(['sec_token' => $token]);
  144. $form->display();
  145. }
  146. break;
  147. case 'edit':
  148. // Action handling: Editing
  149. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']);
  150. $form = $obj->return_form($url, 'edit');
  151. // The validation or display
  152. if ($form->validate()) {
  153. if ($check) {
  154. $values = $form->exportValues();
  155. $res = $obj->update($values);
  156. echo Display::return_message(get_lang('Item updated'), 'confirm', false);
  157. }
  158. $obj->display();
  159. } else {
  160. echo '<div class="actions">';
  161. echo '<a href="'.api_get_self().'">'.
  162. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).
  163. '</a>';
  164. echo '</div>';
  165. $form->addElement('hidden', 'sec_token');
  166. $form->setConstants(['sec_token' => $token]);
  167. $form->display();
  168. }
  169. break;
  170. case 'delete':
  171. // Action handling: delete
  172. if ($check) {
  173. $res = $obj->delete($_GET['id']);
  174. if ($res) {
  175. echo Display::return_message(get_lang('Item deleted'), 'confirm');
  176. }
  177. }
  178. $obj->display();
  179. break;
  180. default:
  181. $obj->display();
  182. break;
  183. }
  184. echo '<script>
  185. $(function () {
  186. $("input").removeClass("form-control");
  187. });
  188. </script>';
  189. Display::display_footer();