index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. // This script is part of the Sloodle project
  3. // Created for Avatar Classroom, with the intention of eventually being ported back to regular Sloodle.
  4. // Some assumptions that are true for Avatar Classroom won't be true for arbitrary Moodle sites.
  5. // I'll try to comment these REGULAR SLOODLE TODO.
  6. /*
  7. * This script is intended to be shown on the surface of the Set.
  8. *
  9. * It will need to be passed all the paramters necessary to register the set in Authorized Objects.
  10. * This will include an http-in channel, it will use to send instructions to the set.
  11. *
  12. * It will allow the user to browse their courses, layouts and objects.
  13. * They will then be able to rez layouts by creating AJAX requests which the server will then pass on to the rezzer object.
  14. *
  15. * When used for regular Sloodle, it will have the user login before showing them anything.
  16. * With Avatar Classroom the user will instead login to the Avatar Classroom site.
  17. * The Avatar Classroom site will then proxy to this page, attaching a token which will be used to authenticate the user.
  18. */
  19. /**
  20. * @package sloodleset
  21. * @copyright Copyright (c) 2010 various contributors (see below)
  22. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  23. *
  24. * @contributor Edmund Edgar
  25. *
  26. */
  27. /** Grab the Sloodle/Moodle configuration. */
  28. require_once('../../../init.php');
  29. /** Include the Sloodle PHP API. */
  30. /** Sloodle core library functionality */
  31. require_once(SLOODLE_DIRROOT.'/lib.php');
  32. /** General Sloodle functions. */
  33. require_once(SLOODLE_LIBROOT.'/general.php');
  34. /** Sloodle course data. */
  35. require_once(SLOODLE_LIBROOT.'/course.php');
  36. require_once(SLOODLE_LIBROOT.'/layout_profile.php');
  37. require_once(SLOODLE_LIBROOT.'/active_object.php');
  38. require_once(SLOODLE_LIBROOT.'/user.php');
  39. include('index.template.php');
  40. $object_uuid = required_param('sloodleobjuuid', PARAM_RAW);
  41. $objname = '';
  42. $httpinurl = '';
  43. $sloodleobjuuid = '';
  44. $sloodleavname = '';
  45. //ini_set('display_errors', 1);
  46. //error_reporting(E_ALL);
  47. // Prior to 2012-03-07, we put a whole bunch of parameters in the URL.
  48. // This turned out to have issues with URLs getting too long,
  49. // ...so we changed it to have the rezzer send them direct to the server
  50. // ...where they were saved in the active_objects table.
  51. // For backwards compatibility, we'll keep using the old method if you pass us the full list of parameters.
  52. $baseurl = 'index.php?sloodleobjuuid='.$object_uuid;
  53. if (isset($_REQUEST['sloodleavname'])) {
  54. // Old method
  55. // Deprecated - can remove when no more alpha users are expected
  56. $baseurl = 'index.php?sloodleobjuuid='.htmlentities($_REQUEST['sloodleobjuuid']).'&sloodleavname='.htmlentities($_REQUEST['sloodleavname']).'&sloodleobjuuid='.htmlentities($_REQUEST['sloodleobjuuid']).'&sloodleobjname='.htmlentities($_REQUEST['sloodleobjname']).'&httpinurl='.htmlentities($_REQUEST['httpinurl']);
  57. $httpinurl = optional_param('httpinurl', NULL, PARAM_RAW);
  58. $objname = required_param('sloodleobjname', PARAM_RAW);
  59. $sloodleobjuuid = optional_param('sloodleobjuuid', '', PARAM_RAW);
  60. $sloodleavname = optional_param('sloodleavname', '', PARAM_RAW);
  61. } else {
  62. // We should have an active object record.
  63. $ao = new SloodleActiveObject();
  64. $sloodleuser = new SloodleUser();
  65. $sloodleuser->user_data = $USER;
  66. $timeupdated = 0;
  67. if ( $ao->loadByUUID($object_uuid) ) {
  68. $object_uuid = $ao->uuid;
  69. $httpinurl = $ao->httpinurl;
  70. $objname = $ao->name;
  71. $timeupdated = $ao->timeupdated;
  72. }
  73. // Do legacy sites the legacy way
  74. if (!isset($_REQUEST['sloodleavname'])) {
  75. // For Avatar Classroom, the rezzer config may have been stored on the avatar classroom server.
  76. // This URL should already have a parameter name and an =, if required.
  77. // NB This will fail on PHP < 5.
  78. if (defined('SLOODLE_SHARED_MEDIA_REZZER_CONFIG_WEB_SERVICE') && (SLOODLE_SHARED_MEDIA_REZZER_CONFIG_WEB_SERVICE != '') ) {
  79. // Initializing curl
  80. $ch = curl_init( SLOODLE_SHARED_MEDIA_REZZER_CONFIG_WEB_SERVICE.$_REQUEST['sloodleobjuuid'] );
  81. $options = array(
  82. CURLOPT_RETURNTRANSFER => true,
  83. );
  84. curl_setopt_array( $ch, $options );
  85. if ($result = curl_exec($ch)) {
  86. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  87. if ($http_code == 200) {
  88. $ao_obj = json_decode($result);
  89. // If the server has been updated after our active object list because of a rezzer reset, use the new information.
  90. if ( ( !$timeupdated ) || ($ao_obj->timeupdated >= $timeupdated) ) {
  91. $object_uuid = $ao_obj->uuid;
  92. $httpinurl = $ao_obj->httpinurl;
  93. $objname = $ao_obj->name;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. $hasCourses = false;
  101. $hasControllers = false;
  102. $hasControllersWithPermission = false;
  103. $sitesURL = '';
  104. if (defined('SLOODLE_SHARED_MEDIA_SITE_LIST_BASE_URL') && (SLOODLE_SHARED_MEDIA_SITE_LIST_BASE_URL != '') ) {
  105. $sitesURL = SLOODLE_SHARED_MEDIA_SITE_LIST_BASE_URL.SLOODLE_SHARED_MEDIA_SITE_LIST_TOP.'?sloodleobjuuid='.$object_uuid;
  106. }
  107. if (isset($_GET['logout'])) {
  108. if ( defined('SLOODLE_SHARED_MEDIA_LOGOUT_INCLUDE') && ( SLOODLE_SHARED_MEDIA_LOGOUT_INCLUDE != '' ) ) {
  109. require(SLOODLE_SHARED_MEDIA_LOGOUT_INCLUDE);
  110. } else {
  111. require_logout();
  112. header('Location: '.$baseurl);
  113. exit;
  114. }
  115. }
  116. if ( (!$USER || !$USER->id) || (function_exists('isguestuser') && isguestuser()) ) {
  117. if ( defined('SLOODLE_SHARED_MEDIA_LOGIN_INCLUDE') && ( SLOODLE_SHARED_MEDIA_LOGIN_INCLUDE != '' ) ) {
  118. require(SLOODLE_SHARED_MEDIA_LOGIN_INCLUDE);
  119. } else {
  120. // If we have auto login guests on, we have to simulate a logout first
  121. // ...before require_login will force a login.
  122. if (function_exists('isguestuser') && isguestuser()) {
  123. $USER = null;
  124. }
  125. require_login(null, false);
  126. }
  127. }
  128. if ( defined('SLOODLE_SHARED_MEDIA_AUTOLINK_REZZER_OWNER') && SLOODLE_SHARED_MEDIA_AUTOLINK_REZZER_OWNER ) {
  129. if( ( $sloodleobjuuid != '' ) && ( $sloodleavname != '' ) ) {
  130. $su = new SloodleUser();
  131. if (!$su->load_avatar($sloodleobjuuid, $sloodleavname)) {
  132. if (!$su->load_avatar_by_user_id(intval($USER->id))) {
  133. $su->add_linked_avatar($USER->id, $sloodleobjuuid, $sloodleavname);
  134. }
  135. }
  136. }
  137. }
  138. if (false && !preg_match('/SecondLife/', $_SERVER['HTTP_USER_AGENT']) && !isset($_REQUEST['frame'] ) ) {
  139. $baseurl .= '&frame=1';
  140. echo '<html><body><div style="width:100%; text-align:center"><iframe width="1000" height="1000" src="'.$baseurl.'"></div></body></html>';
  141. exit;
  142. }
  143. // Register the set using URL parameters
  144. $ao = new SloodleActiveObject();
  145. if (!$ao->loadByUUID($object_uuid)) {
  146. $ao->controllerid = 0; // Hope that's OK...
  147. $ao->userid = $USER->id;
  148. $ao->uuid = $object_uuid;
  149. $ao->httpinurl = $httpinurl;
  150. $ao->httpinpassword = sloodle_random_prim_password();
  151. $ao->password = rand(100000,9999999999);
  152. $ao->name = $objname;
  153. $ao->type = 'set-1.0';
  154. $ao->save();
  155. } else {
  156. if ($httpinurl) {
  157. if ($ao->httpinurl != $httpinurl) {
  158. $ao->httpinurl = $httpinurl;
  159. $ao->save();
  160. }
  161. }
  162. }
  163. // A list of all the sites the user has - gets passed by avatar classroom - won't be used for regular sloodle sites
  164. // If supplied, adds an extra layer at the top above courses allowing you to switch between sites.
  165. $sites = ( isset($_REQUEST['sites']) && is_array($_REQUEST['sites']) ) ? $_REQUEST['sites'] : array();
  166. $hasSites = count($sites) > 0;
  167. $courses = get_courses();
  168. $coursesbyid = array();
  169. foreach($courses as $course) {
  170. $id = $course->id;
  171. $coursesbyid[ $id ] = $course;
  172. $hasCourses = true;
  173. }
  174. // Get a list of controllers which the user is permitted to authorise objects on
  175. $controllers = array();
  176. $recs = sloodle_get_records('sloodle', 'type', SLOODLE_TYPE_CTRL);
  177. // Make sure we have at least one controller
  178. if ($recs == false || count($recs) == 0) {
  179. // error(get_string('objectauthnocontrollers','sloodle'));
  180. // exit();
  181. }
  182. foreach ($recs as $r) {
  183. // Fetch the course module
  184. $cm = get_coursemodule_from_instance('sloodle', $r->id);
  185. $hasControllers = true;
  186. // Check that the person can authorise objects of this module
  187. if (has_capability('mod/sloodle:objectauth', get_context_instance(CONTEXT_MODULE, $cm->id))) {
  188. // Store this controller
  189. $controllers[$cm->course][$cm->id] = $r;
  190. $hasControllersWithPermission = true;
  191. }
  192. }
  193. $courselayouts = array();
  194. //$object_configs = SloodleObjectConfig::AllAvailableAsArrayByGroup();
  195. $object_configs = SloodleObjectConfig::AllAvailableAsArray();
  196. $objectconfigsbygroup = SloodleObjectConfig::AllAvailableAsArrayByGroup();
  197. if (!isset($objectconfigsbygroup['misc'])) {
  198. $objectconfigsbygroup['misc'] = array(); // always make sure we have this group so that we can add misc objects added to the rezzer.
  199. }
  200. // include('object_configs.array.php');
  201. // Construct the list of course names
  202. $coursenames = array();
  203. $layoutentries = array();
  204. foreach ($controllers as $cid => $ctrls) {
  205. $sloodle_course = new SloodleCourse();
  206. if (!$sloodle_course->load($cid)) {
  207. continue;
  208. }
  209. $moodle_course = $sloodle_course->get_course_object();
  210. $coursenames[$cid] = $moodle_course->fullname;
  211. $courselayouts[$cid] = array();
  212. foreach($ctrls as $contid => $ctrl) {
  213. $sloodle_course->ensure_at_least_one_layout('Scene ', $contid);
  214. $layouts = $sloodle_course->get_layouts($contid);
  215. $courselayouts[$cid][$contid] = array();
  216. foreach($layouts as $l) {
  217. $entries = $sloodle_course->get_layout_entries_for_layout_id($l->id);
  218. $entriesbygroup = array('communication'=>array(), 'activity'=>array(), 'registration'=>array(), 'misc'=>array() );
  219. foreach($entries as $e) {
  220. $objectname = $e->name;
  221. $grp = 'misc';
  222. if (isset($object_configs[$objectname])) {
  223. $grp = $object_configs[$objectname]->group;
  224. }
  225. if (!isset($entriesbygroup[ $grp ] )) {
  226. $entriesbygroup[ $grp ] = array();
  227. }
  228. $entriesbygroup[ $grp ][] = $e;
  229. }
  230. $layoutentries[$l->id] = $entriesbygroup;
  231. $courselayouts[$cid][$contid][] = $l;
  232. }
  233. }
  234. }
  235. $full = false;
  236. print_html_top();
  237. print_toolbar( $baseurl, $sitesURL );
  238. print_site_placeholder( $sitesURL );
  239. /*
  240. if ($hasSites) {
  241. print_site_list( $sites );
  242. }
  243. */
  244. print_controller_list( $courses, $controllers, $hasSites = false, $sitesURL, $hasCourses, $hasControllers, $hasControllersWithPermission);
  245. print_layout_list( $courses, $controllers, $courselayouts );
  246. print_add_layout_forms( $courses, $controllers, $object_uuid );
  247. print_layout_lists( $courses, $controllers, $courselayouts, $layoutentries, $object_uuid);
  248. print_layout_add_object_groups( $courses, $controllers, $courselayouts, $objectconfigsbygroup );
  249. print_add_object_forms($courses, $controllers, $courselayouts, $object_configs, $object_uuid);
  250. print_edit_object_forms($courses, $controllers, $courselayouts, $object_configs, $layoutentries, $object_uuid);
  251. print_html_bottom();
  252. exit;
  253. ?>