Display the course short name (#415)

- Added a new select box to choose from course name or short description to display.
This commit is contained in:
Sameer Ahmed 2021-07-06 11:15:24 -05:00 committed by Mark Nelson
parent 227b162721
commit d6d10d21ac
2 changed files with 71 additions and 5 deletions

View file

@ -35,6 +35,33 @@ defined('MOODLE_INTERNAL') || die();
*/ */
class element extends \mod_customcert\element { class element extends \mod_customcert\element {
/**
* This function renders the form elements when adding a customcert element.
*
* @param \MoodleQuickForm $mform the edit_form instance
*/
public function render_form_elements($mform) {
// The course name display options.
$mform->addElement('select', 'coursenamedisplay', get_string('coursenamedisplay', 'customcertelement_coursename'),
self::get_course_name_display_options());
$mform->setType('coursenamedisplay', PARAM_ALPHA);
$mform->addHelpButton('coursenamedisplay', 'coursenamedisplay', 'customcertelement_coursename');
parent::render_form_elements($mform);
}
/**
* This will handle how form data will be saved into the data column in the
* customcert_elements table.
*
* @param \stdClass $data the form data
* @return string the text
*/
public function save_unique_data($data) {
return $data->coursenamedisplay;
}
/** /**
* Handles rendering the element on the pdf. * Handles rendering the element on the pdf.
* *
@ -43,7 +70,7 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for * @param \stdClass $user the user we are rendering this for
*/ */
public function render($pdf, $preview, $user) { public function render($pdf, $preview, $user) {
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_course_name()); \mod_customcert\element_helper::render_content($pdf, $this, $this->get_course_name_detail());
} }
/** /**
@ -55,19 +82,54 @@ class element extends \mod_customcert\element {
* @return string the html * @return string the html
*/ */
public function render_html() { public function render_html() {
return \mod_customcert\element_helper::render_html_content($this, $this->get_course_name()); return \mod_customcert\element_helper::render_html_content($this, $this->get_course_name_detail());
} }
/** /**
* Helper function that returns the category name. * Sets the data on the form when editing an element.
*
* @param \MoodleQuickForm $mform the edit_form instance
*/
public function definition_after_data($mform) {
if (!empty($this->get_data())) {
$element = $mform->getElement('coursenamedisplay');
$element->setValue($this->get_data());
}
parent::definition_after_data($mform);
}
/**
* Helper function that returns the selected course name detail (i.e. name or short description) for display.
* *
* @return string * @return string
*/ */
protected function get_course_name() : string { protected function get_course_name_detail() : string {
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id()); $courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
$course = get_course($courseid); $course = get_course($courseid);
$context = \mod_customcert\element_helper::get_context($this->get_id()); $context = \mod_customcert\element_helper::get_context($this->get_id());
return format_string($course->fullname, true, ['context' => $context]); // The name field to display.
$field = $this->get_data();
// The name value to display.
$value = $course->fullname;
if ($field == 'courseshortdescription') {
$value = $course->shortname;
}
return format_string($value, true, ['context' => $context]);
}
/**
* Helper function to return all the possible name display options.
*
* @return array returns an array of name options
*/
public static function get_course_name_display_options() {
$coursenamedisplayoptions = array(
'coursename' => get_string('coursename', 'customcertelement_coursename'),
'courseshortdescription' => get_string('courseshortdescription', 'customcertelement_coursename')
);
return $coursenamedisplayoptions;
} }
} }

View file

@ -24,3 +24,7 @@
$string['pluginname'] = 'Course name'; $string['pluginname'] = 'Course name';
$string['privacy:metadata'] = 'The Course name plugin does not store any personal data.'; $string['privacy:metadata'] = 'The Course name plugin does not store any personal data.';
$string['coursenamedisplay'] = 'Course name / short description';
$string['coursenamedisplay_help'] = 'Course name or short description you wish to display on certificate.';
$string['coursename'] = 'Course name';
$string['courseshortdescription'] = 'Course short description';