module_sloodleobject.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines a Sloodle Object assignment module for Sloodle.
  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 Sloodle module base. */
  13. require_once(SLOODLE_LIBROOT.'/modules/module_base.php');
  14. /** General Sloodle functions. */
  15. require_once(SLOODLE_LIBROOT.'/general.php');
  16. // Make sure the Sloodle Object assignment type is installed
  17. $incfile = $CFG->dirroot.'/mod/assignment/type/sloodleobject/assignment.class.php';
  18. if (!file_exists($incfile)) return; // Leaves this script, but does not terminate execution
  19. /** The Sloodle Object assignment type. */
  20. require_once($incfile);
  21. /**
  22. * The Sloodle Object assignment module class.
  23. * @package sloodle
  24. */
  25. class SloodleModuleSloodleObject extends SloodleModule
  26. {
  27. // DATA //
  28. /**
  29. * Internal for Moodle only - course module instance.
  30. * Corresponds to one record from the Moodle 'course_modules' table.
  31. * @var object
  32. * @access private
  33. */
  34. var $cm = null;
  35. /**
  36. * Internal only - Moodle assignment module instance database object.
  37. * Corresponds to one record from the Moodle 'assignment' table.
  38. * @var object
  39. * @access private
  40. */
  41. var $moodle_assignment_instance = null;
  42. /**
  43. * Internal only - the Moodle assignment structure.
  44. * @var assignment_sloodleobject
  45. * @access private
  46. */
  47. var $assignment = null;
  48. // FUNCTIONS //
  49. /**
  50. * Constructor
  51. */
  52. function SloodleModuleSloodleObject(&$_session)
  53. {
  54. $constructor = get_parent_class($this);
  55. parent::$constructor($_session);
  56. }
  57. /**
  58. * Loads data from the database.
  59. * Note: even if the function fails, it may still have overwritten some or all existing data in the object.
  60. * @param mixed $id The site-wide unique identifier for all modules. Type depends on VLE. On Moodle, it is an integer course module identifier ('id' field of 'course_modules' table)
  61. * @return bool True if successful, or false otherwise
  62. */
  63. function load($id)
  64. {
  65. // Make sure the ID is valid
  66. $id = (int)$id;
  67. if ($id <= 0) return false;
  68. // Fetch the course module data
  69. if (!($this->cm = get_coursemodule_from_id('assignment', $id))) {
  70. sloodle_debug("Failed to load course module instance #$id.<br/>");
  71. return false;
  72. }
  73. // Make sure the module is visible
  74. if ($this->cm->visible == 0) {
  75. sloodle_debug("Error: course module instance #$id not visible.<br/>");
  76. return false;
  77. }
  78. // Load from the primary table: assignment instance
  79. if (!($this->moodle_assignment_instance = sloodle_get_record('assignment', 'id', $this->cm->instance))) {
  80. sloodle_debug("Failed to load assignment with instance ID #{$cm->instance}.<br/>");
  81. return false;
  82. }
  83. // Make sure this assignment is of the correct type
  84. if ($this->moodle_assignment_instance->assignmenttype != 'sloodleobject') {
  85. sloodle_debug("ERROR assignment \"{$this->moodle_assignment_instance->name}\" is not of type 'sloodleobject' (actual type: '{$this->moodle_assignment_instance->assignmenttype}').");
  86. return false;
  87. }
  88. // Attempt to construct the assignment object
  89. $this->assignment = new assignment_sloodleobject($this->cm->id, $this->moodle_assignment_instance, $this->cm);
  90. return true;
  91. }
  92. /**
  93. * Checks if the specified user is permitted to submit to this assignment.
  94. * (Note: this only checks permissions, and no other settings, such as submission times).
  95. * @param SloodleUser $user The user to be checked
  96. * @return bool True if the user has permission to submit to this assignment, or false otherwise.
  97. */
  98. function user_can_submit($user)
  99. {
  100. // Make sure a user is loaded
  101. if (!$user->is_user_loaded()) return false;
  102. // Login the current user, and check capabilities
  103. if (!$user->login()) return false;
  104. return has_capability('mod/assignment:submit', get_context_instance(CONTEXT_MODULE, $this->cm->id));
  105. }
  106. /**
  107. * Checks if the specified user is permitted to view submissions to this assignment.
  108. * @param SloodleUser $user The user to be checked
  109. * @return bool True if the user has permission to view the submissions, or false otherwise.
  110. */
  111. function user_can_view($user)
  112. {
  113. // Make sure a user is loaded
  114. if (!$user->is_user_loaded()) return false;
  115. // Login the current user, and check capabilities
  116. if (!$user->login()) return false;
  117. return has_capability('mod/assignment:view', get_context_instance(CONTEXT_MODULE, $this->cm->id));
  118. }
  119. /**
  120. * Checks if the specified user has submitted to this assignment already.
  121. * @param SloodleUser $user The user to be checked
  122. * @return bool True if the user has previously attempted this assignment, or false otherwise
  123. */
  124. function user_has_submitted($user)
  125. {
  126. // Make sure a user is loaded
  127. if (!$user->is_user_loaded()) return false;
  128. // Check for a submission by this user
  129. if (sloodle_record_exists('assignment_submissions', 'assignment', $this->moodle_assignment_instance->id, 'userid', $user->get_user_id())) return true;
  130. return false;
  131. }
  132. /**
  133. * Checks if re-submissions are permitted.
  134. * @return bool True if resubmissions are permitted, or false otherwise
  135. */
  136. function resubmit_allowed()
  137. {
  138. if (empty($this->moodle_assignment_instance->resubmit)) return false;
  139. return true;
  140. }
  141. /**
  142. * Checks if an assignment submitted at the specified time would be too early for submission.
  143. * @param int $timestamp A timestamp giving the time to check (if omitted, it defaults to the current timestamp)
  144. * @return bool True if assignment would be too early, or false if it's OK.
  145. */
  146. function is_too_early($timestamp = null)
  147. {
  148. // If no 'available' time is set, then nothing is too early
  149. if (empty($this->moodle_assignment_instance->timeavailable) || $this->moodle_assignment_instance->timeavailable <= 0) return false;
  150. // Use the current timestamp if need be
  151. if ($timestamp == null) $timestamp = time();
  152. // Check the time
  153. return ($timestamp < $this->moodle_assignment_instance->timeavailable);
  154. }
  155. /**
  156. * Checks if an assignment submitted at the specified time would be too late for submission.
  157. * @param int $timestamp A timestamp giving the time to check (if omitted, it defaults to the current timestamp)
  158. * @return int 1 if assignment would be too late and cannot be accepted, 0 if it is OK, or -1 if it would be late but still accepted
  159. */
  160. function is_too_late($timestamp = null)
  161. {
  162. // If no 'due' time is set, then nothing is too early
  163. if (empty($this->moodle_assignment_instance->timedue) || $this->moodle_assignment_instance->timedue <= 0) return false;
  164. // Use the current timestamp if need be
  165. if ($timestamp == null) $timestamp = time();
  166. // Check the time
  167. if ($timestamp > $this->moodle_assignment_instance->timedue) {
  168. // It's late... check if late submissions are prevented
  169. if (empty($this->moodle_assignment_instance->preventlate)) {
  170. return -1;
  171. } else {
  172. return 1;
  173. }
  174. }
  175. // It's OK
  176. return 0;
  177. }
  178. /**
  179. * Add a new submission (or replace an existing one).
  180. * Ignores all submission checks, such as permissions and time.
  181. * @param SloodleUser $user The user making the submission
  182. * @param string $obj_name Name of the object being submitted
  183. * @param int $num_prims Number of prims in the object being submitted
  184. * @param string $primdrop_name Name of the PrimDrop being submitted to
  185. * @param string $primdrop_uuid UUID of the PrimDrop being submitted to
  186. * @param string $primdrop_region Region of the PrimDrop being submitted to
  187. * @param string $primdrop_pos Position vector (<x,y,z>) of the PrimDrop being submitted to
  188. * @return bool True if successful, or false otherwise
  189. */
  190. function submit($user, $obj_name, $num_prims, $primdrop_name, $primdrop_uuid, $primdrop_region, $primdrop_pos)
  191. {
  192. // Make sure the user is loaded
  193. if (!$user->is_user_loaded()) return false;
  194. // Construct a submission object
  195. $sloodle_submission = new assignment_sloodleobject_submission();
  196. $sloodle_submission->obj_name = $obj_name;
  197. $sloodle_submission->num_prims = $num_prims;
  198. $sloodle_submission->primdrop_name = $primdrop_name;
  199. $sloodle_submission->primdrop_uuid = $primdrop_uuid;
  200. $sloodle_submission->primdrop_region = $primdrop_region;
  201. $sloodle_submission->primdrop_pos = $primdrop_pos;
  202. // Update the submission
  203. return $this->assignment->update_submission($user->get_user_id(), $sloodle_submission);
  204. }
  205. // ACCESSORS //
  206. /**
  207. * Gets the name of this module instance.
  208. * @return string The name of this controller
  209. */
  210. function get_name()
  211. {
  212. return $this->moodle_assignment_instance->name;
  213. }
  214. /**
  215. * Gets the intro description of this module instance, if available.
  216. * @return string The intro description of this controller
  217. */
  218. function get_intro()
  219. {
  220. // Changed in Moodle somewhere between 1.9 and 2.3.
  221. return isset($this->moodle_assignment_instance->description) ? $this->moodle_assignment_instance->description : $this->moodle_assignment_instance->intro;
  222. }
  223. /**
  224. * Gets the identifier of the course this controller belongs to.
  225. * @return mixed Course identifier. Type depends on VLE. (In Moodle, it will be an integer).
  226. */
  227. function get_course_id()
  228. {
  229. return (int)$this->moodle_assignment_instance->course;
  230. }
  231. /**
  232. * Gets the time at which this instance was created, or 0 if unknown.
  233. * @return int Timestamp
  234. */
  235. function get_creation_time()
  236. {
  237. return 0;
  238. }
  239. /**
  240. * Gets the time at which this instance was last modified, or 0 if unknown.
  241. * @return int Timestamp
  242. */
  243. function get_modification_time()
  244. {
  245. return (int)$this->moodle_assignment_instance->timemodified;
  246. }
  247. /**
  248. * Gets the short type name of this instance.
  249. * @return string
  250. */
  251. function get_type()
  252. {
  253. return 'sloodleobject';
  254. }
  255. /**
  256. * Gets the full type name of this instance, according to the current language pack, if available.
  257. * Note: should be overridden by sub-classes.
  258. * @return string Full type name if possible, or the short name otherwise.
  259. */
  260. function get_type_full()
  261. {
  262. return get_string('typesloodleobject', 'assignment');
  263. }
  264. }
  265. ?>