b898a479effd387a54ffb00d68f697c59ae4916e
[moodle.git] / mod / scorm / view.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 require_once("../../config.php");
18 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
19 require_once($CFG->dirroot.'/course/lib.php');
21 $id = optional_param('id', '', PARAM_INT);       // Course Module ID, or
22 $a = optional_param('a', '', PARAM_INT);         // scorm ID
23 $organization = optional_param('organization', '', PARAM_INT); // organization ID
24 $action = optional_param('action', '', PARAM_ALPHA);
26 if (!empty($id)) {
27     if (! $cm = get_coursemodule_from_id('scorm', $id, 0, true)) {
28         print_error('invalidcoursemodule');
29     }
30     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
31         print_error('coursemisconf');
32     }
33     if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
34         print_error('invalidcoursemodule');
35     }
36 } else if (!empty($a)) {
37     if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
38         print_error('invalidcoursemodule');
39     }
40     if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
41         print_error('coursemisconf');
42     }
43     if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id, true)) {
44         print_error('invalidcoursemodule');
45     }
46 } else {
47     print_error('missingparameter');
48 }
50 $url = new moodle_url('/mod/scorm/view.php', array('id'=>$cm->id));
51 if ($organization !== '') {
52     $url->param('organization', $organization);
53 }
54 $PAGE->set_url($url);
55 $forcejs = get_config('scorm', 'forcejavascript');
56 if (!empty($forcejs)) {
57     $PAGE->add_body_class('forcejavascript');
58 }
60 require_login($course, false, $cm);
62 $context = context_course::instance($course->id);
63 $contextmodule = context_module::instance($cm->id);
65 $launch = false; // Does this automatically trigger a launch based on skipview.
66 if (!empty($scorm->popup)) {
67     $orgidentifier = '';
68     $scoid = 0;
69     if ($scorm->skipview >= SCORM_SKIPVIEW_FIRST &&
70         has_capability('mod/scorm:skipview', $contextmodule) &&
71         !has_capability('mod/scorm:viewreport', $contextmodule)) { // Don't skip users with the capability to view reports.
73         // do we launch immediately and redirect the parent back ?
74         if ($scorm->skipview == SCORM_SKIPVIEW_ALWAYS || !scorm_has_tracks($scorm->id, $USER->id)) {
75             $orgidentifier = '';
76             if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
77                 if (($sco->organization == '') && ($sco->launch == '')) {
78                     $orgidentifier = $sco->identifier;
79                 } else {
80                     $orgidentifier = $sco->organization;
81                 }
82                 $scoid = $sco->id;
83             }
84             $launch = true;
85         }
86     }
87     // Redirect back to the section with one section per page ?
89     $courseformat = course_get_format($course)->get_course();
90     $sectionid = '';
91     if (isset($courseformat->coursedisplay) && $courseformat->coursedisplay == COURSE_DISPLAY_MULTIPAGE) {
92         $sectionid = $cm->sectionnum;
93     }
95     $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => $launch,
96                                                            'currentorg' => $orgidentifier,
97                                                            'sco' => $scoid,
98                                                            'scorm' => $scorm->id,
99                                                            'courseurl' => course_get_url($course, $sectionid)->out(false),
100                                                            'cwidth' => $scorm->width,
101                                                            'cheight' => $scorm->height,
102                                                            'popupoptions' => $scorm->options), true);
103     $PAGE->requires->string_for_js('popupsblocked', 'scorm');
104     $PAGE->requires->string_for_js('popuplaunched', 'scorm');
105     $PAGE->requires->js('/mod/scorm/view.js', true);
108 if (isset($SESSION->scorm)) {
109     unset($SESSION->scorm);
112 $strscorms = get_string("modulenameplural", "scorm");
113 $strscorm  = get_string("modulename", "scorm");
115 $shortname = format_string($course->shortname, true, array('context' => $context));
116 $pagetitle = strip_tags($shortname.': '.format_string($scorm->name));
118 // Trigger module viewed event.
119 $event = \mod_scorm\event\course_module_viewed::create(array(
120     'objectid' => $scorm->id,
121     'context' => $contextmodule,
122 ));
123 $event->add_record_snapshot('scorm', $scorm);
124 $event->add_record_snapshot('course_modules', $cm);
125 $event->trigger();
127 if (empty($launch) && (has_capability('mod/scorm:skipview', $contextmodule))) {
128     scorm_simple_play($scorm, $USER, $contextmodule, $cm->id);
131 //
132 // Print the page header
133 //
134 $PAGE->set_title($pagetitle);
135 $PAGE->set_heading($course->fullname);
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading(format_string($scorm->name));
139 if (!empty($action) && confirm_sesskey() && has_capability('mod/scorm:deleteownresponses', $contextmodule)) {
140     if ($action == 'delete') {
141         $confirmurl = new moodle_url($PAGE->url, array('action'=>'deleteconfirm'));
142         echo $OUTPUT->confirm(get_string('deleteuserattemptcheck', 'scorm'), $confirmurl, $PAGE->url);
143         echo $OUTPUT->footer();
144         exit;
145     } else if ($action == 'deleteconfirm') {
146         //delete this users attempts.
147         $DB->delete_records('scorm_scoes_track', array('userid' => $USER->id, 'scormid' => $scorm->id));
148         scorm_update_grades($scorm, $USER->id, true);
149         echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
150     }
153 $currenttab = 'info';
154 require($CFG->dirroot . '/mod/scorm/tabs.php');
156 // Print the main part of the page
157 $attemptstatus = '';
158 if (empty($launch) && ($scorm->displayattemptstatus == SCORM_DISPLAY_ATTEMPTSTATUS_ALL ||
159          $scorm->displayattemptstatus == SCORM_DISPLAY_ATTEMPTSTATUS_ENTRY)) {
160     $attemptstatus = scorm_get_attempt_status($USER, $scorm, $cm);
162 echo $OUTPUT->box(format_module_intro('scorm', $scorm, $cm->id).$attemptstatus, 'generalbox boxaligncenter boxwidthwide', 'intro');
164 $scormopen = true;
165 $timenow = time();
166 if (!empty($scorm->timeopen) && $scorm->timeopen > $timenow) {
167     echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
168     $scormopen = false;
170 if (!empty($scorm->timeclose) && $timenow > $scorm->timeclose) {
171     echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
172     $scormopen = false;
174 if ($scormopen && empty($launch)) {
175     scorm_view_display($USER, $scorm, 'view.php?id='.$cm->id, $cm);
177 if (!empty($forcejs)) {
178     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
181 if (!empty($scorm->popup)) {
182     $PAGE->requires->js_init_call('M.mod_scormform.init');
185 echo $OUTPUT->footer();