function scheduler_print_schedulebox(scheduler_instance $scheduler, $studentid, $groupid = 0)
{
    global $output;
    $availableslots = $scheduler->get_slots_available_to_student($studentid);
    $startdatemem = '';
    $starttimemem = '';
    $availableslotsmenu = array();
    foreach ($availableslots as $slot) {
        $startdatecnv = $output->userdate($slot->starttime);
        $starttimecnv = $output->usertime($slot->starttime);
        $startdatestr = ($startdatemem != '' and $startdatemem == $startdatecnv) ? "-----------------" : $startdatecnv;
        $starttimestr = ($starttimemem != '' and $starttimemem == $starttimecnv) ? '' : $starttimecnv;
        $startdatemem = $startdatecnv;
        $starttimemem = $starttimecnv;
        $url = new moodle_url('/mod/scheduler/view.php', array('id' => $scheduler->cmid, 'slotid' => $slot->id, 'sesskey' => sesskey()));
        if ($groupid) {
            $url->param('what', 'schedulegroup');
            $url->param('subaction', 'dochooseslot');
            $url->param('groupid', $groupid);
        } else {
            $url->param('what', 'schedule');
            $url->param('subaction', 'dochooseslot');
            $url->param('studentid', $studentid);
        }
        $availableslotsmenu[$url->out()] = "{$startdatestr} {$starttimestr}";
    }
    $chooser = new url_select($availableslotsmenu);
    if ($availableslots) {
        echo $output->box_start();
        echo $output->heading(get_string('chooseexisting', 'scheduler'), 3);
        echo $output->render($chooser);
        echo $output->box_end();
    }
}
 /**
  * Create a scheduler instance from the database.
  */
 protected static function load_from_record($id, stdClass $coursemodule)
 {
     $scheduler = new scheduler_instance();
     $scheduler->load($id);
     $scheduler->cm = $coursemodule;
     $scheduler->groupmode = groups_get_activity_groupmode($coursemodule);
     return $scheduler;
 }
 /**
  * Get scheduler instance.
  *
  * NOTE: to be used from observers only.
  *
  * @throws \coding_exception
  * @return \scheduler_instance
  */
 public function get_scheduler()
 {
     if ($this->is_restored()) {
         throw new \coding_exception('get_scheduler() is intended for event observers only');
     }
     if (!isset($this->scheduler)) {
         debugging('scheduler property should be initialised in each event', DEBUG_DEVELOPER);
         global $CFG;
         require_once $CFG->dirroot . '/mod/scheduler/locallib.php';
         $this->scheduler = \scheduler_instance::load_by_coursemodule_id($this->contextinstanceid);
     }
     return $this->scheduler;
 }
/**
 * Update activity grades
 *
 * @param object $scheduler
 * @param int $userid specific user only, 0 means all
 */
