[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/resource/type/ims/ -> resource.class.php (source)

   1  <?php // $Id: resource.class.php,v 1.47.2.5 2008/07/10 09:48:47 scyrma Exp $
   2  
   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  ///////////////////////////////////////////////////////////////////////////
  26  
  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.
  37  
  38  
  39  require_once($CFG->libdir.'/filelib.php');
  40  require_once($CFG->dirroot.'/mod/resource/type/ims/repository_config.php');
  41  
  42  
  43  /**
  44  * Extend the base resource class for ims resources
  45  */
  46  class resource_ims extends resource_base {
  47  
  48      var $parameters;  //Attribute of this class where we'll store all the IMS deploy preferences
  49  
  50      function resource_ims($cmid=0) {
  51      /// super constructor
  52          parent::resource_base($cmid);
  53  
  54      /// prevent notice
  55          if (empty($this->resource->alltext)) {
  56              $this->resource->alltext='';
  57          }
  58      /// set own attributes
  59          $this->parameters = $this->alltext2parameters($this->resource->alltext);
  60  
  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;
  66          }
  67  
  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;
  77              }
  78          } else {
  79              $this->isrepository = false;
  80          }
  81      }
  82  
  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
  86      */
  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;
  95  
  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];
 102              }
 103          }
 104  
 105          return $alltextfield;
 106      }
 107  
 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
 112      */
 113      function parameters2alltext($parameters) {
 114          $optionlist = array();
 115  
 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;
 121  
 122          return implode(',', $optionlist);
 123      }
 124  
 125      /***
 126      * This function will convert all the parameters configured in the resource form
 127      * to a this->parameter attribute (object)
 128      */
 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;
 136  
 137          return $parameters;
 138      }
 139  
 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
 149      */
 150      function check4errors($file, $course, $resource) {
 151          global $CFG;
 152  
 153          if ($this->isrepository) {
 154          /// Calculate the path were the IMS package must be deployed
 155              $deploydir = $CFG->repository . $file;
 156  
 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
 161              }
 162          }
 163          else {
 164          /// Check for zip file type
 165              $mimetype = mimeinfo("type", $file);
 166              if ($mimetype != "application/zip") {
 167                  return 1;    //Error
 168              }
 169  
 170          /// Check if the uploaded file exists
 171              if (!file_exists($CFG->dataroot.'/'.$course->id.'/'.$file)) {
 172                  return 2;    //Error
 173              }
 174  
 175          /// Calculate the path were the IMS package must be deployed
 176              $deploydir = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id;
 177  
 178  
 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
 184              }
 185  
 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;
 192                  }
 193              }
 194          }
 195  
 196      /// We've arrived here. Everything is ok
 197          return 0;
 198      }
 199  
 200      /*** This function will check that the ims package (zip file) uploaded
 201      * isn't changed since it was deployed.
 202      */
 203      function checkpackagehash($file, $course, $resource) {
 204          global $CFG;
 205  
 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);
 217  
 218      /// Compare them
 219          return ($deployedhash == $uploadedhash);
 220      }
 221  
 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).
 225      */
 226      function calculatefilehash($filefullpath) {
 227  
 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);
 235  
 236          return md5($filename.'-'.$filesize.'-'.$data);
 237      }
 238  
 239      /**
 240      * Add new instance of file resource
 241      *
 242      * Create alltext field before calling base class function.
 243      *
 244      * @param    resource object
 245      */
 246      function add_instance($resource) {
 247          $this->_postprocess($resource);
 248          return parent::add_instance($resource);
 249      }
 250  
 251  
 252      /**
 253      * Update instance of file resource
 254      *
 255      * Create alltext field before calling base class function.
 256      *
 257      * @param    resource object
 258      */
 259      function update_instance($resource) {
 260          $this->_postprocess($resource);
 261          return parent::update_instance($resource);
 262      }
 263  
 264      function _postprocess(&$resource) {
 265          global $RESOURCE_WINDOW_OPTIONS;
 266          $alloptions = $RESOURCE_WINDOW_OPTIONS;
 267  
 268          if ($resource->windowpopup) {
 269              $optionlist = array();
 270              foreach ($alloptions as $option) {
 271                  $optionlist[] = $option."=".$resource->$option;
 272                  unset($resource->$option);
 273              }
 274              $resource->popup = implode(',', $optionlist);
 275              unset($resource->windowpopup);
 276  
 277          } else {
 278              $resource->popup = '';
 279          }
 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);
 284      }
 285  
 286      /** Delete instance of IMS-CP resource
 287       *
 288       * Delete all the moddata files for the resource
 289       * @param    resource object
 290       */
 291      function delete_instance($resource) {
 292  
 293           global $CFG;
 294  
 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;
 301                  }
 302              }
 303          }
 304  
 305          return parent::delete_instance($resource);
 306      }
 307  
 308  
 309      /**
 310       * Display the file resource
 311       *
 312       * Displays a file resource embedded, in a frame, or in a popup.
 313       * Output depends on type of file resource.
 314       *
 315       * @param    CFG     global object
 316       */
 317      function display() {
 318          global $CFG, $THEME, $USER;
 319  
 320          require_once($CFG->libdir.'/filelib.php');
 321  
 322      /// Set up generic stuff first, including checking for access
 323          parent::display();
 324  
 325      /// Set up some shorthand variables
 326          $cm = $this->cm;
 327          $course = $this->course;
 328          $resource = $this->resource;
 329  
 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);
 334  
 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));
 342  
 343          $formatoptions = new object();
 344          $formatoptions->noclean = true;
 345  
 346      /// Cache this per request
 347          static $items;
 348  
 349      /// Check for errors
 350          $errorcode = $this->check4errors($resource->reference, $course, $resource);
 351  
 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.
 369                  }
 370              }
 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));
 378              }
 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');
 393                  }
 394                  $method='post';
 395              /// Let's go with the button
 396                  echo '<center>';
 397                  print_single_button($link, $options, $label, $method);
 398                  echo '</center>';
 399              }
 400              print_simple_box_end();
 401          /// Close button if inpopup
 402              if ($inpopup) {
 403                  close_window_button();
 404              }
 405  
 406              print_footer();
 407              exit;
 408          }
 409  
 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;
 414              }
 415              else {
 416                  $resourcedir = $CFG->repository . $resource->reference;
 417              }
 418              if (!$items = ims_load_serialized_file($resourcedir.'/moodle_inx.ser')) {
 419                  error (get_string('errorreadingfile', 'error', 'moodle_inx.ser'));
 420              }
 421          }
 422  
 423      /// Check whether this is supposed to be a popup, but was called directly
 424  
 425          if (empty($frameset) && $resource->popup && !$inpopup) {    /// Make a page and a pop-up window
 426              $navigation = build_navigation($this->navlinks, $cm);
 427  
 428              print_header($pagetitle, $course->fullname, $navigation, "", "", true,
 429                      update_module_button($cm->id, $course->id, $this->strresource), navmenu($course, $cm));
 430  
 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>';
 436  
 437              if (trim(strip_tags($resource->summary))) {
 438                  print_simple_box(format_text($resource->summary, FORMAT_MOODLE, $formatoptions), "center");
 439              }
 440  
 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>";
 442  
 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>";
 449  
 450              print_footer($course);
 451              exit;
 452          }
 453  
 454  
 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') {
 460  
 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';
 466              }
 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
 471  
 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"));
 479              }
 480          /// content - this produces everything else
 481              $this->print_ims($cm, $course, $items, $resource, $page);
 482  
 483              print_footer('empty');
 484  
 485          /// log it.
 486              add_to_log($course->id, "resource", "view", "view.php?id={$cm->id}", $resource->id, $cm->id);
 487              exit;
 488          }
 489  
 490          if ($frameset == 'toc') {
 491              print_header();
 492              $this->print_toc($items, $resource, $page);
 493              echo '</div></div></body></html>';
 494              exit;
 495          }
 496      }
 497  
 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;
 501  
 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';
 507          }
 508  
 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);
 513          }
 514          else {
 515              $fileurl = $CFG->repositorywebroot . $resource->reference;
 516          }
 517  
 518  
 519      /// Calculate the view.php correct url
 520          $viewurl = "view.php?id={$cm->id}&amp;type={$resource->type}&amp;frameset=toc&amp;page=";
 521  
 522  
 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++;
 535                      }
 536                  }
 537              }
 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;
 550                  }
 551              }
 552          }
 553  
 554      /// print navigation buttons if needed
 555          if (!empty($this->parameters->navigationbuttons)) {
 556              $this->print_nav($items, $resource, $page);
 557          }
 558  
 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>";
 563          }
 564  
 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>';
 568      }
 569  
 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>';
 577          }
 578          $table->data[] = array(ims_generate_toc ($items, $resource, $page));
 579          $table->width = '60%';
 580          print_table($table);
 581      }
 582  
 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);
 586      }
 587  
 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>';
 601      }
 602  
 603  
 604      function setup_preprocessing(&$defaults){
 605  
 606          if (!isset($defaults['popup'])) {
 607              // use form defaults
 608  
 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];
 616                  }
 617              }
 618          } else {
 619              $defaults['windowpopup'] = 0;
 620          }
 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;
 629          }
 630      }
 631  
 632      function setup_elements(&$mform) {
 633          global $CFG, $RESOURCE_WINDOW_OPTIONS;
 634  
 635          $mform->addElement('choosecoursefileorimsrepo', 'reference', get_string('location'));
 636          $mform->addRule('name', null, 'required', null, 'client');
 637  
 638          $mform->addElement('header', 'displaysettings', get_string('display', 'resource'));
 639  
 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));
 643  
 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);
 653              }
 654              $mform->setAdvanced($option);
 655          }
 656  
 657          $mform->addElement('header', 'parameters', get_string('parameters', 'resource'));
 658  
 659          $mform->addElement('selectyesno', 'param_navigationmenu', get_string('navigationmenu', 'resource'));
 660          $mform->setDefault('param_navigationmenu', 1);
 661  
 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);
 665  
 666          $mform->addElement('selectyesno', 'param_navigationbuttons', get_string('navigationbuttons', 'resource'));
 667          $mform->setDefault('param_navigationbuttons', 0);
 668  
 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);
 672  
 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);
 676  
 677      }
 678  
 679  } //End class
 680  
 681  ///
 682  /// General purpose functions
 683  ///
 684      /*** This function will serialize the variable passed and send it
 685       *   to filesystem
 686       */
 687      function ims_save_serialized_file($destination, $var) {
 688          $status = false;
 689          if ($ser = serialize($var)) {
 690              $status = ims_var2file($destination, $ser);
 691          }
 692          return $status;
 693       }
 694  
 695      /*** This function will unserialize the variable stored
 696       *   in filesystem
 697       */
 698      function ims_load_serialized_file($file) {
 699          $status = false;
 700          if ($ser = ims_file2var($file)) {
 701              $status = unserialize($ser);
 702          }
 703          return $status;
 704      }
 705  
 706      /*** This function will load all the contents of one file to one variable
 707       *   Not suitable for BIG files
 708       */
 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;
 717             }
 718             fclose($fp);
 719          }
 720          if (!$status) {
 721              $var = false;
 722          }
 723          return $var;
 724      }
 725  
 726      /*** This file will write the contents of one variable to a file
 727       *   Not suitable for BIG files
 728       */
 729      function ims_var2file ($file, $var) {
 730          $status = false;
 731          if ($out = fopen($file,"w")) {
 732              $status = fwrite($out, $var);
 733              fclose($out);
 734          }
 735          return $status;
 736      }
 737  
 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.
 741       */
 742      function ims_generate_toc($items, $resource, $page=0, $selected_page = -1) {
 743          global $CFG;
 744  
 745          $contents = '';
 746  
 747      /// Configure links behaviour
 748          $fullurl = $CFG->wwwroot.'/mod/resource/view.php?r='.$resource->id.'&amp;frameset=ims&amp;page=';
 749  
 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;
 758              }
 759          /// Skip pages until we arrive to $page
 760              if ($item->id < $page) {
 761                  continue;
 762              }
 763          /// Arrive to page, we store its level
 764              if ($item->id == $page) {
 765                  $endlevel = $item->level;
 766                  continue;
 767              }
 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;
 774                  }
 775              /// End Level
 776                  if ($item->level < $currlevel) {
 777                      $contents .= '</li>';
 778                      $contents .= '</ol>';
 779                  }
 780              /// If we have some openlielement, just close it
 781                  if ($openlielement) {
 782                      $contents .= '</li>';
 783                  }
 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;
 792                  }
 793                  $currlevel = $item->level;
 794                  $openlielement = true;
 795                  continue;
 796              }
 797          /// We have reached endlevel, exit
 798              if ($item->id > $page && $item->level <= $endlevel) {
 799                  break;
 800              }
 801          }
 802      /// Close up to $endlevel
 803          for ($i=$currlevel;$i>$endlevel;$i--) {
 804              $contents .= '</li>';
 805              $contents .= '</ol>';
 806          }
 807  
 808          return $contents;
 809      }
 810  
 811      /*** This function will return the correct html code needed
 812       *   to show the previous button in the nav frame
 813       **/
 814      function ims_get_prev_nav_button ($items, $resource_obj, $page) {
 815          $strprevious        = get_string("previous", "resource");
 816  
 817          $cm = $resource_obj->cm;
 818          $resource = $resource_obj->resource;
 819  
 820          $contents = '';
 821  
 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--;
 827              }
 828          }
 829  
 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>';
 834          }
 835  
 836          return $contents;
 837      }
 838  
 839      /*** This function will return the correct html code needed
 840       *   to show the next button in the nav frame
 841       **/
 842      function ims_get_next_nav_button ($items, $resource_obj, $page) {
 843          $strnext        = get_string("next", "resource");
 844  
 845          $cm = $resource_obj->cm;
 846          $resource = $resource_obj->resource;
 847  
 848          $contents = '';
 849  
 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++;
 855              }
 856          }
 857  
 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>';
 862          }
 863  
 864  
 865          return $contents;
 866      }
 867  
 868      /*** This function will return the correct html code needed
 869       *   to show the up button in the nav frame
 870       **/
 871      function ims_get_up_nav_button ($items, $resource_obj, $page) {
 872          $strup        = get_string("upbutton", "resource");
 873  
 874          $cm = $resource_obj->cm;
 875          $resource = $resource_obj->resource;
 876  
 877          $contents = '';
 878  
 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>";
 885              }
 886          }
 887          return $contents;
 888      }
 889  
 890      /*** This function will return the correct html code needed
 891       *   to show the toc button in the nav frame
 892       **/
 893      function ims_get_toc_nav_button ($items, $resource_obj, $page) {
 894  
 895          $cm = $resource_obj->cm;
 896          $resource = $resource_obj->resource;
 897  
 898          $strtoc = get_string('tableofcontentsabbrev', 'resource');
 899  
 900          $contents = '';
 901  
 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>";
 905          }
 906  
 907          return $contents;
 908      }
 909  
 910  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7