Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appointmentforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public function save_appointment_data(stdClass $formdata, scheduler_appointment
$scheduler = $appointment->get_scheduler();
$cid = $scheduler->context->id;
$appointment->set_data($formdata);
$appointment->roleid = ($scheduler->uses_roles() && isset($formdata->roleid) &&
!empty($formdata->roleid)) ? intval($formdata->roleid) : 0;
$appointment->attended = isset($formdata->attended);
if ($scheduler->uses_appointmentnotes() && isset($formdata->appointmentnote_editor)) {
$editor = $formdata->appointmentnote_editor;
Expand Down
2 changes: 1 addition & 1 deletion backup/moodle2/backup_scheduler_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function define_structure() {
$appointments = new backup_nested_element('appointments');

$appointment = new backup_nested_element('appointment', array('id'), array(
'studentid', 'attended', 'grade',
'studentid', 'roleid', 'attended', 'grade',
'appointmentnote', 'appointmentnoteformat', 'teachernote', 'teachernoteformat',
'studentnote', 'studentnoteformat', 'timecreated', 'timemodified'));

Expand Down
14 changes: 14 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<FIELD NAME="starttime" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="duration" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="teacherid" TYPE="int" LENGTH="11" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="ignoreconflicts" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="notignoreconflictsstudents" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="appointmentlocation" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="reuse" TYPE="int" LENGTH="5" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="legacy"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
Expand All @@ -64,6 +66,7 @@
<FIELD NAME="id" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="slotid" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="studentid" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="roleid" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="attended" TYPE="int" LENGTH="4" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="grade" TYPE="int" LENGTH="4" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="appointmentnote" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Notes visible to teacher and student"/>
Expand All @@ -83,5 +86,16 @@
<INDEX NAME="studentid" UNIQUE="false" FIELDS="studentid" COMMENT="By student id"/>
</INDEXES>
</TABLE>
<TABLE NAME="scheduler_roles" COMMENT="Scheduler roles">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="schedulerid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Scheduler ID"/>
<FIELD NAME="rname" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false" COMMENT="Role name"/>
<FIELD NAME="rlimit" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Role limit"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for scheduler_roles"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
24 changes: 24 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ function xmldb_scheduler_upgrade($oldversion=0) {

$result = true;

if ($oldversion < 2017051400) {
$table = new xmldb_table('scheduler');
$field = new xmldb_field('rolessupport', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'schedulermode');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$table2 = new xmldb_table('scheduler_slots');
$field2 = new xmldb_field('ignoreconflicts', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'teacherid');
if (!$dbman->field_exists($table2, $field2)) {
$dbman->add_field($table2, $field2);
}
$field3 = new xmldb_field('notignoreconflictsstudents', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'ignoreconflicts');
if (!$dbman->field_exists($table2, $field3)) {
$dbman->add_field($table2, $field3);
}
$table3 = new xmldb_table('scheduler_appointment');
$field3 = new xmldb_field('roleid', XMLDB_TYPE_INTEGER, '11', null, null, null, '0', 'studentid');
if (!$dbman->field_exists($table3, $field3)) {
$dbman->add_field($table3, $field3);
}

upgrade_mod_savepoint(true, 2017051400, 'scheduler');
}

/* ******************* 2.0 upgrade line ********************** */

if ($oldversion < 2011081302) {
Expand Down
5 changes: 5 additions & 0 deletions lang/en/scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
$string['bookinginstructions_help'] = 'This text will be displayed to students before they make a booking. It can, for example, instruct students how to fill out the optional message field or which files to upload.';
$string['bookslot'] = 'Book slot';
$string['bookaslot'] = 'Book a slot';
$string['slotroleslimit'] = 'Limit for this role is exhausted';
$string['bookingdetails'] = 'Booking details';
$string['bookwithteacher'] = 'Teacher';
$string['break'] = 'Break between slots';
Expand Down Expand Up @@ -247,6 +248,8 @@
$string['ignoreconflicts'] = 'Ignore scheduling conflicts';
$string['ignoreconflicts_help'] = 'If this box is ticked, then the slot will be moved to the requested date and time, even if other slots exist at the same time. This may lead to overlapping appointments for some teachers or students, and should therefore be used with care.';
$string['ignoreconflicts_link'] = 'mod/scheduler/conflict';
$string['notignoreconflictsstudents'] = 'Not ignore scheduling conflicts for students';
$string['notignoreconflictsstudents_help'] = 'At the moment, the tracking function works only for teachers, students conflicts are ignored, this option solves this problem';
$string['includeemptyslots'] = 'Include empty slots';
$string['includeslotsfor'] = 'Include slots for';
$string['incourse'] = ' in course ';
Expand Down Expand Up @@ -435,6 +438,8 @@
$string['usenotesstudent'] = 'Appointment note, visible to teacher and student';
$string['usenotesteacher'] = 'Confidential note, visible to teachers only';
$string['usenotesboth'] = 'Both types of notes';
$string['rolessupport'] = 'Support roles';
$string['limit'] = 'Limit';
$string['usestudentnotes'] = 'Let students enter a message';
$string['usestudentnotes_help'] = 'If enabled, the booking screen will contain a text box in which students can enter a message. Use the "booking instructions" above to instruct students what information they should supply.';
$string['viewbooking'] = 'See details';
Expand Down
84 changes: 84 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,87 @@ function scheduler_pluginfile($course, $cm, $context, $filearea, $args, $forcedo
send_stored_file($file, 0, 0, $forcedownload, $options);
}

function get_scheduler_user_roles($userid, $schedulerid, $slotid) {
global $DB;

$ret = array();
$sql = 'SELECT sr.id,sr.rname '
. 'FROM {scheduler_appointment} sa LEFT JOIN {scheduler_roles} sr '
. 'ON sa.roleid = sr.id LEFT JOIN {scheduler_slots} ss '
. 'ON sa.slotid = ss.id LEFT JOIN {scheduler} s '
. 'ON ss.schedulerid = s.id '
. 'WHERE sa.studentid = ? AND s.id = ? AND sa.slotid = ?';
if ($roles = $DB->get_records_sql($sql, array($userid, $schedulerid, $slotid))) {
foreach ($roles as $roleid => $role) {
if (!empty($roleid)) {
$ret[$role->id] = $role->rname;
}
}
}

return $ret;
}

/**
* Get roles for course module
*
* @global stdClass $DB
* @param int $cmid Course module id
* @param boolean $simple
* @param boolean $checklimits
* @param boolean $userid
* @param string $key
* @return array
*/
function get_scheduler_roles($cmid, $simple = false, $checklimits = false, $userid = false, $key = 'rname') {
global $DB;

if ($cmid > 0) {
$sql = 'SELECT '
. 'sr.id, sr.* '
. 'FROM {scheduler_roles} sr '
. 'LEFT JOIN {scheduler} s '
. 'ON sr.schedulerid = s.id '
. 'LEFT JOIN {course_modules} cm '
. 'ON s.id = cm.instance '
. 'LEFT JOIN {modules} m '
. 'ON cm.module = m.id WHERE cm.id = ? AND m.name = ?';
$conditions = array($cmid, 'scheduler');
$array = array_values($DB->get_records_sql($sql, $conditions));
if ($simple) {
$ret = array();
foreach ($array as $item) {
$ret[$item->id] = $item->$key;
}
if ($checklimits) {
$tmp = $ret;
foreach (array_keys($tmp) as $roleid) {
if (!check_slot_role_limit($roleid, $userid)) {
unset($ret[$roleid]);
}
}
}
return $ret;
}
return $array;
}

return array();
}

function check_slot_role_limit($roleid, $studentid = false) {
global $DB;

if ($role = $DB->get_record('scheduler_roles', array('id' => $roleid), '*', MUST_EXIST)) {
if ($role->rlimit > 0 && $studentid) {
$appointments = $DB->get_records('scheduler_appointment',
array('roleid' => $roleid, 'studentid' => $studentid));
if (count($appointments) < $role->rlimit) {
return true;
}
} else {
return true;
}
}
return false;
}
92 changes: 92 additions & 0 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ class mod_scheduler_mod_form extends moodleform_mod {

protected $editoroptions;

protected $_rolescount = 5;

function definition() {

global $CFG, $COURSE, $OUTPUT;
$mform =& $this->_form;

$cmid = optional_param('update', 0, PARAM_INT);
$roles = get_scheduler_roles($cmid);
// General introduction.
$mform->addElement('header', 'general', get_string('general', 'form'));

Expand Down Expand Up @@ -106,6 +109,30 @@ function definition() {
$mform->addElement('select', 'usenotes', get_string('usenotes', 'scheduler'), $noteoptions);
$mform->setDefault('usenotes', '1');

$mform->addElement('header', 'roleshdr', get_string('roles'));

$mform->addElement('checkbox', 'rolessupport', get_string('rolessupport', 'scheduler'));
$mform->setDefault('rolessupport', false);
//$mform->addHelpButton('rolessupport', 'ignoreconflicts', 'scheduler');

for($i=1;$i<=$this->_rolescount;$i++) {
$mform->addElement('text', 'rolename['.($i-1).']', get_string('role').' '.$i.' '. get_string('name'), array('size' => '48'));
$mform->setType('rolename['.($i-1).']', PARAM_TEXT);
$mform->addRule('rolename['.($i-1).']', get_string('error'), 'maxlength', 255);
$mform->disabledIf('rolename['.($i-1).']', 'rolessupport');
if (isset($roles[$i-1]->rname) && !empty($roles[$i-1]->rname)) {
$mform->setDefault('rolename['.($i-1).']', $roles[$i-1]->rname);
}

$mform->addElement('text', 'rolelimit['.($i-1).']', get_string('role').' '.$i.' '.get_string('limit', 'scheduler'), array('size' => '20'));
$mform->setType('rolelimit['.($i-1).']', PARAM_INT);
$mform->addRule('rolelimit['.($i-1).']', get_string('error'), 'maxlength', 4);
$mform->disabledIf('rolelimit['.($i-1).']', 'rolessupport');
if (isset($roles[$i-1]->rlimit) && !empty($roles[$i-1]->rlimit)) {
$mform->setDefault('rolelimit['.($i-1).']', $roles[$i-1]->rlimit);
}
}

// Grade settings.
$this->standard_grading_coursemodule_elements();

Expand Down Expand Up @@ -206,6 +233,71 @@ public function save_mod_data(stdClass $data, context_module $context) {
$this->editoroptions, $editor['text']);
$data->bookinginstructionsformat = $editor['format'];
$DB->update_record('scheduler', $data);
$schedulerid = 0;
if (isset($data->instance) && !empty($data->instance)) {
$schedulerid = intval($data->instance);
} else {
if (isset($data->id) && !empty($data->id)) {
$schedulerid = intval($data->id);
}
}
if ($schedulerid) {
$cmid = optional_param('update', 0, PARAM_INT);
$roles = get_scheduler_roles($cmid);
if (isset($data->rolessupport) && !empty($data->rolessupport)) {
$rolesnames = array();
if (isset($data->rolename) && !empty($data->rolename) && is_array($data->rolename)) {
foreach ($data->rolename as $i => $rolename) {
$rolename = trim(clean_param($rolename, PARAM_CLEAN));
if (!empty($rolename)) {
$rolesnames[$i] = $rolename;
} else {
if (isset($roles[$i]) && !empty($roles[$i])) {
if ($DB->get_record('scheduler_roles', array('id' => $roles[$i]->id))) {
$DB->delete_records('scheduler_roles', array('id' => $roles[$i]->id));
}
}
}
}
}
$roleslimits = array();
if (isset($data->rolelimit) && !empty($data->rolelimit) && is_array($data->rolelimit)) {
foreach ($data->rolelimit as $i => $rolelimit) {
$rolelimit = intval($rolelimit);
if ($rolelimit > 0) {
$roleslimits[$i] = $rolelimit;
} else {
if (isset($roles[$i]) && !empty($roles[$i])) {
if ($DB->get_record('scheduler_roles', array('id' => $roles[$i]->id))) {
$DB->delete_records('scheduler_roles', array('id' => $roles[$i]->id));
}
}
}
}
}
if ($rolesnames) {
$args = new stdClass();
$args->schedulerid = $schedulerid;
foreach ($rolesnames as $i => $rolename) {
if (isset($roleslimits[$i]) && !empty($roleslimits[$i])) {
$args->rname = $rolename;
$args->rlimit = $roleslimits[$i];
if ($row = $DB->get_record('scheduler_roles', array('schedulerid' => $schedulerid, 'rname' => $rolename))) {
$args->id = $row->id;
$DB->update_record('scheduler_roles', $args);
} else {
$DB->insert_record('scheduler_roles', $args);
}
}
}
}
} else {
$DB->set_field('scheduler', 'rolessupport', 0);
if ($DB->get_records('scheduler_roles', array('schedulerid' => $schedulerid))) {
$DB->delete_records('scheduler_roles', array('schedulerid' => $schedulerid));
}
}
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion model/scheduler_appointment.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,36 @@ public function __construct(scheduler_slot $slot) {
$this->data = new stdClass();
$this->set_parent($slot);
$this->data->slotid = $slot->get_id();
$this->data->roleid = 0;
$this->data->attended = 0;
$this->data->appointmentnoteformat = FORMAT_HTML;
$this->data->teachernoteformat = FORMAT_HTML;
}

public function save() {
global $DB;

$this->data->slotid = $this->get_parent()->get_id();
parent::save();
if (isset($_REQUEST['roleid']) && !empty($_REQUEST['roleid'])) {
if (!is_array($_REQUEST['roleid'])) {
$this->data->roleid = intval($_REQUEST['roleid']);
} else {
if ($apps = array_values($DB->get_records('scheduler_appointment',
array('slotid' => intval($_REQUEST['slotid']))))) {
foreach ($apps as $i => $app) {
if ($app->studentid == $this->data->studentid) {
$this->data->roleid = intval($_REQUEST['roleid'][$i]);
break;
}
}
}
}
}
$uses_roles = $this->get_scheduler()->uses_roles();
if (!$uses_roles || ($this->data->roleid && $uses_roles &&
check_slot_role_limit($this->data->roleid, $this->data->studentid))) {
parent::save();
}
$scheddata = $this->get_scheduler()->get_data();
scheduler_update_grades($scheddata, $this->studentid);
}
Expand Down
16 changes: 14 additions & 2 deletions model/scheduler_instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ public function uses_teachernotes() {
return (floor($this->data->usenotes / 2) % 2 == 1);
}

/**
* Is scheduler supports roles or not
* @return int|boolean
*/
public function uses_roles() {
return (isset($this->data->rolessupport) &&
!empty($this->data->rolessupport)) ? intval($this->data->rolessupport) : 0;
}

/**
* Whether this scheduler uses booking forms at all
* @return bool whether the booking form is used
Expand Down Expand Up @@ -899,8 +908,11 @@ function get_conflicts($starttime, $endtime, $teacher = 0, $student = 0,
$teacherscope = "";
}

$studentjoin = ($student != 0) ? "JOIN {scheduler_appointment} a ON a.slotid = sl.id AND a.studentid = :studentid " : '';
$params['studentid'] = $student;
$studentjoin = '';
if ($student != 0) {
$studentjoin = "JOIN {scheduler_appointment} a ON a.slotid = sl.id AND a.studentid = :studentid ";
$params['studentid'] = $student;
}

$timeclause = "( (sl.starttime <= :starttime1 AND sl.starttime + sl.duration * 60 > :starttime2) OR
(sl.starttime < :endtime1 AND sl.starttime + sl.duration * 60 >= :endtime2) OR
Expand Down
Loading