#98 Moved helper functions used by multiple elements

Also did the following -

1. Changed get_grade_items() so it takes a course parameter.
2. Renamed get_grade() and get_mod_grade() and changed functionality
   slightly to use newly introduced class.
3. Do not return a date graded value if there is no grade.
4. Added course grade date as an option to the date element.
5. Fix inconsistency between number of decimals showing for a grade
   between the drag and drop interface and the PDF.
This commit is contained in:
Mark Nelson 2017-09-10 14:55:12 +08:00
parent f3d48abee5
commit cc22ebbac2
6 changed files with 313 additions and 159 deletions

View file

@ -26,6 +26,10 @@ namespace mod_customcert;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/grade/constants.php');
require_once($CFG->dirroot . '/grade/lib.php');
require_once($CFG->dirroot . '/grade/querylib.php');
/** /**
* Class helper. * Class helper.
* *
@ -417,4 +421,145 @@ class element_helper {
\core_collator::asort($options); \core_collator::asort($options);
return $options; return $options;
} }
/**
* Helper function to return all the grades items for a given course.
*
* @param \stdClass $course The course we want to return the grade items for
* @return array the array of gradeable items in the course
*/
public static function get_grade_items($course) {
global $DB;
// Array to store the grade items.
$modules = array();
// 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.
$sectionlabel = get_string('section');
if ($course->format == 'topics') {
$sectionlabel = get_string('topic');
} else if ($course->format == 'weeks') {
$sectionlabel = get_string('week');
}
// 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];
$instance = $DB->get_record($mod->modname, array('id' => $mod->instance));
// Get the grade items for this activity.
if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
$moditem = grade_get_grades($course->id, 'mod', $mod->modname, $mod->instance);
$gradeitem = reset($moditem->items);
if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
}
}
}
}
}
return $modules;
}
/**
* Helper function to return the grade information for a course for a specified user.
*
* @param int $courseid
* @param int $gradeformat
* @param int $userid
* @return grade_information|bool the grade information, or false if there is none.
*/
public static function get_course_grade_info($courseid, $gradeformat, $userid) {
$courseitem = \grade_item::fetch_course_item($courseid);
if (!$courseitem) {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));
return new grade_information(
$courseitem->get_name(),
$grade->finalgrade,
grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat, $decimals),
$grade->get_dategraded()
);
}
/**
* Helper function to return the grade information for a module for a specified user.
*
* @param int $cmid
* @param int $gradeformat
* @param int $userid
* @return grade_information|bool the grade information, or false if there is none.
*/
public static function get_mod_grade_info($cmid, $gradeformat, $userid) {
global $DB;
if (!$cm = $DB->get_record('course_modules', array('id' => $cmid))) {
return false;
}
if (!$module = $DB->get_record('modules', array('id' => $cm->module))) {
return false;
}
$gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid);
if (empty($gradeitem)) {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$item = new \grade_item();
$item->gradetype = GRADE_TYPE_VALUE;
$item->courseid = $cm->course;
$itemproperties = reset($gradeitem->items);
foreach ($itemproperties as $key => $value) {
$item->$key = $value;
}
$objgrade = $item->grades[$userid];
$dategraded = null;
if (!empty($objgrade->dategraded)) {
$dategraded = $objgrade->dategraded;
}
return new grade_information(
$item->name,
$objgrade->grade,
grade_format_gradevalue($objgrade->grade, $item, true, $gradeformat, $decimals),
$dategraded
);
}
} }

View file

@ -0,0 +1,108 @@
<?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/>.
/**
* Contains the class that provides a grade object to be used by elements for display purposes.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die();
/**
* The class that provides a grade object to be used by elements for display purposes.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_information {
/**
* @var string The grade name.
*/
protected $name;
/**
* @var float The raw grade.
*/
protected $grade;
/**
* @var string The grade to display
*/
protected $displaygrade;
/**
* @var int The date it was graded.
*/
protected $dategraded;
/**
* The constructor.
*
* @param string $name
* @param float $grade
* @param string $displaygrade
* @param int $dategraded
*/
public function __construct($name, $grade, $displaygrade, $dategraded) {
$this->name = $name;
$this->grade = $grade;
$this->displaygrade = $displaygrade;
$this->dategraded = $dategraded;
}
/**
* Returns the name.
*
* @return string
*/
public function get_name() {
return $this->name;
}
/**
* Returns the raw grade.
*
* @return float
*/
public function get_grade() {
return $this->grade;
}
/**
* Returns the display grade.
*
* @return string
*/
public function get_displaygrade() {
return $this->displaygrade;
}
/**
* Returns the date it was graded.
*
* @return int
*/
public function get_dategraded() {
return $this->dategraded;
}
}

View file

