-
Notifications
You must be signed in to change notification settings - Fork 1
/
preferences_form.php
163 lines (148 loc) · 8.19 KB
/
preferences_form.php
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
30
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?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/>.
/**
* Course fisher - Course creation preferences form.
*
* @package local_coursefisher
* @copyright 2016 Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* Course fisher - Course creation preferences form class.
*
* @package local_coursefisher
* @copyright 2016 Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preferences_form extends moodleform {
/**
* The preferences form definition.
*/
public function definition() {
$mform = $this->_form;
$selectedcoursehash = $this->_customdata['coursehash'];
$groupcourses = $this->_customdata['groupcourses'];
if (!empty($groupcourses)) {
$coursehashes = array_keys($groupcourses);
$courseidchoices = array();
$existscourse = false;
$singletext = '';
$grouptext = '';
$hiddencoursehash = '';
$addsinglecoursestr = get_string('addsinglecourse', 'local_coursefisher');
$addcoursegroupstr = get_string('addcoursegroup', 'local_coursefisher');
$groupsonly = get_config('local_coursefisher', 'forceonlygroups');
if ((count($coursehashes) == 1) || empty($groupsonly)) {
$coursecategories = html_writer::tag('span', $groupcourses[$selectedcoursehash]->path,
array('class' => 'addcoursecategory'));
$coursename = html_writer::tag('span', $groupcourses[$selectedcoursehash]->fullname,
array('class' => 'addcoursename'));
$singletext .= html_writer::tag ('span', $coursename.$coursecategories, array('class' => 'singlecourse'));
$courseidchoices[] = &$mform->createElement('radio', 'courseid', null, $addsinglecoursestr.$singletext,
$selectedcoursehash);
}
if (count($coursehashes) > 1) {
$grouphash = implode('', $coursehashes);
$grouptext .= html_writer::start_tag ('span', array('class' => 'groupcourses'));
$first = true;
foreach ($groupcourses as $groupcourse) {
$class = 'groupcourse';
$alertmessage = '';
if ($first) {
$class .= ' groupfirstcourse';
$first = false;
}
$coursecategories = html_writer::tag('span', $groupcourse->path, array('class' => 'addcoursecategory'));
$coursename = html_writer::tag('span', $groupcourse->fullname, array('class' => 'addcoursename'));
if (isset($groupcourse->exists) && $groupcourse->exists) {
$class .= ' existentcourse';
$alertmessage = html_writer::tag('span', get_string('existentcourse', 'local_coursefisher'),
array('class' => 'existentcourse'));
$existscourse = true;
$courseurl = new moodle_url('/course/view.php', array('id' => $groupcourse->id));
$courselink = html_writer::tag('a', $groupcourse->fullname,
array('href' => $courseurl, 'target' => '_blank'));
$coursename = html_writer::tag('span', $courselink, array('class' => 'addcoursename'));
}
$grouptext .= html_writer::tag ('span', $coursename.$alertmessage.$coursecategories,
array('class' => $class));
}
$grouptext .= html_writer::end_tag ('span');
$courseidchoices[] = &$mform->createElement('radio', 'courseid', null, $addcoursegroupstr.$grouptext,
$grouphash);
}
if (count($courseidchoices) == 2) {
$mform->addGroup($courseidchoices, 'coursegrp', get_string('choosewhatadd', 'local_coursefisher'),
array(''), false);
$mform->setDefault('courseid', $grouphash);
} else {
if (count($coursehashes) == 1) {
$mform->addElement('static', 'coursegrp', $addsinglecoursestr, $singletext);
$hiddencoursehash = $selectedcoursehash;
} else {
$grouphash = implode('', $coursehashes);
$mform->addElement('static', 'coursegrp', $addcoursegroupstr, $grouptext);
$hiddencoursehash = $grouphash;
}
$mform->addElement('hidden', 'courseid', $hiddencoursehash);
$mform->setType('courseid', PARAM_ALPHANUM);
}
$existentchoices = array();
if ($existscourse) {
$existentactions = array('join', 'separated');
foreach ($existentactions as $existentaction) {
$existentchoices[] = &$mform->createElement('radio', 'existent', null, get_string($existentaction,
'local_coursefisher'), $existentaction);
}
if (!empty($existentchoices)) {
$mform->addGroup($existentchoices, 'exitentgrp', get_string('chooseexistsaction', 'local_coursefisher'),
array(''), false);
$mform->disabledIf('exitentgrp', 'courseid', 'neq', $grouphash);
}
}
$actionchoices = array();
$actions = get_config('local_coursefisher', 'actions');
if (!empty($actions)) {
$permittedactions = explode(',', $actions);
foreach ($permittedactions as $permittedaction) {
$actionchoices[] = &$mform->createElement('radio', 'action', null, get_string($permittedaction,
'local_coursefisher'), $permittedaction);
}
if (!empty($actionchoices)) {
$mform->addGroup($actionchoices, 'actiongrp', get_string('choosenextaction', 'local_coursefisher'),
array(''), false);
}
}
$manycourseid = (count($courseidchoices) > 1);
$manyaction = (!empty($actionchoices) && (count($actionchoices) > 1));
$manyexistentchoice = (!empty($existentchoices) && (count($existentchoices) > 1));
if ($manycourseid || $manyaction || $manyexistentchoice) {
// Normally you use add_action_buttons instead of this code.
$mform->addElement('submit', 'submitbutton', get_string('execute', 'local_coursefisher'));
} else if (!empty($actionchoices) && (count($actionchoices) == 1)) {
redirect(new moodle_url('/local/coursefisher/addcourse.php',
array('courseid' => $hiddencoursehash, 'action' => $permittedactions[0])));
} else {
redirect(new moodle_url('/local/coursefisher/addcourse.php',
array('courseid' => $hiddencoursehash, 'action' => 'view')));
}
} else {
redirect(new moodle_url('/local/coursefisher/addcourse.php'));
}
}
}