"MDL-12304, fix double text"
[moodle-linuxchix.git] / mod / scorm / datamodels / scorm_13lib.php
blob1c95eaadb6bafd5bfa957917bd805ba486055ba0
1 <?php // $Id$
3 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
4 global $CFG;
6 $strexpand = get_string('expcoll','scorm');
7 $modestr = '';
8 if ($mode == 'browse') {
9 $modestr = '&amp;mode='.$mode;
11 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
13 $result = new stdClass();
14 $result->toc = "<ul id='s0' class='$liststyle'>\n";
15 $tocmenus = array();
16 $result->prerequisites = true;
17 $incomplete = false;
20 // Get the current organization infos
22 $organizationsql = '';
23 if (!empty($currentorg)) {
24 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id,'identifier',$currentorg)) != '') {
25 $result->toc .= "\t<li>$organizationtitle</li>\n";
26 $tocmenus[] = $organizationtitle;
28 $organizationsql = "AND organization='$currentorg'";
31 // If not specified retrieve the last attempt number
33 if (empty($attempt)) {
34 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
36 $result->attemptleft = $scorm->maxattempt - $attempt;
37 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
38 // drop keys so that we can access array sequentially
39 $scoes = array_values($scoes);
41 // Retrieve user tracking data for each learning object
42 //
44 $usertracks = array();
45 $optionaldatas = array();
46 foreach ($scoes as $sco) {
47 if (!empty($sco->launch)) {
48 if ($usertrack=scorm_get_tracks($sco->id,$user->id,$attempt)) {
49 if ($usertrack->status == '') {
50 $usertrack->status = 'notattempted';
52 $usertracks[$sco->identifier] = $usertrack;
54 if ($optionaldata = scorm_get_sco($sco->id, SCO_DATA)) {
55 $optionaldatas[$sco->identifier] = $optionaldata;
60 $level=0;
61 $sublist=1;
62 $previd = 0;
63 $nextid = 0;
64 $findnext = false;
65 $parents[$level]='/';
66 foreach ($scoes as $pos=>$sco) {
67 $isvisible = false;
68 $sco->title = stripslashes($sco->title);
69 if (isset($optionaldatas[$sco->identifier])) {
70 if (!isset($optionaldatas[$sco->identifier]->isvisible) ||
71 (isset($optionaldatas[$sco->identifier]->isvisible) && ($optionaldatas[$sco->identifier]->isvisible == 'true'))) {
72 $isvisible = true;
75 if ($parents[$level]!=$sco->parent) {
76 if ($newlevel = array_search($sco->parent,$parents)) {
77 for ($i=0; $i<($level-$newlevel); $i++) {
78 $result->toc .= "\t\t</ul></li>\n";
80 $level = $newlevel;
81 } else {
82 $i = $level;
83 $closelist = '';
84 while (($i > 0) && ($parents[$level] != $sco->parent)) {
85 $closelist .= "\t\t</ul></li>\n";
86 $i--;
88 if (($i == 0) && ($sco->parent != $currentorg)) {
89 $style = '';
90 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
91 $style = ' style="display: none;"';
93 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
94 $level++;
95 } else {
96 $result->toc .= $closelist;
97 $level = $i;
99 $parents[$level]=$sco->parent;
102 if (isset($scoes[$pos+1])) {
103 $nextsco = $scoes[$pos+1];
104 } else {
105 $nextsco = false;
107 $nextisvisible = false;
108 if (($nextsco !== false) && (isset($optionaldatas[$nextsco->identifier]))) {
109 if (!isset($optionaldatas[$nextsco->identifier]->isvisible) ||
110 (isset($optionaldatas[$nextsco->identifier]->isvisible) && ($optionaldatas[$nextsco->identifier]->isvisible == 'true'))) {
111 $nextisvisible = true;
114 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
115 (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
116 $sublist++;
117 $icon = 'minus';
118 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
119 $icon = 'plus';
121 $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
122 '<img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
123 } else if ($isvisible) {
124 $result->toc .= "\t\t".'<li><img src="'.$scormpixdir.'/spacer.gif" />';
126 if (empty($sco->title)) {
127 $sco->title = $sco->identifier;
129 if (!empty($sco->launch)) {
130 if ($isvisible) {
131 $startbold = '';
132 $endbold = '';
133 $score = '';
134 if (empty($scoid) && ($mode != 'normal')) {
135 $scoid = $sco->id;
137 if (isset($usertracks[$sco->identifier])) {
138 $usertrack = $usertracks[$sco->identifier];
139 $strstatus = get_string($usertrack->status,'scorm');
140 if ($sco->scormtype == 'sco') {
141 $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
142 } else {
143 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
146 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
147 $incomplete = true;
148 if ($play && empty($scoid)) {
149 $scoid = $sco->id;
152 if ($usertrack->score_raw != '') {
153 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
155 $strsuspended = get_string('suspended','scorm');
156 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
157 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
159 } else {
160 if ($play && empty($scoid)) {
161 $scoid = $sco->id;
163 if ($sco->scormtype == 'sco') {
164 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
165 $incomplete = true;
166 } else {
167 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
171 if ($sco->id == $scoid) {
172 $startbold = '<b>';
173 $endbold = '</b>';
174 $findnext = true;
175 $shownext = isset($optionaldatas[$sco->identifier]->next) ? $optionaldatas[$sco->identifier]->next : 0;
176 $showprev = isset($optionaldatas[$sco->identifier]->prev) ? $optionaldatas[$sco->identifier]->prev : 0;
179 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
180 if (!empty($sco->launch)) {
181 $previd = $sco->id;
184 require_once('sequencinglib.php');
185 if (scorm_seq_evaluate($sco->id,$usertracks)) {
186 if ($sco->id == $scoid) {
187 $result->prerequisites = true;
189 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
190 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
191 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
192 } else {
193 if ($sco->id == $scoid) {
194 $result->prerequisites = false;
196 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
199 } else {
200 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
202 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
203 if (!empty($nextsco->launch)) {
204 $nextid = $nextsco->id;
208 for ($i=0;$i<$level;$i++) {
209 $result->toc .= "\t\t</ul></li>\n";
212 if ($play) {
213 $sco = get_record('scorm_scoes','id',$scoid);
214 $sco->previd = $previd;
215 $sco->nextid = $nextid;
216 $result->sco = $sco;
217 $result->incomplete = $incomplete;
218 } else {
219 $result->incomplete = $incomplete;
222 $result->toc .= "\t</ul>\n";
223 if ($scorm->hidetoc == 0) {
224 $result->toc .= '
225 <script type="text/javascript">
226 //<![CDATA[
227 function expandCollide(which,list,item) {
228 var nn=document.ids?true:false
229 var w3c=document.getElementById?true:false
230 var beg=nn?"document.ids.":w3c?"document.getElementById(\'":"document.all.";
231 var mid=w3c?"\').style":".style";
233 which = which.substring(0,(which.length));
234 if (eval(beg+list+mid+".display") != "none") {
235 document.getElementById(which).src = "'.$scormpixdir.'/plus.gif";
236 eval(beg+list+mid+".display=\'none\';");
237 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
238 } else {
239 document.getElementById(which).src = "'.$scormpixdir.'/minus.gif";
240 eval(beg+list+mid+".display=\'block\';");
241 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
244 //]]>
245 </script>'."\n";
248 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
249 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
251 return $result;