PageRenderTime 44ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/_standard/gradebook/edit_marks.php

https://github.com/harriswong/ATutor
PHP | 426 lines | 334 code | 65 blank | 27 comment | 58 complexity | c6c6b1226d31532d228f62dc29a9c45d MD5 | raw file
  1. <?php
  2. /****************************************************************/
  3. /* ATutor */
  4. /****************************************************************/
  5. /* Copyright (c) 2002-2010 */
  6. /* Inclusive Design Institute */
  7. /* http://atutor.ca */
  8. /* */
  9. /* This program is free software. You can redistribute it and/or*/
  10. /* modify it under the terms of the GNU General Public License */
  11. /* as published by the Free Software Foundation. */
  12. /****************************************************************/
  13. // $Id$
  14. $page = "gradebook";
  15. define('AT_INCLUDE_PATH', '../../../include/');
  16. require(AT_INCLUDE_PATH.'vitals.inc.php');
  17. authenticate(AT_PRIV_GRADEBOOK);
  18. require_once("lib/gradebook.inc.php");
  19. if (isset($_GET['cancel']))
  20. {
  21. $msg->addFeedback('CANCELLED');
  22. header('Location: gradebook_tests.php');
  23. exit;
  24. }
  25. else if (isset($_GET['save']))
  26. {
  27. foreach($_GET as $key => $value)
  28. {
  29. $value = $addslashes($value);
  30. if (preg_match('/^grade_(.*)_(.*)$/', $key, $matches) > 0)
  31. {
  32. $matches[1] = intval($matches[1]);
  33. $matches[2] = intval($matches[2]);
  34. $sql = "SELECT grade_scale_id FROM ".TABLE_PREFIX."gradebook_tests WHERE gradebook_test_id = ". $matches[1];
  35. $result = mysql_query($sql, $db) or die(mysql_error());
  36. $row = mysql_fetch_assoc($result);
  37. $sql = "REPLACE ".TABLE_PREFIX."gradebook_detail SET gradebook_test_id = ". $matches[1].", member_id=". $matches[2].", grade='".get_mark_by_grade($row["grade_scale_id"], $value)."'";
  38. $result = mysql_query($sql, $db) or die(mysql_error());
  39. }
  40. }
  41. }
  42. $orders = array('asc' => 'desc', 'desc' => 'asc');
  43. if (isset($_GET['asc']))
  44. {
  45. $order = 'asc';
  46. $order_col = $addslashes($_GET['asc']);
  47. }
  48. else if (isset($_GET['desc'])) {
  49. $order = 'desc';
  50. $order_col = $addslashes($_GET['desc']);
  51. } else {
  52. // no order set
  53. $order = 'asc';
  54. $order_col = 'name';
  55. }
  56. if ($_GET['reset_filter']) {
  57. unset($_GET);
  58. }
  59. // Initialize all applicable tests array and all enrolled students array
  60. $all_tests = array();
  61. $all_students = array();
  62. // generate test array
  63. $sql = "(SELECT g.gradebook_test_id, g.id, g.type, t.title".
  64. " FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."tests t".
  65. " WHERE g.type='ATutor Test'".
  66. " AND g.id = t.test_id".
  67. " AND t.course_id=".$_SESSION["course_id"]." ORDER BY title)".
  68. " UNION (SELECT g.gradebook_test_id, g.id, g.type, a.title".
  69. " FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."assignments a".
  70. " WHERE g.type='ATutor Assignment'".
  71. " AND g.id = a.assignment_id".
  72. " AND a.course_id=".$_SESSION["course_id"]." ORDER BY title)".
  73. " UNION (SELECT gradebook_test_id, id, type, title".
  74. " FROM ".TABLE_PREFIX."gradebook_tests".
  75. " WHERE course_id=".$_SESSION["course_id"]." ORDER BY title)";
  76. $result = mysql_query($sql, $db) or die(mysql_error());
  77. while ($row = mysql_fetch_assoc($result))
  78. {
  79. $no_error = true;
  80. if($row["type"]=="ATutor Test")
  81. {
  82. $studs_take_num = get_studs_take_more_than_once($_SESSION["course_id"], $row["id"]);
  83. foreach ($studs_take_num as $member_id => $num)
  84. {
  85. if ($no_error) $no_error = false;
  86. $error_msg .= get_display_name($member_id) . ": " . $num . " times<br>";
  87. }
  88. if (!$no_error)
  89. {
  90. $f = array('ADD_TEST_INTO_GRADEBOOK',
  91. $row['title'],
  92. $error_msg);
  93. $msg->addFeedback($f);
  94. }
  95. }
  96. if ($no_error) array_push($all_tests, $row);
  97. }
  98. // generate students array
  99. $sql_students = "SELECT m.first_name, m.last_name, e.member_id FROM ".TABLE_PREFIX."members m, ".TABLE_PREFIX."course_enrollment e WHERE m.member_id = e.member_id AND e.course_id=".$_SESSION["course_id"]." AND e.approved='y' AND e.role!='Instructor'";
  100. if ($order_col == "name")
  101. {
  102. $sql_students .= " ORDER BY m.first_name ".$order.",m.last_name ".$order;
  103. }
  104. $result = mysql_query($sql_students, $db) or die(mysql_error());
  105. while ($row = mysql_fetch_assoc($result))
  106. array_push($all_students, $row);
  107. // end of initialization
  108. // Creates arrays for filtered test/student
  109. $selected_tests = array();
  110. //$csv_content .= $selected_students[$i]["first_name"]." " . $selected_students[$i]["last_name"];
  111. $selected_students = array();
  112. $grades = array();
  113. // generate test array
  114. if (($_GET["filter"] || $_GET["download"]) && $_GET["gradebook_test_id"]<>0)
  115. {
  116. foreach ($all_tests as $test)
  117. {
  118. if ($test["gradebook_test_id"] == $_GET["gradebook_test_id"])
  119. {
  120. $selected_tests[0]["gradebook_test_id"] = $test["gradebook_test_id"];
  121. $selected_tests[0]["title"] = $test["title"];
  122. $selected_tests[0]["type"] = $test["type"];
  123. }
  124. }
  125. }
  126. else
  127. $selected_tests = $all_tests;
  128. // generate students array
  129. if (($_GET["filter"] || $_GET["download"]) && $_GET["member_id"]<>0)
  130. {
  131. foreach ($all_students as $student)
  132. {
  133. if ($student["member_id"] == $_GET["member_id"])
  134. {
  135. $selected_students[0]["member_id"] = $student["member_id"];
  136. $selected_students[0]["first_name"] = $student["first_name"];
  137. $selected_students[0]["last_name"] = $student["last_name"];
  138. }
  139. }
  140. $_GET["member_id"] = intval($_GET["member_id"]);
  141. $sql_students = "SELECT first_name, last_name, member_id FROM ".TABLE_PREFIX."members WHERE member_id=" . $_GET["member_id"];
  142. }
  143. else
  144. $selected_students = $all_students;
  145. // generate grade 2-dimentional array
  146. foreach ($selected_tests as $selected_test)
  147. foreach($selected_students as $selected_student)
  148. {
  149. $sql = "SELECT grade FROM ".TABLE_PREFIX."gradebook_detail WHERE gradebook_test_id=".$selected_test["gradebook_test_id"]." AND member_id=".$selected_student["member_id"];
  150. $result = mysql_query($sql, $db) or die(mysql_error());
  151. $row = mysql_fetch_assoc($result);
  152. $grades[$selected_test["gradebook_test_id"]][$selected_student["member_id"]] = $row["grade"];
  153. }
  154. // sort grade
  155. if ((isset($_GET["asc"]) || isset($_GET["desc"])) && $order_col <> "name")
  156. {
  157. $sort = '$grades['.$order_col.'], SORT_'.strtoupper($order).', $selected_students, SORT_'.strtoupper($order);
  158. foreach($selected_tests as $test)
  159. {
  160. if ($test["gradebook_test_id"] <> $order_col)
  161. $sort .= ', $grades['.$test["gradebook_test_id"].'], SORT_'.strtoupper($order);
  162. }
  163. $sort='array_multisort('.$sort.');';
  164. eval($sort);
  165. }
  166. // end of initialization
  167. $num_students = count($selected_students);
  168. $results_per_page = 50;
  169. $num_pages = max(ceil($num_students / $results_per_page), 1);
  170. $page = intval($_GET['p']);
  171. if (!$page) {
  172. $page = 1;
  173. }
  174. $count = (($page-1) * $results_per_page) + 1;
  175. $offset = ($page-1)*$results_per_page;
  176. // generate table & csv head
  177. $table_head = "<thead>\n\r";
  178. $table_head .= "<tr>\n\r";
  179. if ($_GET[filter] <> "")
  180. $query_str = '&amp;filter='.$_GET[filter];
  181. if ($_GET[member_id] <> "")
  182. $query_str .= '&amp;member_id='.$_GET[member_id];
  183. if ($_GET[gradebook_test_id] <> "")
  184. $query_str .= '&amp;gradebook_test_id='.$_GET[gradebook_test_id];
  185. $table_head .= " <th scope='col'><a href='". $_SERVER['PHP_SELF'] .'?'.$orders[$order].'=name'.$query_str."'>". _AT('name')."</a></th>\n\r";
  186. $csv_content = _AT('name');
  187. foreach ($selected_tests as $selected_test)
  188. {
  189. $table_head .= " <th scope='col'><a href='". $_SERVER['PHP_SELF'] ."?".$orders[$order]."=".$selected_test[gradebook_test_id].$query_str."'>". $selected_test[title]."</a></th>\n\r";
  190. $csv_content .= ",".$selected_test[title];
  191. }
  192. $table_head .= " <th scope='col'></th>\n\r";
  193. $table_head .= "</tr>\n\r";
  194. $csv_content .= "\n";
  195. $table_head .= "<tr>\n\r";
  196. $table_head .= " <td></td>\n\r";
  197. $has_edit_button = false;
  198. foreach ($selected_tests as $selected_test)
  199. {
  200. if ($selected_test["type"] == "External" || $selected_test["type"] == "ATutor Assignment")
  201. {
  202. $has_edit_button = true;
  203. $table_head .= " <td style='text-align:center'><a href='". $_SERVER['PHP_SELF']. '?edit=c_'.$selected_test['gradebook_test_id'].$query_str."'>". _AT("edit")."</a></td>\n\r";
  204. }
  205. else
  206. {
  207. $table_head .= " <td></td>\n\r";
  208. }
  209. }
  210. if ($has_edit_button) $table_head .= " <td></td>";
  211. $table_head .= "</tr>\n\r";
  212. $table_head .= "</thead>\n\r";
  213. // generate table & csv content
  214. if ($num_students > 0)
  215. {
  216. $table_content = " <tbody>\n\r";
  217. if ($offset + $results_per_page > $num_students) $end_pos = $num_students;
  218. else $end_pos = $offset + $results_per_page;
  219. $tabindex_input = 1;
  220. $tabindex_edit = 2;
  221. for ($i=$offset; $i < $end_pos; $i++)
  222. {
  223. $table_content .= " <tr>\n\r";
  224. $table_content .= " <td>".$selected_students[$i]["first_name"]." " . $selected_students[$i]["last_name"]."</td>\n\r";
  225. $csv_content .= $selected_students[$i]["first_name"]." " . $selected_students[$i]["last_name"];
  226. foreach ($selected_tests as $selected_test)
  227. {
  228. $sql = "SELECT grade FROM ".TABLE_PREFIX."gradebook_detail WHERE gradebook_test_id=".$selected_test["gradebook_test_id"]." AND member_id=".$selected_students[$i]["member_id"];
  229. $result = mysql_query($sql, $db) or die(mysql_error());
  230. $row = mysql_fetch_assoc($result);
  231. $row["grade"] = htmlspecialchars($row["grade"]); // handle html special chars
  232. if ($_GET["edit"]=="c_".$selected_test["gradebook_test_id"] || $_GET["edit"]=="r_".$selected_students[$i]["member_id"] && ($selected_test["type"]=="External" || $selected_test["type"]=="ATutor Assignment"))
  233. {
  234. $table_content .= " <td><input type='text' name='grade_".$selected_test["gradebook_test_id"]."_".$selected_students[$i]["member_id"]."' value=\"".$row["grade"]."\" tabindex='".$tabindex_input."' /></td>\n\r";
  235. $csv_content .= ",".$row["grade"];
  236. }
  237. else
  238. {
  239. if ($row["grade"]=="")
  240. {
  241. $table_content .= " <td style='text-align:center'>"._AT("na")."</td>\n\r";
  242. $csv_content .= ",". _AT("na");
  243. }
  244. else
  245. {
  246. $table_content .= " <td style='text-align:center'>".$row["grade"]."</td>\n\r";
  247. $csv_content .= ",".$row["grade"];
  248. }
  249. }
  250. }
  251. if ($has_edit_button)
  252. $table_content .= " <td style='text-align:center'><a href=\"". $_SERVER['PHP_SELF']. "?edit=r_".$selected_students[$i]['member_id'].$query_str."\" tabindex='".$tabindex_edit."'>". _AT("edit") ."</a></td>\n\r";
  253. $table_content .= " </tr>\n\r";
  254. $csv_content .= "\n";
  255. }
  256. $table_content .= " </tbody>\n\r";
  257. }
  258. // download csv file
  259. if ($_GET['download'])
  260. {
  261. if ($num_students == 0)
  262. {
  263. require (AT_INCLUDE_PATH.'header.inc.php');
  264. $msg->printErrors('ITEM_NOT_FOUND');
  265. require (AT_INCLUDE_PATH.'footer.inc.php');
  266. exit;
  267. }
  268. header('Content-Type: application/x-excel');
  269. header('Content-Disposition: inline; filename="grades.csv"');
  270. header('Expires: 0');
  271. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  272. header('Pragma: public');
  273. echo $csv_content;
  274. exit;
  275. }
  276. require(AT_INCLUDE_PATH.'header.inc.php');
  277. $msg->printFeedbacks();
  278. if (count($selected_tests)==0)
  279. {
  280. echo '<div class="toolcontainer">'._AT('empty_gradebook').'</div>';
  281. require(AT_INCLUDE_PATH.'footer.inc.php');
  282. exit;
  283. }
  284. ?>
  285. <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  286. <div class="input-form" id="jump-area">
  287. <fieldset class="group_form"><legend class="group_form"><?php echo _AT('search'); ?></legend>
  288. <div class="row">
  289. <label for="select_gid"><?php echo _AT("name") ?></label><br />
  290. <select name="gradebook_test_id" id="select_gid">
  291. <option value="0"><?php echo _AT('all') ?></option>
  292. <?php
  293. foreach($all_tests as $test)
  294. {
  295. echo ' <option value="'.$test[gradebook_test_id]. '"';
  296. if ($test[gradebook_test_id]==$_GET["gradebook_test_id"])
  297. echo ' SELECTED ';
  298. echo '>'.$test["title"].'</option>'."\n\r";
  299. }
  300. ?>
  301. </select>
  302. </div>
  303. <div class="row">
  304. <label for="select_mid"><?php echo _AT("students") ?></label><br />
  305. <select name="member_id" id="select_mid">
  306. <option value="0"><?php echo _AT('all') ?></option>
  307. <?php
  308. foreach($all_students as $student)
  309. {
  310. echo ' <option value="'.$student[member_id].'"';
  311. if ($student[member_id]==$_GET["member_id"])
  312. echo ' SELECTED ';
  313. echo '>'.$student[first_name].' '.$student[last_name].'</option>'."\n\r";
  314. }
  315. ?>
  316. </select>
  317. </div>
  318. <div class="row buttons">
  319. <input type="submit" name="filter" value="<?php echo _AT('filter'); ?>" />
  320. <input type="submit" name="reset_filter" value="<?php echo _AT('reset_filter'); ?>" />
  321. <input type="submit" name="download" value="<?php echo _AT('download_test_csv'); ?>" />
  322. </div>
  323. </fieldset>
  324. </div>
  325. </form>
  326. <form name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  327. <input type="hidden" name="filter" value="<?php echo AT_print($_GET[filter], 'input.hidden');?>" />
  328. <input type="hidden" name="gradebook_test_id" value="<?php echo AT_print($_GET[gradebook_test_id], 'input.hidden'); ?>" />
  329. <input type="hidden" name="member_id" value="<?php echo AT_print($_GET[member_id], 'input.hidden'); ?>" />
  330. <input type="hidden" name="p" value="<?php echo $page; ?>" />
  331. <?php print_paginator($page, $num_students, $sql_students, $results_per_page); ?>
  332. <table summary="" class="data" rules="all">
  333. <?php
  334. echo $table_head;
  335. ?>
  336. <tfoot>
  337. <tr>
  338. <td colspan="<?php echo count($selected_tests)+2; ?>">
  339. <div class="row buttons">
  340. <input type="submit" name="save" value="<?php echo _AT('save'); ?>" />
  341. </div>
  342. </td>
  343. </tr>
  344. </tfoot>
  345. <?php
  346. if ($num_students == 0)
  347. {
  348. ?>
  349. <tr>
  350. <td colspan="<?php echo count($selected_tests)+1; ?>"><?php echo _AT('none_found'); ?></td>
  351. </tr>
  352. <?php
  353. }
  354. else
  355. {
  356. echo $table_content;
  357. }
  358. ?>
  359. </table>
  360. </form>
  361. <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>