/application/models/Quest_eval.php

https://gitlab.com/klausmig/CloudSemanticWeb · PHP · 237 lines · 209 code · 18 blank · 10 comment · 20 complexity · a40eb5592c81648142e27e4171b7eeb3 MD5 · raw file

  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * Description of Quest_eval
  8. *
  9. * @author maayan
  10. */
  11. class Quest_eval extends Model{
  12. public $attempt;
  13. public $type;
  14. public $deck_id;
  15. public $max_points;
  16. public $user_id;
  17. public $mode;
  18. public $limit;
  19. public $wiki_app;
  20. public $dich;
  21. public $morgan;
  22. public $ripkey;
  23. public $mtf;
  24. public $title;
  25. private function initConnection() {
  26. // connect to db
  27. if ($this->connect ( DB_DSN, DB_USER, DB_PASSWORD ) == 0)
  28. die ( "Could not connect to db" );
  29. }
  30. function addNew($attempt,$quest_id,$checked){
  31. $this->initConnection();
  32. $this->dbInsert('test_results', array ('quest_id' => $quest_id, 'attempt_id' => $attempt, 'checked' => $checked));
  33. }
  34. function getLast(){
  35. $this->initConnection();
  36. $test_id = $this->dbGetOne( 'SELECT test_id FROM test_results order by test_id DESC');
  37. return $test_id+1;
  38. }
  39. function create(){
  40. $this->initConnection();
  41. $this->dbInsert('testing', array (
  42. 'attempt_id' => $this->attempt,
  43. 'user_id' => $this->user_id,
  44. 'item_id' => $this->deck_id,
  45. 'mode' => $this->mode,
  46. 'limit' => $this->limit,
  47. 'type' => $this->type
  48. ));
  49. }
  50. function finish($attempt, $deck_id){
  51. $this->initConnection();
  52. $this->dbQuery ( 'UPDATE testing SET wiki_app=:wiki_app, ripkey=:ripkey, mtf=:mtf, morgan=:morgan, dich=:dich, max_points=:max_points WHERE attempt_id=:attempt AND item_id=:deck_id', array (
  53. 'attempt'=>$attempt,
  54. 'deck_id' => $deck_id,
  55. 'wiki_app' => $this->wiki_app,
  56. 'ripkey' => $this->ripkey,
  57. 'mtf' => $this->mtf,
  58. 'morgan' => $this->morgan,
  59. 'dich' => $this->dich,
  60. 'max_points' => $this->max_points
  61. ) );
  62. }
  63. function getLastAttempt(){
  64. $this->initConnection();
  65. $last_attempt = 0;
  66. $last_attempt = $this->dbGetOne('SELECT max(attempt_id) FROM testing');
  67. return $last_attempt;
  68. }
  69. function rank(){
  70. $item_id = $this->deck_id;
  71. $model_array = array('wiki_app','mtf','morgan','ripkey','dich');
  72. $res = array();
  73. foreach($model_array as $model){
  74. $rank = 1;
  75. $percentage = $model.'/max_points';
  76. $result = $this->dbQuery('SELECT attempt_id,' . $percentage .' AS points FROM testing WHERE item_id=:item_id AND max_points > 0 ORDER BY ' . $percentage . ' DESC', array('item_id' => $item_id));
  77. for ($i=0; $i<count($result); $i++){
  78. $attempt_id = $result[$i]['attempt_id'];
  79. if ($i > 0){
  80. $points = $result[$i-1]['points'];
  81. if ($points-$result[$i]['points']){
  82. $rank++;
  83. }
  84. }
  85. $res[$attempt_id][$model]['points'] = round($result[$i]['points'],3);
  86. $res[$attempt_id][$model]['rank'] = $rank;
  87. }
  88. }
  89. return $res;
  90. }
  91. function getEmptyAttempt(){
  92. $attempt_id = $this->dbGetOne('SELECT attempt_id FROM testing WHERE user_id=0 ORDER BY attempt_id');
  93. return $attempt_id;
  94. }
  95. function getEmptyAttempts(){
  96. $result = $this->dbQuery('SELECT attempt_id FROM testing WHERE user_id=0');
  97. $res_array = array();
  98. foreach ($result as $att){
  99. $res_array[] = $att['attempt_id'];
  100. }
  101. return $res_array;
  102. }
  103. function getAttemptDecks(){
  104. $result = array();
  105. $result = $this->dbQuery('SELECT item_id FROM testing WHERE attempt_id=:attempt_id', array('attempt_id'=> $this->attempt));
  106. return $result;
  107. }
  108. function createTestFromBD($deck_id){
  109. $this->initConnection();
  110. $questions_bd = array();
  111. $questions_bd_array = array();
  112. $questions = array();
  113. $questions_bd = $this->dbQuery('SELECT quest_id FROM test_results WHERE attempt_id=:attempt_id', array('attempt_id' => $this->attempt));
  114. if (count($questions_bd)){
  115. $questions_bd_array = array();
  116. $deck = new Deck();
  117. $deck->createFromIDLite ( $deck_id );
  118. $questions = array();
  119. $questions = $deck->getQuestions ();
  120. $result = array();
  121. foreach ($questions_bd as $question_id){
  122. $questions_bd_array [] = $question_id['quest_id'];
  123. }
  124. foreach($questions['accepted'] as $question){
  125. if (in_array($question->id, $questions_bd_array)){
  126. $result [] = $question;
  127. }
  128. }
  129. $this->max_points = 0;
  130. $this->wiki_app = 0;
  131. $this->morgan = 0;
  132. $this->ripkey = 0;
  133. $this->mtf = 0;
  134. $this->dich = 0;
  135. if (count($result)){
  136. foreach ($result as $question){
  137. $checked_string = $this->dbGetOne('SELECT checked FROM test_results WHERE attempt_id=:attempt_id AND quest_id=:quest_id', array('attempt_id' => $this->attempt, 'quest_id' =>$question->id));
  138. $checked_res = array();
  139. $checked = array();
  140. $checked = explode(',',$checked_string);
  141. $checked_res = array();
  142. foreach ($checked as $answer){
  143. if ($answer != '') {
  144. $checked_res [] = $answer;
  145. }
  146. }
  147. $this->countAllModels($question, $checked_res);
  148. }
  149. $this->dbQuery('UPDATE testing SET user_id=100500 WHERE item_id=:item_id AND attempt_id=:attempt', array('item_id'=>$deck_id, 'attempt' =>$this->attempt));
  150. $this->finish($this->attempt, $deck_id);
  151. }else{
  152. $this->dbQuery('DELETE FROM testing WHERE attempt_id=:attempt_id AND item_id=:item_id', array('attempt_id' => $this->attempt, 'item_id'=>$deck_id));
  153. }
  154. }else {
  155. $this->dbQuery('DELETE FROM testing WHERE attempt_id=:attempt_id AND item_id=:item_id', array('attempt_id' => $this->attempt, 'item_id'=>$deck_id));
  156. }
  157. }
  158. function countAllModels($question, $checked){
  159. $difficulty = $question->difficulty;
  160. $count_correct = 0;
  161. $all = count($question->getAnswers());
  162. $count_correct_points = 0;
  163. $count_correct = 0;
  164. $mtf_points = 0;
  165. $ripkey_result = false;
  166. $morgan_points = 0;
  167. $dich_points = $difficulty;
  168. $penalty = 0;
  169. echo $question->id;
  170. print_r($checked);
  171. echo '<br>';
  172. if (count($checked)){
  173. $correct_answers = $question->getRightAnswers();
  174. $correct = count($correct_answers);
  175. foreach ($correct_answers as $answer){
  176. if (in_array($answer['id'], $checked)){
  177. $count_correct_points+= $difficulty/$correct;
  178. $mtf_points+= $difficulty/$all;
  179. }
  180. if (!in_array($answer['id'], $checked)){
  181. $dich_points = 0;
  182. }
  183. if (count($checked) > $correct){
  184. $dich_points = 0;
  185. $ripkey_result = false ;
  186. $penalty = $difficulty*((count($checked) - $correct)/($all - $correct));
  187. }else{
  188. $ripkey_result = true;
  189. $penalty = 0;
  190. }
  191. $count_correct = $count_correct_points;
  192. $morgan_points = $count_correct;
  193. }
  194. $distractors = $question->getDistractors();
  195. foreach ($distractors as $distractor){
  196. if (in_array($distractor['id'], $checked)){
  197. $morgan_points-= $difficulty/count($distractors);
  198. }
  199. if (!in_array($distractor['id'], $checked)){
  200. $mtf_points+= $difficulty/$all;
  201. }
  202. }
  203. if ($mtf_points > 0)
  204. $this->mtf += $mtf_points;
  205. if ($ripkey_result) {
  206. $this->ripkey += $count_correct;
  207. }
  208. if ($morgan_points > 0 ){
  209. $this->morgan += $morgan_points;
  210. }
  211. $this->dich += $dich_points;
  212. if ($count_correct > $penalty){
  213. $this->wiki_app += ($count_correct - $penalty);
  214. }
  215. }
  216. $this->max_points += $difficulty;
  217. }
  218. }
  219. ?>