-
Notifications
You must be signed in to change notification settings - Fork 6
/
launch.php
executable file
·139 lines (108 loc) · 4.71 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
135
136
137
138
139
<?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 number.
* The cmi5 player does the actual playing. This file enables handling of launch url from the player and data saving.
* @copyright 2023 Megan Bohland
* @copyright Based on work by 2013 Andrew Downes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_cmi5launch\local\cmi5_connectors;
use mod_cmi5launch\local\au_helpers;
use mod_cmi5launch\local\customException;
use mod_cmi5launch\local\session_helpers;
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require('header.php');
require_once("$CFG->dirroot/lib/outputcomponents.php");
// Include the errorover (error override) funcs.
require_once ($CFG->dirroot . '/mod/cmi5launch/classes/local/errorover.php');
require_login($course, false, $cm);
global $CFG, $cmi5launch, $USER, $DB;
// MB - currently not utilizing events, but may in future.
/*
// 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.
$auhelper = new au_helpers;
$connectors = new cmi5_connectors;
$sessionhelper = new session_helpers;
$savesession = $sessionhelper->cmi5launch_get_create_session();
$cmi5launchretrieveurl = $connectors->cmi5launch_get_retrieve_url();
$retrieveaus = $auhelper->get_cmi5launch_retrieve_aus_from_db();
// Retrieve the registration id from previous page.
$id = required_param('launchform_registration', PARAM_TEXT);
// Reload cmi5 instance.
$record = $DB->get_record('cmi5launch', array('id' => $cmi5launch->id));
// Retrieve user's course record.
$userscourse = $DB->get_record('cmi5launch_usercourse', ['courseid' => $record->courseid, 'userid' => $USER->id]);
// To hold launch url.
$location = "";
// Most of the functions below have their own error handling. We will encapsulate here in case there are any php errors, such
// as json_decode not working, etc.
// Set error and exception handler to catch and override the default PHP error messages, to make messages more user friendly.
set_error_handler('mod_cmi5launch\local\custom_warning', E_WARNING);
//set_exception_handler('mod_cmi5launch\local\customException');
try {
// Retrieve AUs.
$au = $retrieveaus($id);
// Retrieve the au index.
$auindex = $au->auindex;
// Pass in the au index to retrieve a launchurl and session id.
$urldecoded = $cmi5launchretrieveurl($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 before changing them.
$sessionlist = json_decode($au->sessions);
// Now add the new session number.
$sessionlist[] = $sessionid;
} else {
// If it is null 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'];
} catch (\Throwable $e) {
// Restore default handlers.
restore_exception_handler();
restore_error_handler();
// If there is an error, return the error.
throw new customException("Error in launching experience. Report this error to system administrator: ". $e->getMessage());
}
// Create and save session object to session table.
$savesession($sessionid, $location, $launchmethod);
// Last thing check for updates.
cmi5launch_update_grades($cmi5launch, $USER->id);
header("Location: " . $location);
// Restore default handlers.
restore_exception_handler();
restore_error_handler();
exit;