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
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
namespace customcertelement_grade;
|
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2013-02-22 08:15:32 +00:00
|
|
|
|
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-06-12 10:35:27 +00:00
|
|
|
/**
|
|
|
|
* The customcert element grade's core interaction API.
|
|
|
|
*
|
2013-06-21 09:35:14 +00:00
|
|
|
* @package customcertelement_grade
|
2013-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2013-06-12 10:35:27 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2015-12-11 06:53:45 +00:00
|
|
|
class element extends \mod_customcert\element {
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
/**
|
|
|
|
* This function renders the form elements when adding a customcert element.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \mod_customcert_edit_element_form $mform the edit_form instance
|
2013-06-06 10:59:08 +00:00
|
|
|
*/
|
|
|
|
public function render_form_elements($mform) {
|
2013-04-16 09:17:55 +00:00
|
|
|
// Get the grade items we can display.
|
|
|
|
$gradeitems = array();
|
2013-06-21 09:35:14 +00:00
|
|
|
$gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade');
|
2013-06-12 08:23:08 +00:00
|
|
|
$gradeitems = $gradeitems + self::get_grade_items();
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// The grade items.
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
|
2013-06-06 10:59:08 +00:00
|
|
|
$mform->setType('gradeitem', PARAM_INT);
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade');
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
// The grade format.
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addElement('select', 'gradeformat', get_string('gradeformat', 'customcertelement_grade'), self::get_grade_format_options());
|
2013-06-06 10:59:08 +00:00
|
|
|
$mform->setType('gradeformat', PARAM_INT);
|
2013-06-21 09:35:14 +00:00
|
|
|
$mform->addHelpButton('gradeformat', 'gradeformat', 'customcertelement_grade');
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
parent::render_form_elements($mform);
|
2013-07-23 04:43:56 +00:00
|
|
|
}
|
2013-02-22 08:15:32 +00:00
|
|
|
|
2013-07-23 04:43:56 +00:00
|
|
|
/**
|
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
|
|
|
*
|
2015-12-11 06:53:45 +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) {
|
|
|
|
// Array of data we will be storing in the database.
|
|
|
|
$arrtostore = array(
|
2013-06-06 10:59:08 +00:00
|
|
|
'gradeitem' => $data->gradeitem,
|
|
|
|
'gradeformat' => $data->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
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \pdf $pdf the pdf object
|
2013-06-28 07:17:43 +00:00
|
|
|
* @param bool $preview true if it is a preview, false otherwise
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
2013-06-28 07:17:43 +00:00
|
|
|
public function render($pdf, $preview) {
|
2013-07-26 07:04:33 +00:00
|
|
|
global $COURSE, $USER;
|
2013-06-05 08:42:18 +00:00
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// 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-07-23 10:04:46 +00:00
|
|
|
// If we are previewing this certificate then just show a demonstration grade.
|
|
|
|
if ($preview) {
|
2015-12-11 06:53:45 +00:00
|
|
|
$courseitem = \grade_item::fetch_course_item($COURSE->id);
|
2013-07-26 07:04:33 +00:00
|
|
|
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, 2);
|
2013-07-23 10:04:46 +00:00
|
|
|
} else {
|
|
|
|
// Get the grade for the grade item.
|
|
|
|
$grade = self::get_grade($gradeinfo, $USER->id);
|
|
|
|
}
|
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
parent::render_content($pdf, $grade);
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* Render the element in html.
|
|
|
|
*
|
|
|
|
* This function is used to render the element when we are using the
|
|
|
|
* drag and drop interface to position it.
|
|
|
|
*/
|
|
|
|
public function render_html() {
|
|
|
|
global $COURSE;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
$courseitem = \grade_item::fetch_course_item($COURSE->id);
|
2015-12-10 03:02:19 +00:00
|
|
|
// Define how many decimals to display.
|
|
|
|
$decimals = 2;
|
|
|
|
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
|
|
|
|
$decimals = 0;
|
|
|
|
}
|
|
|
|
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
|
2015-08-05 05:38:35 +00:00
|
|
|
|
|
|
|
return parent::render_html_content($grade);
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the form when editing an element.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \mod_customcert_edit_element_form $mform the edit_form instance
|
2013-06-25 03:40:56 +00:00
|
|
|
*/
|
|
|
|
public function definition_after_data($mform) {
|
|
|
|
// Set the item and format for this element.
|
2015-12-10 07:26:49 +00:00
|
|
|
if (!empty($this->element->data)) {
|
|
|
|
$gradeinfo = json_decode($this->element->data);
|
|
|
|
$this->element->gradeitem = $gradeinfo->gradeitem;
|
|
|
|
$this->element->gradeformat = $gradeinfo->gradeformat;
|
|
|
|
}
|
2013-06-25 03:40:56 +00:00
|
|
|
|
|
|
|
parent::definition_after_data($mform);
|
|
|
|
}
|
|
|
|
|
2013-07-23 05:21:54 +00:00
|
|
|
/**
|
|
|
|
* This function is responsible for handling the restoration process of the element.
|
|
|
|
*
|
|
|
|
* We will want to update the course module the grade element is pointing to as it will
|
|
|
|
* have changed in the course restore.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \restore_customcert_activity_task $restore
|
2013-07-23 05:21:54 +00:00
|
|
|
*/
|
|
|
|
public function after_restore($restore) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$gradeinfo = json_decode($this->element->data);
|
2015-12-11 06:53:45 +00:00
|
|
|
if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), 'course_module', $gradeinfo->gradeitem)) {
|
2013-07-23 05:21:54 +00:00
|
|
|
$gradeinfo->gradeitem = $newitem->newitemid;
|
|
|
|
$DB->set_field('customcert_elements', 'data', self::save_unique_data($gradeinfo), array('id' => $this->element->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-25 01:30:45 +00:00
|
|
|
case 'topics':
|
|
|
|
$sectionlabel = get_string('topic');
|
2013-04-24 07:50:29 +00:00
|
|
|
break;
|
2013-06-25 01:30:45 +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:
|
2013-06-25 01:30:45 +00:00
|
|
|
$sectionlabel = get_string('section');
|
2013-04-16 09:17:55 +00:00
|
|
|
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])) {
|
2013-07-23 04:43:56 +00:00
|
|
|
// Get the individual section.
|
2013-04-12 09:25:50 +00:00
|
|
|
$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) {
|
2013-07-23 04:43:56 +00:00
|
|
|
// Should never happen unless DB is borked.
|
2013-02-22 08:15:32 +00:00
|
|
|
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.
|
2013-07-24 07:16:24 +00:00
|
|
|
if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
|
2013-07-23 04:43:56 +00:00
|
|
|
$moditem = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance);
|
|
|
|
$gradeitem = reset($moditem->items);
|
2013-06-06 10:59:08 +00:00
|
|
|
if (isset($gradeitem->grademax)) {
|
2013-02-22 08:15:32 +00:00
|
|
|
$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-06-21 09:35:14 +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.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $gradeinfo
|
2013-04-16 09:17:55 +00:00
|
|
|
* @param int $userid
|
|
|
|
* @return string the grade result
|
|
|
|
*/
|
|
|
|
public static function get_grade($gradeinfo, $userid) {
|
2013-06-05 08:53:28 +00:00
|
|
|
global $COURSE;
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
// Get the grade information.
|
|
|
|
$gradeitem = $gradeinfo->gradeitem;
|
|
|
|
$gradeformat = $gradeinfo->gradeformat;
|
|
|
|
|
|
|
|
// Check if we are displaying the course grade.
|
|
|
|
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
|
2015-12-11 06:53:45 +00:00
|
|
|
if ($courseitem = \grade_item::fetch_course_item($COURSE->id)) {
|
2013-04-16 09:17:55 +00:00
|
|
|
// Set the grade type we want.
|
|
|
|
$courseitem->gradetype = GRADE_TYPE_VALUE;
|
2015-12-11 06:53:45 +00:00
|
|
|
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));
|
2013-04-16 09:17:55 +00:00
|
|
|
$coursegrade = grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat, 2);
|
2013-07-24 06:05:32 +00:00
|
|
|
return $coursegrade;
|
2013-04-16 09:17:55 +00:00
|
|
|
}
|
|
|
|
} else { // Get the module grade.
|
2013-06-12 08:23:08 +00:00
|
|
|
if ($modinfo = self::get_mod_grade($gradeitem, $gradeformat, $userid)) {
|
2013-07-24 06:05:32 +00:00
|
|
|
return $modinfo->gradetodisplay;
|
2013-04-16 09:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-12-11 06:53:45 +00:00
|
|
|
* @return \stdClass the grade information
|
2013-04-16 09:17:55 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2015-12-10 01:45:45 +00:00
|
|
|
$gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid);
|
|
|
|
if (!empty($gradeitem)) {
|
2015-12-11 06:53:45 +00:00
|
|
|
$item = new \grade_item();
|
2015-12-10 01:45:45 +00:00
|
|
|
$item->gradetype = GRADE_TYPE_VALUE;
|
|
|
|
$item->courseid = $cm->course;
|
|
|
|
$itemproperties = reset($gradeitem->items);
|
|
|
|
foreach ($itemproperties as $key => $value) {
|
|
|
|
$item->$key = $value;
|
2013-04-16 09:17:55 +00:00
|
|
|
}
|
|
|
|
// Grade for the user.
|
2015-12-10 01:45:45 +00:00
|
|
|
$grade = $item->grades[$userid]->grade;
|
2015-12-10 03:02:19 +00:00
|
|
|
// Define how many decimals to display.
|
|
|
|
$decimals = 2;
|
|
|
|
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
|
|
|
|
$decimals = 0;
|
|
|
|
}
|
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// Create the object we will be returning.
|
2015-12-11 06:53:45 +00:00
|
|
|
$modinfo = new \stdClass;
|
2013-04-16 09:17:55 +00:00
|
|
|
$modinfo->name = $DB->get_field($module->name, 'name', array('id' => $cm->instance));
|
2015-12-10 03:02:19 +00:00
|
|
|
$modinfo->gradetodisplay = grade_format_gradevalue($grade, $item, true, $gradeformat, $decimals);
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
if ($grade) {
|
2015-12-10 01:45:45 +00:00
|
|
|
$modinfo->dategraded = $item->grades[$userid]->dategraded;
|
2013-04-16 09:17:55 +00:00
|
|
|
} else {
|
|
|
|
$modinfo->dategraded = time();
|
|
|
|
}
|
|
|
|
return $modinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-24 15:41:33 +00:00
|
|
|
}
|