result.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. }
  146. $sql .= ' gr.user_id = '.intval($user_id);
  147. $paramcount++;
  148. }
  149. if (!empty($evaluation_id)) {
  150. if ($paramcount != 0) {
  151. $sql .= ' AND';
  152. } else {
  153. $sql .= ' WHERE';
  154. }
  155. $sql .= ' gr.evaluation_id = '.intval($evaluation_id);
  156. $paramcount++;
  157. }
  158. $sql .= ' ORDER BY u.lastname, u.firstname';
  159. $result = Database::query($sql);
  160. $allres = array();
  161. while ($data = Database::fetch_array($result)) {
  162. $res = new Result();
  163. $res->set_id($data['id']);
  164. $res->set_user_id($data['user_id']);
  165. $res->set_evaluation_id($data['evaluation_id']);
  166. $res->set_date(api_get_local_time($data['created_at']));
  167. $res->set_score($data['score']);
  168. $allres[] = $res;
  169. }
  170. return $allres;
  171. }
  172. /**
  173. * Insert this result into the database
  174. */
  175. public function add()
  176. {
  177. if (isset($this->user_id) && isset($this->evaluation)) {
  178. $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  179. $sql = "INSERT INTO ".$tbl_grade_results
  180. . " (user_id, evaluation_id,
  181. created_at";
  182. if (isset($this->score)) {
  183. $sql .= ",score";
  184. }
  185. $sql .= ") VALUES
  186. (" . (int) $this->get_user_id().", ".(int) $this->get_evaluation_id()
  187. . ", '".$this->get_date()."' ";
  188. if (isset($this->score)) {
  189. $sql .= ", ".$this->get_score();
  190. }
  191. $sql .= ")";
  192. Database::query($sql);
  193. } else {
  194. die('Error in Result add: required field empty');
  195. }
  196. }
  197. /**
  198. * insert log result
  199. */
  200. public function add_result__log($userid, $evaluationid)
  201. {
  202. if (isset($userid) && isset($evaluationid)) {
  203. $tbl_grade_results_log = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG);
  204. $result = new Result();
  205. $arr_result = $result->load(null, $userid, $evaluationid);
  206. $arr = get_object_vars($arr_result[0]);
  207. $sql = 'INSERT INTO '.$tbl_grade_results_log
  208. . ' (id_result,user_id, evaluation_id,created_at';
  209. if (isset($arr['score'])) {
  210. $sql .= ',score';
  211. }
  212. $sql .= ') VALUES
  213. (' . (int) $arr['id'].','.(int) $arr['user_id'].', '.(int) $arr['evaluation']
  214. . ", '".api_get_utc_datetime()."'";
  215. if (isset($arr['score'])) {
  216. $sql .= ', '.$arr['score'];
  217. }
  218. $sql .= ')';
  219. Database::query($sql);
  220. } else {
  221. die('Error in Result add: required field empty');
  222. }
  223. }
  224. /**
  225. * Update the properties of this result in the database
  226. */
  227. public function save()
  228. {
  229. $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  230. $sql = 'UPDATE '.$tbl_grade_results.'
  231. SET user_id = ' . $this->get_user_id()
  232. . ', evaluation_id = '.$this->get_evaluation_id()
  233. . ', score = ';
  234. if (isset($this->score)) {
  235. $sql .= $this->get_score();
  236. } else {
  237. $sql .= 'null';
  238. }
  239. $sql .= ' WHERE id = '.$this->id;
  240. // no need to update creation date
  241. Database::query($sql);
  242. }
  243. /**
  244. * Delete this result from the database
  245. */
  246. public function delete()
  247. {
  248. $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  249. $sql = 'DELETE FROM '.$tbl_grade_results.' WHERE id = '.$this->id;
  250. Database::query($sql);
  251. }
  252. }