controller.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Defines a base class for viewing SLOODLE Controller modules.
  4. * Class is inherited from the base module view 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. /** The base module view class */
  13. require_once(SLOODLE_DIRROOT.'/view/base/base_view_module.php');
  14. /**
  15. * The base class for viewing a SLOODLE Controller module.
  16. * @package sloodle
  17. */
  18. class sloodle_view_controller extends sloodle_base_view_module
  19. {
  20. /**
  21. * SLOODLE Controller data object, retreived directly from the database (table: sloodle_controller).
  22. * @var object
  23. * @access private
  24. */
  25. var $controller = null;
  26. /**
  27. * Constructor.
  28. */
  29. function sloodle_view_controller()
  30. {
  31. }
  32. /**
  33. * Uses the parent class to process basic request information, then obtains additional Controller data.
  34. */
  35. function process_request()
  36. {
  37. // Process basic data first
  38. parent::process_request();
  39. // Obtain the Controller-specific data
  40. if (!$this->controller = sloodle_get_record('sloodle_controller', 'sloodleid', $this->sloodle->id)) error('Failed to locate Controller data');
  41. }
  42. /**
  43. * Process any form data which has been submitted.
  44. */
  45. function process_form()
  46. {
  47. }
  48. /**
  49. * Render the view of the Controller.
  50. */
  51. function render()
  52. {
  53. global $CFG;
  54. // Fetch the controller data
  55. $this->controller = sloodle_get_record('sloodle_controller', 'sloodleid', $this->sloodle->id);
  56. if (!$this->controller) return false;
  57. // The name, type and description of the module should already be displayed by the main "view.php" script.
  58. echo "<div style=\"text-align:center;\">\n";
  59. // Check if some kind of action has been requested
  60. $action = optional_param('action', '', PARAM_TEXT);
  61. // Indicate whether or not this module is enabled
  62. echo '<p style="font-size:14pt;">'.get_string('status', 'sloodle').': ';
  63. if ($this->controller->enabled) {
  64. echo '<span style="color:green; font-weight:bold;">'.get_string('enabled','sloodle').'</span>';
  65. } else {
  66. echo '<span style="color:red; font-weight:bold;">'.get_string('disabled','sloodle').'</span>';
  67. }
  68. echo "</p>\n";
  69. // Can the user access protected data?
  70. if ($this->canedit) {
  71. // Active (authorised) objects
  72. sloodle_print_box_start('generalbox boxaligncenter boxwidthwide');
  73. echo '<h3>'.get_string('authorizedobjects','sloodle').'</h3>';
  74. // Has a delete objects action been requested
  75. if ($action == 'delete_objects') {
  76. // Count how many objects we delete
  77. $numdeleted = 0;
  78. // Go through each request parameter
  79. foreach ($_REQUEST as $name => $val) {
  80. // Is this a delete objects request?
  81. if ($val != 'true') continue;
  82. $parts = explode('_', $name);
  83. if (count($parts) == 2 && $parts[0] == 'sloodledeleteobj') {
  84. // Only delete the object if it belongs to this controller
  85. if (sloodle_delete_records('sloodle_active_object', 'controllerid', $this->cm->id, 'id', (int)$parts[1])) {
  86. $numdeleted++;
  87. // Delete any associated configuration settings too
  88. sloodle_delete_records('sloodle_object_config', 'object', (int)$parts[1]);
  89. }
  90. }
  91. }
  92. // Indicate our results
  93. echo '<span style="color:red; font-weight:bold;">'.get_string('numdeleted','sloodle').': '.$numdeleted.'</span><br><br>';
  94. }
  95. // Get all objects authorised for this controller
  96. $recs = sloodle_get_records('sloodle_active_object', 'controllerid', $this->cm->id, 'timeupdated DESC');
  97. if (is_array($recs) && count($recs) > 0) {
  98. // Construct a table
  99. //TODO: add authorising user link
  100. $objects_table = new stdClass();
  101. $objects_table->head = array(get_string('objectname','sloodle'),get_string('objectuuid','sloodle'),get_string('objecttype','sloodle'),get_string('lastupdated','sloodle'),'');
  102. $objects_table->align = array('left', 'left', 'left', 'left', 'center');
  103. foreach ($recs as $obj) {
  104. // Skip this object if it has no type information
  105. if (empty($obj->type)) continue;
  106. // Construct a link to this object's configuration page
  107. $config_link = "<a href=\"{$CFG->wwwroot}/mod/sloodle/classroom/configure_object.php?sloodleauthid={$obj->id}\">";
  108. $objects_table->data[] = array($config_link.$obj->name.'</a>', $obj->uuid, $obj->type, date('Y-m-d H:i:s T', (int)$obj->timeupdated), "<input type=\"checkbox\" name=\"sloodledeleteobj_{$obj->id}\" value=\"true\" /");
  109. }
  110. // Display a form and the table
  111. echo '<form action="" method="POST">';
  112. echo '<input type="hidden" name="id" value="'.$this->cm->id.'"/>';
  113. echo '<input type="hidden" name="action" value="delete_objects"/>';
  114. sloodle_print_table($objects_table);
  115. echo '<input type="submit" value="'.get_string('deleteselected','sloodle').'"/>';
  116. echo '</form>';
  117. } else {
  118. echo '<span style="text-align:center;color:red">'.get_string('noentries','sloodle').'</span><br>';
  119. }
  120. sloodle_print_box_end();
  121. }
  122. echo "</div>\n";
  123. }
  124. }
  125. ?>