commit 3537d9bedf5f5c8c39937d4f9a72e0cca5f84583
Author: Mark Johnson <mark@barrenfrozenwasteland.com>
Date:   Tue May 4 10:34:16 2010 +0100

    Added latest version of bennu from SVN and calendar import interface.

diff --git a/calendar/calendar_import_form.php b/calendar/calendar_import_form.php
new file mode 100644
index 0000000..e2b9ec0
--- /dev/null
+++ b/calendar/calendar_import_form.php
@@ -0,0 +1,49 @@
+<?php
+require_once '../config.php';
+require_once($CFG->libdir.'/formslib.php');
+require_once($CFG->dirroot.'/calendar/lib.php');
+
+class calendar_import_form extends moodleform {
+    
+    /**
+     * Defines the form elements
+     */
+    function definition() {
+        $mform    =& $this->_form;
+        $mform->addElement('header', 'importheader', 'Select iCal file to import:');
+        $mform->addElement('file', 'importfile', get_string('importcalendar', 'calendar'));
+        $mform->addElement('hidden', 'courseid');
+
+        $mform->addElement('submit', 'preview', 'Preview Import');
+    }
+}
+
+class calendar_import_confirm_form extends moodleform {
+
+    /**
+     * Defines the form elements
+     */
+    function definition() {
+        global $allowed, $courseid;
+        $mform    =& $this->_form;
+        $mform->addElement('header', 'confirmheader', 'Import these events as:');
+        $mform->addElement('hidden', 'calendar');
+        $mform->addElement('hidden', 'courseid');
+        $radio = array();
+
+        if($allowed->site) {
+            $radio[] = &MoodleQuickForm::createElement('radio', 'eventtype', '', get_string('globalevents', 'calendar'), 1);
+        }
+        if($allowed->courses) {
+            $radio[] = &MoodleQuickForm::createElement('radio', 'eventtype', '', get_string('courseevents', 'calendar'), $courseid);
+        }
+        if($allowed->user){
+            $radio[] = &MoodleQuickForm::createElement('radio', 'eventtype', '', get_string('userevents', 'calendar'), 0);
+        }
+        $mform->addGroup($radio, 'eventtypes', get_string('eventkind', 'calendar'));
+
+        $mform->addElement('submit', 'import', 'Import Events');
+    }
+}
+
+?>
diff --git a/calendar/import.php b/calendar/import.php
new file mode 100644
index 0000000..f2ca5ed
--- /dev/null
+++ b/calendar/import.php
@@ -0,0 +1,149 @@
+<?php
+
+require_once('../config.php');
+require_once($CFG->libdir.'/bennu/bennu.inc.php');
+require_once($CFG->libdir.'/tablelib.php');
+require_once($CFG->dirroot.'/calendar/calendar_import_form.php');
+require_once($CFG->dirroot.'/calendar/lib.php');
+
+$courseid = required_param('courseid', PARAM_INT);
+$course = get_record('course', 'id', $courseid);
+
+if ($courseid && $courseid != SITEID) {
+    require_login($courseid);
+} else if ($CFG->forcelogin) {
+    require_login();
+}
+
+
+$now = usergetdate(time());
+$navlinks = array();
+$navlinks[] = array('name' => get_string('calendar', 'calendar'),
+                    'link' =>calendar_get_link_href(CALENDAR_URL.'view.php?view=upcoming&amp;course='.$courseid.'&amp;', $now['mday'], $now['mon'], $now['year']),
+                    'type' => 'misc');
+$navlinks[] = array('name' => get_string('importcalendar', 'calendar'), 'link' =>'', 'type' => 'misc');
+
+$navigation = build_navigation($navlinks);
+
+print_header_simple(get_string('importcalendar', 'calendar'), '', $navigation);
+
+echo '<h1>'.get_string('importcalendar', 'calendar').'</h1>';
+
+$mform = new calendar_import_form();
+$data = new stdClass;
+$data->courseid = $courseid;
+$mform->set_data($data);
+
+$allowed = new stdClass;
+calendar_get_allowed_types($allowed);
+$importform = new calendar_import_confirm_form();
+if($data = $importform->get_data()) {
+    
+    $ical = new iCalendar;
+    $ical->unserialize($data->calendar);
+    $eventcount = 0;
+    $updatecount = 0;
+
+    foreach($ical->components['VEVENT'] as $event) {
+        $eventrecord = new stdClass;
+
+        $name = $event->properties['SUMMARY'][0]->value;
+        $name = str_replace('\n', '<br />', $name);
+        $name = str_replace('\\', '', $name);
+        $name = preg_replace('/\s+/', ' ', $name);
+        $eventrecord->name = clean_param($name, PARAM_CLEAN);
+
+        $description = $event->properties['DESCRIPTION'][0]->value;
+        $description = str_replace('\n', '<br />', $description);
+        $description = str_replace('\\', '', $description);
+        $description = preg_replace('/\s+/', ' ', $description);
+        $eventrecord->description = clean_param($description, PARAM_CLEAN);
+
+        $eventrecord->courseid = $data->eventtypes['eventtype'];
+        $eventrecord->timestart = strtotime($event->properties['DTSTART'][0]->value);
+        $eventrecord->timeduration = strtotime($event->properties['DTEND'][0]->value) - $eventrecord->timestart;
+        $eventrecord->uuid = substr($event->properties['UID'][0]->value, 0, 36); // The UUID field only holds 36 characters.
+        $eventrecord->userid = $USER->id;
+        $eventrecord->timemodified = time();
+        
+        if($updaterecord = get_record('event', 'uuid', $eventrecord->uuid)) {
+            $eventrecord->id = $updaterecord->id;
+            if (update_record('event', $eventrecord)) {
+                $updatecount++;
+            } else {
+                echo '<p>Failed to update event: '.$eventrecord->name.' '.date('H:i d/m/Y', $eventrecord->timestart).'</p>';
+            }
+        } else {
+            if (insert_record('event', $eventrecord)) {
+                $eventcount++;
+            } else {
+                echo '<p>Failed to add event: '.$eventrecord->name.' '.date('H:i d/m/Y', $eventrecord->timestart).'</p>';
+            }
+        }
+    }
+    echo '<p>'.$eventcount.' events imported successfully.</p>';
+    echo '<p>'.$updatecount.' events updated.</p>';
+    echo '<p><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=upcoming&amp;course='.$courseid.'&amp;', $now['mday'], $now['mon'], $now['year']).'">Back to Calendar.</a></p>';
+
+} else {
+    if ($formdata = $mform->get_data()) {
+        $calendar = file_get_contents($_FILES['importfile']['tmp_name']);
+        $ical = new iCalendar;
+        $ical->unserialize($calendar);
+
+        echo '<p>';
+        foreach ($ical->parser_errors as $error) {
+            echo $error.'<br />';
+        }
+        echo '</p>';
+
+        $table = new flexible_table('ical_import');
+        $columns = array('summary', 'description', 'start', 'duration', 'uid');
+        $headers = array('Summary', 'Description', 'Start', 'Duration', 'UUID');
+        $table->define_columns($columns);
+        $table->define_headers($headers);
+        $table->setup();
+        $count = 0;
+
+        foreach($ical->components['VEVENT'] as $event) {
+            if($count < 20) {
+                $mevent = new stdClass;
+                $mevent->name = $event->properties['SUMMARY'][0]->value;
+                $mevent->description = $event->properties['DESCRIPTION'][0]->value;
+                $mevent->timestart = strtotime($event->properties['DTSTART'][0]->value);
+                $mevent->duration = strtotime($event->properties['DTEND'][0]->value) - $mevent->timestart;
+                $mevent->uuid = $event->properties['UID'][0]->value;
+                $mevent->timemodified = time();
+
+                    $row = array();
+                $row[] = $mevent->name;
+                $row[] = $mevent->description;
+                $row[] = date('d/m/Y H:i', $mevent->timestart);
+                $row[] = date('H:i', $mevent->duration);
+                $row[] = $mevent->uuid;
+                $table->add_data($row);
+            }
+            $count++;
+
+        }
+        echo '<h2>Import Preview</h2>';
+        $table->print_html();
+        if($count > 20) {
+            echo ($count-20).' more...';
+        }
+        $toform = new stdClass();
+        $toform->courseid = $courseid;
+        $toform->calendar = $calendar;
+        $importform->set_data($toform);
+        $importform->display();
+
+    }
+    $mform->display();
+
+}
+
+print_footer();
+
+
+?>
+
diff --git a/calendar/view.php b/calendar/view.php
index b3245b1..eb43430 100644
--- a/calendar/view.php
+++ b/calendar/view.php
@@ -179,7 +179,8 @@
                  .'</a>';
         }
     }
-
+    print_single_button('import.php', array('courseid'=>$courseid), get_string('importcalendar', 'calendar'));
+    
     echo '</div>';
     echo '</div>';
     echo '</td>';