function scheduler_update_grades($schedulerrecord, $userid = 0, $nullifnone = true)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/gradelib.php';
    $scheduler = scheduler_instance::load_by_id($schedulerrecord->id);
    if ($scheduler->scale == 0) {
        scheduler_grade_item_update($schedulerrecord);
    } else {
        if ($grades = $scheduler->get_user_grades($userid)) {
            foreach ($grades as $k => $v) {
                if ($v->rawgrade == -1) {
                    $grades[$k]->rawgrade = null;
                }
            }
            scheduler_grade_item_update($schedulerrecord, $grades);
        } else {
            scheduler_grade_item_update($schedulerrecord);
        }
    }
}
 public function teacherview_tabs(scheduler_instance $scheduler, moodle_url $baseurl, $selected, $inactive = null)
 {
     $statstab = $this->teacherview_tab($baseurl, 'statistics', 'viewstatistics', 'overall');
     $statstab->subtree = array($this->teacherview_tab($baseurl, 'overall', 'viewstatistics', 'overall'), $this->teacherview_tab($baseurl, 'studentbreakdown', 'viewstatistics', 'studentbreakdown'), $this->teacherview_tab($baseurl, 'staffbreakdown', 'viewstatistics', 'staffbreakdown', $scheduler->get_teacher_name()), $this->teacherview_tab($baseurl, 'lengthbreakdown', 'viewstatistics', 'lengthbreakdown'), $this->teacherview_tab($baseurl, 'groupbreakdown', 'viewstatistics', 'groupbreakdown'));
     $level1 = array($this->teacherview_tab($baseurl, 'myappointments', 'view', 'myappointments'), $this->teacherview_tab($baseurl, 'allappointments', 'view', 'allappointments'), $this->teacherview_tab($baseurl, 'datelist', 'datelist'), $statstab, $this->teacherview_tab($baseurl, 'export', 'export'));
     return $this->tabtree($level1, $selected, $inactive);
 }
 public function build(scheduler_instance $scheduler, array $fields, $mode, $userid, $groupid, $includeempty, $pageperteacher)
 {
     if ($groupid) {
         $this->studfilter = array_keys(groups_get_members($groupid, 'u.id'));
     }
     $this->canvas->set_title(format_string($scheduler->name));
     if ($userid) {
         $slots = $scheduler->get_slots_for_teacher($userid, $groupid);
         $this->build_page($scheduler, $fields, $slots, $mode, $includeempty);
     } else {
         if ($pageperteacher) {
             $teachers = $scheduler->get_teachers();
             foreach ($teachers as $teacher) {
                 $slots = $scheduler->get_slots_for_teacher($teacher->id, $groupid);
                 $title = fullname($teacher);
                 $this->canvas->start_page($title);
                 $this->build_page($scheduler, $fields, $slots, $mode, $includeempty);
             }
         } else {
             $slots = $scheduler->get_slots_for_group($groupid);
             $this->build_page($scheduler, $fields, $slots, $mode, $includeempty);
         }
     }
 }
/**
 * Construct an array with subtitution rules for mail templates, relating to
 * a single appointment. Any of the parameters can be null.
 * @param scheduler_instance $scheduler The scheduler instance
 * @param scheduler_slot $slot The slot data as an MVC object
 * @param user $attendant A {@link $USER} object describing the attendant (teacher)
 * @param user $attendee A {@link $USER} object describing the attendee (student)
 * @param object $course A course object relating to the ontext of the message
 * @param object $recipient A {@link $USER} object describing the recipient of the message (used for determining the message language)
 * @return array A hash with mail template substitutions
 */
