Source for file layout_profile.php

Documentation is available at layout_profile.php

  1. <?php
  2.     // This file is part of the Sloodle project (www.sloodle.org)
  3.     
  4.     /**
  5.     * This file defines structures for managing Sloodle layout profiles.
  6.     *
  7.     * @package sloodle
  8.     * @copyright Copyright (c) 2008 Sloodle (various contributors)
  9.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10.     *
  11.     * @contributor Peter R. Bloomfield
  12.     */
  13.     
  14.     class SloodleLayout
  15.     {
  16.         var $name;
  17.         var $id;
  18.         var $courseid;
  19.  
  20.         var $entries = array();
  21.         var $originalentryids = array();
  22.  
  23.         function SloodleLayout($r null{
  24.  
  25.             if ($r != null{
  26.                 return $this->load_from_row($r);
  27.             }
  28.             return null;
  29.  
  30.         }
  31.  
  32.         function load_from_row($r{
  33.  
  34.             if (!$rreturn null;
  35.  
  36.             $this->name = $r->name;
  37.             $this->id = $r->id;
  38.             $this->courseid = $r->course;
  39.  
  40.             $this->originalentries $this->get_entries($store false);
  41.  
  42.             return true;
  43.  
  44.         }
  45.  
  46.         function load($id{
  47.  
  48.             $rec get_record('sloodle_layout','id',$id)
  49.             if (!$recreturn null;
  50.             $loaded $this->load_from_row($rec);
  51.  
  52.         }
  53.  
  54.            $entries $this->entries;     
  55.            $ok true;
  56.        for ($i=0$i<count($entries)$i++{
  57.               $entry $entries[$i];
  58.               if ( ($entry->objectuuid != null&& ($entry->objectuuid != '') ) {
  59.                  $entry->populate_from_active_object();
  60.               }
  61.        }
  62.            return $ok;
  63.     }
  64.  
  65.         function get_sloodle_course({
  66.  
  67.             $course new SloodleCourse();
  68.             if ($course->load($this->courseid)) {
  69.                 return $course;
  70.             }
  71.             return null;
  72.  
  73.         }
  74.  
  75.         function get_course({
  76.  
  77.             return get_record('course','id',$this->courseid);
  78.  
  79.         }
  80.  
  81.         // return a set of SloodleLayoutEntry objects for the layout.
  82.      // store parameter indicates whether they should be stored as instance variables.
  83.     // set $store=false if you're planning to update the object entries and you're only reading them in so the object will know what is already there and what to insert and delete.
  84.         function get_entries($store true{
  85.  
  86.             $rows get_records('sloodle_layout_entry','layout',$this->id);
  87.             $entries array();
  88.             if (count($rows0{
  89.                foreach($rows as $row{
  90.                   $entries[new SloodleLayoutEntry($row);
  91.                }
  92.             }
  93.  
  94.             if ($store{
  95.                $this->entries = $entries;
  96.             }
  97.  
  98. //print "<h4>layout.get_entries returning ".count($entries)."entries</h4>";
  99.             return $entries;
  100.  
  101.         }
  102.  
  103.         function add_entry($entry{
  104.  
  105.            $entries $this->entries;
  106.            $entries[$entry;
  107.            $this->entries = $entries;
  108.  
  109.         }
  110.  
  111.         function insert({
  112.  
  113.             $this->id = insert_record('sloodle_layout'$this);
  114.             $this->save_entries();
  115.             return $this->id;
  116.  
  117.         }
  118.  
  119.         function update({
  120.  
  121.             if (!update_record('sloodle_layout'$this)) {
  122.                return false;
  123.             }
  124.             return $this->save_entries();
  125.  
  126.         }
  127.  
  128.         function save_entries({
  129.  
  130.             $neededids array();
  131.             foreach($this->entries as $entry{
  132.                $entry->layout $this->id;
  133.                if (!$entry->id{
  134.                   $entry->id $entry->insert();
  135.                else {
  136.                   $entry->update();
  137.                }
  138.                $neededids[$entry->id;
  139.             }
  140.  
  141.             foreach($this->originalentries as $entry{
  142.                $entryid $entry->id;
  143.                if (!in_array($entryid,$neededids)) {
  144.                   $entry->delete();
  145.                }
  146.             }
  147.  
  148.            return true;
  149.  
  150.         }
  151.  
  152.     }
  153.     
  154.     /**
  155.     * Stores data for a single entry in a layout profile.
  156.     * Used with {@link SloodleCourse}
  157.     * @package sloodle
  158.     */
  159.     class SloodleLayoutEntry
  160.     {
  161.     // DATA //
  162.     
  163.         /**
  164.         * The name of the object this entry represents.
  165.         * @var string 
  166.         * @access public
  167.         */
  168.         var $name = '';
  169.         
  170.         /**
  171.         * The position of the object, as a 3d vector, in string format "<x,y,z>"
  172.         * @var string 
  173.         * @access public
  174.         */
  175.         var $position = '<0.0,0.0,0.0>';
  176.         
  177.         /**
  178.         * The rotation of the object, as a 3d vector of Euler angles, in string format "<x,y,z>"
  179.         * @var string 
  180.         * @access public
  181.         */
  182.         var $rotation = '<0.0,0.0,0.0>';
  183.  
  184.         var $id;
  185.         var $layout;
  186.         var $configs;
  187.         var $originalconfigs;
  188.       
  189.         function SloodleLayoutEntry($r null{
  190.  
  191.              if ($r != null{
  192.                 return $this->load_from_row($r);
  193.              }
  194.  
  195.              return true;
  196.  
  197.         }
  198.  
  199.         function load_from_row($r{
  200.  
  201.             if (!$rreturn null;
  202.  
  203.             $this->name = $r->name;
  204.             $this->position = $r->position;
  205.             $this->rotation = $r->rotation;
  206.             $this->id = $r->id;
  207.             $this->layout = $r->layout;
  208.             $this->objectuuid $r->objectuuid// not saved - just used to populate defaults
  209.  
  210.             $this->configs = $this->get_layout_entry_configs();
  211.             $this->originalconfigs = $this->configs// save the original configs so we know what to delete, what to update and what to add
  212.  
  213.             return true;
  214.  
  215.         }
  216.  
  217.         function load($id{
  218.  
  219.         $rec get_record('sloodle_layout_entry''id'$id);
  220.             if (!$recreturn null;
  221.             return $this->load_from_row($rec);
  222.             
  223.         }
  224.  
  225.     function populate_from_active_object({
  226.  
  227.            if (!$objectuuid $this->objectuuid{
  228.               return false;
  229.            }
  230.  
  231.            $object get_record('sloodle_active_object','uuid',$this->objectuuid)
  232.            $object_id $object->id;
  233.            if (!$object_idreturn false;
  234.  
  235.            $configs get_records('sloodle_object_config','object',$object_id)
  236.            foreach($configs as $config{
  237.               $this->set_config($config->name,$config->value);
  238.            }
  239.  
  240.            return true;
  241.            
  242.            }
  243.  
  244.         function get_layout({
  245.  
  246.             if ($this->layout != null{
  247.  
  248.                $layout new SloodleLayout();
  249.                if $layout->load($this->layout) ) {
  250.                    return $layout;
  251.                }
  252.  
  253.             }
  254.             return null;
  255.         }
  256.  
  257.     /**
  258.         * Return an array of layout_entry_config objects
  259.         * @param integer $layout_entry_id The ID of the layout entry
  260.         * @return array Array if successful (empty if there are no entries) , or null otherwise
  261.         */
  262.     function get_layout_entry_configs({
  263.             
  264.         $recs get_records('sloodle_layout_entry_config''layout_entry'$this->id);
  265.             if (!$recsreturn null;
  266.             
  267.             // Construct the array of SloodleLayoutEntry objects
  268.             $entryconfigs array();
  269.             foreach ($recs as $r{
  270.                 $entryconfigs[new SloodleLayoutEntryConfig($r);
  271.             
  272.             return $entryconfigs;
  273.  
  274.     }
  275.  
  276.         function get_layout_entry_configs_as_hidden_fields($prefix$suffix{
  277.            
  278.            $str '';
  279.            foreach($this->get_layout_entry_configs_as_name_value_hash(as $n=>$v{
  280.               $fieldname $prefix.$n.$suffix;
  281.               $str .= '<input type="hidden" name="'.$fieldname.'" value="'.$v.'" />';
  282.            }
  283.            return $str;
  284.  
  285.     }
  286.  
  287.  
  288.         $objs $this->get_layout_entry_configs();
  289.             $configs array();
  290.             if (count($objs0{
  291.                foreach($objs as $obj{
  292.                   $configs[$obj->name$obj->value;
  293.                }
  294.             }
  295.             return $configs;
  296.  
  297.     }
  298.  
  299.         /**
  300.         * Sets the position as separate X,Y,Z components
  301.         * @param float $x The X component to set
  302.         * @param float $y The Y component to set
  303.         * @param float $z The Z component to set
  304.         * @return void 
  305.         */
  306.         function set_position_xyz($x$y$z)
  307.         {
  308.             $this->position = "<$x,$y,$z>";
  309.         }
  310.         
  311.         /**
  312.         * Sets the rotation as separate X,Y,Z components
  313.         * @param float $x The X component to set
  314.         * @param float $y The Y component to set
  315.         * @param float $z The Z component to set
  316.         * @return void 
  317.         */
  318.         function set_rotation_xyz($x$y$z)
  319.         {
  320.             $this->rotation = "<$x,$y,$z>";
  321.         }
  322.         
  323.         function set_config($name$value// add config with given name/value, overwriting if necessary
  324.             $configs $this->configs;
  325.             $done false;
  326.             if is_array($configs) ) {
  327.                 for$i=0$i<count($configs)$i++{
  328.                     $config $configs[$i];
  329.                     if ($config->name == $name{
  330.                          $config->value $value;
  331.                          $configs[$i$config;
  332.                          $done true;
  333.                     }
  334.                 }  
  335.             else {
  336.                 $configs array();
  337.             }
  338.             if (!$done{
  339.                 $config new SloodleLayoutEntryConfig();
  340.                 $config->layout_entry $this->id;
  341.                 $config->name $name;
  342.                 $config->value $value;
  343.                 $configs[$config;
  344.             }
  345.             $this->configs = $configs;
  346.  
  347.         }
  348.  
  349.         function insert({
  350.  
  351.             $this->id = insert_record('sloodle_layout_entry'$this);
  352.             if (!$this->id{
  353.                return false;
  354.             }
  355.             return $this->save_configs();
  356.  
  357.         }
  358.  
  359.         function update({
  360.  
  361.             if (!update_record('sloodle_layout_entry'$this)) {
  362.                return false;
  363.             }
  364.             return $this->save_configs();
  365.  
  366.         }
  367.  
  368.         function delete({
  369.  
  370.            if (!$this->idreturn false;
  371.  
  372.            if (!$this->delete_configs()) {
  373.               return false;
  374.            }           
  375.  
  376.            $result delete_records('sloodle_layout_entry''id'$this->id);
  377.            return $result;
  378.  
  379.         }
  380.  
  381.         function delete_configs({
  382.  
  383.             return delete_records('sloodle_layout_entry_config''layout_entry'$this->id);
  384.  
  385.         }
  386.  
  387.         function save_configs({
  388.  
  389.             foreach($this->configs as $config{
  390.                $config->layout_entry $this->id
  391.  
  392.                if (!$config->id{
  393.                   $entry_id $config->insert();
  394.                else {
  395.                   $config->update();
  396.                }
  397.             }
  398.  
  399.         }
  400.  
  401.     }
  402.  
  403.     {
  404.  
  405.         var $id;
  406.         var $layout_entry;
  407.         var $name;
  408.         var $value;
  409.  
  410.         function SloodleLayoutEntryConfig($row null{
  411.  
  412.             if ($row != null{
  413.                 return $this->load_from_row($row);
  414.             }
  415.  
  416.             return null;
  417.  
  418.         }
  419.  
  420.         function load_from_row($row{
  421.  
  422.             if (!$rowreturn null;
  423.  
  424.             $this->id = $row->id;
  425.             $this->layout_entry = $row->layout_entry;
  426.             $this->name = $row->name;
  427.             $this->value = $row->value;
  428.  
  429.         }
  430.  
  431.         function load($id{
  432.         $rec get_record('sloodle_layout_entry_config''id'$id);
  433.             if (!$recreturn null;
  434.             return $this->load_from_row($rec);
  435.         }
  436.  
  437.         function insert({
  438.  
  439.             return $this->id = insert_record('sloodle_layout_entry_config'$this);
  440.  
  441.         }
  442.  
  443.         function update({
  444.  
  445.             return update_record('sloodle_layout_entry_config'$this);
  446.  
  447.         }
  448.  
  449.     }
  450. ?>

Documentation generated on Fri, 17 Jul 2009 11:01:32 +0100 by phpDocumentor 1.4.0