entry.name as entryname,
         entry.create_by,
         room.room_name,
         entry.start_time,
       FROM ' . $CFG->prefix . 'mrbs_entry as entry
         join ' . $CFG->prefix . 'mrbs_room as room on entry.room_id = room.id
      WHERE room.id = ' . $room_id . '
      AND ((entry.start_time>=' . $starttime . ' AND entry.end_time<' . $endtime . ')
      OR (entry.start_time<' . $starttime . ' AND entry.end_time>' . $starttime . ')
      OR (entry.start_time<' . $endtime . ' AND entry.end_time>=' . $endtime . '))';
         if ($clashingbookings = get_records_sql($sql)) {
             foreach ($clashingbookings as $clashingbooking) {
                 $oldbookinguser = get_record('user', 'username', $clashingbooking->create_by);
                 $langvars->user = $USER->firstname . ' ' . $USER->lastname;
                 $langvars->room = $clashingbooking->room_name;
                 $langvars->time = to_hr_time($clashingbooking->start_time);
                 $langvars->date = userdate($clashingbooking->start_time, '%A %d/%m/%Y');
                 $langvars->oldbooking = $clashingbooking->entryname;
                 $langvars->newbooking = $name;
                 $langvars->admin = $mrbs_admin . ' (' . $mrbs_admin_email . ')';
                 // Send emails to user with existing booking
                 if (!email_to_user($oldbookinguser, $USER, get_string('doublebookesubject', 'block_mrbs'), get_string('doublebookebody', 'block_mrbs', $langvars))) {
                     email_to_user(get_record('user', 'email', $mrbs_admin_email), $USER, get_string('doublebookefailsubject', 'block_mrbs'), get_string('doublebookefailbody', 'block_mrbs', $oldbookinguser->username) . get_string('doublebookebody', 'block_mrbs', $langvars));
                 }
             }
         }
     } else {
         // If the user hasn't confirmed they want to double book, check the room is free.
         $err .= mrbsCheckFree($room_id, $starttime, $endtime - 1, $ignore_id, 0);
     }
 }
