result.class.php 8.9 KB

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