Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/completion/completion_criteria_duration.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 class completion_criteria_duration extends completion_criteria {
00027 
00032     public $criteriatype = COMPLETION_CRITERIA_TYPE_DURATION;
00033 
00041     public static function fetch($params) {
00042         $params['criteriatype'] = COMPLETION_CRITERIA_TYPE_DURATION;
00043         return self::fetch_helper('course_completion_criteria', __CLASS__, $params);
00044     }
00045 
00053     public function config_form_display(&$mform, $data = null) {
00054 
00055         $mform->addElement('checkbox', 'criteria_duration', get_string('enable'));
00056 
00057         $thresholdmenu=array();
00058         for ($i=1; $i<=30; $i++) {
00059             $seconds = $i * 86400;
00060             $thresholdmenu[$seconds] = get_string('numdays', '', $i);
00061         }
00062         $mform->addElement('select', 'criteria_duration_days', get_string('daysafterenrolment', 'completion'), $thresholdmenu);
00063 
00064         if ($this->id) {
00065             $mform->setDefault('criteria_duration', 1);
00066             $mform->setDefault('criteria_duration_days', $this->enrolperiod);
00067         }
00068     }
00069 
00076     public function update_config(&$data) {
00077 
00078         if (!empty($data->criteria_duration)) {
00079             $this->course = $data->id;
00080             $this->enrolperiod = $data->criteria_duration_days;
00081             $this->insert();
00082         }
00083     }
00084 
00090     private function get_timeenrolled($completion) {
00091         global $DB;
00092 
00093         return $DB->get_field_sql('
00094             SELECT eu.timestart
00095               FROM {user_enrolments} eu
00096               JOIN {enrol} e ON eu.enrolid = e.id
00097              WHERE e.courseid = ?
00098                AND eu.userid = ?', array($this->course, $completion->userid));
00099     }
00100 
00108     public function review($completion, $mark = true) {
00109         $timeenrolled = $this->get_timeenrolled($completion);
00110 
00111         // If duration since enrollment has passed
00112         if (!$this->enrolperiod || !$timeenrolled) {
00113             return false;
00114         }
00115 
00116         if (time() > ($timeenrolled + $this->enrolperiod)) {
00117             if ($mark) {
00118                 $completion->mark_complete();
00119             }
00120 
00121             return true;
00122         }
00123 
00124         return false;
00125     }
00126 
00132     public function get_title() {
00133         return get_string('enrolmentduration', 'completion');
00134     }
00135 
00141     public function get_title_detailed() {
00142         return ceil($this->enrolperiod / (60 * 60 * 24)) . ' days';
00143     }
00144 
00150     public function get_type_title() {
00151         return get_string('days', 'completion');
00152     }
00153 
00160     public function get_status($completion) {
00161         $timeenrolled = $this->get_timeenrolled($completion);
00162         $timeleft = $timeenrolled + $this->enrolperiod - time();
00163         $enrolperiod = ceil($this->enrolperiod / (60 * 60 * 24));
00164 
00165         $daysleft = ceil($timeleft / (60 * 60 * 24));
00166 
00167         return ($daysleft > 0 ? $daysleft : 0).' of '.$enrolperiod;
00168     }
00169 
00175     public function cron() {
00176         global $DB;
00177 
00178         /*
00179          * Get all users who match meet this criteria
00180          *
00181          * We can safely ignore duplicate enrolments for
00182          * a user in a course here as we only care if
00183          * one of the enrolments has passed the set
00184          * duration.
00185          */
00186         $sql = '
00187             SELECT
00188                 c.id AS course,
00189                 cr.id AS criteriaid,
00190                 u.id AS userid,
00191                 ue.timestart AS otimestart,
00192                 (ue.timestart + cr.enrolperiod) AS ctimestart,
00193                 ue.timecreated AS otimeenrolled,
00194                 (ue.timecreated + cr.enrolperiod) AS ctimeenrolled
00195             FROM
00196                 {user} u
00197             INNER JOIN
00198                 {user_enrolments} ue
00199              ON ue.userid = u.id
00200             INNER JOIN
00201                 {enrol} e
00202              ON e.id = ue.enrolid
00203             INNER JOIN
00204                 {course} c
00205              ON c.id = e.courseid
00206             INNER JOIN
00207                 {course_completion_criteria} cr
00208              ON c.id = cr.course
00209             LEFT JOIN
00210                 {course_completion_crit_compl} cc
00211              ON cc.criteriaid = cr.id
00212             AND cc.userid = u.id
00213             WHERE
00214                 cr.criteriatype = '.COMPLETION_CRITERIA_TYPE_DURATION.'
00215             AND c.enablecompletion = 1
00216             AND cc.id IS NULL
00217             AND
00218             (
00219                 ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
00220              OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?
00221             )
00222         ';
00223 
00224         // Loop through completions, and mark as complete
00225         $now = time();
00226         $rs = $DB->get_recordset_sql($sql, array($now, $now));
00227         foreach ($rs as $record) {
00228 
00229             $completion = new completion_criteria_completion((array)$record);
00230 
00231             // Use time start if not 0, otherwise use timeenrolled
00232             if ($record->otimestart) {
00233                 $completion->mark_complete($record->ctimestart);
00234             } else {
00235                 $completion->mark_complete($record->ctimeenrolled);
00236             }
00237         }
00238         $rs->close();
00239     }
00240 
00247     public function get_details($completion) {
00248         $details = array();
00249         $details['type'] = get_string('periodpostenrolment', 'completion');
00250         $details['criteria'] = get_string('remainingenroledfortime', 'completion');
00251         $details['requirement'] = get_string('xdays', 'completion', ceil($this->enrolperiod / (60*60*24)));
00252 
00253         // Get status
00254         $timeenrolled = $this->get_timeenrolled($completion);
00255         $timepassed = time() - $timeenrolled;
00256         $details['status'] = get_string('xdays', 'completion', floor($timepassed / (60*60*24)));
00257 
00258         return $details;
00259     }
00260 }
 All Data Structures Namespaces Files Functions Variables Enumerations