Example #2
0
function mrbsForceMove($room_id, $starttime, $endtime, $name, $id = null)
{
    global $CFG;
    global $USER;
    global $enable_periods;
    global $periods;
    $cfg_mrbs = get_config('block/mrbs');
    $output = '';
    # Select any meetings which overlap ($starttime,$endtime) for this room:
    $sql = 'SELECT ' . $CFG->prefix . 'mrbs_entry.id AS entryid, ' . $CFG->prefix . 'mrbs_entry.name as entryname, ' . $CFG->prefix . 'mrbs_entry.description, type,' . $CFG->prefix . 'mrbs_entry.create_by, ' . $CFG->prefix . 'mrbs_room.room_name, ' . $CFG->prefix . 'mrbs_entry.name, ' . $CFG->prefix . 'mrbs_entry.start_time, ' . $CFG->prefix . 'mrbs_entry.end_time, ' . $CFG->prefix . 'mrbs_room.description, area_id
              FROM ' . $CFG->prefix . 'mrbs_entry join ' . $CFG->prefix . 'mrbs_room on ' . $CFG->prefix . 'mrbs_entry.room_id = ' . $CFG->prefix . 'mrbs_room.id
             WHERE ((' . $CFG->prefix . 'mrbs_entry.start_time>=' . $starttime . ' AND ' . $CFG->prefix . 'mrbs_entry.end_time<' . $endtime . ')
             OR (' . $CFG->prefix . 'mrbs_entry.start_time<' . $starttime . ' AND ' . $CFG->prefix . 'mrbs_entry.end_time>' . $starttime . ')
             OR (' . $CFG->prefix . 'mrbs_entry.start_time<' . $endtime . ' AND ' . $CFG->prefix . 'mrbs_entry.end_time>=' . $endtime . '))';
    //this is so that if a booking is being edited (normally extended) and forcing the booking a confusing email doesn't get sent to the force booker
    if (!empty($id)) {
        $sql .= ' AND ' . $CFG->prefix . 'mrbs_entry.id!=' . $id;
    }
    $sql .= ' AND room_id = ' . $room_id . ' ORDER BY start_time';
    if ($oldbookings = get_records_sql($sql)) {
        foreach ($oldbookings as $oldbooking) {
            $today = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
            $hrstarttime = to_hr_time($oldbooking->start_time - $today);
            //Work out how many students so they don't get put in a tiny classroom
            $sizequery = 'select count(*) as count
                        from ' . $CFG->prefix . 'context
                            join ' . $CFG->prefix . 'role_assignments on ' . $CFG->prefix . 'role_assignments.contextid = ' . $CFG->prefix . 'context.id and ' . $CFG->prefix . 'role_assignments.roleid=5
                            join ' . $CFG->prefix . 'course on ' . $CFG->prefix . 'context.contextlevel = 50 and ' . $CFG->prefix . 'context.instanceid = ' . $CFG->prefix . 'course.id
                        where ' . $CFG->prefix . 'course.shortname  =\'' . clean_param($oldbooking->entryname, PARAM_TEXT) . '\'';
            if ($result = get_record_sql($sizequery)) {
                $class_size = $result->count;
            } else {
                $class_size = 0;
            }
            $findroomquery = 'SELECT DISTINCT ' . $CFG->prefix . 'mrbs_room.id, ' . $CFG->prefix . 'mrbs_room.room_name, ' . $CFG->prefix . 'mrbs_area.area_name,
                                 CONCAT(IF(' . $CFG->prefix . 'mrbs_room.description = \'' . $oldbooking->description . '\',1,0),IF(' . $CFG->prefix . 'mrbs_area.id= \'' . $oldbooking->area_id . '\',1,0)) as sort
                             FROM ' . $CFG->prefix . 'mrbs_room JOIN ' . $CFG->prefix . 'mrbs_area on ' . $CFG->prefix . 'mrbs_room.area_id=' . $CFG->prefix . 'mrbs_area.id join ' . $CFG->prefix . 'mrbs_entry on ' . $CFG->prefix . 'mrbs_room.id=' . $CFG->prefix . 'mrbs_entry.room_id
                             WHERE ( SELECT COUNT(*) FROM ' . $CFG->prefix . 'mrbs_entry
                                 WHERE ((' . $CFG->prefix . 'mrbs_entry.start_time>=' . $oldbooking->start_time . ' AND ' . $CFG->prefix . 'mrbs_entry.end_time<' . $oldbooking->end_time . ')
                                 OR (' . $CFG->prefix . 'mrbs_entry.start_time<' . $oldbooking->start_time . ' AND ' . $CFG->prefix . 'mrbs_entry.end_time>' . $oldbooking->start_time . ')
                                 OR (' . $CFG->prefix . 'mrbs_entry.start_time<' . $oldbooking->end_time . ' AND ' . $CFG->prefix . 'mrbs_entry.end_time>=' . $oldbooking->end_time . '))
                                 AND mdl_mrbs_entry.room_id = mdl_mrbs_room.id ) < 1  
                             AND ' . $CFG->prefix . 'mrbs_room.description like \'%teaching%\'
                             AND ' . $CFG->prefix . 'mrbs_room.capacity >=' . $class_size . '
                             AND (' . $CFG->prefix . 'mrbs_room.description not like \'%special%\' OR ' . $CFG->prefix . 'mrbs_area.id=\'' . $oldbooking->area_id . '\')
                             ORDER BY sort DESC';
            //dump them in first room on the list
            $findroomresult = get_record_sql($findroomquery, true);
            $subject = get_string('bookingmoved', 'block_mrbs');
            $langvars = new object();
            $langvars->name = $oldbooking->entryname;
            $langvars->id = $oldbooking->entryid;
            $langvars->oldroom = $oldbooking->room_name;
            $langvars->newroom = $findroomresult->room_name;
            $langvars->area = $findroomresult->area_name;
            $langvars->date = date('d/m/Y', $oldbooking->start_time);
            $langvars->starttime = $hrstarttime;
            $langvars->newbookingname = $name;
            $booking = new object();
            $booking->id = $oldbooking->entryid;
            $booking->room_id = $findroomresult->id;
            //If it is an imported booking, mark it as edited
            if ($oldbooking->type == 'K') {
                $booking->type = 'L';
            } else {
                $booking->type = $oldbooking->type;
            }
            if ($findroomresult and update_record('mrbs_entry', $booking) and $oldbookingowner = get_record('user', 'username', $oldbooking->create_by)) {
                $message = get_string('bookingmovedmessage', 'block_mrbs', $langvars);
                $output .= '<br>' . get_string('bookingmovedshort', 'block_mrbs', $langvars);
                email_to_user($oldbookingowner, $USER, $subject, $message);
            } else {
                $output .= '<br>' . get_string('bookingmoveerrorshort', 'block_mrbs', $langvars);
                mail($cfg_mrbs->admin_email, get_string('bookingmoveerror', 'block_mrbs'), get_string('bookingmoveerrormessage', 'block_mrbs', $langvars));
            }
        }
    }
    return $output;
}
Example #3
0
 if (!is_timetabled($csvrow->name, $start_time)) {
     ////only timetable class if it isn't already timetabled elsewhere (class been moved)
     $entry->start_time = $start_time;
     $entry->end_time = $end_time;
     $entry->room_id = $room;
     $entry->timestamp = $now;
     $entry->create_by = $csvrow->username;
     $entry->name = $csvrow->name;
     $entry->type = 'K';
     $entry->description = $csvrow->description;
     $newentryid = insert_record('mrbs_entry', $entry);
     //If there is another non-imported booking there, send emails. It is assumed that simultanious imported classes are intentional
     $sql = "SELECT *\n                                FROM {$CFG->prefix}mrbs_entry\n                                WHERE\n                                    (({$CFG->prefix}mrbs_entry.start_time<{$start_time} AND {$CFG->prefix}mrbs_entry.end_time>{$start_time})\n                                  OR ({$CFG->prefix}mrbs_entry.start_time<{$end_time} AND {$CFG->prefix}mrbs_entry.end_time>{$end_time})\n                                  OR ({$CFG->prefix}mrbs_entry.start_time>={$start_time} AND {$CFG->prefix}mrbs_entry.end_time<={$end_time} ))\n                                AND mdl_mrbs_entry.room_id = {$room} AND type<>'K'";
     //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)) {
         $hr_start_time = date("j F, Y", $start_time) . ", " . to_hr_time($start_time);
         $a = new object();
         $a->oldbooking = $existingclass->description . '(' . $existingclass->id . ')';
         $a->newbooking = $csvrow->description . '(' . $newentryid . ')';
         $a->time = $hr_start_time;
         $a->room = $csvrow->room_name;
         $a->admin = $cfg_mrbs->admin . ' (' . $cfg_mrbs->admin_email . ')';
         $output .= get_string('clash', 'block_mrbs', $a);
         $existingteacher = get_record('user', 'username', $existingclass->create_by);
         $newteacher = get_record('user', 'username', $csvrow->username);
         $body = get_string('clashemailbody', 'block_mrbs', $a);
         if (email_to_user($existingteacher, $newteacher, get_string('clashemailsub', 'block_mrbs', $a), $body)) {
             $output .= ', ' . get_string('clashemailsent', 'block_mrbs') . ' ' . $existingteacher->firstname . ' ' . $existingteacher->lastname . '<' . $existingteacher->email . '>';
         } else {
             $output .= get_string('clashemailnotsent', 'block_mrbs') . $existingclass->description . '(' . $existingclass->id . ')';
         }