Gitlab CSE Unil

locallib_admin.php 10.3 KB
Newer Older
M. Chardon's avatar
M. Chardon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

// This file is part of 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
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Description of locallib_admin
 *
 * @package   assignfeedback_editpdfplus
 * @copyright  2017 Université de Lausanne
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
defined('MOODLE_INTERNAL') || die();

use \assignfeedback_editpdfplus\page_editor;
use \assignfeedback_editpdfplus\widget_admin;
use \assignfeedback_editpdfplus\form\axis_form;
30
use \assignfeedback_editpdfplus\form\axis_del_form;
M. Chardon's avatar
M. Chardon committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use \assignfeedback_editpdfplus\admin_editor;

class assign_feedback_editpdfplus_admin {

    private $course = null;
    private $context = null;

    function __construct(stdClass $context, stdClass $course) {
        $this->context = $context;
        $this->course = $course;
    }

    /**
     * todo.
     *
     * @return string
     */
    public function view() {
        global $PAGE;

        //$PAGE->requires->js_call_amd('assignfeedback_editpdfplus/admin_panel', 'init');

        $html = '';
        //$toform = null;
        $renderer = $PAGE->get_renderer('assignfeedback_editpdfplus');
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        /* $formAddAxis = new axis_form(new moodle_url('/mod/assign/feedback/editpdfplus/view_admin.php?id=' . $this->course->id, array('id' => $this->course->id))); //Form processing and displaying is done here
          if ($formAddAxis->is_cancelled()) {
          //Handle form cancel operation, if cancel button is present on form
          } else if ($fromform = $formAddAxis->get_data()) {
          //In this case you process validated data. $mform->get_data() returns data posted in form.
          $label = $fromform->label;
          $axe = $this->addAxis($label);
          $formAddAxis->set_data($toform);
          return $axe; //"<option>tutu</option>";
          } else {
          // this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
          // or on the first display of the form.
          //Set default data (if any)
          $formAddAxis->set_data($toform);
          } */
M. Chardon's avatar
M. Chardon committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        $widget = $this->get_widget();
        //$widget->axisaddform = $formAddAxis;
        $html .= $renderer->render_assignfeedback_editpdfplus_widget_admin($widget);
        return $html;
    }

