Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

php - Create Moodle activities programmatically

Does anyone know if it’s possible to add an activity programmatically to a course in Moodle?

I was thinking to maybe use the class module_add_instance() from the lib.php of my custom plugin...

e.g.

function feedback_add_instance(stdClass $mod) {
   global $DB;

   $newmodule->timecreated = time();

   // You may have to add extra stuff in here.

   $newmodule->id = $DB->insert_record('exams', $newmodule);

   unicexams_grade_item_update($newmodule);

   return $newmodule->id;
}

But then again: What is the $mod variable? What does it contain and how to construct it?

Does anyone has knowledge on the subject? Or something to advise?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

add_moduleinfo() would be better. Below is something I use for facetoface.

To get started, edit /course/modedit.php then temporarily add the following, then add the required activity via the front end - this will give you a list of most of the properties required:

var_dump($fromform);
die();
$fromform = add_moduleinfo($fromform, $course, $mform);

Code to create a facetoface instance

$newfacetoface = new stdClass();
$newfacetoface->name = $facetoface->facetofacename;
$newfacetoface->intro = '';
$newfacetoface->thirdparty = '';
$newfacetoface->display = 6;
$newfacetoface->approvalreqd = 0;
$newfacetoface->selfapprovaltandc = $strmgr->get_string('selfapprovaltandccontents', 'facetoface', $facetoface->langcode);
$newfacetoface->allowcancellationsdefault = 1;
$newfacetoface->cancellationscutoffdefault = 0;
$newfacetoface->multiplesessions = 1; // Allow multiple sessions.
$newfacetoface->managerreserve = '0';
$newfacetoface->maxmanagerreserves = '1';
$newfacetoface->reservecancel = '1';
$newfacetoface->reservecanceldays = '1';
$newfacetoface->reservedays = '2';
$newfacetoface->showoncalendar = '1';
$newfacetoface->usercalentry = '1';
$newfacetoface->shortname = '';
$newfacetoface->published = $facetoface->visible;
$newfacetoface->branches = $facetoface->branches;
$newfacetoface->visible = $facetoface->visible;
$newfacetoface->cmidnumber = $facetoface->facetofaceid;
$newfacetoface->idnumber = $facetoface->facetofaceid;
$newfacetoface->groupmode = '0';
$newfacetoface->availabilityconditionsjson = '{"op":"&","c":[],"showc":[]}';
$newfacetoface->completionunlocked = 1;
$newfacetoface->completionunlockednoreset = 0;
$newfacetoface->completion = COMPLETION_TRACKING_AUTOMATIC;
$newfacetoface->completionstatusrequired = '{"100":1}';
$newfacetoface->completionexpected = 0;
$newfacetoface->course = $course->id;
$newfacetoface->coursemodule = 0;
$newfacetoface->section = 1;
$newfacetoface->module = $moduleid;
$newfacetoface->modulename = 'facetoface';
$newfacetoface->instance = 0;
$newfacetoface->add = 'facetoface';
$newfacetoface->update = 0;
$newfacetoface->return = 0;
$newfacetoface->sr = 0;

$moduleinfo = add_moduleinfo($newfacetoface, $course);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...