elements2.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of elements
  9. *
  10. * @author gregory
  11. */
  12. class elements2 {
  13. private $user;
  14. /**
  15. * Setup new numbers engine
  16. *
  17. * @param bool $test Enable debug mode.
  18. */
  19. public function __construct($user) {
  20. $this->user = $user;
  21. }
  22. public function getElements() {
  23. $difficulty = $this->difficulty();
  24. $switchValue = rand(0,(int)$difficulty['percent']/20);
  25. $question = $this->elementSwitch($switchValue,$difficulty);
  26. $questionHtml = $this->htmlify($question);
  27. e107::getDb()->insert('tkLastQuestion', ['user'=>$this->user,'LastQuestionHtml'=>base64_encode($questionHtml)]);
  28. e107::getRender()->tablerender('Elements', $questionHtml);
  29. }
  30. private function elementSwitch($switchValue,$difficulty) {
  31. switch ($switchValue) {
  32. case 0:
  33. return $this->elementValues('symbol',$difficulty);
  34. break;
  35. case 1:
  36. return $this->elementValues('phase',$difficulty);
  37. break;
  38. case 2:
  39. return $this->elementValues('name',$difficulty);
  40. break;
  41. case 3:
  42. return $this->elementValues('number',$difficulty);
  43. break;
  44. case 4:
  45. default:
  46. return $this->elementValues('atomic_mass',$difficulty);
  47. break;
  48. }
  49. }
  50. /**
  51. * Return the difficulty based on previously answered questions.
  52. *
  53. * @return int Difficulty as percent.
  54. */
  55. private function difficulty() {
  56. $stats = e107::getDb()->retrieve("SELECT (SUM(correct)/COUNT(correct)*100) as percent,COUNT(correct) as count
  57. FROM e107.`e107_tkReport` WHERE
  58. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  59. AND username = '$this->user' AND subject = 'elements';");
  60. if ($stats["count"]==0) {
  61. return 1;
  62. } else {
  63. return $stats;
  64. }
  65. }
  66. private function elementValues($sign,$difficulty) {
  67. if($difficulty['count']>=300) {
  68. $incrementalNumber = mt_rand(1, 100 );
  69. }elseif($difficulty['count']>=200){
  70. $incrementalNumber = mt_rand(1, 50 );
  71. }elseif($difficulty['count']>=100){
  72. $incrementalNumber = mt_rand(1, 20 );
  73. }else{
  74. $incrementalNumber = mt_rand(1, 10);
  75. }
  76. $elements = json_decode(file_get_contents('resources/periodicTable.json'), true)['elements'];
  77. $selectedElement = rand(0, (int) ($incrementalNumber * 1.18));
  78. $TempQuestion = [];
  79. $TempQuestion['element'] = $elements[$selectedElement];
  80. $TempQuestion[] = $selectedElement;
  81. $TempQuestion[] = $sign;
  82. return [ 'answer1'=>$TempQuestion['element'][$sign], 'question'=>$TempQuestion, 'type'=>'elements'];
  83. }
  84. private function htmlify($question) {
  85. $frm = e107::getForm();
  86. $tp = e107::getParser();
  87. $sc = e107::getScBatch('timekeeper', TRUE);
  88. $fString = $frm->open('questionForm', 'post', '', ['autocomplete' => 'off', 'class' => 'formclass']);
  89. $fString .= $frm->label('<h4>Elements</h4>') . "</br>\n";
  90. $fString .= $frm->label('Periodic Table of Elements. Fill in the missing information.') . "\n";
  91. $fString .= '</br>';
  92. $elementData = $question['question']['element'];
  93. $elementData[$question['question'][1]] = '???';
  94. $sc->setVars($elementData);
  95. $fString .= $tp->parseTemplate(file_get_contents('templates/elementTable.html'), true, $sc);
  96. $fString .= '</br></br>';
  97. $fString .= $frm->text('a1', '', 80, ['placeholder' => 'Write answer here!', 'autocomplete' => "off", 'spellcheck' => "false"]) . "\n";
  98. $fString .= $frm->hidden('checkerData', TkAnswerStore::issue($this->user, $question), ['id' => 'checkerData']);
  99. $fString .= $frm->hidden('subject', 'elements', ['id' => 'elements']);
  100. $fString .= '</br>' . "\n";
  101. $fString .= e107::getForm()->button('Answer', 'Check answer') . "\n";
  102. $fString .= e107::getForm()->close();
  103. $fString .= '</br></br>';
  104. return $fString;
  105. }
  106. }