@ -26,6 +26,11 @@ namespace customcertelement_date;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
/**
* Date - Course grade date
*/
define('CUSTOMCERT_DATE_COURSE_GRADE', '0');
/** /**
* Date - Issue * Date - Issue
*/ */
@ -63,13 +68,16 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance * @param \mod_customcert\edit_element_form $mform the edit_form instance
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
global $COURSE;
// Get the possible date options. // Get the possible date options.
$dateoptions = array(); $dateoptions = array();
$dateoptions[CUSTOMCERT_DATE_ISSUE] = get_string('issueddate', 'customcertelement_date'); $dateoptions[CUSTOMCERT_DATE_ISSUE] = get_string('issueddate', 'customcertelement_date');
$dateoptions[CUSTOMCERT_DATE_COMPLETION] = get_string('completiondate', 'customcertelement_date'); $dateoptions[CUSTOMCERT_DATE_COMPLETION] = get_string('completiondate', 'customcertelement_date');
$dateoptions[CUSTOMCERT_DATE_COURSE_START] = get_string('coursestartdate', 'customcertelement_date'); $dateoptions[CUSTOMCERT_DATE_COURSE_START] = get_string('coursestartdate', 'customcertelement_date');
$dateoptions[CUSTOMCERT_DATE_COURSE_END] = get_string('courseenddate', 'customcertelement_date'); $dateoptions[CUSTOMCERT_DATE_COURSE_END] = get_string('courseenddate', 'customcertelement_date');
$dateoptions = $dateoptions + \customcertelement_grade\element::get_grade_items(); $dateoptions[CUSTOMCERT_DATE_COURSE_GRADE] = get_string('coursegradedate', 'customcertelement_date');
$dateoptions = $dateoptions + \mod_customcert\element_helper::get_grade_items($COURSE);
$mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions); $mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions);
$mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date'); $mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date');
@ -150,11 +158,22 @@ class element extends \mod_customcert\element {
} else if ($dateitem == CUSTOMCERT_DATE_COURSE_END) { } else if ($dateitem == CUSTOMCERT_DATE_COURSE_END) {
$date = $DB->get_field('course', 'enddate', array('id' => $courseid)); $date = $DB->get_field('course', 'enddate', array('id' => $courseid));
} else { } else {
if ($modinfo = \customcertelement_grade\element::get_mod_grade($dateitem, GRADE_DISPLAY_TYPE_PERCENTAGE, if ($dateitem == CUSTOMCERT_DATE_COURSE_GRADE) {
$issue->userid)) { $grade = \mod_customcert\element_helper::get_course_grade_info(
if (!empty($modinfo->dategraded)) { $courseid,
$date = $modinfo->dategraded; GRADE_DISPLAY_TYPE_DEFAULT,
} $user->id
);
} else {
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$dateitem,
GRADE_DISPLAY_TYPE_DEFAULT,
$user->id
);
}
if ($grade && !empty($grade->get_dategraded())) {
$date = $grade->get_dategraded();
} }
} }
} }

View file

@ -24,6 +24,7 @@
$string['completiondate'] = 'Completion date'; $string['completiondate'] = 'Completion date';
$string['courseenddate'] = 'Course end date'; $string['courseenddate'] = 'Course end date';
$string['coursegradedate'] = 'Course grade date';
$string['coursestartdate'] = 'Course start date'; $string['coursestartdate'] = 'Course start date';
$string['dateformat'] = 'Date format'; $string['dateformat'] = 'Date format';
$string['dateformat_help'] = 'This is the format of the date that will be displayed'; $string['dateformat_help'] = 'This is the format of the date that will be displayed';

View file

