| 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 |  * Prints information about the reengagement to the user.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    mod_reengagement
 | 
        
           |  |  | 21 |  * @author     Peter Bulmer <peter.bulmer@catlayst.net.nz>
 | 
        
           |  |  | 22 |  * @copyright  2016 Catalyst IT {@link http://www.catalyst.net.nz}
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | use core_table\local\filter\filter;
 | 
        
           |  |  | 27 | use core_table\local\filter\integer_filter;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
 | 
        
           |  |  | 30 | require_once(dirname(__FILE__) . '/lib.php');
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | define('DEFAULT_PAGE_SIZE', 20);
 | 
        
           |  |  | 33 | define('SHOW_ALL_PAGE_SIZE', 5000);
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | $id = optional_param('id', 0, PARAM_INT); // Course_module ID.
 | 
        
           |  |  | 36 | $a = optional_param('a', 0, PARAM_INT);  // Reengagement instance ID.
 | 
        
           |  |  | 37 | $page = optional_param('page', 0, PARAM_INT); // Which page to show.
 | 
        
           |  |  | 38 | $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT); // How many per page.
 | 
        
           |  |  | 39 | $selectall = optional_param('selectall', false, PARAM_BOOL); // When rendering checkboxes against users mark them all checked.
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 | $params = array();
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | if ($id) {
 | 
        
           |  |  | 44 |     $params['id'] = $id;
 | 
        
           |  |  | 45 | } else {
 | 
        
           |  |  | 46 |     $params['a'] = $a;
 | 
        
           |  |  | 47 | }
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 | $PAGE->set_url('/mod/reengagement/view.php', $params);
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | if ($id) {
 | 
        
           |  |  | 52 |     $cm = get_coursemodule_from_id('reengagement', $id, 0, false, MUST_EXIST);
 | 
        
           |  |  | 53 |     $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
 | 
        
           |  |  | 54 |     $reengagement = $DB->get_record('reengagement', array('id' => $cm->instance), '*', MUST_EXIST);
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | } else if ($a) {
 | 
        
           |  |  | 57 |     $reengagement = $DB->get_record('reengagement', array('id' => $a), '*', MUST_EXIST);
 | 
        
           |  |  | 58 |     $course = $DB->get_record('course', array('id' => $reengagement->course), '*', MUST_EXIST);
 | 
        
           |  |  | 59 |     $cm = get_coursemodule_from_instance('reengagement', $reengagement->id, $course->id, false, MUST_EXIST);
 | 
        
           |  |  | 60 | } else {
 | 
        
           |  |  | 61 |     throw new moodle_exception('errornoid', 'mod_reengagement');
 | 
        
           |  |  | 62 | }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | require_login($course, true, $cm);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | // Make sure completion and restriction is enabled.
 | 
        
           |  |  | 67 | if (empty($CFG->enablecompletion) || empty($CFG->enableavailability)) {
 | 
        
           |  |  | 68 |     throw new moodle_exception('mustenablecompletionavailability', 'mod_reengagement');
 | 
        
           |  |  | 69 | }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 | $context = context_module::instance($cm->id);
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 | $event = \mod_reengagement\event\course_module_viewed::create(array(
 | 
        
           |  |  | 74 |     'objectid' => $reengagement->id,
 | 
        
           |  |  | 75 |     'context' => $context,
 | 
        
           |  |  | 76 | ));
 | 
        
           |  |  | 77 | $event->add_record_snapshot('course', $course);
 | 
        
           |  |  | 78 | $event->add_record_snapshot('reengagement', $reengagement);
 | 
        
           |  |  | 79 | $event->trigger();
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 | // Print the page header.
 | 
        
           |  |  | 82 | $strreengagements = get_string('modulenameplural', 'reengagement');
 | 
        
           |  |  | 83 | $strreengagement = get_string('modulename', 'reengagement');
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 | $PAGE->set_title(format_string($reengagement->name));
 | 
        
           |  |  | 86 | $PAGE->set_heading(format_string($course->fullname));
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 | echo $OUTPUT->header();
 | 
        
           |  |  | 89 | // Print the main part of the page.
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | $PAGE->set_context($context);
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | $canstart = has_capability('mod/reengagement:startreengagement', $context, null, false);
 | 
        
           |  |  | 94 | $canedit = has_capability('mod/reengagement:editreengagementduration', $context);
 | 
        
           |  |  | 95 | $bulkoperations = has_capability('mod/reengagement:bulkactions', $context);
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | if (empty($canstart) && empty($canedit)) {
 | 
        
           |  |  | 98 |     throw new moodle_exception('errorreengagementnotvalid', 'mod_reengagement');
 | 
        
           |  |  | 99 | }
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 | if ($canstart) {
 | 
        
           |  |  | 102 |     // Check reengagement record for this user.
 | 
        
           |  |  | 103 |     echo reengagement_checkstart($course, $cm, $reengagement);
 | 
        
           |  |  | 104 | }
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 | if ($canedit) {
 | 
        
           |  |  | 107 |     $task = \core\task\manager::get_scheduled_task('\mod_reengagement\task\cron_task');
 | 
        
           |  |  | 108 |     $lastrun = $task->get_last_run_time();
 | 
        
           |  |  | 109 |     if ($lastrun < time() - 28800) { // Check if cron run in last 8hrs.
 | 
        
           |  |  | 110 |         echo $OUTPUT->notification(get_string('cronwarning', 'reengagement'));
 | 
        
           |  |  | 111 |     }
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |     $filterset = new \mod_reengagement\table\reengagement_participants_filterset();
 | 
        
           |  |  | 114 |     // We pretend the courseid is the cmid, because the core Moodle participants filter doesn't allow adding new filter types.
 | 
        
           |  |  | 115 |     $filterset->add_filter(new integer_filter('courseid', filter::JOINTYPE_DEFAULT, [(int) $cm->id]));
 | 
        
           |  |  | 116 |     $participanttable = new \mod_reengagement\table\reengagement_participants("reengagement-index-participants-{$cm->id}");
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 |     echo '<div class="userlist">';
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |     // Should use this variable so that we don't break stuff every time a variable is added or changed.
 | 
        
           |  |  | 121 |     $baseurl = new moodle_url('/mod/reengagement/view.php', array(
 | 
        
           |  |  | 122 |         'contextid' => $context->id,
 | 
        
           |  |  | 123 |         'id' => $cm->id,
 | 
        
           |  |  | 124 |         'perpage' => $perpage));
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |     $participanttable->set_filterset($filterset);
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |     ob_start();
 | 
        
           |  |  | 129 |     $participanttable->out($perpage, true);
 | 
        
           |  |  | 130 |     $participanttablehtml = ob_get_contents();
 | 
        
           |  |  | 131 |     ob_end_clean();
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 |     echo html_writer::start_tag('form', [
 | 
        
           |  |  | 134 |         'action' => 'bulkchange.php',
 | 
        
           |  |  | 135 |         'method' => 'post',
 | 
        
           |  |  | 136 |         'id' => 'participantsform',
 | 
        
           |  |  | 137 |         'data-course-id' => $cm->id,
 | 
        
           |  |  | 138 |         'data-table-unique-id' => $participanttable->uniqueid,
 | 
        
           |  |  | 139 |         'data-table-default-per-page' => ($perpage < DEFAULT_PAGE_SIZE) ? $perpage : DEFAULT_PAGE_SIZE,
 | 
        
           |  |  | 140 |     ]);
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |     echo '<div>';
 | 
        
           |  |  | 143 |     echo '<input type="hidden" name="id" value="' . $cm->id . '" />';
 | 
        
           |  |  | 144 |     echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
 | 
        
           |  |  | 145 |     echo '<input type="hidden" name="returnto" value="' . s($PAGE->url->out(false)) . '" />';
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |     echo html_writer::tag(
 | 
        
           |  |  | 148 |         'p',
 | 
        
           |  |  | 149 |         get_string('countparticipantsfound', 'core_user', $participanttable->totalrows),
 | 
        
           |  |  | 150 |         [
 | 
        
           |  |  | 151 |             'data-region' => 'participant-count',
 | 
        
           |  |  | 152 |         ]
 | 
        
           |  |  | 153 |     );
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |     echo $participanttablehtml;
 | 
        
           |  |  | 156 |     $perpagevisible = '';
 | 
        
           |  |  | 157 |     $perpagestring = '';
 | 
        
           |  |  | 158 |     $perpagesize = '';
 | 
        
           |  |  | 159 |     $perpageurl = clone($baseurl);
 | 
        
           |  |  | 160 |     $perpageurl->remove_params('perpage');
 | 
        
           |  |  | 161 |     if ($perpage == SHOW_ALL_PAGE_SIZE && $participanttable->totalrows > DEFAULT_PAGE_SIZE) {
 | 
        
           |  |  | 162 |         $perpageurl->param('perpage', $participanttable->totalrows);
 | 
        
           |  |  | 163 |         $perpagesize = SHOW_ALL_PAGE_SIZE;
 | 
        
           |  |  | 164 |         $perpagevisible = true;
 | 
        
           |  |  | 165 |         $perpagestring = get_string('showperpage', '', DEFAULT_PAGE_SIZE);
 | 
        
           |  |  | 166 |     } else if ($participanttable->get_page_size() < $participanttable->totalrows) {
 | 
        
           |  |  | 167 |         $perpageurl->param('perpage', SHOW_ALL_PAGE_SIZE);
 | 
        
           |  |  | 168 |         $perpagesize = SHOW_ALL_PAGE_SIZE;
 | 
        
           |  |  | 169 |         $perpagevisible = true;
 | 
        
           |  |  | 170 |         $perpagestring = get_string('showall', '', $participanttable->totalrows);
 | 
        
           |  |  | 171 |     }
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 |     $perpageclasses = '';
 | 
        
           |  |  | 174 |     if (!$perpagevisible) {
 | 
        
           |  |  | 175 |         $perpageclasses = 'hidden';
 | 
        
           |  |  | 176 |     }
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |     echo $OUTPUT->container(html_writer::link(
 | 
        
           |  |  | 179 |         $perpageurl,
 | 
        
           |  |  | 180 |         $perpagestring,
 | 
        
           |  |  | 181 |         [
 | 
        
           |  |  | 182 |             'data-action' => 'showcount',
 | 
        
           |  |  | 183 |             'data-target-page-size' => $perpagesize,
 | 
        
           |  |  | 184 |             'class' => $perpageclasses,
 | 
        
           |  |  | 185 |         ]
 | 
        
           |  |  | 186 |     ), [], 'showall');
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |     $options = new stdClass();
 | 
        
           |  |  | 189 |     $options->courseid = $cm->id;
 | 
        
           |  |  | 190 |     $options->stateHelpIcon = $OUTPUT->help_icon('publishstate', 'notes');
 | 
        
           |  |  | 191 |   | 
        
           |  |  | 192 |     if ($bulkoperations) {
 | 
        
           |  |  | 193 |         echo '<br /><div class="buttons"><div class="form-inline">';
 | 
        
           |  |  | 194 |   | 
        
           |  |  | 195 |         if ($participanttable->get_page_size() < $participanttable->totalrows) {
 | 
        
           |  |  | 196 |             // Select all users, refresh table showing all users and mark them all selected.
 | 
        
           |  |  | 197 |             $label = get_string('selectalluserswithcount', 'moodle', $participanttable->totalrows);
 | 
        
           |  |  | 198 |             echo html_writer::empty_tag('input', [
 | 
        
           |  |  | 199 |                 'type' => 'button',
 | 
        
           |  |  | 200 |                 'id' => 'checkall',
 | 
        
           |  |  | 201 |                 'class' => 'btn btn-secondary',
 | 
        
           |  |  | 202 |                 'value' => $label,
 | 
        
           |  |  | 203 |                 'data-target-page-size' => $participanttable->totalrows,
 | 
        
           |  |  | 204 |             ]);
 | 
        
           |  |  | 205 |         }
 | 
        
           |  |  | 206 |         echo html_writer::end_tag('div');
 | 
        
           |  |  | 207 |         $displaylist = array();
 | 
        
           |  |  | 208 |         $displaylist['#messageselect'] = get_string('messageselectadd');
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 |         $pluginoptions = [];
 | 
        
           |  |  | 211 |         $params = ['operation' => 'resetbyfirstcourseaccess'];
 | 
        
           |  |  | 212 |         $url = new moodle_url('bulkchange.php', $params);
 | 
        
           |  |  | 213 |         list ($periodcount, $period) = reengagement_get_readable_duration($reengagement->duration, true);
 | 
        
           |  |  | 214 |         $duration = $periodcount . " " . $period;
 | 
        
           |  |  | 215 |         $pluginoptions['resetbyfirstaccess'] = get_string('resetbyfirstaccess', 'mod_reengagement', $duration);
 | 
        
           |  |  | 216 |         $pluginoptions['resetbyenrolment'] = get_string('resetbyenrolment', 'mod_reengagement', $duration);
 | 
        
           |  |  | 217 |         $pluginoptions['resetbyspecificdate'] = get_string('resetbyspecificdate', 'mod_reengagement');
 | 
        
           |  |  | 218 |   | 
        
           |  |  | 219 |         $name = get_string('resetcompletion', 'mod_reengagement');
 | 
        
           |  |  | 220 |         $displaylist[] = [$name => $pluginoptions];
 | 
        
           |  |  | 221 |   | 
        
           |  |  | 222 |         echo $OUTPUT->help_icon('withselectedusers', 'mod_reengagement');
 | 
        
           |  |  | 223 |         echo html_writer::tag('label', get_string("withselectedusers"), array('for' => 'formactionid'));
 | 
        
           |  |  | 224 |         echo html_writer::select($displaylist, 'formaction', '', array('' => 'choosedots'), array('id' => 'formactionid'));
 | 
        
           |  |  | 225 |   | 
        
           |  |  | 226 |         echo '<noscript style="display:inline">';
 | 
        
           |  |  | 227 |         echo '<div><input type="submit" value="' . get_string('ok') . '" /></div>';
 | 
        
           |  |  | 228 |         echo '</noscript>';
 | 
        
           |  |  | 229 |         echo '</div></div>';
 | 
        
           |  |  | 230 |         $options->noteStateNames = note_get_state_names();
 | 
        
           |  |  | 231 |     }
 | 
        
           |  |  | 232 |     echo '</form>';
 | 
        
           |  |  | 233 |     $PAGE->requires->js_call_amd('core_user/participants', 'init', [$options]);
 | 
        
           |  |  | 234 |   | 
        
           |  |  | 235 |     echo '</div>';  // Userlist.
 | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 | }
 | 
        
           |  |  | 238 |   | 
        
           |  |  | 239 | // Finish the page.
 | 
        
           |  |  | 240 | echo $OUTPUT->footer($course);
 |