3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Standard library of functions and constants for lesson
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /* Do not include any libraries here! */
29 * Given an object containing all the necessary data,
30 * (defined by the form in mod_form.php) this function
31 * will create a new instance and return the id number
32 * of the new instance.
36 * @param object $lesson Lesson post data from the form
39 function lesson_add_instance($data, $mform) {
42 $cmid = $data->coursemodule;
44 lesson_process_pre_save($data);
46 unset($data->mediafile);
47 $lessonid = $DB->insert_record("lesson", $data);
48 $data->id = $lessonid;
50 $context = get_context_instance(CONTEXT_MODULE, $cmid);
51 $lesson = $DB->get_record('lesson', array('id'=>$lessonid), '*', MUST_EXIST);
53 if ($filename = $mform->get_new_filename('mediafile')) {
54 if ($file = $mform->save_stored_file('mediafile', $context->id, 'mod_lesson', 'media_file', $lesson->id, '/', $filename)) {
55 $DB->set_field('lesson', 'mediafile', $file->get_filename(), array('id'=>$lesson->id));
59 lesson_process_post_save($data);
61 lesson_grade_item_update($data);
67 * Given an object containing all the necessary data,
68 * (defined by the form in mod_form.php) this function
69 * will update an existing instance with new data.
72 * @param object $lesson Lesson post data from the form
75 function lesson_update_instance($data, $mform) {
78 $data->id = $data->instance;
79 $cmid = $data->coursemodule;
81 lesson_process_pre_save($data);
83 unset($data->mediafile);
84 $DB->update_record("lesson", $data);
86 $context = get_context_instance(CONTEXT_MODULE, $cmid);
87 if ($filename = $mform->get_new_filename('mediafile')) {
88 if ($file = $mform->save_stored_file('mediafile', $context->id, 'mod_lesson', 'media_file', $data->id, '/', $filename, true)) {
89 $DB->set_field('lesson', 'mediafile', $file->get_filename(), array('id'=>$data->id));
93 lesson_process_post_save($data);
95 // update grade item definition
96 lesson_grade_item_update($data);
98 // update grades - TODO: do it only when grading style changes
99 lesson_update_grades($data, 0, false);
106 * Given an ID of an instance of this module,
107 * this function will permanently delete the instance
108 * and any data that depends on it.
114 function lesson_delete_instance($id) {
116 $lesson = $DB->get_record("lesson", array("id"=>$id), '*', MUST_EXIST);
117 $lesson = new lesson($lesson);
118 return $lesson->delete();
122 * Given a course object, this function will clean up anything that
123 * would be leftover after all the instances were deleted
126 * @param object $course an object representing the course that is being deleted
127 * @param boolean $feedback to specify if the process must output a summary of its work
130 function lesson_delete_course($course, $feedback=true) {
135 * Return a small object with summary information about what a
136 * user has done with a given particular instance of this module
137 * Used for user activity reports.
138 * $return->time = the time they did it
139 * $return->info = a short text description
142 * @param object $course
143 * @param object $user
145 * @param object $lesson
148 function lesson_user_outline($course, $user, $mod, $lesson) {
152 require_once("$CFG->libdir/gradelib.php");
153 $grades = grade_get_grades($course->id, 'mod', 'lesson', $lesson->id, $user->id);
155 if (empty($grades->items[0]->grades)) {
156 $return->info = get_string("no")." ".get_string("attempts", "lesson");
158 $grade = reset($grades->items[0]->grades);
159 $return->info = get_string("grade") . ': ' . $grade->str_long_grade;
160 $return->time = $grade->dategraded;
161 $return->info = get_string("no")." ".get_string("attempts", "lesson");
167 * Print a detailed representation of what a user has done with
168 * a given particular instance of this module, for user activity reports.
171 * @param object $course
172 * @param object $user
174 * @param object $lesson
177 function lesson_user_complete($course, $user, $mod, $lesson) {
178 global $DB, $OUTPUT, $CFG;
180 require_once("$CFG->libdir/gradelib.php");
182 $grades = grade_get_grades($course->id, 'mod', 'lesson', $lesson->id, $user->id);
183 if (!empty($grades->items[0]->grades)) {
184 $grade = reset($grades->items[0]->grades);
185 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
186 if ($grade->str_feedback) {
187 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
191 $params = array ("lessonid" => $lesson->id, "userid" => $user->id);
192 if ($attempts = $DB->get_records_select("lesson_attempts", "lessonid = :lessonid AND userid = :userid", $params,
193 "retry, timeseen")) {
194 echo $OUTPUT->box_start();
195 $table = new html_table();
196 $table->head = array (get_string("attempt", "lesson"), get_string("numberofpagesviewed", "lesson"),
197 get_string("numberofcorrectanswers", "lesson"), get_string("time"));
198 $table->width = "100%";
199 $table->align = array ("center", "center", "center", "center");
200 $table->size = array ("*", "*", "*", "*");
201 $table->cellpadding = 2;
202 $table->cellspacing = 0;
208 foreach ($attempts as $attempt) {
209 if ($attempt->retry == $retry) {
211 if ($attempt->correct) {
214 $timeseen = $attempt->timeseen;
216 $table->data[] = array($retry + 1, $npages, $ncorrect, userdate($timeseen));
219 if ($attempt->correct) {
227 $table->data[] = array($retry + 1, $npages, $ncorrect, userdate($timeseen));
229 echo html_writer::table($table);
230 echo $OUTPUT->box_end();
237 * Prints lesson summaries on MyMoodle Page
239 * Prints lesson name, due date and attempt information on
240 * lessons that have a deadline that has not already passed
241 * and it is available for taking.
246 * @uses CONTEXT_MODULE
247 * @param array $courses An array of course objects to get lesson instances from
248 * @param array $htmlarray Store overview output array( course ID => 'lesson' => HTML output )
251 function lesson_print_overview($courses, &$htmlarray) {
252 global $USER, $CFG, $DB, $OUTPUT;
254 if (!$lessons = get_all_instances_in_courses('lesson', $courses)) {
258 /// Get Necessary Strings
259 $strlesson = get_string('modulename', 'lesson');
260 $strnotattempted = get_string('nolessonattempts', 'lesson');
261 $strattempted = get_string('lessonattempted', 'lesson');
264 foreach ($lessons as $lesson) {
265 if ($lesson->deadline != 0 // The lesson has a deadline
266 and $lesson->deadline >= $now // And it is before the deadline has been met
267 and ($lesson->available == 0 or $lesson->available <= $now)) { // And the lesson is available
270 if (!$lesson->visible) {
271 $class = ' class="dimmed"';
275 $str = $OUTPUT->box("$strlesson: <a$class href=\"$CFG->wwwroot/mod/lesson/view.php?id=$lesson->coursemodule\">".
276 format_string($lesson->name).'</a>', 'name');
279 $str .= $OUTPUT->box(get_string('lessoncloseson', 'lesson', userdate($lesson->deadline)), 'info');
281 // Attempt information
282 if (has_capability('mod/lesson:manage', get_context_instance(CONTEXT_MODULE, $lesson->coursemodule))) {
283 // Number of user attempts
284 $attempts = $DB->count_records('lesson_attempts', array('lessonid'=>$lesson->id));
285 $str .= $OUTPUT->box(get_string('xattempts', 'lesson', $attempts), 'info');
287 // Determine if the user has attempted the lesson or not
288 if ($DB->count_records('lesson_attempts', array('lessonid'=>$lesson->id, 'userid'=>$USER->id))) {
289 $str .= $OUTPUT->box($strattempted, 'info');
291 $str .= $OUTPUT->box($strnotattempted, 'info');
294 $str = $OUTPUT->box($str, 'lesson overview');
296 if (empty($htmlarray[$lesson->course]['lesson'])) {
297 $htmlarray[$lesson->course]['lesson'] = $str;
299 $htmlarray[$lesson->course]['lesson'] .= $str;
306 * Function to be run periodically according to the moodle cron
307 * This function searches for things that need to be done, such
308 * as sending out mail, toggling flags etc ...
312 function lesson_cron () {
319 * Return grade for given user or all users.
323 * @param int $lessonid id of lesson
324 * @param int $userid optional user id, 0 means all users
325 * @return array array of grades, false if none
327 function lesson_get_user_grades($lesson, $userid=0) {
330 $params = array("lessonid" => $lesson->id,"lessonid2" => $lesson->id);
332 if (isset($userid)) {
333 $params["userid"] = $userid;
334 $params["userid2"] = $userid;
335 $user = "AND u.id = :userid";
336 $fuser = "AND uu.id = :userid2";
343 if ($lesson->retake) {
344 if ($lesson->usemaxgrade) {
345 $sql = "SELECT u.id, u.id AS userid, MAX(g.grade) AS rawgrade
346 FROM {user} u, {lesson_grades} g
347 WHERE u.id = g.userid AND g.lessonid = :lessonid
351 $sql = "SELECT u.id, u.id AS userid, AVG(g.grade) AS rawgrade
352 FROM {user} u, {lesson_grades} g
353 WHERE u.id = g.userid AND g.lessonid = :lessonid
357 unset($params['lessonid2']);
358 unset($params['userid2']);
360 // use only first attempts (with lowest id in lesson_grades table)
361 $firstonly = "SELECT uu.id AS userid, MIN(gg.id) AS firstcompleted
362 FROM {user} uu, {lesson_grades} gg
363 WHERE uu.id = gg.userid AND gg.lessonid = :lessonid2
367 $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade
368 FROM {user} u, {lesson_grades} g, ($firstonly) f
369 WHERE u.id = g.userid AND g.lessonid = :lessonid
370 AND g.id = f.firstcompleted AND g.userid=f.userid
374 return $DB->get_records_sql($sql, $params);
378 * Update grades in central gradebook
382 * @param object $lesson
383 * @param int $userid specific user only, 0 means all
384 * @param bool $nullifnone
386 function lesson_update_grades($lesson, $userid=0, $nullifnone=true) {
388 require_once($CFG->libdir.'/gradelib.php');
390 if ($lesson->grade == 0) {
391 lesson_grade_item_update($lesson);
393 } else if ($grades = lesson_get_user_grades($lesson, $userid)) {
394 lesson_grade_item_update($lesson, $grades);
396 } else if ($userid and $nullifnone) {
397 $grade = new object();
398 $grade->userid = $userid;
399 $grade->rawgrade = NULL;
400 lesson_grade_item_update($lesson, $grade);
403 lesson_grade_item_update($lesson);
408 * Update all grades in gradebook.
412 function lesson_upgrade_grades() {
415 $sql = "SELECT COUNT('x')
416 FROM {lesson} l, {course_modules} cm, {modules} m
417 WHERE m.name='lesson' AND m.id=cm.module AND cm.instance=l.id";
418 $count = $DB->count_records_sql($sql);
420 $sql = "SELECT l.*, cm.idnumber AS cmidnumber, l.course AS courseid
421 FROM {lesson} l, {course_modules} cm, {modules} m
422 WHERE m.name='lesson' AND m.id=cm.module AND cm.instance=l.id";
423 if ($rs = $DB->get_recordset_sql($sql)) {
424 $pbar = new progress_bar('lessonupgradegrades', 500, true);
426 foreach ($rs as $lesson) {
428 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
429 lesson_update_grades($lesson, 0, false);
430 $pbar->update($i, $count, "Updating Lesson grades ($i/$count).");
437 * Create grade item for given lesson
440 * @uses GRADE_TYPE_VALUE
441 * @uses GRADE_TYPE_NONE
442 * @param object $lesson object with extra cmidnumber
443 * @param array|object $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
444 * @return int 0 if ok, error code otherwise
446 function lesson_grade_item_update($lesson, $grades=NULL) {
448 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
449 require_once($CFG->libdir.'/gradelib.php');
452 if (array_key_exists('cmidnumber', $lesson)) { //it may not be always present
453 $params = array('itemname'=>$lesson->name, 'idnumber'=>$lesson->cmidnumber);
455 $params = array('itemname'=>$lesson->name);
458 if ($lesson->grade > 0) {
459 $params['gradetype'] = GRADE_TYPE_VALUE;
460 $params['grademax'] = $lesson->grade;
461 $params['grademin'] = 0;
464 $params['gradetype'] = GRADE_TYPE_NONE;
467 if ($grades === 'reset') {
468 $params['reset'] = true;
470 } else if (!empty($grades)) {
471 // Need to calculate raw grade (Note: $grades has many forms)
472 if (is_object($grades)) {
473 $grades = array($grades->userid => $grades);
474 } else if (array_key_exists('userid', $grades)) {
475 $grades = array($grades['userid'] => $grades);
477 foreach ($grades as $key => $grade) {
478 if (!is_array($grade)) {
479 $grades[$key] = $grade = (array) $grade;
481 $grades[$key]['rawgrade'] = ($grade['rawgrade'] * $lesson->grade / 100);
485 return grade_update('mod/lesson', $lesson->course, 'mod', 'lesson', $lesson->id, 0, $grades, $params);
489 * Delete grade item for given lesson
492 * @param object $lesson object
493 * @return object lesson
495 function lesson_grade_item_delete($lesson) {
502 * Must return an array of user records (all data) who are participants
503 * for a given instance of lesson. Must include every user involved
504 * in the instance, independent of his role (student, teacher, admin...)
508 * @param int $lessonid
511 function lesson_get_participants($lessonid) {
515 $params = array ("lessonid" => $lessonid);
516 $students = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
519 WHERE a.lessonid = :lessonid and
520 u.id = a.userid", $params);
522 //Return students array (it contains an array of unique users)
529 function lesson_get_view_actions() {
530 return array('view','view all');
536 function lesson_get_post_actions() {
537 return array('end','start');
541 * Runs any processes that must run before
542 * a lesson insert/update
545 * @param object $lesson Lesson form data
548 function lesson_process_pre_save(&$lesson) {
551 $lesson->timemodified = time();
553 if (empty($lesson->timed)) {
556 if (empty($lesson->timespent) or !is_numeric($lesson->timespent) or $lesson->timespent < 0) {
557 $lesson->timespent = 0;
559 if (!isset($lesson->completed)) {
560 $lesson->completed = 0;
562 if (empty($lesson->gradebetterthan) or !is_numeric($lesson->gradebetterthan) or $lesson->gradebetterthan < 0) {
563 $lesson->gradebetterthan = 0;
564 } else if ($lesson->gradebetterthan > 100) {
565 $lesson->gradebetterthan = 100;
568 if (empty($lesson->width)) {
569 $lesson->width = 640;
571 if (empty($lesson->height)) {
572 $lesson->height = 480;
574 if (empty($lesson->bgcolor)) {
575 $lesson->bgcolor = '#FFFFFF';
578 // Conditions for dependency
579 $conditions = new stdClass;
580 $conditions->timespent = $lesson->timespent;
581 $conditions->completed = $lesson->completed;
582 $conditions->gradebetterthan = $lesson->gradebetterthan;
583 $lesson->conditions = serialize($conditions);
584 unset($lesson->timespent);
585 unset($lesson->completed);
586 unset($lesson->gradebetterthan);
588 if (empty($lesson->password)) {
589 unset($lesson->password);
594 * Runs any processes that must be run
595 * after a lesson insert/update
598 * @param object $lesson Lesson form data
601 function lesson_process_post_save(&$lesson) {
603 require_once($CFG->dirroot.'/calendar/lib.php');
604 require_once($CFG->dirroot . '/mod/lesson/locallib.php');
606 if ($events = $DB->get_records('event', array('modulename'=>'lesson', 'instance'=>$lesson->id))) {
607 foreach($events as $event) {
608 $event = calendar_event::load($event->id);
613 $event = new stdClass;
614 $event->description = $lesson->name;
615 $event->courseid = $lesson->course;
618 $event->modulename = 'lesson';
619 $event->instance = $lesson->id;
620 $event->eventtype = 'open';
621 $event->timestart = $lesson->available;
623 $event->visible = instance_is_visible('lesson', $lesson);
625 $event->timeduration = ($lesson->deadline - $lesson->available);
627 if ($lesson->deadline and $lesson->available and $event->timeduration <= LESSON_MAX_EVENT_LENGTH) {
628 // Single event for the whole lesson.
629 $event->name = $lesson->name;
630 calendar_event::create(clone($event));
632 // Separate start and end events.
633 $event->timeduration = 0;
634 if ($lesson->available) {
635 $event->name = $lesson->name.' ('.get_string('lessonopens', 'lesson').')';
636 calendar_event::create(clone($event));
637 } else if ($lesson->deadline) {
638 $event->name = $lesson->name.' ('.get_string('lessoncloses', 'lesson').')';
639 $event->timestart = $lesson->deadline;
640 $event->eventtype = 'close';
641 calendar_event::create(clone($event));
648 * Implementation of the function for printing the form elements that control
649 * whether the course reset functionality affects the lesson.
651 * @param $mform form passed by reference
653 function lesson_reset_course_form_definition(&$mform) {
654 $mform->addElement('header', 'lessonheader', get_string('modulenameplural', 'lesson'));
655 $mform->addElement('advcheckbox', 'reset_lesson', get_string('deleteallattempts','lesson'));
659 * Course reset form defaults.
660 * @param object $course
663 function lesson_reset_course_form_defaults($course) {
664 return array('reset_lesson'=>1);
668 * Removes all grades from gradebook
672 * @param int $courseid
673 * @param string optional type
675 function lesson_reset_gradebook($courseid, $type='') {
678 $sql = "SELECT l.*, cm.idnumber as cmidnumber, l.course as courseid
679 FROM {lesson} l, {course_modules} cm, {modules} m
680 WHERE m.name='lesson' AND m.id=cm.module AND cm.instance=l.id AND l.course=:course";
681 $params = array ("course" => $courseid);
682 if ($lessons = $DB->get_records_sql($sql,$params)) {
683 foreach ($lessons as $lesson) {
684 lesson_grade_item_update($lesson, 'reset');
690 * Actual implementation of the rest coures functionality, delete all the
691 * lesson attempts for course $data->courseid.
695 * @param object $data the data submitted from the reset course.
696 * @return array status array
698 function lesson_reset_userdata($data) {
701 $componentstr = get_string('modulenameplural', 'lesson');
704 if (!empty($data->reset_lesson)) {
705 $lessonssql = "SELECT l.id
707 WHERE l.course=:course";
709 $params = array ("course" => $data->courseid);
710 $DB->delete_records_select('lesson_timer', "lessonid IN ($lessonssql)", $params);
711 $DB->delete_records_select('lesson_high_scores', "lessonid IN ($lessonssql)", $params);
712 $DB->delete_records_select('lesson_grades', "lessonid IN ($lessonssql)", $params);
713 $DB->delete_records_select('lesson_attempts', "lessonid IN ($lessonssql)", $params);
715 // remove all grades from gradebook
716 if (empty($data->reset_gradebook_grades)) {
717 lesson_reset_gradebook($data->courseid);
720 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'lesson'), 'error'=>false);
723 /// updating dates - shift may be negative too
724 if ($data->timeshift) {
725 shift_course_mod_dates('lesson', array('available', 'deadline'), $data->timeshift, $data->courseid);
726 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
733 * Returns all other caps used in module
736 function lesson_get_extra_capabilities() {
737 return array('moodle/site:accessallgroups');
741 * @uses FEATURE_GROUPS
742 * @uses FEATURE_GROUPINGS
743 * @uses FEATURE_GROUPMEMBERSONLY
744 * @uses FEATURE_MOD_INTRO
745 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
746 * @uses FEATURE_GRADE_HAS_GRADE
747 * @uses FEATURE_GRADE_OUTCOMES
748 * @param string $feature FEATURE_xx constant for requested feature
749 * @return mixed True if module supports feature, false if not, null if doesn't know
751 function lesson_supports($feature) {
753 case FEATURE_GROUPS: return false;
754 case FEATURE_GROUPINGS: return false;
755 case FEATURE_GROUPMEMBERSONLY: return true;
756 case FEATURE_MOD_INTRO: return false;
757 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
758 case FEATURE_GRADE_HAS_GRADE: return true;
759 case FEATURE_GRADE_OUTCOMES: return true;
760 case FEATURE_BACKUP_MOODLE2: return true;
761 default: return null;
766 * This function extends the global navigation for the site.
767 * It is important to note that you should not rely on PAGE objects within this
768 * body of code as there is no guarantee that during an AJAX request they are
771 * @param navigation_node $navigation The lesson node within the global navigation
772 * @param stdClass $course The course object returned from the DB
773 * @param stdClass $module The module object returned from the DB
774 * @param stdClass $cm The course module instance returned from the DB
776 function lesson_extend_navigation($navigation, $course, $module, $cm) {
778 * This is currently just a stub so that it can be easily expanded upon.
779 * When expanding just remove this comment and the line below and then add
782 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
786 * This function extends the settings navigation block for the site.
788 * It is safe to rely on PAGE here as we will only ever be within the module
789 * context when this is called
791 * @param settings_navigation $settings
792 * @param navigation_node $lessonnode
794 function lesson_extend_settings_navigation($settings, $lessonnode) {
797 $canedit = has_capability('mod/lesson:edit', $PAGE->cm->context);
799 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id));
800 $lessonnode->add(get_string('preview', 'lesson'), $url);
803 $url = new moodle_url('/mod/lesson/edit.php', array('id'=>$PAGE->cm->id));
804 $lessonnode->add(get_string('edit', 'lesson'), $url);
807 if (has_capability('mod/lesson:manage', $PAGE->cm->context)) {
808 $reportsnode = $lessonnode->add(get_string('reports', 'lesson'));
809 $url = new moodle_url('/mod/lesson/report.php', array('id'=>$PAGE->cm->id, 'action'=>'reportoverview'));
810 $reportsnode->add(get_string('overview', 'lesson'), $url);
811 $url = new moodle_url('/mod/lesson/report.php', array('id'=>$PAGE->cm->id, 'action'=>'reportdetail'));
812 $reportsnode->add(get_string('detailedstats', 'lesson'), $url);
816 $url = new moodle_url('/mod/lesson/essay.php', array('id'=>$PAGE->cm->id));
817 $lessonnode->add(get_string('manualgrading', 'lesson'), $url);
820 if ($PAGE->activityrecord->highscores) {
821 $url = new moodle_url('/mod/lesson/highscores.php', array('id'=>$PAGE->cm->id));
822 $lessonnode->add(get_string('highscores', 'lesson'), $url);
827 * Get list of available import or export formats
829 * Copied and modified from lib/questionlib.php
831 * @param string $type 'import' if import list, otherwise export list assumed
832 * @return array sorted list of import/export formats available
834 function lesson_get_import_export_formats($type) {
836 $fileformats = get_plugin_list("qformat");
838 $fileformatname=array();
839 foreach ($fileformats as $fileformat=>$fdir) {
840 $format_file = "$fdir/format.php";
841 if (file_exists($format_file) ) {
842 require_once($format_file);
846 $classname = "qformat_$fileformat";
847 $format_class = new $classname();
848 if ($type=='import') {
849 $provided = $format_class->provide_import();
851 $provided = $format_class->provide_export();
854 //TODO: do NOT rely on [[]] any more!!
855 $formatname = get_string($fileformat, 'quiz');
856 if ($formatname == "[[$fileformat]]") {
857 $formatname = get_string($fileformat, 'qformat_'.$fileformat);
858 if ($formatname == "[[$fileformat]]") {
859 $formatname = $fileformat; // Just use the raw folder name
862 $fileformatnames[$fileformat] = $formatname;
865 natcasesort($fileformatnames);
867 return $fileformatnames;
871 * Serves the lesson attachments. Implements needed access control ;-)
873 * @param object $course
875 * @param object $context
876 * @param string $filearea
878 * @param bool $forcedownload
879 * @return bool false if file not found, does not return if found - justsend the file
881 function lesson_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
884 if ($context->contextlevel != CONTEXT_MODULE) {
888 $fileareas = lesson_get_file_areas();
889 if (!array_key_exists($filearea, $fileareas)) {
893 if (!$lesson = $DB->get_record('lesson', array('id'=>$cm->instance))) {
897 require_course_login($course, true, $cm);
899 if ($filearea === 'page_contents') {
900 $pageid = (int)array_shift($args);
901 if (!$page = $DB->get_record('lesson_pages', array('id'=>$pageid))) {
904 $fullpath = "/$context->id/mod_lesson/$filearea/$pageid/".implode('/', $args);
905 $forcedownload = true; //TODO: this is strange (skodak)
908 $fullpath = "/$context->id/mod_lesson/$filearea/".implode('/', $args);
911 $fs = get_file_storage();
912 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
916 // finally send the file
917 send_stored_file($file, 0, 0, $forcedownload); // download MUST be forced - security!
921 * Returns an array of file areas
924 function lesson_get_file_areas() {
926 $areas['page_contents'] = 'page_contents'; //TODO: localize!!!!
927 $areas['media_files'] = 'media_files'; //TODO: localize!!!!
931 * Returns a file_info_stored object for the file being requested here
933 * @global <type> $CFG
934 * @param file_browse $browser
935 * @param array $areas
936 * @param object $course
938 * @param object $context
939 * @param string $filearea
941 * @param string $filepath
942 * @param string $filename
943 * @return file_info_stored
945 function lesson_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
947 if (has_capability('moodle/course:managefiles', $context)) {
948 // no peaking here for students!!
952 $fs = get_file_storage();
953 $filepath = is_null($filepath) ? '/' : $filepath;
954 $filename = is_null($filename) ? '.' : $filename;
955 $urlbase = $CFG->wwwroot.'/pluginfile.php';
956 if (!$storedfile = $fs->get_file($context->id, 'mod_lesson', $filearea, $itemid, $filepath, $filename)) {
959 return new file_info_stored($browser, $context, $storedfile, $urlbase, $filearea, $itemid, true, true, false);