result.class.php 9.8 KB

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