diff -Naurw moodle-clean/moodle/blocks/mrbs/block_mrbs.php moodle-dev/mrbs/block_mrbs.php --- moodle-clean/moodle/blocks/mrbs/block_mrbs.php 2008-11-26 11:58:22.000000000 +0000 +++ moodle-dev/mrbs/block_mrbs.php 2009-04-21 10:41:23.000000000 +0100 @@ -6,6 +6,7 @@ $this->title = get_string('blockname','block_mrbs'); $this->content_type = BLOCK_TYPE_TEXT; $this->version = 2006110500; + $this->cron=300; } function has_config() {return true;} @@ -44,6 +45,16 @@ return $this->content; } } + + function cron(){ + global $CFG; + include($CFG->dirroot.'/blocks/mrbs/cron.php'); + + //doesn't seem to update this automatically? + $mrbsblock=get_record('block','name','mrbs'); + $mrbsblock->lastcron=mktime(); + update_record('block',$mrbsblock); + } } ?> diff -Naurw moodle-clean/moodle/blocks/mrbs/cron.php moodle-dev/mrbs/cron.php --- moodle-clean/moodle/blocks/mrbs/cron.php 1970-01-01 01:00:00.000000000 +0100 +++ moodle-dev/mrbs/cron.php 2009-03-12 15:12:51.000000000 +0000 @@ -0,0 +1,204 @@ +cronfile)){ + if($mrbs_sessions = fopen($cfg_mrbs->cronfile,'r')){ + $output.= 'File found, starting processing...'."\n"; + + + //move the processed file to prevent wasted time re-processing + $date=date('Ymd'); + if(rename($cfg_mrbs->cronfile,$cfg_mrbs->cronfile.'.'.$date)){ + $output.=$cfg_mrbs->cronfile.' moved to '.$cfg_mrbs->cronfile.'.'.$date."\n"; + } + + //wipe old mrbs bookings + delete_records_select('mrbs_entry', 'type=\'C\' and start_time > unix_timestamp()'); + + //import timetable into mrbs + $now = time(); + while ($array = fgetcsv($mrbs_sessions)){ + list($year, $month, $day) = split('[\ ]', $array[2]); + $date = mktime(12,00,00,$month,$day,$year); + $room = room_id_lookup($array[4]); + $weeks =str_split($array[3]); + foreach($weeks as $week){ + if (($week==1) and ($date > $now)){ + $start_time = time_to_period($date,$array[0],TRUE); + $end_time = time_to_period($date,$array[1],FALSE); + + //only timetable class if it isn't already timetabled elsewhere (class been moved) + if(!is_timetabled($array[6],$start_time)){ + + $entry->start_time=$start_time; + $entry->end_time=$end_time; + $entry->room_id=$room; + $entry->timestamp=$now; + $entry->create_by=$array[5]; + $entry->name=$array[6]; + $entry->type='C'; + $entry->description=$array[7]; + + $normal_times=array('0830','1000','1020','1150','1250','1420','1425','1555'); + if(!in_array($array[0],$normal_times) or !in_array($array[1],$normal_times)){$entry->description.="\n\n".'Partial period booking:'.$array[0].'-'.$array[1];} + + $newentryid=insert_record('mrbs_entry',$entry); + $newclass=get_record('mrbs_entry','id',$newentryid); + + //If there is another un-timetabled booking there, send emails. It is assumed that simultanious timetabled classes are intentional + + $sql = "SELECT * + FROM {$CFG->prefix}mrbs_entry + WHERE + (({$CFG->prefix}mrbs_entry.start_time<$newclass->start_time AND {$CFG->prefix}mrbs_entry.end_time>$newclass->start_time) + OR ({$CFG->prefix}mrbs_entry.start_time<$newclass->end_time AND {$CFG->prefix}mrbs_entry.end_time>$newclass->end_time) + OR ({$CFG->prefix}mrbs_entry.start_time>=$newclass->start_time AND {$CFG->prefix}mrbs_entry.end_time<=$newclass->end_time )) + AND mdl_mrbs_entry.room_id = $room AND {$CFG->prefix}mrbs_entry.id<>$newentryid";//limit to 1 to keep this simpler- if there is a 3-way clash it will be noticed by one of the 2 teachers notified + + + + if (($existingclass=get_record_sql($sql,true)) and ($existingclass->type!='C')){ + $hr_start_time=date("j F, Y",$start_time) . " at " . period_to_time($start_time); + $output.= "Clash: $existingclass->description($existingclass->id) $newclass->description($newclass->id) $hr_start_time in Room: $array[4]"; + + $existingteacher=get_record('user','username',$existingclass->create_by); + $newteacher=get_record('user','username',$newclass->create_by); + + $body = "Due to a recent timetable change there is a clash involving one of your classes/bookings:\n On $hr_start_time, $existingclass->description and $newclass->description are both booked into room $array[4]\nPlease discuss this between yourselves before the lesson to avoid unnecessary disruption.\n\n\nThis message has been sent by the online room booking system, if you think you have recieved this in error please contact $cfg_mrbs->admin $cfg_mrbs->admin_email"; + + if(email_to_user($existingteacher,$newteacher,'Room Clash Alert',$body)){$output.=', email sent to '.$existingteacher->firstname.' '.$existingteacher->lastname.'<'.$existingteacher->email.'>';}else{$output.='COULDN\'T SEND EMAIL TO TEACHER OF '.$existingclass->description.'('.$existingclass->id.')';} + if(email_to_user($newteacher,$existingteacher,'Room Clash Alert',$body)){$output.=', email sent to '.$newteacher->firstname.' '.$newteacher->lastname.'<'.$newteacher->email.'>';} else{$output.='COULDN\'T SEND EMAIL TO TEACHER OF '.$newclass->description.'('.$newclass->id.')';} + $output.="\n"; + } + } + } + $date += 604800; + + //checks for being an hour out due to BST/GMT change and corrects + if(date('G',$date)==11){$date = $date + 3600;} + if(date('G',$date)==13){$date = $date - 3600;} + + } + } + + + $script_time_taken = time() - $script_start_time; + $output.='Processing complete, time taken:'.$script_time_taken.' seconds'; + + //will only show up if being run via apache + echo $output; + + //email output to admin- dont + //email_to_user($mrbsadmin,$mrbsadmin,'Room Clash Alert',$output); + mail($cfg_mrbs->admin_email,'MRBS import log',$output,'From: '.$cfg_mrbs->admin_email); + } +} +//==========================================FUNCTIONS============================================================== + +//looks up the room id from the name +function room_id_lookup($name){ + if(!$room=get_record('mrbs_room','room_name',$name)){ + $error = "ERROR: failed to return id from database (room $name probably doesn't exist)"; + $output.= $error . "\n"; + return 'error'; + }else{ + return $room->id; + } +} + +//checks if the class is timetabled into a lesson at the time specified +function is_timetabled($class,$time){ + if(!get_record('mrbs_entry','name',$class,'start_time',$time)){ + return FALSE; + }else{ + return TRUE; + } +} + +//changes times to periods (although that is done via minutes past midnight) +function time_to_period($date,$time,$beg){ + switch($time){ + case '0830': + return $date; + break; + case '1000': + return $date + 60; + break; + case '1020': + return $date + 120; + break; + case '1150': + return $date + 180; + break; + case '1250': + return $date + 240; + break; + case '1420': + return $date + 300; + break; + case '1425': + return $date + 300; + break; + case '1555': + return $date + 360; + break; + case '1700': + return $date + 420; + break; + default: + if ($beg){ + if ($time < 1000){ return $date;} + elseif ($time < 1020){ return $date + 60;} + elseif ($time < 1150){ return $date + 120;} + elseif ($time < 1250){ return $date + 180;} + elseif ($time < 1425){ return $date + 240;} + else{ return $date + 300;} + + }else{ + if ($time < 1000){ return $date +60;} + elseif ($time < 1020){ return $date + 120;} + elseif ($time < 1150){ return $date + 180;} + elseif ($time < 1250){ return $date + 240;} + elseif ($time < 1425){ return $date + 300;} + else{ return $date + 360;} + } + } +} +//changes periods back to time(although that is done via minutes past midnight) +function period_to_time($time){ + switch(date('i',$time)){ + case 00: + return '0830'; + break; + case 01: + return '1000'; + break; + case 02: + return '1020'; + break; + case 03: + return '1150'; + break; + case 04: + return '1250'; + break; + case 05: + return '1420'; + break; + case 06: + return '1425'; + break; + case 07: + return '1555'; + break; + case 08: + return '1700'; + break; + } +} + +?> \ No newline at end of file diff -Naurw moodle-clean/moodle/blocks/mrbs/lang/en_utf8/block_mrbs.php moodle-dev/mrbs/lang/en_utf8/block_mrbs.php --- moodle-clean/moodle/blocks/mrbs/lang/en_utf8/block_mrbs.php 2008-11-26 11:58:22.000000000 +0000 +++ moodle-dev/mrbs/lang/en_utf8/block_mrbs.php 2009-04-21 11:05:19.000000000 +0100 @@ -102,6 +102,8 @@ $string['config_weekstarts2'] = 'Select the start of week.';$string['confirmdel'] = 'Are you sure\\nyou want to\\ndelete this entry?\\n\\n'; $string['conflict'] = 'The new booking will conflict with the following entry(s)'; $string['createdby'] = 'Created By'; +$string['cronfile'] = 'Location of session import file'; +$string['cronfiledesc'] = 'If you wish to use the automatic session import feature, enter the location of the file here. The file must be able to be moved by the webserver user'; $string['ctrl_click_type'] = 'Use Control-Click to select more than one type'; $string['ctrl_click'] = 'Use Control-Click to select more than one room'; $string['database'] = 'Database'; diff -Naurw moodle-clean/moodle/blocks/mrbs/settings.php moodle-dev/mrbs/settings.php --- moodle-clean/moodle/blocks/mrbs/settings.php 2008-11-26 11:58:25.000000000 +0000 +++ moodle-dev/mrbs/settings.php 2009-04-21 10:42:56.000000000 +0100 @@ -209,4 +209,7 @@ $settings->add(new admin_setting_configtext('mail_cc', get_string('config_mail_cc', 'block_mrbs'),get_string('config_mail_cc2', 'block_mrbs'), NULL, PARAM_TEXT)); $settings->settings->mail_cc->plugin='block/mrbs'; +$settings->add(new admin_setting_configtext('cronfile', get_string('cronfile', 'block_mrbs'),get_string('cronfiledesc', 'block_mrbs'), NULL, PARAM_TEXT)); +$settings->settings->cronfile->plugin='block/mrbs'; + ?> \ No newline at end of file