Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/pagetypes/essay.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_ESSAY", "10");
00031 
00032 class lesson_page_type_essay extends lesson_page {
00033 
00034     protected $type = lesson_page::TYPE_QUESTION;
00035     protected $typeidstring = 'essay';
00036     protected $typeid = LESSON_PAGE_ESSAY;
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 $PAGE, $CFG, $USER;
00053 
00054         $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
00055 
00056         $data = new stdClass;
00057         $data->id = $PAGE->cm->id;
00058         $data->pageid = $this->properties->id;
00059         if (isset($USER->modattempts[$this->lesson->id])) {
00060             $essayinfo = unserialize($attempt->useranswer);
00061             $data->answer = $essayinfo->answer;
00062         }
00063         $mform->set_data($data);
00064         return $mform->display();
00065     }
00066     public function create_answers($properties) {
00067         global $DB;
00068         // now add the answers
00069         $newanswer = new stdClass;
00070         $newanswer->lessonid = $this->lesson->id;
00071         $newanswer->pageid = $this->properties->id;
00072         $newanswer->timecreated = $this->properties->timecreated;
00073 
00074         if (isset($properties->jumpto[0])) {
00075             $newanswer->jumpto = $properties->jumpto[0];
00076         }
00077         if (isset($properties->score[0])) {
00078             $newanswer->score = $properties->score[0];
00079         }
00080         $newanswer->id = $DB->insert_record("lesson_answers", $newanswer);
00081         $answers = array($newanswer->id => new lesson_page_answer($newanswer));
00082         $this->answers = $answers;
00083         return $answers;
00084     }
00085     public function check_answer() {
00086         global $PAGE, $CFG;
00087         $result = parent::check_answer();
00088         $result->isessayquestion = true;
00089 
00090         $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
00091         $data = $mform->get_data();
00092         require_sesskey();
00093 
00094         if (!$data) {
00095             redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
00096         }
00097 
00098         if (is_array($data->answer)) {
00099             $studentanswer = $data->answer['text'];
00100         } else {
00101             $studentanswer = $data->answer;
00102         }
00103 
00104         if (trim($studentanswer) === '') {
00105             $result->noanswer = true;
00106             return $result;
00107         }
00108 
00109         $answers = $this->get_answers();
00110         foreach ($answers as $answer) {
00111             $result->answerid = $answer->id;
00112             $result->newpageid = $answer->jumpto;
00113         }
00114 
00115         $userresponse = new stdClass;
00116         $userresponse->sent=0;
00117         $userresponse->graded = 0;
00118         $userresponse->score = 0;
00119         $userresponse->answer = $studentanswer;
00120         $userresponse->response = "";
00121         $result->userresponse = serialize($userresponse);
00122 
00123         $result->studentanswer = s($studentanswer);
00124         return $result;
00125     }
00126     public function update($properties) {
00127         global $DB, $PAGE;
00128         $answers  = $this->get_answers();
00129         $properties->id = $this->properties->id;
00130         $properties->lessonid = $this->lesson->id;
00131         $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), get_context_instance(CONTEXT_MODULE, $PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
00132         $DB->update_record("lesson_pages", $properties);
00133 
00134         if (!array_key_exists(0, $this->answers)) {
00135             $this->answers[0] = new stdClass;
00136             $this->answers[0]->lessonid = $this->lesson->id;
00137             $this->answers[0]->pageid = $this->id;
00138             $this->answers[0]->timecreated = $this->timecreated;
00139         }
00140         if (isset($properties->jumpto[0])) {
00141             $this->answers[0]->jumpto = $properties->jumpto[0];
00142         }
00143         if (isset($properties->score[0])) {
00144             $this->answers[0]->score = $properties->score[0];
00145         }
00146         if (!isset($this->answers[0]->id)) {
00147             $this->answers[0]->id =  $DB->insert_record("lesson_answers", $this->answers[0]);
00148         } else {
00149             $DB->update_record("lesson_answers", $this->answers[0]->properties());
00150         }
00151 
00152         return true;
00153     }
00154     public function stats(array &$pagestats, $tries) {
00155         if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
00156             $temp = $tries[$this->lesson->maxattempts - 1];
00157         } else {
00158             // else, user attempted the question less than the max, so grab the last one
00159             $temp = end($tries);
00160         }
00161         $essayinfo = unserialize($temp->useranswer);
00162         if ($essayinfo->graded) {
00163             if (isset($pagestats[$temp->pageid])) {
00164                 $essaystats = $pagestats[$temp->pageid];
00165                 $essaystats->totalscore += $essayinfo->score;
00166                 $essaystats->total++;
00167                 $pagestats[$temp->pageid] = $essaystats;
00168             } else {
00169                 $essaystats = new stdClass();
00170                 $essaystats->totalscore = $essayinfo->score;
00171                 $essaystats->total = 1;
00172                 $pagestats[$temp->pageid] = $essaystats;
00173             }
00174         }
00175         return true;
00176     }
00177     public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
00178         $answers = $this->get_answers();
00179         $formattextdefoptions = new stdClass;
00180         $formattextdefoptions->para = false;  //I'll use it widely in this page
00181         $formattextdefoptions->context = $answerpage->context;
00182 
00183         foreach ($answers as $answer) {
00184             if ($useranswer != NULL) {
00185                 $essayinfo = unserialize($useranswer->useranswer);
00186                 if ($essayinfo->response == NULL) {
00187                     $answerdata->response = get_string("nocommentyet", "lesson");
00188                 } else {
00189                     $answerdata->response = s($essayinfo->response);
00190                 }
00191                 if (isset($pagestats[$this->properties->id])) {
00192                     $percent = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total * 100;
00193                     $percent = round($percent, 2);
00194                     $percent = get_string("averagescore", "lesson").": ". $percent ."%";
00195                 } else {
00196                     // dont think this should ever be reached....
00197                     $percent = get_string("nooneansweredthisquestion", "lesson");
00198                 }
00199                 if ($essayinfo->graded) {
00200                     if ($this->lesson->custom) {
00201                         $answerdata->score = get_string("pointsearned", "lesson").": ".$essayinfo->score;
00202                     } elseif ($essayinfo->score) {
00203                         $answerdata->score = get_string("receivedcredit", "lesson");
00204                     } else {
00205                         $answerdata->score = get_string("didnotreceivecredit", "lesson");
00206                     }
00207                 } else {
00208                     $answerdata->score = get_string("havenotgradedyet", "lesson");
00209                 }
00210             } else {
00211                 $essayinfo = new stdClass();
00212                 $essayinfo->answer = get_string("didnotanswerquestion", "lesson");
00213             }
00214 
00215             if (isset($pagestats[$this->properties->id])) {
00216                 $avescore = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total;
00217                 $avescore = round($avescore, 2);
00218                 $avescore = get_string("averagescore", "lesson").": ". $avescore ;
00219             } else {
00220                 // dont think this should ever be reached....
00221                 $avescore = get_string("nooneansweredthisquestion", "lesson");
00222             }
00223             $answerdata->answers[] = array(format_text($essayinfo->answer, FORMAT_MOODLE, $formattextdefoptions), $avescore);
00224             $answerpage->answerdata = $answerdata;
00225         }
00226         return $answerpage;
00227     }
00228     public function is_unanswered($nretakes) {
00229         global $DB, $USER;
00230         if (!$DB->count_records("lesson_attempts", array('pageid'=>$this->properties->id, 'userid'=>$USER->id, 'retry'=>$nretakes))) {
00231             return true;
00232         }
00233         return false;
00234     }
00235     public function requires_manual_grading() {
00236         return true;
00237     }
00238     public function get_earnedscore($answers, $attempt) {
00239         $essayinfo = unserialize($attempt->useranswer);
00240         return $essayinfo->score;
00241     }
00242 }
00243 
00244 class lesson_add_page_form_essay extends lesson_add_page_form_base {
00245 
00246     public $qtype = 'essay';
00247     public $qtypestring = 'essay';
00248 
00249     public function custom_definition() {
00250 
00251         $this->add_jumpto(0);
00252         $this->add_score(0, null, 1);
00253 
00254     }
00255 }
00256 
00257 class lesson_display_answer_form_essay extends moodleform {
00258 
00259     public function definition() {
00260         global $USER, $OUTPUT;
00261         $mform = $this->_form;
00262         $contents = $this->_customdata['contents'];
00263 
00264         $hasattempt = false;
00265         $attrs = '';
00266         $useranswer = '';
00267         $useranswerraw = '';
00268         if (isset($this->_customdata['lessonid'])) {
00269             $lessonid = $this->_customdata['lessonid'];
00270             if (isset($USER->modattempts[$lessonid]->useranswer) && !empty($USER->modattempts[$lessonid]->useranswer)) {
00271                 $attrs = array('disabled' => 'disabled');
00272                 $hasattempt = true;
00273                 $useranswertemp = unserialize($USER->modattempts[$lessonid]->useranswer);
00274                 $useranswer = htmlspecialchars_decode($useranswertemp->answer, ENT_QUOTES);
00275                 $useranswerraw = $useranswertemp->answer;
00276             }
00277         }
00278 
00279         $mform->addElement('header', 'pageheader');
00280 
00281         $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
00282 
00283         $options = new stdClass;
00284         $options->para = false;
00285         $options->noclean = true;
00286 
00287         $mform->addElement('hidden', 'id');
00288         $mform->setType('id', PARAM_INT);
00289 
00290         $mform->addElement('hidden', 'pageid');
00291         $mform->setType('pageid', PARAM_INT);
00292 
00293         if ($hasattempt) {
00294             $mform->addElement('hidden', 'answer', $useranswerraw);
00295             $mform->setType('answer', PARAM_RAW);
00296             $mform->addElement('html', $OUTPUT->container(get_string('youranswer', 'lesson'), 'youranswer'));
00297             $mform->addElement('html', $OUTPUT->container($useranswer, 'reviewessay'));
00298             $this->add_action_buttons(null, get_string("nextpage", "lesson"));
00299         } else {
00300             $mform->addElement('editor', 'answer', get_string('youranswer', 'lesson'), null, null);
00301             $mform->setType('answer', PARAM_RAW);
00302             $this->add_action_buttons(null, get_string("submit", "lesson"));
00303         }
00304     }
00305 }
 All Data Structures Namespaces Files Functions Variables Enumerations