Skip to content

Commit

Permalink
Improve session signup/cancellation based schedule.
Browse files Browse the repository at this point in the history
- Fix PLS-698
  • Loading branch information
prasanna-lmsace committed Feb 5, 2024
1 parent 82fe336 commit 7dd591f
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 4 deletions.
130 changes: 128 additions & 2 deletions conditions/session/classes/conditionform.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ public function is_user_completed($instancedata, $userid, \completion_info $comp

$sql = "SELECT count(*) FROM {facetoface_signups} f2f_su
JOIN {facetoface_sessions} f2f_ss ON f2f_ss.id = f2f_su.sessionid
WHERE f2f_ss.facetoface = :f2fid AND f2f_su.userid = :userid";
JOIN {facetoface_signups_status} f2f_sts ON f2f_su.id = f2f_sts.signupid
WHERE f2f_ss.facetoface = :f2fid AND f2f_su.userid = :userid
AND f2f_sts.superceded != 1 AND f2f_sts.statuscode = :booked";

$existingsignup = $DB->count_records_sql($sql, array(
'f2fid' => $modules, 'userid' => $userid, 'booked' => MDL_F2F_STATUS_BOOKED));

$existingsignup = $DB->count_records_sql($sql, array('f2fid' => $modules, 'userid' => $userid));
return ($existingsignup) ? true : false;
}
// Not configured any session modules.
Expand Down Expand Up @@ -218,4 +222,126 @@ public static function get_session_data($face2faceid, $userid) {
), 0, 1);
return $existingsignup;
}

/**
* Prepare the schedule for the user signup to the session.
*
* Gets the session from the param, and fetch the list of notification instance configured with this session.
* Filters the users list selected to signup to the session with signup users and its status code.
*
* Then all the notification instances are triggered for the filtered users.
*
* @param int|null $instanceid Face to face instance id.
* @return void
*/
public static function prepare_session_signup_schedule(?int $instanceid=null) {
global $PAGE, $DB;

// Current session id.
$sessionid = required_param('s', PARAM_INT);

// Get the list of signup users.
$session = facetoface_get_session($sessionid);
$instanceid = $instanceid ?: $session->facetoface;

$potentialuserselector = new \facetoface_candidate_selector('addselect', array('sessionid' => $session->id, 'courseid' => $PAGE->course->id));
$addusers = optional_param_array($potentialuserselector->get_name(), array(), PARAM_INT);

list($insql, $inparams) = $DB->get_in_or_equal($addusers, SQL_PARAMS_NAMED, 'f2fu');
$params = ['booked' => MDL_F2F_STATUS_BOOKED, 'sessionid' => $sessionid];

// Filter the users based on the signup status.
$users = $DB->get_fieldset_sql("
SELECT DISTINCT f2f_su.userid FROM {facetoface_signups} f2f_su
JOIN {facetoface_signups_status} f2f_sts ON f2f_su.id = f2f_sts.signupid
WHERE f2f_su.sessionid=:sessionid AND f2f_sts.statuscode = :booked AND f2f_su.userid $insql
GROUP BY f2f_su.userid
", $params + $inparams);

// Self condition instance.
$condition = new self();

// Fetch the session notifications uses this session signup.
$notifications = self::get_session_notifications($instanceid);

foreach ($notifications as $notification) {
// Get the notification suppres module ids.
$additional = $notification->additional ? json_decode($notification->additional, true) : '';
$modules = $additional['modules'] ?? '';

if (!empty($modules)) {

$session = $DB->get_record('facetoface_sessions_dates', array('sessionid' => $sessionid));
// Trigger all the instance for notifications.
foreach ($users as $userid) {
$condition->trigger_instance($notification->instanceid, $userid, $session->timestart);
}
}

}
}

/**
* Remove the schedule for the user removed from the session.
*
* Gets the session from the param, and fetch the list of notification instance configured with this session.
* Filters the users list selected to remove signup from the session.
*
* Then all the notification instances are triggered for the filtered users. this will make the schedule on hold.
*
* @param int|null $instanceid Face to face instance id.
* @return void
*/
public static function remove_session_signup_schedule(?int $instanceid=null) {
global $PAGE, $DB;

// Current session id.
$sessionid = required_param('s', PARAM_INT);

// Get the list of signup users.
$session = facetoface_get_session($sessionid);
$instanceid = $instanceid ?: $session->facetoface;

$potentialuserselector = new \facetoface_candidate_selector('removeselect', array('sessionid' => $session->id, 'courseid' => $PAGE->course->id));
$removeusers = optional_param_array($potentialuserselector->get_name(), array(), PARAM_INT);

list($insql, $inparams) = $DB->get_in_or_equal($removeusers, SQL_PARAMS_NAMED, 'f2fu');
$params = ['booked' => MDL_F2F_STATUS_BOOKED, 'sessionid' => $sessionid];

// Filter the users based on the signup status.
$users = $DB->get_fieldset_sql("
SELECT DISTINCT f2f_su.userid FROM {facetoface_signups} f2f_su
JOIN {facetoface_signups_status} f2f_sts ON f2f_su.id = f2f_sts.signupid
WHERE f2f_su.sessionid=:sessionid AND f2f_sts.statuscode = :booked AND f2f_su.userid $insql
GROUP BY f2f_su.userid
", $params + $inparams);

// Self condition instance.
$condition = new self();

// Fetch the session notifications uses this session signup.
$notifications = self::get_session_notifications($instanceid);

foreach ($notifications as $notification) {
// Get the notification suppres module ids.
$additional = $notification->additional ? json_decode($notification->additional, true) : '';
$modules = $additional['modules'] ?? '';

if (!empty($modules)) {

$session = $DB->get_record('facetoface_sessions_dates', array('sessionid' => $sessionid));
// Trigger all the instance for notifications.
foreach ($removeusers as $userid) {
if (isset($users[$userid])) {
continue;
}
// Trigger the instance will verify the user compleiton status of session signup.
// In this case user is cancelled from the session, so the schedule status will be updated to on-hold.
$condition->trigger_instance($notification->instanceid, $userid, $session->timestart);
}
}

}

}
}
5 changes: 4 additions & 1 deletion conditions/session/db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
'eventname' => '\mod_facetoface\event\signup_failed',
'callback' => '\pulsecondition_session\conditionform::signup_success',
),

