gradebook_edit_all.php 8.9 KB

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