| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of elements
- *
- * @author gregory
- */
- class elements2 {
-
- private $user;
- /**
- * Setup new numbers engine
- *
- * @param bool $test Enable debug mode.
- */
- public function __construct($user) {
-
- $this->user = $user;
- }
- public function getElements() {
- $difficulty = $this->difficulty();
- $switchValue = rand(0,(int)$difficulty['percent']/20);
- $question = $this->elementSwitch($switchValue,$difficulty);
- $questionHtml = $this->htmlify($question);
-
- e107::getDb()->insert('tkLastQuestion', ['user'=>$this->user,'LastQuestionHtml'=>base64_encode($questionHtml)]);
-
- e107::getRender()->tablerender('Elements', $questionHtml);
-
-
-
-
-
-
-
-
- }
-
- private function elementSwitch($switchValue,$difficulty) {
- switch ($switchValue) {
-
- case 0:
- return $this->elementValues('symbol',$difficulty);
- break;
- case 1:
- return $this->elementValues('phase',$difficulty);
- break;
- case 2:
- return $this->elementValues('name',$difficulty);
- break;
- case 3:
- return $this->elementValues('number',$difficulty);
- break;
- case 4:
- default:
- return $this->elementValues('atomic_mass',$difficulty);
- break;
- }
- }
- /**
- * Return the difficulty based on previously answered questions.
- *
- * @return int Difficulty as percent.
- */
- private function difficulty() {
- $stats = e107::getDb()->retrieve("SELECT (SUM(correct)/COUNT(correct)*100) as percent,COUNT(correct) as count
- FROM e107.`e107_tkReport` WHERE
- answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
- AND username = '$this->user' AND subject = 'elements';");
-
- if ($stats["count"]==0) {
- return 1;
- } else {
- return $stats;
- }
-
- }
-
- private function elementValues($sign,$difficulty) {
-
- if($difficulty['count']>=300) {
- $incrementalNumber = mt_rand(1, 100 );
- }elseif($difficulty['count']>=200){
- $incrementalNumber = mt_rand(1, 50 );
- }elseif($difficulty['count']>=100){
- $incrementalNumber = mt_rand(1, 20 );
- }else{
- $incrementalNumber = mt_rand(1, 10);
- }
- $elements = json_decode(file_get_contents('resources/periodicTable.json'), true)['elements'];
-
- $selectedElement = rand(0, (int) ($incrementalNumber * 1.18));
-
- $TempQuestion = [];
-
- $TempQuestion['element'] = $elements[$selectedElement];
- $TempQuestion[] = $selectedElement;
- $TempQuestion[] = $sign;
-
-
- return [ 'answer1'=>$TempQuestion['element'][$sign], 'question'=>$TempQuestion, 'type'=>'elements'];
-
- }
- private function htmlify($question) {
-
- $frm = e107::getForm();
- $tp = e107::getParser();
- $sc = e107::getScBatch('timekeeper', TRUE);
- $fString = $frm->open('questionForm', 'post', '', ['autocomplete' => 'off', 'class' => 'formclass']);
- $fString .= $frm->label('<h4>Elements</h4>') . "</br>\n";
- $fString .= $frm->label('Periodic Table of Elements. Fill in the missing information.') . "\n";
- $fString .= '</br>';
-
- $elementData = $question['question']['element'];
- $elementData[$question['question'][1]] = '???';
- $sc->setVars($elementData);
- $fString .= $tp->parseTemplate(file_get_contents('templates/elementTable.html'), true, $sc);
-
- $fString .= '</br></br>';
- $fString .= $frm->text('a1', '', 80, ['placeholder' => 'Write answer here!', 'autocomplete' => "off", 'spellcheck' => "false"]) . "\n";
- $fString .= $frm->hidden('checkerData', TkAnswerStore::issue($this->user, $question), ['id' => 'checkerData']);
- $fString .= $frm->hidden('subject', 'elements', ['id' => 'elements']);
- $fString .= '</br>' . "\n";
- $fString .= e107::getForm()->button('Answer', 'Check answer') . "\n";
- $fString .= e107::getForm()->close();
- $fString .= '</br></br>';
-
- return $fString;
-
- }
- }
|