array(
'eventname' => '\mod_facetoface\event\cancel_booking',
'callback' => '\pulsecondition_session\conditionform::signup_success',
),
];
81 changes: 81 additions & 0 deletions conditions/session/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Pulse condition session common functions to observe moodle default hooks
*
* @package pulsecondition_session
* @copyright 2023, bdecent gmbh bdecent.de
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Name of the session module.
*/
define('PULSE_SESSION_MOD', 'facetoface');

/**
* Type of the edit attendees page.
*/
define('PULSE_SESSION_MOD_EDITPAGEID', 'mod-facetoface-editattendees');

/**
* Extended the course navigation to observe the user add/remove from session from the backend by admin/managers.
* Verify the add param and verifies the page is session edit attendees page. Then triggers the schedule preparation.
*
* @param navigation_node $navigation
* @param stdClass $course Course info data.
* @param \context $context
* @return void
*/
function pulsecondition_session_extend_navigation_course(navigation_node $navigation, stdClass $course, $context) {
global $PAGE, $SCRIPT;

// Verify the page is facetoface edit attendees page and the admin/teachers added user to signup from backend.
// Trigger the pulse to get the list of new user signup in this session and create a schedule for those users.
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {

if ($PAGE->pagetype == PULSE_SESSION_MOD_EDITPAGEID && $PAGE->cm->modname == PULSE_SESSION_MOD) {
\pulsecondition_session\conditionform::prepare_session_signup_schedule($PAGE->cm->instance);
return true;
}

// When the error is raised during the signup, face to face throw exception,
// This exception prevents the above schedule content to run.
// Throw exception resets the PAGE urls, cm info, for the reason.
// In this case the page is set as site index and the course is not frontpage but the current file path is facetoface.
if ($PAGE->pagetype == 'site-index' && $PAGE->course->id != SITEID && $SCRIPT == '/mod/facetoface/editattendees.php') {
\pulsecondition_session\conditionform::prepare_session_signup_schedule($PAGE->cm->instance);
return true;
}
}

// Verify the page is facetoface edit attendees page and the admin/teachers added user to signup from backend.
// Trigger the pulse to get the list of new user signup in this session and create a schedule for those users.
if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {

if ($PAGE->pagetype == PULSE_SESSION_MOD_EDITPAGEID && $PAGE->cm->modname == PULSE_SESSION_MOD) {
\pulsecondition_session\conditionform::remove_session_signup_schedule($PAGE->cm->instance);
return true;
}

if ($PAGE->pagetype == 'site-index' && $PAGE->course->id != SITEID && $SCRIPT == '/mod/facetoface/editattendees.php') {
\pulsecondition_session\conditionform::remove_session_signup_schedule($PAGE->cm->instance);
return true;
}

}
}
2 changes: 1 addition & 1 deletion conditions/session/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'pulsecondition_session';
$plugin->version = 2023080204;
$plugin->version = 2023080205;
$plugin->dependencies = array('mod_facetoface' => 2021113000); // Dependencies set for the session module "Face to Face".

0 comments on commit 7dd591f

Please sign in to comment.