SCORM MDL-25781 fix links in TOC on view.php
[moodle.git] / mod / scorm / datamodels / scorm_13lib.php
CommitLineData
e5dd8e3b 1<?php
dc383b6f 2
08868f58 3function scorm_get_toc($user,$scorm,$toclink=TOCJSLINK,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false, $tocheader=false) {
d436d197 4 global $CFG, $DB, $PAGE, $OUTPUT;
dc383b6f 5
6 $strexpand = get_string('expcoll','scorm');
7 $modestr = '';
8 if ($mode == 'browse') {
9 $modestr = '&amp;mode='.$mode;
d9e6517a 10 }
d9e6517a 11
dc383b6f 12 $result = new stdClass();
cac50377
PH
13 if ($tocheader) {
14 $result->toc = '<div id="scorm_layout">';
15 $result->toc .= '<div id="scorm_toc">';
16 $result->toc .= '<div id="scorm_tree">';
17 }
18 $result->toc .= '<ul>';
dc383b6f 19 $tocmenus = array();
20 $result->prerequisites = true;
21 $incomplete = false;
d9e6517a 22
dc383b6f 23 //
24 // Get the current organization infos
25 //
dc383b6f 26 if (!empty($currentorg)) {
bf347041 27 if (($organizationtitle = $DB->get_field('scorm_scoes', 'title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
5c2aa157
DM
28 if ($play) {
29 $result->toctitle = "$organizationtitle";
30 }
31 else {
32 $result->toc .= "\t<li>$organizationtitle</li>\n";
33 }
dc383b6f 34 $tocmenus[] = $organizationtitle;
35 }
dc383b6f 36 }
37 //
38 // If not specified retrieve the last attempt number
39 //
40 if (empty($attempt)) {
41 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
42 }
43 $result->attemptleft = $scorm->maxattempt - $attempt;
82605bea 44 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
dc383b6f 45 //
46 // Retrieve user tracking data for each learning object
d9e6517a 47 //
48
dc383b6f 49 $usertracks = array();
50 foreach ($scoes as $sco) {
51 if (!empty($sco->launch)) {
82605bea 52 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
dc383b6f 53 if ($usertrack->status == '') {
54 $usertrack->status = 'notattempted';
55 }
56 $usertracks[$sco->identifier] = $usertrack;
57 }
58 }
59 }
60
61 $level=0;
62 $sublist=1;
63 $previd = 0;
64 $nextid = 0;
65 $findnext = false;
66 $parents[$level]='/';
82605bea 67 foreach ($scoes as $pos => $sco) {
b3659259 68 $isvisible = false;
294ce987 69 $sco->title = $sco->title;
82605bea 70 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
71 $isvisible = true;
b3659259 72 }
dc383b6f 73 if ($parents[$level]!=$sco->parent) {
74 if ($newlevel = array_search($sco->parent,$parents)) {
75 for ($i=0; $i<($level-$newlevel); $i++) {
cac50377 76 $result->toc .= "\t\t</li></ul></li>\n";
dc383b6f 77 }
78 $level = $newlevel;
79 } else {
80 $i = $level;
81 $closelist = '';
82 while (($i > 0) && ($parents[$level] != $sco->parent)) {
cac50377 83 $closelist .= "\t\t</li></ul></li>\n";
dc383b6f 84 $i--;
85 }
86 if (($i == 0) && ($sco->parent != $currentorg)) {
87 $style = '';
88 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
89 $style = ' style="display: none;"';
90 }
5c2aa157
DM
91 //$result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
92 $result->toc .= "\t\t><ul>\n";
dc383b6f 93 $level++;
94 } else {
95 $result->toc .= $closelist;
96 $level = $i;
97 }
98 $parents[$level]=$sco->parent;
99 }
100 }
cac50377
PH
101 if ($isvisible) {
102 $result->toc .= "<li>";
103 }
9fb2de4e 104 if (isset($scoes[$pos+1])) {
105 $nextsco = $scoes[$pos+1];
106 } else {
107 $nextsco = false;
108 }
b3659259 109 $nextisvisible = false;
82605bea 110 if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
111 $nextisvisible = true;
b3659259 112 }
d9e6517a 113 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
10fd330f 114 (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 115 $sublist++;
116 $icon = 'minus';
117 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
118 $icon = 'plus';
119 }
5c2aa157
DM
120// $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
121// '<img id="img'.$sublist.'" src="'.$OUTPUT->pix_url($icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
b3659259 122 } else if ($isvisible) {
5c2aa157 123// $result->toc .= "\t\t".'<li><img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
dc383b6f 124 }
125 if (empty($sco->title)) {
126 $sco->title = $sco->identifier;
127 }
128 if (!empty($sco->launch)) {
b3659259 129 if ($isvisible) {
130 $startbold = '';
131 $endbold = '';
132 $score = '';
133 if (empty($scoid) && ($mode != 'normal')) {
134 $scoid = $sco->id;
530a61d5 135 }
b3659259 136 if (isset($usertracks[$sco->identifier])) {
137 $usertrack = $usertracks[$sco->identifier];
138 $strstatus = get_string($usertrack->status,'scorm');
139 if ($sco->scormtype == 'sco') {
485f4ce6 140 $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
b3659259 141 } else {
485f4ce6 142 $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
b3659259 143 }
d9e6517a 144
b3659259 145 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
146 $incomplete = true;
147 if ($play && empty($scoid)) {
148 $scoid = $sco->id;
149 }
150 }
151 if ($usertrack->score_raw != '') {
152 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
153 }
154 $strsuspended = get_string('suspended','scorm');
946b6f0d
DM
155 if ($incomplete && isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend')) {
156 $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
b3659259 157 }
158 } else {
dc383b6f 159 if ($play && empty($scoid)) {
160 $scoid = $sco->id;
161 }
b3659259 162 if ($sco->scormtype == 'sco') {
485f4ce6 163 $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
b3659259 164 $incomplete = true;
165 } else {
485f4ce6 166 $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
b3659259 167 }
530a61d5 168 }
530a61d5 169
dc383b6f 170 if ($sco->id == $scoid) {
b3659259 171 $startbold = '<b>';
172 $endbold = '</b>';
173 $findnext = true;
82605bea 174 $shownext = isset($sco->next) ? $sco->next : 0;
175 $showprev = isset($sco->prev) ? $sco->prev : 0;
dc383b6f 176 }
d9e6517a 177
b3659259 178 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
179 if (!empty($sco->launch)) {
180 $previd = $sco->id;
181 }
182 }
86996ffe 183 require_once($CFG->dirroot.'/mod/scorm/datamodels/sequencinglib.php');
10fd330f 184 if (scorm_seq_evaluate($sco->id,$usertracks)) {
b3659259 185 if ($sco->id == $scoid) {
186 $result->prerequisites = true;
187 }
08868f58
DM
188
189 if ($toclink == TOCFULLURL) { //display toc with urls for structure page
b3659259 190 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
08868f58
DM
191 $result->toc .= $statusicon.'&nbsp;<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score."\n";
192 } else {
cac50377 193 if ($sco->launch) {
08868f58 194 $link = 'a='.$scorm->id.'&scoid='.$sco->id.'&currentorg='.$currentorg.$modestr.'&attempt='.$attempt;
cac50377 195 $result->toc .= '<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>';
08868f58 196 } else {
cac50377
PH
197 $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
198 }
08868f58
DM
199 }
200 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
b3659259 201 } else {
202 if ($sco->id == $scoid) {
203 $result->prerequisites = false;
204 }
cac50377
PH
205 //$result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
206 if ($play) {
207 // should be disabled
208 $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
209 }
210 else {
211 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
212 }
213 }
214 if (($nextsco === false) || $nextsco->parent == $sco->parent) {
215 $result->toc .= '</li>';
dc383b6f 216 }
dc383b6f 217 }
218 } else {
b3659259 219 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 220 }
221 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
222 if (!empty($nextsco->launch)) {
223 $nextid = $nextsco->id;
224 }
225 }
226 }
227 for ($i=0;$i<$level;$i++) {
228 $result->toc .= "\t\t</ul></li>\n";
229 }
d9e6517a 230
dc383b6f 231 if ($play) {
bf347041 232 $sco = $DB->get_record('scorm_scoes', array('id'=>$scoid));
dc383b6f 233 $sco->previd = $previd;
234 $sco->nextid = $nextid;
235 $result->sco = $sco;
236 $result->incomplete = $incomplete;
237 } else {
238 $result->incomplete = $incomplete;
239 }
240 }
cac50377 241 $result->toc .= '</ul>';
5c2aa157
DM
242
243
244 // NEW IMS TOC
cac50377
PH
245 if ($tocheader) {
246 $result->toc .= '</div></div></div>';
247 $result->toc .= '<div id="scorm_navpanel"></div>';
248 }
5c2aa157
DM
249
250
dc383b6f 251 if ($scorm->hidetoc == 0) {
d436d197 252 $PAGE->requires->data_for_js('scormdata', array(
485f4ce6
PH
253 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
254 'minusicon' => $OUTPUT->pix_url('minus', 'scorm')));
9dec75db
PS
255 $PAGE->requires->js('/lib/cookies.js');
256 $PAGE->requires->js('/mod/scorm/datamodels/scorm_datamodels.js');
dc383b6f 257 }
d9e6517a 258
f8dab966
PS
259 $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
260 $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
dc383b6f 261
262 return $result;
263}
264
e5dd8e3b 265