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
|
|
|
|
2017-02-16 12:12:19 +00:00
|
|
|
/**
|
|
|
|
* This file contains the customcert element grade's core interaction API.
|
|
|
|
*
|
|
|
|
* @package customcertelement_grade
|
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-02-16 09:03:34 +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) {
|
2017-09-10 06:55:12 +00:00
|
|
|
global $COURSE;
|
|
|
|
|
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');
|
2017-09-10 06:55:12 +00:00
|
|
|
$gradeitems = $gradeitems + \mod_customcert\element_helper::get_grade_items($COURSE);
|
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);
|
|
|
|
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade');
|
2013-04-16 09:17:55 +00:00
|
|
|
|
|
|
|
// The grade format.
|
2017-02-16 12:12:19 +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
|
2016-08-23 08:22:58 +00:00
|
|
|
* @param \stdClass $user the user we are rendering this for
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
2016-08-23 08:22:58 +00:00
|
|
|
public function render($pdf, $preview, $user) {
|
2013-04-16 09:17:55 +00:00
|
|
|
// If there is no element data, we have nothing to display.
|
2017-08-26 05:39:47 +00:00
|
|
|
if (empty($this->get_data())) {
|
2013-04-16 09:17:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:38:32 +00:00
|
|
|
$courseid = \mod_customcert\element_helper::get_courseid($this->id);
|
|
|
|
|
2013-04-16 09:17:55 +00:00
|
|
|
// Decode the information stored in the database.
|
2017-08-26 05:39:47 +00:00
|
|
|
$gradeinfo = json_decode($this->get_data());
|
2017-09-10 06:55:12 +00:00
|
|
|
$gradeitem = $gradeinfo->gradeitem;
|
|
|
|
$gradeformat = $gradeinfo->gradeformat;
|
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) {
|
2017-05-30 09:38:32 +00:00
|
|
|
$courseitem = \grade_item::fetch_course_item($courseid);
|
2018-12-19 03:44:30 +00:00
|
|
|
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat);
|
2013-07-23 10:04:46 +00:00
|
|
|
} else {
|
2017-09-10 06:55:12 +00:00
|
|
|
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
|
|
|
|
$grade = \mod_customcert\element_helper::get_course_grade_info(
|
|
|
|
$courseid,
|
|
|
|
$gradeformat,
|
|
|
|
$user->id
|
|
|
|
);
|
2017-11-11 09:16:00 +00:00
|
|
|
} else if (strpos($gradeitem, 'gradeitem:') === 0) {
|
|
|
|
$gradeitemid = substr($gradeitem, 10);
|
|
|
|
$grade = \mod_customcert\element_helper::get_grade_item_info(
|
|
|
|
$gradeitemid,
|
|
|
|
$gradeformat,
|
|
|
|
$user->id
|
|
|
|
);
|
2017-09-10 06:55:12 +00:00
|
|
|
} else {
|
|
|
|
$grade = \mod_customcert\element_helper::get_mod_grade_info(
|
|
|
|
$gradeitem,
|
|
|
|
$gradeformat,
|
|
|
|
$user->id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($grade) {
|
|
|
|
$grade = $grade->get_displaygrade();
|
|
|
|
}
|
2013-07-23 10:04:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
\mod_customcert\element_helper::render_content($pdf, $this, $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.
|
2016-02-16 09:03:34 +00:00
|
|
|
*
|
|
|
|
* @return string the html
|
2015-08-05 05:38:35 +00:00
|
|
|
*/
|
|
|
|
public function render_html() {
|
|
|
|
global $COURSE;
|
|
|
|
|
|
|
|
// If there is no element data, we have nothing to display.
|
2017-08-26 05:39:47 +00:00
|
|
|
if (empty($this->get_data())) {
|
2015-08-05 05:38:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the information stored in the database.
|
2017-08-26 05:39:47 +00:00
|
|
|
$gradeinfo = json_decode($this->get_data());
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
$courseitem = \grade_item::fetch_course_item($COURSE->id);
|
2017-09-10 06:55:12 +00:00
|
|
|
|
2018-12-19 03:44:30 +00:00
|
|
|
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat);
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
return \mod_customcert\element_helper::render_html_content($this, $grade);
|
2015-08-05 05:38:35 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 03:40:56 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the form when editing an element.
|
|
|
|
*
|
2016-02-16 09:03:34 +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.
|
2017-08-26 05:39:47 +00:00
|
|
|
if (!empty($this->get_data())) {
|
|
|
|
$gradeinfo = json_decode($this->get_data());
|
|
|
|
|
|
|
|
$element = $mform->getElement('gradeitem');
|
|
|
|
$element->setValue($gradeinfo->gradeitem);
|
|
|
|
|
|
|
|
$element = $mform->getElement('gradeformat');
|
|
|
|
$element->setValue($gradeinfo->gradeformat);
|
2015-12-10 07:26:49 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2017-08-26 05:39:47 +00:00
|
|
|
$gradeinfo = json_decode($this->get_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;
|
2017-09-10 06:57:14 +00:00
|
|
|
$DB->set_field('customcert_elements', 'data', $this->save_unique_data($gradeinfo), array('id' => $this->get_id()));
|
2013-07-23 05:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-24 15:41:33 +00:00
|
|
|
}
|