configure_object.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Sloodle object configuration/authorization page.
  4. *
  5. * Allows users in Moodle to authorize or deny in-world objects' access to Moodle,
  6. * and to select the configuration settings.
  7. *
  8. * @package sloodleclassroom
  9. * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  10. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11. *
  12. * @contributor Edmund Edgar
  13. * @contributor Peter R. Bloomfield
  14. *
  15. */
  16. // Initially, the script should be accessed with a single parameter:
  17. //
  18. // sloodleauthid = the integer ID of an (unauthorised) entry in the Sloodle 'active objects' table
  19. //
  20. // When the user clicks "yes" or "no" to confirm or deny the object's authorisation,
  21. // the following additional parameters will be added:
  22. //
  23. // sloodlecontrollerid = the ID of the controller which is to be used
  24. // action = either 'auth' or 'cancel'
  25. //
  26. // If the action is 'confirm' then the object is authorised. Otherwise, its entry is deleted.
  27. //
  28. // The following parameter will usually also need to be specified:
  29. //
  30. // sloodleobjtype = gives the type identifier of the object (not necessary if type is already registered)
  31. /** Grab the Sloodle/Moodle configuration. */
  32. require_once('../init.php');
  33. /** Include the Sloodle PHP API. */
  34. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  35. require_once(SLOODLE_LIBROOT.'/general.php');
  36. // Make sure the user is logged-in
  37. sloodle_require_login_no_guest();
  38. // We need to know which object is being authorised and/or configured
  39. $sloodleauthid = required_param('sloodleauthid', PARAM_INT);
  40. $auth_obj = SloodleController::get_object($sloodleauthid);
  41. if (!$auth_obj) {
  42. error(get_string('objectauthnotfound','sloodle'));
  43. exit();
  44. }
  45. // Is the object already authorised?
  46. $object_authorised = $auth_obj->course->controller->is_loaded();
  47. // Get the object type (only necessary if it was not already in the database)
  48. $sloodleobjtype = null;
  49. if (empty($auth_obj->type)) {
  50. $sloodleobjtype = required_param('sloodleobjtype', PARAM_TEXT);
  51. } else {
  52. $sloodleobjtype = optional_param('sloodleobjtype', null, PARAM_TEXT);
  53. }
  54. // Treat this like a Sloodle session, but do not authenticate anything
  55. $sloodle = new SloodleSession();
  56. // Was a controller loaded by the request?
  57. $loaded_controller = false;
  58. if ($sloodle->course->controller->is_loaded()) {
  59. $loaded_controller = true;
  60. } else {
  61. // If a controller was already specified for the object, then use that
  62. if ($auth_obj->course->controller->is_loaded()) {
  63. $loaded_controller = true;
  64. $sloodle->course = $auth_obj->course;
  65. }
  66. }
  67. // Load the user
  68. $sloodle->user->load_user($USER->id);
  69. $sloodle->user->load_linked_avatar();
  70. // Fetch localization strings
  71. $stryes = get_string('yes');
  72. $strno = get_string('no');
  73. // Check if the confirmation action has been specified
  74. $action = optional_param('action', '', PARAM_TEXT);
  75. // If the action was not recognised, then ignore it
  76. if ($action != 'auth' && $action != 'cancel') $action = '';
  77. // Determine the page name - it should be authorisation or configuration
  78. $pagename = get_string('objectauth','sloodle');
  79. if ($object_authorised == true || $action == 'auth') $pagename = get_string('objectconfiguration','sloodle');
  80. // Construct a breadcrumb navigation menu
  81. $nav = get_string('modulename', 'sloodle').' -> ';
  82. $nav .= $pagename;
  83. // Display the page header
  84. sloodle_print_header($pagename, $pagename, $nav, '', '', false);
  85. //------------------------------------------------------------
  86. // AUTHORISATION STEP
  87. // Was an action specified? (Don't do this if the object was already authorised)
  88. $action_success = false;
  89. if (!empty($action) && $object_authorised == false) {
  90. // Check if we were instructed to authorise, and also that we have a controller to authorise on
  91. if ($action == 'auth' && $loaded_controller == true) {
  92. // Make sure the user is allowed to authorise objects
  93. $module_context = get_context_instance(CONTEXT_MODULE, $sloodle->course->controller->get_id());
  94. require_capability('mod/sloodle:objectauth', $module_context);
  95. // Indicate that we are attempting authorisation
  96. echo '<div style="text-align:center;">';
  97. echo get_string('authorizingfor', 'sloodle').$sloodle->course->get_full_name().' &gt; '.$sloodle->course->controller->get_name().'<br><br>';
  98. // Attempt to authorise the entry
  99. if ($sloodle->course->controller->authorise_object($auth_obj->uuid, $sloodle->user, $sloodleobjtype)) {
  100. echo '<span style="font-weight:bold; color:green;">'.get_string('objectauthsuccessful','sloodle').'</span><br>';
  101. $action_success = true;
  102. $object_authorised = true;
  103. // Reload the object data
  104. $auth_obj = SloodleController::get_object($sloodleauthid);
  105. } else {
  106. echo '<span style="font-weight:bold; color:red;">'.get_string('objectauthfailed','sloodle').'</span><br>';
  107. }
  108. echo "</div><br>\n";
  109. } else if ($action == 'cancel') {
  110. // Attempt to delete the entry
  111. $sloodle->course->controller->remove_object($sloodleauthid);
  112. // Display the result
  113. sloodle_print_box(get_string('objectauthcancelled', 'sloodle'), 'generalbox boxaligncenter boxwidthnarrow');
  114. $action_success = true;
  115. }
  116. } else if ($object_authorised) {
  117. // If the object has already been authorised, then display a message about it
  118. sloodle_print_box_start('generalbox boxwidthnarrow boxaligncenter');
  119. echo '<span style="color:red; font-weight:bold; text-align:center;">'.get_string('objectauthalready','sloodle').'</span>';
  120. sloodle_print_box_end();
  121. }
  122. //------------------------------------------------------------
  123. // OBJECT INFO
  124. // Display the information about the object
  125. sloodle_print_box_start('generalbox boxaligncenter boxwidthnarrow');
  126. echo '<div style="text-align:center;">';
  127. echo '<span style="font-weight:bold; font-size:110%;">'.get_string('objectdetails','sloodle').'</span><br>';
  128. echo get_string('objectname','sloodle').': '.$auth_obj->name.'<br>';
  129. echo get_string('objectuuid','sloodle').': '.$auth_obj->uuid.'<br>';
  130. echo get_string('objecttype','sloodle').': ';
  131. if (!empty($sloodleobjtype)) echo $sloodleobjtype.'<br>';
  132. else if (!empty($auth_obj->type)) echo $auth_obj->type.'<br>';
  133. else echo '-<br>';
  134. echo '</div>';
  135. sloodle_print_box_end();
  136. echo '<br>';
  137. //------------------------------------------------------------
  138. // AUTHORISATION FORM
  139. // If the action was not performed or successful, then display the authorisation form
  140. if ($action_success == false && $object_authorised == false) {
  141. sloodle_print_box_start('generalbox boxaligncenter boxwidthnormal');
  142. echo '<div style="text-align:center;">';
  143. echo '<h3>'.get_string('objectauth','sloodle').'</h3>';
  144. // If an authorisation action was attempted but the controller failed to load, then display an error message
  145. if ($action == 'auth' && $loaded_controller = false) {
  146. echo '<span style="font-weight:bold;color:red;">'.get_string('failedauth-trydifferent','sloodle').'</span>';
  147. }
  148. // Get a list of controllers which the user is permitted to authorise objects on
  149. // (It will be a 2d array - top level will be course ID's, 2nd level will be coursemodule ID's, containing database objects from the 'sloodle' table)
  150. $controllers = array();
  151. $recs = sloodle_get_records('sloodle', 'type', SLOODLE_TYPE_CTRL);
  152. // Make sure we have at least one controller
  153. if ($recs == false || count($recs) == 0) {
  154. error(get_string('objectauthnocontrollers','sloodle'));
  155. exit();
  156. }
  157. foreach ($recs as $r) {
  158. // Fetch the course module
  159. $cm = get_coursemodule_from_instance('sloodle', $r->id);
  160. // Check that the person can authorise objects of this module
  161. if (has_capability('mod/sloodle:objectauth', get_context_instance(CONTEXT_MODULE, $cm->id))) {
  162. // Store this controller
  163. $controllers[$cm->course][$cm->id] = $r;
  164. }
  165. }
  166. // Make sure that permission came through on at least one controller
  167. if (count($controllers) == 0) {
  168. error(get_string('objectauthnopermission','sloodle'));
  169. exit();
  170. }
  171. // Construct the list of course names
  172. $coursenames = array();
  173. foreach ($controllers as $cid => $ctrl) {
  174. $courserec = sloodle_get_record('course', 'id', $cid, '','', '','', 'id,shortname,fullname');
  175. if (!$courserec) $coursenames[$cid] = "(unknown)";
  176. else $coursenames[$cid] = $courserec->fullname;
  177. }
  178. // Sort the list alphabetically
  179. natcasesort($coursenames);
  180. // Attempt to get our current controller, if possible, so we can pre-select it in the list
  181. $cur_controller_id = 0;
  182. if ($loaded_controller) {
  183. $cur_controller_id = $sloodle->course->controller->get_id();
  184. }
  185. // Display an authorisation form
  186. echo '<form action="" method="GET">';
  187. if (SLOODLE_DEBUG) echo '<input type="hidden" name="sloodledebug" value="true"/>';
  188. if (!empty($sloodleauthid)) echo '<input type="hidden" name="sloodleauthid" value="'.$sloodleauthid.'"/>';
  189. if (!empty($sloodleobjtype)) echo '<input type="hidden" name="sloodleobjtype" value="'.$sloodleobjtype.'"/>';
  190. // Ask the user to select a controller to authorise on
  191. echo get_string('selectcontroller','sloodle').':<br><br>';
  192. echo '<select name="sloodlecontrollerid" size="6">';
  193. // Go through each course
  194. foreach ($coursenames as $cid => $cname) { // We want to use the alphabetic sorting; get the course ID and name
  195. // Output a disabled entry to act as a course header
  196. echo "<option disabled=\"true\" style=\"font-weight:bold; font-style:italic; background-color:#eeeeee;\">{$cid}. $cname</option>\n";
  197. // Go through each controller in this course
  198. foreach ($controllers[$cid] as $cmid => $ctrl) { // Get each controller in the course; get the course module ID and controller data
  199. echo "<option ";
  200. if ($cmid == $cur_controller_id) echo 'selected="true" ';
  201. echo "style=\"margin-left:8px;\" value=\"$cmid\">{$ctrl->name}</option>\n";
  202. }
  203. }
  204. echo '</select><br><br>';
  205. // Ask the user if they want to confirm or deny the authorisation
  206. echo get_string('confirmobjectauth', 'sloodle').' ';
  207. echo '<select name="action" size="1">';
  208. echo '<option style="color:green; font-weight:bold;" value="auth" selected="true">'.$stryes.'</option>';
  209. echo '<option style="color:red; font-weight:bold;" value="cancel">'.$strno.'</option>';
  210. echo '</select><br><br>';
  211. echo '<input type="submit" value="'.get_string('submit','sloodle').'"/>';
  212. echo "</form>\n";
  213. echo '</div>';
  214. sloodle_print_box_end();
  215. }
  216. //------------------------------------------------------------
  217. // CONFIGURATION FORM
  218. // Get the type
  219. $usetype = "";
  220. if (!empty($auth_obj->type)) $usetype = $auth_obj->type;
  221. else $usetype = $sloodleobjtype;
  222. // If the object is now authorised, we can display its configuration form
  223. if ($object_authorised && !empty($usetype)) {
  224. // Make sure the user has permission to manage activities on this course
  225. $course_context = get_context_instance(CONTEXT_COURSE, $auth_obj->course->get_course_id());
  226. require_capability('moodle/course:manageactivities', $course_context);
  227. // Make sure the type value is stored in the database too
  228. $sloodle->course->controller->update_object_type($auth_obj->uuid, $usetype);
  229. $auth_obj->type = $usetype;
  230. // Make sure the type exists
  231. $objectpath = SLOODLE_DIRROOT."/mod/$usetype";
  232. if (!file_exists($objectpath)) error(get_string('objectnotinstalled','sloodle'));
  233. // Determine if we have a custom configuration page
  234. $hascustomconfig = $auth_obj->has_custom_config();
  235. // Display the configuration section
  236. sloodle_print_box_start('generalbox boxwidthnormal boxaligncenter');
  237. echo '<div style="text-align:center;"><h2>'.get_string('objectconfiguration','sloodle').'</h2>';
  238. // Do we have custom configuration settings?
  239. if ($hascustomconfig) {
  240. // Display our configuration form
  241. echo '<form action="'.SLOODLE_WWWROOT.'/classroom/store_object_config.php" method="POST">';
  242. // Add a hidden field to store the object's type
  243. echo '<input type="hidden" name="sloodleobjtype" value="'.$usetype.'"/>';
  244. // Include the form elements
  245. include('object_configuration_form_template.php');
  246. // Add this object's authorisation ID, and a submit button
  247. echo '<br><input type="hidden" name="sloodleauthid" value="'.$sloodleauthid.'"/>';
  248. if (SLOODLE_DEBUG) echo '<input type="hidden" name="sloodledebug" value="true"/>';
  249. echo '<input type="submit" value="'.get_string('submit','sloodle').'"/>';
  250. echo '</form>';
  251. } else {
  252. // No configuration settings for this object
  253. print_string('noobjectconfig','sloodle');
  254. // Add or udpate a configuration value to store this object's type
  255. $cfgtype = sloodle_get_record('sloodle_object_config', 'object', $sloodleauthid, 'name', 'sloodleobjtype');
  256. if (!$cfgtype) {
  257. $cfgtype = new stdClass();
  258. $cfgtype->object = $sloodleauthid;
  259. $cfgtype->name = 'sloodleobjtype';
  260. $cfgtype->value = $usetype;
  261. sloodle_insert_record('sloodle_object_config', $cfgtype);
  262. } else {
  263. $cfgtype->value = $usetype;
  264. sloodle_update_record('sloodle_object_config', $cfgtype);
  265. }
  266. }
  267. echo '</div>';
  268. sloodle_print_box_end();
  269. }
  270. // Finish
  271. sloodle_print_footer();
  272. ?>