2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Manage attendance settings
20 * @package mod_attendance
21 * @copyright 2011 Artem Andreev <andreev.artem@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(dirname(__FILE__
).'/../../config.php');
26 require_once(dirname(__FILE__
).'/locallib.php');
28 $pageparams = new mod_attendance_preferences_page_params();
30 $id = required_param('id', PARAM_INT
);
31 $pageparams->action
= optional_param('action', null
, PARAM_INT
);
32 $pageparams->statusid
= optional_param('statusid', null
, PARAM_INT
);
33 $pageparams->statusset
= optional_param('statusset', 0, PARAM_INT
); // Set of statuses to view.
35 $cm = get_coursemodule_from_id('attendance', $id, 0, false
, MUST_EXIST
);
36 $course = $DB->get_record('course', array('id' => $cm->course
), '*', MUST_EXIST
);
37 $att = $DB->get_record('attendance', array('id' => $cm->instance
), '*', MUST_EXIST
);
39 require_login($course, true
, $cm);
41 $context = context_module
::instance($cm->id
);
42 require_capability('mod/attendance:changepreferences', $context);
44 // Make sure the statusset is valid.
45 $maxstatusset = attendance_get_max_statusset($att->id
);
46 if ($pageparams->statusset
> $maxstatusset +
1) {
47 $pageparams->statusset
= $maxstatusset +
1;
50 $att = new mod_attendance_structure($att, $cm, $course, $context, $pageparams);
52 $PAGE->set_url($att->url_preferences());
53 $PAGE->set_title($course->shortname
. ": ".$att->name
.' - '.get_string('settings', 'attendance'));
54 $PAGE->set_heading($course->fullname
);
55 $PAGE->set_cacheable(true
);
56 $PAGE->navbar
->add(get_string('settings', 'attendance'));
60 // Check sesskey if we are performing an action.
61 if (!empty($att->pageparams
->action
)) {
65 // TODO: combine this with the stuff in defaultstatus.php to avoid code duplication.
66 switch ($att->pageparams
->action
) {
67 case mod_attendance_preferences_page_params
::ACTION_ADD
:
68 $newacronym = optional_param('newacronym', null
, PARAM_TEXT
);
69 $newdescription = optional_param('newdescription', null
, PARAM_TEXT
);
70 $newgrade = optional_param('newgrade', 0, PARAM_RAW
);
71 $newstudentavailability = optional_param('newstudentavailability', null
, PARAM_INT
);
72 $newgrade = unformat_float($newgrade);
74 $newstatus = new stdClass();
75 $newstatus->attendanceid
= $att->id
;
76 $newstatus->acronym
= $newacronym;
77 $newstatus->description
= $newdescription;
78 $newstatus->grade
= $newgrade;
79 $newstatus->studentavailability
= $newstudentavailability;
80 $newstatus->setnumber
= $att->pageparams
->statusset
;
81 $newstatus->cm
= $att->cm
;
82 $newstatus->context
= $att->context
;
84 $status = attendance_add_status($newstatus);
86 $notification = $OUTPUT->notification(get_string('cantaddstatus', 'attendance'), 'error');
89 if ($pageparams->statusset
> $maxstatusset) {
90 $maxstatusset = $pageparams->statusset
; // Make sure the new maximum is shown without a page refresh.
93 case mod_attendance_preferences_page_params
::ACTION_DELETE
:
94 if (attendance_has_logs_for_status($att->pageparams
->statusid
)) {
95 print_error('cantdeletestatus', 'attendance', "attsettings.php?id=$id");
98 $confirm = optional_param('confirm', null
, PARAM_INT
);
99 $statuses = $att->get_statuses(false
);
100 $status = $statuses[$att->pageparams
->statusid
];
102 if (isset($confirm)) {
103 attendance_remove_status($status);
104 redirect($att->url_preferences(), get_string('statusdeleted', 'attendance'));
107 $message = get_string('deletecheckfull', '', get_string('variable', 'attendance'));
108 $message .= str_repeat(html_writer
::empty_tag('br'), 2);
109 $message .= $status->acronym
.': '.
110 ($status->description ?
$status->description
: get_string('nodescription', 'attendance'));
111 $params = array_merge($att->pageparams
->get_significant_params(), array('confirm' => 1));
112 echo $OUTPUT->header();
113 echo $OUTPUT->heading(get_string('attendanceforthecourse', 'attendance').' :: ' .format_string($course->fullname
));
114 echo $OUTPUT->confirm($message, $att->url_preferences($params), $att->url_preferences());
115 echo $OUTPUT->footer();
117 case mod_attendance_preferences_page_params
::ACTION_HIDE
:
118 $statuses = $att->get_statuses(false
);
119 $status = $statuses[$att->pageparams
->statusid
];
120 attendance_update_status($status, null
, null
, null
, 0, $att->context
, $att->cm
);
122 case mod_attendance_preferences_page_params
::ACTION_SHOW
:
123 $statuses = $att->get_statuses(false
);
124 $status = $statuses[$att->pageparams
->statusid
];
125 attendance_update_status($status, null
, null
, null
, 1, $att->context
, $att->cm
);
127 case mod_attendance_preferences_page_params
::ACTION_SAVE
:
128 $acronym = required_param_array('acronym', PARAM_TEXT
);
129 $description = required_param_array('description', PARAM_TEXT
);
130 $grade = required_param_array('grade', PARAM_RAW
);
131 $studentavailability = optional_param_array('studentavailability', null
, PARAM_RAW
);
132 $unmarkedstatus = optional_param('setunmarked', null
, PARAM_INT
);
134 foreach ($grade as &$val) {
135 $val = unformat_float($val);
137 $statuses = $att->get_statuses(false
);
139 foreach ($acronym as $id => $v) {
140 $status = $statuses[$id];
141 $setunmarked = false
;
142 if ($unmarkedstatus == $id) {
145 $errors[$id] = attendance_update_status($status, $acronym[$id], $description[$id], $grade[$id],
146 null
, $att->context
, $att->cm
, $studentavailability[$id], $setunmarked);
148 attendance_update_users_grade($att);
152 $output = $PAGE->get_renderer('mod_attendance');
153 $tabs = new attendance_tabs($att, attendance_tabs
::TAB_PREFERENCES
);
154 $prefdata = new attendance_preferences_data($att, array_filter($errors));
155 $setselector = new attendance_set_selector($att, $maxstatusset);
157 // Output starts here.
159 echo $output->header();
160 if (!empty($notification)) {
163 echo $output->heading(get_string('attendanceforthecourse', 'attendance').' :: '. format_string($course->fullname
));
164 echo $output->render($tabs);
165 echo $OUTPUT->box(get_string('preferences_desc', 'attendance'), 'generalbox attendancedesc', 'notice');
166 echo $output->render($setselector);
167 echo $output->render($prefdata);
169 echo $output->footer();