1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once "./Services/Certificate/classes/class.ilCertificateAdapter.php";
6
7/**
8 * Skill certificate adapter
9 *
10 * @author	Alex Killing <alex.killing@gmx.de>
11 * @version	$Id$
12 * @ingroup ServicesSkill
13 */
14class ilSkillCertificateAdapter extends ilCertificateAdapter
15{
16    protected $object;
17    private $skill;
18    private $skill_level_id;
19
20    /**
21     * Contructor
22     *
23     * @param object		skill object
24     * @param object		skill level id
25     */
26    public function __construct($a_skill, $a_skill_level_id)
27    {
28        $this->skill = $a_skill;
29        $this->skill_level_id = $a_skill_level_id;
30        parent::__construct();
31        $this->lng->loadLanguageModule('skmg');
32    }
33
34    /**
35     * Returns the certificate path (with a trailing path separator)
36     *
37     * @return string The certificate path
38     */
39    public function getCertificatePath()
40    {
41        return CLIENT_WEB_DIR . "/certificates/skill/" . $this->skill->getId() .
42            "/" . $this->skill_level_id . "/";
43    }
44
45    /**
46     * Returns an array containing all variables and values which can be exchanged in the certificate.
47     * The values will be taken for the certificate preview.
48     *
49     * @return array The certificate variables
50     */
51    public function getCertificateVariablesForPreview()
52    {
53        $old = ilDatePresentation::useRelativeDates();
54        ilDatePresentation::setUseRelativeDates(false);
55
56        $vars = $this->getBaseVariablesForPreview();
57        $vars["SKILL_TITLE"] = ilUtil::prepareFormOutput($this->skill->getTitleForCertificate());
58        $vars["SKILL_LEVEL_TITLE"] = ilUtil::prepareFormOutput($this->skill->getLevelTitleForCertificate($this->skill_level_id));
59        $vars["SKILL_TRIGGER_TITLE"] = ilUtil::prepareFormOutput($this->skill->getTriggerTitleForCertificate($this->skill_level_id));
60
61        ilDatePresentation::setUseRelativeDates($old);
62
63        $insert_tags = array();
64        foreach ($vars as $id => $caption) {
65            $insert_tags["[" . $id . "]"] = $caption;
66        }
67        return $insert_tags;
68    }
69
70    /**
71    * Returns an array containing all variables and values which can be exchanged in the certificate
72    * The values should be calculated from real data. The $params parameter array should contain all
73    * necessary information to calculate the values.
74    *
75    * @param array $params An array of parameters to calculate the certificate parameter values
76    * @return array The certificate variables
77    */
78    public function getCertificateVariablesForPresentation($params = array())
79    {
80        $this->lng->loadLanguageModule('certificate');
81
82        $user_data = $params["user_data"];
83
84        $vars = $this->getBaseVariablesForPresentation($user_data, $params["last_access"], null);
85        $vars["SKILL_TITLE"] = ilUtil::prepareFormOutput($this->skill->getTitleForCertificate());
86        $vars["SKILL_LEVEL_TITLE"] = ilUtil::prepareFormOutput($this->skill->getLevelTitleForCertificate($this->skill_level_id));
87        $vars["SKILL_TRIGGER_TITLE"] = ilUtil::prepareFormOutput($this->skill->getTriggerTitleForCertificate($this->skill_level_id));
88
89        // custom completion date
90        $achievement_date = ilBasicSkill::lookupLevelAchievementDate($user_data["usr_id"], $this->skill_level_id);
91        if ($achievement_date !== false) {
92            $old = ilDatePresentation::useRelativeDates();
93            ilDatePresentation::setUseRelativeDates(false);
94
95            $vars["DATE_COMPLETED"] = ilDatePresentation::formatDate(new ilDate($achievement_date, IL_CAL_DATETIME));
96            $vars["DATETIME_COMPLETED"] = ilDatePresentation::formatDate(new ilDateTime($achievement_date, IL_CAL_DATETIME));
97
98            ilDatePresentation::setUseRelativeDates($old);
99        } else {
100            $vars["DATE_COMPLETED"] = "";
101            $vars["DATETIME_COMPLETED"] = "";
102        }
103
104        foreach ($vars as $id => $caption) {
105            $insert_tags["[" . $id . "]"] = $caption;
106        }
107        return $insert_tags;
108    }
109
110    /**
111    * Returns a description of the available certificate parameters. The description will be shown at
112    * the bottom of the certificate editor text area.
113    *
114    * @return string The certificate parameters description
115    */
116    public function getCertificateVariablesDescription()
117    {
118        $this->lng->loadLanguageModule("skmg");
119
120        $vars = $this->getBaseVariablesDescription();
121        $vars["SKILL_TITLE"] = $this->lng->txt("skmg_cert_skill_title");
122        $vars["SKILL_LEVEL_TITLE"] = $this->lng->txt("skmg_cert_skill_level_title");
123        $vars["SKILL_TRIGGER_TITLE"] = $this->lng->txt("skmg_cert_skill_trigger_title");
124
125        $template = new ilTemplate("tpl.certificate_edit.html", true, true, "Services/Skill");
126        $template->setCurrentBlock("items");
127        foreach ($vars as $id => $caption) {
128            $template->setVariable("ID", $id);
129            $template->setVariable("TXT", $caption);
130            $template->parseCurrentBlock();
131        }
132
133        $template->setVariable("PH_INTRODUCTION", $this->lng->txt("certificate_ph_introduction"));
134
135        return $template->get();
136    }
137
138    /**
139    * Allows to add additional form fields to the certificate editor form
140    * This method will be called when the certificate editor form will built
141    * using the ilPropertyFormGUI class. Additional fields will be added at the
142    * bottom of the form.
143    *
144    * @param object $form An ilPropertyFormGUI instance
145    * @param array $form_fields An array containing the form values. The array keys are the names of the form fields
146    */
147    public function addAdditionalFormElements(&$form, $form_fields)
148    {
149    }
150
151    /**
152    * Allows to add additional form values to the array of form values evaluating a
153    * HTTP POST action.
154    * This method will be called when the certificate editor form will be saved using
155    * the form save button.
156    *
157    * @param array $form_fields A reference to the array of form values
158    */
159    public function addFormFieldsFromPOST(&$form_fields)
160    {
161    }
162
163    /**
164    * Allows to add additional form values to the array of form values evaluating the
165    * associated adapter class if one exists
166    * This method will be called when the certificate editor form will be shown and the
167    * content of the form has to be retrieved from wherever the form values are saved.
168    *
169    * @param array $form_fields A reference to the array of form values
170    */
171    public function addFormFieldsFromObject(&$form_fields)
172    {
173    }
174
175    /**
176    * Allows to save additional adapter form fields
177    * This method will be called when the certificate editor form is complete and the
178    * form values will be saved.
179    *
180    * @param array $form_fields A reference to the array of form values
181    */
182    public function saveFormFields(&$form_fields)
183    {
184    }
185
186    /**
187    * Returns the adapter type
188    * This value will be used to generate file names for the certificates
189    *
190    * @return string A string value to represent the adapter type
191    */
192    public function getAdapterType()
193    {
194        return "skill";
195    }
196
197    /**
198    * Returns a certificate ID
199    * This value will be used to generate unique file names for the certificates
200    *
201    * @return mixed A unique ID which represents a certificate
202    */
203    public function getCertificateID()
204    {
205        return $this->skill_level_id;
206    }
207
208    /**
209    * Set the name of the certificate file
210    * This method will be called when the certificate will be generated
211    *
212    * @return string The certificate file name
213    */
214    public function getCertificateFilename($params = array())
215    {
216        $basename = parent::getCertificateFilename($params);
217
218        $user_data = $params["user_data"];
219        if (!is_array($user_data)) {
220            $short_title = $this->skill->getShortTitleForCertificate();
221            return strftime("%y%m%d", time()) . "_" . $this->lng->txt("certificate_var_user_lastname") . "_" . $short_title . "_" . $basename;
222        } else {
223            return strftime("%y%m%d", time()) . "_" . $user_data["lastname"] . "_" . $params["short_title"] . "_" . $basename;
224        }
225    }
226
227    /**
228    * Is called when the certificate is deleted
229    * Add some adapter specific code if more work has to be done when the
230    * certificate file was deleted
231    */
232    public function deleteCertificate()
233    {
234    }
235}
236