Index: temp_moodle_dev/moodle/mod/lesson/lib.php =================================================================== RCS file: /usr/local/cvsroot/temp_moodle_dev/moodle/mod/lesson/lib.php,v diff -u -r1.1 -r1.2 --- temp_moodle_dev/moodle/mod/lesson/lib.php 14 Nov 2008 06:30:06 -0000 1.1 +++ temp_moodle_dev/moodle/mod/lesson/lib.php 5 Jan 2009 23:25:58 -0000 1.2 @@ -661,4 +661,235 @@ return array('moodle/site:accessallgroups'); } + +// +/** + * LAMS Function + * This function creates new pages, branches and answers of a given lesson + */ +function add_pages__branches_answers($existinglesson,$userid,$oldid) { + + $pages= get_records("lesson_pages","lessonid",$oldid); + + $oldpages= array(); + if ( $pages ) { + foreach ($pages as $page) { + $page->lessonid = $existinglesson->id; + $page->contents = addslashes($page->contents); + $page->oldid = $page->id; + $page->title = addslashes($page->title); + $page->timemodified = time(); + $page->id=insert_record("lesson_pages", $page); + $oldpages[$page->oldid]=$page->id; + } + } + + $branches = get_records("lesson_branch", "lessonid", $oldid); + + if ( $branches ) { + + foreach ($branches as $branch) { + $branch->lessonid=$existinglesson->id; + $branch->userid=$userid; + $num=$branch->pageid; + $branch->pageid=$oldpages[$num]; + + insert_record("lesson_branch", $branch); + + + } + } + $answers = get_records("lesson_answers", "lessonid",$oldid); + if ( $answers ) { + foreach ($answers as $answer) { + $answer->lessonid=$existinglesson->id; + $answer->timecreated=time(); + $answer->answer = addslashes($answer->answer); + $answer->response = addslashes($answer->response); + $num=$answer->pageid; + + $answer->pageid=$oldpages[$num]; + + $answer->id=insert_record("lesson_answers", $answer); + } + } + // change page ids now that all new pages have been created + $newpages= get_records("lesson_pages","lessonid",$existinglesson->id); + if ( $newpages ) { + foreach ($newpages as $page) { + $num=$page->prevpageid; + $page->prevpageid = $oldpages[$num]; + $num=$page->nextpageid; + $page->nextpageid = $oldpages[$num]; + update_record("lesson_pages", $page); + + } + } +} +// +/** + * LAMS Function + * This function clones an existing instance of Moodle lesson + * replacing the course and the userid + */ +function lesson_clone_instance($id, $sectionref, $courseid,$userid) { + global $CFG; + + $cm = get_record('course_modules', 'id', $id); + // echo("hola"); + if (!$cm||$cm->instance==0) { + // create a new lesson with default content + $existinglesson->course = $courseid; + $existinglesson->name = "Lesson"; + $existinglesson->is_lams = 1; + $existinglesson->timemodified = time(); + $existinglesson->id = lesson_add_instance($existinglesson);//insert_record('lesson', $existinglesson); + // print_r("blanc".$existinglesson); + // echo($existinglesson); + } else{ + // make a copy of an existing lesson + $existinglesson = get_record('lesson', 'id', $cm->instance); + $existinglesson->old_id = $existinglesson->id; + unset($existinglesson->id); + $existinglesson->timemodified = time(); + $existinglesson->course = $courseid; + $existinglesson->is_lams = 1; + $existinglesson->id = lesson_add_instance($existinglesson);//insert_record('lesson', $existinglesson); + // print_r("no blanc".$existinglesson); + // echo("hola2"); + } + //echo("hola3"); + $module = get_record('modules', 'name', 'lesson'); + $section = get_course_section($sectionref, $courseid); + + // module parameters + $cm->course = $courseid; + $cm->module = $module->id; + $cm->instance = $existinglesson->id; + $cm->added = time(); + $cm->section = $section->id; + $cm->is_lams = 1; + $cm->course = $courseid; + $cm->id = insert_record('course_modules', $cm); + + //adds the new lesson to the sequence variable in section table + $existinglesson->section = $sectionref; + $existinglesson->coursemodule = $cm->id; + $existinglesson->instance = $cm->instance; + + require_once($CFG->dirroot.'/course/lib.php'); + add_mod_to_section($existinglesson); + + //add pages branches and answers for the new lesson + if($existinglesson){ + + add_pages__branches_answers($existinglesson,$userid, $existinglesson->old_id); + } + + return $cm->id; +} + +/** + * LAMS Function + * Serialize a lesson instance and return serialized string to LAMS + */ +function lesson_export_instance($id) { + $cm = get_record('course_modules', 'id', $id); + if ($cm) { + if ($lesson = get_record('lesson', 'id', $cm->instance)) { + //get all pages from the lesson + $pages = get_records('lesson_pages', 'lessonid', $lesson->id); + $branches = get_record('lesson_branch','lessonid',$lesson->id); + $answers = get_record('lesson_answers','lessonid',$lesson->id); + //our file will contain the lesson, its pages and categories + $all=array($lesson,$pages,$branches,$answers); + // serialize lesson into a string; assuming lesson object + // doesn't contain binary data in any of its columns + $s = serialize($all); + header('Content-Description: File Transfer'); + header('Content-Type: text/plain'); + header('Content-Length: ' . strlen($s)); + echo $s; + exit; + } + } + header('HTTP/1.1 500 Internal Error'); + exit; +} + +/** + * LAMS Function + * Deserializes a serialized Moodle lesson, and creates a new instance of it + */ +function lesson_import_instance($filepath, $userid, $courseid, $sectionid) { + // file contents contains serialized lesson object + $filestr = file_get_contents($filepath); + $all=unserialize($filestr); + + //we split the array so we can get the lesson, pages, branches and answers values + $lesson = $all[0]; + $pages = $all[1]; + $branches = $all[2]; + $answers = $all[3]; + // import this lesson into a new course + $lesson->course = $courseid; + + // escape text columns for saving into database + $lesson->name = addslashes($lesson->name); + $lesson->timemodified =time(); + $lesson->oldid = $lesson->id; + + + if ( ! $lesson->id = insert_record('lesson', $lesson) ) { + return 0; + }else{ + //add pages branches and answers for the new lesson + add_pages__branches_answers($lesson,$userid,$lesson->oldid); + } + $module = get_record('modules', 'name', 'lesson'); + $section = get_course_section($sectionid, $courseid); + + $cm->course = $courseid; + $cm->module = $module->id; + $cm->instance = $lesson->id; + $cm->added = time(); + $cm->section = $section->section; + $cm->is_lams = 1; + $cm->id = insert_record('course_modules', $cm); + + + return $cm->id; +} + +/** + * LAMS Function + * Return a statistic for a given user in this Moodle lesson for use in branching + */ +function lesson_get_tool_output($id, $userid) { + $cm = get_record('course_modules', 'id', $id); + if ($cm) { + $grade = get_record('lesson_grades', 'userid', $userid, 'lessonid', $cm->instance); + if ($grade) { + $percentage =( $grade->grade)*10; + return $percentage; + }else{ + return 0; + } + } + return 0; +} + +/** + * LAMS Function + * Return an HTML representation of a user's activity in this lesson + */ +function lesson_export_portfolio($id, $userid) { + global $CFG; + $text = 'This tool does not support export portfolio. It may be accessible via '; + $text .= 'this link.'; + return $text; + +} + + ?>