Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Page helper.
19
 *
20
 * @package    tool_lp
21
 * @copyright  2015 Frédéric Massart - FMCorz.net
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace tool_lp;
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use coding_exception;
29
use context;
30
use moodle_exception;
31
use moodle_url;
32
use core_user;
33
use context_user;
34
use context_course;
35
use stdClass;
36
 
37
/**
38
 * Page helper.
39
 *
40
 * @package    tool_lp
41
 * @copyright  2015 Frédéric Massart - FMCorz.net
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class page_helper {
45
 
46
    /**
47
     * Set-up a course page.
48
     *
49
     * Example:
50
     * list($title, $subtitle) = page_helper::setup_for_course($pagecontextid, $url, $course, $pagetitle);
51
     * echo $OUTPUT->heading($title);
52
     * echo $OUTPUT->heading($subtitle, 3);
53
     *
54
     * @param  moodle_url $url The current page.
55
     * @param  stdClass $course The course.
56
     * @param  string $subtitle The title of the subpage, if any.
57
     * @return array With the following:
58
     *               - Page title
59
     *               - Page sub title
60
     *               - Return URL (course competencies page)
61
     */
62
    public static function setup_for_course(moodle_url $url, $course, $subtitle = '') {
63
        global $PAGE;
64
 
65
        $context = context_course::instance($course->id);
66
 
67
        $PAGE->set_course($course);
68
 
69
        if (!empty($subtitle)) {
70
            $title = $subtitle;
71
        } else {
72
            $title = get_string('coursecompetencies', 'tool_lp');
73
        }
74
 
75
        $returnurl = new moodle_url('/admin/tool/lp/coursecompetencies.php', array('courseid' => $course->id));
76
 
77
        $heading = $context->get_context_name();
78
        $PAGE->set_pagelayout('incourse');
79
        $PAGE->set_url($url);
80
        $PAGE->set_title($title);
81
        $PAGE->set_heading($heading);
82
 
83
        if (!empty($subtitle)) {
84
            $PAGE->navbar->add(get_string('coursecompetencies', 'tool_lp'), $returnurl);
85
            // We're in a sub page without a specific template.
86
            $PAGE->navbar->add($subtitle, $url);
87
        }
88
 
89
        return array($title, $subtitle, $returnurl);
90
    }
91
 
92
    /**
93
     * Set-up a template page.
94
     *
95
     * Example:
96
     * list($title, $subtitle) = page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle);
97
     * echo $OUTPUT->heading($title);
98
     * echo $OUTPUT->heading($subtitle, 3);
99
     *
100
     * @param  int $pagecontextid The page context ID.
101
     * @param  moodle_url $url The current page.
102
     * @param  \core_competency\template $template The template, if any.
103
     * @param  string $subtitle The title of the subpage, if any.
104
     * @param  string $returntype The desired return page.
105
     * @return array With the following:
106
     *               - Page title
107
     *               - Page sub title
108
     *               - Return URL
109
     */
110
    public static function setup_for_template($pagecontextid, moodle_url $url, $template = null, $subtitle = '',
111
                                              $returntype = null) {
112
        global $PAGE, $SITE;
113
 
114
        $pagecontext = context::instance_by_id($pagecontextid);
115
        $context = $pagecontext;
116
        if (!empty($template)) {
117
            $context = $template->get_context();
118
        }
119
 
120
        $templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid));
121
        $templateurl = null;
122
        if ($template) {
123
            $templateurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', [
124
                'templateid' => $template->get('id'),
125
                'pagecontextid' => $pagecontextid
126
            ]);
127
        }
128
 
129
        $returnurl = $templatesurl;
130
        if ($returntype != 'templates' && $templateurl) {
131
            $returnurl = $templateurl;
132
        }
133
 
134
        $PAGE->navigation->override_active_url($templatesurl);
135
        $PAGE->set_context($pagecontext);
136
        $PAGE->set_url($url);
137
 
138
        if (!empty($template)) {
139
            $title = format_string($template->get('shortname'), true, array('context' => $context));
140
        } else {
141
            $title = get_string('templates', 'tool_lp');
142
        }
143
 
144
        if ($pagecontext->contextlevel == CONTEXT_SYSTEM) {
145
            $PAGE->set_heading($SITE->fullname);
146
        } else if ($pagecontext->contextlevel == CONTEXT_COURSECAT) {
147
            \core_course_category::page_setup();
148
            // Set the learning plan templates node active in the settings navigation block.
149
            if ($learningplannode = $PAGE->settingsnav->find('learningplantemplates', \navigation_node::TYPE_SETTING)) {
150
                $learningplannode->make_active();
151
            }
152
        } else {
153
            throw new coding_exception('Unexpected context!');
154
        }
