PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/pagetypes/branchtable.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 339 lines | 260 code | 38 blank | 41 comment | 43 complexity | c1c00f877db049730eaf4b5e4e27d67a 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. * Branch Table
  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. /** Branch Table page */
  26. define("LESSON_PAGE_BRANCHTABLE", "20");
  27. class lesson_page_type_branchtable extends lesson_page {
  28. protected $type = lesson_page::TYPE_STRUCTURE;
  29. protected $typeid = LESSON_PAGE_BRANCHTABLE;
  30. protected $typeidstring = 'branchtable';
  31. protected $string = null;
  32. protected $jumpto = null;
  33. public function get_typeid() {
  34. return $this->typeid;
  35. }
  36. public function get_typestring() {
  37. if ($this->string===null) {
  38. $this->string = get_string($this->typeidstring, 'lesson');
  39. }
  40. return $this->string;
  41. }
  42. /**
  43. * Gets an array of the jumps used by the answers of this page
  44. *
  45. * @return array
  46. */
  47. public function get_jumps() {
  48. global $DB;
  49. $jumps = array();
  50. $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id);
  51. if ($answers = $this->get_answers()) {
  52. foreach ($answers as $answer) {
  53. if ($answer->answer === '') {
  54. // show only jumps for real branches (==have description)
  55. continue;
  56. }
  57. $jumps[] = $this->get_jump_name($answer->jumpto);
  58. }
  59. } else {
  60. // We get here is the lesson was created on a Moodle 1.9 site and
  61. // the lesson contains question pages without any answers.
  62. $jumps[] = $this->get_jump_name($this->properties->nextpageid);
  63. }
  64. return $jumps;
  65. }
  66. public static function get_jumptooptions($firstpage, lesson $lesson) {
  67. global $DB, $PAGE;
  68. $jump = array();
  69. $jump[0] = get_string("thispage", "lesson");
  70. $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson");
  71. $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson");
  72. $jump[LESSON_EOL] = get_string("endoflesson", "lesson");
  73. $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson");
  74. $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson");
  75. $jump[LESSON_RANDOMBRANCH] = get_string("randombranch", "lesson");
  76. if (!$firstpage) {
  77. if (!$apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0))) {
  78. print_error('cannotfindfirstpage', 'lesson');
  79. }
  80. while (true) {
  81. if ($apageid) {
  82. $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid));
  83. $jump[$apageid] = $title;
  84. $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid));
  85. } else {
  86. // last page reached
  87. break;
  88. }
  89. }
  90. }
  91. return $jump;
  92. }
  93. public function get_idstring() {
  94. return $this->typeidstring;
  95. }
  96. public function display($renderer, $attempt) {
  97. global $PAGE, $CFG;
  98. $output = '';
  99. $options = new stdClass;
  100. $options->para = false;
  101. $options->noclean = true;
  102. if ($this->lesson->slideshow) {
  103. $output .= $renderer->slideshow_start($this->lesson);
  104. }
  105. // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
  106. $output .= $renderer->heading(format_string($this->properties->title), 3);
  107. $output .= $renderer->box($this->get_contents(), 'contents');
  108. $buttons = array();
  109. $i = 0;
  110. foreach ($this->get_answers() as $answer) {
  111. if ($answer->answer === '') {
  112. // not a branch!
  113. continue;
  114. }
  115. $params = array();
  116. $params['id'] = $PAGE->cm->id;
  117. $params['pageid'] = $this->properties->id;
  118. $params['sesskey'] = sesskey();
  119. $params['jumpto'] = $answer->jumpto;
  120. $url = new moodle_url('/mod/lesson/continue.php', $params);
  121. $buttons[] = $renderer->single_button($url, strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)));
  122. $i++;
  123. }
  124. // Set the orientation
  125. if ($this->properties->layout) {
  126. $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer horizontal');
  127. } else {
  128. $buttonshtml = $renderer->box(implode("\n", $buttons), 'branchbuttoncontainer vertical');
  129. }
  130. $output .= $buttonshtml;
  131. if ($this->lesson->slideshow) {
  132. $output .= $renderer->slideshow_end();
  133. }
  134. return $output;
  135. }
  136. public function check_answer() {
  137. global $USER, $DB, $PAGE, $CFG;
  138. require_sesskey();
  139. $newpageid = optional_param('jumpto', null, PARAM_INT);
  140. // going to insert into lesson_branch
  141. if ($newpageid == LESSON_RANDOMBRANCH) {
  142. $branchflag = 1;
  143. } else {
  144. $branchflag = 0;
  145. }
  146. if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $this->lesson->id, "userid" => $USER->id), "grade DESC")) {
  147. $retries = count($grades);
  148. } else {
  149. $retries = 0;
  150. }
  151. $branch = new stdClass;
  152. $branch->lessonid = $this->lesson->id;
  153. $branch->userid = $USER->id;
  154. $branch->pageid = $this->properties->id;
  155. $branch->retry = $retries;
  156. $branch->flag = $branchflag;
  157. $branch->timeseen = time();
  158. $DB->insert_record("lesson_branch", $branch);
  159. // this is called when jumping to random from a branch table
  160. $context = context_module::instance($PAGE->cm->id);
  161. if($newpageid == LESSON_UNSEENBRANCHPAGE) {
  162. if (has_capability('mod/lesson:manage', $context)) {
  163. $newpageid = LESSON_NEXTPAGE;
  164. } else {
  165. $newpageid = lesson_unseen_question_jump($this->lesson, $USER->id, $this->properties->id); // this may return 0
  166. }
  167. }
  168. // convert jumpto page into a proper page id
  169. if ($newpageid == 0) {
  170. $newpageid = $this->properties->id;
  171. } elseif ($newpageid == LESSON_NEXTPAGE) {
  172. if (!$newpageid = $this->nextpageid) {
  173. // no nextpage go to end of lesson
  174. $newpageid = LESSON_EOL;
  175. }
  176. } elseif ($newpageid == LESSON_PREVIOUSPAGE) {
  177. $newpageid = $this->prevpageid;
  178. } elseif ($newpageid == LESSON_RANDOMPAGE) {
  179. $newpageid = lesson_random_question_jump($this->lesson, $this->properties->id);
  180. } elseif ($newpageid == LESSON_RANDOMBRANCH) {
  181. $newpageid = lesson_unseen_branch_jump($this->lesson, $USER->id);
  182. }
  183. // no need to record anything in lesson_attempts
  184. redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id,'pageid'=>$newpageid)));
  185. }
  186. public function display_answers(html_table $table) {
  187. $answers = $this->get_answers();
  188. $options = new stdClass;
  189. $options->noclean = true;
  190. $options->para = false;
  191. $i = 1;
  192. foreach ($answers as $answer) {
  193. if ($answer->answer === '') {
  194. // not a branch!
  195. continue;
  196. }
  197. $cells = array();
  198. $cells[] = "<span class=\"label\">".get_string("branch", "lesson")." $i<span>: ";
  199. $cells[] = format_text($answer->answer, $answer->answerformat, $options);
  200. $table->data[] = new html_table_row($cells);
  201. $cells = array();
  202. $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
  203. $cells[] = $this->get_jump_name($answer->jumpto);
  204. $table->data[] = new html_table_row($cells);
  205. if ($i === 1){
  206. $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
  207. }
  208. $i++;
  209. }
  210. return $table;
  211. }
  212. public function get_grayout() {
  213. return 1;
  214. }
  215. public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
  216. $answers = $this->get_answers();
  217. $formattextdefoptions = new stdClass;
  218. $formattextdefoptions->para = false; //I'll use it widely in this page
  219. foreach ($answers as $answer) {
  220. $data = "<input type=\"button\" name=\"$answer->id\" value=\"".s(strip_tags(format_text($answer->answer, FORMAT_MOODLE,$formattextdefoptions)))."\" disabled=\"disabled\"> ";
  221. $data .= get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto));
  222. $answerdata->answers[] = array($data, "");
  223. $answerpage->answerdata = $answerdata;
  224. }
  225. return $answerpage;
  226. }
  227. public function update($properties, $context = null, $maxbytes = null) {
  228. if (empty($properties->display)) {
  229. $properties->display = '0';
  230. }
  231. if (empty($properties->layout)) {
  232. $properties->layout = '0';
  233. }
  234. return parent::update($properties);
  235. }
  236. public function add_page_link($previd) {
  237. global $PAGE, $CFG;
  238. $addurl = new moodle_url('/mod/lesson/editpage.php', array('id'=>$PAGE->cm->id, 'pageid'=>$previd, 'qtype'=>LESSON_PAGE_BRANCHTABLE));
  239. return array('addurl'=>$addurl, 'type'=>LESSON_PAGE_BRANCHTABLE, 'name'=>get_string('addabranchtable', 'lesson'));
  240. }
  241. protected function get_displayinmenublock() {
  242. return true;
  243. }
  244. public function is_unseen($param) {
  245. global $USER, $DB;
  246. if (is_array($param)) {
  247. $seenpages = $param;
  248. $branchpages = $this->lesson->get_sub_pages_of($this->properties->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
  249. foreach ($branchpages as $branchpage) {
  250. if (array_key_exists($branchpage->id, $seenpages)) { // check if any of the pages have been viewed
  251. return false;
  252. }
  253. }
  254. return true;
  255. } else {
  256. $nretakes = $param;
  257. if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. }
  263. }
  264. class lesson_add_page_form_branchtable extends lesson_add_page_form_base {
  265. public $qtype = LESSON_PAGE_BRANCHTABLE;
  266. public $qtypestring = 'branchtable';
  267. protected $standard = false;
  268. public function custom_definition() {
  269. global $PAGE;
  270. $mform = $this->_form;
  271. $lesson = $this->_customdata['lesson'];
  272. $firstpage = optional_param('firstpage', false, PARAM_BOOL);
  273. $jumptooptions = lesson_page_type_branchtable::get_jumptooptions($firstpage, $lesson);
  274. $mform->setDefault('qtypeheading', get_string('addabranchtable', 'lesson'));
  275. $mform->addElement('hidden', 'firstpage');
  276. $mform->setType('firstpage', PARAM_BOOL);
  277. $mform->setDefault('firstpage', $firstpage);
  278. $mform->addElement('hidden', 'qtype');
  279. $mform->setType('qtype', PARAM_INT);
  280. $mform->addElement('text', 'title', get_string("pagetitle", "lesson"), array('size'=>70));
  281. $mform->setType('title', PARAM_TEXT);
  282. $mform->addRule('title', null, 'required', null, 'server');
  283. $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$PAGE->course->maxbytes);
  284. $mform->addElement('editor', 'contents_editor', get_string("pagecontents", "lesson"), null, $this->editoroptions);
  285. $mform->setType('contents_editor', PARAM_RAW);
  286. $mform->addElement('checkbox', 'layout', null, get_string("arrangebuttonshorizontally", "lesson"));
  287. $mform->setDefault('layout', true);
  288. $mform->addElement('checkbox', 'display', null, get_string("displayinleftmenu", "lesson"));
  289. $mform->setDefault('display', true);
  290. for ($i = 0; $i < $lesson->maxanswers; $i++) {
  291. $mform->addElement('header', 'headeranswer'.$i, get_string('branch', 'lesson').' '.($i+1));
  292. $this->add_answer($i, get_string("description", "lesson"), $i == 0);
  293. $mform->addElement('select', 'jumpto['.$i.']', get_string("jump", "lesson"), $jumptooptions);
  294. if ($i === 0) {
  295. $mform->setDefault('jumpto['.$i.']', 0);
  296. } else {
  297. $mform->setDefault('jumpto['.$i.']', LESSON_NEXTPAGE);
  298. }
  299. }
  300. }
  301. }