Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / resource / type / ims / resource.class.php
blob0ab7c2ba667ce3ee2cd687b8e01680a2a523b5d0
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) 1999 onwards 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 {
375 $navigation = build_navigation($this->navlinks, $cm);
376 print_header($pagetitle, $course->fullname, $navigation, "", "", true,
377 update_module_button($cm->id, $course->id, $this->strresource), navmenu($course, $cm));
379 print_simple_box_start('center', '60%');
380 echo '<p align="center">'.$errortext.'</p>';
381 /// If errors were 3 or 4 and isteacheredit(), show the deploy button
382 if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) && ($errorcode == 3 || $errorcode == 4)) {
383 $link = 'type/ims/deploy.php';
384 $options['courseid'] = $course->id;
385 $options['cmid'] = $cm->id;
386 $options['file'] = $resource->reference;
387 $options['sesskey'] = $USER->sesskey;
388 $options['inpopup'] = $inpopup;
389 if ($errorcode == 3) {
390 $label = get_string ('deploy', 'resource');
391 } else if ($errorcode == 4) {
392 $label = get_string ('redeploy', 'resource');
394 $method='post';
395 /// Let's go with the button
396 echo '<center>';
397 print_single_button($link, $options, $label, $method);
398 echo '</center>';
400 print_simple_box_end();
401 /// Close button if inpopup
402 if ($inpopup) {
403 close_window_button();
406 print_footer();
407 exit;
410 /// Load serialized IMS CP index to memory only once.
411 if (empty($items)) {
412 if (!$this->isrepository) {
413 $resourcedir = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id;
415 else {
416 $resourcedir = $CFG->repository . $resource->reference;
418 if (!$items = ims_load_serialized_file($resourcedir.'/moodle_inx.ser')) {
419 error (get_string('errorreadingfile', 'error', 'moodle_inx.ser'));
423 /// Check whether this is supposed to be a popup, but was called directly
425 if (empty($frameset) && $resource->popup && !$inpopup) { /// Make a page and a pop-up window
426 $navigation = build_navigation($this->navlinks, $cm);
428 print_header($pagetitle, $course->fullname, $navigation, "", "", true,
429 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 $navigation = build_navigation($this->navlinks, $cm);
478 print_header($pagetitle, $course->fullname, $navigation, "", "", true, update_module_button($cm->id, $course->id, $this->strresource), navmenu($course, $cm, "parent"));
480 /// content - this produces everything else
481 $this->print_ims($cm, $course, $items, $resource, $page);
483 print_footer('empty');
485 /// log it.
486 add_to_log($course->id, "resource", "view", "view.php?id={$cm->id}", $resource->id, $cm->id);
487 exit;
490 if ($frameset == 'toc') {
491 print_header();
492 $this->print_toc($items, $resource, $page);
493 echo '</div></div></body></html>';
494 exit;
498 /// Function print_ims prints nearly the whole page. Stupid name subject to change :-)
499 function print_ims($cm, $course, $items, $resource, $page) {
500 global $CFG;
502 /// Set the correct contentframe id based on $this->parameters->navigationmenu
503 if (!empty($this->parameters->navigationmenu)) {
504 $contentframe = 'ims-contentframe';
505 } else {
506 $contentframe = 'ims-contentframe-no-nav';
509 /// Calculate the file.php correct url
510 if (!$this->isrepository) {
511 require_once($CFG->libdir.'/filelib.php');
512 $fileurl = get_file_url($course->id.'/'.$CFG->moddata.'/resource/'.$resource->id);
514 else {
515 $fileurl = $CFG->repositorywebroot . $resource->reference;
519 /// Calculate the view.php correct url
520 $viewurl = "view.php?id={$cm->id}&amp;type={$resource->type}&amp;frameset=toc&amp;page=";
523 /// Decide what to show (full toc, partial toc or package file)
524 $fullurl = '';
525 if (empty($page) && !empty($this->parameters->tableofcontents)) {
526 /// Full toc contents
527 $fullurl = $viewurl.$page;
528 } else {
529 if (empty($page)) {
530 /// If no page and no toc, set page 1 unless skipping submenus, in which case fast forward:
531 $page = 1;
532 if (!empty($this->parameters->skipsubmenus)) {
533 while (empty($items[$page]->href) && !empty($items[$page])) {
534 $page++;
538 if (empty($items[$page]->href)) {
539 /// The page hasn't href, then partial toc contents
540 $fullurl = $viewurl.$page;
541 } else {
542 /// The page has href, then its own file contents
543 /// but considering if it seems to be an external url or a internal one
544 if (strpos($items[$page]->href, '//') !== false) {
545 /// External URL
546 $fullurl = $items[$page]->href;
547 } else {
548 /// Internal URL, use file.php
549 $fullurl = $fileurl.'/'.$items[$page]->href;
554 /// print navigation buttons if needed
555 if (!empty($this->parameters->navigationbuttons)) {
556 $this->print_nav($items, $resource, $page);
559 echo '<div id="ims-containerdiv">';
560 /// adds side navigation bar if needed. must also adjust width of iframe to accomodate
561 if (!empty($this->parameters->navigationmenu)) {
562 echo "<div id=\"ims-menudiv\">"; $this->print_navmenu($items, $resource, $page); echo "</div>";
565 /// prints iframe filled with $fullurl
566 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
567 echo '</div>';
570 /// Prints TOC
571 function print_toc($items, $resource, $page) {
572 $table = new stdClass;
573 if (empty($page)) {
574 $table->head[] = '<b>'.$resource->name.'</b>';
575 } else {
576 $table->head[] = '<b>'.$items[$page]->title.'</b>';
578 $table->data[] = array(ims_generate_toc ($items, $resource, $page));
579 $table->width = '60%';
580 print_table($table);
583 /// Prints side navigation menu. This is just the full TOC with no surround.
584 function print_navmenu($items, $resource, $page=0) {
585 echo ims_generate_toc ($items, $resource, 0, $page);
588 /// Prints navigation bar at the top of the page.
589 function print_nav($items, $resource, $page) {
590 echo '<div class="ims-nav-bar" id="ims-nav-bar">';
591 /// Prev button
592 echo ims_get_prev_nav_button ($items, $this, $page);
593 /// Up button
594 echo ims_get_up_nav_button ($items, $this, $page);
595 /// Next button
596 echo ims_get_next_nav_button ($items, $this, $page);
597 /// Main TOC button
598 echo ims_get_toc_nav_button ($items, $this, $page);
599 /// Footer
600 echo '</div>';
604 function setup_preprocessing(&$defaults){
606 if (!isset($defaults['popup'])) {
607 // use form defaults
609 } else if (!empty($defaults['popup'])) {
610 $defaults['windowpopup'] = 1;
611 if (array_key_exists('popup', $defaults)) {
612 $rawoptions = explode(',', $defaults['popup']);
613 foreach ($rawoptions as $rawoption) {
614 $option = explode('=', trim($rawoption));
615 $defaults[$option[0]] = $option[1];
618 } else {
619 $defaults['windowpopup'] = 0;
621 //Converts the alltext to form fields
622 if (!empty($defaults['alltext'])) {
623 $parameters = $this->alltext2parameters($defaults['alltext']);
624 $defaults['param_tableofcontents'] = $parameters->tableofcontents;
625 $defaults['param_navigationbuttons'] = $parameters->navigationbuttons;
626 $defaults['param_skipsubmenus'] = $parameters->skipsubmenus;
627 $defaults['param_navigationmenu'] = $parameters->navigationmenu;
628 $defaults['param_navigationupbutton'] = $parameters->navigationupbutton;
632 function setup_elements(&$mform) {
633 global $CFG, $RESOURCE_WINDOW_OPTIONS;
635 $mform->addElement('choosecoursefileorimsrepo', 'reference', get_string('location'));
636 $mform->addRule('name', null, 'required', null, 'client');
638 $mform->addElement('header', 'displaysettings', get_string('display', 'resource'));
640 $woptions = array(0 => get_string('pagewindow', 'resource'), 1 => get_string('newwindow', 'resource'));
641 $mform->addElement('select', 'windowpopup', get_string('display', 'resource'), $woptions);
642 $mform->setDefault('windowpopup', !empty($CFG->resource_popup));
644 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
645 if ($option == 'height' or $option == 'width') {
646 $mform->addElement('text', $option, get_string('new'.$option, 'resource'), array('size'=>'4'));
647 $mform->setDefault($option, $CFG->{'resource_popup'.$option});
648 $mform->disabledIf($option, 'windowpopup', 'eq', 0);
649 } else {
650 $mform->addElement('checkbox', $option, get_string('new'.$option, 'resource'));
651 $mform->setDefault($option, $CFG->{'resource_popup'.$option});
652 $mform->disabledIf($option, 'windowpopup', 'eq', 0);
654 $mform->setAdvanced($option);
657 $mform->addElement('header', 'parameters', get_string('parameters', 'resource'));
659 $mform->addElement('selectyesno', 'param_navigationmenu', get_string('navigationmenu', 'resource'));
660 $mform->setDefault('param_navigationmenu', 1);
662 $mform->addElement('selectyesno', 'param_tableofcontents', get_string('tableofcontents', 'resource'));
663 $mform->disabledIf('param_tableofcontents', 'param_navigationmenu', 'eq', 1);
664 $mform->setDefault('param_tableofcontents', 0);
666 $mform->addElement('selectyesno', 'param_navigationbuttons', get_string('navigationbuttons', 'resource'));
667 $mform->setDefault('param_navigationbuttons', 0);
669 $mform->addElement('selectyesno', 'param_skipsubmenus', get_string('skipsubmenus', 'resource'));
670 $mform->setDefault('param_skipsubmenus', 1);
671 $mform->disabledIf('param_skipsubmenus', 'param_navigationmenu', 'eq', 1);
673 $mform->addElement('selectyesno', 'param_navigationupbutton', get_string('navigationup', 'resource'));
674 $mform->setDefault('param_navigationupbutton', 1);
675 $mform->disabledIf('param_navigationupbutton', 'param_navigationmenu', 'eq', 1);
679 } //End class
682 /// General purpose functions
684 /*** This function will serialize the variable passed and send it
685 * to filesystem
687 function ims_save_serialized_file($destination, $var) {
688 $status = false;
689 if ($ser = serialize($var)) {
690 $status = ims_var2file($destination, $ser);
692 return $status;
695 /*** This function will unserialize the variable stored
696 * in filesystem
698 function ims_load_serialized_file($file) {
699 $status = false;
700 if ($ser = ims_file2var($file)) {
701 $status = unserialize($ser);
703 return $status;
706 /*** This function will load all the contents of one file to one variable
707 * Not suitable for BIG files
709 function ims_file2var ($file) {
710 $status = true;
711 $var = '';
712 $fp = fopen($file, 'r')
713 or $status = false;
714 if ($status) {
715 while ($data = fread($fp, 4096)) {
716 $var = $var.$data;
718 fclose($fp);
720 if (!$status) {
721 $var = false;
723 return $var;
726 /*** This file will write the contents of one variable to a file
727 * Not suitable for BIG files
729 function ims_var2file ($file, $var) {
730 $status = false;
731 if ($out = fopen($file,"w")) {
732 $status = fwrite($out, $var);
733 fclose($out);
735 return $status;
738 /*** This function will generate the TOC file for the package
739 * from an specified parent to be used in the view of the IMS
740 * Now hilights 'selected page' also.
742 function ims_generate_toc($items, $resource, $page=0, $selected_page = -1) {
743 global $CFG;
745 $contents = '';
747 /// Configure links behaviour
748 $fullurl = $CFG->wwwroot.'/mod/resource/view.php?r='.$resource->id.'&amp;frameset=ims&amp;page=';
750 /// Iterate over items to build the menu
751 $currlevel = 0;
752 $currorder = 0;
753 $endlevel = 0;
754 $openlielement = false;
755 foreach ($items as $item) {
756 if (!is_object($item)) {
757 continue;
759 /// Skip pages until we arrive to $page
760 if ($item->id < $page) {
761 continue;
763 /// Arrive to page, we store its level
764 if ($item->id == $page) {
765 $endlevel = $item->level;
766 continue;
768 /// We are after page and inside it (level > endlevel)
769 if ($item->id > $page && $item->level > $endlevel) {
770 /// Start Level
771 if ($item->level > $currlevel) {
772 $contents .= '<ol class="listlevel_'.$item->level.'">';
773 $openlielement = false;
775 /// End Level
776 if ($item->level < $currlevel) {
777 $contents .= '</li>';
778 $contents .= '</ol>';
780 /// If we have some openlielement, just close it
781 if ($openlielement) {
782 $contents .= '</li>';
784 /// Add item
785 $contents .= '<li>';
786 if (!empty($item->href)) {
787 if ($item->id == $selected_page) $contents .= '<div id="ims-toc-selected">';
788 $contents .= '<a href="'.$fullurl.$item->id.'" target="_parent">'.$item->title.'</a>';
789 if ($item->id == $selected_page) $contents .= '</div>';
790 } else {
791 $contents .= $item->title;
793 $currlevel = $item->level;
794 $openlielement = true;
795 continue;
797 /// We have reached endlevel, exit
798 if ($item->id > $page && $item->level <= $endlevel) {
799 break;
802 /// Close up to $endlevel
803 for ($i=$currlevel;$i>$endlevel;$i--) {
804 $contents .= '</li>';
805 $contents .= '</ol>';
808 return $contents;
811 /*** This function will return the correct html code needed
812 * to show the previous button in the nav frame
814 function ims_get_prev_nav_button ($items, $resource_obj, $page) {
815 $strprevious = get_string("previous", "resource");
817 $cm = $resource_obj->cm;
818 $resource = $resource_obj->resource;
820 $contents = '';
822 $page--;
823 /// Skips any menu pages since these are redundant with sidemenu.
824 if (!empty($resource_obj->parameters->skipsubmenus)) {
825 while(empty($items[$page]->href) && $page >= 0) {
826 $page--;
830 if ($page >= 1 ) { //0 and 1 pages haven't previous
831 $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>";
832 } else {
833 $contents .= '<span class="ims-nav-dimmed">'.$strprevious.'</span>';
836 return $contents;
839 /*** This function will return the correct html code needed
840 * to show the next button in the nav frame
842 function ims_get_next_nav_button ($items, $resource_obj, $page) {
843 $strnext = get_string("next", "resource");
845 $cm = $resource_obj->cm;
846 $resource = $resource_obj->resource;
848 $contents = '';
850 $page++;
851 /// Skips any menu pages since these are redundant with sidemenu.
852 if (!empty($resource_obj->parameters->skipsubmenus)) {
853 while(empty($items[$page]->href) && !empty($items[$page])) {
854 $page++;
858 if (!empty($items[$page])) { //If the next page exists
859 $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>";
860 } else {
861 $contents .= '<span class="ims-nav-dimmed">'.$strnext.'</span>';
865 return $contents;
868 /*** This function will return the correct html code needed
869 * to show the up button in the nav frame
871 function ims_get_up_nav_button ($items, $resource_obj, $page) {
872 $strup = get_string("upbutton", "resource");
874 $cm = $resource_obj->cm;
875 $resource = $resource_obj->resource;
877 $contents = '';
879 if (!empty($resource_obj->parameters->navigationupbutton)) {
880 if ($page > 1 && $items[$page]->parent > 0) { //If the page has parent
881 $page = $items[$page]->parent;
882 $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>";
883 } else {
884 $contents .= "<span class=\"ims-nav-dimmed\">$strup</span>";
887 return $contents;
890 /*** This function will return the correct html code needed
891 * to show the toc button in the nav frame
893 function ims_get_toc_nav_button ($items, $resource_obj, $page) {
895 $cm = $resource_obj->cm;
896 $resource = $resource_obj->resource;
898 $strtoc = get_string('tableofcontentsabbrev', 'resource');
900 $contents = '';
902 if (!empty($resource_obj->parameters->tableofcontents)) { //The toc is enabled
903 $page = 0;
904 $contents .= "<span class=\"ims-nav-button\"><a href=\"view.php?id={$cm->id}&amp;type={$resource->type}&amp;page={$page}&amp;frameset=ims\">{$strtoc}</a></span>";
907 return $contents;