Introduced the grade element

This commit is contained in:
Mark Nelson 2013-02-22 16:15:32 +08:00
parent 9d0eb727e5
commit b36c0b58dd
6 changed files with 324 additions and 32 deletions

View file

@ -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') . ' ',
$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);
}
}

View file

@ -0,0 +1,40 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'customcertelement_grade', language 'en'.
*
* @package customcertelement_grade
* @copyright Mark Nelson <markn@moodle.com>
* @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.<br /><br />
<strong>Grade:</strong> The grade item you wish to display the grade of.<br />
<strong>Grade Format:</strong> The format you wish to use when displaying the grade.<br />
<strong>Font Size:</strong> This is the size of the font in points.<br />
<strong>Colour:</strong> This is the colour of the font.<br />
<strong>Position x:</strong> This is the position in pixels from the top left corner you wish the element to display in the x direction.<br />
<strong>Position Y:</strong> This is the position in pixels from the top left corner you wish the element to display in the y direction.<br />';
$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';

198
elements/grade/lib.php Normal file
View file

@ -0,0 +1,198 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The grade elements core interaction API.
*
* @package customcertelement_grade
* @copyright Mark Nelson <markn@moodle.com>
* @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;
}
}

View file

@ -0,0 +1,30 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the version information for the grade plugin.
*
* @package customcertelement_grade
* @copyright Mark Nelson <markn@moodle.com>
* @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';

View file

@ -1,4 +1,5 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@ -15,7 +16,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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 <markn@moodle.com>
@ -24,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2013021901;
$plugin->requires = 2012062500;
$plugin->version = 2013022901;
$plugin->requires = 2012112900;
$plugin->component = 'customcertelement_studentname';

View file

@ -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.';