PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assignment/type/uploadsingle/assignment.class.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 472 lines | 334 code | 77 blank | 61 comment | 59 complexity | 6392d5ee7861f1510c9f60f9923f80c5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Extend the base assignment class for assignments where you upload a single file
  18. *
  19. * @package mod-assignment
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. require_once($CFG->dirroot.'/mod/assignment/lib.php');
  23. require_once(dirname(__FILE__).'/upload_form.php');
  24. class assignment_uploadsingle extends assignment_base {
  25. function print_student_answer($userid, $return=false){
  26. global $CFG, $USER, $OUTPUT;
  27. $fs = get_file_storage();
  28. $browser = get_file_browser();
  29. $output = '';
  30. if ($submission = $this->get_submission($userid)) {
  31. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  32. foreach ($files as $file) {
  33. $filename = $file->get_filename();
  34. $found = true;
  35. $mimetype = $file->get_mimetype();
  36. $path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_assignment/submission/'.$submission->id.'/'.$filename);
  37. $output .= '<a href="'.$path.'" >'.$OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')).s($filename).'</a><br />';
  38. $output .= plagiarism_get_links(array('userid'=>$userid, 'file'=>$file, 'cmid'=>$this->cm->id, 'course'=>$this->course, 'assignment'=>$this->assignment));
  39. $output .='<br/>';
  40. }
  41. }
  42. }
  43. $output = '<div class="files">'.$output.'</div>';
  44. return $output;
  45. }
  46. function assignment_uploadsingle($cmid='staticonly', $assignment=NULL, $cm=NULL, $course=NULL) {
  47. parent::assignment_base($cmid, $assignment, $cm, $course);
  48. $this->type = 'uploadsingle';
  49. }
  50. function view() {
  51. global $USER, $OUTPUT;
  52. $context = context_module::instance($this->cm->id);
  53. require_capability('mod/assignment:view', $context);
  54. add_to_log($this->course->id, "assignment", "view", "view.php?id={$this->cm->id}", $this->assignment->id, $this->cm->id);
  55. $this->view_header();
  56. $this->view_intro();
  57. $this->view_dates();
  58. $filecount = false;
  59. if ($submission = $this->get_submission($USER->id)) {
  60. $filecount = $this->count_user_files($submission->id);
  61. if ($submission->timemarked) {
  62. $this->view_feedback($submission);
  63. }
  64. if ($filecount) {
  65. echo $OUTPUT->box($this->print_user_files($USER->id, true), 'generalbox boxaligncenter');
  66. }
  67. }
  68. if (is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
  69. $this->view_upload_form();
  70. }
  71. $this->view_footer();
  72. }
  73. /**
  74. * Display the response file to the student
  75. *
  76. * This default method prints the response file
  77. *
  78. * @param object $submission The submission object
  79. */
  80. function view_responsefile($submission) {
  81. $fs = get_file_storage();
  82. $noresponsefiles = $fs->is_area_empty($this->context->id, 'mod_assignment', 'response', $submission->id);
  83. if (!$noresponsefiles) {
  84. echo '<tr>';
  85. echo '<td class="left side">&nbsp;</td>';
  86. echo '<td class="content">';
  87. echo $this->print_responsefiles($submission->userid);
  88. echo '</td></tr>';
  89. }
  90. }
  91. function process_feedback($formdata=null) {
  92. if (!$feedback = data_submitted() or !confirm_sesskey()) { // No incoming data?
  93. return false;
  94. }
  95. $userid = required_param('userid', PARAM_INT);
  96. $offset = required_param('offset', PARAM_INT);
  97. $mform = $this->display_submission($offset, $userid, false);
  98. parent::process_feedback($mform);
  99. }
  100. /**
  101. * Counts all complete (real) assignment submissions by enrolled students. This overrides assignment_base::count_real_submissions().
  102. * This is necessary for simple file uploads where we need to check that the numfiles field is greater than zero to determine if a
  103. * submission is complete.
  104. *
  105. * @param int $groupid (optional) If nonzero then count is restricted to this group
  106. * @return int The number of submissions
  107. */
  108. function count_real_submissions($groupid=0) {
  109. global $DB;
  110. // Grab the context assocated with our course module
  111. $context = context_module::instance($this->cm->id);
  112. // Get ids of users enrolled in the given course.
  113. list($enroledsql, $params) = get_enrolled_sql($context, 'mod/assignment:submit', $groupid);
  114. $params['assignmentid'] = $this->cm->instance;
  115. // Get ids of users enrolled in the given course.
  116. return $DB->count_records_sql("SELECT COUNT('x')
  117. FROM {assignment_submissions} s
  118. LEFT JOIN {assignment} a ON a.id = s.assignment
  119. INNER JOIN ($enroledsql) u ON u.id = s.userid
  120. WHERE s.assignment = :assignmentid AND
  121. s.numfiles > 0", $params);
  122. }
  123. function print_responsefiles($userid, $return=false) {
  124. global $OUTPUT, $PAGE;
  125. $output = $OUTPUT->box_start('responsefiles');
  126. if ($submission = $this->get_submission($userid)) {
  127. $renderer = $PAGE->get_renderer('mod_assignment');
  128. $output .= $renderer->assignment_files($this->context, $submission->id, 'response');
  129. }
  130. $output .= $OUTPUT->box_end();
  131. if ($return) {
  132. return $output;
  133. }
  134. echo $output;
  135. }
  136. function can_manage_responsefiles() {
  137. if (has_capability('mod/assignment:grade', $this->context)) {
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. function view_upload_form() {
  144. global $OUTPUT, $USER;
  145. echo $OUTPUT->box_start('uploadbox');
  146. $fs = get_file_storage();
  147. // edit files in another page
  148. if ($submission = $this->get_submission($USER->id)) {
  149. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  150. $str = get_string('editthisfile', 'assignment');
  151. } else {
  152. $str = get_string('uploadafile', 'assignment');
  153. }
  154. } else {
  155. $str = get_string('uploadafile', 'assignment');
  156. }
  157. echo $OUTPUT->single_button(new moodle_url('/mod/assignment/type/uploadsingle/upload.php', array('contextid'=>$this->context->id, 'userid'=>$USER->id)), $str, 'get');
  158. echo $OUTPUT->box_end();
  159. }
  160. function upload($mform) {
  161. $action = required_param('action', PARAM_ALPHA);
  162. switch ($action) {
  163. case 'uploadresponse':
  164. $this->upload_responsefile($mform);
  165. break;
  166. case 'uploadfile':
  167. $this->upload_file($mform);
  168. }
  169. }
  170. function upload_file($mform) {
  171. global $CFG, $USER, $DB, $OUTPUT;
  172. $viewurl = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  173. if (!is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
  174. redirect($viewurl);
  175. }
  176. $submission = $this->get_submission($USER->id);
  177. $filecount = 0;
  178. if ($submission) {
  179. $filecount = $this->count_user_files($submission->id);
  180. }
  181. if ($this->isopen() && (!$filecount || $this->assignment->resubmit || !$submission->timemarked)) {
  182. if ($submission = $this->get_submission($USER->id)) {
  183. //TODO: change later to ">= 0", to prevent resubmission when graded 0
  184. if (($submission->grade > 0) and !$this->assignment->resubmit) {
  185. redirect($viewurl, get_string('alreadygraded', 'assignment'));
  186. }
  187. }
  188. if ($formdata = $mform->get_data()) {
  189. $fs = get_file_storage();
  190. $submission = $this->get_submission($USER->id, true); //create new submission if needed
  191. $fs->delete_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id);
  192. if ($newfilename = $mform->get_new_filename('assignment_file')) {
  193. $file = $mform->save_stored_file('assignment_file', $this->context->id, 'mod_assignment', 'submission',
  194. $submission->id, '/', $newfilename);
  195. $updates = new stdClass(); //just enough data for updating the submission
  196. $updates->timemodified = time();
  197. $updates->numfiles = 1;
  198. $updates->id = $submission->id;
  199. $DB->update_record('assignment_submissions', $updates);
  200. add_to_log($this->course->id, 'assignment', 'upload', 'view.php?a='.$this->assignment->id, $this->assignment->id, $this->cm->id);
  201. $this->update_grade($submission);
  202. $this->email_teachers($submission);
  203. // Let Moodle know that an assessable file was uploaded (eg for plagiarism detection)
  204. $eventdata = new stdClass();
  205. $eventdata->modulename = 'assignment';
  206. $eventdata->cmid = $this->cm->id;
  207. $eventdata->itemid = $submission->id;
  208. $eventdata->courseid = $this->course->id;
  209. $eventdata->userid = $USER->id;
  210. $eventdata->file = $file; // This is depreceated - please use pathnamehashes instead!
  211. $eventdata->pathnamehashes = array($file->get_pathnamehash());
  212. events_trigger('assessable_file_uploaded', $eventdata);
  213. }
  214. redirect($viewurl, get_string('uploadedfile'));
  215. } else {
  216. redirect($viewurl, get_string('uploaderror', 'assignment')); //submitting not allowed!
  217. }
  218. }
  219. redirect($viewurl);
  220. }
  221. function upload_responsefile($mform) {
  222. global $CFG, $USER, $OUTPUT, $PAGE;
  223. $userid = required_param('userid', PARAM_INT);
  224. $mode = required_param('mode', PARAM_ALPHA);
  225. $offset = required_param('offset', PARAM_INT);
  226. $returnurl = new moodle_url("/mod/assignment/submissions.php", array('id'=>$this->cm->id,'userid'=>$userid,'mode'=>$mode,'offset'=>$offset)); //not xhtml, just url.
  227. if ($formdata = $mform->get_data() and $this->can_manage_responsefiles()) {
  228. $fs = get_file_storage();
  229. $submission = $this->get_submission($userid, true); //create new submission if needed
  230. $fs->delete_area_files($this->context->id, 'mod_assignment', 'response', $submission->id);
  231. if ($newfilename = $mform->get_new_filename('assignment_file')) {
  232. $file = $mform->save_stored_file('assignment_file', $this->context->id,
  233. 'mod_assignment', 'response',$submission->id, '/', $newfilename);
  234. }
  235. redirect($returnurl, get_string('uploadedfile'));
  236. } else {
  237. redirect($returnurl, get_string('uploaderror', 'assignment')); //submitting not allowed!
  238. }
  239. }
  240. function setup_elements(&$mform) {
  241. global $CFG, $COURSE;
  242. $ynoptions = array( 0 => get_string('no'), 1 => get_string('yes'));
  243. $mform->addElement('select', 'resubmit', get_string('allowresubmit', 'assignment'), $ynoptions);
  244. $mform->addHelpButton('resubmit', 'allowresubmit', 'assignment');
  245. $mform->setDefault('resubmit', 0);
  246. $mform->addElement('select', 'emailteachers', get_string('emailteachers', 'assignment'), $ynoptions);
  247. $mform->addHelpButton('emailteachers', 'emailteachers', 'assignment');
  248. $mform->setDefault('emailteachers', 0);
  249. $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
  250. $mform->addElement('select', 'maxbytes', get_string('maximumsize', 'assignment'), $choices);
  251. $mform->setDefault('maxbytes', $CFG->assignment_maxbytes);
  252. $course_context = context_course::instance($COURSE->id);
  253. plagiarism_get_form_elements_module($mform, $course_context, 'mod_assignment');
  254. }
  255. function portfolio_exportable() {
  256. return true;
  257. }
  258. function send_file($filearea, $args, $forcedownload, array $options=array()) {
  259. global $CFG, $DB, $USER;
  260. require_once($CFG->libdir.'/filelib.php');
  261. require_login($this->course, false, $this->cm);
  262. if ($filearea !== 'submission' && $filearea !== 'response') {
  263. return false;
  264. }
  265. $submissionid = (int)array_shift($args);
  266. if (!$submission = $DB->get_record('assignment_submissions', array('assignment'=>$this->assignment->id, 'id'=>$submissionid))) {
  267. return false;
  268. }
  269. if ($USER->id != $submission->userid and !has_capability('mod/assignment:grade', $this->context)) {
  270. return false;
  271. }
  272. $relativepath = implode('/', $args);
  273. $fullpath = '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$submissionid.'/'.$relativepath;
  274. $fs = get_file_storage();
  275. if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  276. return false;
  277. }
  278. send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
  279. }
  280. function extend_settings_navigation($node) {
  281. global $CFG, $USER, $OUTPUT;
  282. // get users submission if there is one
  283. $submission = $this->get_submission();
  284. if (is_enrolled($this->context, $USER, 'mod/assignment:submit')) {
  285. $editable = $this->isopen() && (!$submission || $this->assignment->resubmit || !$submission->timemarked);
  286. } else {
  287. $editable = false;
  288. }
  289. // If the user has submitted something add some related links and data
  290. if (isset($submission->numfiles) AND $submission->numfiles) {
  291. // Add a view link to the settings nav
  292. $link = new moodle_url('/mod/assignment/view.php', array('id'=>$this->cm->id));
  293. $node->add(get_string('viewmysubmission', 'assignment'), $link, navigation_node::TYPE_SETTING);
  294. if (!empty($submission->timemodified)) {
  295. $submissionnode = $node->add(get_string('submitted', 'assignment') . ' ' . userdate($submission->timemodified));
  296. $submissionnode->text = preg_replace('#([^,])\s#', '$1&nbsp;', $submissionnode->text);
  297. $submissionnode->add_class('note');
  298. if ($submission->timemodified <= $this->assignment->timedue || empty($this->assignment->timedue)) {
  299. $submissionnode->add_class('early');
  300. } else {
  301. $submissionnode->add_class('late');
  302. }
  303. }
  304. }
  305. // Check if the user has uploaded any files, if so we can add some more stuff to the settings nav
  306. if ($submission && is_enrolled($this->context, $USER, 'mod/assignment:submit') && $this->count_user_files($submission->id)) {
  307. $fs = get_file_storage();
  308. if ($files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false)) {
  309. $filenode = $node->add(get_string('submission', 'assignment'));
  310. foreach ($files as $file) {
  311. $filename = $file->get_filename();
  312. $mimetype = $file->get_mimetype();
  313. $link = moodle_url::make_pluginfile_url($this->context->id, 'mod_assignment', 'submission', $submission->id, $file->get_filepath(), $filename);
  314. $filenode->add($filename, $link, navigation_node::TYPE_SETTING, null, null, new pix_icon(file_file_icon($file), ''));
  315. }
  316. }
  317. }
  318. }
  319. /**
  320. * creates a zip of all assignment submissions and sends a zip to the browser
  321. */
  322. function download_submissions() {
  323. global $CFG,$DB;
  324. require_once($CFG->libdir.'/filelib.php');
  325. $submissions = $this->get_submissions('','');
  326. if (empty($submissions)) {
  327. print_error('errornosubmissions', 'assignment');
  328. }
  329. $filesforzipping = array();
  330. $fs = get_file_storage();
  331. $groupmode = groups_get_activity_groupmode($this->cm);
  332. $groupid = 0; // All users
  333. $groupname = '';
  334. if ($groupmode) {
  335. $groupid = groups_get_activity_group($this->cm, true);
  336. $groupname = groups_get_group_name($groupid).'-';
  337. }
  338. $filename = str_replace(' ', '_', clean_filename($this->course->shortname.'-'.$this->assignment->name.'-'.$groupname.$this->assignment->id.".zip")); //name of new zip file.
  339. foreach ($submissions as $submission) {
  340. $a_userid = $submission->userid; //get userid
  341. if ((groups_is_member($groupid,$a_userid)or !$groupmode or !$groupid)) {
  342. $a_assignid = $submission->assignment; //get name of this assignment for use in the file names.
  343. $a_user = $DB->get_record("user", array("id"=>$a_userid),'id,username,firstname,lastname'); //get user firstname/lastname
  344. $files = $fs->get_area_files($this->context->id, 'mod_assignment', 'submission', $submission->id, "timemodified", false);
  345. foreach ($files as $file) {
  346. //get files new name.
  347. $fileext = strstr($file->get_filename(), '.');
  348. $fileoriginal = str_replace($fileext, '', $file->get_filename());
  349. $fileforzipname = clean_filename(fullname($a_user) . "_" . $fileoriginal."_".$a_userid.$fileext);
  350. //save file name to array for zipping.
  351. $filesforzipping[$fileforzipname] = $file;
  352. }
  353. }
  354. } // End of foreach
  355. if ($zipfile = assignment_pack_files($filesforzipping)) {
  356. send_temp_file($zipfile, $filename); //send file and delete after sending.
  357. }
  358. }
  359. /**
  360. * Check the given submission is complete. Preliminary rows are often created in the assignment_submissions
  361. * table before a submission actually takes place. This function checks to see if the given submission has actually
  362. * been submitted.
  363. *
  364. * @param stdClass $submission The submission we want to check for completion
  365. * @return bool Indicates if the submission was found to be complete
  366. */
  367. public function is_submitted_with_required_data($submission) {
  368. return ($submission->timemodified AND $submission->numfiles > 0);
  369. }
  370. }
  371. class mod_assignment_uploadsingle_response_form extends moodleform {
  372. function definition() {
  373. $mform = $this->_form;
  374. $instance = $this->_customdata;
  375. // visible elements
  376. $mform->addElement('filepicker', 'assignment_file', get_string('uploadafile'), null, $instance->options);
  377. // hidden params
  378. $mform->addElement('hidden', 'id', $instance->cm->id);
  379. $mform->setType('id', PARAM_INT);
  380. $mform->addElement('hidden', 'contextid', $instance->contextid);
  381. $mform->setType('contextid', PARAM_INT);
  382. $mform->addElement('hidden', 'action', 'uploadresponse');
  383. $mform->setType('action', PARAM_ALPHA);
  384. $mform->addElement('hidden', 'mode', $instance->mode);
  385. $mform->setType('mode', PARAM_ALPHA);
  386. $mform->addElement('hidden', 'offset', $instance->offset);
  387. $mform->setType('offset', PARAM_INT);
  388. $mform->addElement('hidden', 'forcerefresh' , $instance->forcerefresh);
  389. $mform->setType('forcerefresh', PARAM_INT);
  390. $mform->addElement('hidden', 'userid', $instance->userid);
  391. $mform->setType('userid', PARAM_INT);
  392. // buttons
  393. $this->add_action_buttons(false, get_string('uploadthisfile'));
  394. }
  395. }