155
 
156
        $PAGE->set_pagelayout('admin');
157
        $PAGE->set_title($title);
158
 
159
        if (!empty($template)) {
160
            $PAGE->navbar->add($title, $templateurl);
161
            if (!empty($subtitle)) {
162
                $PAGE->navbar->add($subtitle, $url);
163
            }
164
 
165
        } else if (!empty($subtitle)) {
166
            // We're in a sub page without a specific template.
167
            $PAGE->navbar->add($subtitle, $url);
168
        }
169
 
170
        return array($title, $subtitle, $returnurl);
171
    }
172
 
173
    /**
174
     * Set-up a plan page.
175
     *
176
     * Example:
177
     * list($title, $subtitle) = page_helper::setup_for_plan($url, $template, $pagetitle);
178
     * echo $OUTPUT->heading($title);
179
     * echo $OUTPUT->heading($subtitle, 3);
180
     *
181
     * @param  int $userid The user ID.
182
     * @param  moodle_url $url The current page.
183
     * @param  \core_competency\plan $plan The plan, if any.
184
     * @param  string $subtitle The title of the subpage, if any.
185
     * @param  string $returntype The desired return page.
186
     * @return array With the following:
187
     *               - Page title
188
     *               - Page sub title
189
     *               - Return URL (main plan page)
190
     */
191
    public static function setup_for_plan($userid, moodle_url $url, $plan = null, $subtitle = '', $returntype = null) {
192
        global $PAGE, $USER;
193
 
194
        // Check that the user is a valid user.
195
        $user = core_user::get_user($userid);
196
        if (!$user || !core_user::is_real_user($userid)) {
197
            throw new \moodle_exception('invaliduser', 'error');
198
        }
199
 
200
        $context = context_user::instance($user->id);
201
 
202
        $plansurl = new moodle_url('/admin/tool/lp/plans.php', array('userid' => $userid));
203
        $planurl = null;
204
        if ($plan) {
205
            $planurl = new moodle_url('/admin/tool/lp/plan.php', array('id' => $plan->get('id')));
206
        }
207
 
208
        $returnurl = $plansurl;
209
        if ($returntype != 'plans' && $planurl) {
210
            $returnurl = $planurl;
211
        }
212
 
213
        $PAGE->navigation->override_active_url($plansurl);
214
        $PAGE->set_context($context);
215
 
216
        // If not his own plan, we want to extend the navigation for the user.
217
        $iscurrentuser = ($USER->id == $user->id);
218
        if (!$iscurrentuser) {
219
            $PAGE->navigation->extend_for_user($user);
220
            $PAGE->navigation->set_userid_for_parent_checks($user->id);
221
        }
222
 
223
        if (!empty($plan)) {
224
            $title = format_string($plan->get('name'), true, array('context' => $context));
225
        } else {
226
            $title = get_string('learningplans', 'tool_lp');
227
        }
228
 
229
        $PAGE->set_pagelayout('standard');
230
        $PAGE->set_url($url);
231
        $PAGE->set_title($title);
232
 
233
        if (!empty($plan)) {
234
            $PAGE->navbar->add($title, $planurl);
235
            if (!empty($subtitle)) {
236
                $PAGE->navbar->add($subtitle, $url);
237
            }
238
        } else if (!empty($subtitle)) {
239
            // We're in a sub page without a specific plan.
240
            $PAGE->navbar->add($subtitle, $url);
241
        }
242
 
243
        return array($title, $subtitle, $returnurl);
244
    }
245
 
246
    /**
247
     * Set-up a user evidence page.
248
     *
249
     * Example:
250
     * list($title, $subtitle) = page_helper::setup_for_user_evidence($url, $template, $pagetitle);
251
     * echo $OUTPUT->heading($title);
252
     * echo $OUTPUT->heading($subtitle, 3);
253
     *
254
     * @param  int $userid The user ID.
255
     * @param  moodle_url $url The current page.
256
     * @param  \core_competency\user_evidence $evidence The user evidence, if any.
257
     * @param  string $subtitle The title of the subpage, if any.
258
     * @param  string $returntype The desired return page.
259
     * @return array With the following:
260
     *               - Page title
261
     *               - Page sub title
262
     *               - Return URL (main plan page)
263
     */
