| 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 for editing badges criteria settings.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    core
 | 
        
           |  |  | 21 |  * @subpackage badges
 | 
        
           |  |  | 22 |  * @copyright  2013 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
 | 
        
           |  |  | 25 |  */
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | require_once(__DIR__ . '/../config.php');
 | 
        
           |  |  | 28 | require_once($CFG->libdir . '/badgeslib.php');
 | 
        
           |  |  | 29 | require_once($CFG->dirroot . '/badges/criteria_form.php');
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | $badgeid = optional_param('badgeid', 0, PARAM_INT); // Badge ID.
 | 
        
           |  |  | 32 | $type    = optional_param('type', 0, PARAM_INT); // Criteria type.
 | 
        
           |  |  | 33 | $edit    = optional_param('edit', 0, PARAM_INT); // Edit criteria ID.
 | 
        
           |  |  | 34 | $crit    = optional_param('crit', 0, PARAM_INT); // Criteria ID for managing params.
 | 
        
           |  |  | 35 | $param   = optional_param('param', '', PARAM_TEXT); // Param name for managing params.
 | 
        
           |  |  | 36 | $goback    = optional_param('cancel', '', PARAM_TEXT);
 | 
        
           |  |  | 37 | $addcourse = optional_param('addcourse', '', PARAM_TEXT);
 | 
        
           |  |  | 38 | $submitcourse = optional_param('submitcourse', '', PARAM_TEXT);
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | require_login();
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 | $return = new moodle_url('/badges/criteria.php', array('id' => $badgeid));
 | 
        
           |  |  | 43 | $badge = new badge($badgeid);
 | 
        
           | 1441 | ariadna | 44 | $title = [get_string('addcriterion', 'badges'), $badge->name];
 | 
        
           | 1 | efrain | 45 | $context = $badge->get_context();
 | 
        
           |  |  | 46 | $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | require_capability('moodle/badges:configurecriteria', $context);
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 | if (!empty($goback)) {
 | 
        
           |  |  | 51 |     redirect($return);
 | 
        
           |  |  | 52 | }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 | // Make sure that no actions available for locked or active badges.
 | 
        
           |  |  | 55 | if ($badge->is_active() || $badge->is_locked()) {
 | 
        
           |  |  | 56 |     redirect($return);
 | 
        
           |  |  | 57 | }
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 | // Make sure the criteria type is accepted.
 | 
        
           |  |  | 60 | $accepted = $badge->get_accepted_criteria();
 | 
        
           |  |  | 61 | if (!in_array($type, $accepted)) {
 | 
        
           |  |  | 62 |     redirect($return);
 | 
        
           |  |  | 63 | }
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 | if ($badge->type == BADGE_TYPE_COURSE) {
 | 
        
           |  |  | 66 |     require_login($badge->courseid);
 | 
        
           |  |  | 67 |     $course = get_course($badge->courseid);
 | 
        
           |  |  | 68 |     $heading = format_string($course->fullname, true, ['context' => $context]);
 | 
        
           | 1441 | ariadna | 69 |     $title[] = $heading;
 | 
        
           | 1 | efrain | 70 |     $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
 | 
        
           |  |  | 71 |     $PAGE->set_pagelayout('standard');
 | 
        
           |  |  | 72 |     navigation_node::override_active_url($navurl);
 | 
        
           |  |  | 73 | } else {
 | 
        
           |  |  | 74 |     $PAGE->set_pagelayout('admin');
 | 
        
           |  |  | 75 |     $heading = get_string('administrationsite');
 | 
        
           |  |  | 76 |     navigation_node::override_active_url($navurl, true);
 | 
        
           |  |  | 77 | }
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | $urlparams = array('badgeid' => $badgeid, 'edit' => $edit, 'type' => $type, 'crit' => $crit);
 | 
        
           |  |  | 80 | $PAGE->set_context($context);
 | 
        
           |  |  | 81 | $PAGE->set_url('/badges/criteria_settings.php', $urlparams);
 | 
        
           |  |  | 82 | $PAGE->set_heading($heading);
 | 
        
           | 1441 | ariadna | 83 | $PAGE->set_title(implode(\moodle_page::TITLE_SEPARATOR, $title));
 | 
        
           | 1 | efrain | 84 | $PAGE->navbar->add($badge->name, new moodle_url('overview.php', array('id' => $badge->id)))
 | 
        
           |  |  | 85 |     ->add(get_string('bcriteria', 'badges'), new moodle_url('criteria.php', ['id' => $badge->id]))
 | 
        
           |  |  | 86 |     ->add(get_string('criteria_' . $type, 'badges'));
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 | $cparams = array('criteriatype' => $type, 'badgeid' => $badge->id);
 | 
        
           |  |  | 89 | if ($edit) {
 | 
        
           |  |  | 90 |     $criteria = $badge->criteria[$type];
 | 
        
           |  |  | 91 |     $msg = 'criteriaupdated';
 | 
        
           |  |  | 92 | } else {
 | 
        
           |  |  | 93 |     $criteria = award_criteria::build($cparams);
 | 
        
           |  |  | 94 |     $msg = 'criteriacreated';
 | 
        
           |  |  | 95 | }
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | $mform = new edit_criteria_form($FULLME, array('criteria' => $criteria, 'addcourse' => $addcourse, 'course' => $badge->courseid));
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | if (!empty($addcourse)) {
 | 
        
           |  |  | 100 |     if ($data = $mform->get_data()) {
 | 
        
           |  |  | 101 |         // If no criteria yet, add overall aggregation.
 | 
        
           |  |  | 102 |         if (count($badge->criteria) == 0) {
 | 
        
           |  |  | 103 |             $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 | 
        
           |  |  | 104 |             $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 | 
        
           |  |  | 105 |         }
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         $id = $criteria->add_courses($data->courses);
 | 
        
           |  |  | 108 |         redirect(new moodle_url('/badges/criteria_settings.php',
 | 
        
           |  |  | 109 |             array('badgeid' => $badgeid, 'edit' => true, 'type' => BADGE_CRITERIA_TYPE_COURSESET, 'crit' => $id)));
 | 
        
           |  |  | 110 |     }
 | 
        
           |  |  | 111 | } else if ($data = $mform->get_data()) {
 | 
        
           |  |  | 112 |     // If no criteria yet, add overall aggregation.
 | 
        
           |  |  | 113 |     if (count($badge->criteria) == 0) {
 | 
        
           |  |  | 114 |         $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 | 
        
           |  |  | 115 |         $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 | 
        
           |  |  | 116 |     }
 | 
        
           |  |  | 117 |     $criteria->save((array)$data);
 | 
        
           |  |  | 118 |     $return->param('msg', $msg);
 | 
        
           |  |  | 119 |     redirect($return);
 | 
        
           |  |  | 120 | }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | echo $OUTPUT->header();
 | 
        
           |  |  | 123 | $mform->display();
 | 
        
           | 1441 | ariadna | 124 | echo $OUTPUT->footer();
 |