result.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Defines a gradebook Result object
  5. * @author Bert Steppé, Stijn Konings
  6. * @package chamilo.gradebook
  7. */
  8. class Result
  9. {
  10. private $id;
  11. private $user_id;
  12. private $evaluation;
  13. private $created_at;
  14. private $score;
  15. public function Result()
  16. {
  17. $this->created_at = api_get_utc_datetime();
  18. }
  19. public function get_id()
  20. {
  21. return $this->id;
  22. }
  23. public function get_user_id()
  24. {
  25. return $this->user_id;
  26. }
  27. public function get_evaluation_id()
  28. {
  29. return $this->evaluation;
  30. }
  31. public function get_date()
  32. {
  33. return $this->created_at;
  34. }
  35. public function get_score()
  36. {
  37. return $this->score;
  38. }
  39. public function set_id($id)
  40. {
  41. $this->id = $id;
  42. }
  43. public function set_user_id($user_id)
  44. {
  45. $this->user_id = $user_id;
  46. }
  47. public function set_evaluation_id($evaluation_id)
  48. {
  49. $this->evaluation = $evaluation_id;
  50. }
  51. /**
  52. * @param string $creation_date
  53. */
  54. public function set_date($creation_date)
  55. {
  56. $this->created_at = $creation_date;
  57. }
  58. /**
  59. * @param float $score
  60. */
  61. public function set_score($score)
  62. {
  63. $this->score = $score;
  64. }
  65. /**
  66. * Retrieve results and return them as an array of Result objects
  67. * @param $id result id
  68. * @param $user_id user id (student)
  69. * @param $evaluation_id evaluation where this is a result for
  70. */
  71. public static function load($id = null, $user_id = null, $evaluation_id = null)
  72. {
  73. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  74. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  75. $tbl_course_rel_course = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  76. $tbl_session_rel_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  77. $sessionId = api_get_session_id();
  78. $list_user_course_list = array();
  79. if (is_null($id) && is_null($user_id) && !is_null($evaluation_id)) {
  80. // Verified_if_exist_evaluation
  81. $sql = 'SELECT COUNT(*) AS count
  82. FROM ' . $tbl_grade_results . '
  83. WHERE evaluation_id="' . Database::escape_string($evaluation_id) . '";';
  84. $result = Database::query($sql);
  85. $existEvaluation = Database::result($result, 0, 0);
  86. if ($existEvaluation != 0) {
  87. if ($sessionId) {
  88. $sql = 'SELECT c_id, user_id as user_id, status
  89. FROM ' . $tbl_session_rel_course_user . '
  90. WHERE
  91. status= 0 AND
  92. c_id = "' . api_get_course_int_id() . '" AND
  93. session_id = ' . $sessionId;
  94. } else {
  95. $sql = 'SELECT c_id, user_id, status
  96. FROM ' . $tbl_course_rel_course . '
  97. WHERE status ="' . STUDENT . '" AND c_id = "' . api_get_course_int_id() . '" ';
  98. }
  99. $res_course_rel_user = Database::query($sql);
  100. while ($row_course_rel_user = Database::fetch_array($res_course_rel_user, 'ASSOC')) {
  101. $list_user_course_list[] = $row_course_rel_user;
  102. }
  103. $current_date = api_get_utc_datetime();
  104. for ($i = 0; $i < count($list_user_course_list); $i++) {
  105. $sql_verified = 'SELECT COUNT(*) AS count
  106. FROM ' . $tbl_grade_results . '
  107. WHERE
  108. user_id="' . intval($list_user_course_list[$i]['user_id']) . '" AND
  109. evaluation_id="' . intval($evaluation_id) . '";';
  110. $res_verified = Database::query($sql_verified);
  111. $info_verified = Database::result($res_verified, 0, 0);
  112. if ($info_verified == 0) {
  113. $sql_insert = 'INSERT INTO ' . $tbl_grade_results . '(user_id,evaluation_id,created_at,score)
  114. VALUES ("' . intval($list_user_course_list[$i]['user_id']) . '","' . intval($evaluation_id) . '","' . $current_date . '",0);';
  115. Database::query($sql_insert);
  116. }
  117. }
  118. }
  119. }
  120. $userIdList = array();
  121. foreach ($list_user_course_list as $data) {
  122. $userIdList[] = $data['user_id'];
  123. }
  124. $userIdListToString = implode("', '", $userIdList);
  125. $sql = "SELECT lastname, gr.id, gr.user_id, gr.evaluation_id, gr.created_at, gr.score
  126. FROM $tbl_grade_results gr
  127. INNER JOIN $tbl_user u
  128. ON gr.user_id = u.user_id ";
  129. if (!empty($userIdList)) {
  130. $sql .= " AND u.user_id IN ('$userIdListToString')";
  131. }
  132. $paramcount = 0;
  133. if (!empty($id)) {
  134. $sql.= ' WHERE gr.id = ' . intval($id);
  135. $paramcount ++;
  136. }
  137. if (!empty($user_id)) {
  138. if ($paramcount != 0)
  139. $sql .= ' AND';
  140. else
  141. $sql .= ' WHERE';
  142. $sql .= ' gr.user_id = ' . intval($user_id);
  143. $paramcount ++;
  144. }
  145. if (!empty($evaluation_id)) {
  146. if ($paramcount != 0) {
  147. $sql .= ' AND';
  148. } else {
  149. $sql .= ' WHERE';
  150. }
  151. $sql .= ' gr.evaluation_id = ' . intval($evaluation_id);
  152. $paramcount ++;
  153. }
  154. $sql .= ' ORDER BY u.lastname, u.firstname';
  155. $result = Database::query($sql);
  156. $allres = array();
  157. while ($data = Database::fetch_array($result)) {
  158. $res = new Result();
  159. $res->set_id($data['id']);
  160. $res->set_user_id($data['user_id']);
  161. $res->set_evaluation_id($data['evaluation_id']);
  162. $res->set_date(api_get_local_time($data['created_at']));
  163. $res->set_score($data['score']);
  164. $allres[] = $res;
  165. }
  166. return $allres;
  167. }
  168. /**
  169. * Insert this result into the database
  170. */
  171. public function add()
  172. {
  173. if (isset($this->user_id) && isset($this->evaluation)) {
  174. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  175. $sql = "INSERT INTO " . $tbl_grade_results
  176. . " (user_id, evaluation_id,
  177. created_at";
  178. if (isset($this->score)) {
  179. $sql .= ",score";
  180. }
  181. $sql .= ") VALUES
  182. (" . (int) $this->get_user_id() . ", " . (int) $this->get_evaluation_id()
  183. . ", '" . $this->get_date() . "' ";
  184. if (isset($this->score)) {
  185. $sql .= ", " . $this->get_score();
  186. }
  187. $sql .= ")";
  188. Database::query($sql);
  189. } else {
  190. die('Error in Result add: required field empty');
  191. }
  192. }
  193. /**
  194. * insert log result
  195. */
  196. public function add_result__log($userid, $evaluationid)
  197. {
  198. if (isset($userid) && isset($evaluationid)) {
  199. $tbl_grade_results_log = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG);
  200. $result = new Result();
  201. $arr_result = $result->load(null, $userid, $evaluationid);
  202. $arr = get_object_vars($arr_result[0]);
  203. $sql = 'INSERT INTO ' . $tbl_grade_results_log
  204. . ' (id_result,user_id, evaluation_id,created_at';
  205. if (isset($arr['score'])) {
  206. $sql .= ',score';
  207. }
  208. $sql .= ') VALUES
  209. (' . (int) $arr['id'] . ',' . (int) $arr['user_id'] . ', ' . (int) $arr['evaluation']
  210. . ", '" . api_get_utc_datetime() . "'";
  211. if (isset($arr['score'])) {
  212. $sql .= ', ' . $arr['score'];
  213. }
  214. $sql .= ')';
  215. Database::query($sql);
  216. } else {
  217. die('Error in Result add: required field empty');
  218. }
  219. }
  220. /**
  221. * Update the properties of this result in the database
  222. */
  223. public function save()
  224. {
  225. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  226. $sql = 'UPDATE ' . $tbl_grade_results . '
  227. SET user_id = ' . $this->get_user_id()
  228. . ', evaluation_id = ' . $this->get_evaluation_id()
  229. . ', score = ';
  230. if (isset($this->score)) {
  231. $sql .= $this->get_score();
  232. } else {
  233. $sql .= 'null';
  234. }
  235. $sql .= ' WHERE id = ' . $this->id;
  236. // no need to update creation date
  237. Database::query($sql);
  238. }
  239. /**
  240. * Delete this result from the database
  241. */
  242. public function delete()
  243. {
  244. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  245. $sql = 'DELETE FROM ' . $tbl_grade_results . ' WHERE id = ' . $this->id;
  246. Database::query($sql);
  247. }
  248. }