@ -26,10 +26,6 @@ namespace customcertelement_grade;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/grade/constants.php');
require_once($CFG->dirroot . '/grade/lib.php');
require_once($CFG->dirroot . '/grade/querylib.php');
/** /**
* Grade - Course * Grade - Course
*/ */
@ -50,10 +46,12 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance * @param \mod_customcert\edit_element_form $mform the edit_form instance
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
global $COURSE;
// Get the grade items we can display. // Get the grade items we can display.
$gradeitems = array(); $gradeitems = array();
$gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade'); $gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade');
$gradeitems = $gradeitems + self::get_grade_items(); $gradeitems = $gradeitems + \mod_customcert\element_helper::get_grade_items($COURSE);
// The grade items. // The grade items.
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems); $mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
@ -104,14 +102,37 @@ class element extends \mod_customcert\element {
// Decode the information stored in the database. // Decode the information stored in the database.
$gradeinfo = json_decode($this->get_data()); $gradeinfo = json_decode($this->get_data());
$gradeitem = $gradeinfo->gradeitem;
$gradeformat = $gradeinfo->gradeformat;
// If we are previewing this certificate then just show a demonstration grade. // If we are previewing this certificate then just show a demonstration grade.
if ($preview) { if ($preview) {
// Define how many decimals to display.
$decimals = 2;
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$courseitem = \grade_item::fetch_course_item($courseid); $courseitem = \grade_item::fetch_course_item($courseid);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, 2); $grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
} else { } else {
// Get the grade for the grade item. if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
$grade = self::get_grade($gradeinfo, $user->id, $courseid); $grade = \mod_customcert\element_helper::get_course_grade_info(
$courseid,
$gradeformat,
$user->id
);
} else {
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$gradeitem,
$gradeformat,
$user->id
);
}
if ($grade) {
$grade = $grade->get_displaygrade();
}
} }
\mod_customcert\element_helper::render_content($pdf, $this, $grade); \mod_customcert\element_helper::render_content($pdf, $this, $grade);
@ -137,11 +158,13 @@ class element extends \mod_customcert\element {
$gradeinfo = json_decode($this->get_data()); $gradeinfo = json_decode($this->get_data());
$courseitem = \grade_item::fetch_course_item($COURSE->id); $courseitem = \grade_item::fetch_course_item($COURSE->id);
// Define how many decimals to display. // Define how many decimals to display.
$decimals = 2; $decimals = 2;
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) { if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0; $decimals = 0;
} }
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals); $grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
return \mod_customcert\element_helper::render_html_content($this, $grade); return \mod_customcert\element_helper::render_html_content($this, $grade);
@ -185,66 +208,6 @@ class element extends \mod_customcert\element {
} }
} }
/**
* Helper function to return all the grades items for this course.
*
* @return array the array of gradeable items in the course
*/
public static function get_grade_items() {
global $COURSE, $DB;
// Array to store the grade items.
$modules = array();
// 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 = get_string('topic');
break;
case 'weeks':
$sectionlabel = get_string('week');
break;
default:
$sectionlabel = get_string('section');
break;
}
// 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];
$instance = $DB->get_record($mod->modname, array('id' => $mod->instance));
// Get the grade items for this activity.
if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
$moditem = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance);
$gradeitem = reset($moditem->items);
if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
}
}
}
}
}
return $modules;
}
/** /**
* Helper function to return all the possible grade formats. * Helper function to return all the possible grade formats.
* *
@ -258,88 +221,4 @@ class element extends \mod_customcert\element {
return $gradeformat; return $gradeformat;
} }
/**
* Helper function to return the grade to display.
*
* @param \stdClass $gradeinfo
* @param int $userid
* @param int $courseid
* @return string the grade result
*/
public static function get_grade($gradeinfo, $userid, $courseid) {
// Get the grade information.
$gradeitem = $gradeinfo->gradeitem;
$gradeformat = $gradeinfo->gradeformat;
// Check if we are displaying the course grade.
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
if ($courseitem = \grade_item::fetch_course_item($courseid)) {
// Set the grade type we want.
$courseitem->gradetype = GRADE_TYPE_VALUE;
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));
$coursegrade = grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat, 2);
return $coursegrade;
}
} else { // Get the module grade.
if ($modinfo = self::get_mod_grade($gradeitem, $gradeformat, $userid)) {
return $modinfo->gradetodisplay;
}
}
// Only gets here if no grade was retrieved from the DB.
return '';
}
/**
* Helper function to return the grade the user achieved for a specified module.
*
* @param int $moduleid
* @param int $gradeformat
* @param int $userid
* @return \stdClass|bool the grade information, or false if there is none.
*/
public static function get_mod_grade($moduleid, $gradeformat, $userid) {
global $DB;
if (!$cm = $DB->get_record('course_modules', array('id' => $moduleid))) {
return false;
}
if (!$module = $DB->get_record('modules', array('id' => $cm->module))) {
return false;
}
$gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid);
if (!empty($gradeitem)) {
$item = new \grade_item();
$item->gradetype = GRADE_TYPE_VALUE;
$item->courseid = $cm->course;
$itemproperties = reset($gradeitem->items);
foreach ($itemproperties as $key => $value) {
$item->$key = $value;
}
// Grade for the user.
$grade = $item->grades[$userid]->grade;
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
// Create the object we will be returning.
$modinfo = new \stdClass;
$modinfo->name = $DB->get_field($module->name, 'name', array('id' => $cm->instance));
$modinfo->gradetodisplay = grade_format_gradevalue($grade, $item, true, $gradeformat, $decimals);
if ($grade) {
$modinfo->dategraded = $item->grades[$userid]->dategraded;
} else {
$modinfo->dategraded = time();
}
return $modinfo;
}
return false;
}
} }

View file

@ -41,8 +41,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance * @param \mod_customcert\edit_element_form $mform the edit_form instance
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
global $COURSE;
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_gradeitemname'), $mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_gradeitemname'),
\customcertelement_grade\element::get_grade_items()); \mod_customcert\element_helper::get_grade_items($COURSE));
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_gradeitemname'); $mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_gradeitemname');
parent::render_form_elements($mform); parent::render_form_elements($mform);