result.class.php 8.5 KB

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