| 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 |  * This page handles deleting assigment overrides
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    mod_assign
 | 
        
           |  |  | 21 |  * @copyright  2016 Ilya Tregubov
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | require_once(dirname(__FILE__) . '/../../config.php');
 | 
        
           |  |  | 27 | require_once($CFG->dirroot.'/mod/assign/lib.php');
 | 
        
           |  |  | 28 | require_once($CFG->dirroot.'/mod/assign/locallib.php');
 | 
        
           |  |  | 29 | require_once($CFG->dirroot.'/mod/assign/override_form.php');
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | $overrideid = required_param('id', PARAM_INT);
 | 
        
           |  |  | 32 | $confirm = optional_param('confirm', false, PARAM_BOOL);
 | 
        
           | 1441 | ariadna | 33 | $recalculate = optional_param('recalculate', false, PARAM_BOOL);
 | 
        
           | 1 | efrain | 34 |   | 
        
           |  |  | 35 | if (! $override = $DB->get_record('assign_overrides', array('id' => $overrideid))) {
 | 
        
           |  |  | 36 |     throw new \moodle_exception('invalidoverrideid', 'assign');
 | 
        
           |  |  | 37 | }
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 | list($course, $cm) = get_course_and_cm_from_instance($override->assignid, 'assign');
 | 
        
           |  |  | 40 | $context = context_module::instance($cm->id);
 | 
        
           |  |  | 41 | $assign = new assign($context, null, null);
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | require_login($course, false, $cm);
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | // Check the user has the required capabilities to modify an override.
 | 
        
           |  |  | 46 | require_capability('mod/assign:manageoverrides', $context);
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | if ($override->groupid) {
 | 
        
           |  |  | 49 |     if (!groups_group_visible($override->groupid, $course, $cm)) {
 | 
        
           |  |  | 50 |         throw new \moodle_exception('invalidoverrideid', 'assign');
 | 
        
           |  |  | 51 |     }
 | 
        
           |  |  | 52 | } else {
 | 
        
           |  |  | 53 |     if (!groups_user_groups_visible($course, $override->userid, $cm)) {
 | 
        
           |  |  | 54 |         throw new \moodle_exception('invalidoverrideid', 'assign');
 | 
        
           |  |  | 55 |     }
 | 
        
           |  |  | 56 | }
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 | $url = new moodle_url('/mod/assign/overridedelete.php', array('id' => $override->id));
 | 
        
           |  |  | 59 | $confirmurl = new moodle_url($url, array('id' => $override->id, 'confirm' => 1));
 | 
        
           |  |  | 60 | $cancelurl = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 | if (!empty($override->userid)) {
 | 
        
           |  |  | 63 |     $cancelurl->param('mode', 'user');
 | 
        
           |  |  | 64 | }
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | // If confirm is set (PARAM_BOOL) then we have confirmation of intention to delete.
 | 
        
           |  |  | 67 | if ($confirm) {
 | 
        
           |  |  | 68 |     require_sesskey();
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     $assign->delete_override($override->id);
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |     reorder_group_overrides($assign->get_instance()->id);
 | 
        
           |  |  | 73 |   | 
        
           | 1441 | ariadna | 74 |     // Recalculate grades after the override is deleted.
 | 
        
           |  |  | 75 |     if ($recalculate) {
 | 
        
           |  |  | 76 |         $assignintance = clone $assign->get_instance();
 | 
        
           |  |  | 77 |         $assignintance->cmidnumber = $assign->get_course_module()->idnumber;
 | 
        
           |  |  | 78 |         if (!$override->groupid) {
 | 
        
           |  |  | 79 |             assign_update_grades($assignintance, $override->userid);
 | 
        
           |  |  | 80 |         } else {
 | 
        
           |  |  | 81 |             // If it is group mode.
 | 
        
           |  |  | 82 |             $groupmembers = groups_get_members($override->groupid);
 | 
        
           |  |  | 83 |             foreach ($groupmembers as $groupmember) {
 | 
        
           |  |  | 84 |                 assign_update_grades($assignintance, $groupmember->id);
 | 
        
           |  |  | 85 |             }
 | 
        
           |  |  | 86 |         }
 | 
        
           |  |  | 87 |     }
 | 
        
           |  |  | 88 |   | 
        
           | 1 | efrain | 89 |     redirect($cancelurl);
 | 
        
           |  |  | 90 | }
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 | // Prepare the page to show the confirmation form.
 | 
        
           |  |  | 93 | $stroverride = get_string('override', 'assign');
 | 
        
           |  |  | 94 | $title = get_string('deletecheck', null, $stroverride);
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | $PAGE->set_url($url);
 | 
        
           |  |  | 97 | $PAGE->set_pagelayout('admin');
 | 
        
           |  |  | 98 | $PAGE->add_body_class('limitedwidth');
 | 
        
           |  |  | 99 | $PAGE->navbar->add($title);
 | 
        
           |  |  | 100 | $PAGE->set_title($title);
 | 
        
           |  |  | 101 | $PAGE->set_heading($course->fullname);
 | 
        
           |  |  | 102 | $PAGE->activityheader->set_attrs([
 | 
        
           |  |  | 103 |     "title" => format_string($assign->get_instance()->name, true, ['context' => $context]),
 | 
        
           |  |  | 104 |     "description" => "",
 | 
        
           |  |  | 105 |     "hidecompletion" => true
 | 
        
           |  |  | 106 | ]);
 | 
        
           |  |  | 107 | $PAGE->set_secondary_active_tab('mod_assign_useroverrides');
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 | echo $OUTPUT->header();
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 | if ($override->groupid) {
 | 
        
           |  |  | 112 |     $group = $DB->get_record('groups', array('id' => $override->groupid), 'id, name');
 | 
        
           |  |  | 113 |     $confirmstr = get_string("overridedeletegroupsure", "assign", format_string($group->name, true, ['context' => $context]));
 | 
        
           |  |  | 114 | } else {
 | 
        
           |  |  | 115 |     $userfieldsapi = \core_user\fields::for_name();
 | 
        
           |  |  | 116 |     $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
 | 
        
           |  |  | 117 |     $user = $DB->get_record('user', array('id' => $override->userid),
 | 
        
           |  |  | 118 |             'id, ' . $namefields);
 | 
        
           |  |  | 119 |     $confirmstr = get_string("overridedeleteusersure", "assign", fullname($user));
 | 
        
           |  |  | 120 | }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | echo $OUTPUT->confirm($confirmstr, $confirmurl, $cancelurl);
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 | echo $OUTPUT->footer();
 |