[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/lesson/pagetypes/ -> essay.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Essay
  20   *
  21   * @package mod_lesson
  22   * @copyright  2009 Sam Hemelryk
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   **/
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /** Essay question type */
  29  define("LESSON_PAGE_ESSAY", "10");
  30  
  31  class lesson_page_type_essay extends lesson_page {
  32  
  33      protected $type = lesson_page::TYPE_QUESTION;
  34      protected $typeidstring = 'essay';
  35      protected $typeid = LESSON_PAGE_ESSAY;
  36      protected $string = null;
  37  
  38      public function get_typeid() {
  39          return $this->typeid;
  40      }
  41      public function get_typestring() {
  42          if ($this->string===null) {
  43              $this->string = get_string($this->typeidstring, 'lesson');
  44          }
  45          return $this->string;
  46      }
  47      public function get_idstring() {
  48          return $this->typeidstring;
  49      }
  50      public function display($renderer, $attempt) {
  51          global $PAGE, $CFG, $USER;
  52  
  53          $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents(), 'lessonid'=>$this->lesson->id));
  54  
  55          $data = new stdClass;
  56          $data->id = $PAGE->cm->id;
  57          $data->pageid = $this->properties->id;
  58          if (isset($USER->modattempts[$this->lesson->id])) {
  59              $essayinfo = unserialize($attempt->useranswer);
  60              $data->answer = $essayinfo->answer;
  61          }
  62          $mform->set_data($data);
  63          return $mform->display();
  64      }
  65      public function create_answers($properties) {
  66          global $DB;
  67          // now add the answers
  68          $newanswer = new stdClass;
  69          $newanswer->lessonid = $this->lesson->id;
  70          $newanswer->pageid = $this->properties->id;
  71          $newanswer->timecreated = $this->properties->timecreated;
  72  
  73          if (isset($properties->jumpto[0])) {
  74              $newanswer->jumpto = $properties->jumpto[0];
  75          }
  76          if (isset($properties->score[0])) {
  77              $newanswer->score = $properties->score[0];
  78          }
  79          $newanswer->id = $DB->insert_record("lesson_answers", $newanswer);
  80          $answers = array($newanswer->id => new lesson_page_answer($newanswer));
  81          $this->answers = $answers;
  82          return $answers;
  83      }
  84      public function check_answer() {
  85          global $PAGE, $CFG;
  86          $result = parent::check_answer();
  87          $result->isessayquestion = true;
  88  
  89          $mform = new lesson_display_answer_form_essay($CFG->wwwroot.'/mod/lesson/continue.php', array('contents'=>$this->get_contents()));
  90          $data = $mform->get_data();
  91          require_sesskey();
  92  
  93          if (!$data) {
  94              redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$this->properties->id)));
  95          }
  96  
  97          if (is_array($data->answer)) {
  98              $studentanswer = $data->answer['text'];
  99              $studentanswerformat = $data->answer['format'];
 100          } else {
 101              $studentanswer = $data->answer;
 102              $studentanswerformat = FORMAT_MOODLE;
 103          }
 104  
 105          if (trim($studentanswer) === '') {
 106              $result->noanswer = true;
 107              return $result;
 108          }
 109  
 110          $answers = $this->get_answers();
 111          foreach ($answers as $answer) {
 112              $result->answerid = $answer->id;
 113              $result->newpageid = $answer->jumpto;
 114          }
 115  
 116          $userresponse = new stdClass;
 117          $userresponse->sent=0;
 118          $userresponse->graded = 0;
 119          $userresponse->score = 0;
 120          $userresponse->answer = $studentanswer;
 121          $userresponse->answerformat = $studentanswerformat;
 122          $userresponse->response = "";
 123          $result->userresponse = serialize($userresponse);
 124          $result->studentanswerformat = $studentanswerformat;
 125          $result->studentanswer = s($studentanswer);
 126          return $result;
 127      }
 128      public function update($properties, $context = null, $maxbytes = null) {
 129          global $DB, $PAGE;
 130          $answers  = $this->get_answers();
 131          $properties->id = $this->properties->id;
 132          $properties->lessonid = $this->lesson->id;
 133          $properties->timemodified = time();
 134          $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes), context_module::instance($PAGE->cm->id), 'mod_lesson', 'page_contents', $properties->id);
 135          $DB->update_record("lesson_pages", $properties);
 136  
 137          if (!array_key_exists(0, $this->answers)) {
 138              $this->answers[0] = new stdClass;
 139              $this->answers[0]->lessonid = $this->lesson->id;
 140              $this->answers[0]->pageid = $this->id;
 141              $this->answers[0]->timecreated = $this->timecreated;
 142          }
 143          if (isset($properties->jumpto[0])) {
 144              $this->answers[0]->jumpto = $properties->jumpto[0];
 145          }
 146          if (isset($properties->score[0])) {
 147              $this->answers[0]->score = $properties->score[0];
 148          }
 149          if (!isset($this->answers[0]->id)) {
 150              $this->answers[0]->id =  $DB->insert_record("lesson_answers", $this->answers[0]);
 151          } else {
 152              $DB->update_record("lesson_answers", $this->answers[0]->properties());
 153          }
 154  
 155          return true;
 156      }
 157      public function stats(array &$pagestats, $tries) {
 158          if(count($tries) > $this->lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
 159              $temp = $tries[$this->lesson->maxattempts - 1];
 160          } else {
 161              // else, user attempted the question less than the max, so grab the last one
 162              $temp = end($tries);
 163          }
 164          $essayinfo = unserialize($temp->useranswer);
 165          if ($essayinfo->graded) {
 166              if (isset($pagestats[$temp->pageid])) {
 167                  $essaystats = $pagestats[$temp->pageid];
 168                  $essaystats->totalscore += $essayinfo->score;
 169                  $essaystats->total++;
 170                  $pagestats[$temp->pageid] = $essaystats;
 171              } else {
 172                  $essaystats = new stdClass();
 173                  $essaystats->totalscore = $essayinfo->score;
 174                  $essaystats->total = 1;
 175                  $pagestats[$temp->pageid] = $essaystats;
 176              }
 177          }
 178          return true;
 179      }
 180      public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
 181          $answers = $this->get_answers();
 182          $formattextdefoptions = new stdClass;
 183          $formattextdefoptions->para = false;  //I'll use it widely in this page
 184          $formattextdefoptions->context = $answerpage->context;
 185  
 186          foreach ($answers as $answer) {
 187              if ($useranswer != null) {
 188                  $essayinfo = unserialize($useranswer->useranswer);
 189                  if ($essayinfo->response == null) {
 190                      $answerdata->response = get_string("nocommentyet", "lesson");
 191                  } else {
 192                      $answerdata->response = s($essayinfo->response);
 193                  }
 194                  if (isset($pagestats[$this->properties->id])) {
 195                      $percent = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total * 100;
 196                      $percent = round($percent, 2);
 197                      $percent = get_string("averagescore", "lesson").": ". $percent ."%";
 198                  } else {
 199                      // dont think this should ever be reached....
 200                      $percent = get_string("nooneansweredthisquestion", "lesson");
 201                  }
 202                  if ($essayinfo->graded) {
 203                      if ($this->lesson->custom) {
 204                          $answerdata->score = get_string("pointsearned", "lesson").": ".$essayinfo->score;
 205                      } elseif ($essayinfo->score) {
 206                          $answerdata->score = get_string("receivedcredit", "lesson");
 207                      } else {
 208                          $answerdata->score = get_string("didnotreceivecredit", "lesson");
 209                      }
 210                  } else {
 211                      $answerdata->score = get_string("havenotgradedyet", "lesson");
 212                  }
 213              } else {
 214                  $essayinfo = new stdClass();
 215                  $essayinfo->answer = get_string("didnotanswerquestion", "lesson");
 216                  $essayinfo->answerformat = null;
 217              }
 218  
 219              if (isset($pagestats[$this->properties->id])) {
 220                  $avescore = $pagestats[$this->properties->id]->totalscore / $pagestats[$this->properties->id]->total;
 221                  $avescore = round($avescore, 2);
 222                  $avescore = get_string("averagescore", "lesson").": ". $avescore ;
 223              } else {
 224                  // dont think this should ever be reached....
 225                  $avescore = get_string("nooneansweredthisquestion", "lesson");
 226              }
 227              $answerdata->answers[] = array(format_text($essayinfo->answer, $essayinfo->answerformat, $formattextdefoptions), $avescore);
 228              $answerpage->answerdata = $answerdata;
 229          }
 230          return $answerpage;
 231      }
 232      public function is_unanswered($nretakes) {
 233          global $DB, $USER;
 234          if (!$DB->count_records("lesson_attempts", array('pageid'=>$this->properties->id, 'userid'=>$USER->id, 'retry'=>$nretakes))) {
 235              return true;
 236          }
 237          return false;
 238      }
 239      public function requires_manual_grading() {
 240          return true;
 241      }
 242      public function get_earnedscore($answers, $attempt) {
 243          $essayinfo = unserialize($attempt->useranswer);
 244          return $essayinfo->score;
 245      }
 246  }
 247  
 248  class lesson_add_page_form_essay extends lesson_add_page_form_base {
 249  
 250      public $qtype = 'essay';
 251      public $qtypestring = 'essay';
 252  
 253      public function custom_definition() {
 254  
 255          $this->add_jumpto(0);
 256          $this->add_score(0, null, 1);
 257  
 258      }
 259  }
 260  
 261  class lesson_display_answer_form_essay extends moodleform {
 262  
 263      public function definition() {
 264          global $USER, $OUTPUT;
 265          $mform = $this->_form;
 266          $contents = $this->_customdata['contents'];
 267  
 268          $hasattempt = false;
 269          $attrs = '';
 270          $useranswer = '';
 271          $useranswerraw = '';
 272          if (isset($this->_customdata['lessonid'])) {
 273              $lessonid = $this->_customdata['lessonid'];
 274              if (isset($USER->modattempts[$lessonid]->useranswer) && !empty($USER->modattempts[$lessonid]->useranswer)) {
 275                  $attrs = array('disabled' => 'disabled');
 276                  $hasattempt = true;
 277                  $useranswertemp = unserialize($USER->modattempts[$lessonid]->useranswer);
 278                  $useranswer = htmlspecialchars_decode($useranswertemp->answer, ENT_QUOTES);
 279                  $useranswerraw = $useranswertemp->answer;
 280              }
 281          }
 282  
 283          $mform->addElement('header', 'pageheader');
 284  
 285          $mform->addElement('html', $OUTPUT->container($contents, 'contents'));
 286  
 287          $options = new stdClass;
 288          $options->para = false;
 289          $options->noclean = true;
 290  
 291          $mform->addElement('hidden', 'id');
 292          $mform->setType('id', PARAM_INT);
 293  
 294          $mform->addElement('hidden', 'pageid');
 295          $mform->setType('pageid', PARAM_INT);
 296  
 297          if ($hasattempt) {
 298              $mform->addElement('hidden', 'answer', $useranswerraw);
 299              $mform->setType('answer', PARAM_RAW);
 300              $mform->addElement('html', $OUTPUT->container(get_string('youranswer', 'lesson'), 'youranswer'));
 301              $mform->addElement('html', $OUTPUT->container($useranswer, 'reviewessay'));
 302              $this->add_action_buttons(null, get_string("nextpage", "lesson"));
 303          } else {
 304              $mform->addElement('editor', 'answer', get_string('youranswer', 'lesson'), null, null);
 305              $mform->setType('answer', PARAM_RAW);
 306              $this->add_action_buttons(null, get_string("submit", "lesson"));
 307          }
 308      }
 309  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1