Skip to content

Commit

Permalink
Merge branch 'MDL-31936-26-workshop-reset' of git://github.com/mudrd8…
Browse files Browse the repository at this point in the history
…mz/moodle into MOODLE_26_STABLE
  • Loading branch information
marinaglancy committed Oct 7, 2014
2 parents 6505278 + a9b0e33 commit 5104bb8
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 9 deletions.
6 changes: 6 additions & 0 deletions mod/workshop/lang/en/workshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@
$string['receivedgrades'] = 'Grades received';
$string['recentassessments'] = 'Workshop assessments:';
$string['recentsubmissions'] = 'Workshop submissions:';
$string['resetassessments'] = 'Delete all assessments';
$string['resetassessments_help'] = 'You can choose to delete just allocated assessments without affecting submissions. If submissions are to be deleted, their assessments will be deleted implicitly and this option is ignored. Note this also includes assessments of example submissions.';
$string['resetsubmissions'] = 'Delete all submissions';
$string['resetsubmissions_help'] = 'All the submissions and their assessments will be deleted. This does not affect example submissions.';
$string['resetphase'] = 'Switch to the setup phase';
$string['resetphase_help'] = 'If enabled, all workshops will be put into the initial setup phase.';
$string['saveandclose'] = 'Save and close';
$string['saveandcontinue'] = 'Save and continue editing';
$string['saveandpreview'] = 'Save and preview';
Expand Down
77 changes: 77 additions & 0 deletions mod/workshop/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1700,3 +1700,80 @@ function workshop_calendar_update(stdClass $workshop, $cmid) {
$oldevent->delete();
}
}

////////////////////////////////////////////////////////////////////////////////
// Course reset API //
////////////////////////////////////////////////////////////////////////////////

/**
* Extends the course reset form with workshop specific settings.
*
* @param MoodleQuickForm $mform
*/
function workshop_reset_course_form_definition($mform) {

$mform->addElement('header', 'workshopheader', get_string('modulenameplural', 'mod_workshop'));

$mform->addElement('advcheckbox', 'reset_workshop_submissions', get_string('resetsubmissions', 'mod_workshop'));
$mform->addHelpButton('reset_workshop_submissions', 'resetsubmissions', 'mod_workshop');

$mform->addElement('advcheckbox', 'reset_workshop_assessments', get_string('resetassessments', 'mod_workshop'));
$mform->addHelpButton('reset_workshop_assessments', 'resetassessments', 'mod_workshop');
$mform->disabledIf('reset_workshop_assessments', 'reset_workshop_submissions', 'checked');

$mform->addElement('advcheckbox', 'reset_workshop_phase', get_string('resetphase', 'mod_workshop'));
$mform->addHelpButton('reset_workshop_phase', 'resetphase', 'mod_workshop');
}

/**
* Provides default values for the workshop settings in the course reset form.
*
* @param stdClass $course The course to be reset.
*/
function workshop_reset_course_form_defaults(stdClass $course) {

$defaults = array(
'reset_workshop_submissions' => 1,
'reset_workshop_assessments' => 1,
'reset_workshop_phase' => 1,
);

return $defaults;
}

/**
* Performs the reset of all workshop instances in the course.
*
* @param stdClass $data The actual course reset settings.
* @return array List of results, each being array[(string)component, (string)item, (string)error]
*/
function workshop_reset_userdata(stdClass $data) {
global $CFG, $DB;

if (empty($data->reset_workshop_submissions)
and empty($data->reset_workshop_assessments)
and empty($data->reset_workshop_phase) ) {
// Nothing to do here.
return array();
}

$workshoprecords = $DB->get_records('workshop', array('course' => $data->courseid));

if (empty($workshoprecords)) {
// What a boring course - no workshops here!
return array();
}

require_once($CFG->dirroot . '/mod/workshop/locallib.php');

$course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST);
$status = array();

