From 526077259c8d4c530db1074f70a80b49ff625bf7 Mon Sep 17 00:00:00 2001 From: Isuru Madushanka Weerarathna Date: Fri, 20 Jul 2012 07:12:42 +0530 Subject: [PATCH] choice calendar events added --- mod/choice/lang/en/choice.php | 2 + mod/choice/lib.php | 80 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 0 deletions(-) diff --git a/mod/choice/lang/en/choice.php b/mod/choice/lang/en/choice.php index c069dc1..e328184 100644 --- a/mod/choice/lang/en/choice.php +++ b/mod/choice/lang/en/choice.php @@ -36,12 +36,14 @@ $string['full'] = '(Full)'; $string['havetologin'] = 'You have to log in before you can submit your choice'; $string['choice'] = 'Choice'; $string['choiceclose'] = 'Until'; +$string['choicecloses'] = 'Choice closes'; $string['choice:deleteresponses'] = 'Delete responses'; $string['choice:downloadresponses'] = 'Download responses'; $string['choicefull'] = 'This choice is full and there are no available places.'; $string['choice:choose'] = 'Record a choice'; $string['choicename'] = 'Choice name'; $string['choiceopen'] = 'Open'; +$string['choiceopens'] = 'Choice opens'; $string['choiceoptions'] = 'Choice options'; $string['choiceoptions_help'] = 'Here is where you specify the options that participants have to choose from. diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 6c247c2..75d9084 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -20,6 +20,7 @@ * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +require_once($CFG->dirroot.'/calendar/lib.php'); /** @global int $CHOICE_COLUMN_HEIGHT */ global $CHOICE_COLUMN_HEIGHT; @@ -40,6 +41,12 @@ define('CHOICE_SHOWRESULTS_ALWAYS', '3'); define('CHOICE_DISPLAY_HORIZONTAL', '0'); define('CHOICE_DISPLAY_VERTICAL', '1'); +/** + * If start and end date for the choice are more than this many seconds apart + * they will be represented by two separate events in the calendar + */ +define('CHOICE_MAX_EVENT_LENGTH', 5*24*60*60); // 5 days + /** @global array $CHOICE_PUBLISH */ global $CHOICE_PUBLISH; $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'), @@ -134,6 +141,9 @@ function choice_add_instance($choice) { } } + // add/update calendar event from this event + choice_update_events($choice); + return $choice->id; } @@ -182,11 +192,72 @@ function choice_update_instance($choice) { } } + // add/update calendar event from this event + choice_update_events($choice); + return $DB->update_record('choice', $choice); } /** + * This function updates the events associated to the choice. + * If $override is non-zero, then it updates only the events + * associated with the specified override. + * + * @uses CHOICE_MAX_EVENT_LENGTH + * @param object $choice the choice object. + */ +function choice_update_events($choice) { + global $DB; + + // Load the old events relating to this choice. + $conds = array('modulename'=>'choice', 'instance'=>$choice->id); + $oldevents = $DB->get_records('event', $conds); + foreach($oldevents as $event) { + $event = calendar_event::load($event->id); + $event->delete(); + } + + if (empty($choice->timerestrict)) { + return; + } + + $event = new stdClass; + $event->description = $choice->intro; + $event->courseid = $choice->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'choice'; + $event->instance = $choice->id; + $event->eventtype = 'open'; + $event->timestart = $choice->timeopen; + + $event->visible = instance_is_visible('choice', $choice); + + $event->timeduration = ($choice->timeclose - $choice->timeopen); + + if ($choice->timeclose and $choice->timeopen and $event->timeduration <= CHOICE_MAX_EVENT_LENGTH) { + // Single event for the whole lesson. + $event->name = $choice->name; + calendar_event::create(clone($event)); + } else { + // Separate start and end events. + $event->timeduration = 0; + if ($choice->timeopen) { + $event->name = $choice->name.' ('.get_string('choiceopens', 'choice').')'; + calendar_event::create(clone($event)); + } + + if ($choice->timeclose) { + $event->name = $choice->name.' ('.get_string('choicecloses', 'choice').')'; + $event->timestart = $choice->timeclose; + $event->eventtype = 'close'; + calendar_event::create(clone($event)); + } + } +} + +/** * @global object * @param object $choice * @param object $user @@ -580,6 +651,15 @@ function choice_delete_instance($id) { $result = false; } + // delete caledar event if exist. + $events = $DB->get_records('event', array('modulename' => 'choice', 'instance' => $choice->id)); + if (!empty($events)) { + foreach ($events as $event) { + $event = calendar_event::load($event); + $event->delete(); + } + } + if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) { $result = false; } -- 1.7.8.msysgit.0