result.class.php 7.2 KB

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