foreach ($workshoprecords as $workshoprecord) {
$cm = get_coursemodule_from_instance('workshop', $workshoprecord->id, $course->id, false, MUST_EXIST);
$workshop = new workshop($workshoprecord, $cm, $course);
$status = array_merge($status, $workshop->reset_userdata($data));
}

return $status;
}
158 changes: 149 additions & 9 deletions mod/workshop/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,14 @@ public function prepare_example_reference_assessment(stdClass $record, $form = n
*/
public function delete_submission(stdclass $submission) {
global $DB;

$assessments = $DB->get_records('workshop_assessments', array('submissionid' => $submission->id), '', 'id');
$this->delete_assessment(array_keys($assessments));

$fs = get_file_storage();
$fs->delete_area_files($this->context->id, 'mod_workshop', 'submission_content', $submission->id);
$fs->delete_area_files($this->context->id, 'mod_workshop', 'submission_attachment', $submission->id);

$DB->delete_records('workshop_submissions', array('id' => $submission->id));
}

Expand Down Expand Up @@ -1252,21 +1258,39 @@ public function add_allocation(stdclass $submission, $reviewerid, $weight=1, $bu
}

/**
* Delete assessment record or records
* Delete assessment record or records.
*
* @param mixed $id int|array assessment id or array of assessments ids
* @return bool false if $id not a valid parameter, true otherwise
* Removes associated records from the workshop_grades table, too.
*
* @param int|array $id assessment id or array of assessments ids
* @todo Give grading strategy plugins a chance to clean up their data, too.
* @return bool true
*/
public function delete_assessment($id) {
global $DB;

// todo remove all given grades from workshop_grades;
if (empty($id)) {
return true;
}

$fs = get_file_storage();

if (is_array($id)) {
return $DB->delete_records_list('workshop_assessments', 'id', $id);
$DB->delete_records_list('workshop_grades', 'assessmentid', $id);
foreach ($id as $itemid) {
$fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_content', $itemid);
$fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_attachment', $itemid);
}
$DB->delete_records_list('workshop_assessments', 'id', $id);

} else {
return $DB->delete_records('workshop_assessments', array('id' => $id));
$DB->delete_records('workshop_grades', array('assessmentid' => $id));
$fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_content', $id);
$fs->delete_area_files($this->context->id, 'mod_workshop', 'overallfeedback_attachment', $id);
$DB->delete_records('workshop_assessments', array('id' => $id));
}

return true;
}

/**
Expand Down Expand Up @@ -2344,6 +2368,69 @@ public function overall_feedback_attachment_options() {
);
}

/**
* Performs the reset of this workshop instance.
*
* @param stdClass $data The actual course reset settings.
* @return array List of results, each being array[(string)component, (string)item, (string)error]
*/
public function reset_userdata(stdClass $data) {

$componentstr = get_string('pluginname', 'workshop').': '.format_string($this->name);
$status = array();

if (!empty($data->reset_workshop_assessments) or !empty($data->reset_workshop_submissions)) {
// Reset all data related to assessments, including assessments of
// example submissions.
$result = $this->reset_userdata_assessments($data);
if ($result === true) {
$status[] = array(
'component' => $componentstr,
'item' => get_string('resetassessments', 'mod_workshop'),
'error' => false,
);
} else {
$status[] = array(
'component' => $componentstr,
'item' => get_string('resetassessments', 'mod_workshop'),
'error' => $result,
);
}
}

if (!empty($data->reset_workshop_submissions)) {
// Reset all remaining data related to submissions.
$result = $this->reset_userdata_submissions($data);
if ($result === true) {
$status[] = array(
'component' => $componentstr,
'item' => get_string('resetsubmissions', 'mod_workshop'),
'error' => false,
);
} else {
$status[] = array(
'component' => $componentstr,
'item' => get_string('resetsubmissions', 'mod_workshop'),
'error' => $result,
);
}
}

if (!empty($data->reset_workshop_phase)) {
// Do not use the {@link workshop::switch_phase()} here, we do not
// want to trigger events.
$this->reset_phase();
$status[] = array(
'component' => $componentstr,
'item' => get_string('resetsubmissions', 'mod_workshop'),
'error' => false,
);
}

return $status;
}


////////////////////////////////////////////////////////////////////////////////
// Internal methods (implementation details) //
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2582,6 +2669,59 @@ protected function log_convert_url(moodle_url $fullurl) {

return substr($fullurl->out(), strlen($baseurl));
}

/**
* Removes all user data related to assessments (including allocations).
*
* This includes assessments of example submissions as long as they are not
* referential assessments.
*
* @param stdClass $data The actual course reset settings.
* @return bool|string True on success, error message otherwise.
*/
protected function reset_userdata_assessments(stdClass $data) {
global $DB;

$sql = "SELECT a.id
FROM {workshop_assessments} a
JOIN {workshop_submissions} s ON (a.submissionid = s.id)
WHERE s.workshopid = :workshopid
AND (s.example = 0 OR (s.example = 1 AND a.weight = 0))";

$assessments = $DB->get_records_sql($sql, array('workshopid' => $this->id));
$this->delete_assessment(array_keys($assessments));

$DB->delete_records('workshop_aggregations', array('workshopid' => $this->id));

return true;
}

/**
* Removes all user data related to participants' submissions.
*
* @param stdClass $data The actual course reset settings.
* @return bool|string True on success, error message otherwise.
*/
protected function reset_userdata_submissions(stdClass $data) {
global $DB;

$submissions = $this->get_submissions();
foreach ($submissions as $submission) {
$this->delete_submission($submission);
}

return true;
}

/**
* Hard set the workshop phase to the setup one.
*/
protected function reset_phase() {
global $DB;

$DB->set_field('workshop', 'phase', self::PHASE_SETUP, array('id' => $this->id));
$this->phase = self::PHASE_SETUP;
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3399,10 +3539,10 @@ public function get_overall_feedback_content() {
return null;
}

$content = format_text($this->feedbackauthor, $this->feedbackauthorformat,
array('overflowdiv' => true, 'context' => $this->workshop->context));
$content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $this->workshop->context->id,
$content = file_rewrite_pluginfile_urls($this->feedbackauthor, 'pluginfile.php', $this->workshop->context->id,
'mod_workshop', 'overallfeedback_content', $this->id);
$content = format_text($content, $this->feedbackauthorformat,
array('overflowdiv' => true, 'context' => $this->workshop->context));

return $content;
}
Expand Down
59 changes: 59 additions & 0 deletions mod/workshop/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,63 @@ public function create_instance($record = null, array $options = null) {

return parent::create_instance($record, (array)$options);
}

/**
* Generates a submission authored by the given user.
*
* @param int $workshopid Workshop instance id.
* @param int $authorid Author user id.
* @param stdClass|array $options Optional explicit properties.
* @return int The new submission id.
*/
public function create_submission($workshopid, $authorid, $options = null) {
global $DB;

$timenow = time();
$options = (array)$options;

$record = $options + array(
'workshopid' => $workshopid,
'example' => 0,
'authorid' => $authorid,
'timecreated' => $timenow,
'timemodified' => $timenow,
'title' => 'Generated submission',
'content' => 'Generated content',
'contentformat' => FORMAT_MARKDOWN,
'contenttrust' => 0,
);

$id = $DB->insert_record('workshop_submissions', $record);

return $id;
}

/**
* Generates an allocation of the given submission for peer-assessment by the given user
*
* @param int $submissionid Submission id.
* @param int $reviewerid Reviewer's user id.
* @param stdClass|array $options Optional explicit properties.
* @return int The new assessment id.
*/
public function create_assessment($submissionid, $reviewerid, $options = null) {
global $DB;

$timenow = time();
$options = (array)$options;

$record = $options + array(
'submissionid' => $submissionid,
'reviewerid' => $reviewerid,
'weight' => 1,
'timecreated' => $timenow,
'timemodified' => $timenow,
'grade' => null,
);

$id = $DB->insert_record('workshop_assessments', $record);

return $id;
}
}
Loading

0 comments on commit 5104bb8

Please sign in to comment.