264
    public static function setup_for_user_evidence($userid, moodle_url $url, $evidence = null, $subtitle = '', $returntype = null) {
265
        global $PAGE, $USER;
266
 
267
        // Check that the user is a valid user.
268
        $user = core_user::get_user($userid);
269
        if (!$user || !core_user::is_real_user($userid)) {
270
            throw new \moodle_exception('invaliduser', 'error');
271
        }
272
 
273
        $context = context_user::instance($user->id);
274
 
275
        $evidencelisturl = new moodle_url('/admin/tool/lp/user_evidence_list.php', array('userid' => $userid));
276
        $evidenceurl = null;
277
        if ($evidence) {
278
            $evidenceurl = new moodle_url('/admin/tool/lp/user_evidence.php', array('id' => $evidence->get('id')));
279
        }
280
 
281
        $returnurl = $evidencelisturl;
282
        if ($returntype != 'list' && $evidenceurl) {
283
            $returnurl = $evidenceurl;
284
        }
285
 
286
        $PAGE->navigation->override_active_url($evidencelisturl);
287
        $PAGE->set_context($context);
288
 
289
        // If not his own evidence, we want to extend the navigation for the user.
290
        $iscurrentuser = ($USER->id == $user->id);
291
        if (!$iscurrentuser) {
292
            $PAGE->navigation->extend_for_user($user);
293
            $PAGE->navigation->set_userid_for_parent_checks($user->id);
294
        }
295
 
296
        if (!empty($evidence)) {
297
            $title = format_string($evidence->get('name'), true, array('context' => $context));
298
        } else {
299
            $title = get_string('userevidence', 'tool_lp');
300
        }
301
 
302
        $PAGE->set_pagelayout('standard');
303
        $PAGE->set_url($url);
304
        $PAGE->set_title($title);
305
 
306
        if (!empty($evidence)) {
307
            $PAGE->navbar->add($title, $evidenceurl);
308
            if (!empty($subtitle)) {
309
                $PAGE->navbar->add($subtitle, $url);
310
            }
311
        } else if (!empty($subtitle)) {
312
            // We're in a sub page without a specific evidence.
313
            $PAGE->navbar->add($subtitle, $url);
314
        }
315
 
316
        return array($title, $subtitle, $returnurl);
317
    }
318
 
319
    /**
320
     * Set-up a framework page.
321
     *
322
     * Example:
323
     * list($pagetitle, $pagesubtitle, $url, $frameworksurl) = page_helper::setup_for_framework($id, $pagecontextid);
324
     * echo $OUTPUT->heading($pagetitle);
325
     * echo $OUTPUT->heading($pagesubtitle, 3);
326
     *
327
     * @param  int $id The framework ID.
328
     * @param  int $pagecontextid The page context ID.
329
     * @param  \core_competency\competency_framework $framework The framework.
330
     * @param  string $returntype The desired return page.
331
     * @return array With the following:
332
     *               - Page title
333
     *               - Page sub title
334
     *               - Page URL
335
     *               - Page framework URL
336
     */
337
    public static function setup_for_framework($id, $pagecontextid, $framework = null, $returntype = null) {
338
        global $PAGE, $SITE;
339
 
340
        // We keep the original context in the URLs, so that we remain in the same context.
341
        $url = new moodle_url("/admin/tool/lp/editcompetencyframework.php", array('id' => $id, 'pagecontextid' => $pagecontextid));
342
        if ($returntype) {
343
            $url->param('return', $returntype);
344
        }
345
        $frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $pagecontextid));
346
 
347
        $context = context::instance_by_id($pagecontextid);
348
        $PAGE->set_context($context);
349
        $PAGE->set_pagelayout('admin');
350
        $PAGE->set_url($url);
351
 
352
        $title = get_string('competencies', 'core_competency');
353
 
354
        if ($context->contextlevel == CONTEXT_COURSECAT) {
355
            \core_course_category::page_setup();
356
            // Set the competency frameworks node active in the settings navigation block.
357
            if ($competencyframeworksnode = $PAGE->settingsnav->find('competencyframeworks', \navigation_node::TYPE_SETTING)) {
358
                $competencyframeworksnode->make_active();
359
            }
360
        } else if ($context->contextlevel == CONTEXT_SYSTEM) {
361
            $PAGE->set_heading($SITE->fullname);
362
        } else {
363
            $PAGE->set_heading($title);
364
        }
365
 
366
        $PAGE->navigation->override_active_url($frameworksurl);
