MDL-21432 backup - supress already included include
[moodle.git] / mod / lesson / locallib.php
CommitLineData
87f83794 1<?php
2
0a4abb73
SH
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
5491947a 18/**
19 * Local library file for Lesson. These are non-standard functions that are used
20 * only by Lesson.
21 *
0a4abb73
SH
22 * @package lesson
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
5491947a 25 **/
4b55d2af 26
0a4abb73
SH
27/** Make sure this isn't being directly accessed */
28if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page.
30}
31
32/** Include the files that are required by this module */
33require_once($CFG->dirroot . '/mod/lesson/lib.php');
34
35/** Next page -> any page not seen before */
36define("LESSON_UNSEENPAGE", 1);
37/** Next page -> any page not answered correctly */
38define("LESSON_UNANSWEREDPAGE", 2);
39/** Jump to Next Page */
40define("LESSON_NEXTPAGE", -1);
41/** End of Lesson */
42define("LESSON_EOL", -9);
43/** Jump to an unseen page within a branch and end of branch or end of lesson */
44define("LESSON_UNSEENBRANCHPAGE", -50);
45/** Jump to Previous Page */
46define("LESSON_PREVIOUSPAGE", -40);
47/** Jump to a random page within a branch and end of branch or end of lesson */
48define("LESSON_RANDOMPAGE", -60);
49/** Jump to a random Branch */
50define("LESSON_RANDOMBRANCH", -70);
51/** Cluster Jump */
52define("LESSON_CLUSTERJUMP", -80);
53/** Undefined */
54define("LESSON_UNDEFINED", -99);
5e7856af 55
56//////////////////////////////////////////////////////////////////////////////////////
86342d63 57/// Any other lesson functions go here. Each of them must have a name that
5e7856af 58/// starts with lesson_
59
4b55d2af 60/**
86342d63 61 * Checks to see if a LESSON_CLUSTERJUMP or
4b55d2af 62 * a LESSON_UNSEENBRANCHPAGE is used in a lesson.
63 *
86342d63 64 * This function is only executed when a teacher is
4b55d2af 65 * checking the navigation for a lesson.
66 *
06469639 67 * @param int $lesson Id of the lesson that is to be checked.
4b55d2af 68 * @return boolean True or false.
69 **/
06469639 70function lesson_display_teacher_warning($lesson) {
646fc290 71 global $DB;
86342d63 72
ac8e16be 73 // get all of the lesson answers
0a4abb73 74 $params = array ("lessonid" => $lesson->id);
646fc290 75 if (!$lessonanswers = $DB->get_records_select("lesson_answers", "lessonid = :lessonid", $params)) {
ac8e16be 76 // no answers, then not useing cluster or unseen
77 return false;
78 }
79 // just check for the first one that fulfills the requirements
80 foreach ($lessonanswers as $lessonanswer) {
81 if ($lessonanswer->jumpto == LESSON_CLUSTERJUMP || $lessonanswer->jumpto == LESSON_UNSEENBRANCHPAGE) {
82 return true;
83 }
84 }
86342d63 85
ac8e16be 86 // if no answers use either of the two jumps
87 return false;
5e7856af 88}
89
4b55d2af 90/**
91 * Interprets the LESSON_UNSEENBRANCHPAGE jump.
86342d63 92 *
4b55d2af 93 * will return the pageid of a random unseen page that is within a branch
94 *
0a4abb73 95 * @param lesson $lesson
f521f98a 96 * @param int $userid Id of the user.
4b55d2af 97 * @param int $pageid Id of the page from which we are jumping.
98 * @return int Id of the next page.
4b55d2af 99 **/
5e7856af 100function lesson_unseen_question_jump($lesson, $user, $pageid) {
646fc290 101 global $DB;
86342d63 102
ac8e16be 103 // get the number of retakes
0a4abb73 104 if (!$retakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$user))) {
ac8e16be 105 $retakes = 0;
106 }
107
108 // get all the lesson_attempts aka what the user has seen
0a4abb73 109 if ($viewedpages = $DB->get_records("lesson_attempts", array("lessonid"=>$lesson->id, "userid"=>$user, "retry"=>$retakes), "timeseen DESC")) {
ac8e16be 110 foreach($viewedpages as $viewed) {
111 $seenpages[] = $viewed->pageid;
112 }
113 } else {
114 $seenpages = array();
115 }
116
117 // get the lesson pages
0a4abb73 118 $lessonpages = $lesson->load_all_pages();
86342d63 119
ac8e16be 120 if ($pageid == LESSON_UNSEENBRANCHPAGE) { // this only happens when a student leaves in the middle of an unseen question within a branch series
121 $pageid = $seenpages[0]; // just change the pageid to the last page viewed inside the branch table
122 }
123
124 // go up the pages till branch table
125 while ($pageid != 0) { // this condition should never be satisfied... only happens if there are no branch tables above this page
0a4abb73 126 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
ac8e16be 127 break;
128 }
129 $pageid = $lessonpages[$pageid]->prevpageid;
130 }
86342d63 131
0a4abb73 132 $pagesinbranch = $this->get_sub_pages_of($pageid, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
86342d63 133
ac8e16be 134 // this foreach loop stores all the pages that are within the branch table but are not in the $seenpages array
135 $unseen = array();
86342d63 136 foreach($pagesinbranch as $page) {
ac8e16be 137 if (!in_array($page->id, $seenpages)) {
138 $unseen[] = $page->id;
139 }
140 }
141
142 if(count($unseen) == 0) {
143 if(isset($pagesinbranch)) {
144 $temp = end($pagesinbranch);
145 $nextpage = $temp->nextpageid; // they have seen all the pages in the branch, so go to EOB/next branch table/EOL
146 } else {
147 // there are no pages inside the branch, so return the next page
148 $nextpage = $lessonpages[$pageid]->nextpageid;
149 }
150 if ($nextpage == 0) {
151 return LESSON_EOL;
152 } else {
153 return $nextpage;
154 }
155 } else {
156 return $unseen[rand(0, count($unseen)-1)]; // returns a random page id for the next page
157 }
5e7856af 158}
159
4b55d2af 160/**
161 * Handles the unseen branch table jump.
162 *
0a4abb73 163 * @param lesson $lesson
f521f98a 164 * @param int $userid User id.
4b55d2af 165 * @return int Will return the page id of a branch table or end of lesson
4b55d2af 166 **/
0a4abb73 167function lesson_unseen_branch_jump($lesson, $userid) {
646fc290 168 global $DB;
86342d63 169
0a4abb73 170 if (!$retakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$userid))) {
ac8e16be 171 $retakes = 0;
172 }
173
0a4abb73 174 $params = array ("lessonid" => $lesson->id, "userid" => $userid, "retry" => $retakes);
646fc290 175 if (!$seenbranches = $DB->get_records_select("lesson_branch", "lessonid = :lessonid AND userid = :userid AND retry = :retry", $params,
ac8e16be 176 "timeseen DESC")) {
86f93345 177 print_error('cannotfindrecords', 'lesson');
ac8e16be 178 }
179
180 // get the lesson pages
0a4abb73 181 $lessonpages = $lesson->load_all_pages();
86342d63 182
ac8e16be 183 // this loads all the viewed branch tables into $seen untill it finds the branch table with the flag
184 // which is the branch table that starts the unseenbranch function
86342d63 185 $seen = array();
ac8e16be 186 foreach ($seenbranches as $seenbranch) {
187 if (!$seenbranch->flag) {
188 $seen[$seenbranch->pageid] = $seenbranch->pageid;
189 } else {
190 $start = $seenbranch->pageid;
191 break;
192 }
193 }
194 // this function searches through the lesson pages to find all the branch tables
195 // that follow the flagged branch table
196 $pageid = $lessonpages[$start]->nextpageid; // move down from the flagged branch table
197 while ($pageid != 0) { // grab all of the branch table till eol
0a4abb73 198 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
ac8e16be 199 $branchtables[] = $lessonpages[$pageid]->id;
200 }
201 $pageid = $lessonpages[$pageid]->nextpageid;
202 }
203 $unseen = array();
204 foreach ($branchtables as $branchtable) {
205 // load all of the unseen branch tables into unseen
206 if (!array_key_exists($branchtable, $seen)) {
207 $unseen[] = $branchtable;
208 }
209 }
210 if (count($unseen) > 0) {
211 return $unseen[rand(0, count($unseen)-1)]; // returns a random page id for the next page
212 } else {
213 return LESSON_EOL; // has viewed all of the branch tables
214 }
5e7856af 215}
216
4b55d2af 217/**
218 * Handles the random jump between a branch table and end of branch or end of lesson (LESSON_RANDOMPAGE).
86342d63 219 *
0a4abb73 220 * @param lesson $lesson
4b55d2af 221 * @param int $pageid The id of the page that we are jumping from (?)
222 * @return int The pageid of a random page that is within a branch table
4b55d2af 223 **/
0a4abb73 224function lesson_random_question_jump($lesson, $pageid) {
646fc290 225 global $DB;
86342d63 226
ac8e16be 227 // get the lesson pages
0a4abb73 228 $params = array ("lessonid" => $lesson->id);
646fc290 229 if (!$lessonpages = $DB->get_records_select("lesson_pages", "lessonid = :lessonid", $params)) {
86f93345 230 print_error('cannotfindpages', 'lesson');
ac8e16be 231 }
232
233 // go up the pages till branch table
234 while ($pageid != 0) { // this condition should never be satisfied... only happens if there are no branch tables above this page
235
0a4abb73 236 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
ac8e16be 237 break;
238 }
239 $pageid = $lessonpages[$pageid]->prevpageid;
240 }
241
86342d63 242 // get the pages within the branch
0a4abb73 243 $pagesinbranch = $this->get_sub_pages_of($pageid, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
86342d63 244
ac8e16be 245 if(count($pagesinbranch) == 0) {
246 // there are no pages inside the branch, so return the next page
247 return $lessonpages[$pageid]->nextpageid;
248 } else {
249 return $pagesinbranch[rand(0, count($pagesinbranch)-1)]->id; // returns a random page id for the next page
250 }
5e7856af 251}
252
4b55d2af 253/**
254 * Calculates a user's grade for a lesson.
255 *
4b55d2af 256 * @param object $lesson The lesson that the user is taking.
4b55d2af 257 * @param int $retries The attempt number.
88427c07 258 * @param int $userid Id of the user (optinal, default current user).
259 * @return object { nquestions => number of questions answered
260 attempts => number of question attempts
261 total => max points possible
262 earned => points earned by student
263 grade => calculated percentage grade
264 nmanual => number of manually graded questions
265 manualpoints => point value for manually graded questions }
4b55d2af 266 */
86342d63 267function lesson_grade($lesson, $ntries, $userid = 0) {
646fc290 268 global $USER, $DB;
ac8e16be 269
88427c07 270 if (empty($userid)) {
271 $userid = $USER->id;
272 }
86342d63 273
88427c07 274 // Zero out everything
275 $ncorrect = 0;
276 $nviewed = 0;
277 $score = 0;
278 $nmanual = 0;
279 $manualpoints = 0;
280 $thegrade = 0;
281 $nquestions = 0;
282 $total = 0;
283 $earned = 0;
284
646fc290 285 $params = array ("lessonid" => $lesson->id, "userid" => $userid, "retry" => $ntries);
86342d63 286 if ($useranswers = $DB->get_records_select("lesson_attempts", "lessonid = :lessonid AND
646fc290 287 userid = :userid AND retry = :retry", $params, "timeseen")) {
88427c07 288 // group each try with its page
289 $attemptset = array();
290 foreach ($useranswers as $useranswer) {
86342d63 291 $attemptset[$useranswer->pageid][] = $useranswer;
ac8e16be 292 }
86342d63 293
88427c07 294 // Drop all attempts that go beyond max attempts for the lesson
295 foreach ($attemptset as $key => $set) {
296 $attemptset[$key] = array_slice($set, 0, $lesson->maxattempts);
297 }
86342d63 298
88427c07 299 // get only the pages and their answers that the user answered
646fc290 300 list($usql, $parameters) = $DB->get_in_or_equal(array_keys($attemptset));
0a4abb73
SH
301 array_unshift($parameters, $lesson->id);
302 $pages = $DB->get_records_select("lesson_pages", "lessonid = ? AND id $usql", $parameters);
303 $answers = $DB->get_records_select("lesson_answers", "lessonid = ? AND pageid $usql", $parameters);
86342d63 304
88427c07 305 // Number of pages answered
306 $nquestions = count($pages);
307
308 foreach ($attemptset as $attempts) {
0a4abb73 309 $page = lesson_page::load($pages[end($attempts)->pageid], $lesson);
88427c07 310 if ($lesson->custom) {
311 $attempt = end($attempts);
312 // If essay question, handle it, otherwise add to score
0a4abb73
SH
313 if ($page->requires_manual_grading()) {
314 $earned += $page->earned_score($answers, $attempt);
88427c07 315 $nmanual++;
316 $manualpoints += $answers[$attempt->answerid]->score;
ab1e7c39 317 } else if (!empty($attempt->answerid)) {
0a4abb73 318 $earned += $page->earned_score($answers, $attempt);
88427c07 319 }
320 } else {
321 foreach ($attempts as $attempt) {
322 $earned += $attempt->correct;
323 }
324 $attempt = end($attempts); // doesn't matter which one
325 // If essay question, increase numbers
0a4abb73 326 if ($page->requires_manual_grading()) {
88427c07 327 $nmanual++;
328 $manualpoints++;
ac8e16be 329 }
330 }
88427c07 331 // Number of times answered
332 $nviewed += count($attempts);
333 }
86342d63 334
88427c07 335 if ($lesson->custom) {
ac8e16be 336 $bestscores = array();
88427c07 337 // Find the highest possible score per page to get our total
338 foreach ($answers as $answer) {
46341ab7 339 if(!isset($bestscores[$answer->pageid])) {
88427c07 340 $bestscores[$answer->pageid] = $answer->score;
46341ab7 341 } else if ($bestscores[$answer->pageid] < $answer->score) {
88427c07 342 $bestscores[$answer->pageid] = $answer->score;
ac8e16be 343 }
344 }
88427c07 345 $total = array_sum($bestscores);
346 } else {
347 // Check to make sure the student has answered the minimum questions
348 if ($lesson->minquestions and $nquestions < $lesson->minquestions) {
349 // Nope, increase number viewed by the amount of unanswered questions
350 $total = $nviewed + ($lesson->minquestions - $nquestions);
351 } else {
352 $total = $nviewed;
353 }
ac8e16be 354 }
88427c07 355 }
86342d63 356
88427c07 357 if ($total) { // not zero
358 $thegrade = round(100 * $earned / $total, 5);
359 }
86342d63 360
88427c07 361 // Build the grade information object
362 $gradeinfo = new stdClass;
363 $gradeinfo->nquestions = $nquestions;
364 $gradeinfo->attempts = $nviewed;
365 $gradeinfo->total = $total;
366 $gradeinfo->earned = $earned;
367 $gradeinfo->grade = $thegrade;
368 $gradeinfo->nmanual = $nmanual;
369 $gradeinfo->manualpoints = $manualpoints;
86342d63 370
88427c07 371 return $gradeinfo;
372}
373
62eda6ea 374/**
375 * Determines if a user can view the left menu. The determining factor
376 * is whether a user has a grade greater than or equal to the lesson setting
377 * of displayleftif
378 *
379 * @param object $lesson Lesson object of the current lesson
380 * @return boolean 0 if the user cannot see, or $lesson->displayleft to keep displayleft unchanged
62eda6ea 381 **/
382function lesson_displayleftif($lesson) {
646fc290 383 global $CFG, $USER, $DB;
86342d63 384
62eda6ea 385 if (!empty($lesson->displayleftif)) {
386 // get the current user's max grade for this lesson
646fc290 387 $params = array ("userid" => $USER->id, "lessonid" => $lesson->id);
388 if ($maxgrade = $DB->get_record_sql('SELECT userid, MAX(grade) AS maxgrade FROM {lesson_grades} WHERE userid = :userid AND lessonid = :lessonid GROUP BY userid', $params)) {
62eda6ea 389 if ($maxgrade->maxgrade < $lesson->displayleftif) {
390 return 0; // turn off the displayleft
391 }
392 } else {
393 return 0; // no grades
394 }
395 }
86342d63 396
62eda6ea 397 // if we get to here, keep the original state of displayleft lesson setting
398 return $lesson->displayleft;
399}
5e7856af 400
4262a2f8 401/**
86342d63 402 *
4262a2f8 403 * @param $cm
404 * @param $lesson
405 * @param $page
406 * @return unknown_type
407 */
408function lesson_add_pretend_blocks($page, $cm, $lesson, $timer = null) {
409 $bc = lesson_menu_block_contents($cm->id, $lesson);
410 if (!empty($bc)) {
411 $regions = $page->blocks->get_regions();
412 $firstregion = reset($regions);
413 $page->blocks->add_pretend_block($bc, $firstregion);
414 }
415
416 $bc = lesson_mediafile_block_contents($cm->id, $lesson);
417 if (!empty($bc)) {
418 $page->blocks->add_pretend_block($bc, $page->blocks->get_default_region());
419 }
420
421 if (!empty($timer)) {
422 $bc = lesson_clock_block_contents($cm->id, $lesson, $timer, $page);
423 if (!empty($bc)) {
424 $page->blocks->add_pretend_block($bc, $page->blocks->get_default_region());
425 }
426 }
427}
428
f521f98a 429/**
86342d63 430 * If there is a media file associated with this
4262a2f8 431 * lesson, return a block_contents that displays it.
f521f98a 432 *
433 * @param int $cmid Course Module ID for this lesson
434 * @param object $lesson Full lesson record object
4262a2f8 435 * @return block_contents
f521f98a 436 **/
4262a2f8 437function lesson_mediafile_block_contents($cmid, $lesson) {
d68ccdba 438 global $OUTPUT;
0a4abb73 439 if (empty($lesson->mediafile) && empty($lesson->mediafileid)) {
4262a2f8 440 return null;
f521f98a 441 }
4262a2f8 442
0a4abb73
SH
443 $options = array();
444 $options['menubar'] = 0;
445 $options['location'] = 0;
446 $options['left'] = 5;
447 $options['top'] = 5;
448 $options['scrollbars'] = 1;
449 $options['resizable'] = 1;
450 $options['width'] = $lesson->mediawidth;
451 $options['height'] = $lesson->mediaheight;
4262a2f8 452
9bf16314
PS
453 $link = new moodle_url('/mod/lesson/mediafile.php?id='.$cmid);
454 $action = new popup_action('click', $link, 'lessonmediafile', $options);
455 $content = $OUTPUT->action_link($link, get_string('mediafilepopup', 'lesson'), $action, array('title'=>get_string('mediafilepopup', 'lesson')));
86342d63 456
4262a2f8 457 $bc = new block_contents();
458 $bc->title = get_string('linkedmedia', 'lesson');
0235e247 459 $bc->attributes['class'] = 'mediafile';
4262a2f8 460 $bc->content = $content;
461
462 return $bc;
f521f98a 463}
464
465/**
466 * If a timed lesson and not a teacher, then
4262a2f8 467 * return a block_contents containing the clock.
f521f98a 468 *
469 * @param int $cmid Course Module ID for this lesson
470 * @param object $lesson Full lesson record object
471 * @param object $timer Full timer record object
4262a2f8 472 * @return block_contents
f521f98a 473 **/
4262a2f8 474function lesson_clock_block_contents($cmid, $lesson, $timer, $page) {
475 // Display for timed lessons and for students only
f521f98a 476 $context = get_context_instance(CONTEXT_MODULE, $cmid);
4262a2f8 477 if(!$lesson->timed || has_capability('mod/lesson:manage', $context)) {
478 return null;
479 }
f521f98a 480
4262a2f8 481 $content = '<div class="jshidewhenenabled">';
0a4abb73 482 $content .= $lesson->time_remaining($timer->starttime);
4262a2f8 483 $content .= '</div>';
ba458143 484
4262a2f8 485 $clocksettings = array('starttime'=>$timer->starttime, 'servertime'=>time(),'testlength'=>($lesson->maxtime * 60));
227255b8
PS
486 $page->requires->data_for_js('clocksettings', $clocksettings);
487 $page->requires->js('/mod/lesson/timer.js');
488 $page->requires->js_function_call('show_clock');
ba458143 489
4262a2f8 490 $bc = new block_contents();
491 $bc->title = get_string('timeremaining', 'lesson');
6605ff8c 492 $bc->attributes['class'] = 'clock block';
4262a2f8 493 $bc->content = $content;
494
495 return $bc;
f521f98a 496}
497
498/**
499 * If left menu is turned on, then this will
500 * print the menu in a block
501 *
502 * @param int $cmid Course Module ID for this lesson
0a4abb73 503 * @param lesson $lesson Full lesson record object
f521f98a 504 * @return void
505 **/
4262a2f8 506function lesson_menu_block_contents($cmid, $lesson) {
646fc290 507 global $CFG, $DB;
f521f98a 508
4262a2f8 509 if (!$lesson->displayleft) {
510 return null;
511 }
f521f98a 512
0a4abb73
SH
513 $pages = $lesson->load_all_pages();
514 foreach ($pages as $page) {
515 if ((int)$page->prevpageid === 0) {
516 $pageid = $page->id;
517 break;
518 }
519 }
4262a2f8 520 $currentpageid = optional_param('pageid', $pageid, PARAM_INT);
f521f98a 521
4262a2f8 522 if (!$pageid || !$pages) {
523 return null;
f521f98a 524 }
f521f98a 525
4262a2f8 526 $content = '<a href="#maincontent" class="skip">'.get_string('skip', 'lesson')."</a>\n<div class=\"menuwrapper\">\n<ul>\n";
888f0c54 527
4262a2f8 528 while ($pageid != 0) {
529 $page = $pages[$pageid];
530
531 // Only process branch tables with display turned on
0a4abb73 532 if ($page->displayinmenublock && $page->display) {
86342d63 533 if ($page->id == $currentpageid) {
4262a2f8 534 $content .= '<li class="selected">'.format_string($page->title,true)."</li>\n";
535 } else {
536 $content .= "<li class=\"notselected\"><a href=\"$CFG->wwwroot/mod/lesson/view.php?id=$cmid&amp;pageid=$page->id\">".format_string($page->title,true)."</a></li>\n";
537 }
86342d63 538
888f0c54 539 }
4262a2f8 540 $pageid = $page->nextpageid;
888f0c54 541 }
4262a2f8 542 $content .= "</ul>\n</div>\n";
888f0c54 543
4262a2f8 544 $bc = new block_contents();
545 $bc->title = get_string('lessonmenu', 'lesson');
6605ff8c 546 $bc->attributes['class'] = 'menu block';
4262a2f8 547 $bc->content = $content;
888f0c54 548
4262a2f8 549 return $bc;
448052a5
SH
550}
551
552/**
553 * Adds header buttons to the page for the lesson
554 *
555 * @param object $cm
556 * @param object $context
557 * @param bool $extraeditbuttons
558 * @param int $lessonpageid
559 */
560function lesson_add_header_buttons($cm, $context, $extraeditbuttons=false, $lessonpageid=null) {
561 global $CFG, $PAGE, $OUTPUT;
92059c7e
SH
562 if (has_capability('mod/lesson:edit', $context) && $extraeditbuttons) {
563 if ($lessonpageid === null) {
564 print_error('invalidpageid', 'lesson');
565 }
566 if (!empty($lessonpageid) && $lessonpageid != LESSON_EOL) {
a6855934 567 $url = new moodle_url('/mod/lesson/lesson.php', array('id'=>$cm->id, 'redirect'=>'navigation', 'pageid'=>$lessonpageid));
5c2ed7e2 568 $PAGE->set_button($OUTPUT->single_button($url, get_string('editpagecontent', 'lesson')));
448052a5 569 }
448052a5 570 }
5c2ed7e2 571}
9b56a34f
PS
572
573/**
574 * This is a function used to detect media types and generate html code.
575 *
576 * @global object $CFG
577 * @global object $PAGE
578 * @param object $lesson
579 * @param object $context
580 * @return string $code the html code of media
581 */
582function lesson_get_media_html($lesson, $context) {
583 global $CFG, $PAGE, $OUTPUT;
584 require_once("$CFG->libdir/resourcelib.php");
585
64f93798 586 // get the media file link
d333dc20 587 $path = '/'.$context->id.'/mod_lesson/media_file/'.$lesson->id->revision.'/'.$lesson->mediafile;
64f93798 588 $url = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
9b56a34f
PS
589 $title = $lesson->mediafile;
590
591 $clicktoopen = html_writer::link(new moodle_url($url), get_string('download'));
592
593 $mimetype = resourcelib_guess_url_mimetype($url);
594
595 // find the correct type and print it out
596 if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
597 $code = resourcelib_embed_image($url, $title);
598
599 } else if ($mimetype == 'audio/mp3') {
600 // MP3 audio file
601 $code = resourcelib_embed_mp3($url, $title, $clicktoopen);
602
603 } else if ($mimetype == 'video/x-flv') {
604 // Flash video file
605 $code = resourcelib_embed_flashvideo($url, $title, $clicktoopen);
606
607 } else if ($mimetype == 'application/x-shockwave-flash') {
608 // Flash file
609 $code = resourcelib_embed_flash($url, $title, $clicktoopen);
610
611 } else if (substr($mimetype, 0, 10) == 'video/x-ms') {
612 // Windows Media Player file
613 $code = resourcelib_embed_mediaplayer($url, $title, $clicktoopen);
614
615 } else if ($mimetype == 'video/quicktime') {
616 // Quicktime file
617 $code = resourcelib_embed_quicktime($url, $title, $clicktoopen);
618
619 } else if ($mimetype == 'video/mpeg') {
620 // Mpeg file
621 $code = resourcelib_embed_mpeg($url, $title, $clicktoopen);
622
623 } else if ($mimetype == 'audio/x-pn-realaudio-plugin') {
624 // RealMedia file
625 $code = resourcelib_embed_real($url, $title, $clicktoopen);
626
627 } else {
628 // anything else - just try object tag enlarged as much as possible
629 $code = resourcelib_embed_general($url, $title, $clicktoopen, $mimetype);
630 }
631
632 return $code;
633}