Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / resource / type / ims / resource.class.php
blob4ce1c9262be0f681782e1103cd08c86fe01e1b11
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// Options presented to user :
28 /// (1) Side navigation menu (navigationmenu)
29 /// (2) TOC (tableofcontents)
30 /// (3) Navigation buttons (navigationbuttons)
31 /// (4) Navigation up button (navigationupbutton)
32 /// (5) Skip submenu pages (skipsubmenus)
33 ///
34 /// (1) forces (2), (4) false and (5) true. Forced on setup
35 /// (2) is a bit silly with (5). Maybe make a rule?
36 /// (3) false => (5) false. Add graying out on setup.
39 require_once($CFG->libdir.'/filelib.php');
40 require_once($CFG->dirroot.'/mod/resource/type/ims/repository_config.php');
43 /**
44 * Extend the base resource class for ims resources
46 class resource_ims extends resource_base {
48 var $parameters; //Attribute of this class where we'll store all the IMS deploy preferences
50 function resource_ims($cmid=0) {
51 /// super constructor
52 parent::resource_base($cmid);
54 /// prevent notice
55 if (empty($this->resource->alltext)) {
56 $this->resource->alltext='';
58 /// set own attributes
59 $this->parameters = $this->alltext2parameters($this->resource->alltext);
61 /// navigation menu forces other settings
62 if ($this->parameters->navigationmenu) {
63 $this->parameters->tableofcontents = 0;
64 $this->parameters->navigationuparrow = 0;
65 $this->parameters->skipsubmenus = 1;
68 /// Is it in the repository material or not?
69 if (isset($this->resource->reference)) {
70 $file = $this->resource->reference;
71 if ($file[0] == '#') {
72 $this->isrepository = true;
73 $file = ltrim($file, '#');
74 $this->resource->reference = $file;
75 } else {
76 $this->isrepository = false;
78 } else {
79 $this->isrepository = false;
83 /***
84 * This function converts parameters stored in the alltext field to the proper
85 * this->parameters object storing the special configuration of this resource type
87 function alltext2parameters($alltext) {
88 /// set parameter defaults
89 $alltextfield = new stdClass();
90 $alltextfield->tableofcontents=0;
91 $alltextfield->navigationbuttons=0;
92 $alltextfield->navigationmenu=1;
93 $alltextfield->skipsubmenus=1;
94 $alltextfield->navigationupbutton=1;
96 /// load up any stored parameters
97 if (!empty($alltext)) {
98 $parray = explode(',', $alltext);
99 foreach ($parray as $key => $fieldstring) {
100 $field = explode('=', $fieldstring);
101 $alltextfield->$field[0] = $field[1];
105 return $alltextfield;
108 /***
109 * This function converts the this->parameters attribute (object) to the format
110 * needed to save them in the alltext field to store all the special configuration
111 * of this resource type
113 function parameters2alltext($parameters) {
114 $optionlist = array();
116 $optionlist[] = 'tableofcontents='.$parameters->tableofcontents;
117 $optionlist[] = 'navigationbuttons='.$parameters->navigationbuttons;
118 $optionlist[] = 'skipsubmenus='.$parameters->skipsubmenus;
119 $optionlist[] = 'navigationmenu='.$parameters->navigationmenu;
120 $optionlist[] = 'navigationupbutton='.$parameters->navigationupbutton;
122 return implode(',', $optionlist);
125 /***
126 * This function will convert all the parameters configured in the resource form
127 * to a this->parameter attribute (object)
129 function form2parameters($resource) {
130 $parameters = new stdClass;
131 $parameters->tableofcontents = isset($resource->param_tableofcontents) ? $resource->param_tableofcontents : 0;
132 $parameters->navigationbuttons = $resource->param_navigationbuttons;
133 $parameters->skipsubmenus = isset($resource->param_skipsubmenus) ? $resource->param_skipsubmenus : 0;
134 $parameters->navigationmenu = $resource->param_navigationmenu;
135 $parameters->navigationupbutton = isset($resource->param_navigationupbutton) ? $resource->param_navigationupbutton : 0;
137 return $parameters;
140 /*** This function checks for errors in the status or deployment of the IMS
141 * Content Package returning an error code:
142 * 1 = Not a .zip file.
143 * 2 = Zip file doesn't exist
144 * 3 = Package not deployed.
145 * 4 = Package has changed since deployed.
146 * If the IMS CP is one from the central repository, then we instead check
147 * with the following codes:
148 * 5 = Not deployed. Since repository is central must be admin to deploy so terminate
150 function check4errors($file, $course, $resource) {
151 global $CFG;
153 if ($this->isrepository) {
154 /// Calculate the path were the IMS package must be deployed
155 $deploydir = $CFG->repository . $file;
157 /// Confirm that the IMS package has been deployed. These files must exist if
158 /// the package is deployed: moodle_index.ser and moodle_hash.ser
159 if (!file_exists($deploydir.'/moodle_inx.ser')) {
160 return 5; //Error
163 else {
164 /// Check for zip file type
165 $mimetype = mimeinfo("type", $file);
166 if ($mimetype != "application/zip") {
167 return 1; //Error
170 /// Check if the uploaded file exists
171 if (!file_exists($CFG->dataroot.'/'.$course->id.'/'.$file)) {
172 return 2; //Error
175 /// Calculate the path were the IMS package must be deployed
176 $deploydir = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id;
179 /// Confirm that the IMS package has been deployed. These files must exist if
180 /// the package is deployed: moodle_index.ser and moodle_hash.ser
181 if (!file_exists($deploydir.'/moodle_inx.ser') ||
182 !file_exists($deploydir.'/moodle_hash.ser')) {
183 return 3; //Error
186 /// If teacheredit, make, hash check. It's the md5 of the name of the file
187 /// plus its size and modification date
188 /// not sure if this capability is suitable
189 if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))) {
190 if (!$this->checkpackagehash($file, $course, $resource)) {
191 return 4;
196 /// We've arrived here. Everything is ok
197 return 0;
200 /*** This function will check that the ims package (zip file) uploaded
201 * isn't changed since it was deployed.
203 function checkpackagehash($file, $course, $resource) {
204 global $CFG;
206 /// Calculate paths
207 $zipfile = $CFG->dataroot.'/'.$course->id.'/'.$file;
208 $hashfile= $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id.'/moodle_hash.ser';
209 /// Get deloyed hash value
210 $f = fopen ($hashfile,'r');
211 $deployedhash = fread($f, filesize($hashfile));
212 fclose ($f);
213 /// Unserialize the deployed hash
214 $deployedhash = unserialize($deployedhash);
215 /// Calculate uploaded file hash value
216 $uploadedhash = $this->calculatefilehash($zipfile);
218 /// Compare them
219 return ($deployedhash == $uploadedhash);
222 /*** This function will calculate the hash of any file passes as argument.
223 * It's based in a md5 of the filename, filesize and 20 first bytes (it includes
224 * the zip CRC at byte 15).
226 function calculatefilehash($filefullpath) {
228 /// Name and size
229 $filename = basename($filefullpath);
230 $filesize = filesize($filefullpath);
231 /// Read first 20cc
232 $f = fopen ($filefullpath,'r');
233 $data = fread($f, 20);
234 fclose ($f);
236 return md5($filename.'-'.$filesize.'-'.$data);
240 * Add new instance of file resource
242 * Create alltext field before calling base class function.
244 * @param resource object
246 function add_instance($resource) {
247 $this->_postprocess($resource);
248 return parent::add_instance($resource);
253 * Update instance of file resource
255 * Create alltext field before calling base class function.
257 * @param resource object
259 function update_instance($resource) {
260 $this->_postprocess($resource);
261 return parent::update_instance($resource);
264 function _postprocess(&$resource) {
265 global $RESOURCE_WINDOW_OPTIONS;
266 $alloptions = $RESOURCE_WINDOW_OPTIONS;
268 if ($resource->windowpopup) {
269 $optionlist = array();
270 foreach ($alloptions as $option) {
271 $optionlist[] = $option."=".$resource->$option;
272 unset($resource->$option);
274 $resource->popup = implode(',', $optionlist);
275 unset($resource->windowpopup);
277 } else {
278 $resource->popup = '';
280 /// Load parameters to this->parameters
281 $this->parameters = $this->form2parameters($resource);
282 /// Save parameters into the alltext field
283 $resource->alltext = $this->parameters2alltext($this->parameters);
286 /** Delete instance of IMS-CP resource
288 * Delete all the moddata files for the resource
289 * @param resource object
291 function delete_instance($resource) {
293 global $CFG;
295 /// Delete moddata resource dir completely unless repository.
296 if (!$this->isrepository) {
297 $resource_dir = $CFG->dataroot.'/'.$resource->course.'/'.$CFG->moddata.'/resource/'.$resource->id;
298 if (file_exists($resource_dir)) {
299 if (!$status = fulldelete($resource_dir)) {
300 return false;
305 return parent::delete_instance($resource);
310 * Display the file resource
312 * Displays a file resource embedded, in a frame, or in a popup.
313 * Output depends on type of file resource.
315 * @param CFG global object
317 function display() {
318 global $CFG, $THEME, $USER;
320 require_once($CFG->libdir.'/filelib.php');
322 /// Set up generic stuff first, including checking for access
323 parent::display();
325 /// Set up some shorthand variables
326 $cm = $this->cm;
327 $course = $this->course;
328 $resource = $this->resource;
330 /// Fetch parameters
331 $inpopup = optional_param('inpopup', 0, PARAM_BOOL);
332 $page = optional_param('page', 0, PARAM_INT);
333 $frameset= optional_param('frameset', '', PARAM_ALPHA);
335 /// Init some variables
336 $errorcode = 0;
337 $buttontext = 0;
338 $querystring = '';
339 $resourcetype = '';
340 $mimetype = mimeinfo("type", $resource->reference);
341 $pagetitle = strip_tags($course->shortname.': '.format_string($resource->name));
343 $formatoptions = new object();
344 $formatoptions->noclean = true;
346 /// Cache this per request
347 static $items;
349 /// Check for errors
350 $errorcode = $this->check4errors($resource->reference, $course, $resource);
352 /// If there are any error, show it instead of the resource page
353 if ($errorcode) {
354 if (!has_capability('moodle/course:activityvisibility', get_context_instance(CONTEXT_COURSE, $course->id))) {
355 /// Resource not available page
356 $errortext = get_string('resourcenotavailable','resource');
357 } else {
358 /// Depending of the error, show different messages and pages
359 if ($errorcode ==1) {
360 $errortext = get_string('invalidfiletype','error', $resource->reference);
361 } else if ($errorcode == 2) {
362 $errortext = get_string('filenotfound','error', $resource->reference);
363 } else if ($errorcode == 3) {
364 $errortext = get_string('packagenotdeplyed','resource');
365 } else if ($errorcode == 4) {
366 $errortext = get_string('packagechanged','resource');
367 } else if ($errorcode == 5) {
368 $errortext = get_string('packagenotdeplyed','resource'); // no button though since from repository.
371 /// Display the error and exit
372 if ($inpopup) {
373 print_header($pagetitle, $course->fullname.' : '.$resource->name);
374 } else {
376 $this->navlinks[] = array('name' => format_string($resource->name), 'link' => '', 'type' => 'activityinstance');
377 $this->navigation = build_navigation($this->navlinks);
379 print_header($pagetitle, $course->fullname, $this->navigation, "", "", true, update_module_button($cm->id, $course->id, $this->strresource), navmenu($course, $cm));
381 print_simple_box_start('center', '60%');
382 echo '<p align="center">'.$errortext.'</p>';
383 /// If errors were 3 or 4 and isteacheredit(), show the deploy button
384 if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) && ($errorcode == 3 || $errorcode == 4)) {
385 $link = 'type/ims/deploy.php';
386 $options['courseid'] = $course->id;
387 $options['cmid'] = $cm->id;
388 $options['file'] = $resource->reference;
389 $options['sesskey'] = $USER->sesskey;
390 $options['inpopup'] = $inpopup;
391 if ($errorcode == 3) {
392 $label = get_string ('deploy', 'resource');
393 } else if ($errorcode == 4) {
394 $label = get_string ('redeploy', 'resource');
396 $method='post';
397 /// Let's go with the button
398 echo '<center>';
399 print_single_button($link, $options, $label, $method);
400 echo '</center>';
402 print_simple_box_end();
403 /// Close button if inpopup
404 if ($inpopup) {
405 close_window_button();
408 print_footer();
409 exit;
412 /// Load serialized IMS CP index to memory only once.
413 if (empty($items)) {
414 if (!$this->isrepository) {
415 $resourcedir = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id;
417 else {
418 $resourcedir = $CFG->repository . $resource->reference;
420 if (!$items = ims_load_serialized_file($resourcedir.'/moodle_inx.ser')) {
421 error (get_string('errorreadingfile', 'error', 'moodle_inx.ser'));
425 /// Check whether this is supposed to be a popup, but was called directly
427 if (empty($frameset) && $resource->popup && !$inpopup) { /// Make a page and a pop-up window
429 print_header($pagetitle, $course->fullname, "$this->navigation ".format_string($resource->name), "", "", true, update_module_button($cm->id, $course->id, $this->strresource), navmenu($course, $cm));
431 echo "\n<script type=\"text/javascript\">";
432 echo "\n<!--\n";
433 echo "openpopup('/mod/resource/view.php?inpopup=true&id={$cm->id}','resource{$resource->id}','{$resource->popup}');\n";
434 echo "\n-->\n";
435 echo '</script>';
437 if (trim(strip_tags($resource->summary))) {
438 print_simple_box(format_text($resource->summary, FORMAT_MOODLE, $formatoptions), "center");
441 $link = "<a href=\"$CFG->wwwroot/mod/resource/view.php?inpopup=true&amp;id={$cm->id}\" target=\"resource{$resource->id}\" onclick=\"return openpopup('/mod/resource/view.php?inpopup=true&amp;id={$cm->id}', 'resource{$resource->id}','{$resource->popup}');\">".format_string($resource->name,true)."</a>";
443 echo "<p>&nbsp;</p>";
444 echo '<p align="center">';
445 print_string('popupresource', 'resource');
446 echo '<br />';
447 print_string('popupresourcelink', 'resource', $link);
448 echo "</p>";
450 print_footer($course);
451 exit;
455 /// No frames or framesets anymore, except iframe. in print_ims, iframe filled.
456 /// needs callback to this file to display table of contents in the iframe so
457 /// $frameset = 'toc' leads to output of toc and blank or 'ims' produces the
458 /// iframe.
459 if (empty($frameset) || $frameset=='ims') {
461 /// Conditional argument to pass to IMS JavaScript. Need to be global to retrieve it from our custom javascript! :-(
462 global $jsarg;
463 $jsarg = 'false';
464 if (!empty($this->parameters->navigationmenu)) {
465 $jsarg = 'true';
467 /// Define $CFG->javascript to use our custom javascript. Save the original one to add it from ours. Global too! :-(
468 global $standard_javascript;
469 $standard_javascript = $CFG->javascript; // Save original javascript file
470 $CFG->javascript = $CFG->dirroot.'/mod/resource/type/ims/javascript.php'; //Use our custom IMS javascript code
472 /// moodle header
473 if ($resource->popup) {
474 //print_header($pagetitle, $course->fullname.' : '.$resource->name);
475 print_header();
476 } else {
477 $this->navlinks[] = array('name' => format_string($resource->name), 'link' => '', 'type' => 'activityinstance');
478 $this->navigation = build_navigation($this->navlinks);
479 print_header($pagetitle, $course->fullname, $this->navigation, "", "", true, update_module_button($cm->id, $course->id, $this->strresource), navmenu($course, $cm, "parent"));
481 /// content - this produces everything else
482 $this->print_ims($cm, $course, $items, $resource, $page);
483 /// Moodle footer is back! Now using the DOMContentLoaded event (see resize.js) to trigger the resize
484 /// no Moodle footer (because we cannot insert there the resize script).
485 /// echo "</div></div><script type=\"text/javascript\">resizeiframe($jsarg);</script></body></html>";
486 /// print_footer();
487 echo "</div></div></body></html>";
489 /// log it.
490 add_to_log($course->id, "resource", "view", "view.php?id={$cm->id}", $resource->id, $cm->id);
491 exit;
494 if ($frameset == 'toc') {
495 print_header();
496 $this->print_toc($items, $resource, $page);
497 echo '</div></div></body></html>';
498 exit;
502 /// Function print_ims prints nearly the whole page. Stupid name subject to change :-)
503 function print_ims($cm, $course, $items, $resource, $page) {
504 global $CFG;
506 /// Set the correct contentframe id based on $this->parameters->navigationmenu
507 if (!empty($this->parameters->navigationmenu)) {
508 $contentframe = 'ims-contentframe';
509 } else {
510 $contentframe = 'ims-contentframe-no-nav';
513 /// Calculate the file.php correct url
514 if (!$this->isrepository) {
515 if ($CFG->slasharguments) {
516 $fileurl = "{$CFG->wwwroot}/file.php/{$course->id}/{$CFG->moddata}/resource/{$resource->id}";
517 } else {
518 $fileurl = "{$CFG->wwwroot}/file.php?file=/{$course->id}/{$CFG->moddata}/resource/{$resource->id}";
521 else {
522 $fileurl = $CFG->repositorywebroot . $resource->reference;
526 /// Calculate the view.php correct url
527 $viewurl = "view.php?id={$cm->id}&amp;type={$resource->type}&amp;frameset=toc&amp;page=";
530 /// Decide what to show (full toc, partial toc or package file)
531 $fullurl = '';
532 if (empty($page) && !empty($this->parameters->tableofcontents)) {
533 /// Full toc contents
534 $fullurl = $viewurl.$page;
535 } else {
536 if (empty($page)) {
537 /// If no page and no toc, set page 1 unless skipping submenus, in which case fast forward:
538 $page = 1;
539 if (!empty($this->parameters->skipsubmenus)) {
540 while (empty($items[$page]->href) && !empty($items[$page])) {
541 $page++;
545 if (empty($items[$page]->href)) {
546 /// The page hasn't href, then partial toc contents
547 $fullurl = $viewurl.$page;
548 } else {
549 /// The page has href, then its own file contents
550 /// but considering if it seems to be an external url or a internal one
551 if (strpos($items[$page]->href, '//') !== false) {
552 /// External URL
553 $fullurl = $items[$page]->href;
554 } else {
555 /// Internal URL, use file.php
556 $fullurl = $fileurl.'/'.$items[$page]->href;
561 /// print navigation buttons if needed
562 if (!empty($this->parameters->navigationbuttons)) {
563 $this->print_nav($items, $resource, $page);
566 echo '<div id="ims-containerdiv">';
567 /// adds side navigation bar if needed. must also adjust width of iframe to accomodate
568 if (!empty($this->parameters->navigationmenu)) {
569 echo "<div id=\"ims-menudiv\">"; $this->print_navmenu($items, $resource, $page); echo "</div>";
572 /// prints iframe filled with $fullurl
573 echo "<iframe id=\"".$contentframe."\" name=\"".$contentframe."\" src=\"{$fullurl}\" title=\"".get_string('modulename','resource')."\">Your browser does not support inline frames or is currently configured not to display inline frames. Content can be viewed at {$fullurl}</iframe>"; //Content frame
574 echo '</div>';
577 /// Prints TOC
578 function print_toc($items, $resource, $page) {
579 $table = new stdClass;
580 if (empty($page)) {
581 $table->head[] = '<b>'.$resource->name.'</b>';
582 } else {
583 $table->head[] = '<b>'.$items[$page]->title.'</b>';
585 $table->data[] = array(ims_generate_toc ($items, $resource, $page));
586 $table->width = '60%';
587 print_table($table);
590 /// Prints side navigation menu. This is just the full TOC with no surround.
591 function print_navmenu($items, $resource, $page=0) {
592 echo ims_generate_toc ($items, $resource, 0, $page);
595 /// Prints navigation bar at the top of the page.
596 function print_nav($items, $resource, $page) {
597 echo '<div class="ims-nav-bar" id="ims-nav-bar">';
598 /// Prev button
599 echo ims_get_prev_nav_button ($items, $this, $page);
600 /// Up button
601 echo ims_get_up_nav_button ($items, $this, $page);
602 /// Next button
603 echo ims_get_next_nav_button ($items, $this, $page);
604 /// Main TOC button
605 echo ims_get_toc_nav_button ($items, $this, $page);
606 /// Footer
607 echo '</div>';
611 function setup_preprocessing(&$defaults){
613 if (!isset($defaults['popup'])) {
614 // use form defaults
616 } else if (!empty($defaults['popup'])) {
617 $defaults['windowpopup'] = 1;
618 if (array_key_exists('popup', $defaults)) {
619 $rawoptions = explode(',', $defaults['popup']);
620 foreach ($rawoptions as $rawoption) {
621 $option = explode('=', trim($rawoption));
622 $defaults[$option[0]] = $option[1];
625 } else {
626 $defaults['windowpopup'] = 0;
628 //Converts the alltext to form fields
629 if (!empty($defaults['alltext'])) {
630 $parameters = $this->alltext2parameters($defaults['alltext']);
631 $defaults['param_tableofcontents'] = $parameters->tableofcontents;
632 $defaults['param_navigationbuttons'] = $parameters->navigationbuttons;
633 $defaults['param_skipsubmenus'] = $parameters->skipsubmenus;
634 $defaults['param_navigationmenu'] = $parameters->navigationmenu;
635 $defaults['param_navigationupbutton'] = $parameters->navigationupbutton;
639 function setup_elements(&$mform) {
640 global $CFG, $RESOURCE_WINDOW_OPTIONS;
642 $mform->addElement('choosecoursefileorimsrepo', 'reference', get_string('location'));
643 $mform->addRule('name', null, 'required', null, 'client');
645 $mform->addElement('header', 'displaysettings', get_string('display', 'resource'));
647 $woptions = array(0 => get_string('pagewindow', 'resource'), 1 => get_string('newwindow', 'resource'));
648 $mform->addElement('select', 'windowpopup', get_string('display', 'resource'), $woptions);
649 $mform->setDefault('windowpopup', !empty($CFG->resource_popup));
651 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
652 if ($option == 'height' or $option == 'width') {
653 $mform->addElement('text', $option, get_string('new'.$option, 'resource'), array('size'=>'4'));
654 $mform->setDefault($option, $CFG->{'resource_popup'.$option});
655 $mform->disabledIf($option, 'windowpopup', 'eq', 0);
656 } else {
657 $mform->addElement('checkbox', $option, get_string('new'.$option, 'resource'));
658 $mform->setDefault($option, $CFG->{'resource_popup'.$option});
659 $mform->disabledIf($option, 'windowpopup', 'eq', 0);
661 $mform->setAdvanced($option);
664 $mform->addElement('header', 'parameters', get_string('parameters', 'resource'));
666 $mform->addElement('selectyesno', 'param_navigationmenu', get_string('navigationmenu', 'resource'));
667 $mform->setDefault('param_navigationmenu', 1);
669 $mform->addElement('selectyesno', 'param_tableofcontents', get_string('tableofcontents', 'resource'));
670 $mform->disabledIf('param_tableofcontents', 'param_navigationmenu', 'eq', 1);
671 $mform->setDefault('param_tableofcontents', 0);
673 $mform->addElement('selectyesno', 'param_navigationbuttons', get_string('navigationbuttons', 'resource'));
674 $mform->setDefault('param_navigationbuttons', 0);
676 $mform->addElement('selectyesno', 'param_skipsubmenus', get_string('skipsubmenus', 'resource'));
677 $mform->setDefault('param_skipsubmenus', 1);
678 $mform->disabledIf('param_skipsubmenus', 'param_navigationmenu', 'eq', 1);
680 $mform->addElement('selectyesno', 'param_navigationupbutton', get_string('navigationup', 'resource'));
681 $mform->setDefault('param_navigationupbutton', 1);
682 $mform->disabledIf('param_navigationupbutton', 'param_navigationmenu', 'eq', 1);
686 } //End class
689 /// General purpose functions
691 /*** This function will serialize the variable passed and send it
692 * to filesystem
694 function ims_save_serialized_file($destination, $var) {
695 $status = false;
696 if ($ser = serialize($var)) {
697 $status = ims_var2file($destination, $ser);
699 return $status;
702 /*** This function will unserialize the variable stored
703 * in filesystem
705 function ims_load_serialized_file($file) {
706 $status = false;
707 if ($ser = ims_file2var($file)) {
708 $status = unserialize($ser);
710 return $status;
713 /*** This function will load all the contents of one file to one variable
714 * Not suitable for BIG files
716 function ims_file2var ($file) {
717 $status = true;
718 $var = '';
719 $fp = fopen($file, 'r')
720 or $status = false;
721 if ($status) {
722 while ($data = fread($fp, 4096)) {
723 $var = $var.$data;
725 fclose($fp);
727 if (!$status) {
728 $var = false;
730 return $var;
733 /*** This file will write the contents of one variable to a file
734 * Not suitable for BIG files
736 function ims_var2file ($file, $var) {
737 $status = false;
738 if ($out = fopen($file,"w")) {
739 $status = fwrite($out, $var);
740 fclose($out);
742 return $status;
745 /*** This function will generate the TOC file for the package
746 * from an specified parent to be used in the view of the IMS
747 * Now hilights 'selected page' also.
749 function ims_generate_toc($items, $resource, $page=0, $selected_page = -1) {
750 global $CFG;
752 $contents = '';
754 /// Configure links behaviour
755 $fullurl = $CFG->wwwroot.'/mod/resource/view.php?r='.$resource->id.'&amp;frameset=ims&amp;page=';
757 /// Iterate over items to build the menu
758 $currlevel = 0;
759 $currorder = 0;
760 $endlevel = 0;
761 $openlielement = false;
762 foreach ($items as $item) {
763 if (!is_object($item)) {
764 continue;
766 /// Skip pages until we arrive to $page
767 if ($item->id < $page) {
768 continue;
770 /// Arrive to page, we store its level
771 if ($item->id == $page) {
772 $endlevel = $item->level;
773 continue;
775 /// We are after page and inside it (level > endlevel)
776 if ($item->id > $page && $item->level > $endlevel) {
777 /// Start Level
778 if ($item->level > $currlevel) {
779 $contents .= '<ol class="listlevel_'.$item->level.'">';
780 $openlielement = false;
782 /// End Level
783 if ($item->level < $currlevel) {
784 $contents .= '</li>';
785 $contents .= '</ol>';
787 /// If we have some openlielement, just close it
788 if ($openlielement) {
789 $contents .= '</li>';
791 /// Add item
792 $contents .= '<li>';
793 if (!empty($item->href)) {
794 if ($item->id == $selected_page) $contents .= '<div id="ims-toc-selected">';
795 $contents .= '<a href="'.$fullurl.$item->id.'" target="_parent">'.$item->title.'</a>';
796 if ($item->id == $selected_page) $contents .= '</div>';
797 } else {
798 $contents .= $item->title;
800 $currlevel = $item->level;
801 $openlielement = true;
802 continue;
804 /// We have reached endlevel, exit
805 if ($item->id > $page && $item->level <= $endlevel) {
806 break;
809 /// Close up to $endlevel
810 for ($i=$currlevel;$i>$endlevel;$i--) {
811 $contents .= '</li>';
812 $contents .= '</ol>';
815 return $contents;
818 /*** This function will return the correct html code needed
819 * to show the previous button in the nav frame
821 function ims_get_prev_nav_button ($items, $resource_obj, $page) {
822 $strprevious = get_string("previous", "resource");
824 $cm = $resource_obj->cm;
825 $resource = $resource_obj->resource;
827 $contents = '';
829 $page--;
830 /// Skips any menu pages since these are redundant with sidemenu.
831 if (!empty($resource_obj->parameters->skipsubmenus)) {
832 while(empty($items[$page]->href) && $page >= 0) {
833 $page--;
837 if ($page >= 1 ) { //0 and 1 pages haven't previous
838 $contents .= "<span class=\"ims-nav-button\"><a href=\"view.php?id={$cm->id}&amp;type={$resource->type}&amp;page={$page}&amp;frameset=ims\">$strprevious</a></span>";
839 } else {
840 $contents .= '<span class="ims-nav-dimmed">'.$strprevious.'</span>';
843 return $contents;
846 /*** This function will return the correct html code needed
847 * to show the next button in the nav frame
849 function ims_get_next_nav_button ($items, $resource_obj, $page) {
850 $strnext = get_string("next", "resource");
852 $cm = $resource_obj->cm;
853 $resource = $resource_obj->resource;
855 $contents = '';
857 $page++;
858 /// Skips any menu pages since these are redundant with sidemenu.
859 if (!empty($resource_obj->parameters->skipsubmenus)) {
860 while(empty($items[$page]->href) && !empty($items[$page])) {
861 $page++;
865 if (!empty($items[$page])) { //If the next page exists
866 $contents .= "<span class=\"ims-nav-button\"><a href=\"view.php?id={$cm->id}&amp;type={$resource->type}&amp;page={$page}&amp;frameset=ims\">$strnext</a></span>";
867 } else {
868 $contents .= '<span class="ims-nav-dimmed">'.$strnext.'</span>';
872 return $contents;
875 /*** This function will return the correct html code needed
876 * to show the up button in the nav frame
878 function ims_get_up_nav_button ($items, $resource_obj, $page) {
879 $strup = get_string("upbutton", "resource");
881 $cm = $resource_obj->cm;
882 $resource = $resource_obj->resource;
884 $contents = '';
886 if (!empty($resource_obj->parameters->navigationupbutton)) {
887 if ($page > 1 && $items[$page]->parent > 0) { //If the page has parent
888 $page = $items[$page]->parent;
889 $contents .= "<span class=\"ims-nav-button\"><a href=\"view.php?id={$cm->id}&amp;type={$resource->type}&amp;page={$page}&amp;frameset=ims\">$strup</a></span>";
890 } else {
891 $contents .= "<span class=\"ims-nav-dimmed\">$strup</span>";
894 return $contents;
897 /*** This function will return the correct html code needed
898 * to show the toc button in the nav frame
900 function ims_get_toc_nav_button ($items, $resource_obj, $page) {
902 $cm = $resource_obj->cm;
903 $resource = $resource_obj->resource;
905 $strtoc = get_string('toc', 'resource');
907 $contents = '';
909 if (!empty($resource_obj->parameters->tableofcontents)) { //The toc is enabled
910 $page = 0;
911 $contents .= "<span class=\"ims-nav-button\"><a href=\"view.php?id={$cm->id}&amp;type={$resource->type}&amp;page={$page}&amp;frameset=ims\">TOC</a></span>";
914 return $contents;