gradebook_edit_all.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. * @author Julio Montoya - fixes in order to use gradebook models + some code cleaning
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_COURSES;
  11. $current_course_tool = TOOL_GRADEBOOK;
  12. api_protect_course_script(true);
  13. api_block_anonymous_users();
  14. if (!api_is_allowed_to_edit()) {
  15. header('Location: /index.php');
  16. exit;
  17. }
  18. $my_selectcat = isset($_GET['selectcat']) ? intval($_GET['selectcat']) : '';
  19. if (empty($my_selectcat)) {
  20. api_not_allowed();
  21. }
  22. $course_id = GradebookUtils::get_course_id_by_link_id($my_selectcat);
  23. $table_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  24. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  25. $tbl_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  26. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  27. $table_evaluated[LINK_EXERCISE] = array(
  28. TABLE_QUIZ_TEST,
  29. 'title',
  30. 'id',
  31. get_lang('Exercise'),
  32. );
  33. $table_evaluated[LINK_DROPBOX] = array(
  34. TABLE_DROPBOX_FILE,
  35. 'name',
  36. 'id',
  37. get_lang('Dropbox'),
  38. );
  39. $table_evaluated[LINK_STUDENTPUBLICATION] = array(
  40. TABLE_STUDENT_PUBLICATION,
  41. 'url',
  42. 'id',
  43. get_lang('Student_publication'),
  44. );
  45. $table_evaluated[LINK_LEARNPATH] = array(
  46. TABLE_LP_MAIN,
  47. 'name',
  48. 'id',
  49. get_lang('Learnpath'),
  50. );
  51. $table_evaluated[LINK_FORUM_THREAD] = array(
  52. TABLE_FORUM_THREAD,
  53. 'thread_title_qualify',
  54. 'thread_id',
  55. get_lang('Forum'),
  56. );
  57. $table_evaluated[LINK_ATTENDANCE] = array(
  58. TABLE_ATTENDANCE,
  59. 'attendance_title_qualify',
  60. 'id',
  61. get_lang('Attendance'),
  62. );
  63. $table_evaluated[LINK_SURVEY] = array(
  64. TABLE_SURVEY,
  65. 'code',
  66. 'survey_id',
  67. get_lang('Survey'),
  68. );
  69. $submitted = isset($_POST['submitted']) ? $_POST['submitted'] : '';
  70. if ($submitted == 1) {
  71. Display::addFlash(Display::return_message(get_lang('GradebookWeightUpdated')));
  72. if (isset($_POST['evaluation'])) {
  73. $eval_log = new Evaluation();
  74. }
  75. }
  76. $output = '';
  77. $my_cat = Category::load($my_selectcat);
  78. $my_cat = $my_cat[0];
  79. $parent_id = $my_cat->get_parent_id();
  80. $parent_cat = Category::load($parent_id);
  81. $my_category = array();
  82. $cat = new Category();
  83. $my_category = $cat->shows_all_information_an_category($my_selectcat);
  84. $original_total = $my_category['weight'];
  85. $masked_total = $parent_cat[0]->get_weight();
  86. $sql = 'SELECT * FROM '.$table_link.' WHERE category_id = '.$my_selectcat;
  87. $result = Database::query($sql);
  88. $links = Database::store_result($result, 'ASSOC');
  89. foreach ($links as &$row) {
  90. $item_weight = $row['weight'];
  91. $sql = 'SELECT * FROM '.GradebookUtils::get_table_type_course($row['type']).'
  92. WHERE c_id = '.$course_id.' AND '.$table_evaluated[$row['type']][2].' = '.$row['ref_id'];
  93. $result = Database::query($sql);
  94. $resource_name = Database::fetch_array($result);
  95. if (isset($resource_name['lp_type'])) {
  96. $resource_name = $resource_name[4];
  97. } else {
  98. $resource_name = $resource_name[3];
  99. }
  100. $row['resource_name'] = $resource_name;
  101. // Update only if value changed
  102. if (isset($_POST['link'][$row['id']])) {
  103. $new_weight = trim($_POST['link'][$row['id']]);
  104. GradebookUtils::updateLinkWeight(
  105. $row['id'],
  106. $resource_name,
  107. $new_weight
  108. );
  109. $item_weight = $new_weight;
  110. }
  111. $output .= '<tr><td>'.GradebookUtils::build_type_icon_tag($row['type']).'</td>
  112. <td> '.$resource_name.' '.Display::label(
  113. $table_evaluated[$row['type']][3],
  114. 'info'
  115. ).' </td>';
  116. $output .= '<td>
  117. <input type="hidden" name="link_'.$row['id'].'" value="'.$resource_name.'" />
  118. <input size="10" type="text" name="link['.$row['id'].']" value="'.$item_weight.'"/>
  119. </td></tr>';
  120. }
  121. $sql = 'SELECT * FROM '.$table_evaluation.' WHERE category_id = '.$my_selectcat;
  122. $result = Database::query($sql);
  123. $evaluations = Database::store_result($result);
  124. foreach ($evaluations as $evaluationRow) {
  125. $item_weight = $evaluationRow['weight'];
  126. // update only if value changed
  127. if (isset($_POST['evaluation'][$evaluationRow['id']])) {
  128. $new_weight = trim($_POST['evaluation'][$evaluationRow['id']]);
  129. GradebookUtils::updateEvaluationWeight(
  130. $evaluationRow['id'],
  131. $new_weight
  132. );
  133. $item_weight = $new_weight;
  134. }
  135. $output .= '<tr>
  136. <td>'.GradebookUtils::build_type_icon_tag('evalnotempty').'</td>
  137. <td>'.$evaluationRow['name'].' '.Display::label(
  138. get_lang('Evaluation')
  139. ).'</td>';
  140. $output .= '<td>
  141. <input type="hidden" name="eval_'.$evaluationRow['id'].'" value="'.$evaluationRow['name'].'" />
  142. <input type="text" size="10" name="evaluation['.$evaluationRow['id'].']" value="'.$item_weight.'"/>
  143. </td></tr>';
  144. }
  145. $my_api_cidreq = api_get_cidreq();
  146. $currentUrl = api_get_self().'?'.api_get_cidreq().'&selectcat='.$my_selectcat;
  147. $form = new FormValidator('auto_weight', 'post', $currentUrl);
  148. $form->addHeader(get_lang('AutoWeight'));
  149. $form->addLabel(null, get_lang('AutoWeightExplanation'));
  150. $form->addButtonUpdate(get_lang('AutoWeight'));
  151. if ($form->validate()) {
  152. $itemCount = count($links) + count($evaluations);
  153. $weight = round($original_total / $itemCount, 2);
  154. $total = $weight * $itemCount;
  155. $diff = null;
  156. if ($original_total !== $total) {
  157. if ($total > $original_total) {
  158. $diff = $total - $original_total;
  159. }
  160. }
  161. $total = 0;
  162. $diffApplied = false;
  163. foreach ($links as $link) {
  164. $weightToApply = $weight;
  165. if ($diffApplied == false) {
  166. if (!empty($diff)) {
  167. $weightToApply = $weight - $diff;
  168. $diffApplied = true;
  169. }
  170. }
  171. GradebookUtils::updateLinkWeight(
  172. $link['id'],
  173. $link['resource_name'],
  174. $weightToApply
  175. );
  176. }
  177. foreach ($evaluations as $evaluation) {
  178. $weightToApply = $weight;
  179. if ($diffApplied == false) {
  180. if (!empty($diff)) {
  181. $weightToApply = $weight - $diff;
  182. $diffApplied = true;
  183. }
  184. }
  185. GradebookUtils::updateEvaluationWeight(
  186. $evaluation['id'],
  187. $weightToApply
  188. );
  189. }
  190. header('Location:'.$currentUrl);
  191. exit;
  192. }
  193. // DISPLAY HEADERS AND MESSAGES
  194. if (!isset($_GET['exportpdf']) and !isset($_GET['export_certificate'])) {
  195. if (isset ($_GET['studentoverview'])) {
  196. $interbreadcrumb[] = array(
  197. 'url' => Security::remove_XSS(
  198. $_SESSION['gradebook_dest']
  199. ).'?selectcat='.$my_selectcat,
  200. 'name' => get_lang('Gradebook'),
  201. );
  202. Display:: display_header(get_lang('FlatView'));
  203. } elseif (isset ($_GET['search'])) {
  204. $interbreadcrumb[] = array(
  205. 'url' => Security::remove_XSS(
  206. $_SESSION['gradebook_dest']
  207. ).'?selectcat='.$my_selectcat,
  208. 'name' => get_lang('Gradebook'),
  209. );
  210. Display:: display_header(get_lang('SearchResults'));
  211. } else {
  212. $interbreadcrumb[] = array(
  213. 'url' => Security::remove_XSS(
  214. $_SESSION['gradebook_dest']
  215. ).'?selectcat=1',
  216. 'name' => get_lang('Gradebook'),
  217. );
  218. $interbreadcrumb[] = array(
  219. 'url' => '#',
  220. 'name' => get_lang('EditAllWeights'),
  221. );
  222. Display:: display_header('');
  223. }
  224. }
  225. ?>
  226. <div class="actions">
  227. <a href="<?php echo Security::remove_XSS(
  228. $_SESSION['gradebook_dest']
  229. ).'?'.$my_api_cidreq ?>&selectcat=<?php echo $my_selectcat ?>">
  230. <?php echo Display::return_icon(
  231. 'back.png',
  232. get_lang('FolderView'),
  233. '',
  234. ICON_SIZE_MEDIUM
  235. ); ?>
  236. </a>
  237. </div>
  238. <?php
  239. $form->display();
  240. $formNormal = new FormValidator('normal_weight', 'post', $currentUrl);
  241. $formNormal->addHeader(get_lang('EditWeight'));
  242. $formNormal->display();
  243. $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $original_total);
  244. echo Display::return_message($warning_message, 'warning', false);
  245. ?>
  246. <form method="post"
  247. action="gradebook_edit_all.php?<?php echo $my_api_cidreq ?>&selectcat=<?php echo $my_selectcat ?>">
  248. <table class="data_table">
  249. <tr class="row_odd">
  250. <th style="width: 35px;"><?php echo get_lang('Type'); ?></th>
  251. <th><?php echo get_lang('Resource'); ?></th>
  252. <th><?php echo get_lang('Weight'); ?></th>
  253. </tr>
  254. <?php echo $output; ?>
  255. </table>
  256. <input type="hidden" name="submitted" value="1"/>
  257. <br/>
  258. <button class="btn btn-primary" type="submit" name="name"
  259. value="<?php echo get_lang('Save') ?>">
  260. <?php echo get_lang('SaveScoringRules') ?>
  261. </button>
  262. </form>
  263. <?php
  264. Display:: display_footer();