MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / lesson / edit.php
blobb9d1c2032ef3c87052f6d013ffc5f85c45e3c009
1 <?php // $Id$
2 /**
3 * Provides the interface for overall authoring of lessons
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
10 require_once('../../config.php');
11 require_once('locallib.php');
12 require_once('lib.php');
14 $id = required_param('id', PARAM_INT); // Course Module ID
15 $display = optional_param('display', 0, PARAM_INT);
16 $mode = optional_param('mode', get_user_preferences('lesson_view', 'collapsed'), PARAM_ALPHA);
17 $pageid = optional_param('pageid', 0, PARAM_INT);
19 if ($mode != 'single') {
20 set_user_preference('lesson_view', $mode);
23 list($cm, $course, $lesson) = lesson_get_basics($id);
25 if ($firstpage = get_record('lesson_pages', 'lessonid', $lesson->id, 'prevpageid', 0)) {
26 if (!$pages = get_records('lesson_pages', 'lessonid', $lesson->id)) {
27 error('Could not find lesson pages');
31 if ($pageid) {
32 if (!$singlepage = get_record('lesson_pages', 'id', $pageid)) {
33 error('Could not find page ID: '.$pageid);
37 require_login($course->id, false, $cm);
38 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
39 require_capability('mod/lesson:manage', $context);
41 lesson_print_header($cm, $course, $lesson, $mode);
43 if (empty($firstpage)) {
44 // There are no pages; give teacher some options
45 if (has_capability('mod/lesson:edit', $context)) {
46 print_simple_box( "<table cellpadding=\"5\" border=\"0\">\n<tr><th scope=\"col\">".get_string("whatdofirst", "lesson")."</th></tr><tr><td>".
47 "<a href=\"import.php?id=$cm->id&amp;pageid=0\">".
48 get_string("importquestions", "lesson")."</a></td></tr><tr><td>".
49 "<a href=\"importppt.php?id=$cm->id&amp;pageid=0\">".
50 get_string("importppt", "lesson")."</a></td></tr><tr><td>".
51 "<a href=\"lesson.php?id=$cm->id&amp;action=addbranchtable&amp;pageid=0&amp;firstpage=1\">".
52 get_string("addabranchtable", "lesson")."</a></td></tr><tr><td>".
53 "<a href=\"lesson.php?id=$cm->id&amp;action=addpage&amp;pageid=0&amp;firstpage=1\">".
54 get_string("addaquestionpage", "lesson").
55 "</a></td></tr></table>\n", 'center', '20%');
57 } else {
58 // Set some standard variables
59 $pageid = $firstpage->id;
60 $prevpageid = 0;
61 $npages = count($pages);
63 switch ($mode) {
64 case 'collapsed':
65 $table = new stdClass;
66 $table->head = array(get_string('pagetitle', 'lesson'), get_string('qtype', 'lesson'), get_string('jumps', 'lesson'), get_string('actions', 'lesson'));
67 $table->align = array('left', 'left', 'left', 'center');
68 $table->wrap = array('', 'nowrap', '', 'nowrap');
69 $table->tablealign = 'center';
70 $table->cellspacing = 0;
71 $table->cellpadding = '2px';
72 $table->data = array();
74 while ($pageid != 0) {
75 $page = $pages[$pageid];
77 $jumps = array();
78 if($answers = get_records_select("lesson_answers", "lessonid = $lesson->id and pageid = $pageid")) {
80 foreach ($answers as $answer) {
81 $jumps[] = lesson_get_jump_name($answer->jumpto);
85 $table->data[] = array("<a href=\"$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id&amp;mode=single&amp;pageid=".$page->id."\">".format_string($pages[$pageid]->title,true).'</a>',
86 lesson_get_qtype_name($page->qtype),
87 implode("<br />\n", $jumps),
88 lesson_print_page_actions($cm->id, $page, $npages, true, true)
90 $pageid = $page->nextpageid;
93 print_table($table);
94 break;
96 case 'single':
97 // Only viewing a single page in full - change some variables to display just one
98 $prevpageid = $singlepage->prevpageid;
99 $pageid = $singlepage->id;
101 $pages = array();
102 $pages[$singlepage->id] = $singlepage;
104 case 'full':
105 echo '<table class="boxaligncenter" cellpadding="5" border="0" style="width:80%;">
106 <tr>
107 <td align="left">';
108 lesson_print_add_links($cm->id, $prevpageid);
109 echo ' </td>
110 </tr>';
112 while ($pageid != 0) {
113 $page = $pages[$pageid];
115 echo "<tr><td>\n";
116 echo "<table style=\"width:100%;\" border=\"1\" class=\"generalbox\"><tr><th colspan=\"2\" scope=\"col\">".format_string($page->title)."&nbsp;&nbsp;\n";
117 lesson_print_page_actions($cm->id, $page, $npages);
118 echo "</th></tr>\n";
119 echo "<tr><td colspan=\"2\">\n";
120 $options = new stdClass;
121 $options->noclean = true;
122 echo format_text($page->contents, FORMAT_MOODLE, $options);
123 echo "</td></tr>\n";
124 // get the answers in a set order, the id order
125 if ($answers = get_records("lesson_answers", "pageid", $page->id, "id")) {
126 echo "<tr><td colspan=\"2\" align=\"center\"><strong>\n";
127 echo lesson_get_qtype_name($page->qtype);
128 switch ($page->qtype) {
129 case LESSON_SHORTANSWER :
130 if ($page->qoption) {
131 echo " - ".get_string("casesensitive", "lesson");
133 break;
134 case LESSON_MULTICHOICE :
135 if ($page->qoption) {
136 echo " - ".get_string("multianswer", "lesson");
138 break;
139 case LESSON_MATCHING :
140 echo get_string("firstanswershould", "lesson");
141 break;
143 echo "</strong></td></tr>\n";
144 $i = 1;
145 $n = 0;
146 $options = new stdClass;
147 $options->noclean = true;
148 $options->para = false;
149 foreach ($answers as $answer) {
150 switch ($page->qtype) {
151 case LESSON_MULTICHOICE:
152 case LESSON_TRUEFALSE:
153 case LESSON_SHORTANSWER:
154 case LESSON_NUMERICAL:
155 echo "<tr><td align=\"right\" valign=\"top\" style=\"width:20%;\">\n";
156 if ($lesson->custom) {
157 // if the score is > 0, then it is correct
158 if ($answer->score > 0) {
159 echo '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
160 } else {
161 echo '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
163 } else {
164 if (lesson_iscorrect($page->id, $answer->jumpto)) {
165 // underline correct answers
166 echo '<span class="correct">'.get_string("answer", "lesson")." $i</span>: \n";
167 } else {
168 echo '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
171 echo "</td><td style=\"width:80%;\">\n";
172 echo format_text($answer->answer, FORMAT_MOODLE, $options);
173 echo "</td></tr>\n";
174 echo "<tr><td align=\"right\" valign=\"top\"><span class=\"label\">".get_string("response", "lesson")." $i</span>: \n";
175 echo "</td><td>\n";
176 echo format_text($answer->response, FORMAT_MOODLE, $options);
177 echo "</td></tr>\n";
178 break;
179 case LESSON_MATCHING:
180 if ($n < 2) {
181 if ($answer->answer != NULL) {
182 if ($n == 0) {
183 echo "<tr><td align=\"right\" valign=\"top\"><span class=\"label\">".get_string("correctresponse", "lesson")."</span>: \n";
184 echo "</td><td>\n";
185 echo format_text($answer->answer, FORMAT_MOODLE, $options);
186 echo "</td></tr>\n";
187 } else {
188 echo "<tr><td align=\"right\" valign=\"top\"><span class=\"label\">".get_string("wrongresponse", "lesson")."</span>: \n";
189 echo "</td><td>\n";
190 echo format_text($answer->answer, FORMAT_MOODLE, $options);
191 echo "</td></tr>\n";
194 $n++;
195 $i--;
196 } else {
197 echo "<tr><td align=\"right\" valign=\"top\" style=\"width:20%;\">\n";
198 if ($lesson->custom) {
199 // if the score is > 0, then it is correct
200 if ($answer->score > 0) {
201 echo '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
202 } else {
203 echo '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
205 } else {
206 if (lesson_iscorrect($page->id, $answer->jumpto)) {
207 // underline correct answers
208 echo '<span class="labelcorrect">'.get_string("answer", "lesson")." $i</span>: \n";
209 } else {
210 echo '<span class="label">'.get_string("answer", "lesson")." $i</span>: \n";
213 echo "</td><td style=\"width:80%;\">\n";
214 echo format_text($answer->answer, FORMAT_MOODLE, $options);
215 echo "</td></tr>\n";
216 echo "<tr><td align=\"right\" valign=\"top\">'<span class=\"label\">'.".get_string("matchesanswer", "lesson")." $i</span>: \n";
217 echo "</td><td>\n";
218 echo format_text($answer->response, FORMAT_MOODLE, $options);
219 echo "</td></tr>\n";
221 break;
222 case LESSON_BRANCHTABLE:
223 echo "<tr><td align=\"right\" valign=\"top\" style=\"width:20%;\">\n";
224 echo '<span class="label">'.get_string("description", "lesson")." $i</span>: \n";
225 echo "</td><td style=\"width:80%;\">\n";
226 echo format_text($answer->answer, FORMAT_MOODLE, $options);
227 echo "</td></tr>\n";
228 break;
231 $jumptitle = lesson_get_jump_name($answer->jumpto);
232 if ($page->qtype == LESSON_MATCHING) {
233 if ($i == 1) {
234 echo "<tr><td align=\"right\" style=\"width:20%;\"><span class=\"label\">".get_string("correctanswerscore", "lesson");
235 echo "</span>: </td><td style=\"width:80%;\">\n";
236 echo "$answer->score</td></tr>\n";
237 echo "<tr><td align=\"right\" style=\"width:20%;\"><span class=\"label\">".get_string("correctanswerjump", "lesson");
238 echo "</span>:</td><td style=\"width:80%;\">\n";
239 echo "$jumptitle</td></tr>\n";
240 } elseif ($i == 2) {
241 echo "<tr><td align=\"right\" style=\"width:20%;\"><span class=\"label\">".get_string("wronganswerscore", "lesson");
242 echo "</span>: </td><td style=\"width:80%;\">\n";
243 echo "$answer->score</td></tr>\n";
244 echo "<tr><td align=\"right\" style=\"width:20%;\"><span class=\"label\">".get_string("wronganswerjump", "lesson");
245 echo "</span>: </td><td style=\"width:80%;\">\n";
246 echo "$jumptitle</td></tr>\n";
248 } else {
249 if ($lesson->custom and
250 $page->qtype != LESSON_BRANCHTABLE and
251 $page->qtype != LESSON_ENDOFBRANCH and
252 $page->qtype != LESSON_CLUSTER and
253 $page->qtype != LESSON_ENDOFCLUSTER) {
254 echo "<tr><td align=\"right\" style=\"width:20%;\"><span class=\"label\">".get_string("score", "lesson")." $i";
255 echo "</span>: </td><td style=\"width:80%;\">\n";
256 echo "$answer->score</td></tr>\n";
258 echo "<tr><td align=\"right\" style=\"width:20%;\"><span class=\"label\">".get_string("jump", "lesson")." $i";
259 echo "</span>: </td><td style=\"width:80%;\">\n";
260 echo "$jumptitle</td></tr>\n";
262 $i++;
265 echo "</table></td></tr>\n<tr><td align=\"left\">";
266 lesson_print_add_links($cm->id, $page->id);
267 echo "</td></tr><tr><td>\n";
268 // check the prev links - fix (silently) if necessary - there was a bug in
269 // versions 1 and 2 when add new pages. Not serious then as the backwards
270 // links were not used in those versions
271 if ($page->prevpageid != $prevpageid) {
272 // fix it
273 set_field("lesson_pages", "prevpageid", $prevpageid, "id", $page->id);
274 debugging("<p>***prevpageid of page $page->id set to $prevpageid***");
277 if (count($pages) == 1) {
278 echo "</td></tr>";
279 break;
282 $prevpageid = $page->id;
283 $pageid = $page->nextpageid;
284 echo "</td></tr>";
286 echo "</table>";
287 break;
291 print_footer($course);