result.class.php 8.8 KB

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