/**
  * Substitutes the certificate text variables
  * 
  * @param stdClass $issuecert The issue certificate object
  * @param string $certtext The certificate text without substitutions
  * @return string Return certificate text with all substutions
  */
 protected function get_certificate_text($issuecert, $certtext = null)
 {
     global $OUTPUT, $DB, $CFG;
     if (!($user = get_complete_user_data('id', $issuecert->userid))) {
         print_error('nousersfound', 'moodle');
     }
     //If no text set get firstpage text
     if (empty($certtext)) {
         $certtext = $this->get_instance()->certificatetext;
     }
     $certtext = format_text($certtext, FORMAT_HTML, array('noclean' => true));
     $a = new stdClass();
     $a->username = fullname($user);
     $a->idnumber = $user->idnumber;
     $a->firstname = $user->firstname;
     $a->lastname = $user->lastname;
     $a->email = $user->email;
     $a->icq = $user->icq;
     $a->skype = $user->skype;
     $a->yahoo = $user->yahoo;
     $a->aim = $user->aim;
     $a->msn = $user->msn;
     $a->phone1 = $user->phone1;
     $a->phone2 = $user->phone2;
     $a->institution = $user->institution;
     $a->department = $user->department;
     $a->address = $user->address;
     $a->city = $user->city;
     //Add userimage url
     $a->userimage = $OUTPUT->user_picture($user, array('size' => 1, 'popup' => false));
     if (!empty($user->country)) {
         $a->country = get_string($user->country, 'countries');
     } else {
         $a->country = '';
     }
     //Formatting URL, if needed
     $url = $user->url;
     if (strpos($url, '://') === false) {
         $url = 'http://' . $url;
     }
     $a->url = $url;
     //Getting user custom profiles fields
     $userprofilefields = $this->get_user_profile_fields($user->id);
     foreach ($userprofilefields as $key => $value) {
         $key = 'profile_' . $key;
         $a->{$key} = $value;
     }
     $a->coursename = format_string($this->get_instance()->coursename, true);
     $a->grade = $this->get_grade($user->id);
     $a->date = $this->get_date($issuecert, $user->id);
     $a->outcome = $this->get_outcome($user->id);
     $a->certificatecode = $issuecert->code;
     // this code stay here only beace legacy supporte, coursehours variable was removed
     //see issue 61 https://github.com/bozoh/moodle-mod_simplecertificate/issues/61
     if (isset($this->get_instance()->coursehours)) {
         $a->hours = format_string($this->get_instance()->coursehours . ' ' . get_string('hours', 'simplecertificate'), true);
     } else {
         $a->hours = '';
     }
     try {
         if ($course = $this->get_course()) {
             require_once $CFG->libdir . '/coursecatlib.php';
             $courseinlist = new course_in_list($course);
             if ($courseinlist->has_course_contacts()) {
                 $t = array();
                 foreach ($courseinlist->get_course_contacts() as $userid => $coursecontact) {
                     $t[] = $coursecontact['rolename'] . ': ' . $coursecontact['username'];
                 }
                 $a->teachers = implode("<br>", $t);
             } else {
                 $a->teachers = '';
             }
         } else {
             $a->teachers = '';
         }
     } catch (Exception $e) {
         $a->teachers = '';
     }
     //Fetch user actitivy restuls
     $a->userresults = $this->get_user_results($issuecert->userid);
     //Get User role name in course
     if (!($a->userrolename = get_user_roles_in_course($user->id, $course->id))) {
         $a->userrolename = '';
     }
     // Get user enrollment start date
     // see funtion  enrol_get_enrolment_end($courseid, $userid), which get enddate, not start
     $sql = "SELECT ue.timestart\n              FROM {user_enrolments} ue\n              JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)\n              JOIN {user} u ON u.id = ue.userid\n              WHERE ue.userid = :userid AND e.status = :enabled AND u.deleted = 0";
     $params = array('enabled' => ENROL_INSTANCE_ENABLED, 'userid' => $user->id, 'courseid' => $course->id);
     if ($timestart = $DB->get_field_sql($sql, $params)) {
         $a->timestart = userdate($timestart, $this->get_instance()->timestartdatefmt);
     } else {
         $a->timestart = '';
     }
     $a = (array) $a;
     $search = array();
     $replace = array();
     foreach ($a as $key => $value) {
         $search[] = '{' . strtoupper($key) . '}';
         //Some variables can't use format_string
         if (strtoupper($key) == 'USERIMAGE' || strtoupper($key) == 'URL') {
             $replace[] = $value;
         } else {
             $replace[] = format_string((string) $value, true);
         }
     }
     if ($search) {
         $certtext = str_replace($search, $replace, $certtext);
     }
     //Clear not setted custom profile fiedls {PROFILE_xxxx}
     return preg_replace('[\\{PROFILE_(.*)\\}]', "", $certtext);
 }
 /**
  * Returns HTML to display course content (summary, course contacts and optionally category name)
  *
  * This method is called from coursecat_coursebox() and may be re-used in AJAX
  *
  * @param coursecat_helper $chelper various display options
  * @param stdClass|course_in_list $course
  * @return string
  */
 protected function coursecat_coursebox_content(coursecat_helper $chelper, $course)
 {
     if (!$this->enablecategoryicon) {
         return parent::coursecat_coursebox_content($chelper, $course);
     }
     global $CFG;
     if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
         return '';
     }
     if ($course instanceof stdClass) {
         require_once $CFG->libdir . '/coursecatlib.php';
         $course = new course_in_list($course);
     }
     $content = '';
     $coursehascontacts = $course->has_course_contacts();
     // Display course summary.
     if ($course->has_summary()) {
         $summaryclass = 'summary';
         if ($coursehascontacts == false) {
             $summaryclass .= ' noteachers';
         }
         $content .= html_writer::start_tag('div', array('class' => $summaryclass));
         $content .= $chelper->get_course_formatted_summary($course, array('overflowdiv' => true, 'noclean' => true, 'para' => false));
         $content .= html_writer::end_tag('div');
         // Class .summary.
     }
     // Display course overview files.
     $contentimages = $contentfiles = '';
     foreach ($course->get_course_overviewfiles() as $file) {
         $isimage = $file->is_valid_image();
         $url = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
         if ($isimage) {
             $contentimages .= html_writer::tag('div', html_writer::empty_tag('img', array('src' => $url)), array('class' => 'courseimage'));
         } else {
             $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
             $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')) . html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename'));
             $contentfiles .= html_writer::tag('span', html_writer::link($url, $filename), array('class' => 'coursefile fp-filename-icon'));
         }
     }
     $content .= $contentimages . $contentfiles;
     // Display course contacts.  See course_in_list::get_course_contacts().
     if ($coursehascontacts) {
         $content .= html_writer::start_tag('ul', array('class' => 'teachers'));
         foreach ($course->get_course_contacts() as $userid => $coursecontact) {
             $faiconsetting = \theme_essential\toolbox::get_setting('courselistteachericon');
             $faiconsettinghtml = empty($faiconsetting) ? '' : '<i class="fa fa-' . $faiconsetting . '"></i> ';
             $name = $faiconsettinghtml . $coursecontact['rolename'] . ': ' . html_writer::link(new moodle_url('/user/view.php', array('id' => $userid, 'course' => SITEID)), $coursecontact['username']);
             $content .= html_writer::tag('li', $name);
         }
         $content .= html_writer::end_tag('ul');
         // Class .teachers.
     }
     // Display course category if necessary (for example in search results).
     if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) {
         require_once $CFG->libdir . '/coursecatlib.php';
         if ($cat = coursecat::get($course->category, IGNORE_MISSING)) {
             $content .= html_writer::start_tag('div', array('class' => 'coursecat'));
             $content .= get_string('category') . ': ' . html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
             $content .= html_writer::end_tag('div');
             // Class .coursecat.
         }
     }
     return $content;
 }
    protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) {
        global $CFG;
        // if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {
        //     return '';
        // }
        if ($course instanceof stdClass) {
            require_once($CFG->libdir. '/coursecatlib.php');
            $course = new course_in_list($course);
        }
        $content = '';

        // display course overview files
        $contentimages = $contentfiles = '';
        foreach ($course->get_course_overviewfiles() as $file) {
            $isimage = $file->is_valid_image();
            $url = file_encode_url("$CFG->wwwroot/pluginfile.php",
                '/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
                $file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
            if ($isimage) {
                $contentimages .= html_writer::empty_tag('img', array('src' => $url, 'alt' => 'Course Image '. $course->fullname,
                    'class' => 'courseimage'));
            } else {
                $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
                $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
                    html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename'));
                $contentfiles .= html_writer::tag('span',
                    html_writer::link($url, $filename),
                    array('class' => 'coursefile fp-filename-icon'));
            }
        }
        /**
         * @updateDate  25/09/2014
         * @author      eFaktor     (fbv)
         *
         * Description
         * Check if there is any image connected with the course.
         * Not Image --> Add an empty place
         */
        if (!$contentimages) {
            $contentimages .= html_writer::start_tag('div', array('class' => 'not_courseimage'));
            $contentimages .= html_writer::end_div();//courseimage
        }
        $content .= $contentimages. $contentfiles;

        // display course summary
        if ($course->has_summary()) {

            $content .= html_writer::start_tag('div', array('class' => 'course-summary'));
            $content .= $course->summary;

            $content .= html_writer::end_div();//courseimage
        }

        // display course contacts. See course_in_list::get_course_contacts()
        if ($course->has_course_contacts()) {
            $content .= html_writer::start_tag('ul', array('class' => 'teachers'));
            foreach ($course->get_course_contacts() as $userid => $coursecontact) {
                $name = $coursecontact['rolename'].': '.
                    html_writer::link(new moodle_url('/user/view.php',
                            array('id' => $userid, 'course' => SITEID)),
                        $coursecontact['username']);
                $content .= html_writer::tag('li', $name);
            }
            $content .= html_writer::end_tag('ul'); // .teachers
        }

        // display course category if necessary (for example in search results)
        if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) {
            require_once($CFG->libdir. '/coursecatlib.php');
            if ($cat = coursecat::get($course->category, IGNORE_MISSING)) {
                $content .= html_writer::start_tag('div', array('class' => 'coursecat'));
                $content .= get_string('category').': '.
                    html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)),
                        $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed'));
                $content .= html_writer::end_tag('div'); // .coursecat
            }
        }

        $content .= html_writer::tag('div', '', array('class' => 'boxfooter')); // .coursecat

        return $content;
    }