Merge branch 'MDL-41667' of git://github.com/timhunt/moodle
[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');
20 $id = optional_param('id', '', PARAM_INT);       // Course Module ID, or
21 $a = optional_param('a', '', PARAM_INT);         // scorm ID
22 $organization = optional_param('organization', '', PARAM_INT); // organization ID
23 $action = optional_param('action', '', PARAM_ALPHA);
25 if (!empty($id)) {
26     if (! $cm = get_coursemodule_from_id('scorm', $id)) {
27         print_error('invalidcoursemodule');
28     }
29     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
30         print_error('coursemisconf');
31     }
32     if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
33         print_error('invalidcoursemodule');
34     }
35 } else if (!empty($a)) {
36     if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
37         print_error('invalidcoursemodule');
38     }
39     if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
40         print_error('coursemisconf');
41     }
42     if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
43         print_error('invalidcoursemodule');
44     }
45 } else {
46     print_error('missingparameter');
47 }
49 $url = new moodle_url('/mod/scorm/view.php', array('id'=>$cm->id));
50 if ($organization !== '') {
51     $url->param('organization', $organization);
52 }
53 $PAGE->set_url($url);
54 $forcejs = get_config('scorm', 'forcejavascript');
55 if (!empty($forcejs)) {
56     $PAGE->add_body_class('forcejavascript');
57 }
59 require_login($course, false, $cm);
61 $context = context_course::instance($course->id);
62 $contextmodule = context_module::instance($cm->id);
64 $launch = false; // Does this automatically trigger a launch based on skipview.
65 if (!empty($scorm->popup)) {
66     $orgidentifier = '';
67     $scoid = 0;
68     if ($scorm->skipview >= SCORM_SKIPVIEW_FIRST &&
69         has_capability('mod/scorm:skipview', $contextmodule) &&
70         !has_capability('mod/scorm:viewreport', $contextmodule)) { // Don't skip users with the capability to view reports.
72         // do we launch immediately and redirect the parent back ?
73         if ($scorm->skipview == SCORM_SKIPVIEW_ALWAYS || !scorm_has_tracks($scorm->id, $USER->id)) {
74             $orgidentifier = '';
75             if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
76                 if (($sco->organization == '') && ($sco->launch == '')) {
77                     $orgidentifier = $sco->identifier;
78                 } else {
79                     $orgidentifier = $sco->organization;
80                 }
81                 $scoid = $sco->id;
82             }
83             $launch = true;
84         }
85     }
86     $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => $launch,
87                                                            'currentorg' => $orgidentifier,
88                                                            'sco' => $scoid,
89                                                            'scorm' => $scorm->id,
90                                                            'courseid' => $scorm->course,
91                                                            'cwidth' => $scorm->width,
92                                                            'cheight' => $scorm->height,
93                                                            'popupoptions' => $scorm->options), true);
94     $PAGE->requires->js('/mod/scorm/view.js', true);
95 }
97 if (isset($SESSION->scorm)) {
98     unset($SESSION->scorm);
99 }
101 $strscorms = get_string("modulenameplural", "scorm");
102 $strscorm  = get_string("modulename", "scorm");
104 $shortname = format_string($course->shortname, true, array('context' => $context));
105 $pagetitle = strip_tags($shortname.': '.format_string($scorm->name));
107 add_to_log($course->id, 'scorm', 'pre-view', 'view.php?id='.$cm->id, "$scorm->id", $cm->id);
109 if (empty($launch) && (has_capability('mod/scorm:skipview', $contextmodule))) {
110     scorm_simple_play($scorm, $USER, $contextmodule, $cm->id);
113 //
114 // Print the page header
115 //
116 $PAGE->set_title($pagetitle);
117 $PAGE->set_heading($course->fullname);
118 echo $OUTPUT->header();
120 if (!empty($action) && confirm_sesskey() && has_capability('mod/scorm:deleteownresponses', $contextmodule)) {
121     if ($action == 'delete') {
122         $confirmurl = new moodle_url($PAGE->url, array('action'=>'deleteconfirm'));
123         echo $OUTPUT->confirm(get_string('deleteuserattemptcheck', 'scorm'), $confirmurl, $PAGE->url);
124         echo $OUTPUT->footer();
125         exit;
126     } else if ($action == 'deleteconfirm') {
127         //delete this users attempts.
128         $DB->delete_records('scorm_scoes_track', array('userid' => $USER->id, 'scormid' => $scorm->id));
129         scorm_update_grades($scorm, $USER->id, true);
130         echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
131     }
134 $currenttab = 'info';
135 require($CFG->dirroot . '/mod/scorm/tabs.php');
137 // Print the main part of the page
138 echo $OUTPUT->heading(format_string($scorm->name));
139 $attemptstatus = '';
140 if (empty($launch) && ($scorm->displayattemptstatus == SCORM_DISPLAY_ATTEMPTSTATUS_ALL ||
141          $scorm->displayattemptstatus == SCORM_DISPLAY_ATTEMPTSTATUS_ENTRY)) {
142     $attemptstatus = scorm_get_attempt_status($USER, $scorm, $cm);
144 echo $OUTPUT->box(format_module_intro('scorm', $scorm, $cm->id).$attemptstatus, 'generalbox boxaligncenter boxwidthwide', 'intro');
146 $scormopen = true;
147 $timenow = time();
148 if (!empty($scorm->timeopen) && $scorm->timeopen > $timenow) {
149     echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
150     $scormopen = false;
152 if (!empty($scorm->timeclose) && $timenow > $scorm->timeclose) {
153     echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
154     $scormopen = false;
156 if ($scormopen && empty($launch)) {
157     scorm_view_display($USER, $scorm, 'view.php?id='.$cm->id, $cm);
159 if (!empty($forcejs)) {
160     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
163 if (!empty($scorm->popup)) {
164     $PAGE->requires->js_init_call('M.mod_scormform.init');
167 echo $OUTPUT->footer();