forked from adlnet/Moodle-mod_cmi5launch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.php
executable file
·134 lines (101 loc) · 4.08 KB
/
launch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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/>.
/**
* launches the experience with the requested registration
*
* @copyright 2023 Megan Bohland
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once('header.php');
require_once("$CFG->dirroot/mod/cmi5launch/cmi5PHP/src/sessionHelpers.php");
global $CFG, $cmi5launch, $USER, $DB;
// Trigger Activity launched event.
$event = \mod_cmi5launch\event\activity_launched::create(array(
'objectid' => $cmi5launch->id,
'context' => $context,
));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('cmi5launch', $cmi5launch);
$event->trigger();
//External class and funcs to use
$aus_helpers = new Au_Helpers;
$connectors = new cmi5Connectors;
$ses_helpers = new Session_Helpers;
$saveSession = $ses_helpers->getSaveSession();
$retrieveUrl = $connectors->getRetrieveUrl();
$getAUs = $aus_helpers->getAUsFromDB();
//Retrieve registration id and au index (from AUview.php)
$fromAUview = required_param('launchform_registration', PARAM_TEXT);
//Break it into array (AU or session id is first index)
$idAndStatus = explode(",", $fromAUview);
//Retrieve AU OR session id
$id = array_shift($idAndStatus);
// Reload cmi5 instance.
$record = $DB->get_record('cmi5launch', array('id' => $cmi5launch->id));
//Retrieve user's course record
$usersCourse = $DB->get_record('cmi5launch_course', ['courseid' => $record->courseid, 'userid' => $USER->id]);
//Retrieve registration id
$registrationid = $usersCourse->registrationid;
if (empty($registrationid)) {
echo "<div class='alert alert-error'>" . get_string('cmi5launch_regidempty', 'cmi5launch') . "</div>";
// Failed to connect to LRS.
if ($CFG->debug == 32767) {
echo "<p>Error attempting to get registration id querystring parameter.</p>";
die();
}
}
//To hold launch url or whether launch url is new!
$location = "";
//If true, this is a NEW launch
if ($idAndStatus[0] == "true") {
//Retrieve AUs
$au = $getAUs($id);
//Retrieve AU index
$auIndex = $au->auindex;
//Pass in the au index to retrieve a launchurl and session id
$urlDecoded = $retrieveUrl($cmi5launch->id, $auIndex);
//Retrieve and store session id in the aus table
$sessionID = intval($urlDecoded['id']);
//Check if there are previous sessions
if(!$au->sessions == NULL){
//We don't want to overwrite so retrieve the sessions
$sessionList = json_decode($au->sessions);
//now add the new session number
$sessionList[] = $sessionID;
}else{
//It is null so just start fresh
$sessionList = array();
$sessionList[] = $sessionID;
}
//Save sessions
$au->sessions =json_encode($sessionList );
//The record needs to updated in db
$updated = $DB->update_record('cmi5launch_aus', $au, true);
//Retrieve the launch url
$location = $urlDecoded['url'];
//And launch method
$launchMethod = $urlDecoded['launchMethod'];
//Create and save session object to session table
$saveSession($sessionID, $location, $launchMethod);
} else {
//This is a new session, we want to get the launch url from the sessions
$session = $DB->get_record('cmi5launch_sessions', array('sessionid' => $id));
//Launch url isss in old session record
$location = $session->launchurl;
}
header("Location: ". $location);
exit;