lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /**
  3. * Sloodle module core library functionality.
  4. *
  5. * This script is required by Moodle to contain certain key functionality for the module.
  6. *
  7. * @package sloodle
  8. * @copyright Copyright (c) 2007 Sloodle (various contributors)
  9. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10. *
  11. * @contributor Peter R. Bloomfield
  12. *
  13. */
  14. require_once($CFG->dirroot.'/mod/sloodle/init.php');
  15. require_once(SLOODLE_LIBROOT.'/general.php');
  16. /**
  17. * Processes the module configuration options prior to saving them.
  18. * Only used when the "config.html" file is used for module configuration.
  19. * Moodle 1.9 now prefers the "settings.php" file.
  20. *
  21. * @param object &$config Reference to an object containing the configuration settings (can be edited)
  22. * @return void
  23. */
  24. function sloodle_process_options(&$config)
  25. {
  26. }
  27. /**
  28. * Given an object containing all the necessary data,
  29. * (defined by the form in mod.html) this function
  30. * will create a new instance and return the id number
  31. * of the new instance.
  32. *
  33. * @param object $instance An object from the form in mod.html
  34. * @return int The id of the newly inserted sloodle record
  35. **/
  36. function sloodle_add_instance($sloodle) {
  37. global $CFG;
  38. // Set the creation and modification times
  39. $sloodle->timecreated = time();
  40. $sloodle->timemodified = time();
  41. // Up until 2.0.8-alpha, we prefixed the type name on the instance name.
  42. // We'll stop doing that, because we want teachers to be able to control how things show up on their course.
  43. // Prefix the full type name on the instance name
  44. // $sloodle->name = get_string("moduletype:{$sloodle->type}", 'sloodle') . ': ' . $sloodle->name;
  45. // Attempt to insert the new Sloodle record
  46. if (!$sloodle->id = sloodle_insert_record('sloodle', $sloodle)) {
  47. error(get_string('failedaddinstance', 'sloodle'));
  48. }
  49. // We need to create a new secondary table for this module
  50. $sec_table = new stdClass();
  51. $sec_table->sloodleid = $sloodle->id;
  52. // Check the type of this module
  53. $result = FALSE;
  54. $errormsg = '';
  55. switch ($sloodle->type) {
  56. case SLOODLE_TYPE_CTRL:
  57. // Create a new controller record
  58. if (isset($sloodle->controller_enabled) && $sloodle->controller_enabled) $sec_table->enabled = 1;
  59. else $sec_table->enabled = 0;
  60. $sec_table->password = $sloodle->controller_password;
  61. // Attempt to add it to the database
  62. if (!sloodle_insert_record('sloodle_controller', $sec_table)) {
  63. $errormsg = get_string('failedaddsecondarytable', 'sloodle');
  64. } else {
  65. $result = TRUE;
  66. }
  67. break;
  68. case SLOODLE_TYPE_DISTRIB:
  69. // Add in a default blank channel number and update time
  70. $sec_table->channel = '';
  71. $sec_table->timeupdated = 0;
  72. // Attempt to add it to the database
  73. if (!sloodle_insert_record('sloodle_distributor', $sec_table)) {
  74. $errormsg = get_string('failedaddsecondarytable', 'sloodle');
  75. } else {
  76. $result = TRUE;
  77. }
  78. break;
  79. case SLOODLE_TYPE_PRESENTER:
  80. // Add in the dimensions of the frame
  81. $sec_table->framewidth = (int)$sloodle->presenter_framewidth;
  82. $sec_table->frameheight = (int)$sloodle->presenter_frameheight;
  83. // Attempt to add it to the database
  84. if (!sloodle_insert_record('sloodle_presenter', $sec_table)) {
  85. $errormsg = get_string('failedaddsecondarytable', 'sloodle');
  86. } else {
  87. $result = TRUE;
  88. }
  89. break;
  90. case SLOODLE_TYPE_TRACKER:
  91. // Nothing to add to the secondary table just now - just stick it in the database
  92. if (!sloodle_insert_record('sloodle_tracker', $sec_table)) {
  93. $errormsg = get_string('failedaddsecondarytable', 'sloodle');
  94. } else {
  95. $result = TRUE;
  96. }
  97. break;
  98. // ADD FURTHER MODULE TYPES HERE!
  99. default:
  100. // Type not recognised - this really shouldn't kill the script though... makes development very hard
  101. //$errormsg = error(get_string('moduletypeunknown', 'sloodle'));
  102. $result = TRUE;
  103. break;
  104. }
  105. // Was there a problem?
  106. if (!$result) {
  107. // Yes
  108. // Delete the Sloodle instance
  109. sloodle_delete_records('sloodle', 'id', $sloodle->id);
  110. // Show the error message (if there was one)
  111. if (!empty($errormsg)) error($errormsg);
  112. return FALSE;
  113. }
  114. // Success!
  115. return $sloodle->id;
  116. }
  117. /**
  118. * Given an object containing all the necessary data,
  119. * (defined by the form in mod.html) this function
  120. * will update an existing instance with new data.
  121. *
  122. * @param object $instance An object from the form in mod.html
  123. * @return boolean Success/Fail
  124. **/
  125. function sloodle_update_instance($sloodle) {
  126. global $CFG;
  127. // Update the modification time
  128. $sloodle->timemodified = time();
  129. // Make sure the ID is correct for update
  130. $sloodle->id = $sloodle->instance;
  131. // Make sure the type is the same as the existing record
  132. $existing_record = sloodle_get_record('sloodle', 'id', $sloodle->id);
  133. if (!$existing_record) error(get_string('modulenotfound', 'sloodle'));
  134. if ($existing_record->type != $sloodle->type) error(get_string('moduletypemismatch', 'sloodle'));
  135. // Check the type of this module
  136. switch ($sloodle->type) {
  137. case SLOODLE_TYPE_CTRL:
  138. // Attempt to fetch the controller record
  139. $ctrl = sloodle_get_record('sloodle_controller', 'sloodleid', $sloodle->id);
  140. if (!$ctrl) error(get_string('secondarytablenotfound', 'sloodle'));
  141. // Add the updated 'enabled' value
  142. if (isset($sloodle->controller_enabled) && $sloodle->controller_enabled) $ctrl->enabled = 1;
  143. else $ctrl->enabled = 0;
  144. // Add the updated password
  145. $ctrl->password = $sloodle->controller_password;
  146. // Update the database
  147. sloodle_update_record('sloodle_controller', $ctrl);
  148. break;
  149. case SLOODLE_TYPE_DISTRIB:
  150. // Attempt to fetch the distributor record
  151. $distrib = sloodle_get_record('sloodle_distributor', 'sloodleid', $sloodle->id);
  152. if (!$distrib) error(get_string('secondarytablenotfound', 'sloodle'));
  153. // Has a reset been requested?
  154. if ($sloodle->distributor_reset) {
  155. // Yes - clear the distributor channel
  156. $distrib->channel = '';
  157. // Delete all objects associated with the Distributor
  158. sloodle_delete_records('sloodle_distributor_entry', 'distributorid', $distrib->id);
  159. }
  160. // Update the database
  161. sloodle_update_record('sloodle_distributor', $distrib);
  162. break;
  163. case SLOODLE_TYPE_PRESENTER:
  164. // Attempt to fetch the Presenter record
  165. $presenter = sloodle_get_record('sloodle_presenter', 'sloodleid', $sloodle->id);
  166. if (!$presenter) error(get_string('secondarytablenotfound', 'sloodle'));
  167. // Add the updated frame dimensions
  168. $presenter->framewidth = (int)$sloodle->presenter_framewidth;
  169. $presenter->frameheight = (int)$sloodle->presenter_frameheight;
  170. // Update the database
  171. sloodle_update_record('sloodle_presenter', $presenter);
  172. break;
  173. case SLOODLE_TYPE_TRACKER:
  174. // Attempt to fetch the tracker record
  175. $tracker = sloodle_get_record('sloodle_tracker', 'sloodleid', $sloodle->id);
  176. if (!$tracker) error(get_string('secondarytablenotfound', 'sloodle'));
  177. // Nothing else to do just now...
  178. // Update the database
  179. //sloodle_update_record('sloodle_tracker', $tracker);
  180. break;
  181. break;
  182. // ADD FURTHER MODULE TYPES HERE!
  183. default:
  184. // Type not recognised
  185. //error(get_string('moduletypeunknown', 'sloodle'));
  186. break;
  187. }
  188. // Attempt the update
  189. return sloodle_update_record('sloodle', $sloodle);
  190. }
  191. /**
  192. * Given an ID of an instance of this module,
  193. * this function will permanently delete the instance
  194. * and any data that depends on it.
  195. *
  196. * @param int $id Id of the module instance
  197. * @return boolean Success/Failure
  198. **/
  199. function sloodle_delete_instance($id) {
  200. // Determine our success or otherwise
  201. $result = true;
  202. // Attempt to identify the course module instance ID
  203. $cmid = 0; $moduletype = null; $cm = null;
  204. $moduletype = sloodle_get_record('modules', 'name', 'sloodle');
  205. if ($moduletype && $moduletype->id)
  206. {
  207. $moduletypeid = $moduletype->id;
  208. $cm = sloodle_get_record('course_modules', 'instance', $id, 'module', $moduletypeid);
  209. if ($cm) $cmid = $cm->id;
  210. }
  211. // Attempt to delete the main Sloodle instance
  212. if (!sloodle_delete_records('sloodle', 'id', $id)) {
  213. $result = false;
  214. }
  215. // Delete any secondary controller tables
  216. sloodle_delete_records('sloodle_controller', 'sloodleid', $id);
  217. // Attempt to get any secondary distributor tables
  218. $distribs = sloodle_get_records('sloodle_distributor', 'sloodleid', $id);
  219. if (is_array($distribs) && count($distribs) > 0) {
  220. // Go through each distributor
  221. foreach ($distribs as $d) {
  222. // Delete any related distributor entries
  223. sloodle_delete_records('sloodle_distributor_entry', 'distributorid', $d->id);
  224. }
  225. }
  226. // Delete all the distributors
  227. sloodle_delete_records('sloodle_distributor', 'sloodleid', $id);
  228. // Delete any presenter entries
  229. sloodle_delete_records('sloodle_presenter_entry', 'sloodleid', $id);
  230. // Delete any tracker instances, tools and activities
  231. sloodle_delete_records('sloodle_tracker', 'sloodleid', $id);
  232. if ($cmid)
  233. {
  234. sloodle_delete_records('sloodle_activity_tool', 'trackerid', $cmid);
  235. sloodle_delete_records('sloodle_activity_tracker', 'trackerid', $cmid);
  236. }
  237. // ADD FURTHER MODULE TYPES HERE!
  238. return $result;
  239. }
  240. /**
  241. * Return a small object with summary information about what a
  242. * user has done with a given particular instance of this module
  243. * Used for user activity reports.
  244. * $return->time = the time they did it
  245. * $return->info = a short text description
  246. *
  247. * @return null
  248. * @todo Do we need this function to do anything?
  249. **/
  250. function sloodle_user_outline($course, $user, $mod, $sloodle) {
  251. return NULL;
  252. }
  253. /**
  254. * Print a detailed representation of what a user has done with
  255. * a given particular instance of this module, for user activity reports.
  256. *
  257. * @return boolean
  258. * @todo Do we need this function to do anything?
  259. **/
  260. function sloodle_user_complete($course, $user, $mod, $sloodle) {
  261. return true;
  262. }
  263. /**
  264. * Given a course and a time, this module should find recent activity
  265. * that has occurred in sloodle activities and print it out.
  266. * Return true if there was output, or false is there was none.
  267. *
  268. * @uses $CFG
  269. * @return boolean
  270. * @todo Figure out if we need this function to do anything!
  271. **/
  272. function sloodle_print_recent_activity($course, $isteacher, $timestart) {
  273. global $CFG;
  274. return false; // True if anything was printed, otherwise false
  275. }
  276. /**
  277. * Function to be run periodically according to the Moodle cron.
  278. * Clears out expired pending user entries, session keys, etc.
  279. *
  280. * @return boolean
  281. * @todo Delete expired user entries
  282. * @todo Use a custom delay for expiries of users/objects
  283. **/
  284. function sloodle_cron () {
  285. global $CFG;
  286. echo "\nProcessing Sloodle cron tasks:\n";
  287. // Delete any pending user entries which have expired (more than 30 minutes old)
  288. $expirytime = time() - 1800;
  289. echo "Removing expired pending avatar entries...\n";
  290. sloodle_delete_records_select_params('sloodle_pending_avatars', "timeupdated < ?", array($expirytime));
  291. // Delete any LoginZone allocations which have expired (more than 15 minutes old)
  292. $expirytime = time() - 900;
  293. echo "Removing expired LoginZone allocations...\n";
  294. sloodle_delete_records_select_params('sloodle_loginzone_allocation', "timecreated < ?", array($expirytime));
  295. // Fetch our configuration settings, if they exist
  296. $active_object_days = 7; // Give active objects a week by default
  297. if (!empty($CFG->sloodle_active_object_lifetime)) $active_object_days = $CFG->sloodle_active_object_lifetime;
  298. $user_object_days = 21; // Give user objects 3 weeks by default
  299. if (!empty($CFG->sloodle_user_object_lifetime)) $user_object_days = $CFG->sloodle_user_object_lifetime;
  300. /*
  301. // Edmund Edgar, 2011-11-25: Removing this for now as it'll break objects stored in user inventory with persistent configs.
  302. // We may want to make some new settings for this, but we won't want to use the old ones.
  303. // Delete any active objects and session keys which have expired
  304. // Deletes any authorised objects which have not checked-in for more than X days (where X is specified in module config)
  305. // Deletes any unauthorised objects which have not checked-in for more than 1 hour
  306. $expirytime_auth = time() - (86400 * $active_object_days);
  307. $expirytime_unauth = time() - 3600;
  308. echo "Searching for expired active objects...\n";
  309. $recs = sloodle_get_records_select_params('sloodle_active_object', "(((controllerid = 0 AND userid = 0) OR type = '') AND timeupdated < ?) OR timeupdated < ?", array($expirytime_unauth, $expirytime_auth));
  310. if ($recs) {
  311. // Go through each object
  312. echo "Removing " . count($recs) . " expired active objects and associated configuration settings...\n";
  313. foreach ($recs as $r) {
  314. sloodle_delete_records('sloodle_object_config', 'object', $r->id);
  315. sloodle_delete_records('sloodle_active_object', 'id', $r->id);
  316. }
  317. }
  318. */
  319. // Delete any user-authorised objects which have expired
  320. // (will eventually use custom expiry times, chosen in the configuration)
  321. // Deletes any authorised objects which have not checked-in for more than X days (where X is specified in module config)
  322. // Deletes any unauthorised objects which have not checked-in for more than 1 hour
  323. $expirytime_auth = time() - (86400 * $user_object_days);
  324. $expirytime_unauth = time() - 3600;
  325. echo "Deleting expired user objects...\n";
  326. sloodle_delete_records_select_params('sloodle_user_object', "(authorised = 0 AND timeupdated < ?) OR timeupdated < ?", array( $expirytime_unauth, $expirytime_auth));
  327. // More stuff?
  328. //...
  329. // Email login details to auto-registered avatars in-world
  330. echo "Sending out login notifications...\n";
  331. sloodle_process_login_notifications();
  332. if (defined('SLOODLE_FREEMAIL_ACTIVATE') && SLOODLE_FREEMAIL_ACTIVATE) {
  333. echo "Checking for freemail emails...\n";
  334. require_once 'freemail/lib/freemail_imap_message_handler.php';
  335. require_once 'freemail/lib/freemail_email_processor.php';
  336. require_once 'freemail/lib/freemail_moodle_importer.php';
  337. sloodle_freemail_email_processor::read_mail($CFG, false, false, null, false, true);
  338. }
  339. echo "Done processing Sloodle cron tasks.\n\n";
  340. return true;
  341. }
  342. /**
  343. * Must return an array of grades for a given instance of this module,
  344. * indexed by user. It also returns a maximum allowed grade.
  345. *
  346. * Example:
  347. * $return->grades = array of grades;
  348. * $return->maxgrade = maximum allowed grade;
  349. *
  350. * return $return;
  351. *
  352. * @param int $sloodleid ID of an instance of this module
  353. * @return mixed Null or object with an array of grades and with the maximum grade
  354. **/
  355. function sloodle_grades($sloodleid) {
  356. return NULL;
  357. }
  358. /**
  359. * Must return an array of user records (all data) who are participants
  360. * for a given instance of sloodle. Must include every user involved
  361. * in the instance, independient of his role (student, teacher, admin...)
  362. * See other modules as example.
  363. *
  364. * @param int $sloodleid ID of an instance of this module
  365. * @return mixed boolean/array of students
  366. * @todo Possibly make it return a list of users registered via the Sloodle instance?
  367. **/
  368. function sloodle_get_participants($sloodleid) {
  369. return false;
  370. }
  371. /**
  372. * This function returns if a scale is being used by one sloodle
  373. * it it has support for grading and scales. Commented code should be
  374. * modified if necessary. See forum, glossary or journal modules
  375. * as reference.
  376. *
  377. * @param int $sloodleid ID of an instance of this module
  378. * @return mixed
  379. **/
  380. function sloodle_scale_used ($sloodleid,$scaleid) {
  381. $return = false;
  382. return $return;
  383. }
  384. /**
  385. * Gets the different sub-types of Sloodle module available as a list for the "Add Activity..." menu.
  386. * Also adds the 'Sloodle Map' resource type.
  387. *
  388. * $return array Entries for the "Add Activity..." and "Add Resource..." menus.
  389. */
  390. function sloodle_get_types() {
  391. global $CFG, $SLOODLE_TYPES;
  392. $types = array();
  393. // Start the group of activities
  394. $type = new object();
  395. $type->modclass = MOD_CLASS_ACTIVITY;
  396. $type->type = "sloodle_group_start";
  397. $type->typestr = '--'.get_string('modulenameplural', 'sloodle');
  398. $types[] = $type;
  399. // Go through each Sloodle module type, and add it
  400. foreach ($SLOODLE_TYPES as $st) {
  401. $type = new object();
  402. $type->modclass = MOD_CLASS_ACTIVITY;
  403. $type->type = "sloodle&amp;type=$st";
  404. $type->typestr = get_string("moduletype:$st", 'sloodle');
  405. $types[] = $type;
  406. }
  407. // End the group of activities
  408. $type = new object();
  409. $type->modclass = MOD_CLASS_ACTIVITY;
  410. $type->type = "sloodle_group_end";
  411. $type->typestr = '--';
  412. $types[] = $type;
  413. return $types;
  414. }
  415. /**
  416. * @param string $feature FEATURE_xx constant for requested feature
  417. * @return bool True if sloodle supports feature
  418. */
  419. function sloodle_supports($feature) {
  420. switch($feature) {
  421. case FEATURE_MOD_INTRO: return true;
  422. case FEATURE_BACKUP_MOODLE2: return true;
  423. default: return null;
  424. }
  425. }
  426. function sloodle_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
  427. global $CFG, $DB;
  428. $fs = get_file_storage();
  429. $contextid = $context->id;
  430. $component = 'mod_sloodle';
  431. $itemid = $args[0];
  432. $fullpath = "/$contextid/$component/$filearea/$itemid"."/".$context->id."/mod_sloodle/presenter/".$args[0]."/".$args[1];
  433. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  434. return false;
  435. }
  436. send_stored_file($file, 0, 0, false);
  437. }
  438. ?>