From 1debc80e2dce59beefb810aeff22956c0f6f519a Mon Sep 17 00:00:00 2001 From: Artem Andreev Date: Thu, 30 Jun 2011 17:43:13 +0400 Subject: [PATCH] Finished implementation of adding new sessions --- locallib.php | 46 ++++++++++----------------- sessions.php | 89 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 97 insertions(+), 38 deletions(-) diff --git a/locallib.php b/locallib.php index 7aad9a1..d6971c8 100644 --- a/locallib.php +++ b/locallib.php @@ -651,34 +651,22 @@ class attforblock { return $this->currentgroup; } - public function add_session_from_form_data($formdata) { + public function add_sessions($sessions) { global $DB; - - $duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS; - - $rec->courseid = $this->course->id; - $rec->attendanceid = $this->id; - $rec->sessdate = $formdata->sessiondate; - $rec->duration = $duration; - $rec->description = $formdata->sdescription['text']; - $rec->descriptionformat = $formdata->sdescription['format']; - $rec->timemodified = time(); - - if ($formdata->sessiontype == self::SESSION_COMMON) { - $rec->id = $DB->insert_record('attendance_sessions', $rec); - $description = file_save_draft_area_files($formdata->sdescription['itemid'], - $this->context->id, 'mod_attforblock', 'session', $rec->id, - array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0), $formdata->sdescription['text']); - $DB->set_field('attendance_sessions', 'description', $description, array('id' => $rec->id)); - } else { - foreach ($formdata->groups as $groupid) { - $rec->groupid = $groupid; - $rec->id = $DB->insert_record('attendance_sessions', $rec); - $description = file_save_draft_area_files($formdata->sdescription['itemid'], - $this->context->id, 'mod_attforblock', 'session', $rec->id, - array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0), $formdata->sdescription['text']); - $DB->set_field('attendance_sessions', 'description', $description, array('id' => $rec->id)); - } + + $sessionsids = array(); + + foreach ($sessions as $sess) { + $sess->attendanceid = $this->id; + + $sid = $DB->insert_record('attendance_sessions', $sess); + $description = file_save_draft_area_files($sess->descriptionitemid, + $this->context->id, 'mod_attforblock', 'session', $sid, + array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0), + $sess->description); + $DB->set_field('attendance_sessions', 'description', $description, array('id' => $sid)); + + $sessionsids[] = $sid; } // TODO: log //add_to_log($course->id, 'attendance', 'one session added', 'mod/attforblock/manage.php?id='.$id, $user->lastname.' '.$user->firstname); @@ -714,7 +702,7 @@ class attforblock { foreach($formdata as $key => $value) { if(substr($key, 0, 4) == 'user') { $sid = substr($key, 4); - $sesslog[$sid] = new Object(); + $sesslog[$sid] = new stdClass(); $sesslog[$sid]->studentid = $sid; $sesslog[$sid]->statusid = $value; $sesslog[$sid]->statusset = $statuses; @@ -979,7 +967,7 @@ class attforblock { // TODO: log } - public function change_sessions_duration($sessionsids, $duration) { + public function update_sessions_duration($sessionsids, $duration) { global $DB; $now = time(); diff --git a/sessions.php b/sessions.php index 570d62f..2ab3f36 100644 --- a/sessions.php +++ b/sessions.php @@ -35,14 +35,9 @@ switch ($att->pageparams->action) { $mform = new mod_attforblock_add_form($url, $formparams); if ($mform->is_submitted()) { - $formdata = $mform->get_data(); - if (isset($formdata->addmultiply)) { - notice(get_string('sessionsgenerated','attforblock'), $url); - } - else { - $att->add_session_from_form_data($formdata); - redirect($url, get_string('sessionadded','attforblock')); - } + $sessions = construct_sessions_data_for_add($mform->get_data()); + $att->add_sessions($sessions); + redirect($url, get_string('sessionsgenerated','attforblock')); } break; case att_sessions_page_params::ACTION_UPDATE: @@ -140,7 +135,7 @@ switch ($att->pageparams->action) { $sessionsids = explode('_', $fromform->ids); $duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS; - $att->change_sessions_duration($sessionsids, $duration); + $att->update_sessions_duration($sessionsids, $duration); redirect($att->url_manage(), get_string('sessionupdated','attforblock')); } @@ -160,4 +155,80 @@ $mform->display(); echo $OUTPUT->footer(); +function construct_sessions_data_for_add($formdata) { + global $CFG; + + $duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS; + $now = time(); + + $sessions = array(); + if (isset($formdata->addmultiply)) { + $startdate = $formdata->sessiondate; + $starttime = $startdate - usergetmidnight($startdate); + $enddate = $formdata->sessionenddate + DAYSECS; // because enddate in 0:0am + + if ($enddate < $startdate) return NULL; + + $days = (int)ceil(($enddate - $startdate) / DAYSECS); + + // Getting first day of week + $sdate = $startdate; + $dinfo = usergetdate($sdate); + if ($CFG->calendar_startwday === '0') { //week start from sunday + $startweek = $startdate - $dinfo['wday'] * DAYSECS; //call new variable + } else { + $wday = $dinfo['wday'] === 0 ? 7 : $dinfo['wday']; + $startweek = $startdate - ($wday-1) * DAYSECS; + } + + $wdaydesc = array(0=>'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'); + + while ($sdate < $enddate) { + if($sdate < $startweek + WEEKSECS) { + $dinfo = usergetdate($sdate); + if(key_exists($wdaydesc[$dinfo['wday']], $formdata->sdays)) { + $sess->sessdate = usergetmidnight($sdate) + $starttime; + $sess->duration = $duration; + $sess->descriptionitemid = $formdata->sdescription['itemid']; + $sess->description = $formdata->sdescription['text']; + $sess->descriptionformat = $formdata->sdescription['format']; + $sess->timemodified = $now; + + fill_groupid($formdata, &$sessions, $sess); + } + $sdate += DAYSECS; + } else { + $startweek += WEEKSECS * $formdata->period; + $sdate = $startweek; + } + } + } else { + $sess->sessdate = $formdata->sessiondate; + $sess->duration = $duration; + $sess->descriptionitemid = $formdata->sdescription['itemid']; + $sess->description = $formdata->sdescription['text']; + $sess->descriptionformat = $formdata->sdescription['format']; + $sess->timemodified = $now; + + fill_groupid($formdata, &$sessions, $sess); + } + + return $sessions; +} + +function fill_groupid($formdata, $sessions, $sess) { + if ($formdata->sessiontype == attforblock::SESSION_COMMON) { + $sess = clone $sess; + $sess->groupid = 0; + $sessions[] = $sess; + } + else { + foreach ($formdata->groups as $groupid) { + $sess = clone $sess; + $sess->groupid = $groupid; + $sessions[] = $sess; + } + } +} + ?>