layout_linker.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // This script is part of the Sloodle project
  3. /**
  4. * Layout profile linker script.
  5. * Allows the use and management of layout profiles from within SL.
  6. * See comments in source file for further information.
  7. *
  8. * @package sloodleset
  9. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  10. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11. *
  12. * @contributor Peter R. Bloomfield
  13. *
  14. */
  15. /*
  16. * This script should always be called with the following parameters:
  17. * sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  18. * sloodlepwd = the prim password or object-specific session key to authenticate the request
  19. * sloodleuuid = the UUID of a user agent making the layout profile request
  20. * sloodleavname = the name of an avatar identified by sloodleuuid
  21. *
  22. * There are 3 modes of operation: browse, query, update
  23. * If called with no parameters other than the above, then it adopts browse mode.
  24. * This mode will return a list of all layout profiles in the current course (whichever course the sloodlecontrollerid is part of).
  25. * If successful, the stats code will be 1, and each data line will identify a single layout by name.
  26. *
  27. * Query mode will fetch all entries pertaining to a specific layout.
  28. * To activate this mode the following additional parameter must be specified:
  29. * sloodlelayoutname = the name of a layout profile
  30. *
  31. * If query mode is succesful, it will return status code 1, with one entry per data line, as follows:
  32. * name|position|rotation
  33. *
  34. * The rotation should be a rotation cast to a string... NOT an Euler-angle vector!
  35. *
  36. * Update mode will add or replace entries in a layout.
  37. * To activate this mode, "sloodlelayoutname" must be specified, as well as the following parameter:
  38. * sloodlelayoutentries = a pipe and line-separated list of entries to save against the profile
  39. * The following parameter can also be specified to alter the behaviour (defaults to 'false' if not specified):
  40. * sloodleadd = if 'true', then the given entries are added to the layout instead of replacing them
  41. *
  42. *
  43. * The format for the "sloodlelayoutentries" parameter should be the same as the response from the query mode.
  44. * Due to the potentially large quantity of information this type of query may generate, it is advisable to use POST instead of GET.
  45. */
  46. /** Lets Sloodle know we are in a linker script. */
  47. define('SLOODLE_LINKER_SCRIPT', true);
  48. /** Grab the Sloodle/Moodle configuration. */
  49. require_once('../../init.php');
  50. /** Include the Sloodle PHP API. */
  51. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  52. // Authenticate the request and user, but do not allow auto-registration and enrolment
  53. $sloodle = new SloodleSession();
  54. $sloodle->authenticate_request();
  55. $sloodle->validate_user(true, true, true);
  56. // We need to check certain capabilities
  57. $can_use_layouts = false;
  58. $can_edit_layouts = false;
  59. ///// MOODLE-SPECIFIC /////
  60. $course_context = get_context_instance(CONTEXT_COURSE, $sloodle->course->get_course_id());
  61. $can_use_layouts = has_capability('mod/sloodle:uselayouts', $course_context);
  62. $can_edit_layouts = has_capability('mod/sloodle:editlayouts', $course_context);
  63. ///// END MOODLE-SPECIFIC /////
  64. // If the user cannot use layouts at all, then we cannot do anything
  65. if (!$can_use_layouts) {
  66. $sloodle->response->quick_output(-301, 'USER_AUTH', 'User does not have permission to use layout profiles.', false);
  67. exit();
  68. }
  69. // Get the optional parameters
  70. $sloodlelayoutname = $sloodle->request->optional_param('sloodlelayoutname');
  71. $sloodlelayoutentries = $sloodle->request->optional_param('sloodlelayoutentries');
  72. $sloodleadd = $sloodle->request->optional_param('sloodleadd', 'false');
  73. if (strcasecmp($sloodleadd, 'true') == 0 || $sloodleadd == '1') $sloodleadd = true;
  74. else $sloodleadd = false;
  75. // Determine which mode we're in (0 = browse, 1 = query, 2 = update)
  76. $mode = 0;
  77. if ($sloodlelayoutname === null) $mode = 0;
  78. else if ($sloodlelayoutentries === null) $mode = 1;
  79. else $mode = 2;
  80. // Enter the appropriate mode
  81. switch ($mode) {
  82. case 0:
  83. // BROWSE MODE //
  84. // Fetch the layouts in this course
  85. $layouts = $sloodle->course->get_layout_names();
  86. // Add one data line per layout
  87. $sloodle->response->set_status_code(1);
  88. $sloodle->response->set_status_descriptor('OK');
  89. foreach ($layouts as $id => $name) {
  90. $sloodle->response->add_data_line($name);
  91. }
  92. break;
  93. case 1:
  94. // QUERY MODE //
  95. // Attempt to load the specified profile
  96. $layout_entries = $sloodle->course->get_layout_entries($sloodlelayoutname);
  97. if ($layout_entries === false) {
  98. // Profile not found
  99. $sloodle->response->set_status_code(-902);
  100. $sloodle->response->set_status_descriptor('LAYOUT');
  101. $sloodle->response->add_data_line('Named profile does not exist');
  102. } else {
  103. // Output one entry per line
  104. $sloodle->response->set_status_code(1);
  105. $sloodle->response->set_status_descriptor('OK');
  106. foreach ($layout_entries as $le) {
  107. $sloodle->response->add_data_line(array($le->name, $le->position, $le->rotation, $le->id));
  108. }
  109. }
  110. break;
  111. case 2:
  112. // UPDATE MODE //
  113. // Make sure the user has permission to edit profiles
  114. if ($can_edit_layouts) {
  115. } else {
  116. $sloodle->response->set_status_code(-301);
  117. $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  118. $sloodle->response->add_data_line('User does not have permission to edit layout profiles');
  119. }
  120. // This array will store the new entries for our layout
  121. $entries = array();
  122. // Go through each line of incoming data
  123. $lines = explode("\n", $sloodlelayoutentries);
  124. foreach ($lines as $l) {
  125. // Split the data into separate fields, and check that we have enough in this entry
  126. $fields = explode("|", $l);
  127. if (count($fields) < 3) continue;
  128. // Construct an entry object
  129. $entryobj = new SloodleLayoutEntry();
  130. $entryobj->name = $fields[0];
  131. $entryobj->position = $fields[1];
  132. $entryobj->rotation = $fields[2];
  133. $entryobj->objectuuid = $fields[3];
  134. $entries[] = $entryobj;
  135. }
  136. // Udpate the layout
  137. if ($sloodle->course->save_layout($sloodlelayoutname, $entries, $sloodleadd)) {
  138. $sloodle->response->set_status_code(1);
  139. $sloodle->response->set_status_descriptor('OK');
  140. } else {
  141. $sloodle->response->set_status_code(-901);
  142. $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  143. $sloodle->response->add_data_line('Failed to save new layout');
  144. }
  145. // TODO: Copy the object settings to the layouts table
  146. break;
  147. default:
  148. // Unknown mode
  149. $sloodle->response->set_status_code(-904);
  150. $sloodle->response->set_status_descriptor('LAYOUT_PROFILE');
  151. $sloodle->response->add_data_line('Error determining layout operation');
  152. break;
  153. }
  154. // Render our output
  155. sloodle_debug('<pre>'); // <- to help visualising output in a browser when debugging
  156. $sloodle->response->render_to_output();
  157. sloodle_debug('</pre>');
  158. ?>