From b36c0b58dd06f86a43d75a1ed9b34f223af3a815 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 22 Feb 2013 16:15:32 +0800 Subject: [PATCH] Introduced the grade element --- elements/element.class.php | 80 ++++--- .../grade/lang/en/customcertelement_grade.php | 40 ++++ elements/grade/lib.php | 198 ++++++++++++++++++ elements/grade/version.php | 30 +++ elements/studentname/version.php | 7 +- lang/en/customcert.php | 1 + 6 files changed, 324 insertions(+), 32 deletions(-) create mode 100644 elements/grade/lang/en/customcertelement_grade.php create mode 100644 elements/grade/lib.php create mode 100644 elements/grade/version.php diff --git a/elements/element.class.php b/elements/element.class.php index b01edab..4c5129a 100644 --- a/elements/element.class.php +++ b/elements/element.class.php @@ -83,12 +83,6 @@ class customcert_element_base { // The identifier. $id = $this->element->id; - // The label. - $label = get_string('pluginname', 'customcertelement_' . $this->element->element); - - // String we are going to use often. - $strrequired = get_string('required'); - // The common group of elements. $group = array(); $group[] = $mform->createElement('select', 'font_' . $id, '', customcert_get_fonts()); @@ -98,30 +92,11 @@ class customcert_element_base { $group[] = $mform->createElement('text', 'posy_' . $id, '', array('size' => 10)); // Add this group. - $mform->addElement('group', 'elementfieldgroup_' . $id, $label, $group, - array(' ' . get_string('fontsize', 'customcert') . ' ', ' ' . get_string('colour', 'customcert') . ' ', - ' ' . get_string('posx', 'customcert') . ' ', ' ' . get_string('posy', 'customcert') . ' '), false); + $mform->addElement('group', 'elementfieldgroup_' . $id, get_string('pluginname', 'customcertelement_' . $this->element->element), + $group, array(' ' . get_string('fontsize', 'customcert') . ' ', ' ' . get_string('colour', 'customcert') . ' ', + ' ' . get_string('posx', 'customcert') . ' ', ' ' . get_string('posy', 'customcert') . ' '), false); - // Set the types of these elements. - $mform->setType('font_' . $id, PARAM_TEXT); - $mform->setType('size_' . $id, PARAM_INT); - $mform->setType('colour_' . $id, PARAM_RAW); // Need to validate this is a hexadecimal value. - $mform->setType('posx_' . $id, PARAM_INT); - $mform->setType('posy_' . $id, PARAM_INT); - - // Add some rules. - $grouprule = array(); - $grouprule['colour_' . $id][] = array(null, 'required', null, 'client'); - $grouprule['posx_' . $id][] = array(null, 'required', null, 'client'); - $grouprule['posy_' . $id][] = array(null, 'required', null, 'client'); - $mform->addGroupRule('elementfieldgroup_' . $id, $grouprule); - - // Set the values of these elements. - $mform->setDefault('font_' . $id, $this->element->font); - $mform->setDefault('size_' . $id, $this->element->size); - $mform->setDefault('colour_' . $id, $this->element->colour); - $mform->setDefault('posx_' . $id, $this->element->posx); - $mform->setDefault('posy_' . $id, $this->element->posy); + $this->set_form_element_types($mform); if ($numtimesadded == 0) { $mform->addHelpButton('elementfieldgroup_' . $id, 'commonformelements', 'customcert'); @@ -197,6 +172,7 @@ class customcert_element_base { $id = $this->element->id; // Get the name of the fields we want from the form. + $datainfo = $this->save_unique_data($data); $font = 'font_' . $id; $size = 'size_' . $id; $colour = 'colour_' . $id; @@ -206,6 +182,7 @@ class customcert_element_base { // Get the data from the form. $element = new stdClass(); $element->id = $id; + $element->data = $datainfo; $element->font = (!empty($data->$font)) ? $data->$font : null; $element->size = (!empty($data->$size)) ? $data->$size : null; $element->colour = (!empty($data->$colour)) ? ltrim($data->$colour, "#") : null; @@ -216,6 +193,17 @@ class customcert_element_base { $DB->update_record('customcert_elements', $element); } + /** + * This will handle how form data will be saved into the data column in the + * customcert column. + * Can be overriden if more functionality is needed. + * + * @param stdClass $data the form data. + */ + public function save_unique_data($data) { + return null; + } + /** * Handles displaying the element on the pdf. * Must be overriden. @@ -238,4 +226,38 @@ class customcert_element_base { return $DB->delete_records('customcert_elements', array('id' => $this->element->id)); } + + + /** + * Helper function that sets the types, defaults and rules for the common elements. + * + * @param stdClass $mform the edit_form instance. + * @return array the form elements + */ + public function set_form_element_types($mform) { + + // The identifier. + $id = $this->element->id; + + // Set the types of these elements. + $mform->setType('font_' . $id, PARAM_TEXT); + $mform->setType('size_' . $id, PARAM_INT); + $mform->setType('colour_' . $id, PARAM_RAW); // Need to validate this is a hexadecimal value. + $mform->setType('posx_' . $id, PARAM_INT); + $mform->setType('posy_' . $id, PARAM_INT); + + // Add some rules. + $grouprule = array(); + $grouprule['colour_' . $id][] = array(null, 'required', null, 'client'); + $grouprule['posx_' . $id][] = array(null, 'required', null, 'client'); + $grouprule['posy_' . $id][] = array(null, 'required', null, 'client'); + $mform->addGroupRule('elementfieldgroup_' . $id, $grouprule); + + // Set the values of these elements. + $mform->setDefault('font_' . $id, $this->element->font); + $mform->setDefault('size_' . $id, $this->element->size); + $mform->setDefault('colour_' . $id, $this->element->colour); + $mform->setDefault('posx_' . $id, $this->element->posx); + $mform->setDefault('posy_' . $id, $this->element->posy); + } } \ No newline at end of file diff --git a/elements/grade/lang/en/customcertelement_grade.php b/elements/grade/lang/en/customcertelement_grade.php new file mode 100644 index 0000000..24fa438 --- /dev/null +++ b/elements/grade/lang/en/customcertelement_grade.php @@ -0,0 +1,40 @@ +. + +/** + * Strings for component 'customcertelement_grade', language 'en'. + * + * @package customcertelement_grade + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['gradeformelements'] = 'Grade form elements'; +$string['gradeformelements_help'] = 'These are the most common attributes shared between multiple customcert elements.

+Grade: The grade item you wish to display the grade of.
+Grade Format: The format you wish to use when displaying the grade.
+Font Size: This is the size of the font in points.
+Colour: This is the colour of the font.
+Position x: This is the position in pixels from the top left corner you wish the element to display in the x direction.
+Position Y: This is the position in pixels from the top left corner you wish the element to display in the y direction.
'; +$string['gradeformat'] = 'Grade format'; +$string['gradeitem'] = 'Grade item'; +$string['gradepercent'] = 'Percentage Grade'; +$string['gradepoints'] = 'Points Grade'; +$string['gradeletter'] = 'Letter Grade'; +$string['coursegrade'] = 'Course grade'; +$string['pluginname'] = 'Grade'; diff --git a/elements/grade/lib.php b/elements/grade/lib.php new file mode 100644 index 0000000..c900cfe --- /dev/null +++ b/elements/grade/lib.php @@ -0,0 +1,198 @@ +. + +/** + * The grade elements core interaction API. + * + * @package customcertelement_grade + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); +require_once($CFG->dirroot . '/grade/lib.php'); +require_once($CFG->dirroot . '/grade/querylib.php'); + +class customcert_element_grade extends customcert_element_base { + + /** + * Constructor. + * + * @param stdClass $element the element data + */ + function __construct($element) { + parent::__construct($element); + } + + /** + * This function renders the form elements when adding a customcert element. + * + * @param stdClass $mform the edit_form instance. + * @return array the form elements + */ + public function render_form_elements($mform) { + // Keep track of the number of times these elements have been + // added, so we only add the help icon once. + static $numtimesadded = 0; + + // The identifier. + $id = $this->element->id; + + $gradeinfo = json_decode($this->element->data); + $gradeitem = $gradeinfo->gradeitem; + $gradeformat = $gradeinfo->gradeformat; + + // The common group of elements. + $group = array(); + $group[] = $mform->createElement('select', 'gradeitem_' . $id, '', $this->get_grade_items()); + $group[] = $mform->createElement('select', 'gradeformat_' . $id, '', $this->get_grade_format_options()); + $group[] = $mform->createElement('select', 'font_' . $id, '', customcert_get_fonts()); + $group[] = $mform->createElement('select', 'size_' . $id, '', customcert_get_font_sizes()); + $group[] = $mform->createELement('text', 'colour_' . $id, '', array('size' => 10, 'maxlength' => 6)); + $group[] = $mform->createElement('text', 'posx_' . $id, '', array('size' => 10)); + $group[] = $mform->createElement('text', 'posy_' . $id, '', array('size' => 10)); + + // Add this group. + $mform->addElement('group', 'elementfieldgroup_' . $id, get_string('pluginname', 'customcertelement_grade'), $group, + array(' ' . get_string('gradeformat', 'customcertelement_grade') . ' ', ' ' . get_string('font', 'customcert') . ' ', + ' ' . get_string('fontsize', 'customcert') . ' ', ' ' . get_string('colour', 'customcert') . ' ', + ' ' . get_string('posx', 'customcert') . ' ', ' ' . get_string('posy', 'customcert') . ' '), false); + + $this->set_form_element_types($mform); + + $mform->setDefault('gradeitem_' . $id, $gradeitem); + $mform->setDefault('gradeformat_' . $id, $gradeformat); + + if ($numtimesadded == 0) { + $mform->addHelpButton('elementfieldgroup_' . $id, 'gradeformelements', 'customcertelement_grade'); + } + + $numtimesadded++; + } + + /** + * This will handle how form data will be saved into the data column in the + * customcert column. + * + * @param stdClass $data the form data. + */ + public function save_unique_data($data) { + + // The identifier. + $id = $this->element->id; + + // Get the grade item and format from the form. + $gradeitem = 'gradeitem_' . $id; + $gradeitem = $data->$gradeitem; + $gradeformat = 'gradeformat_' . $id; + $gradeformat = $data->$gradeformat; + + // Array of data we will be storing in the database. + $arrtostore = array( + 'gradeitem' => $gradeitem, + 'gradeformat' => $gradeformat + ); + + // Encode these variables before saving into the DB. + return json_encode($arrtostore); + + } + + /** + * Handles displaying the element on the pdf. + * + * @param $pdf the pdf object, see lib/pdflib.php + * @todo functionality missing, add when we start rendering the pdf + */ + public function display($pdf) { + global $USER; + + // TO DO. + } + + /** + * Helper function to return all the grades items for this course. + */ + public function get_grade_items() { + global $COURSE, $DB; + + $strtopic = get_string("topic"); + $strweek = get_string("week"); + $strsection = get_string("section"); + + // Array to store the grade items. + $modules = array(); + $modules['coursegrade'] = get_string('coursegrade', 'customcertelement_grade'); + + // Collect course modules data + $modinfo = get_fast_modinfo($COURSE); + $mods = $modinfo->get_cms(); + $sections = $modinfo->get_section_info_all(); + + // Create the section label depending on course format. + switch ($COURSE->format) { + case "topics": $sectionlabel = $strtopic; + case "weeks": $sectionlabel = $strweek; + default: $sectionlabel = $strsection; + } + + // Loop through each course section. + for ($i = 0; $i <= count($sections) - 1; $i++) { + // Confirm the index exists, should always be true. + if (isset($sections[$i])) { + // Get the individual section. + $section = $sections[$i]; + // Get the mods for this section. + $sectionmods = explode(",", $section->sequence); + // Loop through the section mods. + foreach ($sectionmods as $sectionmod) { + // Should never happen unless DB is borked. + if (empty($mods[$sectionmod])) { + continue; + } + $mod = $mods[$sectionmod]; + $mod->courseid = $COURSE->id; + $instance = $DB->get_record($mod->modname, array('id' => $mod->instance)); + // Get the grade items for this activity. + if ($grade_items = grade_get_grade_items_for_activity($mod)) { + $mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); + $item = reset($mod_item->items); + if (isset($item->grademax)) { + $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; + } + } + } + } + } + + return $modules; + } + + /** + * Helper function to return all the possible grade formats. + */ + function get_grade_format_options() { + $gradeformat = array(); + $gradeformat[1] = get_string('gradepercent', 'customcertelement_grade'); + $gradeformat[2] = get_string('gradepoints', 'customcertelement_grade'); + $gradeformat[3] = get_string('gradeletter', 'customcertelement_grade'); + + return $gradeformat; + } +} \ No newline at end of file diff --git a/elements/grade/version.php b/elements/grade/version.php new file mode 100644 index 0000000..5450b0b --- /dev/null +++ b/elements/grade/version.php @@ -0,0 +1,30 @@ +. + +/** + * This file contains the version information for the grade plugin. + * + * @package customcertelement_grade + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2013022901; +$plugin->requires = 2012112900; +$plugin->component = 'customcertelement_grade'; diff --git a/elements/studentname/version.php b/elements/studentname/version.php index 44fd9d7..487d020 100644 --- a/elements/studentname/version.php +++ b/elements/studentname/version.php @@ -1,4 +1,5 @@ . /** - * This file contains the version information for the studentname text plugin. + * This file contains the version information for the studentname plugin. * * @package customcertelement_studentname * @copyright Mark Nelson @@ -24,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2013021901; -$plugin->requires = 2012062500; +$plugin->version = 2013022901; +$plugin->requires = 2012112900; $plugin->component = 'customcertelement_studentname'; diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 822816a..4911a0b 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -44,6 +44,7 @@ $string['deleteconfirm'] = 'Delete confirmation'; $string['deleteelementconfirm'] = 'Are you sure you want to delete this element?'; $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['editcustomcert'] = 'Edit custom certificate'; +$string['font'] = 'Font'; $string['fontsize'] = 'Size'; $string['height'] = 'Height'; $string['heightnotvalid'] = 'The height has to be a valid number.';