gradebook_edit_all.php 9.2 KB

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