From a74cadfa773c3f5dd4767462a42e7dd6cc4a0d1e Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Fri, 10 Jun 2011 16:48:09 +0200 Subject: [PATCH] MDL-27065 workshop grades categories can be defined in mod_form Because the Workshop module creates two grade items in the gradebook, we can't use standard modedit features to set the corresponding category in the gradebook (it supports single grade item only). The patch also cleans the mod_form.php so that it does not use the global $COURSE (just because I do not like this global variable). And for the record - QuickForms must die. --- mod/workshop/lib.php | 37 ++++++++++++++++- mod/workshop/mod_form.php | 86 ++++++++++++++++++++++++++++++++++----- 2 files changed, 111 insertions(+), 12 deletions(-) diff --git a/mod/workshop/lib.php b/mod/workshop/lib.php index f586dec17f5..211acb07a16 100644 --- a/mod/workshop/lib.php +++ b/mod/workshop/lib.php @@ -102,8 +102,9 @@ function workshop_add_instance(stdclass $workshop) { // re-save the record with the replaced URLs in editor fields $DB->update_record('workshop', $workshop); - // update gradebook item + // create gradebook items workshop_grade_item_update($workshop); + workshop_grade_item_category_update($workshop); return $workshop->id; } @@ -150,8 +151,9 @@ function workshop_update_instance(stdclass $workshop) { // re-save the record with the replaced URLs in editor fields $DB->update_record('workshop', $workshop); - // update gradebook item + // update gradebook items workshop_grade_item_update($workshop); + workshop_grade_item_category_update($workshop); return true; } @@ -1100,6 +1102,37 @@ function workshop_update_grades(stdclass $workshop, $userid=0) { workshop_grade_item_update($workshop, $submissiongrades, $assessmentgrades); } +/** + * Update the grade items categories if they are changed via mod_form.php + * + * We must do it manually here in the workshop module because modedit supports only + * single grade item while we use two. + * + * @param stdClass $workshop An object from the form in mod_form.php + */ +function workshop_grade_item_category_update($workshop) { + + $gradeitems = grade_item::fetch_all(array( + 'itemtype' => 'mod', + 'itemmodule' => 'workshop', + 'iteminstance' => $workshop->id, + 'courseid' => $workshop->course)); + + if (!empty($gradeitems)) { + foreach ($gradeitems as $gradeitem) { + if ($gradeitem->itemnumber == 0) { + if ($gradeitem->categoryid != $workshop->gradecategory) { + $gradeitem->set_parent($workshop->gradecategory); + } + } else if ($gradeitem->itemnumber == 1) { + if ($gradeitem->categoryid != $workshop->gradinggradecategory) { + $gradeitem->set_parent($workshop->gradinggradecategory); + } + } + } + } +} + //////////////////////////////////////////////////////////////////////////////// // File API // //////////////////////////////////////////////////////////////////////////////// diff --git a/mod/workshop/mod_form.php b/mod/workshop/mod_form.php index 310ab3a6c02..f7e15c30741 100644 --- a/mod/workshop/mod_form.php +++ b/mod/workshop/mod_form.php @@ -34,16 +34,30 @@ require_once($CFG->dirroot . '/course/moodleform_mod.php'); require_once(dirname(__FILE__) . '/locallib.php'); require_once($CFG->libdir . '/filelib.php'); +/** + * Module settings form for Workshop instances + */ class mod_workshop_mod_form extends moodleform_mod { + /** @var object the course this instance is part of */ + protected $course = null; + + /** + * Constructor + */ + public function __construct($current, $section, $cm, $course) { + $this->course = $course; + parent::__construct($current, $section, $cm, $course); + } + /** * Defines the workshop instance configuration form * * @return void */ - function definition() { + public function definition() { + global $CFG; - global $CFG, $COURSE; $workshopconfig = get_config('workshop'); $mform = $this->_form; @@ -86,16 +100,23 @@ class mod_workshop_mod_form extends moodleform_mod { $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop')); $grades = workshop::available_maxgrades_list(); + $gradecategories = grade_get_categories_menu($this->course->id); $label = get_string('submissiongrade', 'workshop'); - $mform->addElement('select', 'grade', $label, $grades); + $mform->addGroup(array( + $mform->createElement('select', 'grade', '', $grades), + $mform->createElement('select', 'gradecategory', '', $gradecategories), + ), 'submissiongradegroup', $label, ' ', false); $mform->setDefault('grade', $workshopconfig->grade); - $mform->addHelpButton('grade', 'submissiongrade', 'workshop'); + $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop'); $label = get_string('gradinggrade', 'workshop'); - $mform->addElement('select', 'gradinggrade', $label , $grades); + $mform->addGroup(array( + $mform->createElement('select', 'gradinggrade', '', $grades), + $mform->createElement('select', 'gradinggradecategory', '', $gradecategories), + ), 'gradinggradegroup', $label, ' ', false); $mform->setDefault('gradinggrade', $workshopconfig->gradinggrade); - $mform->addHelpButton('gradinggrade', 'gradinggrade', 'workshop'); + $mform->addHelpButton('gradinggradegroup', 'gradinggrade', 'workshop'); $label = get_string('strategy', 'workshop'); $mform->addElement('select', 'strategy', $label, workshop::available_strategies_list()); @@ -126,8 +147,8 @@ class mod_workshop_mod_form extends moodleform_mod { $mform->addElement('select', 'nattachments', $label, $options); $mform->setDefault('nattachments', 1); - $options = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes); - $options[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')'; + $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes); + $options[0] = get_string('courseuploadlimit') . ' ('.display_size($this->course->maxbytes).')'; $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options); $mform->setDefault('maxbytes', $workshopconfig->maxbytes); @@ -183,12 +204,13 @@ class mod_workshop_mod_form extends moodleform_mod { /** * Prepares the form before data are set * - * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core + * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core. + * Grade items are set here because the core modedit supports single grade item only. * * @param array $data to be set * @return void */ - function data_preprocessing(&$data) { + public function data_preprocessing(&$data) { if ($this->current->instance) { // editing an existing workshop - let us prepare the added editor elements (intro done automatically) $draftitemid = file_get_submitted_draft_itemid('instructauthors'); @@ -217,4 +239,48 @@ class mod_workshop_mod_form extends moodleform_mod { $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid); } } + + /** + * Set the grade item categories when editing an instance + */ + public function definition_after_data() { + + $mform =& $this->_form; + + if ($id = $mform->getElementValue('update')) { + $instance = $mform->getElementValue('instance'); + + $gradeitems = grade_item::fetch_all(array( + 'itemtype' => 'mod', + 'itemmodule' => 'workshop', + 'iteminstance' => $instance, + 'courseid' => $this->course->id)); + + if (!empty($gradeitems)) { + foreach ($gradeitems as $gradeitem) { + // here comes really crappy way how to set the value of the fields + // gradecategory and gradinggradecategory - grrr QuickForms + if ($gradeitem->itemnumber == 0) { + $group = $mform->getElement('submissiongradegroup'); + $elements = $group->getElements(); + foreach ($elements as $element) { + if ($element->getName() == 'gradecategory') { + $element->setValue($gradeitem->categoryid); + } + } + } else if ($gradeitem->itemnumber == 1) { + $group = $mform->getElement('gradinggradegroup'); + $elements = $group->getElements(); + foreach ($elements as $element) { + if ($element->getName() == 'gradinggradecategory') { + $element->setValue($gradeitem->categoryid); + } + } + } + } + } + } + + parent::definition_after_data(); + } }