Moodle 複製一個回饋活動(feedback activity)到所有課程中

edited 十一月 -1 in 數位學習平台
前因後果請參考
http://twpug.net/modules/newbb/viewtopic.php?topic_id=7909

因為回饋活動提供比較彈性的欄位吧,所以就再做了這個出來

<?php

$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';

$feedbackModule = $DB->get_record('modules', array('name' => 'feedback'));
if(empty($feedbackModule->id)) {
die('feedback 模組不存在!');
}

// confirm if the feedback existed
$feedbackId = intval($argv[1]);
if ($feedbackId > 0) {
$mRecord = $DB->get_record('course_modules', array('id' => $feedbackId, 'module' => $feedbackModule->id));
if (!empty($mRecord->instance)) {
$feedback = $DB->get_record('feedback', array('id' => $mRecord->instance));
if(!empty($feedback->id)) {
$feedbackItems = $DB->get_records_sql(\"SELECT * FROM {feedback_item} WHERE feedback = {$feedback->id}\");
}
}
}

// get the course of the feedback
if (!empty($feedback->course)) {
$course = $DB->get_record('course', array('id' => $feedback->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 feedback first
if (isset($feedback->id)) {
unset($feedback->id);
}

$feedback->course = $lCourse->id;
$targetFeedbackId = $DB->insert_record('feedback', $feedback);

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

foreach($feedbackItems AS $feedbackItem) {
unset($feedbackItem->id);
$feedbackItem->feedback = $targetFeedbackId;
$DB->insert_record('feedback_item', $feedbackItem);
}

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


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