2013-02-22 08:15:32 +00:00
|
|
|
<?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
|
|
|
|
*/
|
|
|
|
|
2013-04-10 09:17:19 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
2013-02-22 08:15:32 +00:00
|
|
|
|
|
|
|
require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
|
2013-04-16 09:17:55 +00:00
|
|
|
require_once($CFG->libdir . '/grade/constants.php');
|
2013-02-22 08:15:32 +00:00
|
|
|
require_once($CFG->dirroot . '/grade/lib.php');
|
|
|
|
require_once($CFG->dirroot . '/grade/querylib.php');
|
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
/**
|
|
|
|
* Grade - Course
|
|
|
|
*/
|
|
|
|
define('CUSTOMCERT_GRADE_COURSE', '0');
|
|
|
|
|
2013-02-22 08:15:32 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public function render_form_elements($mform) {
|
|
|
|
// The identifier.
|
|
|
|
$id = $this->element->id;
|
|
|
|
|
2013-02-22 09:25:14 +00:00
|
|
|
$gradeitem = '';
|
|
|
|
$gradeformat = '';
|
|
|
|
|
|
|
|
// Check if there is any data for this element.
|
|
|
|
if (!empty($this->element->data)) {
|
|
|
|
$gradeinfo = json_decode($this->element->data);
|
|
|
|
$gradeitem = $gradeinfo->gradeitem;
|
|
|
|
$gradeformat = $gradeinfo->gradeformat;
|
|
|
|
}
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// Get the grade items we can display.
|
|
|
|
$gradeitems = array();
|
|
|
|
$gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade');
|
|
|
|
$gradeitems = $gradeitems + customcert_element_grade::get_grade_items();
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// The grade items.
|
|
|
|
$mform->addElement('select', 'gradeitem_' . $id, get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
|
|
|
|
$mform->setType('gradeitem_', PARAM_INT);
|
2013-02-22 08:15:32 +00:00
|
|
|
$mform->setDefault('gradeitem_' . $id, $gradeitem);
|
2013-02-22 09:25:14 +00:00
|
|
|
$mform->addHelpButton('gradeitem_' . $id, 'gradeitem', 'customcertelement_grade');
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
// The grade format.
|
|
|
|
$mform->addElement('select', 'gradeformat_' . $id, get_string('gradeformat', 'customcertelement_grade'),
|
|
|
|
customcert_element_grade::get_grade_format_options());
|
|
|
|
$mform->setType('gradeformat_', PARAM_INT);
|
|
|
|
$mform->setDefault('gradeformat_' . $id, $gradeformat);
|
2013-02-22 09:25:14 +00:00
|
|
|
$mform->addHelpButton('gradeformat_' . $id, 'gradeformat', 'customcertelement_grade');
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
parent::render_form_elements($mform);
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will handle how form data will be saved into the data column in the
|
2013-04-16 09:17:55 +00:00
|
|
|
* customcert_elements table.
|
2013-02-22 08:15:32 +00:00
|
|
|
*
|
|
|
|
* @param stdClass $data the form data.
|
2013-03-14 01:04:19 +00:00
|
|
|
* @return string the json encoded array
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
|
|
|
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(
|
2013-04-12 09:25:50 +00:00
|
|
|
'gradeitem' => $gradeitem,
|
|
|
|
'gradeformat' => $gradeformat
|
2013-02-22 08:15:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Encode these variables before saving into the DB.
|
|
|
|
return json_encode($arrtostore);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-10 09:53:56 +00:00
|
|
|
* Handles rendering the element on the pdf.
|
2013-02-22 08:15:32 +00:00
|
|
|
*
|
2013-04-16 09:17:55 +00:00
|
|
|
* @param stdClass $pdf the pdf object
|
|
|
|
* @param int $userid
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
2013-04-16 09:17:55 +00:00
|
|
|
public function render($pdf, $userid) {
|
|
|
|
// If there is no element data, we have nothing to display.
|
|
|
|
if (empty($this->element->data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the information stored in the database.
|
|
|
|
$gradeinfo = json_decode($this->element->data);
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// Get the grade for the grade item.
|
|
|
|
$grade = customcert_element_grade::get_grade($gradeinfo, $userid);
|
|
|
|
parent::render_content($pdf, $grade);
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to return all the grades items for this course.
|
2013-03-14 01:04:19 +00:00
|
|
|
*
|
|
|
|
* @return array the array of gradeable items in the course
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
2013-04-16 09:17:55 +00:00
|
|
|
public static function get_grade_items() {
|
2013-04-12 09:25:50 +00:00
|
|
|
global $COURSE, $DB;
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-12 09:25:50 +00:00
|
|
|
// Array to store the grade items.
|
|
|
|
$modules = array();
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// Collect course modules data.
|
2013-04-12 09:25:50 +00:00
|
|
|
$modinfo = get_fast_modinfo($COURSE);
|
|
|
|
$mods = $modinfo->get_cms();
|
|
|
|
$sections = $modinfo->get_section_info_all();
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-12 09:25:50 +00:00
|
|
|
// Create the section label depending on course format.
|
2013-02-22 08:15:32 +00:00
|
|
|
switch ($COURSE->format) {
|
2013-04-16 09:17:55 +00:00
|
|
|
case "topics":
|
|
|
|
$sectionlabel = get_string("topic");
|
2013-04-24 07:50:29 +00:00
|
|
|
break;
|
2013-04-16 09:17:55 +00:00
|
|
|
case "weeks":
|
|
|
|
$sectionlabel = get_string("week");
|
2013-04-24 07:50:29 +00:00
|
|
|
break;
|
2013-04-16 09:17:55 +00:00
|
|
|
default:
|
|
|
|
$sectionlabel = get_string("section");
|
|
|
|
break;
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 09:25:50 +00:00
|
|
|
// 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.
|
2013-02-22 08:15:32 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 09:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-12 09:25:50 +00:00
|
|
|
return $modules;
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to return all the possible grade formats.
|
2013-03-14 01:04:19 +00:00
|
|
|
*
|
|
|
|
* @return array returns an array of grade formats
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
2013-04-16 09:17:55 +00:00
|
|
|
public static function get_grade_format_options() {
|
2013-04-12 09:25:50 +00:00
|
|
|
$gradeformat = array();
|
2013-04-16 09:17:55 +00:00
|
|
|
$gradeformat[GRADE_DISPLAY_TYPE_REAL] = get_string('gradepoints', 'customcertelement_grade');
|
|
|
|
$gradeformat[GRADE_DISPLAY_TYPE_PERCENTAGE] = get_string('gradepercent', 'customcertelement_grade');
|
|
|
|
$gradeformat[GRADE_DISPLAY_TYPE_LETTER] = get_string('gradeletter', 'customcertelement_grade');
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-12 09:25:50 +00:00
|
|
|
return $gradeformat;
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to return the grade to display.
|
|
|
|
*
|
|
|
|
* @param stdClass $gradeinfo
|
|
|
|
* @param int $userid
|
|
|
|
* @return string the grade result
|
|
|
|
*/
|
|
|
|
public static function get_grade($gradeinfo, $userid) {
|
|
|
|
global $COURSE, $USER, $DB;
|
|
|
|
|
|
|
|
// 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($COURSE->id)) {
|
|
|
|
// 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 get_string('coursegrade', 'certificate') . ': ' . $coursegrade;
|
|
|
|
}
|
|
|
|
} else { // Get the module grade.
|
|
|
|
if ($modinfo = customcert_element_grade::get_mod_grade($gradeitem, $gradeformat, $userid)) {
|
|
|
|
return get_string('grade', 'certificate') . ': ' . $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 the grade information
|
|
|
|
*/
|
|
|
|
public static function get_mod_grade($moduleid, $gradeformat, $userid) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$cm = $DB->get_record('course_modules', array('id' => $moduleid), '*', MUST_EXIST);
|
|
|
|
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
|
|
|
|
|
|
|
|
if ($gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid)) {
|
|
|
|
$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;
|
|
|
|
// 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, 2);
|
|
|
|
|
|
|
|
if ($grade) {
|
|
|
|
$modinfo->dategraded = $item->grades[$userid]->dategraded;
|
|
|
|
} else {
|
|
|
|
$modinfo->dategraded = time();
|
|
|
|
}
|
|
|
|
return $modinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-24 15:41:33 +00:00
|
|
|
}
|