/application/models/Question.php

https://gitlab.com/klausmig/CloudSemanticWeb · PHP · 279 lines · 261 code · 9 blank · 9 comment · 8 complexity · 1af8dc7c0fbad922f7172bc452b91484 MD5 · raw file

  1. <?php
  2. class Question extends Model {
  3. public $id;
  4. public $user;
  5. public $item_id;
  6. public $item_owner;
  7. public $question;
  8. public $answers;
  9. public $based_on;
  10. public $mark;
  11. public $timestamp;
  12. public $guessing;
  13. public $difficulty;
  14. public $diff_count;
  15. public $module;
  16. public $diff_flag = 0;
  17. public $slide_revision;
  18. public $checked;
  19. private function initConnection() {
  20. // connect to db
  21. if ($this->connect ( DB_DSN, DB_USER, DB_PASSWORD ) == 0)
  22. die ( "Could not connect to db" );
  23. }
  24. public function create() {
  25. $this->initConnection();
  26. $id = $this->dbInsert('questions',array('item_id'=>$this->item_id,'based_on'=>$this->based_on,'mark'=>$this->mark,'difficulty'=>$this->difficulty,'diff_count'=>$this->diff_count,'user_id'=>$this->user->id,'question'=>addslashes($this->question)));
  27. return $id;
  28. }
  29. public function createFromID($id) {
  30. $this->initConnection ();
  31. $res = $this->dbGetRow ( 'SELECT * FROM questions WHERE id=:id', array ('id' => $id ) );
  32. $this->id = $id;
  33. $user = new User();
  34. $user->createFromID($res ['user_id']);
  35. $this->user = $user;
  36. $this->item_id = $res ['item_id'];
  37. $this->question = stripslashes($res ['question']);
  38. $this->answers = $this->getAnswers();
  39. $this->based_on = $res['based_on'];
  40. $this->mark = $res['mark'];
  41. $difficulty = $res['difficulty'];
  42. // if ($res['diff_count'] == NULL || $res['diff_count']<=0) $difficulty = $res['difficulty'];
  43. // else {
  44. // $difficulty = $res['diff_count'];
  45. // $this->diff_flag = 1;
  46. // }
  47. $this->difficulty = $difficulty;
  48. $this->diff_count = $res['diff_count'];
  49. $this->timestamp = $res['timestamp'];
  50. $this->guessing = $this->guessCount();
  51. }
  52. public function copy($user_id){
  53. $this->question = str_replace("\n"," ", $this->question);
  54. $this->question = str_replace("<br>"," ", $this->question);
  55. $id = $this->dbInsert('questions',array('item_id'=>$this->item_id,'based_on'=>$this->based_on,'mark'=>$this->mark,'difficulty'=>$this->difficulty,'diff_count'=>0,'user_id'=>$user_id,'question'=>$this->question));
  56. $copy = new Question();
  57. $copy->id = $id;
  58. foreach ($this->answers as $answer){
  59. $copy->addAnswer(stripslashes($answer['answer']), stripslashes($answer['explanation']), $answer['is_right']);
  60. }
  61. return $id;
  62. }
  63. public function delete(){
  64. $this->dbQuery('DELETE FROM questions WHERE id=:id',array('id'=>$this->id));
  65. $this->dbQuery('DELETE FROM answers WHERE question_id=:id',array('id'=>$this->id));
  66. $children = $this->getAllChildren();
  67. foreach ($children as $question){
  68. $this->dbQuery('DELETE FROM questions WHERE id=:id',array('id'=>$question->id));
  69. $this->dbQuery('DELETE FROM answers WHERE question_id=:id',array('id'=>$question->id));
  70. }
  71. }
  72. public function getFirstRevision(){
  73. $this->initConnection();
  74. if ($this->based_on > 0){
  75. $question = new Question();
  76. $question->createFromID($this->based_on);
  77. $res = $question->getFirstRevision();
  78. }else $res = $this;
  79. return $res;
  80. }
  81. public function getFirstChildren(){
  82. $this->initConnection();
  83. $res = array();
  84. foreach ($this->dbQuery('SELECT id FROM questions WHERE based_on=:id', array('id'=>$this->id))as $row){
  85. $res [] = $row['id'];
  86. }
  87. return $res;
  88. }
  89. public function getAllRevisions() {
  90. $this->initConnection();
  91. $result = array();
  92. $first = new Question();
  93. $first = $this->getFirstRevision();
  94. $sort_numcie = array();
  95. foreach($first->answers as $index=>$answer) {
  96. $sort_numcie[] = $answer['is_right'];
  97. }
  98. array_multisort($sort_numcie, SORT_DESC, $first->answers);
  99. $result [] = $first;
  100. $result = array_merge($result, $first->getAllChildren());
  101. $sort_numcie = array();
  102. foreach($result as $index=>$question) {
  103. $sort_numcie[] = $question->timestamp;
  104. }
  105. array_multisort($sort_numcie, SORT_DESC, $result);
  106. return $result;
  107. }
  108. public function getAllChildren(){
  109. static $result = array();
  110. if (count($this->getFirstChildren())){
  111. foreach ($this->getFirstChildren() as $next){
  112. $question = new Question();
  113. $question->createFromID($next);
  114. $sort_numcie = array();
  115. foreach($question->answers as $index=>$answer) {
  116. $sort_numcie[] = $answer['is_right'];
  117. }
  118. array_multisort($sort_numcie, SORT_DESC, $question->answers);
  119. $result [] = $question;
  120. $question->getAllChildren();
  121. }
  122. }
  123. return $result;
  124. }
  125. public function useQuestRevision(){
  126. $this->initConnection();
  127. $difficulty = $this->dbGetOne ( 'SELECT difficulty FROM questions WHERE id=:id', array ('id' => $this->id ) );
  128. $this->dbQuery('DELETE FROM questions WHERE id=:id',array('id'=>$this->id));
  129. $this->difficulty = $difficulty;
  130. $id = $this->dbInsert('questions',array('id'=>$this->id,'item_id'=>$this->item_id,'based_on'=>$this->based_on,'mark'=>$this->mark,'difficulty'=>$this->difficulty,'diff_count'=>$this->diff_count,'user_id'=>$this->user->id,'question'=> addslashes($this->question)));
  131. // $this->dbQuery('INSERT INTO questions (id, item_id, based_on, mark, difficulty, diff_count, user_id, question) VALUES ('.$this->id .','.$this->item_id.','.$this->based_on.','.$this->mark.','.$this->difficulty.','.$this->diff_count.','.$this->user->id.','.htmlentities($this->question, ENT_QUOTES).')');
  132. return $this->id;
  133. }
  134. public function edit() {
  135. $this->initConnection ();
  136. $this->question = str_replace("\n"," ", $this->question);
  137. $this->question = str_replace("<br>"," ", $this->question);
  138. $this->dbQuery('UPDATE questions SET question=:question, mark=:mark, difficulty=:difficulty, diff_count=:diff_count WHERE id=:id', array ('id' => $this->id, 'mark' => $this->mark, 'question' => addslashes($this->question), 'difficulty' => $this->difficulty, 'diff_count' => $this->diff_count));
  139. }
  140. public function getOwnerID() {
  141. $this->initConnection ();
  142. return $this->dbGetOne('SELECT user_id FROM questions WHERE id=' . $this->id);
  143. }
  144. public function getItemType() {
  145. $this->initConnection ();
  146. return $this->dbGetOne('SELECT item_type FROM questions WHERE id=' . $this->id);
  147. }
  148. public function getLastRevision(){
  149. $timestamp = $this->timestamp;
  150. $last_id = $this->id;
  151. foreach ($this->dbQuery( 'SELECT * FROM questions WHERE based_on=:id', array ('id' => $last_id ) ) as $question_row) {
  152. if ($timestamp < $question_row['timestamp']) {
  153. $question = new Question();
  154. $question->createFromID($question_row['id']);
  155. $last_id = $question->getLastRevision();
  156. }
  157. }
  158. return $last_id;
  159. }
  160. public function addAnswer($answer,$explanation,$is_right){
  161. $answer = str_replace("\n"," ", $answer);
  162. $explanation = str_replace("\n"," ", $explanation);
  163. $answer = str_replace("<br>"," ", $answer);
  164. $explanation = str_replace("<br>"," ", $explanation);
  165. $res = $this->dbInsert('answers',array('question_id'=>$this->id,'answer'=>addslashes($answer), 'is_right'=>$is_right, 'explanation'=>addslashes($explanation)));
  166. $this->guessUpdate();
  167. return $res;
  168. }
  169. public function getAnswer($newAnswerId){
  170. $res = $this->dbGetRow ( 'SELECT * FROM answers WHERE id=:id', array ( 'id' => $newAnswerId ) );
  171. $res['answer'] = stripslashes($res['answer']);
  172. $res['explanation'] = stripslashes($res['explanation']);
  173. return $res;
  174. }
  175. public function getAnswers() {
  176. $answers = array ();
  177. foreach ( $this->dbQuery ( 'SELECT id, answer, explanation, is_right FROM answers WHERE question_id=' . $this->id ) as $row ){
  178. $row['answer'] = stripslashes($row['answer']);
  179. $row['explanation'] = stripslashes($row['explanation']);
  180. $answers[] = $row;
  181. }
  182. return $answers;
  183. }
  184. public function getRightAnswers() {
  185. $answers = array ();
  186. foreach ( $this->dbQuery ( 'SELECT id, answer, explanation, is_right FROM answers WHERE question_id=' . $this->id . ' AND is_right="yes"' ) as $row ){
  187. $row['answer'] = stripslashes($row['answer']);
  188. $row['explanation'] = stripslashes($row['explanation']);
  189. $answers[] = $row;
  190. }
  191. return $answers;
  192. }
  193. public function getDistractors(){
  194. $answers = array ();
  195. foreach ( $this->dbQuery ( 'SELECT id, answer, explanation, is_right FROM answers WHERE question_id=' . $this->id . ' AND is_right="no"' ) as $row ){
  196. $row['answer'] = stripslashes($row['answer']);
  197. $row['explanation'] = stripslashes($row['explanation']);
  198. $answers[] = $row;
  199. }
  200. return $answers;
  201. }
  202. public function editAnswer($answerId, $answer, $explanation, $is_right){
  203. $answer = str_replace("\n"," ", $answer);
  204. $explanation = str_replace("\n"," ", $explanation);
  205. $answer = str_replace("<br>"," ", $answer);
  206. $explanation = str_replace("<br>"," ", $explanation);
  207. $this->dbQuery( 'UPDATE answers SET answer=:answer, explanation=:explanation, is_right=:is_right WHERE id=:id ', array (
  208. 'answer' => addslashes($answer),
  209. 'explanation' => addslashes($explanation),
  210. 'is_right' => $is_right,
  211. 'id' => $answerId
  212. ) );
  213. $this->guessUpdate();
  214. return $this->getAnswer($answerId);
  215. }
  216. public function removeAnswer($answerId){
  217. $this->dbQuery( "DELETE FROM answers WHERE id=:id", array ( 'id' => $answerId ) );
  218. $this->guessUpdate();
  219. }
  220. //get QuestID by answerID
  221. public function getID($answerId){
  222. return $this->dbGetOne('SELECT question_id FROM answers WHERE id=:id LIMIT 1', array ('id' => $answerId ));
  223. }
  224. public function addExplanation($answerId, $explanation){
  225. $this->dbQuery("UPDATE answers SET explanation='" . addslashes($explanation) . "' WHERE id=" . $answerId);
  226. $explanation = str_replace("\n"," ", $explanation);
  227. $explanation = str_replace("<br>"," ", $explanation);
  228. }
  229. public function getExplanation($answerId){
  230. $res = $this->dbGetOne( 'SELECT explanation FROM answers WHERE id=:id LIMIT 1', array ('id' => $answerId ) );
  231. $res = $res;
  232. return $res;
  233. }
  234. public function guessCount() {
  235. $count_right = $this->dbGetOne('SELECT count(id) FROM answers WHERE is_right="yes" AND question_id=:id', array('id'=>$this->id));
  236. $count_false = $this->dbGetOne('SELECT count(id) FROM answers WHERE is_right="no" AND question_id=:id', array('id'=>$this->id));
  237. if ($count_right > 0 && $count_false > 0){
  238. $guessing = $count_right/($count_right+$count_false);
  239. }else $guessing = 0;
  240. return $guessing;
  241. }
  242. public function guessUpdate() {
  243. $guessing = $this->guessCount();
  244. $this->dbQuery("UPDATE questions SET guessing=:guessing WHERE id=:id", array('guessing'=>$guessing,'id'=>$this->id));
  245. $this->guessing = $guessing;
  246. }
  247. public function markDoubtful($user_id, $comment){
  248. $this->dbQuery("UPDATE questions SET mark='doubtful' WHERE id=" . $this->id);
  249. $this->dbInsert('doubtful', array('question_id' => $this->id, 'user_id' => $user_id, 'comment' => $comment));
  250. }
  251. public function unMarkDoubtful(){
  252. $this->dbQuery("UPDATE questions SET mark='accepted' WHERE id=" . $this->id);
  253. $this->dbQuery('DELETE FROM doubtful WHERE question_id = ' . $this->id);
  254. }
  255. //update count of correct/all_answers
  256. public function editCorrect($correct){
  257. $this->dbQuery('UPDATE questions SET all_answers = all_answers+1 WHERE id=' . $this->id);
  258. $incorrect = 1-$correct;
  259. $this->dbQuery('UPDATE questions SET incorrect = incorrect+' . $incorrect . ' WHERE id=' . $this->id);
  260. if ($this->dbGetOne('SELECT all_answers FROM questions WHERE id=' . $this->id)>10){
  261. $this->dbQuery('UPDATE questions SET diff_count=incorrect/all_answers*4+1 WHERE id=' . $this->id);
  262. }
  263. }
  264. public function reAssign($new_slide_id){
  265. $slide = new Slide();
  266. $slide->id = $new_slide_id;
  267. $new_basic_id = $slide->getBasicId();
  268. $this->dbQuery('UPDATE questions SET item_id = '.$new_basic_id.' WHERE id=' . $this->id);
  269. }
  270. }