function scheduler_get_mail_variables(scheduler_instance $scheduler, scheduler_slot $slot, $attendant, $attendee, $course, $recipient)
{
    global $CFG;
    $lang = scheduler_get_message_language($recipient, $course);
    // Force any string formatting to happen in the target language.
    $oldlang = force_current_language($lang);
    $tz = core_date::get_user_timezone($recipient);
    $vars = array();
    if ($scheduler) {
        $vars['MODULE'] = $scheduler->name;
        $vars['STAFFROLE'] = $scheduler->get_teacher_name();
        $vars['SCHEDULER_URL'] = $CFG->wwwroot . '/mod/scheduler/view.php?id=' . $scheduler->cmid;
    }
    if ($slot) {
        $vars['DATE'] = userdate($slot->starttime, get_string('strftimedate'), $tz);
        $vars['TIME'] = userdate($slot->starttime, get_string('strftimetime'), $tz);
        $vars['ENDTIME'] = userdate($slot->endtime, get_string('strftimetime'), $tz);
        $vars['LOCATION'] = format_string($slot->appointmentlocation);
    }
    if ($attendant) {
        $vars['ATTENDANT'] = fullname($attendant);
        $vars['ATTENDANT_URL'] = $CFG->wwwroot . '/user/view.php?id=' . $attendant->id . '&course=' . $scheduler->course;
    }
    if ($attendee) {
        $vars['ATTENDEE'] = fullname($attendee);
        $vars['ATTENDEE_URL'] = $CFG->wwwroot . '/user/view.php?id=' . $attendee->id . '&course=' . $scheduler->course;
    }
    // Reset language settings.
    force_current_language($oldlang);
    return $vars;
}
 /**
  * Creates a scheduler with certain settings,
  * having 10 appointments, from 1 hour in the future to 9 days, 1 hour in the future,
  * and booking a given student into these slots - either unattended bookings ($bookedslots)
  * or attended bookings ($attendedslots).
  *
  * The scheduler is created in a new course, into which the given student is enrolled.
  * Also, two other students (without any slot bookings) is created in the course.
  *
  */
 private function create_data_for_bookable_appointments($schedulermode, $maxbookings, $guardtime, $studentid, array $bookedslots, array $attendedslots)
 {
     global $DB;
     $course = $this->getDataGenerator()->create_course();
     $this->getDataGenerator()->enrol_user($studentid, $course->id);
     $options['slottimes'] = array();
     for ($c = 0; $c < 10; $c++) {
         $options['slottimes'][$c] = time() + $c * DAYSECS + HOURSECS;
         if (in_array($c, $bookedslots) || in_array($c, $attendedslots)) {
             $options['slotstudents'][$c] = $studentid;
         }
     }
     $schedrec = $this->getDataGenerator()->create_module('scheduler', array('course' => $course->id), $options);
     $scheduler = scheduler_instance::load_by_id($schedrec->id);
     $scheduler->schedulermode = $schedulermode;
     $scheduler->maxbookings = $maxbookings;
     $scheduler->guardtime = $guardtime;
     $scheduler->save();
     $slotrecs = $DB->get_records('scheduler_slots', array('schedulerid' => $scheduler->id), 'starttime ASC');
     $slotrecs = array_values($slotrecs);
     foreach ($attendedslots as $id) {
         $DB->set_field('scheduler_appointment', 'attended', 1, array('slotid' => $slotrecs[$id]->id));
     }
     for ($i = 0; $i < 2; $i++) {
         $dummystud = $this->create_student($course->id);
     }
     return $scheduler->id;
 }
 public function test_calendar_events()
 {
     global $DB;
     $scheduler = scheduler_instance::load_by_id($this->schedulerid);
     $slot = scheduler_slot::load_by_id($this->slotid, $scheduler);
     $slot->save();
     $oldstart = $slot->starttime;
     $this->assert_event_exists($this->teacherid, $slot->starttime, "Meeting with your Students");
     foreach ($this->students as $student) {
         $this->assert_event_exists($student, $slot->starttime, "Meeting with your Teacher");
     }
     $newstart = time() + 3 * DAYSECS;
     $slot->starttime = $newstart;
     $slot->save();
     foreach ($this->students as $student) {
         $this->assert_event_absent($student, $oldstart);
         $this->assert_event_exists($student, $newstart, "Meeting with your Teacher");
     }
     $this->assert_event_absent($this->teacherid, $oldstart);
     $this->assert_event_exists($this->teacherid, $newstart, "Meeting with your Students");
     // Delete one of the appointments.
     $app = $slot->get_appointment($this->appointmentids[0]);
     $slot->remove_appointment($app);
     $slot->save();
     $this->assert_event_absent($this->students[0], $newstart);
     $this->assert_event_exists($this->students[1], $newstart, "Meeting with your Teacher");
     $this->assert_event_exists($this->teacherid, $newstart, "Meeting with your Students");
     // Delete all appointments.
     $DB->delete_records('scheduler_appointment', array('slotid' => $this->slotid));
     $slot = scheduler_slot::load_by_id($this->slotid, $scheduler);
     $slot->save();
     foreach ($this->students as $student) {
         $this->assert_event_absent($student, $newstart);
     }
     $this->assert_event_absent($this->teacherid, $newstart);
 }
 /**
  * Test the "appointment" data object
  * (basic functionality, with minimal reference to slots)
  **/
 public function test_appointment()
 {
     global $DB;
     $instance = scheduler_instance::load_by_coursemodule_id($this->moduleid);
     $slot = array_values($instance->get_slots())[0];
     $factory = new scheduler_appointment_factory($slot);
     $user = $this->getdataGenerator()->create_user();
     $app0 = new stdClass();
     $app0->slotid = 1;
     $app0->studentid = $user->id;
     $app0->attended = 0;
     $app0->grade = 0;
     $app0->appointmentnote = 'testnote';
     $app0->timecreated = time();
     $app0->timemodified = time();
     $id1 = $DB->insert_record('scheduler_appointment', $app0);
     $appobj = $factory->create_from_id($id1);
     $this->assertEquals($user->id, $appobj->studentid);
     $this->assertEquals(fullname($user), fullname($appobj->get_student()));
     $this->assertFalse($appobj->is_attended());
     $this->assertEquals(0, $appobj->grade);
     $app0->attended = 1;
     $app0->grade = -7;
     $id2 = $DB->insert_record('scheduler_appointment', $app0);
     $appobj = $factory->create_from_id($id2);
     $this->assertEquals($user->id, $appobj->studentid);
     $this->assertEquals(fullname($user), fullname($appobj->get_student()));
     $this->assertTrue($appobj->is_attended());
     $this->assertEquals(-7, $appobj->grade);
 }
