Moodle 複製一個問卷到所有課程中

edited 十一月 -1 in 數位學習平台
學校的課程經常會需要在期末時進行教學成果的評量,其中一個需求就是對學生發出問卷,但 Moodle 的問卷功能是以課程為單位,因此同樣的問卷內容就需要每個課程逐一操作

為了解決這個問題,寫了一個小程式,在建立了一個問卷之後,將問卷的內容複製到所有課程中。這裡的所有課程侷限在同一個 "根分類" 之下的課程,因為一般分類的最上層會用學期區分。

$moodlePath = dirname(dirname(__FILE__));
if (file_exists($moodlePath . '/config.php') && isset($argv[1])) {
define('CLI_SCRIPT', true);
global $DB, $CFG;
require_once($moodlePath . '/config.php');
include $moodlePath . '/course/lib.php';

// confirm if the survey existed
$surveyId = intval($argv[1]);
if ($surveyId > 0) {
$mRecord = $DB->get_record('course_modules', array('id' => $surveyId, 'module' => 19));
if (!empty($mRecord->instance)) {
$survey = $DB->get_record('survey', array('id' => $mRecord->instance));
}
}

// get the course of the survey
if (!empty($survey->course)) {
$course = $DB->get_record('course', array('id' => $survey->course));
if (!empty($course->category)) {
$category = $DB->get_record('course_categories', array('id' => $course->category));
}
$section = $DB->get_record('course_sections', array(
'id' => $mRecord->section,
'course' => $mRecord->course,
));
}

if (empty($section)) {
die(\"can't get correct record in course_sections\");
}

// get all categories
if (!empty($category->path)) {
$paths = explode('/', $category->path);
if (!empty($paths[1])) {
$categories = $DB->get_records_sql(\"SELECT id FROM {course_categories} WHERE coursecount > 0 AND path LIKE '/{$paths[1]}%'\");
}
}

// loop in courses
if (!empty($categories)) {
$courseStack = array(
$course->id => 1,
);
foreach ($categories AS $lCategory) {
$courses = $DB->get_records_sql(\"SELECT id FROM {course} WHERE category = {$lCategory->id}\");
if (!empty($courses)) {
foreach ($courses AS $lCourse) {
if (isset($courseStack[$lCourse->id])) {
continue;
} else {
$courseStack[$lCourse->id] = 1;
}


//create target survey first
if (isset($survey->id)) {
unset($survey->id);
}

$survey->course = $lCourse->id;
$targetSurveyId = $DB->insert_record('survey', $survey);

//link survey with course_modules
if (!empty($targetSurveyId)) {
if (isset($mRecord->id)) {
unset($mRecord->id);
}

$mRecord->course = $lCourse->id;
$mRecord->instance = $targetSurveyId;
$mRecord->section = '';
$courseModuleId = $DB->insert_record('course_modules', $mRecord);
course_add_cm_to_section($lCourse, $courseModuleId, $section->section);
set_coursemodule_visible($courseModuleId, $mRecord->visible);
}
}
}
}
}
}


程式儲存在 moodle/olctw/survey.php 中,只要先建立一個問卷,檢視問卷時網址會類似 moodle.olc.tw/mod/survey/view.php?id=1020 ,這時候只要進入指令模式執行 php -q survey.php 1020 就會將該問卷複製到同一個根目錄下所有課程。

裡面的程式碼可能需要視情況調整 :)

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=7909
Sign In or Register to comment.