Browse Source

Implemented ACTION_DELETE, ACTION_DELETE_SELECTED, ACTION_CHANGE_DURATION for sessions.php

MOODLE_23_STABLE
Artem Andreev 14 years ago
parent
commit
88805f0aef
  1. 2
      duration_form.php
  2. 2
      lang/en/attforblock.php
  3. 1
      lang/ru/attforblock.php
  4. 93
      locallib.php
  5. 13
      renderer.php
  6. 87
      sessions.php

2
duration_form.php

@ -29,7 +29,7 @@ class mod_attforblock_duration_form extends moodleform {
$mform->addElement('hidden', 'ids', $ids); $mform->addElement('hidden', 'ids', $ids);
$mform->addElement('hidden', 'id', $cm->id); $mform->addElement('hidden', 'id', $cm->id);
$mform->addElement('hidden', 'action', 'changeduration'); $mform->addElement('hidden', 'action', att_sessions_page_params::ACTION_CHANGE_DURATION);
$mform->setDefaults(array('durtime' => array('hours'=>0, 'minutes'=>0))); $mform->setDefaults(array('durtime' => array('hours'=>0, 'minutes'=>0)));

2
lang/en/attforblock.php

@ -110,6 +110,7 @@ $string['noofdayslate'] = 'No of days late';
$string['noofdayspresent'] = 'No of days present'; $string['noofdayspresent'] = 'No of days present';
$string['nosessiondayselected'] = 'No Session day selected'; $string['nosessiondayselected'] = 'No Session day selected';
$string['nosessionexists'] = 'No Session exists for this course'; $string['nosessionexists'] = 'No Session exists for this course';
$string['nosessionsselected'] = 'No sessions selected';
$string['notfound'] = 'Attendance activity not found in this course!'; $string['notfound'] = 'Attendance activity not found in this course!';
$string['olddate'] = 'Old date'; $string['olddate'] = 'Old date';
$string['period'] = 'Frequency'; $string['period'] = 'Frequency';
@ -121,6 +122,7 @@ $string['resetdescription'] = 'Remember that deleting attendance data will erase
$string['resetstatuses'] = 'Reset statuses to default'; $string['resetstatuses'] = 'Reset statuses to default';
$string['restoredefaults'] = 'Restore defaults'; $string['restoredefaults'] = 'Restore defaults';
$string['save'] = 'Save attendance'; $string['save'] = 'Save attendance';
$string['session'] = 'Session';
$string['session_help'] = 'Session'; $string['session_help'] = 'Session';
$string['sessionadded'] = 'Session successfully added'; $string['sessionadded'] = 'Session successfully added';
$string['sessionalreadyexists'] = 'Session already exists for this date'; $string['sessionalreadyexists'] = 'Session already exists for this date';

1
lang/ru/attforblock.php

@ -96,6 +96,7 @@ $string['noofdayslate'] = 'Опозданий';
$string['noofdayspresent'] = 'Вы присутствовали'; $string['noofdayspresent'] = 'Вы присутствовали';
$string['nosessiondayselected'] = 'Не выбран день занятия'; $string['nosessiondayselected'] = 'Не выбран день занятия';
$string['nosessionexists'] = 'В данном курсе отсутствуют занятия. Сначала добавьте их'; $string['nosessionexists'] = 'В данном курсе отсутствуют занятия. Сначала добавьте их';
$string['nosessionsselected'] = 'Не выбрано ни одного занятия';
$string['notfound'] = 'Элемент курса - \"Посещаемость\" не найден в данном курсе!'; $string['notfound'] = 'Элемент курса - \"Посещаемость\" не найден в данном курсе!';
$string['olddate'] = 'Старая дата'; $string['olddate'] = 'Старая дата';
$string['period'] = 'Периодичность'; $string['period'] = 'Периодичность';

93
locallib.php

@ -329,9 +329,14 @@ class attforblock {
private $currentgroup; private $currentgroup;
private $sessioninfo;
private $statuses; private $statuses;
// Cache
// array by sessionid
private $sessioninfo = array();
// arrays by userid // arrays by userid
private $usertakensesscount = array(); private $usertakensesscount = array();
private $userstatusesstat = array(); private $userstatusesstat = array();
@ -480,7 +485,13 @@ class attforblock {
'cgroup' => $this->get_current_group()); 'cgroup' => $this->get_current_group());
$sessions = $DB->get_records_select('attendance_sessions', $where, $params, 'sessdate asc'); $sessions = $DB->get_records_select('attendance_sessions', $where, $params, 'sessdate asc');
foreach ($sessions as $sess) { foreach ($sessions as $sess) {
$sess->description = file_rewrite_pluginfile_urls($sess->description, 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); if (empty($sess->description)) {
$sess->description = get_string('nodescription', 'attforblock');
}
else {
$sess->description = file_rewrite_pluginfile_urls($sess->description,
'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id);
}
} }
return $sessions; return $sessions;
@ -814,12 +825,36 @@ class attforblock {
public function get_session_info($sessionid) { public function get_session_info($sessionid) {
global $DB; global $DB;
if (!isset($this->sessioninfo)) if (!array_key_exists($sessionid, $this->sessioninfo))
$this->sessioninfo = $DB->get_record('attendance_sessions', array('id' => $sessionid)); $this->sessioninfo[$sessionid] = $DB->get_record('attendance_sessions', array('id' => $sessionid));
$this->sessioninfo->description = file_rewrite_pluginfile_urls($this->sessioninfo->description, if (empty($this->sessioninfo[$sessionid]->description)) {
'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $this->sessioninfo->id); $this->sessioninfo[$sessionid]->description = get_string('nodescription', 'attforblock');
}
else {
$this->sessioninfo[$sessionid]->description = file_rewrite_pluginfile_urls($this->sessioninfo[$sessionid]->description,
'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $this->sessioninfo[$sessionid]->id);
}
return $this->sessioninfo[$sessionid];
}
return $this->sessioninfo; public function get_sessions_info($sessionids) {
global $DB;
list($sql, $params) = $DB->get_in_or_equal($sessionids);
$sessions = $DB->get_records_select('attendance_sessions', "id $sql", $params, 'sessdate asc');
foreach ($sessions as $sess) {
if (empty($sess->description)) {
$sess->description = get_string('nodescription', 'attforblock');
}
else {
$sess->description = file_rewrite_pluginfile_urls($sess->description,
'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id);
}
}
return $sessions;
} }
public function get_session_log($sessionid) { public function get_session_log($sessionid) {
@ -890,7 +925,7 @@ class attforblock {
foreach ($userids as $userid) { foreach ($userids as $userid) {
$grades[$userid]->userid = $userid; $grades[$userid]->userid = $userid;
$grades[$userid]->rawgrade = $this->get_user_grade($userid) / $this->get_user_max_grade($userid) * 100; $grades[$userid]->rawgrade = calc_user_grade_percent($this->get_user_grade($userid), $this->get_user_max_grade($userid));
} }
return grade_update('mod/attforblock', $this->course->id, 'mod', 'attforblock', return grade_update('mod/attforblock', $this->course->id, 'mod', 'attforblock',
@ -922,11 +957,40 @@ class attforblock {
'edate' => $this->pageparams->enddate); 'edate' => $this->pageparams->enddate);
$sessions = $DB->get_records_sql($sql, $params); $sessions = $DB->get_records_sql($sql, $params);
foreach ($sessions as $sess) { foreach ($sessions as $sess) {
$sess->description = file_rewrite_pluginfile_urls($sess->description, 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); if (empty($sess->description)) {
$sess->description = get_string('nodescription', 'attforblock');
}
else {
$sess->description = file_rewrite_pluginfile_urls($sess->description,
'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id);
}
} }
return $sessions; return $sessions;
} }
public function delete_sessions($sessionsids) {
global $DB;
list($sql, $params) = $DB->get_in_or_equal($sessionsids);
$DB->delete_records_select('attendance_log', "sessionid $sql", $params);
$DB->delete_records_list('attendance_sessions', 'id', $sessionsids);
// TODO: log
}
public function change_sessions_duration($sessionsids, $duration) {
global $DB;
$now = time();
$sessions = $DB->get_records_list('attendance_sessions', 'id', $sessionsids);
foreach ($sessions as $sess) {
$sess->duration = $duration;
$sess->timemodified = $now;
$DB->update_record('attendance_sessions', $sess);
}
// TODO: log
}
} }
@ -1013,19 +1077,26 @@ function get_user_courses_attendances($userid) {
return $DB->get_records_sql($sql, $params); return $DB->get_records_sql($sql, $params);
} }
function calc_user_grade_percent($grade, $maxgrade) {
if ($maxgrade == 0)
return 0;
else
return $grade / $maxgrade * 100;
}
function update_all_users_grades($attid, $course, $context) { function update_all_users_grades($attid, $course, $context) {
global $COURSE; global $COURSE;
$grades = array(); $grades = array();
$userids = get_enrolled_users($context, 'mod/attforblock:canbelisted', 0, 'u.id'); $userids = array_keys(get_enrolled_users($context, 'mod/attforblock:canbelisted', 0, 'u.id'));
$statuses = get_statuses($attid); $statuses = get_statuses($attid);
foreach ($userids as $userid) { foreach ($userids as $userid) {
$grades[$userid]->userid = $userid; $grades[$userid]->userid = $userid;
$userstatusesstat = get_user_statuses_stat($attid, $course->startdate, $userid); $userstatusesstat = get_user_statuses_stat($attid, $course->startdate, $userid);
$usertakensesscount = get_user_taken_sessions_count($attid, $course->startdate, $userid); $usertakensesscount = get_user_taken_sessions_count($attid, $course->startdate, $userid);
$grades[$userid]->rawgrade = get_user_grade($userstatusesstat, $statuses) / get_user_max_grade($usertakensesscount, $statuses) * 100; $grades[$userid]->rawgrade = calc_user_grade_percent(get_user_grade($userstatusesstat, $statuses), get_user_max_grade($usertakensesscount, $statuses));
} }
return grade_update('mod/attforblock', $course->id, 'mod', 'attforblock', return grade_update('mod/attforblock', $course->id, 'mod', 'attforblock',

13
renderer.php

@ -185,9 +185,9 @@ class mod_attforblock_renderer extends plugin_renderer_base {
$table->data[$sess->id][] = $sess->groupid ? $sessdata->groups[$sess->groupid]->name : get_string('commonsession', 'attforblock'); $table->data[$sess->id][] = $sess->groupid ? $sessdata->groups[$sess->groupid]->name : get_string('commonsession', 'attforblock');
$table->data[$sess->id][] = $dta['date']; $table->data[$sess->id][] = $dta['date'];
$table->data[$sess->id][] = $dta['time']; $table->data[$sess->id][] = $dta['time'];
$table->data[$sess->id][] = empty($sess->description) ? get_string('nodescription', 'attforblock') : $sess->description; $table->data[$sess->id][] = $sess->description;
$table->data[$sess->id][] = $dta['actions']; $table->data[$sess->id][] = $dta['actions'];
$table->data[$sess->id][] = html_writer::checkbox('sessid', $sess->id, false); $table->data[$sess->id][] = html_writer::checkbox('sessid[]', $sess->id, false);
} }
return html_writer::table($table); return html_writer::table($table);
@ -240,8 +240,9 @@ class mod_attforblock_renderer extends plugin_renderer_base {
get_string('hiddensessions', 'attforblock').': '.$sessdata->hiddensessionscount); get_string('hiddensessions', 'attforblock').': '.$sessdata->hiddensessionscount);
if ($sessdata->perm->can_manage()) { if ($sessdata->perm->can_manage()) {
$options = array('deleteselected' => get_string('delete'), $options = array(
'changeduration' => get_string('changeduration', 'attforblock')); att_sessions_page_params::ACTION_DELETE_SELECTED => get_string('delete'),
att_sessions_page_params::ACTION_CHANGE_DURATION => get_string('changeduration', 'attforblock'));
$controls = html_writer::select($options, 'action'); $controls = html_writer::select($options, 'action');
$attributes = array( $attributes = array(
'type' => 'submit', 'type' => 'submit',
@ -292,7 +293,7 @@ class mod_attforblock_renderer extends plugin_renderer_base {
$sessinfo = $date.' '.$time; $sessinfo = $date.' '.$time;
$sessinfo .= html_writer::empty_tag('br'); $sessinfo .= html_writer::empty_tag('br');
$sessinfo .= html_writer::empty_tag('br'); $sessinfo .= html_writer::empty_tag('br');
$sessinfo .= empty($sess->description) ? get_string('nodescription', 'attforblock') : $sess->description; $sessinfo .= $sess->description;
return $sessinfo; return $sessinfo;
} }
@ -608,7 +609,7 @@ class mod_attforblock_renderer extends plugin_renderer_base {
$row->cells[] = $i; $row->cells[] = $i;
$row->cells[] = userdate($sess->sessdate, get_string('strftimedmyw', 'attforblock')); $row->cells[] = userdate($sess->sessdate, get_string('strftimedmyw', 'attforblock'));
$row->cells[] = $this->construct_time($sess->sessdate, $sess->duration); $row->cells[] = $this->construct_time($sess->sessdate, $sess->duration);
$row->cells[] = empty($sess->description) ? get_string('nodescription', 'attforblock') : $sess->description; $row->cells[] = $sess->description;
if (isset($sess->statusid)) { if (isset($sess->statusid)) {
$row->cells[] = $userdata->statuses[$sess->statusid]->description; $row->cells[] = $userdata->statuses[$sess->statusid]->description;
$row->cells[] = $sess->remarks; $row->cells[] = $sess->remarks;

87
sessions.php

@ -44,7 +44,6 @@ switch ($att->pageparams->action) {
redirect($url, get_string('sessionadded','attforblock')); redirect($url, get_string('sessionadded','attforblock'));
} }
} }
break; break;
case att_sessions_page_params::ACTION_UPDATE: case att_sessions_page_params::ACTION_UPDATE:
$sessionid = required_param('sessionid', PARAM_INT); $sessionid = required_param('sessionid', PARAM_INT);
@ -63,6 +62,92 @@ switch ($att->pageparams->action) {
redirect($att->url_manage(), get_string('sessionupdated','attforblock')); redirect($att->url_manage(), get_string('sessionupdated','attforblock'));
} }
break; break;
case att_sessions_page_params::ACTION_DELETE:
$sessionid = required_param('sessionid', PARAM_INT);
$confirm = optional_param('confirm', NULL, PARAM_INT);
if (isset($confirm)) {
$att->delete_sessions(array($sessionid));
update_all_users_grades($att->id, $att->course, $att->context);
redirect($att->url_manage(), get_string('sessiondeleted','attforblock'));
}
$sessinfo = $att->get_session_info($sessionid);
$message = get_string('deletecheckfull', '', get_string('session', 'attforblock'));
$message .= str_repeat(html_writer::empty_tag('br'), 2);
$message .= userdate($sessinfo->sessdate, get_string('strftimedmyhm', 'attforblock'));
$message .= html_writer::empty_tag('br');
$message .= $sessinfo->description;
$params = array('action' => $att->pageparams->action, 'sessionid' => $sessionid, 'confirm' => 1);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname);
echo $OUTPUT->confirm($message, $att->url_sessions($params), $att->url_manage());
echo $OUTPUT->footer();
exit;
case att_sessions_page_params::ACTION_DELETE_SELECTED:
$confirm = optional_param('confirm', NULL, PARAM_INT);
if (isset($confirm)) {
$sessionsids = required_param('sessionsids', PARAM_ALPHANUMEXT);
$sessionsids = explode('_', $sessionsids);
$att->delete_sessions($sessionsids);
update_all_users_grades($att->id, $att->course, $att->context);
redirect($att->url_manage(), get_string('sessiondeleted','attforblock'));
}
$fromform = data_submitted();
// nothing selected
if (!isset($fromform->sessid))
print_error ('nosessionsselected','attforblock', $att->url_manage());
$sessionsinfo = $att->get_sessions_info($fromform->sessid);
$message = get_string('deletecheckfull', '', get_string('session', 'attforblock'));
$message .= html_writer::empty_tag('br');
foreach ($sessionsinfo as $sessinfo) {
$message .= html_writer::empty_tag('br');
$message .= userdate($sessinfo->sessdate, get_string('strftimedmyhm', 'attforblock'));
$message .= html_writer::empty_tag('br');
$message .= $sessinfo->description;
}
$sessionsids = implode('_', $fromform->sessid);
$params = array('action' => $att->pageparams->action, 'sessionsids' => $sessionsids, 'confirm' => 1);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname);
echo $OUTPUT->confirm($message, $att->url_sessions($params), $att->url_manage());
echo $OUTPUT->footer();
exit;
case att_sessions_page_params::ACTION_CHANGE_DURATION:
$fromform = data_submitted();
$slist = isset($fromform->sessid) ? implode('_', $fromform->sessid) : '';
$url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_CHANGE_DURATION));
$formparams['ids'] = $slist;
$mform = new mod_attforblock_duration_form($url, $formparams);
if ($mform->is_cancelled()) {
redirect($att->url_manage());
}
if ($mform->is_submitted()) {
$formdata = $mform->get_data();
$sessionsids = explode('_', $fromform->ids);
$duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS;
$att->change_sessions_duration($sessionsids, $duration);
redirect($att->url_manage(), get_string('sessionupdated','attforblock'));
}
if ($slist === '')
print_error ('nosessionsselected','attforblock', $att->url_manage());
break;
} }
$output = $PAGE->get_renderer('mod_attforblock'); $output = $PAGE->get_renderer('mod_attforblock');

Loading…
Cancel
Save