    public function getAxisForm($axeid = null) {
        global $PAGE, $DB;

        $html = '';
        $toform = null;
        $formAxis = null;
        $axis = null;
        if ($axeid != null) {
            $axis = $DB->get_record('assignfeedback_editpp_axis', array('id' => $axeid), '*', MUST_EXIST);
        }
        if ($axis != null) {
            $formAxis = new axis_form(null, array('id' => $this->course->id), null, null, array('id' => "assignfeedback_editpdfplus_edit_axis")); //Form processing and displaying is done here
89
            $formAxis->set_data(array('axeid' => $axeid, 'label' => $axis->label));
M. Chardon's avatar
M. Chardon committed
90
            $formAxis->id = "assignfeedback_editpdfplus_edit_axis";
91
92
            $formAxis->title = "Renommer l'axe";
            $formAxis->action = "edit";
M. Chardon's avatar
M. Chardon committed
93
94
95
96
        } else {
            $formAxis = new axis_form(null, array('id' => $this->course->id), null, null, array('id' => "assignfeedback_editpdfplus_add_axis")); //Form processing and displaying is done here
            $formAxis->set_data($toform);
            $formAxis->id = "assignfeedback_editpdfplus_add_axis";
97
98
            $formAxis->title = "Ajouter un nouvel axe";
            $formAxis->action = "add";
M. Chardon's avatar
M. Chardon committed
99
100
101
102
103
104
105
        }
        $renderer = $PAGE->get_renderer('assignfeedback_editpdfplus');
        $formAxis->courseid = $this->course->id;
        $html .= $renderer->render_assignfeedback_editpdfplus_widget_admin_axisform($formAxis);
        return $html;
    }

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    public function getAxisDelForm($axeid) {
        global $PAGE, $DB;

        $html = '';
        $formAxis = null;
        $axis = null;
        if ($axeid != null) {
            $axis = $DB->get_record('assignfeedback_editpp_axis', array('id' => $axeid), '*', MUST_EXIST);
        }
        if ($axis != null) {
            $formAxis = new axis_del_form(null, array('id' => $this->course->id), null, null, array('id' => "assignfeedback_editpdfplus_del_axis")); //Form processing and displaying is done here
            $formAxis->set_data(array('axeid' => $axeid, 'label' => $axis->label));
        }
        $formAxis->id = "assignfeedback_editpdfplus_del_axis";
        $formAxis->title = "Supprimer l'axe";
        $formAxis->action = "del";
        $renderer = $PAGE->get_renderer('assignfeedback_editpdfplus');
        $formAxis->courseid = $this->course->id;
        $html .= $renderer->render_assignfeedback_editpdfplus_widget_admin_axisdelform($formAxis);
        return $html;
    }

M. Chardon's avatar
M. Chardon committed
128
    public function getToolForm($toolid = null, $axisid = null) {
M. Chardon's avatar
M. Chardon committed
129
130
131
132
133
134
135
136
137
138
        global $PAGE, $DB;

        $html = '';
        $data = new stdClass;
        $data->courseid = $this->course->id;
        $data->sesskey = sesskey();
        $data->actionurl = "http://localhost/moodle33/moodle/lib/ajax/service.php";
        $data->formid = "assignfeedback_editpdfplus_edit_tool";
        if ($toolid != null) {
            $data->tool = $DB->get_record('assignfeedback_editpp_tool', array('id' => $toolid), '*', MUST_EXIST);
M. Chardon's avatar
M. Chardon committed
139
140
141
142
143
            $data->tool->removable = true;
            $nbEnregistrements = $DB->get_record_sql('SELECT count(*) as val FROM {assignfeedback_editpp_annot} WHERE toolid = ?', array('toolid' => $toolid));
            if ($nbEnregistrements->val > 0) {
                $data->tool->removable = false;
            }
M. Chardon's avatar
M. Chardon committed
144
145
146
147
148
        } else {
            $tool = new \assignfeedback_editpdfplus\tool();
            $tool->contextid = $this->context->id;
            $tool->enabled = true;
            $tool->axis = $axisid;
M. Chardon's avatar
M. Chardon committed
149
            $tool->removable = true;
M. Chardon's avatar
M. Chardon committed
150
            $data->tool = $tool;
M. Chardon's avatar
M. Chardon committed
151
        }
152
153
        $tooltexts = $data->tool->texts;
        if ($tooltexts) {
M. Chardon's avatar
M. Chardon committed
154
            $tooltextsarray = explode("\",\"", $tooltexts);
155
156
            $compteur = 0;
            foreach ($tooltextsarray as $value) {
157
158
159
160
161
162
163
                if ($value && $value != '"') {
                    $obj = new object();
                    $obj->text = /* substr( */$value/* ,1,-1) */;
                    if (substr($obj->text, 0, 1) == '"') {
                        $obj->text = substr($obj->text, 1);
                    }
                    if (substr($obj->text, -1) == '"') {
M. Chardon's avatar
M. Chardon committed
164
                        $obj->text = substr($obj->text, 0, -1);
165
166
167
168
169
                    }
                    $obj->index = $compteur;
                    $data->tool->textsarray[] = $obj;
                    $compteur++;
                }
170
171
172
173
            }
        } else {
            $data->tool->textsarray = null;
        }
174
175
        $data->tools = admin_editor::get_typetools();
        foreach ($data->tools as $toolRef) {
176
            $toolRef->libelle = get_string('typetool_' . $toolRef->label, 'assignfeedback_editpdfplus');
177
        }
M. Chardon's avatar
M. Chardon committed
178
179
180
181
182
183
        $renderer = $PAGE->get_renderer('assignfeedback_editpdfplus');
        //$formTool->courseid = $this->course->id;
        $html .= $renderer->render_assignfeedback_editpdfplus_widget_admin_toolform($data);
        return $html;
    }

M. Chardon's avatar
M. Chardon committed
184
185
186
187
188
189
190
    private function get_widget() {
        global $USER;

        // get the costum toolbars
        $coursecontext = context::instance_by_id($this->context->id);
        $coursecontexts = array_filter(explode('/', $coursecontext->path), 'strlen');
        $tools = page_editor::get_tools($coursecontexts);
191
        $typetools = page_editor::get_typetools(null);
M. Chardon's avatar
M. Chardon committed
192
        $axis = page_editor::get_axis(array($this->context->id));
M. Chardon's avatar
M. Chardon committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        $toolbars = $this->prepareToolbar($axis, $tools);
        // get all accessibled toolbars
        $courses = get_courses();
        $contextListToCheck = array();
        foreach ($courses as $course) {
            $contextid = context_course::instance($course->id);
            $coursecontextsTmp = array_filter(explode('/', $contextid->path), 'strlen');
            foreach ($coursecontextsTmp as $value) {
                if ($value != $this->context->id && !in_array($value, $contextListToCheck)) {
                    $contextListToCheck[] = $value;
                }
            }
        }
        $axisDispo = array();
        $toolDispo = page_editor::get_tools($contextListToCheck);
        foreach ($contextListToCheck as $value) {
            $axistmp = page_editor::get_axis(array($value));
            if ($axistmp && sizeof($axistmp) > 0) {
                $axisDispo = array_merge($axisDispo, $axistmp);
            }
        }
        $toolbarsDispo = $this->prepareToolbar($axisDispo, $toolDispo);
        $widget = new widget_admin($this->context, $this->course, $USER, $toolbars, $axis, $typetools, $toolbarsDispo);
        return $widget;
    }

    private function prepareToolbar($axis, $tools) {
        $toolbars = array();
M. Chardon's avatar
M. Chardon committed
221
        foreach ($axis as $ax) {
222
            $ax->children = 0;
M. Chardon's avatar
M. Chardon committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
            $toolbar = new stdClass();
            $toolbar->axis = $ax;
            $toolbar->tools = array();
            //$toolbars[$ax->id]['label'] = $ax->label;
            foreach ($tools as $tool) {
                if ($tool->axis == $ax->id) {
                    if ($tool->enabled == "1") {
                        $tool->button = "btn-default";
                    } else {
                        $tool->button = "";
                    }
                    if ($tool->type == "4") {
                        $tool->label = '| ' . $tool->label . ' |';
                    } elseif ($tool->type == "5") {
                        $tool->label = '| ' . $tool->label;
                    }
                    if ($tool->type == "4" || $tool->type == "1") {
                        $tool->style = "text-decoration: underline;";
                    }
                    $toolbar->tools[] = $tool;
243
                    $ax->children++;
M. Chardon's avatar
M. Chardon committed
244
245
246
247
                }
            }
            $toolbars[] = $toolbar;
        }
M. Chardon's avatar
M. Chardon committed
248
        return $toolbars;
M. Chardon's avatar
M. Chardon committed
249
250
251
    }

}