Merge branch 'MDL-25660-workshop-calendar' of git://github.com/mudrd8mz/moodle

Conflicts:
	mod/workshop/lang/en/workshop.php
	mod/workshop/version.php
    mod/workshop/db/upgrade.php
This commit is contained in:
Dan Poltawski 2012-04-17 16:29:27 +08:00
commit bb0bd8cb58
4 changed files with 148 additions and 1 deletions

View file

@ -29,6 +29,8 @@
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/calendar/lib.php');
////////////////////////////////////////////////////////////////////////////////
// Moodle core API //
////////////////////////////////////////////////////////////////////////////////
@ -108,6 +110,9 @@ function workshop_add_instance(stdclass $workshop) {
workshop_grade_item_update($workshop);
workshop_grade_item_category_update($workshop);
// create calendar events
workshop_calendar_update($workshop, $workshop->coursemodule);
return $workshop->id;
}
@ -158,6 +163,9 @@ function workshop_update_instance(stdclass $workshop) {
workshop_grade_item_update($workshop);
workshop_grade_item_category_update($workshop);
// update calendar events
workshop_calendar_update($workshop, $workshop->coursemodule);
return true;
}
@ -213,6 +221,13 @@ function workshop_delete_instance($id) {
call_user_func($classname.'::delete_instance', $workshop->id);
}
// delete the calendar events
$events = $DB->get_records('event', array('modulename' => 'workshop', 'instance' => $workshop->id));
foreach ($events as $event) {
$event = calendar_event::load($event);
$event->delete();
}
// finally remove the workshop record itself
$DB->delete_records('workshop', array('id' => $workshop->id));
@ -1426,3 +1441,94 @@ function workshop_page_type_list($pagetype, $parentcontext, $currentcontext) {
$module_pagetype = array('mod-workshop-*'=>get_string('page-mod-workshop-x', 'workshop'));
return $module_pagetype;
}
////////////////////////////////////////////////////////////////////////////////
// Calendar API //
////////////////////////////////////////////////////////////////////////////////
/**
* Updates the calendar events associated to the given workshop
*
* @param stdClass $workshop the workshop instance record
* @param int $cmid course module id
*/
function workshop_calendar_update(stdClass $workshop, $cmid) {
global $DB;
// get the currently registered events so that we can re-use their ids
$currentevents = $DB->get_records('event', array('modulename' => 'workshop', 'instance' => $workshop->id));
// the common properties for all events
$base = new stdClass();
$base->description = format_module_intro('workshop', $workshop, $cmid, false);
$base->courseid = $workshop->course;
$base->groupid = 0;
$base->userid = 0;
$base->modulename = 'workshop';
$base->eventtype = 'pluginname';
$base->instance = $workshop->id;
$base->visible = instance_is_visible('workshop', $workshop);
$base->timeduration = 0;
if ($workshop->submissionstart) {
$event = clone($base);
$event->name = get_string('submissionstartevent', 'mod_workshop', $workshop->name);
$event->timestart = $workshop->submissionstart;
if ($reusedevent = array_shift($currentevents)) {
$event->id = $reusedevent->id;
} else {
// should not be set but just in case
unset($event->id);
}
// calendar_event::create will reuse a db record if the id field is set
calendar_event::create($event);
}
if ($workshop->submissionend) {
$event = clone($base);
$event->name = get_string('submissionendevent', 'mod_workshop', $workshop->name);
$event->timestart = $workshop->submissionend;
if ($reusedevent = array_shift($currentevents)) {
$event->id = $reusedevent->id;
} else {
// should not be set but just in case
unset($event->id);
}
// calendar_event::create will reuse a db record if the id field is set
calendar_event::create($event);
}
if ($workshop->assessmentstart) {
$event = clone($base);
$event->name = get_string('assessmentstartevent', 'mod_workshop', $workshop->name);
$event->timestart = $workshop->assessmentstart;
if ($reusedevent = array_shift($currentevents)) {
$event->id = $reusedevent->id;
} else {
// should not be set but just in case
unset($event->id);
}
// calendar_event::create will reuse a db record if the id field is set
calendar_event::create($event);
}
if ($workshop->assessmentend) {
$event = clone($base);
$event->name = get_string('assessmentendevent', 'mod_workshop', $workshop->name);
$event->timestart = $workshop->assessmentend;
if ($reusedevent = array_shift($currentevents)) {
$event->id = $reusedevent->id;
} else {
// should not be set but just in case
unset($event->id);
}
// calendar_event::create will reuse a db record if the id field is set
calendar_event::create($event);
}
// delete any leftover events
foreach ($currentevents as $oldevent) {
$oldevent = calendar_event::load($oldevent);
$oldevent->delete();
}
}