lib-cookies MDL-19737 Removed need to include cookies.js on every page
[moodle.git] / mod / scorm / datamodels / scorm_13lib.php
CommitLineData
f4ba7e1a 1<?php // $Id$
dc383b6f 2
3function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=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();
9b60b28a 13 $result->toc = "<ul id='s0' class='$liststyle'>\n";
dc383b6f 14 $tocmenus = array();
15 $result->prerequisites = true;
16 $incomplete = false;
d9e6517a 17
dc383b6f 18 //
19 // Get the current organization infos
20 //
dc383b6f 21 if (!empty($currentorg)) {
bf347041 22 if (($organizationtitle = $DB->get_field('scorm_scoes', 'title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
dc383b6f 23 $result->toc .= "\t<li>$organizationtitle</li>\n";
24 $tocmenus[] = $organizationtitle;
25 }
dc383b6f 26 }
27 //
28 // If not specified retrieve the last attempt number
29 //
30 if (empty($attempt)) {
31 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
32 }
33 $result->attemptleft = $scorm->maxattempt - $attempt;
82605bea 34 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
dc383b6f 35 //
36 // Retrieve user tracking data for each learning object
d9e6517a 37 //
38
dc383b6f 39 $usertracks = array();
40 foreach ($scoes as $sco) {
41 if (!empty($sco->launch)) {
82605bea 42 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
dc383b6f 43 if ($usertrack->status == '') {
44 $usertrack->status = 'notattempted';
45 }
46 $usertracks[$sco->identifier] = $usertrack;
47 }
48 }
49 }
50
51 $level=0;
52 $sublist=1;
53 $previd = 0;
54 $nextid = 0;
55 $findnext = false;
56 $parents[$level]='/';
82605bea 57 foreach ($scoes as $pos => $sco) {
b3659259 58 $isvisible = false;
294ce987 59 $sco->title = $sco->title;
82605bea 60 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
61 $isvisible = true;
b3659259 62 }
dc383b6f 63 if ($parents[$level]!=$sco->parent) {
64 if ($newlevel = array_search($sco->parent,$parents)) {
65 for ($i=0; $i<($level-$newlevel); $i++) {
66 $result->toc .= "\t\t</ul></li>\n";
67 }
68 $level = $newlevel;
69 } else {
70 $i = $level;
71 $closelist = '';
72 while (($i > 0) && ($parents[$level] != $sco->parent)) {
73 $closelist .= "\t\t</ul></li>\n";
74 $i--;
75 }
76 if (($i == 0) && ($sco->parent != $currentorg)) {
77 $style = '';
78 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
79 $style = ' style="display: none;"';
80 }
9b60b28a 81 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
dc383b6f 82 $level++;
83 } else {
84 $result->toc .= $closelist;
85 $level = $i;
86 }
87 $parents[$level]=$sco->parent;
88 }
89 }
9fb2de4e 90 if (isset($scoes[$pos+1])) {
91 $nextsco = $scoes[$pos+1];
92 } else {
93 $nextsco = false;
94 }
b3659259 95 $nextisvisible = false;
82605bea 96 if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
97 $nextisvisible = true;
b3659259 98 }
d9e6517a 99 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
10fd330f 100 (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 101 $sublist++;
102 $icon = 'minus';
103 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
104 $icon = 'plus';
105 }
d28548d5 106 $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
d436d197 107 '<img id="img'.$sublist.'" src="'.$OUTPUT->mod_icon_url('pix/'.$icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
b3659259 108 } else if ($isvisible) {
d436d197 109 $result->toc .= "\t\t".'<li><img src="'.$OUTPUT->mod_icon_url('pix/spacer', 'scorm').'" alt="" />';
dc383b6f 110 }
111 if (empty($sco->title)) {
112 $sco->title = $sco->identifier;
113 }
114 if (!empty($sco->launch)) {
b3659259 115 if ($isvisible) {
116 $startbold = '';
117 $endbold = '';
118 $score = '';
119 if (empty($scoid) && ($mode != 'normal')) {
120 $scoid = $sco->id;
530a61d5 121 }
b3659259 122 if (isset($usertracks[$sco->identifier])) {
123 $usertrack = $usertracks[$sco->identifier];
124 $strstatus = get_string($usertrack->status,'scorm');
125 if ($sco->scormtype == 'sco') {
d436d197 126 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/'.$usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
b3659259 127 } else {
d436d197 128 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
b3659259 129 }
d9e6517a 130
b3659259 131 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
132 $incomplete = true;
133 if ($play && empty($scoid)) {
134 $scoid = $sco->id;
135 }
136 }
137 if ($usertrack->score_raw != '') {
138 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
139 }
140 $strsuspended = get_string('suspended','scorm');
141 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
7ef16bf9 142 if($usertrack->status !='completed') {
d436d197 143 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
7ef16bf9 144 }
b3659259 145 }
146 } else {
dc383b6f 147 if ($play && empty($scoid)) {
148 $scoid = $sco->id;
149 }
b3659259 150 if ($sco->scormtype == 'sco') {
d436d197 151 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
b3659259 152 $incomplete = true;
153 } else {
d436d197 154 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
b3659259 155 }
530a61d5 156 }
530a61d5 157
dc383b6f 158 if ($sco->id == $scoid) {
b3659259 159 $startbold = '<b>';
160 $endbold = '</b>';
161 $findnext = true;
82605bea 162 $shownext = isset($sco->next) ? $sco->next : 0;
163 $showprev = isset($sco->prev) ? $sco->prev : 0;
dc383b6f 164 }
d9e6517a 165
b3659259 166 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
167 if (!empty($sco->launch)) {
168 $previd = $sco->id;
169 }
170 }
10fd330f 171 require_once('sequencinglib.php');
172 if (scorm_seq_evaluate($sco->id,$usertracks)) {
b3659259 173 if ($sco->id == $scoid) {
174 $result->prerequisites = true;
175 }
b3659259 176 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
177 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
178 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
b3659259 179 } else {
180 if ($sco->id == $scoid) {
181 $result->prerequisites = false;
182 }
183 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 184 }
dc383b6f 185 }
186 } else {
b3659259 187 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 188 }
189 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
190 if (!empty($nextsco->launch)) {
191 $nextid = $nextsco->id;
192 }
193 }
194 }
195 for ($i=0;$i<$level;$i++) {
196 $result->toc .= "\t\t</ul></li>\n";
197 }
d9e6517a 198
dc383b6f 199 if ($play) {
bf347041 200 $sco = $DB->get_record('scorm_scoes', array('id'=>$scoid));
dc383b6f 201 $sco->previd = $previd;
202 $sco->nextid = $nextid;
203 $result->sco = $sco;
204 $result->incomplete = $incomplete;
205 } else {
206 $result->incomplete = $incomplete;
207 }
208 }
209 $result->toc .= "\t</ul>\n";
210 if ($scorm->hidetoc == 0) {
d436d197 211 $PAGE->requires->data_for_js('scormdata', array(
212 'plusicon' => $OUTPUT->mod_icon_url('pix/plus', 'scorm'),
213 'minusicon' => $OUTPUT->mod_icon_url('pix/minus', 'scorm')));
ac310b3a 214 $PAGE->requires->js('lib/cookies.js');
bd257031 215 $PAGE->requires->js('mod/scorm/datamodels/scorm_datamodels.js');
dc383b6f 216 }
d9e6517a 217
fd21e467 218 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr;
7b1f2c82 219 $select = html_select::make_popup_form($url, 'scoid', $tocmenus, "tocmenu", $sco->id);
fd21e467 220 $select->nothinglabel = false;
221 $result->tocmenu = $OUTPUT->select($select);
dc383b6f 222
223 return $result;
224}
225
dc383b6f 226?>