367
        if (empty($id)) {
368
            $pagetitle = get_string('competencyframeworks', 'tool_lp');
369
            $pagesubtitle = get_string('addnewcompetencyframework', 'tool_lp');
370
 
371
            $url->remove_params(array('id'));
372
            $PAGE->navbar->add($pagesubtitle, $url);
373
        } else {
374
            $pagetitle = $framework->get('shortname');
375
            $pagesubtitle = get_string('editcompetencyframework', 'tool_lp');
376
            if ($returntype == 'competencies') {
377
                $frameworksurl = new moodle_url('/admin/tool/lp/competencies.php', array(
378
                    'pagecontextid' => $pagecontextid,
379
                    'competencyframeworkid' => $id
380
                ));
381
            } else {
382
                $frameworksurl->param('competencyframeworkid', $id);
383
            }
384
 
385
            $PAGE->navbar->add($pagetitle, $frameworksurl);
386
            $PAGE->navbar->add($pagesubtitle, $url);
387
        }
388
 
389
        $PAGE->set_title($title);
390
        return array($pagetitle, $pagesubtitle, $url, $frameworksurl);
391
    }
392
 
393
    /**
394
     * Set-up a competency page.
395
     *
396
     * Example:
397
     * list($title, $subtitle) = page_helper::setup_for_competency($pagecontextid, $url, $competency, $pagetitle);
398
     * echo $OUTPUT->heading($title);
399
     * echo $OUTPUT->heading($subtitle, 3);
400
     *
401
     * @param  int $pagecontextid The page context ID.
402
     * @param  moodle_url $url The current page.
403
     * @param  \core_competency\competency_framework $framework The competency framework.
404
     * @param  \core_competency\competency $competency The competency, if any.
405
     * @param  \core_competency\competency $parent The parent competency, if any.
406
     * @return array With the following:
407
     *               - Page title
408
     *               - Page sub title
409
     *               - Return URL (main competencies page)
410
     * @throws coding_exception
411
     */
412
    public static function setup_for_competency($pagecontextid, moodle_url $url, $framework, $competency = null, $parent = null) {
413
        global $PAGE, $SITE;
414
 
415
        // Set page context.
416
        $pagecontext = context::instance_by_id($pagecontextid);
417
        $PAGE->set_context($pagecontext);
418
 
419
        // Set page heading.
420
        if ($pagecontext->contextlevel == CONTEXT_SYSTEM) {
421
            $heading = $SITE->fullname;
422
        } else if ($pagecontext->contextlevel == CONTEXT_COURSECAT) {
423
            $heading = $pagecontext->get_context_name();
424
        } else {
425
            throw new coding_exception('Unexpected context!');
426
        }
427
        $PAGE->set_heading($heading);
428
 
429
        // Set override active url.
430
        $frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', ['pagecontextid' => $pagecontextid]);
431
        $PAGE->navigation->override_active_url($frameworksurl);
432
 
433
        // Set return url.
434
        $returnurloptions = [
435
            'competencyframeworkid' => $framework->get('id'),
436
            'pagecontextid' => $pagecontextid
437
        ];
438
        $returnurl = new moodle_url('/admin/tool/lp/competencies.php', $returnurloptions);
439
        $PAGE->navbar->add($framework->get('shortname'), $returnurl);
440
 
441
        // Set page layout.
442
        $PAGE->set_pagelayout('admin');
443
 
444
        if (empty($competency)) {
445
            // Add mode.
446
            $title = format_string($framework->get('shortname'), true, ['context' => $pagecontext]);
447
 
448
            // Set the sub-title for add mode.
449
            $level = $parent ? $parent->get_level() + 1 : 1;
450
            $subtitle = get_string('taxonomy_add_' . $framework->get_taxonomy($level), 'tool_lp');
451
 
452
        } else {
453
            // Edit mode.
454
            $title = format_string($competency->get('shortname'), true, ['context' => $competency->get_context()]);
455
 
456
            // Add competency name to breadcrumbs, if available.
457
            $PAGE->navbar->add($title);
458
 
459
            // Set the sub-title for edit mode.
460
            $subtitle = get_string('taxonomy_edit_' . $framework->get_taxonomy($competency->get_level()), 'tool_lp');
461
        }
462
 
463
        // Set page title.
464
        $PAGE->set_title($title);
465
 
466
        // Set page url.
467
        $PAGE->set_url($url);
468
 
469
        // Add editing mode link to breadcrumbs, if available.
470
        if (!empty($subtitle)) {
471
            $PAGE->navbar->add($subtitle, $url);
472
        }
473
 
474
        return [$title, $subtitle, $returnurl];
475
    }
476
}