gradebook_edit_all.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. GradebookUtils::block_students();
  15. $my_selectcat = isset($_GET['selectcat']) ? intval($_GET['selectcat']) : '';
  16. if (empty($my_selectcat)) {
  17. api_not_allowed(true);
  18. }
  19. $course_id = GradebookUtils::get_course_id_by_link_id($my_selectcat);
  20. $table_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  21. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  22. $tbl_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD);
  23. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  24. $table_evaluated = GradebookUtils::getEvaluateList();
  25. $submitted = isset($_POST['submitted']) ? $_POST['submitted'] : '';
  26. if ($submitted == 1) {
  27. Display::addFlash(Display::return_message(get_lang('GradebookWeightUpdated')));
  28. if (isset($_POST['evaluation'])) {
  29. $eval_log = new Evaluation();
  30. }
  31. }
  32. $output = '';
  33. $my_cat = Category::load($my_selectcat);
  34. $my_cat = $my_cat[0];
  35. $parent_id = $my_cat->get_parent_id();
  36. $parent_cat = Category::load($parent_id);
  37. $my_category = array();
  38. $cat = new Category();
  39. $my_category = $cat->showAllCategoryInfo($my_selectcat);
  40. $original_total = $my_category['weight'];
  41. $masked_total = $parent_cat[0]->get_weight();
  42. $sql = 'SELECT * FROM '.$table_link.' WHERE category_id = '.$my_selectcat;
  43. $result = Database::query($sql);
  44. $links = Database::store_result($result, 'ASSOC');
  45. foreach ($links as &$row) {
  46. $item_weight = $row['weight'];
  47. $sql = 'SELECT * FROM '.GradebookUtils::get_table_type_course($row['type']).'
  48. WHERE c_id = '.$course_id.' AND '.$table_evaluated[$row['type']][2].' = '.$row['ref_id'];
  49. $result = Database::query($sql);
  50. $resource_name = Database::fetch_array($result);
  51. if (isset($resource_name['lp_type'])) {
  52. $resource_name = $resource_name[4];
  53. } else {
  54. $resource_name = $resource_name[3];
  55. }
  56. $row['resource_name'] = $resource_name;
  57. // Update only if value changed
  58. if (isset($_POST['link'][$row['id']])) {
  59. $new_weight = trim($_POST['link'][$row['id']]);
  60. GradebookUtils::updateLinkWeight(
  61. $row['id'],
  62. $resource_name,
  63. $new_weight
  64. );
  65. $item_weight = $new_weight;
  66. }
  67. $output .= '<tr><td>'.GradebookUtils::build_type_icon_tag($row['type']).'</td>
  68. <td> '.$resource_name.' '.
  69. Display::label(
  70. $table_evaluated[$row['type']][3],
  71. 'info'
  72. ).' </td>';
  73. $output .= '<td>
  74. <input type="hidden" name="link_'.$row['id'].'" value="'.$resource_name.'" />
  75. <input size="10" type="text" name="link['.$row['id'].']" value="'.$item_weight.'"/>
  76. </td></tr>';
  77. }
  78. $sql = 'SELECT * FROM '.$table_evaluation.' WHERE category_id = '.$my_selectcat;
  79. $result = Database::query($sql);
  80. $evaluations = Database::store_result($result);
  81. foreach ($evaluations as $evaluationRow) {
  82. $item_weight = $evaluationRow['weight'];
  83. // update only if value changed
  84. if (isset($_POST['evaluation'][$evaluationRow['id']])) {
  85. $new_weight = trim($_POST['evaluation'][$evaluationRow['id']]);
  86. GradebookUtils::updateEvaluationWeight(
  87. $evaluationRow['id'],
  88. $new_weight
  89. );
  90. $item_weight = $new_weight;
  91. }
  92. $output .= '<tr>
  93. <td>'.GradebookUtils::build_type_icon_tag('evalnotempty').'</td>
  94. <td>'.$evaluationRow['name'].' '.Display::label(
  95. get_lang('Evaluation')
  96. ).'</td>';
  97. $output .= '<td>
  98. <input type="hidden" name="eval_'.$evaluationRow['id'].'" value="'.$evaluationRow['name'].'" />
  99. <input type="text" size="10" name="evaluation['.$evaluationRow['id'].']" value="'.$item_weight.'"/>
  100. </td></tr>';
  101. }
  102. $my_api_cidreq = api_get_cidreq();
  103. $currentUrl = api_get_self().'?'.api_get_cidreq().'&selectcat='.$my_selectcat;
  104. $form = new FormValidator('auto_weight', 'post', $currentUrl);
  105. $form->addHeader(get_lang('AutoWeight'));
  106. $form->addLabel(null, get_lang('AutoWeightExplanation'));
  107. $form->addButtonUpdate(get_lang('AutoWeight'));
  108. if ($form->validate()) {
  109. $itemCount = count($links) + count($evaluations);
  110. $weight = round($original_total / $itemCount, 2);
  111. $total = $weight * $itemCount;
  112. $diff = null;
  113. if ($original_total !== $total) {
  114. if ($total > $original_total) {
  115. $diff = $total - $original_total;
  116. }
  117. }
  118. $total = 0;
  119. $diffApplied = false;
  120. foreach ($links as $link) {
  121. $weightToApply = $weight;
  122. if ($diffApplied == false) {
  123. if (!empty($diff)) {
  124. $weightToApply = $weight - $diff;
  125. $diffApplied = true;
  126. }
  127. }
  128. GradebookUtils::updateLinkWeight(
  129. $link['id'],
  130. $link['resource_name'],
  131. $weightToApply
  132. );
  133. }
  134. foreach ($evaluations as $evaluation) {
  135. $weightToApply = $weight;
  136. if ($diffApplied == false) {
  137. if (!empty($diff)) {
  138. $weightToApply = $weight - $diff;
  139. $diffApplied = true;
  140. }
  141. }
  142. GradebookUtils::updateEvaluationWeight(
  143. $evaluation['id'],
  144. $weightToApply
  145. );
  146. }
  147. header('Location:'.$currentUrl);
  148. exit;
  149. }
  150. // DISPLAY HEADERS AND MESSAGES
  151. if (!isset($_GET['exportpdf']) and !isset($_GET['export_certificate'])) {
  152. if (isset($_GET['studentoverview'])) {
  153. $interbreadcrumb[] = array(
  154. 'url' => Category::getUrl().'selectcat='.$my_selectcat,
  155. 'name' => get_lang('Gradebook'),
  156. );
  157. Display:: display_header(get_lang('FlatView'));
  158. } elseif (isset($_GET['search'])) {
  159. $interbreadcrumb[] = array(
  160. 'url' => Category::getUrl().'selectcat='.$my_selectcat,
  161. 'name' => get_lang('Gradebook'),
  162. );
  163. Display:: display_header(get_lang('SearchResults'));
  164. } else {
  165. $interbreadcrumb[] = array(
  166. 'url' => Category::getUrl().'selectcat=1',
  167. 'name' => get_lang('Gradebook'),
  168. );
  169. $interbreadcrumb[] = array(
  170. 'url' => '#',
  171. 'name' => get_lang('EditAllWeights'),
  172. );
  173. Display:: display_header('');
  174. }
  175. }
  176. ?>
  177. <div class="actions">
  178. <a href="<?php echo Category::getUrl(); ?>selectcat=<?php echo $my_selectcat ?>">
  179. <?php echo Display::return_icon(
  180. 'back.png',
  181. get_lang('FolderView'),
  182. '',
  183. ICON_SIZE_MEDIUM
  184. ); ?>
  185. </a>
  186. </div>
  187. <?php
  188. $form->display();
  189. $formNormal = new FormValidator('normal_weight', 'post', $currentUrl);
  190. $formNormal->addHeader(get_lang('EditWeight'));
  191. $formNormal->display();
  192. $warning_message = sprintf(get_lang('TotalWeightMustBeX'), $original_total);
  193. echo Display::return_message($warning_message, 'warning', false);
  194. ?>
  195. <form method="post"
  196. action="gradebook_edit_all.php?<?php echo $my_api_cidreq ?>&selectcat=<?php echo $my_selectcat ?>">
  197. <table class="data_table">
  198. <tr class="row_odd">
  199. <th style="width: 35px;"><?php echo get_lang('Type'); ?></th>
  200. <th><?php echo get_lang('Resource'); ?></th>
  201. <th><?php echo get_lang('Weight'); ?></th>
  202. </tr>
  203. <?php echo $output; ?>
  204. </table>
  205. <input type="hidden" name="submitted" value="1"/>
  206. <br/>
  207. <button class="btn btn-primary" type="submit" name="name"
  208. value="<?php echo get_lang('Save') ?>">
  209. <?php echo get_lang('SaveScoringRules') ?>
  210. </button>
  211. </form>
  212. <?php
  213. Display:: display_footer();