Index: temp_moodle_dev/lamstwo/tooladapter_forum.php =================================================================== RCS file: /usr/local/cvsroot/temp_moodle_dev/lamstwo/Attic/tooladapter_forum.php,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ temp_moodle_dev/lamstwo/tooladapter_forum.php 22 Aug 2008 05:11:43 -0000 1.1 @@ -0,0 +1,56 @@ +libdir.'/datalib.php'); + include_once('../forum/lib.php'); + + $ts = optional_param('ts', '', PARAM_TEXT); + $username = optional_param('un', '', PARAM_TEXT); + $hs = optional_param('hs', '', PARAM_TEXT); + + if ( ! isset($CFG->lamstwo_serverid) || ! isset($CFG->lamstwo_serverkey) ) { + header('HTTP/1.1 401 Unauthenticated'); + exit(1); + } + $plaintext = trim($ts).trim($username).trim($CFG->lamstwo_serverid).trim($CFG->lamstwo_serverkey); + $hash = sha1(strtolower($plaintext)); + if ( $hash != $hs ){ + header('HTTP/1.1 401 Unauthenticated'); + exit(1); + } + + $method = optional_param('method', '', PARAM_TEXT); + $cmid = optional_param('extToolContentID', 0, PARAM_INT); + $sectionid = optional_param('section', 1, PARAM_INT); + $courseid = optional_param('cs', 1, PARAM_INT); + $outputname = optional_param('oname', '', PARAM_TEXT); + + $user = get_record('user', 'username', $username); + + switch ($method) { + case 'clone': + $newcmid = forum_clone_instance($cmid, $sectionid, $courseid, $user->id); + echo $newcmid; + break; + case 'import': + $newcmid = forum_import_instance($_FILES['upload_file']['tmp_name'], $user->id, $courseid, $sectionid); + echo $newcmid; + break; + case 'export': + forum_export_instance($cmid); + break; + case 'export_portfolio': + $text = forum_export_portfolio($cmid, $user->id); + echo $text; + break; + case 'output': + $output = forum_get_tool_output($cmid, $outputname, $user->id); + echo $output; + break; + default: + } +?> Index: temp_moodle_dev/moodle/mod/forum/lib.php =================================================================== RCS file: /usr/local/cvsroot/temp_moodle_dev/moodle/mod/forum/lib.php,v diff -u -r1.2 -r1.3 --- temp_moodle_dev/moodle/mod/forum/lib.php 19 Aug 2008 08:15:22 -0000 1.2 +++ temp_moodle_dev/moodle/mod/forum/lib.php 22 Aug 2008 05:11:43 -0000 1.3 @@ -1882,7 +1882,7 @@ $timedsql = "AND (d.timestart < $now AND (d.timeend = 0 OR d.timeend > $now))"; } } - + return get_records_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture, u.imagealt FROM {$CFG->prefix}forum f JOIN {$CFG->prefix}forum_discussions d ON d.forum = f.id @@ -6691,34 +6691,132 @@ } /** - * + * LAMS Function + * This function clones an existing instance of Moodle forum + * replacing the course and the userid */ -function forum_clone_instance($id, $courseid, $userid) { -# global $CFG; -# require_once($CFG->dirroot.'/course/lib.php'); +function forum_clone_instance($id, $sectionid, $courseid, $userid) { + $cm = get_record('course_modules', 'id', $id); - if ( ! $existingforum = get_record('forum', 'id', $id) ) { + if ( ! $cm or ! $existingforum = get_record('forum', 'id', $cm->instance) ) { + // create a new forum with default content $existingforum->course = $courseid; $existingforum->name = "Forum"; $existingforum->intro = ""; $existingforum->is_lams = 1; $existingforum->id = insert_record('forum', $existingforum); } else { + // make a copy of an existing forum unset($existingforum->id); + $existingforum->name = addslashes($existingforum->name); + $existingforum->intro = addslashes($existingforum->intro); $existingforum->course = $courseid; $existingforum->is_lams = 1; $existingforum->id = insert_record('forum', $existingforum); } $module = get_record('modules', 'name', 'forum'); + $section = get_course_section($sectionid, $courseid); $cm->course = $courseid; $cm->module = $module->id; $cm->instance = $existingforum->id; $cm->added = time(); -# $cm->id = add_course_module($cm); + $cm->section = $section->id; $cm->id = insert_record('course_modules', $cm); + + return $cm->id; } +/** + * LAMS Function + * Serialize a forum instance and return serialized string to LAMS + */ +function forum_export_instance($id) { + $cm = get_record('course_modules', 'id', $id); + if ($cm) { + if ($forum = get_record('forum', 'id', $cm->instance)) { + // serialize forum into a string; assuming forum object + // doesn't contain binary data in any of its columns + $s = serialize($forum); + 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 forum, and creates a new instance of it + */ +function forum_import_instance($filepath, $userid, $courseid, $sectionid) { + // file contents contains serialized forum object + $filestr = file_get_contents($filepath); + $forum = unserialize($filestr); + + // import this forum into a new course + $forum->course = $courseid; + + // escape text columns for saving into database + $forum->name = addslashes($forum->name); + $forum->intro = addslashes($forum->intro); + + if ( ! $forum->id = insert_record('forum', $forum) ) { + return 0; + } + + $module = get_record('modules', 'name', 'forum'); + $section = get_course_section($sectionid, $courseid); + + $cm->course = $courseid; + $cm->module = $module->id; + $cm->instance = $forum->id; + $cm->added = time(); + $cm->section = $section->id; + $cm->id = insert_record('course_modules', $cm); + + return $cm->id; +} + +/** + * LAMS Function + * Return a statistic for a given user in this Moodle forum for use in branching + */ +function forum_get_tool_output($id, $outputname, $userid) { + $cm = get_record('course_modules', 'id', $id); + if ($cm) { + $posts = forum_get_user_posts($cm->instance, $userid); + switch ($outputname) { + case ("learner.number.of.words"): + $numwords = 0; + foreach ($posts as $postid=>$post) { + $numwords += count(explode(' ', $post->message)); + } + return $numwords; + case ("learner.number.of.posts"): + return count($posts); + } + } + return 0; +} + +/** + * LAMS Function + * Return an HTML representation of a user's activity in this forum + */ +function forum_export_portfolio($id, $userid) { + global $CFG; + $text = 'This tool does not support export portfolio. It may be accessible via '; + $text .= 'this link.'; + return $text; + +} + ?>