PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/essay.php

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