Browse Source

Finished implementation of adding new sessions

MOODLE_23_STABLE
Artem Andreev 14 years ago
parent
commit
1debc80e2d
  1. 44
      locallib.php
  2. 89
      sessions.php

44
locallib.php

@ -651,34 +651,22 @@ class attforblock {
return $this->currentgroup; return $this->currentgroup;
} }
public function add_session_from_form_data($formdata) { public function add_sessions($sessions) {
global $DB; global $DB;
$duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS; $sessionsids = array();
$rec->courseid = $this->course->id; foreach ($sessions as $sess) {
$rec->attendanceid = $this->id; $sess->attendanceid = $this->id;
$rec->sessdate = $formdata->sessiondate;
$rec->duration = $duration; $sid = $DB->insert_record('attendance_sessions', $sess);
$rec->description = $formdata->sdescription['text']; $description = file_save_draft_area_files($sess->descriptionitemid,
$rec->descriptionformat = $formdata->sdescription['format']; $this->context->id, 'mod_attforblock', 'session', $sid,
$rec->timemodified = time(); array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0),
$sess->description);
if ($formdata->sessiontype == self::SESSION_COMMON) { $DB->set_field('attendance_sessions', 'description', $description, array('id' => $sid));
$rec->id = $DB->insert_record('attendance_sessions', $rec);
$description = file_save_draft_area_files($formdata->sdescription['itemid'], $sessionsids[] = $sid;
$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));
}
} }
// TODO: log // TODO: log
//add_to_log($course->id, 'attendance', 'one session added', 'mod/attforblock/manage.php?id='.$id, $user->lastname.' '.$user->firstname); //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) { foreach($formdata as $key => $value) {
if(substr($key, 0, 4) == 'user') { if(substr($key, 0, 4) == 'user') {
$sid = substr($key, 4); $sid = substr($key, 4);
$sesslog[$sid] = new Object(); $sesslog[$sid] = new stdClass();
$sesslog[$sid]->studentid = $sid; $sesslog[$sid]->studentid = $sid;
$sesslog[$sid]->statusid = $value; $sesslog[$sid]->statusid = $value;
$sesslog[$sid]->statusset = $statuses; $sesslog[$sid]->statusset = $statuses;
@ -979,7 +967,7 @@ class attforblock {
// TODO: log // TODO: log
} }
public function change_sessions_duration($sessionsids, $duration) { public function update_sessions_duration($sessionsids, $duration) {
global $DB; global $DB;
$now = time(); $now = time();

89
sessions.php

@ -35,14 +35,9 @@ switch ($att->pageparams->action) {
$mform = new mod_attforblock_add_form($url, $formparams); $mform = new mod_attforblock_add_form($url, $formparams);
if ($mform->is_submitted()) { if ($mform->is_submitted()) {
$formdata = $mform->get_data(); $sessions = construct_sessions_data_for_add($mform->get_data());
if (isset($formdata->addmultiply)) { $att->add_sessions($sessions);
notice(get_string('sessionsgenerated','attforblock'), $url); redirect($url, get_string('sessionsgenerated','attforblock'));
}
else {
$att->add_session_from_form_data($formdata);
redirect($url, get_string('sessionadded','attforblock'));
}
} }
break; break;
case att_sessions_page_params::ACTION_UPDATE: case att_sessions_page_params::ACTION_UPDATE:
@ -140,7 +135,7 @@ switch ($att->pageparams->action) {
$sessionsids = explode('_', $fromform->ids); $sessionsids = explode('_', $fromform->ids);
$duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS; $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')); redirect($att->url_manage(), get_string('sessionupdated','attforblock'));
} }
@ -160,4 +155,80 @@ $mform->display();
echo $OUTPUT->footer(); 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;
}
}
}
?> ?>

Loading…
Cancel
Save