Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/pagetypes/numerical.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00030 define("LESSON_PAGE_NUMERICAL",     "8");
00031 
00032 class lesson_page_type_numerical extends lesson_page {
00033 
00034     protected $type = lesson_page::TYPE_QUESTION;
00035     protected $typeidstring = 'numerical';
00036     protected $typeid = LESSON_PAGE_NUMERICAL;
00037     protected $string = null;
00038 
00039     public function get_typeid() {
00040         return $this->typeid;
00041     }
00042     public function get_typestring() {
00043         if ($this->string===null) {
00044             $this->string = get_string($this->typeidstring, 'lesson');
00045         }
00046         return $this->string;
00047     }
00048     public function get_idstring() {
00049         return $this->typeidstring;
00050     }
00051     public function display($renderer, $attempt) {
00052         global $USER, $CFG, $PAGE;
00053         $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
00054         $data = new stdClass;
00055         $data->id = $PAGE->cm->id;
00056         $data->pageid = $this->properties->id;
00057         if (isset($USER->modattempts[$this->lesson->id])) {
00058             $data->answer = s($attempt->useranswer);
00059         }
00060         $mform->set_data($data);
00061         return $mform->display();
00062     }
00063     public function check_answer() {
00064         global $CFG;
00065         $result = parent::check_answer();
00066 
00067         $mform = new lesson_display_answer_form_shortanswer($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
00068         $data = $mform->get_data();
00069         require_sesskey();
00070 
00071         // set defaults
00072         $result->response = '';
00073         $result->newpageid = 0;
00074 
00075         if (isset($data->answer)) {
00076             // just doing default PARAM_RAW, not doing PARAM_INT because it could be a float
00077             $result->useranswer = (float)$data->answer;
00078         } else {
00079             $result->noanswer = true;
00080             return $result;
00081         }
00082         $result->studentanswer = $result->userresponse = $result->useranswer;
00083         $answers = $this->get_answers();
00084         foreach ($answers as $answer) {
00085             if (strpos($answer->answer, ':')) {
00086                 // there's a pairs of values
00087                 list($min, $max) = explode(':', $answer->answer);
00088                 $minimum = (float) $min;
00089                 $maximum = (float) $max;
00090             } else {
00091                 // there's only one value
00092                 $minimum = (float) $answer->answer;
00093                 $maximum = $minimum;
00094             }
00095             if (($result->useranswer >= $minimum) && ($result->useranswer <= $maximum)) {
00096                 $result->newpageid = $answer->jumpto;
00097                 $result->response = trim($answer->response);
00098                 if ($this->lesson->jumpto_is_correct($this->properties->id, $result->newpageid)) {
00099                     $result->correctanswer = true;
00100                 }
00101                 if ($this->lesson->custom) {
00102                     if ($answer->score > 0) {
00103                         $result->correctanswer = true;
00104                     } else {
00105                         $result->correctanswer = false;
00106                     }
00107                 }
00108                 $result->answerid = $answer->id;
00109                 return $result;
00110             }
00111         }
00112         return $result;
00113     }
00114 
00115     public function display_answers(html_table $table) {
00116         $answers = $this->get_answers();
00117         $options = new stdClass;
00118         $options->noclean = true;
00119         $options->para = false;
00120         $i = 1;
00121         foreach ($answers as $answer) {
00122             $cells = array();
00123             if ($this->lesson->custom && $answer->score > 0) {
00124                 // if the score is > 0, then it is correct
00125                 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
00126             } else if ($this->lesson->custom) {
00127                 $cells[] = '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
00128             } else if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
00129                 // underline correct answers
00130                 $cells[] = '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
00131             } else {
00132                 $cells[] = '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
00133             }
00134             $cells[] = format_text($answer->answer, $answer->answerformat, $options);
00135             $table->data[] = new html_table_row($cells);
00136 
00137             $cells = array();
00138             $cells[] = "<span class=\"label\">".get_string("response", "lesson")." $i</span>";
00139             $cells[] = format_text($answer->response, $answer->responseformat, $options);
00140             $table->data[] = new html_table_row($cells);
00141 
00142             $cells = array();
00143             $cells[] = "<span class=\"label\">".get_string("score", "lesson").'</span>';
00144             $cells[] = $answer->score;
00145             $table->data[] = new html_table_row($cells);
00146 
00147             $cells = array();
00148             $cells[] = "<span class=\"label\">".get_string("jump", "lesson").'</span>';
00149             $cells[] = $this->get_jump_name($answer->jumpto);
00150             $table->data[] = new html_table_row($cells);
00151             if ($i === 1){
00152                 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
00153             }
00154             $i++;
00155         }
00156         return $table;
00157     }
00158     public function stats(array &$pagestats, $tries) {
00159         if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
00160             $temp = $tries[$this->lesson->maxattempts - 1];
00161         } else {
00162             // else, user attempted the question less than the max, so grab the last one
00163             $temp = end($tries);
00164         }
00165         if (isset($pagestats[$temp->pageid][$temp->useranswer])) {
00166             $pagestats[$temp->pageid][$temp->useranswer]++;
00167         } else {
00168             $pagestats[$temp->pageid][$temp->useranswer] = 1;
00169         }
00170         if (isset($pagestats[$temp->pageid]["total"])) {
00171             $pagestats[$temp->pageid]["total"]++;
00172         } else {
00173             $pagestats[$temp->pageid]["total"] = 1;
00174         }
00175         return true;
00176     }
00177 
00178     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
00179         $answers = $this->get_answers();
00180         $formattextdefoptions = new stdClass;
00181         $formattextdefoptions->para = false;  //I'll use it widely in this page
00182         foreach ($answers as $answer) {
00183             if ($useranswer == null && $i == 0) {
00184                 // I have the $i == 0 because it is easier to blast through it all at once.
00185                 if (isset($pagestats[$this->properties->id])) {
00186                     $stats = $pagestats[$this->properties->id];
00187                     $total = $stats["total"];
00188                     unset($stats["total"]);
00189                     foreach ($stats as $valentered => $ntimes) {
00190                         $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($valentered).'" />';
00191                         $percent = $ntimes / $total * 100;
00192                         $percent = round($percent, 2);
00193                         $percent .= "% ".get_string("enteredthis", "lesson");
00194                         $answerdata->answers[] = array($data, $percent);
00195                     }
00196                 } else {
00197                     $answerdata->answers[] = array(get_string("nooneansweredthisquestion", "lesson"), " ");
00198                 }
00199                 $i++;
00200             } else if ($useranswer != null && ($answer->id == $useranswer->answerid || ($answer == end($answers) && empty($answerdata)))) {
00201                  // get in here when what the user entered is not one of the answers
00202                 $data = '<input type="text" size="50" disabled="disabled" readonly="readonly" value="'.s($useranswer->useranswer).'">';
00203                 if (isset($pagestats[$this->properties->id][$useranswer->useranswer])) {
00204                     $percent = $pagestats[$this->properties->id][$useranswer->useranswer] / $pagestats[$this->properties->id]["total"] * 100;
00205                     $percent = round($percent, 2);
00206                     $percent .= "% ".get_string("enteredthis", "lesson");
00207                 } else {
00208                     $percent = get_string("nooneenteredthis", "lesson");
00209                 }
00210                 $answerdata->answers[] = array($data, $percent);
00211 
00212                 if ($answer->id == $useranswer->answerid) {
00213                     if ($answer->response == NULL) {
00214                         if ($useranswer->correct) {
00215                             $answerdata->response = get_string("thatsthecorrectanswer", "lesson");
00216                         } else {
00217                             $answerdata->response = get_string("thatsthewronganswer", "lesson");
00218                         }
00219                     } else {
00220                         $answerdata->response = $answer->response;
00221                     }
00222                     if ($this->lesson->custom) {
00223                         $answerdata->score = get_string("pointsearned", "lesson").": ".$answer->score;
00224                     } elseif ($useranswer->correct) {
00225                         $answerdata->score = get_string("receivedcredit", "lesson");
00226                     } else {
00227                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
00228                     }
00229                 } else {
00230                     $answerdata->response = get_string("thatsthewronganswer", "lesson");
00231                     if ($this->lesson->custom) {
00232                         $answerdata->score = get_string("pointsearned", "lesson").": 0";
00233                     } else {
00234                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
00235                     }
00236                 }
00237             }
00238             $answerpage->answerdata = $answerdata;
00239         }
00240         return $answerpage;
00241     }
00242 }
00243 
00244 class lesson_add_page_form_numerical extends lesson_add_page_form_base {
00245 
00246     public $qtype = 'numerical';
00247     public $qtypestring = 'numerical';
00248 
00249     public function custom_definition() {
00250         for ($i = 0; $i < $this->_customdata['lesson']->maxanswers; $i++) {
00251             $this->_form->addElement('header', 'answertitle'.$i, get_string('answer').' '.($i+1));
00252             $this->add_answer($i, NULL, ($i < 1));
00253             $this->add_response($i);
00254             $this->add_jumpto($i, NULL, ($i == 0 ? LESSON_NEXTPAGE : LESSON_THISPAGE));
00255             $this->add_score($i, null, ($i===0)?1:0);
00256         }
00257     }
00258 }
00259 
00260 class lesson_display_answer_form_numerical extends moodleform {
00261 
00262     public function definition() {
00263         global $USER, $OUTPUT;
00264         $mform = $this->_form;
00265         $contents = $this->_customdata['contents'];
00266 
00267         $mform->addElement('header', 'pageheader');
00268 
00269         $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
00270 
00271         $hasattempt = false;
00272         $attrs = array('size'=>'50', 'maxlength'=>'200');
00273         if (isset($this->_customdata['lessonid'])) {
00274             $lessonid = $this->_customdata['lessonid'];
00275             if (isset($USER->modattempts[$lessonid]->useranswer)) {
00276                 $attrs['readonly'] = 'readonly';
00277                 $hasattempt = true;
00278             }
00279         }
00280         $options = new stdClass;
00281         $options->para = false;
00282         $options->noclean = true;
00283 
00284         $mform->addElement('hidden', 'id');
00285         $mform->setType('id', PARAM_INT);
00286 
00287         $mform->addElement('hidden', 'pageid');
00288         $mform->setType('pageid', PARAM_INT);
00289 
00290         $mform->addElement('text', 'answer', get_string('youranswer', 'lesson'), $attrs);
00291         $mform->setType('answer', PARAM_FLOAT);
00292 
00293         if ($hasattempt) {
00294             $this->add_action_buttons(null, get_string("nextpage", "lesson"));
00295         } else {
00296             $this->add_action_buttons(null, get_string("submit", "lesson"));
00297         }
00298     }
00299 
00300 }
 All Data Structures Namespaces Files Functions Variables Enumerations