/**
 * Process ajax requests
 *
 * @package    mod
 * @subpackage scheduler
 * @copyright  2014 Henning Bostelmann and others (see README.txt)
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('AJAX_SCRIPT', true);
require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
require_once 'locallib.php';
$id = required_param('id', PARAM_INT);
$action = required_param('action', PARAM_ALPHA);
$cm = get_coursemodule_from_id('scheduler', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$scheduler = scheduler_instance::load_by_coursemodule_id($id);
require_login($course, true, $cm);
require_sesskey();
$return = 'OK';
switch ($action) {
    case 'saveseen':
        $appid = required_param('appointmentid', PARAM_INT);
        $slotid = $DB->get_field('scheduler_appointment', 'slotid', array('id' => $appid));
        $slot = $scheduler->get_slot($slotid);
        $app = $slot->get_appointment($appid);
        $newseen = required_param('seen', PARAM_BOOL);
        if ($USER->id != $slot->teacherid) {
            require_capability('mod/scheduler:manageallappointments', $scheduler->context);
        }
        $app->attended = $newseen;
        $slot->save();
require_once $CFG->dirroot . '/mod/scheduler/lib.php';
require_once $CFG->dirroot . '/mod/scheduler/locallib.php';
require_once $CFG->dirroot . '/mod/scheduler/renderable.php';
// Read common request parameters.
$id = optional_param('id', '', PARAM_INT);
// Course Module ID - if it's not specified, must specify 'a', see below.
$action = optional_param('what', 'view', PARAM_ALPHA);
$subaction = optional_param('subaction', '', PARAM_ALPHA);
$offset = optional_param('offset', -1, PARAM_INT);
if ($id) {
    $cm = get_coursemodule_from_id('scheduler', $id, 0, false, MUST_EXIST);
    $scheduler = scheduler_instance::load_by_coursemodule_id($id);
} else {
    $a = required_param('a', PARAM_INT);
    // Scheduler ID.
    $scheduler = scheduler_instance::load_by_id($a);
    $cm = $scheduler->get_cm();
}
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$defaultsubpage = groups_get_activity_groupmode($cm) ? 'myappointments' : 'allappointments';
$subpage = optional_param('subpage', $defaultsubpage, PARAM_ALPHA);
require_login($course->id, false, $cm);
$context = context_module::instance($cm->id);
// TODO require_capability('mod/scheduler:view', $context);
// Initialize $PAGE, compute blocks.
$PAGE->set_url('/mod/scheduler/view.php', array('id' => $cm->id));
$output = $PAGE->get_renderer('mod_scheduler');
// Print the page header.
$strschedulers = get_string('modulenameplural', 'scheduler');
$strscheduler = get_string('modulename', 'scheduler');
$strtime = get_string('time');