assignment.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. // This file is part of the Sloodle project (www.sloodle.org)
  3. /**
  4. * This file defines the "Sloodle Object" assignment sub-type.
  5. * It allows students to submit 3d objects in Second Life as Moodle assignments.
  6. *
  7. * @package sloodleprimdrop
  8. * @copyright Copyright (c) 2008 Sloodle (various contributors)
  9. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10. *
  11. * @contributor (various Moodle guys!)
  12. * @contributor Peter R. Bloomfield
  13. */
  14. /** Attempt to include the Sloodle configuration. */
  15. require_once($CFG->dirroot.'/mod/sloodle/sl_config.php');
  16. /** Include the general Sloodle functions. */
  17. require_once($CFG->dirroot.'/mod/sloodle/lib/general.php');
  18. /** Include the base assignment class, if necessary. */
  19. require_once($CFG->dirroot.'/mod/assignment/lib.php');
  20. // The assignment plugin seems to be calling this with require or include rather than require_once or include_once.
  21. // Check if the class exists in case we've already defined it.
  22. // mod/assignment/view.php?id=21
  23. if ( !class_exists( 'assignment_sloodleobject' ) ) {
  24. /**
  25. * Extend the base assignment class for assignments where you submit an SL object in-world.
  26. * This has been modified from the "assignment_online" type.
  27. * @package sloodle
  28. */
  29. class assignment_sloodleobject extends assignment_base {
  30. function assignment_sloodleobject($cmid=0, $assignment=NULL, $cm=NULL, $course=NULL) {
  31. parent::assignment_base($cmid, $assignment, $cm, $course);
  32. $this->type = 'sloodleobject';
  33. }
  34. function view() {
  35. // Bring in the global user data
  36. global $USER;
  37. // Check that this user can view assignments
  38. $context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
  39. require_capability('mod/assignment:view', $context);
  40. // Fetch the submission data
  41. $submission = $this->get_submission();
  42. $sloodle_submission = new assignment_sloodleobject_submission();
  43. $sloodle_submission->load_submission($submission);
  44. $this->view_header();
  45. $this->view_intro();
  46. $this->view_dates();
  47. // Display a texture summary of the submission
  48. $this->print_box_start_compat('center', '70%', '', 0, 'generalbox', 'online');
  49. $sloodle_submission->text_summary(false);
  50. $this->print_box_end_compat();
  51. $this->view_feedback();
  52. $this->view_footer();
  53. }
  54. /*
  55. * Display the assignment dates
  56. */
  57. function view_dates() {
  58. // Bring in the global user and configuration data
  59. global $USER, $CFG;
  60. // Make sure the time available and time due dates are set
  61. if (!$this->assignment->timeavailable && !$this->assignment->timedue) {
  62. return;
  63. }
  64. // Start a display box
  65. $this->print_box_start_compat('center', '', '', 0, 'generalbox', 'dates');
  66. echo '<table>';
  67. // Display the time the assignment is available from
  68. if ($this->assignment->timeavailable) {
  69. echo '<tr><td class="c0">'.get_string('availabledate','assignment').':</td>';
  70. echo ' <td class="c1">'.userdate($this->assignment->timeavailable).'</td></tr>';
  71. }
  72. // Display the time the assignment is due by
  73. if ($this->assignment->timedue) {
  74. echo '<tr><td class="c0">'.get_string('duedate','assignment').':</td>';
  75. echo ' <td class="c1">'.userdate($this->assignment->timedue).'</td></tr>';
  76. }
  77. // Is there a submission by this user?
  78. $submission = $this->get_submission($USER->id);
  79. if ($submission) {
  80. // Convert the submission to a Sloodle assignment object
  81. $sloodle_submission = new assignment_sloodleobject_submission();
  82. $sloodle_submission->load_submission($submission);
  83. // Display the date it was last updated
  84. echo '<tr><td class="c0">'.get_string('lastedited').':</td>';
  85. echo ' <td class="c1">'.userdate($submission->timemodified);
  86. // Display the number of prims in the submission
  87. $num_prims = $sloodle_submission->num_prims;
  88. if ($num_prims == 0) $num_prims = '?';
  89. echo ' ('.get_string('numprims', 'sloodle', $num_prims).')</td></tr>';
  90. }
  91. echo '</table>';
  92. $this->print_box_end_compat();
  93. }
  94. /**
  95. * Update the submission with the provided data.
  96. * @param int $userid Integer ID of the user making the submission
  97. * @param assignment_sloodleobject_submission $data A structure containing data about the Sloodle assignment submission
  98. * @return bool True if successful, or false otherwise.
  99. */
  100. function update_submission($userid, $data)
  101. {
  102. $submission = $this->get_submission($userid, true);
  103. $update = new object();
  104. $update->id = $submission->id;
  105. $update->data1 = "{$data->obj_name}|{$data->num_prims}";
  106. $update->data2 = "{$data->primdrop_name}|{$data->primdrop_uuid}|{$data->primdrop_region}|{$data->primdrop_pos}";
  107. $update->timemodified = time();
  108. return sloodle_update_record('assignment_submissions', $update);
  109. }
  110. function print_student_answer($userid, $return=true){
  111. global $CFG;
  112. $text = '';
  113. if (!($submission = $this->get_submission($userid))) {
  114. $text = '';
  115. } else {
  116. // Output the Submission data
  117. $sloodle_submission = new assignment_sloodleobject_submission();
  118. $sloodle_submission->load_submission($submission);
  119. //$text = '<b>'.shorten_text(trim(strip_tags($sloodle_submission->obj_name)), 20).'</b><br>';
  120. $text = '<div class="files">'.
  121. '<img src="'.$CFG->pixpath.'/f/html.gif" class="icon" alt="html" />'.
  122. link_to_popup_window ($CFG->wwwroot.'/mod/assignment/type/sloodleobject/file.php?id='.$this->cm->id.'&userid='.
  123. $submission->userid, 'file'.$userid, shorten_text(trim(strip_tags($sloodle_submission->obj_name)), 20), 450, 580,
  124. get_string('submission', 'assignment'), 'none', true).
  125. '</div>';
  126. }
  127. if ($return) return $text;
  128. echo $text;
  129. }
  130. function print_user_files($userid, $return=false) {
  131. global $CFG;
  132. if (!$submission = $this->get_submission($userid)) {
  133. return '';
  134. }
  135. // Construct a Sloodle submission object
  136. $sloodle_submission = new assignment_sloodleobject_submission();
  137. $sloodle_submission->load_submission($submission);
  138. // Display the number of prims
  139. $num_prims = $sloodle_submission->num_prims;
  140. if ($num_prims == 0) $num_prims = '?';
  141. $this->print_box_start_compat('center', '', '', 0, 'generalbox', 'wordcount');
  142. echo ' ('.get_string('numprims', 'sloodle', $num_prims).')';
  143. $this->print_box_end_compat();
  144. // Display the text summary of this submission
  145. $this->print_box_compat($sloodle_submission->text_summary());
  146. }
  147. function preprocess_submission(&$submission) {
  148. }
  149. function setup_elements(&$mform) {
  150. global $CFG, $COURSE;
  151. $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
  152. $mform->addElement('select', 'resubmit', get_string("allowresubmit", "assignment"), $ynoptions);
  153. //$mform->setHelpButton('resubmit', array('resubmit', get_string('allowresubmit', 'assignment'), 'assignment'));
  154. $mform->setDefault('resubmit', 0);
  155. $mform->addElement('select', 'emailteachers', get_string("emailteachers", "assignment"), $ynoptions);
  156. //$mform->setHelpButton('emailteachers', array('emailteachers', get_string('emailteachers', 'assignment'), 'assignment'));
  157. $mform->setDefault('emailteachers', 0);
  158. }
  159. function print_box_compat($message) {
  160. global $OUTPUT;
  161. if ($OUTPUT && method_exists($OUTPUT, 'box')) {
  162. print $OUTPUT->box($message);
  163. } else {
  164. print_simple_box($message);
  165. }
  166. }
  167. function print_box_start_compat($p1, $p2, $p3, $p4, $p5, $p6) {
  168. global $OUTPUT;
  169. if ($OUTPUT && method_exists($OUTPUT, 'box_start')) {
  170. print $OUTPUT->box_start();
  171. } else {
  172. print_simple_box_start($p1, $p2, $p3, $p4, $p5, $p6);
  173. }
  174. }
  175. function print_box_end_compat() {
  176. global $OUTPUT;
  177. if ($OUTPUT && method_exists($OUTPUT, 'box_end')) {
  178. print $OUTPUT->box_end();
  179. } else {
  180. print_simple_box_end();
  181. }
  182. }
  183. }
  184. /**
  185. * Defines a submission for a Sloodle assignment.
  186. * @package sloodle
  187. */
  188. class assignment_sloodleobject_submission
  189. {
  190. /**
  191. * Indicates whether or not a submission is loaded
  192. * @var bool
  193. * @access public
  194. */
  195. var $is_loaded = false;
  196. /**
  197. * Name of the object which has been submitted.
  198. * @var string
  199. * @access public
  200. */
  201. var $obj_name = '';
  202. /**
  203. * Number of prims in the object which has been submitted
  204. * @var int
  205. * @access public
  206. */
  207. var $num_prims = 0;
  208. /**
  209. * Name of the PrimDrop object into which this object was submitted.
  210. * @var string
  211. * @access public
  212. */
  213. var $primdrop_name = '';
  214. /**
  215. * UUID of the PrimDrop object into which this object was submitted.
  216. * @var string
  217. * @access public
  218. */
  219. var $primdrop_uuid = '';
  220. /**
  221. * Name of the region where this object was submitted
  222. * @var string
  223. * @access public
  224. */
  225. var $primdrop_region = '';
  226. /**
  227. * Position where this object was submitted (as a vector, "<x,y,z>")
  228. * @var string
  229. * @access public
  230. */
  231. var $primdrop_pos = '';
  232. /**
  233. * Parses the data from a Submission database record object.
  234. * @var object $submission The submission database record object.
  235. */
  236. function load_submission($submission)
  237. {
  238. // Make sure the submission is valid
  239. $this->is_loaded = false;
  240. if ($submission == false || $submission == null) return;
  241. // Parse the data lines
  242. $object_data = explode('|', $submission->data1);
  243. $primdrop_data = explode('|', $submission->data2);
  244. // Make sure there's enough data in both
  245. if (count($object_data) < 2 || count($primdrop_data) < 4) return;
  246. // Extract the data items
  247. $this->obj_name = $object_data[0];
  248. $this->num_prims = (int)$object_data[1];
  249. $this->primdrop_name = $primdrop_data[0];
  250. $this->primdrop_uuid = $primdrop_data[1];
  251. $this->primdrop_region = $primdrop_data[2];
  252. $this->primdrop_pos = $primdrop_data[3];
  253. $this->is_loaded = true;
  254. }
  255. /**
  256. * Sets the position as 3 components.
  257. * @param float x The X coordinate
  258. * @param float y The Y coordinate
  259. * @param float z The Z coordinate
  260. */
  261. function set_pos($x, $y, $z)
  262. {
  263. $this->primdrop_pos = "<$x,$y,$z>";
  264. }
  265. /**
  266. * Gets the X coordinate of the position
  267. * @return float The X coordinate of the position
  268. */
  269. function get_x()
  270. {
  271. $arr = sloodle_vector_to_array($this->primdrop_pos);
  272. if (!$arr) return 0.0;
  273. return (float)$arr['x'];
  274. }
  275. /**
  276. * Gets the Y coordinate of the position
  277. * @return float The Y coordinate of the position
  278. */
  279. function get_y()
  280. {
  281. $arr = sloodle_vector_to_array($this->primdrop_pos);
  282. if (!$arr) return 0.0;
  283. return (float)$arr['y'];
  284. }
  285. /**
  286. * Gets the Z coordinate of the position
  287. * @return float The Z coordinate of the position
  288. */
  289. function get_z()
  290. {
  291. $arr = sloodle_vector_to_array($this->primdrop_pos);
  292. if (!$arr) return 0.0;
  293. return (float)$arr['z'];
  294. }
  295. /**
  296. * Construct a text summary of this submission.
  297. * @param bool $return If TRUE (default) then the text will be submitted instead of printed.
  298. * @return string If parameter $return was TRUE, then it returns the string. Otherwise, an empty string.
  299. */
  300. function text_summary($return = true)
  301. {
  302. // Make sure something is loaded
  303. if (!$this->is_loaded) {
  304. $text = get_string('emptysubmission', 'assignment');
  305. } else {
  306. $text = "Object name: <b>{$this->obj_name}</b><br><br>";
  307. $text .= "Submitted to: {$this->primdrop_name}<br>";
  308. $text .= " <i>({$this->primdrop_uuid})</i><br><br>";
  309. $arr = sloodle_vector_to_array($this->primdrop_pos);
  310. if (!$arr) $arr = array('x'=>0, 'y'=>0, 'z'=>0);
  311. else {
  312. $arr['x'] = round($arr['x']);
  313. $arr['y'] = round($arr['y']);
  314. $arr['z'] = round($arr['z']);
  315. }
  316. $loc = "secondlife://{$this->primdrop_region}/{$arr['x']}/{$arr['y']}/{$arr['z']}";
  317. $text .= "<b><a href=\"$loc\">$loc</a></b><br>";
  318. }
  319. if ($return) return $text;
  320. echo $text;
  321. return '';
  322. }
  323. }
  324. }
  325. ?>