Source for file distributor.php

Documentation is available at distributor.php

  1. <?php
  2. /**
  3. * Defines a class for viewing the SLOODLE Distributor module in Moodle.
  4. * Derived from the module view base class.
  5. *
  6. @package sloodle
  7. @copyright Copyright (c) 2008 Sloodle (various contributors)
  8. @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. @contributor Peter R. Bloomfield
  11. */
  12.  
  13. /** The base module view class */
  14. require_once(SLOODLE_DIRROOT.'/view/base/base_view_module.php');
  15.  
  16.  
  17.  
  18. /**
  19. * Class for rendering a view of a Distributor module in Moodle.
  20. @package sloodle
  21. */
  22. {
  23.     /**
  24.     * SLOODLE data about a Distributor, retrieved directly from the database (table: sloodle_distributor)
  25.     * @var object 
  26.     * @access private
  27.     */
  28.     var $distributor = false;
  29.  
  30.  
  31.     /**
  32.     * Constructor.
  33.     */
  34.     function sloodle_base_view_module()
  35.     {
  36.     }
  37.  
  38.     /**
  39.     * Processes request data to determine which Distributor is being accessed.
  40.     */
  41.     function process_request()
  42.     {
  43.         // Process the basic data
  44.         parent::process_request();
  45.         // Grab the Distributor data
  46.         if (!$this->distributor = get_record('sloodle_distributor''sloodleid'$this->sloodle->id)) error('Failed to get SLOODLE Distributor data.');
  47.     }
  48.  
  49.     /**
  50.     * Process any form data which has been submitted.
  51.     */
  52.     function process_form()
  53.     {
  54.     }
  55.  
  56.  
  57.     /**
  58.     * Render the view of the Distributor.
  59.     */
  60.     function render()
  61.     {
  62.         global $CFG$USER;
  63.     
  64.         // Fetch a list of all distributor entries
  65.         $entries get_records('sloodle_distributor_entry''distributorid'$this->distributor->id'name');
  66.         // If the query failed, then assume there were simply no items available
  67.         if (!is_array($entries)) $entries array();
  68.         $numitems count($entries);
  69.         
  70.         
  71.         // A particular default user can be requested (by avatar name) in the HTTP parameters.
  72.         // This could be used with a "send to this avatar" button on a Sloodle user profile.
  73.         $defaultavatar optional_param('defaultavatar'nullPARAM_TEXT);
  74.         
  75.         
  76.         // // SEND OBJECT // //
  77.         
  78.         // If the user and object parameters are set, then try to send an object
  79.         if (isset($_REQUEST['user'])) $send_user $_REQUEST['user'];
  80.         if (isset($_REQUEST['object'])) $send_object $_REQUEST['object'];
  81.         if (!empty($send_user&& !empty($send_object)) {
  82.  
  83.             // Convert the HTML entities back again
  84.             $send_object htmlentities(stripslashes($send_object));
  85.  
  86.             // Construct and send the request
  87.             $request "1|OK\\nSENDOBJECT|$send_user|$send_object";
  88.             $ok sloodle_send_xmlrpc_message($this->distributor->channel0$request);
  89.             
  90.             // What was the result?
  91.             print_box_start('generalbox boxaligncenter boxwidthnarrow centerpara');
  92.             if ($ok{
  93.                 print '<h3 style="color:green;text-align:center;">'.get_string('sloodleobjectdistributor:successful','sloodle').'</h3>';
  94.             else {
  95.                 print '<h3 style="color:red;text-align:center;">'.get_string('sloodleobjectdistributor:failed','sloodle').'</h3>';
  96.             }
  97.             print '<p style="text-align:center;">';
  98.                 print get_string('Object','sloodle').': '.$send_object.'<br/>';
  99.                 print get_string('uuid','sloodle').': '.$send_user.'<br/>';
  100.                 print get_string('xmlrpc:channel','sloodle').': '.$this->distributor->channel.'<br/>';
  101.                 print '</p>';
  102.             print_box_end();
  103.         }
  104.         
  105.         // // ----------- // //
  106.         
  107.  
  108.         // If there are no items in the distributor, then simply display an error message
  109.         if ($numitems 1print_box('<span style="font-weight:bold; color:red;">'.get_string('sloodleobjectdistributor:noobjects','sloodle').'</span>''generalbox boxaligncenter boxwidthnormal centerpara');
  110.         //error(get_string('sloodleobjectdistributor:noobjects','sloodle'));
  111.         // If there is no XMLRPC channel specified, then display a warning message
  112.         $disabledattr '';
  113.         if (empty($this->distributor->channel)) {
  114.             print_box('<span style="font-weight:bold; color:red;">'.get_string('sloodleobjectdistributor:nochannel','sloodle').'</span>''generalbox boxaligncenter boxwidthnormal centerpara');
  115.             $disabledattr 'disabled="true"';
  116.         }
  117.         
  118.         // Construct the selection box of items
  119.         $selection_items '<select name="object" size="1">';
  120.         foreach ($entries as $e{
  121.             $escapedname stripslashes($e->name);
  122.             $selection_items .= "<option value=\"{$e->name}\">{$escapedname}</option>\n";
  123.         }
  124.         $selection_items .= '</select>';
  125.         
  126.         // Get a list of all avatars on the site
  127.         $avatars get_records('sloodle_users''''''avname');
  128.         if (!$avatars$avatars array();
  129.         // Construct the selection box of avatars
  130.         $selection_avatars '<select name="user" size="1">';
  131.         foreach ($avatars as $a{
  132.             // Skip avatars who do not have a UUID or associated Moodle account
  133.             if (empty($a->uuid|| empty($a->userid)) continue;
  134.             // Make sure the associated Moodle user can view the current course
  135.             if (!has_capability('moodle/course:view'$this->course_context$a->userid)) continue;
  136.             // Make sure the associated Moodle user does not have a guest role
  137.             if (has_capability('moodle/legacy:guest'$this->course_context$a->useridfalse)) continue;
  138.  
  139.             $sel '';
  140.             if ($a->avname == $defaultavatar$sel 'selected="true"';
  141.             $selection_avatars .= "<option value=\"{$a->uuid}\" $sel>{$a->avname}</option>\n";
  142.         }
  143.         $selection_avatars .= '</select>';
  144.         
  145.  
  146.         // There will be 3 forms:
  147.         //  - send to self
  148.         //  - send to another avatar on the course
  149.         //  - send to custom UUID
  150.         // The first 1 will be available to any registered user whose avatar is in the database.
  151.         // The other 2 will only be available to those with the activity management capability.
  152.         // Furthermore, the 2nd form will only be available if there is at least 1 avatar registered on the site.
  153.  
  154.         // Start of the sending forms
  155.         print_box_start('generalbox boxaligncenter boxwidthnormal centerpara');
  156.         
  157.     // // SEND TO SELF // //
  158.         
  159.         // Start the form
  160.         echo '<form action="" method="POST">';
  161.         
  162.         // Use a table for layout
  163.         $table_sendtoself new stdClass();
  164.         $table_sendtoself->head array(get_string('sloodleobjectdistributor:sendtomyavatar','sloodle'));
  165.         $table_sendtoself->align array('center');
  166.         
  167.         // Fetch the current user's Sloodle info
  168.         $this->sloodleuser get_record('sloodle_users''userid'$USER->id);
  169.         if (!$this->sloodleuser{
  170.             $table_sendtoself->data[array('<span style="color:red;">'.get_string('avatarnotlinked','sloodle').'</span>');
  171.         else {
  172.             // Output the hidden form data
  173.             echo <<<XXXEODXXX
  174.  <input type="hidden" name="s" value="{$this->sloodle->id}">
  175.  <input type="hidden" name="user" value="{$this->sloodleuser->uuid}">
  176. XXXEODXXX;
  177.         
  178.             // Object selection box
  179.             $table_sendtoself->data[array(get_string('selectobject','sloodle').': '.$selection_items);
  180.             // Submit button
  181.             $table_sendtoself->data[array('<input type="submit" '.$disabledattr.' value="'.get_string('sloodleobjectdistributor:sendtomyavatar','sloodle').' ('.$this->sloodleuser->avname.')" />');
  182.         }
  183.         
  184.         // Print the table
  185.         print_table($table_sendtoself);
  186.         
  187.         // End the form
  188.         echo "</form>";
  189.         
  190.         
  191.         // Only show the other options if the user has permission to edit stuff
  192.         if ($this->canedit{
  193.         // // SEND TO ANOTHER AVATAR // //
  194.             
  195.             // Start the form
  196.             echo '<br><form action="" method="POST">';
  197.             
  198.             // Use a table for layout
  199.             $table new stdClass();
  200.             $table->head array(get_string('sloodleobjectdistributor:sendtoanotheravatar','sloodle'));
  201.             $table->align array('center');
  202.             
  203.             // Do we have any avatars?
  204.             if (count($avatars1{
  205.                 $table->data[array('<span style="color:red;">'.get_string('nosloodleusers','sloodle').'</span>');
  206.             else {
  207.                 // Output the hidden form data
  208.                 echo <<<XXXEODXXX
  209.      <input type="hidden" name="s" value="{$this->sloodle->id}">
  210. XXXEODXXX;
  211.                 // Avatar selection box
  212.                 $table->data[array(get_string('selectuser','sloodle').': '.$selection_avatars);
  213.                 // Object selection box
  214.                 $table->data[array(get_string('selectobject','sloodle').': '.$selection_items);
  215.                 // Submit button
  216.                 $table->data[array('<input type="submit" '.$disabledattr.' value="'.get_string('sloodleobjectdistributor:sendtoanotheravatar','sloodle').'" />');
  217.             }
  218.             
  219.             // Print the table
  220.             print_table($table);
  221.             
  222.             // End the form
  223.             echo "</form>";
  224.             
  225.         // // SEND TO A CUSTOM AVATAR // //
  226.             
  227.             // Start the form
  228.             echo '<br><form action="" method="post">';
  229.             
  230.             // Use a table for layout
  231.             $table new stdClass();
  232.             $table->head array(get_string('sloodleobjectdistributor:sendtocustomavatar','sloodle'));
  233.             $table->align array('center');
  234.             
  235.             // Output the hidden form data
  236.             echo <<<XXXEODXXX
  237. <input type="hidden" name="s" value="{$this->sloodle->id}">
  238. XXXEODXXX;
  239.         
  240.             // UUID box
  241.             $table->data[array(get_string('uuid','sloodle').': '.'<input type="text" name="user" size="46" maxlength="36" />');
  242.             // Object selection box
  243.             $table->data[array(get_string('selectobject','sloodle').': '.$selection_items);
  244.             // Submit button
  245.             $table->data[array('<input type="submit" '.$disabledattr.' value="'.get_string('sloodleobjectdistributor:sendtocustomavatar','sloodle').'" />');
  246.             
  247.             // Print the table
  248.             print_table($table);
  249.             
  250.             // End the form
  251.             echo "</form>";
  252.             
  253.         // // ---------- // //
  254.         }
  255.     
  256.         
  257.         print_box_end();
  258.     
  259.     }
  260.  
  261. }
  262.  
  263.  
  264. ?>

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