MDL-59243 mod_workshop: Move submission creation to API
This commit is contained in:
@@ -2689,6 +2689,32 @@ class workshop {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given user has assessed all his required examples.
|
||||
*
|
||||
* @param int $userid the user to check
|
||||
* @return bool false if there are examples missing assessment, true otherwise.
|
||||
* @since Moodle 3.4
|
||||
*/
|
||||
public function check_examples_assessed($userid) {
|
||||
|
||||
if ($this->useexamples and $this->examplesmode == self::EXAMPLES_BEFORE_SUBMISSION
|
||||
and !has_capability('mod/workshop:manageexamples', $this->context)) {
|
||||
|
||||
// Check that all required examples have been assessed by the user.
|
||||
$examples = $this->get_examples_for_reviewer($userid);
|
||||
foreach ($examples as $exampleid => $example) {
|
||||
if (is_null($example->assessmentid)) {
|
||||
$examples[$exampleid]->assessmentid = $this->add_allocation($example, $userid, 0);
|
||||
}
|
||||
if (is_null($example->grade)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger module viewed event and set the module viewed for completion.
|
||||
*
|
||||
@@ -2714,6 +2740,129 @@ class workshop {
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the submission form or WS data.
|
||||
*
|
||||
* @param array $data the data to be validated
|
||||
* @return array the validation errors (if any)
|
||||
* @since Moodle 3.4
|
||||
*/
|
||||
public function validate_submission_data($data) {
|
||||
global $DB, $USER;
|
||||
|
||||
$errors = array();
|
||||
if (empty($data['id']) and empty($data['example'])) {
|
||||
// Make sure there is no submission saved meanwhile from another browser window.
|
||||
$sql = "SELECT COUNT(s.id)
|
||||
FROM {workshop_submissions} s
|
||||
JOIN {workshop} w ON (s.workshopid = w.id)
|
||||
JOIN {course_modules} cm ON (w.id = cm.instance)
|
||||
JOIN {modules} m ON (m.name = 'workshop' AND m.id = cm.module)
|
||||
WHERE cm.id = ? AND s.authorid = ? AND s.example = 0";
|
||||
|
||||
if ($DB->count_records_sql($sql, array($data['cmid'], $USER->id))) {
|
||||
$errors['title'] = get_string('err_multiplesubmissions', 'mod_workshop');
|
||||
}
|
||||
}
|
||||
|
||||
$getfiles = file_get_drafarea_files($data['attachment_filemanager']);
|
||||
if (empty($getfiles->list) and html_is_blank($data['content_editor']['text'])) {
|
||||
$errors['content_editor'] = get_string('submissionrequiredcontent', 'mod_workshop');
|
||||
$errors['attachment_filemanager'] = get_string('submissionrequiredfile', 'mod_workshop');
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or updates a submission.
|
||||
*
|
||||
* @param stdClass $submission The submissin data (via form or via WS).
|
||||
* @return the new or updated submission id.
|
||||
* @since Moodle 3.4
|
||||
*/
|
||||
public function edit_submission($submission) {
|
||||
global $USER, $DB;
|
||||
|
||||
if ($submission->example == 0) {
|
||||
// This was used just for validation, it must be set to zero when dealing with normal submissions.
|
||||
unset($submission->example);
|
||||
} else {
|
||||
throw new coding_exception('Invalid submission form data value: example');
|
||||
}
|
||||
$timenow = time();
|
||||
if (is_null($submission->id)) {
|
||||
$submission->workshopid = $this->id;
|
||||
$submission->example = 0;
|
||||
$submission->authorid = $USER->id;
|
||||
$submission->timecreated = $timenow;
|
||||
$submission->feedbackauthorformat = editors_get_preferred_format();
|
||||
}
|
||||
$submission->timemodified = $timenow;
|
||||
$submission->title = trim($submission->title);
|
||||
$submission->content = ''; // Updated later.
|
||||
$submission->contentformat = FORMAT_HTML; // Updated later.
|
||||
$submission->contenttrust = 0; // Updated later.
|
||||
$submission->late = 0x0; // Bit mask.
|
||||
if (!empty($this->submissionend) and ($this->submissionend < time())) {
|
||||
$submission->late = $submission->late | 0x1;
|
||||
}
|
||||
if ($this->phase == self::PHASE_ASSESSMENT) {
|
||||
$submission->late = $submission->late | 0x2;
|
||||
}
|
||||
|
||||
// Event information.
|
||||
$params = array(
|
||||
'context' => $this->context,
|
||||
'courseid' => $this->course->id,
|
||||
'other' => array(
|
||||
'submissiontitle' => $submission->title
|
||||
)
|
||||
);
|
||||
$logdata = null;
|
||||
if (is_null($submission->id)) {
|
||||
$submission->id = $DB->insert_record('workshop_submissions', $submission);
|
||||
$params['objectid'] = $submission->id;
|
||||
$event = \mod_workshop\event\submission_created::create($params);
|
||||
$event->trigger();
|
||||
} else {
|
||||
if (empty($submission->id) or empty($submission->id) or ($submission->id != $submission->id)) {
|
||||
throw new moodle_exception('err_submissionid', 'workshop');
|
||||
}
|
||||
}
|
||||
$params['objectid'] = $submission->id;
|
||||
|
||||
// Save and relink embedded images and save attachments.
|
||||
$submission = file_postupdate_standard_editor($submission, 'content', $this->submission_content_options(),
|
||||
$this->context, 'mod_workshop', 'submission_content', $submission->id);
|
||||
|
||||
$submission = file_postupdate_standard_filemanager($submission, 'attachment', $this->submission_attachment_options(),
|
||||
$this->context, 'mod_workshop', 'submission_attachment', $submission->id);
|
||||
|
||||
if (empty($submission->attachment)) {
|
||||
// Explicit cast to zero integer.
|
||||
$submission->attachment = 0;
|
||||
}
|
||||
// Store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten).
|
||||
$DB->update_record('workshop_submissions', $submission);
|
||||
$event = \mod_workshop\event\submission_updated::create($params);
|
||||
$event->add_record_snapshot('workshop', $this->dbrecord);
|
||||
$event->trigger();
|
||||
|
||||
// Send submitted content for plagiarism detection.
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($this->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
|
||||
|
||||
$params['other']['content'] = $submission->content;
|
||||
$params['other']['pathnamehashes'] = array_keys($files);
|
||||
|
||||
$event = \mod_workshop\event\assessable_uploaded::create($params);
|
||||
$event->set_legacy_logdata($logdata);
|
||||
$event->trigger();
|
||||
|
||||
return $submission->id;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Internal methods (implementation details) //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -103,17 +103,7 @@ if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
|
||||
|
||||
$canviewall = $canviewall && $workshop->check_group_membership($submission->authorid);
|
||||
|
||||
if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
|
||||
and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
|
||||
// check that all required examples have been assessed by the user
|
||||
$examples = $workshop->get_examples_for_reviewer($USER->id);
|
||||
foreach ($examples as $exampleid => $example) {
|
||||
if (is_null($example->grade)) {
|
||||
$editable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$editable = $editable && $workshop->check_examples_assessed($USER->id);
|
||||
$edit = ($editable and $edit);
|
||||
|
||||
if (!$candeleteall and $ownsubmission and $editable) {
|
||||
@@ -181,83 +171,12 @@ if ($edit) {
|
||||
redirect($workshop->view_url());
|
||||
|
||||
} elseif ($cansubmit and $formdata = $mform->get_data()) {
|
||||
if ($formdata->example == 0) {
|
||||
// this was used just for validation, it must be set to zero when dealing with normal submissions
|
||||
unset($formdata->example);
|
||||
} else {
|
||||
throw new coding_exception('Invalid submission form data value: example');
|
||||
}
|
||||
$timenow = time();
|
||||
if (is_null($submission->id)) {
|
||||
$formdata->workshopid = $workshop->id;
|
||||
$formdata->example = 0;
|
||||
$formdata->authorid = $USER->id;
|
||||
$formdata->timecreated = $timenow;
|
||||
$formdata->feedbackauthorformat = editors_get_preferred_format();
|
||||
}
|
||||
$formdata->timemodified = $timenow;
|
||||
$formdata->title = trim($formdata->title);
|
||||
$formdata->content = ''; // updated later
|
||||
$formdata->contentformat = FORMAT_HTML; // updated later
|
||||
$formdata->contenttrust = 0; // updated later
|
||||
$formdata->late = 0x0; // bit mask
|
||||
if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
|
||||
$formdata->late = $formdata->late | 0x1;
|
||||
}
|
||||
if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
|
||||
$formdata->late = $formdata->late | 0x2;
|
||||
}
|
||||
|
||||
// Event information.
|
||||
$params = array(
|
||||
'context' => $workshop->context,
|
||||
'courseid' => $workshop->course->id,
|
||||
'other' => array(
|
||||
'submissiontitle' => $formdata->title
|
||||
)
|
||||
);
|
||||
$logdata = null;
|
||||
if (is_null($submission->id)) {
|
||||
$submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
|
||||
$params['objectid'] = $submission->id;
|
||||
$event = \mod_workshop\event\submission_created::create($params);
|
||||
$event->trigger();
|
||||
} else {
|
||||
if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
|
||||
throw new moodle_exception('err_submissionid', 'workshop');
|
||||
}
|
||||
}
|
||||
$params['objectid'] = $submission->id;
|
||||
$formdata->id = $submission->id;
|
||||
// Creates or updates submission.
|
||||
$submission->id = $workshop->edit_submission($formdata);
|
||||
|
||||
// Save and relink embedded images and save attachments.
|
||||
$formdata = file_postupdate_standard_editor($formdata, 'content', $workshop->submission_content_options(),
|
||||
$workshop->context, 'mod_workshop', 'submission_content', $submission->id);
|
||||
|
||||
$formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $workshop->submission_attachment_options(),
|
||||
$workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
|
||||
|
||||
if (empty($formdata->attachment)) {
|
||||
// explicit cast to zero integer
|
||||
$formdata->attachment = 0;
|
||||
}
|
||||
// store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
|
||||
$DB->update_record('workshop_submissions', $formdata);
|
||||
$event = \mod_workshop\event\submission_updated::create($params);
|
||||
$event->add_record_snapshot('workshop', $workshoprecord);
|
||||
$event->trigger();
|
||||
|
||||
// send submitted content for plagiarism detection
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($workshop->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
|
||||
|
||||
$params['other']['content'] = $formdata->content;
|
||||
$params['other']['pathnamehashes'] = array_keys($files);
|
||||
|
||||
$event = \mod_workshop\event\assessable_uploaded::create($params);
|
||||
$event->set_legacy_logdata($logdata);
|
||||
$event->trigger();
|
||||
|
||||
redirect($workshop->submission_url($formdata->id));
|
||||
redirect($workshop->submission_url($submission->id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,25 +74,7 @@ class workshop_submission_form extends moodleform {
|
||||
|
||||
$errors = parent::validation($data, $files);
|
||||
|
||||
if (empty($data['id']) and empty($data['example'])) {
|
||||
// make sure there is no submission saved meanwhile from another browser window
|
||||
$sql = "SELECT COUNT(s.id)
|
||||
FROM {workshop_submissions} s
|
||||
JOIN {workshop} w ON (s.workshopid = w.id)
|
||||
JOIN {course_modules} cm ON (w.id = cm.instance)
|
||||
JOIN {modules} m ON (m.name = 'workshop' AND m.id = cm.module)
|
||||
WHERE cm.id = ? AND s.authorid = ? AND s.example = 0";
|
||||
|
||||
if ($DB->count_records_sql($sql, array($data['cmid'], $USER->id))) {
|
||||
$errors['title'] = get_string('err_multiplesubmissions', 'mod_workshop');
|
||||
}
|
||||
}
|
||||
|
||||
$getfiles = file_get_drafarea_files($data['attachment_filemanager']);
|
||||
if (empty($getfiles->list) and html_is_blank($data['content_editor']['text'])) {
|
||||
$errors['content_editor'] = get_string('submissionrequiredcontent', 'mod_workshop');
|
||||
$errors['attachment_filemanager'] = get_string('submissionrequiredfile', 'mod_workshop');
|
||||
}
|
||||
$errors += $this->_customdata['workshop']->validate_submission_data($data);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user